List of usage examples for org.springframework.data.mongodb.core DbCallback DbCallback
DbCallback
From source file:org.grails.datastore.mapping.mongo.engine.MongoEntityPersister.java
@Override protected DBObject retrieveEntry(final PersistentEntity persistentEntity, String family, final Serializable key) { return mongoTemplate.execute(new DbCallback<DBObject>() { public DBObject doInDB(DB con) throws MongoException, DataAccessException { DBCollection dbCollection = con.getCollection(getCollectionName(persistentEntity)); return dbCollection.findOne(createDBObjectWithKey(key)); }//w w w . ja v a 2 s . co m }); }
From source file:org.grails.datastore.mapping.mongo.engine.MongoEntityPersister.java
@Override protected Object storeEntry(final PersistentEntity persistentEntity, final EntityAccess entityAccess, final Object storeId, final DBObject nativeEntry) { return mongoTemplate.execute(new DbCallback<Object>() { public Object doInDB(DB con) throws MongoException, DataAccessException { nativeEntry.put(MONGO_ID_FIELD, storeId); return nativeEntry.get(MONGO_ID_FIELD); }//from w w w. jav a 2 s .c om }); }
From source file:org.grails.datastore.mapping.mongo.engine.MongoEntityPersister.java
@Override public void updateEntry(final PersistentEntity persistentEntity, final EntityAccess ea, final Object key, final DBObject entry) { mongoTemplate.execute(new DbCallback<Object>() { public Object doInDB(DB con) throws MongoException, DataAccessException { @SuppressWarnings("hiding") String collectionName = getCollectionName(persistentEntity, entry); DBCollection dbCollection = con.getCollection(collectionName); DBObject dbo = createDBObjectWithKey(key); if (isVersioned(ea)) { // TODO this should be done with a CAS approach if possible DBObject previous = dbCollection.findOne(dbo); checkVersion(ea, previous, persistentEntity, key); }//from ww w . j a v a2 s . com MongoSession mongoSession = (MongoSession) session; dbCollection.update(dbo, entry, false, false, mongoSession.getWriteConcern()); return null; } }); }
From source file:org.grails.datastore.mapping.mongo.engine.MongoEntityPersister.java
@Override protected void deleteEntries(String family, final List<Object> keys) { mongoTemplate.execute(new DbCallback<Object>() { public Object doInDB(DB con) throws MongoException, DataAccessException { @SuppressWarnings("hiding") String collectionName = getCollectionName(getPersistentEntity()); DBCollection dbCollection = con.getCollection(collectionName); MongoSession mongoSession = (MongoSession) getSession(); MongoQuery query = mongoSession.createQuery(getPersistentEntity().getJavaClass()); query.in(getPersistentEntity().getIdentity().getName(), keys); dbCollection.remove(query.getMongoQuery()); return null; }//from w w w .j av a2 s . c om }); }
From source file:org.grails.datastore.mapping.mongo.query.MongoQuery.java
@SuppressWarnings("hiding") @Override//w w w .ja v a 2 s . c o m protected List executeQuery(final PersistentEntity entity, final Junction criteria) { final MongoTemplate template = mongoSession.getMongoTemplate(entity); return template.execute(new DbCallback<List>() { @SuppressWarnings("unchecked") public List doInDB(DB db) throws MongoException, DataAccessException { final DBCollection collection = db.getCollection(mongoEntityPersister.getCollectionName(entity)); if (uniqueResult) { final DBObject dbObject; if (criteria.isEmpty()) { if (entity.isRoot()) { dbObject = collection.findOne(); } else { dbObject = collection.findOne(new BasicDBObject(MongoEntityPersister.MONGO_CLASS_FIELD, entity.getDiscriminator())); } } else { dbObject = collection.findOne(getMongoQuery()); } return wrapObjectResultInList(createObjectFromDBObject(dbObject)); } DBCursor cursor = null; DBObject query = createQueryObject(entity); final List<Projection> projectionList = projections().getProjectionList(); if (projectionList.isEmpty()) { cursor = executeQuery(entity, criteria, collection, query); return (List) new MongoResultList(cursor, mongoEntityPersister).clone(); } List projectedResults = new ArrayList(); for (Projection projection : projectionList) { if (projection instanceof CountProjection) { // For some reason the below doesn't return the expected result whilst executing the query and returning the cursor does //projectedResults.add(collection.getCount(query)); if (cursor == null) cursor = executeQuery(entity, criteria, collection, query); projectedResults.add(cursor.size()); } else if (projection instanceof MinProjection) { if (cursor == null) cursor = executeQuery(entity, criteria, collection, query); MinProjection mp = (MinProjection) projection; MongoResultList results = new MongoResultList(cursor, mongoEntityPersister); projectedResults.add(manualProjections.min((Collection) results.clone(), getPropertyName(entity, mp.getPropertyName()))); } else if (projection instanceof MaxProjection) { if (cursor == null) cursor = executeQuery(entity, criteria, collection, query); MaxProjection mp = (MaxProjection) projection; MongoResultList results = new MongoResultList(cursor, mongoEntityPersister); projectedResults.add(manualProjections.max((Collection) results.clone(), getPropertyName(entity, mp.getPropertyName()))); } else if (projection instanceof CountDistinctProjection) { if (cursor == null) cursor = executeQuery(entity, criteria, collection, query); CountDistinctProjection mp = (CountDistinctProjection) projection; MongoResultList results = new MongoResultList(cursor, mongoEntityPersister); projectedResults.add(manualProjections.countDistinct((Collection) results.clone(), getPropertyName(entity, mp.getPropertyName()))); } else if ((projection instanceof PropertyProjection) || (projection instanceof IdProjection)) { final PersistentProperty persistentProperty; final String propertyName; if (projection instanceof IdProjection) { persistentProperty = entity.getIdentity(); propertyName = MongoEntityPersister.MONGO_ID_FIELD; } else { PropertyProjection pp = (PropertyProjection) projection; persistentProperty = entity.getPropertyByName(pp.getPropertyName()); propertyName = getPropertyName(entity, persistentProperty.getName()); } if (persistentProperty != null) { populateMongoQuery(entity, query, criteria); List propertyResults = null; if (max > -1) { // if there is a limit then we have to do a manual projection since the MongoDB driver doesn't support limits and distinct together cursor = executeQueryAndApplyPagination(collection, query); propertyResults = manualProjections .property(new MongoResultList(cursor, mongoEntityPersister), propertyName); } else { propertyResults = collection.distinct(propertyName, query); } if (persistentProperty instanceof ToOne) { Association a = (Association) persistentProperty; propertyResults = session.retrieveAll(a.getAssociatedEntity().getJavaClass(), propertyResults); } if (projectedResults.size() == 0 && projectionList.size() == 1) { return propertyResults; } projectedResults.add(propertyResults); } else { throw new InvalidDataAccessResourceUsageException( "Cannot use [" + projection.getClass().getSimpleName() + "] projection on non-existent property: " + propertyName); } } } return projectedResults; } protected DBCursor executeQuery(final PersistentEntity entity, final Junction criteria, final DBCollection collection, DBObject query) { final DBCursor cursor; if (criteria.isEmpty()) { cursor = executeQueryAndApplyPagination(collection, query); } else { populateMongoQuery(entity, query, criteria); cursor = executeQueryAndApplyPagination(collection, query); } if (queryArguments != null) { if (queryArguments.containsKey(HINT_ARGUMENT)) { Object hint = queryArguments.get(HINT_ARGUMENT); if (hint instanceof Map) { cursor.hint(new BasicDBObject((Map) hint)); } else if (hint != null) { cursor.hint(hint.toString()); } } } return cursor; } protected DBCursor executeQueryAndApplyPagination(final DBCollection collection, DBObject query) { final DBCursor cursor; cursor = collection.find(query); if (offset > 0) { cursor.skip(offset); } if (max > -1) { cursor.limit(max); } if (!orderBy.isEmpty()) { DBObject orderObject = new BasicDBObject(); for (Order order : orderBy) { String property = order.getProperty(); property = getPropertyName(entity, property); orderObject.put(property, order.getDirection() == Order.Direction.DESC ? -1 : 1); } cursor.sort(orderObject); } return cursor; } }); }
From source file:org.springframework.data.mongodb.core.MongoTemplate.java
public CommandResult executeCommand(final DBObject command) { CommandResult result = execute(new DbCallback<CommandResult>() { public CommandResult doInDB(DB db) throws MongoException, DataAccessException { return db.command(command); }/* ww w .j a va 2 s. c om*/ }); logCommandExecutionError(command, result); return result; }
From source file:org.springframework.data.mongodb.core.MongoTemplate.java
public CommandResult executeCommand(final DBObject command, final int options) { CommandResult result = execute(new DbCallback<CommandResult>() { public CommandResult doInDB(DB db) throws MongoException, DataAccessException { return db.command(command, options); }//w w w . ja va 2s .co m }); logCommandExecutionError(command, result); return result; }
From source file:org.springframework.data.mongodb.core.MongoTemplate.java
public <T> T executeInSession(final DbCallback<T> action) { return execute(new DbCallback<T>() { public T doInDB(DB db) throws MongoException, DataAccessException { try { db.requestStart();/*from w w w . j a v a 2s . c o m*/ return action.doInDB(db); } finally { db.requestDone(); } } }); }
From source file:org.springframework.data.mongodb.core.MongoTemplate.java
public DBCollection getCollection(final String collectionName) { return execute(new DbCallback<DBCollection>() { public DBCollection doInDB(DB db) throws MongoException, DataAccessException { return db.getCollection(collectionName); }/*www . ja va2 s. c om*/ }); }
From source file:org.springframework.data.mongodb.core.MongoTemplate.java
public boolean collectionExists(final String collectionName) { return execute(new DbCallback<Boolean>() { public Boolean doInDB(DB db) throws MongoException, DataAccessException { return db.collectionExists(collectionName); }//from ww w .jav a 2 s.c o m }); }