updates related to collection creation
This commit is contained in:
@@ -0,0 +1,45 @@
|
|||||||
|
package org.springframework.datastore.document.mongodb;
|
||||||
|
|
||||||
|
public class CollectionOptions {
|
||||||
|
|
||||||
|
private Integer maxDocuments;
|
||||||
|
|
||||||
|
private Integer size;
|
||||||
|
|
||||||
|
private Boolean capped;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public CollectionOptions(Integer size, Integer maxDocuments, Boolean capped) {
|
||||||
|
super();
|
||||||
|
this.maxDocuments = maxDocuments;
|
||||||
|
this.size = size;
|
||||||
|
this.capped = capped;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Integer getMaxDocuments() {
|
||||||
|
return maxDocuments;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setMaxDocuments(Integer maxDocuments) {
|
||||||
|
this.maxDocuments = maxDocuments;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Integer getSize() {
|
||||||
|
return size;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setSize(Integer size) {
|
||||||
|
this.size = size;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Boolean getCapped() {
|
||||||
|
return capped;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCapped(Boolean capped) {
|
||||||
|
this.capped = capped;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@@ -20,6 +20,7 @@ package org.springframework.datastore.document.mongodb;
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.InitializingBean;
|
||||||
import org.springframework.dao.DataRetrievalFailureException;
|
import org.springframework.dao.DataRetrievalFailureException;
|
||||||
import org.springframework.dao.InvalidDataAccessApiUsageException;
|
import org.springframework.dao.InvalidDataAccessApiUsageException;
|
||||||
import org.springframework.datastore.document.AbstractDocumentStoreTemplate;
|
import org.springframework.datastore.document.AbstractDocumentStoreTemplate;
|
||||||
@@ -27,6 +28,7 @@ import org.springframework.datastore.document.DocumentMapper;
|
|||||||
import org.springframework.datastore.document.DocumentSource;
|
import org.springframework.datastore.document.DocumentSource;
|
||||||
import org.springframework.datastore.document.mongodb.query.Query;
|
import org.springframework.datastore.document.mongodb.query.Query;
|
||||||
|
|
||||||
|
import com.mongodb.BasicDBObject;
|
||||||
import com.mongodb.CommandResult;
|
import com.mongodb.CommandResult;
|
||||||
import com.mongodb.DB;
|
import com.mongodb.DB;
|
||||||
import com.mongodb.DBCollection;
|
import com.mongodb.DBCollection;
|
||||||
@@ -35,10 +37,15 @@ import com.mongodb.MongoException;
|
|||||||
import com.mongodb.WriteResult;
|
import com.mongodb.WriteResult;
|
||||||
import com.mongodb.util.JSON;
|
import com.mongodb.util.JSON;
|
||||||
|
|
||||||
public class MongoTemplate extends AbstractDocumentStoreTemplate<DB> {
|
public class MongoTemplate extends AbstractDocumentStoreTemplate<DB> implements InitializingBean {
|
||||||
|
|
||||||
private DB db;
|
private DB db;
|
||||||
|
|
||||||
|
private String defaultCollectionName;
|
||||||
|
|
||||||
|
//TODO expose configuration...
|
||||||
|
private CollectionOptions defaultCollectionOptions;
|
||||||
|
|
||||||
// public MongoTemplate() {
|
// public MongoTemplate() {
|
||||||
// super();
|
// super();
|
||||||
// }
|
// }
|
||||||
@@ -47,6 +54,20 @@ public class MongoTemplate extends AbstractDocumentStoreTemplate<DB> {
|
|||||||
super();
|
super();
|
||||||
this.db = db;
|
this.db = db;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public String getDefaultCollectionName() {
|
||||||
|
return defaultCollectionName;
|
||||||
|
}
|
||||||
|
|
||||||
|
//TODO would one ever consider passing in a DBCollection object?
|
||||||
|
|
||||||
|
public void setDefaultCollectionName(String defaultCollection) {
|
||||||
|
this.defaultCollectionName = defaultCollection;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public void execute(String command) {
|
public void execute(String command) {
|
||||||
execute((DBObject)JSON.parse(command));
|
execute((DBObject)JSON.parse(command));
|
||||||
@@ -65,9 +86,25 @@ public class MongoTemplate extends AbstractDocumentStoreTemplate<DB> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void createCollection(String collectionName, DocumentSource<DBObject> documentSource) {
|
public DBCollection createCollection(String collectionName) {
|
||||||
try {
|
try {
|
||||||
getConnection().createCollection(collectionName, documentSource.getDocument());
|
return getConnection().createCollection(collectionName, null);
|
||||||
|
} catch (MongoException e) {
|
||||||
|
throw new InvalidDataAccessApiUsageException("Error creating collection " + collectionName + ": " + e.getMessage(), e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void createCollection(String collectionName, CollectionOptions collectionOptions) {
|
||||||
|
try {
|
||||||
|
getConnection().createCollection(collectionName, convertToDbObject(collectionOptions));
|
||||||
|
} catch (MongoException e) {
|
||||||
|
throw new InvalidDataAccessApiUsageException("Error creating collection " + collectionName + ": " + e.getMessage(), e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean collectionExists(String collectionName) {
|
||||||
|
try {
|
||||||
|
return getConnection().collectionExists(collectionName);
|
||||||
} catch (MongoException e) {
|
} catch (MongoException e) {
|
||||||
throw new InvalidDataAccessApiUsageException("Error creating collection " + collectionName + ": " + e.getMessage(), e);
|
throw new InvalidDataAccessApiUsageException("Error creating collection " + collectionName + ": " + e.getMessage(), e);
|
||||||
}
|
}
|
||||||
@@ -77,6 +114,20 @@ public class MongoTemplate extends AbstractDocumentStoreTemplate<DB> {
|
|||||||
getConnection().getCollection(collectionName)
|
getConnection().getCollection(collectionName)
|
||||||
.drop();
|
.drop();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void saveObject(Object object) {
|
||||||
|
saveObject(getRequiredDefaultCollectionName(), object);
|
||||||
|
}
|
||||||
|
|
||||||
|
private String getRequiredDefaultCollectionName() {
|
||||||
|
String name = getDefaultCollectionName();
|
||||||
|
if (name == null) {
|
||||||
|
throw new IllegalStateException(
|
||||||
|
"No 'defaultCollection' or 'defaultCollectionName' specified. Check configuration of MongoTemplate.");
|
||||||
|
}
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
public void saveObject(String collectionName, Object source) {
|
public void saveObject(String collectionName, Object source) {
|
||||||
MongoBeanPropertyDocumentSource docSrc = new MongoBeanPropertyDocumentSource(source);
|
MongoBeanPropertyDocumentSource docSrc = new MongoBeanPropertyDocumentSource(source);
|
||||||
@@ -87,7 +138,7 @@ public class MongoTemplate extends AbstractDocumentStoreTemplate<DB> {
|
|||||||
DBObject dbDoc = documentSource.getDocument();
|
DBObject dbDoc = documentSource.getDocument();
|
||||||
WriteResult wr = null;
|
WriteResult wr = null;
|
||||||
try {
|
try {
|
||||||
wr = getConnection().getCollection(collectionName).save(dbDoc);
|
wr = getConnection().getCollection(collectionName).save(dbDoc);
|
||||||
} catch (MongoException e) {
|
} catch (MongoException e) {
|
||||||
throw new DataRetrievalFailureException(wr.getLastError().getErrorMessage(), e);
|
throw new DataRetrievalFailureException(wr.getLastError().getErrorMessage(), e);
|
||||||
}
|
}
|
||||||
@@ -148,4 +199,31 @@ public class MongoTemplate extends AbstractDocumentStoreTemplate<DB> {
|
|||||||
return db;
|
return db;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
protected DBObject convertToDbObject(CollectionOptions collectionOptions) {
|
||||||
|
DBObject dbo = new BasicDBObject();
|
||||||
|
if (collectionOptions != null) {
|
||||||
|
if (collectionOptions.getCapped() != null) {
|
||||||
|
dbo.put("capped", collectionOptions.getCapped().booleanValue());
|
||||||
|
}
|
||||||
|
if (collectionOptions.getSize() != null) {
|
||||||
|
dbo.put("size", collectionOptions.getSize().intValue());
|
||||||
|
}
|
||||||
|
if (collectionOptions.getMaxDocuments() != null ) {
|
||||||
|
dbo.put("max", collectionOptions.getMaxDocuments().intValue());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return dbo;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public void afterPropertiesSet() throws Exception {
|
||||||
|
if (this.getDefaultCollectionName() != null) {
|
||||||
|
if (! db.collectionExists(getDefaultCollectionName())) {
|
||||||
|
db.createCollection(getDefaultCollectionName(), null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user