All patches
[idzebra-moved-to-github.git] / index / zebraidx.c
1 /* This file is part of the Zebra server.
2    Copyright (C) 1994-2009 Index Data
3
4 Zebra is free software; you can redistribute it and/or modify it under
5 the terms of the GNU General Public License as published by the Free
6 Software Foundation; either version 2, or (at your option) any later
7 version.
8
9 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17
18 */
19
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <assert.h>
24 #ifdef WIN32
25 #include <io.h>
26 #endif
27 #if HAVE_UNISTD_H
28 #include <unistd.h>
29 #endif
30
31 #if YAZ_HAVE_ICU
32 #include <yaz/icu.h>
33 #endif
34 #include <yaz/log.h>
35 #include <yaz/options.h>
36 #include <idzebra/version.h>
37 #include <idzebra/api.h>
38
39 char *prog;
40
41 static void filter_cb(void *cd, const char *name)
42 {
43     puts(name);
44 }
45
46 static void show_filters(ZebraService zs)
47 {
48     zebra_filter_info(zs, 0, filter_cb);
49 }
50
51 int main(int argc, char **argv)
52 {
53     int ret;
54     int cmd = 0;
55     char *arg;
56     char *configName = 0;
57     int nsections = 0;
58     int enable_commit = 1;
59     char *database = 0;
60     Res res = res_open(0, 0);
61     Res default_res = res_open(0, 0);
62     
63     int trans_started = 0;
64 #ifndef WIN32
65     char nbuf[100];
66 #endif
67     ZebraService zs = 0;
68     ZebraHandle zh = 0;
69
70 #ifdef WIN32
71 #else
72     sprintf(nbuf, "%.40s(%ld)", *argv, (long) getpid());
73     yaz_log_init_prefix(nbuf);
74 #endif
75     /* For indexing driver support */
76     char *useIndexDriver = NULL;
77     prog = *argv;
78     if (argc < 2)
79     {
80         fprintf(stderr, "%s [options] command <dir> ...\n"
81         "Commands:\n"
82         " update <dir>  Update index with files below <dir>.\n"
83         "               If <dir> is empty filenames are read from stdin.\n"
84         " delete <dir>  Delete index with files below <dir>.\n"
85         " create <db>   Create database <db>\n"
86         " drop <db>     Drop database <db>\n"
87         " commit        Commit changes\n"
88         " clean         Clean shadow files\n"
89         "Options:\n"
90         " -t <type>     Index files as <type> (grs or text).\n"
91         " -c <config>   Read configuration file <config>.\n"
92         " -g <group>    Index files according to group settings.\n"
93         " -d <database> Records belong to Z39.50 database <database>.\n"
94         " -m <mbytes>   Use <mbytes> before flushing keys to disk.\n"
95         " -n            Don't use shadow system.\n"
96         " -s            Show analysis on stdout, but do no work.\n"
97         " -v <level>    Set logging to <level>.\n"
98         " -l <file>     Write log to <file>.\n"
99         " -L            Don't follow symbolic links.\n"
100         " -f <n>        Display information for the first <n> records.\n"
101         " -i <driver>   Select which index driver to use.\n"
102         "               Note:   when using a driver, the <dir> gets passed\n"
103         "                       to the driver as an argument.\n"
104         "               Current drivers available:\n"
105         "               -       indexplugin_mysql\n\n"
106         " -V            Show version.\n", *argv
107                  );
108         exit(1);
109     }
110     res_set(default_res, "profilePath", DEFAULT_PROFILE_PATH);
111     res_set(default_res, "modulePath", DEFAULT_MODULE_PATH);
112     while ((ret = options("sVt:c:g:d:m:v:nf:l:L:i:", argv, argc, &arg)) != -2)
113     {
114         if (ret == 0)
115         {
116             if(cmd == 0) /* command */
117             {
118                 if (!zs)
119                 {
120                     const char *config = configName ? configName : "zebra.cfg";
121                     zs = zebra_start_res(config, default_res, res);
122                     if (!zs)
123                     {
124                         yaz_log(YLOG_FATAL, "Cannot read config %s", config);
125                         exit(1);
126                     }   
127                     zh = zebra_open(zs, 0);
128                     zebra_shadow_enable(zh, enable_commit);
129                 }
130
131                 if (database &&
132                     zebra_select_database(zh, database) == ZEBRA_FAIL)
133                 {
134                     yaz_log(YLOG_FATAL, "Could not select database %s "
135                             "errCode=%d",
136                             database, zebra_errCode(zh) );
137                     exit(1);
138                 }
139                 if (!strcmp(arg, "update"))
140                     cmd = 'u';
141                 else if (!strcmp(arg, "update1"))
142                     cmd = 'U';
143                 else if (!strcmp(arg, "update2"))
144                     cmd = 'm';
145                 else if (!strcmp(arg, "dump"))
146                     cmd = 's';
147                 else if (!strcmp(arg, "del") || !strcmp(arg, "delete"))
148                     cmd = 'd';
149                 else if (!strcmp(arg, "adelete"))
150                     cmd = 'a';
151                 else if (!strcmp(arg, "init"))
152                 {
153                     zebra_init(zh);
154                 }
155                 else if (!strcmp(arg, "drop"))
156                 {
157                     cmd = 'D';
158                 }
159                 else if (!strcmp(arg, "create"))
160                 {
161                     cmd = 'C';
162                 }
163                 else if (!strcmp(arg, "commit"))
164                 {
165                     zebra_commit(zh);
166                 }
167                 else if (!strcmp(arg, "clean"))
168                 {
169                     zebra_clean(zh);
170                 }
171                 else if (!strcmp(arg, "stat") || !strcmp(arg, "status"))
172                 {
173                     zebra_register_statistics(zh, 0);
174                 }
175                 else if (!strcmp(arg, "dumpdict"))
176                 {
177                     zebra_register_statistics(zh, 1);
178                 }
179                 else if (!strcmp(arg, "compact"))
180                 {
181                     zebra_compact(zh);
182                 }
183                 else if (!strcmp(arg, "filters"))
184                 {
185                     show_filters(zs);
186                 }
187                 else
188                 {
189                     yaz_log(YLOG_FATAL, "unknown command: %s", arg);
190                     exit(1);
191                 }
192             }
193             else
194             {
195                 ZEBRA_RES res = ZEBRA_OK;
196                 if (!trans_started)
197                 {
198                     trans_started=1;
199                     if (zebra_begin_trans(zh, 1) != ZEBRA_OK)
200                         exit(1);
201                 }
202                 switch (cmd)
203                 {
204                 case 'u':
205                     res = zebra_repository_index(zh, arg, action_update, useIndexDriver);
206                     break;
207                 case 'd':
208                     res = zebra_repository_index(zh, arg, action_delete, useIndexDriver);
209                     break;
210                 case 'a':
211                     res = zebra_repository_index(zh, arg, action_a_delete, useIndexDriver);
212                     break;
213                 case 's':
214                     res = zebra_repository_show(zh, arg);
215                     nsections = 0;
216                     break;
217                 case 'C':
218                     res = zebra_create_database(zh, arg);
219                     break;
220                 case 'D':
221                     res = zebra_drop_database(zh, arg);
222                     break;
223                 default:
224                     nsections = 0;
225                 }
226                 if (res != ZEBRA_OK)
227                 {
228                     const char *add = zebra_errAdd(zh);
229                     yaz_log(YLOG_FATAL, "Operation failed: %s %s",
230                             zebra_errString(zh), add ? add : "");
231                     
232                     if (trans_started)
233                         if (zebra_end_trans(zh) != ZEBRA_OK)
234                             yaz_log(YLOG_WARN, "zebra_end_trans failed");
235
236
237                     zebra_close(zh);
238                     zebra_stop(zs);
239                     exit(1);
240                 }
241                 log_event_end(NULL, NULL);
242             }
243         }
244         else if (ret == 'V')
245         {
246             char version_str[20];
247             char sys_str[80];
248             zebra_get_version(version_str, sys_str);
249
250             printf("Zebra %s\n", version_str);
251             printf("(C) 1994-2009, Index Data ApS\n");
252             printf("Zebra is free software, covered by the GNU General Public License, and you are\n");
253             printf("welcome to change it and/or distribute copies of it under certain conditions.\n");
254             printf("SHA1 ID: %s\n", sys_str);
255             if (strcmp(version_str, ZEBRAVER))
256                 printf("zebraidx compiled version %s\n", ZEBRAVER);
257 #if YAZ_HAVE_ICU
258             printf("Using ICU\n");
259 #endif
260         }
261         else if (ret == 'v')
262             yaz_log_init_level(yaz_log_mask_str(arg));
263         else if (ret == 'i')
264             useIndexDriver = arg;
265         else if (ret == 'l')
266             yaz_log_init_file(arg);
267         else if (ret == 'm')
268             res_set(res, "memMax", arg);
269         else if (ret == 'd')
270             database = arg;
271         else if (ret == 's')
272             res_set(res, "openRW", "0");
273         else if (ret == 'g')
274             res_set(res, "group", arg);
275         else if (ret == 'f')
276             res_set(res, "fileVerboseLimit", arg);
277         else if (ret == 'c')
278             configName = arg;
279         else if (ret == 't')
280             res_set(res, "recordType", arg);
281         else if (ret == 'n')
282             enable_commit = 0;
283         else if (ret == 'L')
284             res_set(res, "followLinks", "0");
285         else
286             yaz_log(YLOG_WARN, "unknown option '-%s'", arg);
287     } /* while arg */
288
289     if (trans_started)
290         if (zebra_end_trans(zh) != ZEBRA_OK)
291             yaz_log(YLOG_WARN, "zebra_end_trans failed");
292
293     zebra_close(zh);
294     zebra_stop(zs);
295
296     res_close(res);
297     res_close(default_res);
298     exit(0);
299     return 0;
300 }
301
302 /*
303  * Local variables:
304  * c-basic-offset: 4
305  * c-file-style: "Stroustrup"
306  * indent-tabs-mode: nil
307  * End:
308  * vim: shiftwidth=4 tabstop=8 expandtab
309  */
310