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