Skip to content

Commit f81c44b

Browse files
Index Evaluation
1 parent e41b233 commit f81c44b

File tree

13 files changed

+602
-39
lines changed

13 files changed

+602
-39
lines changed

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import com.google.firebase.firestore.model.FieldIndex;
2121
import com.google.firebase.firestore.model.ResourcePath;
2222
import com.google.firebase.firestore.model.Values;
23+
import com.google.firestore.v1.ArrayValue;
2324
import com.google.firestore.v1.Value;
2425
import java.util.ArrayList;
2526
import java.util.List;
@@ -130,11 +131,16 @@ public Bound getLowerBound(FieldIndex fieldIndex) {
130131
FieldFilter fieldFilter = (FieldFilter) filter;
131132
switch (fieldFilter.getOperator()) {
132133
case LESS_THAN:
133-
case NOT_IN:
134134
case NOT_EQUAL:
135135
case LESS_THAN_OR_EQUAL:
136136
// These filters cannot be used as a lower bound. Skip.
137137
break;
138+
case NOT_IN:
139+
lowestValue =
140+
Value.newBuilder()
141+
.setArrayValue(ArrayValue.newBuilder().addValues(Values.NULL_VALUE))
142+
.build();
143+
break;
138144
case EQUAL:
139145
case IN:
140146
case ARRAY_CONTAINS_ANY:
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// Copyright 2021 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package com.google.firebase.firestore.index;
16+
17+
import com.google.protobuf.ByteString;
18+
19+
/**
20+
* Implements {@link DirectionalIndexByteEncoder} using {@link OrderedCodeWriter} for the actual
21+
* encoding.
22+
*/
23+
public class IndexByteEncoder extends DirectionalIndexByteEncoder {
24+
// Note: This code is copied from the backend.
25+
26+
private final OrderedCodeWriter orderedCode;
27+
28+
public IndexByteEncoder() {
29+
this.orderedCode = new OrderedCodeWriter();
30+
}
31+
32+
public void seed(byte[] encodedBytes) {
33+
orderedCode.seed(encodedBytes);
34+
}
35+
36+
@Override
37+
public void writeBytes(ByteString val) {
38+
orderedCode.writeBytesAscending(val);
39+
}
40+
41+
@Override
42+
public void writeString(String val) {
43+
orderedCode.writeUtf8Ascending(val);
44+
}
45+
46+
@Override
47+
public void writeLong(long val) {
48+
orderedCode.writeSignedLongAscending(val);
49+
}
50+
51+
@Override
52+
public void writeDouble(double val) {
53+
orderedCode.writeDoubleAscending(val);
54+
}
55+
56+
public byte[] getEncodedBytes() {
57+
return orderedCode.encodedBytes();
58+
}
59+
60+
public void reset() {
61+
orderedCode.reset();
62+
}
63+
}

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,14 @@
1414

1515
package com.google.firebase.firestore.local;
1616

17+
import androidx.annotation.Nullable;
18+
import com.google.firebase.firestore.core.Target;
19+
import com.google.firebase.firestore.model.Document;
20+
import com.google.firebase.firestore.model.DocumentKey;
1721
import com.google.firebase.firestore.model.FieldIndex;
1822
import com.google.firebase.firestore.model.ResourcePath;
1923
import java.util.List;
24+
import java.util.Set;
2025

2126
/**
2227
* Represents a set of indexes that are used to execute queries efficiently.
@@ -41,11 +46,21 @@ public interface IndexManager {
4146
*/
4247
List<ResourcePath> getCollectionParents(String collectionId);
4348

49+
/** Adds index entries for all indexed fields in the given document. */
50+
void addIndexEntries(Document document);
51+
4452
/**
4553
* Adds a field path index.
4654
*
4755
* <p>Values for this index are persisted asynchronously. The index will only be used for query
4856
* execution once values are persisted.
4957
*/
5058
void addFieldIndex(FieldIndex index);
59+
60+
/**
61+
* Returns the documents that match the given target based on the configured indices. Returns
62+
* {@code null} if there is no active index to serve this target.
63+
*/
64+
@Nullable
65+
Set<DocumentKey> getDocumentsMatchingTarget(Target target);
5166
}

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import com.google.firebase.firestore.core.Target;
2424
import com.google.firebase.firestore.model.DocumentKey;
2525
import com.google.firebase.firestore.model.FieldIndex;
26+
import com.google.firebase.firestore.model.FieldPath;
2627
import com.google.firebase.firestore.model.MutableDocument;
2728
import com.google.firebase.firestore.model.ObjectValue;
2829
import com.google.firebase.firestore.model.SnapshotVersion;
@@ -309,4 +310,18 @@ public Index encodeFieldIndex(FieldIndex fieldIndex) {
309310

310311
return index.build();
311312
}
313+
314+
public FieldIndex decodeFieldIndex(String collection_group, int indexId, Index index) {
315+
FieldIndex fieldIndex = new FieldIndex(collection_group, indexId);
316+
for (Index.IndexField field : index.getFieldsList()) {
317+
fieldIndex =
318+
fieldIndex.withAddedField(
319+
FieldPath.fromServerFormat(field.getFieldPath()),
320+
field.getValueModeCase().equals(Index.IndexField.ValueModeCase.ARRAY_CONFIG)
321+
? FieldIndex.Segment.Kind.CONTAINS
322+
: FieldIndex.Segment.Kind.ORDERED);
323+
}
324+
325+
return fieldIndex;
326+
}
312327
}

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,18 @@
1515

1616
import static com.google.firebase.firestore.util.Assert.hardAssert;
1717

18+
import androidx.annotation.Nullable;
19+
import com.google.firebase.firestore.core.Target;
20+
import com.google.firebase.firestore.model.Document;
21+
import com.google.firebase.firestore.model.DocumentKey;
1822
import com.google.firebase.firestore.model.FieldIndex;
1923
import com.google.firebase.firestore.model.ResourcePath;
2024
import java.util.ArrayList;
2125
import java.util.Collections;
2226
import java.util.HashMap;
2327
import java.util.HashSet;
2428
import java.util.List;
29+
import java.util.Set;
2530

2631
/** An in-memory implementation of IndexManager. */
2732
class MemoryIndexManager implements IndexManager {
@@ -38,11 +43,23 @@ public List<ResourcePath> getCollectionParents(String collectionId) {
3843
return collectionParentsIndex.getEntries(collectionId);
3944
}
4045

46+
@Override
47+
public void addIndexEntries(Document document) {
48+
// Field indices are not supported with memory persistence.
49+
}
50+
4151
@Override
4252
public void addFieldIndex(FieldIndex index) {
4353
// Field indices are not supported with memory persistence.
4454
}
4555

56+
@Override
57+
@Nullable
58+
public Set<DocumentKey> getDocumentsMatchingTarget(Target target) {
59+
// Field indices are not supported with memory persistence.
60+
return null;
61+
}
62+
4663
/**
4764
* Internal implementation of the collection-parent index. Also used for in-memory caching by
4865
* SQLiteIndexManager and initial index population in SQLiteSchema.

0 commit comments

Comments
 (0)