grn_rc grn_plugin_close(grn_ctx *ctx, grn_id id) { grn_rc rc; grn_plugin *plugin; if (id == GRN_ID_NIL) { return GRN_INVALID_ARGUMENT; } CRITICAL_SECTION_ENTER(grn_plugins_lock); if (!grn_hash_get_value(&grn_gctx, grn_plugins, id, &plugin)) { rc = GRN_INVALID_ARGUMENT; goto exit; } if (--plugin->refcount) { rc = GRN_SUCCESS; goto exit; } if (plugin->dl) { grn_plugin_call_fin(ctx, id); if (!grn_dl_close(plugin->dl)) { const char *label; label = grn_dl_close_error_label(); SERR("%s", label); } } GRN_GFREE(plugin); rc = grn_hash_delete_by_id(&grn_gctx, grn_plugins, id, NULL); exit: CRITICAL_SECTION_LEAVE(grn_plugins_lock); return rc; }
void test_add_and_delete(gconstpointer data) { const grn_test_data *test_data = data; guint i; const guint n_operations = 7500; if (test_data->set_parameters) test_data->set_parameters(); cut_assert_create_hash(); ids = g_array_new(TRUE, TRUE, sizeof(grn_id)); for (i = 0; i < n_operations; i++) { if (grn_test_hash_factory_get_flags(factory) & GRN_OBJ_KEY_VAR_SIZE) key_size = strlen(test_data->key); cut_assert_lookup_add(test_data->key); test_data->increment((grn_test_data *)test_data); g_array_append_val(ids, id); } cut_assert_equal_int(n_operations, GRN_HASH_SIZE(hash)); for (i = 0; i < ids->len; i++) { grn_id delete_id; delete_id = g_array_index(ids, grn_id, i); cut_set_message("i = %d; id = %d", i, delete_id); grn_test_assert(grn_hash_delete_by_id(context, hash, delete_id, NULL)); } cut_assert_equal_int(0, GRN_HASH_SIZE(hash)); }
static grn_id grn_plugin_open_mrb(grn_ctx *ctx, const char *filename, size_t filename_size) { grn_id id = GRN_ID_NIL; grn_plugin **plugin = NULL; if (!ctx->impl->mrb.state) { ERR(GRN_FUNCTION_NOT_IMPLEMENTED, "mruby support isn't enabled"); return GRN_ID_NIL; } id = grn_hash_add(&grn_gctx, grn_plugins, filename, filename_size, (void **)&plugin, NULL); if (!id) { return id; } *plugin = GRN_GMALLOCN(grn_plugin, 1); if (!*plugin) { grn_hash_delete_by_id(&grn_gctx, grn_plugins, id, NULL); return GRN_ID_NIL; } grn_memcpy((*plugin)->path, filename, filename_size); (*plugin)->dl = NULL; (*plugin)->init_func = NULL; (*plugin)->register_func = NULL; (*plugin)->fin_func = NULL; (*plugin)->refcount = 1; return id; }
static void session_close(grn_ctx *ctx, session *s) { if (!s->stat) { return; } grn_com_close(ctx, s->com); s->stat = 0; grn_hash_delete_by_id(ctx, sessions, s->id, NULL); }
grn_id grn_plugin_open(grn_ctx *ctx, const char *filename) { grn_id id; grn_dl dl; grn_plugin **plugin = NULL; CRITICAL_SECTION_ENTER(grn_plugins_lock); if ((id = grn_hash_get(&grn_gctx, grn_plugins, filename, PATHLEN(filename), (void **)&plugin))) { (*plugin)->refcount++; goto exit; } if ((dl = grn_dl_open(filename))) { if ((id = grn_hash_add(&grn_gctx, grn_plugins, filename, PATHLEN(filename), (void **)&plugin, NULL))) { *plugin = GRN_GMALLOCN(grn_plugin, 1); if (*plugin) { if (grn_plugin_initialize(ctx, *plugin, dl, id, filename)) { GRN_GFREE(*plugin); *plugin = NULL; } } if (!*plugin) { grn_hash_delete_by_id(&grn_gctx, grn_plugins, id, NULL); if (grn_dl_close(dl)) { /* Now, __FILE__ set in plugin is invalid. */ ctx->errline = 0; ctx->errfile = NULL; } else { const char *label; label = grn_dl_close_error_label(); SERR(label); } id = GRN_ID_NIL; } else { (*plugin)->refcount = 1; } } else { if (!grn_dl_close(dl)) { const char *label; label = grn_dl_close_error_label(); SERR(label); } } } else { const char *label; label = grn_dl_open_error_label(); SERR(label); } exit: CRITICAL_SECTION_LEAVE(grn_plugins_lock); return id; }
grn_rc grn_plugin_close(grn_ctx *ctx, grn_id id) { grn_plugin *plugin; if (!grn_hash_get_value(ctx, grn_plugins, id, &plugin)) { return GRN_INVALID_ARGUMENT; } if (--plugin->refcount) { return GRN_SUCCESS; } grn_plugin_call_fin(ctx, id); if (!grn_dl_close(plugin->dl)) { const char *label; label = grn_dl_close_error_label; SERR(label); } GRN_GFREE(plugin); return grn_hash_delete_by_id(ctx, grn_plugins, id, NULL); }
bool mrn_hash_remove(grn_ctx *ctx, grn_hash *hash, const char *key) { bool succeed; grn_rc rc; grn_id id; id = grn_hash_get(ctx, hash, (const char*) key, strlen(key), NULL); if (id == GRN_ID_NIL) { GRN_LOG(ctx, GRN_LOG_WARNING, "hash remove not found (key=%s)", key); succeed = false; } else { rc = grn_hash_delete_by_id(ctx, hash, id, NULL); if (rc != GRN_SUCCESS) { GRN_LOG(ctx, GRN_LOG_ERROR, "hash remove error (key=%s)", key); succeed = false; } else { GRN_LOG(ctx, GRN_LOG_DEBUG, "hash remove (key=%s)", key); succeed = true; } } return succeed; }
grn_rc grn_com_event_del(grn_ctx *ctx, grn_com_event *ev, grn_sock fd) { if (!ev) { return GRN_INVALID_ARGUMENT; } { grn_com *c; grn_id id = grn_hash_get(ctx, ev->hash, &fd, sizeof(grn_sock), (void **)&c); if (id) { #ifdef USE_EPOLL if (!c->closed) { struct epoll_event e; memset(&e, 0, sizeof(struct epoll_event)); e.data.fd = fd; e.events = c->events; if (epoll_ctl(ev->epfd, EPOLL_CTL_DEL, fd, &e) == -1) { SERR("epoll_ctl"); return ctx->rc; } } #endif /* USE_EPOLL*/ #ifdef USE_KQUEUE struct kevent e; EV_SET(&e, (fd), c->events, EV_DELETE, 0, 0, NULL); if (kevent(ev->kqfd, &e, 1, NULL, 0, NULL) == -1) { SERR("kevent"); return ctx->rc; } #endif /* USE_KQUEUE */ return grn_hash_delete_by_id(ctx, ev->hash, id, NULL); } else { GRN_LOG(ctx, GRN_LOG_ERROR, "%04x| fd(%" GRN_FMT_SOCKET ") not found in ev(%p)", getpid(), fd, ev); return GRN_INVALID_ARGUMENT; } } }
grn_id grn_plugin_open(grn_ctx *ctx, const char *filename) { grn_id id = GRN_ID_NIL; grn_dl dl; grn_plugin **plugin = NULL; size_t filename_size; filename_size = GRN_PLUGIN_KEY_SIZE(filename); CRITICAL_SECTION_ENTER(grn_plugins_lock); if ((id = grn_hash_get(&grn_gctx, grn_plugins, filename, filename_size, (void **)&plugin))) { (*plugin)->refcount++; goto exit; } #ifdef GRN_WITH_MRUBY { const char *mrb_suffix; mrb_suffix = grn_plugin_get_ruby_suffix(); if (filename_size > strlen(mrb_suffix) && strcmp(filename + (strlen(filename) - strlen(mrb_suffix)), mrb_suffix) == 0) { id = grn_plugin_open_mrb(ctx, filename, filename_size); goto exit; } } #endif /* GRN_WITH_MRUBY */ if ((dl = grn_dl_open(filename))) { if ((id = grn_hash_add(&grn_gctx, grn_plugins, filename, filename_size, (void **)&plugin, NULL))) { *plugin = GRN_GMALLOCN(grn_plugin, 1); if (*plugin) { grn_memcpy((*plugin)->path, filename, filename_size); if (grn_plugin_initialize(ctx, *plugin, dl, id, filename)) { GRN_GFREE(*plugin); *plugin = NULL; } } if (!*plugin) { grn_hash_delete_by_id(&grn_gctx, grn_plugins, id, NULL); if (grn_dl_close(dl)) { /* Now, __FILE__ set in plugin is invalid. */ ctx->errline = 0; ctx->errfile = NULL; } else { const char *label; label = grn_dl_close_error_label(); SERR("%s", label); } id = GRN_ID_NIL; } else { (*plugin)->refcount = 1; } } else { if (!grn_dl_close(dl)) { const char *label; label = grn_dl_close_error_label(); SERR("%s", label); } } } else { const char *label; label = grn_dl_open_error_label(); SERR("%s", label); } exit: CRITICAL_SECTION_LEAVE(grn_plugins_lock); return id; }