Skip to content

Commit 9f7044d

Browse files
Review
1 parent 9c9c4b2 commit 9f7044d

File tree

4 files changed

+41
-15
lines changed

4 files changed

+41
-15
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,11 +217,11 @@ public Bound getLowerBound(FieldIndex fieldIndex) {
217217
case ARRAY_CONTAINS:
218218
case LESS_THAN_OR_EQUAL:
219219
largestValue = fieldFilter.getValue();
220-
before = true;
220+
before = false;
221221
break;
222222
case LESS_THAN:
223223
largestValue = fieldFilter.getValue();
224-
before = false;
224+
before = true;
225225
break;
226226
}
227227
}

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

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,8 @@ public void addFieldIndex(FieldIndex index) {
110110

111111
@Override
112112
public void addIndexEntries(Document document) {
113-
ResourcePath documentPath = document.getKey().getPath();
114-
String collectionGroup = documentPath.getSegment(documentPath.length() - 2);
113+
DocumentKey documentKey = document.getKey();
114+
String collectionGroup = documentKey.getCollectionGroup();
115115
db.query(
116116
"SELECT index_id, index_proto FROM index_configuration WHERE collection_group = ? AND active = 1")
117117
.binding(collectionGroup)
@@ -127,10 +127,10 @@ public void addIndexEntries(Document document) {
127127
if (values == null) return;
128128

129129
if (Logger.isDebugEnabled()) {
130-
Logger.warn(
130+
Logger.debug(
131131
TAG,
132132
"Adding index values for document '%s' to index '%s'",
133-
documentPath,
133+
documentKey,
134134
fieldIndex);
135135
}
136136

@@ -144,7 +144,7 @@ public void addIndexEntries(Document document) {
144144
+ "document_name) VALUES(?, ?, ?)",
145145
indexId,
146146
encoded,
147-
documentPath.canonicalString());
147+
documentKey.toString());
148148
}
149149
} catch (InvalidProtocolBufferException e) {
150150
throw fail("Invalid index: " + e);
@@ -176,6 +176,8 @@ public Set<DocumentKey> getDocumentsMatchingTarget(Target target) {
176176
if (fieldIndex == null) return null;
177177

178178
Bound lowerBound = target.getLowerBound(fieldIndex);
179+
String lowerBoundOp = lowerBound.isBefore() ? ">=" : ">"; // `startAt()` versus `startAfter()`
180+
179181
@Nullable Bound upperBound = target.getUpperBound(fieldIndex);
180182

181183
if (Logger.isDebugEnabled()) {
@@ -200,13 +202,15 @@ public Set<DocumentKey> getDocumentsMatchingTarget(Target target) {
200202
lowerBoundValues.size() == upperBoundValues.size(),
201203
"Expected upper and lower bound size to match");
202204

205+
String upperBoundOp = upperBound.isBefore() ? "<" : "<="; // `endBefore()` versus `endAt()`
206+
203207
// TODO(indexing): To avoid reading the same documents multiple times, we should ideally only
204208
// send one query that combines all clauses.
205209
for (int i = 0; i < lowerBoundValues.size(); ++i) {
206210
db.query(
207211
String.format(
208212
"SELECT document_name from index_entries WHERE index_id = ? AND index_value %s ? AND index_value %s ?",
209-
lowerBound.isBefore() ? ">=" : ">", upperBound.isBefore() ? "<=" : "<"))
213+
lowerBoundOp, upperBoundOp))
210214
.binding(fieldIndex.getIndexId(), lowerBoundValues.get(i), upperBoundValues.get(i))
211215
.forEach(
212216
row -> result.add(DocumentKey.fromPath(ResourcePath.fromString(row.getString(0)))));
@@ -218,7 +222,7 @@ public Set<DocumentKey> getDocumentsMatchingTarget(Target target) {
218222
db.query(
219223
String.format(
220224
"SELECT document_name from index_entries WHERE index_id = ? AND index_value %s ?",
221-
lowerBound.isBefore() ? ">=" : ">"))
225+
lowerBoundOp))
222226
.binding(fieldIndex.getIndexId(), lowerBoundValue)
223227
.forEach(
224228
row -> result.add(DocumentKey.fromPath(ResourcePath.fromString(row.getString(0)))));
@@ -259,15 +263,14 @@ public Set<DocumentKey> getDocumentsMatchingTarget(Target target) {
259263
throw fail("Failed to decode index: " + e);
260264
}
261265
});
262-
;
263266

264267
if (activeIndices.isEmpty()) {
265268
return null;
266269
}
267270

268271
// Return the index with the most number of segments
269-
Collections.sort(activeIndices, (l, r) -> Integer.compare(r.segmentCount(), l.segmentCount()));
270-
return activeIndices.get(0);
272+
return Collections.max(
273+
activeIndices, (l, r) -> Integer.compare(l.segmentCount(), r.segmentCount()));
271274
}
272275

273276
/**

firebase-firestore/src/main/java/com/google/firebase/firestore/model/DocumentKey.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,11 @@ public ResourcePath getPath() {
108108
return path;
109109
}
110110

111+
/** Returns the collection group (i.e. the name of the parent collection) for this key. */
112+
public String getCollectionGroup() {
113+
return path.getSegment(path.length() - 2);
114+
}
115+
111116
/** Returns true if the document is in the specified collectionId. */
112117
public boolean hasCollectionId(String collectionId) {
113118
return path.length() >= 2 && path.segments.get(path.length() - 2).equals(collectionId);

firebase-firestore/src/test/java/com/google/firebase/firestore/local/SQLiteIndexManagerTest.java

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,31 @@ Persistence getPersistence() {
7777
return PersistenceTestHelpers.createSQLitePersistence();
7878
}
7979

80+
@Test
81+
public void addsDocuments() {
82+
indexManager.addFieldIndex(
83+
new FieldIndex("coll").withAddedField(field("exists"), FieldIndex.Segment.Kind.ORDERED));
84+
addDoc("coll/doc1", map("exists", 1));
85+
addDoc("coll/doc2", map());
86+
}
87+
8088
@Test
8189
public void testEqualityFilter() {
8290
setUpSingleValueFilter();
8391
Query query = query("coll").filter(filter("count", "==", 2));
8492
verifyResults(query, "coll/doc2");
8593
}
8694

95+
@Test
96+
public void testNestedFieldEqualityFilter() {
97+
indexManager.addFieldIndex(
98+
new FieldIndex("coll").withAddedField(field("a.b"), FieldIndex.Segment.Kind.ORDERED));
99+
addDoc("coll/doc1", map("a", map("b", 1)));
100+
addDoc("coll/doc2", map("a", map("b", 2)));
101+
Query query = query("coll").filter(filter("a.b", "==", 2));
102+
verifyResults(query, "coll/doc2");
103+
}
104+
87105
@Test
88106
public void testNotEqualityFilter() {
89107
// TODO(indexing): Optimize != filters. We currently return all documents and do not exclude
@@ -145,14 +163,14 @@ public void testStartAfterFilter() {
145163
@Test
146164
public void testEndAtFilter() {
147165
setUpSingleValueFilter();
148-
Query query = query("coll").orderBy(orderBy("count")).endAt(bound(true, 2));
166+
Query query = query("coll").orderBy(orderBy("count")).endAt(bound(false, 2));
149167
verifyResults(query, "coll/doc1", "coll/doc2");
150168
}
151169

152170
@Test
153171
public void testEndBeforeFilter() {
154172
setUpSingleValueFilter();
155-
Query query = query("coll").orderBy(orderBy("count")).endAt(bound(false, 2));
173+
Query query = query("coll").orderBy(orderBy("count")).endAt(bound(true, 2));
156174
verifyResults(query, "coll/doc1");
157175
}
158176

@@ -165,7 +183,7 @@ public void testRangeWithBoundFilter() {
165183
.filter(filter("count", "<=", 3))
166184
.orderBy(orderBy("count"))
167185
.startAt(bound(false, 1))
168-
.endAt(bound(true, 2));
186+
.endAt(bound(false, 2));
169187
verifyResults(startAt, "coll/doc2");
170188
}
171189

0 commit comments

Comments
 (0)