Skip to content

Commit e761f17

Browse files
authored
Added range index support for queryable encryption
Note: The Range algorithm is experimental only. It is not intended for public use. It is subject to breaking changes. JAVA-4625
1 parent ff83341 commit e761f17

File tree

92 files changed

+40720
-78
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

92 files changed

+40720
-78
lines changed

build.gradle

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ ext {
4949
zstdVersion = '1.5.2-5'
5050
awsSdkV2Version = '2.18.9'
5151
awsSdkV1Version = '1.12.337'
52-
mongoCryptVersion = '1.6.1'
52+
mongoCryptVersion = '1.7.1'
5353
projectReactorVersion = '2022.0.0'
5454
junitBomVersion = '5.8.2'
5555
gitVersion = getGitVersion()
@@ -244,6 +244,7 @@ configure(javaCodeCheckedProjects) {
244244
testImplementation(platform("org.junit:junit-bom:$junitBomVersion"))
245245
testImplementation('org.junit.jupiter:junit-jupiter')
246246
testImplementation('org.junit.jupiter:junit-jupiter-params')
247+
testImplementation('org.junit.jupiter:junit-jupiter-engine')
247248
testImplementation('org.junit.vintage:junit-vintage-engine')
248249

249250
testImplementation platform('org.spockframework:spock-bom:2.1-groovy-3.0')

driver-core/src/main/com/mongodb/client/model/vault/EncryptOptions.java

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public class EncryptOptions {
3131
private final String algorithm;
3232
private Long contentionFactor;
3333
private String queryType;
34+
private RangeOptions rangeOptions;
3435

3536
/**
3637
* Construct an instance with the given algorithm.
@@ -50,6 +51,7 @@ public EncryptOptions(final String algorithm) {
5051
* <li>AEAD_AES_256_CBC_HMAC_SHA_512-Random</li>
5152
* <li>Indexed</li>
5253
* <li>Unindexed</li>
54+
* <li>RangePreview</li>
5355
* </ul>
5456
*
5557
* @return the encryption algorithm
@@ -113,7 +115,8 @@ public EncryptOptions keyAltName(final String keyAltName) {
113115
/**
114116
* The contention factor.
115117
*
116-
* <p>It is an error to set contentionFactor when algorithm is not "Indexed".
118+
* <p>It is an error to set contentionFactor when algorithm is not "Indexed" or "RangePreview".
119+
* <p>Note: The Range algorithm is experimental only. It is not intended for public use. It is subject to breaking changes.</p>
117120
* @param contentionFactor the contention factor, which must be {@code >= 0} or null.
118121
* @return this
119122
* @since 4.7
@@ -140,9 +143,9 @@ public Long getContentionFactor() {
140143
/**
141144
* The QueryType.
142145
*
143-
* <p>Currently, we support only "equality" queryType.</p>
144-
* <p>It is an error to set queryType when the algorithm is not "Indexed".</p>
145-
*
146+
* <p>Currently, we support only "equality" or "RangePreview" queryType.</p>
147+
* <p>It is an error to set queryType when the algorithm is not "Indexed" or "RangePreview".</p>
148+
* <p>Note: The Range algorithm is experimental only. It is not intended for public use. It is subject to breaking changes.</p>
146149
* @param queryType the query type
147150
* @return this
148151
* @since 4.7
@@ -156,7 +159,7 @@ public EncryptOptions queryType(@Nullable final String queryType) {
156159
/**
157160
* Gets the QueryType.
158161
*
159-
* <p>Currently, we support only "equality" queryType.</p>
162+
* <p>Currently, we support only "equality" or "RangePreview" queryType.</p>
160163
* @see #queryType(String)
161164
* @return the queryType or null
162165
* @since 4.7
@@ -167,14 +170,45 @@ public String getQueryType() {
167170
return queryType;
168171
}
169172

173+
/**
174+
* The RangeOptions
175+
*
176+
* <p>It is an error to set RangeOptions when the algorithm is not "RangePreview".
177+
* <p>Note: The Range algorithm is experimental only. It is not intended for public use. It is subject to breaking changes.
178+
* @param rangeOptions the range options
179+
* @return this
180+
* @since 4.9
181+
* @mongodb.server.release 6.2
182+
* @mongodb.driver.manual /core/queryable-encryption/ queryable encryption
183+
*/
184+
@Beta(Beta.Reason.SERVER)
185+
public EncryptOptions rangeOptions(@Nullable final RangeOptions rangeOptions) {
186+
this.rangeOptions = rangeOptions;
187+
return this;
188+
}
189+
190+
/**
191+
* Gets the RangeOptions
192+
* @return the range options or null if not set
193+
* @since 4.9
194+
* @mongodb.server.release 6.2
195+
* @mongodb.driver.manual /core/queryable-encryption/ queryable encryption
196+
*/
197+
@Nullable
198+
@Beta(Beta.Reason.SERVER)
199+
public RangeOptions getRangeOptions() {
200+
return rangeOptions;
201+
}
202+
170203
@Override
171204
public String toString() {
172205
return "EncryptOptions{"
173206
+ "keyId=" + keyId
174207
+ ", keyAltName='" + keyAltName + '\''
175208
+ ", algorithm='" + algorithm + '\''
176209
+ ", contentionFactor=" + contentionFactor
177-
+ ", queryType=" + queryType
210+
+ ", queryType='" + queryType + '\''
211+
+ ", rangeOptions=" + rangeOptions
178212
+ '}';
179213
}
180214
}
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
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.vault;
18+
19+
import com.mongodb.annotations.Beta;
20+
import com.mongodb.lang.Nullable;
21+
import org.bson.BsonValue;
22+
23+
/**
24+
* Range options specifies index options for a Queryable Encryption field supporting "rangePreview" queries.
25+
*
26+
* <p>{@code min}, {@code max}, {@code sparsity}, and {@code precision} must match the values set in the {@code encryptedFields}
27+
* of the destination collection.
28+
*
29+
* <p>For {@code double} and {@code decimal128}, {@code min}/{@code max}/{@code precision} must all be set, or all be unset.
30+
*
31+
* <p>Note: The Range algorithm is experimental only. It is not intended for public use. It is subject to breaking changes.
32+
* @since 4.9
33+
* @mongodb.server.release 6.2
34+
* @mongodb.driver.manual /core/queryable-encryption/ queryable encryption
35+
*/
36+
@Beta(Beta.Reason.SERVER)
37+
public class RangeOptions {
38+
39+
private BsonValue min;
40+
private BsonValue max;
41+
private Long sparsity;
42+
private Integer precision;
43+
44+
/**
45+
* Construct a new instance
46+
*/
47+
public RangeOptions() {
48+
}
49+
50+
/**
51+
* Set the minimum value set in the encryptedFields of the destination collection.
52+
* @param min the minimum value
53+
* @return this
54+
*/
55+
public RangeOptions min(@Nullable final BsonValue min) {
56+
this.min = min;
57+
return this;
58+
}
59+
60+
/**
61+
* @return the minimum value if set
62+
*/
63+
@Nullable
64+
public BsonValue getMin() {
65+
return min;
66+
}
67+
68+
/**
69+
* Set the maximum value set in the encryptedFields of the destination collection.
70+
* @param max the maximum value
71+
* @return this
72+
*/
73+
public RangeOptions max(@Nullable final BsonValue max) {
74+
this.max = max;
75+
return this;
76+
}
77+
78+
/**
79+
* @return the maximum value if set
80+
*/
81+
@Nullable
82+
public BsonValue getMax() {
83+
return max;
84+
}
85+
86+
/**
87+
* Set the Queryable Encryption range hypergraph sparsity factor
88+
* @param sparsity the sparsity
89+
* @return this
90+
*/
91+
public RangeOptions sparsity(@Nullable final Long sparsity) {
92+
this.sparsity = sparsity;
93+
return this;
94+
}
95+
96+
/**
97+
* @return the sparsity value if set
98+
*/
99+
@Nullable
100+
public Long getSparsity() {
101+
return sparsity;
102+
}
103+
104+
/**
105+
* Set the precision of double or decimal128 values in the encryptedFields of the destination collection.
106+
* @param precision the precision
107+
* @return this
108+
*/
109+
public RangeOptions precision(@Nullable final Integer precision) {
110+
this.precision = precision;
111+
return this;
112+
}
113+
114+
/**
115+
* @return the precision value if set
116+
*/
117+
@Nullable
118+
public Integer getPrecision() {
119+
return precision;
120+
}
121+
122+
@Override
123+
public String toString() {
124+
return "RangeOptions{"
125+
+ "min=" + min
126+
+ ", max=" + max
127+
+ ", sparsity=" + sparsity
128+
+ ", precision=" + precision
129+
+ '}';
130+
}
131+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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+
package com.mongodb.internal.client.vault;
17+
18+
import com.mongodb.client.model.vault.EncryptOptions;
19+
import com.mongodb.client.model.vault.RangeOptions;
20+
import com.mongodb.crypt.capi.MongoExplicitEncryptOptions;
21+
import org.bson.BsonDocument;
22+
import org.bson.BsonInt32;
23+
import org.bson.BsonInt64;
24+
import org.bson.BsonValue;
25+
26+
public final class EncryptOptionsHelper {
27+
28+
public static MongoExplicitEncryptOptions asMongoExplicitEncryptOptions(final EncryptOptions options) {
29+
MongoExplicitEncryptOptions.Builder encryptOptionsBuilder = MongoExplicitEncryptOptions.builder()
30+
.algorithm(options.getAlgorithm());
31+
32+
if (options.getKeyId() != null) {
33+
encryptOptionsBuilder.keyId(options.getKeyId());
34+
}
35+
36+
if (options.getKeyAltName() != null) {
37+
encryptOptionsBuilder.keyAltName(options.getKeyAltName());
38+
}
39+
40+
if (options.getContentionFactor() != null) {
41+
encryptOptionsBuilder.contentionFactor(options.getContentionFactor());
42+
}
43+
44+
if (options.getQueryType() != null) {
45+
encryptOptionsBuilder.queryType(options.getQueryType());
46+
}
47+
48+
RangeOptions rangeOptions = options.getRangeOptions();
49+
if (rangeOptions != null) {
50+
BsonDocument rangeOptionsBsonDocument = new BsonDocument();
51+
BsonValue min = rangeOptions.getMin();
52+
if (min != null) {
53+
rangeOptionsBsonDocument.put("min", min);
54+
}
55+
BsonValue max = rangeOptions.getMax();
56+
if (max != null) {
57+
rangeOptionsBsonDocument.put("max", max);
58+
}
59+
Long sparsity = rangeOptions.getSparsity();
60+
if (sparsity != null) {
61+
rangeOptionsBsonDocument.put("sparsity", new BsonInt64(sparsity));
62+
}
63+
Integer precision = rangeOptions.getPrecision();
64+
if (precision != null) {
65+
rangeOptionsBsonDocument.put("precision", new BsonInt32(precision));
66+
}
67+
encryptOptionsBuilder.rangeOptions(rangeOptionsBsonDocument);
68+
}
69+
return encryptOptionsBuilder.build();
70+
}
71+
private EncryptOptionsHelper() {
72+
}
73+
}
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+
/**
18+
* This package contains classes that manage binding to MongoDB servers for various operations.
19+
*/
20+
21+
@NonNullApi
22+
package com.mongodb.internal.client.vault;
23+
24+
import com.mongodb.lang.NonNullApi;

0 commit comments

Comments
 (0)