| 1 | /* |
| 2 | Copyright (C) 2010-2015 Brazil |
| 3 | Copyright (C) 2022 Sutou Kouhei <[email protected]> |
| 4 | |
| 5 | This library is free software; you can redistribute it and/or |
| 6 | modify it under the terms of the GNU Lesser General Public |
| 7 | License as published by the Free Software Foundation; either |
| 8 | version 2.1 of the License, or (at your option) any later version. |
| 9 | |
| 10 | This library is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | Lesser General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU Lesser General Public |
| 16 | License along with this library; if not, write to the Free Software |
| 17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | */ |
| 19 | |
| 20 | /* For grn_str_getopt() */ |
| 21 | #include <grn_str.h>cxx . . $ `<file>/lib/grn_str.h`/ |
| 22 | |
| 23 | #include <stdio.h> |
| 24 | #include <stdlib.h> |
| 25 | #include <string.h> |
| 26 | #include <groonga.h>cxx . . $ `<file>/include/groonga.h`/ |
| 27 | |
| 28 | typedef enumcxx . . $ $anonymous_type_3c700a4db5f87a2b_0# { |
| 29 | MODE_NONEcxx . . $ MODE_NONE., |
| 30 | MODE_USAGEcxx . . $ MODE_USAGE. |
| 31 | } ModeFlagscxx . . $ ModeFlags#; |
| 32 | |
| 33 | static const char *DEFAULT_DEFAULT_TOKENIZERcxx . . $ DEFAULT_DEFAULT_TOKENIZER. = "TokenBigram"; |
| 34 | |
| 35 | static void |
| 36 | usagecxx . . $ usage(afa9166f821cbfff).(FILEcxx . . $ FILE# *output, int argc, char **argv) |
| 37 | { |
| 38 | #define OUTPUTcxx . . $ `src/suggest/groonga_suggest_create_dataset.c:38:9`!(...) fprintf(output, __VA_ARGS__) |
| 39 | |
| 40 | OUTPUTcxx . . $ `src/suggest/groonga_suggest_create_dataset.c:38:9`!("Usage: %s [OPTIONS] DB_PATH DATASET_NAME\n", argv[0]); |
| 41 | OUTPUTcxx . . $ `src/suggest/groonga_suggest_create_dataset.c:38:9`!(" e.g.: %s /tmp/db shops\n", argv[0]); |
| 42 | OUTPUTcxx . . $ `src/suggest/groonga_suggest_create_dataset.c:38:9`!("\n"); |
| 43 | OUTPUTcxx . . $ `src/suggest/groonga_suggest_create_dataset.c:38:9`!("Options:\n"); |
| 44 | OUTPUTcxx . . $ `src/suggest/groonga_suggest_create_dataset.c:38:9`!(" --default-tokenizer=TOKENIZER Use TOKENIZER as the default\n"); |
| 45 | OUTPUTcxx . . $ `src/suggest/groonga_suggest_create_dataset.c:38:9`!(" tokenizer for item name\n"); |
| 46 | OUTPUTcxx . . $ `src/suggest/groonga_suggest_create_dataset.c:38:9`!(" (default: %s)\n", |
| 47 | DEFAULT_DEFAULT_TOKENIZER); |
| 48 | OUTPUTcxx . . $ `src/suggest/groonga_suggest_create_dataset.c:38:9`!(" -h, --help Show this message and exit\n"); |
| 49 | |
| 50 | #undef OUTPUTcxx . . $ `src/suggest/groonga_suggest_create_dataset.c:38:9`! |
| 51 | } |
| 52 | |
| 53 | static void |
| 54 | outputcxx . . $ output(b578cd6e53d5fe91).(grn_ctxcxx . . $ grn_ctx# *ctx) |
| 55 | { |
| 56 | int flags = 0; |
| 57 | char *str; |
| 58 | unsigned int str_len; |
| 59 | |
| 60 | do { |
| 61 | grn_ctx_recvcxx . . $ grn_ctx_recv(8b5d6c8a2a7bb976).(ctx, &str, &str_len, &flags); |
| 62 | if (str_len > 0 || ctx->rccxx . . $ _grn_ctx#rc.) { |
| 63 | if (ctx->rccxx . . $ _grn_ctx#rc.) { |
| 64 | printfcxx . . $ printf(581fddd72a8db8fc).("ERROR (%d): %s\n", ctx->rccxx . . $ _grn_ctx#rc., ctx->errbufcxx . . $ _grn_ctx#errbuf.); |
| 65 | } |
| 66 | if (str_len > 0) { |
| 67 | printfcxx . . $ printf(581fddd72a8db8fc).("%.*s\n", str_len, str); |
| 68 | } |
| 69 | } |
| 70 | } while (flags & GRN_CTX_MOREcxx . . $ `include/groonga/groonga.h:3293:9`!); |
| 71 | } |
| 72 | |
| 73 | static void |
| 74 | send_commandcxx . . $ send_command(5a6327189c7fed1).(grn_ctxcxx . . $ grn_ctx# *ctx, grn_objcxx . . $ grn_obj# *buffer, const char *command, |
| 75 | const char *dataset_name) |
| 76 | { |
| 77 | const char *p = command; |
| 78 | const char *dataset_place_holder = "${DATASET}"; |
| 79 | char *dataset_place_holder_position; |
| 80 | |
| 81 | if (ctx->rccxx . . $ _grn_ctx#rc. != GRN_SUCCESScxx . . $ GRN_SUCCESS.) { |
| 82 | return; |
| 83 | } |
| 84 | |
| 85 | GRN_BULK_REWINDcxx . . $ `include/groonga/groonga.h:2599:9`!(buffer); |
| 86 | while ((dataset_place_holder_position = strstrcxx . . $ `<external>/13506128340496143423/string.h:380:11`!(p, dataset_place_holder))) { |
| 87 | GRN_TEXT_PUTcxx . . $ `include/groonga/groonga.h:2773:9`!(ctx, buffer, p, dataset_place_holder_position - p); |
| 88 | GRN_TEXT_PUTScxx . . $ `include/groonga/groonga.h:2781:9`!(ctx, buffer, dataset_name); |
| 89 | p = dataset_place_holder_position + strlencxx . . $ strlen(751346f8406fd082).(dataset_place_holder); |
| 90 | } |
| 91 | GRN_TEXT_PUTScxx . . $ `include/groonga/groonga.h:2781:9`!(ctx, buffer, p); |
| 92 | printfcxx . . $ printf(581fddd72a8db8fc).("> %.*s\n", (int)GRN_TEXT_LENcxx . . $ `include/groonga/groonga.h:2786:9`!(buffer), GRN_TEXT_VALUEcxx . . $ `include/groonga/groonga.h:2785:9`!(buffer)); |
| 93 | grn_ctx_sendcxx . . $ grn_ctx_send(dac8ec282379d9e0).(ctx, |
| 94 | GRN_TEXT_VALUEcxx . . $ `include/groonga/groonga.h:2785:9`!(buffer), |
| 95 | (unsigned int)GRN_TEXT_LENcxx . . $ `include/groonga/groonga.h:2786:9`!(buffer), |
| 96 | 0); |
| 97 | outputcxx . . $ output(b578cd6e53d5fe91).(ctx); |
| 98 | } |
| 99 | |
| 100 | |
| 101 | int |
| 102 | maincxx . . $ main(afcd3168fc1188a4).(int argc, char **argv) |
| 103 | { |
| 104 | const char *db_path; |
| 105 | const char *dataset_name; |
| 106 | grn_ctxcxx . . $ grn_ctx# ctx_, *ctx; |
| 107 | grn_objcxx . . $ grn_obj# *db; |
| 108 | boolcxx . . $ `<external>/13015445228558014418/stdbool.h:36:9`! success = truecxx . . $ `<external>/13015445228558014418/stdbool.h:37:9`!; |
| 109 | int parsed_argc, rest_argc; |
| 110 | int flags = MODE_NONEcxx . . $ MODE_NONE.; |
| 111 | const char *default_tokenizer = NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!; |
| 112 | static grn_str_getopt_optcxx . . $ grn_str_getopt_opt# opts[] = { |
| 113 | {'\0', "default-tokenizer", NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!, 0, GETOPT_OP_NONEcxx . . $ GETOPT_OP_NONE.}, |
| 114 | {'h', "help", NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!, MODE_USAGEcxx . . $ MODE_USAGE., GETOPT_OP_UPDATEcxx . . $ GETOPT_OP_UPDATE.} |
| 115 | }; |
| 116 | |
| 117 | opts[0].argcxx . . $ $anonymous_type_946308ef283cea1e_1#arg. = &default_tokenizer; |
| 118 | |
| 119 | parsed_argc = grn_str_getoptcxx . . $ grn_str_getopt(1f77e49aad5fba88).(argc, argv, opts, &flags); |
| 120 | if (parsed_argc < 0) { |
| 121 | usagecxx . . $ usage(afa9166f821cbfff).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, argc, argv); |
| 122 | return EXIT_FAILUREcxx . . $ `<external>/10696550936717951914/stdlib.h:96:9`!; |
| 123 | } |
| 124 | |
| 125 | if (flags & MODE_USAGEcxx . . $ MODE_USAGE.) { |
| 126 | usagecxx . . $ usage(afa9166f821cbfff).(stdoutcxx . . $ `<external>/232824719582829767/stdio.h:158:9`!, argc, argv); |
| 127 | return EXIT_SUCCESScxx . . $ `<external>/10696550936717951914/stdlib.h:97:9`!; |
| 128 | } |
| 129 | |
| 130 | rest_argc = argc - parsed_argc; |
| 131 | if (rest_argc != 2) { |
| 132 | usagecxx . . $ usage(afa9166f821cbfff).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, argc, argv); |
| 133 | return EXIT_FAILUREcxx . . $ `<external>/10696550936717951914/stdlib.h:96:9`!; |
| 134 | } |
| 135 | |
| 136 | db_path = argv[parsed_argc]; |
| 137 | dataset_name = argv[parsed_argc + 1]; |
| 138 | |
| 139 | grn_initcxx . . $ grn_init(d5bb96df31a8101).(); |
| 140 | |
| 141 | ctx = &ctx_; |
| 142 | grn_ctx_initcxx . . $ grn_ctx_init(670fdc69e3f5dbb5).(ctx, 0); |
| 143 | db = grn_db_opencxx . . $ grn_db_open(7a18fc36feec5011).(ctx, db_path); |
| 144 | if (!db) { |
| 145 | if (ctx->rccxx . . $ _grn_ctx#rc. == GRN_NO_SUCH_FILE_OR_DIRECTORYcxx . . $ GRN_NO_SUCH_FILE_OR_DIRECTORY.) { |
| 146 | db = grn_db_createcxx . . $ grn_db_create(d9bca101602a0e9b).(ctx, db_path, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!); |
| 147 | if (!db) { |
| 148 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, "DB create failed (%s): %s\n", db_path, ctx->errbufcxx . . $ _grn_ctx#errbuf.); |
| 149 | } |
| 150 | } else { |
| 151 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, "DB open failed (%s): %s\n", db_path, ctx->errbufcxx . . $ _grn_ctx#errbuf.); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | if (db) { |
| 156 | grn_objcxx . . $ grn_obj# text; |
| 157 | GRN_TEXT_INITcxx . . $ `include/groonga/groonga.h:2750:9`!(&text, 0); |
| 158 | #define SENDcxx . . $ `src/suggest/groonga_suggest_create_dataset.c:158:9`!(string) send_command(ctx, &text, string, dataset_name) |
| 159 | SENDcxx . . $ `src/suggest/groonga_suggest_create_dataset.c:158:9`!("plugin_register suggest/suggest"); |
| 160 | SENDcxx . . $ `src/suggest/groonga_suggest_create_dataset.c:158:9`!("table_create event_type TABLE_HASH_KEY ShortText"); |
| 161 | { |
| 162 | grn_objcxx . . $ grn_obj# query; |
| 163 | GRN_TEXT_INITcxx . . $ `include/groonga/groonga.h:2750:9`!(&query, 0); |
| 164 | GRN_TEXT_PUTScxx . . $ `include/groonga/groonga.h:2781:9`!(ctx, &query, |
| 165 | "table_create bigram TABLE_PAT_KEY ShortText " |
| 166 | "--default_tokenizer "); |
| 167 | if (default_tokenizer) { |
| 168 | GRN_TEXT_PUTScxx . . $ `include/groonga/groonga.h:2781:9`!(ctx, &query, default_tokenizer); |
| 169 | } else { |
| 170 | GRN_TEXT_PUTScxx . . $ `include/groonga/groonga.h:2781:9`!(ctx, &query, DEFAULT_DEFAULT_TOKENIZER); |
| 171 | } |
| 172 | GRN_TEXT_PUTScxx . . $ `include/groonga/groonga.h:2781:9`!(ctx, &query, " --normalizer NormalizerAuto"); |
| 173 | GRN_TEXT_PUTCcxx . . $ `include/groonga/groonga.h:2775:9`!(ctx, &query, '\0'); |
| 174 | SENDcxx . . $ `src/suggest/groonga_suggest_create_dataset.c:158:9`!(GRN_TEXT_VALUEcxx . . $ `include/groonga/groonga.h:2785:9`!(&query)); |
| 175 | GRN_OBJ_FINcxx . . $ `include/groonga/groonga.h:958:9`!(ctx, &query); |
| 176 | } |
| 177 | SENDcxx . . $ `src/suggest/groonga_suggest_create_dataset.c:158:9`!("table_create kana TABLE_PAT_KEY ShortText " |
| 178 | "--normalizer NormalizerAuto"); |
| 179 | SENDcxx . . $ `src/suggest/groonga_suggest_create_dataset.c:158:9`!("table_create item_${DATASET} TABLE_PAT_KEY " |
| 180 | "ShortText --default_tokenizer TokenDelimit " |
| 181 | "--normalizer NormalizerAuto"); |
| 182 | SENDcxx . . $ `src/suggest/groonga_suggest_create_dataset.c:158:9`!("column_create bigram item_${DATASET}_key " |
| 183 | "COLUMN_INDEX|WITH_POSITION item_${DATASET} _key"); |
| 184 | SENDcxx . . $ `src/suggest/groonga_suggest_create_dataset.c:158:9`!("column_create item_${DATASET} kana COLUMN_VECTOR kana"); |
| 185 | SENDcxx . . $ `src/suggest/groonga_suggest_create_dataset.c:158:9`!("column_create kana item_${DATASET}_kana COLUMN_INDEX " |
| 186 | "item_${DATASET} kana"); |
| 187 | SENDcxx . . $ `src/suggest/groonga_suggest_create_dataset.c:158:9`!("column_create item_${DATASET} freq COLUMN_SCALAR Int32"); |
| 188 | SENDcxx . . $ `src/suggest/groonga_suggest_create_dataset.c:158:9`!("column_create item_${DATASET} last COLUMN_SCALAR Time"); |
| 189 | SENDcxx . . $ `src/suggest/groonga_suggest_create_dataset.c:158:9`!("column_create item_${DATASET} boost COLUMN_SCALAR Int32"); |
| 190 | SENDcxx . . $ `src/suggest/groonga_suggest_create_dataset.c:158:9`!("column_create item_${DATASET} freq2 COLUMN_SCALAR Int32"); |
| 191 | SENDcxx . . $ `src/suggest/groonga_suggest_create_dataset.c:158:9`!("column_create item_${DATASET} buzz COLUMN_SCALAR Int32"); |
| 192 | |
| 193 | SENDcxx . . $ `src/suggest/groonga_suggest_create_dataset.c:158:9`!("table_create pair_${DATASET} TABLE_HASH_KEY UInt64"); |
| 194 | SENDcxx . . $ `src/suggest/groonga_suggest_create_dataset.c:158:9`!("column_create pair_${DATASET} pre COLUMN_SCALAR item_${DATASET}"); |
| 195 | SENDcxx . . $ `src/suggest/groonga_suggest_create_dataset.c:158:9`!("column_create pair_${DATASET} post COLUMN_SCALAR item_${DATASET}"); |
| 196 | SENDcxx . . $ `src/suggest/groonga_suggest_create_dataset.c:158:9`!("column_create pair_${DATASET} freq0 COLUMN_SCALAR Int32"); |
| 197 | SENDcxx . . $ `src/suggest/groonga_suggest_create_dataset.c:158:9`!("column_create pair_${DATASET} freq1 COLUMN_SCALAR Int32"); |
| 198 | SENDcxx . . $ `src/suggest/groonga_suggest_create_dataset.c:158:9`!("column_create pair_${DATASET} freq2 COLUMN_SCALAR Int32"); |
| 199 | SENDcxx . . $ `src/suggest/groonga_suggest_create_dataset.c:158:9`!("column_create item_${DATASET} co COLUMN_INDEX pair_${DATASET} pre"); |
| 200 | |
| 201 | SENDcxx . . $ `src/suggest/groonga_suggest_create_dataset.c:158:9`!("table_create sequence_${DATASET} TABLE_HASH_KEY ShortText"); |
| 202 | SENDcxx . . $ `src/suggest/groonga_suggest_create_dataset.c:158:9`!("table_create event_${DATASET} TABLE_NO_KEY"); |
| 203 | SENDcxx . . $ `src/suggest/groonga_suggest_create_dataset.c:158:9`!("column_create sequence_${DATASET} events " |
| 204 | "COLUMN_VECTOR|RING_BUFFER event_${DATASET}"); |
| 205 | SENDcxx . . $ `src/suggest/groonga_suggest_create_dataset.c:158:9`!("column_create event_${DATASET} type COLUMN_SCALAR event_type"); |
| 206 | SENDcxx . . $ `src/suggest/groonga_suggest_create_dataset.c:158:9`!("column_create event_${DATASET} time COLUMN_SCALAR Time"); |
| 207 | SENDcxx . . $ `src/suggest/groonga_suggest_create_dataset.c:158:9`!("column_create event_${DATASET} item COLUMN_SCALAR item_${DATASET}"); |
| 208 | SENDcxx . . $ `src/suggest/groonga_suggest_create_dataset.c:158:9`!("column_create event_${DATASET} sequence COLUMN_SCALAR " |
| 209 | "sequence_${DATASET}"); |
| 210 | |
| 211 | SENDcxx . . $ `src/suggest/groonga_suggest_create_dataset.c:158:9`!("table_create configuration TABLE_HASH_KEY ShortText"); |
| 212 | SENDcxx . . $ `src/suggest/groonga_suggest_create_dataset.c:158:9`!("column_create configuration weight COLUMN_SCALAR UInt32"); |
| 213 | SENDcxx . . $ `src/suggest/groonga_suggest_create_dataset.c:158:9`!("load --table configuration"); |
| 214 | SENDcxx . . $ `src/suggest/groonga_suggest_create_dataset.c:158:9`!("["); |
| 215 | SENDcxx . . $ `src/suggest/groonga_suggest_create_dataset.c:158:9`!("{\"_key\": \"${DATASET}\", \"weight\": 1}"); |
| 216 | SENDcxx . . $ `src/suggest/groonga_suggest_create_dataset.c:158:9`!("]"); |
| 217 | #undef SENDcxx . . $ `src/suggest/groonga_suggest_create_dataset.c:158:9`! |
| 218 | success = ctx->rccxx . . $ _grn_ctx#rc. == GRN_SUCCESScxx . . $ GRN_SUCCESS.; |
| 219 | GRN_OBJ_FINcxx . . $ `include/groonga/groonga.h:958:9`!(ctx, &text); |
| 220 | GRN_OBJ_FINcxx . . $ `include/groonga/groonga.h:958:9`!(ctx, db); |
| 221 | } else { |
| 222 | success = falsecxx . . $ `<external>/13015445228558014418/stdbool.h:38:9`!; |
| 223 | } |
| 224 | grn_ctx_fincxx . . $ grn_ctx_fin(2b4fa601bf4f1e5b).(ctx); |
| 225 | grn_fincxx . . $ grn_fin(d5bb96df31a8101).(); |
| 226 | |
| 227 | return success ? EXIT_SUCCESScxx . . $ `<external>/10696550936717951914/stdlib.h:97:9`! : EXIT_FAILUREcxx . . $ `<external>/10696550936717951914/stdlib.h:96:9`!; |
| 228 | } |