Kouhei Sutou
null+****@clear*****
Thu Nov 22 17:46:50 JST 2012
Kouhei Sutou 2012-11-22 17:46:50 +0900 (Thu, 22 Nov 2012) New Revision: 62ce9d02623f808984122c247bd5212ce198440f https://github.com/groonga/groonga/commit/62ce9d02623f808984122c247bd5212ce198440f Merged 0f20163: Merge pull request #41 from groonga/support-range-search-by-index Log: Support range search by index Added files: test/command/suite/select/index/range/greater/time.expected test/command/suite/select/index/range/greater/time.test test/command/suite/select/index/range/greater_equal/time.expected test/command/suite/select/index/range/greater_equal/time.test test/command/suite/select/index/range/less/time.expected test/command/suite/select/index/range/less/time.test test/command/suite/select/index/range/less_equal/time.expected test/command/suite/select/index/range/less_equal/time.test Modified files: lib/expr.c Modified: lib/expr.c (+73 -0) =================================================================== --- lib/expr.c 2012-11-22 17:30:52 +0900 (53251c8) +++ lib/expr.c 2012-11-22 17:46:50 +0900 (bb5e98c) @@ -4031,6 +4031,73 @@ grn_view_select(grn_ctx *ctx, grn_obj *table, grn_obj *expr, } static inline grn_bool +grn_table_select_index_range(grn_ctx *ctx, grn_obj *table, grn_obj *index, + scan_info *si, grn_obj *res) +{ + grn_bool processed = GRN_FALSE; + grn_obj *index_table; + grn_obj range; + + index_table = grn_ctx_at(ctx, index->header.domain); + if (!index_table) { + return GRN_FALSE; + } + + GRN_OBJ_INIT(&range, GRN_BULK, 0, index_table->header.domain); + if (grn_obj_cast(ctx, si->query, &range, GRN_FALSE) == GRN_SUCCESS) { + grn_table_cursor *cursor; + const void *min = NULL, *max = NULL; + unsigned int min_size = 0, max_size = 0; + int offset = 0; + int limit = -1; + int flags = GRN_CURSOR_ASCENDING; + + switch (si->op) { + case GRN_OP_LESS : + flags |= GRN_CURSOR_LT; + max = GRN_BULK_HEAD(&range); + max_size = GRN_BULK_VSIZE(&range); + break; + case GRN_OP_GREATER : + flags |= GRN_CURSOR_GT; + min = GRN_BULK_HEAD(&range); + min_size = GRN_BULK_VSIZE(&range); + break; + case GRN_OP_LESS_EQUAL : + flags |= GRN_CURSOR_LE; + max = GRN_BULK_HEAD(&range); + max_size = GRN_BULK_VSIZE(&range); + break; + case GRN_OP_GREATER_EQUAL : + flags |= GRN_CURSOR_GE; + min = GRN_BULK_HEAD(&range); + min_size = GRN_BULK_VSIZE(&range); + break; + default : + break; + } + cursor = grn_table_cursor_open(ctx, index_table, + min, min_size, max, max_size, + offset, limit, flags); + if (cursor) { + grn_id index_id; + while ((index_id = grn_table_cursor_next(ctx, cursor))) { + grn_ii_at(ctx, (grn_ii *)index, index_id, + (grn_hash *)res, si->logical_op); + } + grn_table_cursor_close(ctx, cursor); + processed = GRN_TRUE; + } + grn_obj_unlink(ctx, index_table); + + grn_ii_resolve_sel_and(ctx, (grn_hash *)res, si->logical_op); + } + GRN_OBJ_FIN(ctx, &range); + + return processed; +} + +static inline grn_bool grn_table_select_index(grn_ctx *ctx, grn_obj *table, scan_info *si, grn_obj *res) { @@ -4252,6 +4319,12 @@ grn_table_select_index(grn_ctx *ctx, grn_obj *table, scan_info *si, } } break; + case GRN_OP_LESS : + case GRN_OP_GREATER : + case GRN_OP_LESS_EQUAL : + case GRN_OP_GREATER_EQUAL : + processed = grn_table_select_index_range(ctx, table, index, si, res); + break; default : /* todo : implement */ /* todo : handle SCAN_PRE_CONST */ Added: test/command/suite/select/index/range/greater/time.expected (+49 -0) 100644 =================================================================== --- /dev/null +++ test/command/suite/select/index/range/greater/time.expected 2012-11-22 17:46:50 +0900 (675a5a6) @@ -0,0 +1,49 @@ +table_create Users TABLE_HASH_KEY ShortText +[[0,0.0,0.0],true] +column_create Users birthday COLUMN_SCALAR Time +[[0,0.0,0.0],true] +table_create Birthdays TABLE_PAT_KEY Time +[[0,0.0,0.0],true] +column_create Birthdays users_birthday COLUMN_INDEX Users birthday +[[0,0.0,0.0],true] +load --table Users +[ +{"_key": "Alice", "birthday": "1992-02-09 00:00:00"}, +{"_key": "Bob", "birthday": "1988-01-04 00:00:00"}, +{"_key": "Carlos", "birthday": "1982-12-29 00:00:00"} +] +[[0,0.0,0.0],3] +select Users --filter 'birthday > "1988-01-04 00:00:00"' +[ + [ + 0, + 0.0, + 0.0 + ], + [ + [ + [ + 1 + ], + [ + [ + "_id", + "UInt32" + ], + [ + "_key", + "ShortText" + ], + [ + "birthday", + "Time" + ] + ], + [ + 1, + "Alice", + 697561200.0 + ] + ] + ] +] Added: test/command/suite/select/index/range/greater/time.test (+14 -0) 100644 =================================================================== --- /dev/null +++ test/command/suite/select/index/range/greater/time.test 2012-11-22 17:46:50 +0900 (ede6cd0) @@ -0,0 +1,14 @@ +table_create Users TABLE_HASH_KEY ShortText +column_create Users birthday COLUMN_SCALAR Time + +table_create Birthdays TABLE_PAT_KEY Time +column_create Birthdays users_birthday COLUMN_INDEX Users birthday + +load --table Users +[ +{"_key": "Alice", "birthday": "1992-02-09 00:00:00"}, +{"_key": "Bob", "birthday": "1988-01-04 00:00:00"}, +{"_key": "Carlos", "birthday": "1982-12-29 00:00:00"} +] + +select Users --filter 'birthday > "1988-01-04 00:00:00"' Added: test/command/suite/select/index/range/greater_equal/time.expected (+54 -0) 100644 =================================================================== --- /dev/null +++ test/command/suite/select/index/range/greater_equal/time.expected 2012-11-22 17:46:50 +0900 (a311d49) @@ -0,0 +1,54 @@ +table_create Users TABLE_HASH_KEY ShortText +[[0,0.0,0.0],true] +column_create Users birthday COLUMN_SCALAR Time +[[0,0.0,0.0],true] +table_create Birthdays TABLE_PAT_KEY Time +[[0,0.0,0.0],true] +column_create Birthdays users_birthday COLUMN_INDEX Users birthday +[[0,0.0,0.0],true] +load --table Users +[ +{"_key": "Alice", "birthday": "1992-02-09 00:00:00"}, +{"_key": "Bob", "birthday": "1988-01-04 00:00:00"}, +{"_key": "Carlos", "birthday": "1982-12-29 00:00:00"} +] +[[0,0.0,0.0],3] +select Users --filter 'birthday >= "1988-01-04 00:00:00"' +[ + [ + 0, + 0.0, + 0.0 + ], + [ + [ + [ + 2 + ], + [ + [ + "_id", + "UInt32" + ], + [ + "_key", + "ShortText" + ], + [ + "birthday", + "Time" + ] + ], + [ + 2, + "Bob", + 568220400.0 + ], + [ + 1, + "Alice", + 697561200.0 + ] + ] + ] +] Added: test/command/suite/select/index/range/greater_equal/time.test (+14 -0) 100644 =================================================================== --- /dev/null +++ test/command/suite/select/index/range/greater_equal/time.test 2012-11-22 17:46:50 +0900 (f2bf0a3) @@ -0,0 +1,14 @@ +table_create Users TABLE_HASH_KEY ShortText +column_create Users birthday COLUMN_SCALAR Time + +table_create Birthdays TABLE_PAT_KEY Time +column_create Birthdays users_birthday COLUMN_INDEX Users birthday + +load --table Users +[ +{"_key": "Alice", "birthday": "1992-02-09 00:00:00"}, +{"_key": "Bob", "birthday": "1988-01-04 00:00:00"}, +{"_key": "Carlos", "birthday": "1982-12-29 00:00:00"} +] + +select Users --filter 'birthday >= "1988-01-04 00:00:00"' Added: test/command/suite/select/index/range/less/time.expected (+49 -0) 100644 =================================================================== --- /dev/null +++ test/command/suite/select/index/range/less/time.expected 2012-11-22 17:46:50 +0900 (9babc28) @@ -0,0 +1,49 @@ +table_create Users TABLE_HASH_KEY ShortText +[[0,0.0,0.0],true] +column_create Users birthday COLUMN_SCALAR Time +[[0,0.0,0.0],true] +table_create Birthdays TABLE_PAT_KEY Time +[[0,0.0,0.0],true] +column_create Birthdays users_birthday COLUMN_INDEX Users birthday +[[0,0.0,0.0],true] +load --table Users +[ +{"_key": "Alice", "birthday": "1992-02-09 00:00:00"}, +{"_key": "Bob", "birthday": "1988-01-04 00:00:00"}, +{"_key": "Carlos", "birthday": "1982-12-29 00:00:00"} +] +[[0,0.0,0.0],3] +select Users --filter 'birthday < "1988-01-04 00:00:00"' +[ + [ + 0, + 0.0, + 0.0 + ], + [ + [ + [ + 1 + ], + [ + [ + "_id", + "UInt32" + ], + [ + "_key", + "ShortText" + ], + [ + "birthday", + "Time" + ] + ], + [ + 3, + "Carlos", + 409935600.0 + ] + ] + ] +] Added: test/command/suite/select/index/range/less/time.test (+14 -0) 100644 =================================================================== --- /dev/null +++ test/command/suite/select/index/range/less/time.test 2012-11-22 17:46:50 +0900 (f3ee65d) @@ -0,0 +1,14 @@ +table_create Users TABLE_HASH_KEY ShortText +column_create Users birthday COLUMN_SCALAR Time + +table_create Birthdays TABLE_PAT_KEY Time +column_create Birthdays users_birthday COLUMN_INDEX Users birthday + +load --table Users +[ +{"_key": "Alice", "birthday": "1992-02-09 00:00:00"}, +{"_key": "Bob", "birthday": "1988-01-04 00:00:00"}, +{"_key": "Carlos", "birthday": "1982-12-29 00:00:00"} +] + +select Users --filter 'birthday < "1988-01-04 00:00:00"' Added: test/command/suite/select/index/range/less_equal/time.expected (+54 -0) 100644 =================================================================== --- /dev/null +++ test/command/suite/select/index/range/less_equal/time.expected 2012-11-22 17:46:50 +0900 (5e3b170) @@ -0,0 +1,54 @@ +table_create Users TABLE_HASH_KEY ShortText +[[0,0.0,0.0],true] +column_create Users birthday COLUMN_SCALAR Time +[[0,0.0,0.0],true] +table_create Birthdays TABLE_PAT_KEY Time +[[0,0.0,0.0],true] +column_create Birthdays users_birthday COLUMN_INDEX Users birthday +[[0,0.0,0.0],true] +load --table Users +[ +{"_key": "Alice", "birthday": "1992-02-09 00:00:00"}, +{"_key": "Bob", "birthday": "1988-01-04 00:00:00"}, +{"_key": "Carlos", "birthday": "1982-12-29 00:00:00"} +] +[[0,0.0,0.0],3] +select Users --filter 'birthday <= "1988-01-04 00:00:00"' +[ + [ + 0, + 0.0, + 0.0 + ], + [ + [ + [ + 2 + ], + [ + [ + "_id", + "UInt32" + ], + [ + "_key", + "ShortText" + ], + [ + "birthday", + "Time" + ] + ], + [ + 3, + "Carlos", + 409935600.0 + ], + [ + 2, + "Bob", + 568220400.0 + ] + ] + ] +] Added: test/command/suite/select/index/range/less_equal/time.test (+14 -0) 100644 =================================================================== --- /dev/null +++ test/command/suite/select/index/range/less_equal/time.test 2012-11-22 17:46:50 +0900 (7bb0fd7) @@ -0,0 +1,14 @@ +table_create Users TABLE_HASH_KEY ShortText +column_create Users birthday COLUMN_SCALAR Time + +table_create Birthdays TABLE_PAT_KEY Time +column_create Birthdays users_birthday COLUMN_INDEX Users birthday + +load --table Users +[ +{"_key": "Alice", "birthday": "1992-02-09 00:00:00"}, +{"_key": "Bob", "birthday": "1988-01-04 00:00:00"}, +{"_key": "Carlos", "birthday": "1982-12-29 00:00:00"} +] + +select Users --filter 'birthday <= "1988-01-04 00:00:00"' -------------- next part -------------- HTML����������������������������...ダウンロード