16
16
17
17
import static com .google .firebase .firestore .model .Values .max ;
18
18
import static com .google .firebase .firestore .model .Values .min ;
19
+ import static com .google .firebase .firestore .util .Assert .fail ;
19
20
20
21
import androidx .annotation .Nullable ;
21
- import com .google .firebase .firestore .core .OrderBy .Direction ;
22
22
import com .google .firebase .firestore .model .DocumentKey ;
23
23
import com .google .firebase .firestore .model .FieldIndex ;
24
24
import com .google .firebase .firestore .model .ResourcePath ;
25
25
import com .google .firebase .firestore .model .Values ;
26
- import com .google .firestore .v1 .ArrayValue ;
27
26
import com .google .firestore .v1 .Value ;
28
27
import java .util .ArrayList ;
28
+ import java .util .Collections ;
29
29
import java .util .List ;
30
30
31
31
/**
@@ -115,6 +115,27 @@ public boolean hasLimit() {
115
115
return endAt ;
116
116
}
117
117
118
+ /** Returns the list of values that are used in ARRAY_CONTAINS and ARRAY_CONTAINS_ANY filter. */
119
+ public List <Value > getArrayValues (FieldIndex fieldIndex ) {
120
+ for (FieldIndex .Segment segment : fieldIndex .getArraySegments ()) {
121
+ for (Filter filter : filters ) {
122
+ if (filter .getField ().equals (segment .getFieldPath ())) {
123
+ FieldFilter fieldFilter = (FieldFilter ) filter ;
124
+ switch (fieldFilter .getOperator ()) {
125
+ case ARRAY_CONTAINS_ANY :
126
+ return fieldFilter .getValue ().getArrayValue ().getValuesList ();
127
+ case ARRAY_CONTAINS :
128
+ return Collections .singletonList (fieldFilter .getValue ());
129
+ default :
130
+ fail ("Unexpected operator: " + fieldFilter .getOperator ());
131
+ }
132
+ }
133
+ }
134
+ }
135
+
136
+ return Collections .emptyList ();
137
+ }
138
+
118
139
/**
119
140
* Returns a lower bound of field values that can be used as a starting point to scan the index
120
141
* defined by {@code fieldIndex}.
@@ -127,7 +148,7 @@ public Bound getLowerBound(FieldIndex fieldIndex) {
127
148
boolean inclusive = true ;
128
149
129
150
// Go through all filters to find a value for the current field segment
130
- for (FieldIndex .Segment segment : fieldIndex ) {
151
+ for (FieldIndex .Segment segment : fieldIndex . getDirectionalSegments () ) {
131
152
Value segmentValue = Values .NULL_VALUE ;
132
153
boolean segmentInclusive = true ;
133
154
@@ -143,25 +164,19 @@ public Bound getLowerBound(FieldIndex fieldIndex) {
143
164
filterValue = Values .getLowerBound (fieldFilter .getValue ().getValueTypeCase ());
144
165
break ;
145
166
case NOT_EQUAL :
146
- filterValue = Values .NULL_VALUE ;
147
- break ;
148
167
case NOT_IN :
149
- filterValue =
150
- Value .newBuilder ()
151
- .setArrayValue (ArrayValue .newBuilder ().addValues (Values .NULL_VALUE ))
152
- .build ();
153
168
break ;
154
169
case EQUAL :
155
170
case IN :
156
- case ARRAY_CONTAINS_ANY :
157
- case ARRAY_CONTAINS :
158
171
case GREATER_THAN_OR_EQUAL :
159
172
filterValue = fieldFilter .getValue ();
160
173
break ;
161
174
case GREATER_THAN :
162
175
filterValue = fieldFilter .getValue ();
163
176
filterInclusive = false ;
164
177
break ;
178
+ default :
179
+ fail ("Unexpected operator: " + fieldFilter .getOperator ());
165
180
}
166
181
167
182
if (max (segmentValue , filterValue ) == filterValue ) {
@@ -206,7 +221,7 @@ public Bound getLowerBound(FieldIndex fieldIndex) {
206
221
List <Value > values = new ArrayList <>();
207
222
boolean inclusive = true ;
208
223
209
- for (FieldIndex .Segment segment : fieldIndex ) {
224
+ for (FieldIndex .Segment segment : fieldIndex . getDirectionalSegments () ) {
210
225
@ Nullable Value segmentValue = null ;
211
226
boolean segmentInclusive = true ;
212
227
@@ -229,15 +244,15 @@ public Bound getLowerBound(FieldIndex fieldIndex) {
229
244
break ;
230
245
case EQUAL :
231
246
case IN :
232
- case ARRAY_CONTAINS_ANY :
233
- case ARRAY_CONTAINS :
234
247
case LESS_THAN_OR_EQUAL :
235
248
filterValue = fieldFilter .getValue ();
236
249
break ;
237
250
case LESS_THAN :
238
251
filterValue = fieldFilter .getValue ();
239
252
filterInclusive = false ;
240
253
break ;
254
+ default :
255
+ fail ("Unexpected operator: " + fieldFilter .getOperator ());
241
256
}
242
257
243
258
if (min (segmentValue , filterValue ) == filterValue ) {
@@ -283,6 +298,11 @@ public List<OrderBy> getOrderBy() {
283
298
return this .orderBys ;
284
299
}
285
300
301
+ /** Returns the first order by (which always exists). */
302
+ public OrderBy getFirstOrderBy () {
303
+ return this .orderBys .get (0 );
304
+ }
305
+
286
306
/** Returns a canonical string representing this target. */
287
307
public String getCanonicalId () {
288
308
if (memoizedCannonicalId != null ) {
@@ -307,7 +327,7 @@ public String getCanonicalId() {
307
327
builder .append ("|ob:" );
308
328
for (OrderBy orderBy : getOrderBy ()) {
309
329
builder .append (orderBy .getField ().canonicalString ());
310
- builder .append (orderBy .getDirection ().equals ( Direction . ASCENDING ) ? "asc" : "desc" );
330
+ builder .append (orderBy .getDirection ().canonicalString () );
311
331
}
312
332
313
333
// Add limit.
0 commit comments