Beispiel #1
0
static mrb_value
mrb_grn_table_array_reference(mrb_state *mrb, mrb_value self)
{
  grn_ctx *ctx = (grn_ctx *)mrb->ud;
  grn_obj *table;
  grn_id key_domain_id;
  mrb_value mrb_key;
  grn_id record_id;
  grn_mrb_value_to_raw_data_buffer buffer;
  void *key;
  unsigned int key_size;

  mrb_get_args(mrb, "o", &mrb_key);

  table = DATA_PTR(self);
  if (table->header.type == GRN_DB) {
    key_domain_id = GRN_DB_SHORT_TEXT;
  } else {
    key_domain_id = table->header.domain;
  }

  grn_mrb_value_to_raw_data_buffer_init(mrb, &buffer);
  grn_mrb_value_to_raw_data(mrb, "key", mrb_key, key_domain_id,
                            &buffer, &key, &key_size);
  record_id = grn_table_get(ctx, table, key, key_size);
  grn_mrb_value_to_raw_data_buffer_fin(mrb, &buffer);

  if (record_id == GRN_ID_NIL) {
    return mrb_nil_value();
  } else {
    return mrb_fixnum_value(record_id);
  }
}
Beispiel #2
0
int
table_get(void)
{
  int i;
  grn_obj buf;
  grn_obj *table = grn_ctx_get(&ctx, "<t1>", 4);
  if (!table) { return -1; }
  GRN_TEXT_INIT(&buf, 0);
  for (i = 0; i < nloops; i++) {
    int key = GENKEY(i);
    GRN_BULK_REWIND(&buf);
    grn_text_itoh(&ctx, &buf, key, key_size);
    {
      grn_id rid = grn_table_get(&ctx, table, GRN_BULK_HEAD(&buf), key_size);
      if (!rid) {
        fprintf(stderr, "table_lookup failed");
      } else {
        grn_obj obj, *p;
        GRN_TEXT_INIT(&obj, 0);
        p = grn_obj_get_value(&ctx, table, rid, &obj);
        if (!p) {
          fprintf(stderr, "grn_obj_get_value failed\n");
        } else {
          if (memcmp(GRN_BULK_HEAD(p), GRN_BULK_HEAD(&buf), value_size)) {
            fprintf(stderr, "value unmatch\n");
          }
          grn_obj_close(&ctx, p);
        }
      }
    }
  }
  grn_obj_close(&ctx, &buf);
  return 0;
}
Beispiel #3
0
static GList *
retrieve_record_ids(const gchar *term)
{
    grn_id term_id;
    grn_ii_cursor *cursor;
    grn_ii_posting *posting;

    term_id = grn_table_get(context, lexicon, term, strlen(term));
    if (term_id == GRN_ID_NIL)
        return NULL;

    record_ids_free();

    cursor = grn_ii_cursor_open(context, inverted_index, term_id,
                                GRN_ID_NIL, GRN_ID_MAX, 5, 0);
    if (!cursor)
        return NULL;

    while ((posting = grn_ii_cursor_next(context, cursor))) {
        record_ids = g_list_append(record_ids, g_strdup_printf("%d", posting->rid));
    }
    grn_ii_cursor_close(context, cursor);

    return record_ids;
}
Beispiel #4
0
void
test_read_write(gconstpointer *data)
{
  gint i;
  int added;
  grn_ctx *context;
  grn_obj *table;
  const gchar *path;
  const gchar *value_string;
  gint process_number = 0;
  const gchar *process_number_string;
  const gchar table_name[] = "performance-read-write";
  grn_obj value;
  grn_obj *retrieved_value;
  grn_id id;
  grn_rc rc;

  i = GPOINTER_TO_INT(data);
  process_number_string = g_getenv(GRN_TEST_ENV_PROCESS_NUMBER);
  if (process_number_string)
    process_number = atoi(process_number_string);

  rc = grn_ctx_init(&contexts[i], GRN_CTX_USE_QL);
  grn_test_assert(rc, cut_set_message("context: %d (%d)", i, process_number));
  context = &contexts[i];

  path = g_getenv(GRN_TEST_ENV_TABLE_PATH);
  cut_assert_not_null(path);
  tables[i] = grn_table_open(context, table_name, strlen(table_name),
                             path);
  cut_assert_not_null(tables[i],
                      cut_message("table: %d (%d)", i, process_number));
  table = tables[i];

  grn_test_assert_nil(grn_table_get(context, table, &i, sizeof(grn_id)),
                      cut_message("lookup - fail: (%d:%d)", i, process_number));

  value_string = cut_take_printf("value: (%d:%d)", i, process_number);
  id = grn_table_add(context, table, &i, sizeof(grn_id), &added);
  grn_test_assert_not_nil(id);
  cut_assert_equal_int(1, added);

  GRN_TEXT_INIT(&value, GRN_OBJ_DO_SHALLOW_COPY);
  GRN_TEXT_SET_REF(&value, value_string, strlen(value_string));
  grn_obj_set_value(context, table, id, &value, GRN_OBJ_SET);

  retrieved_value = grn_obj_get_value(context, table, id, NULL);
  grn_test_assert_not_nil(
    id,
    cut_message("lookup - success: (%d:%d)", i, process_number));
  GRN_TEXT_PUTC(context, retrieved_value, '\0');
  cut_assert_equal_string(value_string, GRN_BULK_HEAD(retrieved_value));

  tables[i] = NULL;
  grn_test_assert(grn_obj_close(context, table));

  //  contexts[i] = NULL;
  grn_test_assert(grn_ctx_fin(context));
}
Beispiel #5
0
Datei: put.c Projekt: genki/hog
// <cmd> {<len> <column id>} <types> <#kvs> [{<len> <key>} {<len> <value>}]...
void put_or_set(server_t *s, grn_ctx *ctx, int set)
{
    uint32_t len;
    HOG_RECV(s, &len, sizeof(len), return);
    len = ntohl(len);
    char *buf = hog_alloc(NULL, len);
    HOG_RECV(s, buf, len, goto cleanup);
    grn_obj *col, *table;
    col = grn_ctx_get(ctx, buf, len);
    if(grn_obj_is_table(ctx, col)) table = col;
    else table = grn_column_table(ctx, col);
    // get key and value types
    char types[2];
    HOG_RECV(s, types, 2, goto cleanup);
    // receive keys and values
    uint32_t nkeys;
    HOG_RECV(s, &nkeys, sizeof(nkeys), goto cleanup);
    nkeys = ntohl(nkeys);
    grn_obj value;
    GRN_OBJ_INIT(&value, GRN_BULK, 0, types[1]);
    for(uint32_t i = 0; i < nkeys; ++i){
        HOG_RECV(s, &len, sizeof(len), goto value_fin);
        len = ntohl(len);
        buf = hog_alloc(buf, len);
        HOG_RECV(s, buf, len, goto value_fin);
        ntoh_buf(buf, len, types[0]);
        grn_id id;
        if(set){
            id = grn_table_get(ctx, table, buf, len);
        }else{
            id = grn_table_add(ctx, table, buf, len, NULL);
        }
        HOG_RECV(s, &len, sizeof(len), goto value_fin);
        len = ntohl(len);
        buf = hog_alloc(buf, len);
        HOG_RECV(s, buf, len, goto value_fin);
        if(id == GRN_ID_NIL) continue;
        ntoh_buf(buf, len, types[1]);
        GRN_BULK_REWIND(&value);
        grn_bulk_write(ctx, &value, buf, len);
        grn_obj_set_value(ctx, col, id, &value, GRN_OBJ_SET);
    }
    submit_one(s->socket);
value_fin:
    GRN_OBJ_FIN(ctx, &value);
cleanup:
    free(buf);
}
/*
 * テーブルに主キーが _key_ のレコードがあるならtrueを返す。
 *
 * @overload has_key?(key)
 */
static VALUE
rb_grn_table_key_support_has_key (VALUE self, VALUE rb_key)
{
    grn_ctx *context;
    grn_obj *table, *key, *domain;
    grn_id id, domain_id;

    rb_grn_table_key_support_deconstruct(SELF(self), &table, &context,
                                         &key, &domain_id, &domain,
                                         NULL, NULL, NULL,
                                         NULL);

    GRN_BULK_REWIND(key);
    RVAL2GRNKEY(rb_key, context, key, domain_id, domain, self);
    id = grn_table_get(context, table, GRN_BULK_HEAD(key), GRN_BULK_VSIZE(key));

    return id == GRN_ID_NIL ? Qfalse : Qtrue;
}
Beispiel #7
0
int
column_get(void)
{
  int i, s = 0;
  grn_obj buf;
  grn_obj *table = grn_ctx_get(&ctx, "<t1>", 4);
  grn_obj *column = grn_ctx_get(&ctx, "<t1>.c1", 7);
  if (!table || !column) { return -1; }
  GRN_TEXT_INIT(&buf, 0);
  for (i = 0; i < nloops; i++) {
    int key = GENKEY(i);
    GRN_BULK_REWIND(&buf);
    grn_text_itoh(&ctx, &buf, key, key_size);
    {
      grn_id rid = grn_table_get(&ctx, table, GRN_BULK_HEAD(&buf), key_size);
      if (!rid) {
        fprintf(stderr, "table_lookup failed\n");
      } else {
        grn_obj obj, *p;
        unsigned int v = key % value_size;
        GRN_TEXT_INIT(&obj, 0);
        p = grn_obj_get_value(&ctx, column, rid, &obj);
        if (!p) {
          fprintf(stderr, "grn_obj_get_value failed\n");
        } else {
          if (GRN_BULK_VSIZE(p) != v) {
            fprintf(stderr, "value_size unmatch %d (%ld:%u)\n", i, GRN_BULK_VSIZE(p), v);
          } else {
            if (v && GRN_BULK_HEAD(p)[v-1] != GRN_BULK_HEAD(&buf)[0]) {
              fprintf(stderr, "value unmatch\n");
            } else {
              s++;
            }
          }
          grn_obj_close(&ctx, p);
        }
      }
    }
  }
  grn_obj_close(&ctx, &buf);
  if (i != s) { printf("successed: %d\n", s); }
  return 0;
}
Beispiel #8
0
static grn_obj *
yangram_next(grn_ctx *ctx, GNUC_UNUSED int nargs, GNUC_UNUSED grn_obj **args,
             grn_user_data *user_data)
{
  grn_yangram_tokenizer *tokenizer = user_data->ptr;
  const unsigned char *string_end = tokenizer->end;
  const unsigned char *token_top = tokenizer->next;
  const unsigned char *token_next = token_top;
  const unsigned char *token_tail = token_top;
  int token_size = 0;
  grn_bool is_token_grouped = GRN_FALSE;
  const unsigned char *token_ctypes = NULL;
  unsigned int ctypes_skip_size;
  int char_length = 0;
  grn_tokenizer_status status = 0;
  grn_bool is_token_hit = GRN_FALSE;
  grn_obj *lexicon = args[0];

  if (tokenizer->phrase_table) {
    if (tokenizer->nhits > 0 &&
        token_top - (const unsigned char *)tokenizer->scan_start >
        tokenizer->hits[tokenizer->current_hit].offset) {
      tokenizer->current_hit++;
    }
    if (tokenizer->current_hit >= tokenizer->nhits) {
      tokenizer->scan_start = tokenizer->scan_rest;
      unsigned int scan_rest_length = tokenizer->end - (const unsigned char *)tokenizer->scan_rest;
      if (scan_rest_length > 0) {
        tokenizer->nhits = grn_pat_scan(ctx, (grn_pat *)tokenizer->phrase_table,
                                        tokenizer->scan_rest,
                                        scan_rest_length,
                                        tokenizer->hits, MAX_N_HITS, &(tokenizer->scan_rest));
        tokenizer->current_hit = 0;
      }
    }
    if (tokenizer->nhits > 0 &&
        tokenizer->current_hit < tokenizer->nhits &&
        token_top - (const unsigned char *)tokenizer->scan_start ==
        tokenizer->hits[tokenizer->current_hit].offset) {
      is_token_hit = GRN_TRUE;
    }
  }

  if (tokenizer->ctypes) {
    token_ctypes = tokenizer->ctypes + tokenizer->ctypes_next;
  } else {
    token_ctypes = NULL;
  }

  if (is_token_hit) {
   token_size = forward_scan_hit_token_tail(ctx, tokenizer, &token_tail,
                                            tokenizer->hits[tokenizer->current_hit].length);
   token_next = token_tail;
   tokenizer->current_hit++;
  } else {
    is_token_grouped = is_token_group(tokenizer, token_ctypes);
    if (is_token_grouped) {
      token_size = forward_grouped_token_tail(ctx, tokenizer, token_ctypes, &token_tail);
      token_next = token_tail;
    } else {
      token_size = forward_ngram_token_tail(ctx, tokenizer, token_ctypes, &token_tail);
      char_length = grn_plugin_charlen(ctx, (char *)token_next,
                                       tokenizer->rest_length,
                                       tokenizer->query->encoding);
      token_next += char_length;
    }
  }

  if (token_top == token_tail || token_next == string_end) {
    ctypes_skip_size = 0;
  } else {
    if (is_token_grouped || is_token_hit) {
      ctypes_skip_size = token_size;
    } else {
      ctypes_skip_size = 1;
    }
  }

  if (tokenizer->use_vgram > 0 && !is_token_grouped) {
    grn_bool maybe_vgram = GRN_FALSE;

    grn_id id;
    id = grn_table_get(ctx, tokenizer->vgram_table,
                       (const char *)token_top, token_tail - token_top);
    if (id) {
      maybe_vgram = GRN_TRUE;
    }

    if (tokenizer->use_vgram >= VGRAM_BOTH && !maybe_vgram) {
      if (token_tail < string_end &&
          !is_group_border(ctx, tokenizer, token_tail, token_ctypes, token_size)) {
        grn_id id;
        const unsigned char *token_next_tail;
        char_length = grn_plugin_charlen(ctx, (char *)token_tail,
                                         tokenizer->rest_length,
                                         tokenizer->query->encoding);
        token_next_tail = token_tail + char_length;
        id = grn_table_get(ctx, tokenizer->vgram_table,
                           (const char *)token_next, token_next_tail - token_next);
        if (id) {
          maybe_vgram = GRN_TRUE;
        }
      } else if (token_tail == string_end &&
                 tokenizer->query->tokenize_mode == GRN_TOKENIZE_GET) {
        maybe_vgram = GRN_TRUE;
      }
    }

    if (maybe_vgram) {
      if (token_tail < string_end &&
          !is_group_border(ctx, tokenizer, token_tail, token_ctypes, token_size)) {
        char_length = grn_plugin_charlen(ctx, (char *)token_tail,
                                         tokenizer->rest_length,
                                         tokenizer->query->encoding);
        token_size++;
        token_tail += char_length;


        if (tokenizer->use_vgram == VGRAM_QUAD) {
          if (token_tail < string_end &&
              !is_group_border(ctx, tokenizer, token_tail, token_ctypes, token_size)) {
            id = grn_table_get(ctx, tokenizer->vgram_table,
                               (const char *)token_top, token_tail - token_top);
            if (id) {
              char_length = grn_plugin_charlen(ctx, (char *)token_tail,
                                               tokenizer->rest_length,
                                               tokenizer->query->encoding);
              token_size++;
              token_tail += char_length;
            }
          } else {
            if (tokenizer->query->tokenize_mode == GRN_TOKENIZE_GET) {
              grn_id tid;
              tid = grn_table_get(ctx, lexicon,
                                  (const char *)token_top, token_tail - token_top);
              if (tid == GRN_ID_NIL) {
                int added;
                grn_table_add(ctx, lexicon,
                              (const char *)token_top, token_tail - token_top, &added);
              }
              status |= GRN_TOKEN_FORCE_PREFIX;
            }
          }
        }
      } else {
        if (tokenizer->query->tokenize_mode == GRN_TOKENIZE_GET) {
          grn_id tid;
          tid = grn_table_get(ctx, lexicon,
                             (const char *)token_top, token_tail - token_top);
          if (tid == GRN_ID_NIL) {
            int added;
            grn_table_add(ctx, lexicon,
                          (const char *)token_top, token_tail - token_top, &added);
          }
          status |= GRN_TOKEN_FORCE_PREFIX;
        }
      }
    }
  }

  if (token_top == token_tail || token_next == string_end) {
    status |= GRN_TOKEN_LAST;
  }

  if (token_tail == string_end) {
    status |= GRN_TOKEN_REACH_END;
  }

  if (!is_token_grouped && !is_token_hit && token_size < tokenizer->ngram_unit) {
    status |= GRN_TOKEN_UNMATURED;
  }

  if (tokenizer->pushed_token_tail &&
      token_top < tokenizer->pushed_token_tail) {
    status |= GRN_TOKEN_OVERLAP;
    if (tokenizer->skip_overlap &&
        !grn_ii_overlap_token_skip_enable &&
        !(status & GRN_TOKEN_REACH_END) &&
        !(status & GRN_TOKEN_SKIP_WITH_POSITION) &&
      tokenizer->query->tokenize_mode == GRN_TOKENIZE_GET) {
      if (token_tail <= tokenizer->pushed_token_tail) {
        status |= GRN_TOKEN_SKIP;
      } else {
        if (!is_group_border(ctx, tokenizer, token_tail, token_ctypes, token_size)) {
          status |= GRN_TOKEN_SKIP;
        }
      }
    }
  }

  if (!(status & GRN_TOKEN_SKIP) &&
      !(status & GRN_TOKEN_SKIP_WITH_POSITION)) {
    tokenizer->pushed_token_tail = token_tail;
  }

  tokenizer->next = token_next;
  tokenizer->rest_length = string_end - token_next;
  tokenizer->ctypes_next = tokenizer->ctypes_next + ctypes_skip_size;

  grn_tokenizer_token_push(ctx,
                           &(tokenizer->token),
                           (const char *)token_top,
                           token_tail - token_top,
                           status);

  return NULL;
}