Skip to content

Commit 5fc45e1

Browse files
authored
Remove useless runtime checks from the $search query building API (#967)
See https://github.com/mongodb/specifications#where-possible-depend-on-server-to-return-errors for more details
1 parent 74d31a2 commit 5fc45e1

File tree

8 files changed

+19
-133
lines changed

8 files changed

+19
-133
lines changed

driver-core/src/main/com/mongodb/client/model/search/RangeConstructibleBsonElement.java

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import com.mongodb.lang.Nullable;
2020
import org.bson.conversions.Bson;
2121

22-
import static com.mongodb.assertions.Assertions.isTrueArgument;
2322
import static com.mongodb.assertions.Assertions.notNull;
2423

2524
abstract class RangeConstructibleBsonElement<T, S extends RangeConstructibleBsonElement<T, S>> extends AbstractConstructibleBsonElement<S> {
@@ -75,15 +74,6 @@ private S newWithMutatedValue(
7574
} else if (u == null) {
7675
notNull("l", l);
7776
}
78-
if (l instanceof Comparable && u != null && l.getClass().isAssignableFrom(u.getClass())) {
79-
@SuppressWarnings("unchecked")
80-
Comparable<T> comparableL = (Comparable<T>) l;
81-
if (includeL && includeU) {
82-
isTrueArgument("l must be smaller than or equal to u", comparableL.compareTo(u) <= 0);
83-
} else {
84-
isTrueArgument("l must be smaller than u", comparableL.compareTo(u) < 0);
85-
}
86-
}
8777
return newWithMutatedValue(doc -> {
8878
doc.remove("gte");
8979
doc.remove("gt");

driver-core/src/main/com/mongodb/client/model/search/SearchConstructibleBsonElement.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,6 @@ public GaussSearchScoreExpression offset(final double offset) {
152152

153153
@Override
154154
public GaussSearchScoreExpression decay(final double decay) {
155-
isTrueArgument("decay must be greater than 0", decay > 0);
156-
isTrueArgument("decay must be less than 1", decay < 1);
157155
return newWithAppendedValue("decay", decay);
158156
}
159157
}

driver-core/src/main/com/mongodb/client/model/search/SearchOperator.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ static DateRangeSearchOperatorBase dateRange(final Iterable<? extends FieldSearc
188188
*
189189
* @param origin The origin from which the proximity of the results is measured.
190190
* The relevance score is 1 if the values of the fields are {@code origin}.
191-
* @param pivot The positive distance from the {@code origin} at which the relevance score drops in half.
191+
* @param pivot The distance from the {@code origin} at which the relevance score drops in half.
192192
* @param path The field to be searched.
193193
* @return The requested {@link SearchOperator}.
194194
* @mongodb.atlas.manual atlas-search/near/ near operator
@@ -202,7 +202,7 @@ static NumberNearSearchOperator near(final Number origin, final Number pivot, fi
202202
*
203203
* @param origin The origin from which the proximity of the results is measured.
204204
* The relevance score is 1 if the values of the fields are {@code origin}.
205-
* @param pivot The positive distance from the {@code origin} at which the relevance score drops in half.
205+
* @param pivot The distance from the {@code origin} at which the relevance score drops in half.
206206
* @param paths The non-empty fields to be searched.
207207
* @return The requested {@link SearchOperator}.
208208
* @mongodb.atlas.manual atlas-search/near/ near operator
@@ -220,7 +220,7 @@ static NumberNearSearchOperator near(final Number origin, final Number pivot, fi
220220
*
221221
* @param origin The origin from which the proximity of the results is measured.
222222
* The relevance score is 1 if the values of the fields are {@code origin}.
223-
* @param pivot The positive distance from the {@code origin} at which the relevance score drops in half.
223+
* @param pivot The distance from the {@code origin} at which the relevance score drops in half.
224224
* Data is extracted via {@link Duration#toMillis()}.
225225
* @param path The field to be searched.
226226
* @return The requested {@link SearchOperator}.
@@ -236,7 +236,7 @@ static DateNearSearchOperator near(final Instant origin, final Duration pivot, f
236236
*
237237
* @param origin The origin from which the proximity of the results is measured.
238238
* The relevance score is 1 if the values of the fields are {@code origin}.
239-
* @param pivot The positive distance from the {@code origin} at which the relevance score drops in half.
239+
* @param pivot The distance from the {@code origin} at which the relevance score drops in half.
240240
* Data is extracted via {@link Duration#toMillis()}.
241241
* @param paths The non-empty fields to be searched.
242242
* @return The requested {@link SearchOperator}.
@@ -247,8 +247,6 @@ static DateNearSearchOperator near(final Instant origin, final Duration pivot, f
247247
Iterator<? extends SearchPath> pathIterator = notNull("paths", paths).iterator();
248248
isTrueArgument("paths must not be empty", pathIterator.hasNext());
249249
notNull("pivot", pivot);
250-
isTrueArgument("pivot must be positive", !pivot.isZero());
251-
isTrueArgument("pivot must be positive", !pivot.isNegative());
252250
return new SearchConstructibleBsonElement("near", new Document("origin", notNull("origin", origin))
253251
.append("path", combineToBsonValue(pathIterator, true))
254252
.append("pivot", pivot.toMillis()));
@@ -259,7 +257,7 @@ static DateNearSearchOperator near(final Instant origin, final Duration pivot, f
259257
*
260258
* @param origin The origin from which the proximity of the results is measured.
261259
* The relevance score is 1 if the values of the fields are {@code origin}.
262-
* @param pivot The positive distance in meters from the {@code origin} at which the relevance score drops in half.
260+
* @param pivot The distance in meters from the {@code origin} at which the relevance score drops in half.
263261
* @param path The field to be searched.
264262
* @return The requested {@link SearchOperator}.
265263
* @mongodb.atlas.manual atlas-search/near/ near operator
@@ -273,7 +271,7 @@ static GeoNearSearchOperator near(final Point origin, final Number pivot, final
273271
*
274272
* @param origin The origin from which the proximity of the results is measured.
275273
* The relevance score is 1 if the values of the fields are {@code origin}.
276-
* @param pivot The positive distance in meters from the {@code origin} at which the relevance score drops in half.
274+
* @param pivot The distance in meters from the {@code origin} at which the relevance score drops in half.
277275
* @param paths The non-empty fields to be searched.
278276
* @return The requested {@link SearchOperator}.
279277
* @mongodb.atlas.manual atlas-search/near/ near operator

driver-core/src/main/com/mongodb/client/model/search/SearchScore.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import org.bson.Document;
2424
import org.bson.conversions.Bson;
2525

26-
import static com.mongodb.assertions.Assertions.isTrueArgument;
2726
import static com.mongodb.assertions.Assertions.notNull;
2827

2928
/**
@@ -40,12 +39,11 @@ public interface SearchScore extends Bson {
4039
/**
4140
* Returns a {@link SearchScore} that instructs to multiply the score by the specified {@code value}.
4241
*
43-
* @param value The positive value to multiply the score by.
42+
* @param value The value to multiply the score by.
4443
* @return The requested {@link SearchScore}.
4544
* @mongodb.atlas.manual atlas-search/scoring/#boost boost score modifier
4645
*/
4746
static ValueBoostSearchScore boost(final float value) {
48-
isTrueArgument("value must be positive", value > 0);
4947
return new SearchConstructibleBsonElement("boost", new BsonDocument("value", new BsonDouble(value)));
5048
}
5149

@@ -64,13 +62,12 @@ static PathBoostSearchScore boost(final FieldSearchPath path) {
6462
/**
6563
* Returns a {@link SearchScore} that instructs to replace the score with the specified {@code value}.
6664
*
67-
* @param value The positive value to replace the score with.
65+
* @param value The value to replace the score with.
6866
* @return The requested {@link SearchScore}.
6967
* @mongodb.atlas.manual atlas-search/scoring/#constant constant score modifier
7068
* @see SearchScoreExpression#constantExpression(float)
7169
*/
7270
static ConstantSearchScore constant(final float value) {
73-
isTrueArgument("value must be positive", value > 0);
7471
return new SearchConstructibleBsonElement("constant", new BsonDocument("value", new BsonDouble(value)));
7572
}
7673

driver-core/src/main/com/mongodb/client/model/search/SearchScoreExpression.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,12 @@ static ConstantSearchScoreExpression constantExpression(final float value) {
8383
* @param origin The point of origin, see {@link GaussSearchScoreExpression#offset(double)}.
8484
* The value of the Gaussian function is 1 if the value of the {@code path} expression is {@code origin}.
8585
* @param path The expression whose value is used to calculate the input of the Gaussian function.
86-
* @param scale The non-zero distance from the points {@code origin} ± {@link GaussSearchScoreExpression#offset(double) offset}
86+
* @param scale The distance from the points {@code origin} ± {@link GaussSearchScoreExpression#offset(double) offset}
8787
* at which the output of the Gaussian function must decay by the factor of {@link GaussSearchScoreExpression#decay(double) decay}.
8888
* @return The requested {@link SearchScoreExpression}.
8989
*/
9090
static GaussSearchScoreExpression gaussExpression(final double origin, final PathSearchScoreExpression path, final double scale) {
9191
notNull("path", path);
92-
isTrueArgument("scale must not be 0", scale != 0);
9392
Bson value = new Bson() {
9493
@Override
9594
public <TDocument> BsonDocument toBsonDocument(final Class<TDocument> documentClass, final CodecRegistry codecRegistry) {

driver-core/src/test/unit/com/mongodb/client/model/search/SearchOperatorTest.java

Lines changed: 0 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -273,30 +273,6 @@ void numberRange() {
273273
// queries must not be empty
274274
SearchOperator.numberRange(emptyList())
275275
),
276-
() -> assertThrows(IllegalArgumentException.class, () ->
277-
// l must be smaller than u in an open interval
278-
SearchOperator.numberRange(fieldPath("fieldName")).gtLt(0, 0)
279-
),
280-
() -> assertThrows(IllegalArgumentException.class, () ->
281-
// l must be smaller than u in an open interval
282-
SearchOperator.numberRange(fieldPath("fieldName")).gtLt(1, 0)
283-
),
284-
() -> assertThrows(IllegalArgumentException.class, () ->
285-
// l must be smaller than u in a half-open interval
286-
SearchOperator.numberRange(fieldPath("fieldName")).gtLte(0, 0)
287-
),
288-
() -> assertThrows(IllegalArgumentException.class, () ->
289-
// l must be smaller than u in a half-open interval
290-
SearchOperator.numberRange(fieldPath("fieldName")).gtLte(1, 0)
291-
),
292-
() -> assertThrows(IllegalArgumentException.class, () ->
293-
// l must be smaller than u in a half-open interval
294-
SearchOperator.numberRange(fieldPath("fieldName")).gteLt(0, 0)
295-
),
296-
() -> assertThrows(IllegalArgumentException.class, () ->
297-
// l must be smaller than u in a half-open interval
298-
SearchOperator.numberRange(fieldPath("fieldName")).gteLt(1, 0)
299-
),
300276
() -> assertEquals(
301277
new BsonDocument("range",
302278
new BsonDocument("path", fieldPath("fieldName").toBsonValue())
@@ -395,30 +371,6 @@ void dateRange() {
395371
// queries must not be empty
396372
SearchOperator.dateRange(emptyList())
397373
),
398-
() -> assertThrows(IllegalArgumentException.class, () ->
399-
// l must be smaller than u in an open interval
400-
SearchOperator.dateRange(fieldPath("fieldName")).gtLt(Instant.EPOCH, Instant.EPOCH)
401-
),
402-
() -> assertThrows(IllegalArgumentException.class, () ->
403-
// l must be smaller than u in an open interval
404-
SearchOperator.dateRange(fieldPath("fieldName")).gtLt(Instant.now(), Instant.EPOCH)
405-
),
406-
() -> assertThrows(IllegalArgumentException.class, () ->
407-
// l must be smaller than u in a half-open interval
408-
SearchOperator.dateRange(fieldPath("fieldName")).gtLte(Instant.EPOCH, Instant.EPOCH)
409-
),
410-
() -> assertThrows(IllegalArgumentException.class, () ->
411-
// l must be smaller than u in a half-open interval
412-
SearchOperator.dateRange(fieldPath("fieldName")).gtLte(Instant.now(), Instant.EPOCH)
413-
),
414-
() -> assertThrows(IllegalArgumentException.class, () ->
415-
// l must be smaller than u in a half-open interval
416-
SearchOperator.dateRange(fieldPath("fieldName")).gteLt(Instant.EPOCH, Instant.EPOCH)
417-
),
418-
() -> assertThrows(IllegalArgumentException.class, () ->
419-
// l must be smaller than u in a half-open interval
420-
SearchOperator.dateRange(fieldPath("fieldName")).gteLt(Instant.now(), Instant.EPOCH)
421-
),
422374
() -> assertEquals(
423375
new BsonDocument("range",
424376
new BsonDocument("path", fieldPath("fieldName").toBsonValue())
@@ -529,14 +481,6 @@ void near() {
529481
// paths must not be empty
530482
SearchOperator.near(new Point(new Position(0, 0)), 1, emptyList())
531483
),
532-
() -> assertThrows(IllegalArgumentException.class, () ->
533-
// pivot must be positive
534-
SearchOperator.near(Instant.EPOCH, Duration.ZERO, fieldPath("fieldPath"))
535-
),
536-
() -> assertThrows(IllegalArgumentException.class, () ->
537-
// pivot must be positive
538-
SearchOperator.near(Instant.EPOCH, Duration.ofMillis(-1), fieldPath("fieldPath"))
539-
),
540484
() -> assertEquals(
541485
new BsonDocument("near",
542486
new BsonDocument("origin", new BsonInt32(0))

driver-core/src/test/unit/com/mongodb/client/model/search/SearchScoreExpressionTest.java

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -84,25 +84,6 @@ void constantExpression() {
8484
@Test
8585
void gaussExpression() {
8686
assertAll(
87-
() -> assertThrows(IllegalArgumentException.class, () ->
88-
// scale must not be 0
89-
SearchScoreExpression.gaussExpression(0, SearchScoreExpression.pathExpression(fieldPath("fieldName")), 0)
90-
),
91-
() -> assertThrows(IllegalArgumentException.class, () ->
92-
// decay must be positive
93-
SearchScoreExpression.gaussExpression(0, SearchScoreExpression.pathExpression(fieldPath("fieldName")), 1)
94-
.decay(-1)
95-
),
96-
() -> assertThrows(IllegalArgumentException.class, () ->
97-
// decay must be positive
98-
SearchScoreExpression.gaussExpression(0, SearchScoreExpression.pathExpression(fieldPath("fieldName")), 1)
99-
.decay(0)
100-
),
101-
() -> assertThrows(IllegalArgumentException.class, () ->
102-
// decay must be less than 1
103-
SearchScoreExpression.gaussExpression(0, SearchScoreExpression.pathExpression(fieldPath("fieldName")), 1)
104-
.decay(1)
105-
),
10687
() -> assertEquals(
10788
new BsonDocument("gauss",
10889
new BsonDocument("origin", new BsonDouble(50))

driver-core/src/test/unit/com/mongodb/client/model/search/SearchScoreTest.java

Lines changed: 10 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import static com.mongodb.client.model.search.SearchScoreExpression.constantExpression;
2626
import static org.junit.jupiter.api.Assertions.assertAll;
2727
import static org.junit.jupiter.api.Assertions.assertEquals;
28-
import static org.junit.jupiter.api.Assertions.assertThrows;
2928

3029
final class SearchScoreTest {
3130
@Test
@@ -40,21 +39,11 @@ void of() {
4039

4140
@Test
4241
void valueBoost() {
43-
assertAll(
44-
() -> assertThrows(IllegalArgumentException.class, () ->
45-
// value must be positive
46-
SearchScore.boost(0f)
47-
),
48-
() -> assertThrows(IllegalArgumentException.class, () ->
49-
// value must be positive
50-
SearchScore.boost(-1f)
51-
),
52-
() -> assertEquals(
53-
new BsonDocument("boost",
54-
new BsonDocument("value", new BsonDouble(0.5))),
55-
SearchScore.boost(0.5f)
56-
.toBsonDocument()
57-
)
42+
assertEquals(
43+
new BsonDocument("boost",
44+
new BsonDocument("value", new BsonDouble(0.5))),
45+
SearchScore.boost(0.5f)
46+
.toBsonDocument()
5847
);
5948
}
6049

@@ -84,21 +73,11 @@ void pathBoost() {
8473

8574
@Test
8675
void constant() {
87-
assertAll(
88-
() -> assertThrows(IllegalArgumentException.class, () ->
89-
// value must be positive
90-
SearchScore.constant(0f)
91-
),
92-
() -> assertThrows(IllegalArgumentException.class, () ->
93-
// value must be positive
94-
SearchScore.constant(-1f)
95-
),
96-
() -> assertEquals(
97-
new BsonDocument("constant",
98-
new BsonDocument("value", new BsonDouble(0.5))),
99-
SearchScore.constant(0.5f)
100-
.toBsonDocument()
101-
)
76+
assertEquals(
77+
new BsonDocument("constant",
78+
new BsonDocument("value", new BsonDouble(0.5))),
79+
SearchScore.constant(0.5f)
80+
.toBsonDocument()
10281
);
10382
}
10483

0 commit comments

Comments
 (0)