17
17
18
18
import java .util .Optional ;
19
19
20
+ import org .springframework .data .mongodb .core .mapping .Field ;
20
21
import org .springframework .data .mongodb .core .query .Collation ;
21
22
import org .springframework .data .mongodb .core .schema .MongoJsonSchema ;
23
+ import org .springframework .data .mongodb .core .timeseries .Granularities ;
24
+ import org .springframework .data .mongodb .core .timeseries .Granularity ;
22
25
import org .springframework .data .mongodb .core .validation .Validator ;
23
26
import org .springframework .data .util .Optionals ;
24
27
import org .springframework .lang .Nullable ;
@@ -42,6 +45,7 @@ public class CollectionOptions {
42
45
private @ Nullable Boolean capped ;
43
46
private @ Nullable Collation collation ;
44
47
private ValidationOptions validationOptions ;
48
+ private @ Nullable TimeSeriesOptions timeSeriesOptions ;
45
49
46
50
/**
47
51
* Constructs a new <code>CollectionOptions</code> instance.
@@ -54,17 +58,19 @@ public class CollectionOptions {
54
58
*/
55
59
@ Deprecated
56
60
public CollectionOptions (@ Nullable Long size , @ Nullable Long maxDocuments , @ Nullable Boolean capped ) {
57
- this (size , maxDocuments , capped , null , ValidationOptions .none ());
61
+ this (size , maxDocuments , capped , null , ValidationOptions .none (), null );
58
62
}
59
63
60
64
private CollectionOptions (@ Nullable Long size , @ Nullable Long maxDocuments , @ Nullable Boolean capped ,
61
- @ Nullable Collation collation , ValidationOptions validationOptions ) {
65
+ @ Nullable Collation collation , ValidationOptions validationOptions ,
66
+ @ Nullable TimeSeriesOptions timeSeriesOptions ) {
62
67
63
68
this .maxDocuments = maxDocuments ;
64
69
this .size = size ;
65
70
this .capped = capped ;
66
71
this .collation = collation ;
67
72
this .validationOptions = validationOptions ;
73
+ this .timeSeriesOptions = timeSeriesOptions ;
68
74
}
69
75
70
76
/**
@@ -78,7 +84,7 @@ public static CollectionOptions just(Collation collation) {
78
84
79
85
Assert .notNull (collation , "Collation must not be null!" );
80
86
81
- return new CollectionOptions (null , null , null , collation , ValidationOptions .none ());
87
+ return new CollectionOptions (null , null , null , collation , ValidationOptions .none (), null );
82
88
}
83
89
84
90
/**
@@ -88,7 +94,21 @@ public static CollectionOptions just(Collation collation) {
88
94
* @since 2.0
89
95
*/
90
96
public static CollectionOptions empty () {
91
- return new CollectionOptions (null , null , null , null , ValidationOptions .none ());
97
+ return new CollectionOptions (null , null , null , null , ValidationOptions .none (), null );
98
+ }
99
+
100
+ /**
101
+ * Quick way to set up {@link CollectionOptions} for a Time Series collection. For more advanced settings use
102
+ * {@link #timeSeries(TimeSeriesOptions)}.
103
+ *
104
+ * @param timeField The name of the property which contains the date in each time series document. Must not be
105
+ * {@literal null}.
106
+ * @return new instance of {@link CollectionOptions}.
107
+ * @see #timeSeries(TimeSeriesOptions)
108
+ * @since 3.3
109
+ */
110
+ public static CollectionOptions timeSeries (String timeField ) {
111
+ return empty ().timeSeries (TimeSeriesOptions .timeSeries (timeField ));
92
112
}
93
113
94
114
/**
@@ -99,7 +119,7 @@ public static CollectionOptions empty() {
99
119
* @since 2.0
100
120
*/
101
121
public CollectionOptions capped () {
102
- return new CollectionOptions (size , maxDocuments , true , collation , validationOptions );
122
+ return new CollectionOptions (size , maxDocuments , true , collation , validationOptions , null );
103
123
}
104
124
105
125
/**
@@ -110,7 +130,7 @@ public CollectionOptions capped() {
110
130
* @since 2.0
111
131
*/
112
132
public CollectionOptions maxDocuments (long maxDocuments ) {
113
- return new CollectionOptions (size , maxDocuments , capped , collation , validationOptions );
133
+ return new CollectionOptions (size , maxDocuments , capped , collation , validationOptions , timeSeriesOptions );
114
134
}
115
135
116
136
/**
@@ -121,7 +141,7 @@ public CollectionOptions maxDocuments(long maxDocuments) {
121
141
* @since 2.0
122
142
*/
123
143
public CollectionOptions size (long size ) {
124
- return new CollectionOptions (size , maxDocuments , capped , collation , validationOptions );
144
+ return new CollectionOptions (size , maxDocuments , capped , collation , validationOptions , timeSeriesOptions );
125
145
}
126
146
127
147
/**
@@ -132,7 +152,7 @@ public CollectionOptions size(long size) {
132
152
* @since 2.0
133
153
*/
134
154
public CollectionOptions collation (@ Nullable Collation collation ) {
135
- return new CollectionOptions (size , maxDocuments , capped , collation , validationOptions );
155
+ return new CollectionOptions (size , maxDocuments , capped , collation , validationOptions , timeSeriesOptions );
136
156
}
137
157
138
158
/**
@@ -252,7 +272,20 @@ public CollectionOptions schemaValidationAction(ValidationAction validationActio
252
272
public CollectionOptions validation (ValidationOptions validationOptions ) {
253
273
254
274
Assert .notNull (validationOptions , "ValidationOptions must not be null!" );
255
- return new CollectionOptions (size , maxDocuments , capped , collation , validationOptions );
275
+ return new CollectionOptions (size , maxDocuments , capped , collation , validationOptions , timeSeriesOptions );
276
+ }
277
+
278
+ /**
279
+ * Create new {@link CollectionOptions} with the given {@link TimeSeriesOptions}.
280
+ *
281
+ * @param timeSeriesOptions must not be {@literal null}.
282
+ * @return new instance of {@link CollectionOptions}.
283
+ * @since 3.3
284
+ */
285
+ public CollectionOptions timeSeries (TimeSeriesOptions timeSeriesOptions ) {
286
+
287
+ Assert .notNull (timeSeriesOptions , "TimeSeriesOptions must not be null!" );
288
+ return new CollectionOptions (size , maxDocuments , capped , collation , validationOptions , timeSeriesOptions );
256
289
}
257
290
258
291
/**
@@ -303,6 +336,16 @@ public Optional<ValidationOptions> getValidationOptions() {
303
336
return validationOptions .isEmpty () ? Optional .empty () : Optional .of (validationOptions );
304
337
}
305
338
339
+ /**
340
+ * Get the {@link TimeSeriesOptions} if available.
341
+ *
342
+ * @return {@link Optional#empty()} if not specified.
343
+ * @since 3.3
344
+ */
345
+ public Optional <TimeSeriesOptions > getTimeSeriesOptions () {
346
+ return Optional .ofNullable (timeSeriesOptions );
347
+ }
348
+
306
349
/**
307
350
* Encapsulation of ValidationOptions options.
308
351
*
@@ -398,4 +441,87 @@ boolean isEmpty() {
398
441
return !Optionals .isAnyPresent (getValidator (), getValidationAction (), getValidationLevel ());
399
442
}
400
443
}
444
+
445
+ /**
446
+ * Options applicable to Time Series collections.
447
+ *
448
+ * @author Christoph Strobl
449
+ * @since 3.3
450
+ * @see <a href=
451
+ * "https://docs.mongodb.com/manual/core/timeseries-collections">https://docs.mongodb.com/manual/core/timeseries-collections</a>
452
+ */
453
+ public static class TimeSeriesOptions {
454
+
455
+ private final String timeField ;
456
+
457
+ @ Nullable //
458
+ private String metaField ;
459
+
460
+ private Granularity granularity ;
461
+
462
+ private TimeSeriesOptions (String timeField , @ Nullable String metaField , Granularity granularity ) {
463
+
464
+ this .timeField = timeField ;
465
+ this .metaField = metaField ;
466
+ this .granularity = granularity ;
467
+ }
468
+
469
+ /**
470
+ * Create a new instance of {@link TimeSeriesOptions} using the given field as its {@literal timeField}. The one,
471
+ * that contains the date in each time series document. <br />
472
+ * {@link Field#name() Annotated fieldnames} will be considered during the mapping process.
473
+ *
474
+ * @param timeField must not be {@literal null}.
475
+ * @return new instance of {@link TimeSeriesOptions}.
476
+ */
477
+ public static TimeSeriesOptions timeSeries (String timeField ) {
478
+ return new TimeSeriesOptions (timeField , null , Granularities .DEFAULT );
479
+ }
480
+
481
+ /**
482
+ * Set the name of the field which contains metadata in each time series document. Should not be the {@literal id}
483
+ * nor {@link TimeSeriesOptions#timeSeries(String)} timeField} nor point to an {@literal array} or
484
+ * {@link java.util.Collection}. <br />
485
+ * {@link Field#name() Annotated fieldnames} will be considered during the mapping process.
486
+ *
487
+ * @param metaField must not be {@literal null}.
488
+ * @return new instance of {@link TimeSeriesOptions}.
489
+ */
490
+ public TimeSeriesOptions metaField (String metaField ) {
491
+ return new TimeSeriesOptions (timeField , metaField , granularity );
492
+ }
493
+
494
+ /**
495
+ * Select the {@link Granularity} parameter to define how data in the time series collection is organized. Select
496
+ * one that is closest to the time span between incoming measurements.
497
+ *
498
+ * @return new instance of {@link TimeSeriesOptions}.
499
+ */
500
+ public TimeSeriesOptions granularity (Granularity granularity ) {
501
+ return new TimeSeriesOptions (timeField , metaField , granularity );
502
+ }
503
+
504
+ /**
505
+ * @return never {@literal null}.
506
+ */
507
+ public String getTimeField () {
508
+ return timeField ;
509
+ }
510
+
511
+ /**
512
+ * @return can be {@literal null}. Might be an {@literal empty} {@link String} as well, so maybe check via
513
+ * {@link org.springframework.util.StringUtils#hasText(String)}.
514
+ */
515
+ @ Nullable
516
+ public String getMetaField () {
517
+ return metaField ;
518
+ }
519
+
520
+ /**
521
+ * @return never {@literal null}.
522
+ */
523
+ public Granularity getGranularity () {
524
+ return granularity ;
525
+ }
526
+ }
401
527
}
0 commit comments