Display Tcl version if enabled
[idzebra-moved-to-github.git] / index / main.c
1 /* $Id: main.c,v 1.106 2003-09-16 13:58:25 adam Exp $
2    Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003
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 #include <data1.h>
43 #include "zebraapi.h"
44
45 char *prog;
46
47 int main (int argc, char **argv)
48 {
49     int ret;
50     int cmd = 0;
51     char *arg;
52     char *configName = 0;
53     int nsections = 0;
54     int disableCommit = 0;
55     char *mem_max = 0;
56     
57     int trans_started=0;
58 #if HAVE_SYS_TIMES_H
59     struct tms tms1, tms2;
60     struct timeval start_time, end_time;
61     long usec;
62 #endif
63 #ifndef WIN32
64     char nbuf[100];
65 #endif
66     struct recordGroup rGroupDef;
67     ZebraService zs = 0;
68     ZebraHandle zh = 0;
69
70     nmem_init ();
71
72 #ifdef WIN32
73 #else
74     sprintf(nbuf, "%.40s(%d)", *argv, getpid());
75     yaz_log_init_prefix (nbuf);
76 #endif
77 #if HAVE_SYS_TIMES_H
78     times(&tms1);
79     gettimeofday(&start_time, 0);
80 #endif
81
82     rGroupDef.groupName = NULL;
83     rGroupDef.databaseName = NULL;
84     rGroupDef.path = NULL;
85     rGroupDef.recordId = NULL;
86     rGroupDef.recordType = NULL;
87     rGroupDef.flagStoreData = -1;
88     rGroupDef.flagStoreKeys = -1;
89     rGroupDef.flagRw = 1;
90     rGroupDef.databaseNamePath = 0;
91     rGroupDef.explainDatabase = 0;
92     rGroupDef.fileVerboseLimit = 100000;
93     rGroupDef.followLinks = -1;
94
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"
124                            , argv, argc, &arg)) != -2)
125     {
126         if (ret == 0)
127         {
128             if(cmd == 0) /* command */
129             {
130                 if (!zs)
131                 {
132                     const char *config = configName ? configName : "zebra.cfg";
133                     logf (LOG_LOG, "Zebra version %s %s",
134                           ZEBRAVER, ZEBRADATE);
135                     zs = zebra_start (config);
136                     if (!zs)
137                     {
138                         yaz_log (LOG_FATAL, "Cannot read config %s", config);
139                         exit (1);
140                     }   
141                     zh = zebra_open (zs);
142                     if (disableCommit)
143                         zebra_shadow_enable (zh, 0);
144                 }
145
146                 if (rGroupDef.databaseName)
147                 {
148                     if (zebra_select_database (zh, rGroupDef.databaseName))
149                     {
150                         logf(LOG_FATAL, "Could not select database %s errCode=%d",
151                                           rGroupDef.databaseName, zebra_errCode(zh) );
152                         exit (1);
153                     }
154                 }
155                 else
156                 {
157                     if (zebra_select_database (zh, "Default"))
158                     {
159                         logf(LOG_FATAL, "Could not select database Default errCode=%d",
160                                         zebra_errCode(zh) );
161                         exit (1);
162                     }
163                 }
164                 if (mem_max)
165                     zebra_set_resource(zh, "memmax",mem_max); 
166
167                 if (!strcmp (arg, "update"))
168                     cmd = 'u';
169                 else if (!strcmp (arg, "update1"))
170                     cmd = 'U';
171                 else if (!strcmp (arg, "update2"))
172                     cmd = 'm';
173                 else if (!strcmp (arg, "dump"))
174                     cmd = 's';
175                 else if (!strcmp (arg, "del") || !strcmp(arg, "delete"))
176                     cmd = 'd';
177                 else if (!strcmp (arg, "init"))
178                 {
179                     zebra_init (zh);
180                 }
181                 else if (!strcmp(arg, "drop"))
182                 {
183                     cmd = 'D';
184                 }
185                 else if (!strcmp(arg, "create"))
186                 {
187                     cmd = 'C';
188                 }
189                 else if (!strcmp (arg, "commit"))
190                 {
191                     zebra_commit (zh);
192                 }
193                 else if (!strcmp (arg, "clean"))
194                 {
195                     zebra_clean (zh);
196                 }
197                 else if (!strcmp (arg, "stat") || !strcmp (arg, "status"))
198                 {
199                     zebra_register_statistics (zh,0);
200                 }
201                 else if (!strcmp (arg, "dumpdict"))
202                 {
203                     zebra_register_statistics (zh,1);
204                 }
205                 else if (!strcmp (arg, "compact"))
206                 {
207                     zebra_compact (zh);
208                 }
209                 else
210                 {
211                     logf (LOG_FATAL, "unknown command: %s", arg);
212                     exit (1);
213                 }
214             }
215             else
216             {
217                 rGroupDef.path = arg;
218                 zebra_set_group (zh, &rGroupDef);
219                 if (!trans_started)
220                 {
221                     trans_started=1;
222                     if (zebra_begin_trans (zh, 1))
223                         exit(1);
224                 }
225                 switch (cmd)
226                 {
227                 case 'u':
228                     zebra_repository_update (zh);
229                     break;
230                 case 'd':
231                     zebra_repository_delete (zh);
232                     break;
233                 case 's':
234                     logf (LOG_LOG, "dumping %s", rGroupDef.path);
235                     zebra_repository_show (zh);
236                     nsections = 0;
237                     break;
238                 case 'C':
239                     zebra_create_database(zh, rGroupDef.path);
240                     break;
241                 case 'D':
242                     zebra_drop_database(zh, rGroupDef.path);
243                     break;
244                 default:
245                     nsections = 0;
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-2003, 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 #if HAVE_TCL_H
265             printf("Using: Tcl %s\n", TCL_VERSION);
266 #endif
267         }
268         else if (ret == 'v')
269             yaz_log_init_level (yaz_log_mask_str(arg));
270         else if (ret == 'l')
271             yaz_log_init_file (arg);
272         else if (ret == 'm')
273             mem_max = arg; 
274         else if (ret == 'd')
275             rGroupDef.databaseName = arg;
276         else if (ret == 's')
277             rGroupDef.flagRw = 0;
278         else if (ret == 'g')
279             rGroupDef.groupName = arg;
280         else if (ret == 'f')
281             rGroupDef.fileVerboseLimit = atoi(arg);
282         else if (ret == 'c')
283             configName = arg;
284         else if (ret == 't')
285             rGroupDef.recordType = arg;
286         else if (ret == 'n')
287             disableCommit = 1;
288         else if (ret == 'L')
289             rGroupDef.followLinks = 0;
290         else
291             logf (LOG_WARN, "unknown option '-%s'", arg);
292     } /* while arg */
293
294     if (trans_started)
295         zebra_end_trans (zh);
296
297     zebra_close (zh);
298     zebra_stop (zs);
299 #if HAVE_SYS_TIMES_H
300     if (trans_started)
301     {
302         gettimeofday(&end_time, 0);
303         usec = (end_time.tv_sec - start_time.tv_sec) * 1000000L +
304             end_time.tv_usec - start_time.tv_usec;
305         times(&tms2);
306         yaz_log (LOG_LOG, "zebraidx times: %5.2f %5.2f %5.2f",
307                 (double) usec / 1000000.0,
308                 (double) (tms2.tms_utime - tms1.tms_utime)/100,
309                 (double) (tms2.tms_stime - tms1.tms_stime)/100);
310     }
311 #endif
312     nmem_exit();
313     exit (0);
314     return 0;
315 }
316