Added dumpdict command line option to dump the
[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.92 2002-07-11 13:03:01 heikki 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,0);
155                 }
156                 else if (!strcmp (arg, "dump") || !strcmp (arg, "dumpdict"))
157                 {
158                     zebra_register_statistics (zh,1);
159                 }
160                 else if (!strcmp (arg, "compact"))
161                 {
162                     zebra_compact (zh);
163                 }
164                 else
165                 {
166                     logf (LOG_FATAL, "unknown command: %s", arg);
167                     exit (1);
168                 }
169             }
170             else
171             {
172                 rGroupDef.path = arg;
173                 zebra_set_group (zh, &rGroupDef);
174                 zebra_begin_trans (zh);
175
176                 switch (cmd)
177                 {
178                 case 'u':
179                     zebra_repository_update (zh);
180                     break;
181                 case 'd':
182                     zebra_repository_delete (zh);
183                     break;
184                 case 's':
185                     logf (LOG_LOG, "dumping %s", rGroupDef.path);
186                     zebra_repository_show (zh);
187                     nsections = 0;
188                     break;
189                 default:
190                     nsections = 0;
191                 }
192                 cmd = 0;
193                 zebra_end_trans (zh);
194                 log_event_end (NULL, NULL);
195             }
196         }
197         else if (ret == 'V')
198         {
199 #if ZMBOL
200             fprintf (stderr, "Z'mbol %s %s\n", ZEBRAVER, ZEBRADATE);
201 #else
202             fprintf (stderr, "Zebra %s %s\n", ZEBRAVER, ZEBRADATE);
203 #endif
204             fprintf (stderr, " (C) 1994-2002, Index Data ApS\n");
205 #ifdef WIN32
206 #ifdef _DEBUG
207             fprintf (stderr, " WIN32 Debug\n");
208 #else
209             fprintf (stderr, " WIN32 Release\n");
210 #endif
211 #endif
212 #if HAVE_BZLIB_H
213             fprintf (stderr, "libbzip2\n"
214                      " (C) 1996-1999 Julian R Seward.  All rights reserved.\n");
215 #endif
216         }
217         else if (ret == 'v')
218             yaz_log_init_level (yaz_log_mask_str(arg));
219         else if (ret == 'l')
220             yaz_log_init_file (arg);
221         else if (ret == 'm')
222             mem_max = 1024*1024*atoi(arg);
223         else if (ret == 'd')
224             rGroupDef.databaseName = arg;
225         else if (ret == 's')
226             rGroupDef.flagRw = 0;
227         else if (ret == 'g')
228             rGroupDef.groupName = arg;
229         else if (ret == 'f')
230             rGroupDef.fileVerboseLimit = atoi(arg);
231         else if (ret == 'c')
232             configName = arg;
233         else if (ret == 't')
234             rGroupDef.recordType = arg;
235         else if (ret == 'n')
236             disableCommit = 1;
237         else
238             logf (LOG_WARN, "unknown option '-%s'", arg);
239     }
240     zebra_close (zh);
241     zebra_stop (zs);
242 #if HAVE_SYS_TIMES_H
243     gettimeofday(&end_time, 0);
244     usec = (end_time.tv_sec - start_time.tv_sec) * 1000000L +
245             end_time.tv_usec - start_time.tv_usec;
246     times(&tms2);
247     yaz_log (LOG_LOG, "zebraidx times: %5.2f %5.2f %5.2f",
248                 (double) usec / 1000000.0,
249                 (double) (tms2.tms_utime - tms1.tms_utime)/100,
250                 (double) (tms2.tms_stime - tms1.tms_stime)/100);
251 #endif
252     exit (0);
253     return 0;
254 }
255