| 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> |
| 22 | |
| 23 | #include <stdio.h> |
| 24 | #include <stdlib.h> |
| 25 | #include <string.h> |
| 26 | #include <groonga.h> |
| 27 | |
| 28 | typedef enum { |
| 29 | MODE_NONE, |
| 30 | MODE_USAGE |
| 31 | } ModeFlags; |
| 32 | |
| 33 | static const char *DEFAULT_DEFAULT_TOKENIZER = "TokenBigram"; |
| 34 | |
| 35 | static void |
| 36 | usage(FILE *output, int argc, char **argv) |
| 37 | { |
| 38 | #define OUTPUT(...) fprintf(output, __VA_ARGS__) |
| 39 | |
| 40 | OUTPUT("Usage: %s [OPTIONS] DB_PATH DATASET_NAME\n", argv[0]); |
| 41 | OUTPUT(" e.g.: %s /tmp/db shops\n", argv[0]); |
| 42 | OUTPUT("\n"); |
| 43 | OUTPUT("Options:\n"); |
| 44 | OUTPUT(" --default-tokenizer=TOKENIZER Use TOKENIZER as the default\n"); |
| 45 | OUTPUT(" tokenizer for item name\n"); |
| 46 | OUTPUT(" (default: %s)\n", |
| 47 | DEFAULT_DEFAULT_TOKENIZER); |
| 48 | OUTPUT(" -h, --help Show this message and exit\n"); |
| 49 | |
| 50 | #undef OUTPUT |
| 51 | } |
| 52 | |
| 53 | static void |
| 54 | output(grn_ctx *ctx) |
| 55 | { |
| 56 | int flags = 0; |
| 57 | char *str; |
| 58 | unsigned int str_len; |
| 59 | |
| 60 | do { |
| 61 | grn_ctx_recv(ctx, &str, &str_len, &flags); |
| 62 | if (str_len > 0 || ctx->rc) { |
| 63 | if (ctx->rc) { |
| 64 | printf("ERROR (%d): %s\n", ctx->rc, ctx->errbuf); |
| 65 | } |
| 66 | if (str_len > 0) { |
| 67 | printf("%.*s\n", str_len, str); |
| 68 | } |
| 69 | } |
| 70 | } while (flags & GRN_CTX_MORE); |
| 71 | } |
| 72 | |
| 73 | static void |
| 74 | send_command(grn_ctx *ctx, 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->rc != GRN_SUCCESS) { |
| 82 | return; |
| 83 | } |
| 84 | |
| 85 | GRN_BULK_REWIND(buffer); |
| 86 | while ((dataset_place_holder_position = strstr(p, dataset_place_holder))) { |
| 87 | GRN_TEXT_PUT(ctx, buffer, p, dataset_place_holder_position - p); |
| 88 | GRN_TEXT_PUTS(ctx, buffer, dataset_name); |
| 89 | p = dataset_place_holder_position + strlen(dataset_place_holder); |
| 90 | } |
| 91 | GRN_TEXT_PUTS(ctx, buffer, p); |
| 92 | printf("> %.*s\n", (int)GRN_TEXT_LEN(buffer), GRN_TEXT_VALUE(buffer)); |
| 93 | grn_ctx_send(ctx, |
| 94 | GRN_TEXT_VALUE(buffer), |
| 95 | (unsigned int)GRN_TEXT_LEN(buffer), |
| 96 | 0); |
| 97 | output(ctx); |
| 98 | } |
| 99 | |
| 100 | |
| 101 | int |
| 102 | main(int argc, char **argv) |
| 103 | { |
| 104 | const char *db_path; |
| 105 | const char *dataset_name; |
| 106 | grn_ctx ctx_, *ctx; |
| 107 | grn_obj *db; |
| 108 | bool success = true; |
| 109 | int parsed_argc, rest_argc; |
| 110 | int flags = MODE_NONE; |
| 111 | const char *default_tokenizer = NULL; |
| 112 | static grn_str_getopt_opt opts[] = { |
| 113 | {'\0', "default-tokenizer", NULL, 0, GETOPT_OP_NONE}, |
| 114 | {'h', "help", NULL, MODE_USAGE, GETOPT_OP_UPDATE} |
| 115 | }; |
| 116 | |
| 117 | opts[0].arg = &default_tokenizer; |
| 118 | |
| 119 | parsed_argc = grn_str_getopt(argc, argv, opts, &flags); |
| 120 | if (parsed_argc < 0) { |
| 121 | usage(stderr, argc, argv); |
| 122 | return EXIT_FAILURE; |
| 123 | } |
| 124 | |
| 125 | if (flags & MODE_USAGE) { |
| 126 | usage(stdout, argc, argv); |
| 127 | return EXIT_SUCCESS; |
| 128 | } |
| 129 | |
| 130 | rest_argc = argc - parsed_argc; |
| 131 | if (rest_argc != 2) { |
| 132 | usage(stderr, argc, argv); |
| 133 | return EXIT_FAILURE; |
| 134 | } |
| 135 | |
| 136 | db_path = argv[parsed_argc]; |
| 137 | dataset_name = argv[parsed_argc + 1]; |
| 138 | |
| 139 | grn_init(); |
| 140 | |
| 141 | ctx = &ctx_; |
| 142 | grn_ctx_init(ctx, 0); |
| 143 | db = grn_db_open(ctx, db_path); |
| 144 | if (!db) { |
| 145 | if (ctx->rc == GRN_NO_SUCH_FILE_OR_DIRECTORY) { |
| 146 | db = grn_db_create(ctx, db_path, NULL); |
| 147 | if (!db) { |
| 148 | fprintf(stderr, "DB create failed (%s): %s\n", db_path, ctx->errbuf); |
| 149 | } |
| 150 | } else { |
| 151 | fprintf(stderr, "DB open failed (%s): %s\n", db_path, ctx->errbuf); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | if (db) { |
| 156 | grn_obj text; |
| 157 | GRN_TEXT_INIT(&text, 0); |
| 158 | #define SEND(string) send_command(ctx, &text, string, dataset_name) |
| 159 | SEND("plugin_register suggest/suggest"); |
| 160 | SEND("table_create event_type TABLE_HASH_KEY ShortText"); |
| 161 | { |
| 162 | grn_obj query; |
| 163 | GRN_TEXT_INIT(&query, 0); |
| 164 | GRN_TEXT_PUTS(ctx, &query, |
| 165 | "table_create bigram TABLE_PAT_KEY ShortText " |
| 166 | "--default_tokenizer "); |
| 167 | if (default_tokenizer) { |
| 168 | GRN_TEXT_PUTS(ctx, &query, default_tokenizer); |
| 169 | } else { |
| 170 | GRN_TEXT_PUTS(ctx, &query, DEFAULT_DEFAULT_TOKENIZER); |
| 171 | } |
| 172 | GRN_TEXT_PUTS(ctx, &query, " --normalizer NormalizerAuto"); |
| 173 | GRN_TEXT_PUTC(ctx, &query, '\0'); |
| 174 | SEND(GRN_TEXT_VALUE(&query)); |
| 175 | GRN_OBJ_FIN(ctx, &query); |
| 176 | } |
| 177 | SEND("table_create kana TABLE_PAT_KEY ShortText " |
| 178 | "--normalizer NormalizerAuto"); |
| 179 | SEND("table_create item_${DATASET} TABLE_PAT_KEY " |
| 180 | "ShortText --default_tokenizer TokenDelimit " |
| 181 | "--normalizer NormalizerAuto"); |
| 182 | SEND("column_create bigram item_${DATASET}_key " |
| 183 | "COLUMN_INDEX|WITH_POSITION item_${DATASET} _key"); |
| 184 | SEND("column_create item_${DATASET} kana COLUMN_VECTOR kana"); |
| 185 | SEND("column_create kana item_${DATASET}_kana COLUMN_INDEX " |
| 186 | "item_${DATASET} kana"); |
| 187 | SEND("column_create item_${DATASET} freq COLUMN_SCALAR Int32"); |
| 188 | SEND("column_create item_${DATASET} last COLUMN_SCALAR Time"); |
| 189 | SEND("column_create item_${DATASET} boost COLUMN_SCALAR Int32"); |
| 190 | SEND("column_create item_${DATASET} freq2 COLUMN_SCALAR Int32"); |
| 191 | SEND("column_create item_${DATASET} buzz COLUMN_SCALAR Int32"); |
| 192 | |
| 193 | SEND("table_create pair_${DATASET} TABLE_HASH_KEY UInt64"); |
| 194 | SEND("column_create pair_${DATASET} pre COLUMN_SCALAR item_${DATASET}"); |
| 195 | SEND("column_create pair_${DATASET} post COLUMN_SCALAR item_${DATASET}"); |
| 196 | SEND("column_create pair_${DATASET} freq0 COLUMN_SCALAR Int32"); |
| 197 | SEND("column_create pair_${DATASET} freq1 COLUMN_SCALAR Int32"); |
| 198 | SEND("column_create pair_${DATASET} freq2 COLUMN_SCALAR Int32"); |
| 199 | SEND("column_create item_${DATASET} co COLUMN_INDEX pair_${DATASET} pre"); |
| 200 | |
| 201 | SEND("table_create sequence_${DATASET} TABLE_HASH_KEY ShortText"); |
| 202 | SEND("table_create event_${DATASET} TABLE_NO_KEY"); |
| 203 | SEND("column_create sequence_${DATASET} events " |
| 204 | "COLUMN_VECTOR|RING_BUFFER event_${DATASET}"); |
| 205 | SEND("column_create event_${DATASET} type COLUMN_SCALAR event_type"); |
| 206 | SEND("column_create event_${DATASET} time COLUMN_SCALAR Time"); |
| 207 | SEND("column_create event_${DATASET} item COLUMN_SCALAR item_${DATASET}"); |
| 208 | SEND("column_create event_${DATASET} sequence COLUMN_SCALAR " |
| 209 | "sequence_${DATASET}"); |
| 210 | |
| 211 | SEND("table_create configuration TABLE_HASH_KEY ShortText"); |
| 212 | SEND("column_create configuration weight COLUMN_SCALAR UInt32"); |
| 213 | SEND("load --table configuration"); |
| 214 | SEND("["); |
| 215 | SEND("{\"_key\": \"${DATASET}\", \"weight\": 1}"); |
| 216 | SEND("]"); |
| 217 | #undef SEND |
| 218 | success = ctx->rc == GRN_SUCCESS; |
| 219 | GRN_OBJ_FIN(ctx, &text); |
| 220 | GRN_OBJ_FIN(ctx, db); |
| 221 | } else { |
| 222 | success = false; |
| 223 | } |
| 224 | grn_ctx_fin(ctx); |
| 225 | grn_fin(); |
| 226 | |
| 227 | return success ? EXIT_SUCCESS : EXIT_FAILURE; |
| 228 | } |