Browse Source

tweaking the MongoOperstions API for consistency across methods

pull/1/head
Thomas Risberg 15 years ago
parent
commit
e734277c3c
  1. 14
      spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/MongoOperations.java
  2. 50
      spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/MongoTemplate.java
  3. 2
      spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/query/GeospatialIndex.java
  4. 2
      spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/query/Index.java
  5. 2
      spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/query/IndexDefinition.java
  6. 4
      spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/repository/MongoQuery.java
  7. 4
      spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/MongoOperationsUnitTests.java

14
spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/MongoOperations.java

@ -18,7 +18,7 @@ package org.springframework.data.document.mongodb;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
import org.springframework.data.document.mongodb.query.IndexSpecification; import org.springframework.data.document.mongodb.query.IndexDefinition;
import org.springframework.data.document.mongodb.query.Query; import org.springframework.data.document.mongodb.query.Query;
import org.springframework.data.document.mongodb.query.Update; import org.springframework.data.document.mongodb.query.Update;
@ -95,12 +95,12 @@ public interface MongoOperations {
* Allows for returning a result object, that is a domain object or a collection of domain objects. * Allows for returning a result object, that is a domain object or a collection of domain objects.
* *
* @param <T> return type * @param <T> return type
* @param action callback object that specifies the MongoDB action
* @param collectionName the name of the collection that specifies which DBCollection instance will be passed into * @param collectionName the name of the collection that specifies which DBCollection instance will be passed into
* @param action callback object that specifies the MongoDB action
* the callback action. * the callback action.
* @return a result object returned by the action or <tt>null</tt> * @return a result object returned by the action or <tt>null</tt>
*/ */
<T> T execute(CollectionCallback<T> action, String collectionName); <T> T execute(String collectionName, CollectionCallback<T> action);
/** /**
* Executes the given {@link DbCallback} within the same connection to the database so as to ensure * Executes the given {@link DbCallback} within the same connection to the database so as to ensure
@ -208,21 +208,21 @@ public interface MongoOperations {
MongoReader<T> reader); MongoReader<T> reader);
/** /**
* Ensure that an index for the provided {@link IndexSpecification} exists for the default collection. * Ensure that an index for the provided {@link IndexDefinition} exists for the default collection.
* If not it will be created. * If not it will be created.
* *
* @param index * @param index
*/ */
void ensureIndex(IndexSpecification indexSpecification); void ensureIndex(IndexDefinition indexDefinition);
/** /**
* Ensure that an index for the provided {@link IndexSpecification} exists. If not it will be * Ensure that an index for the provided {@link IndexDefinition} exists. If not it will be
* created. * created.
* *
* @param collectionName * @param collectionName
* @param index * @param index
*/ */
void ensureIndex(String collectionName, IndexSpecification indexSpecification); void ensureIndex(String collectionName, IndexDefinition indexDefinition);
/** /**
* Map the results of an ad-hoc query on the default MongoDB collection to a single instance of an object * Map the results of an ad-hoc query on the default MongoDB collection to a single instance of an object

50
spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/MongoTemplate.java

@ -33,7 +33,7 @@ import org.springframework.core.convert.ConversionFailedException;
import org.springframework.dao.DataAccessException; import org.springframework.dao.DataAccessException;
import org.springframework.dao.DataIntegrityViolationException; import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.data.document.mongodb.MongoPropertyDescriptors.MongoPropertyDescriptor; import org.springframework.data.document.mongodb.MongoPropertyDescriptors.MongoPropertyDescriptor;
import org.springframework.data.document.mongodb.query.IndexSpecification; import org.springframework.data.document.mongodb.query.IndexDefinition;
import org.springframework.data.document.mongodb.query.Query; import org.springframework.data.document.mongodb.query.Query;
import org.springframework.data.document.mongodb.query.Update; import org.springframework.data.document.mongodb.query.Update;
import org.springframework.jca.cci.core.ConnectionCallback; import org.springframework.jca.cci.core.ConnectionCallback;
@ -280,13 +280,13 @@ public class MongoTemplate implements InitializingBean, MongoOperations {
* @see org.springframework.data.document.mongodb.MongoOperations#execute(org.springframework.data.document.mongodb.CollectionCallback) * @see org.springframework.data.document.mongodb.MongoOperations#execute(org.springframework.data.document.mongodb.CollectionCallback)
*/ */
public <T> T execute(CollectionCallback<T> callback) { public <T> T execute(CollectionCallback<T> callback) {
return execute(callback, defaultCollectionName); return execute(getDefaultCollectionName(), callback);
} }
/* (non-Javadoc) /* (non-Javadoc)
* @see org.springframework.data.document.mongodb.MongoOperations#execute(org.springframework.data.document.mongodb.CollectionCallback, java.lang.String) * @see org.springframework.data.document.mongodb.MongoOperations#execute(org.springframework.data.document.mongodb.CollectionCallback, java.lang.String)
*/ */
public <T> T execute(CollectionCallback<T> callback, String collectionName) { public <T> T execute(String collectionName, CollectionCallback<T> callback) {
Assert.notNull(callback); Assert.notNull(callback);
@ -416,33 +416,33 @@ public class MongoTemplate implements InitializingBean, MongoOperations {
*/ */
public void dropCollection(String collectionName) { public void dropCollection(String collectionName) {
execute(new CollectionCallback<Void>() { execute(collectionName, new CollectionCallback<Void>() {
public Void doInCollection(DBCollection collection) throws MongoException, DataAccessException { public Void doInCollection(DBCollection collection) throws MongoException, DataAccessException {
collection.drop(); collection.drop();
return null; return null;
} }
}, collectionName); });
} }
// Indexing methods // Indexing methods
public void ensureIndex(IndexSpecification indexSpecification) { public void ensureIndex(IndexDefinition indexDefinition) {
ensureIndex(getDefaultCollectionName(), indexSpecification); ensureIndex(getDefaultCollectionName(), indexDefinition);
} }
public void ensureIndex(String collectionName, final IndexSpecification indexSpecification) { public void ensureIndex(String collectionName, final IndexDefinition indexDefinition) {
execute(new CollectionCallback<Object>() { execute(collectionName, new CollectionCallback<Object>() {
public Object doInCollection(DBCollection collection) throws MongoException, DataAccessException { public Object doInCollection(DBCollection collection) throws MongoException, DataAccessException {
DBObject indexOptions = indexSpecification.getIndexOptions(); DBObject indexOptions = indexDefinition.getIndexOptions();
if (indexOptions != null) { if (indexOptions != null) {
collection.ensureIndex(indexSpecification.getIndexObject(), indexOptions); collection.ensureIndex(indexDefinition.getIndexObject(), indexOptions);
} }
else { else {
collection.ensureIndex(indexSpecification.getIndexObject()); collection.ensureIndex(indexDefinition.getIndexObject());
} }
return null; return null;
} }
}, collectionName); });
} }
// Find methods that take a Query to express the query and that return a single object. // Find methods that take a Query to express the query and that return a single object.
@ -623,7 +623,7 @@ public class MongoTemplate implements InitializingBean, MongoOperations {
return null; return null;
} }
return execute(new CollectionCallback<Object>() { return execute(collectionName, new CollectionCallback<Object>() {
public Object doInCollection(DBCollection collection) throws MongoException, DataAccessException { public Object doInCollection(DBCollection collection) throws MongoException, DataAccessException {
if (writeConcern == null) { if (writeConcern == null) {
collection.insert(dbDoc); collection.insert(dbDoc);
@ -633,7 +633,7 @@ public class MongoTemplate implements InitializingBean, MongoOperations {
} }
return dbDoc.get(ID); return dbDoc.get(ID);
} }
}, collectionName); });
} }
protected List<ObjectId> insertDBObjectList(String collectionName, final List<DBObject> dbDocList) { protected List<ObjectId> insertDBObjectList(String collectionName, final List<DBObject> dbDocList) {
@ -642,7 +642,7 @@ public class MongoTemplate implements InitializingBean, MongoOperations {
return Collections.emptyList(); return Collections.emptyList();
} }
execute(new CollectionCallback<Void>() { execute(collectionName, new CollectionCallback<Void>() {
public Void doInCollection(DBCollection collection) throws MongoException, DataAccessException { public Void doInCollection(DBCollection collection) throws MongoException, DataAccessException {
if (writeConcern == null) { if (writeConcern == null) {
collection.insert(dbDocList); collection.insert(dbDocList);
@ -652,7 +652,7 @@ public class MongoTemplate implements InitializingBean, MongoOperations {
} }
return null; return null;
} }
}, collectionName); });
List<ObjectId> ids = new ArrayList<ObjectId>(); List<ObjectId> ids = new ArrayList<ObjectId>();
for (DBObject dbo : dbDocList) { for (DBObject dbo : dbDocList) {
@ -674,7 +674,7 @@ public class MongoTemplate implements InitializingBean, MongoOperations {
return null; return null;
} }
return execute(new CollectionCallback<ObjectId>() { return execute(collectionName, new CollectionCallback<ObjectId>() {
public ObjectId doInCollection(DBCollection collection) throws MongoException, DataAccessException { public ObjectId doInCollection(DBCollection collection) throws MongoException, DataAccessException {
if (writeConcern == null) { if (writeConcern == null) {
collection.save(dbDoc); collection.save(dbDoc);
@ -684,7 +684,7 @@ public class MongoTemplate implements InitializingBean, MongoOperations {
} }
return (ObjectId) dbDoc.get(ID); return (ObjectId) dbDoc.get(ID);
} }
}, collectionName); });
} }
/* (non-Javadoc) /* (non-Javadoc)
@ -698,7 +698,7 @@ public class MongoTemplate implements InitializingBean, MongoOperations {
* @see org.springframework.data.document.mongodb.MongoOperations#updateFirst(java.lang.String, com.mongodb.DBObject, com.mongodb.DBObject) * @see org.springframework.data.document.mongodb.MongoOperations#updateFirst(java.lang.String, com.mongodb.DBObject, com.mongodb.DBObject)
*/ */
public WriteResult updateFirst(String collectionName, final Query query, final Update update) { public WriteResult updateFirst(String collectionName, final Query query, final Update update) {
return execute(new CollectionCallback<WriteResult>() { return execute(collectionName, new CollectionCallback<WriteResult>() {
public WriteResult doInCollection(DBCollection collection) throws MongoException, DataAccessException { public WriteResult doInCollection(DBCollection collection) throws MongoException, DataAccessException {
WriteResult wr; WriteResult wr;
if (writeConcern == null) { if (writeConcern == null) {
@ -710,7 +710,7 @@ public class MongoTemplate implements InitializingBean, MongoOperations {
handleAnyWriteResultErrors(wr, query.getQueryObject(), "update with '" +update.getUpdateObject() + "'"); handleAnyWriteResultErrors(wr, query.getQueryObject(), "update with '" +update.getUpdateObject() + "'");
return wr; return wr;
} }
}, collectionName); });
} }
/* (non-Javadoc) /* (non-Javadoc)
@ -724,7 +724,7 @@ public class MongoTemplate implements InitializingBean, MongoOperations {
* @see org.springframework.data.document.mongodb.MongoOperations#updateMulti(java.lang.String, com.mongodb.DBObject, com.mongodb.DBObject) * @see org.springframework.data.document.mongodb.MongoOperations#updateMulti(java.lang.String, com.mongodb.DBObject, com.mongodb.DBObject)
*/ */
public WriteResult updateMulti(String collectionName, final Query query, final Update update) { public WriteResult updateMulti(String collectionName, final Query query, final Update update) {
return execute(new CollectionCallback<WriteResult>() { return execute(collectionName, new CollectionCallback<WriteResult>() {
public WriteResult doInCollection(DBCollection collection) throws MongoException, DataAccessException { public WriteResult doInCollection(DBCollection collection) throws MongoException, DataAccessException {
WriteResult wr = null; WriteResult wr = null;
if (writeConcern == null) { if (writeConcern == null) {
@ -736,7 +736,7 @@ public class MongoTemplate implements InitializingBean, MongoOperations {
handleAnyWriteResultErrors(wr, query.getQueryObject(), "update with '" +update.getUpdateObject() + "'"); handleAnyWriteResultErrors(wr, query.getQueryObject(), "update with '" +update.getUpdateObject() + "'");
return wr; return wr;
} }
}, collectionName); });
} }
/* (non-Javadoc) /* (non-Javadoc)
@ -750,7 +750,7 @@ public class MongoTemplate implements InitializingBean, MongoOperations {
* @see org.springframework.data.document.mongodb.MongoOperations#remove(java.lang.String, com.mongodb.DBObject) * @see org.springframework.data.document.mongodb.MongoOperations#remove(java.lang.String, com.mongodb.DBObject)
*/ */
public void remove(String collectionName, final Query query) { public void remove(String collectionName, final Query query) {
execute(new CollectionCallback<Void>() { execute(collectionName, new CollectionCallback<Void>() {
public Void doInCollection(DBCollection collection) throws MongoException, DataAccessException { public Void doInCollection(DBCollection collection) throws MongoException, DataAccessException {
WriteResult wr = null; WriteResult wr = null;
if (writeConcern == null) { if (writeConcern == null) {
@ -762,7 +762,7 @@ public class MongoTemplate implements InitializingBean, MongoOperations {
handleAnyWriteResultErrors(wr, query.getQueryObject(), "remove"); handleAnyWriteResultErrors(wr, query.getQueryObject(), "remove");
return null; return null;
} }
}, collectionName); });
} }

2
spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/query/GeospatialIndex.java

@ -19,7 +19,7 @@ import com.mongodb.BasicDBObject;
import com.mongodb.DBObject; import com.mongodb.DBObject;
public class GeospatialIndex implements IndexSpecification { public class GeospatialIndex implements IndexDefinition {
private String keyField; private String keyField;

2
spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/query/Index.java

@ -22,7 +22,7 @@ import com.mongodb.BasicDBObject;
import com.mongodb.DBObject; import com.mongodb.DBObject;
public class Index implements IndexSpecification { public class Index implements IndexDefinition {
public enum Duplicates { public enum Duplicates {
RETAIN, RETAIN,

2
spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/query/IndexSpecification.java → spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/query/IndexDefinition.java

@ -2,7 +2,7 @@ package org.springframework.data.document.mongodb.query;
import com.mongodb.DBObject; import com.mongodb.DBObject;
public interface IndexSpecification { public interface IndexDefinition {
DBObject getIndexObject(); DBObject getIndexObject();

4
spring-data-mongodb/src/main/java/org/springframework/data/document/mongodb/repository/MongoQuery.java

@ -176,13 +176,13 @@ public class MongoQuery implements RepositoryQuery {
private DBCursor getCollectionCursor(String collectionName, final DBObject query) { private DBCursor getCollectionCursor(String collectionName, final DBObject query) {
return template.execute(new CollectionCallback<DBCursor>() { return template.execute(collectionName, new CollectionCallback<DBCursor>() {
public DBCursor doInCollection(DBCollection collection) { public DBCursor doInCollection(DBCollection collection) {
return collection.find(query); return collection.find(query);
} }
}, collectionName); });
} }
} }

4
spring-data-mongodb/src/test/java/org/springframework/data/document/mongodb/MongoOperationsUnitTests.java

@ -87,7 +87,7 @@ public abstract class MongoOperationsUnitTests {
@Test(expected = IllegalArgumentException.class) @Test(expected = IllegalArgumentException.class)
@SuppressWarnings({"unchecked", "rawtypes"}) @SuppressWarnings({"unchecked", "rawtypes"})
public void rejectsNullForCollectionCallback2() { public void rejectsNullForCollectionCallback2() {
getOperations().execute((CollectionCallback) null, "collection"); getOperations().execute("collection", (CollectionCallback) null);
} }
@Test(expected = IllegalArgumentException.class) @Test(expected = IllegalArgumentException.class)
@ -161,7 +161,7 @@ public abstract class MongoOperationsUnitTests {
new Execution() { new Execution() {
@Override @Override
public void doWith(MongoOperations operations) { public void doWith(MongoOperations operations) {
operations.execute(collectionCallback, "collection"); operations.execute("collection", collectionCallback);
} }
}.assertDataAccessException(); }.assertDataAccessException();
} }

Loading…
Cancel
Save