Version info to stdout.
[idzebra-moved-to-github.git] / index / main.c
1 /* $Id: main.c,v 1.98 2002-10-04 18:15:09 adam Exp $
2    Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002
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 #include <yaz/data1.h>
39 #include "zebraapi.h"
40
41 char *prog;
42
43 int main (int argc, char **argv)
44 {
45     int ret;
46     int cmd = 0;
47     char *arg;
48     char *configName = 0;
49     int nsections = 0;
50     int disableCommit = 0;
51     size_t mem_max = 0;
52     int trans_started=0;
53 #if HAVE_SYS_TIMES_H
54     struct tms tms1, tms2;
55     struct timeval start_time, end_time;
56     long usec;
57 #endif
58 #ifndef WIN32
59     char nbuf[100];
60 #endif
61     struct recordGroup rGroupDef;
62     ZebraService zs = 0;
63     ZebraHandle zh = 0;
64
65     nmem_init ();
66
67 #ifdef WIN32
68 #else
69     sprintf(nbuf, "%.40s(%d)", *argv, getpid());
70     yaz_log_init_prefix (nbuf);
71 #endif
72 #if HAVE_SYS_TIMES_H
73     times(&tms1);
74     gettimeofday(&start_time, 0);
75 #endif
76
77     rGroupDef.groupName = NULL;
78     rGroupDef.databaseName = NULL;
79     rGroupDef.path = NULL;
80     rGroupDef.recordId = NULL;
81     rGroupDef.recordType = NULL;
82     rGroupDef.flagStoreData = -1;
83     rGroupDef.flagStoreKeys = -1;
84     rGroupDef.flagRw = 1;
85     rGroupDef.databaseNamePath = 0;
86     rGroupDef.explainDatabase = 0;
87     rGroupDef.fileVerboseLimit = 100000;
88     rGroupDef.followLinks = -1;
89
90     prog = *argv;
91     if (argc < 2)
92     {
93         fprintf (stderr, "%s [options] command <dir> ...\n"
94         "Commands:\n"
95         " update <dir>  Update index with files below <dir>.\n"
96         "               If <dir> is empty filenames are read from stdin.\n"
97         " delete <dir>  Delete index with files below <dir>.\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"
117                            , argv, argc, &arg)) != -2)
118     {
119         if (ret == 0)
120         {
121             if(cmd == 0) /* command */
122             {
123                 if (!zs)
124                 {
125                     const char *config = configName ? configName : "zebra.cfg";
126                     logf (LOG_LOG, "Zebra version %s %s",
127                           ZEBRAVER, ZEBRADATE);
128                     zs = zebra_start (config);
129                     if (!zs)
130                     {
131                         yaz_log (LOG_FATAL, "Cannot read config %s", config);
132                         exit (1);
133                     }   
134                     zh = zebra_open (zs);
135                     if (disableCommit)
136                         zebra_shadow_enable (zh, 0);
137                 }
138                 if (rGroupDef.databaseName)
139                 {
140                     if (zebra_select_database (zh, rGroupDef.databaseName))
141                         exit (1);
142                 }
143                 else
144                 {
145                     if (zebra_select_database (zh, "Default"))
146                         exit (1);
147                 }
148
149                 if (!strcmp (arg, "update"))
150                     cmd = 'u';
151                 else if (!strcmp (arg, "update1"))
152                     cmd = 'U';
153                 else if (!strcmp (arg, "update2"))
154                     cmd = 'm';
155                 else if (!strcmp (arg, "dump"))
156                     cmd = 's';
157                 else if (!strcmp (arg, "del") || !strcmp(arg, "delete"))
158                     cmd = 'd';
159                 else if (!strcmp (arg, "init"))
160                 {
161                     zebra_init (zh);
162                 }
163                 else if (!strcmp (arg, "commit"))
164                 {
165                     zebra_commit (zh);
166                 }
167                 else if (!strcmp (arg, "clean"))
168                 {
169                     assert (!"todo");
170                 }
171                 else if (!strcmp (arg, "stat") || !strcmp (arg, "status"))
172                 {
173                     zebra_register_statistics (zh,0);
174                 }
175                 else if (!strcmp (arg, "dump") || !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                 rGroupDef.path = arg;
192                 zebra_set_group (zh, &rGroupDef);
193                 if (!trans_started)
194                 {
195                     trans_started=1;
196                     zebra_begin_trans (zh);
197                 }
198
199                 switch (cmd)
200                 {
201                 case 'u':
202                     zebra_repository_update (zh);
203                     break;
204                 case 'd':
205                     zebra_repository_delete (zh);
206                     break;
207                 case 's':
208                     logf (LOG_LOG, "dumping %s", rGroupDef.path);
209                     zebra_repository_show (zh);
210                     nsections = 0;
211                     break;
212                 default:
213                     nsections = 0;
214                 }
215                 log_event_end (NULL, NULL);
216             }
217         }
218         else if (ret == 'V')
219         {
220             printf("Zebra %s %s\n", ZEBRAVER, ZEBRADATE);
221             printf(" (C) 1994-2002, Index Data ApS\n");
222 #ifdef WIN32
223 #ifdef _DEBUG
224             printf(" WIN32 Debug\n");
225 #else
226             printf(" WIN32 Release\n");
227 #endif
228 #endif
229 #if HAVE_BZLIB_H
230             printf("libbzip2\n"
231                    " (C) 1996-1999 Julian R Seward.  All rights reserved.\n");
232 #endif
233         }
234         else if (ret == 'v')
235             yaz_log_init_level (yaz_log_mask_str(arg));
236         else if (ret == 'l')
237             yaz_log_init_file (arg);
238         else if (ret == 'm')
239             mem_max = 1024*1024*atoi(arg);
240         else if (ret == 'd')
241             rGroupDef.databaseName = arg;
242         else if (ret == 's')
243             rGroupDef.flagRw = 0;
244         else if (ret == 'g')
245             rGroupDef.groupName = arg;
246         else if (ret == 'f')
247             rGroupDef.fileVerboseLimit = atoi(arg);
248         else if (ret == 'c')
249             configName = arg;
250         else if (ret == 't')
251             rGroupDef.recordType = arg;
252         else if (ret == 'n')
253             disableCommit = 1;
254         else if (ret == 'L')
255             rGroupDef.followLinks = 0;
256         else
257             logf (LOG_WARN, "unknown option '-%s'", arg);
258     } /* while arg */
259
260     if (trans_started)
261         zebra_end_trans (zh);
262
263     zebra_close (zh);
264     zebra_stop (zs);
265 #if HAVE_SYS_TIMES_H
266     if (trans_started)
267     {
268         gettimeofday(&end_time, 0);
269         usec = (end_time.tv_sec - start_time.tv_sec) * 1000000L +
270             end_time.tv_usec - start_time.tv_usec;
271         times(&tms2);
272         yaz_log (LOG_LOG, "zebraidx times: %5.2f %5.2f %5.2f",
273                 (double) usec / 1000000.0,
274                 (double) (tms2.tms_utime - tms1.tms_utime)/100,
275                 (double) (tms2.tms_stime - tms1.tms_stime)/100);
276     }
277 #endif
278     exit (0);
279     return 0;
280 }
281