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