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); }
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; } }