Debian Codegraph @

storage/mroonga/vendor/groonga/src/suggest/groonga_suggest_create_dataset.c

raw
1 /* -*- c-basic-offset: 2 -*- */
2 /* Copyright(C) 2010-2015 Brazil
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License version 2.1 as published by the Free Software Foundation.
7
8 This library is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 Lesser General Public License for more details.
12
13 You should have received a copy of the GNU Lesser General Public
14 License along with this library; if not, write to the Free Software
15 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335 USA
16 */
17
18 /* For grn_str_getopt() */
19 #include <grn_str.h>
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <groonga.h>
25
26 typedef enum {
27 MODE_NONE,
28 MODE_USAGE
29 } ModeFlags;
30
31 static const char *DEFAULT_DEFAULT_TOKENIZER = "TokenBigram";
32
33 static void
34 usage(FILE *output, int argc, char **argv)
35 {
36 #define OUTPUT(...) fprintf(output, __VA_ARGS__)
37
38 OUTPUT("Usage: %s [OPTIONS] DB_PATH DATASET_NAME\n", argv[0]);
39 OUTPUT(" e.g.: %s /tmp/db shops\n", argv[0]);
40 OUTPUT("\n");
41 OUTPUT("Options:\n");
42 OUTPUT(" --default-tokenizer=TOKENIZER Use TOKENIZER as the default\n");
43 OUTPUT(" tokenizer for item name\n");
44 OUTPUT(" (default: %s)\n",
45 DEFAULT_DEFAULT_TOKENIZER);
46 OUTPUT(" -h, --help Show this message and exit\n");
47
48 #undef OUTPUT
49 }
50
51 static void
52 output(grn_ctx *ctx)
53 {
54 int flags = 0;
55 char *str;
56 unsigned int str_len;
57
58 do {
59 grn_ctx_recv(ctx, &str, &str_len, &flags);
60 if (str_len > 0 || ctx->rc) {
61 if (ctx->rc) {
62 printf("ERROR (%d): %s\n", ctx->rc, ctx->errbuf);
63 }
64 if (str_len > 0) {
65 printf("%.*s\n", str_len, str);
66 }
67 }
68 } while (flags & GRN_CTX_MORE);
69 }
70
71 static void
72 send_command(grn_ctx *ctx, grn_obj *buffer, const char *command,
73 const char *dataset_name)
74 {
75 const char *p = command;
76 const char *dataset_place_holder = "${DATASET}";
77 char *dataset_place_holder_position;
78
79 if (ctx->rc != GRN_SUCCESS) {
80 return;
81 }
82
83 GRN_BULK_REWIND(buffer);
84 while ((dataset_place_holder_position = strstr(p, dataset_place_holder))) {
85 GRN_TEXT_PUT(ctx, buffer, p, dataset_place_holder_position - p);
86 GRN_TEXT_PUTS(ctx, buffer, dataset_name);
87 p = dataset_place_holder_position + strlen(dataset_place_holder);
88 }
89 GRN_TEXT_PUTS(ctx, buffer, p);
90 printf("> %.*s\n", (int)GRN_TEXT_LEN(buffer), GRN_TEXT_VALUE(buffer));
91 grn_ctx_send(ctx, GRN_TEXT_VALUE(buffer), GRN_TEXT_LEN(buffer), 0);
92 output(ctx);
93 }
94
95
96 int
97 main(int argc, char **argv)
98 {
99 const char *db_path;
100 const char *dataset_name;
101 grn_ctx ctx_, *ctx;
102 grn_obj *db;
103 grn_bool success = GRN_TRUE;
104 int parsed_argc, rest_argc;
105 int flags = MODE_NONE;
106 const char *default_tokenizer = NULL;
107 static grn_str_getopt_opt opts[] = {
108 {'\0', "default-tokenizer", NULL, 0, GETOPT_OP_NONE},
109 {'h', "help", NULL, MODE_USAGE, GETOPT_OP_UPDATE}
110 };
111
112 opts[0].arg = &default_tokenizer;
113
114 parsed_argc = grn_str_getopt(argc, argv, opts, &flags);
115 if (parsed_argc < 0) {
116 usage(stderr, argc, argv);
117 return EXIT_FAILURE;
118 }
119
120 if (flags & MODE_USAGE) {
121 usage(stdout, argc, argv);
122 return EXIT_SUCCESS;
123 }
124
125 rest_argc = argc - parsed_argc;
126 if (rest_argc != 2) {
127 usage(stderr, argc, argv);
128 return EXIT_FAILURE;
129 }
130
131 db_path = argv[parsed_argc];
132 dataset_name = argv[parsed_argc + 1];
133
134 grn_init();
135
136 ctx = &ctx_;
137 grn_ctx_init(ctx, 0);
138 db = grn_db_open(ctx, db_path);
139 if (!db) {
140 if (ctx->rc == GRN_NO_SUCH_FILE_OR_DIRECTORY) {
141 db = grn_db_create(ctx, db_path, NULL);
142 if (!db) {
143 fprintf(stderr, "DB create failed (%s): %s\n", db_path, ctx->errbuf);
144 }
145 } else {
146 fprintf(stderr, "DB open failed (%s): %s\n", db_path, ctx->errbuf);
147 }
148 }
149
150 if (db) {
151 grn_obj text;
152 GRN_TEXT_INIT(&text, 0);
153 #define SEND(string) send_command(ctx, &text, string, dataset_name)
154 SEND("plugin_register suggest/suggest");
155 SEND("table_create event_type TABLE_HASH_KEY ShortText");
156 {
157 grn_obj query;
158 GRN_TEXT_INIT(&query, 0);
159 GRN_TEXT_PUTS(ctx, &query,
160 "table_create bigram TABLE_PAT_KEY ShortText "
161 "--default_tokenizer ");
162 if (default_tokenizer) {
163 GRN_TEXT_PUTS(ctx, &query, default_tokenizer);
164 } else {
165 GRN_TEXT_PUTS(ctx, &query, DEFAULT_DEFAULT_TOKENIZER);
166 }
167 GRN_TEXT_PUTS(ctx, &query, " --normalizer NormalizerAuto");
168 GRN_TEXT_PUTC(ctx, &query, '\0');
169 SEND(GRN_TEXT_VALUE(&query));
170 GRN_OBJ_FIN(ctx, &query);
171 }
172 SEND("table_create kana TABLE_PAT_KEY ShortText "
173 "--normalizer NormalizerAuto");
174 SEND("table_create item_${DATASET} TABLE_PAT_KEY "
175 "ShortText --default_tokenizer TokenDelimit "
176 "--normalizer NormalizerAuto");
177 SEND("column_create bigram item_${DATASET}_key "
178 "COLUMN_INDEX|WITH_POSITION item_${DATASET} _key");
179 SEND("column_create item_${DATASET} kana COLUMN_VECTOR kana");
180 SEND("column_create kana item_${DATASET}_kana COLUMN_INDEX "
181 "item_${DATASET} kana");
182 SEND("column_create item_${DATASET} freq COLUMN_SCALAR Int32");
183 SEND("column_create item_${DATASET} last COLUMN_SCALAR Time");
184 SEND("column_create item_${DATASET} boost COLUMN_SCALAR Int32");
185 SEND("column_create item_${DATASET} freq2 COLUMN_SCALAR Int32");
186 SEND("column_create item_${DATASET} buzz COLUMN_SCALAR Int32");
187
188 SEND("table_create pair_${DATASET} TABLE_HASH_KEY UInt64");
189 SEND("column_create pair_${DATASET} pre COLUMN_SCALAR item_${DATASET}");
190 SEND("column_create pair_${DATASET} post COLUMN_SCALAR item_${DATASET}");
191 SEND("column_create pair_${DATASET} freq0 COLUMN_SCALAR Int32");
192 SEND("column_create pair_${DATASET} freq1 COLUMN_SCALAR Int32");
193 SEND("column_create pair_${DATASET} freq2 COLUMN_SCALAR Int32");
194 SEND("column_create item_${DATASET} co COLUMN_INDEX pair_${DATASET} pre");
195
196 SEND("table_create sequence_${DATASET} TABLE_HASH_KEY ShortText");
197 SEND("table_create event_${DATASET} TABLE_NO_KEY");
198 SEND("column_create sequence_${DATASET} events "
199 "COLUMN_VECTOR|RING_BUFFER event_${DATASET}");
200 SEND("column_create event_${DATASET} type COLUMN_SCALAR event_type");
201 SEND("column_create event_${DATASET} time COLUMN_SCALAR Time");
202 SEND("column_create event_${DATASET} item COLUMN_SCALAR item_${DATASET}");
203 SEND("column_create event_${DATASET} sequence COLUMN_SCALAR "
204 "sequence_${DATASET}");
205
206 SEND("table_create configuration TABLE_HASH_KEY ShortText");
207 SEND("column_create configuration weight COLUMN_SCALAR UInt32");
208 SEND("load --table configuration");
209 SEND("[");
210 SEND("{\"_key\": \"${DATASET}\", \"weight\": 1}");
211 SEND("]");
212 #undef SEND
213 success = ctx->rc == GRN_SUCCESS;
214 GRN_OBJ_FIN(ctx, &text);
215 GRN_OBJ_FIN(ctx, db);
216 } else {
217 success = GRN_FALSE;
218 }
219 grn_ctx_fin(ctx);
220 grn_fin();
221
222 return success ? EXIT_SUCCESS : EXIT_FAILURE;
223 }