Skip to content

Commit 84910ac

Browse files
authored
Support creation of time-series collections (#723)
JAVA-3997
1 parent 0fd3b04 commit 84910ac

File tree

11 files changed

+697
-5
lines changed

11 files changed

+697
-5
lines changed

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

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

22+
import java.util.concurrent.TimeUnit;
23+
2224
import static com.mongodb.assertions.Assertions.notNull;
2325

2426
/**
2527
* Options for creating a collection
2628
*
2729
* @mongodb.driver.manual reference/method/db.createCollection/ Create Collection
30+
* @mongodb.driver.manual core/timeseries-collections/ Time-series collections
2831
* @since 3.0
2932
*/
3033
public class CreateCollectionOptions {
@@ -35,6 +38,8 @@ public class CreateCollectionOptions {
3538
private IndexOptionDefaults indexOptionDefaults = new IndexOptionDefaults();
3639
private ValidationOptions validationOptions = new ValidationOptions();
3740
private Collation collation;
41+
private long expireAfterSeconds;
42+
private TimeSeriesOptions timeSeriesOptions;
3843

3944
/**
4045
* Gets the maximum number of documents allowed in a capped collection.
@@ -194,6 +199,71 @@ public CreateCollectionOptions collation(@Nullable final Collation collation) {
194199
return this;
195200
}
196201

202+
/**
203+
* Returns the expire-after option. The default value is 0, which indicates no expiration.
204+
*
205+
* @param timeUnit the time unit
206+
* @return the expire-after option, which may be null.
207+
* @since 4.3
208+
* @mongodb.driver.manual core/timeseries-collections/ Time-series collections
209+
*/
210+
public long getExpireAfter(final TimeUnit timeUnit) {
211+
notNull("timeUnit", timeUnit);
212+
return timeUnit.convert(expireAfterSeconds, TimeUnit.SECONDS);
213+
}
214+
215+
/**
216+
* Sets the expire-after option.
217+
*
218+
* <p>
219+
* A duration indicating after how long old time-series data should be deleted. Partial seconds are ignored.
220+
* </p>
221+
* <p>
222+
* Currently applies only to time-series collections, so if this value is set then so must the time-series options
223+
* </p>
224+
* @param expireAfter the expire-after duration. After conversion to seconds using
225+
* {@link TimeUnit#convert(long, java.util.concurrent.TimeUnit)}, the value must be &gt;= 0. A value of 0 indicates no expiration.
226+
* @param timeUnit the time unit
227+
* @return this
228+
* @since 4.3
229+
* @see #timeSeriesOptions(TimeSeriesOptions)
230+
* @mongodb.driver.manual core/timeseries-collections/ Time-series collections
231+
*/
232+
public CreateCollectionOptions expireAfter(final long expireAfter, final TimeUnit timeUnit) {
233+
notNull("timeUnit", timeUnit);
234+
long asSeconds = TimeUnit.SECONDS.convert(expireAfter, timeUnit);
235+
if (asSeconds < 0) {
236+
throw new IllegalArgumentException("expireAfter, after conversion to seconds, must be >= 0");
237+
}
238+
this.expireAfterSeconds = asSeconds;
239+
return this;
240+
}
241+
242+
/**
243+
* Gets the time series collection options.
244+
*
245+
* @return the options for a time-series collection
246+
* @since 4.3
247+
* @mongodb.driver.manual core/timeseries-collections/ Time-series collections
248+
*/
249+
@Nullable
250+
public TimeSeriesOptions getTimeSeriesOptions() {
251+
return timeSeriesOptions;
252+
}
253+
254+
/**
255+
* Sets the time-series collection options.
256+
*
257+
* @param timeSeriesOptions the time-series options
258+
* @return this
259+
* @since 4.3
260+
* @mongodb.driver.manual core/timeseries-collections/ Time-series collections
261+
*/
262+
public CreateCollectionOptions timeSeriesOptions(final TimeSeriesOptions timeSeriesOptions) {
263+
this.timeSeriesOptions = timeSeriesOptions;
264+
return this;
265+
}
266+
197267
@Override
198268
public String toString() {
199269
return "CreateCollectionOptions{"
@@ -204,6 +274,8 @@ public String toString() {
204274
+ ", indexOptionDefaults=" + indexOptionDefaults
205275
+ ", validationOptions=" + validationOptions
206276
+ ", collation=" + collation
277+
+ ", expireAfterSeconds=" + expireAfterSeconds
278+
+ ", timeSeriesOptions=" + timeSeriesOptions
207279
+ '}';
208280
}
209281
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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;
18+
19+
/**
20+
* An enumeration of time-series data granularity.
21+
* <p>
22+
* It describes the units one would use to describe the expected interval between subsequent measurements for a time-series.
23+
* </p>
24+
* @since 4.3
25+
* @see TimeSeriesOptions
26+
* @see CreateCollectionOptions
27+
*/
28+
public enum TimeSeriesGranularity {
29+
/**
30+
* Seconds-level granularity.
31+
* <p>
32+
* If granularity of a time-series collection is unspecified, this is the default value.
33+
* </p>
34+
*/
35+
SECONDS,
36+
37+
/**
38+
* Minutes-level granularity.
39+
*/
40+
MINUTES,
41+
42+
/**
43+
* Hours-level granularity.
44+
*/
45+
HOURS
46+
}
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
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;
18+
19+
import com.mongodb.lang.Nullable;
20+
21+
import static com.mongodb.assertions.Assertions.notNull;
22+
23+
/**
24+
* Options related to the creation of time-series collections.
25+
*
26+
* @since 4.3
27+
* @see CreateCollectionOptions
28+
* @mongodb.driver.manual core/timeseries-collections/ Time-series collections
29+
*/
30+
public final class TimeSeriesOptions {
31+
private final String timeField;
32+
private String metaField;
33+
private TimeSeriesGranularity granularity;
34+
35+
/**
36+
* Construct a new instance.
37+
*
38+
* @param timeField the name of the top-level field to be used for time. Inserted documents must have this field, and the field must be
39+
* of the BSON datetime type.
40+
*/
41+
public TimeSeriesOptions(final String timeField) {
42+
this.timeField = notNull("timeField", timeField);
43+
}
44+
45+
/**
46+
* Gets the name of the field holding the time value.
47+
*
48+
* @return the name of the field holding the time value.
49+
*/
50+
public String getTimeField() {
51+
return timeField;
52+
}
53+
54+
/**
55+
* Gets the name of the meta field.
56+
*
57+
* @return the name of the meta field
58+
* @see #metaField(String)
59+
*/
60+
@Nullable
61+
public String getMetaField() {
62+
return metaField;
63+
}
64+
65+
/**
66+
* Sets the name of the meta field.
67+
* <p>
68+
* The name of the field which contains metadata in each time series document. The metadata in the specified field should be data
69+
* that is used to label a unique series of documents. The metadata should rarely, if ever, change. This field is used to group
70+
* related data and may be of any BSON type, except for array. This name may not be the same as the {@code timeField} or "_id".
71+
* </p>
72+
* @param metaField the name of the meta field
73+
* @return this
74+
* @see #getMetaField()
75+
*/
76+
public TimeSeriesOptions metaField(@Nullable final String metaField) {
77+
this.metaField = metaField;
78+
return this;
79+
}
80+
81+
/**
82+
* Gets the granularity of the time-series data.
83+
*
84+
* @return the time-series granularity
85+
* @see #granularity(TimeSeriesGranularity)
86+
*/
87+
@Nullable
88+
public TimeSeriesGranularity getGranularity() {
89+
return granularity;
90+
}
91+
92+
/**
93+
* Sets the granularity of the time-series data.
94+
* <p>
95+
* The default value is {@link TimeSeriesGranularity#SECONDS}.
96+
* </p>
97+
*
98+
* @param granularity the time-series granularity
99+
* @return this
100+
* @see #getGranularity()
101+
*/
102+
public TimeSeriesOptions granularity(@Nullable final TimeSeriesGranularity granularity) {
103+
this.granularity = granularity;
104+
return this;
105+
}
106+
107+
@Override
108+
public String toString() {
109+
return "TimeSeriesOptions{"
110+
+ "timeField='" + timeField + '\''
111+
+ ", metaField='" + metaField + '\''
112+
+ ", granularity=" + granularity
113+
+ '}';
114+
}
115+
}

driver-core/src/main/com/mongodb/internal/operation/CreateCollectionOperation.java

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,19 @@
1717
package com.mongodb.internal.operation;
1818

1919
import com.mongodb.WriteConcern;
20-
import com.mongodb.internal.async.SingleResultCallback;
2120
import com.mongodb.client.model.Collation;
21+
import com.mongodb.client.model.TimeSeriesGranularity;
22+
import com.mongodb.client.model.TimeSeriesOptions;
2223
import com.mongodb.client.model.ValidationAction;
2324
import com.mongodb.client.model.ValidationLevel;
2425
import com.mongodb.connection.ConnectionDescription;
26+
import com.mongodb.internal.async.SingleResultCallback;
2527
import com.mongodb.internal.binding.AsyncWriteBinding;
2628
import com.mongodb.internal.binding.WriteBinding;
2729
import com.mongodb.internal.connection.AsyncConnection;
2830
import com.mongodb.internal.connection.Connection;
2931
import com.mongodb.internal.operation.OperationHelper.CallableWithConnection;
32+
import com.mongodb.lang.Nullable;
3033
import org.bson.BsonBoolean;
3134
import org.bson.BsonDocument;
3235
import org.bson.BsonString;
@@ -67,6 +70,8 @@ public class CreateCollectionOperation implements AsyncWriteOperation<Void>, Wri
6770
private ValidationLevel validationLevel = null;
6871
private ValidationAction validationAction = null;
6972
private Collation collation = null;
73+
private long expireAfterSeconds;
74+
private TimeSeriesOptions timeSeriesOptions;
7075

7176
/**
7277
* Construct a new instance.
@@ -341,6 +346,16 @@ public CreateCollectionOperation collation(final Collation collation) {
341346
return this;
342347
}
343348

349+
public CreateCollectionOperation expireAfter(final long expireAfterSeconds) {
350+
this.expireAfterSeconds = expireAfterSeconds;
351+
return this;
352+
}
353+
354+
public CreateCollectionOperation timeSeriesOptions(@Nullable final TimeSeriesOptions timeSeriesOptions) {
355+
this.timeSeriesOptions = timeSeriesOptions;
356+
return this;
357+
}
358+
344359
@Override
345360
public Void execute(final WriteBinding binding) {
346361
return withConnection(binding, new CallableWithConnection<Void>() {
@@ -407,7 +422,32 @@ private BsonDocument getCommand(final ConnectionDescription description) {
407422
if (collation != null) {
408423
document.put("collation", collation.asDocument());
409424
}
425+
putIfNotZero(document, "expireAfterSeconds", expireAfterSeconds);
426+
if (timeSeriesOptions != null) {
427+
BsonDocument timeSeriesDocument = new BsonDocument("timeField", new BsonString(timeSeriesOptions.getTimeField()));
428+
String metaField = timeSeriesOptions.getMetaField();
429+
if (metaField != null) {
430+
timeSeriesDocument.put("metaField", new BsonString(metaField));
431+
}
432+
TimeSeriesGranularity granularity = timeSeriesOptions.getGranularity();
433+
if (granularity != null) {
434+
timeSeriesDocument.put("granularity", new BsonString(getGranularityAsString(granularity)));
435+
}
436+
document.put("timeseries", timeSeriesDocument);
437+
}
410438
return document;
411439
}
412440

441+
private String getGranularityAsString(final TimeSeriesGranularity granularity) {
442+
switch (granularity) {
443+
case SECONDS:
444+
return "seconds";
445+
case MINUTES:
446+
return "minutes";
447+
case HOURS:
448+
return "hours";
449+
default:
450+
throw new AssertionError("Unexpected granularity " + granularity);
451+
}
452+
}
413453
}

0 commit comments

Comments
 (0)