Navigation Menu

Skip to content

Commit

Permalink
Add all_records()
Browse files Browse the repository at this point in the history
It matches all records. It's fast rather than "true" literal because
all_records() doesn't evaluate on each record. It just copies all
record IDs to result set table.

Note that getting all records is heavy process. You should use it only
when you need it.
  • Loading branch information
kou committed Aug 24, 2012
1 parent b0a2c79 commit 8cc2e2f
Show file tree
Hide file tree
Showing 6 changed files with 174 additions and 0 deletions.
17 changes: 17 additions & 0 deletions lib/expr.c
Expand Up @@ -4281,6 +4281,23 @@ grn_table_select(grn_ctx *ctx, grn_obj *table, grn_obj *expr,
/* todo : handle SCAN_PRE_CONST */
break;
}
} else {
switch (si->op) {
case GRN_OP_CALL :
if (selector_proc_p(si->args[0])) {
grn_rc rc;
grn_proc *proc = (grn_obj *)(si->args[0]);
rc = proc->selector(ctx, table, NULL, si->nargs, si->args,
res, si->logical_op);
if (rc) {
/* TODO: report error */
} else {
done++;
}
}
default :
break;
}
}
if (!done) {
e->codes = codes + si->start;
Expand Down
41 changes: 41 additions & 0 deletions lib/proc.c
Expand Up @@ -2724,6 +2724,39 @@ func_edit_distance(grn_ctx *ctx, int nargs, grn_obj **args, grn_user_data *user_
return obj;
}

static grn_obj *
func_all_records(grn_ctx *ctx, int nargs, grn_obj **args,
grn_user_data *user_data)
{
grn_obj *true_value;
if ((true_value = GRN_PROC_ALLOC(GRN_DB_BOOL, 0))) {
GRN_BOOL_SET(ctx, true_value, GRN_TRUE);
}
return true_value;
}

static grn_rc
selector_all_records(grn_ctx *ctx, grn_obj *table, grn_obj *index,
int nargs, grn_obj **args,
grn_obj *res, grn_operator op)
{
grn_obj score;

GRN_UINT32_INIT(&score, 0);
GRN_UINT32_SET(ctx, &score, 1);

GRN_TABLE_EACH(ctx, table, 0, 0, id, NULL, NULL, NULL, {
grn_id result_id;
result_id = grn_table_add(ctx, res, &id, sizeof(grn_id), NULL);
grn_obj_set_value(ctx, res, result_id, &score, GRN_OBJ_SET);
});

GRN_OBJ_FIN(ctx, &score);

return ctx->rc;
}


#define DEF_VAR(v,name_str) do {\
(v).name = (name_str);\
(v).name_size = GRN_STRLEN(name_str);\
Expand Down Expand Up @@ -2885,4 +2918,12 @@ grn_db_init_builtin_query(grn_ctx *ctx)

grn_proc_create(ctx, "edit_distance", 13, GRN_PROC_FUNCTION,
func_edit_distance, NULL, NULL, 0, NULL);

{
grn_obj *selector_proc;

selector_proc = grn_proc_create(ctx, "all_records", 11, GRN_PROC_FUNCTION,
func_all_records, NULL, NULL, 0, NULL);
grn_proc_set_selector(ctx, selector_proc, selector_all_records);
}
}
47 changes: 47 additions & 0 deletions test/function/suite/select/function/all_records/function.expected
@@ -0,0 +1,47 @@
table_create Softwares TABLE_HASH_KEY ShortText
[[0,0.0,0.0],true]
load --table Softwares
[
["_key"],
["groonga"],
["mroonga"],
["rroonga"]
]
[[0,0.0,0.0],3]
select Softwares --filter 'all_records() == true'
[
[
0,
0.0,
0.0
],
[
[
[
3
],
[
[
"_id",
"UInt32"
],
[
"_key",
"ShortText"
]
],
[
1,
"groonga"
],
[
2,
"mroonga"
],
[
3,
"rroonga"
]
]
]
]
11 changes: 11 additions & 0 deletions test/function/suite/select/function/all_records/function.test
@@ -0,0 +1,11 @@
table_create Softwares TABLE_HASH_KEY ShortText

load --table Softwares
[
["_key"],
["groonga"],
["mroonga"],
["rroonga"]
]

select Softwares --filter 'all_records() == true'
47 changes: 47 additions & 0 deletions test/function/suite/select/function/all_records/selector.expected
@@ -0,0 +1,47 @@
table_create Softwares TABLE_HASH_KEY ShortText
[[0,0.0,0.0],true]
load --table Softwares
[
["_key"],
["groonga"],
["mroonga"],
["rroonga"]
]
[[0,0.0,0.0],3]
select Softwares --filter 'all_records()'
[
[
0,
0.0,
0.0
],
[
[
[
3
],
[
[
"_id",
"UInt32"
],
[
"_key",
"ShortText"
]
],
[
1,
"groonga"
],
[
2,
"mroonga"
],
[
3,
"rroonga"
]
]
]
]
11 changes: 11 additions & 0 deletions test/function/suite/select/function/all_records/selector.test
@@ -0,0 +1,11 @@
table_create Softwares TABLE_HASH_KEY ShortText

load --table Softwares
[
["_key"],
["groonga"],
["mroonga"],
["rroonga"]
]

select Softwares --filter 'all_records()'

0 comments on commit 8cc2e2f

Please sign in to comment.