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