2 * Copyright (C) 1994-1999, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.90 1999-02-02 14:50:52 adam
8 * Updated WIN32 code specific sections. Changed header.
10 * Revision 1.89 1998/10/28 10:54:38 adam
13 * Revision 1.88 1998/10/16 08:14:29 adam
14 * Updated record control system.
16 * Revision 1.87 1998/10/15 13:10:33 adam
17 * Fixed bug in Zebra that caused it to stop indexing when empty
20 * Revision 1.86 1998/10/13 20:33:53 adam
21 * Fixed one log message and change use ordinal to be an unsigned char.
23 * Revision 1.85 1998/09/22 10:03:41 adam
24 * Changed result sets to be persistent in the sense that they can
25 * be re-searched if needed.
26 * Fixed memory leak in rsm_or.
28 * Revision 1.84 1998/06/11 15:42:22 adam
29 * Changed the way use attributes are specified in the recordId
32 * Revision 1.83 1998/06/08 14:43:10 adam
33 * Added suport for EXPLAIN Proxy servers - added settings databasePath
34 * and explainDatabase to facilitate this. Increased maximum number
35 * of databases and attributes in one register.
37 * Revision 1.82 1998/05/20 10:12:15 adam
38 * Implemented automatic EXPLAIN database maintenance.
39 * Modified Zebra to work with ASN.1 compiled version of YAZ.
41 * Revision 1.81 1998/03/11 11:19:04 adam
42 * Changed the way sequence numbers are generated.
44 * Revision 1.80 1998/03/05 08:45:11 adam
45 * New result set model and modular ranking system. Moved towards
46 * descent server API. System information stored as "SGML" records.
48 * Revision 1.79 1998/02/17 10:32:52 adam
49 * Fixed bug: binary files weren't opened with flag b on NT.
51 * Revision 1.78 1998/02/10 12:03:05 adam
54 * Revision 1.77 1998/01/12 15:04:08 adam
55 * The test option (-s) only uses read-lock (and not write lock).
57 * Revision 1.76 1997/10/27 14:33:04 adam
58 * Moved towards generic character mapping depending on "structure"
59 * field in abstract syntax file. Fixed a few memory leaks. Fixed
60 * bug with negative integers when doing searches with relational
63 * Revision 1.75 1997/09/17 12:19:12 adam
64 * Zebra version corresponds to YAZ version 1.4.
65 * Changed Zebra server so that it doesn't depend on global common_resource.
67 * Revision 1.74 1997/09/09 13:38:06 adam
68 * Partial port to WIN95/NT.
70 * Revision 1.73 1997/09/04 13:57:20 adam
71 * New file extract/retrieve method tellf (added).
72 * Added O_BINARY for open calls.
74 * Revision 1.72 1997/07/15 16:32:29 adam
75 * Bug fix: Match handler didn't terminate the resulting string!
77 * Revision 1.71 1997/07/15 16:28:41 adam
78 * Bug fix: storeData didn't work with files with multiple records.
79 * Bug fix: fixed memory management with records; not really well
82 * Revision 1.70 1997/07/01 13:00:42 adam
83 * Bug fix in routine searchRecordKey: uninitialized variables.
85 * Revision 1.69 1997/04/29 09:26:03 adam
86 * Bug fix: generic recordId handling didn't work for compressed internal
89 * Revision 1.68 1997/02/12 20:39:45 adam
90 * Implemented options -f <n> that limits the log to the first <n>
92 * Changed some log messages also.
94 * Revision 1.67 1996/11/15 15:02:14 adam
95 * Minor changes regarding logging.
97 * Revision 1.66 1996/11/14 09:52:21 adam
98 * Strings in record keys bound by IT_MAX_WORD.
100 * Revision 1.65 1996/11/14 08:57:56 adam
101 * Reduction of storeKeys area.
103 * Revision 1.64 1996/11/08 11:10:16 adam
104 * Buffers used during file match got bigger.
105 * Compressed ISAM support everywhere.
106 * Bug fixes regarding masking characters in queries.
107 * Redesigned Regexp-2 queries.
109 * Revision 1.63 1996/10/29 14:09:39 adam
110 * Use of cisam system - enabled if setting isamc is 1.
112 * Revision 1.62 1996/10/11 10:57:01 adam
113 * New module recctrl. Used to manage records (extract/retrieval).
114 * Several files have been moved to the recctrl sub directory.
116 * Revision 1.61 1996/06/06 12:08:37 quinn
117 * Added showRecord function
119 * Revision 1.60 1996/06/04 10:18:12 adam
120 * Search/scan uses character mapping module.
122 * Revision 1.59 1996/05/14 15:47:07 adam
123 * Cleanup of various buffer size entities.
125 * Revision 1.58 1996/05/14 06:16:38 adam
126 * Compact use/set bytes used in search service.
128 * Revision 1.57 1996/05/13 14:23:04 adam
129 * Work on compaction of set/use bytes in dictionary.
131 * Revision 1.56 1996/05/09 09:54:42 adam
132 * Server supports maps from one logical attributes to a list of physical
134 * The extraction process doesn't make space consuming 'any' keys.
136 * Revision 1.55 1996/05/09 07:28:55 quinn
137 * Work towards phrases and multiple registers
139 * Revision 1.54 1996/05/01 13:46:35 adam
140 * First work on multiple records in one file.
141 * New option, -offset, to the "unread" command in the filter module.
143 * Revision 1.53 1996/04/26 12:09:43 adam
144 * Added a few comments.
146 * Revision 1.52 1996/04/25 13:27:57 adam
147 * Function recordExtract modified so that files with no keys (possibly empty)
150 * Revision 1.51 1996/03/19 11:08:42 adam
151 * Bug fix: Log preamble wasn't always turned off after recordExtract.
153 * Revision 1.50 1996/02/12 18:45:36 adam
154 * New fileVerboseFlag in record group control.
156 * Revision 1.49 1996/02/05 12:29:57 adam
157 * Logging reduced a bit.
158 * The remaining running time is estimated during register merge.
160 * Revision 1.48 1996/02/01 20:53:26 adam
161 * The temporary per-record keys are compacted a little, and duplication
162 * of the per-records keys are avoided when they are saved in the record
163 * information buffer.
165 * Revision 1.47 1996/01/17 14:57:48 adam
166 * Prototype changed for reader functions in extract/retrieve. File
167 * is identified by 'void *' instead of 'int.
169 * Revision 1.46 1995/12/15 14:57:16 adam
172 * Revision 1.45 1995/12/15 12:37:41 adam
173 * In addRecordKeyAny: Writes key only when attrSet != -1.
175 * Revision 1.44 1995/12/12 16:00:54 adam
176 * System call sync(2) used after update/commit.
177 * Locking (based on fcntl) uses F_EXLCK and F_SHLCK instead of F_WRLCK
180 * Revision 1.43 1995/12/11 09:12:46 adam
181 * The rec_get function returns NULL if record doesn't exist - will
182 * happen in the server if the result set records have been deleted since
183 * the creation of the set (i.e. the search).
184 * The server saves a result temporarily if it is 'volatile', i.e. the
185 * set is register dependent.
187 * Revision 1.42 1995/12/07 17:38:46 adam
188 * Work locking mechanisms for concurrent updates/commit.
190 * Revision 1.41 1995/12/06 16:06:42 adam
191 * Better diagnostics. Work on 'real' dictionary deletion.
193 * Revision 1.40 1995/12/05 16:57:40 adam
194 * More work on regular patterns.
196 * Revision 1.39 1995/12/05 13:20:18 adam
197 * Bug fix: file_read sometimes returned early EOF.
199 * Revision 1.38 1995/12/04 17:59:21 adam
200 * More work on regular expression conversion.
202 * Revision 1.37 1995/12/04 14:22:27 adam
203 * Extra arg to recType_byName.
204 * Started work on new regular expression parsed input to
205 * structured records.
207 * Revision 1.36 1995/11/30 08:34:29 adam
208 * Started work on commit facility.
209 * Changed a few malloc/free to xmalloc/xfree.
211 * Revision 1.35 1995/11/28 14:26:21 adam
212 * Bug fix: recordId with constant wasn't right.
213 * Bug fix: recordId dictionary entry wasn't deleted when needed.
215 * Revision 1.34 1995/11/28 09:09:38 adam
216 * Zebra config renamed.
217 * Use setting 'recordId' to identify record now.
218 * Bug fix in recindex.c: rec_release_blocks was invokeded even
219 * though the blocks were already released.
220 * File traversal properly deletes records when needed.
222 * Revision 1.33 1995/11/27 09:56:20 adam
223 * Record info elements better enumerated. Internal store of records.
225 * Revision 1.32 1995/11/25 10:24:05 adam
226 * More record fields - they are enumerated now.
227 * New options: flagStoreData flagStoreKey.
229 * Revision 1.31 1995/11/24 11:31:35 adam
230 * Commands add & del read filenames from stdin if source directory is
232 * Match criteria supports 'constant' strings.
234 * Revision 1.30 1995/11/22 17:19:16 adam
235 * Record management uses the bfile system.
237 * Revision 1.29 1995/11/21 15:01:14 adam
238 * New general match criteria implemented.
239 * New feature: document groups.
241 * Revision 1.28 1995/11/21 09:20:30 adam
242 * Yet more work on record match.
244 * Revision 1.27 1995/11/20 16:59:45 adam
245 * New update method: the 'old' keys are saved for each records.
247 * Revision 1.26 1995/11/20 11:56:24 adam
248 * Work on new traversal.
250 * Revision 1.25 1995/11/16 15:34:54 adam
251 * Uses new record management system in both indexer and server.
253 * Revision 1.24 1995/11/15 19:13:08 adam
254 * Work on record management.
256 * Revision 1.23 1995/10/27 14:00:10 adam
257 * Implemented detection of database availability.
259 * Revision 1.22 1995/10/17 18:02:07 adam
260 * New feature: databases. Implemented as prefix to words in dictionary.
262 * Revision 1.21 1995/10/10 12:24:38 adam
263 * Temporary sort files are compressed.
265 * Revision 1.20 1995/10/06 13:52:05 adam
266 * Bug fixes. Handler may abort further scanning.
268 * Revision 1.19 1995/10/04 12:55:16 adam
269 * Bug fix in ranked search. Use=Any keys inserted.
271 * Revision 1.18 1995/10/04 09:37:08 quinn
274 * Revision 1.17 1995/10/03 14:28:57 adam
275 * Buffered read in extract works.
277 * Revision 1.16 1995/10/03 14:28:45 adam
278 * Work on more effecient read handler in extract.
280 * Revision 1.15 1995/10/02 15:42:53 adam
281 * Extract uses file descriptors instead of FILE pointers.
283 * Revision 1.14 1995/10/02 15:29:13 adam
284 * More logging in file_extract.
286 * Revision 1.13 1995/09/29 14:01:39 adam
289 * Revision 1.12 1995/09/28 14:22:56 adam
290 * Sort uses smaller temporary files.
292 * Revision 1.11 1995/09/28 12:10:31 adam
293 * Bug fixes. Field prefix used in queries.
295 * Revision 1.10 1995/09/28 09:19:41 adam
296 * xfree/xmalloc used everywhere.
297 * Extract/retrieve method seems to work for text records.
299 * Revision 1.9 1995/09/27 12:22:28 adam
300 * More work on extract in record control.
301 * Field name is not in isam keys but in prefix in dictionary words.
303 * Revision 1.8 1995/09/14 07:48:22 adam
304 * Record control management.
306 * Revision 1.7 1995/09/11 13:09:32 adam
307 * More work on relevance feedback.
309 * Revision 1.6 1995/09/08 14:52:27 adam
310 * Minor changes. Dictionary is lower case now.
312 * Revision 1.5 1995/09/06 16:11:16 adam
313 * Option: only one word key per file.
315 * Revision 1.4 1995/09/05 15:28:39 adam
316 * More work on search engine.
318 * Revision 1.3 1995/09/04 12:33:41 adam
319 * Various cleanup. YAZ util used instead.
321 * Revision 1.2 1995/09/04 09:10:34 adam
322 * More work on index add/del/update.
323 * Merge sort implemented.
324 * Initial work on z39 server.
326 * Revision 1.1 1995/09/01 14:06:35 adam
327 * Split of work into more files.
351 #include "zebrasdr.h"
354 static Dict matchDict;
356 static Records records = NULL;
357 static SortIdx sortIdx = NULL;
359 static char **key_buf;
360 static size_t ptr_top;
362 static size_t key_buf_used;
363 static int key_file_no;
365 static int records_inserted = 0;
366 static int records_updated = 0;
367 static int records_deleted = 0;
368 static int records_processed = 0;
370 static ZebraExplainInfo zti = NULL;
372 static void logRecord (int showFlag)
376 if (showFlag || !(records_processed % 1000))
378 logf (LOG_LOG, "Records: %7d i/u/d %d/%d/%d",
379 records_processed, records_inserted, records_updated,
384 static int explain_extract (void *handle, Record drec, data1_node *n);
386 int key_open (struct recordGroup *rGroup, int mem)
388 BFiles bfs = rGroup->bfs;
389 int rw = rGroup->flagRw;
390 data1_handle dh = rGroup->dh;
392 mem = atoi(res_get_def (common_resource, "memMax", "4"))*1024*1024;
395 key_buf = xmalloc (mem);
396 ptr_top = mem/sizeof(char*);
402 if (!(matchDict = dict_open (bfs, GMATCH_DICT, 50, rw)))
404 logf (LOG_FATAL, "dict_open fail of %s", GMATCH_DICT);
408 records = rec_open (bfs, rw);
411 dict_close (matchDict);
414 zti = zebraExplain_open (records, dh, common_resource,
415 rw, rGroup, explain_extract);
418 rec_close (&records);
419 dict_close (matchDict);
422 sortIdx = sortIdx_open (bfs, 1);
432 void encode_key_init (struct encode_info *i)
438 char *encode_key_int (int d, char *bp)
447 else if (d <= 4194303)
449 *bp++ = 128 + (d>>16);
450 *bp++ = (d>>8) & 255;
455 *bp++ = 192 + (d>>24);
456 *bp++ = (d>>16) & 255;
457 *bp++ = (d>>8) & 255;
463 void encode_key_write (char *k, struct encode_info *i, FILE *outf)
468 while ((*bp++ = *k++))
470 memcpy (&key, k+1, sizeof(struct it_key));
471 bp = encode_key_int ( (key.sysno - i->sysno) * 2 + *k, bp);
472 if (i->sysno != key.sysno)
474 i->sysno = key.sysno;
477 bp = encode_key_int (key.seqno - i->seqno, bp);
478 i->seqno = key.seqno;
479 if (fwrite (i->buf, bp - i->buf, 1, outf) != 1)
481 logf (LOG_FATAL|LOG_ERRNO, "fwrite");
489 static int key_y_len;
491 static int key_y_compare (const void *p1, const void *p2)
495 if ((r = key_compare (*(char**) p1 + key_y_len + 1,
496 *(char**) p2 + key_y_len + 1)))
498 return *(*(char**) p1 + key_y_len) - *(*(char**) p2 + key_y_len);
501 static int key_x_compare (const void *p1, const void *p2)
503 return strcmp (*(char**) p1, *(char**) p2);
507 void key_flush (void)
512 struct encode_info encode_info;
521 logf (LOG_LOG, "sorting section %d", key_file_no);
523 qsort (key_buf + ptr_top-ptr_i, ptr_i, sizeof(char*), key_qsort_compare);
524 getFnameTmp (out_fname, key_file_no);
526 if (!(outf = fopen (out_fname, "wb")))
528 logf (LOG_FATAL|LOG_ERRNO, "fopen %s", out_fname);
531 logf (LOG_LOG, "writing section %d", key_file_no);
532 prevcp = cp = key_buf[ptr_top-ptr_i];
534 encode_key_init (&encode_info);
535 encode_key_write (cp, &encode_info, outf);
538 cp = key_buf[ptr_top-ptr_i];
539 if (strcmp (cp, prevcp))
541 encode_key_init (&encode_info);
542 encode_key_write (cp, &encode_info, outf);
546 encode_key_write (cp + strlen(cp), &encode_info, outf);
549 qsort (key_buf + ptr_top-ptr_i, ptr_i, sizeof(char*), key_x_compare);
550 getFnameTmp (out_fname, key_file_no);
552 if (!(outf = fopen (out_fname, "wb")))
554 logf (LOG_FATAL|LOG_ERRNO, "fopen %s", out_fname);
557 logf (LOG_LOG, "writing section %d", key_file_no);
559 prevcp = key_buf[ptr_top-i];
561 if (!--i || strcmp (prevcp, key_buf[ptr_top-i]))
563 key_y_len = strlen(prevcp)+1;
565 logf (LOG_LOG, "key_y_len: %2d %02x %02x %s",
566 key_y_len, prevcp[0], prevcp[1], 2+prevcp);
568 qsort (key_buf + ptr_top-ptr_i, ptr_i - i,
569 sizeof(char*), key_y_compare);
570 cp = key_buf[ptr_top-ptr_i];
572 encode_key_init (&encode_info);
573 encode_key_write (cp, &encode_info, outf);
576 cp = key_buf[ptr_top-ptr_i];
577 encode_key_write (cp+key_y_len, &encode_info, outf);
581 prevcp = key_buf[ptr_top-ptr_i];
586 logf (LOG_FATAL|LOG_ERRNO, "fclose %s", out_fname);
589 logf (LOG_LOG, "finished section %d", key_file_no);
594 int key_close (struct recordGroup *rGroup)
596 int rw = rGroup->flagRw;
598 zebraExplain_runNumberIncrement (zti, 1);
599 zebraExplain_close (zti, rw, 0);
602 rec_close (&records);
603 dict_close (matchDict);
604 sortIdx_close (sortIdx);
610 static void wordInit (struct recExtractCtrl *p, RecWord *w)
612 w->zebra_maps = p->zebra_maps;
613 w->seqnos = p->seqno;
614 w->attrSet = VAL_BIB1;
619 static struct sortKey {
624 struct sortKey *next;
627 static struct recKeys {
636 static void addIndexString (RecWord *p, const char *string, int length)
639 unsigned char attrSet;
640 unsigned short attrUse;
643 int *pseqno = &p->seqnos[p->reg_type];
645 if (reckeys.buf_used+1024 > reckeys.buf_max)
649 b = xmalloc (reckeys.buf_max += 128000);
650 if (reckeys.buf_used > 0)
651 memcpy (b, reckeys.buf, reckeys.buf_used);
655 dst = reckeys.buf + reckeys.buf_used;
657 attrSet = p->attrSet;
658 if (reckeys.buf_used > 0 && reckeys.prevAttrSet == attrSet)
661 reckeys.prevAttrSet = attrSet;
662 attrUse = p->attrUse;
663 if (reckeys.buf_used > 0 && reckeys.prevAttrUse == attrUse)
666 reckeys.prevAttrUse = attrUse;
668 diff = 1 + *pseqno - reckeys.prevSeqNo;
669 if (diff >= 1 && diff <= 15)
674 reckeys.prevSeqNo = *pseqno;
680 memcpy (dst, &attrSet, sizeof(attrSet));
681 dst += sizeof(attrSet);
685 memcpy (dst, &attrUse, sizeof(attrUse));
686 dst += sizeof(attrUse);
688 *dst++ = p->reg_type;
689 memcpy (dst, string, length);
695 memcpy (dst, pseqno, sizeof(*pseqno));
696 dst += sizeof(*pseqno);
698 reckeys.buf_used = dst - reckeys.buf;
702 static void addSortString (RecWord *p, const char *string, int length)
706 for (sk = sortKeys; sk; sk = sk->next)
707 if (sk->attrSet == p->attrSet && sk->attrUse == p->attrUse)
710 sk = xmalloc (sizeof(*sk));
714 sk->string = xmalloc (length);
716 memcpy (sk->string, string, length);
718 sk->attrSet = p->attrSet;
719 sk->attrUse = p->attrUse;
722 static void addString (RecWord *p, const char *string, int length)
725 if (zebra_maps_is_sort (p->zebra_maps, p->reg_type))
726 addSortString (p, string, length);
728 addIndexString (p, string, length);
731 static void addIncompleteField (RecWord *p)
733 const char *b = p->string;
734 int remain = p->length;
735 const char **map = 0;
738 map = zebra_maps_input(p->zebra_maps, p->reg_type, &b, remain);
742 char buf[IT_MAX_WORD+1];
746 while (map && *map && **map == *CHR_SPACE)
748 remain = p->length - (b - p->string);
750 map = zebra_maps_input(p->zebra_maps, p->reg_type, &b, remain);
757 while (map && *map && **map != *CHR_SPACE)
759 const char *cp = *map;
761 while (i < IT_MAX_WORD && *cp)
763 remain = p->length - (b - p->string);
765 map = zebra_maps_input(p->zebra_maps, p->reg_type, &b, remain);
771 addString (p, buf, i);
775 static void addCompleteField (RecWord *p)
777 const char *b = p->string;
778 char buf[IT_MAX_WORD+1];
779 const char **map = 0;
780 int i = 0, remain = p->length;
783 map = zebra_maps_input (p->zebra_maps, p->reg_type, &b, remain);
785 while (remain > 0 && i < IT_MAX_WORD)
787 while (map && *map && **map == *CHR_SPACE)
789 remain = p->length - (b - p->string);
791 map = zebra_maps_input(p->zebra_maps, p->reg_type, &b, remain);
798 if (i && i < IT_MAX_WORD)
799 buf[i++] = *CHR_SPACE;
800 while (map && *map && **map != *CHR_SPACE)
802 const char *cp = *map;
804 if (i >= IT_MAX_WORD)
806 while (i < IT_MAX_WORD && *cp)
808 remain = p->length - (b - p->string);
810 map = zebra_maps_input (p->zebra_maps, p->reg_type, &b,
818 addString (p, buf, i);
821 static void addRecordKey (RecWord *p)
823 if (zebra_maps_is_complete (p->zebra_maps, p->reg_type))
824 addCompleteField (p);
826 addIncompleteField(p);
829 static void flushSortKeys (SYSNO sysno, int cmd)
831 struct sortKey *sk = sortKeys;
833 sortIdx_sysno (sortIdx, sysno);
836 struct sortKey *sk_next = sk->next;
837 sortIdx_type (sortIdx, sk->attrUse);
838 sortIdx_add (sortIdx, sk->string, sk->length);
846 static void flushRecordKeys (SYSNO sysno, int cmd, struct recKeys *reckeys)
848 unsigned char attrSet = (unsigned char) -1;
849 unsigned short attrUse = (unsigned short) -1;
853 zebraExplain_recordCountIncrement (zti, cmd ? 1 : -1);
854 while (off < reckeys->buf_used)
856 const char *src = reckeys->buf + off;
864 memcpy (&attrSet, src, sizeof(attrSet));
865 src += sizeof(attrSet);
869 memcpy (&attrUse, src, sizeof(attrUse));
870 src += sizeof(attrUse);
872 if (key_buf_used + 1024 > (ptr_top-ptr_i)*sizeof(char*))
875 key_buf[ptr_top-ptr_i] = (char*)key_buf + key_buf_used;
877 ch = zebraExplain_lookupSU (zti, attrSet, attrUse);
879 ch = zebraExplain_addSU (zti, attrSet, attrUse);
881 key_buf_used += key_SU_code (ch, ((char*)key_buf) + key_buf_used);
884 ((char*)key_buf) [key_buf_used++] = *src++;
886 ((char*)key_buf) [key_buf_used++] = '\0';
887 ((char*) key_buf)[key_buf_used++] = cmd;
890 seqno += ((lead>>2) & 15)-1;
893 memcpy (&seqno, src, sizeof(seqno));
894 src += sizeof(seqno);
898 memcpy ((char*)key_buf + key_buf_used, &key, sizeof(key));
899 key_buf_used += sizeof(key);
900 off = src - reckeys->buf;
902 assert (off == reckeys->buf_used);
905 static const char **searchRecordKey (struct recKeys *reckeys,
906 int attrSetS, int attrUseS)
908 static const char *ws[32];
916 for (i = 0; i<32; i++)
919 while (off < reckeys->buf_used)
922 const char *src = reckeys->buf + off;
930 memcpy (&attrSet, src, sizeof(attrSet));
931 src += sizeof(attrSet);
935 memcpy (&attrUse, src, sizeof(attrUse));
936 src += sizeof(attrUse);
942 seqno += ((lead>>2) & 15)-1;
945 memcpy (&seqno, src, sizeof(seqno));
946 src += sizeof(seqno);
948 if (attrUseS == attrUse && attrSetS == attrSet)
955 woff = seqno - startSeq;
956 if (woff >= 0 && woff < 31)
960 off = src - reckeys->buf;
962 assert (off == reckeys->buf_used);
966 struct file_read_info {
967 off_t file_max; /* maximum offset so far */
968 off_t file_offset; /* current offset */
969 off_t file_moffset; /* offset of rec/rec boundary */
976 static struct file_read_info *file_read_start (int fd)
978 struct file_read_info *fi = xmalloc (sizeof(*fi));
982 fi->file_moffset = 0;
988 static void file_read_stop (struct file_read_info *fi)
993 static off_t file_seek (void *handle, off_t offset)
995 struct file_read_info *p = handle;
996 p->file_offset = offset;
999 return lseek (p->fd, offset, SEEK_SET);
1002 static off_t file_tell (void *handle)
1004 struct file_read_info *p = handle;
1005 return p->file_offset;
1008 static int file_read (void *handle, char *buf, size_t count)
1010 struct file_read_info *p = handle;
1016 if (r > p->sdrmax - p->file_offset)
1017 r = p->sdrmax - p->file_offset;
1019 memcpy (buf, p->sdrbuf + p->file_offset, r);
1022 r = read (fd, buf, count);
1025 p->file_offset += r;
1026 if (p->file_offset > p->file_max)
1027 p->file_max = p->file_offset;
1032 static void file_begin (void *handle)
1034 struct file_read_info *p = handle;
1036 p->file_offset = p->file_moffset;
1037 if (!p->sdrbuf && p->file_moffset)
1038 lseek (p->fd, p->file_moffset, SEEK_SET);
1042 static void file_end (void *handle, off_t offset)
1044 struct file_read_info *p = handle;
1046 assert (p->file_more == 0);
1048 p->file_moffset = offset;
1051 static char *fileMatchStr (struct recKeys *reckeys, struct recordGroup *rGroup,
1052 const char *fname, const char *spec)
1054 static char dstBuf[2048];
1056 const char *s = spec;
1057 static const char **w;
1061 while (*s == ' ' || *s == '\t')
1067 char attset_str[64], attname_str[64];
1068 data1_attset *attset;
1071 int attSet = 1, attUse = 1;
1075 for (i = 0; *s && *s != ',' && *s != ')'; s++)
1077 attset_str[i++] = *s;
1078 attset_str[i] = '\0';
1083 for (i = 0; *s && *s != ')'; s++)
1085 attname_str[i++] = *s;
1086 attname_str[i] = '\0';
1089 if ((attset = data1_get_attset (rGroup->dh, attset_str)))
1092 attSet = attset->reference;
1093 att = data1_getattbyname(rGroup->dh, attset, attname_str);
1095 attUse = att->value;
1097 attUse = atoi (attname_str);
1099 w = searchRecordKey (reckeys, attSet, attUse);
1104 for (i = 0; i<32; i++)
1109 logf (LOG_WARN, "Missing ) in match criteria %s in group %s",
1110 spec, rGroup->groupName ? rGroup->groupName : "none");
1115 for (i = 0; i<32; i++)
1116 if (matchFlag[i] && w[i])
1124 dst += strlen(w[i]);
1128 logf (LOG_WARN, "Record didn't contain match"
1129 " fields in (%s,%s)", attset_str, attname_str);
1137 const char *spec_src = NULL;
1138 const char *s1 = ++s;
1139 while (*s1 && *s1 != ' ' && *s1 != '\t')
1145 memcpy (special, s, spec_len);
1146 special[spec_len] = '\0';
1149 if (!strcmp (special, "group"))
1150 spec_src = rGroup->groupName;
1151 else if (!strcmp (special, "database"))
1152 spec_src = rGroup->databaseName;
1153 else if (!strcmp (special, "filename"))
1155 else if (!strcmp (special, "type"))
1156 spec_src = rGroup->recordType;
1161 strcpy (dst, spec_src);
1162 dst += strlen (spec_src);
1165 else if (*s == '\"' || *s == '\'')
1167 int stopMarker = *s++;
1171 while (*s && *s != stopMarker)
1174 tmpString[i++] = *s++;
1178 tmpString[i] = '\0';
1179 strcpy (dst, tmpString);
1180 dst += strlen (tmpString);
1184 logf (LOG_WARN, "Syntax error in match criteria %s in group %s",
1185 spec, rGroup->groupName ? rGroup->groupName : "none");
1192 logf (LOG_WARN, "No match criteria for record %s in group %s",
1193 fname, rGroup->groupName ? rGroup->groupName : "none");
1200 struct recordLogInfo {
1203 struct recordGroup *rGroup;
1206 static void recordLogPreamble (int level, const char *msg, void *info)
1208 struct recordLogInfo *p = info;
1209 FILE *outf = log_file ();
1211 if (level & LOG_LOG)
1213 fprintf (outf, "File %s, offset %d, type %s\n",
1214 p->rGroup->recordType, p->recordOffset, p->fname);
1215 log_event_start (NULL, NULL);
1218 void addSchema (struct recExtractCtrl *p, Odr_oid *oid)
1220 zebraExplain_addSchema (zti, oid);
1223 static int recordExtract (SYSNO *sysno, const char *fname,
1224 struct recordGroup *rGroup, int deleteFlag,
1225 struct file_read_info *fi,
1226 RecType recType, char *subType)
1228 RecordAttr *recordAttr;
1233 struct recordLogInfo logInfo;
1234 off_t recordOffset = 0;
1238 struct recExtractCtrl extractCtrl;
1240 /* we are going to read from a file, so prepare the extraction */
1243 reckeys.buf_used = 0;
1244 reckeys.prevAttrUse = -1;
1245 reckeys.prevAttrSet = -1;
1246 reckeys.prevSeqNo = 0;
1248 recordOffset = fi->file_moffset;
1249 extractCtrl.offset = fi->file_moffset;
1250 extractCtrl.readf = file_read;
1251 extractCtrl.seekf = file_seek;
1252 extractCtrl.tellf = file_tell;
1253 extractCtrl.endf = file_end;
1254 extractCtrl.fh = fi;
1255 extractCtrl.subType = subType;
1256 extractCtrl.init = wordInit;
1257 extractCtrl.addWord = addRecordKey;
1258 extractCtrl.addSchema = addSchema;
1259 extractCtrl.dh = rGroup->dh;
1260 for (i = 0; i<256; i++)
1261 extractCtrl.seqno[i] = 0;
1262 extractCtrl.zebra_maps = rGroup->zebra_maps;
1263 extractCtrl.flagShowRecords = !rGroup->flagRw;
1265 if (!rGroup->flagRw)
1266 printf ("File: %s %ld\n", fname, (long) recordOffset);
1268 logInfo.fname = fname;
1269 logInfo.recordOffset = recordOffset;
1270 logInfo.rGroup = rGroup;
1271 log_event_start (recordLogPreamble, &logInfo);
1273 r = (*recType->extract)(&extractCtrl);
1275 log_event_start (NULL, NULL);
1279 /* error occured during extraction ... */
1280 if (rGroup->flagRw &&
1281 records_processed < rGroup->fileVerboseLimit)
1283 logf (LOG_WARN, "fail %s %s %ld code = %d", rGroup->recordType,
1284 fname, (long) recordOffset, r);
1288 if (reckeys.buf_used == 0)
1290 /* the extraction process returned no information - the record
1291 is probably empty - unless flagShowRecords is in use */
1292 if (!rGroup->flagRw)
1294 logf (LOG_WARN, "No keys generated for file %s", fname);
1295 logf (LOG_WARN, " The file is probably empty");
1300 /* perform match if sysno not known and if match criteria is specified */
1307 if (rGroup->recordId && *rGroup->recordId)
1311 matchStr = fileMatchStr (&reckeys, rGroup, fname,
1315 rinfo = dict_lookup (matchDict, matchStr);
1317 memcpy (sysno, rinfo+1, sizeof(*sysno));
1321 logf (LOG_WARN, "Bad match criteria");
1332 logf (LOG_LOG, "delete %s %s %ld", rGroup->recordType,
1333 fname, (long) recordOffset);
1334 logf (LOG_WARN, "cannot delete record above (seems new)");
1337 if (records_processed < rGroup->fileVerboseLimit)
1338 logf (LOG_LOG, "add %s %s %ld", rGroup->recordType,
1339 fname, (long) recordOffset);
1340 rec = rec_new (records);
1342 *sysno = rec->sysno;
1344 recordAttr = rec_init_attr (zti, rec);
1348 dict_insert (matchDict, matchStr, sizeof(*sysno), sysno);
1350 flushRecordKeys (*sysno, 1, &reckeys);
1351 flushSortKeys (*sysno, 1);
1357 /* record already exists */
1358 struct recKeys delkeys;
1360 rec = rec_get (records, *sysno);
1363 recordAttr = rec_init_attr (zti, rec);
1365 if (recordAttr->runNumber == zebraExplain_runNumberIncrement (zti, 0))
1367 logf (LOG_LOG, "skipped %s %s %ld", rGroup->recordType,
1368 fname, (long) recordOffset);
1373 delkeys.buf_used = rec->size[recInfo_delKeys];
1374 delkeys.buf = rec->info[recInfo_delKeys];
1375 flushSortKeys (*sysno, 0);
1376 flushRecordKeys (*sysno, 0, &delkeys);
1379 /* record going to be deleted */
1380 if (!delkeys.buf_used)
1382 logf (LOG_LOG, "delete %s %s %ld", rGroup->recordType,
1383 fname, (long) recordOffset);
1384 logf (LOG_WARN, "cannot delete file above, storeKeys false");
1388 if (records_processed < rGroup->fileVerboseLimit)
1389 logf (LOG_LOG, "delete %s %s %ld", rGroup->recordType,
1390 fname, (long) recordOffset);
1393 dict_delete (matchDict, matchStr);
1394 rec_del (records, &rec);
1402 /* record going to be updated */
1403 if (!delkeys.buf_used)
1405 logf (LOG_LOG, "update %s %s %ld", rGroup->recordType,
1406 fname, (long) recordOffset);
1407 logf (LOG_WARN, "cannot update file above, storeKeys false");
1411 if (records_processed < rGroup->fileVerboseLimit)
1412 logf (LOG_LOG, "update %s %s %ld", rGroup->recordType,
1413 fname, (long) recordOffset);
1414 flushRecordKeys (*sysno, 1, &reckeys);
1419 /* update file type */
1420 xfree (rec->info[recInfo_fileType]);
1421 rec->info[recInfo_fileType] =
1422 rec_strdup (rGroup->recordType, &rec->size[recInfo_fileType]);
1424 /* update filename */
1425 xfree (rec->info[recInfo_filename]);
1426 rec->info[recInfo_filename] =
1427 rec_strdup (fname, &rec->size[recInfo_filename]);
1429 /* update delete keys */
1430 xfree (rec->info[recInfo_delKeys]);
1431 if (reckeys.buf_used > 0 && rGroup->flagStoreKeys == 1)
1434 rec->size[recInfo_delKeys] = reckeys.buf_used;
1435 rec->info[recInfo_delKeys] = reckeys.buf;
1437 reckeys.buf_max = 0;
1439 rec->info[recInfo_delKeys] = xmalloc (reckeys.buf_used);
1440 rec->size[recInfo_delKeys] = reckeys.buf_used;
1441 memcpy (rec->info[recInfo_delKeys], reckeys.buf,
1442 rec->size[recInfo_delKeys]);
1447 rec->info[recInfo_delKeys] = NULL;
1448 rec->size[recInfo_delKeys] = 0;
1451 /* save file size of original record */
1452 zebraExplain_recordBytesIncrement (zti, - recordAttr->recordSize);
1453 recordAttr->recordSize = fi->file_moffset - recordOffset;
1454 if (!recordAttr->recordSize)
1455 recordAttr->recordSize = fi->file_max - recordOffset;
1456 zebraExplain_recordBytesIncrement (zti, recordAttr->recordSize);
1458 /* set run-number for this record */
1459 recordAttr->runNumber = zebraExplain_runNumberIncrement (zti, 0);
1461 /* update store data */
1462 xfree (rec->info[recInfo_storeData]);
1463 if (rGroup->flagStoreData == 1)
1465 rec->size[recInfo_storeData] = recordAttr->recordSize;
1466 rec->info[recInfo_storeData] = xmalloc (recordAttr->recordSize);
1467 if (lseek (fi->fd, recordOffset, SEEK_SET) < 0)
1469 logf (LOG_ERRNO|LOG_FATAL, "seek to %ld in %s",
1470 (long) recordOffset, fname);
1473 if (read (fi->fd, rec->info[recInfo_storeData], recordAttr->recordSize)
1474 < recordAttr->recordSize)
1476 logf (LOG_ERRNO|LOG_FATAL, "read %d bytes of %s",
1477 recordAttr->recordSize, fname);
1483 rec->info[recInfo_storeData] = NULL;
1484 rec->size[recInfo_storeData] = 0;
1486 /* update database name */
1487 xfree (rec->info[recInfo_databaseName]);
1488 rec->info[recInfo_databaseName] =
1489 rec_strdup (rGroup->databaseName, &rec->size[recInfo_databaseName]);
1492 recordAttr->recordOffset = recordOffset;
1494 /* commit this record */
1495 rec_put (records, &rec);
1500 int fileExtract (SYSNO *sysno, const char *fname,
1501 const struct recordGroup *rGroupP, int deleteFlag)
1509 struct recordGroup rGroupM;
1510 struct recordGroup *rGroup = &rGroupM;
1511 struct file_read_info *fi;
1513 memcpy (rGroup, rGroupP, sizeof(*rGroupP));
1515 if (!rGroup->groupName || !*rGroup->groupName)
1518 sprintf (gprefix, "%s.", rGroup->groupName);
1520 logf (LOG_DEBUG, "fileExtract %s", fname);
1522 /* determine file extension */
1524 for (i = strlen(fname); --i >= 0; )
1525 if (fname[i] == '/')
1527 else if (fname[i] == '.')
1529 strcpy (ext, fname+i+1);
1532 /* determine file type - depending on extension */
1533 if (!rGroup->recordType)
1535 sprintf (ext_res, "%srecordType.%s", gprefix, ext);
1536 if (!(rGroup->recordType = res_get (common_resource, ext_res)))
1538 sprintf (ext_res, "%srecordType", gprefix);
1539 rGroup->recordType = res_get (common_resource, ext_res);
1542 if (!rGroup->recordType)
1544 if (records_processed < rGroup->fileVerboseLimit)
1545 logf (LOG_LOG, "? %s", fname);
1548 if (!*rGroup->recordType)
1551 recType_byName (rGroup->recTypes, rGroup->recordType, subType)))
1553 logf (LOG_WARN, "No such record type: %s", rGroup->recordType);
1557 /* determine match criteria */
1558 if (!rGroup->recordId)
1560 sprintf (ext_res, "%srecordId.%s", gprefix, ext);
1561 rGroup->recordId = res_get (common_resource, ext_res);
1564 /* determine database name */
1565 if (!rGroup->databaseName)
1567 sprintf (ext_res, "%sdatabase.%s", gprefix, ext);
1568 if (!(rGroup->databaseName = res_get (common_resource, ext_res)))
1570 sprintf (ext_res, "%sdatabase", gprefix);
1571 rGroup->databaseName = res_get (common_resource, ext_res);
1574 if (!rGroup->databaseName)
1575 rGroup->databaseName = "Default";
1577 /* determine if explain database */
1579 sprintf (ext_res, "%sexplainDatabase", gprefix);
1580 rGroup->explainDatabase =
1581 atoi (res_get_def (common_resource, ext_res, "0"));
1583 /* announce database */
1584 if (zebraExplain_curDatabase (zti, rGroup->databaseName))
1586 if (zebraExplain_newDatabase (zti, rGroup->databaseName,
1587 rGroup->explainDatabase))
1591 if (rGroup->flagStoreData == -1)
1594 sprintf (ext_res, "%sstoreData.%s", gprefix, ext);
1595 if (!(sval = res_get (common_resource, ext_res)))
1597 sprintf (ext_res, "%sstoreData", gprefix);
1598 sval = res_get (common_resource, ext_res);
1601 rGroup->flagStoreData = atoi (sval);
1603 if (rGroup->flagStoreData == -1)
1604 rGroup->flagStoreData = 0;
1606 if (rGroup->flagStoreKeys == -1)
1610 sprintf (ext_res, "%sstoreKeys.%s", gprefix, ext);
1611 if (!(sval = res_get (common_resource, ext_res)))
1613 sprintf (ext_res, "%sstoreKeys", gprefix);
1614 sval = res_get (common_resource, ext_res);
1617 rGroup->flagStoreKeys = atoi (sval);
1619 if (rGroup->flagStoreKeys == -1)
1620 rGroup->flagStoreKeys = 0;
1626 char xname[128], *xp;
1628 strncpy (xname, fname, 127);
1629 if ((xp = strchr (xname, '.')))
1632 h = zebraSdr_open (xname);
1635 logf (LOG_WARN, "sdr open %s", xname);
1644 segmentno = zebraSdr_segment (h, 0);
1645 sprintf (sdr_name, "%%%s.%d", xname, segmentno);
1646 logf (LOG_LOG, "SDR: %s", sdr_name);
1652 r = zebraSdr_read (h, &buf);
1657 fi = file_read_start (0);
1663 r = recordExtract (sysno, sdr_name, rGroup, deleteFlag, fi,
1665 } while (r && !sysno && fi->file_more);
1666 file_read_stop (fi);
1673 if (sysno && deleteFlag)
1677 if ((fd = open (fname, O_BINARY|O_RDONLY)) == -1)
1679 logf (LOG_WARN|LOG_ERRNO, "open %s", fname);
1683 fi = file_read_start (fd);
1687 r = recordExtract (sysno, fname, rGroup, deleteFlag, fi,
1689 } while (r && !sysno && fi->file_more);
1690 file_read_stop (fi);
1696 static int explain_extract (void *handle, Record rec, data1_node *n)
1698 struct recordGroup *rGroup = (struct recordGroup*) handle;
1699 struct recExtractCtrl extractCtrl;
1702 if (zebraExplain_curDatabase (zti, rec->info[recInfo_databaseName]))
1704 if (zebraExplain_newDatabase (zti, rec->info[recInfo_databaseName], 0))
1708 reckeys.buf_used = 0;
1709 reckeys.prevAttrUse = -1;
1710 reckeys.prevAttrSet = -1;
1711 reckeys.prevSeqNo = 0;
1713 extractCtrl.init = wordInit;
1714 extractCtrl.addWord = addRecordKey;
1715 extractCtrl.addSchema = addSchema;
1716 extractCtrl.dh = rGroup->dh;
1717 for (i = 0; i<256; i++)
1718 extractCtrl.seqno[i] = 0;
1719 extractCtrl.zebra_maps = rGroup->zebra_maps;
1720 extractCtrl.flagShowRecords = !rGroup->flagRw;
1722 grs_extract_tree(&extractCtrl, n);
1724 logf (LOG_DEBUG, "flush explain record, sysno=%d", rec->sysno);
1726 if (rec->size[recInfo_delKeys])
1728 struct recKeys delkeys;
1730 delkeys.buf_used = rec->size[recInfo_delKeys];
1731 delkeys.buf = rec->info[recInfo_delKeys];
1732 flushSortKeys (rec->sysno, 0);
1733 flushRecordKeys (rec->sysno, 0, &delkeys);
1735 flushRecordKeys (rec->sysno, 1, &reckeys);
1736 flushSortKeys (rec->sysno, 1);
1738 xfree (rec->info[recInfo_delKeys]);
1739 rec->size[recInfo_delKeys] = reckeys.buf_used;
1740 rec->info[recInfo_delKeys] = reckeys.buf;
1742 reckeys.buf_max = 0;