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