Ejemplo n.º 1
0
/*
 * 既存のデータベースを開く。ブロックを指定した場合はブロッ
 * クに開いたデータベースを渡し、ブロックを抜けるときに閉じ
 * る。
 *
 * @overload new(path, options=nil)
 *   @!macro [new] database.new.arguments
 *     @param options [::Hash] The name and value
 *       pairs. Omitted names are initialized as the default value.
 *     @option options :context (Groonga::Context.default)
 *       データベースを結びつけるコンテキスト。省略すると
 *       {Groonga::Context.default} を利用する。
 *   @!macro database.new.arguments
 *   @return [Groonga::Database]
 * @overload new(path, options=nil)
 *   @!macro database.new.arguments
 *   @yield [database]
 *   @yieldparam [Groonga::Database] database 開いたデータベース
 */
static VALUE
rb_grn_database_initialize (int argc, VALUE *argv, VALUE self)
{
    grn_ctx *context;
    grn_obj *old_database, *database;
    const char *path;
    VALUE rb_path, options, rb_context;

    rb_scan_args(argc, argv, "11", &rb_path, &options);

    path = StringValuePtr(rb_path);
    rb_grn_scan_options(options,
                        "context", &rb_context,
                        NULL);

    context = rb_grn_context_ensure(&rb_context);

    old_database = grn_ctx_db(context);
    if (old_database)
        grn_obj_unlink(context, old_database);
    reset_floating_objects(rb_context);
    database = grn_db_open(context, path);
    rb_grn_object_assign(Qnil, self, rb_context, context, database);
    rb_grn_context_check(context, self);

    rb_iv_set(self, "@context", rb_context);
    if (!NIL_P(rb_context))
        rb_iv_set(rb_context, "database", self);

    return Qnil;
}
Ejemplo n.º 2
0
void
test_get_persistent_object_from_opened_database(void)
{
  const gchar table_name[] = "Users";
  const gchar *path;

  path = cut_build_path(tmp_directory, "database.groonga", NULL);
  database = grn_db_create(context, path, NULL);
  grn_test_assert_not_null(context, database);
  grn_test_assert_not_null(context,
                           grn_table_create(context,
                                            table_name,
                                            strlen(table_name),
                                            NULL,
                                            GRN_OBJ_TABLE_HASH_KEY |
                                            GRN_OBJ_PERSISTENT,
                                            grn_ctx_at(context, GRN_DB_UINT32),
                                            NULL));

  database2 = grn_db_open(context2, path);
  grn_test_assert_not_null(context2, database2);
  grn_test_assert_not_null(context2,
                           grn_ctx_get(context2,
                                       table_name,
                                       strlen(table_name)));
}
Ejemplo n.º 3
0
static int
run(grn_ctx *ctx, const char *db_path, const char *ruby_script_path)
{
  grn_obj *db;

  db = grn_db_open(ctx, db_path);
  if (!db) {
    if (ctx->rc == GRN_NO_SUCH_FILE_OR_DIRECTORY) {
      db = grn_db_create(ctx, db_path, NULL);
      if (!db) {
        fprintf(stderr, "Failed to create database: <%s>: %s",
                db_path, ctx->errbuf);
        return EXIT_FAILURE;
      }
    } else {
      fprintf(stderr, "Failed to open database: <%s>: %s",
              db_path, ctx->errbuf);
      return EXIT_FAILURE;
    }
  }

  grn_mrb_load(ctx, ruby_script_path);
  if (ctx->rc != GRN_SUCCESS) {
      fprintf(stderr, "Failed to load Ruby script: <%s>: %s",
              ruby_script_path, ctx->errbuf);
  }

  grn_obj_close(ctx, db);

  if (ctx->rc == GRN_SUCCESS) {
    return EXIT_SUCCESS;
  } else {
    return EXIT_FAILURE;
  }
}
Ejemplo n.º 4
0
void
test_recreate_temporary_object_on_opened_database(void)
{
  const gchar table_name[] = "<users>";
  const gchar *path;

  path = cut_build_path(tmp_directory, "database.groonga", NULL);
  database = grn_db_create(context, path, NULL);
  grn_test_assert_not_null(context, database);
  grn_test_assert_null(context,
                           grn_table_create(context,
                                            table_name,
                                            strlen(table_name),
                                            NULL,
                                            GRN_OBJ_TABLE_HASH_KEY,
                                            grn_ctx_at(context, GRN_DB_UINT32),
                                            NULL));

  database2 = grn_db_open(context2, path);
  grn_test_assert_not_null(context2, database2);
  grn_test_assert_null(context2,
                       grn_ctx_get(context2,
                                   table_name,
                                   strlen(table_name)));
  grn_test_assert_null(context2,
                           grn_table_create(context,
                                            table_name,
                                            strlen(table_name),
                                            NULL,
                                            GRN_OBJ_TABLE_HASH_KEY,
                                            grn_ctx_at(context, GRN_DB_UINT32),
                                            NULL));
}
Ejemplo n.º 5
0
static void
ngx_http_groonga_open_database_callback(ngx_http_groonga_loc_conf_t *location_conf,
                                        void *user_data)
{
  ngx_http_groonga_database_callback_data_t *data = user_data;
  grn_ctx *context;

  context = &(location_conf->context);
  data->rc = ngx_http_groonga_context_init(context, location_conf,
                                           data->pool, data->log);
  if (data->rc != NGX_OK) {
    return;
  }

  if (!location_conf->database_path.data) {
    ngx_log_error(NGX_LOG_EMERG, data->log, 0,
                  "%s: \"groonga_database\" must be specified in block at %s:%d",
                  location_conf->name,
                  location_conf->config_file,
                  location_conf->config_line);
    data->rc = NGX_ERROR;
    return;
  }

  if (!location_conf->database_path_cstr) {
    location_conf->database_path_cstr =
      ngx_str_null_terminate(data->pool, &(location_conf->database_path));
  }

  grn_db_open(context, location_conf->database_path_cstr);
  if (context->rc != GRN_SUCCESS) {
    if (location_conf->database_auto_create) {
      ngx_http_groonga_create_database(location_conf, data);
    } else {
      ngx_log_error(NGX_LOG_EMERG, data->log, 0,
                    "failed to open groonga database: %s",
                    context->errbuf);
      data->rc = NGX_ERROR;
      return;
    }
  }

  location_conf->cache = grn_cache_open(context);
  if (!location_conf->cache) {
    ngx_log_error(NGX_LOG_EMERG, data->log, 0,
                  "failed to open groonga cache: %s",
                  context->errbuf);
    data->rc = NGX_ERROR;
    return;
  }
  if (location_conf->cache_limit != NGX_CONF_UNSET_SIZE) {
    grn_cache_set_max_n_entries(context,
                                location_conf->cache,
                                location_conf->cache_limit);
  }
}
Ejemplo n.º 6
0
static void
setup_database(BenchmarkData *data)
{
  grn_ctx *context = &(data->context);
  gchar *tmp_dir;
  gchar *database_last_component_name;
  gchar *database_path;
  guint i;

  tmp_dir = get_tmp_dir();
  database_last_component_name =
    g_strdup_printf("db-%d-%s-mruby",
                    data->n_records,
                    data->use_mruby ? "with" : "without");
  database_path = g_build_filename(tmp_dir,
                                   "range-select",
                                   database_last_component_name,
                                   NULL);
  g_free(database_last_component_name);

  if (g_file_test(database_path, G_FILE_TEST_EXISTS)) {
    data->database = grn_db_open(context, database_path);
    run_command(context, "dump");
  } else {
    data->database = grn_db_create(context, database_path, NULL);

    run_command(context, "table_create Entries TABLE_NO_KEY");
    run_command(context, "column_create Entries rank COLUMN_SCALAR Int32");
    run_command(context, "table_create Ranks TABLE_PAT_KEY Int32");
    run_command(context,
                "column_create Ranks entries_rank COLUMN_INDEX Entries rank");

    run_command(context, "load --table Entries");
    run_command(context, "[");
    for (i = 0; i < data->n_records; i++) {
#define BUFFER_SIZE 4096
      gchar buffer[BUFFER_SIZE];
      const gchar *separator;
      if (i == (data->n_records - 1)) {
        separator = "";
      } else {
        separator = ",";
      }
      snprintf(buffer, BUFFER_SIZE, "{\"rank\": %u}%s", i, separator);
      run_command(context, buffer);
#undef BUFFER_SIZE
    }
    run_command(context, "]");
  }

  g_free(database_path);
}
Ejemplo n.º 7
0
Archivo: server.c Proyecto: kou/hog
void* server(void *arg)
{
    int loop = 1;
    char num_handlers = sizeof(cmd_handlers)/sizeof(cmd_handlers[0]);
    server_t *s = (server_t*)arg;
    fprintf(stdout, "connection opening: %d\n", s->socket);
    hog_t *hog = s->hog;
    grn_ctx ctx;
    grn_ctx_init(&ctx, 0);
    // open db
    grn_obj *db = grn_db_open(&ctx, hog->db_path);
    // setup socket
    struct timeval to;
    to.tv_sec = 10;
    to.tv_usec = 0;
    //setsockopt(s->socket, SOL_SOCKET, SO_RCVTIMEO, (char*)&to, sizeof(to));
    setsockopt(s->socket, SOL_SOCKET, SO_SNDTIMEO, (char*)&to, sizeof(to));
    // send the command list
    if(submit(s->socket, &num_handlers, 1) != 0){
        fprintf(stderr, "Failed to send num handlers\n");
    }
    for(char i = 0; i < num_handlers; ++i){
        const char *name = cmd_handlers[i].name;
        if(submit_chunk(s->socket, name) != 0){
            fprintf(stderr, "Failed to send cmd (%d) %s\n", i, name);
        }
    }
    while(loop){
        unsigned char cmd;
        if(receive(s->socket, &cmd, 1) != 0){
            switch(errno){
            case 0: case EBADF: case ENOENT: case EAGAIN: break;
            case ETIMEDOUT: continue;
            default:
                fprintf(stderr, "Failed to recv cmd: %s\n", strerror(errno));
                break;
            }
            loop = 0;
            break;
        }
        if(cmd < num_handlers) (cmd_handlers[cmd].handler)(s, &ctx);
        else fprintf(stderr, "Invalid cmd: %d\n", cmd);
    }
cleanup:
    fprintf(stdout, "connection closing: %d\n", s->socket);
    GRN_OBJ_FIN(&ctx, db);
    grn_ctx_fin(&ctx);
    close(s->socket);
    free(s);
    return NULL;
}
Ejemplo n.º 8
0
void
test_expire_cache_on_recreate(void)
{
  const gchar *path;

  path = cut_build_path(tmp_directory, "database.groonga", NULL);
  database = grn_db_create(context, path, NULL);
  assert_send_command("table_create Sites 0 ShortText");
  assert_send_command("load '[[\"_key\"],[\"groonga.org\"]]' Sites");
  cut_assert_equal_string("[[[1],[[\"_key\",\"ShortText\"]],[\"groonga.org\"]]]",
                          send_command("select Sites --output_columns _key"));
  assert_send_command("table_remove Sites");
  grn_obj_close(context, database);

  database = grn_db_open(context, path);
  assert_send_command("table_create Sites 0 ShortText");
  cut_assert_equal_string("[[[0],[[\"_key\",\"ShortText\"]]]]",
                          send_command("select Sites --output_columns _key"));
}
Ejemplo n.º 9
0
static void
ngx_http_groonga_open_database_callback(ngx_http_groonga_loc_conf_t *location_conf,
                                        void *user_data)
{
  ngx_http_groonga_database_callback_data_t *data = user_data;
  grn_ctx *context;

  context = &(location_conf->context);
  grn_ctx_init(context, GRN_NO_FLAGS);

  if (!location_conf->database_path.data) {
    ngx_log_error(NGX_LOG_EMERG, data->log, 0,
                  "%s: \"groonga_database\" must be specified in block at %s:%d",
                  location_conf->name,
                  location_conf->config_file,
                  location_conf->config_line);
    data->rc = NGX_ERROR;
    return;
  }

  if (!location_conf->database_path_cstr) {
    location_conf->database_path_cstr =
      ngx_str_null_terminate(data->pool, &(location_conf->database_path));
  }

  grn_db_open(context, location_conf->database_path_cstr);
  if (context->rc == GRN_SUCCESS) {
    return;
  }

  if (location_conf->database_auto_create) {
    ngx_http_groonga_create_database(location_conf, data);
  } else {
    ngx_log_error(NGX_LOG_EMERG, data->log, 0,
                  "failed to open groonga database: %s",
                  context->errbuf);
    data->rc = NGX_ERROR;
  }
}
Ejemplo n.º 10
0
int
main(int argc, char **argv)
{
  int port_no = DEFAULT_PORT, daemon = 0;
  const char *max_threads_string = NULL, *port_string = NULL;
  const char *address;
  const char *send_endpoint = NULL, *recv_endpoint = NULL, *log_base_path = NULL;
  int n_processed_args, flags = RUN_MODE_ENABLE_MAX_FD_CHECK;
  run_mode mode = run_mode_none;

  if (!(default_max_threads = get_core_number())) {
    default_max_threads = DEFAULT_MAX_THREADS;
  }

  /* parse options */
  {
    static grn_str_getopt_opt opts[] = {
      {'c', NULL, NULL, 0, getopt_op_none}, /* deprecated */
      {'t', "n-threads", NULL, 0, getopt_op_none},
      {'h', "help", NULL, run_mode_usage, getopt_op_update},
      {'p', "port", NULL, 0, getopt_op_none},
      {'\0', "bind-address", NULL, 0, getopt_op_none}, /* not supported yet */
      {'s', "send-endpoint", NULL, 0, getopt_op_none},
      {'r', "receive-endpoint", NULL, 0, getopt_op_none},
      {'l', "log-base-path", NULL, 0, getopt_op_none},
      {'d', "daemon", NULL, run_mode_daemon, getopt_op_update},
      {'\0', "disable-max-fd-check", NULL, RUN_MODE_ENABLE_MAX_FD_CHECK,
       getopt_op_off},
      {'\0', NULL, NULL, 0, 0}
    };
    opts[0].arg = &max_threads_string;
    opts[1].arg = &max_threads_string;
    opts[3].arg = &port_string;
    opts[4].arg = &address;
    opts[5].arg = &send_endpoint;
    opts[6].arg = &recv_endpoint;
    opts[7].arg = &log_base_path;

    n_processed_args = grn_str_getopt(argc, argv, opts, &flags);
  }

  /* main */
  mode = (flags & RUN_MODE_MASK);
  if (n_processed_args < 0 ||
      (argc - n_processed_args) != 1 ||
      mode == run_mode_error) {
    usage(stderr);
    return EXIT_FAILURE;
  } else if (mode == run_mode_usage) {
    usage(stdout);
    return EXIT_SUCCESS;
  } else {
    grn_ctx ctx;
    void *zmq_ctx;
    int max_threads;

    if (max_threads_string) {
      max_threads = atoi(max_threads_string);
      if (max_threads > MAX_THREADS) {
        print_error("too many threads. limit to %d.", MAX_THREADS);
        max_threads = MAX_THREADS;
      }
    } else {
      max_threads = default_max_threads;
    }

    if (port_string) {
      port_no = atoi(port_string);
    }

    if (flags & RUN_MODE_ENABLE_MAX_FD_CHECK) {
      /* check environment */
      struct rlimit rlim;
      if (!getrlimit(RLIMIT_NOFILE, &rlim)) {
        if (rlim.rlim_max < MIN_MAX_FDS) {
          print_error("too small max fds. %d required.", MIN_MAX_FDS);
          return -1;
        }
        rlim.rlim_cur = rlim.rlim_cur;
        setrlimit(RLIMIT_NOFILE, &rlim);
      }
    }

    if (mode == run_mode_daemon) {
      daemonize();
    }

    grn_init();
    grn_ctx_init(&ctx, 0);
    if ((db = grn_db_open(&ctx, argv[n_processed_args]))) {
      if ((zmq_ctx = zmq_init(1))) {
        signal(SIGTERM, signal_handler);
        signal(SIGINT, signal_handler);
        signal(SIGQUIT, signal_handler);

        serve_threads(max_threads, port_no, argv[n_processed_args], zmq_ctx,
                      send_endpoint, recv_endpoint, log_base_path);
        zmq_term(zmq_ctx);
      } else {
        print_error("cannot create zmq context.");
      }
      grn_obj_close(&ctx, db);
    } else {
      print_error("cannot open db.");
    }
    grn_ctx_fin(&ctx);
    grn_fin();
  }
  return 0;
}
Ejemplo n.º 11
0
MRN_API my_bool mroonga_command_init(UDF_INIT *initid, UDF_ARGS *args,
                                     char *message)
{
  CommandInfo *info = NULL;

  initid->ptr = NULL;
  if (args->arg_count != 1) {
    sprintf(message,
            "mroonga_command(): Incorrect number of arguments: %u for 1",
            args->arg_count);
    goto error;
  }
  if (args->arg_type[0] != STRING_RESULT) {
    strcpy(message,
           "mroonga_command(): The 1st argument must be command as string");
    goto error;
  }
  initid->maybe_null = 1;
  initid->const_item = 1;

  info = (CommandInfo *)my_malloc(sizeof(CommandInfo),
                                  MYF(MY_WME | MY_ZEROFILL));
  if (!info) {
    strcpy(message, "mroonga_command(): out of memory");
    goto error;
  }

  grn_ctx_init(&(info->ctx), 0);
  {
    const char *current_db_path = current_thd->db;
    const char *action;
    if (current_db_path) {
      action = "open database";
      mrn::PathMapper mapper(current_db_path);
      grn_db_open(&(info->ctx), mapper.db_path());
    } else {
      action = "create anonymous database";
      grn_db_create(&(info->ctx), NULL, NULL);
    }
    if (info->ctx.rc != GRN_SUCCESS) {
      sprintf(message,
              "mroonga_command(): failed to %s: %s",
              action,
              info->ctx.errbuf);
      goto error;
    }
  }

  initid->ptr = (char *)info;

  return FALSE;

error:
  if (info) {
    grn_obj *db;
    db = grn_ctx_db(&(info->ctx));
    if (db) {
      grn_obj_close(&(info->ctx), db);
    }
    grn_ctx_fin(&(info->ctx));
    my_free(info, MYF(0));
  }
  return TRUE;
}
Ejemplo n.º 12
0
Archivo: kv.c Proyecto: darashi/groonga
int
main(int argc, char **argv)
{
  int r, op = 'p', method = 'c';
  const char *path;
  if (argc < 2) {
    fprintf(stderr, "usage: kv dbpath [put|get] [col|table|hash|pat|ql] [value_size]\n");
    return -1;
  }
  path = *argv[1] ? argv[1] : NULL;
  if (argc > 2) { op = *argv[2]; }
  if (argc > 3) { method = *argv[3]; }
  if (argc > 4) { value_size = atoi(argv[4]); }
  if (argc > 5) { nloops = atoi(argv[5]); }
  if (grn_init()) {
    fprintf(stderr, "grn_init() failed\n");
    return -1;
  }
  if (grn_ctx_init(&ctx, (method == 'q' ? GRN_CTX_USE_QL|GRN_CTX_BATCH_MODE : 0))) {
    fprintf(stderr, "grn_ctx_init() failed\n");
    return -1;
  }
  srand(0);
  if (method == 'h' || method == 'p') {
    switch (method) {
    case 'h' :
      r = (op == 'p') ? hash_put(path) : hash_get(path);
      break;
    case 'p' :
      r = (op == 'p') ? pat_put(path) : pat_get(path);
      break;
    default :
      r = -1;
      fprintf(stderr, "invalid method\n");
      break;
    }
  } else {
    if (path) { db = grn_db_open(&ctx, path); }
    if (!db) { db = grn_db_create(&ctx, path, NULL); }
    if (!db) {
      fprintf(stderr, "db initialize failed\n");
      return -1;
    }
    value_type = grn_type_create(&ctx, "<value_type>", 12, 0, value_size);
    switch (method) {
    case 'q' :
      r = (op == 'p') ? ql_put() : ql_get();
      break;
    case 'c' :
      r = (op == 'p') ? column_put() : column_get();
      break;
    case 't' :
      r = (op == 'p') ? table_put() : table_get();
      //r = (op == 'p') ? table_put_allocate() : table_get();
      //r = (op == 'p') ? table_put2() : table_get();
      break;
    default :
      r = -1;
      fprintf(stderr, "invalid method\n");
      break;
    }
    if (grn_obj_close(&ctx, db)) {
      fprintf(stderr, "grn_obj_close() failed\n");
      return -1;
    }
  }
  if (grn_ctx_fin(&ctx)) {
    fprintf(stderr, "grn_ctx_fin() failed\n");
    return -1;
  }
  if (grn_fin()) {
    fprintf(stderr, "grn_fin() failed\n");
    return -1;
  }
  return r;
}
Ejemplo n.º 13
0
void
test_persistent_expr(void)
{
  int i;
  grn_obj *t1, *t2, *c1, *c2, buf;
  t1 = grn_table_create(&context, "t1", 2, NULL,
                        GRN_OBJ_TABLE_NO_KEY|GRN_OBJ_PERSISTENT, NULL, 0);
  cut_assert_not_null(t1);
  t2 = grn_table_create(&context, "t2", 2, NULL,
                        GRN_OBJ_TABLE_NO_KEY|GRN_OBJ_PERSISTENT, NULL, 0);
  cut_assert_not_null(t2);
  c1 = grn_column_create(&context, t1, "c1", 2, NULL,
                         GRN_OBJ_PERSISTENT, t2);
  cut_assert_not_null(c1);
  c2 = grn_column_create(&context, t2, "c2", 2, NULL,
                         GRN_OBJ_PERSISTENT, t1);
  cut_assert_not_null(c2);
  GRN_TEXT_INIT(&buf, 0);
  for (i = 0; i < NRECORDS; i++) {
    grn_id i1, i2;
    i1 = grn_table_add(&context, t1, NULL, 0, NULL);
    i2 = grn_table_add(&context, t2, NULL, 0, NULL);
    GRN_BULK_REWIND(&buf);
    grn_bulk_write(&context, &buf, (char *)&i2, sizeof(grn_id));
    grn_obj_set_value(&context, c1, i1, &buf, GRN_OBJ_SET);
    grn_obj_set_value(&context, c2, i2, &buf, GRN_OBJ_SET);
  }
  {
    grn_obj *expr = grn_expr_create(&context, "test", 4);
    grn_obj *v;
    cut_assert_not_null(expr);
    v = grn_expr_add_var(&context, expr, "foo", 3);
    GRN_RECORD_INIT(v, 0, grn_obj_id(&context, t1));
    grn_expr_append_obj(&context, expr, v);

    GRN_TEXT_SETS(&context, &buf, "c1");
    grn_expr_append_const(&context, expr, &buf);
    grn_expr_append_op(&context, expr, GRN_OP_OBJ_GET_VALUE, 2);
    GRN_TEXT_SETS(&context, &buf, "c2");
    grn_expr_append_const(&context, expr, &buf);
    grn_expr_append_op(&context, expr, GRN_OP_OBJ_GET_VALUE, 2);
    GRN_TEXT_SETS(&context, &buf, "c1");
    grn_expr_append_const(&context, expr, &buf);

    /*
    GRN_TEXT_SETS(&context, &buf, "c1.c2.c1");
    grn_expr_append_const(&context, expr, &buf);
    */

    grn_expr_append_op(&context, expr, GRN_OP_OBJ_GET_VALUE, 2);
    grn_expr_compile(&context, expr);
  }
  cut_assert_equal_uint(0, grn_obj_close(&context, &buf));

  grn_db_close(&context, database);
  database = grn_db_open(&context, path);

  GRN_TEXT_INIT(&buf, 0);

  {
    grn_id id;
    uint64_t et;
    int nerr = 0;
    grn_obj *r, *v;
    grn_table_cursor *tc;
    struct timeval tvb, tve;
    grn_obj *expr = grn_ctx_get(&context, "test", 4);
    v = grn_expr_get_var(&context, expr, "foo", 3);
    t1 = grn_ctx_get(&context, "t1", 2);
    tc = grn_table_cursor_open(&context, t1, NULL, 0, NULL, 0, 0);
    cut_assert_not_null(tc);
    gettimeofday(&tvb, NULL);
    while ((id = grn_table_cursor_next(&context, tc))) {
      GRN_RECORD_SET(&context, v, id);
      r = grn_expr_exec(&context, expr);
      if (GRN_RECORD_VALUE(r) != id) { nerr++; }
    }
    gettimeofday(&tve, NULL);
    et = (tve.tv_sec - tvb.tv_sec) * 1000000 + (tve.tv_usec - tvb.tv_usec);
    // printf("et=%zu\n", et);
    cut_assert_equal_uint(0, nerr);
    cut_assert_equal_uint(0, grn_table_cursor_close(&context, tc));
  }
  cut_assert_equal_uint(0, grn_obj_close(&context, &buf));
}
Ejemplo n.º 14
0
int
main(int argc, char **argv)
{
  const char *db_path;
  const char *dataset_name;
  grn_ctx ctx_, *ctx;
  grn_obj *db;
  grn_bool success = GRN_TRUE;
  int parsed_argc, rest_argc;
  int flags = MODE_NONE;
  const char *default_tokenizer = NULL;
  static grn_str_getopt_opt opts[] = {
    {'\0', "default-tokenizer", NULL, 0, GETOPT_OP_NONE},
    {'h', "help", NULL, MODE_USAGE, GETOPT_OP_UPDATE}
  };

  opts[0].arg = &default_tokenizer;

  parsed_argc = grn_str_getopt(argc, argv, opts, &flags);
  if (parsed_argc < 0) {
    usage(stderr, argc, argv);
    return EXIT_FAILURE;
  }

  if (flags & MODE_USAGE) {
    usage(stdout, argc, argv);
    return EXIT_SUCCESS;
  }

  rest_argc = argc - parsed_argc;
  if (rest_argc != 2) {
    usage(stderr, argc, argv);
    return EXIT_FAILURE;
  }

  db_path = argv[parsed_argc];
  dataset_name = argv[parsed_argc + 1];

  grn_init();

  ctx = &ctx_;
  grn_ctx_init(ctx, 0);
  db = grn_db_open(ctx, db_path);
  if (!db) {
    if (ctx->rc == GRN_NO_SUCH_FILE_OR_DIRECTORY) {
      db = grn_db_create(ctx, db_path, NULL);
      if (!db) {
        fprintf(stderr, "DB create failed (%s): %s\n", db_path, ctx->errbuf);
      }
    } else {
      fprintf(stderr, "DB open failed (%s): %s\n", db_path, ctx->errbuf);
    }
  }

  if (db) {
    grn_obj text;
    GRN_TEXT_INIT(&text, 0);
#define SEND(string) send_command(ctx, &text, string, dataset_name)
    SEND("register suggest/suggest");
    SEND("table_create event_type TABLE_HASH_KEY ShortText");
    {
      grn_obj query;
      GRN_TEXT_INIT(&query, 0);
      GRN_TEXT_PUTS(ctx, &query,
                    "table_create bigram TABLE_PAT_KEY ShortText "
                    "--default_tokenizer ");
      if (default_tokenizer) {
        GRN_TEXT_PUTS(ctx, &query, default_tokenizer);
      } else {
        GRN_TEXT_PUTS(ctx, &query, DEFAULT_DEFAULT_TOKENIZER);
      }
      GRN_TEXT_PUTS(ctx, &query, " --normalizer NormalizerAuto");
      GRN_TEXT_PUTC(ctx, &query, '\0');
      SEND(GRN_TEXT_VALUE(&query));
      GRN_OBJ_FIN(ctx, &query);
    }
    SEND("table_create kana TABLE_PAT_KEY ShortText "
         "--normalizer NormalizerAuto");
    SEND("table_create item_${DATASET} TABLE_PAT_KEY "
         "ShortText --default_tokenizer TokenDelimit "
         "--normalizer NormalizerAuto");
    SEND("column_create bigram item_${DATASET}_key "
         "COLUMN_INDEX|WITH_POSITION item_${DATASET} _key");
    SEND("column_create item_${DATASET} kana COLUMN_VECTOR kana");
    SEND("column_create kana item_${DATASET}_kana COLUMN_INDEX "
         "item_${DATASET} kana");
    SEND("column_create item_${DATASET} freq COLUMN_SCALAR Int32");
    SEND("column_create item_${DATASET} last COLUMN_SCALAR Time");
    SEND("column_create item_${DATASET} boost COLUMN_SCALAR Int32");
    SEND("column_create item_${DATASET} freq2 COLUMN_SCALAR Int32");
    SEND("column_create item_${DATASET} buzz COLUMN_SCALAR Int32");

    SEND("table_create pair_${DATASET} TABLE_HASH_KEY UInt64");
    SEND("column_create pair_${DATASET} pre COLUMN_SCALAR item_${DATASET}");
    SEND("column_create pair_${DATASET} post COLUMN_SCALAR item_${DATASET}");
    SEND("column_create pair_${DATASET} freq0 COLUMN_SCALAR Int32");
    SEND("column_create pair_${DATASET} freq1 COLUMN_SCALAR Int32");
    SEND("column_create pair_${DATASET} freq2 COLUMN_SCALAR Int32");
    SEND("column_create item_${DATASET} co COLUMN_INDEX pair_${DATASET} pre");

    SEND("table_create sequence_${DATASET} TABLE_HASH_KEY ShortText");
    SEND("table_create event_${DATASET} TABLE_NO_KEY");
    SEND("column_create sequence_${DATASET} events "
         "COLUMN_VECTOR|RING_BUFFER event_${DATASET}");
    SEND("column_create event_${DATASET} type COLUMN_SCALAR event_type");
    SEND("column_create event_${DATASET} time COLUMN_SCALAR Time");
    SEND("column_create event_${DATASET} item COLUMN_SCALAR item_${DATASET}");
    SEND("column_create event_${DATASET} sequence COLUMN_SCALAR "
         "sequence_${DATASET}");

    SEND("table_create configuration TABLE_HASH_KEY ShortText");
    SEND("column_create configuration weight COLUMN_SCALAR UInt32");
    SEND("load --table configuration");
    SEND("[");
    SEND("{\"_key\": \"${DATASET}\", \"weight\": 1}");
    SEND("]");
#undef SEND
    success = ctx->rc == GRN_SUCCESS;
    GRN_OBJ_FIN(ctx, &text);
    GRN_OBJ_FIN(ctx, db);
  } else {
    success = GRN_FALSE;
  }
  grn_ctx_fin(ctx);
  grn_fin();

  return success ? EXIT_SUCCESS : EXIT_FAILURE;
}
Ejemplo n.º 15
0
int
main(int argc, char **argv)
{
  int port_no = DEFAULT_PORT, daemon = 0;
  const char *send_endpoint = NULL, *recv_endpoint = NULL, *log_path = NULL;

  /* check environment */
  {
    struct rlimit rlim;
    if (!getrlimit(RLIMIT_NOFILE, &rlim)) {
      if (rlim.rlim_max < MIN_MAX_FDS) {
        print_error("too small max fds. %d required.", MIN_MAX_FDS);
        return -1;
      }
      rlim.rlim_cur = rlim.rlim_cur;
      setrlimit(RLIMIT_NOFILE, &rlim);
    }
  }
  if (!(default_max_threads = get_core_number())) {
    default_max_threads = DEFAULT_MAX_THREADS;
  }

  /* parse options */
  {
    int ch;

    while ((ch = getopt(argc, argv, "c:p:s:r:l:d")) != -1) {
      switch(ch) {
      case 'c':
        default_max_threads = atoi(optarg);
        if (default_max_threads > MAX_THREADS) {
          print_error("too many threads. limit to %d.", MAX_THREADS);
          default_max_threads = MAX_THREADS;
        }
        break;
      case 'p':
        port_no = atoi(optarg);
        break;
      case 's':
        send_endpoint = optarg;
        break;
      case 'r':
        recv_endpoint = optarg;
        break;
      case 'l':
        log_path = optarg;
        break;
      case 'd':
        daemon = 1;
        break;
      }
    }
    argc -= optind; argv += optind;
  }

  /* main */
  if (argc != 1) {
    usage(stderr);
  } else {
    grn_ctx ctx;
    void *zmq_ctx;

    if (daemon) {
      daemonize();
    }

    grn_init();
    grn_ctx_init(&ctx, 0);
    if ((db = grn_db_open(&ctx, argv[0]))) {
      if ((zmq_ctx = zmq_init(1))) {
        signal(SIGTERM, signal_handler);
        signal(SIGINT, signal_handler);
        signal(SIGQUIT, signal_handler);

        serve_threads(default_max_threads, port_no, argv[0], zmq_ctx,
          send_endpoint, recv_endpoint, log_path);
        zmq_term(zmq_ctx);
      } else {
        print_error("cannot create zmq context.");
      }
      grn_obj_close(&ctx, db);
    } else {
      print_error("cannot open db.");
    }
    grn_ctx_fin(&ctx);
    grn_fin();
  }
  return 0;
}
Ejemplo n.º 16
0
int
main(int argc, char **argv)
{
    const char *db_path;
    const char *dataset_name;
    grn_ctx ctx_, *ctx;
    grn_obj *db;
    grn_bool success = GRN_TRUE;

    if (argc != 3) {
        usage(stderr, argc, argv);
        return(EXIT_FAILURE);
    }

    db_path = argv[1];
    dataset_name = argv[2];

    grn_init();

    ctx = &ctx_;
    grn_ctx_init(ctx, 0);
    if (access(db_path, F_OK) == 0) {
        db = grn_db_open(ctx, db_path);
        if (!db) {
            fprintf(stderr, "DB open failed (%s): %s\n", db_path, ctx->errbuf);
        }
    } else {
        db = grn_db_create(ctx, db_path, NULL);
        if (!db) {
            fprintf(stderr, "DB create failed (%s): %s\n", db_path, ctx->errbuf);
        }
    }

    if (db) {
        grn_obj text;
        GRN_TEXT_INIT(&text, 0);
#define SEND(string) send_command(ctx, &text, string, dataset_name)
        SEND("register suggest/suggest");
        SEND("table_create event_type TABLE_HASH_KEY ShortText");
        SEND("table_create bigram TABLE_PAT_KEY|KEY_NORMALIZE ShortText "
             "--default_tokenizer TokenBigram");
        SEND("table_create kana TABLE_PAT_KEY|KEY_NORMALIZE ShortText");
        SEND("table_create item_${DATASET} TABLE_PAT_KEY|KEY_NORMALIZE "
             "ShortText --default_tokenizer TokenDelimit");
        SEND("column_create bigram item_${DATASET}_key "
             "COLUMN_INDEX|WITH_POSITION item_${DATASET} _key");
        SEND("column_create item_${DATASET} kana COLUMN_VECTOR kana");
        SEND("column_create kana item_${DATASET}_kana COLUMN_INDEX "
             "item_${DATASET} kana");
        SEND("column_create item_${DATASET} freq COLUMN_SCALAR Int32");
        SEND("column_create item_${DATASET} last COLUMN_SCALAR Time");
        SEND("column_create item_${DATASET} boost COLUMN_SCALAR Int32");
        SEND("column_create item_${DATASET} freq2 COLUMN_SCALAR Int32");
        SEND("column_create item_${DATASET} buzz COLUMN_SCALAR Int32");

        SEND("table_create pair_${DATASET} TABLE_HASH_KEY UInt64");
        SEND("column_create pair_${DATASET} pre COLUMN_SCALAR item_${DATASET}");
        SEND("column_create pair_${DATASET} post COLUMN_SCALAR item_${DATASET}");
        SEND("column_create pair_${DATASET} freq0 COLUMN_SCALAR Int32");
        SEND("column_create pair_${DATASET} freq1 COLUMN_SCALAR Int32");
        SEND("column_create pair_${DATASET} freq2 COLUMN_SCALAR Int32");
        SEND("column_create item_${DATASET} co COLUMN_INDEX pair_${DATASET} pre");

        SEND("table_create sequence_${DATASET} TABLE_HASH_KEY ShortText");
        SEND("table_create event_${DATASET} TABLE_NO_KEY");
        SEND("column_create sequence_${DATASET} events "
             "COLUMN_VECTOR|RING_BUFFER event_${DATASET}");
        SEND("column_create event_${DATASET} type COLUMN_SCALAR event_type");
        SEND("column_create event_${DATASET} time COLUMN_SCALAR Time");
        SEND("column_create event_${DATASET} item COLUMN_SCALAR item_${DATASET}");
        SEND("column_create event_${DATASET} sequence COLUMN_SCALAR "
             "sequence_${DATASET}");
#undef SEND
        success = ctx->rc == GRN_SUCCESS;
        GRN_OBJ_FIN(ctx, &text);
        GRN_OBJ_FIN(ctx, db);
    } else {
        success = GRN_FALSE;
    }
    grn_ctx_fin(ctx);
    grn_fin();

    return success ? EXIT_SUCCESS : EXIT_FAILURE;
}