public MongoQuery inc(String key, Number value) { data.add(Updates.inc(key, value)); return this; }
public MongoQuery inc(String key, Number value) { data.add(Updates.inc(key, value)); return this; }
public long getNewAutoIncrementId(String field) { //, MongoDBCollection metaCollection Bson query = METADATA_QUERY; Document projection = new Document(field, true); Bson inc = Updates.inc(field, 1L); QueryOptions queryOptions = new QueryOptions("returnNew", true); QueryResult<Document> result = metaCollection.findAndUpdate(query, projection, null, inc, queryOptions); return result.getResult().get(0).getLong(field); }
@Deprecated static long getNewAutoIncrementId(String field, MongoDBCollection metaCollection) { Bson query = Filters.eq(PRIVATE_ID, MongoDBAdaptorFactory.METADATA_OBJECT_ID); Document projection = new Document(field, true); Bson inc = Updates.inc(field, 1); QueryOptions queryOptions = new QueryOptions("returnNew", true); QueryResult<Document> result = metaCollection.findAndUpdate(query, projection, null, inc, queryOptions); // return (int) Float.parseFloat(result.getResult().get(0).get(field).toString()); return result.getResult().get(0).getInteger(field); }
protected synchronized Long getNextSequenceId() { if (sequenceLeft == 0) { // allocate a new sequence block // the database contains the last value from the last block Bson filter = Filters.eq(MONGODB_ID, COUNTER_NAME_UUID); Bson update = Updates.inc(COUNTER_FIELD, Long.valueOf(sequenceBlockSize)); Document idCounter = countersColl.findOneAndUpdate(filter, update, new FindOneAndUpdateOptions().returnDocument(ReturnDocument.AFTER)); if (idCounter == null) { throw new NuxeoException("Repository id counter not initialized"); } sequenceLeft = sequenceBlockSize; sequenceLastValue = ((Long) idCounter.get(COUNTER_FIELD)).longValue() - sequenceBlockSize; } sequenceLeft--; sequenceLastValue++; return Long.valueOf(sequenceLastValue); }
/*** * This method is called every time a file has been inserted, modified or deleted to keep track of the current study size. * * @param studyId Study Identifier * @param size disk usage of a new created, updated or deleted file belonging to studyId. This argument * will be > 0 to increment the size field in the study collection or < 0 to decrement it. * @throws CatalogDBException An exception is launched when the update crashes. */ public void updateDiskUsage(long studyId, long size) throws CatalogDBException { Bson query = new Document(QueryParams.UID.key(), studyId); Bson update = Updates.inc(QueryParams.SIZE.key(), size); if (studyCollection.update(query, update, null).getNumTotalResults() == 0) { throw new CatalogDBException("CatalogMongoStudyDBAdaptor updateDiskUsage: Couldn't update the size field of" + " the study " + studyId); } } }
if (autoincrementId) { Document filter = MongoDBSerializationHelper.fieldMapToBson(MONGODB_ID, directoryName); Bson update = Updates.inc(MONGODB_SEQ, 1L); FindOneAndUpdateOptions options = new FindOneAndUpdateOptions().returnDocument(ReturnDocument.AFTER); Long longId = getCountersCollection().findOneAndUpdate(filter, update, options).getLong(MONGODB_SEQ);
@Override public long decrement(String aCounterName) { Document doc = collection .findOneAndUpdate( eq("_id", aCounterName), inc(DSL.VALUE, -1), new FindOneAndUpdateOptions() .returnDocument(AFTER) .projection(include(DSL.VALUE)) ); if (doc == null) { throw new IllegalArgumentException("Counter not found: " + aCounterName); } return doc.getLong(DSL.VALUE); }
private int generateId(String idType, boolean retry) throws StorageEngineException { String field = COUNTERS_FIELD + '.' + idType; Document projection = new Document(field, true); Bson inc = Updates.inc(field, 1); QueryOptions queryOptions = new QueryOptions("returnNew", true); QueryResult<Document> result = collection.findAndUpdate(QUERY, projection, null, inc, queryOptions); if (result.first() == null) { if (retry) { ensureProjectMetadataExists(); return generateId(idType, false); } else { throw new StorageEngineException("Error creating new ID. Project Metadata not found"); } } else { Document document = result.getResult().get(0); Document counters = document.get(COUNTERS_FIELD, Document.class); Integer id = counters.getInteger(idType); // System.out.println("New ID " + idType + " : " + id); return id; } }