Towards 1.3.15
[idzebra-moved-to-github.git] / index / main.c
1 /* $Id: main.c,v 1.109 2004-01-15 14:22:22 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 "zebraapi.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 disableCommit = 0;
59     char *mem_max = 0;
60     
61     int trans_started=0;
62 #if HAVE_SYS_TIMES_H
63     struct tms tms1, tms2;
64     struct timeval start_time, end_time;
65     double usec;
66 #endif
67 #ifndef WIN32
68     char nbuf[100];
69 #endif
70     struct recordGroup rGroupDef;
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
86     rGroupDef.groupName = NULL;
87     rGroupDef.databaseName = NULL;
88     rGroupDef.path = NULL;
89     rGroupDef.recordId = NULL;
90     rGroupDef.recordType = NULL;
91     rGroupDef.flagStoreData = -1;
92     rGroupDef.flagStoreKeys = -1;
93     rGroupDef.flagRw = 1;
94     rGroupDef.databaseNamePath = 0;
95     rGroupDef.explainDatabase = 0;
96     rGroupDef.fileVerboseLimit = 100000;
97     rGroupDef.followLinks = -1;
98
99     prog = *argv;
100     if (argc < 2)
101     {
102         fprintf (stderr, "%s [options] command <dir> ...\n"
103         "Commands:\n"
104         " update <dir>  Update index with files below <dir>.\n"
105         "               If <dir> is empty filenames are read from stdin.\n"
106         " delete <dir>  Delete index with files below <dir>.\n"
107         " create <db>   Create database <db>\n"
108         " drop <db>     Drop database <db>\n"
109         " commit        Commit changes\n"
110         " clean         Clean shadow files\n"
111         "Options:\n"
112         " -t <type>     Index files as <type> (grs or text).\n"
113         " -c <config>   Read configuration file <config>.\n"
114         " -g <group>    Index files according to group settings.\n"
115         " -d <database> Records belong to Z39.50 database <database>.\n"
116         " -m <mbytes>   Use <mbytes> before flushing keys to disk.\n"
117         " -n            Don't use shadow system.\n"
118         " -s            Show analysis on stdout, but do no work.\n"
119         " -v <level>    Set logging to <level>.\n"
120         " -l <file>     Write log to <file>.\n"
121         " -L            Don't follow symbolic links.\n"
122         " -f <n>        Display information for the first <n> records.\n"
123         " -V            Show version.\n", *argv
124                  );
125         exit (1);
126     }
127     while ((ret = options ("sVt:c:g:d:m:v:nf:l:L"
128                            , argv, argc, &arg)) != -2)
129     {
130         if (ret == 0)
131         {
132             if(cmd == 0) /* command */
133             {
134                 if (!zs)
135                 {
136                     const char *config = configName ? configName : "zebra.cfg";
137                     logf (LOG_LOG, "Zebra version %s %s",
138                           ZEBRAVER, ZEBRADATE);
139                     zs = zebra_start (config);
140                     if (!zs)
141                     {
142                         yaz_log (LOG_FATAL, "Cannot read config %s", config);
143                         exit (1);
144                     }   
145                     zh = zebra_open (zs);
146                     if (disableCommit)
147                         zebra_shadow_enable (zh, 0);
148                 }
149
150                 if (rGroupDef.databaseName)
151                 {
152                     if (zebra_select_database (zh, rGroupDef.databaseName))
153                     {
154                         logf(LOG_FATAL, "Could not select database %s errCode=%d",
155                                           rGroupDef.databaseName, zebra_errCode(zh) );
156                         exit (1);
157                     }
158                 }
159                 else
160                 {
161                     if (zebra_select_database (zh, "Default"))
162                     {
163                         logf(LOG_FATAL, "Could not select database Default errCode=%d",
164                                         zebra_errCode(zh) );
165                         exit (1);
166                     }
167                 }
168                 if (mem_max)
169                     zebra_set_resource(zh, "memmax",mem_max); 
170
171                 if (!strcmp (arg, "update"))
172                     cmd = 'u';
173                 else if (!strcmp (arg, "update1"))
174                     cmd = 'U';
175                 else if (!strcmp (arg, "update2"))
176                     cmd = 'm';
177                 else if (!strcmp (arg, "dump"))
178                     cmd = 's';
179                 else if (!strcmp (arg, "del") || !strcmp(arg, "delete"))
180                     cmd = 'd';
181                 else if (!strcmp (arg, "init"))
182                 {
183                     zebra_init (zh);
184                 }
185                 else if (!strcmp(arg, "drop"))
186                 {
187                     cmd = 'D';
188                 }
189                 else if (!strcmp(arg, "create"))
190                 {
191                     cmd = 'C';
192                 }
193                 else if (!strcmp (arg, "commit"))
194                 {
195                     zebra_commit (zh);
196                 }
197                 else if (!strcmp (arg, "clean"))
198                 {
199                     zebra_clean (zh);
200                 }
201                 else if (!strcmp (arg, "stat") || !strcmp (arg, "status"))
202                 {
203                     zebra_register_statistics (zh,0);
204                 }
205                 else if (!strcmp (arg, "dumpdict"))
206                 {
207                     zebra_register_statistics (zh,1);
208                 }
209                 else if (!strcmp (arg, "compact"))
210                 {
211                     zebra_compact (zh);
212                 }
213                 else
214                 {
215                     logf (LOG_FATAL, "unknown command: %s", arg);
216                     exit (1);
217                 }
218             }
219             else
220             {
221                 rGroupDef.path = arg;
222                 zebra_set_group (zh, &rGroupDef);
223                 if (!trans_started)
224                 {
225                     trans_started=1;
226                     if (zebra_begin_trans (zh, 1))
227                         exit(1);
228                 }
229                 switch (cmd)
230                 {
231                 case 'u':
232                     zebra_repository_update (zh);
233                     break;
234                 case 'd':
235                     zebra_repository_delete (zh);
236                     break;
237                 case 's':
238                     logf (LOG_LOG, "dumping %s", rGroupDef.path);
239                     zebra_repository_show (zh);
240                     nsections = 0;
241                     break;
242                 case 'C':
243                     zebra_create_database(zh, rGroupDef.path);
244                     break;
245                 case 'D':
246                     zebra_drop_database(zh, rGroupDef.path);
247                     break;
248                 default:
249                     nsections = 0;
250                 }
251                 log_event_end (NULL, NULL);
252             }
253         }
254         else if (ret == 'V')
255         {
256             printf("Zebra %s %s\n", ZEBRAVER, ZEBRADATE);
257             printf(" (C) 1994-2004, Index Data ApS\n");
258 #ifdef WIN32
259 #ifdef _DEBUG
260             printf(" WIN32 Debug\n");
261 #else
262             printf(" WIN32 Release\n");
263 #endif
264 #endif
265 #if HAVE_BZLIB_H
266             printf("Using: libbzip2, (C) 1996-1999 Julian R Seward.  All rights reserved.\n");
267 #endif
268 #if HAVE_TCL_H
269             printf("Using: Tcl %s\n", TCL_VERSION);
270 #endif
271 #if HAVE_EXPAT_H
272             if (1)
273             {
274                 XML_Expat_Version v = XML_ExpatVersionInfo();
275                 printf ("Using: Expat %d.%d.%d\n",
276                         v.major, v.minor, v.micro);
277             }
278 #endif
279         }
280         else if (ret == 'v')
281             yaz_log_init_level (yaz_log_mask_str(arg));
282         else if (ret == 'l')
283             yaz_log_init_file (arg);
284         else if (ret == 'm')
285             mem_max = arg; 
286         else if (ret == 'd')
287             rGroupDef.databaseName = arg;
288         else if (ret == 's')
289             rGroupDef.flagRw = 0;
290         else if (ret == 'g')
291             rGroupDef.groupName = arg;
292         else if (ret == 'f')
293             rGroupDef.fileVerboseLimit = atoi(arg);
294         else if (ret == 'c')
295             configName = arg;
296         else if (ret == 't')
297             rGroupDef.recordType = arg;
298         else if (ret == 'n')
299             disableCommit = 1;
300         else if (ret == 'L')
301             rGroupDef.followLinks = 0;
302         else
303             logf (LOG_WARN, "unknown option '-%s'", arg);
304     } /* while arg */
305
306     if (trans_started)
307         zebra_end_trans (zh);
308
309     zebra_close (zh);
310     zebra_stop (zs);
311 #if HAVE_SYS_TIMES_H
312     if (trans_started)
313     {
314         gettimeofday(&end_time, 0);
315         usec = (end_time.tv_sec - start_time.tv_sec) * 1000000.0 +
316             end_time.tv_usec - start_time.tv_usec;
317         times(&tms2);
318         yaz_log (LOG_LOG, "zebraidx times: %5.2f %5.2f %5.2f",
319                 usec / 1000000,
320                 (double) (tms2.tms_utime - tms1.tms_utime)/100,
321                 (double) (tms2.tms_stime - tms1.tms_stime)/100);
322     }
323 #endif
324     nmem_exit();
325     exit (0);
326     return 0;
327 }
328