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