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