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