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