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"); } }
grn_id grn_dat_add(grn_ctx *ctx, grn_dat *dat, const void *key, unsigned int key_size, void **, int *added) { if (!key_size) { return GRN_ID_NIL; } else if (!grn_dat_open_trie_if_needed(ctx, dat)) { return GRN_ID_NIL; } if (!dat->trie) { char trie_path[PATH_MAX]; grn_dat_generate_trie_path(grn_io_path(dat->io), trie_path, 1); grn::dat::Trie * const new_trie = new (std::nothrow) grn::dat::Trie; if (!new_trie) { MERR("new grn::dat::Trie failed"); return GRN_ID_NIL; } try { new_trie->create(trie_path); } catch (const grn::dat::Exception &ex) { ERR(grn_dat_translate_error_code(ex.code()), "grn::dat::Trie::create failed"); delete new_trie; return GRN_ID_NIL; } dat->trie = new_trie; dat->file_id = dat->header->file_id = 1; } grn::dat::Trie * const trie = static_cast<grn::dat::Trie *>(dat->trie); try { grn::dat::UInt32 key_pos; const bool res = trie->insert(key, key_size, &key_pos); if (added) { *added = res ? 1 : 0; } return trie->get_key(key_pos).id(); } catch (const grn::dat::SizeError &ex) { if (!grn_dat_rebuild_trie(ctx, dat)) { return GRN_ID_NIL; } grn::dat::Trie * const new_trie = static_cast<grn::dat::Trie *>(dat->trie); grn::dat::UInt32 key_pos; const bool res = new_trie->insert(key, key_size, &key_pos); if (added) { *added = res ? 1 : 0; } return new_trie->get_key(key_pos).id(); } catch (const grn::dat::Exception &ex) { ERR(grn_dat_translate_error_code(ex.code()), "grn::dat::Trie::insert failed"); return GRN_ID_NIL; } }