Kouhei Sutou
null+****@clear*****
Tue Sep 26 11:25:22 JST 2017
Kouhei Sutou 2017-09-26 11:25:22 +0900 (Tue, 26 Sep 2017) New Revision: 85e93c900e6443bf8874673658d90b7b28f51640 https://github.com/groonga/groonga/commit/85e93c900e6443bf8874673658d90b7b28f51640 Message: Support caching select result with function call New API: * grn_proc_set_is_stable() * grn_proc_is_stable() CAUTION: If you add a new function that may return different result with the same argument, you must call grn_proc_is_stable(ctx, proc, GRN_FALSE). If you don't call it, select result with the function call is cached and is wrong result for multiple requests. I choose GRN_TRUE as the default is_stable value. Because the most existing Groonga functions are stable. Modified files: include/groonga/groonga.h lib/db.c lib/expr.c lib/grn_db.h lib/proc.c Modified: include/groonga/groonga.h (+5 -0) =================================================================== --- include/groonga/groonga.h 2017-09-25 15:05:53 +0900 (f51b132d2) +++ include/groonga/groonga.h 2017-09-26 11:25:22 +0900 (e289bf97f) @@ -826,6 +826,11 @@ GRN_API grn_rc grn_proc_set_selector_operator(grn_ctx *ctx, GRN_API grn_operator grn_proc_get_selector_operator(grn_ctx *ctx, grn_obj *proc); +GRN_API grn_rc grn_proc_set_is_stable(grn_ctx *ctx, + grn_obj *proc, + grn_bool is_stable); +GRN_API grn_bool grn_proc_is_stable(grn_ctx *ctx, grn_obj *proc); + /*------------------------------------------------------------- * grn_vector */ Modified: lib/db.c (+1 -0) =================================================================== --- lib/db.c 2017-09-25 15:05:53 +0900 (cf7fc4c37) +++ lib/db.c 2017-09-26 11:25:22 +0900 (34e339224) @@ -901,6 +901,7 @@ grn_proc_create(grn_ctx *ctx, const char *name, int name_size, grn_proc_type typ res->funcs[PROC_FIN] = fin; memset(&(res->callbacks), 0, sizeof(res->callbacks)); res->callbacks.function.selector_op = GRN_OP_NOP; + res->callbacks.function.is_stable = GRN_TRUE; GRN_TEXT_INIT(&res->name_buf, 0); res->vars = NULL; res->nvars = 0; Modified: lib/expr.c (+34 -10) =================================================================== --- lib/expr.c 2017-09-25 15:05:53 +0900 (d6e49ad40) +++ lib/expr.c 2017-09-26 11:25:22 +0900 (77fb99880) @@ -264,6 +264,27 @@ grn_proc_get_selector_operator(grn_ctx *ctx, grn_obj *proc) return proc_->callbacks.function.selector_op; } +grn_rc +grn_proc_set_is_stable(grn_ctx *ctx, grn_obj *proc, grn_bool is_stable) +{ + grn_proc *proc_ = (grn_proc *)proc; + if (!grn_obj_is_function_proc(ctx, proc)) { + return GRN_INVALID_ARGUMENT; + } + proc_->callbacks.function.is_stable = is_stable; + return GRN_SUCCESS; +} + +grn_bool +grn_proc_is_stable(grn_ctx *ctx, grn_obj *proc) +{ + grn_proc *proc_ = (grn_proc *)proc; + if (!grn_obj_is_function_proc(ctx, proc)) { + return GRN_FALSE; + } + return proc_->callbacks.function.is_stable; +} + /* grn_expr */ grn_obj * @@ -963,17 +984,20 @@ grn_expr_append_obj(grn_ctx *ctx, grn_obj *expr, grn_obj *obj, grn_operator op, GRN_OBJ_FIN(ctx, &buffer); goto exit; } + + PUSH_CODE(e, op, obj, nargs, code); + { + int i = nargs - 1; + while (i--) { dfi = grn_expr_dfi_pop(e); } + } + if (!obj) { dfi = grn_expr_dfi_pop(e); } + // todo : increment e->values_tail. + /* cannot identify type of return value */ + grn_expr_dfi_put(ctx, e, type, domain, code); + if (!grn_proc_is_stable(ctx, proc)) { + e->cacheable = 0; + } } - PUSH_CODE(e, op, obj, nargs, code); - { - int i = nargs - 1; - while (i--) { dfi = grn_expr_dfi_pop(e); } - } - if (!obj) { dfi = grn_expr_dfi_pop(e); } - // todo : increment e->values_tail. - /* cannot identify type of return value */ - grn_expr_dfi_put(ctx, e, type, domain, code); - e->cacheable = 0; break; case GRN_OP_INTERN : if (obj && CONSTP(obj)) { Modified: lib/grn_db.h (+1 -0) =================================================================== --- lib/grn_db.h 2017-09-25 15:05:53 +0900 (b270b000e) +++ lib/grn_db.h 2017-09-26 11:25:22 +0900 (f0fbddad8) @@ -243,6 +243,7 @@ struct _grn_proc { struct { grn_selector_func *selector; grn_operator selector_op; + grn_bool is_stable; } function; struct { grn_command_run_func *run; Modified: lib/proc.c (+14 -7) =================================================================== --- lib/proc.c 2017-09-25 15:05:53 +0900 (ff6fe978e) +++ lib/proc.c 2017-09-26 11:25:22 +0900 (e21769f3f) @@ -4027,17 +4027,24 @@ grn_db_init_builtin_commands(grn_ctx *ctx) DEF_COMMAND("normalizer_list", proc_normalizer_list, 0, vars); - DEF_VAR(vars[0], "seed"); - grn_proc_create(ctx, "rand", -1, GRN_PROC_FUNCTION, func_rand, - NULL, NULL, 0, vars); + { + grn_obj *proc; + proc = grn_proc_create(ctx, "rand", -1, GRN_PROC_FUNCTION, func_rand, + NULL, NULL, 0, NULL); + grn_proc_set_is_stable(ctx, proc, GRN_FALSE); + } - grn_proc_create(ctx, "now", -1, GRN_PROC_FUNCTION, func_now, - NULL, NULL, 0, vars); + { + grn_obj *proc; + proc = grn_proc_create(ctx, "now", -1, GRN_PROC_FUNCTION, func_now, + NULL, NULL, 0, NULL); + grn_proc_set_is_stable(ctx, proc, GRN_FALSE); + } grn_proc_create(ctx, "max", -1, GRN_PROC_FUNCTION, func_max, - NULL, NULL, 0, vars); + NULL, NULL, 0, NULL); grn_proc_create(ctx, "min", -1, GRN_PROC_FUNCTION, func_min, - NULL, NULL, 0, vars); + NULL, NULL, 0, NULL); { grn_obj *selector_proc; -------------- next part -------------- HTML����������������������������... URL: https://lists.osdn.me/mailman/archives/groonga-commit/attachments/20170926/6cb2ad01/attachment-0001.htm