2 * Copyright (C) 1994-1998, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.78 1998-02-10 12:03:05 adam
10 * Revision 1.77 1998/01/12 15:04:08 adam
11 * The test option (-s) only uses read-lock (and not write lock).
13 * Revision 1.76 1997/10/27 14:33:04 adam
14 * Moved towards generic character mapping depending on "structure"
15 * field in abstract syntax file. Fixed a few memory leaks. Fixed
16 * bug with negative integers when doing searches with relational
19 * Revision 1.75 1997/09/17 12:19:12 adam
20 * Zebra version corresponds to YAZ version 1.4.
21 * Changed Zebra server so that it doesn't depend on global common_resource.
23 * Revision 1.74 1997/09/09 13:38:06 adam
24 * Partial port to WIN95/NT.
26 * Revision 1.73 1997/09/04 13:57:20 adam
27 * New file extract/retrieve method tellf (added).
28 * Added O_BINARY for open calls.
30 * Revision 1.72 1997/07/15 16:32:29 adam
31 * Bug fix: Match handler didn't terminate the resulting string!
33 * Revision 1.71 1997/07/15 16:28:41 adam
34 * Bug fix: storeData didn't work with files with multiple records.
35 * Bug fix: fixed memory management with records; not really well
38 * Revision 1.70 1997/07/01 13:00:42 adam
39 * Bug fix in routine searchRecordKey: uninitialized variables.
41 * Revision 1.69 1997/04/29 09:26:03 adam
42 * Bug fix: generic recordId handling didn't work for compressed internal
45 * Revision 1.68 1997/02/12 20:39:45 adam
46 * Implemented options -f <n> that limits the log to the first <n>
48 * Changed some log messages also.
50 * Revision 1.67 1996/11/15 15:02:14 adam
51 * Minor changes regarding logging.
53 * Revision 1.66 1996/11/14 09:52:21 adam
54 * Strings in record keys bound by IT_MAX_WORD.
56 * Revision 1.65 1996/11/14 08:57:56 adam
57 * Reduction of storeKeys area.
59 * Revision 1.64 1996/11/08 11:10:16 adam
60 * Buffers used during file match got bigger.
61 * Compressed ISAM support everywhere.
62 * Bug fixes regarding masking characters in queries.
63 * Redesigned Regexp-2 queries.
65 * Revision 1.63 1996/10/29 14:09:39 adam
66 * Use of cisam system - enabled if setting isamc is 1.
68 * Revision 1.62 1996/10/11 10:57:01 adam
69 * New module recctrl. Used to manage records (extract/retrieval).
70 * Several files have been moved to the recctrl sub directory.
72 * Revision 1.61 1996/06/06 12:08:37 quinn
73 * Added showRecord function
75 * Revision 1.60 1996/06/04 10:18:12 adam
76 * Search/scan uses character mapping module.
78 * Revision 1.59 1996/05/14 15:47:07 adam
79 * Cleanup of various buffer size entities.
81 * Revision 1.58 1996/05/14 06:16:38 adam
82 * Compact use/set bytes used in search service.
84 * Revision 1.57 1996/05/13 14:23:04 adam
85 * Work on compaction of set/use bytes in dictionary.
87 * Revision 1.56 1996/05/09 09:54:42 adam
88 * Server supports maps from one logical attributes to a list of physical
90 * The extraction process doesn't make space consuming 'any' keys.
92 * Revision 1.55 1996/05/09 07:28:55 quinn
93 * Work towards phrases and multiple registers
95 * Revision 1.54 1996/05/01 13:46:35 adam
96 * First work on multiple records in one file.
97 * New option, -offset, to the "unread" command in the filter module.
99 * Revision 1.53 1996/04/26 12:09:43 adam
100 * Added a few comments.
102 * Revision 1.52 1996/04/25 13:27:57 adam
103 * Function recordExtract modified so that files with no keys (possibly empty)
106 * Revision 1.51 1996/03/19 11:08:42 adam
107 * Bug fix: Log preamble wasn't always turned off after recordExtract.
109 * Revision 1.50 1996/02/12 18:45:36 adam
110 * New fileVerboseFlag in record group control.
112 * Revision 1.49 1996/02/05 12:29:57 adam
113 * Logging reduced a bit.
114 * The remaining running time is estimated during register merge.
116 * Revision 1.48 1996/02/01 20:53:26 adam
117 * The temporary per-record keys are compacted a little, and duplication
118 * of the per-records keys are avoided when they are saved in the record
119 * information buffer.
121 * Revision 1.47 1996/01/17 14:57:48 adam
122 * Prototype changed for reader functions in extract/retrieve. File
123 * is identified by 'void *' instead of 'int.
125 * Revision 1.46 1995/12/15 14:57:16 adam
128 * Revision 1.45 1995/12/15 12:37:41 adam
129 * In addRecordKeyAny: Writes key only when attrSet != -1.
131 * Revision 1.44 1995/12/12 16:00:54 adam
132 * System call sync(2) used after update/commit.
133 * Locking (based on fcntl) uses F_EXLCK and F_SHLCK instead of F_WRLCK
136 * Revision 1.43 1995/12/11 09:12:46 adam
137 * The rec_get function returns NULL if record doesn't exist - will
138 * happen in the server if the result set records have been deleted since
139 * the creation of the set (i.e. the search).
140 * The server saves a result temporarily if it is 'volatile', i.e. the
141 * set is register dependent.
143 * Revision 1.42 1995/12/07 17:38:46 adam
144 * Work locking mechanisms for concurrent updates/commit.
146 * Revision 1.41 1995/12/06 16:06:42 adam
147 * Better diagnostics. Work on 'real' dictionary deletion.
149 * Revision 1.40 1995/12/05 16:57:40 adam
150 * More work on regular patterns.
152 * Revision 1.39 1995/12/05 13:20:18 adam
153 * Bug fix: file_read sometimes returned early EOF.
155 * Revision 1.38 1995/12/04 17:59:21 adam
156 * More work on regular expression conversion.
158 * Revision 1.37 1995/12/04 14:22:27 adam
159 * Extra arg to recType_byName.
160 * Started work on new regular expression parsed input to
161 * structured records.
163 * Revision 1.36 1995/11/30 08:34:29 adam
164 * Started work on commit facility.
165 * Changed a few malloc/free to xmalloc/xfree.
167 * Revision 1.35 1995/11/28 14:26:21 adam
168 * Bug fix: recordId with constant wasn't right.
169 * Bug fix: recordId dictionary entry wasn't deleted when needed.
171 * Revision 1.34 1995/11/28 09:09:38 adam
172 * Zebra config renamed.
173 * Use setting 'recordId' to identify record now.
174 * Bug fix in recindex.c: rec_release_blocks was invokeded even
175 * though the blocks were already released.
176 * File traversal properly deletes records when needed.
178 * Revision 1.33 1995/11/27 09:56:20 adam
179 * Record info elements better enumerated. Internal store of records.
181 * Revision 1.32 1995/11/25 10:24:05 adam
182 * More record fields - they are enumerated now.
183 * New options: flagStoreData flagStoreKey.
185 * Revision 1.31 1995/11/24 11:31:35 adam
186 * Commands add & del read filenames from stdin if source directory is
188 * Match criteria supports 'constant' strings.
190 * Revision 1.30 1995/11/22 17:19:16 adam
191 * Record management uses the bfile system.
193 * Revision 1.29 1995/11/21 15:01:14 adam
194 * New general match criteria implemented.
195 * New feature: document groups.
197 * Revision 1.28 1995/11/21 09:20:30 adam
198 * Yet more work on record match.
200 * Revision 1.27 1995/11/20 16:59:45 adam
201 * New update method: the 'old' keys are saved for each records.
203 * Revision 1.26 1995/11/20 11:56:24 adam
204 * Work on new traversal.
206 * Revision 1.25 1995/11/16 15:34:54 adam
207 * Uses new record management system in both indexer and server.
209 * Revision 1.24 1995/11/15 19:13:08 adam
210 * Work on record management.
212 * Revision 1.23 1995/10/27 14:00:10 adam
213 * Implemented detection of database availability.
215 * Revision 1.22 1995/10/17 18:02:07 adam
216 * New feature: databases. Implemented as prefix to words in dictionary.
218 * Revision 1.21 1995/10/10 12:24:38 adam
219 * Temporary sort files are compressed.
221 * Revision 1.20 1995/10/06 13:52:05 adam
222 * Bug fixes. Handler may abort further scanning.
224 * Revision 1.19 1995/10/04 12:55:16 adam
225 * Bug fix in ranked search. Use=Any keys inserted.
227 * Revision 1.18 1995/10/04 09:37:08 quinn
230 * Revision 1.17 1995/10/03 14:28:57 adam
231 * Buffered read in extract works.
233 * Revision 1.16 1995/10/03 14:28:45 adam
234 * Work on more effecient read handler in extract.
236 * Revision 1.15 1995/10/02 15:42:53 adam
237 * Extract uses file descriptors instead of FILE pointers.
239 * Revision 1.14 1995/10/02 15:29:13 adam
240 * More logging in file_extract.
242 * Revision 1.13 1995/09/29 14:01:39 adam
245 * Revision 1.12 1995/09/28 14:22:56 adam
246 * Sort uses smaller temporary files.
248 * Revision 1.11 1995/09/28 12:10:31 adam
249 * Bug fixes. Field prefix used in queries.
251 * Revision 1.10 1995/09/28 09:19:41 adam
252 * xfree/xmalloc used everywhere.
253 * Extract/retrieve method seems to work for text records.
255 * Revision 1.9 1995/09/27 12:22:28 adam
256 * More work on extract in record control.
257 * Field name is not in isam keys but in prefix in dictionary words.
259 * Revision 1.8 1995/09/14 07:48:22 adam
260 * Record control management.
262 * Revision 1.7 1995/09/11 13:09:32 adam
263 * More work on relevance feedback.
265 * Revision 1.6 1995/09/08 14:52:27 adam
266 * Minor changes. Dictionary is lower case now.
268 * Revision 1.5 1995/09/06 16:11:16 adam
269 * Option: only one word key per file.
271 * Revision 1.4 1995/09/05 15:28:39 adam
272 * More work on search engine.
274 * Revision 1.3 1995/09/04 12:33:41 adam
275 * Various cleanup. YAZ util used instead.
277 * Revision 1.2 1995/09/04 09:10:34 adam
278 * More work on index add/del/update.
279 * Merge sort implemented.
280 * Initial work on z39 server.
282 * Revision 1.1 1995/09/01 14:06:35 adam
283 * Split of work into more files.
302 static Dict matchDict;
304 static Records records = NULL;
305 static SortIdx sortIdx = NULL;
307 static char **key_buf;
308 static size_t ptr_top;
310 static size_t key_buf_used;
311 static int key_file_no;
313 static int records_inserted = 0;
314 static int records_updated = 0;
315 static int records_deleted = 0;
316 static int records_processed = 0;
318 static ZebTargetInfo *zti = NULL;
320 static void logRecord (int showFlag)
324 if (showFlag || !(records_processed % 1000))
326 logf (LOG_LOG, "Records: %7d i/u/d %d/%d/%d",
327 records_processed, records_inserted, records_updated,
332 int key_open (BFiles bfs, int mem, int rw)
335 mem = atoi(res_get_def (common_resource, "memMax", "4"))*1024*1024;
338 key_buf = xmalloc (mem);
339 ptr_top = mem/sizeof(char*);
345 if (!(matchDict = dict_open (bfs, GMATCH_DICT, 50, rw)))
347 logf (LOG_FATAL, "dict_open fail of %s", GMATCH_DICT);
351 records = rec_open (bfs, rw);
354 dict_close (matchDict);
357 zti = zebTargetInfo_open (records, rw);
360 rec_close (&records);
361 dict_close (matchDict);
364 sortIdx = sortIdx_open (bfs, 1);
374 void encode_key_init (struct encode_info *i)
380 char *encode_key_int (int d, char *bp)
389 else if (d <= 4194303)
391 *bp++ = 128 + (d>>16);
392 *bp++ = (d>>8) & 255;
397 *bp++ = 192 + (d>>24);
398 *bp++ = (d>>16) & 255;
399 *bp++ = (d>>8) & 255;
405 void encode_key_write (char *k, struct encode_info *i, FILE *outf)
410 while ((*bp++ = *k++))
412 memcpy (&key, k+1, sizeof(struct it_key));
413 bp = encode_key_int ( (key.sysno - i->sysno) * 2 + *k, bp);
414 if (i->sysno != key.sysno)
416 i->sysno = key.sysno;
419 bp = encode_key_int (key.seqno - i->seqno, bp);
420 i->seqno = key.seqno;
421 if (fwrite (i->buf, bp - i->buf, 1, outf) != 1)
423 logf (LOG_FATAL|LOG_ERRNO, "fwrite");
431 static int key_y_len;
433 static int key_y_compare (const void *p1, const void *p2)
437 if ((r = key_compare (*(char**) p1 + key_y_len + 1,
438 *(char**) p2 + key_y_len + 1)))
440 return *(*(char**) p1 + key_y_len) - *(*(char**) p2 + key_y_len);
443 static int key_x_compare (const void *p1, const void *p2)
445 return strcmp (*(char**) p1, *(char**) p2);
449 void key_flush (void)
454 struct encode_info encode_info;
463 logf (LOG_LOG, "sorting section %d", key_file_no);
465 qsort (key_buf + ptr_top-ptr_i, ptr_i, sizeof(char*), key_qsort_compare);
466 getFnameTmp (out_fname, key_file_no);
468 if (!(outf = fopen (out_fname, "w")))
470 logf (LOG_FATAL|LOG_ERRNO, "fopen %s", out_fname);
473 logf (LOG_LOG, "writing section %d", key_file_no);
474 prevcp = cp = key_buf[ptr_top-ptr_i];
476 encode_key_init (&encode_info);
477 encode_key_write (cp, &encode_info, outf);
480 cp = key_buf[ptr_top-ptr_i];
481 if (strcmp (cp, prevcp))
483 encode_key_init (&encode_info);
484 encode_key_write (cp, &encode_info, outf);
488 encode_key_write (cp + strlen(cp), &encode_info, outf);
491 qsort (key_buf + ptr_top-ptr_i, ptr_i, sizeof(char*), key_x_compare);
492 getFnameTmp (out_fname, key_file_no);
494 if (!(outf = fopen (out_fname, "w")))
496 logf (LOG_FATAL|LOG_ERRNO, "fopen %s", out_fname);
499 logf (LOG_LOG, "writing section %d", key_file_no);
501 prevcp = key_buf[ptr_top-i];
503 if (!--i || strcmp (prevcp, key_buf[ptr_top-i]))
505 key_y_len = strlen(prevcp)+1;
507 logf (LOG_LOG, "key_y_len: %2d %02x %02x %s",
508 key_y_len, prevcp[0], prevcp[1], 2+prevcp);
510 qsort (key_buf + ptr_top-ptr_i, ptr_i - i,
511 sizeof(char*), key_y_compare);
512 cp = key_buf[ptr_top-ptr_i];
514 encode_key_init (&encode_info);
515 encode_key_write (cp, &encode_info, outf);
518 cp = key_buf[ptr_top-ptr_i];
519 encode_key_write (cp+key_y_len, &encode_info, outf);
523 prevcp = key_buf[ptr_top-ptr_i];
528 logf (LOG_FATAL|LOG_ERRNO, "fclose %s", out_fname);
531 logf (LOG_LOG, "finished section %d", key_file_no);
541 zebTargetInfo_close (zti, 1);
543 rec_close (&records);
544 dict_close (matchDict);
545 sortIdx_close (sortIdx);
551 static void wordInit (struct recExtractCtrl *p, RecWord *w)
553 w->zebra_maps = p->zebra_maps;
559 static struct sortKey {
564 struct sortKey *next;
567 static struct recKeys {
576 static void addIndexString (RecWord *p, const char *string, int length)
584 if (reckeys.buf_used+1024 > reckeys.buf_max)
588 b = xmalloc (reckeys.buf_max += 128000);
589 if (reckeys.buf_used > 0)
590 memcpy (b, reckeys.buf, reckeys.buf_used);
594 dst = reckeys.buf + reckeys.buf_used;
596 attrSet = p->attrSet;
597 if (reckeys.buf_used > 0 && reckeys.prevAttrSet == attrSet)
600 reckeys.prevAttrSet = attrSet;
601 attrUse = p->attrUse;
602 if (reckeys.buf_used > 0 && reckeys.prevAttrUse == attrUse)
605 reckeys.prevAttrUse = attrUse;
607 diff = 1 + p->seqno - reckeys.prevSeqNo;
608 if (diff >= 1 && diff <= 15)
613 reckeys.prevSeqNo = p->seqno;
619 memcpy (dst, &attrSet, sizeof(attrSet));
620 dst += sizeof(attrSet);
624 memcpy (dst, &attrUse, sizeof(attrUse));
625 dst += sizeof(attrUse);
627 *dst++ = p->reg_type;
628 memcpy (dst, string, length);
634 memcpy (dst, &p->seqno, sizeof(p->seqno));
635 dst += sizeof(p->seqno);
637 reckeys.buf_used = dst - reckeys.buf;
641 static void addSortString (RecWord *p, const char *string, int length)
645 for (sk = sortKeys; sk; sk = sk->next)
646 if (sk->attrSet == p->attrSet && sk->attrUse == p->attrUse)
649 sk = xmalloc (sizeof(*sk));
653 sk->string = xmalloc (p->length);
654 sk->length = p->length;
655 memcpy (sk->string, p->string, p->length);
656 sk->attrSet = p->attrSet;
657 sk->attrUse = p->attrUse;
660 static void addString (RecWord *p, const char *string, int length)
662 if (zebra_maps_is_sort (p->zebra_maps, p->reg_type))
663 addSortString (p, string, length);
665 addIndexString (p, string, length);
668 static void addIncompleteField (RecWord *p)
670 const char *b = p->string;
671 int remain = p->length;
672 const char **map = 0;
675 map = zebra_maps_input(p->zebra_maps, p->reg_type, &b, remain);
679 char buf[IT_MAX_WORD+1];
683 while (map && *map && **map == *CHR_SPACE)
685 remain = p->length - (b - p->string);
687 map = zebra_maps_input(p->zebra_maps, p->reg_type, &b, remain);
694 while (map && *map && **map != *CHR_SPACE)
696 const char *cp = *map;
698 while (i < IT_MAX_WORD && *cp)
700 remain = p->length - (b - p->string);
702 map = zebra_maps_input(p->zebra_maps, p->reg_type, &b, remain);
708 addString (p, buf, i);
712 static void addCompleteField (RecWord *p)
714 const char *b = p->string;
715 char buf[IT_MAX_WORD+1];
716 const char **map = 0;
717 int i = 0, remain = p->length;
720 map = zebra_maps_input (p->zebra_maps, p->reg_type, &b, remain);
722 while (remain > 0 && i < IT_MAX_WORD)
724 while (map && *map && **map == *CHR_SPACE)
726 remain = p->length - (b - p->string);
728 map = zebra_maps_input(p->zebra_maps, p->reg_type, &b, remain);
735 if (i && i < IT_MAX_WORD)
736 buf[i++] = *CHR_SPACE;
737 while (map && *map && **map != *CHR_SPACE)
739 const char *cp = *map;
741 if (i >= IT_MAX_WORD)
743 while (i < IT_MAX_WORD && *cp)
745 remain = p->length - (b - p->string);
747 map = zebra_maps_input (p->zebra_maps, p->reg_type, &b,
755 addString (p, buf, i);
758 static void addRecordKey (RecWord *p)
760 if (zebra_maps_is_complete (p->zebra_maps, p->reg_type))
761 addCompleteField (p);
763 addIncompleteField(p);
766 static void flushSortKeys (SYSNO sysno, int cmd)
768 struct sortKey *sk = sortKeys;
770 sortIdx_sysno (sortIdx, sysno);
773 struct sortKey *sk_next = sk->next;
774 sortIdx_type (sortIdx, sk->attrUse);
775 sortIdx_add (sortIdx, sk->string, sk->length);
783 static void flushRecordKeys (SYSNO sysno, int cmd, struct recKeys *reckeys,
784 const char *databaseName)
791 if (zebTargetInfo_curDatabase (zti, databaseName))
793 if (zebTargetInfo_newDatabase (zti, databaseName))
796 while (off < reckeys->buf_used)
798 const char *src = reckeys->buf + off;
806 memcpy (&attrSet, src, sizeof(attrSet));
807 src += sizeof(attrSet);
811 memcpy (&attrUse, src, sizeof(attrUse));
812 src += sizeof(attrUse);
814 if (key_buf_used + 1024 > (ptr_top-ptr_i)*sizeof(char*))
817 key_buf[ptr_top-ptr_i] = (char*)key_buf + key_buf_used;
819 ch = zebTargetInfo_lookupSU (zti, attrSet, attrUse);
821 ch = zebTargetInfo_addSU (zti, attrSet, attrUse);
823 ((char*) key_buf) [key_buf_used++] = ch;
825 ((char*)key_buf) [key_buf_used++] = *src++;
827 ((char*)key_buf) [key_buf_used++] = '\0';
828 ((char*) key_buf)[key_buf_used++] = cmd;
831 seqno += ((lead>>2) & 15)-1;
834 memcpy (&seqno, src, sizeof(seqno));
835 src += sizeof(seqno);
839 memcpy ((char*)key_buf + key_buf_used, &key, sizeof(key));
840 key_buf_used += sizeof(key);
841 off = src - reckeys->buf;
843 assert (off == reckeys->buf_used);
846 static const char **searchRecordKey (struct recKeys *reckeys,
847 int attrSetS, int attrUseS)
849 static const char *ws[32];
857 for (i = 0; i<32; i++)
860 while (off < reckeys->buf_used)
863 const char *src = reckeys->buf + off;
871 memcpy (&attrSet, src, sizeof(attrSet));
872 src += sizeof(attrSet);
876 memcpy (&attrUse, src, sizeof(attrUse));
877 src += sizeof(attrUse);
883 seqno += ((lead>>2) & 15)-1;
886 memcpy (&seqno, src, sizeof(seqno));
887 src += sizeof(seqno);
889 if (attrUseS == attrUse && attrSetS == attrSet)
896 woff = seqno - startSeq;
897 if (woff >= 0 && woff < 31)
901 off = src - reckeys->buf;
903 assert (off == reckeys->buf_used);
907 struct file_read_info {
915 static struct file_read_info *file_read_start (int fd)
917 struct file_read_info *fi = xmalloc (sizeof(*fi));
921 fi->file_moffset = 0;
925 static void file_read_stop (struct file_read_info *fi)
931 static off_t file_seek (void *handle, off_t offset)
933 struct file_read_info *p = handle;
934 p->file_offset = offset;
935 return lseek (p->fd, offset, SEEK_SET);
938 static off_t file_tell (void *handle)
940 struct file_read_info *p = handle;
941 return p->file_offset;
944 static int file_read (void *handle, char *buf, size_t count)
946 struct file_read_info *p = handle;
949 r = read (fd, buf, count);
953 if (p->file_offset > p->file_max)
954 p->file_max = p->file_offset;
959 static void file_begin (void *handle)
961 struct file_read_info *p = handle;
963 p->file_offset = p->file_moffset;
965 lseek (p->fd, p->file_moffset, SEEK_SET);
969 static void file_end (void *handle, off_t offset)
971 struct file_read_info *p = handle;
973 assert (p->file_more == 0);
975 p->file_moffset = offset;
978 static int atois (const char **s)
981 while ( (c=**s) >= '0' && c <= '9')
983 val = val*10 + c - '0';
989 static char *fileMatchStr (struct recKeys *reckeys, struct recordGroup *rGroup,
993 static char dstBuf[2048];
995 const char *s = spec;
996 static const char **w;
1001 while (*s == ' ' || *s == '\t')
1008 int attrSet, attrUse;
1012 attrSet = atois (&s);
1015 logf (LOG_WARN, "Missing , in match criteria %s in group %s",
1016 spec, rGroup->groupName ? rGroup->groupName : "none");
1020 attrUse = atois (&s);
1021 w = searchRecordKey (reckeys, attrSet, attrUse);
1026 for (i = 0; i<32; i++)
1031 logf (LOG_WARN, "Missing ) in match criteria %s in group %s",
1032 spec, rGroup->groupName ? rGroup->groupName : "none");
1037 for (i = 0; i<32; i++)
1038 if (matchFlag[i] && w[i])
1046 dst += strlen(w[i]);
1050 logf (LOG_WARN, "Record didn't contain match"
1051 " fields in (%d,%d)", attrSet, attrUse);
1059 const char *spec_src = NULL;
1060 const char *s1 = ++s;
1061 while (*s1 && *s1 != ' ' && *s1 != '\t')
1067 memcpy (special, s, spec_len);
1068 special[spec_len] = '\0';
1071 if (!strcmp (special, "group"))
1072 spec_src = rGroup->groupName;
1073 else if (!strcmp (special, "database"))
1074 spec_src = rGroup->databaseName;
1075 else if (!strcmp (special, "filename"))
1077 else if (!strcmp (special, "type"))
1078 spec_src = rGroup->recordType;
1083 strcpy (dst, spec_src);
1084 dst += strlen (spec_src);
1087 else if (*s == '\"' || *s == '\'')
1089 int stopMarker = *s++;
1093 while (*s && *s != stopMarker)
1096 tmpString[i++] = *s++;
1100 tmpString[i] = '\0';
1101 strcpy (dst, tmpString);
1102 dst += strlen (tmpString);
1106 logf (LOG_WARN, "Syntax error in match criteria %s in group %s",
1107 spec, rGroup->groupName ? rGroup->groupName : "none");
1114 logf (LOG_WARN, "No match criteria for record %s in group %s",
1115 fname, rGroup->groupName ? rGroup->groupName : "none");
1122 struct recordLogInfo {
1125 struct recordGroup *rGroup;
1128 static void recordLogPreamble (int level, const char *msg, void *info)
1130 struct recordLogInfo *p = info;
1131 FILE *outf = log_file ();
1133 if (level & LOG_LOG)
1135 fprintf (outf, "File %s, offset %d, type %s\n",
1136 p->rGroup->recordType, p->recordOffset, p->fname);
1137 log_event_start (NULL, NULL);
1140 static int recordExtract (SYSNO *sysno, const char *fname,
1141 struct recordGroup *rGroup, int deleteFlag,
1142 struct file_read_info *fi, RecType recType,
1145 struct recExtractCtrl extractCtrl;
1149 off_t recordOffset = 0;
1151 struct recordLogInfo logInfo;
1155 /* we are going to read from a file, so prepare the extraction */
1156 extractCtrl.fh = fi;
1157 extractCtrl.subType = subType;
1158 extractCtrl.init = wordInit;
1159 extractCtrl.add = addRecordKey;
1160 extractCtrl.dh = rGroup->dh;
1162 reckeys.buf_used = 0;
1163 reckeys.prevAttrUse = -1;
1164 reckeys.prevAttrSet = -1;
1165 reckeys.prevSeqNo = 0;
1167 recordOffset = fi->file_moffset;
1168 extractCtrl.offset = recordOffset;
1169 extractCtrl.readf = file_read;
1170 extractCtrl.seekf = file_seek;
1171 extractCtrl.tellf = file_tell;
1172 extractCtrl.endf = file_end;
1173 extractCtrl.zebra_maps = rGroup->zebra_maps;
1174 extractCtrl.flagShowRecords = !rGroup->flagRw;
1175 if (!rGroup->flagRw)
1176 printf ("File: %s %ld\n", fname, (long) recordOffset);
1178 logInfo.fname = fname;
1179 logInfo.recordOffset = recordOffset;
1180 logInfo.rGroup = rGroup;
1181 log_event_start (recordLogPreamble, &logInfo);
1183 r = (*recType->extract)(&extractCtrl);
1185 log_event_start (NULL, NULL);
1189 /* error occured during extraction ... */
1190 if (rGroup->flagRw &&
1191 records_processed < rGroup->fileVerboseLimit)
1193 logf (LOG_WARN, "fail %s %s %ld code = %d", rGroup->recordType,
1194 fname, (long) recordOffset, r);
1198 if (reckeys.buf_used == 0)
1200 /* the extraction process returned no information - the record
1201 is probably empty - unless flagShowRecords is in use */
1202 if (!rGroup->flagRw)
1204 logf (LOG_WARN, "No keys generated for file %s", fname);
1205 logf (LOG_WARN, " The file is probably empty");
1210 /* perform match if sysno not known and if match criteria is specified */
1217 if (rGroup->recordId && *rGroup->recordId)
1221 matchStr = fileMatchStr (&reckeys, rGroup, fname,
1225 rinfo = dict_lookup (matchDict, matchStr);
1227 memcpy (sysno, rinfo+1, sizeof(*sysno));
1231 logf (LOG_WARN, "Bad match criteria");
1242 logf (LOG_LOG, "delete %s %s %ld", rGroup->recordType,
1243 fname, (long) recordOffset);
1244 logf (LOG_WARN, "cannot delete record above (seems new)");
1247 if (records_processed < rGroup->fileVerboseLimit)
1248 logf (LOG_LOG, "add %s %s %ld", rGroup->recordType,
1249 fname, (long) recordOffset);
1250 rec = rec_new (records);
1251 *sysno = rec->sysno;
1255 dict_insert (matchDict, matchStr, sizeof(*sysno), sysno);
1257 flushRecordKeys (*sysno, 1, &reckeys, rGroup->databaseName);
1258 flushSortKeys (*sysno, 1);
1264 /* record already exists */
1265 struct recKeys delkeys;
1267 rec = rec_get (records, *sysno);
1269 delkeys.buf_used = rec->size[recInfo_delKeys];
1270 delkeys.buf = rec->info[recInfo_delKeys];
1271 flushSortKeys (*sysno, 0);
1272 flushRecordKeys (*sysno, 0, &delkeys, rec->info[recInfo_databaseName]);
1275 /* record going to be deleted */
1276 if (!delkeys.buf_used)
1278 logf (LOG_LOG, "delete %s %s %ld", rGroup->recordType,
1279 fname, (long) recordOffset);
1280 logf (LOG_WARN, "cannot delete file above, storeKeys false");
1284 if (records_processed < rGroup->fileVerboseLimit)
1285 logf (LOG_LOG, "delete %s %s %ld", rGroup->recordType,
1286 fname, (long) recordOffset);
1289 dict_delete (matchDict, matchStr);
1290 rec_del (records, &rec);
1297 /* record going to be updated */
1298 if (!delkeys.buf_used)
1300 logf (LOG_LOG, "update %s %s %ld", rGroup->recordType,
1301 fname, (long) recordOffset);
1302 logf (LOG_WARN, "cannot update file above, storeKeys false");
1306 if (records_processed < rGroup->fileVerboseLimit)
1307 logf (LOG_LOG, "update %s %s %ld", rGroup->recordType,
1308 fname, (long) recordOffset);
1309 flushRecordKeys (*sysno, 1, &reckeys, rGroup->databaseName);
1314 /* update file type */
1315 xfree (rec->info[recInfo_fileType]);
1316 rec->info[recInfo_fileType] =
1317 rec_strdup (rGroup->recordType, &rec->size[recInfo_fileType]);
1319 /* update filename */
1320 xfree (rec->info[recInfo_filename]);
1321 rec->info[recInfo_filename] =
1322 rec_strdup (fname, &rec->size[recInfo_filename]);
1324 /* update delete keys */
1325 xfree (rec->info[recInfo_delKeys]);
1326 if (reckeys.buf_used > 0 && rGroup->flagStoreKeys == 1)
1329 rec->size[recInfo_delKeys] = reckeys.buf_used;
1330 rec->info[recInfo_delKeys] = reckeys.buf;
1332 reckeys.buf_max = 0;
1334 rec->info[recInfo_delKeys] = xmalloc (reckeys.buf_used);
1335 rec->size[recInfo_delKeys] = reckeys.buf_used;
1336 memcpy (rec->info[recInfo_delKeys], reckeys.buf,
1337 rec->size[recInfo_delKeys]);
1342 rec->info[recInfo_delKeys] = NULL;
1343 rec->size[recInfo_delKeys] = 0;
1346 /* update store data */
1347 xfree (rec->info[recInfo_storeData]);
1348 if (rGroup->flagStoreData == 1)
1350 int size = fi->file_moffset - recordOffset;
1352 size = fi->file_max - recordOffset;
1353 rec->size[recInfo_storeData] = size;
1354 rec->info[recInfo_storeData] = xmalloc (size);
1355 if (lseek (fi->fd, recordOffset, SEEK_SET) < 0)
1357 logf (LOG_ERRNO|LOG_FATAL, "seek to %ld in %s", fname,
1358 (long) recordOffset);
1361 if (read (fi->fd, rec->info[recInfo_storeData], size) < size)
1363 logf (LOG_ERRNO|LOG_FATAL, "read %d bytes of %s",
1364 fi->file_max, fname);
1370 rec->info[recInfo_storeData] = NULL;
1371 rec->size[recInfo_storeData] = 0;
1373 /* update database name */
1374 xfree (rec->info[recInfo_databaseName]);
1375 rec->info[recInfo_databaseName] =
1376 rec_strdup (rGroup->databaseName, &rec->size[recInfo_databaseName]);
1379 xfree (rec->info[recInfo_offset]);
1381 rec->size[recInfo_offset] = sizeof(recordOffset);
1382 rec->info[recInfo_offset] = xmalloc (sizeof(recordOffset));
1383 memcpy (rec->info[recInfo_offset], &recordOffset, sizeof(recordOffset));
1385 /* commit this record */
1386 rec_put (records, &rec);
1391 int fileExtract (SYSNO *sysno, const char *fname,
1392 const struct recordGroup *rGroupP, int deleteFlag)
1400 struct recordGroup rGroupM;
1401 struct recordGroup *rGroup = &rGroupM;
1402 struct file_read_info *fi;
1404 memcpy (rGroup, rGroupP, sizeof(*rGroupP));
1406 if (!rGroup->groupName || !*rGroup->groupName)
1409 sprintf (gprefix, "%s.", rGroup->groupName);
1411 logf (LOG_DEBUG, "fileExtract %s", fname);
1413 /* determine file extension */
1414 for (i = strlen(fname); --i >= 0; )
1415 if (fname[i] == '/')
1420 else if (fname[i] == '.')
1422 strcpy (ext, fname+i+1);
1425 /* determine file type - depending on extension */
1426 if (!rGroup->recordType)
1428 sprintf (ext_res, "%srecordType.%s", gprefix, ext);
1429 if (!(rGroup->recordType = res_get (common_resource, ext_res)))
1431 sprintf (ext_res, "%srecordType", gprefix);
1432 if (!(rGroup->recordType = res_get (common_resource, ext_res)))
1434 if (records_processed < rGroup->fileVerboseLimit)
1435 logf (LOG_LOG, "? %s", fname);
1440 if (!rGroup->recordType)
1442 if (records_processed < rGroup->fileVerboseLimit)
1443 logf (LOG_LOG, "? record %s", fname);
1446 if (!(recType = recType_byName (rGroup->recordType, subType)))
1448 logf (LOG_WARN, "No such record type: %s", rGroup->recordType);
1452 /* determine match criteria */
1453 if (!rGroup->recordId)
1455 sprintf (ext_res, "%srecordId.%s", gprefix, ext);
1456 rGroup->recordId = res_get (common_resource, ext_res);
1459 /* determine database name */
1460 if (!rGroup->databaseName)
1462 sprintf (ext_res, "%sdatabase.%s", gprefix, ext);
1463 if (!(rGroup->databaseName = res_get (common_resource, ext_res)))
1465 sprintf (ext_res, "%sdatabase", gprefix);
1466 rGroup->databaseName = res_get (common_resource, ext_res);
1469 if (!rGroup->databaseName)
1470 rGroup->databaseName = "Default";
1472 if (rGroup->flagStoreData == -1)
1475 sprintf (ext_res, "%sstoreData.%s", gprefix, ext);
1476 if (!(sval = res_get (common_resource, ext_res)))
1478 sprintf (ext_res, "%sstoreData", gprefix);
1479 sval = res_get (common_resource, ext_res);
1482 rGroup->flagStoreData = atoi (sval);
1484 if (rGroup->flagStoreData == -1)
1485 rGroup->flagStoreData = 0;
1487 if (rGroup->flagStoreKeys == -1)
1491 sprintf (ext_res, "%sstoreKeys.%s", gprefix, ext);
1492 if (!(sval = res_get (common_resource, ext_res)))
1494 sprintf (ext_res, "%sstoreKeys", gprefix);
1495 sval = res_get (common_resource, ext_res);
1498 rGroup->flagStoreKeys = atoi (sval);
1500 if (rGroup->flagStoreKeys == -1)
1501 rGroup->flagStoreKeys = 0;
1503 if (sysno && deleteFlag)
1507 if ((fd = open (fname, O_BINARY|O_RDONLY)) == -1)
1509 logf (LOG_WARN|LOG_ERRNO, "open %s", fname);
1513 fi = file_read_start (fd);
1517 r = recordExtract (sysno, fname, rGroup, deleteFlag, fi,
1519 } while (r && !sysno && fi->file_more);
1520 file_read_stop (fi);