Пример #1
0
const GList *
grn_test_table_collect_string(grn_ctx          *context,
                              grn_obj          *table,
                              const gchar      *text_column_name)
{
  GList *records = NULL;
  grn_table_cursor *cursor;
  grn_id id;
  grn_obj *text_column;
  grn_obj value;

  cursor = grn_table_cursor_open(context, table, NULL, 0, NULL, 0,
                                 0, -1, GRN_CURSOR_ASCENDING);
  grn_test_assert_context(context);
  text_column = grn_obj_column(context, table,
                               text_column_name, strlen(text_column_name));
  GRN_TEXT_INIT(&value, 0);
  while ((id = grn_table_cursor_next(context, cursor)) != GRN_ID_NIL) {
    GRN_BULK_REWIND(&value);
    grn_obj_get_value(context, text_column, id, &value);
    records = g_list_append(records, g_strndup(GRN_TEXT_VALUE(&value),
                                               GRN_TEXT_LEN(&value)));
  }
  grn_obj_unlink(context, &value);
  grn_obj_unlink(context, text_column);
  gcut_take_list(records, g_free);
  grn_test_assert(grn_table_cursor_close(context, cursor));
  grn_test_assert_context(context);

  return records;
}
Пример #2
0
void
test_unsequantial_records_in_table_with_keys(void)
{
  grn_obj *table;
  grn_id id, expected_id = 1;
  const gchar *keys[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
  int i, n_keys = sizeof(keys) / sizeof(keys[0]);

  table = table_create("Weekdays", GRN_OBJ_TABLE_HASH_KEY, "ShortText", NULL);
  grn_test_assert_context(context);

  for (i = 0; i < n_keys; ++i) {
    id = grn_table_add(context, table, keys[i], strlen(keys[i]), NULL);
    cut_assert_equal_int(expected_id++, id);
    grn_test_assert_context(context);
  }

  grn_table_delete_by_id(context, table, 3);
  grn_table_delete_by_id(context, table, 6);

  cut_assert_equal_string("table_create Weekdays TABLE_HASH_KEY ShortText\n"
                          "load --table Weekdays\n"
                          "[\n"
                          "[\"_key\"],\n"
                          "[\"Sun\"],\n"
                          "[\"Mon\"],\n"
                          "[\"Wed\"],\n"
                          "[\"Thu\"],\n"
                          "[\"Sat\"]\n"
                          "]",
                          send_command("dump"));
}
Пример #3
0
void
test_expression_lifetime_over_database(void)
{
  const gchar *path;
  gint i, n_tries = 100;
  grn_obj *expression;

  cut_omit("will be SEGVed.");
  path = cut_build_path(tmp_directory, "database.groonga", NULL);
  for (i = 0; i < n_tries; i++) {
    gint j, n_records = 100;
    const gchar *query;
    grn_obj *table, *variable;
    grn_obj default_column;

    database = grn_db_create(context, path, NULL);
    grn_test_assert_context(context);

    assert_send_command("table_create Sites 0 ShortText");
    assert_send_command("column_create Sites point COLUMN_SCALAR Int32");
    for (j = 0; j < n_records; j++) {
      gchar *command;

      command = g_strdup_printf("load '"
                                "[[\"_key\", \"point\"],"
                                "[\"http://groonga.org/version/%d\",%d]]' "
                                "Sites",
                                j, j);
      assert_send_command(command);
      g_free(command);
    }

    table = get_object("Sites");
    GRN_EXPR_CREATE_FOR_QUERY(context, table, expression, variable);
    grn_obj_unlink(context, table);

    GRN_TEXT_INIT(&default_column, 0);
    GRN_TEXT_PUTS(context, &default_column, "point");
    query = "point:50";
    grn_expr_parse(context, expression,
                   query, strlen(query),
                   &default_column,
                   GRN_OP_MATCH, GRN_OP_AND,
                   GRN_EXPR_SYNTAX_QUERY | GRN_EXPR_ALLOW_COLUMN);
    grn_test_assert_context(context);
    grn_obj_unlink(context, &default_column);
    grn_expr_compile(context, expression);

    grn_obj_remove(context, database);
    database = NULL;

    remove_tmp_directory();
    g_mkdir_with_parents(tmp_directory, 0700);
  }

  grn_ctx_fin(context);
  g_free(context);
  context = NULL;
}
void
test_near_uint32(gpointer data)
{
  grn_id id;
  int min_size, offset, limit, flags;
  guint32 max;
  const GList *expected_keys;
  GList *actual_keys = NULL;

  create_uint32_table();

  min_size = gcut_data_get_int(data, "min-size");
  max = gcut_data_get_uint(data, "max");
  offset = gcut_data_get_int(data, "offset");
  limit = gcut_data_get_int(data, "limit");
  flags = gcut_data_get_int(data, "flags");
  cursor = grn_table_cursor_open(context, table,
                                 NULL, min_size,
                                 &max, sizeof(max),
                                 offset, limit,
                                 flags | GRN_CURSOR_PREFIX);
  grn_test_assert_context(context);
  while ((id = grn_table_cursor_next(context, cursor))) {
    guint32 *key;
    int key_size;

    key_size = grn_table_cursor_get_key(context, cursor, (void **)&key);
    actual_keys = g_list_append(actual_keys, GUINT_TO_POINTER(*key));
  }
  gcut_take_list(actual_keys, NULL);

  expected_keys = gcut_data_get_pointer(data, "expected");
  gcut_assert_equal_list_uint(expected_keys, actual_keys);
}
Пример #5
0
const gchar *
grn_test_send_command(grn_ctx *context, const gchar *command)
{
  unsigned int send_id, receive_id;
  GString *result;
  const gchar **lines;

  result = g_string_new(NULL);
  lines = cut_take_string_array(g_strsplit(command, "\n", 0));
  for (; *lines; lines++) {
    gchar *command_result;
    unsigned int command_result_length;
    int flags = 0;

    send_id = grn_ctx_send(context, *lines, strlen(*lines), 0);
    receive_id = grn_ctx_recv(context, &command_result, &command_result_length,
                              &flags);
    cut_assert_equal_uint(send_id, receive_id);
    g_string_append_len(result, command_result, command_result_length);
    grn_test_assert_context(context,
                            cut_message("<%s>:<%s>", command, result->str));
  }

  return cut_take_strdup(g_string_free(result, FALSE));
}
Пример #6
0
const GList *
grn_test_view_collect_string(grn_ctx          *context,
                             grn_obj          *view,
                             const gchar      *text_column_name)
{
  GList *records = NULL;
  grn_table_cursor *cursor;
  grn_obj id, value;
  grn_obj *text_column;

  cursor = grn_table_cursor_open(context, view, NULL, 0, NULL, 0,
                                 0, -1, GRN_CURSOR_ASCENDING);
  cut_assert_not_null(cursor);
  GRN_TEXT_INIT(&id, 0);
  GRN_TEXT_INIT(&value, 0);
  text_column = grn_obj_column(context, view,
                               text_column_name, strlen(text_column_name));
  while (grn_table_cursor_next_o(context, cursor, &id) == GRN_SUCCESS) {
    GRN_BULK_REWIND(&value);
    grn_obj_get_value_o(context, text_column, &id, &value);
    records = g_list_append(records, g_strndup(GRN_TEXT_VALUE(&value),
                                               GRN_TEXT_LEN(&value)));
  }
  grn_obj_unlink(context, &id);
  grn_obj_unlink(context, &value);
  grn_obj_unlink(context, text_column);
  gcut_take_list(records, g_free);

  grn_test_assert(grn_table_cursor_close(context, cursor));
  grn_test_assert_context(context);

  return records;
}
Пример #7
0
void
test_vector_column(gconstpointer data)
{
  const gchar *expected;
  grn_id id, type_id;
  grn_obj vector;
  grn_obj *elements;
  grn_obj *table, *column;
  const gchar *type_name;
  type_name = gcut_data_get_string(data, "type_name");
  type_id = grn_obj_id(context, get_object(type_name));

  table = table_create("Table", GRN_OBJ_TABLE_NO_KEY, NULL, NULL);
  grn_test_assert_context(context);
  column = column_create("Table", "Column", GRN_OBJ_COLUMN_VECTOR,
                         type_name, NULL);
  grn_test_assert_context(context);
  id = grn_table_add(context, table, NULL, 0, NULL);
  grn_test_assert_context(context);
  cut_assert_equal_int(1, id);
  elements = construct_elements(data);

  GRN_TEXT_INIT(&vector, GRN_OBJ_VECTOR);
  grn_vector_add_element(context, &vector,
                         GRN_TEXT_VALUE(&elements[0]),
                         GRN_TEXT_LEN(&elements[0]), 0, type_id);
  grn_vector_add_element(context, &vector,
                         GRN_TEXT_VALUE(&elements[1]),
                         GRN_TEXT_LEN(&elements[1]), 0, type_id);
  grn_vector_add_element(context, &vector,
                         GRN_TEXT_VALUE(&elements[2]),
                         GRN_TEXT_LEN(&elements[2]), 0, type_id);
  grn_obj_set_value(context, column, id, &vector, GRN_OBJ_SET);

  expected = cut_take_printf("table_create Table TABLE_NO_KEY\n"
                             "column_create Table Column COLUMN_VECTOR %s\n"
                             "load --table Table\n"
                             "[\n"
                             "[\"_id\",\"Column\"],\n"
                             "[1,%s]\n"
                             "]",
                             type_name,
                             gcut_data_get_string(data, "expected"));
  cut_assert_equal_string(expected, send_command("dump"));
  GRN_OBJ_FIN(context, &vector);
}
Пример #8
0
static void
add_groonga_bookmark(void)
{
  gchar key[] = "groonga";
  groonga_bookmark_id = grn_table_add(&context, bookmarks,
                                      &key, strlen(key), NULL);
  grn_test_assert_context(&context);
  cut_set_message("%s", cut_take_string(grn_collect_logger_to_string(logger)));
  grn_test_assert_not_nil(groonga_bookmark_id);
}
Пример #9
0
static void
setup_database(void)
{
  remove_tmp_directory();
  g_mkdir_with_parents(tmp_directory, 0700);

  database = grn_db_create(context, database_path, NULL);
  grn_test_assert_context(context);

  setup_ddl();
  setup_data();
}
Пример #10
0
static grn_obj *
query(const gchar *string)
{
  GRN_EXPR_CREATE_FOR_QUERY(context, comments, expression, variable);
  grn_test_assert(grn_expr_parse(context, expression,
                                 string, strlen(string),
                                 content, GRN_OP_MATCH, GRN_OP_AND,
                                 GRN_EXPR_SYNTAX_QUERY |
                                 GRN_EXPR_ALLOW_PRAGMA |
                                 GRN_EXPR_ALLOW_COLUMN));
  grn_test_assert_context(context);
  return expression;
}
Пример #11
0
void
test_add(void)
{
  assert_send_command("table_create Entries TABLE_VIEW");
  assert_send_command("table_create Users --key_type ShortText");
  assert_send_command("table_create Dogs --key_type ShortText");

  entries = get_object("Entries");
  users = get_object("Users");
  dogs = get_object("Dogs");

  grn_view_add(context, entries, users);
  grn_test_assert_context(context);
  grn_view_add(context, entries, dogs);
  grn_test_assert_context(context);

  cut_assert_equal_uint(0, grn_table_size(context, entries));
  assert_send_command("load '[[\"_key\"],[\"morita\"]]' Users");
  cut_assert_equal_uint(1, grn_table_size(context, entries));
  assert_send_command("load '[[\"_key\"],[\"pochi\"]]' Dogs");
  cut_assert_equal_uint(2, grn_table_size(context, entries));
}
Пример #12
0
void
test_create_with_valid_name(gpointer data)
{
  grn_obj *table;
  const gchar *table_name;

  table_name = gcut_data_get_string(data, "name");
  table = grn_table_create(context, table_name, strlen(table_name),
                           NULL,
                           GRN_OBJ_TABLE_NO_KEY | GRN_OBJ_PERSISTENT,
                           NULL, NULL);
  grn_test_assert_context(context);
}
Пример #13
0
void
test_size(void)
{
  guint n_builtin_objects = 255;
  const gchar table_name[] = "bookmarks";

  database = grn_db_create(context, NULL, NULL);

  cut_assert_equal_uint(n_builtin_objects, grn_table_size(context, database));
  grn_test_assert_context(context);

  grn_table_create(context,
                   table_name,
                   strlen(table_name),
                   NULL,
                   GRN_OBJ_TABLE_HASH_KEY,
                   grn_ctx_at(context, GRN_DB_UINT32),
                   NULL);
  cut_assert_equal_uint(n_builtin_objects + 1,
                        grn_table_size(context, database));
  grn_test_assert_context(context);
}
Пример #14
0
static void
add_count_column_to_bookmarks_table (void)
{
  const gchar count_column_name[] = "count";

  count_column = grn_column_create(&context,
                                   bookmarks,
                                   count_column_name,
                                   strlen(count_column_name),
                                   NULL, 0,
                                   LOOKUP("<int>"));
  grn_test_assert_context(&context);
  cut_set_message("%s", cut_take_string(grn_collect_logger_to_string(logger)));
  cut_assert_not_null(count_column);
}
Пример #15
0
static void
create_bookmarks_table(void)
{
  const gchar bookmarks_table_name[] = "bookmarks";

  bookmarks = grn_table_create(&context,
                               bookmarks_table_name,
                               strlen(bookmarks_table_name),
                               NULL,
                               GRN_OBJ_TABLE_HASH_KEY,
                               LOOKUP("<shorttext>"),
                               1024);
  grn_test_assert_context(&context);
  cut_set_message("%s", cut_take_string(grn_collect_logger_to_string(logger)));
  cut_assert_not_null(bookmarks);
}
Пример #16
0
static void
add_count_column_to_bookmarks_table (void)
{
  const gchar count_column_name[] = "count";

  count_column = grn_column_create(context,
                                   bookmarks,
                                   count_column_name,
                                   strlen(count_column_name),
                                   NULL, 0,
                                   get_object("Int32"));
  grn_test_assert_context(context);
  cut_assert_not_null(
    count_column,
    cut_message("%s", grn_collect_logger_to_string(logger)));
}
Пример #17
0
void
cut_setup(void)
{
  reset_variables();

  logger = setup_grn_logger();

  context = g_new(grn_ctx, 1);
  grn_ctx_init(context, 0);

  setup_database();

  result = grn_table_create(context, NULL, 0, NULL,
                            GRN_TABLE_HASH_KEY | GRN_OBJ_WITH_SUBREC,
                            comments, NULL);
  grn_test_assert_context(context);
}
Пример #18
0
static void
create_bookmarks_table(void)
{
  const gchar bookmarks_table_name[] = "bookmarks";

  bookmarks = grn_table_create(context,
                               bookmarks_table_name,
                               strlen(bookmarks_table_name),
                               NULL,
                               GRN_OBJ_TABLE_HASH_KEY,
                               get_object("ShortText"),
                               NULL);
  grn_test_assert_context(context);
  cut_assert_not_null(
    bookmarks,
    cut_message("%s", grn_collect_logger_to_string(logger)));
}
void
test_common_prefix_search(gpointer data)
{
  grn_id id;
  const gchar *max;
  int min_size, offset, limit, flags;
  const GList *expected_keys;
  GList *actual_keys = NULL;

  cut_omit("crashed. Is it right usage?");
  create_short_text_table(gcut_take_new_list_string("abra",
                                                    "abracada",
                                                    "abracadabra",
                                                    "abubu",
                                                    "あ",
                                                    "ああ",
                                                    "あああ",
                                                    "い",
                                                    NULL));

  min_size = gcut_data_get_int(data, "min-size");
  max = gcut_data_get_string(data, "max");
  offset = gcut_data_get_int(data, "offset");
  limit = gcut_data_get_int(data, "limit");
  flags = gcut_data_get_int(data, "flags");
  cursor = grn_table_cursor_open(context, table,
                                 NULL, min_size,
                                 max, strlen(max),
                                 offset, limit,
                                 flags | GRN_CURSOR_PREFIX);
  grn_test_assert_context(context);
  while ((id = grn_table_cursor_next(context, cursor))) {
    gchar *key;
    int key_size;

    key_size = grn_table_cursor_get_key(context, cursor, (void **)&key);
    actual_keys = g_list_append(actual_keys, g_strndup(key, key_size));
  }
  gcut_take_list(actual_keys, g_free);

  expected_keys = gcut_data_get_pointer(data, "expected");
  gcut_assert_equal_list_string(expected_keys, actual_keys);
}
Пример #20
0
void
cut_setup(void)
{
  view = NULL;
  entries = NULL;
  users = NULL;
  dogs = NULL;

  logger = setup_grn_logger();

  context = g_new(grn_ctx, 1);
  grn_ctx_init(context, 0);

  remove_tmp_directory();
  g_mkdir_with_parents(tmp_directory, 0700);

  database = grn_db_create(context, database_path, NULL);
  grn_test_assert_context(context);
}
Пример #21
0
static grn_obj *
table_create(const gchar *name, grn_obj_flags flags,
             const gchar *key_type_name, const gchar *value_type_name)
{
  grn_obj *table;
  grn_obj *key_type = NULL, *value_type = NULL;

  if (key_type_name)
    key_type = get_object(key_type_name);
  if (value_type_name)
    value_type = get_object(value_type_name);

  table = grn_table_create(context,
                           name, strlen(name),
                           NULL,
                           flags | GRN_OBJ_PERSISTENT,
                           key_type, value_type);
  grn_test_assert_context(context);
  return table;
}
Пример #22
0
static grn_obj *
column_create(const gchar *table_name, const gchar *name, grn_obj_flags flags,
              const gchar *type_name, const gchar *sources)
{
  grn_obj *column;
  grn_obj *table = NULL, *type = NULL;

  if (table_name)
    table = get_object(table_name);
  if (type_name)
    type = get_object(type_name);

  column = grn_column_create(context,
                             table,
                             name, strlen(name),
                             NULL,
                             flags | GRN_OBJ_PERSISTENT,
                             type);
  grn_test_assert_context(context);
  return column;
}
Пример #23
0
void
test_create_on_temporary_table(void)
{
  grn_obj *table;
  grn_obj *column;
  const gchar *column_name = "count";

  table = grn_table_create(context, NULL, 0, NULL,
                           GRN_OBJ_TABLE_NO_KEY,
                           NULL,
                           NULL);
  grn_test_assert_context(context);
  column = grn_column_create(context,
                             table,
                             column_name,
                             strlen(column_name),
                             NULL, 0,
                             get_object("Int32"));
  grn_test_assert_error(GRN_INVALID_ARGUMENT,
                        "temporary table doesn't support column",
                        context);
}
void
test_near_geo_point(gpointer data)
{
  grn_id id;
  int min_size, offset, limit, flags;
  grn_obj max_string, max;
  const GList *expected_keys;
  GList *actual_keys = NULL;

  create_geo_point_table(
    cut_take_printf(" [\"%s\"],"
                    " [\"%s\"],"
                    " [\"%s\"],"
                    " [\"%s\"],"
                    " [\"%s\"],"
                    " [\"%s\"],"
                    " [\"%s\"],"
                    " [\"%s\"],"
                    " [\"%s\"]",
                    TAKEN_POINT(1, 2, 3,
                                4, 5, 6),
                    TAKEN_POINT(1, 2, 3,
                                7, 8, 9),
                    TAKEN_POINT(7, 8, 9,
                                4, 5, 6),
                    TAKEN_POINT(89, 59, 59,
                                179, 59, 59),
                    TAKEN_POINT(89, 59, 59,
                                179, -59, -59),
                    TAKEN_POINT(88, 58, 58,
                                178, 58, 58),
                    TAKEN_POINT(-89, -59, -59,
                                -179, -59, -59),
                    TAKEN_POINT(-89, -59, -59,
                                179, 59, 59),
                    TAKEN_POINT(-88, -58, -58,
                                -178, -58, -58)));

  min_size = gcut_data_get_int(data, "min-size");
  GRN_TEXT_INIT(&max_string, 0);
  GRN_TEXT_PUTS(context, &max_string, gcut_data_get_string(data, "max"));
  GRN_WGS84_GEO_POINT_INIT(&max, 0);
  grn_obj_cast(context, &max_string, &max, FALSE);
  grn_obj_unlink(context, &max_string);
  offset = gcut_data_get_int(data, "offset");
  limit = gcut_data_get_int(data, "limit");
  flags = gcut_data_get_int(data, "flags");
  cursor = grn_table_cursor_open(context, table,
                                 NULL, min_size,
                                 GRN_BULK_HEAD(&max), GRN_BULK_VSIZE(&max),
                                 offset, limit,
                                 flags | GRN_CURSOR_PREFIX);
  grn_obj_unlink(context, &max);
  grn_test_assert_context(context);
  while ((id = grn_table_cursor_next(context, cursor))) {
    grn_geo_point *key;
    int key_size;

    key_size = grn_table_cursor_get_key(context, cursor, (void **)&key);
    actual_keys = g_list_append(actual_keys,
                                g_strdup_printf("%dx%d",
                                                key->latitude,
                                                key->longitude));
  }
  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_prefix_geo_point(gpointer data)
{
  grn_id id;
  grn_obj min, min_string;
  int offset, limit, flags;
  unsigned min_size;
  const GList *expected_keys;
  GList *actual_keys = NULL;

  create_geo_point_table(
    geo_byte_load_data(
      "00000000 00111111 01010000 00110000 01001010 01011100 01101010 00010001",
      "00000000 00111111 01010000 00001101 01011101 01011011 01011001 01010011",
      "00000000 00111111 01010000 00000001 00011110 01010001 01101001 00110000",
      "00000000 00111111 01010000 00011000 01110000 00001011 00101110 01001010",
      "00000000 00111111 01010000 00010010 00110001 00110111 01111000 01110000",
      "00000000 00111111 01010000 00010010 00110001 00001000 00001010 00110011",
      "00000000 00111111 01010000 00101110 01110010 00111001 00011011 01101010",
      "00000000 00111111 01010000 00101101 00101000 00111111 01010110 00010110",
      "00000000 00111111 01010000 00101111 01001010 01101000 01000100 01100011",
      "00000000 00111111 01010000 00101111 00011000 01000110 00100101 01011110",
      "00000000 00111111 01000101 01011001 01010110 00000111 00110100 01111111",
      "00000000 00111111 01000101 01010010 01100101 01100110 00010111 01111110",
      "00000000 00111111 01000101 01111111 01011011 01111101 00001001 01100001",
      "00000000 00111111 01010000 00100011 00001010 00000000 00001101 00111010",
      "00000000 00111111 01010000 00101110 01010011 00101001 00101001 00100011",
      "00000000 00111111 01010000 00111010 01011111 00000010 00101001 01010000",
      "00000000 00111111 01010000 00111001 00111101 00001001 00001011 01010011",
      "00000000 00111111 01000101 01011100 00001100 01000001 01011010 00010011",
      "00000000 00111111 01010000 00100011 00000001 00000111 01011100 01110011",
      "00000000 00111111 01010000 00100011 01100100 01011000 00000111 01110010",
      "00000000 00111111 01010000 00111000 01100100 01101011 01111100 01111011",
      "00000000 00111111 01010000 00001111 00101011 00011111 00110011 00001001",
      "00000000 00111111 01000101 01111011 01001011 01101011 00001001 00000001",
      "00000000 00111111 01000101 01011010 00110100 00000010 01111010 00000000",
      "00000000 00111111 01000101 01011011 00011010 00010111 00011000 00100000",
      "00000000 00111111 01010000 00100000 00010111 01000111 00110100 00101010",
      "00000000 00111111 01010000 00000000 01111101 00010000 00011101 00001111",
      "00000000 00111111 01000101 01000100 01010010 00100100 01100011 00111011",
      "00000000 00111111 01000101 01010001 00011100 01010110 00100110 00000110",
      "00000000 00111111 01010000 00101101 01111100 01101100 00111000 01111001",
      "00000000 00111111 01000101 01001101 00111110 00000101 00101010 01000101",
      "00000000 00111111 01000101 01000100 01111100 01101011 01101111 00010101",
      "00000000 00111111 01000101 01110111 01010100 01110100 01111000 01111000",
      "00000000 00111111 01010000 00100010 00111011 01000000 00111000 01100100",
      "00000000 00111111 01010000 00100010 00100111 01000011 00000010 01101001",
      "00000000 00111111 01000101 01011100 00110110 00100010 00111000 01100001",
      "00000000 00111101 01010101 00111101 01110000 01001011 01110011 00101100",
      "00000000 00111101 00000101 00111101 01110000 01001011 01110011 00101100",
      NULL));

  GRN_TEXT_INIT(&min_string, 0);
  GRN_TEXT_PUTS(context, &min_string,
                geo_byte_parse(gcut_data_get_string(data, "min")));
  GRN_WGS84_GEO_POINT_INIT(&min, 0);
  grn_obj_cast(context, &min_string, &min, FALSE);
  grn_obj_unlink(context, &min_string);

  min_size = gcut_data_get_uint(data, "min-size");
  offset = gcut_data_get_int(data, "offset");
  limit = gcut_data_get_int(data, "limit");
  flags = gcut_data_get_int(data, "flags");
  cursor = grn_table_cursor_open(context, table,
                                 GRN_BULK_HEAD(&min), min_size,
                                 NULL, 0,
                                 offset, limit,
                                 flags | GRN_CURSOR_PREFIX);
  grn_obj_unlink(context, &min);
  grn_test_assert_context(context);
  while ((id = grn_table_cursor_next(context, cursor))) {
    grn_geo_point *key;
    int i, j, key_size;
    uint8_t encoded_key[sizeof(grn_geo_point)];
    GString *geo_byte;

    key_size = grn_table_cursor_get_key(context, cursor, (void **)&key);
    grn_gton(encoded_key, key, key_size);
    geo_byte = g_string_new(NULL);
    for (i = 0; i < sizeof(grn_geo_point); i++) {
      if (i != 0) {
        g_string_append(geo_byte, " ");
      }
      for (j = 0; j < 8; j++) {
        g_string_append_printf(geo_byte, "%d", (encoded_key[i] >> (7 - j)) & 1);
      }
    }
    actual_keys = g_list_append(actual_keys, g_string_free(geo_byte, FALSE));
  }
  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_prefix_rk(gpointer data)
{
  grn_id id;
  const gchar *min;
  int offset, limit;
  const GList *expected_keys;
  GList *actual_keys = NULL;

  create_short_text_table(
    gcut_take_new_list_string("インデックス",
                              "エヌグラム",
                              "エンジン",
                              "カネソナエタ",
                              "カノウ",
                              "キノウ",
                              "キョウカ",
                              "クミコミ",
                              "クミコム",
                              "グルンガ",
                              "ケンサク",
                              "ケンサクヨウキュウ",
                              "ゲンゴ",
                              "コウセイド",
                              "コウソク",
                              "コンパクト",
                              "サクセイ",
                              "ショリ",
                              "ショリケイ",
                              "ジッソウ",
                              "ジュンスイ",
                              "スクリプト",
                              "セッケイ",
                              "ゼンブン",
                              "タイプ",
                              "タンゴ",
                              "ダイキボ",
                              "テンチ",
                              "ディービーエムエス",
                              "トウ",
                              "トクチョウ",
                              "ブンショリョウ",
                              "ヨウキュウ",
                              NULL));

  min = gcut_data_get_string(data, "min");
  offset = gcut_data_get_int(data, "offset");
  limit = gcut_data_get_int(data, "limit");
  cursor = grn_table_cursor_open(context, table,
                                 min, strlen(min),
                                 NULL, 0,
                                 offset, limit,
                                 GRN_CURSOR_PREFIX | GRN_CURSOR_RK);
  grn_test_assert_context(context);
  while ((id = grn_table_cursor_next(context, cursor))) {
    gchar *key;
    int key_size;

    key_size = grn_table_cursor_get_key(context, cursor, (void **)&key);
    actual_keys = g_list_append(actual_keys, g_strndup(key, key_size));
  }
  actual_keys = g_list_sort(actual_keys, (GCompareFunc)strcmp);
  gcut_take_list(actual_keys, g_free);

  expected_keys = gcut_data_get_pointer(data, "expected");
  gcut_assert_equal_list_string(expected_keys, actual_keys);
}
Пример #27
0
void
test_truncate_named(gconstpointer data)
{
  grn_obj_flags flags;
  const gchar *table_name = "SearchEngines";
  const gchar *key;
  grn_obj *key_type;
  unsigned key_size;
  const gchar *column_name = "description";
  grn_obj *column_type;
  const gchar *column_value = "An open-source fulltext search engine";
  grn_bool array_p;
  grn_id record_id;
  int added;

  flags = gcut_data_get_int(data, "flags");
  array_p = ((flags & GRN_OBJ_TABLE_TYPE_MASK) == GRN_OBJ_TABLE_NO_KEY);

  if (array_p) {
    key = NULL;
    key_size = 0;
    key_type = NULL;
  } else {
    key = "groonga";
    key_size = strlen(key);
    key_type = grn_ctx_at(context, GRN_DB_SHORT_TEXT);
  }
  table = grn_table_create(context,
			   table_name, strlen(table_name), NULL,
                           flags | GRN_OBJ_PERSISTENT,
                           key_type, NULL);
  if (key_type) {
    grn_obj_unlink(context, key_type);
  }
  grn_test_assert_context(context);

  column_type = grn_ctx_at(context, GRN_DB_SHORT_TEXT);
  column = grn_column_create(context, table, column_name, strlen(column_name),
			     NULL,
			     GRN_OBJ_COLUMN_SCALAR | GRN_OBJ_PERSISTENT,
			     column_type);
  grn_obj_unlink(context, column_type);
  grn_test_assert_context(context);

  record_id = grn_table_add(context, table, key, key_size, &added);
  grn_test_assert_not_nil(record_id);
  cut_assert_true(added);

  grn_obj_reinit(context, &buffer, GRN_DB_SHORT_TEXT, 0);
  GRN_TEXT_PUTS(context, &buffer, column_value);
  grn_test_assert(grn_obj_set_value(context, column, record_id,
				    &buffer, GRN_OBJ_SET));

  GRN_BULK_REWIND(&buffer);
  grn_obj_get_value(context, column, record_id, &buffer);
  GRN_TEXT_PUTC(context, &buffer, '\0');
  cut_assert_equal_string(column_value, GRN_TEXT_VALUE(&buffer));
  cut_assert_equal_uint(1, grn_table_size(context, table));

  grn_test_assert(grn_table_truncate(context, table));

  GRN_BULK_REWIND(&buffer);
  grn_obj_get_value(context, column, record_id, &buffer);
  GRN_TEXT_PUTC(context, &buffer, '\0');
  cut_assert_equal_string("", GRN_TEXT_VALUE(&buffer));
  cut_assert_equal_uint(0, grn_table_size(context, table));
}
Пример #28
0
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);
}