Version 2.0.2
[idzebra-moved-to-github.git] / index / zebraidx.c
1 /* $Id: zebraidx.c,v 1.1 2006-09-22 10:18:08 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 #if HAVE_SYS_TIME_H
34 #include <sys/time.h>
35 #endif
36 #include <time.h>
37 #if HAVE_SYS_TIMES_H
38 #include <sys/times.h>
39 #endif
40
41 #include <yaz/log.h>
42 #include <yaz/options.h>
43 #include <idzebra/api.h>
44
45 char *prog;
46
47 static void filter_cb(void *cd, const char *name)
48 {
49     puts (name);
50 }
51
52 static void show_filters(ZebraService zs)
53 {
54     zebra_filter_info(zs, 0, filter_cb);
55 }
56
57 int main (int argc, char **argv)
58 {
59     int ret;
60     int cmd = 0;
61     char *arg;
62     char *configName = 0;
63     int nsections = 0;
64     int enable_commit = 1;
65     char *database = 0;
66     Res res = res_open(0, 0);
67     
68     int trans_started=0;
69 #if HAVE_SYS_TIMES_H
70     struct tms tms1, tms2;
71     double usec;
72 #endif
73 #if HAVE_SYS_TIME_H
74     struct timeval start_time, end_time;
75 #endif
76 #ifndef WIN32
77     char nbuf[100];
78 #endif
79     ZebraService zs = 0;
80     ZebraHandle zh = 0;
81
82     nmem_init ();
83
84 #ifdef WIN32
85 #else
86     sprintf(nbuf, "%.40s(%ld)", *argv, (long) getpid());
87     yaz_log_init_prefix (nbuf);
88 #endif
89 #if HAVE_SYS_TIMES_H
90     times(&tms1);
91 #endif
92 #if HAVE_SYS_TIME_H
93     gettimeofday(&start_time, 0);
94 #endif
95     prog = *argv;
96     if (argc < 2)
97     {
98         fprintf (stderr, "%s [options] command <dir> ...\n"
99         "Commands:\n"
100         " update <dir>  Update index with files below <dir>.\n"
101         "               If <dir> is empty filenames are read from stdin.\n"
102         " delete <dir>  Delete index with files below <dir>.\n"
103         " create <db>   Create database <db>\n"
104         " drop <db>     Drop database <db>\n"
105         " commit        Commit changes\n"
106         " clean         Clean shadow files\n"
107         "Options:\n"
108         " -t <type>     Index files as <type> (grs or text).\n"
109         " -c <config>   Read configuration file <config>.\n"
110         " -g <group>    Index files according to group settings.\n"
111         " -d <database> Records belong to Z39.50 database <database>.\n"
112         " -m <mbytes>   Use <mbytes> before flushing keys to disk.\n"
113         " -n            Don't use shadow system.\n"
114         " -s            Show analysis on stdout, but do no work.\n"
115         " -v <level>    Set logging to <level>.\n"
116         " -l <file>     Write log to <file>.\n"
117         " -L            Don't follow symbolic links.\n"
118         " -f <n>        Display information for the first <n> records.\n"
119         " -V            Show version.\n", *argv
120                  );
121         exit (1);
122     }
123     while ((ret = options("sVt:c:g:d:m:v:nf:l:L", argv, argc, &arg)) != -2)
124     {
125         if (ret == 0)
126         {
127             if(cmd == 0) /* command */
128             {
129                 if (!zs)
130                 {
131                     const char *config = configName ? configName : "zebra.cfg";
132                     zs = zebra_start_res (config, 0, res);
133                     if (!zs)
134                     {
135                         yaz_log (YLOG_FATAL, "Cannot read config %s", config);
136                         exit (1);
137                     }   
138                     zh = zebra_open (zs, 0);
139                     zebra_shadow_enable (zh, enable_commit);
140                 }
141
142                 if (database &&
143                     zebra_select_database (zh, database) == ZEBRA_FAIL)
144                 {
145                     yaz_log(YLOG_FATAL, "Could not select database %s "
146                             "errCode=%d",
147                             database, zebra_errCode(zh) );
148                     exit (1);
149                 }
150                 if (!strcmp (arg, "update"))
151                     cmd = 'u';
152                 else if (!strcmp (arg, "update1"))
153                     cmd = 'U';
154                 else if (!strcmp (arg, "update2"))
155                     cmd = 'm';
156                 else if (!strcmp (arg, "dump"))
157                     cmd = 's';
158                 else if (!strcmp (arg, "del") || !strcmp(arg, "delete"))
159                     cmd = 'd';
160                 else if (!strcmp (arg, "init"))
161                 {
162                     zebra_init (zh);
163                 }
164                 else if (!strcmp(arg, "drop"))
165                 {
166                     cmd = 'D';
167                 }
168                 else if (!strcmp(arg, "create"))
169                 {
170                     cmd = 'C';
171                 }
172                 else if (!strcmp (arg, "commit"))
173                 {
174                     zebra_commit (zh);
175                 }
176                 else if (!strcmp (arg, "clean"))
177                 {
178                     zebra_clean (zh);
179                 }
180                 else if (!strcmp (arg, "stat") || !strcmp (arg, "status"))
181                 {
182                     zebra_register_statistics (zh,0);
183                 }
184                 else if (!strcmp (arg, "dumpdict"))
185                 {
186                     zebra_register_statistics (zh,1);
187                 }
188                 else if (!strcmp (arg, "compact"))
189                 {
190                     zebra_compact (zh);
191                 }
192                 else if (!strcmp (arg, "filters"))
193                 {
194                     show_filters(zs);
195                 }
196                 else
197                 {
198                     yaz_log (YLOG_FATAL, "unknown command: %s", arg);
199                     exit (1);
200                 }
201             }
202             else
203             {
204                 ZEBRA_RES res = ZEBRA_OK;
205                 if (!trans_started)
206                 {
207                     trans_started=1;
208                     if (zebra_begin_trans (zh, 1) != ZEBRA_OK)
209                         exit(1);
210                 }
211                 switch (cmd)
212                 {
213                 case 'u':
214                     res = zebra_repository_update (zh, arg);
215                     break;
216                 case 'd':
217                     res = zebra_repository_delete (zh, arg);
218                     break;
219                 case 's':
220                     res = zebra_repository_show (zh, arg);
221                     nsections = 0;
222                     break;
223                 case 'C':
224                     res = zebra_create_database(zh, arg);
225                     break;
226                 case 'D':
227                     res = zebra_drop_database(zh, arg);
228                     break;
229                 default:
230                     nsections = 0;
231                 }
232                 if (res != ZEBRA_OK)
233                 {
234                     const char *add = zebra_errAdd(zh);
235                     yaz_log(YLOG_FATAL, "Operation failed: %s %s",
236                             zebra_errString(zh), add ? add : "");
237                     
238                     if (trans_started)
239                         if (zebra_end_trans (zh) != ZEBRA_OK)
240                             yaz_log (YLOG_WARN, "zebra_end_trans failed");
241
242
243                     zebra_close (zh);
244                     zebra_stop (zs);
245                     exit(1);
246                 }
247                 log_event_end (NULL, NULL);
248             }
249         }
250         else if (ret == 'V')
251         {
252             printf("Zebra %s %s\n", ZEBRAVER, ZEBRADATE);
253             printf(" (C) 1994-2006, Index Data ApS\n");
254 #ifdef WIN32
255 #ifdef _DEBUG
256             printf(" WIN32 Debug\n");
257 #else
258             printf(" WIN32 Release\n");
259 #endif
260 #endif
261 #if HAVE_BZLIB_H
262             printf("Using: libbzip2, (C) 1996-1999 Julian R Seward.  All rights reserved.\n");
263 #endif
264         }
265         else if (ret == 'v')
266             yaz_log_init_level (yaz_log_mask_str(arg));
267         else if (ret == 'l')
268             yaz_log_init_file (arg);
269         else if (ret == 'm')
270             res_set(res, "memMax", arg);
271         else if (ret == 'd')
272             database = arg;
273         else if (ret == 's')
274             res_set(res, "openRW", "0");
275         else if (ret == 'g')
276             res_set(res, "group", arg);
277         else if (ret == 'f')
278             res_set(res, "fileVerboseLimit", arg);
279         else if (ret == 'c')
280             configName = arg;
281         else if (ret == 't')
282             res_set(res, "recordType", arg);
283         else if (ret == 'n')
284             enable_commit = 0;
285         else if (ret == 'L')
286             res_set(res, "followLinks", "0");
287         else
288             yaz_log (YLOG_WARN, "unknown option '-%s'", arg);
289     } /* while arg */
290
291     if (trans_started)
292         if (zebra_end_trans (zh) != ZEBRA_OK)
293             yaz_log (YLOG_WARN, "zebra_end_trans failed");
294
295     zebra_close (zh);
296     zebra_stop (zs);
297 #if HAVE_SYS_TIMES_H
298 #if HAVE_SYS_TIME_H
299     if (trans_started)
300     {
301         gettimeofday(&end_time, 0);
302         usec = (end_time.tv_sec - start_time.tv_sec) * 1000000.0 +
303             end_time.tv_usec - start_time.tv_usec;
304         times(&tms2);
305         yaz_log (YLOG_LOG, "zebraidx times: %5.2f %5.2f %5.2f",
306                 usec / 1000000,
307                 (double) (tms2.tms_utime - tms1.tms_utime)/100,
308                 (double) (tms2.tms_stime - tms1.tms_stime)/100);
309     }
310 #endif
311 #endif
312     nmem_exit();
313     exit (0);
314     return 0;
315 }
316
317 /*
318  * Local variables:
319  * c-basic-offset: 4
320  * indent-tabs-mode: nil
321  * End:
322  * vim: shiftwidth=4 tabstop=8 expandtab
323  */
324