Implemented drop database
[idzebra-moved-to-github.git] / index / main.c
1 /* $Id: main.c,v 1.105 2003-06-30 19:37:12 adam Exp $
2    Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003
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 <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         " create <db>   Create database <db>\n"
100         " drop <db>     Drop database <db>\n"
101         " commit        Commit changes\n"
102         " clean         Clean shadow files\n"
103         "Options:\n"
104         " -t <type>     Index files as <type> (grs or text).\n"
105         " -c <config>   Read configuration file <config>.\n"
106         " -g <group>    Index files according to group settings.\n"
107         " -d <database> Records belong to Z39.50 database <database>.\n"
108         " -m <mbytes>   Use <mbytes> before flushing keys to disk.\n"
109         " -n            Don't use shadow system.\n"
110         " -s            Show analysis on stdout, but do no work.\n"
111         " -v <level>    Set logging to <level>.\n"
112         " -l <file>     Write log to <file>.\n"
113         " -L            Don't follow symbolic links.\n"
114         " -f <n>        Display information for the first <n> records.\n"
115         " -V            Show version.\n", *argv
116                  );
117         exit (1);
118     }
119     while ((ret = options ("sVt:c:g:d:m:v:nf:l:L"
120                            , argv, argc, &arg)) != -2)
121     {
122         if (ret == 0)
123         {
124             if(cmd == 0) /* command */
125             {
126                 if (!zs)
127                 {
128                     const char *config = configName ? configName : "zebra.cfg";
129                     logf (LOG_LOG, "Zebra version %s %s",
130                           ZEBRAVER, ZEBRADATE);
131                     zs = zebra_start (config);
132                     if (!zs)
133                     {
134                         yaz_log (LOG_FATAL, "Cannot read config %s", config);
135                         exit (1);
136                     }   
137                     zh = zebra_open (zs);
138                     if (disableCommit)
139                         zebra_shadow_enable (zh, 0);
140                 }
141
142                 if (rGroupDef.databaseName)
143                 {
144                     if (zebra_select_database (zh, rGroupDef.databaseName))
145                     {
146                         logf(LOG_FATAL, "Could not select database %s errCode=%d",
147                                           rGroupDef.databaseName, zebra_errCode(zh) );
148                         exit (1);
149                     }
150                 }
151                 else
152                 {
153                     if (zebra_select_database (zh, "Default"))
154                     {
155                         logf(LOG_FATAL, "Could not select database Default errCode=%d",
156                                         zebra_errCode(zh) );
157                         exit (1);
158                     }
159                 }
160                 if (mem_max)
161                     zebra_set_resource(zh, "memmax",mem_max); 
162
163                 if (!strcmp (arg, "update"))
164                     cmd = 'u';
165                 else if (!strcmp (arg, "update1"))
166                     cmd = 'U';
167                 else if (!strcmp (arg, "update2"))
168                     cmd = 'm';
169                 else if (!strcmp (arg, "dump"))
170                     cmd = 's';
171                 else if (!strcmp (arg, "del") || !strcmp(arg, "delete"))
172                     cmd = 'd';
173                 else if (!strcmp (arg, "init"))
174                 {
175                     zebra_init (zh);
176                 }
177                 else if (!strcmp(arg, "drop"))
178                 {
179                     cmd = 'D';
180                 }
181                 else if (!strcmp(arg, "create"))
182                 {
183                     cmd = 'C';
184                 }
185                 else if (!strcmp (arg, "commit"))
186                 {
187                     zebra_commit (zh);
188                 }
189                 else if (!strcmp (arg, "clean"))
190                 {
191                     zebra_clean (zh);
192                 }
193                 else if (!strcmp (arg, "stat") || !strcmp (arg, "status"))
194                 {
195                     zebra_register_statistics (zh,0);
196                 }
197                 else if (!strcmp (arg, "dumpdict"))
198                 {
199                     zebra_register_statistics (zh,1);
200                 }
201                 else if (!strcmp (arg, "compact"))
202                 {
203                     zebra_compact (zh);
204                 }
205                 else
206                 {
207                     logf (LOG_FATAL, "unknown command: %s", arg);
208                     exit (1);
209                 }
210             }
211             else
212             {
213                 rGroupDef.path = arg;
214                 zebra_set_group (zh, &rGroupDef);
215                 if (!trans_started)
216                 {
217                     trans_started=1;
218                     if (zebra_begin_trans (zh, 1))
219                         exit(1);
220                 }
221                 switch (cmd)
222                 {
223                 case 'u':
224                     zebra_repository_update (zh);
225                     break;
226                 case 'd':
227                     zebra_repository_delete (zh);
228                     break;
229                 case 's':
230                     logf (LOG_LOG, "dumping %s", rGroupDef.path);
231                     zebra_repository_show (zh);
232                     nsections = 0;
233                     break;
234                 case 'C':
235                     zebra_create_database(zh, rGroupDef.path);
236                     break;
237                 case 'D':
238                     zebra_drop_database(zh, rGroupDef.path);
239                     break;
240                 default:
241                     nsections = 0;
242                 }
243                 log_event_end (NULL, NULL);
244             }
245         }
246         else if (ret == 'V')
247         {
248             printf("Zebra %s %s\n", ZEBRAVER, ZEBRADATE);
249             printf(" (C) 1994-2002, Index Data ApS\n");
250 #ifdef WIN32
251 #ifdef _DEBUG
252             printf(" WIN32 Debug\n");
253 #else
254             printf(" WIN32 Release\n");
255 #endif
256 #endif
257 #if HAVE_BZLIB_H
258             printf("libbzip2\n"
259                    " (C) 1996-1999 Julian R Seward.  All rights reserved.\n");
260 #endif
261         }
262         else if (ret == 'v')
263             yaz_log_init_level (yaz_log_mask_str(arg));
264         else if (ret == 'l')
265             yaz_log_init_file (arg);
266         else if (ret == 'm')
267             mem_max = arg; 
268         else if (ret == 'd')
269             rGroupDef.databaseName = arg;
270         else if (ret == 's')
271             rGroupDef.flagRw = 0;
272         else if (ret == 'g')
273             rGroupDef.groupName = arg;
274         else if (ret == 'f')
275             rGroupDef.fileVerboseLimit = atoi(arg);
276         else if (ret == 'c')
277             configName = arg;
278         else if (ret == 't')
279             rGroupDef.recordType = arg;
280         else if (ret == 'n')
281             disableCommit = 1;
282         else if (ret == 'L')
283             rGroupDef.followLinks = 0;
284         else
285             logf (LOG_WARN, "unknown option '-%s'", arg);
286     } /* while arg */
287
288     if (trans_started)
289         zebra_end_trans (zh);
290
291     zebra_close (zh);
292     zebra_stop (zs);
293 #if HAVE_SYS_TIMES_H
294     if (trans_started)
295     {
296         gettimeofday(&end_time, 0);
297         usec = (end_time.tv_sec - start_time.tv_sec) * 1000000L +
298             end_time.tv_usec - start_time.tv_usec;
299         times(&tms2);
300         yaz_log (LOG_LOG, "zebraidx times: %5.2f %5.2f %5.2f",
301                 (double) usec / 1000000.0,
302                 (double) (tms2.tms_utime - tms1.tms_utime)/100,
303                 (double) (tms2.tms_stime - tms1.tms_stime)/100);
304     }
305 #endif
306     nmem_exit();
307     exit (0);
308     return 0;
309 }
310