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