Add include of sys/time.h
[idzebra-moved-to-github.git] / index / main.c
1 /*
2  * Copyright (C) 1994-2002, Index Data
3  * All rights reserved.
4  *
5  * $Id: main.c,v 1.91 2002-06-19 13:19:43 adam Exp $
6  */
7 #include <stdio.h>
8 #include <string.h>
9 #include <assert.h>
10 #ifdef WIN32
11 #include <io.h>
12 #else
13 #include <unistd.h>
14 #include <sys/time.h>
15 #endif
16 #include <time.h>
17 #if HAVE_SYS_TIMES_H
18 #include <sys/times.h>
19 #endif
20
21 #include <yaz/data1.h>
22 #include "zebraapi.h"
23
24 char *prog;
25
26 int main (int argc, char **argv)
27 {
28     int ret;
29     int cmd = 0;
30     char *arg;
31     char *configName = 0;
32     int nsections = 0;
33     int disableCommit = 0;
34     size_t mem_max = 0;
35 #if HAVE_SYS_TIMES_H
36     struct tms tms1, tms2;
37     struct timeval start_time, end_time;
38     long usec;
39 #endif
40 #ifndef WIN32
41     char nbuf[100];
42 #endif
43     struct recordGroup rGroupDef;
44     ZebraService zs = 0;
45     ZebraHandle zh = 0;
46
47     nmem_init ();
48
49 #ifdef WIN32
50 #else
51     sprintf(nbuf, "%.40s(%d)", *argv, getpid());
52     yaz_log_init_prefix (nbuf);
53 #endif
54 #if HAVE_SYS_TIMES_H
55     times(&tms1);
56     gettimeofday(&start_time, 0);
57 #endif
58
59     rGroupDef.groupName = NULL;
60     rGroupDef.databaseName = NULL;
61     rGroupDef.path = NULL;
62     rGroupDef.recordId = NULL;
63     rGroupDef.recordType = NULL;
64     rGroupDef.flagStoreData = -1;
65     rGroupDef.flagStoreKeys = -1;
66     rGroupDef.flagRw = 1;
67     rGroupDef.databaseNamePath = 0;
68     rGroupDef.explainDatabase = 0;
69     rGroupDef.fileVerboseLimit = 100000;
70
71     prog = *argv;
72     if (argc < 2)
73     {
74         fprintf (stderr, "%s [options] command <dir> ...\n"
75         "Commands:\n"
76         " update <dir>  Update index with files below <dir>.\n"
77         "               If <dir> is empty filenames are read from stdin.\n"
78         " delete <dir>  Delete index with files below <dir>.\n"
79         " commit        Commit changes\n"
80         " clean         Clean shadow files\n"
81         "Options:\n"
82         " -t <type>     Index files as <type> (grs or text).\n"
83         " -c <config>   Read configuration file <config>.\n"
84         " -g <group>    Index files according to group settings.\n"
85         " -d <database> Records belong to Z39.50 database <database>.\n"
86         " -m <mbytes>   Use <mbytes> before flushing keys to disk.\n"
87         " -n            Don't use shadow system.\n"
88         " -s            Show analysis on stdout, but do no work.\n"
89         " -v <level>    Set logging to <level>.\n"
90         " -l <file>     Write log to <file>.\n"
91         " -f <n>        Display information for the first <n> records.\n"
92         " -V            Show version.\n", *argv
93                  );
94         exit (1);
95     }
96     while ((ret = options ("sVt:c:g:d:m:v:nf:l:"
97                            , argv, argc, &arg)) != -2)
98     {
99         if (ret == 0)
100         {
101             if(cmd == 0) /* command */
102             {
103                 if (!zs)
104                 {
105 #if ZMBOL
106                     logf (LOG_LOG, "Z'mbol version %s %s",
107                           ZEBRAVER, ZEBRADATE);
108 #else
109                     logf (LOG_LOG, "Zebra version %s %s",
110                           ZEBRAVER, ZEBRADATE);
111 #endif
112                     zs = zebra_start (configName ? configName : "zebra.cfg");
113                     if (!zs)
114                         exit (1);
115                     zh = zebra_open (zs);
116                     if (disableCommit)
117                         zebra_shadow_enable (zh, 0);
118                 }
119                 if (rGroupDef.databaseName)
120                 {
121                     if (zebra_select_database (zh, rGroupDef.databaseName))
122                         exit (1);
123                 }
124                 else
125                 {
126                     if (zebra_select_database (zh, "Default"))
127                         exit (1);
128                 }
129
130                 if (!strcmp (arg, "update"))
131                     cmd = 'u';
132                 else if (!strcmp (arg, "update1"))
133                     cmd = 'U';
134                 else if (!strcmp (arg, "update2"))
135                     cmd = 'm';
136                 else if (!strcmp (arg, "dump"))
137                     cmd = 's';
138                 else if (!strcmp (arg, "del") || !strcmp(arg, "delete"))
139                     cmd = 'd';
140                 else if (!strcmp (arg, "init"))
141                 {
142                     zebra_init (zh);
143                 }
144                 else if (!strcmp (arg, "commit"))
145                 {
146                     zebra_commit (zh);
147                 }
148                 else if (!strcmp (arg, "clean"))
149                 {
150                     assert (!"todo");
151                 }
152                 else if (!strcmp (arg, "stat") || !strcmp (arg, "status"))
153                 {
154                     zebra_register_statistics (zh);
155                 }
156                 else if (!strcmp (arg, "compact"))
157                 {
158                     zebra_compact (zh);
159                 }
160                 else
161                 {
162                     logf (LOG_FATAL, "unknown command: %s", arg);
163                     exit (1);
164                 }
165             }
166             else
167             {
168                 rGroupDef.path = arg;
169                 zebra_set_group (zh, &rGroupDef);
170                 zebra_begin_trans (zh);
171
172                 switch (cmd)
173                 {
174                 case 'u':
175                     zebra_repository_update (zh);
176                     break;
177                 case 'd':
178                     zebra_repository_delete (zh);
179                     break;
180                 case 's':
181                     logf (LOG_LOG, "dumping %s", rGroupDef.path);
182                     zebra_repository_show (zh);
183                     nsections = 0;
184                     break;
185                 default:
186                     nsections = 0;
187                 }
188                 cmd = 0;
189                 zebra_end_trans (zh);
190                 log_event_end (NULL, NULL);
191             }
192         }
193         else if (ret == 'V')
194         {
195 #if ZMBOL
196             fprintf (stderr, "Z'mbol %s %s\n", ZEBRAVER, ZEBRADATE);
197 #else
198             fprintf (stderr, "Zebra %s %s\n", ZEBRAVER, ZEBRADATE);
199 #endif
200             fprintf (stderr, " (C) 1994-2002, Index Data ApS\n");
201 #ifdef WIN32
202 #ifdef _DEBUG
203             fprintf (stderr, " WIN32 Debug\n");
204 #else
205             fprintf (stderr, " WIN32 Release\n");
206 #endif
207 #endif
208 #if HAVE_BZLIB_H
209             fprintf (stderr, "libbzip2\n"
210                      " (C) 1996-1999 Julian R Seward.  All rights reserved.\n");
211 #endif
212         }
213         else if (ret == 'v')
214             yaz_log_init_level (yaz_log_mask_str(arg));
215         else if (ret == 'l')
216             yaz_log_init_file (arg);
217         else if (ret == 'm')
218             mem_max = 1024*1024*atoi(arg);
219         else if (ret == 'd')
220             rGroupDef.databaseName = arg;
221         else if (ret == 's')
222             rGroupDef.flagRw = 0;
223         else if (ret == 'g')
224             rGroupDef.groupName = arg;
225         else if (ret == 'f')
226             rGroupDef.fileVerboseLimit = atoi(arg);
227         else if (ret == 'c')
228             configName = arg;
229         else if (ret == 't')
230             rGroupDef.recordType = arg;
231         else if (ret == 'n')
232             disableCommit = 1;
233         else
234             logf (LOG_WARN, "unknown option '-%s'", arg);
235     }
236     zebra_close (zh);
237     zebra_stop (zs);
238 #if HAVE_SYS_TIMES_H
239     gettimeofday(&end_time, 0);
240     usec = (end_time.tv_sec - start_time.tv_sec) * 1000000L +
241             end_time.tv_usec - start_time.tv_usec;
242     times(&tms2);
243     yaz_log (LOG_LOG, "zebraidx times: %5.2f %5.2f %5.2f",
244                 (double) usec / 1000000.0,
245                 (double) (tms2.tms_utime - tms1.tms_utime)/100,
246                 (double) (tms2.tms_stime - tms1.tms_stime)/100);
247 #endif
248     exit (0);
249     return 0;
250 }
251