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