tweaking the MongoOperstions API for consistency across methods

This commit is contained in:
Thomas Risberg
2011-02-14 15:48:12 -05:00
parent 8884205234
commit e734277c3c
7 changed files with 39 additions and 39 deletions

View File

@@ -18,7 +18,7 @@ package org.springframework.data.document.mongodb;
import java.util.List;
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.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.
*
* @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 action callback object that specifies the MongoDB action
* the callback action.
* @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
@@ -208,21 +208,21 @@ public interface MongoOperations {
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.
*
* @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.
*
* @param collectionName
* @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

View File

@@ -33,7 +33,7 @@ import org.springframework.core.convert.ConversionFailedException;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.DataIntegrityViolationException;
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.Update;
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)
*/
public <T> T execute(CollectionCallback<T> callback) {
return execute(callback, defaultCollectionName);
return execute(getDefaultCollectionName(), callback);
}
/* (non-Javadoc)
* @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);
@@ -416,33 +416,33 @@ public class MongoTemplate implements InitializingBean, MongoOperations {
*/
public void dropCollection(String collectionName) {
execute(new CollectionCallback<Void>() {
execute(collectionName, new CollectionCallback<Void>() {
public Void doInCollection(DBCollection collection) throws MongoException, DataAccessException {
collection.drop();
return null;
}
}, collectionName);
});
}
// Indexing methods
public void ensureIndex(IndexSpecification indexSpecification) {
ensureIndex(getDefaultCollectionName(), indexSpecification);
public void ensureIndex(IndexDefinition indexDefinition) {
ensureIndex(getDefaultCollectionName(), indexDefinition);
}
public void ensureIndex(String collectionName, final IndexSpecification indexSpecification) {
execute(new CollectionCallback<Object>() {
public void ensureIndex(String collectionName, final IndexDefinition indexDefinition) {
execute(collectionName, new CollectionCallback<Object>() {
public Object doInCollection(DBCollection collection) throws MongoException, DataAccessException {
DBObject indexOptions = indexSpecification.getIndexOptions();
DBObject indexOptions = indexDefinition.getIndexOptions();
if (indexOptions != null) {
collection.ensureIndex(indexSpecification.getIndexObject(), indexOptions);
collection.ensureIndex(indexDefinition.getIndexObject(), indexOptions);
}
else {
collection.ensureIndex(indexSpecification.getIndexObject());
collection.ensureIndex(indexDefinition.getIndexObject());
}
return null;
}
}, collectionName);
});
}
// 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 execute(new CollectionCallback<Object>() {
return execute(collectionName, new CollectionCallback<Object>() {
public Object doInCollection(DBCollection collection) throws MongoException, DataAccessException {
if (writeConcern == null) {
collection.insert(dbDoc);
@@ -633,7 +633,7 @@ public class MongoTemplate implements InitializingBean, MongoOperations {
}
return dbDoc.get(ID);
}
}, collectionName);
});
}
protected List<ObjectId> insertDBObjectList(String collectionName, final List<DBObject> dbDocList) {
@@ -642,7 +642,7 @@ public class MongoTemplate implements InitializingBean, MongoOperations {
return Collections.emptyList();
}
execute(new CollectionCallback<Void>() {
execute(collectionName, new CollectionCallback<Void>() {
public Void doInCollection(DBCollection collection) throws MongoException, DataAccessException {
if (writeConcern == null) {
collection.insert(dbDocList);
@@ -652,7 +652,7 @@ public class MongoTemplate implements InitializingBean, MongoOperations {
}
return null;
}
}, collectionName);
});
List<ObjectId> ids = new ArrayList<ObjectId>();
for (DBObject dbo : dbDocList) {
@@ -674,7 +674,7 @@ public class MongoTemplate implements InitializingBean, MongoOperations {
return null;
}
return execute(new CollectionCallback<ObjectId>() {
return execute(collectionName, new CollectionCallback<ObjectId>() {
public ObjectId doInCollection(DBCollection collection) throws MongoException, DataAccessException {
if (writeConcern == null) {
collection.save(dbDoc);
@@ -684,7 +684,7 @@ public class MongoTemplate implements InitializingBean, MongoOperations {
}
return (ObjectId) dbDoc.get(ID);
}
}, collectionName);
});
}
/* (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)
*/
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 {
WriteResult wr;
if (writeConcern == null) {
@@ -710,7 +710,7 @@ public class MongoTemplate implements InitializingBean, MongoOperations {
handleAnyWriteResultErrors(wr, query.getQueryObject(), "update with '" +update.getUpdateObject() + "'");
return wr;
}
}, collectionName);
});
}
/* (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)
*/
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 {
WriteResult wr = null;
if (writeConcern == null) {
@@ -736,7 +736,7 @@ public class MongoTemplate implements InitializingBean, MongoOperations {
handleAnyWriteResultErrors(wr, query.getQueryObject(), "update with '" +update.getUpdateObject() + "'");
return wr;
}
}, collectionName);
});
}
/* (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)
*/
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 {
WriteResult wr = null;
if (writeConcern == null) {
@@ -762,7 +762,7 @@ public class MongoTemplate implements InitializingBean, MongoOperations {
handleAnyWriteResultErrors(wr, query.getQueryObject(), "remove");
return null;
}
}, collectionName);
});
}

View File

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

View File

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

View File

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

View File

@@ -176,13 +176,13 @@ public class MongoQuery implements RepositoryQuery {
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) {
return collection.find(query);
}
}, collectionName);
});
}
}

View File

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