@Override public void prePersist(final Object entity, final DBObject dbObj, final Mapper mapper) { MappedClass mclass = mapper.getMappedClass(entity); Field id = mclass.getIdField(); if (id != null && id.getAnnotation(GeneratedValue.class) != null) { try { id.setAccessible(true); final String collName = gen.value(mclass.getClazz()); final Query<StoredId> q = db.find(StoredId.class, "_id", collName); final UpdateOperations<StoredId> uOps = db.createUpdateOperations(StoredId.class) .inc("value"); StoredId newId = db.findAndModify(q, uOps); if (newId == null) { newId = new StoredId(collName); db.save(newId); } id.set(entity, newId.value); } catch (Exception ex) { throw new IllegalStateException("Can't generate ID on " + mclass, ex); } } }
@Override public void addTag(@Nonnull OWLEntity entity, @Nonnull TagId tagId) { try { writeLock.lock(); Query<EntityTags> query = tagWithProjectIdAndEntity(projectId, entity); UpdateOperations<EntityTags> updateOps = datastore.createUpdateOperations(EntityTags.class); updateOps.addToSet(TAGS, tagId); datastore.update(query, updateOps); } finally { writeLock.unlock(); } }
@Override public void removeTag(@Nonnull OWLEntity entity, @Nonnull TagId tagId) { try { writeLock.lock(); Query<EntityTags> query = tagWithProjectIdAndEntity(projectId, entity); UpdateOperations<EntityTags> updateOps = datastore.createUpdateOperations(EntityTags.class); updateOps.removeAll(TAGS, tagId); datastore.update(query, updateOps); } finally { writeLock.unlock(); } }
public boolean checkIfExistsAndInsertOtherwise(short id){ Datastore datastore = getDatastore(); Query<OrganizationId> updateQuery = datastore.createQuery(OrganizationId.class).field("identificationNumber").equal(id); //Bogus operation, just set id to itself UpdateOperations<OrganizationId> ops = datastore.createUpdateOperations(OrganizationId.class).set("identificationNumber", id); UpdateResults<OrganizationId> result = datastore.updateFirst(updateQuery, ops, true,WriteConcern.SAFE); return result.getInsertedCount() == 0; }
@Override public void setApiKeys(@Nonnull UserId userId, List<ApiKeyRecord> records) { Set<ApiKeyId> ids = new HashSet<>(); List<ApiKeyRecord> nonDuplicates = records.stream() .filter(r -> ids.add(r.getApiKeyId())) .collect(Collectors.toList()); UpdateOperations<UserApiKeys> ops = datastore.createUpdateOperations(UserApiKeys.class) .set(API_KEYS, nonDuplicates); Query<UserApiKeys> query = datastore.createQuery(UserApiKeys.class) .field(USER_ID).equal(userId); datastore.update(query, ops, new UpdateOptions().upsert(true)); }
@Override public void removeTag(@Nonnull TagId tagId) { try { writeLock.lock(); Query<EntityTags> query = datastore.createQuery(EntityTags.class) .field(PROJECT_ID).equal(projectId); UpdateOperations<EntityTags> updateOps = datastore.createUpdateOperations(EntityTags.class); updateOps.removeAll(TAGS, tagId); datastore.update(query, updateOps); } finally { writeLock.unlock(); } }
public void setLastLogin(@Nonnull UserId userId, long lastLogin) { if(userId.isGuest()) { return; } getByUserId(userId); Query<UserActivityRecord> query = queryByUserId(userId); UpdateOperations<UserActivityRecord> operations = datastore.createUpdateOperations(UserActivityRecord.class) .set(LAST_LOGIN, new Date(lastLogin)); datastore.update(query, operations); }
public void setLastLogout(@Nonnull UserId userId, long lastLogout) { if(userId.isGuest()) { return; } getByUserId(userId); Query<UserActivityRecord> query = queryByUserId(userId); UpdateOperations<UserActivityRecord> operations = datastore.createUpdateOperations(UserActivityRecord.class) .set(LAST_LOGOUT, new Date(lastLogout)); datastore.update(query, operations); }
/** * Returns the next number in the sequence specified . If sequence does not exists * then it will be created * @param name the sequence name * @return the next number in the sequence */ public long next(String name) { UpdateOperations<Sequence> op = ds.createUpdateOperations(Sequence.class); op.inc("number"); Query<Sequence> q = ds.createQuery(Sequence.class).field("_id").equal(name); Sequence seq = ds.findAndModify(q, op, false, true); return seq.number; }
public static Seq next(String name) { Datastore ds = MorphiaPlugin.ds(); Query<Seq> q = ds.find(Seq.class, "_id", name); UpdateOperations<Seq> o = ds.createUpdateOperations(Seq.class).inc("value"); Seq newId = ds.findAndModify(q, o); if (null == newId) { newId = new Seq(name); ds.save(newId); } return newId; }
public void replaceEntity(ProjectId projectId, OWLEntity entity, OWLEntity withEntity) { Query<EntityDiscussionThread> query = datastore.find(EntityDiscussionThread.class) .field(PROJECT_ID).equal(projectId) .field(ENTITY).equal(entity); UpdateOperations<EntityDiscussionThread> updateOperations = datastore.createUpdateOperations(EntityDiscussionThread.class); updateOperations.set("entity", withEntity); datastore.update(query, updateOperations); }
public void updatePush(String jobId, String var, String value) { Query<LGJob> query = createQuery().field("_id").equal(jobId); UpdateOperations<LGJob> ops = getDatastore().createUpdateOperations(LGJob.class).add(var, value); getDatastore().update(query, ops); }
public void update(String taskId, String var, String value) { Query<LGTask> query = createQuery().field("_id").equal(taskId); UpdateOperations<LGTask> ops = getDatastore().createUpdateOperations(LGTask.class).set(var, value); getDatastore().update(query, ops); }
public void update(String taskId, String var, long value) { Query<LGTask> query = createQuery().field("_id").equal(taskId); UpdateOperations<LGTask> ops = getDatastore().createUpdateOperations(LGTask.class).set(var, value); getDatastore().update(query, ops); }
public void update(String jobId, String var, String value) { Query<LGJob> query = createQuery().field("_id").equal(jobId); UpdateOperations<LGJob> ops = getDatastore().createUpdateOperations(LGJob.class).set(var, value); getDatastore().update(query, ops); }
public void updateInt(String jobId, String var, int value) { Query<LGJob> query = createQuery().field("_id").equal(jobId); UpdateOperations<LGJob> ops = getDatastore().createUpdateOperations(LGJob.class).set(var, value); getDatastore().update(query, ops); }
public void update(String taskId, String var, boolean value) { Query<LGTask> query = createQuery().field("_id").equal(taskId); UpdateOperations<LGTask> ops = getDatastore().createUpdateOperations(LGTask.class).set(var, value); getDatastore().update(query, ops); }
public void updateInt(String taskId, String var, int value) { Query<LGTask> query = createQuery().field("_id").equal(taskId); UpdateOperations<LGTask> ops = getDatastore().createUpdateOperations(LGTask.class).set(var, value); getDatastore().update(query.disableValidation(), ops); }
public int assignNextBuildNumber(final DynamicProject project) { final Datastore datastore = getDatastore(); BuildNumberCounter seq = datastore.findAndModify( datastore.find(BuildNumberCounter.class, "key = ", project.getFullName()), // query datastore.createUpdateOperations(BuildNumberCounter.class).inc("counter") // update ); if (seq == null) { seq = new BuildNumberCounter(project.getFullName(), 1); datastore.save(seq); } return seq.getCounter(); }
public void store(ProjectId projectId, CollectionId collectionId, FormId formId, OWLEntity entity, FormData formData) { Query<FormDataRecord> query = datastore.createQuery(FormDataRecord.class) .field("projectId").equal(projectId) .field("collectionId").equal(collectionId) .field("formId").equal(formId) .field("subjectId").equal(entity.toString()); UpdateOperations<FormDataRecord> update = datastore.createUpdateOperations(FormDataRecord.class); update.set("data", formData); datastore.update(query, update, new UpdateOptions().upsert(true)); }