GList * grn_test_pat_cursor_get_pairs(grn_ctx *context, grn_table_cursor *cursor) { grn_id id; GList *pairs = NULL; id = grn_table_cursor_next(context, cursor); while (id != GRN_ID_NIL) { int length; void *key, *value; GString *null_terminated_key, *null_terminated_value; length = grn_table_cursor_get_key(context, cursor, &key); null_terminated_key = g_string_new_len(key, length); pairs = g_list_append(pairs, g_string_free(null_terminated_key, FALSE)); length = grn_table_cursor_get_value(context, cursor, &value); null_terminated_value = g_string_new_len(value, length); pairs = g_list_append(pairs, g_string_free(null_terminated_value, FALSE)); id = grn_table_cursor_next(context, cursor); } return pairs; }
/* * call-seq: * cursor.value -> 値 * * カレントレコードの値を返す。 */ static VALUE rb_grn_table_cursor_get_value (VALUE self) { grn_ctx *context; grn_table_cursor *cursor; VALUE rb_value = Qnil; rb_grn_table_cursor_deconstruct(SELF(self), &cursor, &context, NULL, NULL, NULL, NULL); if (context && cursor) { int n; void *value; n = grn_table_cursor_get_value(context, cursor, &value); rb_value = rb_str_new(value, n); } return rb_value; }
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_array_sort(gpointer data) { const gint32 values[] = { 5, 6, 18, 9, 0, 4, 13, 12, 8, 14, 19, 11, 7, 3, 1, 10, 15, 2, 17, 16 }; const int n_values = sizeof(values) / sizeof(values[0]); const gchar table_name[] = "Store"; const gchar column_name[] = "sample_column"; const int n_keys = 1; grn_table_sort_key keys[n_keys]; grn_obj *table, *column, *result; grn_table_cursor *cursor; int n_results; guint i; guint n_expected_values; GList *expected_values, *sorted_values = NULL; table = grn_table_create(context, table_name, strlen(table_name), NULL, GRN_OBJ_TABLE_NO_KEY | GRN_OBJ_PERSISTENT, NULL, NULL); column = grn_column_create(context, table, column_name, strlen(column_name), NULL, 0, get_object("Int32")); keys[0].key = column; keys[0].flags = GRN_TABLE_SORT_ASC; for(i = 0; i < n_values; ++i) { grn_obj record_value; grn_id record_id; record_id = grn_table_add(context, table, NULL, 0, NULL); GRN_INT32_INIT(&record_value, 0); GRN_INT32_SET(context, &record_value, values[i]); grn_test_assert(grn_obj_set_value(context, column, record_id, &record_value, GRN_OBJ_SET)); GRN_OBJ_FIN(context, &record_value); } cut_assert_equal_int(n_values, grn_table_size(context, table)); result = grn_table_create(context, NULL, 0, NULL, GRN_TABLE_NO_KEY, NULL, table); n_results = grn_table_sort(context, table, gcut_data_get_int(data, "offset"), gcut_data_get_int(data, "limit"), result, keys, n_keys); expected_values = (GList *)gcut_data_get_pointer(data, "expected_values"); n_expected_values = g_list_length(expected_values); cut_assert_equal_int(n_expected_values, n_results); cut_assert_equal_int(n_expected_values, grn_table_size(context, result)); cursor = grn_table_cursor_open(context, result, NULL, 0, NULL, 0, 0, -1, GRN_CURSOR_ASCENDING); while (grn_table_cursor_next(context, cursor) != GRN_ID_NIL) { void *value; grn_id *id; grn_obj record_value; grn_table_cursor_get_value(context, cursor, &value); id = value; GRN_INT32_INIT(&record_value, 0); grn_obj_get_value(context, column, *id, &record_value); sorted_values = g_list_append(sorted_values, GINT_TO_POINTER(GRN_INT32_VALUE(&record_value))); GRN_OBJ_FIN(context, &record_value); } gcut_take_list(sorted_values, NULL); gcut_assert_equal_list_int(expected_values, sorted_values); grn_table_cursor_close(context, cursor); grn_obj_close(context, result); }