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