Skip to content

Commit b7f2277

Browse files
committed
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 4de305e commit b7f2277

File tree

92 files changed

+40730
-81
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

+40730
-81
lines changed

build.gradle

Lines changed: 8 additions & 7 deletions
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.0-SNAPSHOT'
5353
projectReactorVersion = '2022.0.0'
5454
junitBomVersion = '5.8.2'
5555
gitVersion = getGitVersion()
@@ -76,12 +76,12 @@ configure(coreProjects) {
7676
mavenCentral()
7777

7878
// Uncomment this to test with a snapshot build of mongodb-crypt
79-
// maven {
80-
// url 'https://oss.sonatype.org/content/repositories/snapshots'
81-
// content {
82-
// includeGroup "org.mongodb"
83-
// }
84-
// }
79+
maven {
80+
url 'https://oss.sonatype.org/content/repositories/snapshots'
81+
content {
82+
includeGroup "org.mongodb"
83+
}
84+
}
8585
}
8686
}
8787

@@ -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: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
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.BsonDocument;
22+
import org.bson.BsonInt32;
23+
import org.bson.BsonInt64;
24+
import org.bson.BsonValue;
25+
26+
/**
27+
* Range options specifies index options for a Queryable Encryption field supporting "rangePreview" queries.
28+
*
29+
* <p>{@code min}, {@code max}, {@code sparsity}, and {@code range} must match the values set in the {@code encryptedFields}
30+
* of the destination collection.
31+
*
32+
* <p>For {@code double} and {@code decimal128}, {@code min}/{@code max}/{@code precision} must all be set, or all be unset.
33+
*
34+
* <p>Note: The Range algorithm is experimental only. It is not intended for public use. It is subject to breaking changes.
35+
* @since 4.9
36+
* @mongodb.server.release 6.2
37+
* @mongodb.driver.manual /core/queryable-encryption/ queryable encryption
38+
*/
39+
@Beta(Beta.Reason.SERVER)
40+
public class RangeOptions {
41+
42+
private BsonValue min;
43+
private BsonValue max;
44+
private Long sparsity;
45+
private Integer precision;
46+
47+
/**
48+
* Construct a new instance
49+
*/
50+
public RangeOptions() {
51+
}
52+
53+
/**
54+
* Set the minimum value set in the encryptedFields of the destination collection.
55+
* @param min the minimum value
56+
* @return this
57+
*/
58+
public RangeOptions min(@Nullable final BsonValue min) {
59+
this.min = min;
60+
return this;
61+
}
62+
63+
/**
64+
* @return the minimum value if set
65+
*/
66+
@Nullable
67+
public BsonValue getMin() {
68+
return min;
69+
}
70+
71+
/**
72+
* Set the maximum value set in the encryptedFields of the destination collection.
73+
* @param max the maximum value
74+
* @return this
75+
*/
76+
public RangeOptions max(@Nullable final BsonValue max) {
77+
this.max = max;
78+
return this;
79+
}
80+
81+
/**
82+
* @return the maximum value if set
83+
*/
84+
@Nullable
85+
public BsonValue getMax() {
86+
return max;
87+
}
88+
89+
/**
90+
* Set the Queryable Encryption range hypergraph sparsity factor
91+
* @param sparsity the sparsity
92+
* @return this
93+
*/
94+
public RangeOptions sparsity(@Nullable final Long sparsity) {
95+
this.sparsity = sparsity;
96+
return this;
97+
}
98+
99+
/**
100+
* @return the sparsity value if set
101+
*/
102+
@Nullable
103+
public Long getSparsity() {
104+
return sparsity;
105+
}
106+
107+
/**
108+
* Set the precision of double or decimal128 values in the encryptedFields of the destination collection.
109+
* @param precision the precision
110+
* @return this
111+
*/
112+
public RangeOptions precision(@Nullable final Integer precision) {
113+
this.precision = precision;
114+
return this;
115+
}
116+
117+
/**
118+
* @return the precision value if set
119+
*/
120+
@Nullable
121+
public Integer getPrecision() {
122+
return precision;
123+
}
124+
125+
/**
126+
* @return a BsonDocument representation of the RangeOptions
127+
*/
128+
@Beta(Beta.Reason.CLIENT)
129+
public BsonDocument asBsonDocument() {
130+
BsonDocument rangeOptions = new BsonDocument();
131+
if (min != null) {
132+
rangeOptions.put("min", min);
133+
}
134+
if (max != null) {
135+
rangeOptions.put("max", max);
136+
}
137+
if (sparsity != null) {
138+
rangeOptions.put("sparsity", new BsonInt64(sparsity));
139+
}
140+
if (precision != null) {
141+
rangeOptions.put("precision", new BsonInt32(precision));
142+
}
143+
return rangeOptions;
144+
}
145+
146+
@Override
147+
public String toString() {
148+
return "RangeOptions{"
149+
+ "min=" + min
150+
+ ", max=" + max
151+
+ ", sparsity=" + sparsity
152+
+ ", precision=" + precision
153+
+ '}';
154+
}
155+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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+
22+
public final class EncryptOptionsHelper {
23+
24+
public static MongoExplicitEncryptOptions asMongoExplicitEncryptOptions(final EncryptOptions options) {
25+
MongoExplicitEncryptOptions.Builder encryptOptionsBuilder = MongoExplicitEncryptOptions.builder()
26+
.algorithm(options.getAlgorithm());
27+
28+
if (options.getKeyId() != null) {
29+
encryptOptionsBuilder.keyId(options.getKeyId());
30+
}
31+
32+
if (options.getKeyAltName() != null) {
33+
encryptOptionsBuilder.keyAltName(options.getKeyAltName());
34+
}
35+
36+
if (options.getContentionFactor() != null) {
37+
encryptOptionsBuilder.contentionFactor(options.getContentionFactor());
38+
}
39+
40+
if (options.getQueryType() != null) {
41+
encryptOptionsBuilder.queryType(options.getQueryType());
42+
}
43+
44+
RangeOptions rangeOptions = options.getRangeOptions();
45+
if (rangeOptions != null) {
46+
encryptOptionsBuilder.rangeOptions(rangeOptions.asBsonDocument());
47+
}
48+
return encryptOptionsBuilder.build();
49+
}
50+
private EncryptOptionsHelper() {
51+
}
52+
}
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)