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