Skip to content

Commit a301274

Browse files
Make LowerBound nullable
1 parent 6afe0b7 commit a301274

File tree

15 files changed

+184
-105
lines changed

15 files changed

+184
-105
lines changed

firebase-firestore/src/main/java/com/google/firebase/firestore/FirebaseFirestore.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ Task<Void> configureIndices(String json) {
312312
JSONObject field = fields.getJSONObject(f);
313313
FieldPath fieldPath = FieldPath.fromServerFormat(field.getString("fieldPath"));
314314
if ("CONTAINS".equals(field.optString("arrayConfig"))) {
315-
fieldIndex = fieldIndex.withAddedField(fieldPath, FieldIndex.Segment.Kind.CONTAINS);
315+
fieldIndex = fieldIndex.withAddedField(fieldPath, FieldIndex.Segment.Kind.ARRAYS);
316316
} else {
317317
fieldIndex = fieldIndex.withAddedField(fieldPath, FieldIndex.Segment.Kind.ORDERED);
318318
}

firebase-firestore/src/main/java/com/google/firebase/firestore/core/Target.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import com.google.firestore.v1.ArrayValue;
2626
import com.google.firestore.v1.Value;
2727
import java.util.ArrayList;
28+
import java.util.Collections;
2829
import java.util.List;
2930

3031
/**
@@ -114,6 +115,36 @@ public boolean hasLimit() {
114115
return endAt;
115116
}
116117

118+
/**
119+
* Returns a lower bound of field values that can be used as a starting point to scan the index
120+
* defined by {@code fieldIndex}.
121+
*
122+
* <p>Unlike {@link #getUpperBound}, lower bounds always exist as the SDK can use {@code null} as
123+
* a starting point for missing boundary values.
124+
*/
125+
public List<Value> getArrayValues(FieldIndex fieldIndex) {
126+
// Go through all filters to find a value for the current field segment
127+
for (FieldIndex.Segment segment : fieldIndex) {
128+
if (!segment.getKind().equals(FieldIndex.Segment.Kind.ARRAYS)) {
129+
continue;
130+
}
131+
132+
for (Filter filter : filters) {
133+
if (filter.getField().equals(segment.getFieldPath())) {
134+
FieldFilter fieldFilter = (FieldFilter) filter;
135+
switch (fieldFilter.getOperator()) {
136+
case ARRAY_CONTAINS_ANY:
137+
return fieldFilter.getValue().getArrayValue().getValuesList();
138+
case ARRAY_CONTAINS:
139+
return Collections.singletonList(fieldFilter.getValue());
140+
}
141+
}
142+
}
143+
}
144+
145+
return null;
146+
}
147+
117148
/**
118149
* Returns a lower bound of field values that can be used as a starting point to scan the index
119150
* defined by {@code fieldIndex}.
@@ -127,6 +158,9 @@ public Bound getLowerBound(FieldIndex fieldIndex) {
127158

128159
// Go through all filters to find a value for the current field segment
129160
for (FieldIndex.Segment segment : fieldIndex) {
161+
if (!segment.getKind().equals(FieldIndex.Segment.Kind.ORDERED)) {
162+
continue;
163+
}
130164
Value segmentValue = Values.NULL_VALUE;
131165
boolean segmentInclusive = true;
132166

firebase-firestore/src/main/java/com/google/firebase/firestore/index/IndexEntry.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,16 @@
2121
// TODO(indexing)
2222
public class IndexEntry {
2323
private final int indexId;
24-
private final byte[] indexValue;
24+
private final byte[] arrayValue;
25+
private final byte[] directionalValue;
2526
private final String uid;
2627
private final String documentName;
2728

28-
public IndexEntry(int indexId, byte[] indexValue, String uid, String documentName) {
29+
public IndexEntry(
30+
int indexId, byte[] arrayValue, byte[] directionalValue, String uid, String documentName) {
2931
this.indexId = indexId;
30-
this.indexValue = indexValue;
32+
this.arrayValue = arrayValue;
33+
this.directionalValue = directionalValue;
3134
this.uid = uid;
3235
this.documentName = documentName;
3336
}
@@ -36,8 +39,12 @@ public int getIndexId() {
3639
return indexId;
3740
}
3841

39-
public byte[] getIndexValue() {
40-
return indexValue;
42+
public byte[] getArrayValue() {
43+
return arrayValue;
44+
}
45+
46+
public byte[] getDirectionalValue() {
47+
return directionalValue;
4148
}
4249

4350
public String getUid() {

firebase-firestore/src/main/java/com/google/firebase/firestore/local/IndexBackfiller.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,11 +115,13 @@ void addIndexEntry(IndexEntry entry) {
115115
persistence.execute(
116116
"INSERT OR IGNORE INTO index_entries ("
117117
+ "index_id, "
118-
+ "index_value, "
118+
+ "array_index, "
119+
+ "directional_index, "
119120
+ "uid, "
120121
+ "document_name) VALUES(?, ?, ?, ?)",
121122
entry.getIndexId(),
122-
entry.getIndexValue(),
123+
entry.getArrayValue(),
124+
entry.getDirectionalValue(),
123125
entry.getUid(),
124126
entry.getDocumentName());
125127
}
@@ -141,12 +143,18 @@ void removeIndexEntry(int indexId, String uid, String documentName) {
141143
@VisibleForTesting
142144
IndexEntry getIndexEntry(int indexId) {
143145
return persistence
144-
.query("SELECT index_value, uid, document_name FROM index_entries WHERE index_id = ?")
146+
.query(
147+
"SELECT array_index, directional_index, uid, document_name FROM index_entries WHERE index_id = ?")
145148
.binding(indexId)
146149
.firstValue(
147150
row ->
148151
row == null
149152
? null
150-
: new IndexEntry(indexId, row.getBlob(0), row.getString(1), row.getString(2)));
153+
: new IndexEntry(
154+
indexId,
155+
row.getBlob(0),
156+
row.getBlob(1),
157+
row.getString(2),
158+
row.getString(3)));
151159
}
152160
}

firebase-firestore/src/main/java/com/google/firebase/firestore/local/LocalSerializer.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ public Index encodeFieldIndex(FieldIndex fieldIndex) {
300300
for (FieldIndex.Segment segment : fieldIndex) {
301301
Index.IndexField.Builder indexField = Index.IndexField.newBuilder();
302302
indexField.setFieldPath(segment.getFieldPath().canonicalString());
303-
if (segment.getKind() == FieldIndex.Segment.Kind.CONTAINS) {
303+
if (segment.getKind() == FieldIndex.Segment.Kind.ARRAYS) {
304304
indexField.setArrayConfig(Index.IndexField.ArrayConfig.CONTAINS);
305305
} else {
306306
indexField.setOrder(Index.IndexField.Order.ASCENDING);
@@ -319,7 +319,7 @@ public FieldIndex decodeFieldIndex(
319319
fieldIndex.withAddedField(
320320
FieldPath.fromServerFormat(field.getFieldPath()),
321321
field.getValueModeCase().equals(Index.IndexField.ValueModeCase.ARRAY_CONFIG)
322-
? FieldIndex.Segment.Kind.CONTAINS
322+
? FieldIndex.Segment.Kind.ARRAYS
323323
: FieldIndex.Segment.Kind.ORDERED);
324324
}
325325
fieldIndex =

0 commit comments

Comments
 (0)