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