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