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