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