@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); } } }
private <T> void updateForVersioning(final Query<T> query, final UpdateOperations<T> operations) { final MappedClass mc = mapper.getMappedClass(query.getEntityClass()); if (!mc.getFieldsAnnotatedWith(Version.class).isEmpty()) { operations.inc(mc.getMappedVersionField().getNameToStore()); } }
/** * 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; }
@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); } } }
public long nextRevision() { return (Long) ds .findAndModify( ds.createQuery(MetadataEntity.class).field("name").equal(NAME), ds.createUpdateOperations(MetadataEntity.class).inc("value") ).value; }
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 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(); }
/** * 增加属性的值 * * @param num * 增加量 * @param fieldName * 属性名称 * @return */ public static HallInfo incFieldNum(int num, String fieldName) { Query<HallInfo> query = hallInfoDao.createQuery(); UpdateOperations<HallInfo> updateOperations = hallInfoDao.createUpdateOperations().inc(fieldName, num); HallInfo hallInfo = hallInfoDao.getDs().findAndModify(query, updateOperations); if (hallInfo == null) { hallInfo = new HallInfo(); hallInfoDao.save(hallInfo); hallInfo = hallInfoDao.getDs().findAndModify(query, updateOperations); } return hallInfo; }
@Override public <T> UpdateResults update(final Query<T> query, final UpdateOperations<T> operations, final UpdateOptions options) { DBCollection dbColl = query.getCollection(); // TODO remove this after testing. if (dbColl == null) { dbColl = getCollection(query.getEntityClass()); } final MappedClass mc = getMapper().getMappedClass(query.getEntityClass()); final List<MappedField> fields = mc.getFieldsAnnotatedWith(Version.class); DBObject queryObject = query.getQueryObject(); if (operations.isIsolated()) { queryObject.put("$isolated", true); } if (!fields.isEmpty()) { operations.inc(fields.get(0).getNameToStore(), 1); } final BasicDBObject update = (BasicDBObject) ((UpdateOpsImpl) operations).getOps(); if (LOG.isTraceEnabled()) { LOG.trace(format("Executing update(%s) for query: %s, ops: %s, multi: %s, upsert: %s", dbColl.getName(), queryObject, update, options.isMulti(), options.isUpsert())); } return new UpdateResults(dbColl.update(queryObject, update, enforceWriteConcern(options, query.getEntityClass()) .getOptions())); }