Skip to content

Commit

Permalink
Add grn_expr_get_keywords()
Browse files Browse the repository at this point in the history
It gets keywords from expression.

TODO: Document it.
  • Loading branch information
naoa committed Aug 9, 2014
1 parent 1789121 commit 99886d9
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
2 changes: 2 additions & 0 deletions include/groonga.h
Expand Up @@ -1748,6 +1748,8 @@ GRN_API grn_rc grn_expr_snip_add_conditions(grn_ctx *ctx,
const char **closetags,
unsigned int *closetag_lens);

GRN_API grn_rc grn_expr_get_keywords(grn_ctx *ctx, grn_obj *expr, grn_obj *got_keywords);

GRN_API grn_table_sort_key *grn_table_sort_key_from_str(grn_ctx *ctx,
const char *str, unsigned int str_size,
grn_obj *table, unsigned int *nkeys);
Expand Down
41 changes: 41 additions & 0 deletions lib/expr.c
Expand Up @@ -7000,3 +7000,44 @@ grn_expr_syntax_escape_query(grn_ctx *ctx, const char *query, int query_size,
target_characters, GRN_QUERY_ESCAPE,
escaped_query);
}

grn_rc
grn_expr_get_keywords(grn_ctx *ctx, grn_obj *expr, grn_obj *got_keywords)
{
int i, n;
scan_info **sis, *si;
GRN_API_ENTER;
if ((sis = scan_info_build(ctx, expr, &n, GRN_OP_OR, 0))) {
int butp = 0, nparens = 0, npbut = 0;
grn_obj but_stack;
GRN_UINT32_INIT(&but_stack, GRN_OBJ_VECTOR);
for (i = n; i--;) {
si = sis[i];
if (si->flags & SCAN_POP) {
nparens++;
if (si->logical_op == GRN_OP_AND_NOT) {
GRN_UINT32_PUT(ctx, &but_stack, npbut);
npbut = nparens;
butp = 1 - butp;
}
} else {
if (si->op == GRN_OP_MATCH && si->query) {
if (butp == (si->logical_op == GRN_OP_AND_NOT)) {
GRN_PTR_PUT(ctx, got_keywords, si->query);
}
}
if (si->flags & SCAN_PUSH) {
if (nparens == npbut) {
butp = 1 - butp;
GRN_UINT32_POP(&but_stack, npbut);
}
nparens--;
}
}
}
GRN_OBJ_FIN(ctx, &but_stack);
for (i = n; i--;) { SI_FREE(sis[i]); }
GRN_FREE(sis);
}
GRN_API_RETURN(GRN_SUCCESS);
}

0 comments on commit 99886d9

Please sign in to comment.