Skip to content

Commit c352c5c

Browse files
committed
Add documentation
1 parent af95074 commit c352c5c

File tree

2 files changed

+32
-16
lines changed

2 files changed

+32
-16
lines changed

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,12 +122,14 @@ public ImmutableSortedMap<DocumentKey, Document> getDocumentsMatchingQuery(
122122
return result;
123123
}
124124

125-
public ImmutableSortedMap<DocumentKey, Document> getDocumentsMatchingQueryTest(
126-
Query query, boolean autoIndexing, QueryContext counter) {
125+
// Used for auto indexing experiment, allows test running specifically with or without field
126+
// indexes
127+
public ImmutableSortedMap<DocumentKey, Document> getDocumentsMatchingQueryForTest(
128+
Query query, boolean usingIndex, QueryContext counter) {
127129
hardAssert(initialized, "initialize() not called");
128130

129131
ImmutableSortedMap<DocumentKey, Document> result;
130-
if (autoIndexing) {
132+
if (usingIndex) {
131133
result = performQueryUsingIndex(query);
132134
if (result == null) {
133135
fail("createTargetIndices fails");

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

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -148,24 +148,26 @@ private <T> T expectFullCollectionScan(Callable<T> c) throws Exception {
148148
}
149149
}
150150

151-
protected DocumentSet runQuery(Query query, boolean autoIndexing, QueryContext counter) {
151+
protected DocumentSet runQuery(Query query, boolean usingIndex, QueryContext context) {
152152
Preconditions.checkNotNull(
153153
expectFullCollectionScan,
154154
"Encountered runQuery() call not wrapped in expectOptimizedCollectionQuery()/expectFullCollectionQuery()");
155155
ImmutableSortedMap<DocumentKey, Document> docs =
156-
queryEngine.getDocumentsMatchingQueryTest(query, autoIndexing, counter);
156+
queryEngine.getDocumentsMatchingQueryForTest(query, usingIndex, context);
157157
View view =
158158
new View(query, new ImmutableSortedSet<>(Collections.emptyList(), DocumentKey::compareTo));
159159
View.DocumentChanges viewDocChanges = view.computeDocChanges(docs);
160160
return view.applyChanges(viewDocChanges).getSnapshot().getDocuments();
161161
}
162162

163+
/** Creates one test document based on requirements. */
163164
private void createTestingDocument(
164165
String basePath, int documentID, boolean isMatched, int numOfFields) {
165166
Map<String, Object> fields = map("match", isMatched);
166167

167-
// Randomly generate the rest of fields
168+
// Randomly generate the rest of fields.
168169
for (int i = 2; i <= numOfFields; i++) {
170+
// Randomly select a field in values table.
169171
int valueIndex = (int) (Math.random() * values.size()) % values.size();
170172
fields.put("field" + i, values.get(valueIndex));
171173
}
@@ -180,14 +182,18 @@ private void createTestingDocument(
180182
private void createTestingCollection(
181183
String basePath, int totalSetCount, int portion /*0 - 10*/, int numOfFields /* 1 - 30*/) {
182184
int documentCounter = 0;
185+
186+
// A set contains 10 documents.
183187
for (int i = 1; i <= totalSetCount; i++) {
184-
// Generate a random order list of 0 ... 9
188+
// Generate a random order list of 0 ... 9, to make sure the matching documents stay in
189+
// random positions.
185190
ArrayList<Integer> indexes = new ArrayList<>();
186191
for (int index = 0; index < 10; index++) {
187192
indexes.add(index);
188193
}
189194
Collections.shuffle(indexes);
190195

196+
// portion% of the set match
191197
for (int match = 0; match < portion; match++) {
192198
int currentID = documentCounter + indexes.get(match);
193199
createTestingDocument(basePath, currentID, true, numOfFields);
@@ -200,8 +206,11 @@ private void createTestingCollection(
200206
}
201207
}
202208

209+
/** Create mutation for 10% of total documents. */
203210
private void createMutationForCollection(String basePath, int totalSetCount) {
204211
ArrayList<Integer> indexes = new ArrayList<>();
212+
213+
// Randomly selects 10% of documents.
205214
for (int index = 0; index < totalSetCount * 10; index++) {
206215
indexes.add(index);
207216
}
@@ -215,14 +224,14 @@ private void createMutationForCollection(String basePath, int totalSetCount) {
215224
@Test
216225
public void testCombinesIndexedWithNonIndexedResults() throws Exception {
217226
// Every set contains 10 documents
218-
final int numOfSet = 100;
227+
final int numOfSet = 1000;
219228
// could overflow. Currently it is safe when numOfSet set to 1000 and running on macbook M1
220229
long totalBeforeIndex = 0;
221230
long totalAfterIndex = 0;
222231
long totalDocumentCount = 0;
223232
long totalResultCount = 0;
224233

225-
// Temperate heuristic
234+
// Temperate heuristic, gets when setting numOfSet to 1000.
226235
double without = 3.7;
227236
double with = 4.9;
228237

@@ -231,33 +240,38 @@ public void testCombinesIndexedWithNonIndexedResults() throws Exception {
231240
for (int portion = 0; portion <= 10; portion++) {
232241
for (int numOfFields = 1; numOfFields <= 31; numOfFields += 10) {
233242
String basePath = "documentCount" + totalSetCount;
234-
// Auto indexing
235243
Query query = query(basePath).filter(filter("match", "==", true));
244+
245+
// Creates a full matched index for given query.
236246
indexManager.createTargetIndices(query.toTarget());
247+
237248
createTestingCollection(basePath, totalSetCount, portion, numOfFields);
238249
createMutationForCollection(basePath, totalSetCount);
239250

240-
QueryContext counterWithoutIndex = new QueryContext();
251+
// runs query using full collection scan.
252+
QueryContext contextWithoutIndex = new QueryContext();
241253
long beforeAutoStart = System.nanoTime();
242254
DocumentSet results =
243-
expectFullCollectionScan(() -> runQuery(query, false, counterWithoutIndex));
255+
expectFullCollectionScan(() -> runQuery(query, false, contextWithoutIndex));
244256
long beforeAutoEnd = System.nanoTime();
245257
long millisecondsBeforeAuto =
246258
TimeUnit.MILLISECONDS.convert(
247259
(beforeAutoEnd - beforeAutoStart), TimeUnit.NANOSECONDS);
248260
totalBeforeIndex += (beforeAutoEnd - beforeAutoStart);
249-
totalDocumentCount += counterWithoutIndex.getDocumentReadCount();
261+
totalDocumentCount += contextWithoutIndex.getDocumentReadCount();
250262
assertEquals(portion * totalSetCount, results.size());
251263

252-
QueryContext counterWithIndex = new QueryContext();
264+
// runs query using index look up.
265+
QueryContext contextWithIndex = new QueryContext();
253266
long autoStart = System.nanoTime();
254-
results = expectOptimizedCollectionScan(() -> runQuery(query, true, counterWithIndex));
267+
results = expectOptimizedCollectionScan(() -> runQuery(query, true, contextWithIndex));
255268
long autoEnd = System.nanoTime();
256269
long millisecondsAfterAuto =
257270
TimeUnit.MILLISECONDS.convert((autoEnd - autoStart), TimeUnit.NANOSECONDS);
258271
totalAfterIndex += (autoEnd - autoStart);
259272
assertEquals(portion * totalSetCount, results.size());
260273
totalResultCount += results.size();
274+
261275
if (millisecondsBeforeAuto > millisecondsAfterAuto) {
262276
System.out.println(
263277
"Auto Indexing saves time when total of documents inside collection is "
@@ -268,7 +282,7 @@ public void testCombinesIndexedWithNonIndexedResults() throws Exception {
268282
+ numOfFields
269283
+ " fields.\n"
270284
+ "Weight result for without auto indexing is "
271-
+ without * counterWithoutIndex.getDocumentReadCount()
285+
+ without * contextWithoutIndex.getDocumentReadCount()
272286
+ ". And weight result for auto indexing is "
273287
+ with * results.size());
274288
}

0 commit comments

Comments
 (0)