2 * Copyright (C) 1994-1999, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.98 1999-09-07 07:19:21 adam
8 * Work on character mapping. Implemented replace rules.
10 * Revision 1.97 1999/07/06 12:28:04 adam
11 * Updated record index structure. Format includes version ID. Compression
12 * algorithm ID is stored for each record block.
14 * Revision 1.96 1999/05/26 07:49:13 adam
17 * Revision 1.95 1999/05/21 12:00:17 adam
18 * Better diagnostics for extraction process.
20 * Revision 1.94 1999/05/20 12:57:18 adam
21 * Implemented TCL filter. Updated recctrl system.
23 * Revision 1.93 1999/05/15 14:36:38 adam
24 * Updated dictionary. Implemented "compression" of dictionary.
26 * Revision 1.92 1999/03/09 16:27:49 adam
27 * More work on SDRKit integration.
29 * Revision 1.91 1999/02/12 13:29:22 adam
30 * Implemented position-flag for registers.
32 * Revision 1.90 1999/02/02 14:50:52 adam
33 * Updated WIN32 code specific sections. Changed header.
35 * Revision 1.89 1998/10/28 10:54:38 adam
38 * Revision 1.88 1998/10/16 08:14:29 adam
39 * Updated record control system.
41 * Revision 1.87 1998/10/15 13:10:33 adam
42 * Fixed bug in Zebra that caused it to stop indexing when empty
45 * Revision 1.86 1998/10/13 20:33:53 adam
46 * Fixed one log message and change use ordinal to be an unsigned char.
48 * Revision 1.85 1998/09/22 10:03:41 adam
49 * Changed result sets to be persistent in the sense that they can
50 * be re-searched if needed.
51 * Fixed memory leak in rsm_or.
53 * Revision 1.84 1998/06/11 15:42:22 adam
54 * Changed the way use attributes are specified in the recordId
57 * Revision 1.83 1998/06/08 14:43:10 adam
58 * Added suport for EXPLAIN Proxy servers - added settings databasePath
59 * and explainDatabase to facilitate this. Increased maximum number
60 * of databases and attributes in one register.
62 * Revision 1.82 1998/05/20 10:12:15 adam
63 * Implemented automatic EXPLAIN database maintenance.
64 * Modified Zebra to work with ASN.1 compiled version of YAZ.
66 * Revision 1.81 1998/03/11 11:19:04 adam
67 * Changed the way sequence numbers are generated.
69 * Revision 1.80 1998/03/05 08:45:11 adam
70 * New result set model and modular ranking system. Moved towards
71 * descent server API. System information stored as "SGML" records.
73 * Revision 1.79 1998/02/17 10:32:52 adam
74 * Fixed bug: binary files weren't opened with flag b on NT.
76 * Revision 1.78 1998/02/10 12:03:05 adam
79 * Revision 1.77 1998/01/12 15:04:08 adam
80 * The test option (-s) only uses read-lock (and not write lock).
82 * Revision 1.76 1997/10/27 14:33:04 adam
83 * Moved towards generic character mapping depending on "structure"
84 * field in abstract syntax file. Fixed a few memory leaks. Fixed
85 * bug with negative integers when doing searches with relational
88 * Revision 1.75 1997/09/17 12:19:12 adam
89 * Zebra version corresponds to YAZ version 1.4.
90 * Changed Zebra server so that it doesn't depend on global common_resource.
92 * Revision 1.74 1997/09/09 13:38:06 adam
93 * Partial port to WIN95/NT.
95 * Revision 1.73 1997/09/04 13:57:20 adam
96 * New file extract/retrieve method tellf (added).
97 * Added O_BINARY for open calls.
99 * Revision 1.72 1997/07/15 16:32:29 adam
100 * Bug fix: Match handler didn't terminate the resulting string!
102 * Revision 1.71 1997/07/15 16:28:41 adam
103 * Bug fix: storeData didn't work with files with multiple records.
104 * Bug fix: fixed memory management with records; not really well
107 * Revision 1.70 1997/07/01 13:00:42 adam
108 * Bug fix in routine searchRecordKey: uninitialized variables.
110 * Revision 1.69 1997/04/29 09:26:03 adam
111 * Bug fix: generic recordId handling didn't work for compressed internal
114 * Revision 1.68 1997/02/12 20:39:45 adam
115 * Implemented options -f <n> that limits the log to the first <n>
117 * Changed some log messages also.
119 * Revision 1.67 1996/11/15 15:02:14 adam
120 * Minor changes regarding logging.
122 * Revision 1.66 1996/11/14 09:52:21 adam
123 * Strings in record keys bound by IT_MAX_WORD.
125 * Revision 1.65 1996/11/14 08:57:56 adam
126 * Reduction of storeKeys area.
128 * Revision 1.64 1996/11/08 11:10:16 adam
129 * Buffers used during file match got bigger.
130 * Compressed ISAM support everywhere.
131 * Bug fixes regarding masking characters in queries.
132 * Redesigned Regexp-2 queries.
134 * Revision 1.63 1996/10/29 14:09:39 adam
135 * Use of cisam system - enabled if setting isamc is 1.
137 * Revision 1.62 1996/10/11 10:57:01 adam
138 * New module recctrl. Used to manage records (extract/retrieval).
139 * Several files have been moved to the recctrl sub directory.
141 * Revision 1.61 1996/06/06 12:08:37 quinn
142 * Added showRecord function
144 * Revision 1.60 1996/06/04 10:18:12 adam
145 * Search/scan uses character mapping module.
147 * Revision 1.59 1996/05/14 15:47:07 adam
148 * Cleanup of various buffer size entities.
150 * Revision 1.58 1996/05/14 06:16:38 adam
151 * Compact use/set bytes used in search service.
153 * Revision 1.57 1996/05/13 14:23:04 adam
154 * Work on compaction of set/use bytes in dictionary.
156 * Revision 1.56 1996/05/09 09:54:42 adam
157 * Server supports maps from one logical attributes to a list of physical
159 * The extraction process doesn't make space consuming 'any' keys.
161 * Revision 1.55 1996/05/09 07:28:55 quinn
162 * Work towards phrases and multiple registers
164 * Revision 1.54 1996/05/01 13:46:35 adam
165 * First work on multiple records in one file.
166 * New option, -offset, to the "unread" command in the filter module.
168 * Revision 1.53 1996/04/26 12:09:43 adam
169 * Added a few comments.
171 * Revision 1.52 1996/04/25 13:27:57 adam
172 * Function recordExtract modified so that files with no keys (possibly empty)
175 * Revision 1.51 1996/03/19 11:08:42 adam
176 * Bug fix: Log preamble wasn't always turned off after recordExtract.
178 * Revision 1.50 1996/02/12 18:45:36 adam
179 * New fileVerboseFlag in record group control.
181 * Revision 1.49 1996/02/05 12:29:57 adam
182 * Logging reduced a bit.
183 * The remaining running time is estimated during register merge.
185 * Revision 1.48 1996/02/01 20:53:26 adam
186 * The temporary per-record keys are compacted a little, and duplication
187 * of the per-records keys are avoided when they are saved in the record
188 * information buffer.
190 * Revision 1.47 1996/01/17 14:57:48 adam
191 * Prototype changed for reader functions in extract/retrieve. File
192 * is identified by 'void *' instead of 'int.
194 * Revision 1.46 1995/12/15 14:57:16 adam
197 * Revision 1.45 1995/12/15 12:37:41 adam
198 * In addRecordKeyAny: Writes key only when attrSet != -1.
200 * Revision 1.44 1995/12/12 16:00:54 adam
201 * System call sync(2) used after update/commit.
202 * Locking (based on fcntl) uses F_EXLCK and F_SHLCK instead of F_WRLCK
205 * Revision 1.43 1995/12/11 09:12:46 adam
206 * The rec_get function returns NULL if record doesn't exist - will
207 * happen in the server if the result set records have been deleted since
208 * the creation of the set (i.e. the search).
209 * The server saves a result temporarily if it is 'volatile', i.e. the
210 * set is register dependent.
212 * Revision 1.42 1995/12/07 17:38:46 adam
213 * Work locking mechanisms for concurrent updates/commit.
215 * Revision 1.41 1995/12/06 16:06:42 adam
216 * Better diagnostics. Work on 'real' dictionary deletion.
218 * Revision 1.40 1995/12/05 16:57:40 adam
219 * More work on regular patterns.
221 * Revision 1.39 1995/12/05 13:20:18 adam
222 * Bug fix: file_read sometimes returned early EOF.
224 * Revision 1.38 1995/12/04 17:59:21 adam
225 * More work on regular expression conversion.
227 * Revision 1.37 1995/12/04 14:22:27 adam
228 * Extra arg to recType_byName.
229 * Started work on new regular expression parsed input to
230 * structured records.
232 * Revision 1.36 1995/11/30 08:34:29 adam
233 * Started work on commit facility.
234 * Changed a few malloc/free to xmalloc/xfree.
236 * Revision 1.35 1995/11/28 14:26:21 adam
237 * Bug fix: recordId with constant wasn't right.
238 * Bug fix: recordId dictionary entry wasn't deleted when needed.
240 * Revision 1.34 1995/11/28 09:09:38 adam
241 * Zebra config renamed.
242 * Use setting 'recordId' to identify record now.
243 * Bug fix in recindex.c: rec_release_blocks was invokeded even
244 * though the blocks were already released.
245 * File traversal properly deletes records when needed.
247 * Revision 1.33 1995/11/27 09:56:20 adam
248 * Record info elements better enumerated. Internal store of records.
250 * Revision 1.32 1995/11/25 10:24:05 adam
251 * More record fields - they are enumerated now.
252 * New options: flagStoreData flagStoreKey.
254 * Revision 1.31 1995/11/24 11:31:35 adam
255 * Commands add & del read filenames from stdin if source directory is
257 * Match criteria supports 'constant' strings.
259 * Revision 1.30 1995/11/22 17:19:16 adam
260 * Record management uses the bfile system.
262 * Revision 1.29 1995/11/21 15:01:14 adam
263 * New general match criteria implemented.
264 * New feature: document groups.
266 * Revision 1.28 1995/11/21 09:20:30 adam
267 * Yet more work on record match.
269 * Revision 1.27 1995/11/20 16:59:45 adam
270 * New update method: the 'old' keys are saved for each records.
272 * Revision 1.26 1995/11/20 11:56:24 adam
273 * Work on new traversal.
275 * Revision 1.25 1995/11/16 15:34:54 adam
276 * Uses new record management system in both indexer and server.
278 * Revision 1.24 1995/11/15 19:13:08 adam
279 * Work on record management.
281 * Revision 1.23 1995/10/27 14:00:10 adam
282 * Implemented detection of database availability.
284 * Revision 1.22 1995/10/17 18:02:07 adam
285 * New feature: databases. Implemented as prefix to words in dictionary.
287 * Revision 1.21 1995/10/10 12:24:38 adam
288 * Temporary sort files are compressed.
290 * Revision 1.20 1995/10/06 13:52:05 adam
291 * Bug fixes. Handler may abort further scanning.
293 * Revision 1.19 1995/10/04 12:55:16 adam
294 * Bug fix in ranked search. Use=Any keys inserted.
296 * Revision 1.18 1995/10/04 09:37:08 quinn
299 * Revision 1.17 1995/10/03 14:28:57 adam
300 * Buffered read in extract works.
302 * Revision 1.16 1995/10/03 14:28:45 adam
303 * Work on more effecient read handler in extract.
305 * Revision 1.15 1995/10/02 15:42:53 adam
306 * Extract uses file descriptors instead of FILE pointers.
308 * Revision 1.14 1995/10/02 15:29:13 adam
309 * More logging in file_extract.
311 * Revision 1.13 1995/09/29 14:01:39 adam
314 * Revision 1.12 1995/09/28 14:22:56 adam
315 * Sort uses smaller temporary files.
317 * Revision 1.11 1995/09/28 12:10:31 adam
318 * Bug fixes. Field prefix used in queries.
320 * Revision 1.10 1995/09/28 09:19:41 adam
321 * xfree/xmalloc used everywhere.
322 * Extract/retrieve method seems to work for text records.
324 * Revision 1.9 1995/09/27 12:22:28 adam
325 * More work on extract in record control.
326 * Field name is not in isam keys but in prefix in dictionary words.
328 * Revision 1.8 1995/09/14 07:48:22 adam
329 * Record control management.
331 * Revision 1.7 1995/09/11 13:09:32 adam
332 * More work on relevance feedback.
334 * Revision 1.6 1995/09/08 14:52:27 adam
335 * Minor changes. Dictionary is lower case now.
337 * Revision 1.5 1995/09/06 16:11:16 adam
338 * Option: only one word key per file.
340 * Revision 1.4 1995/09/05 15:28:39 adam
341 * More work on search engine.
343 * Revision 1.3 1995/09/04 12:33:41 adam
344 * Various cleanup. YAZ util used instead.
346 * Revision 1.2 1995/09/04 09:10:34 adam
347 * More work on index add/del/update.
348 * Merge sort implemented.
349 * Initial work on z39 server.
351 * Revision 1.1 1995/09/01 14:06:35 adam
352 * Split of work into more files.
376 #include "zebrasdr.h"
379 static Dict matchDict;
381 static Records records = NULL;
382 static SortIdx sortIdx = NULL;
384 static char **key_buf;
385 static size_t ptr_top;
387 static size_t key_buf_used;
388 static int key_file_no;
390 static int records_inserted = 0;
391 static int records_updated = 0;
392 static int records_deleted = 0;
393 static int records_processed = 0;
395 static ZebraExplainInfo zti = NULL;
397 static void logRecord (int showFlag)
401 if (showFlag || !(records_processed % 1000))
403 logf (LOG_LOG, "Records: %7d i/u/d %d/%d/%d",
404 records_processed, records_inserted, records_updated,
409 static int explain_extract (void *handle, Record drec, data1_node *n);
411 int key_open (struct recordGroup *rGroup, int mem)
413 BFiles bfs = rGroup->bfs;
414 int rw = rGroup->flagRw;
415 data1_handle dh = rGroup->dh;
416 char *recordCompression;
417 int record_compression = REC_COMPRESS_NONE;
419 mem = atoi(res_get_def (common_resource, "memMax", "4"))*1024*1024;
422 key_buf = (char **) xmalloc (mem);
423 ptr_top = mem/sizeof(char*);
429 if (!(matchDict = dict_open (bfs, GMATCH_DICT, 50, rw, 0)))
431 logf (LOG_FATAL, "dict_open fail of %s", GMATCH_DICT);
435 recordCompression = res_get_def (common_resource,
436 "recordCompression", "none");
437 if (!strcmp (recordCompression, "none"))
438 record_compression = REC_COMPRESS_NONE;
439 if (!strcmp (recordCompression, "bzip2"))
440 record_compression = REC_COMPRESS_BZIP2;
441 records = rec_open (bfs, rw, record_compression);
444 dict_close (matchDict);
447 zti = zebraExplain_open (records, dh, common_resource,
448 rw, rGroup, explain_extract);
451 rec_close (&records);
452 dict_close (matchDict);
455 sortIdx = sortIdx_open (bfs, 1);
466 void encode_key_init (struct encode_info *i)
473 char *encode_key_int (int d, char *bp)
482 else if (d <= 4194303)
484 *bp++ = 128 + (d>>16);
485 *bp++ = (d>>8) & 255;
490 *bp++ = 192 + (d>>24);
491 *bp++ = (d>>16) & 255;
492 *bp++ = (d>>8) & 255;
498 void encode_key_write (char *k, struct encode_info *i, FILE *outf)
503 while ((*bp++ = *k++))
505 memcpy (&key, k+1, sizeof(struct it_key));
506 bp = encode_key_int ( (key.sysno - i->sysno) * 2 + *k, bp);
507 if (i->sysno != key.sysno)
509 i->sysno = key.sysno;
512 else if (!i->seqno && !key.seqno && i->cmd == *k)
514 bp = encode_key_int (key.seqno - i->seqno, bp);
515 i->seqno = key.seqno;
517 if (fwrite (i->buf, bp - i->buf, 1, outf) != 1)
519 logf (LOG_FATAL|LOG_ERRNO, "fwrite");
527 static int key_y_len;
529 static int key_y_compare (const void *p1, const void *p2)
533 if ((r = key_compare (*(char**) p1 + key_y_len + 1,
534 *(char**) p2 + key_y_len + 1)))
536 return *(*(char**) p1 + key_y_len) - *(*(char**) p2 + key_y_len);
539 static int key_x_compare (const void *p1, const void *p2)
541 return strcmp (*(char**) p1, *(char**) p2);
545 void key_flush (void)
550 struct encode_info encode_info;
559 logf (LOG_LOG, "sorting section %d", key_file_no);
561 qsort (key_buf + ptr_top-ptr_i, ptr_i, sizeof(char*), key_qsort_compare);
562 getFnameTmp (out_fname, key_file_no);
564 if (!(outf = fopen (out_fname, "wb")))
566 logf (LOG_FATAL|LOG_ERRNO, "fopen %s", out_fname);
569 logf (LOG_LOG, "writing section %d", key_file_no);
570 prevcp = 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 if (strcmp (cp, prevcp))
579 encode_key_init (&encode_info);
580 encode_key_write (cp, &encode_info, outf);
584 encode_key_write (cp + strlen(cp), &encode_info, outf);
587 qsort (key_buf + ptr_top-ptr_i, ptr_i, sizeof(char*), key_x_compare);
588 getFnameTmp (out_fname, key_file_no);
590 if (!(outf = fopen (out_fname, "wb")))
592 logf (LOG_FATAL|LOG_ERRNO, "fopen %s", out_fname);
595 logf (LOG_LOG, "writing section %d", key_file_no);
597 prevcp = key_buf[ptr_top-i];
599 if (!--i || strcmp (prevcp, key_buf[ptr_top-i]))
601 key_y_len = strlen(prevcp)+1;
603 logf (LOG_LOG, "key_y_len: %2d %02x %02x %s",
604 key_y_len, prevcp[0], prevcp[1], 2+prevcp);
606 qsort (key_buf + ptr_top-ptr_i, ptr_i - i,
607 sizeof(char*), key_y_compare);
608 cp = key_buf[ptr_top-ptr_i];
610 encode_key_init (&encode_info);
611 encode_key_write (cp, &encode_info, outf);
614 cp = key_buf[ptr_top-ptr_i];
615 encode_key_write (cp+key_y_len, &encode_info, outf);
619 prevcp = key_buf[ptr_top-ptr_i];
624 logf (LOG_FATAL|LOG_ERRNO, "fclose %s", out_fname);
627 logf (LOG_LOG, "finished section %d", key_file_no);
632 int key_close (struct recordGroup *rGroup)
634 int rw = rGroup->flagRw;
636 zebraExplain_runNumberIncrement (zti, 1);
637 zebraExplain_close (zti, rw, 0);
640 rec_close (&records);
641 dict_close (matchDict);
642 sortIdx_close (sortIdx);
648 static void wordInit (struct recExtractCtrl *p, RecWord *w)
650 w->zebra_maps = p->zebra_maps;
651 w->seqnos = p->seqno;
652 w->attrSet = VAL_BIB1;
658 static struct sortKey {
663 struct sortKey *next;
666 static struct recKeys {
675 static void addIndexString (RecWord *p, const char *string, int length)
678 unsigned char attrSet;
679 unsigned short attrUse;
682 int *pseqno = &p->seqnos[p->reg_type];
684 if (reckeys.buf_used+1024 > reckeys.buf_max)
688 b = (char *) xmalloc (reckeys.buf_max += 128000);
689 if (reckeys.buf_used > 0)
690 memcpy (b, reckeys.buf, reckeys.buf_used);
694 dst = reckeys.buf + reckeys.buf_used;
696 attrSet = p->attrSet;
697 if (reckeys.buf_used > 0 && reckeys.prevAttrSet == attrSet)
700 reckeys.prevAttrSet = attrSet;
701 attrUse = p->attrUse;
702 if (reckeys.buf_used > 0 && reckeys.prevAttrUse == attrUse)
705 reckeys.prevAttrUse = attrUse;
707 diff = 1 + *pseqno - reckeys.prevSeqNo;
708 if (diff >= 1 && diff <= 15)
713 reckeys.prevSeqNo = *pseqno;
719 memcpy (dst, &attrSet, sizeof(attrSet));
720 dst += sizeof(attrSet);
724 memcpy (dst, &attrUse, sizeof(attrUse));
725 dst += sizeof(attrUse);
727 *dst++ = p->reg_type;
728 memcpy (dst, string, length);
734 memcpy (dst, pseqno, sizeof(*pseqno));
735 dst += sizeof(*pseqno);
737 reckeys.buf_used = dst - reckeys.buf;
742 static void addSortString (RecWord *p, const char *string, int length)
746 for (sk = sortKeys; sk; sk = sk->next)
747 if (sk->attrSet == p->attrSet && sk->attrUse == p->attrUse)
750 sk = (struct sortKey *) xmalloc (sizeof(*sk));
754 sk->string = (char *) xmalloc (length);
756 memcpy (sk->string, string, length);
758 sk->attrSet = p->attrSet;
759 sk->attrUse = p->attrUse;
762 static void addString (RecWord *p, const char *string, int length)
765 if (zebra_maps_is_sort (p->zebra_maps, p->reg_type))
766 addSortString (p, string, length);
768 addIndexString (p, string, length);
771 static void addIncompleteField (RecWord *p)
773 const char *b = p->string;
774 int remain = p->length;
775 const char **map = 0;
778 map = zebra_maps_input(p->zebra_maps, p->reg_type, &b, remain);
782 char buf[IT_MAX_WORD+1];
786 while (map && *map && **map == *CHR_SPACE)
788 remain = p->length - (b - p->string);
790 map = zebra_maps_input(p->zebra_maps, p->reg_type, &b, remain);
797 while (map && *map && **map != *CHR_SPACE)
799 const char *cp = *map;
801 while (i < IT_MAX_WORD && *cp)
803 remain = p->length - (b - p->string);
805 map = zebra_maps_input(p->zebra_maps, p->reg_type, &b, remain);
811 addString (p, buf, i);
815 static void addCompleteField (RecWord *p)
817 const char *b = p->string;
818 char buf[IT_MAX_WORD+1];
819 const char **map = 0;
820 int i = 0, remain = p->length;
823 map = zebra_maps_input (p->zebra_maps, p->reg_type, &b, remain);
825 while (remain > 0 && i < IT_MAX_WORD)
827 while (map && *map && **map == *CHR_SPACE)
829 remain = p->length - (b - p->string);
831 map = zebra_maps_input(p->zebra_maps, p->reg_type, &b, remain);
838 if (i && i < IT_MAX_WORD)
839 buf[i++] = *CHR_SPACE;
840 while (map && *map && **map != *CHR_SPACE)
842 const char *cp = *map;
844 if (i >= IT_MAX_WORD)
846 while (i < IT_MAX_WORD && *cp)
848 remain = p->length - (b - p->string);
850 map = zebra_maps_input (p->zebra_maps, p->reg_type, &b,
858 addString (p, buf, i);
861 static void addRecordKey (RecWord *p)
864 if ((wrbuf = zebra_replace(p->zebra_maps, p->reg_type, 0,
865 p->string, p->length)))
867 p->string = wrbuf_buf(wrbuf);
868 p->length = wrbuf_len(wrbuf);
870 if (zebra_maps_is_complete (p->zebra_maps, p->reg_type))
871 addCompleteField (p);
873 addIncompleteField(p);
876 static void flushSortKeys (SYSNO sysno, int cmd)
878 struct sortKey *sk = sortKeys;
880 sortIdx_sysno (sortIdx, sysno);
883 struct sortKey *sk_next = sk->next;
884 sortIdx_type (sortIdx, sk->attrUse);
885 sortIdx_add (sortIdx, sk->string, sk->length);
893 static void flushRecordKeys (SYSNO sysno, int cmd, struct recKeys *reckeys)
895 unsigned char attrSet = (unsigned char) -1;
896 unsigned short attrUse = (unsigned short) -1;
900 zebraExplain_recordCountIncrement (zti, cmd ? 1 : -1);
901 while (off < reckeys->buf_used)
903 const char *src = reckeys->buf + off;
911 memcpy (&attrSet, src, sizeof(attrSet));
912 src += sizeof(attrSet);
916 memcpy (&attrUse, src, sizeof(attrUse));
917 src += sizeof(attrUse);
919 if (key_buf_used + 1024 > (ptr_top-ptr_i)*sizeof(char*))
922 key_buf[ptr_top-ptr_i] = (char*)key_buf + key_buf_used;
924 ch = zebraExplain_lookupSU (zti, attrSet, attrUse);
926 ch = zebraExplain_addSU (zti, attrSet, attrUse);
928 key_buf_used += key_SU_code (ch, ((char*)key_buf) + key_buf_used);
931 ((char*)key_buf) [key_buf_used++] = *src++;
933 ((char*)key_buf) [key_buf_used++] = '\0';
934 ((char*) key_buf)[key_buf_used++] = cmd;
937 seqno += ((lead>>2) & 15)-1;
940 memcpy (&seqno, src, sizeof(seqno));
941 src += sizeof(seqno);
945 memcpy ((char*)key_buf + key_buf_used, &key, sizeof(key));
946 key_buf_used += sizeof(key);
947 off = src - reckeys->buf;
949 assert (off == reckeys->buf_used);
952 static const char **searchRecordKey (struct recKeys *reckeys,
953 int attrSetS, int attrUseS)
955 static const char *ws[32];
963 for (i = 0; i<32; i++)
966 while (off < reckeys->buf_used)
969 const char *src = reckeys->buf + off;
977 memcpy (&attrSet, src, sizeof(attrSet));
978 src += sizeof(attrSet);
982 memcpy (&attrUse, src, sizeof(attrUse));
983 src += sizeof(attrUse);
989 seqno += ((lead>>2) & 15)-1;
992 memcpy (&seqno, src, sizeof(seqno));
993 src += sizeof(seqno);
995 if (attrUseS == attrUse && attrSetS == attrSet)
1002 woff = seqno - startSeq;
1003 if (woff >= 0 && woff < 31)
1007 off = src - reckeys->buf;
1009 assert (off == reckeys->buf_used);
1013 struct file_read_info {
1014 off_t file_max; /* maximum offset so far */
1015 off_t file_offset; /* current offset */
1016 off_t file_moffset; /* offset of rec/rec boundary */
1023 static struct file_read_info *file_read_start (int fd)
1025 struct file_read_info *fi = (struct file_read_info *)
1026 xmalloc (sizeof(*fi));
1030 fi->file_moffset = 0;
1036 static void file_read_stop (struct file_read_info *fi)
1041 static off_t file_seek (void *handle, off_t offset)
1043 struct file_read_info *p = (struct file_read_info *) handle;
1044 p->file_offset = offset;
1047 return lseek (p->fd, offset, SEEK_SET);
1050 static off_t file_tell (void *handle)
1052 struct file_read_info *p = (struct file_read_info *) handle;
1053 return p->file_offset;
1056 static int file_read (void *handle, char *buf, size_t count)
1058 struct file_read_info *p = (struct file_read_info *) handle;
1064 if (r > p->sdrmax - p->file_offset)
1065 r = p->sdrmax - p->file_offset;
1067 memcpy (buf, p->sdrbuf + p->file_offset, r);
1070 r = read (fd, buf, count);
1073 p->file_offset += r;
1074 if (p->file_offset > p->file_max)
1075 p->file_max = p->file_offset;
1080 static void file_begin (void *handle)
1082 struct file_read_info *p = (struct file_read_info *) handle;
1084 p->file_offset = p->file_moffset;
1085 if (!p->sdrbuf && p->file_moffset)
1086 lseek (p->fd, p->file_moffset, SEEK_SET);
1090 static void file_end (void *handle, off_t offset)
1092 struct file_read_info *p = (struct file_read_info *) handle;
1094 assert (p->file_more == 0);
1096 p->file_moffset = offset;
1099 static char *fileMatchStr (struct recKeys *reckeys, struct recordGroup *rGroup,
1100 const char *fname, const char *spec)
1102 static char dstBuf[2048];
1104 const char *s = spec;
1105 static const char **w;
1109 while (*s == ' ' || *s == '\t')
1115 char attset_str[64], attname_str[64];
1116 data1_attset *attset;
1119 int attSet = 1, attUse = 1;
1123 for (i = 0; *s && *s != ',' && *s != ')'; s++)
1125 attset_str[i++] = *s;
1126 attset_str[i] = '\0';
1131 for (i = 0; *s && *s != ')'; s++)
1133 attname_str[i++] = *s;
1134 attname_str[i] = '\0';
1137 if ((attset = data1_get_attset (rGroup->dh, attset_str)))
1140 attSet = attset->reference;
1141 att = data1_getattbyname(rGroup->dh, attset, attname_str);
1143 attUse = att->value;
1145 attUse = atoi (attname_str);
1147 w = searchRecordKey (reckeys, attSet, attUse);
1152 for (i = 0; i<32; i++)
1157 logf (LOG_WARN, "Missing ) in match criteria %s in group %s",
1158 spec, rGroup->groupName ? rGroup->groupName : "none");
1163 for (i = 0; i<32; i++)
1164 if (matchFlag[i] && w[i])
1172 dst += strlen(w[i]);
1176 logf (LOG_WARN, "Record didn't contain match"
1177 " fields in (%s,%s)", attset_str, attname_str);
1185 const char *spec_src = NULL;
1186 const char *s1 = ++s;
1187 while (*s1 && *s1 != ' ' && *s1 != '\t')
1193 memcpy (special, s, spec_len);
1194 special[spec_len] = '\0';
1197 if (!strcmp (special, "group"))
1198 spec_src = rGroup->groupName;
1199 else if (!strcmp (special, "database"))
1200 spec_src = rGroup->databaseName;
1201 else if (!strcmp (special, "filename"))
1203 else if (!strcmp (special, "type"))
1204 spec_src = rGroup->recordType;
1209 strcpy (dst, spec_src);
1210 dst += strlen (spec_src);
1213 else if (*s == '\"' || *s == '\'')
1215 int stopMarker = *s++;
1219 while (*s && *s != stopMarker)
1222 tmpString[i++] = *s++;
1226 tmpString[i] = '\0';
1227 strcpy (dst, tmpString);
1228 dst += strlen (tmpString);
1232 logf (LOG_WARN, "Syntax error in match criteria %s in group %s",
1233 spec, rGroup->groupName ? rGroup->groupName : "none");
1240 logf (LOG_WARN, "No match criteria for record %s in group %s",
1241 fname, rGroup->groupName ? rGroup->groupName : "none");
1248 struct recordLogInfo {
1251 struct recordGroup *rGroup;
1254 static void recordLogPreamble (int level, const char *msg, void *info)
1256 struct recordLogInfo *p = (struct recordLogInfo *) info;
1257 FILE *outf = log_file ();
1259 if (level & LOG_LOG)
1261 fprintf (outf, "File %s, offset %d, type %s\n",
1262 p->rGroup->recordType, p->recordOffset, p->fname);
1263 log_event_start (NULL, NULL);
1266 void addSchema (struct recExtractCtrl *p, Odr_oid *oid)
1268 zebraExplain_addSchema (zti, oid);
1271 static int recordExtract (SYSNO *sysno, const char *fname,
1272 struct recordGroup *rGroup, int deleteFlag,
1273 struct file_read_info *fi,
1274 RecType recType, char *subType, void *clientData)
1276 RecordAttr *recordAttr;
1281 struct recordLogInfo logInfo;
1282 off_t recordOffset = 0;
1286 struct recExtractCtrl extractCtrl;
1288 /* we are going to read from a file, so prepare the extraction */
1291 reckeys.buf_used = 0;
1292 reckeys.prevAttrUse = -1;
1293 reckeys.prevAttrSet = -1;
1294 reckeys.prevSeqNo = 0;
1296 recordOffset = fi->file_moffset;
1297 extractCtrl.offset = fi->file_moffset;
1298 extractCtrl.readf = file_read;
1299 extractCtrl.seekf = file_seek;
1300 extractCtrl.tellf = file_tell;
1301 extractCtrl.endf = file_end;
1302 extractCtrl.fh = fi;
1303 extractCtrl.subType = subType;
1304 extractCtrl.init = wordInit;
1305 extractCtrl.tokenAdd = addRecordKey;
1306 extractCtrl.schemaAdd = addSchema;
1307 extractCtrl.dh = rGroup->dh;
1308 for (i = 0; i<256; i++)
1310 if (zebra_maps_is_positioned(rGroup->zebra_maps, i))
1311 extractCtrl.seqno[i] = 1;
1313 extractCtrl.seqno[i] = 0;
1315 extractCtrl.zebra_maps = rGroup->zebra_maps;
1316 extractCtrl.flagShowRecords = !rGroup->flagRw;
1318 if (!rGroup->flagRw)
1319 printf ("File: %s %ld\n", fname, (long) recordOffset);
1321 logInfo.fname = fname;
1322 logInfo.recordOffset = recordOffset;
1323 logInfo.rGroup = rGroup;
1324 log_event_start (recordLogPreamble, &logInfo);
1326 r = (*recType->extract)(clientData, &extractCtrl);
1328 log_event_start (NULL, NULL);
1330 if (r == RECCTRL_EXTRACT_EOF)
1332 else if (r == RECCTRL_EXTRACT_ERROR)
1334 /* error occured during extraction ... */
1335 if (rGroup->flagRw &&
1336 records_processed < rGroup->fileVerboseLimit)
1338 logf (LOG_WARN, "fail %s %s %ld", rGroup->recordType,
1339 fname, (long) recordOffset);
1343 if (reckeys.buf_used == 0)
1345 /* the extraction process returned no information - the record
1346 is probably empty - unless flagShowRecords is in use */
1347 if (!rGroup->flagRw)
1349 logf (LOG_WARN, "No keys generated for file %s", fname);
1350 logf (LOG_WARN, " The file is probably empty");
1355 /* perform match if sysno not known and if match criteria is specified */
1362 if (rGroup->recordId && *rGroup->recordId)
1366 matchStr = fileMatchStr (&reckeys, rGroup, fname,
1370 rinfo = dict_lookup (matchDict, matchStr);
1372 memcpy (sysno, rinfo+1, sizeof(*sysno));
1376 logf (LOG_WARN, "Bad match criteria");
1387 logf (LOG_LOG, "delete %s %s %ld", rGroup->recordType,
1388 fname, (long) recordOffset);
1389 logf (LOG_WARN, "cannot delete record above (seems new)");
1392 if (records_processed < rGroup->fileVerboseLimit)
1393 logf (LOG_LOG, "add %s %s %ld", rGroup->recordType,
1394 fname, (long) recordOffset);
1395 rec = rec_new (records);
1397 *sysno = rec->sysno;
1399 recordAttr = rec_init_attr (zti, rec);
1403 dict_insert (matchDict, matchStr, sizeof(*sysno), sysno);
1405 flushRecordKeys (*sysno, 1, &reckeys);
1406 flushSortKeys (*sysno, 1);
1412 /* record already exists */
1413 struct recKeys delkeys;
1415 rec = rec_get (records, *sysno);
1418 recordAttr = rec_init_attr (zti, rec);
1420 if (recordAttr->runNumber == zebraExplain_runNumberIncrement (zti, 0))
1422 logf (LOG_LOG, "skipped %s %s %ld", rGroup->recordType,
1423 fname, (long) recordOffset);
1428 delkeys.buf_used = rec->size[recInfo_delKeys];
1429 delkeys.buf = rec->info[recInfo_delKeys];
1430 flushSortKeys (*sysno, 0);
1431 flushRecordKeys (*sysno, 0, &delkeys);
1434 /* record going to be deleted */
1435 if (!delkeys.buf_used)
1437 logf (LOG_LOG, "delete %s %s %ld", rGroup->recordType,
1438 fname, (long) recordOffset);
1439 logf (LOG_WARN, "cannot delete file above, storeKeys false");
1443 if (records_processed < rGroup->fileVerboseLimit)
1444 logf (LOG_LOG, "delete %s %s %ld", rGroup->recordType,
1445 fname, (long) recordOffset);
1448 dict_delete (matchDict, matchStr);
1449 rec_del (records, &rec);
1457 /* record going to be updated */
1458 if (!delkeys.buf_used)
1460 logf (LOG_LOG, "update %s %s %ld", rGroup->recordType,
1461 fname, (long) recordOffset);
1462 logf (LOG_WARN, "cannot update file above, storeKeys false");
1466 if (records_processed < rGroup->fileVerboseLimit)
1467 logf (LOG_LOG, "update %s %s %ld", rGroup->recordType,
1468 fname, (long) recordOffset);
1469 flushRecordKeys (*sysno, 1, &reckeys);
1474 /* update file type */
1475 xfree (rec->info[recInfo_fileType]);
1476 rec->info[recInfo_fileType] =
1477 rec_strdup (rGroup->recordType, &rec->size[recInfo_fileType]);
1479 /* update filename */
1480 xfree (rec->info[recInfo_filename]);
1481 rec->info[recInfo_filename] =
1482 rec_strdup (fname, &rec->size[recInfo_filename]);
1484 /* update delete keys */
1485 xfree (rec->info[recInfo_delKeys]);
1486 if (reckeys.buf_used > 0 && rGroup->flagStoreKeys == 1)
1489 rec->size[recInfo_delKeys] = reckeys.buf_used;
1490 rec->info[recInfo_delKeys] = reckeys.buf;
1492 reckeys.buf_max = 0;
1494 rec->info[recInfo_delKeys] = xmalloc (reckeys.buf_used);
1495 rec->size[recInfo_delKeys] = reckeys.buf_used;
1496 memcpy (rec->info[recInfo_delKeys], reckeys.buf,
1497 rec->size[recInfo_delKeys]);
1502 rec->info[recInfo_delKeys] = NULL;
1503 rec->size[recInfo_delKeys] = 0;
1506 /* save file size of original record */
1507 zebraExplain_recordBytesIncrement (zti, - recordAttr->recordSize);
1508 recordAttr->recordSize = fi->file_moffset - recordOffset;
1509 if (!recordAttr->recordSize)
1510 recordAttr->recordSize = fi->file_max - recordOffset;
1511 zebraExplain_recordBytesIncrement (zti, recordAttr->recordSize);
1513 /* set run-number for this record */
1514 recordAttr->runNumber = zebraExplain_runNumberIncrement (zti, 0);
1516 /* update store data */
1517 xfree (rec->info[recInfo_storeData]);
1518 if (rGroup->flagStoreData == 1)
1520 rec->size[recInfo_storeData] = recordAttr->recordSize;
1521 rec->info[recInfo_storeData] = (char *)
1522 xmalloc (recordAttr->recordSize);
1523 if (lseek (fi->fd, recordOffset, SEEK_SET) < 0)
1525 logf (LOG_ERRNO|LOG_FATAL, "seek to %ld in %s",
1526 (long) recordOffset, fname);
1529 if (read (fi->fd, rec->info[recInfo_storeData], recordAttr->recordSize)
1530 < recordAttr->recordSize)
1532 logf (LOG_ERRNO|LOG_FATAL, "read %d bytes of %s",
1533 recordAttr->recordSize, fname);
1539 rec->info[recInfo_storeData] = NULL;
1540 rec->size[recInfo_storeData] = 0;
1542 /* update database name */
1543 xfree (rec->info[recInfo_databaseName]);
1544 rec->info[recInfo_databaseName] =
1545 rec_strdup (rGroup->databaseName, &rec->size[recInfo_databaseName]);
1548 recordAttr->recordOffset = recordOffset;
1550 /* commit this record */
1551 rec_put (records, &rec);
1556 int fileExtract (SYSNO *sysno, const char *fname,
1557 const struct recordGroup *rGroupP, int deleteFlag)
1565 struct recordGroup rGroupM;
1566 struct recordGroup *rGroup = &rGroupM;
1567 struct file_read_info *fi;
1570 memcpy (rGroup, rGroupP, sizeof(*rGroupP));
1572 if (!rGroup->groupName || !*rGroup->groupName)
1575 sprintf (gprefix, "%s.", rGroup->groupName);
1577 logf (LOG_DEBUG, "fileExtract %s", fname);
1579 /* determine file extension */
1581 for (i = strlen(fname); --i >= 0; )
1582 if (fname[i] == '/')
1584 else if (fname[i] == '.')
1586 strcpy (ext, fname+i+1);
1589 /* determine file type - depending on extension */
1590 if (!rGroup->recordType)
1592 sprintf (ext_res, "%srecordType.%s", gprefix, ext);
1593 if (!(rGroup->recordType = res_get (common_resource, ext_res)))
1595 sprintf (ext_res, "%srecordType", gprefix);
1596 rGroup->recordType = res_get (common_resource, ext_res);
1599 if (!rGroup->recordType)
1601 if (records_processed < rGroup->fileVerboseLimit)
1602 logf (LOG_LOG, "? %s", fname);
1605 if (!*rGroup->recordType)
1608 recType_byName (rGroup->recTypes, rGroup->recordType, subType,
1611 logf (LOG_WARN, "No such record type: %s", rGroup->recordType);
1615 /* determine match criteria */
1616 if (!rGroup->recordId)
1618 sprintf (ext_res, "%srecordId.%s", gprefix, ext);
1619 rGroup->recordId = res_get (common_resource, ext_res);
1622 /* determine database name */
1623 if (!rGroup->databaseName)
1625 sprintf (ext_res, "%sdatabase.%s", gprefix, ext);
1626 if (!(rGroup->databaseName = res_get (common_resource, ext_res)))
1628 sprintf (ext_res, "%sdatabase", gprefix);
1629 rGroup->databaseName = res_get (common_resource, ext_res);
1632 if (!rGroup->databaseName)
1633 rGroup->databaseName = "Default";
1635 /* determine if explain database */
1637 sprintf (ext_res, "%sexplainDatabase", gprefix);
1638 rGroup->explainDatabase =
1639 atoi (res_get_def (common_resource, ext_res, "0"));
1641 /* announce database */
1642 if (zebraExplain_curDatabase (zti, rGroup->databaseName))
1644 if (zebraExplain_newDatabase (zti, rGroup->databaseName,
1645 rGroup->explainDatabase))
1649 if (rGroup->flagStoreData == -1)
1652 sprintf (ext_res, "%sstoreData.%s", gprefix, ext);
1653 if (!(sval = res_get (common_resource, ext_res)))
1655 sprintf (ext_res, "%sstoreData", gprefix);
1656 sval = res_get (common_resource, ext_res);
1659 rGroup->flagStoreData = atoi (sval);
1661 if (rGroup->flagStoreData == -1)
1662 rGroup->flagStoreData = 0;
1664 if (rGroup->flagStoreKeys == -1)
1668 sprintf (ext_res, "%sstoreKeys.%s", gprefix, ext);
1669 if (!(sval = res_get (common_resource, ext_res)))
1671 sprintf (ext_res, "%sstoreKeys", gprefix);
1672 sval = res_get (common_resource, ext_res);
1675 rGroup->flagStoreKeys = atoi (sval);
1677 if (rGroup->flagStoreKeys == -1)
1678 rGroup->flagStoreKeys = 0;
1684 char xname[128], *xp;
1686 strncpy (xname, fname, 127);
1687 if (!(xp = strchr (xname, '.')))
1690 if (strcmp (xp+1, "sdr.bits"))
1693 h = zebraSdr_open (xname);
1696 logf (LOG_WARN, "sdr open %s", xname);
1705 segmentno = zebraSdr_segment (h, 0);
1706 sprintf (sdr_name, "%%%s.%d", xname, segmentno);
1712 r = zebraSdr_read (h, &buf);
1717 fi = file_read_start (0);
1723 r = recordExtract (sysno, sdr_name, rGroup, deleteFlag, fi,
1725 } while (r && !sysno && fi->file_more);
1726 file_read_stop (fi);
1733 if (sysno && deleteFlag)
1737 if ((fd = open (fname, O_BINARY|O_RDONLY)) == -1)
1739 logf (LOG_WARN|LOG_ERRNO, "open %s", fname);
1743 fi = file_read_start (fd);
1747 r = recordExtract (sysno, fname, rGroup, deleteFlag, fi,
1748 recType, subType, clientData);
1749 } while (r && !sysno && fi->file_more);
1750 file_read_stop (fi);
1756 static int explain_extract (void *handle, Record rec, data1_node *n)
1758 struct recordGroup *rGroup = (struct recordGroup*) handle;
1759 struct recExtractCtrl extractCtrl;
1762 if (zebraExplain_curDatabase (zti, rec->info[recInfo_databaseName]))
1764 if (zebraExplain_newDatabase (zti, rec->info[recInfo_databaseName], 0))
1768 reckeys.buf_used = 0;
1769 reckeys.prevAttrUse = -1;
1770 reckeys.prevAttrSet = -1;
1771 reckeys.prevSeqNo = 0;
1773 extractCtrl.init = wordInit;
1774 extractCtrl.tokenAdd = addRecordKey;
1775 extractCtrl.schemaAdd = addSchema;
1776 extractCtrl.dh = rGroup->dh;
1777 for (i = 0; i<256; i++)
1778 extractCtrl.seqno[i] = 0;
1779 extractCtrl.zebra_maps = rGroup->zebra_maps;
1780 extractCtrl.flagShowRecords = !rGroup->flagRw;
1782 grs_extract_tree(&extractCtrl, n);
1784 logf (LOG_DEBUG, "flush explain record, sysno=%d", rec->sysno);
1786 if (rec->size[recInfo_delKeys])
1788 struct recKeys delkeys;
1790 delkeys.buf_used = rec->size[recInfo_delKeys];
1791 delkeys.buf = rec->info[recInfo_delKeys];
1792 flushSortKeys (rec->sysno, 0);
1793 flushRecordKeys (rec->sysno, 0, &delkeys);
1795 flushRecordKeys (rec->sysno, 1, &reckeys);
1796 flushSortKeys (rec->sysno, 1);
1798 xfree (rec->info[recInfo_delKeys]);
1799 rec->size[recInfo_delKeys] = reckeys.buf_used;
1800 rec->info[recInfo_delKeys] = reckeys.buf;
1802 reckeys.buf_max = 0;