@ -339,8 +339,8 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
@@ -339,8 +339,8 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
DBCursor cursor = collection . find ( mappedQuery , mappedFields ) ;
QueryCursorPreparer cursorPreparer = new QueryCursorPreparer ( query , entityType ) ;
ReadDbObjectCallback < T > readCallback = new ReadDbObjectCallback < T > ( mongoConverter , entityType , collection
. getName ( ) ) ;
ReadDbObjectCallback < T > readCallback = new ReadDbObjectCallback < T > ( mongoConverter , entityType ,
collection . getName ( ) ) ;
return new CloseableIterableCursorAdapter < T > ( cursorPreparer . prepare ( cursor ) , exceptionTranslator , readCallback ) ;
}
@ -373,8 +373,8 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
@@ -373,8 +373,8 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
* /
@Deprecated
public CommandResult executeCommand ( final DBObject command , final int options ) {
return executeCommand ( command , ( options & Bytes . QUERYOPTION_SLAVEOK ) ! = 0 ? ReadPreference . secondaryPreferred ( )
: ReadPreference . primary ( ) ) ;
return executeCommand ( command ,
( options & Bytes . QUERYOPTION_SLAVEOK ) ! = 0 ? ReadPreference . secondaryPreferred ( ) : ReadPreference . primary ( ) ) ;
}
/ *
@ -422,7 +422,8 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
@@ -422,7 +422,8 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
* @param preparer allows for customization of the { @link DBCursor } used when iterating over the result set , ( apply
* limits , skips and so on ) .
* /
protected void executeQuery ( Query query , String collectionName , DocumentCallbackHandler dch , CursorPreparer preparer ) {
protected void executeQuery ( Query query , String collectionName , DocumentCallbackHandler dch ,
CursorPreparer preparer ) {
Assert . notNull ( query ) ;
@ -546,13 +547,27 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
@@ -546,13 +547,27 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
}
public BulkOperations bulkOps ( BulkMode bulkMode , String collectionName ) {
return new DefaultBulkOperations ( this , bulkMode , collectionName , writeConcern ) ;
return bulkOps ( bulkMode , null , collectionName ) ;
}
public BulkOperations bulkOps ( BulkMode bulkMode , Class < ? > entityClass ) {
return new DefaultBulkOperations ( this , bulkMode , determineCollectionName ( entityClass ) , writeConcern ) ;
return bulkOps ( bulkMode , entityClass , determineCollectionName ( entityClass ) ) ;
}
public BulkOperations bulkOps ( BulkMode mode , Class < ? > entityType , String collectionName ) {
Assert . notNull ( mode , "BulkMode must not be null!" ) ;
Assert . hasText ( collectionName , "Collection name must not be null or empty!" ) ;
DefaultBulkOperations operations = new DefaultBulkOperations ( this , mode , collectionName , entityType ) ;
operations . setExceptionTranslator ( exceptionTranslator ) ;
operations . setWriteConcernResolver ( writeConcernResolver ) ;
operations . setDefaultWriteConcern ( writeConcern ) ;
return operations ;
}
/ *
* ( non - Javadoc )
* @see org . springframework . data . mongodb . core . MongoOperations # scriptOps ( )
@ -646,8 +661,8 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
@@ -646,8 +661,8 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
List < Object > results = ( List < Object > ) commandResult . get ( "results" ) ;
results = results = = null ? Collections . emptyList ( ) : results ;
DbObjectCallback < GeoResult < T > > callback = new GeoNearResultDbObjectCallback < T > ( new ReadDbObjectCallback < T > (
mongoConverter , entityClass , collectionName ) , near . getMetric ( ) ) ;
DbObjectCallback < GeoResult < T > > callback = new GeoNearResultDbObjectCallback < T > (
new ReadDbObjectCallback < T > ( mongoConverter , entityClass , collectionName ) , near . getMetric ( ) ) ;
List < GeoResult < T > > result = new ArrayList < GeoResult < T > > ( results . size ( ) ) ;
int index = 0 ;
@ -724,8 +739,9 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
@@ -724,8 +739,9 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
public long count ( Query query , Class < ? > entityClass , String collectionName ) {
Assert . hasText ( collectionName ) ;
final DBObject dbObject = query = = null ? null : queryMapper . getMappedObject ( query . getQueryObject ( ) ,
entityClass = = null ? null : mappingContext . getPersistentEntity ( entityClass ) ) ;
final DBObject dbObject = query = = null ? null
: queryMapper . getMappedObject ( query . getQueryObject ( ) ,
entityClass = = null ? null : mappingContext . getPersistentEntity ( entityClass ) ) ;
return execute ( collectionName , new CollectionCallback < Long > ( ) {
public Long doInCollection ( DBCollection collection ) throws MongoException , DataAccessException {
@ -996,8 +1012,8 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
@@ -996,8 +1012,8 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
MongoAction mongoAction = new MongoAction ( writeConcern , MongoActionOperation . INSERT , collectionName ,
entityClass , dbDoc , null ) ;
WriteConcern writeConcernToUse = prepareWriteConcern ( mongoAction ) ;
WriteResult writeResult = writeConcernToUse = = null ? collection . insert ( dbDoc ) : collection . insert ( dbDoc ,
writeConcernToUse ) ;
WriteResult writeResult = writeConcernToUse = = null ? collection . insert ( dbDoc )
: collection . insert ( dbDoc , writeConcernToUse ) ;
handleAnyWriteResultErrors ( writeResult , dbDoc , MongoActionOperation . INSERT ) ;
return dbDoc . get ( ID_FIELD ) ;
}
@ -1017,8 +1033,8 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
@@ -1017,8 +1033,8 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
MongoAction mongoAction = new MongoAction ( writeConcern , MongoActionOperation . INSERT_LIST , collectionName , null ,
null , null ) ;
WriteConcern writeConcernToUse = prepareWriteConcern ( mongoAction ) ;
WriteResult writeResult = writeConcernToUse = = null ? collection . insert ( dbDocList ) : collection . insert (
dbDocList . toArray ( ( DBObject [ ] ) new BasicDBObject [ dbDocList . size ( ) ] ) , writeConcernToUse ) ;
WriteResult writeResult = writeConcernToUse = = null ? collection . insert ( dbDocList )
: collection . insert ( dbDocList . toArray ( ( DBObject [ ] ) new BasicDBObject [ dbDocList . size ( ) ] ) , writeConcernToUse ) ;
handleAnyWriteResultErrors ( writeResult , null , MongoActionOperation . INSERT_LIST ) ;
return null ;
}
@ -1046,8 +1062,8 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
@@ -1046,8 +1062,8 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
MongoAction mongoAction = new MongoAction ( writeConcern , MongoActionOperation . SAVE , collectionName , entityClass ,
dbDoc , null ) ;
WriteConcern writeConcernToUse = prepareWriteConcern ( mongoAction ) ;
WriteResult writeResult = writeConcernToUse = = null ? collection . save ( dbDoc ) : collection . save ( dbDoc ,
writeConcernToUse ) ;
WriteResult writeResult = writeConcernToUse = = null ? collection . save ( dbDoc )
: collection . save ( dbDoc , writeConcernToUse ) ;
handleAnyWriteResultErrors ( writeResult , dbDoc , MongoActionOperation . SAVE ) ;
return dbDoc . get ( ID_FIELD ) ;
}
@ -1100,10 +1116,10 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
@@ -1100,10 +1116,10 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
increaseVersionForUpdateIfNecessary ( entity , update ) ;
DBObject queryObj = query = = null ? new BasicDBObject ( ) : queryMapper . getMappedObject ( query . getQueryObject ( ) ,
entity ) ;
DBObject updateObj = update = = null ? new BasicDBObject ( ) : updateMapper . getMappedObject (
update . getUpdateObject ( ) , entity ) ;
DBObject queryObj = query = = null ? new BasicDBObject ( )
: queryMapper . getMappedObject ( query . getQueryObject ( ) , entity ) ;
DBObject updateObj = update = = null ? new BasicDBObject ( )
: updateMapper . getMappedObject ( update . getUpdateObject ( ) , entity ) ;
if ( LOGGER . isDebugEnabled ( ) ) {
LOGGER . debug ( String . format ( "Calling update using query: %s and update: %s in collection: %s" ,
@ -1244,9 +1260,9 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
@@ -1244,9 +1260,9 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
Object idValue = persistentEntity . getPropertyAccessor ( entity ) . getProperty ( idProperty ) ;
if ( idValue = = null & & ! MongoSimpleTypes . AUTOGENERATED_ID_TYPES . contains ( idProperty . getType ( ) ) ) {
throw new InvalidDataAccessApiUsageException ( String . format (
"Cannot autogenerate id of type %s for entity of type %s!" , idProperty . getType ( ) . getName ( ) , entity . getClass ( )
. getName ( ) ) ) ;
throw new InvalidDataAccessApiUsageException (
String . format ( "Cannot autogenerate id of type %s for entity of type %s!" , idProperty . getType ( ) . getName ( ) ,
entity . getClass ( ) . getName ( ) ) ) ;
}
}
@ -1285,12 +1301,12 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
@@ -1285,12 +1301,12 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
WriteConcern writeConcernToUse = prepareWriteConcern ( mongoAction ) ;
if ( LOGGER . isDebugEnabled ( ) ) {
LOGGER . debug ( "Remove using query: {} in collection: {}." , new Object [ ] { serializeToJsonSafely ( dboq ) ,
collection . getName ( ) } ) ;
LOGGER . debug ( "Remove using query: {} in collection: {}." ,
new Object [ ] { serializeToJsonSafely ( dboq ) , collection . getName ( ) } ) ;
}
WriteResult wr = writeConcernToUse = = null ? collection . remove ( dboq ) : collection . remove ( dboq ,
writeConcernToUse ) ;
WriteResult wr = writeConcernToUse = = null ? collection . remove ( dboq )
: collection . remove ( dboq , writeConcernToUse ) ;
handleAnyWriteResultErrors ( wr , dboq , MongoActionOperation . REMOVE ) ;
@ -1306,8 +1322,8 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
@@ -1306,8 +1322,8 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
}
public < T > List < T > findAll ( Class < T > entityClass , String collectionName ) {
return executeFindMultiInternal ( new FindCallback ( null ) , null , new ReadDbObjectCallback < T > ( mongoConverter ,
entityClass , collectionName ) , collectionName ) ;
return executeFindMultiInternal ( new FindCallback ( null ) , null ,
new ReadDbObjectCallback < T > ( mongoConverter , entityClass , collectionName ) , collectionName ) ;
}
public < T > MapReduceResults < T > mapReduce ( String inputCollectionName , String mapFunction , String reduceFunction ,
@ -1323,8 +1339,8 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
@@ -1323,8 +1339,8 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
public < T > MapReduceResults < T > mapReduce ( Query query , String inputCollectionName , String mapFunction ,
String reduceFunction , Class < T > entityClass ) {
return mapReduce ( query , inputCollectionName , mapFunction , reduceFunction ,
new MapReduceOptions ( ) . outputTypeInline ( ) , entityClass ) ;
return mapReduce ( query , inputCollectionName , mapFunction , reduceFunction , new MapReduceOptions ( ) . outputTypeInline ( ) ,
entityClass ) ;
}
public < T > MapReduceResults < T > mapReduce ( Query query , String inputCollectionName , String mapFunction ,
@ -1335,8 +1351,9 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
@@ -1335,8 +1351,9 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
DBCollection inputCollection = getCollection ( inputCollectionName ) ;
MapReduceCommand command = new MapReduceCommand ( inputCollection , mapFunc , reduceFunc ,
mapReduceOptions . getOutputCollection ( ) , mapReduceOptions . getOutputType ( ) , query = = null
| | query . getQueryObject ( ) = = null ? null : queryMapper . getMappedObject ( query . getQueryObject ( ) , null ) ) ;
mapReduceOptions . getOutputCollection ( ) , mapReduceOptions . getOutputType ( ) ,
query = = null | | query . getQueryObject ( ) = = null ? null
: queryMapper . getMappedObject ( query . getQueryObject ( ) , null ) ) ;
copyMapReduceOptionsToCommand ( query , mapReduceOptions , command ) ;
@ -1669,8 +1686,8 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
@@ -1669,8 +1686,8 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
serializeToJsonSafely ( query ) , mappedFields , entityClass , collectionName ) ) ;
}
return executeFindOneInternal ( new FindOneCallback ( mappedQuery , mappedFields ) , new ReadDbObjectCallback < T > (
this . mongoConverter , entityClass , collectionName ) , collectionName ) ;
return executeFindOneInternal ( new FindOneCallback ( mappedQuery , mappedFields ) ,
new ReadDbObjectCallback < T > ( this . mongoConverter , entityClass , collectionName ) , collectionName ) ;
}
/ * *
@ -1684,8 +1701,8 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
@@ -1684,8 +1701,8 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
* @return the List of converted objects .
* /
protected < T > List < T > doFind ( String collectionName , DBObject query , DBObject fields , Class < T > entityClass ) {
return doFind ( collectionName , query , fields , entityClass , null , new ReadDbObjectCallback < T > ( this . mongoConverter ,
entityClass , collectionName ) ) ;
return doFind ( collectionName , query , fields , entityClass , null ,
new ReadDbObjectCallback < T > ( this . mongoConverter , entityClass , collectionName ) ) ;
}
/ * *
@ -1703,8 +1720,8 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
@@ -1703,8 +1720,8 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
* /
protected < T > List < T > doFind ( String collectionName , DBObject query , DBObject fields , Class < T > entityClass ,
CursorPreparer preparer ) {
return doFind ( collectionName , query , fields , entityClass , preparer , new ReadDbObjectCallback < T > ( mongoConverter ,
entityClass , collectionName ) ) ;
return doFind ( collectionName , query , fields , entityClass , preparer ,
new ReadDbObjectCallback < T > ( mongoConverter , entityClass , collectionName ) ) ;
}
protected < S , T > List < T > doFind ( String collectionName , DBObject query , DBObject fields , Class < S > entityClass ,
@ -1780,9 +1797,13 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
@@ -1780,9 +1797,13 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
DBObject mappedUpdate = updateMapper . getMappedObject ( update . getUpdateObject ( ) , entity ) ;
if ( LOGGER . isDebugEnabled ( ) ) {
LOGGER . debug ( String . format ( "findAndModify using query: %s fields: %s sort: %s for class: %s and update: %s "
+ "in collection: %s" , serializeToJsonSafely ( mappedQuery ) , fields , sort , entityClass ,
serializeToJsonSafely ( mappedUpdate ) , collectionName ) ) ;
LOGGER
. debug (
String . format (
"findAndModify using query: %s fields: %s sort: %s for class: %s and update: %s "
+ "in collection: %s" ,
serializeToJsonSafely ( mappedQuery ) , fields , sort , entityClass , serializeToJsonSafely ( mappedUpdate ) ,
collectionName ) ) ;
}
return executeFindOneInternal ( new FindAndModifyCallback ( mappedQuery , fields , sort , mappedUpdate , options ) ,
@ -1852,8 +1873,8 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
@@ -1852,8 +1873,8 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
DbObjectCallback < T > objectCallback , String collectionName ) {
try {
T result = objectCallback . doWith ( collectionCallback . doInCollection ( getAndPrepareCollection ( getDb ( ) ,
collectionName ) ) ) ;
T result = objectCallback
. doWith ( collectionCallback . doInCollection ( getAndPrepareCollection ( getDb ( ) , collectionName ) ) ) ;
return result ;
} catch ( RuntimeException e ) {
throw potentiallyConvertRuntimeException ( e , exceptionTranslator ) ;
@ -1878,8 +1899,8 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
@@ -1878,8 +1899,8 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
* @param collectionName the collection to be queried
* @return
* /
private < T > List < T > executeFindMultiInternal ( CollectionCallback < DBCursor > collectionCallback ,
CursorPreparer preparer , DbObjectCallback < T > objectCallback , String collectionName ) {
private < T > List < T > executeFindMultiInternal ( CollectionCallback < DBCursor > collectionCallback , CursorPreparer preparer ,
DbObjectCallback < T > objectCallback , String collectionName ) {
try {
@ -1969,8 +1990,8 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
@@ -1969,8 +1990,8 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
MongoPersistentEntity < ? > entity = mappingContext . getPersistentEntity ( entityClass ) ;
if ( entity = = null ) {
throw new InvalidDataAccessApiUsageException ( "No Persistent Entity information found for the class "
+ entityClass . getName ( ) ) ;
throw new InvalidDataAccessApiUsageException (
"No Persistent Entity information found for the class " + entityClass . getName ( ) ) ;
}
return entity . getCollection ( ) ;
}
@ -2034,8 +2055,8 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
@@ -2034,8 +2055,8 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
String error = result . getErrorMessage ( ) ;
error = error = = null ? "NO MESSAGE" : error ;
throw new InvalidDataAccessApiUsageException ( "Command execution failed: Error [" + error + "], Command = "
+ source , ex ) ;
throw new InvalidDataAccessApiUsageException (
"Command execution failed: Error [" + error + "], Command = " + source , ex ) ;
}
}
@ -2231,7 +2252,8 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
@@ -2231,7 +2252,8 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
class UnwrapAndReadDbObjectCallback < T > extends ReadDbObjectCallback < T > {
public UnwrapAndReadDbObjectCallback ( EntityReader < ? super T , DBObject > reader , Class < T > type , String collectionName ) {
public UnwrapAndReadDbObjectCallback ( EntityReader < ? super T , DBObject > reader , Class < T > type ,
String collectionName ) {
super ( reader , type , collectionName ) ;
}
@ -2258,15 +2280,6 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
@@ -2258,15 +2280,6 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
}
}
private enum DefaultWriteConcernResolver implements WriteConcernResolver {
INSTANCE ;
public WriteConcern resolve ( MongoAction action ) {
return action . getDefaultWriteConcern ( ) ;
}
}
class QueryCursorPreparer implements CursorPreparer {
private final Query query ;