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