@@ -148,24 +148,26 @@ private <T> T expectFullCollectionScan(Callable<T> c) throws Exception {
148
148
}
149
149
}
150
150
151
- protected DocumentSet runQuery (Query query , boolean autoIndexing , QueryContext counter ) {
151
+ protected DocumentSet runQuery (Query query , boolean usingIndex , QueryContext context ) {
152
152
Preconditions .checkNotNull (
153
153
expectFullCollectionScan ,
154
154
"Encountered runQuery() call not wrapped in expectOptimizedCollectionQuery()/expectFullCollectionQuery()" );
155
155
ImmutableSortedMap <DocumentKey , Document > docs =
156
- queryEngine .getDocumentsMatchingQueryTest (query , autoIndexing , counter );
156
+ queryEngine .getDocumentsMatchingQueryForTest (query , usingIndex , context );
157
157
View view =
158
158
new View (query , new ImmutableSortedSet <>(Collections .emptyList (), DocumentKey ::compareTo ));
159
159
View .DocumentChanges viewDocChanges = view .computeDocChanges (docs );
160
160
return view .applyChanges (viewDocChanges ).getSnapshot ().getDocuments ();
161
161
}
162
162
163
+ /** Creates one test document based on requirements. */
163
164
private void createTestingDocument (
164
165
String basePath , int documentID , boolean isMatched , int numOfFields ) {
165
166
Map <String , Object > fields = map ("match" , isMatched );
166
167
167
- // Randomly generate the rest of fields
168
+ // Randomly generate the rest of fields.
168
169
for (int i = 2 ; i <= numOfFields ; i ++) {
170
+ // Randomly select a field in values table.
169
171
int valueIndex = (int ) (Math .random () * values .size ()) % values .size ();
170
172
fields .put ("field" + i , values .get (valueIndex ));
171
173
}
@@ -180,14 +182,18 @@ private void createTestingDocument(
180
182
private void createTestingCollection (
181
183
String basePath , int totalSetCount , int portion /*0 - 10*/ , int numOfFields /* 1 - 30*/ ) {
182
184
int documentCounter = 0 ;
185
+
186
+ // A set contains 10 documents.
183
187
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.
185
190
ArrayList <Integer > indexes = new ArrayList <>();
186
191
for (int index = 0 ; index < 10 ; index ++) {
187
192
indexes .add (index );
188
193
}
189
194
Collections .shuffle (indexes );
190
195
196
+ // portion% of the set match
191
197
for (int match = 0 ; match < portion ; match ++) {
192
198
int currentID = documentCounter + indexes .get (match );
193
199
createTestingDocument (basePath , currentID , true , numOfFields );
@@ -200,8 +206,11 @@ private void createTestingCollection(
200
206
}
201
207
}
202
208
209
+ /** Create mutation for 10% of total documents. */
203
210
private void createMutationForCollection (String basePath , int totalSetCount ) {
204
211
ArrayList <Integer > indexes = new ArrayList <>();
212
+
213
+ // Randomly selects 10% of documents.
205
214
for (int index = 0 ; index < totalSetCount * 10 ; index ++) {
206
215
indexes .add (index );
207
216
}
@@ -215,14 +224,14 @@ private void createMutationForCollection(String basePath, int totalSetCount) {
215
224
@ Test
216
225
public void testCombinesIndexedWithNonIndexedResults () throws Exception {
217
226
// Every set contains 10 documents
218
- final int numOfSet = 100 ;
227
+ final int numOfSet = 1000 ;
219
228
// could overflow. Currently it is safe when numOfSet set to 1000 and running on macbook M1
220
229
long totalBeforeIndex = 0 ;
221
230
long totalAfterIndex = 0 ;
222
231
long totalDocumentCount = 0 ;
223
232
long totalResultCount = 0 ;
224
233
225
- // Temperate heuristic
234
+ // Temperate heuristic, gets when setting numOfSet to 1000.
226
235
double without = 3.7 ;
227
236
double with = 4.9 ;
228
237
@@ -231,33 +240,38 @@ public void testCombinesIndexedWithNonIndexedResults() throws Exception {
231
240
for (int portion = 0 ; portion <= 10 ; portion ++) {
232
241
for (int numOfFields = 1 ; numOfFields <= 31 ; numOfFields += 10 ) {
233
242
String basePath = "documentCount" + totalSetCount ;
234
- // Auto indexing
235
243
Query query = query (basePath ).filter (filter ("match" , "==" , true ));
244
+
245
+ // Creates a full matched index for given query.
236
246
indexManager .createTargetIndices (query .toTarget ());
247
+
237
248
createTestingCollection (basePath , totalSetCount , portion , numOfFields );
238
249
createMutationForCollection (basePath , totalSetCount );
239
250
240
- QueryContext counterWithoutIndex = new QueryContext ();
251
+ // runs query using full collection scan.
252
+ QueryContext contextWithoutIndex = new QueryContext ();
241
253
long beforeAutoStart = System .nanoTime ();
242
254
DocumentSet results =
243
- expectFullCollectionScan (() -> runQuery (query , false , counterWithoutIndex ));
255
+ expectFullCollectionScan (() -> runQuery (query , false , contextWithoutIndex ));
244
256
long beforeAutoEnd = System .nanoTime ();
245
257
long millisecondsBeforeAuto =
246
258
TimeUnit .MILLISECONDS .convert (
247
259
(beforeAutoEnd - beforeAutoStart ), TimeUnit .NANOSECONDS );
248
260
totalBeforeIndex += (beforeAutoEnd - beforeAutoStart );
249
- totalDocumentCount += counterWithoutIndex .getDocumentReadCount ();
261
+ totalDocumentCount += contextWithoutIndex .getDocumentReadCount ();
250
262
assertEquals (portion * totalSetCount , results .size ());
251
263
252
- QueryContext counterWithIndex = new QueryContext ();
264
+ // runs query using index look up.
265
+ QueryContext contextWithIndex = new QueryContext ();
253
266
long autoStart = System .nanoTime ();
254
- results = expectOptimizedCollectionScan (() -> runQuery (query , true , counterWithIndex ));
267
+ results = expectOptimizedCollectionScan (() -> runQuery (query , true , contextWithIndex ));
255
268
long autoEnd = System .nanoTime ();
256
269
long millisecondsAfterAuto =
257
270
TimeUnit .MILLISECONDS .convert ((autoEnd - autoStart ), TimeUnit .NANOSECONDS );
258
271
totalAfterIndex += (autoEnd - autoStart );
259
272
assertEquals (portion * totalSetCount , results .size ());
260
273
totalResultCount += results .size ();
274
+
261
275
if (millisecondsBeforeAuto > millisecondsAfterAuto ) {
262
276
System .out .println (
263
277
"Auto Indexing saves time when total of documents inside collection is "
@@ -268,7 +282,7 @@ public void testCombinesIndexedWithNonIndexedResults() throws Exception {
268
282
+ numOfFields
269
283
+ " fields.\n "
270
284
+ "Weight result for without auto indexing is "
271
- + without * counterWithoutIndex .getDocumentReadCount ()
285
+ + without * contextWithoutIndex .getDocumentReadCount ()
272
286
+ ". And weight result for auto indexing is "
273
287
+ with * results .size ());
274
288
}
0 commit comments