Kouhei Sutou
null+****@clear*****
Wed Apr 19 16:42:20 JST 2017
Kouhei Sutou 2017-04-19 16:42:20 +0900 (Wed, 19 Apr 2017) New Revision: 25ed3071a374e2068290ac25c0349ec9d90f2961 https://github.com/groonga/groonga/commit/25ed3071a374e2068290ac25c0349ec9d90f2961 Message: in_records: support customizing mode Added files: test/command/suite/select/function/in_records/match.test Copied files: test/command/suite/select/function/in_records/match.expected (from test/command/suite/select/function/in_records/one.expected) Modified files: lib/proc/proc_in_records.c test/command/suite/select/function/in_records/multiple.expected test/command/suite/select/function/in_records/multiple.test test/command/suite/select/function/in_records/one.expected test/command/suite/select/function/in_records/one.test Modified: lib/proc/proc_in_records.c (+83 -16) =================================================================== --- lib/proc/proc_in_records.c 2017-04-19 16:38:05 +0900 (35d39e6) +++ lib/proc/proc_in_records.c 2017-04-19 16:42:20 +0900 (308655d) @@ -29,6 +29,7 @@ func_in_records(grn_ctx *ctx, grn_obj *found; grn_obj *condition_table; grn_obj condition_columns; + grn_operator_exec_func **condition_functions = NULL; int i; found = grn_plugin_proc_alloc(ctx, user_data, GRN_DB_BOOL, 0); @@ -37,18 +38,18 @@ func_in_records(grn_ctx *ctx, } GRN_BOOL_SET(ctx, found, GRN_FALSE); - if (n_args < 3) { + if (n_args < 4) { GRN_PLUGIN_ERROR(ctx, GRN_INVALID_ARGUMENT, - "in_records(): wrong number of arguments (%d for 3..)", + "in_records(): wrong number of arguments (%d for 4..)", n_args); return found; } - if ((n_args % 2) != 1) { + if ((n_args % 3) != 1) { GRN_PLUGIN_ERROR(ctx, GRN_INVALID_ARGUMENT, - "in_records(): the number of arguments must be odd (%d)", + "in_records(): the number of arguments must be 1 + 3n (%d)", n_args); return found; } @@ -68,10 +69,12 @@ func_in_records(grn_ctx *ctx, } GRN_PTR_INIT(&condition_columns, GRN_OBJ_VECTOR, GRN_ID_NIL); - for (i = 1; i < n_args; i += 2) { - grn_obj *column_name = args[i + 1]; + for (i = 1; i < n_args; i += 3) { + int column_name_i = i + 2; + grn_obj *column_name; grn_obj *condition_column; + column_name = args[column_name_i]; if (!grn_obj_is_text_family_bulk(ctx, column_name)) { grn_obj inspected; GRN_TEXT_INIT(&inspected, 0); @@ -81,7 +84,7 @@ func_in_records(grn_ctx *ctx, "in_records(): " "the %dth argument must be column name as string: " "<%.*s>", - i + 1, + column_name_i, (int)GRN_TEXT_LEN(&inspected), GRN_TEXT_VALUE(&inspected)); GRN_OBJ_FIN(ctx, &inspected); @@ -100,7 +103,7 @@ func_in_records(grn_ctx *ctx, "in_records(): " "the %dth argument must be existing column name: " "<%.*s>: <%.*s>", - i + 1, + column_name_i, (int)GRN_TEXT_LEN(column_name), GRN_TEXT_VALUE(column_name), (int)GRN_TEXT_LEN(&inspected), @@ -111,6 +114,64 @@ func_in_records(grn_ctx *ctx, GRN_PTR_PUT(ctx, &condition_columns, condition_column); } + condition_functions = + GRN_PLUGIN_MALLOCN(ctx, grn_operator_exec_func *, (n_args - 1 / 3)); + for (i = 1; i < n_args; i += 3) { + int nth = (i - 1) / 3; + int mode_name_i = i + 1; + grn_obj *mode_name; + grn_operator mode; + + mode_name = args[mode_name_i]; + mode = grn_proc_option_value_mode(ctx, + mode_name, + GRN_OP_EQUAL, + "in_records()"); + if (ctx->rc != GRN_SUCCESS) { + goto exit; + } + + switch (mode) { + case GRN_OP_EQUAL : + condition_functions[nth] = grn_operator_exec_equal; + break; + case GRN_OP_NOT_EQUAL : + condition_functions[nth] = grn_operator_exec_not_equal; + break; + case GRN_OP_LESS : + condition_functions[nth] = grn_operator_exec_less; + break; + case GRN_OP_GREATER : + condition_functions[nth] = grn_operator_exec_greater; + break; + case GRN_OP_LESS_EQUAL : + condition_functions[nth] = grn_operator_exec_less_equal; + break; + case GRN_OP_GREATER_EQUAL : + condition_functions[nth] = grn_operator_exec_greater_equal; + break; + case GRN_OP_MATCH : + condition_functions[nth] = grn_operator_exec_match; + break; + case GRN_OP_PREFIX : + condition_functions[nth] = grn_operator_exec_prefix; + break; + case GRN_OP_REGEXP : + condition_functions[nth] = grn_operator_exec_regexp; + break; + default : + GRN_PLUGIN_ERROR(ctx, + GRN_INVALID_ARGUMENT, + "in_records(): " + "the %dth argument is unsupported mode: <%.*s>", + mode_name_i, + (int)GRN_TEXT_LEN(mode_name), + GRN_TEXT_VALUE(mode_name)); + goto exit; + break; + } + } + { grn_obj condition_column_value; @@ -118,11 +179,14 @@ func_in_records(grn_ctx *ctx, GRN_TABLE_EACH_BEGIN(ctx, condition_table, cursor, id) { grn_bool found_record = GRN_TRUE; - for (i = 1; i < n_args; i += 2) { - grn_obj *value = args[i]; + for (i = 1; i < n_args; i += 3) { + int nth = (i - 1) / 3; grn_obj *condition_column; + grn_operator_exec_func *condition_function; + grn_obj *value = args[i]; - condition_column = GRN_PTR_VALUE_AT(&condition_columns, (i - 1) / 2); + condition_column = GRN_PTR_VALUE_AT(&condition_columns, nth); + condition_function = condition_functions[nth]; if (grn_obj_is_data_column(ctx, condition_column)) { grn_bool found_value = GRN_FALSE; @@ -132,9 +196,9 @@ func_in_records(grn_ctx *ctx, id, &condition_column_value); - found_value = grn_operator_exec_equal(ctx, - value, - &condition_column_value); + found_value = condition_function(ctx, + value, + &condition_column_value); if (ctx->rc != GRN_SUCCESS) { found_record = GRN_FALSE; break; @@ -159,10 +223,13 @@ func_in_records(grn_ctx *ctx, } exit : + GRN_PLUGIN_FREE(ctx, condition_functions); + for (i = 1; i < n_args; i += 2) { + int nth = (i - 1) / 3; grn_obj *condition_column; - condition_column = GRN_PTR_VALUE_AT(&condition_columns, (i - 1) / 2); - if (condition_column->header.type == GRN_ACCESSOR) { + condition_column = GRN_PTR_VALUE_AT(&condition_columns, nth); + if (condition_column && condition_column->header.type == GRN_ACCESSOR) { grn_obj_unlink(ctx, condition_column); } } Copied: test/command/suite/select/function/in_records/match.expected (+10 -12) 58% =================================================================== --- test/command/suite/select/function/in_records/one.expected 2017-04-19 16:38:05 +0900 (22ac7e1) +++ test/command/suite/select/function/in_records/match.expected 2017-04-19 16:42:20 +0900 (e05d94d) @@ -1,17 +1,15 @@ -table_create Users TABLE_HASH_KEY ShortText +table_create Conditions TABLE_NO_KEY [[0,0.0,0.0],true] -table_create Reports TABLE_NO_KEY -[[0,0.0,0.0],true] -column_create Reports user COLUMN_SCALAR Users +column_create Conditions user_pattern COLUMN_SCALAR ShortText [[0,0.0,0.0],true] table_create Logs TABLE_NO_KEY [[0,0.0,0.0],true] -column_create Logs user COLUMN_SCALAR Users +column_create Logs user COLUMN_SCALAR ShortText [[0,0.0,0.0],true] -load --table Reports +load --table Conditions [ -{"user": "alice"}, -{"user": "david"} +{"user_pattern": "ali"}, +{"user_pattern": "ris"} ] [[0,0.0,0.0],2] load --table Logs @@ -22,7 +20,7 @@ load --table Logs {"user": "david"} ] [[0,0.0,0.0],4] -select Logs --filter 'in_records(Reports, user, "user")' +select Logs --filter 'in_records(Conditions, user, "@", "user_pattern")' [ [ 0, @@ -41,7 +39,7 @@ select Logs --filter 'in_records(Reports, user, "user")' ], [ "user", - "Users" + "ShortText" ] ], [ @@ -49,8 +47,8 @@ select Logs --filter 'in_records(Reports, user, "user")' "alice" ], [ - 4, - "david" + 3, + "chris" ] ] ] Added: test/command/suite/select/function/in_records/match.test (+22 -0) 100644 =================================================================== --- /dev/null +++ test/command/suite/select/function/in_records/match.test 2017-04-19 16:42:20 +0900 (c13c387) @@ -0,0 +1,22 @@ +table_create Conditions TABLE_NO_KEY +column_create Conditions user_pattern COLUMN_SCALAR ShortText + +table_create Logs TABLE_NO_KEY +column_create Logs user COLUMN_SCALAR ShortText + +load --table Conditions +[ +{"user_pattern": "ali"}, +{"user_pattern": "ris"} +] + +load --table Logs +[ +{"user": "alice"}, +{"user": "bob"}, +{"user": "chris"}, +{"user": "david"} +] + +select Logs \ + --filter 'in_records(Conditions, user, "@", "user_pattern")' Modified: test/command/suite/select/function/in_records/multiple.expected (+1 -1) =================================================================== --- test/command/suite/select/function/in_records/multiple.expected 2017-04-19 16:38:05 +0900 (ea0c26e) +++ test/command/suite/select/function/in_records/multiple.expected 2017-04-19 16:42:20 +0900 (86061ab) @@ -37,7 +37,7 @@ load --table Logs {"user": "david", "tag": "tag2", "time": "2017-04-20 11:22:33"} ] [[0,0.0,0.0],5] -select Logs --filter 'in_records(Reports, user, "user", tag, "tag", time_classify_day(time), "day")' +select Logs --filter 'in_records(Reports, user, "==", "user", tag, "==", "tag", time_classify_day(time), "==", "day")' [ [ 0, Modified: test/command/suite/select/function/in_records/multiple.test (+3 -3) =================================================================== --- test/command/suite/select/function/in_records/multiple.test 2017-04-19 16:38:05 +0900 (6c04630) +++ test/command/suite/select/function/in_records/multiple.test 2017-04-19 16:42:20 +0900 (a47a235) @@ -32,6 +32,6 @@ load --table Logs select Logs \ --filter 'in_records(Reports, \ - user, "user", \ - tag, "tag", \ - time_classify_day(time), "day")' + user, "==", "user", \ + tag, "==", "tag", \ + time_classify_day(time), "==", "day")' Modified: test/command/suite/select/function/in_records/one.expected (+1 -1) =================================================================== --- test/command/suite/select/function/in_records/one.expected 2017-04-19 16:38:05 +0900 (22ac7e1) +++ test/command/suite/select/function/in_records/one.expected 2017-04-19 16:42:20 +0900 (9bc77b6) @@ -22,7 +22,7 @@ load --table Logs {"user": "david"} ] [[0,0.0,0.0],4] -select Logs --filter 'in_records(Reports, user, "user")' +select Logs --filter 'in_records(Reports, user, "==", "user")' [ [ 0, Modified: test/command/suite/select/function/in_records/one.test (+1 -1) =================================================================== --- test/command/suite/select/function/in_records/one.test 2017-04-19 16:38:05 +0900 (9e7163c) +++ test/command/suite/select/function/in_records/one.test 2017-04-19 16:42:20 +0900 (9ebfbac) @@ -21,4 +21,4 @@ load --table Logs ] select Logs \ - --filter 'in_records(Reports, user, "user")' + --filter 'in_records(Reports, user, "==", "user")' -------------- next part -------------- HTML����������������������������... 下載