b86f13bc65b04485f982ade634a9ea34309687a7
[idzebra-moved-to-github.git] / index / main.c
1 /* $Id: main.c,v 1.99 2002-10-22 09:37:55 heikki 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     char *mem_max = 0;
52     
53     int trans_started=0;
54 #if HAVE_SYS_TIMES_H
55     struct tms tms1, tms2;
56     struct timeval start_time, end_time;
57     long usec;
58 #endif
59 #ifndef WIN32
60     char nbuf[100];
61 #endif
62     struct recordGroup rGroupDef;
63     ZebraService zs = 0;
64     ZebraHandle zh = 0;
65
66     nmem_init ();
67
68 #ifdef WIN32
69 #else
70     sprintf(nbuf, "%.40s(%d)", *argv, getpid());
71     yaz_log_init_prefix (nbuf);
72 #endif
73 #if HAVE_SYS_TIMES_H
74     times(&tms1);
75     gettimeofday(&start_time, 0);
76 #endif
77
78     rGroupDef.groupName = NULL;
79     rGroupDef.databaseName = NULL;
80     rGroupDef.path = NULL;
81     rGroupDef.recordId = NULL;
82     rGroupDef.recordType = NULL;
83     rGroupDef.flagStoreData = -1;
84     rGroupDef.flagStoreKeys = -1;
85     rGroupDef.flagRw = 1;
86     rGroupDef.databaseNamePath = 0;
87     rGroupDef.explainDatabase = 0;
88     rGroupDef.fileVerboseLimit = 100000;
89     rGroupDef.followLinks = -1;
90
91     prog = *argv;
92     if (argc < 2)
93     {
94         fprintf (stderr, "%s [options] command <dir> ...\n"
95         "Commands:\n"
96         " update <dir>  Update index with files below <dir>.\n"
97         "               If <dir> is empty filenames are read from stdin.\n"
98         " delete <dir>  Delete index with files below <dir>.\n"
99         " commit        Commit changes\n"
100         " clean         Clean shadow files\n"
101         "Options:\n"
102         " -t <type>     Index files as <type> (grs or text).\n"
103         " -c <config>   Read configuration file <config>.\n"
104         " -g <group>    Index files according to group settings.\n"
105         " -d <database> Records belong to Z39.50 database <database>.\n"
106         " -m <mbytes>   Use <mbytes> before flushing keys to disk.\n"
107         " -n            Don't use shadow system.\n"
108         " -s            Show analysis on stdout, but do no work.\n"
109         " -v <level>    Set logging to <level>.\n"
110         " -l <file>     Write log to <file>.\n"
111         " -L            Don't follow symbolic links.\n"
112         " -f <n>        Display information for the first <n> records.\n"
113         " -V            Show version.\n", *argv
114                  );
115         exit (1);
116     }
117     while ((ret = options ("sVt:c:g:d:m:v:nf:l:L"
118                            , argv, argc, &arg)) != -2)
119     {
120         if (ret == 0)
121         {
122             if(cmd == 0) /* command */
123             {
124                 if (!zs)
125                 {
126                     const char *config = configName ? configName : "zebra.cfg";
127                     logf (LOG_LOG, "Zebra version %s %s",
128                           ZEBRAVER, ZEBRADATE);
129                     zs = zebra_start (config);
130                     if (!zs)
131                     {
132                         yaz_log (LOG_FATAL, "Cannot read config %s", config);
133                         exit (1);
134                     }   
135                     zh = zebra_open (zs);
136                     if (disableCommit)
137                         zebra_shadow_enable (zh, 0);
138                 }
139
140                 if (rGroupDef.databaseName)
141                 {
142                     if (zebra_select_database (zh, rGroupDef.databaseName))
143                     {
144                         logf(LOG_FATAL, "Could not select database %s errCode=%d",
145                                           rGroupDef.databaseName, zebra_errCode(zh) );
146                         exit (1);
147                     }
148                 }
149                 else
150                 {
151                     if (zebra_select_database (zh, "Default"))
152                     {
153                         logf(LOG_FATAL, "Could not select database Default errCode=%d",
154                                         zebra_errCode(zh) );
155                         exit (1);
156                     }
157                 }
158                 if (mem_max)
159                     zebra_set_resource(zh, "memmax",mem_max); 
160
161                 if (!strcmp (arg, "update"))
162                     cmd = 'u';
163                 else if (!strcmp (arg, "update1"))
164                     cmd = 'U';
165                 else if (!strcmp (arg, "update2"))
166                     cmd = 'm';
167                 else if (!strcmp (arg, "dump"))
168                     cmd = 's';
169                 else if (!strcmp (arg, "del") || !strcmp(arg, "delete"))
170                     cmd = 'd';
171                 else if (!strcmp (arg, "init"))
172                 {
173                     zebra_init (zh);
174                 }
175                 else if (!strcmp (arg, "commit"))
176                 {
177                     zebra_commit (zh);
178                 }
179                 else if (!strcmp (arg, "clean"))
180                 {
181                     assert (!"todo");
182                 }
183                 else if (!strcmp (arg, "stat") || !strcmp (arg, "status"))
184                 {
185                     zebra_register_statistics (zh,0);
186                 }
187                 else if (!strcmp (arg, "dump") || !strcmp (arg, "dumpdict"))
188                 {
189                     zebra_register_statistics (zh,1);
190                 }
191                 else if (!strcmp (arg, "compact"))
192                 {
193                     zebra_compact (zh);
194                 }
195                 else
196                 {
197                     logf (LOG_FATAL, "unknown command: %s", arg);
198                     exit (1);
199                 }
200             }
201             else
202             {
203                 rGroupDef.path = arg;
204                 zebra_set_group (zh, &rGroupDef);
205                 if (!trans_started)
206                 {
207                     trans_started=1;
208                     zebra_begin_trans (zh);
209                 }
210
211                 switch (cmd)
212                 {
213                 case 'u':
214                     zebra_repository_update (zh);
215                     break;
216                 case 'd':
217                     zebra_repository_delete (zh);
218                     break;
219                 case 's':
220                     logf (LOG_LOG, "dumping %s", rGroupDef.path);
221                     zebra_repository_show (zh);
222                     nsections = 0;
223                     break;
224                 default:
225                     nsections = 0;
226                 }
227                 log_event_end (NULL, NULL);
228             }
229         }
230         else if (ret == 'V')
231         {
232             printf("Zebra %s %s\n", ZEBRAVER, ZEBRADATE);
233             printf(" (C) 1994-2002, Index Data ApS\n");
234 #ifdef WIN32
235 #ifdef _DEBUG
236             printf(" WIN32 Debug\n");
237 #else
238             printf(" WIN32 Release\n");
239 #endif
240 #endif
241 #if HAVE_BZLIB_H
242             printf("libbzip2\n"
243                    " (C) 1996-1999 Julian R Seward.  All rights reserved.\n");
244 #endif
245         }
246         else if (ret == 'v')
247             yaz_log_init_level (yaz_log_mask_str(arg));
248         else if (ret == 'l')
249             yaz_log_init_file (arg);
250         else if (ret == 'm')
251             mem_max = arg; 
252         else if (ret == 'd')
253             rGroupDef.databaseName = arg;
254         else if (ret == 's')
255             rGroupDef.flagRw = 0;
256         else if (ret == 'g')
257             rGroupDef.groupName = arg;
258         else if (ret == 'f')
259             rGroupDef.fileVerboseLimit = atoi(arg);
260         else if (ret == 'c')
261             configName = arg;
262         else if (ret == 't')
263             rGroupDef.recordType = arg;
264         else if (ret == 'n')
265             disableCommit = 1;
266         else if (ret == 'L')
267             rGroupDef.followLinks = 0;
268         else
269             logf (LOG_WARN, "unknown option '-%s'", arg);
270     } /* while arg */
271
272     if (trans_started)
273         zebra_end_trans (zh);
274
275     zebra_close (zh);
276     zebra_stop (zs);
277 #if HAVE_SYS_TIMES_H
278     if (trans_started)
279     {
280         gettimeofday(&end_time, 0);
281         usec = (end_time.tv_sec - start_time.tv_sec) * 1000000L +
282             end_time.tv_usec - start_time.tv_usec;
283         times(&tms2);
284         yaz_log (LOG_LOG, "zebraidx times: %5.2f %5.2f %5.2f",
285                 (double) usec / 1000000.0,
286                 (double) (tms2.tms_utime - tms1.tms_utime)/100,
287                 (double) (tms2.tms_stime - tms1.tms_stime)/100);
288     }
289 #endif
290     exit (0);
291     return 0;
292 }
293