2 * Copyright (C) 1994-1998, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.77 1998-01-12 15:04:08 adam
8 * The test option (-s) only uses read-lock (and not write lock).
10 * Revision 1.76 1997/10/27 14:33:04 adam
11 * Moved towards generic character mapping depending on "structure"
12 * field in abstract syntax file. Fixed a few memory leaks. Fixed
13 * bug with negative integers when doing searches with relational
16 * Revision 1.75 1997/09/17 12:19:12 adam
17 * Zebra version corresponds to YAZ version 1.4.
18 * Changed Zebra server so that it doesn't depend on global common_resource.
20 * Revision 1.74 1997/09/09 13:38:06 adam
21 * Partial port to WIN95/NT.
23 * Revision 1.73 1997/09/04 13:57:20 adam
24 * New file extract/retrieve method tellf (added).
25 * Added O_BINARY for open calls.
27 * Revision 1.72 1997/07/15 16:32:29 adam
28 * Bug fix: Match handler didn't terminate the resulting string!
30 * Revision 1.71 1997/07/15 16:28:41 adam
31 * Bug fix: storeData didn't work with files with multiple records.
32 * Bug fix: fixed memory management with records; not really well
35 * Revision 1.70 1997/07/01 13:00:42 adam
36 * Bug fix in routine searchRecordKey: uninitialized variables.
38 * Revision 1.69 1997/04/29 09:26:03 adam
39 * Bug fix: generic recordId handling didn't work for compressed internal
42 * Revision 1.68 1997/02/12 20:39:45 adam
43 * Implemented options -f <n> that limits the log to the first <n>
45 * Changed some log messages also.
47 * Revision 1.67 1996/11/15 15:02:14 adam
48 * Minor changes regarding logging.
50 * Revision 1.66 1996/11/14 09:52:21 adam
51 * Strings in record keys bound by IT_MAX_WORD.
53 * Revision 1.65 1996/11/14 08:57:56 adam
54 * Reduction of storeKeys area.
56 * Revision 1.64 1996/11/08 11:10:16 adam
57 * Buffers used during file match got bigger.
58 * Compressed ISAM support everywhere.
59 * Bug fixes regarding masking characters in queries.
60 * Redesigned Regexp-2 queries.
62 * Revision 1.63 1996/10/29 14:09:39 adam
63 * Use of cisam system - enabled if setting isamc is 1.
65 * Revision 1.62 1996/10/11 10:57:01 adam
66 * New module recctrl. Used to manage records (extract/retrieval).
67 * Several files have been moved to the recctrl sub directory.
69 * Revision 1.61 1996/06/06 12:08:37 quinn
70 * Added showRecord function
72 * Revision 1.60 1996/06/04 10:18:12 adam
73 * Search/scan uses character mapping module.
75 * Revision 1.59 1996/05/14 15:47:07 adam
76 * Cleanup of various buffer size entities.
78 * Revision 1.58 1996/05/14 06:16:38 adam
79 * Compact use/set bytes used in search service.
81 * Revision 1.57 1996/05/13 14:23:04 adam
82 * Work on compaction of set/use bytes in dictionary.
84 * Revision 1.56 1996/05/09 09:54:42 adam
85 * Server supports maps from one logical attributes to a list of physical
87 * The extraction process doesn't make space consuming 'any' keys.
89 * Revision 1.55 1996/05/09 07:28:55 quinn
90 * Work towards phrases and multiple registers
92 * Revision 1.54 1996/05/01 13:46:35 adam
93 * First work on multiple records in one file.
94 * New option, -offset, to the "unread" command in the filter module.
96 * Revision 1.53 1996/04/26 12:09:43 adam
97 * Added a few comments.
99 * Revision 1.52 1996/04/25 13:27:57 adam
100 * Function recordExtract modified so that files with no keys (possibly empty)
103 * Revision 1.51 1996/03/19 11:08:42 adam
104 * Bug fix: Log preamble wasn't always turned off after recordExtract.
106 * Revision 1.50 1996/02/12 18:45:36 adam
107 * New fileVerboseFlag in record group control.
109 * Revision 1.49 1996/02/05 12:29:57 adam
110 * Logging reduced a bit.
111 * The remaining running time is estimated during register merge.
113 * Revision 1.48 1996/02/01 20:53:26 adam
114 * The temporary per-record keys are compacted a little, and duplication
115 * of the per-records keys are avoided when they are saved in the record
116 * information buffer.
118 * Revision 1.47 1996/01/17 14:57:48 adam
119 * Prototype changed for reader functions in extract/retrieve. File
120 * is identified by 'void *' instead of 'int.
122 * Revision 1.46 1995/12/15 14:57:16 adam
125 * Revision 1.45 1995/12/15 12:37:41 adam
126 * In addRecordKeyAny: Writes key only when attrSet != -1.
128 * Revision 1.44 1995/12/12 16:00:54 adam
129 * System call sync(2) used after update/commit.
130 * Locking (based on fcntl) uses F_EXLCK and F_SHLCK instead of F_WRLCK
133 * Revision 1.43 1995/12/11 09:12:46 adam
134 * The rec_get function returns NULL if record doesn't exist - will
135 * happen in the server if the result set records have been deleted since
136 * the creation of the set (i.e. the search).
137 * The server saves a result temporarily if it is 'volatile', i.e. the
138 * set is register dependent.
140 * Revision 1.42 1995/12/07 17:38:46 adam
141 * Work locking mechanisms for concurrent updates/commit.
143 * Revision 1.41 1995/12/06 16:06:42 adam
144 * Better diagnostics. Work on 'real' dictionary deletion.
146 * Revision 1.40 1995/12/05 16:57:40 adam
147 * More work on regular patterns.
149 * Revision 1.39 1995/12/05 13:20:18 adam
150 * Bug fix: file_read sometimes returned early EOF.
152 * Revision 1.38 1995/12/04 17:59:21 adam
153 * More work on regular expression conversion.
155 * Revision 1.37 1995/12/04 14:22:27 adam
156 * Extra arg to recType_byName.
157 * Started work on new regular expression parsed input to
158 * structured records.
160 * Revision 1.36 1995/11/30 08:34:29 adam
161 * Started work on commit facility.
162 * Changed a few malloc/free to xmalloc/xfree.
164 * Revision 1.35 1995/11/28 14:26:21 adam
165 * Bug fix: recordId with constant wasn't right.
166 * Bug fix: recordId dictionary entry wasn't deleted when needed.
168 * Revision 1.34 1995/11/28 09:09:38 adam
169 * Zebra config renamed.
170 * Use setting 'recordId' to identify record now.
171 * Bug fix in recindex.c: rec_release_blocks was invokeded even
172 * though the blocks were already released.
173 * File traversal properly deletes records when needed.
175 * Revision 1.33 1995/11/27 09:56:20 adam
176 * Record info elements better enumerated. Internal store of records.
178 * Revision 1.32 1995/11/25 10:24:05 adam
179 * More record fields - they are enumerated now.
180 * New options: flagStoreData flagStoreKey.
182 * Revision 1.31 1995/11/24 11:31:35 adam
183 * Commands add & del read filenames from stdin if source directory is
185 * Match criteria supports 'constant' strings.
187 * Revision 1.30 1995/11/22 17:19:16 adam
188 * Record management uses the bfile system.
190 * Revision 1.29 1995/11/21 15:01:14 adam
191 * New general match criteria implemented.
192 * New feature: document groups.
194 * Revision 1.28 1995/11/21 09:20:30 adam
195 * Yet more work on record match.
197 * Revision 1.27 1995/11/20 16:59:45 adam
198 * New update method: the 'old' keys are saved for each records.
200 * Revision 1.26 1995/11/20 11:56:24 adam
201 * Work on new traversal.
203 * Revision 1.25 1995/11/16 15:34:54 adam
204 * Uses new record management system in both indexer and server.
206 * Revision 1.24 1995/11/15 19:13:08 adam
207 * Work on record management.
209 * Revision 1.23 1995/10/27 14:00:10 adam
210 * Implemented detection of database availability.
212 * Revision 1.22 1995/10/17 18:02:07 adam
213 * New feature: databases. Implemented as prefix to words in dictionary.
215 * Revision 1.21 1995/10/10 12:24:38 adam
216 * Temporary sort files are compressed.
218 * Revision 1.20 1995/10/06 13:52:05 adam
219 * Bug fixes. Handler may abort further scanning.
221 * Revision 1.19 1995/10/04 12:55:16 adam
222 * Bug fix in ranked search. Use=Any keys inserted.
224 * Revision 1.18 1995/10/04 09:37:08 quinn
227 * Revision 1.17 1995/10/03 14:28:57 adam
228 * Buffered read in extract works.
230 * Revision 1.16 1995/10/03 14:28:45 adam
231 * Work on more effecient read handler in extract.
233 * Revision 1.15 1995/10/02 15:42:53 adam
234 * Extract uses file descriptors instead of FILE pointers.
236 * Revision 1.14 1995/10/02 15:29:13 adam
237 * More logging in file_extract.
239 * Revision 1.13 1995/09/29 14:01:39 adam
242 * Revision 1.12 1995/09/28 14:22:56 adam
243 * Sort uses smaller temporary files.
245 * Revision 1.11 1995/09/28 12:10:31 adam
246 * Bug fixes. Field prefix used in queries.
248 * Revision 1.10 1995/09/28 09:19:41 adam
249 * xfree/xmalloc used everywhere.
250 * Extract/retrieve method seems to work for text records.
252 * Revision 1.9 1995/09/27 12:22:28 adam
253 * More work on extract in record control.
254 * Field name is not in isam keys but in prefix in dictionary words.
256 * Revision 1.8 1995/09/14 07:48:22 adam
257 * Record control management.
259 * Revision 1.7 1995/09/11 13:09:32 adam
260 * More work on relevance feedback.
262 * Revision 1.6 1995/09/08 14:52:27 adam
263 * Minor changes. Dictionary is lower case now.
265 * Revision 1.5 1995/09/06 16:11:16 adam
266 * Option: only one word key per file.
268 * Revision 1.4 1995/09/05 15:28:39 adam
269 * More work on search engine.
271 * Revision 1.3 1995/09/04 12:33:41 adam
272 * Various cleanup. YAZ util used instead.
274 * Revision 1.2 1995/09/04 09:10:34 adam
275 * More work on index add/del/update.
276 * Merge sort implemented.
277 * Initial work on z39 server.
279 * Revision 1.1 1995/09/01 14:06:35 adam
280 * Split of work into more files.
297 static Dict matchDict;
299 static Records records = NULL;
301 static char **key_buf;
302 static size_t ptr_top;
304 static size_t key_buf_used;
305 static int key_file_no;
307 static int records_inserted = 0;
308 static int records_updated = 0;
309 static int records_deleted = 0;
310 static int records_processed = 0;
312 static ZebTargetInfo *zti = NULL;
314 static void logRecord (int showFlag)
318 if (showFlag || !(records_processed % 1000))
320 logf (LOG_LOG, "Records: %7d i/u/d %d/%d/%d",
321 records_processed, records_inserted, records_updated,
326 int key_open (BFiles bfs, int mem, int rw)
329 mem = atoi(res_get_def (common_resource, "memMax", "4"))*1024*1024;
332 key_buf = xmalloc (mem);
333 ptr_top = mem/sizeof(char*);
339 if (!(matchDict = dict_open (bfs, GMATCH_DICT, 50, rw)))
341 logf (LOG_FATAL, "dict_open fail of %s", GMATCH_DICT);
345 records = rec_open (bfs, rw);
348 dict_close (matchDict);
351 zti = zebTargetInfo_open (records, rw);
354 rec_close (&records);
355 dict_close (matchDict);
367 void encode_key_init (struct encode_info *i)
373 char *encode_key_int (int d, char *bp)
382 else if (d <= 4194303)
384 *bp++ = 128 + (d>>16);
385 *bp++ = (d>>8) & 255;
390 *bp++ = 192 + (d>>24);
391 *bp++ = (d>>16) & 255;
392 *bp++ = (d>>8) & 255;
398 void encode_key_write (char *k, struct encode_info *i, FILE *outf)
403 while ((*bp++ = *k++))
405 memcpy (&key, k+1, sizeof(struct it_key));
406 bp = encode_key_int ( (key.sysno - i->sysno) * 2 + *k, bp);
407 if (i->sysno != key.sysno)
409 i->sysno = key.sysno;
412 bp = encode_key_int (key.seqno - i->seqno, bp);
413 i->seqno = key.seqno;
414 if (fwrite (i->buf, bp - i->buf, 1, outf) != 1)
416 logf (LOG_FATAL|LOG_ERRNO, "fwrite");
424 static int key_y_len;
426 static int key_y_compare (const void *p1, const void *p2)
430 if ((r = key_compare (*(char**) p1 + key_y_len + 1,
431 *(char**) p2 + key_y_len + 1)))
433 return *(*(char**) p1 + key_y_len) - *(*(char**) p2 + key_y_len);
436 static int key_x_compare (const void *p1, const void *p2)
438 return strcmp (*(char**) p1, *(char**) p2);
442 void key_flush (void)
447 struct encode_info encode_info;
456 logf (LOG_LOG, "sorting section %d", key_file_no);
458 qsort (key_buf + ptr_top-ptr_i, ptr_i, sizeof(char*), key_qsort_compare);
459 getFnameTmp (out_fname, key_file_no);
461 if (!(outf = fopen (out_fname, "w")))
463 logf (LOG_FATAL|LOG_ERRNO, "fopen %s", out_fname);
466 logf (LOG_LOG, "writing section %d", key_file_no);
467 prevcp = cp = key_buf[ptr_top-ptr_i];
469 encode_key_init (&encode_info);
470 encode_key_write (cp, &encode_info, outf);
473 cp = key_buf[ptr_top-ptr_i];
474 if (strcmp (cp, prevcp))
476 encode_key_init (&encode_info);
477 encode_key_write (cp, &encode_info, outf);
481 encode_key_write (cp + strlen(cp), &encode_info, outf);
484 qsort (key_buf + ptr_top-ptr_i, ptr_i, sizeof(char*), key_x_compare);
485 getFnameTmp (out_fname, key_file_no);
487 if (!(outf = fopen (out_fname, "w")))
489 logf (LOG_FATAL|LOG_ERRNO, "fopen %s", out_fname);
492 logf (LOG_LOG, "writing section %d", key_file_no);
494 prevcp = key_buf[ptr_top-i];
496 if (!--i || strcmp (prevcp, key_buf[ptr_top-i]))
498 key_y_len = strlen(prevcp)+1;
500 logf (LOG_LOG, "key_y_len: %2d %02x %02x %s",
501 key_y_len, prevcp[0], prevcp[1], 2+prevcp);
503 qsort (key_buf + ptr_top-ptr_i, ptr_i - i,
504 sizeof(char*), key_y_compare);
505 cp = key_buf[ptr_top-ptr_i];
507 encode_key_init (&encode_info);
508 encode_key_write (cp, &encode_info, outf);
511 cp = key_buf[ptr_top-ptr_i];
512 encode_key_write (cp+key_y_len, &encode_info, outf);
516 prevcp = key_buf[ptr_top-ptr_i];
521 logf (LOG_FATAL|LOG_ERRNO, "fclose %s", out_fname);
524 logf (LOG_LOG, "finished section %d", key_file_no);
534 zebTargetInfo_close (zti, 1);
536 rec_close (&records);
537 dict_close (matchDict);
543 static void wordInit (RecWord *p)
559 static void addRecordKey (const RecWord *p)
568 if (reckeys.buf_used+1024 > reckeys.buf_max)
572 b = xmalloc (reckeys.buf_max += 128000);
573 if (reckeys.buf_used > 0)
574 memcpy (b, reckeys.buf, reckeys.buf_used);
578 dst = reckeys.buf + reckeys.buf_used;
580 attrSet = p->attrSet;
581 if (reckeys.buf_used > 0 && reckeys.prevAttrSet == attrSet)
584 reckeys.prevAttrSet = attrSet;
585 attrUse = p->attrUse;
586 if (reckeys.buf_used > 0 && reckeys.prevAttrUse == attrUse)
589 reckeys.prevAttrUse = attrUse;
591 diff = 1 + p->seqno - reckeys.prevSeqNo;
592 if (diff >= 1 && diff <= 15)
597 reckeys.prevSeqNo = p->seqno;
603 memcpy (dst, &attrSet, sizeof(attrSet));
604 dst += sizeof(attrSet);
608 memcpy (dst, &attrUse, sizeof(attrUse));
609 dst += sizeof(attrUse);
611 *dst++ = p->reg_type;
612 for (i = 0; p->string[i] && i < IT_MAX_WORD-3; i++)
613 *dst++ = p->string[i];
618 memcpy (dst, &p->seqno, sizeof(p->seqno));
619 dst += sizeof(p->seqno);
621 reckeys.buf_used = dst - reckeys.buf;
624 static void flushRecordKeys (SYSNO sysno, int cmd, struct recKeys *reckeys,
625 const char *databaseName)
632 if (zebTargetInfo_curDatabase (zti, databaseName))
634 if (zebTargetInfo_newDatabase (zti, databaseName))
637 while (off < reckeys->buf_used)
639 const char *src = reckeys->buf + off;
647 memcpy (&attrSet, src, sizeof(attrSet));
648 src += sizeof(attrSet);
652 memcpy (&attrUse, src, sizeof(attrUse));
653 src += sizeof(attrUse);
655 if (key_buf_used + 1024 > (ptr_top-ptr_i)*sizeof(char*))
658 key_buf[ptr_top-ptr_i] = (char*)key_buf + key_buf_used;
660 ch = zebTargetInfo_lookupSU (zti, attrSet, attrUse);
662 ch = zebTargetInfo_addSU (zti, attrSet, attrUse);
664 ((char*) key_buf) [key_buf_used++] = ch;
666 ((char*)key_buf) [key_buf_used++] = *src++;
668 ((char*)key_buf) [key_buf_used++] = '\0';
669 ((char*) key_buf)[key_buf_used++] = cmd;
672 seqno += ((lead>>2) & 15)-1;
675 memcpy (&seqno, src, sizeof(seqno));
676 src += sizeof(seqno);
680 memcpy ((char*)key_buf + key_buf_used, &key, sizeof(key));
681 key_buf_used += sizeof(key);
682 off = src - reckeys->buf;
684 assert (off == reckeys->buf_used);
687 static const char **searchRecordKey (struct recKeys *reckeys,
688 int attrSetS, int attrUseS)
690 static const char *ws[32];
698 for (i = 0; i<32; i++)
701 while (off < reckeys->buf_used)
704 const char *src = reckeys->buf + off;
712 memcpy (&attrSet, src, sizeof(attrSet));
713 src += sizeof(attrSet);
717 memcpy (&attrUse, src, sizeof(attrUse));
718 src += sizeof(attrUse);
724 seqno += ((lead>>2) & 15)-1;
727 memcpy (&seqno, src, sizeof(seqno));
728 src += sizeof(seqno);
730 if (attrUseS == attrUse && attrSetS == attrSet)
737 woff = seqno - startSeq;
738 if (woff >= 0 && woff < 31)
742 off = src - reckeys->buf;
744 assert (off == reckeys->buf_used);
748 struct file_read_info {
756 static struct file_read_info *file_read_start (int fd)
758 struct file_read_info *fi = xmalloc (sizeof(*fi));
762 fi->file_moffset = 0;
766 static void file_read_stop (struct file_read_info *fi)
772 static off_t file_seek (void *handle, off_t offset)
774 struct file_read_info *p = handle;
775 p->file_offset = offset;
776 return lseek (p->fd, offset, SEEK_SET);
779 static off_t file_tell (void *handle)
781 struct file_read_info *p = handle;
782 return p->file_offset;
785 static int file_read (void *handle, char *buf, size_t count)
787 struct file_read_info *p = handle;
790 r = read (fd, buf, count);
794 if (p->file_offset > p->file_max)
795 p->file_max = p->file_offset;
800 static void file_begin (void *handle)
802 struct file_read_info *p = handle;
804 p->file_offset = p->file_moffset;
806 lseek (p->fd, p->file_moffset, SEEK_SET);
810 static void file_end (void *handle, off_t offset)
812 struct file_read_info *p = handle;
814 assert (p->file_more == 0);
816 p->file_moffset = offset;
819 static int atois (const char **s)
822 while ( (c=**s) >= '0' && c <= '9')
824 val = val*10 + c - '0';
830 static char *fileMatchStr (struct recKeys *reckeys, struct recordGroup *rGroup,
834 static char dstBuf[2048];
836 const char *s = spec;
837 static const char **w;
842 while (*s == ' ' || *s == '\t')
849 int attrSet, attrUse;
853 attrSet = atois (&s);
856 logf (LOG_WARN, "Missing , in match criteria %s in group %s",
857 spec, rGroup->groupName ? rGroup->groupName : "none");
861 attrUse = atois (&s);
862 w = searchRecordKey (reckeys, attrSet, attrUse);
867 for (i = 0; i<32; i++)
872 logf (LOG_WARN, "Missing ) in match criteria %s in group %s",
873 spec, rGroup->groupName ? rGroup->groupName : "none");
878 for (i = 0; i<32; i++)
879 if (matchFlag[i] && w[i])
891 logf (LOG_WARN, "Record didn't contain match"
892 " fields in (%d,%d)", attrSet, attrUse);
900 const char *spec_src = NULL;
901 const char *s1 = ++s;
902 while (*s1 && *s1 != ' ' && *s1 != '\t')
908 memcpy (special, s, spec_len);
909 special[spec_len] = '\0';
912 if (!strcmp (special, "group"))
913 spec_src = rGroup->groupName;
914 else if (!strcmp (special, "database"))
915 spec_src = rGroup->databaseName;
916 else if (!strcmp (special, "filename"))
918 else if (!strcmp (special, "type"))
919 spec_src = rGroup->recordType;
924 strcpy (dst, spec_src);
925 dst += strlen (spec_src);
928 else if (*s == '\"' || *s == '\'')
930 int stopMarker = *s++;
934 while (*s && *s != stopMarker)
937 tmpString[i++] = *s++;
942 strcpy (dst, tmpString);
943 dst += strlen (tmpString);
947 logf (LOG_WARN, "Syntax error in match criteria %s in group %s",
948 spec, rGroup->groupName ? rGroup->groupName : "none");
955 logf (LOG_WARN, "No match criteria for record %s in group %s",
956 fname, rGroup->groupName ? rGroup->groupName : "none");
963 struct recordLogInfo {
966 struct recordGroup *rGroup;
969 static void recordLogPreamble (int level, const char *msg, void *info)
971 struct recordLogInfo *p = info;
972 FILE *outf = log_file ();
976 fprintf (outf, "File %s, offset %d, type %s\n",
977 p->rGroup->recordType, p->recordOffset, p->fname);
978 log_event_start (NULL, NULL);
981 static int recordExtract (SYSNO *sysno, const char *fname,
982 struct recordGroup *rGroup, int deleteFlag,
983 struct file_read_info *fi, RecType recType,
986 struct recExtractCtrl extractCtrl;
990 off_t recordOffset = 0;
992 struct recordLogInfo logInfo;
996 /* we are going to read from a file, so prepare the extraction */
998 extractCtrl.subType = subType;
999 extractCtrl.init = wordInit;
1000 extractCtrl.add = addRecordKey;
1001 extractCtrl.dh = rGroup->dh;
1003 reckeys.buf_used = 0;
1004 reckeys.prevAttrUse = -1;
1005 reckeys.prevAttrSet = -1;
1006 reckeys.prevSeqNo = 0;
1008 recordOffset = fi->file_moffset;
1009 extractCtrl.offset = recordOffset;
1010 extractCtrl.readf = file_read;
1011 extractCtrl.seekf = file_seek;
1012 extractCtrl.tellf = file_tell;
1013 extractCtrl.endf = file_end;
1014 extractCtrl.zebra_maps = rGroup->zebra_maps;
1015 extractCtrl.flagShowRecords = !rGroup->flagRw;
1016 if (!rGroup->flagRw)
1017 printf ("File: %s %ld\n", fname, (long) recordOffset);
1019 logInfo.fname = fname;
1020 logInfo.recordOffset = recordOffset;
1021 logInfo.rGroup = rGroup;
1022 log_event_start (recordLogPreamble, &logInfo);
1024 r = (*recType->extract)(&extractCtrl);
1026 log_event_start (NULL, NULL);
1030 /* error occured during extraction ... */
1031 if (rGroup->flagRw &&
1032 records_processed < rGroup->fileVerboseLimit)
1034 logf (LOG_WARN, "fail %s %s %ld code = %d", rGroup->recordType,
1035 fname, (long) recordOffset, r);
1039 if (reckeys.buf_used == 0)
1041 /* the extraction process returned no information - the record
1042 is probably empty - unless flagShowRecords is in use */
1043 if (!rGroup->flagRw)
1045 logf (LOG_WARN, "No keys generated for file %s", fname);
1046 logf (LOG_WARN, " The file is probably empty");
1051 /* perform match if sysno not known and if match criteria is specified */
1058 if (rGroup->recordId && *rGroup->recordId)
1062 matchStr = fileMatchStr (&reckeys, rGroup, fname,
1066 rinfo = dict_lookup (matchDict, matchStr);
1068 memcpy (sysno, rinfo+1, sizeof(*sysno));
1072 logf (LOG_WARN, "Bad match criteria");
1083 logf (LOG_LOG, "delete %s %s %ld", rGroup->recordType,
1084 fname, (long) recordOffset);
1085 logf (LOG_WARN, "cannot delete record above (seems new)");
1088 if (records_processed < rGroup->fileVerboseLimit)
1089 logf (LOG_LOG, "add %s %s %ld", rGroup->recordType,
1090 fname, (long) recordOffset);
1091 rec = rec_new (records);
1092 *sysno = rec->sysno;
1096 dict_insert (matchDict, matchStr, sizeof(*sysno), sysno);
1098 flushRecordKeys (*sysno, 1, &reckeys, rGroup->databaseName);
1104 /* record already exists */
1105 struct recKeys delkeys;
1107 rec = rec_get (records, *sysno);
1109 delkeys.buf_used = rec->size[recInfo_delKeys];
1110 delkeys.buf = rec->info[recInfo_delKeys];
1111 flushRecordKeys (*sysno, 0, &delkeys, rec->info[recInfo_databaseName]);
1114 /* record going to be deleted */
1115 if (!delkeys.buf_used)
1117 logf (LOG_LOG, "delete %s %s %ld", rGroup->recordType,
1118 fname, (long) recordOffset);
1119 logf (LOG_WARN, "cannot delete file above, storeKeys false");
1123 if (records_processed < rGroup->fileVerboseLimit)
1124 logf (LOG_LOG, "delete %s %s %ld", rGroup->recordType,
1125 fname, (long) recordOffset);
1128 dict_delete (matchDict, matchStr);
1129 rec_del (records, &rec);
1136 /* record going to be updated */
1137 if (!delkeys.buf_used)
1139 logf (LOG_LOG, "update %s %s %ld", rGroup->recordType,
1140 fname, (long) recordOffset);
1141 logf (LOG_WARN, "cannot update file above, storeKeys false");
1145 if (records_processed < rGroup->fileVerboseLimit)
1146 logf (LOG_LOG, "update %s %s %ld", rGroup->recordType,
1147 fname, (long) recordOffset);
1148 flushRecordKeys (*sysno, 1, &reckeys, rGroup->databaseName);
1153 /* update file type */
1154 xfree (rec->info[recInfo_fileType]);
1155 rec->info[recInfo_fileType] =
1156 rec_strdup (rGroup->recordType, &rec->size[recInfo_fileType]);
1158 /* update filename */
1159 xfree (rec->info[recInfo_filename]);
1160 rec->info[recInfo_filename] =
1161 rec_strdup (fname, &rec->size[recInfo_filename]);
1163 /* update delete keys */
1164 xfree (rec->info[recInfo_delKeys]);
1165 if (reckeys.buf_used > 0 && rGroup->flagStoreKeys == 1)
1168 rec->size[recInfo_delKeys] = reckeys.buf_used;
1169 rec->info[recInfo_delKeys] = reckeys.buf;
1171 reckeys.buf_max = 0;
1173 rec->info[recInfo_delKeys] = xmalloc (reckeys.buf_used);
1174 rec->size[recInfo_delKeys] = reckeys.buf_used;
1175 memcpy (rec->info[recInfo_delKeys], reckeys.buf,
1176 rec->size[recInfo_delKeys]);
1181 rec->info[recInfo_delKeys] = NULL;
1182 rec->size[recInfo_delKeys] = 0;
1185 /* update store data */
1186 xfree (rec->info[recInfo_storeData]);
1187 if (rGroup->flagStoreData == 1)
1189 int size = fi->file_moffset - recordOffset;
1191 size = fi->file_max - recordOffset;
1192 rec->size[recInfo_storeData] = size;
1193 rec->info[recInfo_storeData] = xmalloc (size);
1194 if (lseek (fi->fd, recordOffset, SEEK_SET) < 0)
1196 logf (LOG_ERRNO|LOG_FATAL, "seek to %ld in %s", fname,
1197 (long) recordOffset);
1200 if (read (fi->fd, rec->info[recInfo_storeData], size) < size)
1202 logf (LOG_ERRNO|LOG_FATAL, "read %d bytes of %s",
1203 fi->file_max, fname);
1209 rec->info[recInfo_storeData] = NULL;
1210 rec->size[recInfo_storeData] = 0;
1212 /* update database name */
1213 xfree (rec->info[recInfo_databaseName]);
1214 rec->info[recInfo_databaseName] =
1215 rec_strdup (rGroup->databaseName, &rec->size[recInfo_databaseName]);
1218 xfree (rec->info[recInfo_offset]);
1220 rec->size[recInfo_offset] = sizeof(recordOffset);
1221 rec->info[recInfo_offset] = xmalloc (sizeof(recordOffset));
1222 memcpy (rec->info[recInfo_offset], &recordOffset, sizeof(recordOffset));
1224 /* commit this record */
1225 rec_put (records, &rec);
1230 int fileExtract (SYSNO *sysno, const char *fname,
1231 const struct recordGroup *rGroupP, int deleteFlag)
1239 struct recordGroup rGroupM;
1240 struct recordGroup *rGroup = &rGroupM;
1241 struct file_read_info *fi;
1243 memcpy (rGroup, rGroupP, sizeof(*rGroupP));
1245 if (!rGroup->groupName || !*rGroup->groupName)
1248 sprintf (gprefix, "%s.", rGroup->groupName);
1250 logf (LOG_DEBUG, "fileExtract %s", fname);
1252 /* determine file extension */
1253 for (i = strlen(fname); --i >= 0; )
1254 if (fname[i] == '/')
1259 else if (fname[i] == '.')
1261 strcpy (ext, fname+i+1);
1264 /* determine file type - depending on extension */
1265 if (!rGroup->recordType)
1267 sprintf (ext_res, "%srecordType.%s", gprefix, ext);
1268 if (!(rGroup->recordType = res_get (common_resource, ext_res)))
1270 sprintf (ext_res, "%srecordType", gprefix);
1271 if (!(rGroup->recordType = res_get (common_resource, ext_res)))
1273 if (records_processed < rGroup->fileVerboseLimit)
1274 logf (LOG_LOG, "? %s", fname);
1279 if (!rGroup->recordType)
1281 if (records_processed < rGroup->fileVerboseLimit)
1282 logf (LOG_LOG, "? record %s", fname);
1285 if (!(recType = recType_byName (rGroup->recordType, subType)))
1287 logf (LOG_WARN, "No such record type: %s", rGroup->recordType);
1291 /* determine match criteria */
1292 if (!rGroup->recordId)
1294 sprintf (ext_res, "%srecordId.%s", gprefix, ext);
1295 rGroup->recordId = res_get (common_resource, ext_res);
1298 /* determine database name */
1299 if (!rGroup->databaseName)
1301 sprintf (ext_res, "%sdatabase.%s", gprefix, ext);
1302 if (!(rGroup->databaseName = res_get (common_resource, ext_res)))
1304 sprintf (ext_res, "%sdatabase", gprefix);
1305 rGroup->databaseName = res_get (common_resource, ext_res);
1308 if (!rGroup->databaseName)
1309 rGroup->databaseName = "Default";
1311 if (rGroup->flagStoreData == -1)
1314 sprintf (ext_res, "%sstoreData.%s", gprefix, ext);
1315 if (!(sval = res_get (common_resource, ext_res)))
1317 sprintf (ext_res, "%sstoreData", gprefix);
1318 sval = res_get (common_resource, ext_res);
1321 rGroup->flagStoreData = atoi (sval);
1323 if (rGroup->flagStoreData == -1)
1324 rGroup->flagStoreData = 0;
1326 if (rGroup->flagStoreKeys == -1)
1330 sprintf (ext_res, "%sstoreKeys.%s", gprefix, ext);
1331 if (!(sval = res_get (common_resource, ext_res)))
1333 sprintf (ext_res, "%sstoreKeys", gprefix);
1334 sval = res_get (common_resource, ext_res);
1337 rGroup->flagStoreKeys = atoi (sval);
1339 if (rGroup->flagStoreKeys == -1)
1340 rGroup->flagStoreKeys = 0;
1342 if (sysno && deleteFlag)
1346 if ((fd = open (fname, O_BINARY|O_RDONLY)) == -1)
1348 logf (LOG_WARN|LOG_ERRNO, "open %s", fname);
1352 fi = file_read_start (fd);
1356 r = recordExtract (sysno, fname, rGroup, deleteFlag, fi,
1358 } while (r && !sysno && fi->file_more);
1359 file_read_stop (fi);