New result set model and modular ranking system. Moved towards
[idzebra-moved-to-github.git] / index / extract.c
1 /*
2  * Copyright (C) 1994-1998, Index Data I/S 
3  * All rights reserved.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: extract.c,v $
7  * Revision 1.80  1998-03-05 08:45:11  adam
8  * New result set model and modular ranking system. Moved towards
9  * descent server API. System information stored as "SGML" records.
10  *
11  * Revision 1.79  1998/02/17 10:32:52  adam
12  * Fixed bug: binary files weren't opened with flag b on NT.
13  *
14  * Revision 1.78  1998/02/10 12:03:05  adam
15  * Implemented Sort.
16  *
17  * Revision 1.77  1998/01/12 15:04:08  adam
18  * The test option (-s) only uses read-lock (and not write lock).
19  *
20  * Revision 1.76  1997/10/27 14:33:04  adam
21  * Moved towards generic character mapping depending on "structure"
22  * field in abstract syntax file. Fixed a few memory leaks. Fixed
23  * bug with negative integers when doing searches with relational
24  * operators.
25  *
26  * Revision 1.75  1997/09/17 12:19:12  adam
27  * Zebra version corresponds to YAZ version 1.4.
28  * Changed Zebra server so that it doesn't depend on global common_resource.
29  *
30  * Revision 1.74  1997/09/09 13:38:06  adam
31  * Partial port to WIN95/NT.
32  *
33  * Revision 1.73  1997/09/04 13:57:20  adam
34  * New file extract/retrieve method tellf (added).
35  * Added O_BINARY for open calls.
36  *
37  * Revision 1.72  1997/07/15 16:32:29  adam
38  * Bug fix: Match handler didn't terminate the resulting string!
39  *
40  * Revision 1.71  1997/07/15 16:28:41  adam
41  * Bug fix: storeData didn't work with files with multiple records.
42  * Bug fix: fixed memory management with records; not really well
43  *  thought through.
44  *
45  * Revision 1.70  1997/07/01 13:00:42  adam
46  * Bug fix in routine searchRecordKey: uninitialized variables.
47  *
48  * Revision 1.69  1997/04/29 09:26:03  adam
49  * Bug fix: generic recordId handling didn't work for compressed internal
50  * keys.
51  *
52  * Revision 1.68  1997/02/12 20:39:45  adam
53  * Implemented options -f <n> that limits the log to the first <n>
54  * records.
55  * Changed some log messages also.
56  *
57  * Revision 1.67  1996/11/15 15:02:14  adam
58  * Minor changes regarding logging.
59  *
60  * Revision 1.66  1996/11/14  09:52:21  adam
61  * Strings in record keys bound by IT_MAX_WORD.
62  *
63  * Revision 1.65  1996/11/14  08:57:56  adam
64  * Reduction of storeKeys area.
65  *
66  * Revision 1.64  1996/11/08 11:10:16  adam
67  * Buffers used during file match got bigger.
68  * Compressed ISAM support everywhere.
69  * Bug fixes regarding masking characters in queries.
70  * Redesigned Regexp-2 queries.
71  *
72  * Revision 1.63  1996/10/29 14:09:39  adam
73  * Use of cisam system - enabled if setting isamc is 1.
74  *
75  * Revision 1.62  1996/10/11 10:57:01  adam
76  * New module recctrl. Used to manage records (extract/retrieval).
77  * Several files have been moved to the recctrl sub directory.
78  *
79  * Revision 1.61  1996/06/06 12:08:37  quinn
80  * Added showRecord function
81  *
82  * Revision 1.60  1996/06/04  10:18:12  adam
83  * Search/scan uses character mapping module.
84  *
85  * Revision 1.59  1996/05/14  15:47:07  adam
86  * Cleanup of various buffer size entities.
87  *
88  * Revision 1.58  1996/05/14  06:16:38  adam
89  * Compact use/set bytes used in search service.
90  *
91  * Revision 1.57  1996/05/13 14:23:04  adam
92  * Work on compaction of set/use bytes in dictionary.
93  *
94  * Revision 1.56  1996/05/09  09:54:42  adam
95  * Server supports maps from one logical attributes to a list of physical
96  * attributes.
97  * The extraction process doesn't make space consuming 'any' keys.
98  *
99  * Revision 1.55  1996/05/09  07:28:55  quinn
100  * Work towards phrases and multiple registers
101  *
102  * Revision 1.54  1996/05/01  13:46:35  adam
103  * First work on multiple records in one file.
104  * New option, -offset, to the "unread" command in the filter module.
105  *
106  * Revision 1.53  1996/04/26  12:09:43  adam
107  * Added a few comments.
108  *
109  * Revision 1.52  1996/04/25  13:27:57  adam
110  * Function recordExtract modified so that files with no keys (possibly empty)
111  * are ignored.
112  *
113  * Revision 1.51  1996/03/19  11:08:42  adam
114  * Bug fix: Log preamble wasn't always turned off after recordExtract.
115  *
116  * Revision 1.50  1996/02/12  18:45:36  adam
117  * New fileVerboseFlag in record group control.
118  *
119  * Revision 1.49  1996/02/05  12:29:57  adam
120  * Logging reduced a bit.
121  * The remaining running time is estimated during register merge.
122  *
123  * Revision 1.48  1996/02/01  20:53:26  adam
124  * The temporary per-record keys are compacted a little, and duplication
125  * of the per-records keys are avoided when they are saved in the record
126  * information buffer.
127  *
128  * Revision 1.47  1996/01/17  14:57:48  adam
129  * Prototype changed for reader functions in extract/retrieve. File
130  *  is identified by 'void *' instead of 'int.
131  *
132  * Revision 1.46  1995/12/15  14:57:16  adam
133  * Bug fix.
134  *
135  * Revision 1.45  1995/12/15  12:37:41  adam
136  * In addRecordKeyAny: Writes key only when attrSet != -1.
137  *
138  * Revision 1.44  1995/12/12  16:00:54  adam
139  * System call sync(2) used after update/commit.
140  * Locking (based on fcntl) uses F_EXLCK and F_SHLCK instead of F_WRLCK
141  * and F_RDLCK.
142  *
143  * Revision 1.43  1995/12/11  09:12:46  adam
144  * The rec_get function returns NULL if record doesn't exist - will
145  * happen in the server if the result set records have been deleted since
146  * the creation of the set (i.e. the search).
147  * The server saves a result temporarily if it is 'volatile', i.e. the
148  * set is register dependent.
149  *
150  * Revision 1.42  1995/12/07  17:38:46  adam
151  * Work locking mechanisms for concurrent updates/commit.
152  *
153  * Revision 1.41  1995/12/06  16:06:42  adam
154  * Better diagnostics. Work on 'real' dictionary deletion.
155  *
156  * Revision 1.40  1995/12/05  16:57:40  adam
157  * More work on regular patterns.
158  *
159  * Revision 1.39  1995/12/05  13:20:18  adam
160  * Bug fix: file_read sometimes returned early EOF.
161  *
162  * Revision 1.38  1995/12/04  17:59:21  adam
163  * More work on regular expression conversion.
164  *
165  * Revision 1.37  1995/12/04  14:22:27  adam
166  * Extra arg to recType_byName.
167  * Started work on new regular expression parsed input to
168  * structured records.
169  *
170  * Revision 1.36  1995/11/30  08:34:29  adam
171  * Started work on commit facility.
172  * Changed a few malloc/free to xmalloc/xfree.
173  *
174  * Revision 1.35  1995/11/28  14:26:21  adam
175  * Bug fix: recordId with constant wasn't right.
176  * Bug fix: recordId dictionary entry wasn't deleted when needed.
177  *
178  * Revision 1.34  1995/11/28  09:09:38  adam
179  * Zebra config renamed.
180  * Use setting 'recordId' to identify record now.
181  * Bug fix in recindex.c: rec_release_blocks was invokeded even
182  * though the blocks were already released.
183  * File traversal properly deletes records when needed.
184  *
185  * Revision 1.33  1995/11/27  09:56:20  adam
186  * Record info elements better enumerated. Internal store of records.
187  *
188  * Revision 1.32  1995/11/25  10:24:05  adam
189  * More record fields - they are enumerated now.
190  * New options: flagStoreData flagStoreKey.
191  *
192  * Revision 1.31  1995/11/24  11:31:35  adam
193  * Commands add & del read filenames from stdin if source directory is
194  * empty.
195  * Match criteria supports 'constant' strings.
196  *
197  * Revision 1.30  1995/11/22  17:19:16  adam
198  * Record management uses the bfile system.
199  *
200  * Revision 1.29  1995/11/21  15:01:14  adam
201  * New general match criteria implemented.
202  * New feature: document groups.
203  *
204  * Revision 1.28  1995/11/21  09:20:30  adam
205  * Yet more work on record match.
206  *
207  * Revision 1.27  1995/11/20  16:59:45  adam
208  * New update method: the 'old' keys are saved for each records.
209  *
210  * Revision 1.26  1995/11/20  11:56:24  adam
211  * Work on new traversal.
212  *
213  * Revision 1.25  1995/11/16  15:34:54  adam
214  * Uses new record management system in both indexer and server.
215  *
216  * Revision 1.24  1995/11/15  19:13:08  adam
217  * Work on record management.
218  *
219  * Revision 1.23  1995/10/27  14:00:10  adam
220  * Implemented detection of database availability.
221  *
222  * Revision 1.22  1995/10/17  18:02:07  adam
223  * New feature: databases. Implemented as prefix to words in dictionary.
224  *
225  * Revision 1.21  1995/10/10  12:24:38  adam
226  * Temporary sort files are compressed.
227  *
228  * Revision 1.20  1995/10/06  13:52:05  adam
229  * Bug fixes. Handler may abort further scanning.
230  *
231  * Revision 1.19  1995/10/04  12:55:16  adam
232  * Bug fix in ranked search. Use=Any keys inserted.
233  *
234  * Revision 1.18  1995/10/04  09:37:08  quinn
235  * Fixed bug.
236  *
237  * Revision 1.17  1995/10/03  14:28:57  adam
238  * Buffered read in extract works.
239  *
240  * Revision 1.16  1995/10/03  14:28:45  adam
241  * Work on more effecient read handler in extract.
242  *
243  * Revision 1.15  1995/10/02  15:42:53  adam
244  * Extract uses file descriptors instead of FILE pointers.
245  *
246  * Revision 1.14  1995/10/02  15:29:13  adam
247  * More logging in file_extract.
248  *
249  * Revision 1.13  1995/09/29  14:01:39  adam
250  * Bug fixes.
251  *
252  * Revision 1.12  1995/09/28  14:22:56  adam
253  * Sort uses smaller temporary files.
254  *
255  * Revision 1.11  1995/09/28  12:10:31  adam
256  * Bug fixes. Field prefix used in queries.
257  *
258  * Revision 1.10  1995/09/28  09:19:41  adam
259  * xfree/xmalloc used everywhere.
260  * Extract/retrieve method seems to work for text records.
261  *
262  * Revision 1.9  1995/09/27  12:22:28  adam
263  * More work on extract in record control.
264  * Field name is not in isam keys but in prefix in dictionary words.
265  *
266  * Revision 1.8  1995/09/14  07:48:22  adam
267  * Record control management.
268  *
269  * Revision 1.7  1995/09/11  13:09:32  adam
270  * More work on relevance feedback.
271  *
272  * Revision 1.6  1995/09/08  14:52:27  adam
273  * Minor changes. Dictionary is lower case now.
274  *
275  * Revision 1.5  1995/09/06  16:11:16  adam
276  * Option: only one word key per file.
277  *
278  * Revision 1.4  1995/09/05  15:28:39  adam
279  * More work on search engine.
280  *
281  * Revision 1.3  1995/09/04  12:33:41  adam
282  * Various cleanup. YAZ util used instead.
283  *
284  * Revision 1.2  1995/09/04  09:10:34  adam
285  * More work on index add/del/update.
286  * Merge sort implemented.
287  * Initial work on z39 server.
288  *
289  * Revision 1.1  1995/09/01  14:06:35  adam
290  * Split of work into more files.
291  *
292  */
293 #include <stdio.h>
294 #include <assert.h>
295 #ifdef WINDOWS
296 #include <io.h>
297 #else
298 #include <unistd.h>
299 #endif
300 #include <fcntl.h>
301
302 #include <recctrl.h>
303 #include <charmap.h>
304 #include <sortidx.h>
305 #include "index.h"
306
307 #include "zinfo.h"
308
309 static Dict matchDict;
310
311 static Records records = NULL;
312 static SortIdx sortIdx = NULL;
313
314 static char **key_buf;
315 static size_t ptr_top;
316 static size_t ptr_i;
317 static size_t key_buf_used;
318 static int key_file_no;
319
320 static int records_inserted = 0;
321 static int records_updated = 0;
322 static int records_deleted = 0;
323 static int records_processed = 0;
324
325 static ZebraExplainInfo zti = NULL;
326
327 static void logRecord (int showFlag)
328 {
329     if (!showFlag)
330         ++records_processed;
331     if (showFlag || !(records_processed % 1000))
332     {
333         logf (LOG_LOG, "Records: %7d i/u/d %d/%d/%d", 
334               records_processed, records_inserted, records_updated,
335               records_deleted);
336     }
337 }
338
339 int key_open (BFiles bfs, int mem, int rw, data1_handle dh)
340 {
341     if (!mem)
342         mem = atoi(res_get_def (common_resource, "memMax", "4"))*1024*1024;
343     if (mem < 50000)
344         mem = 50000;
345     key_buf = xmalloc (mem);
346     ptr_top = mem/sizeof(char*);
347     ptr_i = 0;
348
349     key_buf_used = 0;
350     key_file_no = 0;
351
352     if (!(matchDict = dict_open (bfs, GMATCH_DICT, 50, rw)))
353     {
354         logf (LOG_FATAL, "dict_open fail of %s", GMATCH_DICT);
355         return -1;
356     }
357     assert (!records);
358     records = rec_open (bfs, rw);
359     if (!records)
360     {
361         dict_close (matchDict);
362         return -1;
363     }
364     zti = zebraExplain_open (records, dh, rw);
365     if (!zti)
366     {
367         rec_close (&records);
368         dict_close (matchDict);
369         return -1;      
370     }
371     sortIdx = sortIdx_open (bfs, 1);
372     return 0;
373 }
374
375 struct encode_info {
376     int  sysno;
377     int  seqno;
378     char buf[768];
379 };
380
381 void encode_key_init (struct encode_info *i)
382 {
383     i->sysno = 0;
384     i->seqno = 0;
385 }
386
387 char *encode_key_int (int d, char *bp)
388 {
389     if (d <= 63)
390         *bp++ = d;
391     else if (d <= 16383)
392     {
393         *bp++ = 64 + (d>>8);
394         *bp++ = d  & 255;
395     }
396     else if (d <= 4194303)
397     {
398         *bp++ = 128 + (d>>16);
399         *bp++ = (d>>8) & 255;
400         *bp++ = d & 255;
401     }
402     else
403     {
404         *bp++ = 192 + (d>>24);
405         *bp++ = (d>>16) & 255;
406         *bp++ = (d>>8) & 255;
407         *bp++ = d & 255;
408     }
409     return bp;
410 }
411
412 void encode_key_write (char *k, struct encode_info *i, FILE *outf)
413 {
414     struct it_key key;
415     char *bp = i->buf;
416
417     while ((*bp++ = *k++))
418         ;
419     memcpy (&key, k+1, sizeof(struct it_key));
420     bp = encode_key_int ( (key.sysno - i->sysno) * 2 + *k, bp);
421     if (i->sysno != key.sysno)
422     {
423         i->sysno = key.sysno;
424         i->seqno = 0;
425     }
426     bp = encode_key_int (key.seqno - i->seqno, bp);
427     i->seqno = key.seqno;
428     if (fwrite (i->buf, bp - i->buf, 1, outf) != 1)
429     {
430         logf (LOG_FATAL|LOG_ERRNO, "fwrite");
431         exit (1);
432     }
433 }
434
435 #define SORT_EXTRA 0
436
437 #if SORT_EXTRA
438 static int key_y_len;
439
440 static int key_y_compare (const void *p1, const void *p2)
441 {
442     int r;
443
444     if ((r = key_compare (*(char**) p1 + key_y_len + 1,
445                           *(char**) p2 + key_y_len + 1)))
446          return r;
447     return *(*(char**) p1 + key_y_len) - *(*(char**) p2 + key_y_len);
448 }
449
450 static int key_x_compare (const void *p1, const void *p2)
451 {
452     return strcmp (*(char**) p1, *(char**) p2);
453 }
454 #endif
455
456 void key_flush (void)
457 {
458     FILE *outf;
459     char out_fname[200];
460     char *prevcp, *cp;
461     struct encode_info encode_info;
462 #if SORT_EXTRA
463     int i;
464 #endif
465     
466     if (ptr_i <= 0)
467         return;
468
469     key_file_no++;
470     logf (LOG_LOG, "sorting section %d", key_file_no);
471 #if !SORT_EXTRA
472     qsort (key_buf + ptr_top-ptr_i, ptr_i, sizeof(char*), key_qsort_compare);
473     getFnameTmp (out_fname, key_file_no);
474
475     if (!(outf = fopen (out_fname, "wb")))
476     {
477         logf (LOG_FATAL|LOG_ERRNO, "fopen %s", out_fname);
478         exit (1);
479     }
480     logf (LOG_LOG, "writing section %d", key_file_no);
481     prevcp = cp = key_buf[ptr_top-ptr_i];
482     
483     encode_key_init (&encode_info);
484     encode_key_write (cp, &encode_info, outf);
485     while (--ptr_i > 0)
486     {
487         cp = key_buf[ptr_top-ptr_i];
488         if (strcmp (cp, prevcp))
489         {
490             encode_key_init (&encode_info);
491             encode_key_write (cp, &encode_info, outf);
492             prevcp = cp;
493         }
494         else
495             encode_key_write (cp + strlen(cp), &encode_info, outf);
496     }
497 #else
498     qsort (key_buf + ptr_top-ptr_i, ptr_i, sizeof(char*), key_x_compare);
499     getFnameTmp (out_fname, key_file_no);
500
501     if (!(outf = fopen (out_fname, "wb")))
502     {
503         logf (LOG_FATAL|LOG_ERRNO, "fopen %s", out_fname);
504         exit (1);
505     }
506     logf (LOG_LOG, "writing section %d", key_file_no);
507     i = ptr_i;
508     prevcp =  key_buf[ptr_top-i];
509     while (1)
510         if (!--i || strcmp (prevcp, key_buf[ptr_top-i]))
511         {
512             key_y_len = strlen(prevcp)+1;
513 #if 0
514             logf (LOG_LOG, "key_y_len: %2d %02x %02x %s",
515                       key_y_len, prevcp[0], prevcp[1], 2+prevcp);
516 #endif
517             qsort (key_buf + ptr_top-ptr_i, ptr_i - i,
518                                    sizeof(char*), key_y_compare);
519             cp = key_buf[ptr_top-ptr_i];
520             --key_y_len;
521             encode_key_init (&encode_info);
522             encode_key_write (cp, &encode_info, outf);
523             while (--ptr_i > i)
524             {
525                 cp = key_buf[ptr_top-ptr_i];
526                 encode_key_write (cp+key_y_len, &encode_info, outf);
527             }
528             if (!i)
529                 break;
530             prevcp = key_buf[ptr_top-ptr_i];
531         }
532 #endif
533     if (fclose (outf))
534     {
535         logf (LOG_FATAL|LOG_ERRNO, "fclose %s", out_fname);
536         exit (1);
537     }
538     logf (LOG_LOG, "finished section %d", key_file_no);
539     ptr_i = 0;
540     key_buf_used = 0;
541 }
542
543 int key_close (int rw)
544 {
545     key_flush ();
546     xfree (key_buf);
547     if (rw)
548         zebraExplain_runNumberIncrement (zti, 1);
549     zebraExplain_close (zti, rw);
550     rec_close (&records);
551     dict_close (matchDict);
552     sortIdx_close (sortIdx);
553
554     logRecord (1);
555     return key_file_no;
556 }
557
558 static void wordInit (struct recExtractCtrl *p, RecWord *w)
559 {
560     w->zebra_maps = p->zebra_maps;
561     w->attrSet = 1;
562     w->attrUse = 1016;
563     w->reg_type = 'w';
564 }
565
566 static struct sortKey {
567     char *string;
568     int length;
569     int attrSet;
570     int attrUse;
571     struct sortKey *next;
572 } *sortKeys = NULL;
573
574 static struct recKeys {
575     int buf_used;
576     int buf_max;
577     char *buf;
578     char prevAttrSet;
579     short prevAttrUse;
580     int prevSeqNo;
581 } reckeys;
582
583 static void addIndexString (RecWord *p, const char *string, int length)
584 {
585     char *dst;
586     char attrSet;
587     short attrUse;
588     int lead = 0;
589     int diff = 0;
590
591     if (reckeys.buf_used+1024 > reckeys.buf_max)
592     {
593         char *b;
594
595         b = xmalloc (reckeys.buf_max += 128000);
596         if (reckeys.buf_used > 0)
597             memcpy (b, reckeys.buf, reckeys.buf_used);
598         xfree (reckeys.buf);
599         reckeys.buf = b;
600     }
601     dst = reckeys.buf + reckeys.buf_used;
602
603     attrSet = p->attrSet;
604     if (reckeys.buf_used > 0 && reckeys.prevAttrSet == attrSet)
605         lead |= 1;
606     else
607         reckeys.prevAttrSet = attrSet;
608     attrUse = p->attrUse;
609     if (reckeys.buf_used > 0 && reckeys.prevAttrUse == attrUse)
610         lead |= 2;
611     else
612         reckeys.prevAttrUse = attrUse;
613 #if 1
614     diff = 1 + p->seqno - reckeys.prevSeqNo;
615     if (diff >= 1 && diff <= 15)
616         lead |= (diff << 2);
617     else
618         diff = 0;
619 #endif
620     reckeys.prevSeqNo = p->seqno;
621
622     *dst++ = lead;
623
624     if (!(lead & 1))
625     {
626         memcpy (dst, &attrSet, sizeof(attrSet));
627         dst += sizeof(attrSet);
628     }
629     if (!(lead & 2))
630     {
631         memcpy (dst, &attrUse, sizeof(attrUse));
632         dst += sizeof(attrUse);
633     }
634     *dst++ = p->reg_type;
635     memcpy (dst, string, length);
636     dst += length;
637     *dst++ = '\0';
638
639     if (!diff)
640     {
641         memcpy (dst, &p->seqno, sizeof(p->seqno));
642         dst += sizeof(p->seqno);
643     }
644     reckeys.buf_used = dst - reckeys.buf;
645     (p->seqno)++;
646 }
647
648 static void addSortString (RecWord *p, const char *string, int length)
649 {
650     struct sortKey *sk;
651
652     for (sk = sortKeys; sk; sk = sk->next)
653         if (sk->attrSet == p->attrSet && sk->attrUse == p->attrUse)
654             return;
655
656     sk = xmalloc (sizeof(*sk));
657     sk->next = sortKeys;
658     sortKeys = sk;
659
660     sk->string = xmalloc (p->length);
661     sk->length = p->length;
662     memcpy (sk->string, p->string, p->length);
663     sk->attrSet = p->attrSet;
664     sk->attrUse = p->attrUse;
665 }
666
667 static void addString (RecWord *p, const char *string, int length)
668 {
669     assert (length > 0);
670     if (zebra_maps_is_sort (p->zebra_maps, p->reg_type))
671         addSortString (p, string, length);
672     else
673         addIndexString (p, string, length);
674 }
675
676 static void addIncompleteField (RecWord *p)
677 {
678     const char *b = p->string;
679     int remain = p->length;
680     const char **map = 0;
681
682     if (remain > 0)
683         map = zebra_maps_input(p->zebra_maps, p->reg_type, &b, remain);
684
685     while (map)
686     {
687         char buf[IT_MAX_WORD+1];
688         int i, remain;
689
690         /* Skip spaces */
691         while (map && *map && **map == *CHR_SPACE)
692         {
693             remain = p->length - (b - p->string);
694             if (remain > 0)
695                 map = zebra_maps_input(p->zebra_maps, p->reg_type, &b, remain);
696             else
697                 map = 0;
698         }
699         if (!map)
700             break;
701         i = 0;
702         while (map && *map && **map != *CHR_SPACE)
703         {
704             const char *cp = *map;
705
706             while (i < IT_MAX_WORD && *cp)
707                 buf[i++] = *(cp++);
708             remain = p->length - (b - p->string);
709             if (remain > 0)
710                 map = zebra_maps_input(p->zebra_maps, p->reg_type, &b, remain);
711             else
712                 map = 0;
713         }
714         if (!i)
715             return;
716         addString (p, buf, i);
717     }
718 }
719
720 static void addCompleteField (RecWord *p)
721 {
722     const char *b = p->string;
723     char buf[IT_MAX_WORD+1];
724     const char **map = 0;
725     int i = 0, remain = p->length;
726
727     if (remain > 0)
728         map = zebra_maps_input (p->zebra_maps, p->reg_type, &b, remain);
729
730     while (remain > 0 && i < IT_MAX_WORD)
731     {
732         while (map && *map && **map == *CHR_SPACE)
733         {
734             remain = p->length - (b - p->string);
735             if (remain > 0)
736                 map = zebra_maps_input(p->zebra_maps, p->reg_type, &b, remain);
737             else
738                 map = 0;
739         }
740         if (!map)
741             break;
742
743         if (i && i < IT_MAX_WORD)
744             buf[i++] = *CHR_SPACE;
745         while (map && *map && **map != *CHR_SPACE)
746         {
747             const char *cp = *map;
748
749             if (i >= IT_MAX_WORD)
750                 break;
751             while (i < IT_MAX_WORD && *cp)
752                 buf[i++] = *(cp++);
753             remain = p->length  - (b - p->string);
754             if (remain > 0)
755                 map = zebra_maps_input (p->zebra_maps, p->reg_type, &b,
756                                         remain);
757             else
758                 map = 0;
759         }
760     }
761     if (!i)
762         return;
763     addString (p, buf, i);
764 }
765
766 static void addRecordKey (RecWord *p)
767 {
768     if (zebra_maps_is_complete (p->zebra_maps, p->reg_type))
769         addCompleteField (p);
770     else
771         addIncompleteField(p);
772 }
773
774 static void flushSortKeys (SYSNO sysno, int cmd)
775 {
776     struct sortKey *sk = sortKeys;
777
778     sortIdx_sysno (sortIdx, sysno);
779     while (sk)
780     {
781         struct sortKey *sk_next = sk->next;
782         sortIdx_type (sortIdx, sk->attrUse);
783         sortIdx_add (sortIdx, sk->string, sk->length);
784         xfree (sk->string);
785         xfree (sk);
786         sk = sk_next;
787     }
788     sortKeys = NULL;
789 }
790
791 static void flushRecordKeys (SYSNO sysno, int cmd, struct recKeys *reckeys, 
792                              const char *databaseName)
793 {
794     char attrSet = -1;
795     short attrUse = -1;
796     int seqno = 0;
797     int off = 0;
798
799     if (zebraExplain_curDatabase (zti, databaseName))
800     {
801         if (zebraExplain_newDatabase (zti, databaseName))
802             abort ();
803     }
804     zebraExplain_recordCountIncrement (zti, cmd ? 1 : -1);
805     while (off < reckeys->buf_used)
806     {
807         const char *src = reckeys->buf + off;
808         struct it_key key;
809         int lead, ch;
810     
811         lead = *src++;
812
813         if (!(lead & 1))
814         {
815             memcpy (&attrSet, src, sizeof(attrSet));
816             src += sizeof(attrSet);
817         }
818         if (!(lead & 2))
819         {
820             memcpy (&attrUse, src, sizeof(attrUse));
821             src += sizeof(attrUse);
822         }
823         if (key_buf_used + 1024 > (ptr_top-ptr_i)*sizeof(char*))
824             key_flush ();
825         ++ptr_i;
826         key_buf[ptr_top-ptr_i] = (char*)key_buf + key_buf_used;
827
828         ch = zebraExplain_lookupSU (zti, attrSet, attrUse);
829         if (ch < 0)
830             ch = zebraExplain_addSU (zti, attrSet, attrUse);
831         assert (ch > 0);
832         ((char*) key_buf) [key_buf_used++] = ch;
833         while (*src)
834             ((char*)key_buf) [key_buf_used++] = *src++;
835         src++;
836         ((char*)key_buf) [key_buf_used++] = '\0';
837         ((char*) key_buf)[key_buf_used++] = cmd;
838
839         if (lead & 60)
840             seqno += ((lead>>2) & 15)-1;
841         else
842         {
843             memcpy (&seqno, src, sizeof(seqno));
844             src += sizeof(seqno);
845         }
846         key.seqno = seqno;
847         key.sysno = sysno;
848         memcpy ((char*)key_buf + key_buf_used, &key, sizeof(key));
849         key_buf_used += sizeof(key);
850         off = src - reckeys->buf;
851     }
852     assert (off == reckeys->buf_used);
853 }
854
855 static const char **searchRecordKey (struct recKeys *reckeys,
856                                      int attrSetS, int attrUseS)
857 {
858     static const char *ws[32];
859     int off = 0;
860     int startSeq = -1;
861     int i;
862     short attrUse;
863     char attrSet;
864     int seqno = 0;
865
866     for (i = 0; i<32; i++)
867         ws[i] = NULL;
868     
869     while (off < reckeys->buf_used)
870     {
871
872         const char *src = reckeys->buf + off;
873         const char *wstart;
874         int lead;
875     
876         lead = *src++;
877
878         if (!(lead & 1))
879         {
880             memcpy (&attrSet, src, sizeof(attrSet));
881             src += sizeof(attrSet);
882         }
883         if (!(lead & 2))
884         {
885             memcpy (&attrUse, src, sizeof(attrUse));
886             src += sizeof(attrUse);
887         }
888         wstart = src;
889         while (*src++)
890             ;
891         if (lead & 60)
892             seqno += ((lead>>2) & 15)-1;
893         else
894         {
895             memcpy (&seqno, src, sizeof(seqno));
896             src += sizeof(seqno);
897         }
898         if (attrUseS == attrUse && attrSetS == attrSet)
899         {
900             int woff;
901
902
903             if (startSeq == -1)
904                 startSeq = seqno;
905             woff = seqno - startSeq;
906             if (woff >= 0 && woff < 31)
907                 ws[woff] = wstart;
908         }
909
910         off = src - reckeys->buf;
911     }
912     assert (off == reckeys->buf_used);
913     return ws;
914 }
915
916 struct file_read_info {
917     off_t file_max;
918     off_t file_offset;
919     off_t file_moffset;
920     int file_more;
921     int fd;
922 };
923
924 static struct file_read_info *file_read_start (int fd)
925 {
926     struct file_read_info *fi = xmalloc (sizeof(*fi));
927
928     fi->fd = fd;
929     fi->file_max = 0;
930     fi->file_moffset = 0;
931     return fi;
932 }
933
934 static void file_read_stop (struct file_read_info *fi)
935 {
936     assert (fi);
937     xfree (fi);
938 }
939
940 static off_t file_seek (void *handle, off_t offset)
941 {
942     struct file_read_info *p = handle;
943     p->file_offset = offset;
944     return lseek (p->fd, offset, SEEK_SET);
945 }
946
947 static off_t file_tell (void *handle)
948 {
949     struct file_read_info *p = handle;
950     return p->file_offset;
951 }
952
953 static int file_read (void *handle, char *buf, size_t count)
954 {
955     struct file_read_info *p = handle;
956     int fd = p->fd;
957     int r;
958     r = read (fd, buf, count);
959     if (r > 0)
960     {
961         p->file_offset += r;
962         if (p->file_offset > p->file_max)
963             p->file_max = p->file_offset;
964     }
965     return r;
966 }
967
968 static void file_begin (void *handle)
969 {
970     struct file_read_info *p = handle;
971
972     p->file_offset = p->file_moffset;
973     if (p->file_moffset)
974         lseek (p->fd, p->file_moffset, SEEK_SET);
975     p->file_more = 0;
976 }
977
978 static void file_end (void *handle, off_t offset)
979 {
980     struct file_read_info *p = handle;
981
982     assert (p->file_more == 0);
983     p->file_more = 1;
984     p->file_moffset = offset;
985 }
986
987 static int atois (const char **s)
988 {
989     int val = 0, c;
990     while ( (c=**s) >= '0' && c <= '9')
991     {
992         val = val*10 + c - '0';
993         ++(*s);
994     }
995     return val;
996 }
997
998 static char *fileMatchStr (struct recKeys *reckeys, struct recordGroup *rGroup,
999                            const char *fname,
1000                            const char *spec)
1001 {
1002     static char dstBuf[2048];
1003     char *dst = dstBuf;
1004     const char *s = spec;
1005     static const char **w;
1006     int i;
1007
1008     while (1)
1009     {
1010         while (*s == ' ' || *s == '\t')
1011             s++;
1012         if (!*s)
1013             break;
1014         if (*s == '(')
1015         {
1016             char matchFlag[32];
1017             int attrSet, attrUse;
1018             int first = 1;
1019
1020             s++;
1021             attrSet = atois (&s);
1022             if (*s != ',')
1023             {
1024                 logf (LOG_WARN, "Missing , in match criteria %s in group %s",
1025                       spec, rGroup->groupName ? rGroup->groupName : "none");
1026                 return NULL;
1027             }
1028             s++;
1029             attrUse = atois (&s);
1030             w = searchRecordKey (reckeys, attrSet, attrUse);
1031             assert (w);
1032
1033             if (*s == ')')
1034             {
1035                 for (i = 0; i<32; i++)
1036                     matchFlag[i] = 1;
1037             }
1038             else
1039             {
1040                 logf (LOG_WARN, "Missing ) in match criteria %s in group %s",
1041                       spec, rGroup->groupName ? rGroup->groupName : "none");
1042                 return NULL;
1043             }
1044             s++;
1045
1046             for (i = 0; i<32; i++)
1047                 if (matchFlag[i] && w[i])
1048                 {
1049                     if (first)
1050                     {
1051                         *dst++ = ' ';
1052                         first = 0;
1053                     }
1054                     strcpy (dst, w[i]);
1055                     dst += strlen(w[i]);
1056                 }
1057             if (first)
1058             {
1059                 logf (LOG_WARN, "Record didn't contain match"
1060                       " fields in (%d,%d)", attrSet, attrUse);
1061                 return NULL;
1062             }
1063         }
1064         else if (*s == '$')
1065         {
1066             int spec_len;
1067             char special[64];
1068             const char *spec_src = NULL;
1069             const char *s1 = ++s;
1070             while (*s1 && *s1 != ' ' && *s1 != '\t')
1071                 s1++;
1072
1073             spec_len = s1 - s;
1074             if (spec_len > 63)
1075                 spec_len = 63;
1076             memcpy (special, s, spec_len);
1077             special[spec_len] = '\0';
1078             s = s1;
1079
1080             if (!strcmp (special, "group"))
1081                 spec_src = rGroup->groupName;
1082             else if (!strcmp (special, "database"))
1083                 spec_src = rGroup->databaseName;
1084             else if (!strcmp (special, "filename"))
1085                 spec_src = fname;
1086             else if (!strcmp (special, "type"))
1087                 spec_src = rGroup->recordType;
1088             else 
1089                 spec_src = NULL;
1090             if (spec_src)
1091             {
1092                 strcpy (dst, spec_src);
1093                 dst += strlen (spec_src);
1094             }
1095         }
1096         else if (*s == '\"' || *s == '\'')
1097         {
1098             int stopMarker = *s++;
1099             char tmpString[64];
1100             int i = 0;
1101
1102             while (*s && *s != stopMarker)
1103             {
1104                 if (i < 63)
1105                     tmpString[i++] = *s++;
1106             }
1107             if (*s)
1108                 s++;
1109             tmpString[i] = '\0';
1110             strcpy (dst, tmpString);
1111             dst += strlen (tmpString);
1112         }
1113         else
1114         {
1115             logf (LOG_WARN, "Syntax error in match criteria %s in group %s",
1116                   spec, rGroup->groupName ? rGroup->groupName : "none");
1117             return NULL;
1118         }
1119         *dst++ = 1;
1120     }
1121     if (dst == dstBuf)
1122     {
1123         logf (LOG_WARN, "No match criteria for record %s in group %s",
1124               fname, rGroup->groupName ? rGroup->groupName : "none");
1125         return NULL;
1126     }
1127     *dst = '\0';
1128     return dstBuf;
1129 }
1130
1131 struct recordLogInfo {
1132     const char *fname;
1133     int recordOffset;
1134     struct recordGroup *rGroup;
1135 };
1136      
1137 static void recordLogPreamble (int level, const char *msg, void *info)
1138 {
1139     struct recordLogInfo *p = info;
1140     FILE *outf = log_file ();
1141
1142     if (level & LOG_LOG)
1143         return ;
1144     fprintf (outf, "File %s, offset %d, type %s\n",
1145              p->rGroup->recordType, p->recordOffset, p->fname);
1146     log_event_start (NULL, NULL);
1147 }
1148
1149 static int recordExtract (SYSNO *sysno, const char *fname,
1150                           struct recordGroup *rGroup, int deleteFlag,
1151                           struct file_read_info *fi, RecType recType,
1152                           char *subType)
1153 {
1154     struct recExtractCtrl extractCtrl;
1155     RecordAttr *recordAttr;
1156     int r;
1157     char *matchStr;
1158     SYSNO sysnotmp;
1159     off_t recordOffset = 0;
1160     Record rec;
1161     struct recordLogInfo logInfo;
1162
1163     if (fi->fd != -1)
1164     {
1165         /* we are going to read from a file, so prepare the extraction */
1166         extractCtrl.fh = fi;
1167         extractCtrl.subType = subType;
1168         extractCtrl.init = wordInit;
1169         extractCtrl.add = addRecordKey;
1170         extractCtrl.dh = rGroup->dh;
1171
1172         reckeys.buf_used = 0;
1173         reckeys.prevAttrUse = -1;
1174         reckeys.prevAttrSet = -1;
1175         reckeys.prevSeqNo = 0;
1176
1177         recordOffset = fi->file_moffset;
1178         extractCtrl.offset = recordOffset;
1179         extractCtrl.readf = file_read;
1180         extractCtrl.seekf = file_seek;
1181         extractCtrl.tellf = file_tell;
1182         extractCtrl.endf = file_end;
1183         extractCtrl.zebra_maps = rGroup->zebra_maps;
1184         extractCtrl.flagShowRecords = !rGroup->flagRw;
1185         if (!rGroup->flagRw)
1186             printf ("File: %s %ld\n", fname, (long) recordOffset);
1187
1188         logInfo.fname = fname;
1189         logInfo.recordOffset = recordOffset;
1190         logInfo.rGroup = rGroup;
1191         log_event_start (recordLogPreamble, &logInfo);
1192
1193         r = (*recType->extract)(&extractCtrl);
1194
1195         log_event_start (NULL, NULL);
1196
1197         if (r)      
1198         {
1199             /* error occured during extraction ... */
1200             if (rGroup->flagRw &&
1201                 records_processed < rGroup->fileVerboseLimit)
1202             {
1203                 logf (LOG_WARN, "fail %s %s %ld code = %d", rGroup->recordType,
1204                       fname, (long) recordOffset, r);
1205             }
1206             return 0;
1207         }
1208         if (reckeys.buf_used == 0)
1209         {
1210             /* the extraction process returned no information - the record
1211                is probably empty - unless flagShowRecords is in use */
1212             if (!rGroup->flagRw)
1213                 return 1;
1214             logf (LOG_WARN, "No keys generated for file %s", fname);
1215             logf (LOG_WARN, " The file is probably empty");
1216             return 0;
1217         }
1218     }
1219
1220     /* perform match if sysno not known and if match criteria is specified */
1221        
1222     matchStr = NULL;
1223     if (!sysno) 
1224     {
1225         sysnotmp = 0;
1226         sysno = &sysnotmp;
1227         if (rGroup->recordId && *rGroup->recordId)
1228         {
1229             char *rinfo;
1230         
1231             matchStr = fileMatchStr (&reckeys, rGroup, fname, 
1232                                      rGroup->recordId);
1233             if (matchStr)
1234             {
1235                 rinfo = dict_lookup (matchDict, matchStr);
1236                 if (rinfo)
1237                     memcpy (sysno, rinfo+1, sizeof(*sysno));
1238             }
1239             else
1240             {
1241                 logf (LOG_WARN, "Bad match criteria");
1242                 return 0;
1243             }
1244         }
1245     }
1246
1247     if (! *sysno)
1248     {
1249         /* new record */
1250         if (deleteFlag)
1251         {
1252             logf (LOG_LOG, "delete %s %s %ld", rGroup->recordType,
1253                   fname, (long) recordOffset);
1254             logf (LOG_WARN, "cannot delete record above (seems new)");
1255             return 1;
1256         }
1257         if (records_processed < rGroup->fileVerboseLimit)
1258             logf (LOG_LOG, "add %s %s %ld", rGroup->recordType,
1259                   fname, (long) recordOffset);
1260         rec = rec_new (records);
1261
1262         *sysno = rec->sysno;
1263
1264         recordAttr = rec_init_attr (zti, rec);
1265
1266         if (matchStr)
1267         {
1268             dict_insert (matchDict, matchStr, sizeof(*sysno), sysno);
1269         }
1270         flushRecordKeys (*sysno, 1, &reckeys, rGroup->databaseName);
1271         flushSortKeys (*sysno, 1);
1272
1273         records_inserted++;
1274     }
1275     else
1276     {
1277         /* record already exists */
1278         struct recKeys delkeys;
1279
1280         rec = rec_get (records, *sysno);
1281         assert (rec);
1282         
1283         recordAttr = rec_init_attr (zti, rec);
1284
1285         if (recordAttr->runNumber == zebraExplain_runNumberIncrement (zti, 0))
1286         {
1287             logf (LOG_LOG, "skipped %s %s %ld", rGroup->recordType,
1288                   fname, (long) recordOffset);
1289             rec_rm (&rec);
1290             logRecord (0);
1291             return 1;
1292         }
1293         delkeys.buf_used = rec->size[recInfo_delKeys];
1294         delkeys.buf = rec->info[recInfo_delKeys];
1295         flushSortKeys (*sysno, 0);
1296         flushRecordKeys (*sysno, 0, &delkeys, rec->info[recInfo_databaseName]);
1297         if (deleteFlag)
1298         {
1299             /* record going to be deleted */
1300             if (!delkeys.buf_used)
1301             {
1302                 logf (LOG_LOG, "delete %s %s %ld", rGroup->recordType,
1303                       fname, (long) recordOffset);
1304                 logf (LOG_WARN, "cannot delete file above, storeKeys false");
1305             }
1306             else
1307             {
1308                 if (records_processed < rGroup->fileVerboseLimit)
1309                     logf (LOG_LOG, "delete %s %s %ld", rGroup->recordType,
1310                           fname, (long) recordOffset);
1311                 records_deleted++;
1312                 if (matchStr)
1313                     dict_delete (matchDict, matchStr);
1314                 rec_del (records, &rec);
1315             }
1316             rec_rm (&rec);
1317             logRecord (0);
1318             return 1;
1319         }
1320         else
1321         {
1322             /* record going to be updated */
1323             if (!delkeys.buf_used)
1324             {
1325                 logf (LOG_LOG, "update %s %s %ld", rGroup->recordType,
1326                       fname, (long) recordOffset);
1327                 logf (LOG_WARN, "cannot update file above, storeKeys false");
1328             }
1329             else
1330             {
1331                 if (records_processed < rGroup->fileVerboseLimit)
1332                     logf (LOG_LOG, "update %s %s %ld", rGroup->recordType,
1333                           fname, (long) recordOffset);
1334                 flushRecordKeys (*sysno, 1, &reckeys, rGroup->databaseName); 
1335                 records_updated++;
1336             }
1337         }
1338     }
1339     /* update file type */
1340     xfree (rec->info[recInfo_fileType]);
1341     rec->info[recInfo_fileType] =
1342         rec_strdup (rGroup->recordType, &rec->size[recInfo_fileType]);
1343
1344     /* update filename */
1345     xfree (rec->info[recInfo_filename]);
1346     rec->info[recInfo_filename] =
1347         rec_strdup (fname, &rec->size[recInfo_filename]);
1348
1349     /* update delete keys */
1350     xfree (rec->info[recInfo_delKeys]);
1351     if (reckeys.buf_used > 0 && rGroup->flagStoreKeys == 1)
1352     {
1353 #if 1
1354         rec->size[recInfo_delKeys] = reckeys.buf_used;
1355         rec->info[recInfo_delKeys] = reckeys.buf;
1356         reckeys.buf = NULL;
1357         reckeys.buf_max = 0;
1358 #else
1359         rec->info[recInfo_delKeys] = xmalloc (reckeys.buf_used);
1360         rec->size[recInfo_delKeys] = reckeys.buf_used;
1361         memcpy (rec->info[recInfo_delKeys], reckeys.buf,
1362                 rec->size[recInfo_delKeys]);
1363 #endif
1364     }
1365     else
1366     {
1367         rec->info[recInfo_delKeys] = NULL;
1368         rec->size[recInfo_delKeys] = 0;
1369     }
1370
1371     /* save file size of original record */
1372     zebraExplain_recordBytesIncrement (zti, - recordAttr->recordSize);
1373     recordAttr->recordSize = fi->file_moffset - recordOffset;
1374     if (!recordAttr->recordSize)
1375         recordAttr->recordSize = fi->file_max - recordOffset;
1376     zebraExplain_recordBytesIncrement (zti, recordAttr->recordSize);
1377
1378     /* set run-number for this record */
1379     recordAttr->runNumber = zebraExplain_runNumberIncrement (zti, 0);
1380
1381     /* update store data */
1382     xfree (rec->info[recInfo_storeData]);
1383     if (rGroup->flagStoreData == 1)
1384     {
1385         rec->size[recInfo_storeData] = recordAttr->recordSize;
1386         rec->info[recInfo_storeData] = xmalloc (recordAttr->recordSize);
1387         if (lseek (fi->fd, recordOffset, SEEK_SET) < 0)
1388         {
1389             logf (LOG_ERRNO|LOG_FATAL, "seek to %ld in %s", fname,
1390                   (long) recordOffset);
1391             exit (1);
1392         }
1393         if (read (fi->fd, rec->info[recInfo_storeData], recordAttr->recordSize)
1394             < recordAttr->recordSize)
1395         {
1396             logf (LOG_ERRNO|LOG_FATAL, "read %d bytes of %s",
1397                   recordAttr->recordSize, fname);
1398             exit (1);
1399         }
1400     }
1401     else
1402     {
1403         rec->info[recInfo_storeData] = NULL;
1404         rec->size[recInfo_storeData] = 0;
1405     }
1406     /* update database name */
1407     xfree (rec->info[recInfo_databaseName]);
1408     rec->info[recInfo_databaseName] =
1409         rec_strdup (rGroup->databaseName, &rec->size[recInfo_databaseName]); 
1410
1411     /* update offset */
1412     recordAttr->recordOffset = recordOffset;
1413     
1414     /* commit this record */
1415     rec_put (records, &rec);
1416     logRecord (0);
1417     return 1;
1418 }
1419
1420 int fileExtract (SYSNO *sysno, const char *fname, 
1421                  const struct recordGroup *rGroupP, int deleteFlag)
1422 {
1423     int r, i, fd;
1424     char gprefix[128];
1425     char ext[128];
1426     char ext_res[128];
1427     char subType[128];
1428     RecType recType;
1429     struct recordGroup rGroupM;
1430     struct recordGroup *rGroup = &rGroupM;
1431     struct file_read_info *fi;
1432
1433     memcpy (rGroup, rGroupP, sizeof(*rGroupP));
1434    
1435     if (!rGroup->groupName || !*rGroup->groupName)
1436         *gprefix = '\0';
1437     else
1438         sprintf (gprefix, "%s.", rGroup->groupName);
1439
1440     logf (LOG_DEBUG, "fileExtract %s", fname);
1441
1442     /* determine file extension */
1443     for (i = strlen(fname); --i >= 0; )
1444         if (fname[i] == '/')
1445         {
1446             strcpy (ext, "");
1447             break;
1448         }
1449         else if (fname[i] == '.')
1450         {
1451             strcpy (ext, fname+i+1);
1452             break;
1453         }
1454     /* determine file type - depending on extension */
1455     if (!rGroup->recordType)
1456     {
1457         sprintf (ext_res, "%srecordType.%s", gprefix, ext);
1458         if (!(rGroup->recordType = res_get (common_resource, ext_res)))
1459         {
1460             sprintf (ext_res, "%srecordType", gprefix);
1461             if (!(rGroup->recordType = res_get (common_resource, ext_res)))
1462             {
1463                 if (records_processed < rGroup->fileVerboseLimit)
1464                     logf (LOG_LOG, "? %s", fname);
1465                 return 0;
1466             }
1467         }
1468     }
1469     if (!rGroup->recordType)
1470     {
1471         if (records_processed < rGroup->fileVerboseLimit)
1472             logf (LOG_LOG, "? record %s", fname);
1473         return 0;
1474     }
1475     if (!(recType = recType_byName (rGroup->recordType, subType)))
1476     {
1477         logf (LOG_WARN, "No such record type: %s", rGroup->recordType);
1478         return 0;
1479     }
1480
1481     /* determine match criteria */
1482     if (!rGroup->recordId)
1483     {
1484         sprintf (ext_res, "%srecordId.%s", gprefix, ext);
1485         rGroup->recordId = res_get (common_resource, ext_res);
1486     }
1487
1488     /* determine database name */
1489     if (!rGroup->databaseName)
1490     {
1491         sprintf (ext_res, "%sdatabase.%s", gprefix, ext);
1492         if (!(rGroup->databaseName = res_get (common_resource, ext_res)))
1493         {
1494             sprintf (ext_res, "%sdatabase", gprefix);
1495             rGroup->databaseName = res_get (common_resource, ext_res);
1496         }
1497     }
1498     if (!rGroup->databaseName)
1499         rGroup->databaseName = "Default";
1500
1501     if (rGroup->flagStoreData == -1)
1502     {
1503         const char *sval;
1504         sprintf (ext_res, "%sstoreData.%s", gprefix, ext);
1505         if (!(sval = res_get (common_resource, ext_res)))
1506         {
1507             sprintf (ext_res, "%sstoreData", gprefix);
1508             sval = res_get (common_resource, ext_res);
1509         }
1510         if (sval)
1511             rGroup->flagStoreData = atoi (sval);
1512     }
1513     if (rGroup->flagStoreData == -1)
1514         rGroup->flagStoreData = 0;
1515
1516     if (rGroup->flagStoreKeys == -1)
1517     {
1518         const char *sval;
1519
1520         sprintf (ext_res, "%sstoreKeys.%s", gprefix, ext);
1521         if (!(sval = res_get (common_resource, ext_res)))
1522         {
1523             sprintf (ext_res, "%sstoreKeys", gprefix);
1524             sval = res_get (common_resource, ext_res);
1525         }
1526         if (sval)
1527             rGroup->flagStoreKeys = atoi (sval);
1528     }
1529     if (rGroup->flagStoreKeys == -1)
1530         rGroup->flagStoreKeys = 0;
1531
1532     if (sysno && deleteFlag)
1533         fd = -1;
1534     else
1535     {
1536         if ((fd = open (fname, O_BINARY|O_RDONLY)) == -1)
1537         {
1538             logf (LOG_WARN|LOG_ERRNO, "open %s", fname);
1539             return 0;
1540         }
1541     }
1542     fi = file_read_start (fd);
1543     do
1544     {
1545         file_begin (fi);
1546         r = recordExtract (sysno, fname, rGroup, deleteFlag, fi,
1547                            recType, subType);
1548     } while (r && !sysno && fi->file_more);
1549     file_read_stop (fi);
1550     if (fd != -1)
1551         close (fd);
1552     return r;
1553 }
1554