示例#1
0
文件: tsv.c 项目: cosmo0920/groonga
static grn_obj *
func_query_expander_tsv(grn_ctx *ctx, int nargs, grn_obj **args,
                        grn_user_data *user_data)
{
  grn_rc rc = GRN_END_OF_DATA;
  grn_id id;
  grn_obj *term, *expanded_term;
  void *value;
  grn_obj *rc_object;

  term = args[0];
  expanded_term = args[1];
  id = grn_hash_get(ctx, synonyms,
                    GRN_TEXT_VALUE(term), GRN_TEXT_LEN(term),
                    &value);
  if (id != GRN_ID_NIL) {
    const char *query = value;
    GRN_TEXT_PUTS(ctx, expanded_term, query);
    rc = GRN_SUCCESS;
  }

  rc_object = grn_plugin_proc_alloc(ctx, user_data, GRN_DB_INT32, 0);
  if (rc_object) {
    GRN_INT32_SET(ctx, rc_object, rc);
  }

  return rc_object;
}
示例#2
0
void
test_fix_size_set_value_set(void)
{
  gint32 count = 29;
  gint32 retrieved_count;
  grn_obj record_value;
  grn_obj retrieved_record_value;

  GRN_INT32_INIT(&record_value, 0);
  GRN_INT32_SET(context, &record_value, count);
  grn_test_assert(grn_obj_set_value(context, count_column, groonga_bookmark_id,
                                    &record_value, GRN_OBJ_SET));

  GRN_INT32_INIT(&retrieved_record_value, 0);
  grn_obj_get_value(context, count_column, groonga_bookmark_id, &retrieved_record_value);
  retrieved_count = GRN_INT32_VALUE(&retrieved_record_value);
  cut_assert_equal_int(count, retrieved_count);
  GRN_OBJ_FIN(context, &record_value);
  GRN_OBJ_FIN(context, &retrieved_record_value);
}
示例#3
0
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;
  }
}
示例#4
0
grn_obj *
rb_grn_bulk_from_ruby_object (VALUE object, grn_ctx *context, grn_obj *bulk)
{
    if (bulk && bulk->header.domain == GRN_DB_TIME)
        return RVAL2GRNBULK_WITH_TYPE(object, context, bulk,
                                      bulk->header.domain,
                                      grn_ctx_at(context, bulk->header.domain));

    if (!bulk) {
        bulk = grn_obj_open(context, GRN_BULK, 0, GRN_ID_NIL);
        rb_grn_context_check(context, object);
    }

    switch (TYPE(object)) {
    case T_NIL:
        grn_obj_reinit(context, bulk, GRN_DB_VOID, 0);
        break;
    case T_SYMBOL:
        object = rb_funcall(object, rb_intern("to_s"), 0);
    case T_STRING:
        grn_obj_reinit(context, bulk, GRN_DB_TEXT, 0);
        rb_grn_context_text_set(context, bulk, object);
        break;
    case T_FIXNUM:
    case T_BIGNUM: {
        int64_t int64_value;
        int64_value = NUM2LL(object);
        if (int64_value <= INT32_MAX) {
            grn_obj_reinit(context, bulk, GRN_DB_INT32, 0);
            GRN_INT32_SET(context, bulk, int64_value);
        } else {
            grn_obj_reinit(context, bulk, GRN_DB_INT64, 0);
            GRN_INT64_SET(context, bulk, int64_value);
        }
        break;
    }
    case T_FLOAT:
        grn_obj_reinit(context, bulk, GRN_DB_FLOAT, 0);
        GRN_FLOAT_SET(context, bulk, NUM2DBL(object));
        break;
    case T_TRUE:
        grn_obj_reinit(context, bulk, GRN_DB_BOOL, 0);
        GRN_BOOL_SET(context, bulk, GRN_TRUE);
        break;
    case T_FALSE:
        grn_obj_reinit(context, bulk, GRN_DB_BOOL, 0);
        GRN_BOOL_SET(context, bulk, GRN_FALSE);
        break;
    default:
        if (RVAL2CBOOL(rb_obj_is_kind_of(object, rb_cTime))) {
            VALUE sec, usec;
            int64_t time_value;

            sec = rb_funcall(object, rb_intern("to_i"), 0);
            usec = rb_funcall(object, rb_intern("usec"), 0);
            time_value = GRN_TIME_PACK(NUM2LL(sec), NUM2LL(usec));
            grn_obj_reinit(context, bulk, GRN_DB_TIME, 0);
            GRN_TIME_SET(context, bulk, time_value);
        } else if (RVAL2CBOOL(rb_obj_is_kind_of(object, rb_cGrnObject))) {
            grn_obj *grn_object;
            grn_id id_value;

            grn_object = RVAL2GRNOBJECT(object, &context);
            grn_obj_reinit(context, bulk, grn_object->header.domain, 0);
            id_value = grn_obj_id(context, grn_object);
            GRN_RECORD_SET(context, bulk, id_value);
        } else if (RVAL2CBOOL(rb_obj_is_kind_of(object, rb_cGrnRecord))) {
            grn_obj *table;
            grn_id id_value;

            table = RVAL2GRNOBJECT(rb_funcall(object, rb_intern("table"), 0),
                                   &context);
            id_value = NUM2UINT(rb_funcall(object, rb_intern("id"), 0));
            grn_obj_reinit(context, bulk, grn_obj_id(context, table), 0);
            GRN_RECORD_SET(context, bulk, id_value);
        } else {
            rb_raise(rb_eTypeError,
                     "bulked object should be one of "
                     "[nil, true, false, String, Symbol, Integer, Float, Time, "
                     "Groonga::Object, Groonga::Record]: %s",
                     rb_grn_inspect(object));
        }
        break;
    }

    return bulk;
}
示例#5
0
static void
set_int32(gint32 number)
{
  grn_obj_reinit(&context, &src, GRN_DB_INT32, 0);
  GRN_INT32_SET(&context, &src, number);
}
示例#6
0
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);
}