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