Skip to content

Commit 4621303

Browse files
committed
spring-projectsGH-3726: Add support for $sampleRate aggregation operation
Closes spring-projects#3726
1 parent 45971b2 commit 4621303

File tree

3 files changed

+129
-1
lines changed

3 files changed

+129
-1
lines changed

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

+13-1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
* @author Nikolay Bogdanov
5151
* @author Gustavo de Geus
5252
* @author Jérôme Guyon
53+
* @author James McNee
5354
* @since 1.3
5455
*/
5556
public class Aggregation {
@@ -128,7 +129,7 @@ public static AggregationUpdate newUpdate(AggregationOperation... operations) {
128129
return AggregationUpdate.from(Arrays.asList(operations));
129130
}
130131

131-
/**
132+
/**
132133
* Returns a copy of this {@link Aggregation} with the given {@link AggregationOptions} set. Note that options are
133134
* supported in MongoDB version 2.6+.
134135
*
@@ -478,6 +479,17 @@ public static SampleOperation sample(long sampleSize) {
478479
return new SampleOperation(sampleSize);
479480
}
480481

482+
/**
483+
* Creates a new {@link SampleRateOperation} to select the documents from its input randomly using the provided sample rate.
484+
*
485+
* @param sampleRate must not be less than zero or more than one.
486+
* @return new instance of {@link SampleRateOperation}.
487+
* @since 3.2.4
488+
*/
489+
public static SampleRateOperation sampleRate(double sampleRate) {
490+
return new SampleRateOperation(sampleRate);
491+
}
492+
481493
/**
482494
* Creates a new {@link MatchOperation} using the given {@link Criteria}.
483495
*
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* Copyright 2017-2021 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+
* https://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 $sampleRate}-operation.
23+
* <p>
24+
* We recommend using the static factory method {@link Aggregation#sampleRate(double)} instead of creating instances of this
25+
* class directly.
26+
*
27+
* @author James McNee
28+
* @see <a href="https://docs.mongodb.com/manual/reference/operator/aggregation/sampleRate/">MongoDB Aggregation Framework:
29+
* $sampleRate</a>
30+
* @since 3.2.4
31+
*/
32+
public class SampleRateOperation implements AggregationOperation {
33+
34+
private final double sampleRate;
35+
36+
/**
37+
* @param sampleRate sample rate to determine number of documents to be randomly selected from the input.
38+
*/
39+
public SampleRateOperation(double sampleRate) {
40+
Assert.isTrue(sampleRate >= 0, "Sample rate must be greater than zero!");
41+
Assert.isTrue(sampleRate <= 1, "Sample rate must not be greater than one!");
42+
this.sampleRate = sampleRate;
43+
}
44+
45+
/*
46+
(non-Javadoc)
47+
* @see org.springframework.data.mongodb.core.aggregation.BucketOperationSupport#toDocument(org.springframework.data.mongodb.core.aggregation.AggregationOperationContext)
48+
*/
49+
@Override
50+
public Document toDocument(AggregationOperationContext context) {
51+
return new Document(getOperator(), this.sampleRate);
52+
}
53+
54+
/*
55+
* (non-Javadoc)
56+
* @see org.springframework.data.mongodb.core.aggregation.AggregationOperation#getOperator()
57+
*/
58+
@Override
59+
public String getOperator() {
60+
return "$sampleRate";
61+
}
62+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright 2017-2021 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+
* https://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.junit.jupiter.api.Test;
20+
21+
import static org.assertj.core.api.Assertions.assertThat;
22+
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
23+
24+
/**
25+
* Unit tests for {@link SampleRateOperation}.
26+
*
27+
* @author James McNee
28+
*/
29+
public class SampleRateOperationUnitTests {
30+
31+
private static final String OPERATION = "$sampleRate";
32+
33+
@Test // GH-3726
34+
public void shouldRejectSampleNegativeSampleRate() {
35+
assertThatIllegalArgumentException().isThrownBy(() -> new SampleRateOperation(-1.0));
36+
}
37+
38+
@Test // GH-3726
39+
public void shouldRejectSampleRateGreaterThan1() {
40+
assertThatIllegalArgumentException().isThrownBy(() -> new SampleRateOperation(1.1));
41+
}
42+
43+
@Test // GH-3726
44+
public void rendersSampleRateOperation() {
45+
double sampleRate = 0.34;
46+
47+
SampleRateOperation sampleOperation = Aggregation.sampleRate(sampleRate);
48+
49+
Document sampleOperationDocument = sampleOperation.toDocument(Aggregation.DEFAULT_CONTEXT);
50+
51+
assertThat(sampleOperationDocument.get(OPERATION)).isNotNull();
52+
assertThat(sampleOperationDocument.get(OPERATION)).isEqualTo(sampleRate);
53+
}
54+
}

0 commit comments

Comments
 (0)