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