Added option -V to zebraidx to display version information.
[idzebra-moved-to-github.git] / index / main.c
1 /*
2  * Copyright (C) 1994-1995, Index Data I/S 
3  * All rights reserved.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: main.c,v $
7  * Revision 1.40  1996-04-26 10:00:23  adam
8  * Added option -V to zebraidx to display version information.
9  * Removed stupid warnings from file update.
10  *
11  * Revision 1.39  1996/04/09  10:05:21  adam
12  * Bug fix: prev_name buffer possibly too small; allocated in key_file_init.
13  *
14  * Revision 1.38  1996/03/26  16:01:14  adam
15  * New setting lockPath: directory of various lock files.
16  *
17  * Revision 1.37  1996/03/19  12:43:26  adam
18  * Bug fix: File update traversal didn't handle trailing slashes correctly.
19  * Bug fix: Update of sub directory groups wasn't handled correctly.
20  *
21  * Revision 1.36  1996/02/12  18:45:37  adam
22  * New fileVerboseFlag in record group control.
23  *
24  * Revision 1.35  1996/02/12  15:56:11  adam
25  * New code command: unread.
26  *
27  * Revision 1.34  1996/02/07  14:06:39  adam
28  * Better progress report during register merge.
29  * New command: clean - removes temporary shadow files.
30  *
31  * Revision 1.33  1996/02/06  17:11:18  adam
32  * Minor changes.
33  *
34  * Revision 1.32  1996/02/01  20:50:04  adam
35  * Bug fix: zebraIndexUnlock was always called even though zebraIndexLock
36  * was never called - happens when no commands are specified.
37  *
38  * Revision 1.31  1996/01/08  19:15:46  adam
39  * New input filter that works!
40  *
41  * Revision 1.30  1995/12/12  16:00:59  adam
42  * System call sync(2) used after update/commit.
43  * Locking (based on fcntl) uses F_EXLCK and F_SHLCK instead of F_WRLCK
44  * and F_RDLCK.
45  *
46  * Revision 1.29  1995/12/11  11:43:30  adam
47  * Locking based on fcntl instead of flock.
48  * Setting commitEnable removed. Command line option -n can be used to
49  * prevent commit if commit setting is defined in the configuration file.
50  *
51  * Revision 1.28  1995/12/08  16:22:56  adam
52  * Work on update while servers are running. Three lock files introduced.
53  * The servers reload their registers when necessary, but they don't
54  * reestablish result sets yet.
55  *
56  * Revision 1.27  1995/12/07  17:38:47  adam
57  * Work locking mechanisms for concurrent updates/commit.
58  *
59  * Revision 1.26  1995/12/06  12:41:23  adam
60  * New command 'stat' for the index program.
61  * Filenames can be read from stdin by specifying '-'.
62  * Bug fix/enhancement of the transformation from terms to regular
63  * expressons in the search engine.
64  *
65  * Revision 1.25  1995/12/01  16:24:39  adam
66  * Commit files use separate meta file area.
67  *
68  * Revision 1.24  1995/11/30  17:01:38  adam
69  * New setting commitCache: points to commit directories/files.
70  * New command commit: commits at the end of a zebraidx run.
71  *
72  * Revision 1.23  1995/11/30  08:34:31  adam
73  * Started work on commit facility.
74  * Changed a few malloc/free to xmalloc/xfree.
75  *
76  * Revision 1.22  1995/11/28  09:09:42  adam
77  * Zebra config renamed.
78  * Use setting 'recordId' to identify record now.
79  * Bug fix in recindex.c: rec_release_blocks was invokeded even
80  * though the blocks were already released.
81  * File traversal properly deletes records when needed.
82  *
83  * Revision 1.21  1995/11/27  14:27:39  adam
84  * Renamed 'update' command to 'dir'.
85  *
86  * Revision 1.20  1995/11/27  13:58:53  adam
87  * New option -t. storeStore data implemented in server.
88  *
89  * Revision 1.19  1995/11/25  10:24:06  adam
90  * More record fields - they are enumerated now.
91  * New options: flagStoreData flagStoreKey.
92  *
93  * Revision 1.18  1995/11/22  17:19:17  adam
94  * Record management uses the bfile system.
95  *
96  * Revision 1.17  1995/11/21  15:01:16  adam
97  * New general match criteria implemented.
98  * New feature: document groups.
99  *
100  * Revision 1.16  1995/11/20  11:56:27  adam
101  * Work on new traversal.
102  *
103  * Revision 1.15  1995/11/01  16:25:51  quinn
104  * *** empty log message ***
105  *
106  * Revision 1.14  1995/10/17  18:02:09  adam
107  * New feature: databases. Implemented as prefix to words in dictionary.
108  *
109  * Revision 1.13  1995/10/10  12:24:39  adam
110  * Temporary sort files are compressed.
111  *
112  * Revision 1.12  1995/10/04  16:57:20  adam
113  * Key input and merge sort in one pass.
114  *
115  * Revision 1.11  1995/09/29  14:01:45  adam
116  * Bug fixes.
117  *
118  * Revision 1.10  1995/09/28  14:22:57  adam
119  * Sort uses smaller temporary files.
120  *
121  * Revision 1.9  1995/09/14  07:48:24  adam
122  * Record control management.
123  *
124  * Revision 1.8  1995/09/06  16:11:18  adam
125  * Option: only one word key per file.
126  *
127  * Revision 1.7  1995/09/05  15:28:39  adam
128  * More work on search engine.
129  *
130  * Revision 1.6  1995/09/04  12:33:43  adam
131  * Various cleanup. YAZ util used instead.
132  *
133  * Revision 1.5  1995/09/04  09:10:39  adam
134  * More work on index add/del/update.
135  * Merge sort implemented.
136  * Initial work on z39 server.
137  *
138  * Revision 1.4  1995/09/01  14:06:36  adam
139  * Split of work into more files.
140  *
141  * Revision 1.3  1995/09/01  10:57:07  adam
142  * Minor changes.
143  *
144  * Revision 1.2  1995/09/01  10:30:24  adam
145  * More work on indexing. Not working yet.
146  *
147  * Revision 1.1  1995/08/31  14:50:24  adam
148  * New simple file index tool.
149  *
150  */
151 #include <stdio.h>
152 #include <assert.h>
153 #include <unistd.h>
154
155 #include <alexutil.h>
156 #include <data1.h>
157 #include "index.h"
158
159 char *prog;
160 size_t mem_max = 4*1024*1024;
161 extern char *data1_tabpath;
162
163 static void abort_func (int level, const char *msg, void *info)
164 {
165     if (level & LOG_FATAL)
166         abort ();
167 }
168
169 int main (int argc, char **argv)
170 {
171     int ret;
172     int cmd = 0;
173     char *arg;
174     char *configName = NULL;
175     int nsections;
176     int disableCommit = 0;
177
178     struct recordGroup rGroupDef;
179     
180     rGroupDef.groupName = NULL;
181     rGroupDef.databaseName = NULL;
182     rGroupDef.path = NULL;
183     rGroupDef.recordId = NULL;
184     rGroupDef.recordType = NULL;
185     rGroupDef.flagStoreData = -1;
186     rGroupDef.flagStoreKeys = -1;
187     rGroupDef.fileVerboseFlag = 1;
188
189     prog = *argv;
190     if (argc < 2)
191     {
192         fprintf (stderr, "zebraidx [options] command <dir> ...\n"
193         "Commands:\n"
194         " update <dir>  Update index with files below <dir>.\n"
195         "               If <dir> is empty filenames are read from stdin.\n"
196         " delete <dir>  Delete index with files below <dir>.\n"
197         " commit        Commit changes\n"
198         " clean         Clean shadow files\n"
199         "Options:\n"
200         " -t <type>     Index files as <type> (grs or text).\n"
201         " -c <config>   Read configuration file <config>.\n"
202         " -g <group>    Index files according to group settings.\n"
203         " -d <database> Records belong to Z39.50 database <database>.\n"
204         " -m <mbytes>   Use <mbytes> before flushing keys to disk.\n"
205         " -n            Don't use shadow system\n"
206         " -v <level>    Set logging to <level>\n"
207         " -V            Show version\n"
208                  );
209         exit (1);
210     }
211     log_event_end (abort_func, NULL);
212     while ((ret = options ("Vt:c:g:d:m:v:n", argv, argc, &arg)) != -2)
213     {
214         if (ret == 0)
215         {
216             const char *rval;
217             if(cmd == 0) /* command */
218             {
219                 if (!common_resource)
220                 {
221                     common_resource = res_open (configName ?
222                                                 configName : FNAME_CONFIG);
223                     if (!common_resource)
224                     {
225                         logf (LOG_FATAL, "Cannot open resource `%s'",
226                               configName);
227                         exit (1);
228                     }
229                     data1_tabpath = res_get (common_resource, "profilePath");
230                     bf_lockDir (res_get (common_resource, "lockPath"));
231                 }
232                 if (!strcmp (arg, "update"))
233                     cmd = 'u';
234                 else if (!strcmp (arg, "update1"))
235                     cmd = 'U';
236                 else if (!strcmp (arg, "update2"))
237                     cmd = 'm';
238                 else if (!strcmp (arg, "dump"))
239                     cmd = 's';
240                 else if (!strcmp (arg, "del") || !strcmp(arg, "delete"))
241                     cmd = 'd';
242                 else if (!strcmp (arg, "commit"))
243                 {
244                     zebraIndexLock (1);
245                     rval = res_get (common_resource, "shadow");
246                     if (rval && *rval)
247                         bf_cache (1);
248                     else
249                     {
250                         logf (LOG_FATAL, "Cannot perform commit");
251                         logf (LOG_FATAL, "No shadow area defined");
252                         exit (1);
253                     }
254                     if (bf_commitExists ())
255                     {
256                         logf (LOG_LOG, "Commit start");
257                         zebraIndexLockMsg ("c");
258                         zebraIndexWait (1);
259                         logf (LOG_LOG, "Commit execute");
260                         bf_commitExec ();
261                         sync ();
262                         zebraIndexLockMsg ("d");
263                         zebraIndexWait (0);
264                         logf (LOG_LOG, "Commit clean");
265                         bf_commitClean ();
266                     }
267                     else
268                         logf (LOG_LOG, "Nothing to commit");
269                 }
270                 else if (!strcmp (arg, "clean"))
271                 {
272                     zebraIndexLock (1);
273                     if (bf_commitExists ())
274                     {
275                         zebraIndexLockMsg ("d");
276                         zebraIndexWait (0);
277                         logf (LOG_LOG, "Commit clean");
278                         bf_commitClean ();
279                     }
280                     else
281                         logf (LOG_LOG, "Nothing to clean");
282                 }
283                 else if (!strcmp (arg, "stat") || !strcmp (arg, "status"))
284                 {
285                     zebraIndexLock (0);
286                     rval = res_get (common_resource, "shadow");
287                     if (rval && *rval)
288                     {
289                         bf_cache (1);
290                         zebraIndexLockMsg ("r");
291                     }
292                     rec_prstat ();
293                 }
294                 else
295                 {
296                     logf (LOG_FATAL, "Unknown command: %s", arg);
297                     exit (1);
298                 }
299             }
300             else
301             {
302                 struct recordGroup rGroup;
303
304                 zebraIndexLock (0);
305                 rval = res_get (common_resource, "shadow");
306                 if (rval && *rval && !disableCommit)
307                 {
308                     bf_cache (1);
309                     zebraIndexLockMsg ("r");
310                 }
311                 else
312                 {
313                     bf_cache (0);
314                     zebraIndexLockMsg ("w");
315                 }
316                 zebraIndexWait (0);
317                 
318                 memcpy (&rGroup, &rGroupDef, sizeof(rGroup));
319                 rGroup.path = arg;
320                 switch (cmd)
321                 {
322                 case 'u':
323                     key_open (mem_max);
324                     logf (LOG_LOG, "Updating %s", rGroup.path);
325                     repositoryUpdate (&rGroup);
326                     nsections = key_close ();
327                     break;
328                 case 'U':
329                     key_open (mem_max);
330                     logf (LOG_LOG, "Updating (pass 1) %s", rGroup.path);
331                     repositoryUpdate (&rGroup);
332                     key_close ();
333                     nsections = 0;
334                     break;
335                 case 'd':
336                     key_open (mem_max);
337                     logf (LOG_LOG, "Deleting %s", rGroup.path);
338                     repositoryDelete (&rGroup);
339                     nsections = key_close ();
340                     break;
341                 case 's':
342                     logf (LOG_LOG, "Dumping %s", rGroup.path);
343                     repositoryShow (&rGroup);
344                     nsections = 0;
345                     break;
346                 case 'm':
347                     nsections = -1;
348                     break;
349                 default:
350                     nsections = 0;
351                 }
352                 cmd = 0;
353                 if (nsections)
354                 {
355                     logf (LOG_LOG, "Merging with index");
356                     key_input (FNAME_WORD_DICT, FNAME_WORD_ISAM, nsections,
357                                60);
358                     sync ();
359                 }
360             }
361         }
362         else if (ret == 'V')
363         {
364             fprintf (stderr, "Zebra %s %s\n",
365                      ZEBRAVER, ZEBRADATE);
366         }
367         else if (ret == 'v')
368         {
369             log_init (log_mask_str(arg), prog, NULL);
370         }
371         else if (ret == 'm')
372         {
373             mem_max = 1024*1024*atoi(arg);
374         }
375         else if (ret == 'd')
376         {
377             rGroupDef.databaseName = arg;
378         }
379         else if (ret == 'g')
380         {
381             rGroupDef.groupName = arg;
382         }
383         else if (ret == 'c')
384             configName = arg;
385         else if (ret == 't')
386             rGroupDef.recordType = arg;
387         else if (ret == 'n')
388             disableCommit = 1;
389         else
390         {
391             logf (LOG_FATAL, "Unknown option '-%s'", arg);
392             exit (1);
393         }
394     }
395     if (common_resource)
396         zebraIndexUnlock ();
397     exit (0);
398 }
399