Cleanup of various buffer size entities.
[idzebra-moved-to-github.git] / index / extract.c
1 /*
2  * Copyright (C) 1994-1996, Index Data I/S 
3  * All rights reserved.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: extract.c,v $
7  * Revision 1.59  1996-05-14 15:47:07  adam
8  * Cleanup of various buffer size entities.
9  *
10  * Revision 1.58  1996/05/14  06:16:38  adam
11  * Compact use/set bytes used in search service.
12  *
13  * Revision 1.57  1996/05/13 14:23:04  adam
14  * Work on compaction of set/use bytes in dictionary.
15  *
16  * Revision 1.56  1996/05/09  09:54:42  adam
17  * Server supports maps from one logical attributes to a list of physical
18  * attributes.
19  * The extraction process doesn't make space consuming 'any' keys.
20  *
21  * Revision 1.55  1996/05/09  07:28:55  quinn
22  * Work towards phrases and multiple registers
23  *
24  * Revision 1.54  1996/05/01  13:46:35  adam
25  * First work on multiple records in one file.
26  * New option, -offset, to the "unread" command in the filter module.
27  *
28  * Revision 1.53  1996/04/26  12:09:43  adam
29  * Added a few comments.
30  *
31  * Revision 1.52  1996/04/25  13:27:57  adam
32  * Function recordExtract modified so that files with no keys (possibly empty)
33  * are ignored.
34  *
35  * Revision 1.51  1996/03/19  11:08:42  adam
36  * Bug fix: Log preamble wasn't always turned off after recordExtract.
37  *
38  * Revision 1.50  1996/02/12  18:45:36  adam
39  * New fileVerboseFlag in record group control.
40  *
41  * Revision 1.49  1996/02/05  12:29:57  adam
42  * Logging reduced a bit.
43  * The remaining running time is estimated during register merge.
44  *
45  * Revision 1.48  1996/02/01  20:53:26  adam
46  * The temporary per-record keys are compacted a little, and duplication
47  * of the per-records keys are avoided when they are saved in the record
48  * information buffer.
49  *
50  * Revision 1.47  1996/01/17  14:57:48  adam
51  * Prototype changed for reader functions in extract/retrieve. File
52  *  is identified by 'void *' instead of 'int.
53  *
54  * Revision 1.46  1995/12/15  14:57:16  adam
55  * Bug fix.
56  *
57  * Revision 1.45  1995/12/15  12:37:41  adam
58  * In addRecordKeyAny: Writes key only when attrSet != -1.
59  *
60  * Revision 1.44  1995/12/12  16:00:54  adam
61  * System call sync(2) used after update/commit.
62  * Locking (based on fcntl) uses F_EXLCK and F_SHLCK instead of F_WRLCK
63  * and F_RDLCK.
64  *
65  * Revision 1.43  1995/12/11  09:12:46  adam
66  * The rec_get function returns NULL if record doesn't exist - will
67  * happen in the server if the result set records have been deleted since
68  * the creation of the set (i.e. the search).
69  * The server saves a result temporarily if it is 'volatile', i.e. the
70  * set is register dependent.
71  *
72  * Revision 1.42  1995/12/07  17:38:46  adam
73  * Work locking mechanisms for concurrent updates/commit.
74  *
75  * Revision 1.41  1995/12/06  16:06:42  adam
76  * Better diagnostics. Work on 'real' dictionary deletion.
77  *
78  * Revision 1.40  1995/12/05  16:57:40  adam
79  * More work on regular patterns.
80  *
81  * Revision 1.39  1995/12/05  13:20:18  adam
82  * Bug fix: file_read sometimes returned early EOF.
83  *
84  * Revision 1.38  1995/12/04  17:59:21  adam
85  * More work on regular expression conversion.
86  *
87  * Revision 1.37  1995/12/04  14:22:27  adam
88  * Extra arg to recType_byName.
89  * Started work on new regular expression parsed input to
90  * structured records.
91  *
92  * Revision 1.36  1995/11/30  08:34:29  adam
93  * Started work on commit facility.
94  * Changed a few malloc/free to xmalloc/xfree.
95  *
96  * Revision 1.35  1995/11/28  14:26:21  adam
97  * Bug fix: recordId with constant wasn't right.
98  * Bug fix: recordId dictionary entry wasn't deleted when needed.
99  *
100  * Revision 1.34  1995/11/28  09:09:38  adam
101  * Zebra config renamed.
102  * Use setting 'recordId' to identify record now.
103  * Bug fix in recindex.c: rec_release_blocks was invokeded even
104  * though the blocks were already released.
105  * File traversal properly deletes records when needed.
106  *
107  * Revision 1.33  1995/11/27  09:56:20  adam
108  * Record info elements better enumerated. Internal store of records.
109  *
110  * Revision 1.32  1995/11/25  10:24:05  adam
111  * More record fields - they are enumerated now.
112  * New options: flagStoreData flagStoreKey.
113  *
114  * Revision 1.31  1995/11/24  11:31:35  adam
115  * Commands add & del read filenames from stdin if source directory is
116  * empty.
117  * Match criteria supports 'constant' strings.
118  *
119  * Revision 1.30  1995/11/22  17:19:16  adam
120  * Record management uses the bfile system.
121  *
122  * Revision 1.29  1995/11/21  15:01:14  adam
123  * New general match criteria implemented.
124  * New feature: document groups.
125  *
126  * Revision 1.28  1995/11/21  09:20:30  adam
127  * Yet more work on record match.
128  *
129  * Revision 1.27  1995/11/20  16:59:45  adam
130  * New update method: the 'old' keys are saved for each records.
131  *
132  * Revision 1.26  1995/11/20  11:56:24  adam
133  * Work on new traversal.
134  *
135  * Revision 1.25  1995/11/16  15:34:54  adam
136  * Uses new record management system in both indexer and server.
137  *
138  * Revision 1.24  1995/11/15  19:13:08  adam
139  * Work on record management.
140  *
141  * Revision 1.23  1995/10/27  14:00:10  adam
142  * Implemented detection of database availability.
143  *
144  * Revision 1.22  1995/10/17  18:02:07  adam
145  * New feature: databases. Implemented as prefix to words in dictionary.
146  *
147  * Revision 1.21  1995/10/10  12:24:38  adam
148  * Temporary sort files are compressed.
149  *
150  * Revision 1.20  1995/10/06  13:52:05  adam
151  * Bug fixes. Handler may abort further scanning.
152  *
153  * Revision 1.19  1995/10/04  12:55:16  adam
154  * Bug fix in ranked search. Use=Any keys inserted.
155  *
156  * Revision 1.18  1995/10/04  09:37:08  quinn
157  * Fixed bug.
158  *
159  * Revision 1.17  1995/10/03  14:28:57  adam
160  * Buffered read in extract works.
161  *
162  * Revision 1.16  1995/10/03  14:28:45  adam
163  * Work on more effecient read handler in extract.
164  *
165  * Revision 1.15  1995/10/02  15:42:53  adam
166  * Extract uses file descriptors instead of FILE pointers.
167  *
168  * Revision 1.14  1995/10/02  15:29:13  adam
169  * More logging in file_extract.
170  *
171  * Revision 1.13  1995/09/29  14:01:39  adam
172  * Bug fixes.
173  *
174  * Revision 1.12  1995/09/28  14:22:56  adam
175  * Sort uses smaller temporary files.
176  *
177  * Revision 1.11  1995/09/28  12:10:31  adam
178  * Bug fixes. Field prefix used in queries.
179  *
180  * Revision 1.10  1995/09/28  09:19:41  adam
181  * xfree/xmalloc used everywhere.
182  * Extract/retrieve method seems to work for text records.
183  *
184  * Revision 1.9  1995/09/27  12:22:28  adam
185  * More work on extract in record control.
186  * Field name is not in isam keys but in prefix in dictionary words.
187  *
188  * Revision 1.8  1995/09/14  07:48:22  adam
189  * Record control management.
190  *
191  * Revision 1.7  1995/09/11  13:09:32  adam
192  * More work on relevance feedback.
193  *
194  * Revision 1.6  1995/09/08  14:52:27  adam
195  * Minor changes. Dictionary is lower case now.
196  *
197  * Revision 1.5  1995/09/06  16:11:16  adam
198  * Option: only one word key per file.
199  *
200  * Revision 1.4  1995/09/05  15:28:39  adam
201  * More work on search engine.
202  *
203  * Revision 1.3  1995/09/04  12:33:41  adam
204  * Various cleanup. YAZ util used instead.
205  *
206  * Revision 1.2  1995/09/04  09:10:34  adam
207  * More work on index add/del/update.
208  * Merge sort implemented.
209  * Initial work on z39 server.
210  *
211  * Revision 1.1  1995/09/01  14:06:35  adam
212  * Split of work into more files.
213  *
214  */
215 #include <stdio.h>
216 #include <assert.h>
217 #include <unistd.h>
218 #include <fcntl.h>
219 #include <ctype.h>
220
221 #include <alexutil.h>
222 #include <recctrl.h>
223 #include "index.h"
224
225 #include "zinfo.h"
226
227 static Dict matchDict;
228
229 static Records records = NULL;
230
231 static char **key_buf;
232 static size_t ptr_top;
233 static size_t ptr_i;
234 static size_t key_buf_used;
235 static int key_file_no;
236
237 static int records_inserted = 0;
238 static int records_updated = 0;
239 static int records_deleted = 0;
240 static int records_processed = 0;
241
242 static ZebTargetInfo *zti = NULL;
243
244 static void logRecord (int showFlag)
245 {
246     if (!showFlag)
247         ++records_processed;
248     if (showFlag || !(records_processed % 1000))
249     {
250         logf (LOG_LOG, "Records: %7d i/u/d %d/%d/%d", 
251               records_processed, records_inserted, records_updated,
252               records_deleted);
253     }
254 }
255
256 void key_open (int mem)
257 {
258     if (mem < 50000)
259         mem = 50000;
260     key_buf = xmalloc (mem);
261     ptr_top = mem/sizeof(char*);
262     ptr_i = 0;
263
264     key_buf_used = 0;
265     key_file_no = 0;
266
267     if (!(matchDict = dict_open (GMATCH_DICT, 50, 1)))
268     {
269         logf (LOG_FATAL, "dict_open fail of %s", GMATCH_DICT);
270         exit (1);
271     }
272     assert (!records);
273     records = rec_open (1);
274 #if 1
275     zti = zebTargetInfo_open (records, 1);
276 #endif
277 }
278
279 struct encode_info {
280     int  sysno;
281     int  seqno;
282     char buf[768];
283 };
284
285 void encode_key_init (struct encode_info *i)
286 {
287     i->sysno = 0;
288     i->seqno = 0;
289 }
290
291 char *encode_key_int (int d, char *bp)
292 {
293     if (d <= 63)
294         *bp++ = d;
295     else if (d <= 16383)
296     {
297         *bp++ = 64 + (d>>8);
298         *bp++ = d  & 255;
299     }
300     else if (d <= 4194303)
301     {
302         *bp++ = 128 + (d>>16);
303         *bp++ = (d>>8) & 255;
304         *bp++ = d & 255;
305     }
306     else
307     {
308         *bp++ = 192 + (d>>24);
309         *bp++ = (d>>16) & 255;
310         *bp++ = (d>>8) & 255;
311         *bp++ = d & 255;
312     }
313     return bp;
314 }
315
316 void encode_key_write (char *k, struct encode_info *i, FILE *outf)
317 {
318     struct it_key key;
319     char *bp = i->buf;
320
321     while ((*bp++ = *k++))
322         ;
323     memcpy (&key, k+1, sizeof(struct it_key));
324     bp = encode_key_int ( (key.sysno - i->sysno) * 2 + *k, bp);
325     if (i->sysno != key.sysno)
326     {
327         i->sysno = key.sysno;
328         i->seqno = 0;
329     }
330     bp = encode_key_int (key.seqno - i->seqno, bp);
331     i->seqno = key.seqno;
332     if (fwrite (i->buf, bp - i->buf, 1, outf) != 1)
333     {
334         logf (LOG_FATAL|LOG_ERRNO, "fwrite");
335         exit (1);
336     }
337 }
338
339 void key_flush (void)
340 {
341     FILE *outf;
342     char out_fname[200];
343     char *prevcp, *cp;
344     struct encode_info encode_info;
345     
346     if (ptr_i <= 0)
347         return;
348
349     key_file_no++;
350     logf (LOG_LOG, "sorting section %d", key_file_no);
351     qsort (key_buf + ptr_top-ptr_i, ptr_i, sizeof(char*), key_qsort_compare);
352     sprintf (out_fname, TEMP_FNAME, key_file_no);
353
354     if (!(outf = fopen (out_fname, "w")))
355     {
356         logf (LOG_FATAL|LOG_ERRNO, "fopen (4) %s", out_fname);
357         exit (1);
358     }
359     logf (LOG_LOG, "writing section %d", key_file_no);
360     prevcp = cp = key_buf[ptr_top-ptr_i];
361     
362     encode_key_init (&encode_info);
363     encode_key_write (cp, &encode_info, outf);
364     while (--ptr_i > 0)
365     {
366         cp = key_buf[ptr_top-ptr_i];
367         if (strcmp (cp, prevcp))
368         {
369             encode_key_init (&encode_info);
370             encode_key_write (cp, &encode_info, outf);
371             prevcp = cp;
372         }
373         else
374             encode_key_write (cp + strlen(cp), &encode_info, outf);
375     }
376     if (fclose (outf))
377     {
378         logf (LOG_FATAL|LOG_ERRNO, "fclose %s", out_fname);
379         exit (1);
380     }
381     logf (LOG_LOG, "finished section %d", key_file_no);
382     ptr_i = 0;
383     key_buf_used = 0;
384 }
385
386 int key_close (void)
387 {
388     key_flush ();
389     xfree (key_buf);
390 #if 1
391     zebTargetInfo_close (zti, 1);
392 #endif
393     rec_close (&records);
394     dict_close (matchDict);
395
396     logRecord (1);
397     return key_file_no;
398 }
399
400 static void wordInit (RecWord *p)
401 {
402     p->attrSet = 1;
403     p->attrUse = 1016;
404     p->which = Word_String;
405 }
406
407 struct recKeys {
408     int buf_used;
409     int buf_max;
410     char *buf;
411     char prevAttrSet;
412     short prevAttrUse;
413 } reckeys;
414
415 static void addRecordKey (const RecWord *p)
416 {
417     char *dst;
418     char attrSet;
419     short attrUse;
420     size_t i;
421     int lead = 0;
422
423     if (reckeys.buf_used+1024 > reckeys.buf_max)
424     {
425         char *b;
426
427         b = xmalloc (reckeys.buf_max += 128000);
428         if (reckeys.buf_used > 0)
429             memcpy (b, reckeys.buf, reckeys.buf_used);
430         xfree (reckeys.buf);
431         reckeys.buf = b;
432     }
433     dst = reckeys.buf + reckeys.buf_used;
434
435     attrSet = p->attrSet;
436     if (reckeys.buf_used > 0 && reckeys.prevAttrSet == attrSet)
437         lead |= 1;
438     else
439         reckeys.prevAttrSet = attrSet;
440     attrUse = p->attrUse;
441     if (reckeys.buf_used > 0 && reckeys.prevAttrUse == attrUse)
442         lead |= 2;
443     else
444         reckeys.prevAttrUse = attrUse;
445
446     *dst++ = lead;
447
448     if (!(lead & 1))
449     {
450         memcpy (dst, &attrSet, sizeof(attrSet));
451         dst += sizeof(attrSet);
452     }
453     if (!(lead & 2))
454     {
455         memcpy (dst, &attrUse, sizeof(attrUse));
456         dst += sizeof(attrUse);
457     }
458     switch (p->which)
459     {
460         case Word_String:
461             *dst++ = 'w';
462             break;
463         case Word_Phrase:
464             *dst++ = 'p';
465             break;
466         case Word_Numeric:
467             *dst++ = 'n';
468     }
469     for (i = 0; p->u.string[i]; i++)
470         *dst++ = p->u.string[i];
471     *dst++ = '\0';
472
473     memcpy (dst, &p->seqno, sizeof(p->seqno));
474     dst += sizeof(p->seqno);
475     reckeys.buf_used = dst - reckeys.buf;
476 }
477
478 static void flushRecordKeys (SYSNO sysno, int cmd, struct recKeys *reckeys, 
479                              const char *databaseName)
480 {
481     char attrSet = -1;
482     short attrUse = -1;
483     int off = 0;
484
485 #if 1
486     if (zebTargetInfo_curDatabase (zti, databaseName))
487     {
488         if (zebTargetInfo_newDatabase (zti, databaseName))
489             abort ();
490     }
491 #endif
492     while (off < reckeys->buf_used)
493     {
494         const char *src = reckeys->buf + off;
495         struct it_key key;
496         int lead;
497     
498         lead = *src++;
499
500         if (!(lead & 1))
501         {
502             memcpy (&attrSet, src, sizeof(attrSet));
503             src += sizeof(attrSet);
504         }
505         if (!(lead & 2))
506         {
507             memcpy (&attrUse, src, sizeof(attrUse));
508             src += sizeof(attrUse);
509         }
510         if (key_buf_used + 1024 > (ptr_top-ptr_i)*sizeof(char*))
511             key_flush ();
512         ++ptr_i;
513         key_buf[ptr_top-ptr_i] = (char*)key_buf + key_buf_used;
514 #if 1
515         lead = zebTargetInfo_lookupSU (zti, attrSet, attrUse);
516         if (lead < 0)
517             lead = zebTargetInfo_addSU (zti, attrSet, attrUse);
518         assert (lead > 0);
519         ((char*) key_buf) [key_buf_used++] = lead;
520 #else
521         key_buf_used += index_word_prefix ((char*)key_buf + key_buf_used,
522                                            attrSet, attrUse, databaseName);
523 #endif
524         while (*src)
525             ((char*)key_buf) [key_buf_used++] = index_char_cvt (*src++);
526         src++;
527         ((char*)key_buf) [key_buf_used++] = '\0';
528         
529         ((char*) key_buf)[key_buf_used++] = cmd;
530
531         memcpy (&key.seqno, src, sizeof(key.seqno));
532         src += sizeof(key.seqno);
533         key.sysno = sysno;
534         memcpy ((char*)key_buf + key_buf_used, &key, sizeof(key));
535         key_buf_used += sizeof(key);
536         off = src - reckeys->buf;
537     }
538     assert (off == reckeys->buf_used);
539 }
540
541 static const char **searchRecordKey (struct recKeys *reckeys,
542                                int attrSetS, int attrUseS)
543 {
544     static const char *ws[32];
545     int off = 0;
546     int startSeq = -1;
547     int i;
548
549     for (i = 0; i<32; i++)
550         ws[i] = NULL;
551     
552     while (off < reckeys->buf_used)
553     {
554         const char *src = reckeys->buf + off;
555         char attrSet;
556         short attrUse;
557         int seqno;
558         const char *wstart;
559         
560         memcpy (&attrSet, src, sizeof(attrSet));
561         src += sizeof(attrSet);
562
563         memcpy (&attrUse, src, sizeof(attrUse));
564         src += sizeof(attrUse);
565
566         wstart = src;
567         while (*src++)
568             ;
569
570         memcpy (&seqno, src, sizeof(seqno));
571         src += sizeof(seqno);
572
573 #if 0
574         logf (LOG_LOG, "(%d,%d) %d %s", attrSet, attrUse, seqno, wstart);
575 #endif
576         if (attrUseS == attrUse && attrSetS == attrSet)
577         {
578             int woff;
579
580
581             if (startSeq == -1)
582                 startSeq = seqno;
583             woff = seqno - startSeq;
584             if (woff >= 0 && woff < 31)
585                 ws[woff] = wstart;
586         }
587
588         off = src - reckeys->buf;
589     }
590     assert (off == reckeys->buf_used);
591     return ws;
592 }
593
594 struct file_read_info {
595     off_t file_max;
596     off_t file_offset;
597     off_t file_moffset;
598     int file_more;
599     int fd;
600 };
601
602 static struct file_read_info *file_read_start (int fd)
603 {
604     struct file_read_info *fi = xmalloc (sizeof(*fi));
605
606     fi->fd = fd;
607     fi->file_max = 0;
608     fi->file_moffset = 0;
609     return fi;
610 }
611
612 static void file_read_stop (struct file_read_info *fi)
613 {
614     assert (fi);
615     xfree (fi);
616 }
617
618 static off_t file_seek (void *handle, off_t offset)
619 {
620     struct file_read_info *p = handle;
621     p->file_offset = offset;
622     return lseek (p->fd, offset, SEEK_SET);
623 }
624
625 static int file_read (void *handle, char *buf, size_t count)
626 {
627     struct file_read_info *p = handle;
628     int fd = p->fd;
629     int r;
630     r = read (fd, buf, count);
631     if (r > 0)
632     {
633         p->file_offset += r;
634         if (p->file_offset > p->file_max)
635             p->file_max = p->file_offset;
636     }
637     return r;
638 }
639
640 static void file_begin (void *handle)
641 {
642     struct file_read_info *p = handle;
643
644     p->file_offset = p->file_moffset;
645     if (p->file_moffset)
646         lseek (p->fd, p->file_moffset, SEEK_SET);
647     p->file_more = 0;
648 }
649
650 static void file_end (void *handle, off_t offset)
651 {
652     struct file_read_info *p = handle;
653
654     assert (p->file_more == 0);
655     p->file_more = 1;
656     p->file_moffset = offset;
657 }
658
659 static int atois (const char **s)
660 {
661     int val = 0, c;
662     while ( (c=**s) >= '0' && c <= '9')
663     {
664         val = val*10 + c - '0';
665         ++(*s);
666     }
667     return val;
668 }
669
670 static char *fileMatchStr (struct recKeys *reckeys, struct recordGroup *rGroup,
671                            const char *fname,
672                            const char *spec)
673 {
674     static char dstBuf[2048];
675     char *dst = dstBuf;
676     const char *s = spec;
677     static const char **w;
678     int i;
679
680     while (1)
681     {
682         while (*s == ' ' || *s == '\t')
683             s++;
684         if (!*s)
685             break;
686         if (*s == '(')
687         {
688             char matchFlag[32];
689             int attrSet, attrUse;
690             int first = 1;
691
692             s++;
693             attrSet = atois (&s);
694             if (*s != ',')
695             {
696                 logf (LOG_WARN, "Missing , in match criteria %s in group %s",
697                       spec, rGroup->groupName ? rGroup->groupName : "none");
698                 return NULL;
699             }
700             s++;
701             attrUse = atois (&s);
702             w = searchRecordKey (reckeys, attrSet, attrUse);
703             assert (w);
704
705             if (*s == ')')
706             {
707                 for (i = 0; i<32; i++)
708                     matchFlag[i] = 1;
709             }
710             else
711             {
712                 logf (LOG_WARN, "Missing ) in match criteria %s in group %s",
713                       spec, rGroup->groupName ? rGroup->groupName : "none");
714                 return NULL;
715             }
716             s++;
717
718             for (i = 0; i<32; i++)
719                 if (matchFlag[i] && w[i])
720                 {
721                     if (first)
722                     {
723                         *dst++ = ' ';
724                         first = 0;
725                     }
726                     strcpy (dst, w[i]);
727                     dst += strlen(w[i]);
728                 }
729             if (first)
730             {
731                 logf (LOG_WARN, "Record didn't contain match"
732                       " fields in (%d,%d)", attrSet, attrUse);
733                 return NULL;
734             }
735         }
736         else if (*s == '$')
737         {
738             int spec_len;
739             char special[64];
740             const char *spec_src = NULL;
741             const char *s1 = ++s;
742             while (*s1 && *s1 != ' ' && *s1 != '\t')
743                 s1++;
744
745             spec_len = s1 - s;
746             if (spec_len > 63)
747                 spec_len = 63;
748             memcpy (special, s, spec_len);
749             special[spec_len] = '\0';
750             s = s1;
751
752             if (!strcmp (special, "group"))
753                 spec_src = rGroup->groupName;
754             else if (!strcmp (special, "database"))
755                 spec_src = rGroup->databaseName;
756             else if (!strcmp (special, "filename"))
757                 spec_src = fname;
758             else if (!strcmp (special, "type"))
759                 spec_src = rGroup->recordType;
760             else 
761                 spec_src = NULL;
762             if (spec_src)
763             {
764                 strcpy (dst, spec_src);
765                 dst += strlen (spec_src);
766             }
767         }
768         else if (*s == '\"' || *s == '\'')
769         {
770             int stopMarker = *s++;
771             char tmpString[64];
772             int i = 0;
773
774             while (*s && *s != stopMarker)
775             {
776                 if (i < 63)
777                     tmpString[i++] = *s++;
778             }
779             if (*s)
780                 s++;
781             tmpString[i] = '\0';
782             strcpy (dst, tmpString);
783             dst += strlen (tmpString);
784         }
785         else
786         {
787             logf (LOG_WARN, "Syntax error in match criteria %s in group %s",
788                   spec, rGroup->groupName ? rGroup->groupName : "none");
789             return NULL;
790         }
791         *dst++ = 1;
792     }
793     if (dst == dstBuf)
794     {
795         logf (LOG_WARN, "No match criteria for record %s in group %s",
796               fname, rGroup->groupName ? rGroup->groupName : "none");
797         return NULL;
798     }
799     return dstBuf;
800 }
801
802 struct recordLogInfo {
803     const char *fname;
804     char *op;
805     struct recordGroup *rGroup;
806 };
807      
808 static void recordLogPreamble (int level, const char *msg, void *info)
809 {
810     struct recordLogInfo *p = info;
811     FILE *outf = log_file ();
812
813     if (level & LOG_LOG)
814         return ;
815     if (p->op) 
816         fprintf (outf, "%s of ", p->op);
817     fprintf (outf, "%s type %s\n", p->rGroup->recordType, p->fname);
818     log_event_start (NULL, NULL);
819 }
820
821 static int recordExtract (SYSNO *sysno, const char *fname,
822                           struct recordGroup *rGroup, int deleteFlag,
823                           struct file_read_info *fi, RecType recType,
824                           char *subType)
825 {
826     struct recExtractCtrl extractCtrl;
827     int r;
828     char *matchStr;
829     SYSNO sysnotmp;
830     off_t recordOffset = 0;
831     Record rec;
832     struct recordLogInfo logInfo;
833
834     logInfo.fname = fname;
835     logInfo.op = NULL;
836     logInfo.rGroup = rGroup;
837     log_event_start (recordLogPreamble, &logInfo);
838     
839     if (fi->fd != -1)
840     {
841         /* we are going to read from a file, so prepare the extraction */
842         extractCtrl.fh = fi;
843         extractCtrl.subType = subType;
844         extractCtrl.init = wordInit;
845         extractCtrl.add = addRecordKey;
846
847         reckeys.buf_used = 0;
848         reckeys.prevAttrUse = -1;
849         reckeys.prevAttrSet = -1;
850
851         recordOffset = fi->file_moffset;
852         extractCtrl.offset = recordOffset;
853         extractCtrl.readf = file_read;
854         extractCtrl.seekf = file_seek;
855         extractCtrl.endf = file_end;
856         r = (*recType->extract)(&extractCtrl);
857
858         if (r)      
859         {
860             /* error occured during extraction ... */
861             logf (LOG_WARN, "Couldn't extract file %s, code %d", fname, r);
862             return 0;
863         }
864         if (reckeys.buf_used == 0)
865         {
866             /* the extraction process returned no information - the record
867                is probably empty */
868             logf (LOG_WARN, "Empty file %s", fname);
869             return 0;
870         }
871     }
872
873     /* perform match if sysno not known and if match criteria is specified */
874        
875     matchStr = NULL;
876     if (!sysno) 
877     {
878         sysnotmp = 0;
879         sysno = &sysnotmp;
880         if (rGroup->recordId && *rGroup->recordId)
881         {
882             char *rinfo;
883         
884             matchStr = fileMatchStr (&reckeys, rGroup, fname, 
885                                      rGroup->recordId);
886             if (matchStr)
887             {
888                 rinfo = dict_lookup (matchDict, matchStr);
889                 if (rinfo)
890                     memcpy (sysno, rinfo+1, sizeof(*sysno));
891             }
892             else
893             {
894                 logf (LOG_WARN, "Bad match criteria");
895                 return 0;
896             }
897         }
898     }
899
900     if (! *sysno)
901     {
902         /* new record */
903         if (deleteFlag)
904         {
905             logf (LOG_LOG, "Cannot delete new record");
906             return 1;
907         }
908         logInfo.op = "add";
909         if (rGroup->fileVerboseFlag)
910             logf (LOG_LOG, "add %s %s+%ld", rGroup->recordType,
911                   fname, (long) recordOffset);
912         rec = rec_new (records);
913         *sysno = rec->sysno;
914
915         if (matchStr)
916         {
917             dict_insert (matchDict, matchStr, sizeof(*sysno), sysno);
918         }
919         flushRecordKeys (*sysno, 1, &reckeys, rGroup->databaseName);
920
921         records_inserted++;
922     }
923     else
924     {
925         /* record already exists */
926         struct recKeys delkeys;
927
928         rec = rec_get (records, *sysno);
929         assert (rec);
930         delkeys.buf_used = rec->size[recInfo_delKeys];
931         delkeys.buf = rec->info[recInfo_delKeys];
932         flushRecordKeys (*sysno, 0, &delkeys, rec->info[recInfo_databaseName]);
933         if (deleteFlag)
934         {
935             /* record going to be deleted */
936             logInfo.op = "delete";
937             if (!delkeys.buf_used)
938             {
939                 logf (LOG_WARN, "cannot delete; storeKeys false");
940             }
941             else
942             {
943                 if (rGroup->fileVerboseFlag)
944                     logf (LOG_LOG, "delete %s %s %ld", rGroup->recordType,
945                           fname, (long) recordOffset);
946                 records_deleted++;
947                 if (matchStr)
948                     dict_delete (matchDict, matchStr);
949                 rec_del (records, &rec);
950             }
951             logRecord (0);
952             return 1;
953         }
954         else
955         {
956             /* record going to be updated */
957             logInfo.op = "update";
958             if (!delkeys.buf_used)
959             {
960                 logf (LOG_WARN, "cannot update; storeKeys false");
961             }
962             else
963             {
964                 if (rGroup->fileVerboseFlag)
965                     logf (LOG_LOG, "update %s %s %ld", rGroup->recordType,
966                           fname, (long) recordOffset);
967                 flushRecordKeys (*sysno, 1, &reckeys, rGroup->databaseName); 
968                 records_updated++;
969             }
970         }
971     }
972     /* update file type */
973     xfree (rec->info[recInfo_fileType]);
974     rec->info[recInfo_fileType] =
975         rec_strdup (rGroup->recordType, &rec->size[recInfo_fileType]);
976
977     /* update filename */
978     xfree (rec->info[recInfo_filename]);
979     rec->info[recInfo_filename] =
980         rec_strdup (fname, &rec->size[recInfo_filename]);
981
982     /* update delete keys */
983     xfree (rec->info[recInfo_delKeys]);
984     if (reckeys.buf_used > 0 && rGroup->flagStoreKeys == 1)
985     {
986 #if 1
987         rec->size[recInfo_delKeys] = reckeys.buf_used;
988         rec->info[recInfo_delKeys] = reckeys.buf;
989         reckeys.buf = NULL;
990         reckeys.buf_max = 0;
991 #else
992         rec->info[recInfo_delKeys] = xmalloc (reckeys.buf_used);
993         rec->size[recInfo_delKeys] = reckeys.buf_used;
994         memcpy (rec->info[recInfo_delKeys], reckeys.buf,
995                 rec->size[recInfo_delKeys]);
996 #endif
997     }
998     else
999     {
1000         rec->info[recInfo_delKeys] = NULL;
1001         rec->size[recInfo_delKeys] = 0;
1002     }
1003
1004     /* update store data */
1005     xfree (rec->info[recInfo_storeData]);
1006     if (rGroup->flagStoreData == 1)
1007     {
1008         rec->size[recInfo_storeData] = fi->file_max;
1009         rec->info[recInfo_storeData] = xmalloc (fi->file_max);
1010         if (lseek (fi->fd, recordOffset, SEEK_SET) < 0)
1011         {
1012             logf (LOG_ERRNO|LOG_FATAL, "seek to %ld in %s", fname,
1013                   (long) recordOffset);
1014             exit (1);
1015         }
1016         if (read (fi->fd, rec->info[recInfo_storeData], fi->file_max)
1017             < fi->file_max)
1018         {
1019             logf (LOG_ERRNO|LOG_FATAL, "read %d bytes of %s",
1020                   fi->file_max, fname);
1021             exit (1);
1022         }
1023     }
1024     else
1025     {
1026         rec->info[recInfo_storeData] = NULL;
1027         rec->size[recInfo_storeData] = 0;
1028     }
1029     /* update database name */
1030     xfree (rec->info[recInfo_databaseName]);
1031     rec->info[recInfo_databaseName] =
1032         rec_strdup (rGroup->databaseName, &rec->size[recInfo_databaseName]); 
1033
1034     /* update offset */
1035     xfree (rec->info[recInfo_offset]);
1036
1037     rec->size[recInfo_offset] = sizeof(recordOffset);
1038     rec->info[recInfo_offset] = xmalloc (sizeof(recordOffset));
1039     memcpy (rec->info[recInfo_offset], &recordOffset, sizeof(recordOffset));
1040     
1041     /* commit this record */
1042     rec_put (records, &rec);
1043     logRecord (0);
1044     return 1;
1045 }
1046
1047 int fileExtract (SYSNO *sysno, const char *fname, 
1048                  const struct recordGroup *rGroupP, int deleteFlag)
1049 {
1050     int r, i, fd;
1051     char gprefix[128];
1052     char ext[128];
1053     char ext_res[128];
1054     char subType[128];
1055     RecType recType;
1056     struct recordGroup rGroupM;
1057     struct recordGroup *rGroup = &rGroupM;
1058     struct file_read_info *fi;
1059
1060     memcpy (rGroup, rGroupP, sizeof(*rGroupP));
1061    
1062     if (!rGroup->groupName || !*rGroup->groupName)
1063         *gprefix = '\0';
1064     else
1065         sprintf (gprefix, "%s.", rGroup->groupName);
1066
1067     logf (LOG_DEBUG, "fileExtract %s", fname);
1068
1069     /* determine file extension */
1070     for (i = strlen(fname); --i >= 0; )
1071         if (fname[i] == '/')
1072         {
1073             strcpy (ext, "");
1074             break;
1075         }
1076         else if (fname[i] == '.')
1077         {
1078             strcpy (ext, fname+i+1);
1079             break;
1080         }
1081     /* determine file type - depending on extension */
1082     if (!rGroup->recordType)
1083     {
1084         sprintf (ext_res, "%srecordType.%s", gprefix, ext);
1085         if (!(rGroup->recordType = res_get (common_resource, ext_res)))
1086         {
1087             sprintf (ext_res, "%srecordType", gprefix);
1088             if (!(rGroup->recordType = res_get (common_resource, ext_res)))
1089             {
1090                 if (rGroup->fileVerboseFlag)
1091                     logf (LOG_LOG, "? %s", fname);
1092                 return 0;
1093             }
1094         }
1095     }
1096     if (!rGroup->recordType)
1097     {
1098         if (rGroup->fileVerboseFlag)
1099             logf (LOG_LOG, "? record %s", fname);
1100         return 0;
1101     }
1102     if (!(recType = recType_byName (rGroup->recordType, subType)))
1103     {
1104         logf (LOG_WARN, "No such record type: %s", rGroup->recordType);
1105         return 0;
1106     }
1107
1108     /* determine match criteria */
1109     if (!rGroup->recordId)
1110     {
1111         sprintf (ext_res, "%srecordId.%s", gprefix, ext);
1112         rGroup->recordId = res_get (common_resource, ext_res);
1113     }
1114
1115     /* determine database name */
1116     if (!rGroup->databaseName)
1117     {
1118         sprintf (ext_res, "%sdatabase.%s", gprefix, ext);
1119         if (!(rGroup->databaseName = res_get (common_resource, ext_res)))
1120         {
1121             sprintf (ext_res, "%sdatabase", gprefix);
1122             rGroup->databaseName = res_get (common_resource, ext_res);
1123         }
1124     }
1125     if (!rGroup->databaseName)
1126         rGroup->databaseName = "Default";
1127
1128     if (rGroup->flagStoreData == -1)
1129     {
1130         const char *sval;
1131         sprintf (ext_res, "%sstoreData.%s", gprefix, ext);
1132         if (!(sval = res_get (common_resource, ext_res)))
1133         {
1134             sprintf (ext_res, "%sstoreData", gprefix);
1135             sval = res_get (common_resource, ext_res);
1136         }
1137         if (sval)
1138             rGroup->flagStoreData = atoi (sval);
1139     }
1140     if (rGroup->flagStoreData == -1)
1141         rGroup->flagStoreData = 0;
1142
1143     if (rGroup->flagStoreKeys == -1)
1144     {
1145         const char *sval;
1146
1147         sprintf (ext_res, "%sstoreKeys.%s", gprefix, ext);
1148         if (!(sval = res_get (common_resource, ext_res)))
1149         {
1150             sprintf (ext_res, "%sstoreKeys", gprefix);
1151             sval = res_get (common_resource, ext_res);
1152         }
1153         if (sval)
1154             rGroup->flagStoreKeys = atoi (sval);
1155     }
1156     if (rGroup->flagStoreKeys == -1)
1157         rGroup->flagStoreKeys = 0;
1158
1159     if (sysno && deleteFlag)
1160         fd = -1;
1161     else
1162     {
1163         if ((fd = open (fname, O_RDONLY)) == -1)
1164         {
1165             logf (LOG_WARN|LOG_ERRNO, "open %s", fname);
1166             return 0;
1167         }
1168     }
1169     fi = file_read_start (fd);
1170     do
1171     {
1172         file_begin (fi);
1173         r = recordExtract (sysno, fname, rGroup, deleteFlag, fi,
1174                            recType, subType);
1175     } while (r && !sysno && fi->file_more);
1176     log_event_start (NULL, NULL);
1177     file_read_stop (fi);
1178     if (fd != -1)
1179         close (fd);
1180     return r;
1181 }
1182