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