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