1 #include "ejdb2_internal.h"
2
3 // Primary key scanner
jbi_pk_scanner(struct _JBEXEC * ctx,JB_SCAN_CONSUMER consumer)4 iwrc jbi_pk_scanner(struct _JBEXEC *ctx, JB_SCAN_CONSUMER consumer) {
5 iwrc rc = 0;
6 int64_t id, step;
7 bool matched;
8 struct JQP_AUX *aux = ctx->ux->q->aux;
9 assert(aux->expr->flags & JQP_EXPR_NODE_FLAG_PK);
10 JQP_EXPR_NODE_PK *pk = (void*) aux->expr;
11 assert(pk->argument);
12 JQVAL *jqvp = jql_unit_to_jqval(aux, pk->argument, &rc);
13 RCGO(rc, finish);
14
15 if ((jqvp->type == JQVAL_JBLNODE) && (jqvp->vnode->type == JBV_ARRAY)) {
16 JQVAL jqv;
17 JBL_NODE nv = jqvp->vnode->child;
18 if (!nv) {
19 goto finish;
20 }
21 step = 1;
22 do {
23 jql_node_to_jqval(nv, &jqv);
24 if (jql_jqval_as_int(&jqv, &id)) {
25 if (step > 0) {
26 --step;
27 } else if (step < 0) {
28 ++step;
29 }
30 if (!step) {
31 step = 1;
32 rc = consumer(ctx, 0, id, &step, &matched, 0);
33 RCGO(rc, finish);
34 }
35 }
36 } while (step && (step > 0 ? (nv = nv->next) : (nv = nv->prev)));
37 } else if (jql_jqval_as_int(jqvp, &id)) {
38 rc = consumer(ctx, 0, id, &step, &matched, 0);
39 }
40
41 finish:
42 return consumer(ctx, 0, 0, 0, 0, rc);
43 }
44