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