Kouhei Sutou
null+****@clear*****
Mon Apr 2 18:37:32 JST 2018
Kouhei Sutou 2018-04-02 18:37:32 +0900 (Mon, 02 Apr 2018) New Revision: 685c1612fae2e9a72e07042bf16f184ddc4ebc5b https://github.com/groonga/groonga/commit/685c1612fae2e9a72e07042bf16f184ddc4ebc5b Message: Add grn_option It's for keeping option values for tokenizers, normalizers and so on. Added files: include/groonga/option.h lib/grn_options.h lib/options.c Modified files: include/groonga.h include/groonga/Makefile.am lib/c_sources.am Modified: include/groonga.h (+1 -0) =================================================================== --- include/groonga.h 2018-04-02 18:03:17 +0900 (689010d75) +++ include/groonga.h 2018-04-02 18:37:32 +0900 (d984ab232) @@ -39,6 +39,7 @@ #include "groonga/ii.h" #include "groonga/obj.h" #include "groonga/operator.h" +#include "groonga/option.h" #include "groonga/output.h" #include "groonga/pat.h" #include "groonga/request_canceler.h" Modified: include/groonga/Makefile.am (+1 -0) =================================================================== --- include/groonga/Makefile.am 2018-04-02 18:03:17 +0900 (4442937c7) +++ include/groonga/Makefile.am 2018-04-02 18:37:32 +0900 (3c5018fd8) @@ -21,6 +21,7 @@ groonga_include_HEADERS = \ ii.h \ obj.h \ operator.h \ + option.h \ output.h \ pat.h \ plugin.h \ Added: include/groonga/option.h (+59 -0) 100644 =================================================================== --- /dev/null +++ include/groonga/option.h 2018-04-02 18:37:32 +0900 (f3f9ffecd) @@ -0,0 +1,59 @@ +/* -*- c-basic-offset: 2 -*- */ +/* + Copyright(C) 2018 Brazil + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License version 2.1 as published by the Free Software Foundation. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#pragma once + +#include <groonga.h> + +#ifdef __cplusplus +extern "C" { +#endif + +#define GRN_OPTION_VALUES_EACH_BEGIN(ctx, \ + option_values, \ + i, \ + name, \ + name_size) do { \ + grn_ctx *ctx_ = (ctx); \ + grn_obj *option_values_ = (option_values); \ + unsigned int i_, n_; \ + \ + n_ = grn_vector_size(ctx_, option_values_); \ + for (i_ = 0; i_ < n_; i_++) { \ + unsigned int i = i_ + 1; \ + const char *name; \ + unsigned int name_size; \ + grn_id name_domain_; \ + \ + name_size = grn_vector_get_element(ctx_, \ + option_values_, \ + i_, \ + &name, \ + NULL, \ + &name_domain_); \ + if (!grn_type_id_is_text_family(ctx_, name_domain_)) { \ + continue; \ + } + +#define GRN_OPTION_VALUES_EACH_END() \ + } \ +} while (GRN_FALSE) + +#ifdef __cplusplus +} +#endif Modified: lib/c_sources.am (+2 -0) =================================================================== --- lib/c_sources.am 2018-04-02 18:03:17 +0900 (3ccbb58d7) +++ lib/c_sources.am 2018-04-02 18:37:32 +0900 (090e174f7) @@ -59,6 +59,8 @@ libgroonga_c_sources = \ obj.c \ grn_obj.h \ operator.c \ + options.c \ + grn_options.h \ output.c \ grn_output.h \ pat.c \ Added: lib/grn_options.h (+54 -0) 100644 =================================================================== --- /dev/null +++ lib/grn_options.h 2018-04-02 18:37:32 +0900 (bab2f1911) @@ -0,0 +1,54 @@ +/* -*- c-basic-offset: 2 -*- */ +/* + Copyright(C) 2018 Brazil + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License version 2.1 as published by the Free Software Foundation. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#pragma once + +#include "grn.h" + +#ifdef __cplusplus +extern "C" { +#endif + +typedef struct _grn_options grn_options; + +grn_options *grn_options_create(grn_ctx *ctx, + const char *path, + const char *context_tag); +grn_options *grn_options_open(grn_ctx *ctx, + const char *path, + const char *context_tag); +grn_rc grn_options_close(grn_ctx *ctx, grn_options *options); +grn_rc grn_options_remove(grn_ctx *ctx, const char *path); + +grn_bool grn_options_is_locked(grn_ctx *ctx, grn_options *options); +grn_rc grn_options_clear_lock(grn_ctx *ctx, grn_options *options); +grn_bool grn_options_is_corrupt(grn_ctx *ctx, grn_options *options); +grn_rc grn_options_flush(grn_ctx *ctx, grn_options *options); + +grn_rc grn_options_set(grn_ctx *ctx, + grn_options *options, + grn_id id, + grn_obj *values); +grn_bool grn_options_get(grn_ctx *ctx, + grn_options *options, + grn_id id, + grn_obj *values); + +#ifdef __cplusplus +} +#endif Added: lib/options.c (+201 -0) 100644 =================================================================== --- /dev/null +++ lib/options.c 2018-04-02 18:37:32 +0900 (3f069bc23) @@ -0,0 +1,201 @@ +/* -*- c-basic-offset: 2 -*- */ +/* + Copyright(C) 2018 Brazil + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License version 2.1 as published by the Free Software Foundation. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA +*/ + +#include "grn_options.h" +#include "grn_db.h" +#include "grn_util.h" + +#include <stdio.h> + +struct _grn_options { + grn_ja *values; +}; + +static const char *GRN_OPTIONS_PATH_FORMAT = "%s.options"; +static const unsigned int GRN_OPTIONS_MAX_VALUE_SIZE = 65536; + +grn_options * +grn_options_create(grn_ctx *ctx, + const char *path, + const char *context_tag) +{ + char *options_path; + char options_path_buffer[PATH_MAX]; + grn_options *options; + uint32_t flags = 0; + + if (path) { + grn_snprintf(options_path_buffer, + PATH_MAX, + PATH_MAX, + GRN_OPTIONS_PATH_FORMAT, + path); + options_path = options_path_buffer; + } else { + options_path = NULL; + } + + options = GRN_MALLOC(sizeof(grn_options)); + if (!options) { + ERR(GRN_NO_MEMORY_AVAILABLE, + "%s failed to allocate memory for options: <%s>", + context_tag, + options_path ? options_path : "(temporary)"); + return NULL; + } + + options->values = grn_ja_create(ctx, + options_path, + GRN_OPTIONS_MAX_VALUE_SIZE, + flags); + if (!options->values) { + GRN_FREE(options); + ERR(GRN_NO_MEMORY_AVAILABLE, + "%s failed to create data store for options: <%s>", + context_tag, + options_path ? options_path : "(temporary)"); + return NULL; + } + + return options; +} + +grn_options * +grn_options_open(grn_ctx *ctx, + const char *path, + const char *context_tag) +{ + char options_path[PATH_MAX]; + grn_options *options; + + grn_snprintf(options_path, + PATH_MAX, + PATH_MAX, + GRN_OPTIONS_PATH_FORMAT, + path); + if (!grn_path_exist(options_path)) { + return grn_options_create(ctx, path, context_tag); + } + + options = GRN_MALLOC(sizeof(grn_options)); + if (!options) { + ERR(GRN_NO_MEMORY_AVAILABLE, + "%s failed to allocate memory for options: <%s>", + context_tag, + options_path); + return NULL; + } + + options->values = grn_ja_open(ctx, options_path); + if (!options->values) { + GRN_FREE(options); + ERR(GRN_NO_MEMORY_AVAILABLE, + "%s failed to open data store for options: <%s>", + context_tag, + options_path); + return NULL; + } + + return options; +} + +grn_rc +grn_options_close(grn_ctx *ctx, grn_options *options) +{ + grn_rc rc; + + if (!options) { + return GRN_SUCCESS; + } + + rc = grn_ja_close(ctx, options->values); + GRN_FREE(options); + + return rc; +} + +grn_rc +grn_options_remove(grn_ctx *ctx, const char *path) +{ + char options_path[PATH_MAX]; + + grn_snprintf(options_path, + PATH_MAX, + PATH_MAX, + GRN_OPTIONS_PATH_FORMAT, + path); + return grn_ja_remove(ctx, options_path); +} + +grn_bool +grn_options_is_locked(grn_ctx *ctx, grn_options *options) +{ + return grn_obj_is_locked(ctx, (grn_obj *)(options->values)); +} + +grn_rc +grn_options_clear_lock(grn_ctx *ctx, grn_options *options) +{ + return grn_obj_clear_lock(ctx, (grn_obj *)(options->values)); +} + +grn_bool +grn_options_is_corrupt(grn_ctx *ctx, grn_options *options) +{ + return grn_obj_is_corrupt(ctx, (grn_obj *)(options->values)); +} + +grn_rc +grn_options_flush(grn_ctx *ctx, grn_options *options) +{ + return grn_obj_flush(ctx, (grn_obj *)(options->values)); +} + +grn_rc +grn_options_set(grn_ctx *ctx, + grn_options *options, + grn_id id, + grn_obj *values) +{ + return grn_ja_putv(ctx, options->values, id, values, 0); +} + +grn_bool +grn_options_get(grn_ctx *ctx, + grn_options *options, + grn_id id, + grn_obj *value) +{ + grn_io_win iw; + void *raw_value; + uint32_t length; + + raw_value = grn_ja_ref(ctx, options->values, id, &iw, &length); + if (!raw_value) { + return GRN_FALSE; + } + + if (value->header.type != GRN_VECTOR) { + grn_obj_reinit(ctx, value, value->header.domain, GRN_OBJ_VECTOR); + } + grn_vector_decode(ctx, value, raw_value, length); + + grn_ja_unref(ctx, &iw); + + return GRN_TRUE; +} -------------- next part -------------- HTML����������������������������... URL: https://lists.osdn.me/mailman/archives/groonga-commit/attachments/20180402/42de1030/attachment-0001.htm