2 * Copyright (C) 1994-1998, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.80 1998-03-05 08:45:11 adam
8 * New result set model and modular ranking system. Moved towards
9 * descent server API. System information stored as "SGML" records.
11 * Revision 1.79 1998/02/17 10:32:52 adam
12 * Fixed bug: binary files weren't opened with flag b on NT.
14 * Revision 1.78 1998/02/10 12:03:05 adam
17 * Revision 1.77 1998/01/12 15:04:08 adam
18 * The test option (-s) only uses read-lock (and not write lock).
20 * Revision 1.76 1997/10/27 14:33:04 adam
21 * Moved towards generic character mapping depending on "structure"
22 * field in abstract syntax file. Fixed a few memory leaks. Fixed
23 * bug with negative integers when doing searches with relational
26 * Revision 1.75 1997/09/17 12:19:12 adam
27 * Zebra version corresponds to YAZ version 1.4.
28 * Changed Zebra server so that it doesn't depend on global common_resource.
30 * Revision 1.74 1997/09/09 13:38:06 adam
31 * Partial port to WIN95/NT.
33 * Revision 1.73 1997/09/04 13:57:20 adam
34 * New file extract/retrieve method tellf (added).
35 * Added O_BINARY for open calls.
37 * Revision 1.72 1997/07/15 16:32:29 adam
38 * Bug fix: Match handler didn't terminate the resulting string!
40 * Revision 1.71 1997/07/15 16:28:41 adam
41 * Bug fix: storeData didn't work with files with multiple records.
42 * Bug fix: fixed memory management with records; not really well
45 * Revision 1.70 1997/07/01 13:00:42 adam
46 * Bug fix in routine searchRecordKey: uninitialized variables.
48 * Revision 1.69 1997/04/29 09:26:03 adam
49 * Bug fix: generic recordId handling didn't work for compressed internal
52 * Revision 1.68 1997/02/12 20:39:45 adam
53 * Implemented options -f <n> that limits the log to the first <n>
55 * Changed some log messages also.
57 * Revision 1.67 1996/11/15 15:02:14 adam
58 * Minor changes regarding logging.
60 * Revision 1.66 1996/11/14 09:52:21 adam
61 * Strings in record keys bound by IT_MAX_WORD.
63 * Revision 1.65 1996/11/14 08:57:56 adam
64 * Reduction of storeKeys area.
66 * Revision 1.64 1996/11/08 11:10:16 adam
67 * Buffers used during file match got bigger.
68 * Compressed ISAM support everywhere.
69 * Bug fixes regarding masking characters in queries.
70 * Redesigned Regexp-2 queries.
72 * Revision 1.63 1996/10/29 14:09:39 adam
73 * Use of cisam system - enabled if setting isamc is 1.
75 * Revision 1.62 1996/10/11 10:57:01 adam
76 * New module recctrl. Used to manage records (extract/retrieval).
77 * Several files have been moved to the recctrl sub directory.
79 * Revision 1.61 1996/06/06 12:08:37 quinn
80 * Added showRecord function
82 * Revision 1.60 1996/06/04 10:18:12 adam
83 * Search/scan uses character mapping module.
85 * Revision 1.59 1996/05/14 15:47:07 adam
86 * Cleanup of various buffer size entities.
88 * Revision 1.58 1996/05/14 06:16:38 adam
89 * Compact use/set bytes used in search service.
91 * Revision 1.57 1996/05/13 14:23:04 adam
92 * Work on compaction of set/use bytes in dictionary.
94 * Revision 1.56 1996/05/09 09:54:42 adam
95 * Server supports maps from one logical attributes to a list of physical
97 * The extraction process doesn't make space consuming 'any' keys.
99 * Revision 1.55 1996/05/09 07:28:55 quinn
100 * Work towards phrases and multiple registers
102 * Revision 1.54 1996/05/01 13:46:35 adam
103 * First work on multiple records in one file.
104 * New option, -offset, to the "unread" command in the filter module.
106 * Revision 1.53 1996/04/26 12:09:43 adam
107 * Added a few comments.
109 * Revision 1.52 1996/04/25 13:27:57 adam
110 * Function recordExtract modified so that files with no keys (possibly empty)
113 * Revision 1.51 1996/03/19 11:08:42 adam
114 * Bug fix: Log preamble wasn't always turned off after recordExtract.
116 * Revision 1.50 1996/02/12 18:45:36 adam
117 * New fileVerboseFlag in record group control.
119 * Revision 1.49 1996/02/05 12:29:57 adam
120 * Logging reduced a bit.
121 * The remaining running time is estimated during register merge.
123 * Revision 1.48 1996/02/01 20:53:26 adam
124 * The temporary per-record keys are compacted a little, and duplication
125 * of the per-records keys are avoided when they are saved in the record
126 * information buffer.
128 * Revision 1.47 1996/01/17 14:57:48 adam
129 * Prototype changed for reader functions in extract/retrieve. File
130 * is identified by 'void *' instead of 'int.
132 * Revision 1.46 1995/12/15 14:57:16 adam
135 * Revision 1.45 1995/12/15 12:37:41 adam
136 * In addRecordKeyAny: Writes key only when attrSet != -1.
138 * Revision 1.44 1995/12/12 16:00:54 adam
139 * System call sync(2) used after update/commit.
140 * Locking (based on fcntl) uses F_EXLCK and F_SHLCK instead of F_WRLCK
143 * Revision 1.43 1995/12/11 09:12:46 adam
144 * The rec_get function returns NULL if record doesn't exist - will
145 * happen in the server if the result set records have been deleted since
146 * the creation of the set (i.e. the search).
147 * The server saves a result temporarily if it is 'volatile', i.e. the
148 * set is register dependent.
150 * Revision 1.42 1995/12/07 17:38:46 adam
151 * Work locking mechanisms for concurrent updates/commit.
153 * Revision 1.41 1995/12/06 16:06:42 adam
154 * Better diagnostics. Work on 'real' dictionary deletion.
156 * Revision 1.40 1995/12/05 16:57:40 adam
157 * More work on regular patterns.
159 * Revision 1.39 1995/12/05 13:20:18 adam
160 * Bug fix: file_read sometimes returned early EOF.
162 * Revision 1.38 1995/12/04 17:59:21 adam
163 * More work on regular expression conversion.
165 * Revision 1.37 1995/12/04 14:22:27 adam
166 * Extra arg to recType_byName.
167 * Started work on new regular expression parsed input to
168 * structured records.
170 * Revision 1.36 1995/11/30 08:34:29 adam
171 * Started work on commit facility.
172 * Changed a few malloc/free to xmalloc/xfree.
174 * Revision 1.35 1995/11/28 14:26:21 adam
175 * Bug fix: recordId with constant wasn't right.
176 * Bug fix: recordId dictionary entry wasn't deleted when needed.
178 * Revision 1.34 1995/11/28 09:09:38 adam
179 * Zebra config renamed.
180 * Use setting 'recordId' to identify record now.
181 * Bug fix in recindex.c: rec_release_blocks was invokeded even
182 * though the blocks were already released.
183 * File traversal properly deletes records when needed.
185 * Revision 1.33 1995/11/27 09:56:20 adam
186 * Record info elements better enumerated. Internal store of records.
188 * Revision 1.32 1995/11/25 10:24:05 adam
189 * More record fields - they are enumerated now.
190 * New options: flagStoreData flagStoreKey.
192 * Revision 1.31 1995/11/24 11:31:35 adam
193 * Commands add & del read filenames from stdin if source directory is
195 * Match criteria supports 'constant' strings.
197 * Revision 1.30 1995/11/22 17:19:16 adam
198 * Record management uses the bfile system.
200 * Revision 1.29 1995/11/21 15:01:14 adam
201 * New general match criteria implemented.
202 * New feature: document groups.
204 * Revision 1.28 1995/11/21 09:20:30 adam
205 * Yet more work on record match.
207 * Revision 1.27 1995/11/20 16:59:45 adam
208 * New update method: the 'old' keys are saved for each records.
210 * Revision 1.26 1995/11/20 11:56:24 adam
211 * Work on new traversal.
213 * Revision 1.25 1995/11/16 15:34:54 adam
214 * Uses new record management system in both indexer and server.
216 * Revision 1.24 1995/11/15 19:13:08 adam
217 * Work on record management.
219 * Revision 1.23 1995/10/27 14:00:10 adam
220 * Implemented detection of database availability.
222 * Revision 1.22 1995/10/17 18:02:07 adam
223 * New feature: databases. Implemented as prefix to words in dictionary.
225 * Revision 1.21 1995/10/10 12:24:38 adam
226 * Temporary sort files are compressed.
228 * Revision 1.20 1995/10/06 13:52:05 adam
229 * Bug fixes. Handler may abort further scanning.
231 * Revision 1.19 1995/10/04 12:55:16 adam
232 * Bug fix in ranked search. Use=Any keys inserted.
234 * Revision 1.18 1995/10/04 09:37:08 quinn
237 * Revision 1.17 1995/10/03 14:28:57 adam
238 * Buffered read in extract works.
240 * Revision 1.16 1995/10/03 14:28:45 adam
241 * Work on more effecient read handler in extract.
243 * Revision 1.15 1995/10/02 15:42:53 adam
244 * Extract uses file descriptors instead of FILE pointers.
246 * Revision 1.14 1995/10/02 15:29:13 adam
247 * More logging in file_extract.
249 * Revision 1.13 1995/09/29 14:01:39 adam
252 * Revision 1.12 1995/09/28 14:22:56 adam
253 * Sort uses smaller temporary files.
255 * Revision 1.11 1995/09/28 12:10:31 adam
256 * Bug fixes. Field prefix used in queries.
258 * Revision 1.10 1995/09/28 09:19:41 adam
259 * xfree/xmalloc used everywhere.
260 * Extract/retrieve method seems to work for text records.
262 * Revision 1.9 1995/09/27 12:22:28 adam
263 * More work on extract in record control.
264 * Field name is not in isam keys but in prefix in dictionary words.
266 * Revision 1.8 1995/09/14 07:48:22 adam
267 * Record control management.
269 * Revision 1.7 1995/09/11 13:09:32 adam
270 * More work on relevance feedback.
272 * Revision 1.6 1995/09/08 14:52:27 adam
273 * Minor changes. Dictionary is lower case now.
275 * Revision 1.5 1995/09/06 16:11:16 adam
276 * Option: only one word key per file.
278 * Revision 1.4 1995/09/05 15:28:39 adam
279 * More work on search engine.
281 * Revision 1.3 1995/09/04 12:33:41 adam
282 * Various cleanup. YAZ util used instead.
284 * Revision 1.2 1995/09/04 09:10:34 adam
285 * More work on index add/del/update.
286 * Merge sort implemented.
287 * Initial work on z39 server.
289 * Revision 1.1 1995/09/01 14:06:35 adam
290 * Split of work into more files.
309 static Dict matchDict;
311 static Records records = NULL;
312 static SortIdx sortIdx = NULL;
314 static char **key_buf;
315 static size_t ptr_top;
317 static size_t key_buf_used;
318 static int key_file_no;
320 static int records_inserted = 0;
321 static int records_updated = 0;
322 static int records_deleted = 0;
323 static int records_processed = 0;
325 static ZebraExplainInfo zti = NULL;
327 static void logRecord (int showFlag)
331 if (showFlag || !(records_processed % 1000))
333 logf (LOG_LOG, "Records: %7d i/u/d %d/%d/%d",
334 records_processed, records_inserted, records_updated,
339 int key_open (BFiles bfs, int mem, int rw, data1_handle dh)
342 mem = atoi(res_get_def (common_resource, "memMax", "4"))*1024*1024;
345 key_buf = xmalloc (mem);
346 ptr_top = mem/sizeof(char*);
352 if (!(matchDict = dict_open (bfs, GMATCH_DICT, 50, rw)))
354 logf (LOG_FATAL, "dict_open fail of %s", GMATCH_DICT);
358 records = rec_open (bfs, rw);
361 dict_close (matchDict);
364 zti = zebraExplain_open (records, dh, rw);
367 rec_close (&records);
368 dict_close (matchDict);
371 sortIdx = sortIdx_open (bfs, 1);
381 void encode_key_init (struct encode_info *i)
387 char *encode_key_int (int d, char *bp)
396 else if (d <= 4194303)
398 *bp++ = 128 + (d>>16);
399 *bp++ = (d>>8) & 255;
404 *bp++ = 192 + (d>>24);
405 *bp++ = (d>>16) & 255;
406 *bp++ = (d>>8) & 255;
412 void encode_key_write (char *k, struct encode_info *i, FILE *outf)
417 while ((*bp++ = *k++))
419 memcpy (&key, k+1, sizeof(struct it_key));
420 bp = encode_key_int ( (key.sysno - i->sysno) * 2 + *k, bp);
421 if (i->sysno != key.sysno)
423 i->sysno = key.sysno;
426 bp = encode_key_int (key.seqno - i->seqno, bp);
427 i->seqno = key.seqno;
428 if (fwrite (i->buf, bp - i->buf, 1, outf) != 1)
430 logf (LOG_FATAL|LOG_ERRNO, "fwrite");
438 static int key_y_len;
440 static int key_y_compare (const void *p1, const void *p2)
444 if ((r = key_compare (*(char**) p1 + key_y_len + 1,
445 *(char**) p2 + key_y_len + 1)))
447 return *(*(char**) p1 + key_y_len) - *(*(char**) p2 + key_y_len);
450 static int key_x_compare (const void *p1, const void *p2)
452 return strcmp (*(char**) p1, *(char**) p2);
456 void key_flush (void)
461 struct encode_info encode_info;
470 logf (LOG_LOG, "sorting section %d", key_file_no);
472 qsort (key_buf + ptr_top-ptr_i, ptr_i, sizeof(char*), key_qsort_compare);
473 getFnameTmp (out_fname, key_file_no);
475 if (!(outf = fopen (out_fname, "wb")))
477 logf (LOG_FATAL|LOG_ERRNO, "fopen %s", out_fname);
480 logf (LOG_LOG, "writing section %d", key_file_no);
481 prevcp = cp = key_buf[ptr_top-ptr_i];
483 encode_key_init (&encode_info);
484 encode_key_write (cp, &encode_info, outf);
487 cp = key_buf[ptr_top-ptr_i];
488 if (strcmp (cp, prevcp))
490 encode_key_init (&encode_info);
491 encode_key_write (cp, &encode_info, outf);
495 encode_key_write (cp + strlen(cp), &encode_info, outf);
498 qsort (key_buf + ptr_top-ptr_i, ptr_i, sizeof(char*), key_x_compare);
499 getFnameTmp (out_fname, key_file_no);
501 if (!(outf = fopen (out_fname, "wb")))
503 logf (LOG_FATAL|LOG_ERRNO, "fopen %s", out_fname);
506 logf (LOG_LOG, "writing section %d", key_file_no);
508 prevcp = key_buf[ptr_top-i];
510 if (!--i || strcmp (prevcp, key_buf[ptr_top-i]))
512 key_y_len = strlen(prevcp)+1;
514 logf (LOG_LOG, "key_y_len: %2d %02x %02x %s",
515 key_y_len, prevcp[0], prevcp[1], 2+prevcp);
517 qsort (key_buf + ptr_top-ptr_i, ptr_i - i,
518 sizeof(char*), key_y_compare);
519 cp = key_buf[ptr_top-ptr_i];
521 encode_key_init (&encode_info);
522 encode_key_write (cp, &encode_info, outf);
525 cp = key_buf[ptr_top-ptr_i];
526 encode_key_write (cp+key_y_len, &encode_info, outf);
530 prevcp = key_buf[ptr_top-ptr_i];
535 logf (LOG_FATAL|LOG_ERRNO, "fclose %s", out_fname);
538 logf (LOG_LOG, "finished section %d", key_file_no);
543 int key_close (int rw)
548 zebraExplain_runNumberIncrement (zti, 1);
549 zebraExplain_close (zti, rw);
550 rec_close (&records);
551 dict_close (matchDict);
552 sortIdx_close (sortIdx);
558 static void wordInit (struct recExtractCtrl *p, RecWord *w)
560 w->zebra_maps = p->zebra_maps;
566 static struct sortKey {
571 struct sortKey *next;
574 static struct recKeys {
583 static void addIndexString (RecWord *p, const char *string, int length)
591 if (reckeys.buf_used+1024 > reckeys.buf_max)
595 b = xmalloc (reckeys.buf_max += 128000);
596 if (reckeys.buf_used > 0)
597 memcpy (b, reckeys.buf, reckeys.buf_used);
601 dst = reckeys.buf + reckeys.buf_used;
603 attrSet = p->attrSet;
604 if (reckeys.buf_used > 0 && reckeys.prevAttrSet == attrSet)
607 reckeys.prevAttrSet = attrSet;
608 attrUse = p->attrUse;
609 if (reckeys.buf_used > 0 && reckeys.prevAttrUse == attrUse)
612 reckeys.prevAttrUse = attrUse;
614 diff = 1 + p->seqno - reckeys.prevSeqNo;
615 if (diff >= 1 && diff <= 15)
620 reckeys.prevSeqNo = p->seqno;
626 memcpy (dst, &attrSet, sizeof(attrSet));
627 dst += sizeof(attrSet);
631 memcpy (dst, &attrUse, sizeof(attrUse));
632 dst += sizeof(attrUse);
634 *dst++ = p->reg_type;
635 memcpy (dst, string, length);
641 memcpy (dst, &p->seqno, sizeof(p->seqno));
642 dst += sizeof(p->seqno);
644 reckeys.buf_used = dst - reckeys.buf;
648 static void addSortString (RecWord *p, const char *string, int length)
652 for (sk = sortKeys; sk; sk = sk->next)
653 if (sk->attrSet == p->attrSet && sk->attrUse == p->attrUse)
656 sk = xmalloc (sizeof(*sk));
660 sk->string = xmalloc (p->length);
661 sk->length = p->length;
662 memcpy (sk->string, p->string, p->length);
663 sk->attrSet = p->attrSet;
664 sk->attrUse = p->attrUse;
667 static void addString (RecWord *p, const char *string, int length)
670 if (zebra_maps_is_sort (p->zebra_maps, p->reg_type))
671 addSortString (p, string, length);
673 addIndexString (p, string, length);
676 static void addIncompleteField (RecWord *p)
678 const char *b = p->string;
679 int remain = p->length;
680 const char **map = 0;
683 map = zebra_maps_input(p->zebra_maps, p->reg_type, &b, remain);
687 char buf[IT_MAX_WORD+1];
691 while (map && *map && **map == *CHR_SPACE)
693 remain = p->length - (b - p->string);
695 map = zebra_maps_input(p->zebra_maps, p->reg_type, &b, remain);
702 while (map && *map && **map != *CHR_SPACE)
704 const char *cp = *map;
706 while (i < IT_MAX_WORD && *cp)
708 remain = p->length - (b - p->string);
710 map = zebra_maps_input(p->zebra_maps, p->reg_type, &b, remain);
716 addString (p, buf, i);
720 static void addCompleteField (RecWord *p)
722 const char *b = p->string;
723 char buf[IT_MAX_WORD+1];
724 const char **map = 0;
725 int i = 0, remain = p->length;
728 map = zebra_maps_input (p->zebra_maps, p->reg_type, &b, remain);
730 while (remain > 0 && i < IT_MAX_WORD)
732 while (map && *map && **map == *CHR_SPACE)
734 remain = p->length - (b - p->string);
736 map = zebra_maps_input(p->zebra_maps, p->reg_type, &b, remain);
743 if (i && i < IT_MAX_WORD)
744 buf[i++] = *CHR_SPACE;
745 while (map && *map && **map != *CHR_SPACE)
747 const char *cp = *map;
749 if (i >= IT_MAX_WORD)
751 while (i < IT_MAX_WORD && *cp)
753 remain = p->length - (b - p->string);
755 map = zebra_maps_input (p->zebra_maps, p->reg_type, &b,
763 addString (p, buf, i);
766 static void addRecordKey (RecWord *p)
768 if (zebra_maps_is_complete (p->zebra_maps, p->reg_type))
769 addCompleteField (p);
771 addIncompleteField(p);
774 static void flushSortKeys (SYSNO sysno, int cmd)
776 struct sortKey *sk = sortKeys;
778 sortIdx_sysno (sortIdx, sysno);
781 struct sortKey *sk_next = sk->next;
782 sortIdx_type (sortIdx, sk->attrUse);
783 sortIdx_add (sortIdx, sk->string, sk->length);
791 static void flushRecordKeys (SYSNO sysno, int cmd, struct recKeys *reckeys,
792 const char *databaseName)
799 if (zebraExplain_curDatabase (zti, databaseName))
801 if (zebraExplain_newDatabase (zti, databaseName))
804 zebraExplain_recordCountIncrement (zti, cmd ? 1 : -1);
805 while (off < reckeys->buf_used)
807 const char *src = reckeys->buf + off;
815 memcpy (&attrSet, src, sizeof(attrSet));
816 src += sizeof(attrSet);
820 memcpy (&attrUse, src, sizeof(attrUse));
821 src += sizeof(attrUse);
823 if (key_buf_used + 1024 > (ptr_top-ptr_i)*sizeof(char*))
826 key_buf[ptr_top-ptr_i] = (char*)key_buf + key_buf_used;
828 ch = zebraExplain_lookupSU (zti, attrSet, attrUse);
830 ch = zebraExplain_addSU (zti, attrSet, attrUse);
832 ((char*) key_buf) [key_buf_used++] = ch;
834 ((char*)key_buf) [key_buf_used++] = *src++;
836 ((char*)key_buf) [key_buf_used++] = '\0';
837 ((char*) key_buf)[key_buf_used++] = cmd;
840 seqno += ((lead>>2) & 15)-1;
843 memcpy (&seqno, src, sizeof(seqno));
844 src += sizeof(seqno);
848 memcpy ((char*)key_buf + key_buf_used, &key, sizeof(key));
849 key_buf_used += sizeof(key);
850 off = src - reckeys->buf;
852 assert (off == reckeys->buf_used);
855 static const char **searchRecordKey (struct recKeys *reckeys,
856 int attrSetS, int attrUseS)
858 static const char *ws[32];
866 for (i = 0; i<32; i++)
869 while (off < reckeys->buf_used)
872 const char *src = reckeys->buf + off;
880 memcpy (&attrSet, src, sizeof(attrSet));
881 src += sizeof(attrSet);
885 memcpy (&attrUse, src, sizeof(attrUse));
886 src += sizeof(attrUse);
892 seqno += ((lead>>2) & 15)-1;
895 memcpy (&seqno, src, sizeof(seqno));
896 src += sizeof(seqno);
898 if (attrUseS == attrUse && attrSetS == attrSet)
905 woff = seqno - startSeq;
906 if (woff >= 0 && woff < 31)
910 off = src - reckeys->buf;
912 assert (off == reckeys->buf_used);
916 struct file_read_info {
924 static struct file_read_info *file_read_start (int fd)
926 struct file_read_info *fi = xmalloc (sizeof(*fi));
930 fi->file_moffset = 0;
934 static void file_read_stop (struct file_read_info *fi)
940 static off_t file_seek (void *handle, off_t offset)
942 struct file_read_info *p = handle;
943 p->file_offset = offset;
944 return lseek (p->fd, offset, SEEK_SET);
947 static off_t file_tell (void *handle)
949 struct file_read_info *p = handle;
950 return p->file_offset;
953 static int file_read (void *handle, char *buf, size_t count)
955 struct file_read_info *p = handle;
958 r = read (fd, buf, count);
962 if (p->file_offset > p->file_max)
963 p->file_max = p->file_offset;
968 static void file_begin (void *handle)
970 struct file_read_info *p = handle;
972 p->file_offset = p->file_moffset;
974 lseek (p->fd, p->file_moffset, SEEK_SET);
978 static void file_end (void *handle, off_t offset)
980 struct file_read_info *p = handle;
982 assert (p->file_more == 0);
984 p->file_moffset = offset;
987 static int atois (const char **s)
990 while ( (c=**s) >= '0' && c <= '9')
992 val = val*10 + c - '0';
998 static char *fileMatchStr (struct recKeys *reckeys, struct recordGroup *rGroup,
1002 static char dstBuf[2048];
1004 const char *s = spec;
1005 static const char **w;
1010 while (*s == ' ' || *s == '\t')
1017 int attrSet, attrUse;
1021 attrSet = atois (&s);
1024 logf (LOG_WARN, "Missing , in match criteria %s in group %s",
1025 spec, rGroup->groupName ? rGroup->groupName : "none");
1029 attrUse = atois (&s);
1030 w = searchRecordKey (reckeys, attrSet, attrUse);
1035 for (i = 0; i<32; i++)
1040 logf (LOG_WARN, "Missing ) in match criteria %s in group %s",
1041 spec, rGroup->groupName ? rGroup->groupName : "none");
1046 for (i = 0; i<32; i++)
1047 if (matchFlag[i] && w[i])
1055 dst += strlen(w[i]);
1059 logf (LOG_WARN, "Record didn't contain match"
1060 " fields in (%d,%d)", attrSet, attrUse);
1068 const char *spec_src = NULL;
1069 const char *s1 = ++s;
1070 while (*s1 && *s1 != ' ' && *s1 != '\t')
1076 memcpy (special, s, spec_len);
1077 special[spec_len] = '\0';
1080 if (!strcmp (special, "group"))
1081 spec_src = rGroup->groupName;
1082 else if (!strcmp (special, "database"))
1083 spec_src = rGroup->databaseName;
1084 else if (!strcmp (special, "filename"))
1086 else if (!strcmp (special, "type"))
1087 spec_src = rGroup->recordType;
1092 strcpy (dst, spec_src);
1093 dst += strlen (spec_src);
1096 else if (*s == '\"' || *s == '\'')
1098 int stopMarker = *s++;
1102 while (*s && *s != stopMarker)
1105 tmpString[i++] = *s++;
1109 tmpString[i] = '\0';
1110 strcpy (dst, tmpString);
1111 dst += strlen (tmpString);
1115 logf (LOG_WARN, "Syntax error in match criteria %s in group %s",
1116 spec, rGroup->groupName ? rGroup->groupName : "none");
1123 logf (LOG_WARN, "No match criteria for record %s in group %s",
1124 fname, rGroup->groupName ? rGroup->groupName : "none");
1131 struct recordLogInfo {
1134 struct recordGroup *rGroup;
1137 static void recordLogPreamble (int level, const char *msg, void *info)
1139 struct recordLogInfo *p = info;
1140 FILE *outf = log_file ();
1142 if (level & LOG_LOG)
1144 fprintf (outf, "File %s, offset %d, type %s\n",
1145 p->rGroup->recordType, p->recordOffset, p->fname);
1146 log_event_start (NULL, NULL);
1149 static int recordExtract (SYSNO *sysno, const char *fname,
1150 struct recordGroup *rGroup, int deleteFlag,
1151 struct file_read_info *fi, RecType recType,
1154 struct recExtractCtrl extractCtrl;
1155 RecordAttr *recordAttr;
1159 off_t recordOffset = 0;
1161 struct recordLogInfo logInfo;
1165 /* we are going to read from a file, so prepare the extraction */
1166 extractCtrl.fh = fi;
1167 extractCtrl.subType = subType;
1168 extractCtrl.init = wordInit;
1169 extractCtrl.add = addRecordKey;
1170 extractCtrl.dh = rGroup->dh;
1172 reckeys.buf_used = 0;
1173 reckeys.prevAttrUse = -1;
1174 reckeys.prevAttrSet = -1;
1175 reckeys.prevSeqNo = 0;
1177 recordOffset = fi->file_moffset;
1178 extractCtrl.offset = recordOffset;
1179 extractCtrl.readf = file_read;
1180 extractCtrl.seekf = file_seek;
1181 extractCtrl.tellf = file_tell;
1182 extractCtrl.endf = file_end;
1183 extractCtrl.zebra_maps = rGroup->zebra_maps;
1184 extractCtrl.flagShowRecords = !rGroup->flagRw;
1185 if (!rGroup->flagRw)
1186 printf ("File: %s %ld\n", fname, (long) recordOffset);
1188 logInfo.fname = fname;
1189 logInfo.recordOffset = recordOffset;
1190 logInfo.rGroup = rGroup;
1191 log_event_start (recordLogPreamble, &logInfo);
1193 r = (*recType->extract)(&extractCtrl);
1195 log_event_start (NULL, NULL);
1199 /* error occured during extraction ... */
1200 if (rGroup->flagRw &&
1201 records_processed < rGroup->fileVerboseLimit)
1203 logf (LOG_WARN, "fail %s %s %ld code = %d", rGroup->recordType,
1204 fname, (long) recordOffset, r);
1208 if (reckeys.buf_used == 0)
1210 /* the extraction process returned no information - the record
1211 is probably empty - unless flagShowRecords is in use */
1212 if (!rGroup->flagRw)
1214 logf (LOG_WARN, "No keys generated for file %s", fname);
1215 logf (LOG_WARN, " The file is probably empty");
1220 /* perform match if sysno not known and if match criteria is specified */
1227 if (rGroup->recordId && *rGroup->recordId)
1231 matchStr = fileMatchStr (&reckeys, rGroup, fname,
1235 rinfo = dict_lookup (matchDict, matchStr);
1237 memcpy (sysno, rinfo+1, sizeof(*sysno));
1241 logf (LOG_WARN, "Bad match criteria");
1252 logf (LOG_LOG, "delete %s %s %ld", rGroup->recordType,
1253 fname, (long) recordOffset);
1254 logf (LOG_WARN, "cannot delete record above (seems new)");
1257 if (records_processed < rGroup->fileVerboseLimit)
1258 logf (LOG_LOG, "add %s %s %ld", rGroup->recordType,
1259 fname, (long) recordOffset);
1260 rec = rec_new (records);
1262 *sysno = rec->sysno;
1264 recordAttr = rec_init_attr (zti, rec);
1268 dict_insert (matchDict, matchStr, sizeof(*sysno), sysno);
1270 flushRecordKeys (*sysno, 1, &reckeys, rGroup->databaseName);
1271 flushSortKeys (*sysno, 1);
1277 /* record already exists */
1278 struct recKeys delkeys;
1280 rec = rec_get (records, *sysno);
1283 recordAttr = rec_init_attr (zti, rec);
1285 if (recordAttr->runNumber == zebraExplain_runNumberIncrement (zti, 0))
1287 logf (LOG_LOG, "skipped %s %s %ld", rGroup->recordType,
1288 fname, (long) recordOffset);
1293 delkeys.buf_used = rec->size[recInfo_delKeys];
1294 delkeys.buf = rec->info[recInfo_delKeys];
1295 flushSortKeys (*sysno, 0);
1296 flushRecordKeys (*sysno, 0, &delkeys, rec->info[recInfo_databaseName]);
1299 /* record going to be deleted */
1300 if (!delkeys.buf_used)
1302 logf (LOG_LOG, "delete %s %s %ld", rGroup->recordType,
1303 fname, (long) recordOffset);
1304 logf (LOG_WARN, "cannot delete file above, storeKeys false");
1308 if (records_processed < rGroup->fileVerboseLimit)
1309 logf (LOG_LOG, "delete %s %s %ld", rGroup->recordType,
1310 fname, (long) recordOffset);
1313 dict_delete (matchDict, matchStr);
1314 rec_del (records, &rec);
1322 /* record going to be updated */
1323 if (!delkeys.buf_used)
1325 logf (LOG_LOG, "update %s %s %ld", rGroup->recordType,
1326 fname, (long) recordOffset);
1327 logf (LOG_WARN, "cannot update file above, storeKeys false");
1331 if (records_processed < rGroup->fileVerboseLimit)
1332 logf (LOG_LOG, "update %s %s %ld", rGroup->recordType,
1333 fname, (long) recordOffset);
1334 flushRecordKeys (*sysno, 1, &reckeys, rGroup->databaseName);
1339 /* update file type */
1340 xfree (rec->info[recInfo_fileType]);
1341 rec->info[recInfo_fileType] =
1342 rec_strdup (rGroup->recordType, &rec->size[recInfo_fileType]);
1344 /* update filename */
1345 xfree (rec->info[recInfo_filename]);
1346 rec->info[recInfo_filename] =
1347 rec_strdup (fname, &rec->size[recInfo_filename]);
1349 /* update delete keys */
1350 xfree (rec->info[recInfo_delKeys]);
1351 if (reckeys.buf_used > 0 && rGroup->flagStoreKeys == 1)
1354 rec->size[recInfo_delKeys] = reckeys.buf_used;
1355 rec->info[recInfo_delKeys] = reckeys.buf;
1357 reckeys.buf_max = 0;
1359 rec->info[recInfo_delKeys] = xmalloc (reckeys.buf_used);
1360 rec->size[recInfo_delKeys] = reckeys.buf_used;
1361 memcpy (rec->info[recInfo_delKeys], reckeys.buf,
1362 rec->size[recInfo_delKeys]);
1367 rec->info[recInfo_delKeys] = NULL;
1368 rec->size[recInfo_delKeys] = 0;
1371 /* save file size of original record */
1372 zebraExplain_recordBytesIncrement (zti, - recordAttr->recordSize);
1373 recordAttr->recordSize = fi->file_moffset - recordOffset;
1374 if (!recordAttr->recordSize)
1375 recordAttr->recordSize = fi->file_max - recordOffset;
1376 zebraExplain_recordBytesIncrement (zti, recordAttr->recordSize);
1378 /* set run-number for this record */
1379 recordAttr->runNumber = zebraExplain_runNumberIncrement (zti, 0);
1381 /* update store data */
1382 xfree (rec->info[recInfo_storeData]);
1383 if (rGroup->flagStoreData == 1)
1385 rec->size[recInfo_storeData] = recordAttr->recordSize;
1386 rec->info[recInfo_storeData] = xmalloc (recordAttr->recordSize);
1387 if (lseek (fi->fd, recordOffset, SEEK_SET) < 0)
1389 logf (LOG_ERRNO|LOG_FATAL, "seek to %ld in %s", fname,
1390 (long) recordOffset);
1393 if (read (fi->fd, rec->info[recInfo_storeData], recordAttr->recordSize)
1394 < recordAttr->recordSize)
1396 logf (LOG_ERRNO|LOG_FATAL, "read %d bytes of %s",
1397 recordAttr->recordSize, fname);
1403 rec->info[recInfo_storeData] = NULL;
1404 rec->size[recInfo_storeData] = 0;
1406 /* update database name */
1407 xfree (rec->info[recInfo_databaseName]);
1408 rec->info[recInfo_databaseName] =
1409 rec_strdup (rGroup->databaseName, &rec->size[recInfo_databaseName]);
1412 recordAttr->recordOffset = recordOffset;
1414 /* commit this record */
1415 rec_put (records, &rec);
1420 int fileExtract (SYSNO *sysno, const char *fname,
1421 const struct recordGroup *rGroupP, int deleteFlag)
1429 struct recordGroup rGroupM;
1430 struct recordGroup *rGroup = &rGroupM;
1431 struct file_read_info *fi;
1433 memcpy (rGroup, rGroupP, sizeof(*rGroupP));
1435 if (!rGroup->groupName || !*rGroup->groupName)
1438 sprintf (gprefix, "%s.", rGroup->groupName);
1440 logf (LOG_DEBUG, "fileExtract %s", fname);
1442 /* determine file extension */
1443 for (i = strlen(fname); --i >= 0; )
1444 if (fname[i] == '/')
1449 else if (fname[i] == '.')
1451 strcpy (ext, fname+i+1);
1454 /* determine file type - depending on extension */
1455 if (!rGroup->recordType)
1457 sprintf (ext_res, "%srecordType.%s", gprefix, ext);
1458 if (!(rGroup->recordType = res_get (common_resource, ext_res)))
1460 sprintf (ext_res, "%srecordType", gprefix);
1461 if (!(rGroup->recordType = res_get (common_resource, ext_res)))
1463 if (records_processed < rGroup->fileVerboseLimit)
1464 logf (LOG_LOG, "? %s", fname);
1469 if (!rGroup->recordType)
1471 if (records_processed < rGroup->fileVerboseLimit)
1472 logf (LOG_LOG, "? record %s", fname);
1475 if (!(recType = recType_byName (rGroup->recordType, subType)))
1477 logf (LOG_WARN, "No such record type: %s", rGroup->recordType);
1481 /* determine match criteria */
1482 if (!rGroup->recordId)
1484 sprintf (ext_res, "%srecordId.%s", gprefix, ext);
1485 rGroup->recordId = res_get (common_resource, ext_res);
1488 /* determine database name */
1489 if (!rGroup->databaseName)
1491 sprintf (ext_res, "%sdatabase.%s", gprefix, ext);
1492 if (!(rGroup->databaseName = res_get (common_resource, ext_res)))
1494 sprintf (ext_res, "%sdatabase", gprefix);
1495 rGroup->databaseName = res_get (common_resource, ext_res);
1498 if (!rGroup->databaseName)
1499 rGroup->databaseName = "Default";
1501 if (rGroup->flagStoreData == -1)
1504 sprintf (ext_res, "%sstoreData.%s", gprefix, ext);
1505 if (!(sval = res_get (common_resource, ext_res)))
1507 sprintf (ext_res, "%sstoreData", gprefix);
1508 sval = res_get (common_resource, ext_res);
1511 rGroup->flagStoreData = atoi (sval);
1513 if (rGroup->flagStoreData == -1)
1514 rGroup->flagStoreData = 0;
1516 if (rGroup->flagStoreKeys == -1)
1520 sprintf (ext_res, "%sstoreKeys.%s", gprefix, ext);
1521 if (!(sval = res_get (common_resource, ext_res)))
1523 sprintf (ext_res, "%sstoreKeys", gprefix);
1524 sval = res_get (common_resource, ext_res);
1527 rGroup->flagStoreKeys = atoi (sval);
1529 if (rGroup->flagStoreKeys == -1)
1530 rGroup->flagStoreKeys = 0;
1532 if (sysno && deleteFlag)
1536 if ((fd = open (fname, O_BINARY|O_RDONLY)) == -1)
1538 logf (LOG_WARN|LOG_ERRNO, "open %s", fname);
1542 fi = file_read_start (fd);
1546 r = recordExtract (sysno, fname, rGroup, deleteFlag, fi,
1548 } while (r && !sysno && fi->file_more);
1549 file_read_stop (fi);