2 * Copyright (C) 1994-1999, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.100 2000-03-20 19:08:36 adam
8 * Added remote record import using Z39.50 extended services and Segment
11 * Revision 1.99 2000/02/24 10:57:02 adam
12 * Sequence number incremented after each incomplete-field.
14 * Revision 1.98 1999/09/07 07:19:21 adam
15 * Work on character mapping. Implemented replace rules.
17 * Revision 1.97 1999/07/06 12:28:04 adam
18 * Updated record index structure. Format includes version ID. Compression
19 * algorithm ID is stored for each record block.
21 * Revision 1.96 1999/05/26 07:49:13 adam
24 * Revision 1.95 1999/05/21 12:00:17 adam
25 * Better diagnostics for extraction process.
27 * Revision 1.94 1999/05/20 12:57:18 adam
28 * Implemented TCL filter. Updated recctrl system.
30 * Revision 1.93 1999/05/15 14:36:38 adam
31 * Updated dictionary. Implemented "compression" of dictionary.
33 * Revision 1.92 1999/03/09 16:27:49 adam
34 * More work on SDRKit integration.
36 * Revision 1.91 1999/02/12 13:29:22 adam
37 * Implemented position-flag for registers.
39 * Revision 1.90 1999/02/02 14:50:52 adam
40 * Updated WIN32 code specific sections. Changed header.
42 * Revision 1.89 1998/10/28 10:54:38 adam
45 * Revision 1.88 1998/10/16 08:14:29 adam
46 * Updated record control system.
48 * Revision 1.87 1998/10/15 13:10:33 adam
49 * Fixed bug in Zebra that caused it to stop indexing when empty
52 * Revision 1.86 1998/10/13 20:33:53 adam
53 * Fixed one log message and change use ordinal to be an unsigned char.
55 * Revision 1.85 1998/09/22 10:03:41 adam
56 * Changed result sets to be persistent in the sense that they can
57 * be re-searched if needed.
58 * Fixed memory leak in rsm_or.
60 * Revision 1.84 1998/06/11 15:42:22 adam
61 * Changed the way use attributes are specified in the recordId
64 * Revision 1.83 1998/06/08 14:43:10 adam
65 * Added suport for EXPLAIN Proxy servers - added settings databasePath
66 * and explainDatabase to facilitate this. Increased maximum number
67 * of databases and attributes in one register.
69 * Revision 1.82 1998/05/20 10:12:15 adam
70 * Implemented automatic EXPLAIN database maintenance.
71 * Modified Zebra to work with ASN.1 compiled version of YAZ.
73 * Revision 1.81 1998/03/11 11:19:04 adam
74 * Changed the way sequence numbers are generated.
76 * Revision 1.80 1998/03/05 08:45:11 adam
77 * New result set model and modular ranking system. Moved towards
78 * descent server API. System information stored as "SGML" records.
80 * Revision 1.79 1998/02/17 10:32:52 adam
81 * Fixed bug: binary files weren't opened with flag b on NT.
83 * Revision 1.78 1998/02/10 12:03:05 adam
86 * Revision 1.77 1998/01/12 15:04:08 adam
87 * The test option (-s) only uses read-lock (and not write lock).
89 * Revision 1.76 1997/10/27 14:33:04 adam
90 * Moved towards generic character mapping depending on "structure"
91 * field in abstract syntax file. Fixed a few memory leaks. Fixed
92 * bug with negative integers when doing searches with relational
95 * Revision 1.75 1997/09/17 12:19:12 adam
96 * Zebra version corresponds to YAZ version 1.4.
97 * Changed Zebra server so that it doesn't depend on global common_resource.
99 * Revision 1.74 1997/09/09 13:38:06 adam
100 * Partial port to WIN95/NT.
102 * Revision 1.73 1997/09/04 13:57:20 adam
103 * New file extract/retrieve method tellf (added).
104 * Added O_BINARY for open calls.
106 * Revision 1.72 1997/07/15 16:32:29 adam
107 * Bug fix: Match handler didn't terminate the resulting string!
109 * Revision 1.71 1997/07/15 16:28:41 adam
110 * Bug fix: storeData didn't work with files with multiple records.
111 * Bug fix: fixed memory management with records; not really well
114 * Revision 1.70 1997/07/01 13:00:42 adam
115 * Bug fix in routine searchRecordKey: uninitialized variables.
117 * Revision 1.69 1997/04/29 09:26:03 adam
118 * Bug fix: generic recordId handling didn't work for compressed internal
121 * Revision 1.68 1997/02/12 20:39:45 adam
122 * Implemented options -f <n> that limits the log to the first <n>
124 * Changed some log messages also.
126 * Revision 1.67 1996/11/15 15:02:14 adam
127 * Minor changes regarding logging.
129 * Revision 1.66 1996/11/14 09:52:21 adam
130 * Strings in record keys bound by IT_MAX_WORD.
132 * Revision 1.65 1996/11/14 08:57:56 adam
133 * Reduction of storeKeys area.
135 * Revision 1.64 1996/11/08 11:10:16 adam
136 * Buffers used during file match got bigger.
137 * Compressed ISAM support everywhere.
138 * Bug fixes regarding masking characters in queries.
139 * Redesigned Regexp-2 queries.
141 * Revision 1.63 1996/10/29 14:09:39 adam
142 * Use of cisam system - enabled if setting isamc is 1.
144 * Revision 1.62 1996/10/11 10:57:01 adam
145 * New module recctrl. Used to manage records (extract/retrieval).
146 * Several files have been moved to the recctrl sub directory.
148 * Revision 1.61 1996/06/06 12:08:37 quinn
149 * Added showRecord function
151 * Revision 1.60 1996/06/04 10:18:12 adam
152 * Search/scan uses character mapping module.
154 * Revision 1.59 1996/05/14 15:47:07 adam
155 * Cleanup of various buffer size entities.
157 * Revision 1.58 1996/05/14 06:16:38 adam
158 * Compact use/set bytes used in search service.
160 * Revision 1.57 1996/05/13 14:23:04 adam
161 * Work on compaction of set/use bytes in dictionary.
163 * Revision 1.56 1996/05/09 09:54:42 adam
164 * Server supports maps from one logical attributes to a list of physical
166 * The extraction process doesn't make space consuming 'any' keys.
168 * Revision 1.55 1996/05/09 07:28:55 quinn
169 * Work towards phrases and multiple registers
171 * Revision 1.54 1996/05/01 13:46:35 adam
172 * First work on multiple records in one file.
173 * New option, -offset, to the "unread" command in the filter module.
175 * Revision 1.53 1996/04/26 12:09:43 adam
176 * Added a few comments.
178 * Revision 1.52 1996/04/25 13:27:57 adam
179 * Function recordExtract modified so that files with no keys (possibly empty)
182 * Revision 1.51 1996/03/19 11:08:42 adam
183 * Bug fix: Log preamble wasn't always turned off after recordExtract.
185 * Revision 1.50 1996/02/12 18:45:36 adam
186 * New fileVerboseFlag in record group control.
188 * Revision 1.49 1996/02/05 12:29:57 adam
189 * Logging reduced a bit.
190 * The remaining running time is estimated during register merge.
192 * Revision 1.48 1996/02/01 20:53:26 adam
193 * The temporary per-record keys are compacted a little, and duplication
194 * of the per-records keys are avoided when they are saved in the record
195 * information buffer.
197 * Revision 1.47 1996/01/17 14:57:48 adam
198 * Prototype changed for reader functions in extract/retrieve. File
199 * is identified by 'void *' instead of 'int.
201 * Revision 1.46 1995/12/15 14:57:16 adam
204 * Revision 1.45 1995/12/15 12:37:41 adam
205 * In addRecordKeyAny: Writes key only when attrSet != -1.
207 * Revision 1.44 1995/12/12 16:00:54 adam
208 * System call sync(2) used after update/commit.
209 * Locking (based on fcntl) uses F_EXLCK and F_SHLCK instead of F_WRLCK
212 * Revision 1.43 1995/12/11 09:12:46 adam
213 * The rec_get function returns NULL if record doesn't exist - will
214 * happen in the server if the result set records have been deleted since
215 * the creation of the set (i.e. the search).
216 * The server saves a result temporarily if it is 'volatile', i.e. the
217 * set is register dependent.
219 * Revision 1.42 1995/12/07 17:38:46 adam
220 * Work locking mechanisms for concurrent updates/commit.
222 * Revision 1.41 1995/12/06 16:06:42 adam
223 * Better diagnostics. Work on 'real' dictionary deletion.
225 * Revision 1.40 1995/12/05 16:57:40 adam
226 * More work on regular patterns.
228 * Revision 1.39 1995/12/05 13:20:18 adam
229 * Bug fix: file_read sometimes returned early EOF.
231 * Revision 1.38 1995/12/04 17:59:21 adam
232 * More work on regular expression conversion.
234 * Revision 1.37 1995/12/04 14:22:27 adam
235 * Extra arg to recType_byName.
236 * Started work on new regular expression parsed input to
237 * structured records.
239 * Revision 1.36 1995/11/30 08:34:29 adam
240 * Started work on commit facility.
241 * Changed a few malloc/free to xmalloc/xfree.
243 * Revision 1.35 1995/11/28 14:26:21 adam
244 * Bug fix: recordId with constant wasn't right.
245 * Bug fix: recordId dictionary entry wasn't deleted when needed.
247 * Revision 1.34 1995/11/28 09:09:38 adam
248 * Zebra config renamed.
249 * Use setting 'recordId' to identify record now.
250 * Bug fix in recindex.c: rec_release_blocks was invokeded even
251 * though the blocks were already released.
252 * File traversal properly deletes records when needed.
254 * Revision 1.33 1995/11/27 09:56:20 adam
255 * Record info elements better enumerated. Internal store of records.
257 * Revision 1.32 1995/11/25 10:24:05 adam
258 * More record fields - they are enumerated now.
259 * New options: flagStoreData flagStoreKey.
261 * Revision 1.31 1995/11/24 11:31:35 adam
262 * Commands add & del read filenames from stdin if source directory is
264 * Match criteria supports 'constant' strings.
266 * Revision 1.30 1995/11/22 17:19:16 adam
267 * Record management uses the bfile system.
269 * Revision 1.29 1995/11/21 15:01:14 adam
270 * New general match criteria implemented.
271 * New feature: document groups.
273 * Revision 1.28 1995/11/21 09:20:30 adam
274 * Yet more work on record match.
276 * Revision 1.27 1995/11/20 16:59:45 adam
277 * New update method: the 'old' keys are saved for each records.
279 * Revision 1.26 1995/11/20 11:56:24 adam
280 * Work on new traversal.
282 * Revision 1.25 1995/11/16 15:34:54 adam
283 * Uses new record management system in both indexer and server.
285 * Revision 1.24 1995/11/15 19:13:08 adam
286 * Work on record management.
288 * Revision 1.23 1995/10/27 14:00:10 adam
289 * Implemented detection of database availability.
291 * Revision 1.22 1995/10/17 18:02:07 adam
292 * New feature: databases. Implemented as prefix to words in dictionary.
294 * Revision 1.21 1995/10/10 12:24:38 adam
295 * Temporary sort files are compressed.
297 * Revision 1.20 1995/10/06 13:52:05 adam
298 * Bug fixes. Handler may abort further scanning.
300 * Revision 1.19 1995/10/04 12:55:16 adam
301 * Bug fix in ranked search. Use=Any keys inserted.
303 * Revision 1.18 1995/10/04 09:37:08 quinn
306 * Revision 1.17 1995/10/03 14:28:57 adam
307 * Buffered read in extract works.
309 * Revision 1.16 1995/10/03 14:28:45 adam
310 * Work on more effecient read handler in extract.
312 * Revision 1.15 1995/10/02 15:42:53 adam
313 * Extract uses file descriptors instead of FILE pointers.
315 * Revision 1.14 1995/10/02 15:29:13 adam
316 * More logging in file_extract.
318 * Revision 1.13 1995/09/29 14:01:39 adam
321 * Revision 1.12 1995/09/28 14:22:56 adam
322 * Sort uses smaller temporary files.
324 * Revision 1.11 1995/09/28 12:10:31 adam
325 * Bug fixes. Field prefix used in queries.
327 * Revision 1.10 1995/09/28 09:19:41 adam
328 * xfree/xmalloc used everywhere.
329 * Extract/retrieve method seems to work for text records.
331 * Revision 1.9 1995/09/27 12:22:28 adam
332 * More work on extract in record control.
333 * Field name is not in isam keys but in prefix in dictionary words.
335 * Revision 1.8 1995/09/14 07:48:22 adam
336 * Record control management.
338 * Revision 1.7 1995/09/11 13:09:32 adam
339 * More work on relevance feedback.
341 * Revision 1.6 1995/09/08 14:52:27 adam
342 * Minor changes. Dictionary is lower case now.
344 * Revision 1.5 1995/09/06 16:11:16 adam
345 * Option: only one word key per file.
347 * Revision 1.4 1995/09/05 15:28:39 adam
348 * More work on search engine.
350 * Revision 1.3 1995/09/04 12:33:41 adam
351 * Various cleanup. YAZ util used instead.
353 * Revision 1.2 1995/09/04 09:10:34 adam
354 * More work on index add/del/update.
355 * Merge sort implemented.
356 * Initial work on z39 server.
358 * Revision 1.1 1995/09/01 14:06:35 adam
359 * Split of work into more files.
383 #include "zebrasdr.h"
386 static Dict matchDict;
388 static Records records = NULL;
389 static SortIdx sortIdx = NULL;
391 static char **key_buf;
392 static size_t ptr_top;
394 static size_t key_buf_used;
395 static int key_file_no;
397 static int records_inserted = 0;
398 static int records_updated = 0;
399 static int records_deleted = 0;
400 static int records_processed = 0;
402 static ZebraExplainInfo zti = NULL;
404 static void logRecord (int showFlag)
408 if (showFlag || !(records_processed % 1000))
410 logf (LOG_LOG, "Records: %7d i/u/d %d/%d/%d",
411 records_processed, records_inserted, records_updated,
416 static int explain_extract (void *handle, Record drec, data1_node *n);
418 int key_open (struct recordGroup *rGroup, int mem)
420 BFiles bfs = rGroup->bfs;
421 int rw = rGroup->flagRw;
422 data1_handle dh = rGroup->dh;
423 char *recordCompression;
424 int record_compression = REC_COMPRESS_NONE;
426 mem = atoi(res_get_def (common_resource, "memMax", "4"))*1024*1024;
429 key_buf = (char **) xmalloc (mem);
430 ptr_top = mem/sizeof(char*);
436 if (!(matchDict = dict_open (bfs, GMATCH_DICT, 50, rw, 0)))
438 logf (LOG_FATAL, "dict_open fail of %s", GMATCH_DICT);
442 recordCompression = res_get_def (common_resource,
443 "recordCompression", "none");
444 if (!strcmp (recordCompression, "none"))
445 record_compression = REC_COMPRESS_NONE;
446 if (!strcmp (recordCompression, "bzip2"))
447 record_compression = REC_COMPRESS_BZIP2;
448 records = rec_open (bfs, rw, record_compression);
451 dict_close (matchDict);
454 zti = zebraExplain_open (records, dh, common_resource,
455 rw, rGroup, explain_extract);
458 rec_close (&records);
459 dict_close (matchDict);
462 sortIdx = sortIdx_open (bfs, 1);
473 void encode_key_init (struct encode_info *i)
480 char *encode_key_int (int d, char *bp)
489 else if (d <= 4194303)
491 *bp++ = 128 + (d>>16);
492 *bp++ = (d>>8) & 255;
497 *bp++ = 192 + (d>>24);
498 *bp++ = (d>>16) & 255;
499 *bp++ = (d>>8) & 255;
505 void encode_key_write (char *k, struct encode_info *i, FILE *outf)
510 while ((*bp++ = *k++))
512 memcpy (&key, k+1, sizeof(struct it_key));
513 bp = encode_key_int ( (key.sysno - i->sysno) * 2 + *k, bp);
514 if (i->sysno != key.sysno)
516 i->sysno = key.sysno;
519 else if (!i->seqno && !key.seqno && i->cmd == *k)
521 bp = encode_key_int (key.seqno - i->seqno, bp);
522 i->seqno = key.seqno;
524 if (fwrite (i->buf, bp - i->buf, 1, outf) != 1)
526 logf (LOG_FATAL|LOG_ERRNO, "fwrite");
534 static int key_y_len;
536 static int key_y_compare (const void *p1, const void *p2)
540 if ((r = key_compare (*(char**) p1 + key_y_len + 1,
541 *(char**) p2 + key_y_len + 1)))
543 return *(*(char**) p1 + key_y_len) - *(*(char**) p2 + key_y_len);
546 static int key_x_compare (const void *p1, const void *p2)
548 return strcmp (*(char**) p1, *(char**) p2);
552 void key_flush (void)
557 struct encode_info encode_info;
566 logf (LOG_LOG, "sorting section %d", key_file_no);
568 qsort (key_buf + ptr_top-ptr_i, ptr_i, sizeof(char*), key_qsort_compare);
569 getFnameTmp (common_resource, out_fname, key_file_no);
571 if (!(outf = fopen (out_fname, "wb")))
573 logf (LOG_FATAL|LOG_ERRNO, "fopen %s", out_fname);
576 logf (LOG_LOG, "writing section %d", key_file_no);
577 prevcp = cp = key_buf[ptr_top-ptr_i];
579 encode_key_init (&encode_info);
580 encode_key_write (cp, &encode_info, outf);
583 cp = key_buf[ptr_top-ptr_i];
584 if (strcmp (cp, prevcp))
586 encode_key_init (&encode_info);
587 encode_key_write (cp, &encode_info, outf);
591 encode_key_write (cp + strlen(cp), &encode_info, outf);
594 qsort (key_buf + ptr_top-ptr_i, ptr_i, sizeof(char*), key_x_compare);
595 getFnameTmp (out_fname, key_file_no);
597 if (!(outf = fopen (out_fname, "wb")))
599 logf (LOG_FATAL|LOG_ERRNO, "fopen %s", out_fname);
602 logf (LOG_LOG, "writing section %d", key_file_no);
604 prevcp = key_buf[ptr_top-i];
606 if (!--i || strcmp (prevcp, key_buf[ptr_top-i]))
608 key_y_len = strlen(prevcp)+1;
610 logf (LOG_LOG, "key_y_len: %2d %02x %02x %s",
611 key_y_len, prevcp[0], prevcp[1], 2+prevcp);
613 qsort (key_buf + ptr_top-ptr_i, ptr_i - i,
614 sizeof(char*), key_y_compare);
615 cp = key_buf[ptr_top-ptr_i];
617 encode_key_init (&encode_info);
618 encode_key_write (cp, &encode_info, outf);
621 cp = key_buf[ptr_top-ptr_i];
622 encode_key_write (cp+key_y_len, &encode_info, outf);
626 prevcp = key_buf[ptr_top-ptr_i];
631 logf (LOG_FATAL|LOG_ERRNO, "fclose %s", out_fname);
634 logf (LOG_LOG, "finished section %d", key_file_no);
639 int key_close (struct recordGroup *rGroup)
641 int rw = rGroup->flagRw;
643 zebraExplain_runNumberIncrement (zti, 1);
644 zebraExplain_close (zti, rw);
647 rec_close (&records);
648 dict_close (matchDict);
649 sortIdx_close (sortIdx);
655 static void wordInit (struct recExtractCtrl *p, RecWord *w)
657 w->zebra_maps = p->zebra_maps;
658 w->seqnos = p->seqno;
659 w->attrSet = VAL_BIB1;
665 static struct sortKey {
670 struct sortKey *next;
673 static struct recKeys {
682 static void addIndexString (RecWord *p, const char *string, int length)
685 unsigned char attrSet;
686 unsigned short attrUse;
689 int *pseqno = &p->seqnos[p->reg_type];
691 if (reckeys.buf_used+1024 > reckeys.buf_max)
695 b = (char *) xmalloc (reckeys.buf_max += 128000);
696 if (reckeys.buf_used > 0)
697 memcpy (b, reckeys.buf, reckeys.buf_used);
701 dst = reckeys.buf + reckeys.buf_used;
703 attrSet = p->attrSet;
704 if (reckeys.buf_used > 0 && reckeys.prevAttrSet == attrSet)
707 reckeys.prevAttrSet = attrSet;
708 attrUse = p->attrUse;
709 if (reckeys.buf_used > 0 && reckeys.prevAttrUse == attrUse)
712 reckeys.prevAttrUse = attrUse;
714 diff = 1 + *pseqno - reckeys.prevSeqNo;
715 if (diff >= 1 && diff <= 15)
720 reckeys.prevSeqNo = *pseqno;
726 memcpy (dst, &attrSet, sizeof(attrSet));
727 dst += sizeof(attrSet);
731 memcpy (dst, &attrUse, sizeof(attrUse));
732 dst += sizeof(attrUse);
734 *dst++ = p->reg_type;
735 memcpy (dst, string, length);
741 memcpy (dst, pseqno, sizeof(*pseqno));
742 dst += sizeof(*pseqno);
744 reckeys.buf_used = dst - reckeys.buf;
749 static void addSortString (RecWord *p, const char *string, int length)
753 for (sk = sortKeys; sk; sk = sk->next)
754 if (sk->attrSet == p->attrSet && sk->attrUse == p->attrUse)
757 sk = (struct sortKey *) xmalloc (sizeof(*sk));
761 sk->string = (char *) xmalloc (length);
763 memcpy (sk->string, string, length);
765 sk->attrSet = p->attrSet;
766 sk->attrUse = p->attrUse;
769 static void addString (RecWord *p, const char *string, int length)
772 if (zebra_maps_is_sort (p->zebra_maps, p->reg_type))
773 addSortString (p, string, length);
775 addIndexString (p, string, length);
778 static void addIncompleteField (RecWord *p)
780 const char *b = p->string;
781 int remain = p->length;
782 const char **map = 0;
785 map = zebra_maps_input(p->zebra_maps, p->reg_type, &b, remain);
789 char buf[IT_MAX_WORD+1];
793 while (map && *map && **map == *CHR_SPACE)
795 remain = p->length - (b - p->string);
797 map = zebra_maps_input(p->zebra_maps, p->reg_type, &b, remain);
804 while (map && *map && **map != *CHR_SPACE)
806 const char *cp = *map;
808 while (i < IT_MAX_WORD && *cp)
810 remain = p->length - (b - p->string);
812 map = zebra_maps_input(p->zebra_maps, p->reg_type, &b, remain);
818 addString (p, buf, i);
820 (p->seqnos[p->reg_type])++; /* to separate this from next one */
823 static void addCompleteField (RecWord *p)
825 const char *b = p->string;
826 char buf[IT_MAX_WORD+1];
827 const char **map = 0;
828 int i = 0, remain = p->length;
831 map = zebra_maps_input (p->zebra_maps, p->reg_type, &b, remain);
833 while (remain > 0 && i < IT_MAX_WORD)
835 while (map && *map && **map == *CHR_SPACE)
837 remain = p->length - (b - p->string);
839 map = zebra_maps_input(p->zebra_maps, p->reg_type, &b, remain);
846 if (i && i < IT_MAX_WORD)
847 buf[i++] = *CHR_SPACE;
848 while (map && *map && **map != *CHR_SPACE)
850 const char *cp = *map;
852 if (i >= IT_MAX_WORD)
854 while (i < IT_MAX_WORD && *cp)
856 remain = p->length - (b - p->string);
858 map = zebra_maps_input (p->zebra_maps, p->reg_type, &b,
866 addString (p, buf, i);
869 static void addRecordKey (RecWord *p)
872 if ((wrbuf = zebra_replace(p->zebra_maps, p->reg_type, 0,
873 p->string, p->length)))
875 p->string = wrbuf_buf(wrbuf);
876 p->length = wrbuf_len(wrbuf);
878 if (zebra_maps_is_complete (p->zebra_maps, p->reg_type))
879 addCompleteField (p);
881 addIncompleteField(p);
884 static void flushSortKeys (SYSNO sysno, int cmd)
886 struct sortKey *sk = sortKeys;
888 sortIdx_sysno (sortIdx, sysno);
891 struct sortKey *sk_next = sk->next;
892 sortIdx_type (sortIdx, sk->attrUse);
893 sortIdx_add (sortIdx, sk->string, sk->length);
901 static void flushRecordKeys (SYSNO sysno, int cmd, struct recKeys *reckeys)
903 unsigned char attrSet = (unsigned char) -1;
904 unsigned short attrUse = (unsigned short) -1;
908 zebraExplain_recordCountIncrement (zti, cmd ? 1 : -1);
909 while (off < reckeys->buf_used)
911 const char *src = reckeys->buf + off;
919 memcpy (&attrSet, src, sizeof(attrSet));
920 src += sizeof(attrSet);
924 memcpy (&attrUse, src, sizeof(attrUse));
925 src += sizeof(attrUse);
927 if (key_buf_used + 1024 > (ptr_top-ptr_i)*sizeof(char*))
930 key_buf[ptr_top-ptr_i] = (char*)key_buf + key_buf_used;
932 ch = zebraExplain_lookupSU (zti, attrSet, attrUse);
934 ch = zebraExplain_addSU (zti, attrSet, attrUse);
936 key_buf_used += key_SU_code (ch, ((char*)key_buf) + key_buf_used);
939 ((char*)key_buf) [key_buf_used++] = *src++;
941 ((char*)key_buf) [key_buf_used++] = '\0';
942 ((char*) key_buf)[key_buf_used++] = cmd;
945 seqno += ((lead>>2) & 15)-1;
948 memcpy (&seqno, src, sizeof(seqno));
949 src += sizeof(seqno);
953 memcpy ((char*)key_buf + key_buf_used, &key, sizeof(key));
954 key_buf_used += sizeof(key);
955 off = src - reckeys->buf;
957 assert (off == reckeys->buf_used);
960 static const char **searchRecordKey (struct recKeys *reckeys,
961 int attrSetS, int attrUseS)
963 static const char *ws[32];
971 for (i = 0; i<32; i++)
974 while (off < reckeys->buf_used)
977 const char *src = reckeys->buf + off;
985 memcpy (&attrSet, src, sizeof(attrSet));
986 src += sizeof(attrSet);
990 memcpy (&attrUse, src, sizeof(attrUse));
991 src += sizeof(attrUse);
997 seqno += ((lead>>2) & 15)-1;
1000 memcpy (&seqno, src, sizeof(seqno));
1001 src += sizeof(seqno);
1003 if (attrUseS == attrUse && attrSetS == attrSet)
1010 woff = seqno - startSeq;
1011 if (woff >= 0 && woff < 31)
1015 off = src - reckeys->buf;
1017 assert (off == reckeys->buf_used);
1021 struct file_read_info {
1022 off_t file_max; /* maximum offset so far */
1023 off_t file_offset; /* current offset */
1024 off_t file_moffset; /* offset of rec/rec boundary */
1031 static struct file_read_info *file_read_start (int fd)
1033 struct file_read_info *fi = (struct file_read_info *)
1034 xmalloc (sizeof(*fi));
1038 fi->file_moffset = 0;
1044 static void file_read_stop (struct file_read_info *fi)
1049 static off_t file_seek (void *handle, off_t offset)
1051 struct file_read_info *p = (struct file_read_info *) handle;
1052 p->file_offset = offset;
1055 return lseek (p->fd, offset, SEEK_SET);
1058 static off_t file_tell (void *handle)
1060 struct file_read_info *p = (struct file_read_info *) handle;
1061 return p->file_offset;
1064 static int file_read (void *handle, char *buf, size_t count)
1066 struct file_read_info *p = (struct file_read_info *) handle;
1072 if (r > p->sdrmax - p->file_offset)
1073 r = p->sdrmax - p->file_offset;
1075 memcpy (buf, p->sdrbuf + p->file_offset, r);
1078 r = read (fd, buf, count);
1081 p->file_offset += r;
1082 if (p->file_offset > p->file_max)
1083 p->file_max = p->file_offset;
1088 static void file_begin (void *handle)
1090 struct file_read_info *p = (struct file_read_info *) handle;
1092 p->file_offset = p->file_moffset;
1093 if (!p->sdrbuf && p->file_moffset)
1094 lseek (p->fd, p->file_moffset, SEEK_SET);
1098 static void file_end (void *handle, off_t offset)
1100 struct file_read_info *p = (struct file_read_info *) handle;
1102 assert (p->file_more == 0);
1104 p->file_moffset = offset;
1107 static char *fileMatchStr (struct recKeys *reckeys, struct recordGroup *rGroup,
1108 const char *fname, const char *spec)
1110 static char dstBuf[2048];
1112 const char *s = spec;
1113 static const char **w;
1117 while (*s == ' ' || *s == '\t')
1123 char attset_str[64], attname_str[64];
1124 data1_attset *attset;
1127 int attSet = 1, attUse = 1;
1131 for (i = 0; *s && *s != ',' && *s != ')'; s++)
1133 attset_str[i++] = *s;
1134 attset_str[i] = '\0';
1139 for (i = 0; *s && *s != ')'; s++)
1141 attname_str[i++] = *s;
1142 attname_str[i] = '\0';
1145 if ((attset = data1_get_attset (rGroup->dh, attset_str)))
1148 attSet = attset->reference;
1149 att = data1_getattbyname(rGroup->dh, attset, attname_str);
1151 attUse = att->value;
1153 attUse = atoi (attname_str);
1155 w = searchRecordKey (reckeys, attSet, attUse);
1160 for (i = 0; i<32; i++)
1165 logf (LOG_WARN, "Missing ) in match criteria %s in group %s",
1166 spec, rGroup->groupName ? rGroup->groupName : "none");
1171 for (i = 0; i<32; i++)
1172 if (matchFlag[i] && w[i])
1180 dst += strlen(w[i]);
1184 logf (LOG_WARN, "Record didn't contain match"
1185 " fields in (%s,%s)", attset_str, attname_str);
1193 const char *spec_src = NULL;
1194 const char *s1 = ++s;
1195 while (*s1 && *s1 != ' ' && *s1 != '\t')
1201 memcpy (special, s, spec_len);
1202 special[spec_len] = '\0';
1205 if (!strcmp (special, "group"))
1206 spec_src = rGroup->groupName;
1207 else if (!strcmp (special, "database"))
1208 spec_src = rGroup->databaseName;
1209 else if (!strcmp (special, "filename"))
1211 else if (!strcmp (special, "type"))
1212 spec_src = rGroup->recordType;
1217 strcpy (dst, spec_src);
1218 dst += strlen (spec_src);
1221 else if (*s == '\"' || *s == '\'')
1223 int stopMarker = *s++;
1227 while (*s && *s != stopMarker)
1230 tmpString[i++] = *s++;
1234 tmpString[i] = '\0';
1235 strcpy (dst, tmpString);
1236 dst += strlen (tmpString);
1240 logf (LOG_WARN, "Syntax error in match criteria %s in group %s",
1241 spec, rGroup->groupName ? rGroup->groupName : "none");
1248 logf (LOG_WARN, "No match criteria for record %s in group %s",
1249 fname, rGroup->groupName ? rGroup->groupName : "none");
1256 struct recordLogInfo {
1259 struct recordGroup *rGroup;
1262 static void recordLogPreamble (int level, const char *msg, void *info)
1264 struct recordLogInfo *p = (struct recordLogInfo *) info;
1265 FILE *outf = log_file ();
1267 if (level & LOG_LOG)
1269 fprintf (outf, "File %s, offset %d, type %s\n",
1270 p->rGroup->recordType, p->recordOffset, p->fname);
1271 log_event_start (NULL, NULL);
1274 void addSchema (struct recExtractCtrl *p, Odr_oid *oid)
1276 zebraExplain_addSchema (zti, oid);
1279 static int recordExtract (SYSNO *sysno, const char *fname,
1280 struct recordGroup *rGroup, int deleteFlag,
1281 struct file_read_info *fi,
1282 RecType recType, char *subType, void *clientData)
1284 RecordAttr *recordAttr;
1289 struct recordLogInfo logInfo;
1290 off_t recordOffset = 0;
1294 struct recExtractCtrl extractCtrl;
1296 /* we are going to read from a file, so prepare the extraction */
1299 reckeys.buf_used = 0;
1300 reckeys.prevAttrUse = -1;
1301 reckeys.prevAttrSet = -1;
1302 reckeys.prevSeqNo = 0;
1304 recordOffset = fi->file_moffset;
1305 extractCtrl.offset = fi->file_moffset;
1306 extractCtrl.readf = file_read;
1307 extractCtrl.seekf = file_seek;
1308 extractCtrl.tellf = file_tell;
1309 extractCtrl.endf = file_end;
1310 extractCtrl.fh = fi;
1311 extractCtrl.subType = subType;
1312 extractCtrl.init = wordInit;
1313 extractCtrl.tokenAdd = addRecordKey;
1314 extractCtrl.schemaAdd = addSchema;
1315 extractCtrl.dh = rGroup->dh;
1316 for (i = 0; i<256; i++)
1318 if (zebra_maps_is_positioned(rGroup->zebra_maps, i))
1319 extractCtrl.seqno[i] = 1;
1321 extractCtrl.seqno[i] = 0;
1323 extractCtrl.zebra_maps = rGroup->zebra_maps;
1324 extractCtrl.flagShowRecords = !rGroup->flagRw;
1326 if (!rGroup->flagRw)
1327 printf ("File: %s %ld\n", fname, (long) recordOffset);
1329 logInfo.fname = fname;
1330 logInfo.recordOffset = recordOffset;
1331 logInfo.rGroup = rGroup;
1332 log_event_start (recordLogPreamble, &logInfo);
1334 r = (*recType->extract)(clientData, &extractCtrl);
1336 log_event_start (NULL, NULL);
1338 if (r == RECCTRL_EXTRACT_EOF)
1340 else if (r == RECCTRL_EXTRACT_ERROR)
1342 /* error occured during extraction ... */
1343 if (rGroup->flagRw &&
1344 records_processed < rGroup->fileVerboseLimit)
1346 logf (LOG_WARN, "fail %s %s %ld", rGroup->recordType,
1347 fname, (long) recordOffset);
1351 if (reckeys.buf_used == 0)
1353 /* the extraction process returned no information - the record
1354 is probably empty - unless flagShowRecords is in use */
1355 if (!rGroup->flagRw)
1357 logf (LOG_WARN, "No keys generated for file %s", fname);
1358 logf (LOG_WARN, " The file is probably empty");
1363 /* perform match if sysno not known and if match criteria is specified */
1370 if (rGroup->recordId && *rGroup->recordId)
1374 matchStr = fileMatchStr (&reckeys, rGroup, fname,
1378 rinfo = dict_lookup (matchDict, matchStr);
1380 memcpy (sysno, rinfo+1, sizeof(*sysno));
1384 logf (LOG_WARN, "Bad match criteria");
1395 logf (LOG_LOG, "delete %s %s %ld", rGroup->recordType,
1396 fname, (long) recordOffset);
1397 logf (LOG_WARN, "cannot delete record above (seems new)");
1400 if (records_processed < rGroup->fileVerboseLimit)
1401 logf (LOG_LOG, "add %s %s %ld", rGroup->recordType,
1402 fname, (long) recordOffset);
1403 rec = rec_new (records);
1405 *sysno = rec->sysno;
1407 recordAttr = rec_init_attr (zti, rec);
1411 dict_insert (matchDict, matchStr, sizeof(*sysno), sysno);
1413 flushRecordKeys (*sysno, 1, &reckeys);
1414 flushSortKeys (*sysno, 1);
1420 /* record already exists */
1421 struct recKeys delkeys;
1423 rec = rec_get (records, *sysno);
1426 recordAttr = rec_init_attr (zti, rec);
1428 if (recordAttr->runNumber == zebraExplain_runNumberIncrement (zti, 0))
1430 logf (LOG_LOG, "skipped %s %s %ld", rGroup->recordType,
1431 fname, (long) recordOffset);
1436 delkeys.buf_used = rec->size[recInfo_delKeys];
1437 delkeys.buf = rec->info[recInfo_delKeys];
1438 flushSortKeys (*sysno, 0);
1439 flushRecordKeys (*sysno, 0, &delkeys);
1442 /* record going to be deleted */
1443 if (!delkeys.buf_used)
1445 logf (LOG_LOG, "delete %s %s %ld", rGroup->recordType,
1446 fname, (long) recordOffset);
1447 logf (LOG_WARN, "cannot delete file above, storeKeys false");
1451 if (records_processed < rGroup->fileVerboseLimit)
1452 logf (LOG_LOG, "delete %s %s %ld", rGroup->recordType,
1453 fname, (long) recordOffset);
1456 dict_delete (matchDict, matchStr);
1457 rec_del (records, &rec);
1465 /* record going to be updated */
1466 if (!delkeys.buf_used)
1468 logf (LOG_LOG, "update %s %s %ld", rGroup->recordType,
1469 fname, (long) recordOffset);
1470 logf (LOG_WARN, "cannot update file above, storeKeys false");
1474 if (records_processed < rGroup->fileVerboseLimit)
1475 logf (LOG_LOG, "update %s %s %ld", rGroup->recordType,
1476 fname, (long) recordOffset);
1477 flushRecordKeys (*sysno, 1, &reckeys);
1482 /* update file type */
1483 xfree (rec->info[recInfo_fileType]);
1484 rec->info[recInfo_fileType] =
1485 rec_strdup (rGroup->recordType, &rec->size[recInfo_fileType]);
1487 /* update filename */
1488 xfree (rec->info[recInfo_filename]);
1489 rec->info[recInfo_filename] =
1490 rec_strdup (fname, &rec->size[recInfo_filename]);
1492 /* update delete keys */
1493 xfree (rec->info[recInfo_delKeys]);
1494 if (reckeys.buf_used > 0 && rGroup->flagStoreKeys == 1)
1497 rec->size[recInfo_delKeys] = reckeys.buf_used;
1498 rec->info[recInfo_delKeys] = reckeys.buf;
1500 reckeys.buf_max = 0;
1502 rec->info[recInfo_delKeys] = xmalloc (reckeys.buf_used);
1503 rec->size[recInfo_delKeys] = reckeys.buf_used;
1504 memcpy (rec->info[recInfo_delKeys], reckeys.buf,
1505 rec->size[recInfo_delKeys]);
1510 rec->info[recInfo_delKeys] = NULL;
1511 rec->size[recInfo_delKeys] = 0;
1514 /* save file size of original record */
1515 zebraExplain_recordBytesIncrement (zti, - recordAttr->recordSize);
1516 recordAttr->recordSize = fi->file_moffset - recordOffset;
1517 if (!recordAttr->recordSize)
1518 recordAttr->recordSize = fi->file_max - recordOffset;
1519 zebraExplain_recordBytesIncrement (zti, recordAttr->recordSize);
1521 /* set run-number for this record */
1522 recordAttr->runNumber = zebraExplain_runNumberIncrement (zti, 0);
1524 /* update store data */
1525 xfree (rec->info[recInfo_storeData]);
1526 if (rGroup->flagStoreData == 1)
1528 rec->size[recInfo_storeData] = recordAttr->recordSize;
1529 rec->info[recInfo_storeData] = (char *)
1530 xmalloc (recordAttr->recordSize);
1531 if (lseek (fi->fd, recordOffset, SEEK_SET) < 0)
1533 logf (LOG_ERRNO|LOG_FATAL, "seek to %ld in %s",
1534 (long) recordOffset, fname);
1537 if (read (fi->fd, rec->info[recInfo_storeData], recordAttr->recordSize)
1538 < recordAttr->recordSize)
1540 logf (LOG_ERRNO|LOG_FATAL, "read %d bytes of %s",
1541 recordAttr->recordSize, fname);
1547 rec->info[recInfo_storeData] = NULL;
1548 rec->size[recInfo_storeData] = 0;
1550 /* update database name */
1551 xfree (rec->info[recInfo_databaseName]);
1552 rec->info[recInfo_databaseName] =
1553 rec_strdup (rGroup->databaseName, &rec->size[recInfo_databaseName]);
1556 recordAttr->recordOffset = recordOffset;
1558 /* commit this record */
1559 rec_put (records, &rec);
1564 int fileExtract (SYSNO *sysno, const char *fname,
1565 const struct recordGroup *rGroupP, int deleteFlag)
1573 struct recordGroup rGroupM;
1574 struct recordGroup *rGroup = &rGroupM;
1575 struct file_read_info *fi;
1578 memcpy (rGroup, rGroupP, sizeof(*rGroupP));
1580 if (!rGroup->groupName || !*rGroup->groupName)
1583 sprintf (gprefix, "%s.", rGroup->groupName);
1585 logf (LOG_DEBUG, "fileExtract %s", fname);
1587 /* determine file extension */
1589 for (i = strlen(fname); --i >= 0; )
1590 if (fname[i] == '/')
1592 else if (fname[i] == '.')
1594 strcpy (ext, fname+i+1);
1597 /* determine file type - depending on extension */
1598 if (!rGroup->recordType)
1600 sprintf (ext_res, "%srecordType.%s", gprefix, ext);
1601 if (!(rGroup->recordType = res_get (common_resource, ext_res)))
1603 sprintf (ext_res, "%srecordType", gprefix);
1604 rGroup->recordType = res_get (common_resource, ext_res);
1607 if (!rGroup->recordType)
1609 if (records_processed < rGroup->fileVerboseLimit)
1610 logf (LOG_LOG, "? %s", fname);
1613 if (!*rGroup->recordType)
1616 recType_byName (rGroup->recTypes, rGroup->recordType, subType,
1619 logf (LOG_WARN, "No such record type: %s", rGroup->recordType);
1623 /* determine match criteria */
1624 if (!rGroup->recordId)
1626 sprintf (ext_res, "%srecordId.%s", gprefix, ext);
1627 rGroup->recordId = res_get (common_resource, ext_res);
1630 /* determine database name */
1631 if (!rGroup->databaseName)
1633 sprintf (ext_res, "%sdatabase.%s", gprefix, ext);
1634 if (!(rGroup->databaseName = res_get (common_resource, ext_res)))
1636 sprintf (ext_res, "%sdatabase", gprefix);
1637 rGroup->databaseName = res_get (common_resource, ext_res);
1640 if (!rGroup->databaseName)
1641 rGroup->databaseName = "Default";
1643 /* determine if explain database */
1645 sprintf (ext_res, "%sexplainDatabase", gprefix);
1646 rGroup->explainDatabase =
1647 atoi (res_get_def (common_resource, ext_res, "0"));
1649 /* announce database */
1650 if (zebraExplain_curDatabase (zti, rGroup->databaseName))
1652 if (zebraExplain_newDatabase (zti, rGroup->databaseName,
1653 rGroup->explainDatabase))
1657 if (rGroup->flagStoreData == -1)
1660 sprintf (ext_res, "%sstoreData.%s", gprefix, ext);
1661 if (!(sval = res_get (common_resource, ext_res)))
1663 sprintf (ext_res, "%sstoreData", gprefix);
1664 sval = res_get (common_resource, ext_res);
1667 rGroup->flagStoreData = atoi (sval);
1669 if (rGroup->flagStoreData == -1)
1670 rGroup->flagStoreData = 0;
1672 if (rGroup->flagStoreKeys == -1)
1676 sprintf (ext_res, "%sstoreKeys.%s", gprefix, ext);
1677 if (!(sval = res_get (common_resource, ext_res)))
1679 sprintf (ext_res, "%sstoreKeys", gprefix);
1680 sval = res_get (common_resource, ext_res);
1683 rGroup->flagStoreKeys = atoi (sval);
1685 if (rGroup->flagStoreKeys == -1)
1686 rGroup->flagStoreKeys = 0;
1688 if (sysno && deleteFlag)
1692 if ((fd = open (fname, O_BINARY|O_RDONLY)) == -1)
1694 logf (LOG_WARN|LOG_ERRNO, "open %s", fname);
1698 fi = file_read_start (fd);
1702 r = recordExtract (sysno, fname, rGroup, deleteFlag, fi,
1703 recType, subType, clientData);
1704 } while (r && !sysno && fi->file_more);
1705 file_read_stop (fi);
1711 static int explain_extract (void *handle, Record rec, data1_node *n)
1713 struct recordGroup *rGroup = (struct recordGroup*) handle;
1714 struct recExtractCtrl extractCtrl;
1717 if (zebraExplain_curDatabase (zti, rec->info[recInfo_databaseName]))
1719 if (zebraExplain_newDatabase (zti, rec->info[recInfo_databaseName], 0))
1723 reckeys.buf_used = 0;
1724 reckeys.prevAttrUse = -1;
1725 reckeys.prevAttrSet = -1;
1726 reckeys.prevSeqNo = 0;
1728 extractCtrl.init = wordInit;
1729 extractCtrl.tokenAdd = addRecordKey;
1730 extractCtrl.schemaAdd = addSchema;
1731 extractCtrl.dh = rGroup->dh;
1732 for (i = 0; i<256; i++)
1733 extractCtrl.seqno[i] = 0;
1734 extractCtrl.zebra_maps = rGroup->zebra_maps;
1735 extractCtrl.flagShowRecords = !rGroup->flagRw;
1737 grs_extract_tree(&extractCtrl, n);
1739 logf (LOG_DEBUG, "flush explain record, sysno=%d", rec->sysno);
1741 if (rec->size[recInfo_delKeys])
1743 struct recKeys delkeys;
1745 delkeys.buf_used = rec->size[recInfo_delKeys];
1746 delkeys.buf = rec->info[recInfo_delKeys];
1747 flushSortKeys (rec->sysno, 0);
1748 flushRecordKeys (rec->sysno, 0, &delkeys);
1750 flushRecordKeys (rec->sysno, 1, &reckeys);
1751 flushSortKeys (rec->sysno, 1);
1753 xfree (rec->info[recInfo_delKeys]);
1754 rec->size[recInfo_delKeys] = reckeys.buf_used;
1755 rec->info[recInfo_delKeys] = reckeys.buf;
1757 reckeys.buf_max = 0;