23
23
import org .springframework .data .mongodb .core .ReadConcernAware ;
24
24
import org .springframework .data .mongodb .core .ReadPreferenceAware ;
25
25
import org .springframework .data .mongodb .core .query .Collation ;
26
+ import org .springframework .data .mongodb .core .query .DiskUse ;
26
27
import org .springframework .data .mongodb .util .BsonUtils ;
27
28
import org .springframework .lang .Contract ;
28
29
import org .springframework .util .Assert ;
@@ -60,7 +61,7 @@ public class AggregationOptions implements ReadConcernAware, ReadPreferenceAware
60
61
private static final String MAX_TIME = "maxTimeMS" ;
61
62
private static final String HINT = "hint" ;
62
63
63
- private final Optional < Boolean > allowDiskUse ;
64
+ private final DiskUse diskUse ;
64
65
private final boolean explain ;
65
66
private final Optional <Document > cursor ;
66
67
private final Optional <Collation > collation ;
@@ -85,6 +86,15 @@ public AggregationOptions(boolean allowDiskUse, boolean explain, @Nullable Docum
85
86
this (allowDiskUse , explain , cursor , null );
86
87
}
87
88
89
+ public AggregationOptions (DiskUse diskUse , boolean explain , @ Nullable Document cursor ) {
90
+ this (diskUse , explain , cursor , null );
91
+ }
92
+
93
+ public AggregationOptions (DiskUse allowDiskUse , boolean explain , @ Nullable Document cursor ,
94
+ @ Nullable Collation collation ) {
95
+ this (allowDiskUse , explain , cursor , collation , null );
96
+ }
97
+
88
98
/**
89
99
* Creates a new {@link AggregationOptions}.
90
100
*
@@ -97,7 +107,7 @@ public AggregationOptions(boolean allowDiskUse, boolean explain, @Nullable Docum
97
107
*/
98
108
public AggregationOptions (boolean allowDiskUse , boolean explain , @ Nullable Document cursor ,
99
109
@ Nullable Collation collation ) {
100
- this (allowDiskUse , explain , cursor , collation , null , null );
110
+ this (DiskUse . of ( allowDiskUse ) , explain , cursor , collation , null , null );
101
111
}
102
112
103
113
/**
@@ -113,13 +123,18 @@ public AggregationOptions(boolean allowDiskUse, boolean explain, @Nullable Docum
113
123
*/
114
124
public AggregationOptions (boolean allowDiskUse , boolean explain , @ Nullable Document cursor ,
115
125
@ Nullable Collation collation , @ Nullable String comment ) {
126
+ this (allowDiskUse ? DiskUse .ALLOW : DiskUse .DENY , explain , cursor , collation , comment , null );
127
+ }
128
+
129
+ public AggregationOptions (DiskUse allowDiskUse , boolean explain , @ Nullable Document cursor ,
130
+ @ Nullable Collation collation , @ Nullable String comment ) {
116
131
this (allowDiskUse , explain , cursor , collation , comment , null );
117
132
}
118
133
119
134
/**
120
135
* Creates a new {@link AggregationOptions}.
121
136
*
122
- * @param allowDiskUse whether to off-load intensive sort-operations to disk.
137
+ * @param diskUse whether to off-load intensive sort-operations to disk.
123
138
* @param explain whether to get the execution plan for the aggregation instead of the actual results.
124
139
* @param cursor can be {@literal null}, used to pass additional options (such as {@code batchSize}) to the
125
140
* aggregation.
@@ -128,10 +143,10 @@ public AggregationOptions(boolean allowDiskUse, boolean explain, @Nullable Docum
128
143
* @param hint can be {@literal null}, used to provide an index that would be forcibly used by query optimizer.
129
144
* @since 3.1
130
145
*/
131
- private AggregationOptions (@ Nullable Boolean allowDiskUse , boolean explain , @ Nullable Document cursor ,
146
+ private AggregationOptions (DiskUse diskUse , boolean explain , @ Nullable Document cursor ,
132
147
@ Nullable Collation collation , @ Nullable String comment , @ Nullable Object hint ) {
133
148
134
- this .allowDiskUse = Optional . ofNullable ( allowDiskUse ) ;
149
+ this .diskUse = diskUse ;
135
150
this .explain = explain ;
136
151
this .cursor = Optional .ofNullable (cursor );
137
152
this .collation = Optional .ofNullable (collation );
@@ -172,7 +187,7 @@ public static AggregationOptions fromDocument(Document document) {
172
187
String comment = document .getString (COMMENT );
173
188
Document hint = document .get (HINT , Document .class );
174
189
175
- AggregationOptions options = new AggregationOptions (allowDiskUse , explain , cursor , collation , comment , hint );
190
+ AggregationOptions options = new AggregationOptions (DiskUse . of ( allowDiskUse ) , explain , cursor , collation , comment , hint );
176
191
if (document .containsKey (MAX_TIME )) {
177
192
options .maxTime = Duration .ofMillis (document .getLong (MAX_TIME ));
178
193
}
@@ -196,7 +211,7 @@ public static Builder builder() {
196
211
* @return {@literal true} if enabled; {@literal false} otherwise (or if not set).
197
212
*/
198
213
public boolean isAllowDiskUse () {
199
- return allowDiskUse . orElse ( false );
214
+ return diskUse . equals ( DiskUse . ALLOW );
200
215
}
201
216
202
217
/**
@@ -206,7 +221,7 @@ public boolean isAllowDiskUse() {
206
221
* @since 4.2.5
207
222
*/
208
223
public boolean isAllowDiskUseSet () {
209
- return allowDiskUse . isPresent ( );
224
+ return ! diskUse . equals ( DiskUse . DEFAULT );
210
225
}
211
226
212
227
/**
@@ -427,7 +442,7 @@ static Document createCursor(int cursorBatchSize) {
427
442
*/
428
443
public static class Builder {
429
444
430
- private @ Nullable Boolean allowDiskUse ;
445
+ private @ Nullable DiskUse diskUse = DiskUse . DEFAULT ;
431
446
private boolean explain ;
432
447
private @ Nullable Document cursor ;
433
448
private @ Nullable Collation collation ;
@@ -447,8 +462,19 @@ public static class Builder {
447
462
*/
448
463
@ Contract ("_ -> this" )
449
464
public Builder allowDiskUse (boolean allowDiskUse ) {
465
+ return diskUse (DiskUse .of (allowDiskUse ));
466
+ }
467
+
468
+ /**
469
+ * Defines whether to off-load intensive sort-operations to disk.
470
+ *
471
+ * @param diskUse use {@literal true} to allow disk use during the aggregation.
472
+ * @return this.
473
+ */
474
+ @ Contract ("_ -> this" )
475
+ public Builder diskUse (DiskUse diskUse ) {
450
476
451
- this .allowDiskUse = allowDiskUse ;
477
+ this .diskUse = diskUse ;
452
478
return this ;
453
479
}
454
480
@@ -655,7 +681,7 @@ public Builder noMapping() {
655
681
@ Contract ("-> new" )
656
682
public AggregationOptions build () {
657
683
658
- AggregationOptions options = new AggregationOptions (allowDiskUse , explain , cursor , collation , comment , hint );
684
+ AggregationOptions options = new AggregationOptions (diskUse , explain , cursor , collation , comment , hint );
659
685
if (maxTime != null ) {
660
686
options .maxTime = maxTime ;
661
687
}
0 commit comments