Groonga 3.0.9 Source Code Document
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
groonga_suggest_create_dataset.c
Go to the documentation of this file.
1 /* -*- c-basic-offset: 2 -*- */
2 /* Copyright(C) 2010-2013 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-1301 USA
16 */
17 
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <groonga.h>
22 
23 /* For grn_str_getopt() */
24 #include <str.h>
25 
26 typedef enum {
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("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|KEY_NORMALIZE 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_PUTC(ctx, &query, '\0');
168  SEND(GRN_TEXT_VALUE(&query));
169  GRN_OBJ_FIN(ctx, &query);
170  }
171  SEND("table_create kana TABLE_PAT_KEY|KEY_NORMALIZE ShortText");
172  SEND("table_create item_${DATASET} TABLE_PAT_KEY|KEY_NORMALIZE "
173  "ShortText --default_tokenizer TokenDelimit");
174  SEND("column_create bigram item_${DATASET}_key "
175  "COLUMN_INDEX|WITH_POSITION item_${DATASET} _key");
176  SEND("column_create item_${DATASET} kana COLUMN_VECTOR kana");
177  SEND("column_create kana item_${DATASET}_kana COLUMN_INDEX "
178  "item_${DATASET} kana");
179  SEND("column_create item_${DATASET} freq COLUMN_SCALAR Int32");
180  SEND("column_create item_${DATASET} last COLUMN_SCALAR Time");
181  SEND("column_create item_${DATASET} boost COLUMN_SCALAR Int32");
182  SEND("column_create item_${DATASET} freq2 COLUMN_SCALAR Int32");
183  SEND("column_create item_${DATASET} buzz COLUMN_SCALAR Int32");
184 
185  SEND("table_create pair_${DATASET} TABLE_HASH_KEY UInt64");
186  SEND("column_create pair_${DATASET} pre COLUMN_SCALAR item_${DATASET}");
187  SEND("column_create pair_${DATASET} post COLUMN_SCALAR item_${DATASET}");
188  SEND("column_create pair_${DATASET} freq0 COLUMN_SCALAR Int32");
189  SEND("column_create pair_${DATASET} freq1 COLUMN_SCALAR Int32");
190  SEND("column_create pair_${DATASET} freq2 COLUMN_SCALAR Int32");
191  SEND("column_create item_${DATASET} co COLUMN_INDEX pair_${DATASET} pre");
192 
193  SEND("table_create sequence_${DATASET} TABLE_HASH_KEY ShortText");
194  SEND("table_create event_${DATASET} TABLE_NO_KEY");
195  SEND("column_create sequence_${DATASET} events "
196  "COLUMN_VECTOR|RING_BUFFER event_${DATASET}");
197  SEND("column_create event_${DATASET} type COLUMN_SCALAR event_type");
198  SEND("column_create event_${DATASET} time COLUMN_SCALAR Time");
199  SEND("column_create event_${DATASET} item COLUMN_SCALAR item_${DATASET}");
200  SEND("column_create event_${DATASET} sequence COLUMN_SCALAR "
201  "sequence_${DATASET}");
202 
203  SEND("table_create configuration TABLE_HASH_KEY ShortText");
204  SEND("column_create configuration weight COLUMN_SCALAR UInt32");
205  SEND("load --table configuration");
206  SEND("[");
207  SEND("{\"_key\": \"${DATASET}\", \"weight\": 1}");
208  SEND("]");
209 #undef SEND
210  success = ctx->rc == GRN_SUCCESS;
211  GRN_OBJ_FIN(ctx, &text);
212  GRN_OBJ_FIN(ctx, db);
213  } else {
214  success = GRN_FALSE;
215  }
216  grn_ctx_fin(ctx);
217  grn_fin();
218 
219  return success ? EXIT_SUCCESS : EXIT_FAILURE;
220 }