Skip to content

Commit 0fed328

Browse files
committed
Implement boolean expressions (#1025)
JAVA-4779
1 parent 54a72fe commit 0fed328

19 files changed

+780
-23
lines changed

config/checkstyle/suppressions.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,4 +157,6 @@
157157
<suppress checks="ParameterName" files=".*org[\\/]bson[\\/]codecs[\\/]pojo[\\/]bench[\\/].*"/>
158158
<suppress checks="Regexp" files=".*org[\\/]bson[\\/]codecs[\\/]pojo[\\/]bench[\\/].*"/>
159159

160+
<!-- Complicated expressions for demo purposes -->
161+
<suppress checks="SimplifyBooleanExpressionCheck" files="ExpressionsFunctionalTest"/>
160162
</suppressions>

driver-core/src/main/com/mongodb/MongoClientSettings.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import com.mongodb.annotations.Immutable;
2020
import com.mongodb.annotations.NotThreadSafe;
2121
import com.mongodb.client.gridfs.codecs.GridFSFileCodecProvider;
22+
import com.mongodb.client.model.expressions.ExpressionCodecProvider;
2223
import com.mongodb.client.model.geojson.codecs.GeoJsonCodecProvider;
2324
import com.mongodb.connection.ClusterSettings;
2425
import com.mongodb.connection.ConnectionPoolSettings;
@@ -76,6 +77,7 @@ public final class MongoClientSettings {
7677
new JsonObjectCodecProvider(),
7778
new BsonCodecProvider(),
7879
new EnumCodecProvider(),
80+
new ExpressionCodecProvider(),
7981
new Jep395RecordCodecProvider()));
8082

8183
private final ReadPreference readPreference;
@@ -123,6 +125,7 @@ public final class MongoClientSettings {
123125
* <li>{@link org.bson.codecs.JsonObjectCodecProvider}</li>
124126
* <li>{@link org.bson.codecs.BsonCodecProvider}</li>
125127
* <li>{@link org.bson.codecs.EnumCodecProvider}</li>
128+
* <li>{@link ExpressionCodecProvider}</li>
126129
* <li>{@link com.mongodb.Jep395RecordCodecProvider}</li>
127130
* </ul>
128131
*
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright 2008-present MongoDB, Inc.
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+
17+
package com.mongodb.client.model.expressions;
18+
19+
/**
20+
* Expresses an array value. An array value is a finite, ordered collection of
21+
* elements of a certain type.
22+
*
23+
* @param <T> the type of the elements in the array
24+
*/
25+
public interface ArrayExpression<T extends Expression> extends Expression {
26+
27+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Copyright 2008-present MongoDB, Inc.
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+
17+
package com.mongodb.client.model.expressions;
18+
19+
/**
20+
* Expresses a boolean value.
21+
*/
22+
public interface BooleanExpression extends Expression {
23+
24+
/**
25+
* Returns logical true if this expression evaluates to logical false.
26+
* Returns logical false if this expression evaluates to logical true.
27+
*
28+
* @return True if false; false if true.
29+
*/
30+
BooleanExpression not();
31+
32+
/**
33+
* Returns logical true if this or the other expression evaluates to logical
34+
* true. Returns logical false if both evaluate to logical false.
35+
*
36+
* @param or the other boolean expression.
37+
* @return True if either true, false if both false.
38+
*/
39+
BooleanExpression or(BooleanExpression or);
40+
41+
/**
42+
* Returns logical true if both this and the other expression evaluate to
43+
* logical true. Returns logical false if either evaluate to logical false.
44+
*
45+
* @param and the other boolean expression.
46+
* @return true if both true, false if either false.
47+
*/
48+
BooleanExpression and(BooleanExpression and);
49+
50+
/**
51+
* If this expression evaluates to logical true, returns the result of the
52+
* evaluated left branch expression. If this evaluates to logical false,
53+
* returns the result of the evaluated right branch expression.
54+
*
55+
* @param left the left branch expression
56+
* @param right the right branch expression
57+
* @return left if true, right if false.
58+
* @param <T> The type of the resulting expression.
59+
*/
60+
<T extends Expression> T cond(T left, T right);
61+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Copyright 2008-present MongoDB, Inc.
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+
17+
package com.mongodb.client.model.expressions;
18+
19+
/**
20+
* Expresses a date value.
21+
*/
22+
public interface DateExpression extends Expression {
23+
24+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Copyright 2008-present MongoDB, Inc.
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+
17+
package com.mongodb.client.model.expressions;
18+
19+
/**
20+
* Expresses a document value. A document is an ordered set of fields, where the
21+
* key is a string value, mapping to a value of any other expression type.
22+
*/
23+
public interface DocumentExpression extends Expression {
24+
25+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright 2008-present MongoDB, Inc.
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+
17+
package com.mongodb.client.model.expressions;
18+
19+
import com.mongodb.annotations.Evolving;
20+
21+
/**
22+
* Expressions express values that may be represented in (or computations that
23+
* may be performed within) a MongoDB server. Each expression evaluates to some
24+
* value, much like any Java expression evaluates to some value. Expressions may
25+
* be thought of as boxed values. Evaluation of an expression will usually occur
26+
* on a MongoDB server.
27+
*
28+
* <p>Users should treat these interfaces as sealed, and must not implement any
29+
* expression interfaces.
30+
*
31+
* <p>Expressions are typed. It is possible to execute expressions against data
32+
* that is of the wrong type, such as by applying the "not" boolean expression
33+
* to a document field that is an integer, null, or missing. This API does not
34+
* define the output in such cases (though the output may be defined within the
35+
* execution context - the server - where the expression is evaluated). Users of
36+
* this API must mitigate any risk of applying an expression to some type where
37+
* resulting behaviour is not defined by this API (for example, by checking for
38+
* null, by ensuring that field types are correctly specified). Likewise, unless
39+
* otherwise specified, this API does not define the order of evaluation for all
40+
* arguments, and whether all arguments to some expression will be evaluated.
41+
*
42+
* @see Expressions
43+
*/
44+
@Evolving
45+
public interface Expression {
46+
47+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright 2008-present MongoDB, Inc.
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+
17+
package com.mongodb.client.model.expressions;
18+
19+
import com.mongodb.annotations.Beta;
20+
import com.mongodb.annotations.Immutable;
21+
import com.mongodb.lang.Nullable;
22+
import org.bson.codecs.Codec;
23+
import org.bson.codecs.configuration.CodecProvider;
24+
import org.bson.codecs.configuration.CodecRegistry;
25+
26+
/**
27+
* Provides Codec instances for MQL expressions.
28+
*
29+
* <p>Responsible for converting values and computations expressed using the
30+
* driver's implementation of the {@link Expression} API into the corresponding
31+
* values and computations expressed in MQL BSON. Booleans are converted to BSON
32+
* booleans, documents to BSON documents, and so on. The specific structure
33+
* representing numbers is preserved where possible (that is, number literals
34+
* specified as Java longs are converted into BSON int64, and so on).
35+
*
36+
* <p>This API is marked Beta because it may be replaced with a generalized
37+
* mechanism for converting expressions. This would only affect users who use
38+
* MqlExpressionCodecProvider directly in custom codec providers. This Beta
39+
* annotation does not imply that the Expressions API in general is Beta.
40+
*/
41+
@Beta(Beta.Reason.CLIENT)
42+
@Immutable
43+
public final class ExpressionCodecProvider implements CodecProvider {
44+
@Override
45+
@SuppressWarnings("unchecked")
46+
@Nullable
47+
public <T> Codec<T> get(final Class<T> clazz, final CodecRegistry registry) {
48+
if (MqlExpression.class.equals(clazz)) {
49+
return (Codec<T>) new MqlExpressionCodec(registry);
50+
}
51+
return null;
52+
}
53+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Copyright 2008-present MongoDB, Inc.
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+
17+
package com.mongodb.client.model.expressions;
18+
19+
import org.bson.BsonBoolean;
20+
import org.bson.BsonInt32;
21+
import org.bson.BsonString;
22+
23+
/**
24+
* Convenience methods related to {@link Expression}.
25+
*/
26+
public final class Expressions {
27+
28+
private Expressions() {}
29+
30+
/**
31+
* Returns an expression having the same boolean value as the provided
32+
* boolean primitive.
33+
*
34+
* @param of the boolean primitive
35+
* @return the boolean expression
36+
*/
37+
public static BooleanExpression of(final boolean of) {
38+
// we intentionally disallow ofBoolean(null)
39+
return new MqlExpression<>((codecRegistry) -> new BsonBoolean(of));
40+
}
41+
42+
/**
43+
* Returns an expression having the same integer value as the provided
44+
* int primitive.
45+
*
46+
* @param of the int primitive
47+
* @return the integer expression
48+
*/
49+
public static IntegerExpression of(final int of) {
50+
return new MqlExpression<>((codecRegistry) -> new BsonInt32(of));
51+
}
52+
53+
/**
54+
* Returns an expression having the same string value as the provided
55+
* string.
56+
*
57+
* @param of the string
58+
* @return the string expression
59+
*/
60+
public static StringExpression of(final String of) {
61+
return new MqlExpression<>((codecRegistry) -> new BsonString(of));
62+
}
63+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Copyright 2008-present MongoDB, Inc.
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+
17+
package com.mongodb.client.model.expressions;
18+
19+
/**
20+
* Expresses an integer value.
21+
*/
22+
public interface IntegerExpression extends NumberExpression {
23+
24+
}

0 commit comments

Comments
 (0)