Browse Source

DATAMONGO-835 - Code cleanups.

pull/112/head
Oliver Gierke 12 years ago
parent
commit
af4b84ea43
  1. 12
      spring-data-mongodb-cross-store/src/main/java/org/springframework/data/mongodb/crossstore/MongoChangeSetPersister.java
  2. 11
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/config/MappingMongoConverterParser.java
  3. 28
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoAdminOperations.java
  4. 69
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoOptionsFactoryBean.java
  5. 12
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoTemplate.java
  6. 4
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/ExposedFields.java
  7. 10
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/Fields.java
  8. 4
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/GroupOperation.java
  9. 25
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/ProjectionOperation.java
  10. 18
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/DBObjectPropertyAccessor.java
  11. 6
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MappingMongoConverter.java
  12. 24
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/MongoPersistentEntityIndexCreator.java
  13. 4
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapreduce/MapReduceCounts.java
  14. 16
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/Criteria.java
  15. 4
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/NearQuery.java
  16. 21
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/Query.java
  17. 10
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/query/AbstractMongoQuery.java

12
spring-data-mongodb-cross-store/src/main/java/org/springframework/data/mongodb/crossstore/MongoChangeSetPersister.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2011-2013 the original author or authors. * Copyright 2011-2014 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -41,17 +41,13 @@ import com.mongodb.MongoException;
public class MongoChangeSetPersister implements ChangeSetPersister<Object> { public class MongoChangeSetPersister implements ChangeSetPersister<Object> {
private static final String ENTITY_CLASS = "_entity_class"; private static final String ENTITY_CLASS = "_entity_class";
private static final String ENTITY_ID = "_entity_id"; private static final String ENTITY_ID = "_entity_id";
private static final String ENTITY_FIELD_NAME = "_entity_field_name"; private static final String ENTITY_FIELD_NAME = "_entity_field_name";
private static final String ENTITY_FIELD_CLASS = "_entity_field_class"; private static final String ENTITY_FIELD_CLASS = "_entity_field_class";
protected final Logger log = LoggerFactory.getLogger(getClass()); protected final Logger log = LoggerFactory.getLogger(getClass());
private MongoTemplate mongoTemplate; private MongoTemplate mongoTemplate;
private EntityManagerFactory entityManagerFactory; private EntityManagerFactory entityManagerFactory;
public void setMongoTemplate(MongoTemplate mongoTemplate) { public void setMongoTemplate(MongoTemplate mongoTemplate) {
@ -113,12 +109,14 @@ public class MongoChangeSetPersister implements ChangeSetPersister<Object> {
* @see org.springframework.data.crossstore.ChangeSetPersister#getPersistentId(org.springframework.data.crossstore.ChangeSetBacked, org.springframework.data.crossstore.ChangeSet) * @see org.springframework.data.crossstore.ChangeSetPersister#getPersistentId(org.springframework.data.crossstore.ChangeSetBacked, org.springframework.data.crossstore.ChangeSet)
*/ */
public Object getPersistentId(ChangeSetBacked entity, ChangeSet cs) throws DataAccessException { public Object getPersistentId(ChangeSetBacked entity, ChangeSet cs) throws DataAccessException {
log.debug("getPersistentId called on " + entity); log.debug("getPersistentId called on " + entity);
if (entityManagerFactory == null) { if (entityManagerFactory == null) {
throw new DataAccessResourceFailureException("EntityManagerFactory cannot be null"); throw new DataAccessResourceFailureException("EntityManagerFactory cannot be null");
} }
Object o = entityManagerFactory.getPersistenceUnitUtil().getIdentifier(entity);
return o; return entityManagerFactory.getPersistenceUnitUtil().getIdentifier(entity);
} }
/* /*

11
spring-data-mongodb/src/main/java/org/springframework/data/mongodb/config/MappingMongoConverterParser.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2011-2013 the original author or authors. * Copyright 2011-2014 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -75,7 +75,7 @@ import org.w3c.dom.Element;
public class MappingMongoConverterParser implements BeanDefinitionParser { public class MappingMongoConverterParser implements BeanDefinitionParser {
private static final String BASE_PACKAGE = "base-package"; private static final String BASE_PACKAGE = "base-package";
private static final boolean jsr303Present = ClassUtils.isPresent("javax.validation.Validator", private static final boolean JSR_303_PRESENT = ClassUtils.isPresent("javax.validation.Validator",
MappingMongoConverterParser.class.getClassLoader()); MappingMongoConverterParser.class.getClassLoader());
/* (non-Javadoc) /* (non-Javadoc)
@ -166,7 +166,7 @@ public class MappingMongoConverterParser implements BeanDefinitionParser {
private RuntimeBeanReference getValidator(Object source, ParserContext parserContext) { private RuntimeBeanReference getValidator(Object source, ParserContext parserContext) {
if (!jsr303Present) { if (!JSR_303_PRESENT) {
return null; return null;
} }
@ -195,7 +195,8 @@ public class MappingMongoConverterParser implements BeanDefinitionParser {
BeanDefinitionBuilder mappingContextBuilder = BeanDefinitionBuilder BeanDefinitionBuilder mappingContextBuilder = BeanDefinitionBuilder
.genericBeanDefinition(MongoMappingContext.class); .genericBeanDefinition(MongoMappingContext.class);
Set<String> classesToAdd = getInititalEntityClasses(element, mappingContextBuilder); Set<String> classesToAdd = getInititalEntityClasses(element);
if (classesToAdd != null) { if (classesToAdd != null) {
mappingContextBuilder.addPropertyValue("initialEntitySet", classesToAdd); mappingContextBuilder.addPropertyValue("initialEntitySet", classesToAdd);
} }
@ -262,7 +263,7 @@ public class MappingMongoConverterParser implements BeanDefinitionParser {
return null; return null;
} }
private static Set<String> getInititalEntityClasses(Element element, BeanDefinitionBuilder builder) { private static Set<String> getInititalEntityClasses(Element element) {
String basePackage = element.getAttribute(BASE_PACKAGE); String basePackage = element.getAttribute(BASE_PACKAGE);

28
spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoAdminOperations.java

@ -1,16 +1,34 @@
/*
* Copyright 2011-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.mongodb.core; package org.springframework.data.mongodb.core;
import org.springframework.jmx.export.annotation.ManagedOperation; import org.springframework.jmx.export.annotation.ManagedOperation;
/**
* @author Mark Pollack
* @author Oliver Gierke
*/
public interface MongoAdminOperations { public interface MongoAdminOperations {
@ManagedOperation @ManagedOperation
public abstract void dropDatabase(String databaseName); void dropDatabase(String databaseName);
@ManagedOperation @ManagedOperation
public abstract void createDatabase(String databaseName); void createDatabase(String databaseName);
@ManagedOperation @ManagedOperation
public abstract String getDatabaseStats(String databaseName); String getDatabaseStats(String databaseName);
}
}

69
spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoOptionsFactoryBean.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2010-2013 the original author or authors. * Copyright 2010-2014 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -30,27 +30,28 @@ import com.mongodb.MongoOptions;
* @author Mike Saavedra * @author Mike Saavedra
* @author Thomas Darimont * @author Thomas Darimont
*/ */
@SuppressWarnings("deprecation")
public class MongoOptionsFactoryBean implements FactoryBean<MongoOptions>, InitializingBean { public class MongoOptionsFactoryBean implements FactoryBean<MongoOptions>, InitializingBean {
@SuppressWarnings("deprecation")// private static final MongoOptions DEFAULT_MONGO_OPTIONS = new MongoOptions();
private final MongoOptions MONGO_OPTIONS = new MongoOptions();
private int connectionsPerHost = DEFAULT_MONGO_OPTIONS.connectionsPerHost;
private int connectionsPerHost = MONGO_OPTIONS.connectionsPerHost; private int threadsAllowedToBlockForConnectionMultiplier = DEFAULT_MONGO_OPTIONS.threadsAllowedToBlockForConnectionMultiplier;
private int maxWaitTime = DEFAULT_MONGO_OPTIONS.maxWaitTime;
private int threadsAllowedToBlockForConnectionMultiplier = MONGO_OPTIONS.threadsAllowedToBlockForConnectionMultiplier; private int connectTimeout = DEFAULT_MONGO_OPTIONS.connectTimeout;
private int maxWaitTime = MONGO_OPTIONS.maxWaitTime; private int socketTimeout = DEFAULT_MONGO_OPTIONS.socketTimeout;
private int connectTimeout = MONGO_OPTIONS.connectTimeout; private boolean socketKeepAlive = DEFAULT_MONGO_OPTIONS.socketKeepAlive;
private int socketTimeout = MONGO_OPTIONS.socketTimeout; private boolean autoConnectRetry = DEFAULT_MONGO_OPTIONS.autoConnectRetry;
private boolean socketKeepAlive = MONGO_OPTIONS.socketKeepAlive; private long maxAutoConnectRetryTime = DEFAULT_MONGO_OPTIONS.maxAutoConnectRetryTime;
private boolean autoConnectRetry = MONGO_OPTIONS.autoConnectRetry; private int writeNumber = DEFAULT_MONGO_OPTIONS.w;
private long maxAutoConnectRetryTime = MONGO_OPTIONS.maxAutoConnectRetryTime; private int writeTimeout = DEFAULT_MONGO_OPTIONS.wtimeout;
private int writeNumber; private boolean writeFsync = DEFAULT_MONGO_OPTIONS.fsync;
private int writeTimeout; private boolean slaveOk = DEFAULT_MONGO_OPTIONS.slaveOk;
private boolean writeFsync;
@SuppressWarnings("deprecation") private boolean slaveOk = MONGO_OPTIONS.slaveOk;
private boolean ssl; private boolean ssl;
private SSLSocketFactory sslSocketFactory; private SSLSocketFactory sslSocketFactory;
private MongoOptions options;
/** /**
* Configures the maximum number of connections allowed per host until we will block. * Configures the maximum number of connections allowed per host until we will block.
* *
@ -197,24 +198,28 @@ public class MongoOptionsFactoryBean implements FactoryBean<MongoOptions>, Initi
* (non-Javadoc) * (non-Javadoc)
* @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet() * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet()
*/ */
@SuppressWarnings("deprecation")
public void afterPropertiesSet() { public void afterPropertiesSet() {
MONGO_OPTIONS.connectionsPerHost = connectionsPerHost; MongoOptions options = new MongoOptions();
MONGO_OPTIONS.threadsAllowedToBlockForConnectionMultiplier = threadsAllowedToBlockForConnectionMultiplier;
MONGO_OPTIONS.maxWaitTime = maxWaitTime; options.connectionsPerHost = connectionsPerHost;
MONGO_OPTIONS.connectTimeout = connectTimeout; options.threadsAllowedToBlockForConnectionMultiplier = threadsAllowedToBlockForConnectionMultiplier;
MONGO_OPTIONS.socketTimeout = socketTimeout; options.maxWaitTime = maxWaitTime;
MONGO_OPTIONS.socketKeepAlive = socketKeepAlive; options.connectTimeout = connectTimeout;
MONGO_OPTIONS.autoConnectRetry = autoConnectRetry; options.socketTimeout = socketTimeout;
MONGO_OPTIONS.maxAutoConnectRetryTime = maxAutoConnectRetryTime; options.socketKeepAlive = socketKeepAlive;
MONGO_OPTIONS.slaveOk = slaveOk; options.autoConnectRetry = autoConnectRetry;
MONGO_OPTIONS.w = writeNumber; options.maxAutoConnectRetryTime = maxAutoConnectRetryTime;
MONGO_OPTIONS.wtimeout = writeTimeout; options.slaveOk = slaveOk;
MONGO_OPTIONS.fsync = writeFsync; options.w = writeNumber;
options.wtimeout = writeTimeout;
options.fsync = writeFsync;
if (ssl) { if (ssl) {
MONGO_OPTIONS.setSocketFactory(sslSocketFactory != null ? sslSocketFactory : SSLSocketFactory.getDefault()); options.setSocketFactory(sslSocketFactory != null ? sslSocketFactory : SSLSocketFactory.getDefault());
} }
this.options = options;
} }
/* /*
@ -222,7 +227,7 @@ public class MongoOptionsFactoryBean implements FactoryBean<MongoOptions>, Initi
* @see org.springframework.beans.factory.FactoryBean#getObject() * @see org.springframework.beans.factory.FactoryBean#getObject()
*/ */
public MongoOptions getObject() { public MongoOptions getObject() {
return MONGO_OPTIONS; return this.options;
} }
/* /*

12
spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/MongoTemplate.java

@ -375,7 +375,7 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
DBObject fieldsObject = query.getFieldsObject(); DBObject fieldsObject = query.getFieldsObject();
if (LOGGER.isDebugEnabled()) { if (LOGGER.isDebugEnabled()) {
LOGGER.debug(String.format("Executing query: %s sort: %s fields: %s in collection: $s", LOGGER.debug(String.format("Executing query: %s sort: %s fields: %s in collection: %s",
serializeToJsonSafely(queryObject), sortObject, fieldsObject, collectionName)); serializeToJsonSafely(queryObject), sortObject, fieldsObject, collectionName));
} }
@ -1181,6 +1181,7 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
public <T> MapReduceResults<T> mapReduce(Query query, String inputCollectionName, String mapFunction, public <T> MapReduceResults<T> mapReduce(Query query, String inputCollectionName, String mapFunction,
String reduceFunction, MapReduceOptions mapReduceOptions, Class<T> entityClass) { String reduceFunction, MapReduceOptions mapReduceOptions, Class<T> entityClass) {
String mapFunc = replaceWithResourceIfNecessary(mapFunction); String mapFunc = replaceWithResourceIfNecessary(mapFunction);
String reduceFunc = replaceWithResourceIfNecessary(reduceFunction); String reduceFunc = replaceWithResourceIfNecessary(reduceFunction);
DBCollection inputCollection = getCollection(inputCollectionName); DBCollection inputCollection = getCollection(inputCollectionName);
@ -1205,12 +1206,12 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
MapReduceOutput mapReduceOutput = new MapReduceOutput(inputCollection, commandObject, commandResult); MapReduceOutput mapReduceOutput = new MapReduceOutput(inputCollection, commandObject, commandResult);
List<T> mappedResults = new ArrayList<T>(); List<T> mappedResults = new ArrayList<T>();
DbObjectCallback<T> callback = new ReadDbObjectCallback<T>(mongoConverter, entityClass); DbObjectCallback<T> callback = new ReadDbObjectCallback<T>(mongoConverter, entityClass);
for (DBObject dbObject : mapReduceOutput.results()) { for (DBObject dbObject : mapReduceOutput.results()) {
mappedResults.add(callback.doWith(dbObject)); mappedResults.add(callback.doWith(dbObject));
} }
MapReduceResults<T> mapReduceResult = new MapReduceResults<T>(mappedResults, commandResult); return new MapReduceResults<T>(mappedResults, commandResult);
return mapReduceResult;
} }
public <T> GroupByResults<T> group(String inputCollectionName, GroupBy groupBy, Class<T> entityClass) { public <T> GroupByResults<T> group(String inputCollectionName, GroupBy groupBy, Class<T> entityClass) {
@ -1264,15 +1265,14 @@ public class MongoTemplate implements MongoOperations, ApplicationContextAware {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
Iterable<DBObject> resultSet = (Iterable<DBObject>) commandResult.get("retval"); Iterable<DBObject> resultSet = (Iterable<DBObject>) commandResult.get("retval");
List<T> mappedResults = new ArrayList<T>(); List<T> mappedResults = new ArrayList<T>();
DbObjectCallback<T> callback = new ReadDbObjectCallback<T>(mongoConverter, entityClass); DbObjectCallback<T> callback = new ReadDbObjectCallback<T>(mongoConverter, entityClass);
for (DBObject dbObject : resultSet) { for (DBObject dbObject : resultSet) {
mappedResults.add(callback.doWith(dbObject)); mappedResults.add(callback.doWith(dbObject));
} }
GroupByResults<T> groupByResult = new GroupByResults<T>(mappedResults, commandResult);
return groupByResult;
return new GroupByResults<T>(mappedResults, commandResult);
} }
@Override @Override

4
spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/ExposedFields.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2013 the original author or authors. * Copyright 2013-2014 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -32,7 +32,7 @@ import org.springframework.util.CompositeIterator;
* @author Thomas Darimont * @author Thomas Darimont
* @since 1.3 * @since 1.3
*/ */
public class ExposedFields implements Iterable<ExposedField> { public final class ExposedFields implements Iterable<ExposedField> {
private static final List<ExposedField> NO_FIELDS = Collections.emptyList(); private static final List<ExposedField> NO_FIELDS = Collections.emptyList();
private static final ExposedFields EMPTY = new ExposedFields(NO_FIELDS, NO_FIELDS); private static final ExposedFields EMPTY = new ExposedFields(NO_FIELDS, NO_FIELDS);

10
spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/Fields.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2013 the original author or authors. * Copyright 2013-2014 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -32,13 +32,13 @@ import org.springframework.util.StringUtils;
* @author Oliver Gierke * @author Oliver Gierke
* @since 1.3 * @since 1.3
*/ */
public class Fields implements Iterable<Field> { public final class Fields implements Iterable<Field> {
private static final String AMBIGUOUS_EXCEPTION = "Found two fields both using '%s' as name: %s and %s! Please " private static final String AMBIGUOUS_EXCEPTION = "Found two fields both using '%s' as name: %s and %s! Please "
+ "customize your field definitions to get to unique field names!"; + "customize your field definitions to get to unique field names!";
public static String UNDERSCORE_ID = "_id"; public static final String UNDERSCORE_ID = "_id";
public static String UNDERSCORE_ID_REF = "$_id"; public static final String UNDERSCORE_ID_REF = "$_id";
private final List<Field> fields; private final List<Field> fields;
@ -203,7 +203,7 @@ public class Fields implements Iterable<Field> {
Assert.hasText(nameToSet, "AggregationField name must not be null or empty!"); Assert.hasText(nameToSet, "AggregationField name must not be null or empty!");
if (target == null && name.contains(".")) { if (target == null && name.contains(".")) {
this.name = nameToSet.substring(nameToSet.indexOf(".") + 1); this.name = nameToSet.substring(nameToSet.indexOf('.') + 1);
this.target = nameToSet; this.target = nameToSet;
} else { } else {
this.name = nameToSet; this.name = nameToSet;

4
spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/GroupOperation.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2013 the original author or authors. * Copyright 2013-2014 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -99,7 +99,7 @@ public class GroupOperation implements FieldsExposingAggregationOperation {
* *
* @author Thomas Darimont * @author Thomas Darimont
*/ */
public class GroupOperationBuilder { public static final class GroupOperationBuilder {
private final GroupOperation groupOperation; private final GroupOperation groupOperation;
private final Operation operation; private final Operation operation;

25
spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/ProjectionOperation.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2013 the original author or authors. * Copyright 2013-2014 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -252,7 +252,7 @@ public class ProjectionOperation implements FieldsExposingAggregationOperation {
public ExpressionProjectionOperationBuilder(Object value, ProjectionOperation operation, Object[] parameters) { public ExpressionProjectionOperationBuilder(Object value, ProjectionOperation operation, Object[] parameters) {
super(value, operation); super(value, operation);
this.params = parameters; this.params = parameters.clone();
} }
/* /*
@ -294,7 +294,7 @@ public class ProjectionOperation implements FieldsExposingAggregationOperation {
Assert.notNull(parameters, "Parameters must not be null!"); Assert.notNull(parameters, "Parameters must not be null!");
this.expression = expression; this.expression = expression;
this.params = parameters; this.params = parameters.clone();
} }
/* /*
@ -316,6 +316,9 @@ public class ProjectionOperation implements FieldsExposingAggregationOperation {
*/ */
public static class ProjectionOperationBuilder extends AbstractProjectionOperationBuilder { public static class ProjectionOperationBuilder extends AbstractProjectionOperationBuilder {
private static final String NUMBER_NOT_NULL = "Number must not be null!";
private static final String FIELD_REFERENCE_NOT_NULL = "Field reference must not be null!";
private final String name; private final String name;
private final ProjectionOperation operation; private final ProjectionOperation operation;
private final OperationProjection previousProjection; private final OperationProjection previousProjection;
@ -382,7 +385,7 @@ public class ProjectionOperation implements FieldsExposingAggregationOperation {
*/ */
public ProjectionOperationBuilder plus(Number number) { public ProjectionOperationBuilder plus(Number number) {
Assert.notNull(number, "Number must not be null!"); Assert.notNull(number, NUMBER_NOT_NULL);
return project("add", number); return project("add", number);
} }
@ -419,7 +422,7 @@ public class ProjectionOperation implements FieldsExposingAggregationOperation {
*/ */
public ProjectionOperationBuilder minus(String fieldReference) { public ProjectionOperationBuilder minus(String fieldReference) {
Assert.notNull(fieldReference, "Field reference must not be null!"); Assert.notNull(fieldReference, FIELD_REFERENCE_NOT_NULL);
return project("subtract", Fields.field(fieldReference)); return project("subtract", Fields.field(fieldReference));
} }
@ -431,7 +434,7 @@ public class ProjectionOperation implements FieldsExposingAggregationOperation {
*/ */
public ProjectionOperationBuilder multiply(Number number) { public ProjectionOperationBuilder multiply(Number number) {
Assert.notNull(number, "Number must not be null!"); Assert.notNull(number, NUMBER_NOT_NULL);
return project("multiply", number); return project("multiply", number);
} }
@ -444,7 +447,7 @@ public class ProjectionOperation implements FieldsExposingAggregationOperation {
*/ */
public ProjectionOperationBuilder multiply(String fieldReference) { public ProjectionOperationBuilder multiply(String fieldReference) {
Assert.notNull(fieldReference, "Field reference must not be null!"); Assert.notNull(fieldReference, FIELD_REFERENCE_NOT_NULL);
return project("multiply", Fields.field(fieldReference)); return project("multiply", Fields.field(fieldReference));
} }
@ -456,7 +459,7 @@ public class ProjectionOperation implements FieldsExposingAggregationOperation {
*/ */
public ProjectionOperationBuilder divide(Number number) { public ProjectionOperationBuilder divide(Number number) {
Assert.notNull(number, "Number must not be null!"); Assert.notNull(number, FIELD_REFERENCE_NOT_NULL);
Assert.isTrue(Math.abs(number.intValue()) != 0, "Number must not be zero!"); Assert.isTrue(Math.abs(number.intValue()) != 0, "Number must not be zero!");
return project("divide", number); return project("divide", number);
} }
@ -470,7 +473,7 @@ public class ProjectionOperation implements FieldsExposingAggregationOperation {
*/ */
public ProjectionOperationBuilder divide(String fieldReference) { public ProjectionOperationBuilder divide(String fieldReference) {
Assert.notNull(fieldReference, "Field reference must not be null!"); Assert.notNull(fieldReference, FIELD_REFERENCE_NOT_NULL);
return project("divide", Fields.field(fieldReference)); return project("divide", Fields.field(fieldReference));
} }
@ -483,7 +486,7 @@ public class ProjectionOperation implements FieldsExposingAggregationOperation {
*/ */
public ProjectionOperationBuilder mod(Number number) { public ProjectionOperationBuilder mod(Number number) {
Assert.notNull(number, "Number must not be null!"); Assert.notNull(number, NUMBER_NOT_NULL);
Assert.isTrue(Math.abs(number.intValue()) != 0, "Number must not be zero!"); Assert.isTrue(Math.abs(number.intValue()) != 0, "Number must not be zero!");
return project("mod", number); return project("mod", number);
} }
@ -497,7 +500,7 @@ public class ProjectionOperation implements FieldsExposingAggregationOperation {
*/ */
public ProjectionOperationBuilder mod(String fieldReference) { public ProjectionOperationBuilder mod(String fieldReference) {
Assert.notNull(fieldReference, "Field reference must not be null!"); Assert.notNull(fieldReference, FIELD_REFERENCE_NOT_NULL);
return project("mod", Fields.field(fieldReference)); return project("mod", Fields.field(fieldReference));
} }

18
spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/DBObjectPropertyAccessor.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2012 the original author or authors. * Copyright 2012-2014 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -31,18 +31,30 @@ import com.mongodb.DBObject;
*/ */
class DBObjectPropertyAccessor extends MapAccessor { class DBObjectPropertyAccessor extends MapAccessor {
static MapAccessor INSTANCE = new DBObjectPropertyAccessor(); static final MapAccessor INSTANCE = new DBObjectPropertyAccessor();
/*
* (non-Javadoc)
* @see org.springframework.context.expression.MapAccessor#getSpecificTargetClasses()
*/
@Override @Override
public Class<?>[] getSpecificTargetClasses() { public Class<?>[] getSpecificTargetClasses() {
return new Class[] { DBObject.class }; return new Class[] { DBObject.class };
} }
/*
* (non-Javadoc)
* @see org.springframework.context.expression.MapAccessor#canRead(org.springframework.expression.EvaluationContext, java.lang.Object, java.lang.String)
*/
@Override @Override
public boolean canRead(EvaluationContext context, Object target, String name) { public boolean canRead(EvaluationContext context, Object target, String name) {
return true; return true;
} }
/*
* (non-Javadoc)
* @see org.springframework.context.expression.MapAccessor#read(org.springframework.expression.EvaluationContext, java.lang.Object, java.lang.String)
*/
@Override @Override
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public TypedValue read(EvaluationContext context, Object target, String name) { public TypedValue read(EvaluationContext context, Object target, String name) {
@ -52,4 +64,4 @@ class DBObjectPropertyAccessor extends MapAccessor {
Object value = source.get(name); Object value = source.get(name);
return value == null ? TypedValue.NULL : new TypedValue(value); return value == null ? TypedValue.NULL : new TypedValue(value);
} }
} }

6
spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/convert/MappingMongoConverter.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2011-2013 by the original author(s). * Copyright 2011-2014 by the original author(s).
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -74,7 +74,7 @@ import com.mongodb.DBRef;
*/ */
public class MappingMongoConverter extends AbstractMongoConverter implements ApplicationContextAware { public class MappingMongoConverter extends AbstractMongoConverter implements ApplicationContextAware {
protected static final Logger log = LoggerFactory.getLogger(MappingMongoConverter.class); protected static final Logger LOGGER = LoggerFactory.getLogger(MappingMongoConverter.class);
protected final MappingContext<? extends MongoPersistentEntity<?>, MongoPersistentProperty> mappingContext; protected final MappingContext<? extends MongoPersistentEntity<?>, MongoPersistentProperty> mappingContext;
protected final SpelExpressionParser spelExpressionParser = new SpelExpressionParser(); protected final SpelExpressionParser spelExpressionParser = new SpelExpressionParser();
@ -668,7 +668,7 @@ public class MappingMongoConverter extends AbstractMongoConverter implements App
*/ */
protected void addCustomTypeKeyIfNecessary(TypeInformation<?> type, Object value, DBObject dbObject) { protected void addCustomTypeKeyIfNecessary(TypeInformation<?> type, Object value, DBObject dbObject) {
TypeInformation<?> actualType = type != null ? type.getActualType() : type; TypeInformation<?> actualType = type != null ? type.getActualType() : null;
Class<?> reference = actualType == null ? Object.class : actualType.getType(); Class<?> reference = actualType == null ? Object.class : actualType.getType();
boolean notTheSameClass = !value.getClass().equals(reference); boolean notTheSameClass = !value.getClass().equals(reference);

24
spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/index/MongoPersistentEntityIndexCreator.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2011-2013 the original author or authors. * Copyright 2011-2014 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -50,7 +50,7 @@ import com.mongodb.util.JSON;
public class MongoPersistentEntityIndexCreator implements public class MongoPersistentEntityIndexCreator implements
ApplicationListener<MappingContextEvent<MongoPersistentEntity<?>, MongoPersistentProperty>> { ApplicationListener<MappingContextEvent<MongoPersistentEntity<?>, MongoPersistentProperty>> {
private static final Logger log = LoggerFactory.getLogger(MongoPersistentEntityIndexCreator.class); private static final Logger LOGGER = LoggerFactory.getLogger(MongoPersistentEntityIndexCreator.class);
private final Map<Class<?>, Boolean> classesSeen = new ConcurrentHashMap<Class<?>, Boolean>(); private final Map<Class<?>, Boolean> classesSeen = new ConcurrentHashMap<Class<?>, Boolean>();
private final MongoDbFactory mongoDbFactory; private final MongoDbFactory mongoDbFactory;
@ -97,8 +97,8 @@ public class MongoPersistentEntityIndexCreator implements
protected void checkForIndexes(final MongoPersistentEntity<?> entity) { protected void checkForIndexes(final MongoPersistentEntity<?> entity) {
final Class<?> type = entity.getType(); final Class<?> type = entity.getType();
if (!classesSeen.containsKey(type)) { if (!classesSeen.containsKey(type)) {
if (log.isDebugEnabled()) { if (LOGGER.isDebugEnabled()) {
log.debug("Analyzing class " + type + " for index information."); LOGGER.debug("Analyzing class " + type + " for index information.");
} }
// Make sure indexes get created // Make sure indexes get created
@ -112,8 +112,8 @@ public class MongoPersistentEntityIndexCreator implements
ensureIndex(indexColl, index.name(), definition, index.unique(), index.dropDups(), index.sparse(), ensureIndex(indexColl, index.name(), definition, index.unique(), index.dropDups(), index.sparse(),
index.background(), index.expireAfterSeconds()); index.background(), index.expireAfterSeconds());
if (log.isDebugEnabled()) { if (LOGGER.isDebugEnabled()) {
log.debug("Created compound index " + index); LOGGER.debug("Created compound index " + index);
} }
} }
} }
@ -133,8 +133,8 @@ public class MongoPersistentEntityIndexCreator implements
} else { } else {
if (!name.equals(field.getName()) && index.unique() && !index.sparse()) { if (!name.equals(field.getName()) && index.unique() && !index.sparse()) {
// Names don't match, and sparse is not true. This situation will generate an error on the server. // Names don't match, and sparse is not true. This situation will generate an error on the server.
if (log.isWarnEnabled()) { if (LOGGER.isWarnEnabled()) {
log.warn("The index name " + name + " doesn't match this property name: " + field.getName() LOGGER.warn("The index name " + name + " doesn't match this property name: " + field.getName()
+ ". Setting sparse=true on this index will prevent errors when inserting documents."); + ". Setting sparse=true on this index will prevent errors when inserting documents.");
} }
} }
@ -147,8 +147,8 @@ public class MongoPersistentEntityIndexCreator implements
ensureIndex(collection, name, definition, index.unique(), index.dropDups(), index.sparse(), ensureIndex(collection, name, definition, index.unique(), index.dropDups(), index.sparse(),
index.background(), index.expireAfterSeconds()); index.background(), index.expireAfterSeconds());
if (log.isDebugEnabled()) { if (LOGGER.isDebugEnabled()) {
log.debug("Created property index " + index); LOGGER.debug("Created property index " + index);
} }
} else if (field.isAnnotationPresent(GeoSpatialIndexed.class)) { } else if (field.isAnnotationPresent(GeoSpatialIndexed.class)) {
@ -165,8 +165,8 @@ public class MongoPersistentEntityIndexCreator implements
mongoDbFactory.getDb().getCollection(collection) mongoDbFactory.getDb().getCollection(collection)
.ensureIndex(indexObject.getIndexKeys(), indexObject.getIndexOptions()); .ensureIndex(indexObject.getIndexKeys(), indexObject.getIndexOptions());
if (log.isDebugEnabled()) { if (LOGGER.isDebugEnabled()) {
log.debug(String.format("Created %s for entity %s in collection %s! ", indexObject, entity.getType(), LOGGER.debug(String.format("Created %s for entity %s in collection %s! ", indexObject, entity.getType(),
collection)); collection));
} }
} }

4
spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/mapreduce/MapReduceCounts.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2010-2012 the original author or authors. * Copyright 2010-2014 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -23,7 +23,7 @@ package org.springframework.data.mongodb.core.mapreduce;
*/ */
public class MapReduceCounts { public class MapReduceCounts {
public static MapReduceCounts NONE = new MapReduceCounts(-1, -1, -1); public static final MapReduceCounts NONE = new MapReduceCounts(-1, -1, -1);
private final long inputCount; private final long inputCount;
private final long emitCount; private final long emitCount;

16
spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/Criteria.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2010-2013 the original author or authors. * Copyright 2010-2014 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -52,11 +52,8 @@ public class Criteria implements CriteriaDefinition {
private static final Object NOT_SET = new Object(); private static final Object NOT_SET = new Object();
private String key; private String key;
private List<Criteria> criteriaChain; private List<Criteria> criteriaChain;
private LinkedHashMap<String, Object> criteria = new LinkedHashMap<String, Object>(); private LinkedHashMap<String, Object> criteria = new LinkedHashMap<String, Object>();
private Object isValue = NOT_SET; private Object isValue = NOT_SET;
public Criteria() { public Criteria() {
@ -101,13 +98,16 @@ public class Criteria implements CriteriaDefinition {
* @return * @return
*/ */
public Criteria is(Object o) { public Criteria is(Object o) {
if (isValue != NOT_SET) {
if (!isValue.equals(NOT_SET)) {
throw new InvalidMongoDbApiUsageException( throw new InvalidMongoDbApiUsageException(
"Multiple 'is' values declared. You need to use 'and' with multiple criteria"); "Multiple 'is' values declared. You need to use 'and' with multiple criteria");
} }
if (lastOperatorWasNot()) { if (lastOperatorWasNot()) {
throw new InvalidMongoDbApiUsageException("Invalid query: 'not' can't be used with 'is' - use 'ne' instead."); throw new InvalidMongoDbApiUsageException("Invalid query: 'not' can't be used with 'is' - use 'ne' instead.");
} }
this.isValue = o; this.isValue = o;
return this; return this;
} }
@ -475,8 +475,10 @@ public class Criteria implements CriteriaDefinition {
} }
protected DBObject getSingleCriteriaObject() { protected DBObject getSingleCriteriaObject() {
DBObject dbo = new BasicDBObject(); DBObject dbo = new BasicDBObject();
boolean not = false; boolean not = false;
for (String k : this.criteria.keySet()) { for (String k : this.criteria.keySet()) {
Object value = this.criteria.get(k); Object value = this.criteria.get(k);
if (not) { if (not) {
@ -494,12 +496,14 @@ public class Criteria implements CriteriaDefinition {
} }
DBObject queryCriteria = new BasicDBObject(); DBObject queryCriteria = new BasicDBObject();
if (isValue != NOT_SET) {
if (!NOT_SET.equals(isValue)) {
queryCriteria.put(this.key, this.isValue); queryCriteria.put(this.key, this.isValue);
queryCriteria.putAll(dbo); queryCriteria.putAll(dbo);
} else { } else {
queryCriteria.put(this.key, dbo); queryCriteria.put(this.key, dbo);
} }
return queryCriteria; return queryCriteria;
} }

4
spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/NearQuery.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2011-2013 the original author or authors. * Copyright 2011-2014 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -32,7 +32,7 @@ import com.mongodb.DBObject;
* @author Oliver Gierke * @author Oliver Gierke
* @author Thomas Darimont * @author Thomas Darimont
*/ */
public class NearQuery { public final class NearQuery {
private final Point point; private final Point point;
private Query query; private Query query;

21
spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/query/Query.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2010-2013 the original author or authors. * Copyright 2010-2014 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -23,6 +23,7 @@ import java.util.Collections;
import java.util.HashSet; import java.util.HashSet;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.Set; import java.util.Set;
import org.springframework.data.domain.Pageable; import org.springframework.data.domain.Pageable;
@ -41,10 +42,10 @@ import com.mongodb.DBObject;
*/ */
public class Query { public class Query {
private final static String RESTRICTED_TYPES_KEY = "_$RESTRICTED_TYPES"; private static final String RESTRICTED_TYPES_KEY = "_$RESTRICTED_TYPES";
private final Set<Class<?>> restrictedTypes = new HashSet<Class<?>>(); private final Set<Class<?>> restrictedTypes = new HashSet<Class<?>>();
private LinkedHashMap<String, Criteria> criteria = new LinkedHashMap<String, Criteria>(); private final Map<String, Criteria> criteria = new LinkedHashMap<String, Criteria>();
private Field fieldSpec; private Field fieldSpec;
private Sort sort; private Sort sort;
private int skip; private int skip;
@ -197,6 +198,7 @@ public class Query {
public DBObject getQueryObject() { public DBObject getQueryObject() {
DBObject dbo = new BasicDBObject(); DBObject dbo = new BasicDBObject();
for (String k : criteria.keySet()) { for (String k : criteria.keySet()) {
CriteriaDefinition c = criteria.get(k); CriteriaDefinition c = criteria.get(k);
DBObject cl = c.getCriteriaObject(); DBObject cl = c.getCriteriaObject();
@ -211,24 +213,19 @@ public class Query {
} }
public DBObject getFieldsObject() { public DBObject getFieldsObject() {
if (this.fieldSpec == null) { return this.fieldSpec == null ? null : fieldSpec.getFieldsObject();
return null;
}
return fieldSpec.getFieldsObject();
} }
public DBObject getSortObject() { public DBObject getSortObject() {
if (this.sort == null && this.sort == null) { if (this.sort == null) {
return null; return null;
} }
DBObject dbo = new BasicDBObject(); DBObject dbo = new BasicDBObject();
if (this.sort != null) { for (org.springframework.data.domain.Sort.Order order : this.sort) {
for (org.springframework.data.domain.Sort.Order order : this.sort) { dbo.put(order.getProperty(), order.isAscending() ? 1 : -1);
dbo.put(order.getProperty(), order.isAscending() ? 1 : -1);
}
} }
return dbo; return dbo;

10
spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/query/AbstractMongoQuery.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2010-2013 the original author or authors. * Copyright 2010-2014 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -153,7 +153,7 @@ public abstract class AbstractMongoQuery implements RepositoryQuery {
* *
* @author Oliver Gierke * @author Oliver Gierke
*/ */
class CollectionExecution extends Execution { final class CollectionExecution extends Execution {
private final Pageable pageable; private final Pageable pageable;
@ -176,7 +176,7 @@ public abstract class AbstractMongoQuery implements RepositoryQuery {
* *
* @author Oliver Gierke * @author Oliver Gierke
*/ */
class PagedExecution extends Execution { final class PagedExecution extends Execution {
private final Pageable pageable; private final Pageable pageable;
@ -213,7 +213,7 @@ public abstract class AbstractMongoQuery implements RepositoryQuery {
* *
* @author Oliver Gierke * @author Oliver Gierke
*/ */
class SingleEntityExecution extends Execution { final class SingleEntityExecution extends Execution {
private final boolean countProjection; private final boolean countProjection;
@ -239,7 +239,7 @@ public abstract class AbstractMongoQuery implements RepositoryQuery {
* *
* @author Oliver Gierke * @author Oliver Gierke
*/ */
class GeoNearExecution extends Execution { final class GeoNearExecution extends Execution {
private final MongoParameterAccessor accessor; private final MongoParameterAccessor accessor;

Loading…
Cancel
Save