New setting commitCache: points to commit directories/files.
[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.24  1995-11-30 17:01:38  adam
8  * New setting commitCache: points to commit directories/files.
9  * New command commit: commits at the end of a zebraidx run.
10  *
11  * Revision 1.23  1995/11/30  08:34:31  adam
12  * Started work on commit facility.
13  * Changed a few malloc/free to xmalloc/xfree.
14  *
15  * Revision 1.22  1995/11/28  09:09:42  adam
16  * Zebra config renamed.
17  * Use setting 'recordId' to identify record now.
18  * Bug fix in recindex.c: rec_release_blocks was invokeded even
19  * though the blocks were already released.
20  * File traversal properly deletes records when needed.
21  *
22  * Revision 1.21  1995/11/27  14:27:39  adam
23  * Renamed 'update' command to 'dir'.
24  *
25  * Revision 1.20  1995/11/27  13:58:53  adam
26  * New option -t. storeStore data implemented in server.
27  *
28  * Revision 1.19  1995/11/25  10:24:06  adam
29  * More record fields - they are enumerated now.
30  * New options: flagStoreData flagStoreKey.
31  *
32  * Revision 1.18  1995/11/22  17:19:17  adam
33  * Record management uses the bfile system.
34  *
35  * Revision 1.17  1995/11/21  15:01:16  adam
36  * New general match criteria implemented.
37  * New feature: document groups.
38  *
39  * Revision 1.16  1995/11/20  11:56:27  adam
40  * Work on new traversal.
41  *
42  * Revision 1.15  1995/11/01  16:25:51  quinn
43  * *** empty log message ***
44  *
45  * Revision 1.14  1995/10/17  18:02:09  adam
46  * New feature: databases. Implemented as prefix to words in dictionary.
47  *
48  * Revision 1.13  1995/10/10  12:24:39  adam
49  * Temporary sort files are compressed.
50  *
51  * Revision 1.12  1995/10/04  16:57:20  adam
52  * Key input and merge sort in one pass.
53  *
54  * Revision 1.11  1995/09/29  14:01:45  adam
55  * Bug fixes.
56  *
57  * Revision 1.10  1995/09/28  14:22:57  adam
58  * Sort uses smaller temporary files.
59  *
60  * Revision 1.9  1995/09/14  07:48:24  adam
61  * Record control management.
62  *
63  * Revision 1.8  1995/09/06  16:11:18  adam
64  * Option: only one word key per file.
65  *
66  * Revision 1.7  1995/09/05  15:28:39  adam
67  * More work on search engine.
68  *
69  * Revision 1.6  1995/09/04  12:33:43  adam
70  * Various cleanup. YAZ util used instead.
71  *
72  * Revision 1.5  1995/09/04  09:10:39  adam
73  * More work on index add/del/update.
74  * Merge sort implemented.
75  * Initial work on z39 server.
76  *
77  * Revision 1.4  1995/09/01  14:06:36  adam
78  * Split of work into more files.
79  *
80  * Revision 1.3  1995/09/01  10:57:07  adam
81  * Minor changes.
82  *
83  * Revision 1.2  1995/09/01  10:30:24  adam
84  * More work on indexing. Not working yet.
85  *
86  * Revision 1.1  1995/08/31  14:50:24  adam
87  * New simple file index tool.
88  *
89  */
90 #include <stdio.h>
91 #include <assert.h>
92 #include <unistd.h>
93
94 #include <alexutil.h>
95 #include <data1.h>
96 #include "index.h"
97
98 char *prog;
99 size_t mem_max = 4*1024*1024;
100 extern char *data1_tabpath;
101
102 int main (int argc, char **argv)
103 {
104     int commit_at_end = 0;
105     int ret;
106     int cmd = 0;
107     char *arg;
108     char *configName = NULL;
109     int nsections;
110     int key_open_flag = 0;
111
112     struct recordGroup rGroupDef;
113     
114     rGroupDef.groupName = NULL;
115     rGroupDef.databaseName = NULL;
116     rGroupDef.path = NULL;
117     rGroupDef.recordId = NULL;
118     rGroupDef.recordType = NULL;
119     rGroupDef.flagStoreData = -1;
120     rGroupDef.flagStoreKeys = -1;
121
122     prog = *argv;
123     if (argc < 2)
124     {
125         fprintf (stderr, "zebraidx [options] command <dir> ...\n"
126         "Commands:\n"
127         " update <dir>  Update index with files below <dir>.\n"
128         "               If <dir> is empty filenames are read from stdin.\n"
129         " delete <dir>  Delete index with files below <dir>.\n"
130         "Options:\n"
131         " -t <type>     Index files as <type> (grs or text).\n"
132         " -c <config>   Read configuration file <config>.\n"
133         " -g <group>    Index files according to group settings.\n"
134         " -d <database> Records belong to Z39.50 database <database>.\n"
135         " -m <mbytes>   Use <mbytes> before flushing keys to disk.\n"
136         " -v <level>    Set logging to <level>.\n");
137         exit (1);
138     }
139     while ((ret = options ("t:c:g:d:m:v:", argv, argc, &arg)) != -2)
140     {
141         if (ret == 0)
142         {
143             if(cmd == 0) /* command */
144             {
145                 if (!common_resource)
146                 {
147                     common_resource = res_open (configName ?
148                                                 configName : FNAME_CONFIG);
149                     if (!common_resource)
150                     {
151                         logf (LOG_FATAL, "Cannot open resource `%s'",
152                               configName);
153                         exit (1);
154                     }
155                     data1_tabpath = res_get (common_resource, "profilePath");
156                     bf_cache (res_get (common_resource, "commitCache"));
157                 }
158                 if (!strcmp (arg, "update"))
159                     cmd = 'u';
160                 else if (!strcmp (arg, "del") || !strcmp(arg, "delete"))
161                     cmd = 'd';
162                 else if (!strcmp (arg, "commit"))
163                     commit_at_end = 1;
164                 else
165                 {
166                     logf (LOG_FATAL, "Unknown command: %s", arg);
167                     exit (1);
168                 }
169             }
170             else
171             {
172                 struct recordGroup rGroup;
173
174                 memcpy (&rGroup, &rGroupDef, sizeof(rGroup));
175                 if (!key_open_flag)
176                 {
177                     key_open (mem_max);
178                     key_open_flag = 1;
179                 }
180                 rGroup.path = arg;
181                 if (cmd == 'u')
182                     repositoryUpdate (&rGroup);
183                 else if (cmd == 'd')
184                     repositoryDelete (&rGroup);
185                 cmd = 0;
186             }
187         }
188         else if (ret == 'v')
189         {
190             log_init (log_mask_str(arg), prog, NULL);
191         }
192         else if (ret == 'm')
193         {
194             mem_max = 1024*1024*atoi(arg);
195         }
196         else if (ret == 'd')
197         {
198             rGroupDef.databaseName = arg;
199         }
200         else if (ret == 'g')
201         {
202             rGroupDef.groupName = arg;
203         }
204         else if (ret == 'c')
205             configName = arg;
206         else if (ret == 't')
207             rGroupDef.recordType = arg;
208         else
209         {
210             logf (LOG_FATAL, "Unknown option '-%s'", arg);
211             exit (1);
212         }
213     }
214     if (key_open_flag)
215     {
216         nsections = key_close ();
217         if (nsections)
218         {
219             logf (LOG_LOG, "Merging with index");
220             key_input (FNAME_WORD_DICT, FNAME_WORD_ISAM, nsections, 60);
221         }
222     }
223     if (commit_at_end)
224     {
225         logf (LOG_LOG, "commiting");
226         bf_commit (res_get (common_resource, "commitCache"));
227     }
228     exit (0);
229 }
230