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