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