void test_equal(void) { grn_obj *v; prepare_data(); cut_assert_not_null((cond = grn_expr_create(&context, NULL, 0))); v = grn_expr_add_var(&context, cond, NULL, 0); GRN_RECORD_INIT(v, 0, grn_obj_id(&context, docs)); grn_expr_append_obj(&context, cond, v, GRN_OP_PUSH, 1); GRN_TEXT_SETS(&context, &textbuf, "body"); grn_expr_append_const(&context, cond, &textbuf, GRN_OP_PUSH, 1); grn_expr_append_op(&context, cond, GRN_OP_GET_VALUE, 2); GRN_TEXT_SETS(&context, &textbuf, "poyo moge hoge moge moge moge"); grn_expr_append_const(&context, cond, &textbuf, GRN_OP_PUSH, 1); grn_expr_append_op(&context, cond, GRN_OP_EQUAL, 2); grn_expr_compile(&context, cond); res = grn_table_create(&context, NULL, 0, NULL, GRN_TABLE_HASH_KEY|GRN_OBJ_WITH_SUBREC, docs, NULL); cut_assert_not_null(res); cut_assert_not_null(grn_table_select(&context, docs, cond, res, GRN_OP_OR)); grn_test_assert_select(&context, gcut_take_new_list_string("poyo moge hoge " "moge moge moge", NULL), res, "body"); }
void test_table_scan(void) { grn_obj *cond, *v, *res, textbuf, intbuf; GRN_TEXT_INIT(&textbuf, 0); GRN_UINT32_INIT(&intbuf, 0); prepare_data(&textbuf, &intbuf); cut_assert_not_null((cond = grn_expr_create(&context, NULL, 0))); v = grn_expr_add_var(&context, cond, NULL, 0); GRN_RECORD_INIT(v, 0, grn_obj_id(&context, docs)); grn_expr_append_obj(&context, cond, v); GRN_TEXT_SETS(&context, &textbuf, "size"); grn_expr_append_const(&context, cond, &textbuf); grn_expr_append_op(&context, cond, GRN_OP_OBJ_GET_VALUE, 2); GRN_UINT32_SET(&context, &intbuf, 14); grn_expr_append_const(&context, cond, &intbuf); grn_expr_append_op(&context, cond, GRN_OP_EQUAL, 2); grn_expr_compile(&context, cond); res = grn_table_create(&context, NULL, 0, NULL, GRN_TABLE_HASH_KEY|GRN_OBJ_WITH_SUBREC, docs, 0); cut_assert_not_null(res); grn_test_assert(grn_table_scan(&context, docs, cond, res, GRN_SEL_OR)); cut_assert_equal_uint(3, grn_table_size(&context, res)); grn_test_assert(grn_obj_close(&context, res)); grn_test_assert(grn_obj_close(&context, cond)); grn_test_assert(grn_obj_close(&context, &textbuf)); grn_test_assert(grn_obj_close(&context, &intbuf)); }
void test_match_without_index(void) { grn_obj *v; create_properties_table(); create_documents_table(); insert_data(); cut_assert_not_null((cond = grn_expr_create(&context, NULL, 0))); v = grn_expr_add_var(&context, cond, NULL, 0); GRN_RECORD_INIT(v, 0, grn_obj_id(&context, docs)); grn_expr_append_obj(&context, cond, v, GRN_OP_PUSH, 1); GRN_TEXT_SETS(&context, &text_buf, "body"); grn_expr_append_const(&context, cond, &text_buf, GRN_OP_PUSH, 1); grn_expr_append_op(&context, cond, GRN_OP_GET_VALUE, 1); GRN_TEXT_SETS(&context, &text_buf, "moge"); grn_expr_append_const(&context, cond, &text_buf, GRN_OP_PUSH, 1); grn_expr_append_op(&context, cond, GRN_OP_MATCH, 2); grn_expr_compile(&context, cond); res = grn_table_create(&context, NULL, 0, NULL, GRN_TABLE_HASH_KEY|GRN_OBJ_WITH_SUBREC, docs, NULL); cut_assert_not_null(res); cut_assert_not_null(grn_table_select(&context, docs, cond, res, GRN_OP_OR)); grn_test_assert_select( &context, gcut_take_new_list_string("moge moge moge", "hoge moge moge moge", "moge hoge hoge", "moge hoge fuga fuga", "moge hoge moge moge moge", "poyo moge hoge moge moge moge", NULL), res, "body"); }
void test_mrn_hash_get() { const char *key = "mroonga"; const char *value = "A storage engine based on groonga."; grn_obj *result; GRN_TEXT_SETS(ctx, &buffer, value); GRN_TEXT_PUT(ctx, &buffer, "\0", 1); mrn_hash_put(ctx, hash, key, &buffer); cut_assert_equal_int(0, mrn_hash_get(ctx, hash, key, &result)); cut_assert_equal_string(value, GRN_TEXT_VALUE(&buffer)); }
void test_set_value(void) { grn_obj *v; prepare_data(); GRN_EXPR_CREATE_FOR_QUERY(&context, docs, cond, v); cut_assert_not_null(cond); cut_assert_not_null(v); PARSE(cond, "size:14", GRN_EXPR_SYNTAX_QUERY|GRN_EXPR_ALLOW_PRAGMA|GRN_EXPR_ALLOW_COLUMN); res = grn_table_select(&context, docs, cond, NULL, GRN_OP_OR); cut_assert_not_null(res); grn_test_assert_select(&context, gcut_take_new_list_string("moge moge moge", "hoge fuga fuga", "moge hoge hoge", NULL), res, "body"); grn_test_assert(grn_obj_close(&context, res)); res = NULL; GRN_EXPR_CREATE_FOR_QUERY(&context, docs, expr, v); grn_expr_append_obj(&context, expr, v, GRN_OP_PUSH, 1); GRN_TEXT_SETS(&context, &textbuf, "size"); grn_expr_append_const(&context, expr, &textbuf, GRN_OP_PUSH, 1); grn_expr_append_op(&context, expr, GRN_OP_GET_VALUE, 2); GRN_UINT32_SET(&context, &intbuf, 14); grn_expr_append_const(&context, expr, &intbuf, GRN_OP_PUSH, 1); grn_expr_append_op(&context, expr, GRN_OP_ASSIGN, 2); { grn_id id; grn_table_cursor *tc; tc = grn_table_cursor_open(&context, docs, NULL, 0, NULL, 0, 0, -1, 0); cut_assert_not_null(tc); while ((id = grn_table_cursor_next(&context, tc))) { GRN_RECORD_SET(&context, v, id); grn_expr_exec(&context, expr, 0); } grn_test_assert(grn_table_cursor_close(&context, tc)); } res = grn_table_select(&context, docs, cond, NULL, GRN_OP_OR); cut_assert_not_null(res); grn_test_assert_select_all(res); }
static void bench_setup_points(gpointer user_data, const gchar *start_point_string, const gchar *end_point_string, grn_builtin_type wgs84_or_tgs) { BenchmarkData *data = user_data; grn_obj start_point_text, end_point_text; GRN_TEXT_INIT(&start_point_text, 0); GRN_TEXT_INIT(&end_point_text, 0); GRN_TEXT_SETS(data->context, &start_point_text, start_point_string); GRN_TEXT_SETS(data->context, &end_point_text, end_point_string); data->start_point = grn_obj_open(data->context, GRN_BULK, 0, wgs84_or_tgs); data->end_point = grn_obj_open(data->context, GRN_BULK, 0, wgs84_or_tgs); grn_obj_cast(data->context, &start_point_text, data->start_point, GRN_FALSE); grn_obj_cast(data->context, &end_point_text, data->end_point, GRN_FALSE); grn_ctx_push(data->context, data->start_point); grn_ctx_push(data->context, data->end_point); grn_obj_unlink(data->context, &start_point_text); grn_obj_unlink(data->context, &end_point_text); }
void test_select(void) { grn_obj *v; prepare_data(); cut_assert_not_null((cond = grn_expr_create(&context, NULL, 0))); v = grn_expr_add_var(&context, cond, NULL, 0); GRN_RECORD_INIT(v, 0, grn_obj_id(&context, docs)); grn_expr_append_obj(&context, cond, v, GRN_OP_PUSH, 1); GRN_TEXT_SETS(&context, &text_buf, "size"); grn_expr_append_const(&context, cond, &text_buf, GRN_OP_PUSH, 1); grn_expr_append_op(&context, cond, GRN_OP_GET_VALUE, 2); GRN_UINT32_SET(&context, &int_buf, 14); grn_expr_append_const(&context, cond, &int_buf, GRN_OP_PUSH, 1); grn_expr_append_op(&context, cond, GRN_OP_EQUAL, 2); grn_expr_compile(&context, cond); res = grn_table_create(&context, NULL, 0, NULL, GRN_TABLE_HASH_KEY|GRN_OBJ_WITH_SUBREC, docs, NULL); cut_assert_not_null(res); cut_assert_not_null((expr = grn_expr_create(&context, NULL, 0))); grn_expr_append_obj(&context, expr, docs, GRN_OP_PUSH, 1); grn_expr_append_obj(&context, expr, cond, GRN_OP_PUSH, 1); grn_expr_append_obj(&context, expr, res, GRN_OP_PUSH, 1); GRN_UINT32_SET(&context, &int_buf, GRN_OP_OR); grn_expr_append_const(&context, expr, &int_buf, GRN_OP_PUSH, 1); grn_expr_append_op(&context, expr, GRN_OP_TABLE_SELECT, 4); grn_expr_exec(&context, expr, 0); grn_test_assert_select(&context, gcut_take_new_list_string("moge moge moge", "hoge fuga fuga", "moge hoge hoge", NULL), res, "body"); }
static void construct_object(gconstpointer data, grn_builtin_type type, grn_obj *object) { switch (type) { case GRN_DB_VOID: GRN_VOID_INIT(object); break; case GRN_DB_BOOL: GRN_BOOL_INIT(object, 0); GRN_BOOL_SET(&context, object, gcut_data_get_boolean(data, "value")); break; case GRN_DB_INT8: GRN_INT8_INIT(object, 0); GRN_INT8_SET(&context, object, gcut_data_get_int(data, "value")); break; case GRN_DB_UINT8: GRN_UINT8_INIT(object, 0); GRN_UINT8_SET(&context, object, gcut_data_get_uint(data, "value")); break; case GRN_DB_INT16: GRN_INT16_INIT(object, 0); GRN_INT16_SET(&context, object, gcut_data_get_int(data, "value")); break; case GRN_DB_UINT16: GRN_UINT16_INIT(object, 0); GRN_UINT16_SET(&context, object, gcut_data_get_uint(data, "value")); break; case GRN_DB_INT32: GRN_INT32_INIT(object, 0); GRN_INT32_SET(&context, object, gcut_data_get_int(data, "value")); break; case GRN_DB_UINT32: GRN_UINT32_INIT(object, 0); GRN_UINT32_SET(&context, object, gcut_data_get_uint(data, "value")); break; case GRN_DB_INT64: GRN_INT64_INIT(object, 0); GRN_INT64_SET(&context, object, gcut_data_get_int64(data, "value")); break; case GRN_DB_UINT64: GRN_UINT64_INIT(object, 0); GRN_UINT64_SET(&context, object, gcut_data_get_uint64(data, "value")); break; case GRN_DB_FLOAT: GRN_FLOAT_INIT(object, 0); GRN_FLOAT_SET(&context, object, gcut_data_get_double(data, "value")); break; case GRN_DB_TIME: GRN_TIME_INIT(object, 0); GRN_TIME_SET(&context, object, gcut_data_get_int64(data, "value")); break; case GRN_DB_SHORT_TEXT: GRN_SHORT_TEXT_INIT(object, 0); GRN_TEXT_SETS(&context, object, gcut_data_get_string(data, "value")); break; case GRN_DB_TEXT: GRN_TEXT_INIT(object, 0); GRN_TEXT_SETS(&context, object, gcut_data_get_string(data, "value")); break; case GRN_DB_LONG_TEXT: GRN_LONG_TEXT_INIT(object, 0); GRN_TEXT_SETS(&context, object, gcut_data_get_string(data, "value")); break; case GRN_DB_TOKYO_GEO_POINT: GRN_TOKYO_GEO_POINT_INIT(object, 0); GRN_GEO_POINT_SET(&context, object, gcut_data_get_int(data, "latitude"), gcut_data_get_int(data, "longitude")); break; case GRN_DB_WGS84_GEO_POINT: GRN_WGS84_GEO_POINT_INIT(object, 0); GRN_GEO_POINT_SET(&context, object, gcut_data_get_int(data, "latitude"), gcut_data_get_int(data, "longitude")); break; default: cut_fail("unknown type: %d", type); break; } }
void test_select_search(void) { grn_obj *v; prepare_data(); cut_assert_not_null((cond = grn_expr_create(&context, NULL, 0))); v = grn_expr_add_var(&context, cond, NULL, 0); GRN_RECORD_INIT(v, 0, grn_obj_id(&context, docs)); grn_expr_append_obj(&context, cond, v, GRN_OP_PUSH, 1); GRN_TEXT_SETS(&context, &text_buf, "size"); grn_expr_append_const(&context, cond, &text_buf, GRN_OP_PUSH, 1); grn_expr_append_op(&context, cond, GRN_OP_GET_VALUE, 2); GRN_UINT32_SET(&context, &int_buf, 14); grn_expr_append_const(&context, cond, &int_buf, GRN_OP_PUSH, 1); grn_expr_append_op(&context, cond, GRN_OP_EQUAL, 2); grn_expr_compile(&context, cond); cut_assert_not_null((expr = grn_expr_create(&context, NULL, 0))); v = grn_expr_add_var(&context, expr, NULL, 0); grn_expr_append_obj(&context, expr, v, GRN_OP_PUSH, 1); GRN_BULK_REWIND(&text_buf); grn_expr_append_const(&context, expr, &text_buf, GRN_OP_PUSH, 1); GRN_UINT32_SET(&context, &int_buf, GRN_TABLE_HASH_KEY|GRN_OBJ_WITH_SUBREC); grn_expr_append_const(&context, expr, &int_buf, GRN_OP_PUSH, 1); grn_expr_append_obj(&context, expr, docs, GRN_OP_PUSH, 1); GRN_PTR_SET(&context, &ptr_buf, NULL); grn_expr_append_obj(&context, expr, &ptr_buf, GRN_OP_PUSH, 1); grn_expr_append_op(&context, expr, GRN_OP_TABLE_CREATE, 4); grn_expr_append_op(&context, expr, GRN_OP_ASSIGN, 2); grn_expr_append_obj(&context, expr, docs, GRN_OP_PUSH, 1); grn_expr_append_obj(&context, expr, cond, GRN_OP_PUSH, 1); grn_expr_append_obj(&context, expr, v, GRN_OP_PUSH, 1); GRN_UINT32_SET(&context, &int_buf, GRN_OP_OR); grn_expr_append_const(&context, expr, &int_buf, GRN_OP_PUSH, 1); grn_expr_append_op(&context, expr, GRN_OP_TABLE_SELECT, 4); grn_expr_append_obj(&context, expr, index_body, GRN_OP_PUSH, 1); GRN_TEXT_SETS(&context, &text_buf, "moge"); grn_expr_append_const(&context, expr, &text_buf, GRN_OP_PUSH, 1); grn_expr_append_obj(&context, expr, v, GRN_OP_PUSH, 1); GRN_UINT32_SET(&context, &int_buf, GRN_OP_AND); grn_expr_append_const(&context, expr, &int_buf, GRN_OP_PUSH, 1); grn_expr_append_op(&context, expr, GRN_OP_OBJ_SEARCH, 4); grn_expr_append_obj(&context, expr, v, GRN_OP_PUSH, 1); GRN_TEXT_SETS(&context, &text_buf, ".size ._score .body"); grn_expr_append_const(&context, expr, &text_buf, GRN_OP_PUSH, 1); GRN_BULK_REWIND(&text_buf); grn_expr_append_obj(&context, expr, &text_buf, GRN_OP_PUSH, 1); grn_expr_append_op(&context, expr, GRN_OP_JSON_PUT, 3); grn_expr_exec(&context, expr, 0); cut_assert_equal_substring("[[2],[14,4,\"moge moge moge\"],[14,2,\"moge hoge hoge\"]]", GRN_TEXT_VALUE(&text_buf), GRN_TEXT_LEN(&text_buf)); }
void test_scan_search(void) { grn_obj *cond, *expr, *v, textbuf, intbuf; GRN_TEXT_INIT(&textbuf, 0); GRN_UINT32_INIT(&intbuf, 0); prepare_data(&textbuf, &intbuf); cut_assert_not_null((cond = grn_expr_create(&context, NULL, 0))); v = grn_expr_add_var(&context, cond, NULL, 0); GRN_RECORD_INIT(v, 0, grn_obj_id(&context, docs)); grn_expr_append_obj(&context, cond, v); GRN_TEXT_SETS(&context, &textbuf, "size"); grn_expr_append_const(&context, cond, &textbuf); grn_expr_append_op(&context, cond, GRN_OP_OBJ_GET_VALUE, 2); GRN_UINT32_SET(&context, &intbuf, 14); grn_expr_append_const(&context, cond, &intbuf); grn_expr_append_op(&context, cond, GRN_OP_EQUAL, 2); grn_expr_compile(&context, cond); cut_assert_not_null((expr = grn_expr_create(&context, NULL, 0))); v = grn_expr_add_var(&context, expr, NULL, 0); GRN_BULK_REWIND(&textbuf); grn_expr_append_const(&context, expr, &textbuf); GRN_UINT32_SET(&context, &intbuf, GRN_TABLE_HASH_KEY|GRN_OBJ_WITH_SUBREC); grn_expr_append_const(&context, expr, &intbuf); grn_expr_append_obj(&context, expr, docs); GRN_UINT32_SET(&context, &intbuf, 0); grn_expr_append_const(&context, expr, &intbuf); grn_expr_append_op(&context, expr, GRN_OP_TABLE_CREATE, 4); grn_expr_append_obj(&context, expr, v); grn_expr_append_op(&context, expr, GRN_OP_VAR_SET_VALUE, 2); grn_expr_append_obj(&context, expr, docs); grn_expr_append_obj(&context, expr, cond); grn_expr_append_obj(&context, expr, v); GRN_UINT32_SET(&context, &intbuf, GRN_SEL_OR); grn_expr_append_const(&context, expr, &intbuf); grn_expr_append_op(&context, expr, GRN_OP_TABLE_SCAN, 4); grn_expr_append_obj(&context, expr, index_body); GRN_TEXT_SETS(&context, &textbuf, "moge"); grn_expr_append_const(&context, expr, &textbuf); grn_expr_append_obj(&context, expr, v); GRN_UINT32_SET(&context, &intbuf, GRN_SEL_AND); grn_expr_append_const(&context, expr, &intbuf); grn_expr_append_op(&context, expr, GRN_OP_OBJ_SEARCH, 4); grn_expr_append_obj(&context, expr, v); GRN_TEXT_SETS(&context, &textbuf, ".size .:score .body"); grn_expr_append_const(&context, expr, &textbuf); GRN_BULK_REWIND(&textbuf); grn_expr_append_obj(&context, expr, &textbuf); grn_expr_append_op(&context, expr, GRN_OP_JSON_PUT, 3); grn_expr_exec(&context, expr); cut_assert_equal_substring("[[14, 4, \"moge moge moge\"], [14, 2, \"moge hoge hoge\"]]", GRN_TEXT_VALUE(&textbuf), GRN_TEXT_LEN(&textbuf)); grn_test_assert(grn_obj_close(&context, expr)); grn_test_assert(grn_obj_close(&context, cond)); grn_test_assert(grn_obj_close(&context, &textbuf)); grn_test_assert(grn_obj_close(&context, &intbuf)); }
void test_expr_query(void) { grn_obj *t1, *c1, *lc, *ft, *v, *expr; grn_obj textbuf, intbuf; grn_id r1, r2, r3, r4; /* actual table */ t1 = grn_table_create(&context, "t1", 2, NULL, GRN_OBJ_TABLE_NO_KEY|GRN_OBJ_PERSISTENT, NULL, 0); cut_assert_not_null(t1); /* lexicon table */ lc = grn_table_create(&context, "lc", 2, NULL, GRN_OBJ_TABLE_PAT_KEY|GRN_OBJ_PERSISTENT, grn_ctx_at(&context, GRN_DB_SHORTTEXT), 0); cut_assert_not_null(lc); grn_test_assert(grn_obj_set_info(&context, lc, GRN_INFO_DEFAULT_TOKENIZER, grn_ctx_at(&context, GRN_DB_BIGRAM))); /* actual column */ c1 = grn_column_create(&context, t1, "c1", 2, NULL, GRN_OBJ_COLUMN_SCALAR|GRN_OBJ_PERSISTENT, grn_ctx_at(&context, GRN_DB_TEXT)); cut_assert_not_null(c1); /* fulltext index */ ft = grn_column_create(&context, lc, "ft", 2, NULL, GRN_OBJ_COLUMN_INDEX|GRN_OBJ_PERSISTENT|GRN_OBJ_WITH_POSITION, t1); cut_assert_not_null(ft); GRN_TEXT_INIT(&textbuf, 0); GRN_UINT32_INIT(&intbuf, 0); /* link between actual column and fulltext index */ GRN_UINT32_SET(&context, &intbuf, grn_obj_id(&context, c1)); grn_obj_set_info(&context, ft, GRN_INFO_SOURCE, &intbuf); /* need to use grn_id */ /* insert row */ r1 = grn_table_add(&context, t1, NULL, 0, NULL); cut_assert_equal_int(1, r1); GRN_TEXT_SETS(&context, &textbuf, "abhij"); grn_test_assert(grn_obj_set_value(&context, c1, r1, &textbuf, GRN_OBJ_SET)); r2 = grn_table_add(&context, t1, NULL, 0, NULL); cut_assert_equal_int(2, r2); GRN_TEXT_SETS(&context, &textbuf, "fghij"); grn_test_assert(grn_obj_set_value(&context, c1, r2, &textbuf, GRN_OBJ_SET)); r3 = grn_table_add(&context, t1, NULL, 0, NULL); cut_assert_equal_int(3, r3); GRN_TEXT_SETS(&context, &textbuf, "11 22 33"); grn_test_assert(grn_obj_set_value(&context, c1, r3, &textbuf, GRN_OBJ_SET)); r4 = grn_table_add(&context, t1, NULL, 0, NULL); cut_assert_equal_int(4, r4); GRN_TEXT_SETS(&context, &textbuf, "44 22 55"); grn_test_assert(grn_obj_set_value(&context, c1, r4, &textbuf, GRN_OBJ_SET)); /* confirm record are inserted in both column and index */ cut_assert_equal_int(4, grn_table_size(&context, t1)); cut_assert_equal_int(17, grn_table_size(&context, lc)); cut_assert_not_null((expr = grn_expr_create(&context, NULL, 0))); v = grn_expr_add_var(&context, expr, NULL, 0); GRN_BULK_REWIND(&textbuf); grn_expr_append_const(&context, expr, &textbuf); GRN_UINT32_SET(&context, &intbuf, GRN_TABLE_HASH_KEY|GRN_OBJ_WITH_SUBREC); grn_expr_append_const(&context, expr, &intbuf); grn_expr_append_obj(&context, expr, t1); GRN_UINT32_SET(&context, &intbuf, 0); grn_expr_append_const(&context, expr, &intbuf); grn_expr_append_op(&context, expr, GRN_OP_TABLE_CREATE, 4); grn_expr_append_obj(&context, expr, v); grn_expr_append_op(&context, expr, GRN_OP_VAR_SET_VALUE, 2); grn_expr_append_obj(&context, expr, ft); GRN_TEXT_SETS(&context, &textbuf, "hij"); grn_expr_append_const(&context, expr, &textbuf); grn_expr_append_obj(&context, expr, v); GRN_UINT32_SET(&context, &intbuf, GRN_SEL_OR); grn_expr_append_const(&context, expr, &intbuf); grn_expr_append_op(&context, expr, GRN_OP_OBJ_SEARCH, 4); grn_expr_append_obj(&context, expr, v); GRN_TEXT_SETS(&context, &textbuf, ".c1 .:score"); grn_expr_append_const(&context, expr, &textbuf); GRN_BULK_REWIND(&textbuf); grn_expr_append_obj(&context, expr, &textbuf); grn_expr_append_op(&context, expr, GRN_OP_JSON_PUT, 3); grn_expr_compile(&context, expr); grn_expr_exec(&context, expr); cut_assert_equal_uint(0, grn_obj_close(&context, expr)); cut_assert_equal_substring("[[\"abhij\", 1], [\"fghij\", 1]]", GRN_TEXT_VALUE(&textbuf), GRN_TEXT_LEN(&textbuf)); grn_obj_close(&context, &textbuf); grn_obj_close(&context, ft); grn_obj_close(&context, c1); grn_obj_close(&context, lc); grn_obj_close(&context, t1); }
void test_persistent_expr(void) { int i; grn_obj *t1, *t2, *c1, *c2, buf; t1 = grn_table_create(&context, "t1", 2, NULL, GRN_OBJ_TABLE_NO_KEY|GRN_OBJ_PERSISTENT, NULL, 0); cut_assert_not_null(t1); t2 = grn_table_create(&context, "t2", 2, NULL, GRN_OBJ_TABLE_NO_KEY|GRN_OBJ_PERSISTENT, NULL, 0); cut_assert_not_null(t2); c1 = grn_column_create(&context, t1, "c1", 2, NULL, GRN_OBJ_PERSISTENT, t2); cut_assert_not_null(c1); c2 = grn_column_create(&context, t2, "c2", 2, NULL, GRN_OBJ_PERSISTENT, t1); cut_assert_not_null(c2); GRN_TEXT_INIT(&buf, 0); for (i = 0; i < NRECORDS; i++) { grn_id i1, i2; i1 = grn_table_add(&context, t1, NULL, 0, NULL); i2 = grn_table_add(&context, t2, NULL, 0, NULL); GRN_BULK_REWIND(&buf); grn_bulk_write(&context, &buf, (char *)&i2, sizeof(grn_id)); grn_obj_set_value(&context, c1, i1, &buf, GRN_OBJ_SET); grn_obj_set_value(&context, c2, i2, &buf, GRN_OBJ_SET); } { grn_obj *expr = grn_expr_create(&context, "test", 4); grn_obj *v; cut_assert_not_null(expr); v = grn_expr_add_var(&context, expr, "foo", 3); GRN_RECORD_INIT(v, 0, grn_obj_id(&context, t1)); grn_expr_append_obj(&context, expr, v); GRN_TEXT_SETS(&context, &buf, "c1"); grn_expr_append_const(&context, expr, &buf); grn_expr_append_op(&context, expr, GRN_OP_OBJ_GET_VALUE, 2); GRN_TEXT_SETS(&context, &buf, "c2"); grn_expr_append_const(&context, expr, &buf); grn_expr_append_op(&context, expr, GRN_OP_OBJ_GET_VALUE, 2); GRN_TEXT_SETS(&context, &buf, "c1"); grn_expr_append_const(&context, expr, &buf); /* GRN_TEXT_SETS(&context, &buf, "c1.c2.c1"); grn_expr_append_const(&context, expr, &buf); */ grn_expr_append_op(&context, expr, GRN_OP_OBJ_GET_VALUE, 2); grn_expr_compile(&context, expr); } cut_assert_equal_uint(0, grn_obj_close(&context, &buf)); grn_db_close(&context, database); database = grn_db_open(&context, path); GRN_TEXT_INIT(&buf, 0); { grn_id id; uint64_t et; int nerr = 0; grn_obj *r, *v; grn_table_cursor *tc; struct timeval tvb, tve; grn_obj *expr = grn_ctx_get(&context, "test", 4); v = grn_expr_get_var(&context, expr, "foo", 3); t1 = grn_ctx_get(&context, "t1", 2); tc = grn_table_cursor_open(&context, t1, NULL, 0, NULL, 0, 0); cut_assert_not_null(tc); gettimeofday(&tvb, NULL); while ((id = grn_table_cursor_next(&context, tc))) { GRN_RECORD_SET(&context, v, id); r = grn_expr_exec(&context, expr); if (GRN_RECORD_VALUE(r) != id) { nerr++; } } gettimeofday(&tve, NULL); et = (tve.tv_sec - tvb.tv_sec) * 1000000 + (tve.tv_usec - tvb.tv_usec); // printf("et=%zu\n", et); cut_assert_equal_uint(0, nerr); cut_assert_equal_uint(0, grn_table_cursor_close(&context, tc)); } cut_assert_equal_uint(0, grn_obj_close(&context, &buf)); }
void test_near_geo_point(gpointer data) { grn_id id; int offset, limit; const GList *expected_keys; GList *actual_keys = NULL; grn_table_sort_key keys[2]; grn_obj base, base_string, location; create_geo_table(cut_take_printf(" [\"%s\"],\n" " [\"%s\"],\n" " [\"%s\"],\n" " [\"%s\"],\n" " [\"%s\"],\n" " [\"%s\"],\n" " [\"%s\"],\n" " [\"%s\"],\n" " [\"%s\"],\n" " [\"%s\"],\n" " [\"%s\"],\n" " [\"%s\"],\n" " [\"%s\"],\n" " [\"%s\"],\n" " [\"%s\"]", TAKEN_POINT(0, 0, 0, 180, 0, 0), TAKEN_POINT(0, 0, 0, -179, -59, -59), TAKEN_POINT(-1, -1, -1, 180, 0, 0), TAKEN_POINT(2, 1, 1, 180, 0, 0), TAKEN_POINT(-2, -1, -1, -179, -59, -59), TAKEN_POINT(1, 2, 1, -179, -59, -59), TAKEN_POINT(90, 0, 0, 0, 0, 0), TAKEN_POINT(-90, 0, 0, 1, 0, 0), TAKEN_POINT(1, 0, 0, 1, 0, 0), TAKEN_POINT(1, 1, 0, 1, 1, 0), TAKEN_POINT(1, 1, 1, 1, 1, 1), TAKEN_POINT(-1, 0, 0, 1, 1, 1), TAKEN_POINT(-1, -1, -1, 0, 0, 0), TAKEN_POINT(-1, -2, -1, -1, -1, -1), TAKEN_POINT(1, 1, 10, -1, -1, -1))); result = grn_table_create(context, NULL, 0, NULL, GRN_TABLE_NO_KEY, NULL, table); grn_test_assert_context(context); GRN_TEXT_INIT(&base_string, 0); GRN_TEXT_SETS(context, &base_string, gcut_data_get_string(data, "base")); GRN_WGS84_GEO_POINT_INIT(&base, 0); grn_obj_cast(context, &base_string, &base, FALSE); GRN_OBJ_FIN(context, &base_string); offset = gcut_data_get_int(data, "offset"); if (offset > 0) { cut_omit("geo sort doesn't support offset yet."); } limit = gcut_data_get_int(data, "limit"); keys[0].key = column; keys[0].flags = GRN_TABLE_SORT_GEO; keys[0].offset = 0; keys[1].key = &base; keys[1].flags = 0; keys[1].offset = 0; grn_table_sort(context, table, offset, limit, result, keys, 2); GRN_OBJ_FIN(context, &base); grn_test_assert_context(context); cursor = grn_table_cursor_open(context, result, NULL, 0, NULL, 0, 0, -1, GRN_CURSOR_ASCENDING); grn_test_assert_context(context); GRN_WGS84_GEO_POINT_INIT(&location, 0); while ((id = grn_table_cursor_next(context, cursor))) { gint32 *key; int key_size; gint latitude, longitude; key_size = grn_table_cursor_get_value(context, cursor, (void **)&key); GRN_BULK_REWIND(&location); grn_obj_get_value(context, column, *key, &location); GRN_GEO_POINT_VALUE(&location, latitude, longitude); actual_keys = g_list_append(actual_keys, inspect_point(latitude, longitude)); } GRN_OBJ_FIN(context, &location); gcut_take_list(actual_keys, g_free); expected_keys = gcut_data_get_pointer(data, "expected"); gcut_assert_equal_list_string(expected_keys, actual_keys); }
void test_expr(void) { int i; grn_obj *t1, *t2, *c1, *c2, r1, r2, buf; t1 = grn_table_create(context, "t1", 2, NULL, GRN_OBJ_TABLE_NO_KEY|GRN_OBJ_PERSISTENT, NULL, NULL); cut_assert_not_null(t1); t2 = grn_table_create(context, "t2", 2, NULL, GRN_OBJ_TABLE_NO_KEY|GRN_OBJ_PERSISTENT, NULL, NULL); cut_assert_not_null(t2); c1 = grn_column_create(context, t1, "c1", 2, NULL, GRN_OBJ_PERSISTENT, t2); cut_assert_not_null(c1); c2 = grn_column_create(context, t2, "c2", 2, NULL, GRN_OBJ_PERSISTENT, t1); cut_assert_not_null(c2); GRN_TEXT_INIT(&buf, 0); GRN_RECORD_INIT(&r1, 0, grn_obj_id(context, t1)); GRN_RECORD_INIT(&r2, 0, grn_obj_id(context, t2)); for (i = 0; i < NRECORDS; i++) { grn_id i1, i2; i1 = grn_table_add(context, t1, NULL, 0, NULL); i2 = grn_table_add(context, t2, NULL, 0, NULL); GRN_RECORD_SET(context, &r1, i1); GRN_RECORD_SET(context, &r2, i2); grn_obj_set_value(context, c1, i1, &r2, GRN_OBJ_SET); grn_obj_set_value(context, c2, i2, &r1, GRN_OBJ_SET); } { grn_obj *r, *v; expr = grn_expr_create(context, NULL, 0); cut_assert_not_null(expr); v = grn_expr_add_var(context, expr, NULL, 0); GRN_RECORD_INIT(v, 0, grn_obj_id(context, t1)); grn_expr_append_obj(context, expr, v, GRN_OP_PUSH, 1); GRN_TEXT_SETS(context, &buf, "c1"); grn_expr_append_const(context, expr, &buf, GRN_OP_PUSH, 1); grn_expr_append_op(context, expr, GRN_OP_GET_VALUE, 2); GRN_TEXT_SETS(context, &buf, "c2"); grn_expr_append_const(context, expr, &buf, GRN_OP_PUSH, 1); grn_expr_append_op(context, expr, GRN_OP_GET_VALUE, 2); GRN_TEXT_SETS(context, &buf, "c1"); grn_expr_append_const(context, expr, &buf, GRN_OP_PUSH, 1); // GRN_TEXT_SETS(context, &buf, "c1.c2.c1"); // grn_expr_append_const(context, expr, &buf); grn_expr_append_op(context, expr, GRN_OP_GET_VALUE, 2); grn_expr_compile(context, expr); { grn_id id; uint64_t et; int nerr = 0; grn_table_cursor *tc; struct timeval tvb, tve; tc = grn_table_cursor_open(context, t1, NULL, 0, NULL, 0, 0, -1, 0); cut_assert_not_null(tc); gettimeofday(&tvb, NULL); while ((id = grn_table_cursor_next(context, tc))) { GRN_RECORD_SET(context, v, id); grn_expr_exec(context, expr, 0); r = grn_ctx_pop(context); if (GRN_RECORD_VALUE(r) != id) { nerr++; } } gettimeofday(&tve, NULL); et = (tve.tv_sec - tvb.tv_sec) * 1000000 + (tve.tv_usec - tvb.tv_usec); // printf("et=%zu\n", et); cut_assert_equal_uint(0, nerr); grn_test_assert(grn_table_cursor_close(context, tc)); } } grn_test_assert(grn_obj_close(context, &r1)); grn_test_assert(grn_obj_close(context, &r2)); grn_test_assert(grn_obj_close(context, &buf)); }
void test_mroonga_index_score(void) { grn_obj *t1,*c1,*lc,*ft; grn_obj buff; grn_id r1,r2,r3,r4; remove_tmp_directory(); g_mkdir_with_parents(tmp_directory,0700); g_chdir(tmp_directory); g_mkdir_with_parents("mrn",0700); db = grn_db_create(context,"mroonga.grn",NULL); cut_assert_not_null(db); /* actual table */ t1 = grn_table_create(context,"t1",2,"mrn/t1.grn", GRN_OBJ_TABLE_NO_KEY|GRN_OBJ_PERSISTENT,NULL,0); cut_assert_not_null(t1); /* lexicon table */ lc = grn_table_create(context,"lc",2,"mrn/lc.grn", GRN_OBJ_TABLE_PAT_KEY|GRN_OBJ_PERSISTENT, grn_ctx_at(context, GRN_DB_SHORT_TEXT), 0); cut_assert_not_null(lc); grn_test_assert(grn_obj_set_info(context, lc, GRN_INFO_DEFAULT_TOKENIZER, grn_ctx_at(context, GRN_DB_BIGRAM))); /* actual column */ c1 = grn_column_create(context,t1,"c1",2,"mrn/t1.c1.grn", GRN_OBJ_COLUMN_SCALAR|GRN_OBJ_PERSISTENT, grn_ctx_at(context, GRN_DB_TEXT)); cut_assert_not_null(c1); /* fulltext index */ ft = grn_column_create(context,lc,"ft",2,"mrn/lc.ft.grn", GRN_OBJ_COLUMN_INDEX|GRN_OBJ_PERSISTENT,t1); cut_assert_not_null(ft); GRN_TEXT_INIT(&buff,0); /* link between actual column and fulltext index */ GRN_UINT32_SET(context, &buff, grn_obj_id(context, c1)); grn_obj_set_info(context, ft, GRN_INFO_SOURCE, &buff); /* need to use grn_id */ /* insert row */ r1 = grn_table_add(context, t1, NULL, 0, NULL); cut_assert_equal_int(1,r1); GRN_TEXT_SETS(context, &buff, "abcde"); grn_test_assert(grn_obj_set_value(context, c1, r1, &buff, GRN_OBJ_SET)); r2 = grn_table_add(context, t1, NULL, 0, NULL); cut_assert_equal_int(2,r2); GRN_TEXT_SETS(context, &buff, "fghij"); grn_test_assert(grn_obj_set_value(context, c1, r2, &buff, GRN_OBJ_SET)); r3 = grn_table_add(context, t1, NULL, 0, NULL); cut_assert_equal_int(3,r3); GRN_TEXT_SETS(context, &buff, "11 22 33"); grn_test_assert(grn_obj_set_value(context, c1, r3, &buff, GRN_OBJ_SET)); r4 = grn_table_add(context, t1, NULL, 0, NULL); cut_assert_equal_int(4,r4); GRN_TEXT_SETS(context, &buff, "44 22 55"); grn_test_assert(grn_obj_set_value(context, c1, r4, &buff, GRN_OBJ_SET)); /* confirm record are inserted in both column and index */ cut_assert_equal_int(4,grn_table_size(context,t1)); cut_assert_equal_int(23,grn_table_size(context,lc)); /* nlq search */ { grn_id id, docid; grn_obj *res; grn_table_cursor *tc; grn_obj score, *score_column; res = grn_table_create(context, NULL, 0, NULL, GRN_TABLE_HASH_KEY|GRN_OBJ_WITH_SUBREC, t1, 0); GRN_UINT32_INIT(&score, 0); GRN_BULK_REWIND(&buff); GRN_TEXT_SETS(context, &buff, "hij"); grn_obj_search(context, ft, &buff, res, GRN_OP_OR, NULL); cut_assert_equal_int(1, grn_table_size(context, res)); score_column = grn_obj_column(context, res, ".:score", 7); tc = grn_table_cursor_open(context, res, NULL, 0, NULL, 0, 0, 0, 0); while ((id = grn_table_cursor_next(context, tc))) { GRN_BULK_REWIND(&buff); grn_table_get_key(context, res, id, &docid, sizeof(grn_id)); cut_assert_equal_int(2, docid); cut_assert_not_null(grn_obj_get_value(context, c1, docid, &buff)); cut_assert_equal_int(5 ,GRN_TEXT_LEN(&buff)); cut_assert_equal_substring("fghij", (char*) GRN_BULK_HEAD(&buff),GRN_TEXT_LEN(&buff)); grn_obj_get_value(context, score_column, id, &score); cut_assert_equal_uint(1, GRN_UINT32_VALUE(&score)); } grn_table_cursor_close(context, tc); grn_obj_close(context, score_column); grn_obj_close(context, res); } /* boolean search */ { grn_id id, docid; grn_obj *res; grn_query *query; grn_table_cursor *tc; grn_obj score, *score_column; const char *qstr = "+22 -55"; res = grn_table_create(context, NULL, 0, NULL, GRN_TABLE_HASH_KEY|GRN_OBJ_WITH_SUBREC, t1, 0); GRN_UINT32_INIT(&score, 0); query = grn_query_open(context, qstr, strlen(qstr), GRN_OP_OR, 32); grn_obj_search(context, ft, (grn_obj*) query, res, GRN_OP_OR, NULL); cut_assert_equal_int(1, grn_table_size(context, res)); score_column = grn_obj_column(context, res, ".:score", 7); tc = grn_table_cursor_open(context, res, NULL, 0, NULL, 0, 0, 0, 0); while ((id = grn_table_cursor_next(context, tc))) { GRN_BULK_REWIND(&buff); grn_table_get_key(context, res, id, &docid, sizeof(grn_id)); cut_assert_equal_int(3, docid); cut_assert_not_null(grn_obj_get_value(context, c1, docid, &buff)); cut_assert_equal_int(8 ,GRN_TEXT_LEN(&buff)); cut_assert_equal_substring("11 22 33", (char*) GRN_BULK_HEAD(&buff),GRN_TEXT_LEN(&buff)); grn_obj_get_value(context, score_column, id, &score); cut_assert_equal_uint(5, GRN_UINT32_VALUE(&score)); } grn_query_close(context, query); grn_table_cursor_close(context ,tc); grn_obj_close(context, score_column); grn_obj_close(context, res); } grn_obj_close(context, &buff); grn_obj_close(context, ft); grn_obj_close(context, c1); grn_obj_close(context, lc); grn_obj_close(context, t1); }
void test_mroonga_index_score(void) { grn_obj *t1,*c1,*lc,*ft; grn_obj buff; grn_id r1,r2,r3,r4; const gchar *mrn_dir; mrn_dir = cut_build_path(tmp_directory, "mrn", NULL); g_mkdir_with_parents(mrn_dir, 0700); grn_obj_close(context, db); db = grn_db_create(context, cut_build_path(mrn_dir, "mroonga.grn", NULL), NULL); cut_assert_not_null(db); /* actual table */ t1 = grn_table_create(context, "t1", 2, cut_build_path(mrn_dir, "t1.grn", NULL), GRN_OBJ_TABLE_NO_KEY|GRN_OBJ_PERSISTENT, NULL, 0); cut_assert_not_null(t1); /* lexicon table */ lc = grn_table_create(context, "lc", 2, cut_build_path(mrn_dir, "lc.grn", NULL), GRN_OBJ_TABLE_PAT_KEY|GRN_OBJ_PERSISTENT, grn_ctx_at(context, GRN_DB_SHORT_TEXT), 0); cut_assert_not_null(lc); grn_test_assert(grn_obj_set_info(context, lc, GRN_INFO_DEFAULT_TOKENIZER, grn_ctx_at(context, GRN_DB_BIGRAM))); /* actual column */ c1 = grn_column_create(context, t1, "c1", 2, cut_build_path(mrn_dir, "t1.c1.grn", NULL), GRN_OBJ_COLUMN_SCALAR|GRN_OBJ_PERSISTENT, grn_ctx_at(context, GRN_DB_TEXT)); cut_assert_not_null(c1); /* fulltext index */ ft = grn_column_create(context, lc, "ft", 2, cut_build_path(mrn_dir, "lc.ft.grn", NULL), GRN_OBJ_COLUMN_INDEX|GRN_OBJ_PERSISTENT, t1); cut_assert_not_null(ft); GRN_TEXT_INIT(&buff,0); /* link between actual column and fulltext index */ GRN_UINT32_SET(context, &buff, grn_obj_id(context, c1)); grn_obj_set_info(context, ft, GRN_INFO_SOURCE, &buff); /* need to use grn_id */ /* insert row */ r1 = grn_table_add(context, t1, NULL, 0, NULL); cut_assert_equal_int(1,r1); GRN_TEXT_SETS(context, &buff, "abcde"); grn_test_assert(grn_obj_set_value(context, c1, r1, &buff, GRN_OBJ_SET)); r2 = grn_table_add(context, t1, NULL, 0, NULL); cut_assert_equal_int(2,r2); GRN_TEXT_SETS(context, &buff, "fghij"); grn_test_assert(grn_obj_set_value(context, c1, r2, &buff, GRN_OBJ_SET)); r3 = grn_table_add(context, t1, NULL, 0, NULL); cut_assert_equal_int(3,r3); GRN_TEXT_SETS(context, &buff, "11 22 33"); grn_test_assert(grn_obj_set_value(context, c1, r3, &buff, GRN_OBJ_SET)); r4 = grn_table_add(context, t1, NULL, 0, NULL); cut_assert_equal_int(4,r4); GRN_TEXT_SETS(context, &buff, "44 22 55"); grn_test_assert(grn_obj_set_value(context, c1, r4, &buff, GRN_OBJ_SET)); /* confirm record are inserted in both column and index */ cut_assert_equal_int(4,grn_table_size(context,t1)); cut_assert_equal_int(23,grn_table_size(context,lc)); /* nlq search */ { grn_id id, docid; grn_obj *res; grn_table_cursor *tc; grn_obj score, *score_column; res = grn_table_create(context, NULL, 0, NULL, GRN_TABLE_HASH_KEY|GRN_OBJ_WITH_SUBREC, t1, 0); GRN_FLOAT_INIT(&score, 0); GRN_BULK_REWIND(&buff); GRN_TEXT_SETS(context, &buff, "hij"); grn_obj_search(context, ft, &buff, res, GRN_OP_OR, NULL); cut_assert_equal_int(1, grn_table_size(context, res)); score_column = grn_obj_column(context, res, "_score", 6); tc = grn_table_cursor_open(context, res, NULL, 0, NULL, 0, 0, -1, 0); while ((id = grn_table_cursor_next(context, tc))) { GRN_BULK_REWIND(&buff); grn_table_get_key(context, res, id, &docid, sizeof(grn_id)); cut_assert_equal_int(2, docid); cut_assert_not_null(grn_obj_get_value(context, c1, docid, &buff)); cut_assert_equal_int(5 ,GRN_TEXT_LEN(&buff)); cut_assert_equal_substring("fghij", (char*) GRN_BULK_HEAD(&buff),GRN_TEXT_LEN(&buff)); grn_obj_get_value(context, score_column, id, &score); cut_assert_equal_double(1.0, DBL_EPSILON, GRN_FLOAT_VALUE(&score)); } grn_table_cursor_close(context, tc); grn_obj_close(context, score_column); grn_obj_close(context, res); } /* boolean search */ { grn_id id, docid; grn_obj *res; grn_obj *match_columns, *match_columns_variable; grn_obj *expression, *expression_variable; grn_table_cursor *tc; grn_obj score, *score_column; const char *match_columns_expression = "c1 * 5"; const char *qstr = "+22 -55"; GRN_EXPR_CREATE_FOR_QUERY(context, t1, match_columns, match_columns_variable); grn_expr_parse(context, match_columns, match_columns_expression, strlen(match_columns_expression), NULL, GRN_OP_MATCH, GRN_OP_AND, GRN_EXPR_SYNTAX_SCRIPT); GRN_EXPR_CREATE_FOR_QUERY(context, t1, expression, expression_variable); res = grn_table_create(context, NULL, 0, NULL, GRN_TABLE_HASH_KEY|GRN_OBJ_WITH_SUBREC, t1, 0); grn_test_assert(grn_expr_parse(context, expression, qstr, strlen(qstr), match_columns, GRN_OP_MATCH, GRN_OP_OR, GRN_EXPR_SYNTAX_QUERY)); grn_table_select(context, t1, expression, res, GRN_OP_OR); cut_assert_equal_int(1, grn_table_size(context, res)); GRN_FLOAT_INIT(&score, 0); score_column = grn_obj_column(context, res, "_score", 6); tc = grn_table_cursor_open(context, res, NULL, 0, NULL, 0, 0, -1, 0); while ((id = grn_table_cursor_next(context, tc))) { GRN_BULK_REWIND(&buff); grn_table_get_key(context, res, id, &docid, sizeof(grn_id)); cut_assert_equal_int(3, docid); cut_assert_not_null(grn_obj_get_value(context, c1, docid, &buff)); cut_assert_equal_int(8, GRN_TEXT_LEN(&buff)); cut_assert_equal_substring("11 22 33", (char*) GRN_BULK_HEAD(&buff),GRN_TEXT_LEN(&buff)); grn_obj_get_value(context, score_column, id, &score); cut_assert_equal_double(5, DBL_EPSILON, GRN_FLOAT_VALUE(&score)); } grn_obj_close(context, expression); grn_obj_close(context, match_columns); grn_table_cursor_close(context ,tc); grn_obj_close(context, score_column); grn_obj_close(context, res); } grn_obj_close(context, &buff); grn_obj_close(context, ft); grn_obj_close(context, c1); grn_obj_close(context, lc); grn_obj_close(context, t1); }