Skip to content

Commit d40084f

Browse files
gustavodegeusmp911de
authored andcommittedApr 20, 2017
DATAMONGO-1325 - Add $sample aggregation stage.
We now support the $sample aggregation pipeline stage via Aggregation to select a random subset of result documents. TypedAggregation<Employee> agg = Aggregation.newAggregation(Employee.class, sample(5)); Original pull request: spring-projects#452.
1 parent 7bfbff0 commit d40084f

File tree

3 files changed

+117
-0
lines changed

3 files changed

+117
-0
lines changed
 

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
* @author Alessio Fachechi
5252
* @author Christoph Strobl
5353
* @author Nikolay Bogdanov
54+
* @author Gustavo de Geus
5455
* @since 1.3
5556
*/
5657
public class Aggregation {
@@ -382,6 +383,16 @@ public static LimitOperation limit(long maxElements) {
382383
return new LimitOperation(maxElements);
383384
}
384385

386+
/**
387+
* Creates a new {@link SampleOperation} to selects the specified number of documents from its input randomly.
388+
*
389+
* @param sampleSize must not be less than zero.
390+
* @return
391+
*/
392+
public static SampleOperation sample(long sampleSize) {
393+
return new SampleOperation(sampleSize);
394+
}
395+
385396
/**
386397
* Creates a new {@link MatchOperation} using the given {@link Criteria}.
387398
*
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright 2013-2017 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.mongodb.core.aggregation;
17+
18+
import org.bson.Document;
19+
import org.springframework.util.Assert;
20+
21+
/**
22+
* Encapsulates the {@code $sample}-operation.
23+
* <p>
24+
* We recommend to use the static factory method {@link Aggregation#sample(long)} instead of creating instances of this
25+
* class directly.
26+
*
27+
* @author Gustavo de Geus
28+
* @since 2.0
29+
* @see <a href="https://docs.mongodb.com/master/reference/operator/aggregation/sample/">MongoDB Aggregation Framework: $sample</a>
30+
*/
31+
public class SampleOperation implements AggregationOperation {
32+
33+
private final long sampleSize;
34+
35+
/**
36+
* @param sampleSize number of documents to be randomly selected from its input.
37+
*/
38+
public SampleOperation(long sampleSize) {
39+
40+
Assert.isTrue(sampleSize > 0, "Sample size must be greater than zero!");
41+
this.sampleSize = sampleSize;
42+
}
43+
44+
/*
45+
(non-Javadoc)
46+
* @see org.springframework.data.mongodb.core.aggregation.BucketOperationSupport#toDocument(org.springframework.data.mongodb.core.aggregation.AggregationOperationContext)
47+
*/
48+
@Override
49+
public Document toDocument(AggregationOperationContext context) {
50+
return new Document("$sample", new Document("size", this.sampleSize));
51+
}
52+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright 2017 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.mongodb.core.aggregation;
17+
18+
import static org.hamcrest.CoreMatchers.*;
19+
import static org.junit.Assert.*;
20+
21+
import org.bson.Document;
22+
import org.junit.Test;
23+
24+
/**
25+
* Unit tests for {@link SampleOperation}.
26+
*
27+
* @author Gustavo de Geus
28+
*/
29+
public class SampleOperationUnitTests {
30+
31+
private static final String SIZE = "size";
32+
private static final String OP = "$sample";
33+
34+
@Test(expected = IllegalArgumentException.class)
35+
public void rejectsNegativeSample() {
36+
new SampleOperation(-1L);
37+
}
38+
39+
@Test(expected = IllegalArgumentException.class)
40+
public void rejectsZeroSample() {
41+
new SampleOperation(0L);
42+
}
43+
44+
@Test
45+
public void rendersSampleOperation() {
46+
long sampleSize = 5L;
47+
SampleOperation sampleOperation = Aggregation.sample(sampleSize);
48+
Document sampleOperationDocument = sampleOperation.toDocument(Aggregation.DEFAULT_CONTEXT);
49+
assertNotNull(sampleOperationDocument.get(OP));
50+
assertThat(sampleOperationDocument.get(OP), is(instanceOf(Document.class)));
51+
Document sampleSizeDocument = sampleOperationDocument.get(OP, Document.class);
52+
assertEquals(sampleSize, sampleSizeDocument.get(SIZE));
53+
}
54+
}

0 commit comments

Comments
 (0)
Please sign in to comment.