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