2 * Copyright (C) 1994-1998, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.79 1998-02-17 10:32:52 adam
8 * Fixed bug: binary files weren't opened with flag b on NT.
10 * Revision 1.78 1998/02/10 12:03:05 adam
13 * Revision 1.77 1998/01/12 15:04:08 adam
14 * The test option (-s) only uses read-lock (and not write lock).
16 * Revision 1.76 1997/10/27 14:33:04 adam
17 * Moved towards generic character mapping depending on "structure"
18 * field in abstract syntax file. Fixed a few memory leaks. Fixed
19 * bug with negative integers when doing searches with relational
22 * Revision 1.75 1997/09/17 12:19:12 adam
23 * Zebra version corresponds to YAZ version 1.4.
24 * Changed Zebra server so that it doesn't depend on global common_resource.
26 * Revision 1.74 1997/09/09 13:38:06 adam
27 * Partial port to WIN95/NT.
29 * Revision 1.73 1997/09/04 13:57:20 adam
30 * New file extract/retrieve method tellf (added).
31 * Added O_BINARY for open calls.
33 * Revision 1.72 1997/07/15 16:32:29 adam
34 * Bug fix: Match handler didn't terminate the resulting string!
36 * Revision 1.71 1997/07/15 16:28:41 adam
37 * Bug fix: storeData didn't work with files with multiple records.
38 * Bug fix: fixed memory management with records; not really well
41 * Revision 1.70 1997/07/01 13:00:42 adam
42 * Bug fix in routine searchRecordKey: uninitialized variables.
44 * Revision 1.69 1997/04/29 09:26:03 adam
45 * Bug fix: generic recordId handling didn't work for compressed internal
48 * Revision 1.68 1997/02/12 20:39:45 adam
49 * Implemented options -f <n> that limits the log to the first <n>
51 * Changed some log messages also.
53 * Revision 1.67 1996/11/15 15:02:14 adam
54 * Minor changes regarding logging.
56 * Revision 1.66 1996/11/14 09:52:21 adam
57 * Strings in record keys bound by IT_MAX_WORD.
59 * Revision 1.65 1996/11/14 08:57:56 adam
60 * Reduction of storeKeys area.
62 * Revision 1.64 1996/11/08 11:10:16 adam
63 * Buffers used during file match got bigger.
64 * Compressed ISAM support everywhere.
65 * Bug fixes regarding masking characters in queries.
66 * Redesigned Regexp-2 queries.
68 * Revision 1.63 1996/10/29 14:09:39 adam
69 * Use of cisam system - enabled if setting isamc is 1.
71 * Revision 1.62 1996/10/11 10:57:01 adam
72 * New module recctrl. Used to manage records (extract/retrieval).
73 * Several files have been moved to the recctrl sub directory.
75 * Revision 1.61 1996/06/06 12:08:37 quinn
76 * Added showRecord function
78 * Revision 1.60 1996/06/04 10:18:12 adam
79 * Search/scan uses character mapping module.
81 * Revision 1.59 1996/05/14 15:47:07 adam
82 * Cleanup of various buffer size entities.
84 * Revision 1.58 1996/05/14 06:16:38 adam
85 * Compact use/set bytes used in search service.
87 * Revision 1.57 1996/05/13 14:23:04 adam
88 * Work on compaction of set/use bytes in dictionary.
90 * Revision 1.56 1996/05/09 09:54:42 adam
91 * Server supports maps from one logical attributes to a list of physical
93 * The extraction process doesn't make space consuming 'any' keys.
95 * Revision 1.55 1996/05/09 07:28:55 quinn
96 * Work towards phrases and multiple registers
98 * Revision 1.54 1996/05/01 13:46:35 adam
99 * First work on multiple records in one file.
100 * New option, -offset, to the "unread" command in the filter module.
102 * Revision 1.53 1996/04/26 12:09:43 adam
103 * Added a few comments.
105 * Revision 1.52 1996/04/25 13:27:57 adam
106 * Function recordExtract modified so that files with no keys (possibly empty)
109 * Revision 1.51 1996/03/19 11:08:42 adam
110 * Bug fix: Log preamble wasn't always turned off after recordExtract.
112 * Revision 1.50 1996/02/12 18:45:36 adam
113 * New fileVerboseFlag in record group control.
115 * Revision 1.49 1996/02/05 12:29:57 adam
116 * Logging reduced a bit.
117 * The remaining running time is estimated during register merge.
119 * Revision 1.48 1996/02/01 20:53:26 adam
120 * The temporary per-record keys are compacted a little, and duplication
121 * of the per-records keys are avoided when they are saved in the record
122 * information buffer.
124 * Revision 1.47 1996/01/17 14:57:48 adam
125 * Prototype changed for reader functions in extract/retrieve. File
126 * is identified by 'void *' instead of 'int.
128 * Revision 1.46 1995/12/15 14:57:16 adam
131 * Revision 1.45 1995/12/15 12:37:41 adam
132 * In addRecordKeyAny: Writes key only when attrSet != -1.
134 * Revision 1.44 1995/12/12 16:00:54 adam
135 * System call sync(2) used after update/commit.
136 * Locking (based on fcntl) uses F_EXLCK and F_SHLCK instead of F_WRLCK
139 * Revision 1.43 1995/12/11 09:12:46 adam
140 * The rec_get function returns NULL if record doesn't exist - will
141 * happen in the server if the result set records have been deleted since
142 * the creation of the set (i.e. the search).
143 * The server saves a result temporarily if it is 'volatile', i.e. the
144 * set is register dependent.
146 * Revision 1.42 1995/12/07 17:38:46 adam
147 * Work locking mechanisms for concurrent updates/commit.
149 * Revision 1.41 1995/12/06 16:06:42 adam
150 * Better diagnostics. Work on 'real' dictionary deletion.
152 * Revision 1.40 1995/12/05 16:57:40 adam
153 * More work on regular patterns.
155 * Revision 1.39 1995/12/05 13:20:18 adam
156 * Bug fix: file_read sometimes returned early EOF.
158 * Revision 1.38 1995/12/04 17:59:21 adam
159 * More work on regular expression conversion.
161 * Revision 1.37 1995/12/04 14:22:27 adam
162 * Extra arg to recType_byName.
163 * Started work on new regular expression parsed input to
164 * structured records.
166 * Revision 1.36 1995/11/30 08:34:29 adam
167 * Started work on commit facility.
168 * Changed a few malloc/free to xmalloc/xfree.
170 * Revision 1.35 1995/11/28 14:26:21 adam
171 * Bug fix: recordId with constant wasn't right.
172 * Bug fix: recordId dictionary entry wasn't deleted when needed.
174 * Revision 1.34 1995/11/28 09:09:38 adam
175 * Zebra config renamed.
176 * Use setting 'recordId' to identify record now.
177 * Bug fix in recindex.c: rec_release_blocks was invokeded even
178 * though the blocks were already released.
179 * File traversal properly deletes records when needed.
181 * Revision 1.33 1995/11/27 09:56:20 adam
182 * Record info elements better enumerated. Internal store of records.
184 * Revision 1.32 1995/11/25 10:24:05 adam
185 * More record fields - they are enumerated now.
186 * New options: flagStoreData flagStoreKey.
188 * Revision 1.31 1995/11/24 11:31:35 adam
189 * Commands add & del read filenames from stdin if source directory is
191 * Match criteria supports 'constant' strings.
193 * Revision 1.30 1995/11/22 17:19:16 adam
194 * Record management uses the bfile system.
196 * Revision 1.29 1995/11/21 15:01:14 adam
197 * New general match criteria implemented.
198 * New feature: document groups.
200 * Revision 1.28 1995/11/21 09:20:30 adam
201 * Yet more work on record match.
203 * Revision 1.27 1995/11/20 16:59:45 adam
204 * New update method: the 'old' keys are saved for each records.
206 * Revision 1.26 1995/11/20 11:56:24 adam
207 * Work on new traversal.
209 * Revision 1.25 1995/11/16 15:34:54 adam
210 * Uses new record management system in both indexer and server.
212 * Revision 1.24 1995/11/15 19:13:08 adam
213 * Work on record management.
215 * Revision 1.23 1995/10/27 14:00:10 adam
216 * Implemented detection of database availability.
218 * Revision 1.22 1995/10/17 18:02:07 adam
219 * New feature: databases. Implemented as prefix to words in dictionary.
221 * Revision 1.21 1995/10/10 12:24:38 adam
222 * Temporary sort files are compressed.
224 * Revision 1.20 1995/10/06 13:52:05 adam
225 * Bug fixes. Handler may abort further scanning.
227 * Revision 1.19 1995/10/04 12:55:16 adam
228 * Bug fix in ranked search. Use=Any keys inserted.
230 * Revision 1.18 1995/10/04 09:37:08 quinn
233 * Revision 1.17 1995/10/03 14:28:57 adam
234 * Buffered read in extract works.
236 * Revision 1.16 1995/10/03 14:28:45 adam
237 * Work on more effecient read handler in extract.
239 * Revision 1.15 1995/10/02 15:42:53 adam
240 * Extract uses file descriptors instead of FILE pointers.
242 * Revision 1.14 1995/10/02 15:29:13 adam
243 * More logging in file_extract.
245 * Revision 1.13 1995/09/29 14:01:39 adam
248 * Revision 1.12 1995/09/28 14:22:56 adam
249 * Sort uses smaller temporary files.
251 * Revision 1.11 1995/09/28 12:10:31 adam
252 * Bug fixes. Field prefix used in queries.
254 * Revision 1.10 1995/09/28 09:19:41 adam
255 * xfree/xmalloc used everywhere.
256 * Extract/retrieve method seems to work for text records.
258 * Revision 1.9 1995/09/27 12:22:28 adam
259 * More work on extract in record control.
260 * Field name is not in isam keys but in prefix in dictionary words.
262 * Revision 1.8 1995/09/14 07:48:22 adam
263 * Record control management.
265 * Revision 1.7 1995/09/11 13:09:32 adam
266 * More work on relevance feedback.
268 * Revision 1.6 1995/09/08 14:52:27 adam
269 * Minor changes. Dictionary is lower case now.
271 * Revision 1.5 1995/09/06 16:11:16 adam
272 * Option: only one word key per file.
274 * Revision 1.4 1995/09/05 15:28:39 adam
275 * More work on search engine.
277 * Revision 1.3 1995/09/04 12:33:41 adam
278 * Various cleanup. YAZ util used instead.
280 * Revision 1.2 1995/09/04 09:10:34 adam
281 * More work on index add/del/update.
282 * Merge sort implemented.
283 * Initial work on z39 server.
285 * Revision 1.1 1995/09/01 14:06:35 adam
286 * Split of work into more files.
305 static Dict matchDict;
307 static Records records = NULL;
308 static SortIdx sortIdx = NULL;
310 static char **key_buf;
311 static size_t ptr_top;
313 static size_t key_buf_used;
314 static int key_file_no;
316 static int records_inserted = 0;
317 static int records_updated = 0;
318 static int records_deleted = 0;
319 static int records_processed = 0;
321 static ZebTargetInfo *zti = NULL;
323 static void logRecord (int showFlag)
327 if (showFlag || !(records_processed % 1000))
329 logf (LOG_LOG, "Records: %7d i/u/d %d/%d/%d",
330 records_processed, records_inserted, records_updated,
335 int key_open (BFiles bfs, int mem, int rw)
338 mem = atoi(res_get_def (common_resource, "memMax", "4"))*1024*1024;
341 key_buf = xmalloc (mem);
342 ptr_top = mem/sizeof(char*);
348 if (!(matchDict = dict_open (bfs, GMATCH_DICT, 50, rw)))
350 logf (LOG_FATAL, "dict_open fail of %s", GMATCH_DICT);
354 records = rec_open (bfs, rw);
357 dict_close (matchDict);
360 zti = zebTargetInfo_open (records, rw);
363 rec_close (&records);
364 dict_close (matchDict);
367 sortIdx = sortIdx_open (bfs, 1);
377 void encode_key_init (struct encode_info *i)
383 char *encode_key_int (int d, char *bp)
392 else if (d <= 4194303)
394 *bp++ = 128 + (d>>16);
395 *bp++ = (d>>8) & 255;
400 *bp++ = 192 + (d>>24);
401 *bp++ = (d>>16) & 255;
402 *bp++ = (d>>8) & 255;
408 void encode_key_write (char *k, struct encode_info *i, FILE *outf)
413 while ((*bp++ = *k++))
415 memcpy (&key, k+1, sizeof(struct it_key));
416 bp = encode_key_int ( (key.sysno - i->sysno) * 2 + *k, bp);
417 if (i->sysno != key.sysno)
419 i->sysno = key.sysno;
422 bp = encode_key_int (key.seqno - i->seqno, bp);
423 i->seqno = key.seqno;
424 if (fwrite (i->buf, bp - i->buf, 1, outf) != 1)
426 logf (LOG_FATAL|LOG_ERRNO, "fwrite");
434 static int key_y_len;
436 static int key_y_compare (const void *p1, const void *p2)
440 if ((r = key_compare (*(char**) p1 + key_y_len + 1,
441 *(char**) p2 + key_y_len + 1)))
443 return *(*(char**) p1 + key_y_len) - *(*(char**) p2 + key_y_len);
446 static int key_x_compare (const void *p1, const void *p2)
448 return strcmp (*(char**) p1, *(char**) p2);
452 void key_flush (void)
457 struct encode_info encode_info;
466 logf (LOG_LOG, "sorting section %d", key_file_no);
468 qsort (key_buf + ptr_top-ptr_i, ptr_i, sizeof(char*), key_qsort_compare);
469 getFnameTmp (out_fname, key_file_no);
471 if (!(outf = fopen (out_fname, "wb")))
473 logf (LOG_FATAL|LOG_ERRNO, "fopen %s", out_fname);
476 logf (LOG_LOG, "writing section %d", key_file_no);
477 prevcp = cp = key_buf[ptr_top-ptr_i];
479 encode_key_init (&encode_info);
480 encode_key_write (cp, &encode_info, outf);
483 cp = key_buf[ptr_top-ptr_i];
484 if (strcmp (cp, prevcp))
486 encode_key_init (&encode_info);
487 encode_key_write (cp, &encode_info, outf);
491 encode_key_write (cp + strlen(cp), &encode_info, outf);
494 qsort (key_buf + ptr_top-ptr_i, ptr_i, sizeof(char*), key_x_compare);
495 getFnameTmp (out_fname, key_file_no);
497 if (!(outf = fopen (out_fname, "wb")))
499 logf (LOG_FATAL|LOG_ERRNO, "fopen %s", out_fname);
502 logf (LOG_LOG, "writing section %d", key_file_no);
504 prevcp = key_buf[ptr_top-i];
506 if (!--i || strcmp (prevcp, key_buf[ptr_top-i]))
508 key_y_len = strlen(prevcp)+1;
510 logf (LOG_LOG, "key_y_len: %2d %02x %02x %s",
511 key_y_len, prevcp[0], prevcp[1], 2+prevcp);
513 qsort (key_buf + ptr_top-ptr_i, ptr_i - i,
514 sizeof(char*), key_y_compare);
515 cp = key_buf[ptr_top-ptr_i];
517 encode_key_init (&encode_info);
518 encode_key_write (cp, &encode_info, outf);
521 cp = key_buf[ptr_top-ptr_i];
522 encode_key_write (cp+key_y_len, &encode_info, outf);
526 prevcp = key_buf[ptr_top-ptr_i];
531 logf (LOG_FATAL|LOG_ERRNO, "fclose %s", out_fname);
534 logf (LOG_LOG, "finished section %d", key_file_no);
544 zebTargetInfo_close (zti, 1);
546 rec_close (&records);
547 dict_close (matchDict);
548 sortIdx_close (sortIdx);
554 static void wordInit (struct recExtractCtrl *p, RecWord *w)
556 w->zebra_maps = p->zebra_maps;
562 static struct sortKey {
567 struct sortKey *next;
570 static struct recKeys {
579 static void addIndexString (RecWord *p, const char *string, int length)
587 if (reckeys.buf_used+1024 > reckeys.buf_max)
591 b = xmalloc (reckeys.buf_max += 128000);
592 if (reckeys.buf_used > 0)
593 memcpy (b, reckeys.buf, reckeys.buf_used);
597 dst = reckeys.buf + reckeys.buf_used;
599 attrSet = p->attrSet;
600 if (reckeys.buf_used > 0 && reckeys.prevAttrSet == attrSet)
603 reckeys.prevAttrSet = attrSet;
604 attrUse = p->attrUse;
605 if (reckeys.buf_used > 0 && reckeys.prevAttrUse == attrUse)
608 reckeys.prevAttrUse = attrUse;
610 diff = 1 + p->seqno - reckeys.prevSeqNo;
611 if (diff >= 1 && diff <= 15)
616 reckeys.prevSeqNo = p->seqno;
622 memcpy (dst, &attrSet, sizeof(attrSet));
623 dst += sizeof(attrSet);
627 memcpy (dst, &attrUse, sizeof(attrUse));
628 dst += sizeof(attrUse);
630 *dst++ = p->reg_type;
631 memcpy (dst, string, length);
637 memcpy (dst, &p->seqno, sizeof(p->seqno));
638 dst += sizeof(p->seqno);
640 reckeys.buf_used = dst - reckeys.buf;
644 static void addSortString (RecWord *p, const char *string, int length)
648 for (sk = sortKeys; sk; sk = sk->next)
649 if (sk->attrSet == p->attrSet && sk->attrUse == p->attrUse)
652 sk = xmalloc (sizeof(*sk));
656 sk->string = xmalloc (p->length);
657 sk->length = p->length;
658 memcpy (sk->string, p->string, p->length);
659 sk->attrSet = p->attrSet;
660 sk->attrUse = p->attrUse;
663 static void addString (RecWord *p, const char *string, int length)
666 if (zebra_maps_is_sort (p->zebra_maps, p->reg_type))
667 addSortString (p, string, length);
669 addIndexString (p, string, length);
672 static void addIncompleteField (RecWord *p)
674 const char *b = p->string;
675 int remain = p->length;
676 const char **map = 0;
679 map = zebra_maps_input(p->zebra_maps, p->reg_type, &b, remain);
683 char buf[IT_MAX_WORD+1];
687 while (map && *map && **map == *CHR_SPACE)
689 remain = p->length - (b - p->string);
691 map = zebra_maps_input(p->zebra_maps, p->reg_type, &b, remain);
698 while (map && *map && **map != *CHR_SPACE)
700 const char *cp = *map;
702 while (i < IT_MAX_WORD && *cp)
704 remain = p->length - (b - p->string);
706 map = zebra_maps_input(p->zebra_maps, p->reg_type, &b, remain);
712 addString (p, buf, i);
716 static void addCompleteField (RecWord *p)
718 const char *b = p->string;
719 char buf[IT_MAX_WORD+1];
720 const char **map = 0;
721 int i = 0, remain = p->length;
724 map = zebra_maps_input (p->zebra_maps, p->reg_type, &b, remain);
726 while (remain > 0 && i < IT_MAX_WORD)
728 while (map && *map && **map == *CHR_SPACE)
730 remain = p->length - (b - p->string);
732 map = zebra_maps_input(p->zebra_maps, p->reg_type, &b, remain);
739 if (i && i < IT_MAX_WORD)
740 buf[i++] = *CHR_SPACE;
741 while (map && *map && **map != *CHR_SPACE)
743 const char *cp = *map;
745 if (i >= IT_MAX_WORD)
747 while (i < IT_MAX_WORD && *cp)
749 remain = p->length - (b - p->string);
751 map = zebra_maps_input (p->zebra_maps, p->reg_type, &b,
759 addString (p, buf, i);
762 static void addRecordKey (RecWord *p)
764 if (zebra_maps_is_complete (p->zebra_maps, p->reg_type))
765 addCompleteField (p);
767 addIncompleteField(p);
770 static void flushSortKeys (SYSNO sysno, int cmd)
772 struct sortKey *sk = sortKeys;
774 sortIdx_sysno (sortIdx, sysno);
777 struct sortKey *sk_next = sk->next;
778 sortIdx_type (sortIdx, sk->attrUse);
779 sortIdx_add (sortIdx, sk->string, sk->length);
787 static void flushRecordKeys (SYSNO sysno, int cmd, struct recKeys *reckeys,
788 const char *databaseName)
795 if (zebTargetInfo_curDatabase (zti, databaseName))
797 if (zebTargetInfo_newDatabase (zti, databaseName))
800 while (off < reckeys->buf_used)
802 const char *src = reckeys->buf + off;
810 memcpy (&attrSet, src, sizeof(attrSet));
811 src += sizeof(attrSet);
815 memcpy (&attrUse, src, sizeof(attrUse));
816 src += sizeof(attrUse);
818 if (key_buf_used + 1024 > (ptr_top-ptr_i)*sizeof(char*))
821 key_buf[ptr_top-ptr_i] = (char*)key_buf + key_buf_used;
823 ch = zebTargetInfo_lookupSU (zti, attrSet, attrUse);
825 ch = zebTargetInfo_addSU (zti, attrSet, attrUse);
827 ((char*) key_buf) [key_buf_used++] = ch;
829 ((char*)key_buf) [key_buf_used++] = *src++;
831 ((char*)key_buf) [key_buf_used++] = '\0';
832 ((char*) key_buf)[key_buf_used++] = cmd;
835 seqno += ((lead>>2) & 15)-1;
838 memcpy (&seqno, src, sizeof(seqno));
839 src += sizeof(seqno);
843 memcpy ((char*)key_buf + key_buf_used, &key, sizeof(key));
844 key_buf_used += sizeof(key);
845 off = src - reckeys->buf;
847 assert (off == reckeys->buf_used);
850 static const char **searchRecordKey (struct recKeys *reckeys,
851 int attrSetS, int attrUseS)
853 static const char *ws[32];
861 for (i = 0; i<32; i++)
864 while (off < reckeys->buf_used)
867 const char *src = reckeys->buf + off;
875 memcpy (&attrSet, src, sizeof(attrSet));
876 src += sizeof(attrSet);
880 memcpy (&attrUse, src, sizeof(attrUse));
881 src += sizeof(attrUse);
887 seqno += ((lead>>2) & 15)-1;
890 memcpy (&seqno, src, sizeof(seqno));
891 src += sizeof(seqno);
893 if (attrUseS == attrUse && attrSetS == attrSet)
900 woff = seqno - startSeq;
901 if (woff >= 0 && woff < 31)
905 off = src - reckeys->buf;
907 assert (off == reckeys->buf_used);
911 struct file_read_info {
919 static struct file_read_info *file_read_start (int fd)
921 struct file_read_info *fi = xmalloc (sizeof(*fi));
925 fi->file_moffset = 0;
929 static void file_read_stop (struct file_read_info *fi)
935 static off_t file_seek (void *handle, off_t offset)
937 struct file_read_info *p = handle;
938 p->file_offset = offset;
939 return lseek (p->fd, offset, SEEK_SET);
942 static off_t file_tell (void *handle)
944 struct file_read_info *p = handle;
945 return p->file_offset;
948 static int file_read (void *handle, char *buf, size_t count)
950 struct file_read_info *p = handle;
953 r = read (fd, buf, count);
957 if (p->file_offset > p->file_max)
958 p->file_max = p->file_offset;
963 static void file_begin (void *handle)
965 struct file_read_info *p = handle;
967 p->file_offset = p->file_moffset;
969 lseek (p->fd, p->file_moffset, SEEK_SET);
973 static void file_end (void *handle, off_t offset)
975 struct file_read_info *p = handle;
977 assert (p->file_more == 0);
979 p->file_moffset = offset;
982 static int atois (const char **s)
985 while ( (c=**s) >= '0' && c <= '9')
987 val = val*10 + c - '0';
993 static char *fileMatchStr (struct recKeys *reckeys, struct recordGroup *rGroup,
997 static char dstBuf[2048];
999 const char *s = spec;
1000 static const char **w;
1005 while (*s == ' ' || *s == '\t')
1012 int attrSet, attrUse;
1016 attrSet = atois (&s);
1019 logf (LOG_WARN, "Missing , in match criteria %s in group %s",
1020 spec, rGroup->groupName ? rGroup->groupName : "none");
1024 attrUse = atois (&s);
1025 w = searchRecordKey (reckeys, attrSet, attrUse);
1030 for (i = 0; i<32; i++)
1035 logf (LOG_WARN, "Missing ) in match criteria %s in group %s",
1036 spec, rGroup->groupName ? rGroup->groupName : "none");
1041 for (i = 0; i<32; i++)
1042 if (matchFlag[i] && w[i])
1050 dst += strlen(w[i]);
1054 logf (LOG_WARN, "Record didn't contain match"
1055 " fields in (%d,%d)", attrSet, attrUse);
1063 const char *spec_src = NULL;
1064 const char *s1 = ++s;
1065 while (*s1 && *s1 != ' ' && *s1 != '\t')
1071 memcpy (special, s, spec_len);
1072 special[spec_len] = '\0';
1075 if (!strcmp (special, "group"))
1076 spec_src = rGroup->groupName;
1077 else if (!strcmp (special, "database"))
1078 spec_src = rGroup->databaseName;
1079 else if (!strcmp (special, "filename"))
1081 else if (!strcmp (special, "type"))
1082 spec_src = rGroup->recordType;
1087 strcpy (dst, spec_src);
1088 dst += strlen (spec_src);
1091 else if (*s == '\"' || *s == '\'')
1093 int stopMarker = *s++;
1097 while (*s && *s != stopMarker)
1100 tmpString[i++] = *s++;
1104 tmpString[i] = '\0';
1105 strcpy (dst, tmpString);
1106 dst += strlen (tmpString);
1110 logf (LOG_WARN, "Syntax error in match criteria %s in group %s",
1111 spec, rGroup->groupName ? rGroup->groupName : "none");
1118 logf (LOG_WARN, "No match criteria for record %s in group %s",
1119 fname, rGroup->groupName ? rGroup->groupName : "none");
1126 struct recordLogInfo {
1129 struct recordGroup *rGroup;
1132 static void recordLogPreamble (int level, const char *msg, void *info)
1134 struct recordLogInfo *p = info;
1135 FILE *outf = log_file ();
1137 if (level & LOG_LOG)
1139 fprintf (outf, "File %s, offset %d, type %s\n",
1140 p->rGroup->recordType, p->recordOffset, p->fname);
1141 log_event_start (NULL, NULL);
1144 static int recordExtract (SYSNO *sysno, const char *fname,
1145 struct recordGroup *rGroup, int deleteFlag,
1146 struct file_read_info *fi, RecType recType,
1149 struct recExtractCtrl extractCtrl;
1153 off_t recordOffset = 0;
1155 struct recordLogInfo logInfo;
1159 /* we are going to read from a file, so prepare the extraction */
1160 extractCtrl.fh = fi;
1161 extractCtrl.subType = subType;
1162 extractCtrl.init = wordInit;
1163 extractCtrl.add = addRecordKey;
1164 extractCtrl.dh = rGroup->dh;
1166 reckeys.buf_used = 0;
1167 reckeys.prevAttrUse = -1;
1168 reckeys.prevAttrSet = -1;
1169 reckeys.prevSeqNo = 0;
1171 recordOffset = fi->file_moffset;
1172 extractCtrl.offset = recordOffset;
1173 extractCtrl.readf = file_read;
1174 extractCtrl.seekf = file_seek;
1175 extractCtrl.tellf = file_tell;
1176 extractCtrl.endf = file_end;
1177 extractCtrl.zebra_maps = rGroup->zebra_maps;
1178 extractCtrl.flagShowRecords = !rGroup->flagRw;
1179 if (!rGroup->flagRw)
1180 printf ("File: %s %ld\n", fname, (long) recordOffset);
1182 logInfo.fname = fname;
1183 logInfo.recordOffset = recordOffset;
1184 logInfo.rGroup = rGroup;
1185 log_event_start (recordLogPreamble, &logInfo);
1187 r = (*recType->extract)(&extractCtrl);
1189 log_event_start (NULL, NULL);
1193 /* error occured during extraction ... */
1194 if (rGroup->flagRw &&
1195 records_processed < rGroup->fileVerboseLimit)
1197 logf (LOG_WARN, "fail %s %s %ld code = %d", rGroup->recordType,
1198 fname, (long) recordOffset, r);
1202 if (reckeys.buf_used == 0)
1204 /* the extraction process returned no information - the record
1205 is probably empty - unless flagShowRecords is in use */
1206 if (!rGroup->flagRw)
1208 logf (LOG_WARN, "No keys generated for file %s", fname);
1209 logf (LOG_WARN, " The file is probably empty");
1214 /* perform match if sysno not known and if match criteria is specified */
1221 if (rGroup->recordId && *rGroup->recordId)
1225 matchStr = fileMatchStr (&reckeys, rGroup, fname,
1229 rinfo = dict_lookup (matchDict, matchStr);
1231 memcpy (sysno, rinfo+1, sizeof(*sysno));
1235 logf (LOG_WARN, "Bad match criteria");
1246 logf (LOG_LOG, "delete %s %s %ld", rGroup->recordType,
1247 fname, (long) recordOffset);
1248 logf (LOG_WARN, "cannot delete record above (seems new)");
1251 if (records_processed < rGroup->fileVerboseLimit)
1252 logf (LOG_LOG, "add %s %s %ld", rGroup->recordType,
1253 fname, (long) recordOffset);
1254 rec = rec_new (records);
1255 *sysno = rec->sysno;
1259 dict_insert (matchDict, matchStr, sizeof(*sysno), sysno);
1261 flushRecordKeys (*sysno, 1, &reckeys, rGroup->databaseName);
1262 flushSortKeys (*sysno, 1);
1268 /* record already exists */
1269 struct recKeys delkeys;
1271 rec = rec_get (records, *sysno);
1273 delkeys.buf_used = rec->size[recInfo_delKeys];
1274 delkeys.buf = rec->info[recInfo_delKeys];
1275 flushSortKeys (*sysno, 0);
1276 flushRecordKeys (*sysno, 0, &delkeys, rec->info[recInfo_databaseName]);
1279 /* record going to be deleted */
1280 if (!delkeys.buf_used)
1282 logf (LOG_LOG, "delete %s %s %ld", rGroup->recordType,
1283 fname, (long) recordOffset);
1284 logf (LOG_WARN, "cannot delete file above, storeKeys false");
1288 if (records_processed < rGroup->fileVerboseLimit)
1289 logf (LOG_LOG, "delete %s %s %ld", rGroup->recordType,
1290 fname, (long) recordOffset);
1293 dict_delete (matchDict, matchStr);
1294 rec_del (records, &rec);
1301 /* record going to be updated */
1302 if (!delkeys.buf_used)
1304 logf (LOG_LOG, "update %s %s %ld", rGroup->recordType,
1305 fname, (long) recordOffset);
1306 logf (LOG_WARN, "cannot update file above, storeKeys false");
1310 if (records_processed < rGroup->fileVerboseLimit)
1311 logf (LOG_LOG, "update %s %s %ld", rGroup->recordType,
1312 fname, (long) recordOffset);
1313 flushRecordKeys (*sysno, 1, &reckeys, rGroup->databaseName);
1318 /* update file type */
1319 xfree (rec->info[recInfo_fileType]);
1320 rec->info[recInfo_fileType] =
1321 rec_strdup (rGroup->recordType, &rec->size[recInfo_fileType]);
1323 /* update filename */
1324 xfree (rec->info[recInfo_filename]);
1325 rec->info[recInfo_filename] =
1326 rec_strdup (fname, &rec->size[recInfo_filename]);
1328 /* update delete keys */
1329 xfree (rec->info[recInfo_delKeys]);
1330 if (reckeys.buf_used > 0 && rGroup->flagStoreKeys == 1)
1333 rec->size[recInfo_delKeys] = reckeys.buf_used;
1334 rec->info[recInfo_delKeys] = reckeys.buf;
1336 reckeys.buf_max = 0;
1338 rec->info[recInfo_delKeys] = xmalloc (reckeys.buf_used);
1339 rec->size[recInfo_delKeys] = reckeys.buf_used;
1340 memcpy (rec->info[recInfo_delKeys], reckeys.buf,
1341 rec->size[recInfo_delKeys]);
1346 rec->info[recInfo_delKeys] = NULL;
1347 rec->size[recInfo_delKeys] = 0;
1350 /* update store data */
1351 xfree (rec->info[recInfo_storeData]);
1352 if (rGroup->flagStoreData == 1)
1354 int size = fi->file_moffset - recordOffset;
1356 size = fi->file_max - recordOffset;
1357 rec->size[recInfo_storeData] = size;
1358 rec->info[recInfo_storeData] = xmalloc (size);
1359 if (lseek (fi->fd, recordOffset, SEEK_SET) < 0)
1361 logf (LOG_ERRNO|LOG_FATAL, "seek to %ld in %s", fname,
1362 (long) recordOffset);
1365 if (read (fi->fd, rec->info[recInfo_storeData], size) < size)
1367 logf (LOG_ERRNO|LOG_FATAL, "read %d bytes of %s",
1368 fi->file_max, fname);
1374 rec->info[recInfo_storeData] = NULL;
1375 rec->size[recInfo_storeData] = 0;
1377 /* update database name */
1378 xfree (rec->info[recInfo_databaseName]);
1379 rec->info[recInfo_databaseName] =
1380 rec_strdup (rGroup->databaseName, &rec->size[recInfo_databaseName]);
1383 xfree (rec->info[recInfo_offset]);
1385 rec->size[recInfo_offset] = sizeof(recordOffset);
1386 rec->info[recInfo_offset] = xmalloc (sizeof(recordOffset));
1387 memcpy (rec->info[recInfo_offset], &recordOffset, sizeof(recordOffset));
1389 /* commit this record */
1390 rec_put (records, &rec);
1395 int fileExtract (SYSNO *sysno, const char *fname,
1396 const struct recordGroup *rGroupP, int deleteFlag)
1404 struct recordGroup rGroupM;
1405 struct recordGroup *rGroup = &rGroupM;
1406 struct file_read_info *fi;
1408 memcpy (rGroup, rGroupP, sizeof(*rGroupP));
1410 if (!rGroup->groupName || !*rGroup->groupName)
1413 sprintf (gprefix, "%s.", rGroup->groupName);
1415 logf (LOG_DEBUG, "fileExtract %s", fname);
1417 /* determine file extension */
1418 for (i = strlen(fname); --i >= 0; )
1419 if (fname[i] == '/')
1424 else if (fname[i] == '.')
1426 strcpy (ext, fname+i+1);
1429 /* determine file type - depending on extension */
1430 if (!rGroup->recordType)
1432 sprintf (ext_res, "%srecordType.%s", gprefix, ext);
1433 if (!(rGroup->recordType = res_get (common_resource, ext_res)))
1435 sprintf (ext_res, "%srecordType", gprefix);
1436 if (!(rGroup->recordType = res_get (common_resource, ext_res)))
1438 if (records_processed < rGroup->fileVerboseLimit)
1439 logf (LOG_LOG, "? %s", fname);
1444 if (!rGroup->recordType)
1446 if (records_processed < rGroup->fileVerboseLimit)
1447 logf (LOG_LOG, "? record %s", fname);
1450 if (!(recType = recType_byName (rGroup->recordType, subType)))
1452 logf (LOG_WARN, "No such record type: %s", rGroup->recordType);
1456 /* determine match criteria */
1457 if (!rGroup->recordId)
1459 sprintf (ext_res, "%srecordId.%s", gprefix, ext);
1460 rGroup->recordId = res_get (common_resource, ext_res);
1463 /* determine database name */
1464 if (!rGroup->databaseName)
1466 sprintf (ext_res, "%sdatabase.%s", gprefix, ext);
1467 if (!(rGroup->databaseName = res_get (common_resource, ext_res)))
1469 sprintf (ext_res, "%sdatabase", gprefix);
1470 rGroup->databaseName = res_get (common_resource, ext_res);
1473 if (!rGroup->databaseName)
1474 rGroup->databaseName = "Default";
1476 if (rGroup->flagStoreData == -1)
1479 sprintf (ext_res, "%sstoreData.%s", gprefix, ext);
1480 if (!(sval = res_get (common_resource, ext_res)))
1482 sprintf (ext_res, "%sstoreData", gprefix);
1483 sval = res_get (common_resource, ext_res);
1486 rGroup->flagStoreData = atoi (sval);
1488 if (rGroup->flagStoreData == -1)
1489 rGroup->flagStoreData = 0;
1491 if (rGroup->flagStoreKeys == -1)
1495 sprintf (ext_res, "%sstoreKeys.%s", gprefix, ext);
1496 if (!(sval = res_get (common_resource, ext_res)))
1498 sprintf (ext_res, "%sstoreKeys", gprefix);
1499 sval = res_get (common_resource, ext_res);
1502 rGroup->flagStoreKeys = atoi (sval);
1504 if (rGroup->flagStoreKeys == -1)
1505 rGroup->flagStoreKeys = 0;
1507 if (sysno && deleteFlag)
1511 if ((fd = open (fname, O_BINARY|O_RDONLY)) == -1)
1513 logf (LOG_WARN|LOG_ERRNO, "open %s", fname);
1517 fi = file_read_start (fd);
1521 r = recordExtract (sysno, fname, rGroup, deleteFlag, fi,
1523 } while (r && !sysno && fi->file_more);
1524 file_read_stop (fi);