Browse Source

DATAMONGO-1391 - Polishing.

Removed white spaces, updated Javadoc and return early when using non 3.2 $unwind options.

Original Pull Request: #355
pull/359/merge
Christoph Strobl 10 years ago
parent
commit
5485f2fcd4
  1. 18
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/Aggregation.java
  2. 78
      spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/UnwindOperation.java
  3. 4
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/aggregation/AggregationTests.java
  4. 5
      spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/aggregation/UnwindOperationUnitTests.java

18
spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/Aggregation.java

@ -44,6 +44,7 @@ import com.mongodb.DBObject; @@ -44,6 +44,7 @@ import com.mongodb.DBObject;
* @author Oliver Gierke
* @author Mark Paluch
* @author Alessio Fachechi
* @author Christoph Strobl
* @since 1.3
*/
public class Aggregation {
@ -209,7 +210,7 @@ public class Aggregation { @@ -209,7 +210,7 @@ public class Aggregation {
* @param field must not be {@literal null} or empty.
* @param preserveNullAndEmptyArrays {@literal true} to output the document if path is {@literal null}, missing or
* array is empty.
* @return
* @return new {@link UnwindOperation}
* @since 1.10
*/
public static UnwindOperation unwind(String field, boolean preserveNullAndEmptyArrays) {
@ -217,12 +218,13 @@ public class Aggregation { @@ -217,12 +218,13 @@ public class Aggregation {
}
/**
* Factory method to create a new {@link UnwindOperation} for the field with the given name and to include the index
* field as {@code arrayIndex}. Note that extended unwind is supported in MongoDB version 3.2+.
* Factory method to create a new {@link UnwindOperation} for the field with the given name including the name of a
* new field to hold the array index of the element as {@code arrayIndex}. Note that extended unwind is supported in
* MongoDB version 3.2+.
*
* @param field must not be {@literal null} or empty.
* @param arrayIndex must not be {@literal null} or empty.
* @return
* @return new {@link UnwindOperation}
* @since 1.10
*/
public static UnwindOperation unwind(String field, String arrayIndex) {
@ -230,15 +232,15 @@ public class Aggregation { @@ -230,15 +232,15 @@ public class Aggregation {
}
/**
* Factory method to create a new {@link UnwindOperation} for the field with the given name, to include the index
* field as {@code arrayIndex} and {@code preserveNullAndEmptyArrays}. Note that extended unwind is supported in
* MongoDB version 3.2+.
* Factory method to create a new {@link UnwindOperation} for the field with the given nameincluding the name of a new
* field to hold the array index of the element as {@code arrayIndex} using {@code preserveNullAndEmptyArrays}. Note
* that extended unwind is supported in MongoDB version 3.2+.
*
* @param field must not be {@literal null} or empty.
* @param arrayIndex must not be {@literal null} or empty.
* @param preserveNullAndEmptyArrays {@literal true} to output the document if path is {@literal null}, missing or
* array is empty.
* @return
* @return new {@link UnwindOperation}
* @since 1.10
*/
public static UnwindOperation unwind(String field, String arrayIndex, boolean preserveNullAndEmptyArrays) {

78
spring-data-mongodb/src/main/java/org/springframework/data/mongodb/core/aggregation/UnwindOperation.java

@ -19,7 +19,6 @@ import org.springframework.data.mongodb.core.aggregation.ExposedFields.ExposedFi @@ -19,7 +19,6 @@ import org.springframework.data.mongodb.core.aggregation.ExposedFields.ExposedFi
import org.springframework.util.Assert;
import com.mongodb.BasicDBObject;
import com.mongodb.BasicDBObjectBuilder;
import com.mongodb.DBObject;
/**
@ -32,6 +31,7 @@ import com.mongodb.DBObject; @@ -32,6 +31,7 @@ import com.mongodb.DBObject;
* @author Thomas Darimont
* @author Oliver Gierke
* @author Mark Paluch
* @author Christoph Strobl
* @since 1.3
*/
public class UnwindOperation
@ -43,7 +43,7 @@ public class UnwindOperation @@ -43,7 +43,7 @@ public class UnwindOperation
/**
* Creates a new {@link UnwindOperation} for the given {@link Field}.
*
*
* @param field must not be {@literal null}.
*/
public UnwindOperation(Field field) {
@ -52,7 +52,7 @@ public class UnwindOperation @@ -52,7 +52,7 @@ public class UnwindOperation
/**
* Creates a new {@link UnwindOperation} using Mongo 3.2 syntax.
*
*
* @param field must not be {@literal null}.
* @param preserveNullAndEmptyArrays {@literal true} to output the document if path is {@literal null}, missing or
* array is empty.
@ -68,7 +68,7 @@ public class UnwindOperation @@ -68,7 +68,7 @@ public class UnwindOperation
/**
* Creates a new {@link UnwindOperation} using Mongo 3.2 syntax.
*
*
* @param field must not be {@literal null}.
* @param arrayIndex optional field name to expose the field array index, must not be {@literal null}.
* @param preserveNullAndEmptyArrays {@literal true} to output the document if path is {@literal null}, missing or
@ -92,26 +92,26 @@ public class UnwindOperation @@ -92,26 +92,26 @@ public class UnwindOperation
@Override
public DBObject toDBObject(AggregationOperationContext context) {
String unwindField = context.getReference(field).toString();
Object unwindArg;
if (preserveNullAndEmptyArrays || arrayIndex != null) {
BasicDBObjectBuilder builder = BasicDBObjectBuilder.start().add("path", unwindField);
builder.add("preserveNullAndEmptyArrays", preserveNullAndEmptyArrays);
String path = context.getReference(field).toString();
if (arrayIndex != null) {
builder.add("includeArrayIndex", arrayIndex.getName());
}
if (!preserveNullAndEmptyArrays && arrayIndex == null) {
return new BasicDBObject("$unwind", path);
}
unwindArg = builder.get();
} else {
unwindArg = unwindField;
DBObject unwindArgs = new BasicDBObject();
unwindArgs.put("path", path);
if (arrayIndex != null) {
unwindArgs.put("includeArrayIndex", arrayIndex.getName());
}
unwindArgs.put("preserveNullAndEmptyArrays", preserveNullAndEmptyArrays);
return new BasicDBObject("$unwind", unwindArg);
return new BasicDBObject("$unwind", unwindArgs);
}
/*
* (non-Javadoc)
* @see org.springframework.data.mongodb.core.aggregation.FieldsExposingAggregationOperation#getFields()
*/
@Override
public ExposedFields getFields() {
return arrayIndex != null ? ExposedFields.from(arrayIndex) : ExposedFields.from();
@ -121,11 +121,16 @@ public class UnwindOperation @@ -121,11 +121,16 @@ public class UnwindOperation
* Get a builder that allows creation of {@link LookupOperation}.
*
* @return
* @since 1.10
*/
public static PathBuilder newUnwind() {
return UnwindOperationBuilder.newBuilder();
}
/**
* @author Mark Paluch
* @since 1.10
*/
public static interface PathBuilder {
/**
@ -135,11 +140,15 @@ public class UnwindOperation @@ -135,11 +140,15 @@ public class UnwindOperation
IndexBuilder path(String path);
}
/**
* @author Mark Paluch
* @since 1.10
*/
public static interface IndexBuilder {
/**
* Exposes the array index as {@code field}.
*
*
* @param field field name to expose the field array index, must not be {@literal null} or empty.
* @return
*/
@ -147,7 +156,7 @@ public class UnwindOperation @@ -147,7 +156,7 @@ public class UnwindOperation
/**
* Do not expose the array index.
*
*
* @return
*/
EmptyArraysBuilder noArrayIndex();
@ -157,14 +166,14 @@ public class UnwindOperation @@ -157,14 +166,14 @@ public class UnwindOperation
/**
* Output documents if the array is null or empty.
*
*
* @return
*/
UnwindOperation preserveNullAndEmptyArrays();
/**
* Do not output documents if the array is null or empty.
*
*
* @return
*/
UnwindOperation skipNullAndEmptyArrays();
@ -192,6 +201,10 @@ public class UnwindOperation @@ -192,6 +201,10 @@ public class UnwindOperation
return new UnwindOperationBuilder();
}
/*
* (non-Javadoc)
* @see org.springframework.data.mongodb.core.aggregation.UnwindOperation.EmptyArraysBuilder#preserveNullAndEmptyArrays()
*/
@Override
public UnwindOperation preserveNullAndEmptyArrays() {
@ -202,6 +215,10 @@ public class UnwindOperation @@ -202,6 +215,10 @@ public class UnwindOperation
return new UnwindOperation(field, true);
}
/*
* (non-Javadoc)
* @see org.springframework.data.mongodb.core.aggregation.UnwindOperation.EmptyArraysBuilder#skipNullAndEmptyArrays()
*/
@Override
public UnwindOperation skipNullAndEmptyArrays() {
@ -212,26 +229,39 @@ public class UnwindOperation @@ -212,26 +229,39 @@ public class UnwindOperation
return new UnwindOperation(field, false);
}
/*
* (non-Javadoc)
* @see org.springframework.data.mongodb.core.aggregation.UnwindOperation.IndexBuilder#arrayIndex(java.lang.String)
*/
@Override
public EmptyArraysBuilder arrayIndex(String field) {
Assert.hasText(field, "'ArrayIndex' must not be null or empty!");
arrayIndex = Fields.field(field);
return this;
}
/*
* (non-Javadoc)
* @see org.springframework.data.mongodb.core.aggregation.UnwindOperation.IndexBuilder#noArrayIndex()
*/
@Override
public EmptyArraysBuilder noArrayIndex() {
arrayIndex = null;
return this;
}
/*
* (non-Javadoc)
* @see org.springframework.data.mongodb.core.aggregation.UnwindOperation.PathBuilder#path(java.lang.String)
*/
@Override
public UnwindOperationBuilder path(String path) {
Assert.hasText(path, "'Path' must not be null or empty!");
field = Fields.field(path);
return this;
}
}
}

4
spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/aggregation/AggregationTests.java

@ -247,6 +247,8 @@ public class AggregationTests { @@ -247,6 +247,8 @@ public class AggregationTests {
@Test
public void shouldUnwindWithIndex() {
assumeTrue(mongoVersion.isGreaterThanOrEqualTo(THREE_DOT_TWO));
DBCollection coll = mongoTemplate.getCollection(INPUT_COLLECTION);
coll.insert(createDocument("Doc1", "spring", "mongodb", "nosql"));
@ -276,6 +278,8 @@ public class AggregationTests { @@ -276,6 +278,8 @@ public class AggregationTests {
@Test
public void shouldUnwindPreserveEmpty() {
assumeTrue(mongoVersion.isGreaterThanOrEqualTo(THREE_DOT_TWO));
DBCollection coll = mongoTemplate.getCollection(INPUT_COLLECTION);
coll.insert(createDocument("Doc1", "spring", "mongodb", "nosql"));

5
spring-data-mongodb/src/test/java/org/springframework/data/mongodb/core/aggregation/UnwindOperationUnitTests.java

@ -26,8 +26,9 @@ import com.mongodb.DBObject; @@ -26,8 +26,9 @@ import com.mongodb.DBObject;
/**
* Unit tests for {@link UnwindOperation}.
*
*
* @author Mark Paluch
* @author Christoph Strobl
*/
public class UnwindOperationUnitTests {
@ -124,7 +125,7 @@ public class UnwindOperationUnitTests { @@ -124,7 +125,7 @@ public class UnwindOperationUnitTests {
assertThat(unwindClause,
isBsonObject().containing("path", "$foo").//
containing("preserveNullAndEmptyArrays", true).//
notContaining("myindex"));
containing("includeArrayIndex", "myindex"));
}
private DBObject extractDbObjectFromUnwindOperation(UnwindOperation unwindOperation) {

Loading…
Cancel
Save