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