@@ -26,14 +26,19 @@ static int coundChildren(ExtractedNode *node, ExtractedNodeType type, bool first
26
26
static void fillChildren (ExtractedNode * node , ExtractedNodeType type , bool first , ExtractedNode * * items , int * i );
27
27
static void flatternTree (ExtractedNode * node );
28
28
static int comparePathItems (PathItem * i1 , PathItem * i2 );
29
- static ExtractedNode * makeEntries (ExtractedNode * node , MakeEntryHandler handler , Pointer extra );
30
29
static int compareNodes (const void * a1 , const void * a2 );
30
+ static int compareJsQueryItem (JsQueryItem * v1 , JsQueryItem * v2 );
31
31
static void processGroup (ExtractedNode * node , int start , int end );
32
32
static void simplifyRecursive (ExtractedNode * node );
33
- static int compareJsQueryItem (JsQueryItem * v1 , JsQueryItem * v2 );
33
+ static ExtractedNode * makeEntries (ExtractedNode * node , MakeEntryHandler handler , Pointer extra );
34
+ static bool queryHasPositive (ExtractedNode * node );
35
+ static bool needRecheckRecursive (ExtractedNode * node , bool not );
34
36
37
+ /*
38
+ * Recursive function that turns jsquery into tree of ExtractedNode items.
39
+ */
35
40
static ExtractedNode *
36
- recursiveExtract (JsQueryItem * jsq , bool indirect , PathItem * path )
41
+ recursiveExtract (JsQueryItem * jsq ,bool indirect , PathItem * path )
37
42
{
38
43
ExtractedNode * leftNode , * rightNode , * result ;
39
44
PathItem * pathItem ;
@@ -199,8 +204,12 @@ recursiveExtract(JsQueryItem *jsq, bool indirect, PathItem *path)
199
204
return NULL ;
200
205
}
201
206
207
+ /*
208
+ * Count number of children connected with nodes of same type.
209
+ */
202
210
static int
203
- coundChildren (ExtractedNode * node , ExtractedNodeType type , bool first , bool * found )
211
+ coundChildren (ExtractedNode * node , ExtractedNodeType type ,
212
+ bool first , bool * found )
204
213
{
205
214
if ((node -> indirect || node -> type != type ) && !first )
206
215
{
@@ -216,6 +225,9 @@ coundChildren(ExtractedNode *node, ExtractedNodeType type, bool first, bool *fou
216
225
}
217
226
}
218
227
228
+ /*
229
+ * Fill array of children connected with nodes of same type.
230
+ */
219
231
static void
220
232
fillChildren (ExtractedNode * node , ExtractedNodeType type , bool first ,
221
233
ExtractedNode * * items , int * i )
@@ -233,6 +245,10 @@ fillChildren(ExtractedNode *node, ExtractedNodeType type, bool first,
233
245
}
234
246
}
235
247
248
+ /*
249
+ * Turn tree into "flat" form, turning nested binary AND/OR operators into
250
+ * single n-ary AND/OR operators.
251
+ */
236
252
static void
237
253
flatternTree (ExtractedNode * node )
238
254
{
@@ -260,6 +276,9 @@ flatternTree(ExtractedNode *node)
260
276
}
261
277
}
262
278
279
+ /*
280
+ * Compare path items chains from child to parent.
281
+ */
263
282
static int
264
283
comparePathItems (PathItem * i1 , PathItem * i2 )
265
284
{
@@ -295,6 +314,10 @@ comparePathItems(PathItem *i1, PathItem *i2)
295
314
}
296
315
}
297
316
317
+ /*
318
+ * Compare nodes in the order where conditions to the same fields are located
319
+ * together.
320
+ */
298
321
static int
299
322
compareNodes (const void * a1 , const void * a2 )
300
323
{
@@ -332,6 +355,9 @@ compareNodes(const void *a1, const void *a2)
332
355
}
333
356
}
334
357
358
+ /*
359
+ * Compare json values represented by JsQueryItems.
360
+ */
335
361
static int
336
362
compareJsQueryItem (JsQueryItem * v1 , JsQueryItem * v2 )
337
363
{
@@ -368,6 +394,10 @@ compareJsQueryItem(JsQueryItem *v1, JsQueryItem *v2)
368
394
return 0 ; /* make compiler happy */
369
395
}
370
396
397
+ /*
398
+ * Process group of nodes representing conditions for the same field. After
399
+ * processing group of nodes is replaced with one node.
400
+ */
371
401
static void
372
402
processGroup (ExtractedNode * node , int start , int end )
373
403
{
@@ -455,6 +485,10 @@ processGroup(ExtractedNode *node, int start, int end)
455
485
node -> args .items [i ] = NULL ;
456
486
}
457
487
488
+ /*
489
+ * Reduce number of nodes in tree, by turning multiple conditions about
490
+ * same field in same context into one node.
491
+ */
458
492
static void
459
493
simplifyRecursive (ExtractedNode * node )
460
494
{
@@ -494,6 +528,9 @@ simplifyRecursive(ExtractedNode *node)
494
528
}
495
529
}
496
530
531
+ /*
532
+ * Make entries for all leaf tree nodes using user-provided handler.
533
+ */
497
534
static ExtractedNode *
498
535
makeEntries (ExtractedNode * node , MakeEntryHandler handler , Pointer extra )
499
536
{
@@ -542,6 +579,10 @@ makeEntries(ExtractedNode *node, MakeEntryHandler handler, Pointer extra)
542
579
}
543
580
}
544
581
582
+ /*
583
+ * Returns false when query can be satisfied with no entries matching. True
584
+ * return value guarantees than it can be evaluated using index.
585
+ */
545
586
static bool
546
587
queryHasPositive (ExtractedNode * node )
547
588
{
@@ -578,6 +619,10 @@ queryHasPositive(ExtractedNode *node)
578
619
}
579
620
}
580
621
622
+ /*
623
+ * Checks if node evaluating with index needs recheck assuming match of
624
+ * entries itself doesn't need recheck.
625
+ */
581
626
static bool
582
627
needRecheckRecursive (ExtractedNode * node , bool not )
583
628
{
@@ -603,6 +648,18 @@ needRecheckRecursive(ExtractedNode *node, bool not)
603
648
}
604
649
}
605
650
651
+ /*
652
+ * Wrapper for "needRecheckRecursive".
653
+ */
654
+ bool
655
+ queryNeedRecheck (ExtractedNode * node )
656
+ {
657
+ return needRecheckRecursive (node , false);
658
+ }
659
+
660
+ /*
661
+ * Turn jsquery into tree of entries using user-provided handler.
662
+ */
606
663
ExtractedNode *
607
664
extractJsQuery (JsQuery * jq , MakeEntryHandler handler , Pointer extra )
608
665
{
@@ -619,11 +676,12 @@ extractJsQuery(JsQuery *jq, MakeEntryHandler handler, Pointer extra)
619
676
}
620
677
if (root && !queryHasPositive (root ))
621
678
root = NULL ;
622
- if (root )
623
- root -> indirect = needRecheckRecursive (root , false);
624
679
return root ;
625
680
}
626
681
682
+ /*
683
+ * Evaluate previously extracted tree.
684
+ */
627
685
bool
628
686
execRecursive (ExtractedNode * node , bool * check )
629
687
{
@@ -647,6 +705,9 @@ execRecursive(ExtractedNode *node, bool *check)
647
705
}
648
706
}
649
707
708
+ /*
709
+ * Evaluate previously extracted tree using tri-state logic.
710
+ */
650
711
bool
651
712
execRecursiveTristate (ExtractedNode * node , GinTernaryValue * check )
652
713
{
0 commit comments