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