DATADOC-34 - Create indexes for repository query methods on start.
Adapted changes of Spring Data Commons. Implemented IndexEnsuringQueryCreationListener that creates an index for all the properties used in a query. Applies descending order by default but consideres potentially added OrderBy clauses in the query to define the order of the index attributes.
This commit is contained in:
@@ -26,7 +26,6 @@ import org.springframework.data.domain.PageImpl;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.repository.query.ParameterAccessor;
|
||||
import org.springframework.data.repository.query.ParametersParameterAccessor;
|
||||
import org.springframework.data.repository.query.QueryMethod;
|
||||
import org.springframework.data.repository.query.RepositoryQuery;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
@@ -34,7 +33,6 @@ import com.mongodb.DBCollection;
|
||||
import com.mongodb.DBCursor;
|
||||
import com.mongodb.DBObject;
|
||||
|
||||
|
||||
/**
|
||||
* Base class for {@link RepositoryQuery} implementations for Mongo.
|
||||
*
|
||||
@@ -42,167 +40,159 @@ import com.mongodb.DBObject;
|
||||
*/
|
||||
public abstract class AbstractMongoQuery implements RepositoryQuery {
|
||||
|
||||
private final MongoQueryMethod method;
|
||||
private final MongoTemplate template;
|
||||
private final MongoQueryMethod method;
|
||||
private final MongoTemplate template;
|
||||
|
||||
/**
|
||||
* Creates a new {@link AbstractMongoQuery} from the given {@link MongoQueryMethod} and {@link MongoTemplate}.
|
||||
*
|
||||
* @param method
|
||||
* @param template
|
||||
*/
|
||||
public AbstractMongoQuery(MongoQueryMethod method, MongoTemplate template) {
|
||||
|
||||
/**
|
||||
* Creates a new {@link AbstractMongoQuery} from the given {@link QueryMethod} and
|
||||
* {@link MongoTemplate}.
|
||||
*
|
||||
* @param method
|
||||
* @param template
|
||||
*/
|
||||
public AbstractMongoQuery(MongoQueryMethod method, MongoTemplate template) {
|
||||
Assert.notNull(template);
|
||||
Assert.notNull(method);
|
||||
|
||||
Assert.notNull(template);
|
||||
Assert.notNull(method);
|
||||
this.method = method;
|
||||
this.template = template;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.data.repository.query.RepositoryQuery#getQueryMethod()
|
||||
*/
|
||||
public MongoQueryMethod getQueryMethod() {
|
||||
|
||||
return method;
|
||||
}
|
||||
|
||||
this.method = method;
|
||||
this.template = template;
|
||||
}
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
* @see org.springframework.data.repository.query.RepositoryQuery#execute(java .lang.Object[])
|
||||
*/
|
||||
public Object execute(Object[] parameters) {
|
||||
|
||||
ParameterAccessor accessor = new ParametersParameterAccessor(method.getParameters(), parameters);
|
||||
Query query = createQuery(new ConvertingParameterAccessor(template.getConverter(), accessor));
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
* @see
|
||||
* org.springframework.data.repository.query.RepositoryQuery#execute(java
|
||||
* .lang.Object[])
|
||||
*/
|
||||
public Object execute(Object[] parameters) {
|
||||
switch (method.getType()) {
|
||||
case COLLECTION:
|
||||
return new CollectionExecution().execute(query);
|
||||
case PAGING:
|
||||
return new PagedExecution(accessor.getPageable()).execute(query);
|
||||
default:
|
||||
return new SingleEntityExecution().execute(query);
|
||||
}
|
||||
}
|
||||
|
||||
ParameterAccessor accessor =
|
||||
new ParametersParameterAccessor(method.getParameters(), parameters);
|
||||
Query query = createQuery(new ConvertingParameterAccessor(template.getConverter(), accessor));
|
||||
/**
|
||||
* Create a {@link Query} instance using the given {@link ParameterAccessor}
|
||||
* @param accessor
|
||||
* @param converter
|
||||
* @return
|
||||
*/
|
||||
protected abstract Query createQuery(ConvertingParameterAccessor accessor);
|
||||
|
||||
if (method.isCollectionQuery()) {
|
||||
return new CollectionExecution().execute(query);
|
||||
} else if (method.isPageQuery()) {
|
||||
return new PagedExecution(accessor.getPageable()).execute(query);
|
||||
} else {
|
||||
return new SingleEntityExecution().execute(query);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a {@link Query} instance using the given {@link ParameterAccessor}
|
||||
* @param accessor
|
||||
* @param converter
|
||||
* @return
|
||||
*/
|
||||
protected abstract Query createQuery(ConvertingParameterAccessor accessor);
|
||||
|
||||
private abstract class Execution {
|
||||
|
||||
private abstract class Execution {
|
||||
abstract Object execute(Query query);
|
||||
|
||||
abstract Object execute(Query query);
|
||||
protected List<?> readCollection(Query query) {
|
||||
|
||||
MongoEntityInformation<?> metadata = method.getEntityMetadata();
|
||||
|
||||
protected List<?> readCollection(Query query) {
|
||||
String collectionName = metadata.getCollectionName();
|
||||
return template.find(collectionName, query, metadata.getJavaType());
|
||||
}
|
||||
}
|
||||
|
||||
String collectionName = getCollectionName(method.getDomainClass());
|
||||
return template
|
||||
.find(collectionName, query, method.getReturnedDomainClass());
|
||||
}
|
||||
}
|
||||
/**
|
||||
* {@link Execution} for collection returning queries.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
class CollectionExecution extends Execution {
|
||||
|
||||
/**
|
||||
* {@link Execution} for collection returning queries.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
class CollectionExecution extends Execution {
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
* @see org.springframework.data.document.mongodb.repository.MongoQuery.Execution #execute(com.mongodb.DBObject)
|
||||
*/
|
||||
@Override
|
||||
public Object execute(Query query) {
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
* @see
|
||||
* org.springframework.data.document.mongodb.repository.MongoQuery.Execution
|
||||
* #execute(com.mongodb.DBObject)
|
||||
*/
|
||||
@Override
|
||||
public Object execute(Query query) {
|
||||
return readCollection(query);
|
||||
}
|
||||
}
|
||||
|
||||
return readCollection(query);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* {@link Execution} for pagination queries.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
class PagedExecution extends Execution {
|
||||
|
||||
/**
|
||||
* {@link Execution} for pagination queries.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
class PagedExecution extends Execution {
|
||||
private final Pageable pageable;
|
||||
|
||||
private final Pageable pageable;
|
||||
/**
|
||||
* Creates a new {@link PagedExecution}.
|
||||
*
|
||||
* @param pageable
|
||||
*/
|
||||
public PagedExecution(Pageable pageable) {
|
||||
|
||||
Assert.notNull(pageable);
|
||||
this.pageable = pageable;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new {@link PagedExecution}.
|
||||
*
|
||||
* @param pageable
|
||||
*/
|
||||
public PagedExecution(Pageable pageable) {
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
* @see org.springframework.data.document.mongodb.repository.MongoQuery.Execution #execute(com.mongodb.DBObject)
|
||||
*/
|
||||
@Override
|
||||
@SuppressWarnings({ "rawtypes", "unchecked" })
|
||||
Object execute(Query query) {
|
||||
|
||||
Assert.notNull(pageable);
|
||||
this.pageable = pageable;
|
||||
}
|
||||
MongoEntityInformation<?> metadata = method.getEntityMetadata();
|
||||
int count = getCollectionCursor(metadata.getCollectionName(), query.getQueryObject()).count();
|
||||
|
||||
List<?> result = template.find(metadata.getCollectionName(), applyPagination(query, pageable),
|
||||
metadata.getJavaType());
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
* @see
|
||||
* org.springframework.data.document.mongodb.repository.MongoQuery.Execution
|
||||
* #execute(com.mongodb.DBObject)
|
||||
*/
|
||||
@Override
|
||||
@SuppressWarnings({ "rawtypes", "unchecked" })
|
||||
Object execute(Query query) {
|
||||
return new PageImpl(result, pageable, count);
|
||||
}
|
||||
|
||||
String collectionName = getCollectionName(method.getDomainClass());
|
||||
int count = getCollectionCursor(collectionName, query.getQueryObject()).count();
|
||||
private DBCursor getCollectionCursor(String collectionName, final DBObject query) {
|
||||
|
||||
List<?> result =
|
||||
template.find(collectionName, applyPagination(query, pageable),
|
||||
method.getReturnedDomainClass());
|
||||
return template.execute(collectionName, new CollectionCallback<DBCursor>() {
|
||||
|
||||
return new PageImpl(result, pageable, count);
|
||||
}
|
||||
public DBCursor doInCollection(DBCollection collection) {
|
||||
|
||||
return collection.find(query);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
private DBCursor getCollectionCursor(String collectionName, final DBObject query) {
|
||||
/**
|
||||
* {@link Execution} to return a single entity.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
class SingleEntityExecution extends Execution {
|
||||
|
||||
return template.execute(collectionName, new CollectionCallback<DBCursor>() {
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
* @see org.springframework.data.document.mongodb.repository.MongoQuery.Execution #execute(com.mongodb.DBObject)
|
||||
*/
|
||||
@Override
|
||||
Object execute(Query query) {
|
||||
|
||||
public DBCursor doInCollection(DBCollection collection) {
|
||||
|
||||
return collection.find(query);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@link Execution} to return a single entity.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
class SingleEntityExecution extends Execution {
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
* @see
|
||||
* org.springframework.data.document.mongodb.repository.MongoQuery.Execution
|
||||
* #execute(com.mongodb.DBObject)
|
||||
*/
|
||||
@Override
|
||||
Object execute(Query query) {
|
||||
|
||||
List<?> result = readCollection(query);
|
||||
return result.isEmpty() ? null : result.get(0);
|
||||
}
|
||||
}
|
||||
List<?> result = readCollection(query);
|
||||
return result.isEmpty() ? null : result.get(0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,8 +19,9 @@ import java.lang.reflect.Field;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.data.repository.support.AbstractEntityMetadata;
|
||||
import org.springframework.data.repository.support.AbstractEntityInformation;
|
||||
import org.springframework.util.ReflectionUtils;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
|
||||
/**
|
||||
@@ -29,18 +30,18 @@ import org.springframework.util.ReflectionUtils;
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
class MongoEntityMetadata<T extends Object> extends AbstractEntityMetadata<T> {
|
||||
class MongoEntityInformation<T extends Object> extends AbstractEntityInformation<T> {
|
||||
|
||||
private static final List<String> FIELD_NAMES = Arrays.asList("ID", "id", "_id");
|
||||
private Field field;
|
||||
|
||||
|
||||
/**
|
||||
* Creates a new {@link MongoEntityMetadata}.
|
||||
* Creates a new {@link MongoEntityInformation}.
|
||||
*
|
||||
* @param domainClass
|
||||
*/
|
||||
public MongoEntityMetadata(Class<T> domainClass) {
|
||||
public MongoEntityInformation(Class<T> domainClass) {
|
||||
|
||||
super(domainClass);
|
||||
|
||||
@@ -61,16 +62,16 @@ class MongoEntityMetadata<T extends Object> extends AbstractEntityMetadata<T> {
|
||||
domainClass.getName()));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the actual field name containing the id.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public String getFieldName() {
|
||||
|
||||
return field.getName();
|
||||
|
||||
|
||||
public String getCollectionName() {
|
||||
|
||||
return StringUtils.uncapitalize(getJavaType().getSimpleName());
|
||||
}
|
||||
|
||||
public String getIdAttribute() {
|
||||
|
||||
return "_id";
|
||||
}
|
||||
|
||||
|
||||
@@ -31,7 +31,7 @@ import org.springframework.util.StringUtils;
|
||||
class MongoQueryMethod extends QueryMethod {
|
||||
|
||||
private final Method method;
|
||||
private final Class<?> domainClass;
|
||||
private final MongoEntityInformation<?> entityInformation;
|
||||
|
||||
/**
|
||||
* Creates a new {@link MongoQueryMethod} from the given {@link Method}.
|
||||
@@ -41,27 +41,9 @@ class MongoQueryMethod extends QueryMethod {
|
||||
public MongoQueryMethod(Method method, Class<?> domainClass) {
|
||||
super(method);
|
||||
this.method = method;
|
||||
this.domainClass = domainClass;
|
||||
this.entityInformation = new MongoEntityInformation(ClassUtils.getReturnedDomainClass(method));
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.data.repository.query.QueryMethod#getDomainClass()
|
||||
*/
|
||||
@Override
|
||||
public Class<?> getDomainClass() {
|
||||
return this.domainClass;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the type that will be returned by the query method.
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public Class<?> getReturnedDomainClass() {
|
||||
return ClassUtils.getReturnedDomainClass(method);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Returns whether the method has an annotated query.
|
||||
@@ -95,6 +77,15 @@ class MongoQueryMethod extends QueryMethod {
|
||||
return StringUtils.hasText(value) ? value : null;
|
||||
}
|
||||
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.data.repository.query.QueryMethod#getEntityMetadata()
|
||||
*/
|
||||
@Override
|
||||
public MongoEntityInformation<?> getEntityMetadata() {
|
||||
|
||||
return entityInformation;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the {@link Query} annotation that is applied to the method or {@code null} if none available.
|
||||
|
||||
@@ -18,12 +18,20 @@ package org.springframework.data.document.mongodb.repository;
|
||||
import java.io.Serializable;
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.data.document.mongodb.MongoOperations;
|
||||
import org.springframework.data.document.mongodb.MongoPropertyDescriptors.MongoPropertyDescriptor;
|
||||
import org.springframework.data.document.mongodb.MongoTemplate;
|
||||
import org.springframework.data.document.mongodb.query.Index;
|
||||
import org.springframework.data.document.mongodb.query.Order;
|
||||
import org.springframework.data.domain.Sort;
|
||||
import org.springframework.data.repository.query.QueryLookupStrategy;
|
||||
import org.springframework.data.repository.query.QueryLookupStrategy.Key;
|
||||
import org.springframework.data.repository.query.RepositoryQuery;
|
||||
import org.springframework.data.repository.support.EntityMetadata;
|
||||
import org.springframework.data.repository.query.parser.Part;
|
||||
import org.springframework.data.repository.query.parser.PartTree;
|
||||
import org.springframework.data.repository.support.QueryCreationListener;
|
||||
import org.springframework.data.repository.support.RepositoryFactoryBeanSupport;
|
||||
import org.springframework.data.repository.support.RepositoryFactorySupport;
|
||||
import org.springframework.data.repository.support.RepositoryMetadata;
|
||||
@@ -57,7 +65,9 @@ public class MongoRepositoryFactoryBean extends RepositoryFactoryBeanSupport<Mon
|
||||
@Override
|
||||
protected RepositoryFactorySupport createRepositoryFactory() {
|
||||
|
||||
return new MongoRepositoryFactory(template);
|
||||
MongoRepositoryFactory factory = new MongoRepositoryFactory(template);
|
||||
factory.addQueryCreationListener(new IndexEnsuringQueryCreationListener(template));
|
||||
return factory;
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -95,7 +105,8 @@ public class MongoRepositoryFactoryBean extends RepositoryFactoryBeanSupport<Mon
|
||||
@SuppressWarnings("unchecked")
|
||||
protected Object getTargetRepository(RepositoryMetadata metadata) {
|
||||
|
||||
EntityMetadata<Object> info = new MongoEntityMetadata<Object>((Class<Object>) metadata.getDomainClass());
|
||||
MongoEntityInformation<Object> info = new MongoEntityInformation<Object>(
|
||||
(Class<Object>) metadata.getDomainClass());
|
||||
return new SimpleMongoRepository<Object, Serializable>(info, template);
|
||||
}
|
||||
|
||||
@@ -152,4 +163,55 @@ public class MongoRepositoryFactoryBean extends RepositoryFactoryBeanSupport<Mon
|
||||
super.validate(metadata, customImplementation);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@link QueryCreationListener} inspecting {@link PartTreeMongoQuery}s and creating an index for the properties it
|
||||
* refers to.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
private static class IndexEnsuringQueryCreationListener implements QueryCreationListener<PartTreeMongoQuery> {
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(IndexEnsuringQueryCreationListener.class);
|
||||
private final MongoOperations operations;
|
||||
|
||||
public IndexEnsuringQueryCreationListener(MongoOperations operations) {
|
||||
this.operations = operations;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
* @see
|
||||
* org.springframework.data.repository.support.QueryCreationListener#onCreation(org.springframework.data.repository
|
||||
* .query.RepositoryQuery)
|
||||
*/
|
||||
public void onCreation(PartTreeMongoQuery query) {
|
||||
|
||||
PartTree tree = query.getTree();
|
||||
Index index = new Index();
|
||||
index.named(query.getQueryMethod().getName());
|
||||
Sort sort = tree.getSort();
|
||||
|
||||
for (Part part : tree.getParts()) {
|
||||
String property = part.getProperty().toDotPath();
|
||||
Order order = toOrder(sort, property);
|
||||
index.on(property, order);
|
||||
}
|
||||
|
||||
MongoEntityInformation<?> metadata = query.getQueryMethod().getEntityMetadata();
|
||||
operations.ensureIndex(metadata.getCollectionName(), index);
|
||||
LOG.debug(String.format("Created index %s!", index.toString()));
|
||||
}
|
||||
|
||||
private static Order toOrder(Sort sort, String property) {
|
||||
|
||||
if (sort == null) {
|
||||
return Order.DESCENDING;
|
||||
}
|
||||
|
||||
org.springframework.data.domain.Sort.Order order = sort.getOrderFor(property);
|
||||
return order == null ? Order.DESCENDING : order.isAscending() ? Order.ASCENDING : Order.DESCENDING;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -39,7 +39,14 @@ public class PartTreeMongoQuery extends AbstractMongoQuery {
|
||||
public PartTreeMongoQuery(MongoQueryMethod method, MongoTemplate template) {
|
||||
|
||||
super(method, template);
|
||||
this.tree = new PartTree(method.getName(), method.getDomainClass());
|
||||
this.tree = new PartTree(method.getName(), method.getEntityMetadata().getJavaType());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the tree
|
||||
*/
|
||||
public PartTree getTree() {
|
||||
return tree;
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
@@ -19,7 +19,6 @@ import org.springframework.data.document.mongodb.query.Query;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.domain.Sort;
|
||||
import org.springframework.data.domain.Sort.Order;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import com.mongodb.DBCursor;
|
||||
|
||||
@@ -84,10 +83,4 @@ abstract class QueryUtils {
|
||||
|
||||
return query;
|
||||
}
|
||||
|
||||
|
||||
public static String getCollectionName(Class<?> domainClass) {
|
||||
|
||||
return StringUtils.uncapitalize(domainClass.getSimpleName());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,25 +16,23 @@
|
||||
package org.springframework.data.document.mongodb.repository;
|
||||
|
||||
import static org.springframework.data.document.mongodb.query.Criteria.*;
|
||||
import static org.springframework.data.document.mongodb.repository.QueryUtils.*;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import org.bson.types.ObjectId;
|
||||
import org.springframework.data.document.mongodb.MongoConverter;
|
||||
import org.springframework.data.document.mongodb.MongoTemplate;
|
||||
import org.springframework.data.document.mongodb.query.Criteria;
|
||||
import org.springframework.data.document.mongodb.query.Query;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.PageImpl;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.domain.Sort;
|
||||
import org.springframework.data.repository.PagingAndSortingRepository;
|
||||
import org.springframework.data.repository.support.EntityMetadata;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
|
||||
/**
|
||||
* Repository base implementation for Mongo.
|
||||
*
|
||||
@@ -43,190 +41,181 @@ import org.springframework.util.Assert;
|
||||
public class SimpleMongoRepository<T, ID extends Serializable> implements PagingAndSortingRepository<T, ID> {
|
||||
|
||||
private final MongoTemplate template;
|
||||
private final EntityMetadata<T> entityInformation;
|
||||
private final MongoEntityInformation<T> entityInformation;
|
||||
|
||||
/**
|
||||
* Creates a ew {@link SimpleMongoRepository} for the given {@link MongoInformation} and {@link MongoTemplate}.
|
||||
*
|
||||
* @param metadata
|
||||
* @param template
|
||||
*/
|
||||
public SimpleMongoRepository(MongoEntityInformation<T> metadata, MongoTemplate template) {
|
||||
|
||||
Assert.notNull(template);
|
||||
Assert.notNull(metadata);
|
||||
this.entityInformation = metadata;
|
||||
this.template = template;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
* @see org.springframework.data.repository.Repository#save(java.lang.Object)
|
||||
*/
|
||||
public T save(T entity) {
|
||||
|
||||
template.save(entityInformation.getCollectionName(), entity);
|
||||
return entity;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
* @see org.springframework.data.repository.Repository#save(java.lang.Iterable)
|
||||
*/
|
||||
public List<T> save(Iterable<? extends T> entities) {
|
||||
|
||||
List<T> result = new ArrayList<T>();
|
||||
|
||||
for (T entity : entities) {
|
||||
save(entity);
|
||||
result.add(entity);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
* @see org.springframework.data.repository.Repository#findById(java.io.Serializable )
|
||||
*/
|
||||
public T findById(ID id) {
|
||||
|
||||
return template.findOne(entityInformation.getCollectionName(), getIdQuery(id), entityInformation.getJavaType());
|
||||
}
|
||||
|
||||
private Query getIdQuery(Object id) {
|
||||
|
||||
return new Query(getIdCriteria(id));
|
||||
}
|
||||
|
||||
private Criteria getIdCriteria(Object id) {
|
||||
ObjectId objectId = template.getConverter().convertObjectId(id);
|
||||
return where(entityInformation.getIdAttribute()).is(objectId);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Creates a ew {@link SimpleMongoRepository} for the given domain class and
|
||||
* {@link MongoTemplate}.
|
||||
*
|
||||
* @param domainClass
|
||||
* @param template
|
||||
*/
|
||||
public SimpleMongoRepository(EntityMetadata<T> entityInformation, MongoTemplate template) {
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
* @see org.springframework.data.repository.Repository#exists(java.io.Serializable )
|
||||
*/
|
||||
public boolean exists(ID id) {
|
||||
|
||||
Assert.notNull(entityInformation);
|
||||
Assert.notNull(template);
|
||||
this.entityInformation = entityInformation;
|
||||
this.template = template;
|
||||
}
|
||||
|
||||
private Class<T> getDomainClass() {
|
||||
return entityInformation.getJavaType();
|
||||
}
|
||||
return findById(id) != null;
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
* @see org.springframework.data.repository.Repository#count()
|
||||
*/
|
||||
public Long count() {
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
* @see
|
||||
* org.springframework.data.repository.Repository#save(java.lang.Object)
|
||||
*/
|
||||
public T save(T entity) {
|
||||
return template.getCollection(entityInformation.getCollectionName()).count();
|
||||
}
|
||||
|
||||
template.save(getCollectionName(getDomainClass()), entity);
|
||||
return entity;
|
||||
}
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
* @see org.springframework.data.repository.Repository#delete(java.lang.Object)
|
||||
*/
|
||||
public void delete(T entity) {
|
||||
|
||||
template.remove(entityInformation.getCollectionName(), getIdQuery(entityInformation.getId(entity)));
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
* @see
|
||||
* org.springframework.data.repository.Repository#save(java.lang.Iterable)
|
||||
*/
|
||||
public List<T> save(Iterable<? extends T> entities) {
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
* @see org.springframework.data.repository.Repository#delete(java.lang.Iterable)
|
||||
*/
|
||||
public void delete(Iterable<? extends T> entities) {
|
||||
|
||||
List<T> result = new ArrayList<T>();
|
||||
for (T entity : entities) {
|
||||
delete(entity);
|
||||
}
|
||||
}
|
||||
|
||||
for (T entity : entities) {
|
||||
save(entity);
|
||||
result.add(entity);
|
||||
}
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
* @see org.springframework.data.repository.Repository#deleteAll()
|
||||
*/
|
||||
public void deleteAll() {
|
||||
|
||||
return result;
|
||||
}
|
||||
template.dropCollection(entityInformation.getCollectionName());
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see org.springframework.data.repository.Repository#findAll()
|
||||
*/
|
||||
public List<T> findAll() {
|
||||
return findAll(new Query());
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
* @see org.springframework.data.repository.PagingAndSortingRepository#findAll
|
||||
* (org.springframework.data.domain.Pageable)
|
||||
*/
|
||||
public Page<T> findAll(final Pageable pageable) {
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
* @see
|
||||
* org.springframework.data.repository.Repository#findById(java.io.Serializable
|
||||
* )
|
||||
*/
|
||||
public T findById(ID id) {
|
||||
Long count = count();
|
||||
List<T> list = findAll(QueryUtils.applyPagination(new Query(), pageable));
|
||||
|
||||
MongoConverter converter = template.getConverter();
|
||||
ObjectId objectId = converter.convertObjectId(id);
|
||||
return new PageImpl<T>(list, pageable, count);
|
||||
}
|
||||
|
||||
return template.findOne(getCollectionName(getDomainClass()), new Query(
|
||||
where("_id").is(objectId)), getDomainClass());
|
||||
}
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
* @see org.springframework.data.repository.PagingAndSortingRepository#findAll
|
||||
* (org.springframework.data.domain.Sort)
|
||||
*/
|
||||
public List<T> findAll(final Sort sort) {
|
||||
|
||||
return findAll(QueryUtils.applySorting(new Query(), sort));
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
* @see
|
||||
* org.springframework.data.repository.Repository#exists(java.io.Serializable
|
||||
* )
|
||||
*/
|
||||
public boolean exists(ID id) {
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
* @see org.springframework.data.repository.Repository#findAll(java.lang.Iterable)
|
||||
*/
|
||||
public List<T> findAll(Iterable<ID> ids) {
|
||||
|
||||
return findById(id) != null;
|
||||
}
|
||||
Query query = null;
|
||||
|
||||
for (ID id : ids) {
|
||||
if (query == null) {
|
||||
query = getIdQuery(id);
|
||||
} else {
|
||||
query = new Query().or(getIdQuery(id));
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
* @see org.springframework.data.repository.Repository#findAll()
|
||||
*/
|
||||
public List<T> findAll() {
|
||||
return findAll(query);
|
||||
}
|
||||
|
||||
return template.getCollection(getCollectionName(getDomainClass()),
|
||||
getDomainClass());
|
||||
}
|
||||
private List<T> findAll(Query query) {
|
||||
|
||||
if (query == null) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
* @see org.springframework.data.repository.Repository#count()
|
||||
*/
|
||||
public Long count() {
|
||||
|
||||
return template.getCollection(getCollectionName(getDomainClass()))
|
||||
.count();
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
* @see
|
||||
* org.springframework.data.repository.Repository#delete(java.lang.Object)
|
||||
*/
|
||||
public void delete(T entity) {
|
||||
|
||||
Object id = entityInformation.getId(entity);
|
||||
ObjectId objectId = template.getConverter().convertObjectId(id);
|
||||
|
||||
Query query =
|
||||
new Query(where("_id").is(
|
||||
objectId));
|
||||
template.remove(getCollectionName(getDomainClass()), query);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
* @see
|
||||
* org.springframework.data.repository.Repository#delete(java.lang.Iterable)
|
||||
*/
|
||||
public void delete(Iterable<? extends T> entities) {
|
||||
|
||||
for (T entity : entities) {
|
||||
delete(entity);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
* @see org.springframework.data.repository.Repository#deleteAll()
|
||||
*/
|
||||
public void deleteAll() {
|
||||
|
||||
template.dropCollection(getCollectionName(getDomainClass()));
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
* @see
|
||||
* org.springframework.data.repository.PagingAndSortingRepository#findAll
|
||||
* (org.springframework.data.domain.Pageable)
|
||||
*/
|
||||
public Page<T> findAll(final Pageable pageable) {
|
||||
|
||||
Long count = count();
|
||||
Query spec = new Query();
|
||||
|
||||
List<T> list =
|
||||
template.find(getCollectionName(getDomainClass()),
|
||||
QueryUtils.applyPagination(spec, pageable),
|
||||
getDomainClass());
|
||||
|
||||
return new PageImpl<T>(list, pageable, count);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* (non-Javadoc)
|
||||
*
|
||||
* @see
|
||||
* org.springframework.data.repository.PagingAndSortingRepository#findAll
|
||||
* (org.springframework.data.domain.Sort)
|
||||
*/
|
||||
public List<T> findAll(final Sort sort) {
|
||||
|
||||
Query query = QueryUtils.applySorting(new Query(), sort);
|
||||
return template.find(getCollectionName(getDomainClass()), query,
|
||||
getDomainClass());
|
||||
}
|
||||
return template.find(entityInformation.getCollectionName(), query, entityInformation.getJavaType());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,7 +22,7 @@ import org.junit.Test;
|
||||
|
||||
|
||||
/**
|
||||
* Unit test for {@link MongoEntityMetadata}.
|
||||
* Unit test for {@link MongoEntityInformation}.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
@@ -31,8 +31,8 @@ public class MongoEntityMetadataUnitTests {
|
||||
@Test
|
||||
public void findsIdField() throws Exception {
|
||||
|
||||
MongoEntityMetadata<Person> isNewAware =
|
||||
new MongoEntityMetadata<Person>(Person.class);
|
||||
MongoEntityInformation<Person> isNewAware =
|
||||
new MongoEntityInformation<Person>(Person.class);
|
||||
|
||||
Person person = new Person();
|
||||
assertThat(isNewAware.isNew(person), is(true));
|
||||
@@ -44,7 +44,7 @@ public class MongoEntityMetadataUnitTests {
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void rejectsClassIfNoIdField() throws Exception {
|
||||
|
||||
new MongoEntityMetadata<InvalidPerson>(InvalidPerson.class);
|
||||
new MongoEntityInformation<InvalidPerson>(InvalidPerson.class);
|
||||
}
|
||||
|
||||
class Person {
|
||||
|
||||
Reference in New Issue
Block a user