8a1dac47cdf50da28ab504be6085b3524c3eb151
[idzebra-moved-to-github.git] / index / main.c
1 /* $Id: main.c,v 1.114 2004-09-26 20:38:50 adam Exp $
2    Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003,2004
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
24 #include <stdio.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 #if HAVE_TCL_H
39 #include <tcl.h>
40 #endif
41
42 #if HAVE_EXPAT_H
43 #include <expat.h>
44 #endif
45
46 #include <data1.h>
47 #include <idzebra/api.h>
48
49 char *prog;
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 = "Default";
60     Res res = res_open(0, 0, 0);
61     
62     int trans_started=0;
63 #if HAVE_SYS_TIMES_H
64     struct tms tms1, tms2;
65     struct timeval start_time, end_time;
66     double usec;
67 #endif
68 #ifndef WIN32
69     char nbuf[100];
70 #endif
71     ZebraService zs = 0;
72     ZebraHandle zh = 0;
73
74     nmem_init ();
75
76 #ifdef WIN32
77 #else
78     sprintf(nbuf, "%.40s(%d)", *argv, getpid());
79     yaz_log_init_prefix (nbuf);
80 #endif
81 #if HAVE_SYS_TIMES_H
82     times(&tms1);
83     gettimeofday(&start_time, 0);
84 #endif
85     prog = *argv;
86     if (argc < 2)
87     {
88         fprintf (stderr, "%s [options] command <dir> ...\n"
89         "Commands:\n"
90         " update <dir>  Update index with files below <dir>.\n"
91         "               If <dir> is empty filenames are read from stdin.\n"
92         " delete <dir>  Delete index with files below <dir>.\n"
93         " create <db>   Create database <db>\n"
94         " drop <db>     Drop database <db>\n"
95         " commit        Commit changes\n"
96         " clean         Clean shadow files\n"
97         "Options:\n"
98         " -t <type>     Index files as <type> (grs or text).\n"
99         " -c <config>   Read configuration file <config>.\n"
100         " -g <group>    Index files according to group settings.\n"
101         " -d <database> Records belong to Z39.50 database <database>.\n"
102         " -m <mbytes>   Use <mbytes> before flushing keys to disk.\n"
103         " -n            Don't use shadow system.\n"
104         " -s            Show analysis on stdout, but do no work.\n"
105         " -v <level>    Set logging to <level>.\n"
106         " -l <file>     Write log to <file>.\n"
107         " -L            Don't follow symbolic links.\n"
108         " -f <n>        Display information for the first <n> records.\n"
109         " -V            Show version.\n", *argv
110                  );
111         exit (1);
112     }
113     while ((ret = options ("sVt:c:g:d:m:v:nf:l:L"
114                            , argv, argc, &arg)) != -2)
115     {
116         if (ret == 0)
117         {
118             if(cmd == 0) /* command */
119             {
120                 if (!zs)
121                 {
122                     const char *config = configName ? configName : "zebra.cfg";
123                     logf (LOG_LOG, "Zebra version %s %s",
124                           ZEBRAVER, ZEBRADATE);
125                     zs = zebra_start_res (config, 0, res);
126                     if (!zs)
127                     {
128                         yaz_log (LOG_FATAL, "Cannot read config %s", config);
129                         exit (1);
130                     }   
131                     zh = zebra_open (zs);
132                     zebra_shadow_enable (zh, enable_commit);
133                 }
134
135                 if (zebra_select_database (zh, database))
136                 {
137                     logf(LOG_FATAL, "Could not select database %s errCode=%d",
138                          database, zebra_errCode(zh) );
139                     exit (1);
140                 }
141                 if (!strcmp (arg, "update"))
142                     cmd = 'u';
143                 else if (!strcmp (arg, "update1"))
144                     cmd = 'U';
145                 else if (!strcmp (arg, "update2"))
146                     cmd = 'm';
147                 else if (!strcmp (arg, "dump"))
148                     cmd = 's';
149                 else if (!strcmp (arg, "del") || !strcmp(arg, "delete"))
150                     cmd = 'd';
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
184                 {
185                     logf (LOG_FATAL, "unknown command: %s", arg);
186                     exit (1);
187                 }
188             }
189             else
190             {
191                 if (!trans_started)
192                 {
193                     trans_started=1;
194                     if (zebra_begin_trans (zh, 1))
195                         exit(1);
196                 }
197                 switch (cmd)
198                 {
199                 case 'u':
200                     zebra_repository_update (zh, arg);
201                     break;
202                 case 'd':
203                     zebra_repository_delete (zh, arg);
204                     break;
205                 case 's':
206                     zebra_repository_show (zh, arg);
207                     nsections = 0;
208                     break;
209                 case 'C':
210                     zebra_create_database(zh, arg);
211                     break;
212                 case 'D':
213                     zebra_drop_database(zh, arg);
214                     break;
215                 default:
216                     nsections = 0;
217                 }
218                 log_event_end (NULL, NULL);
219             }
220         }
221         else if (ret == 'V')
222         {
223             printf("Zebra %s %s\n", ZEBRAVER, ZEBRADATE);
224             printf(" (C) 1994-2004, Index Data ApS\n");
225 #ifdef WIN32
226 #ifdef _DEBUG
227             printf(" WIN32 Debug\n");
228 #else
229             printf(" WIN32 Release\n");
230 #endif
231 #endif
232 #if HAVE_BZLIB_H
233             printf("Using: libbzip2, (C) 1996-1999 Julian R Seward.  All rights reserved.\n");
234 #endif
235 #if HAVE_TCL_H
236             printf("Using: Tcl %s\n", TCL_VERSION);
237 #endif
238 #if HAVE_EXPAT_H
239             if (1)
240             {
241                 XML_Expat_Version v = XML_ExpatVersionInfo();
242                 printf ("Using: Expat %d.%d.%d\n",
243                         v.major, v.minor, v.micro);
244             }
245 #endif
246         }
247         else if (ret == 'v')
248             yaz_log_init_level (yaz_log_mask_str(arg));
249         else if (ret == 'l')
250             yaz_log_init_file (arg);
251         else if (ret == 'm')
252             res_set(res, "memMax", arg);
253         else if (ret == 'd')
254             database = arg;
255         else if (ret == 's')
256             res_set(res, "openRW", "0");
257         else if (ret == 'g')
258             res_set(res, "group", arg);
259         else if (ret == 'f')
260             res_set(res, "fileVerboseLimit", arg);
261         else if (ret == 'c')
262             configName = arg;
263         else if (ret == 't')
264             res_set(res, "recordType", arg);
265         else if (ret == 'n')
266             enable_commit = 0;
267         else if (ret == 'L')
268             res_set(res, "followLinks", "0");
269         else
270             logf (LOG_WARN, "unknown option '-%s'", arg);
271     } /* while arg */
272
273     if (trans_started)
274         zebra_end_trans (zh);
275
276     zebra_close (zh);
277     zebra_stop (zs);
278 #if HAVE_SYS_TIMES_H
279     if (trans_started)
280     {
281         gettimeofday(&end_time, 0);
282         usec = (end_time.tv_sec - start_time.tv_sec) * 1000000.0 +
283             end_time.tv_usec - start_time.tv_usec;
284         times(&tms2);
285         yaz_log (LOG_LOG, "zebraidx times: %5.2f %5.2f %5.2f",
286                 usec / 1000000,
287                 (double) (tms2.tms_utime - tms1.tms_utime)/100,
288                 (double) (tms2.tms_stime - tms1.tms_stime)/100);
289     }
290 #endif
291     nmem_exit();
292     exit (0);
293     return 0;
294 }
295