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