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