void grn_ctx_impl_mrb_init(grn_ctx *ctx) { if (!grn_ctx_impl_mrb_mruby_enabled) { ctx->impl->mrb.state = NULL; ctx->impl->mrb.base_directory[0] = '\0'; ctx->impl->mrb.module = NULL; ctx->impl->mrb.object_class = NULL; ctx->impl->mrb.checked_procs = NULL; ctx->impl->mrb.registered_plugins = NULL; ctx->impl->mrb.builtin.time_class = NULL; ctx->impl->mrb.groonga.operator_class = NULL; } else { mrb_state *mrb; mrb = mrb_open(); ctx->impl->mrb.state = mrb; ctx->impl->mrb.base_directory[0] = '\0'; grn_ctx_impl_mrb_init_bindings(ctx); /* TODO: Implement better error handling on init. */ if (ctx->impl->mrb.state->exc) { mrb_print_error(mrb); } ctx->impl->mrb.checked_procs = grn_hash_create(ctx, NULL, sizeof(grn_id), 0, GRN_HASH_TINY); ctx->impl->mrb.registered_plugins = grn_hash_create(ctx, NULL, sizeof(grn_id), 0, GRN_HASH_TINY); GRN_VOID_INIT(&(ctx->impl->mrb.buffer.from)); GRN_VOID_INIT(&(ctx->impl->mrb.buffer.to)); ctx->impl->mrb.builtin.time_class = mrb_class_get(mrb, "Time"); } }
int hash_put(const char *path) { int i; grn_obj buf; grn_hash *hash = grn_hash_create(&ctx, path, key_size, value_size, GRN_OBJ_PERSISTENT|GRN_OBJ_KEY_VAR_SIZE); if (!hash) { 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); { void *value; grn_id rid = grn_hash_add(&ctx, hash, GRN_BULK_HEAD(&buf), key_size, &value, NULL); if (!rid) { fprintf(stderr, "table_lookup failed"); } else { memcpy(value, GRN_BULK_HEAD(&buf), value_size); } } } grn_obj_close(&ctx, &buf); grn_hash_close(&ctx, hash); return 0; }
static void grn_ctx_ql_init(grn_ctx *ctx, int flags) { if (!ctx->impl) { grn_ctx_impl_init(ctx); if (ERRP(ctx, GRN_ERROR)) { return; } } if (flags & GRN_CTX_BATCH_MODE) { ctx->impl->batchmode = 1; } if ((ctx->impl->objects = grn_array_create(ctx, NULL, sizeof(grn_cell), GRN_ARRAY_TINY))) { if ((ctx->impl->symbols = grn_hash_create(ctx, NULL, GRN_TABLE_MAX_KEY_SIZE, sizeof(grn_cell), GRN_OBJ_KEY_VAR_SIZE|GRN_HASH_TINY))) { if (!ERRP(ctx, GRN_ERROR)) { grn_ql_init_globals(ctx); if (!ERRP(ctx, GRN_ERROR)) { return; } } grn_hash_close(ctx, ctx->impl->symbols); ctx->impl->symbols = NULL; } else { MERR("ctx->impl->symbols init failed"); } grn_array_close(ctx, ctx->impl->objects); ctx->impl->objects = NULL; } else { MERR("ctx->impl->objects init failed"); } }
static mrb_value mrb_grn_table_get_column_ids(mrb_state *mrb, mrb_value self) { grn_ctx *ctx = (grn_ctx *)mrb->ud; grn_obj *table; grn_hash *columns; int n_columns; mrb_value mrb_column_ids; table = DATA_PTR(self); columns = grn_hash_create(ctx, NULL, sizeof(grn_id), 0, GRN_OBJ_TABLE_HASH_KEY | GRN_HASH_TINY); if (!columns) { grn_mrb_ctx_check(mrb); return mrb_ary_new(mrb); } n_columns = grn_table_columns(ctx, table, "", 0, (grn_obj *)columns); mrb_column_ids = mrb_ary_new_capa(mrb, n_columns); { grn_id *key; GRN_HASH_EACH(ctx, columns, id, &key, NULL, NULL, { mrb_ary_push(mrb, mrb_column_ids, mrb_fixnum_value(*key)); }); }
grn_rc grn_plugins_init(void) { grn_plugins = grn_hash_create(&grn_gctx, NULL, PATH_MAX, sizeof(grn_plugin *), GRN_OBJ_KEY_VAR_SIZE); if (!grn_plugins) { return GRN_NO_MEMORY_AVAILABLE; } return GRN_SUCCESS; }
static grn_rc grn_table_inspect(grn_ctx *ctx, grn_obj *buf, grn_obj *obj) { grn_hash *cols; grn_id range_id; grn_obj *range; GRN_TEXT_PUTS(ctx, buf, "#<table:"); grn_table_type_inspect(ctx, buf, obj); GRN_TEXT_PUTS(ctx, buf, " "); grn_inspect_name(ctx, buf, obj); if (obj->header.type != GRN_TABLE_NO_KEY) { grn_obj *domain; grn_id domain_id; GRN_TEXT_PUTS(ctx, buf, " key:"); domain_id = obj->header.domain; domain = grn_ctx_at(ctx, domain_id); if (domain) { grn_inspect_name(ctx, buf, domain); grn_obj_unlink(ctx, domain); } else if (domain_id) { grn_text_lltoa(ctx, buf, domain_id); } else { GRN_TEXT_PUTS(ctx, buf, "(nil)"); } } GRN_TEXT_PUTS(ctx, buf, " value:"); range_id = grn_obj_get_range(ctx, obj); range = grn_ctx_at(ctx, range_id); if (range) { grn_inspect_name(ctx, buf, range); } else if (range_id) { grn_text_lltoa(ctx, buf, range_id); } else { GRN_TEXT_PUTS(ctx, buf, "(nil)"); } GRN_TEXT_PUTS(ctx, buf, " size:"); grn_text_lltoa(ctx, buf, grn_table_size(ctx, obj)); GRN_TEXT_PUTS(ctx, buf, " columns:["); if ((cols = grn_hash_create(ctx, NULL, sizeof(grn_id), 0, GRN_OBJ_TABLE_HASH_KEY|GRN_HASH_TINY))) { if (grn_table_columns(ctx, obj, "", 0, (grn_obj *)cols)) { int i = 0; grn_id *key; GRN_HASH_EACH(ctx, cols, id, &key, NULL, NULL, { grn_obj *col = grn_ctx_at(ctx, *key); if (col) { if (i++ > 0) { GRN_TEXT_PUTS(ctx, buf, ", "); } grn_column_name_(ctx, col, buf); grn_obj_unlink(ctx, col); } }); }
static void grn_ctx_impl_init(grn_ctx *ctx) { if (!(ctx->impl = GRN_MALLOC(sizeof(struct _grn_ctx_impl)))) { return; } if (!(ctx->impl->segs = grn_io_anon_map(ctx, &ctx->impl->mi, sizeof(grn_io_mapinfo) * N_SEGMENTS))) { GRN_FREE(ctx->impl); ctx->impl = NULL; return; } #ifdef USE_DYNAMIC_MALLOC_CHANGE grn_ctx_impl_init_malloc(ctx); #endif if (!(ctx->impl->values = grn_array_create(ctx, NULL, sizeof(grn_tmp_db_obj), GRN_ARRAY_TINY))) { grn_io_anon_unmap(ctx, &ctx->impl->mi, sizeof(grn_io_mapinfo) * N_SEGMENTS); GRN_FREE(ctx->impl); ctx->impl = NULL; return; } ctx->impl->encoding = ctx->encoding; ctx->impl->lifoseg = -1; ctx->impl->currseg = -1; ctx->impl->db = NULL; ctx->impl->qe = grn_hash_create(ctx, NULL, sizeof(grn_id), sizeof(void *), 0); ctx->impl->stack_curr = 0; ctx->impl->phs = NIL; ctx->impl->code = NIL; ctx->impl->dump = NIL; ctx->impl->op = GRN_OP_T0LVL; ctx->impl->args = NIL; ctx->impl->envir = NIL; ctx->impl->value = NIL; ctx->impl->ncells = 0; ctx->impl->n_entries = 0; ctx->impl->seqno = 0; ctx->impl->lseqno = 0; ctx->impl->nbinds = 0; ctx->impl->nunbinds = 0; ctx->impl->feed_mode = grn_ql_atonce; ctx->impl->cur = NULL; ctx->impl->str_end = NULL; ctx->impl->batchmode = 0; ctx->impl->gc_verbose = 0; ctx->impl->inbuf = NULL; ctx->impl->co.mode = 0; ctx->impl->co.func = NULL; ctx->impl->objects = NULL; ctx->impl->symbols = NULL; ctx->impl->com = NULL; ctx->impl->outbuf = grn_obj_open(ctx, GRN_BULK, 0, 0); GRN_TEXT_INIT(&ctx->impl->subbuf, 0); }
grn_rc grn_plugins_init(void) { CRITICAL_SECTION_INIT(grn_plugins_lock); grn_ctx_init(&grn_plugins_ctx, 0); grn_plugins = grn_hash_create(&grn_plugins_ctx, NULL, PATH_MAX, sizeof(grn_plugin *), GRN_OBJ_KEY_VAR_SIZE); if (!grn_plugins) { grn_ctx_fin(&grn_plugins_ctx); return GRN_NO_MEMORY_AVAILABLE; } return GRN_SUCCESS; }
void grn_ctx_impl_mrb_init(grn_ctx *ctx) { if (!grn_ctx_impl_mrb_mruby_enabled) { ctx->impl->mrb.state = NULL; ctx->impl->mrb.base_directory[0] = '\0'; ctx->impl->mrb.module = NULL; ctx->impl->mrb.object_class = NULL; ctx->impl->mrb.checked_procs = NULL; ctx->impl->mrb.registered_plugins = NULL; ctx->impl->mrb.builtin.time_class = NULL; ctx->impl->mrb.groonga.operator_class = NULL; } else { mrb_state *mrb; mrb = mrb_open_allocf(grn_ctx_impl_mrb_allocf, ctx); ctx->impl->mrb.state = mrb; ctx->impl->mrb.base_directory[0] = '\0'; grn_ctx_impl_mrb_init_bindings(ctx); if (ctx->impl->mrb.state->exc) { mrb_value reason; reason = mrb_funcall(mrb, mrb_obj_value(mrb->exc), "inspect", 0); ERR(GRN_UNKNOWN_ERROR, "failed to initialize mruby: %.*s", RSTRING_LEN(reason), RSTRING_PTR(reason)); mrb_close(ctx->impl->mrb.state); ctx->impl->mrb.state = NULL; } else { ctx->impl->mrb.checked_procs = grn_hash_create(ctx, NULL, sizeof(grn_id), 0, GRN_HASH_TINY); ctx->impl->mrb.registered_plugins = grn_hash_create(ctx, NULL, sizeof(grn_id), 0, GRN_HASH_TINY); GRN_VOID_INIT(&(ctx->impl->mrb.buffer.from)); GRN_VOID_INIT(&(ctx->impl->mrb.buffer.to)); ctx->impl->mrb.builtin.time_class = mrb_class_get(mrb, "Time"); } } }
static const char * get_weight_vector(grn_ctx *ctx, grn_query *query, const char *source) { const char *p; if (!query->opt.weight_vector && !query->weight_set && !(query->opt.weight_vector = GRN_CALLOC(sizeof(int) * DEFAULT_WEIGHT_VECTOR_SIZE))) { GRN_LOG(ctx, GRN_LOG_ALERT, "get_weight_vector malloc fail"); return source; } for (p = source; p < query->str_end; ) { unsigned int key; int value; /* key, key is not zero */ key = grn_atoui(p, query->str_end, &p); if (!key || key > GRN_ID_MAX) { break; } /* value */ if (*p == ':') { p++; value = grn_atoi(p, query->str_end, &p); } else { value = 1; } if (query->weight_set) { int *pval; if (grn_hash_add(ctx, query->weight_set, &key, sizeof(unsigned int), (void **)&pval, NULL)) { *pval = value; } } else if (key < DEFAULT_WEIGHT_VECTOR_SIZE) { query->opt.weight_vector[key - 1] = value; } else { GRN_FREE(query->opt.weight_vector); query->opt.weight_vector = NULL; if (!(query->weight_set = grn_hash_create(ctx, NULL, sizeof(unsigned int), sizeof(int), 0))) { return source; } p = source; /* reparse */ continue; } if (*p != ',') { break; } p++; } return p; }
grn_rc GRN_PLUGIN_INIT(grn_ctx *ctx) { if (!synonyms) { synonyms = grn_hash_create(ctx, NULL, GRN_TABLE_MAX_KEY_SIZE, MAX_SYNONYM_BYTES, GRN_OBJ_TABLE_HASH_KEY | GRN_OBJ_KEY_VAR_SIZE); if (!synonyms) { return ctx->rc; } load_synonyms(ctx); } return ctx->rc; }
grn_rc grn_com_event_init(grn_ctx *ctx, grn_com_event *ev, int max_nevents, int data_size) { ev->max_nevents = max_nevents; if ((ev->hash = grn_hash_create(ctx, NULL, sizeof(grn_sock), data_size, 0))) { MUTEX_INIT(ev->mutex); COND_INIT(ev->cond); GRN_COM_QUEUE_INIT(&ev->recv_old); ev->msg_handler = NULL; memset(&(ev->curr_edge_id), 0, sizeof(grn_com_addr)); ev->acceptor = NULL; ev->opaque = NULL; #ifndef USE_SELECT # ifdef USE_EPOLL if ((ev->events = GRN_MALLOC(sizeof(struct epoll_event) * max_nevents))) { if ((ev->epfd = epoll_create(max_nevents)) != -1) { goto exit; } else { SERR("epoll_create"); } GRN_FREE(ev->events); } # else /* USE_EPOLL */ # ifdef USE_KQUEUE if ((ev->events = GRN_MALLOC(sizeof(struct kevent) * max_nevents))) { if ((ev->kqfd = kqueue()) != -1) { goto exit; } else { SERR("kqueue"); } GRN_FREE(ev->events); } # else /* USE_KQUEUE */ if ((ev->events = GRN_MALLOC(sizeof(struct pollfd) * max_nevents))) { goto exit; } # endif /* USE_KQUEUE*/ # endif /* USE_EPOLL */ grn_hash_close(ctx, ev->hash); ev->hash = NULL; ev->events = NULL; #else /* USE_SELECT */ goto exit; #endif /* USE_SELECT */ } exit : return ctx->rc; }
static grn_obj * proc_column_list(grn_ctx *ctx, int nargs, grn_obj **args, grn_user_data *user_data) { uint32_t nvars; grn_obj *buf = args[0]; grn_expr_var *vars; grn_proc_get_info(ctx, user_data, &vars, &nvars, NULL); if (nvars == 2) { grn_obj *table; grn_content_type otype = GET_OTYPE(&vars[1].value); if ((table = grn_ctx_get(ctx, GRN_TEXT_VALUE(&vars[0].value), GRN_TEXT_LEN(&vars[0].value)))) { grn_hash *cols; if ((cols = grn_hash_create(ctx, NULL, sizeof(grn_id), 0, GRN_OBJ_TABLE_HASH_KEY|GRN_HASH_TINY))) { if (grn_table_columns(ctx, table, NULL, 0, (grn_obj *)cols) >= 0) { grn_id *key; char line_delimiter, column_delimiter; switch (otype) { case GRN_CONTENT_TSV: line_delimiter = '\n'; column_delimiter = '\t'; GRN_TEXT_PUTS(ctx, buf, "id\tname\tpath\ttype\tflags\tdomain"); break; case GRN_CONTENT_JSON: line_delimiter = ','; column_delimiter = ','; GRN_TEXT_PUTS(ctx, buf, "[[\"id\",\"name\",\"path\",\"type\",\"flags\",\"domain\"]"); break; } GRN_HASH_EACH(ctx, cols, id, &key, NULL, NULL, { grn_obj *col; if ((col = grn_ctx_at(ctx, *key))) { GRN_TEXT_PUTC(ctx, buf, line_delimiter); if (!print_columninfo(ctx, col, buf, otype)) { grn_bulk_truncate(ctx, buf, GRN_BULK_VSIZE(buf) - 1); } grn_obj_unlink(ctx, col); } }); if (otype == GRN_CONTENT_JSON) { GRN_TEXT_PUTC(ctx, buf, ']'); } }
/* grn_ts_writer_expand() expands a wildcard. */ static grn_rc grn_ts_writer_expand(grn_ctx *ctx, grn_ts_writer *writer, grn_obj *table, grn_ts_str str) { grn_rc rc = GRN_SUCCESS; grn_hash_cursor *cursor; grn_hash *hash = grn_hash_create(ctx, NULL, sizeof(grn_ts_id), 0, GRN_OBJ_TABLE_HASH_KEY | GRN_HASH_TINY); if (!hash) { return GRN_INVALID_ARGUMENT; } grn_table_columns(ctx, table, str.ptr, str.size - 1, (grn_obj *)hash); if (ctx->rc != GRN_SUCCESS) { return ctx->rc; } cursor = grn_hash_cursor_open(ctx, hash, NULL, 0, NULL, 0, 0, -1, 0); if (!cursor) { rc = GRN_INVALID_ARGUMENT; } else { while (grn_hash_cursor_next(ctx, cursor) != GRN_ID_NIL) { char name_buf[GRN_TABLE_MAX_KEY_SIZE]; size_t name_size; grn_obj *column; grn_ts_id *column_id; if (!grn_hash_cursor_get_key(ctx, cursor, (void **)&column_id)) { rc = GRN_INVALID_ARGUMENT; break; } column = grn_ctx_at(ctx, *column_id); if (!column) { rc = GRN_INVALID_ARGUMENT; break; } name_size = grn_column_name(ctx, column, name_buf, sizeof(name_buf)); grn_obj_unlink(ctx, column); rc = grn_vector_add_element(ctx, &writer->name_buf, name_buf, name_size, 0, GRN_DB_TEXT); if (rc != GRN_SUCCESS) { break; } } grn_hash_cursor_close(ctx, cursor); } grn_hash_close(ctx, hash); return rc; }
/* * _object_ を追加し、 _n_arguments_ 個の引数を取る _operation_ を追加する。 * * @overload append_object(object, operation=Groonga::Operator::PUSH, n_arguments=1) * @param [Object] object 追加するオブジェクト * @param [Groonga::Operator::XXX] operation 追加する _operation_ * @param [Integer] n_arguments _operation_ の取る引数 * @return [Self] self * */ static VALUE rb_grn_expression_append_object (int argc, VALUE *argv, VALUE self) { VALUE rb_object, rb_operation, rb_n_arguments; grn_ctx *context = NULL; grn_obj *expression, *object; grn_operator operation = GRN_OP_PUSH; int n_arguments = 1; rb_scan_args(argc, argv, "12", &rb_object, &rb_operation, &rb_n_arguments); if (!NIL_P(rb_operation)) operation = RVAL2GRNOPERATOR(rb_operation); if (!NIL_P(rb_n_arguments)) n_arguments = NUM2INT(rb_n_arguments); rb_grn_expression_deconstruct(SELF(self), &expression, &context, NULL, NULL, NULL, NULL, NULL); if (RB_TYPE_P(rb_object, RUBY_T_HASH)) { RbGrnHashFromRubyHashData data; data.context = context; data.hash = grn_hash_create(context, NULL, GRN_TABLE_MAX_KEY_SIZE, sizeof(grn_obj), GRN_OBJ_KEY_VAR_SIZE | GRN_OBJ_TEMPORARY | GRN_HASH_TINY); grn_expr_take_obj(context, expression, (grn_obj *)(data.hash)); data.rb_hash = rb_object; rb_hash_foreach(rb_object, rb_grn_hash_from_ruby_hash_body, (VALUE)&data); grn_expr_append_obj(context, expression, (grn_obj *)(data.hash), operation, n_arguments); } else { object = RVAL2GRNOBJECT(rb_object, &context); grn_expr_append_obj(context, expression, object, operation, n_arguments); } rb_grn_context_check(context, self); rb_ary_push(rb_iv_get(self, "@objects"), rb_object); return self; }
static grn_rc grn_table_columns_inspect(grn_ctx *ctx, grn_obj *buf, grn_obj *obj) { grn_hash *cols; GRN_TEXT_PUTS(ctx, buf, "columns:["); if ((cols = grn_hash_create(ctx, NULL, sizeof(grn_id), 0, GRN_OBJ_TABLE_HASH_KEY|GRN_HASH_TINY))) { if (grn_table_columns(ctx, obj, "", 0, (grn_obj *)cols)) { int i = 0; grn_id *key; GRN_HASH_EACH(ctx, cols, id, &key, NULL, NULL, { grn_obj *col = grn_ctx_at(ctx, *key); if (col) { if (i++ > 0) { GRN_TEXT_PUTS(ctx, buf, ", "); } grn_column_name_(ctx, col, buf); grn_obj_unlink(ctx, col); } }); }
static void command_schema_table_output_columns(grn_ctx *ctx, grn_obj *table) { grn_hash *columns; columns = grn_hash_create(ctx, NULL, sizeof(grn_id), 0, GRN_OBJ_TABLE_HASH_KEY | GRN_HASH_TINY); if (!columns) { grn_ctx_output_map_open(ctx, "columns", 0); grn_ctx_output_map_close(ctx); return; } grn_table_columns(ctx, table, "", 0, (grn_obj *)columns); grn_ctx_output_map_open(ctx, "columns", grn_hash_size(ctx, columns)); { grn_id *key; GRN_HASH_EACH(ctx, columns, id, &key, NULL, NULL, { grn_obj *column; column = grn_ctx_at(ctx, *key); command_schema_column_output(ctx, table, column); }); }
static void grn_table_reindex(grn_ctx *ctx, grn_obj *table) { grn_hash *columns; columns = grn_hash_create(ctx, NULL, sizeof(grn_id), 0, GRN_OBJ_TABLE_HASH_KEY | GRN_HASH_TINY); if (!columns) { ERR(GRN_NO_MEMORY_AVAILABLE, "[table][reindex] failed to create a table to store columns"); return; } if (grn_table_columns(ctx, table, "", 0, (grn_obj *)columns) > 0) { grn_bool have_data_column = GRN_FALSE; grn_id *key; GRN_HASH_EACH(ctx, columns, id, &key, NULL, NULL, { grn_obj *column = grn_ctx_at(ctx, *key); if (column && column->header.type != GRN_COLUMN_INDEX) { have_data_column = GRN_TRUE; break; } });
void cut_setup() { hash = grn_hash_create(ctx, NULL, 1024, sizeof(grn_obj *), GRN_OBJ_KEY_VAR_SIZE); GRN_TEXT_INIT(&buffer, 0); }
static grn_rc grn_ctx_impl_init(grn_ctx *ctx) { grn_io_mapinfo mi; if (!(ctx->impl = grn_io_anon_map(ctx, &mi, IMPL_SIZE))) { return ctx->rc; } grn_alloc_init_ctx_impl(ctx); ctx->impl->encoding = ctx->encoding; ctx->impl->lifoseg = -1; ctx->impl->currseg = -1; CRITICAL_SECTION_INIT(ctx->impl->lock); if (!(ctx->impl->values = grn_array_create(ctx, NULL, sizeof(grn_db_obj *), GRN_ARRAY_TINY))) { CRITICAL_SECTION_FIN(ctx->impl->lock); grn_io_anon_unmap(ctx, &mi, IMPL_SIZE); ctx->impl = NULL; return ctx->rc; } if (!(ctx->impl->temporary_columns = grn_pat_create(ctx, NULL, GRN_TABLE_MAX_KEY_SIZE, sizeof(grn_obj *), 0))) { grn_array_close(ctx, ctx->impl->values); CRITICAL_SECTION_FIN(ctx->impl->lock); grn_io_anon_unmap(ctx, &mi, IMPL_SIZE); ctx->impl = NULL; return ctx->rc; } if (!(ctx->impl->ios = grn_hash_create(ctx, NULL, GRN_TABLE_MAX_KEY_SIZE, sizeof(grn_io *), GRN_OBJ_KEY_VAR_SIZE|GRN_HASH_TINY))) { grn_array_close(ctx, ctx->impl->values); grn_pat_close(ctx, ctx->impl->temporary_columns); CRITICAL_SECTION_FIN(ctx->impl->lock); grn_io_anon_unmap(ctx, &mi, IMPL_SIZE); ctx->impl = NULL; return ctx->rc; } ctx->impl->db = NULL; ctx->impl->expr_vars = grn_hash_create(ctx, NULL, sizeof(grn_id), sizeof(grn_obj *), 0); ctx->impl->stack_curr = 0; ctx->impl->curr_expr = NULL; ctx->impl->qe_next = NULL; GRN_TEXT_INIT(&ctx->impl->current_request_id, 0); ctx->impl->current_request_timer_id = NULL; ctx->impl->parser = NULL; GRN_TEXT_INIT(&ctx->impl->output.names, GRN_OBJ_VECTOR); GRN_UINT32_INIT(&ctx->impl->output.levels, GRN_OBJ_VECTOR); if (ctx == &grn_gctx) { ctx->impl->command_version = GRN_COMMAND_VERSION_STABLE; } else { ctx->impl->command_version = grn_get_default_command_version(); } if (ctx == &grn_gctx) { ctx->impl->match_escalation_threshold = GRN_DEFAULT_MATCH_ESCALATION_THRESHOLD; } else { ctx->impl->match_escalation_threshold = grn_get_default_match_escalation_threshold(); } ctx->impl->finalizer = NULL; ctx->impl->com = NULL; ctx->impl->output.buf = grn_obj_open(ctx, GRN_BULK, 0, GRN_DB_TEXT); ctx->impl->output.func = NULL; ctx->impl->output.data.ptr = NULL; #ifdef GRN_WITH_MESSAGE_PACK msgpack_packer_init(&ctx->impl->output.msgpacker, ctx, grn_msgpack_buffer_write); #endif ctx->impl->tv.tv_sec = 0; ctx->impl->tv.tv_nsec = 0; ctx->impl->edge = NULL; grn_loader_init(&ctx->impl->loader); ctx->impl->plugin_path = NULL; GRN_TEXT_INIT(&ctx->impl->query_log_buf, 0); ctx->impl->previous_errbuf[0] = '\0'; ctx->impl->n_same_error_messages = 0; grn_ctx_impl_mrb_init(ctx); return ctx->rc; }
static void exec_search(grn_ctx *ctx, grn_ii *i, grn_query *q, grn_cell *c, grn_hash *r, grn_operator op) { grn_hash *s; grn_cell *e, *ope = NIL; int n = *r->n_entries; grn_operator op0 = GRN_OP_OR, *opp = &op0, op1 = q->default_op; if (!n && op != GRN_OP_OR) { return; } if (n) { s = grn_hash_create(ctx, NULL, r->key_size, r->value_size, r->obj.header.flags); s->obj.header.impl_flags = 0; s->obj.header.domain = r->obj.header.domain; s->obj.range = r->obj.range; s->obj.max_n_subrecs = r->obj.max_n_subrecs; s->obj.subrec_size = r->obj.subrec_size; s->obj.subrec_offset = r->obj.subrec_offset; s->obj.id = r->obj.id; s->obj.db = r->obj.db; s->obj.source = r->obj.source; s->obj.source_size = r->obj.source_size; /* grn_hook_entry entry; for (entry = 0; entry < N_HOOK_ENTRIES; entry++) { s->obj.hooks[entry] = NULL; } */ } else { s = r; } while (c != NIL) { POP(e, c); switch (e->header.type) { case GRN_CELL_OP : if (opp == &op0 && e->u.op.op == GRN_OP_BUT) { POP(e, c); } else { ope = e; op1 = ope->u.op.op; } continue; case GRN_CELL_STR : if (ope != NIL) { q->opt.mode = ope->u.op.mode == -1 ? q->default_mode : ope->u.op.mode; q->opt.max_interval = q->opt.similarity_threshold = ope->u.op.option; if (!q->opt.weight_vector) { q->opt.vector_size = ope->u.op.weight + q->weight_offset; } if (ope->u.op.mode == GRN_OP_SIMILAR) { q->opt.max_interval = q->default_mode; } } else { q->opt.mode = q->default_mode; q->opt.max_interval = DEFAULT_MAX_INTERVAL; q->opt.similarity_threshold = DEFAULT_SIMILARITY_THRESHOLD; if (!q->opt.weight_vector) { q->opt.vector_size = DEFAULT_WEIGHT + q->weight_offset; } } if (grn_ii_select(ctx, i, e->u.b.value, e->u.b.size, s, *opp, &q->opt)) { GRN_LOG(ctx, GRN_LOG_ERROR, "grn_inv_select on exec_search failed !"); return; } break; case GRN_CELL_LIST : exec_search(ctx, i, q, e, s, *opp); break; default : GRN_LOG(ctx, GRN_LOG_NOTICE, "invalid object assigned in query (%d)", e->header.type); break; } opp = &op1; ope = NIL; op1 = q->default_op; } if (n) { grn_table_setoperation(ctx, (grn_obj *)r, (grn_obj *)s, (grn_obj *)r, op); grn_hash_close(ctx, s); } }
static int do_client() { int rc = -1; char *buf; grn_thread thread; struct timeval tvb, tve; grn_com_header sheader; grn_ctx ctx_, *ctx = &ctx_; grn_ctx_init(ctx, 0); GRN_COM_QUEUE_INIT(&fsessions); sessions = grn_hash_create(ctx, NULL, sizeof(grn_sock), sizeof(session), 0); sheader.proto = GRN_COM_PROTO_GQTP; sheader.qtype = 0; sheader.keylen = 0; sheader.level = 0; sheader.flags = 0; sheader.status = 0; sheader.opaque = 0; sheader.cas = 0; if ((buf = GRN_MALLOC(BUFSIZE))) { if (!grn_com_event_init(ctx, &ev, 1000, sizeof(grn_com))) { ev.msg_handler = msg_handler; if (!THREAD_CREATE(thread, receiver, NULL)) { int cnt = 0; gettimeofday(&tvb, NULL); lprint(ctx, "begin: max_concurrency=%d max_tp=%d", max_con, max_tp); while (fgets(buf, BUFSIZE, stdin)) { uint32_t size = strlen(buf) - 1; session *s = session_alloc(ctx, dests + (cnt++ % dest_cnt)); if (s) { gettimeofday(&s->tv, NULL); s->n_query++; s->query_id = ++nsent; s->n_sessions = (nsent - nrecv); switch (proto) { case 'H' : case 'h' : if (grn_com_send_text(ctx, s->com, buf, size, 0)) { fprintf(stderr, "grn_com_send_text failed\n"); } s->stat = 2; /* lprint(ctx, "sent %04d %04d %d", s->n_query, s->query_id, s->com->fd); */ break; default : if (grn_com_send(ctx, s->com, &sheader, buf, size, 0)) { fprintf(stderr, "grn_com_send failed\n"); } break; } } else { fprintf(stderr, "grn_com_copen failed\n"); } for (;;) { gettimeofday(&tve, NULL); if ((nrecv < max_tp * (tve.tv_sec - tvb.tv_sec)) && (nsent - nrecv) < max_con) { break; } /* lprint(ctx, "s:%d r:%d", nsent, nrecv); */ usleep(1000); } if (!(nsent % 1000)) { lprint(ctx, " : %d", nsent); } } done = 1; pthread_join(thread, NULL); gettimeofday(&tve, NULL); { double qps; uint64_t etime = (tve.tv_sec - tvb.tv_sec); etime *= 1000000; etime += (tve.tv_usec - tvb.tv_usec); qps = (double)nsent * 1000000 / etime; lprint(ctx, "end : n=%d min=%d max=%d avg=%d qps=%f etime=%d.%06d", nsent, etime_min, etime_max, (int)(etime_amount / nsent), qps, etime / 1000000, etime % 1000000); } { session *s; GRN_HASH_EACH(ctx, sessions, id, NULL, NULL, &s, { session_close(ctx, s); }); } rc = 0; } else {