1 /* $Id: extract.c,v 1.267 2007-10-31 16:56:14 adam Exp $
2 Copyright (C) 1995-2007
5 This file is part of the Zebra server.
7 Zebra is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24 \brief indexes records and extract tokens for indexing and sorting
43 #include <yaz/snprintf.h>
45 static int log_level_extract = 0;
46 static int log_level_details = 0;
47 static int log_level_initialized = 0;
49 /* 1 if we use eliminitate identical delete/insert keys */
50 /* eventually this the 0-case code will be removed */
53 void extract_flush_record_keys2(ZebraHandle zh, zint sysno,
54 zebra_rec_keys_t ins_keys,
56 zebra_rec_keys_t del_keys,
59 static void zebra_init_log_level(void)
61 if (!log_level_initialized)
63 log_level_initialized = 1;
65 log_level_extract = yaz_log_module_level("extract");
66 log_level_details = yaz_log_module_level("indexdetails");
70 static void extract_flush_sort_keys(ZebraHandle zh, zint sysno,
71 int cmd, zebra_rec_keys_t skp);
72 static void extract_schema_add(struct recExtractCtrl *p, Odr_oid *oid);
73 static void extract_token_add(RecWord *p);
74 static void extract_token_add2(RecWord *p);
76 static void check_log_limit(ZebraHandle zh)
78 if (zh->records_processed + zh->records_skipped == zh->m_file_verbose_limit)
80 yaz_log(YLOG_LOG, "More than %d file log entries. Omitting rest",
81 zh->m_file_verbose_limit);
85 static void logRecord(ZebraHandle zh)
88 ++zh->records_processed;
89 if (!(zh->records_processed % 1000))
91 yaz_log(YLOG_LOG, "Records: "ZINT_FORMAT" i/u/d "
92 ZINT_FORMAT"/"ZINT_FORMAT"/"ZINT_FORMAT,
93 zh->records_processed, zh->records_inserted,
94 zh->records_updated, zh->records_deleted);
98 static void init_extractCtrl(ZebraHandle zh, struct recExtractCtrl *ctrl)
100 ctrl->flagShowRecords = !zh->m_flag_rw;
104 static void extract_add_index_string(RecWord *p,
105 zinfo_index_category_t cat,
106 const char *str, int length);
108 static void extract_set_store_data_prepare(struct recExtractCtrl *p);
110 static void extract_init(struct recExtractCtrl *p, RecWord *w)
113 w->index_name = "any";
121 struct snip_rec_info {
123 zebra_snippets *snippets;
127 static void snippet_add_complete_field(RecWord *p, int ord,
130 struct snip_rec_info *h = p->extractCtrl->handle;
132 const char *b = p->term_buf;
133 char buf[IT_MAX_WORD+1];
134 const char **map = 0;
135 int i = 0, remain = p->term_len;
136 const char *start = b;
137 const char *last = 0;
140 map = zebra_maps_input(zm, &b, remain, 1);
142 while (remain > 0 && i < IT_MAX_WORD)
144 while (map && *map && **map == *CHR_SPACE)
146 remain = p->term_len - (b - p->term_buf);
149 start = b; /* set to first non-ws area */
152 int first = i ? 0 : 1; /* first position */
154 map = zebra_maps_input(zm, &b, remain, first);
162 if (i && i < IT_MAX_WORD)
163 buf[i++] = *CHR_SPACE;
164 while (map && *map && **map != *CHR_SPACE)
166 const char *cp = *map;
168 if (**map == *CHR_CUT)
174 if (i >= IT_MAX_WORD)
176 while (i < IT_MAX_WORD && *cp)
180 remain = p->term_len - (b - p->term_buf);
183 map = zebra_maps_input(zm, &b, remain, 0);
191 if (last && start != last)
192 zebra_snippets_appendn(h->snippets, p->seqno, 0, ord,
193 start, last - start);
196 static void snippet_add_incomplete_field(RecWord *p, int ord, zebra_map_t zm)
198 struct snip_rec_info *h = p->extractCtrl->handle;
199 const char *b = p->term_buf;
200 int remain = p->term_len;
202 const char **map = 0;
203 const char *start = b;
204 const char *last = b;
207 map = zebra_maps_input(zm, &b, remain, 0);
211 char buf[IT_MAX_WORD+1];
215 while (map && *map && **map == *CHR_SPACE)
217 remain = p->term_len - (b - p->term_buf);
220 map = zebra_maps_input(zm, &b, remain, 0);
228 zebra_snippets_appendn(h->snippets, p->seqno, 1, ord,
229 start, last - start);
235 while (map && *map && **map != *CHR_SPACE)
237 const char *cp = *map;
239 while (i < IT_MAX_WORD && *cp)
241 remain = p->term_len - (b - p->term_buf);
244 map = zebra_maps_input(zm, &b, remain, 0);
254 if (zebra_maps_is_first_in_field(zm))
256 /* first in field marker */
261 zebra_snippets_appendn(h->snippets, p->seqno, 0, ord,
262 start, last - start);
269 static void snippet_token_add(RecWord *p)
271 struct snip_rec_info *h = p->extractCtrl->handle;
272 ZebraHandle zh = h->zh;
273 zebra_map_t zm = zebra_map_get(zh->reg->zebra_maps, p->index_type);
275 if (zm && zebra_maps_is_index(zm))
277 ZebraExplainInfo zei = zh->reg->zei;
278 int ch = zebraExplain_lookup_attr_str(
279 zei, zinfo_index_category_index, p->index_type, p->index_name);
281 if (zebra_maps_is_complete(zm))
282 snippet_add_complete_field(p, ch, zm);
284 snippet_add_incomplete_field(p, ch, zm);
288 static void snippet_schema_add(
289 struct recExtractCtrl *p, Odr_oid *oid)
294 void extract_snippet(ZebraHandle zh, zebra_snippets *sn,
295 struct ZebraRecStream *stream,
296 RecType rt, void *recTypeClientData)
298 struct recExtractCtrl extractCtrl;
299 struct snip_rec_info info;
302 extractCtrl.stream = stream;
303 extractCtrl.first_record = 1;
304 extractCtrl.init = extract_init;
305 extractCtrl.tokenAdd = snippet_token_add;
306 extractCtrl.schemaAdd = snippet_schema_add;
310 extractCtrl.dh = zh->reg->dh;
314 extractCtrl.handle = &info;
315 extractCtrl.match_criteria[0] = '\0';
316 extractCtrl.staticrank = 0;
317 extractCtrl.action = action_insert;
319 init_extractCtrl(zh, &extractCtrl);
321 extractCtrl.setStoreData = 0;
323 r = (*rt->extract)(recTypeClientData, &extractCtrl);
327 static void searchRecordKey(ZebraHandle zh,
328 zebra_rec_keys_t reckeys,
329 const char *index_name,
330 const char **ws, int ws_length)
334 zinfo_index_category_t cat = zinfo_index_category_index;
336 for (i = 0; i<ws_length; i++)
340 ch = zebraExplain_lookup_attr_str(zh->reg->zei, cat, "0", index_name);
342 ch = zebraExplain_lookup_attr_str(zh->reg->zei, cat, "p", index_name);
344 ch = zebraExplain_lookup_attr_str(zh->reg->zei, cat, "w", index_name);
349 if (zebra_rec_keys_rewind(reckeys))
356 while (zebra_rec_keys_read(reckeys, &str, &slen, &key))
358 assert(key.len <= IT_KEY_LEVEL_MAX && key.len > 2);
360 seqno = key.mem[key.len-1];
362 if (key.mem[0] == ch)
368 woff = seqno - startSeq;
369 if (woff >= 0 && woff < ws_length)
376 #define FILE_MATCH_BLANK "\t "
378 static char *get_match_from_spec(ZebraHandle zh,
379 zebra_rec_keys_t reckeys,
380 const char *fname, const char *spec)
382 static char dstBuf[2048]; /* static here ??? */
384 const char *s = spec;
388 for (; *s && strchr(FILE_MATCH_BLANK, *s); s++)
395 char attset_str[64], attname_str[64];
399 for (s++; strchr(FILE_MATCH_BLANK, *s); s++)
401 for (i = 0; *s && *s != ',' && *s != ')' &&
402 !strchr(FILE_MATCH_BLANK, *s); s++)
403 if (i+1 < sizeof(attset_str))
404 attset_str[i++] = *s;
405 attset_str[i] = '\0';
407 for (; strchr(FILE_MATCH_BLANK, *s); s++)
410 strcpy(attname_str, attset_str);
413 for (s++; strchr(FILE_MATCH_BLANK, *s); s++)
415 for (i = 0; *s && *s != ')' &&
416 !strchr(FILE_MATCH_BLANK, *s); s++)
417 if (i+1 < sizeof(attname_str))
418 attname_str[i++] = *s;
419 attname_str[i] = '\0';
422 searchRecordKey(zh, reckeys, attname_str, ws, 32);
426 yaz_log(YLOG_WARN, "Missing ) in match criteria %s in group %s",
427 spec, zh->m_group ? zh->m_group : "none");
432 for (i = 0; i<32; i++)
441 dst += strlen(ws[i]);
445 yaz_log(YLOG_WARN, "Record didn't contain match"
446 " fields in (%s,%s)", attset_str, attname_str);
454 const char *spec_src = NULL;
455 const char *s1 = ++s;
456 while (*s1 && !strchr(FILE_MATCH_BLANK, *s1))
460 if (spec_len > sizeof(special)-1)
461 spec_len = sizeof(special)-1;
462 memcpy(special, s, spec_len);
463 special[spec_len] = '\0';
466 if (!strcmp(special, "group"))
467 spec_src = zh->m_group;
468 else if (!strcmp(special, "database"))
469 spec_src = zh->basenames[0];
470 else if (!strcmp(special, "filename")) {
473 else if (!strcmp(special, "type"))
474 spec_src = zh->m_record_type;
479 strcpy(dst, spec_src);
480 dst += strlen(spec_src);
483 else if (*s == '\"' || *s == '\'')
485 int stopMarker = *s++;
489 while (*s && *s != stopMarker)
491 if (i+1 < sizeof(tmpString))
492 tmpString[i++] = *s++;
497 strcpy(dst, tmpString);
498 dst += strlen(tmpString);
502 yaz_log(YLOG_WARN, "Syntax error in match criteria %s in group %s",
503 spec, zh->m_group ? zh->m_group : "none");
510 yaz_log(YLOG_WARN, "No match criteria for record %s in group %s",
511 fname, zh->m_group ? zh->m_group : "none");
518 struct recordLogInfo {
521 struct recordGroup *rGroup;
524 static void all_matches_add(struct recExtractCtrl *ctrl)
527 extract_init(ctrl, &word);
528 word.index_name = "_ALLRECORDS";
529 word.index_type = "w";
531 extract_add_index_string(&word, zinfo_index_category_alwaysmatches,
535 ZEBRA_RES zebra_extract_records_stream(ZebraHandle zh,
536 struct ZebraRecStream *stream,
537 enum zebra_recctrl_action_t action,
539 const char *recordType,
541 const char *match_criteria,
544 void *recTypeClientData);
547 ZEBRA_RES zebra_extract_file(ZebraHandle zh, zint *sysno, const char *fname,
550 ZEBRA_RES r = ZEBRA_OK;
555 struct file_read_info *fi = 0;
556 const char *original_record_type = 0;
558 void *recTypeClientData;
559 struct ZebraRecStream stream, *streamp;
561 zebra_init_log_level();
563 if (!zh->m_group || !*zh->m_group)
566 sprintf(gprefix, "%s.", zh->m_group);
568 yaz_log(log_level_extract, "zebra_extract_file %s", fname);
570 /* determine file extension */
572 for (i = strlen(fname); --i >= 0; )
575 else if (fname[i] == '.')
577 strcpy(ext, fname+i+1);
580 /* determine file type - depending on extension */
581 original_record_type = zh->m_record_type;
582 if (!zh->m_record_type)
584 sprintf(ext_res, "%srecordType.%s", gprefix, ext);
585 zh->m_record_type = res_get(zh->res, ext_res);
587 if (!zh->m_record_type)
590 if (zh->records_processed + zh->records_skipped
591 < zh->m_file_verbose_limit)
592 yaz_log(YLOG_LOG, "? %s", fname);
593 zh->records_skipped++;
596 /* determine match criteria */
597 if (!zh->m_record_id)
599 sprintf(ext_res, "%srecordId.%s", gprefix, ext);
600 zh->m_record_id = res_get(zh->res, ext_res);
604 recType_byName(zh->reg->recTypes, zh->res, zh->m_record_type,
605 &recTypeClientData)))
607 yaz_log(YLOG_WARN, "No such record type: %s", zh->m_record_type);
611 switch(recType->version)
616 yaz_log(YLOG_WARN, "Bad filter version: %s", zh->m_record_type);
618 if (sysno && deleteFlag)
627 if (zh->path_reg && !yaz_is_abspath(fname))
629 strcpy(full_rep, zh->path_reg);
630 strcat(full_rep, "/");
631 strcat(full_rep, fname);
634 strcpy(full_rep, fname);
636 if ((fd = open(full_rep, O_BINARY|O_RDONLY)) == -1)
638 yaz_log(YLOG_WARN|YLOG_ERRNO, "open %s", full_rep);
639 zh->m_record_type = original_record_type;
643 zebra_create_stream_fd(streamp, fd, 0);
645 r = zebra_extract_records_stream(zh, streamp,
647 action_delete : action_update,
651 0, /*match_criteria */
653 recType, recTypeClientData);
655 stream.destroy(streamp);
656 zh->m_record_type = original_record_type;
661 If sysno is provided, then it's used to identify the reocord.
662 If not, and match_criteria is provided, then sysno is guessed
663 If not, and a record is provided, then sysno is got from there
667 ZEBRA_RES zebra_buffer_extract_record(ZebraHandle zh,
668 const char *buf, size_t buf_size,
669 enum zebra_recctrl_action_t action,
671 const char *recordType,
673 const char *match_criteria,
676 struct ZebraRecStream stream;
681 if (recordType && *recordType)
683 yaz_log(log_level_extract,
684 "Record type explicitly specified: %s", recordType);
685 recType = recType_byName(zh->reg->recTypes, zh->res, recordType,
690 if (!(zh->m_record_type))
692 yaz_log(YLOG_WARN, "No such record type defined");
695 yaz_log(log_level_extract, "Get record type from rgroup: %s",
697 recType = recType_byName(zh->reg->recTypes, zh->res,
698 zh->m_record_type, &clientData);
699 recordType = zh->m_record_type;
704 yaz_log(YLOG_WARN, "No such record type: %s", recordType);
708 zebra_create_stream_mem(&stream, buf, buf_size);
710 res = zebra_extract_records_stream(zh, &stream,
717 recType, clientData);
718 stream.destroy(&stream);
722 ZEBRA_RES zebra_extract_records_stream(ZebraHandle zh,
723 struct ZebraRecStream *stream,
724 enum zebra_recctrl_action_t action,
726 const char *recordType,
728 const char *match_criteria,
731 void *recTypeClientData)
733 ZEBRA_RES res = ZEBRA_OK;
737 res = zebra_extract_record_stream(zh, stream,
744 recType, recTypeClientData, &more);
759 static WRBUF wrbuf_hex_str(const char *cstr)
762 WRBUF w = wrbuf_alloc();
763 for (i = 0; cstr[i]; i++)
765 if (cstr[i] < ' ' || cstr[i] > 126)
766 wrbuf_printf(w, "\\%02X", cstr[i] & 0xff);
768 wrbuf_putc(w, cstr[i]);
773 ZEBRA_RES zebra_extract_record_stream(ZebraHandle zh,
774 struct ZebraRecStream *stream,
775 enum zebra_recctrl_action_t action,
777 const char *recordType,
779 const char *match_criteria,
782 void *recTypeClientData,
787 RecordAttr *recordAttr;
788 struct recExtractCtrl extractCtrl;
790 const char *matchStr = 0;
792 off_t start_offset = 0, end_offset = 0;
793 const char *pr_fname = fname; /* filename to print .. */
794 int show_progress = zh->records_processed + zh->records_skipped
795 < zh->m_file_verbose_limit ? 1:0;
797 zebra_init_log_level();
800 pr_fname = "<no file>"; /* make it printable if file is omitted */
802 zebra_rec_keys_reset(zh->reg->keys);
803 zebra_rec_keys_reset(zh->reg->sortKeys);
805 if (zebraExplain_curDatabase(zh->reg->zei, zh->basenames[0]))
807 if (zebraExplain_newDatabase(zh->reg->zei, zh->basenames[0],
808 zh->m_explain_database))
814 off_t null_offset = 0;
815 extractCtrl.stream = stream;
817 start_offset = stream->tellf(stream);
819 extractCtrl.first_record = start_offset ? 0 : 1;
821 stream->endf(stream, &null_offset);;
823 extractCtrl.init = extract_init;
824 if (zh->reg->index_types)
826 extractCtrl.tokenAdd = extract_token_add2;
830 extractCtrl.tokenAdd = extract_token_add;
832 extractCtrl.schemaAdd = extract_schema_add;
833 extractCtrl.dh = zh->reg->dh;
834 extractCtrl.handle = zh;
835 extractCtrl.match_criteria[0] = '\0';
836 extractCtrl.staticrank = 0;
837 extractCtrl.action = action;
839 init_extractCtrl(zh, &extractCtrl);
841 extract_set_store_data_prepare(&extractCtrl);
843 r = (*recType->extract)(recTypeClientData, &extractCtrl);
845 if (action == action_update)
847 action = extractCtrl.action;
852 case RECCTRL_EXTRACT_EOF:
854 case RECCTRL_EXTRACT_ERROR_GENERIC:
855 /* error occured during extraction ... */
856 yaz_log(YLOG_WARN, "extract error: generic");
858 case RECCTRL_EXTRACT_ERROR_NO_SUCH_FILTER:
859 /* error occured during extraction ... */
860 yaz_log(YLOG_WARN, "extract error: no such filter");
862 case RECCTRL_EXTRACT_SKIP:
864 yaz_log(YLOG_LOG, "skip %s %s " ZINT_FORMAT,
865 recordType, pr_fname, (zint) start_offset);
868 end_offset = stream->endf(stream, 0);
870 stream->seekf(stream, end_offset);
873 case RECCTRL_EXTRACT_OK:
876 yaz_log(YLOG_WARN, "extract error: unknown error: %d", r);
879 end_offset = stream->endf(stream, 0);
881 stream->seekf(stream, end_offset);
883 end_offset = stream->tellf(stream);
885 all_matches_add(&extractCtrl);
887 if (extractCtrl.match_criteria[0])
888 match_criteria = extractCtrl.match_criteria;
896 if (match_criteria && *match_criteria) {
897 matchStr = match_criteria;
899 if (zh->m_record_id && *zh->m_record_id) {
900 matchStr = get_match_from_spec(zh, zh->reg->keys, pr_fname,
904 yaz_log(YLOG_LOG, "error %s %s " ZINT_FORMAT, recordType,
905 pr_fname, (zint) start_offset);
912 int db_ord = zebraExplain_get_database_ord(zh->reg->zei);
913 char *rinfo = dict_lookup_ord(zh->reg->matchDict, db_ord,
917 if (log_level_extract)
919 WRBUF w = wrbuf_hex_str(matchStr);
920 yaz_log(log_level_extract, "matchStr: %s", wrbuf_cstr(w));
925 assert(*rinfo == sizeof(*sysno));
926 memcpy(sysno, rinfo+1, sizeof(*sysno));
930 if (zebra_rec_keys_empty(zh->reg->keys))
932 /* the extraction process returned no information - the record
933 is probably empty - unless flagShowRecords is in use */
941 if (action == action_delete)
943 yaz_log(YLOG_LOG, "delete %s %s " ZINT_FORMAT, recordType,
944 pr_fname, (zint) start_offset);
945 yaz_log(YLOG_WARN, "cannot delete record above (seems new)");
948 else if (action == action_replace)
950 yaz_log(YLOG_LOG, "update %s %s " ZINT_FORMAT, recordType,
951 pr_fname, (zint) start_offset);
952 yaz_log(YLOG_WARN, "cannot update record above (seems new)");
956 yaz_log(YLOG_LOG, "add %s %s " ZINT_FORMAT, recordType, pr_fname,
957 (zint) start_offset);
958 rec = rec_new(zh->reg->records);
962 recordAttr = rec_init_attr(zh->reg->zei, rec);
963 if (extractCtrl.staticrank < 0)
965 yaz_log(YLOG_WARN, "Negative staticrank for record. Set to 0");
966 extractCtrl.staticrank = 0;
971 int db_ord = zebraExplain_get_database_ord(zh->reg->zei);
972 dict_insert_ord(zh->reg->matchDict, db_ord, matchStr,
973 sizeof(*sysno), sysno);
976 extract_flush_sort_keys(zh, *sysno, 1, zh->reg->sortKeys);
978 extract_flush_record_keys2(zh, *sysno,
979 zh->reg->keys, extractCtrl.staticrank,
980 0, recordAttr->staticrank);
982 extract_flush_record_keys(zh, *sysno, 1, zh->reg->keys,
983 extractCtrl.staticrank);
985 recordAttr->staticrank = extractCtrl.staticrank;
986 zh->records_inserted++;
990 /* record already exists */
991 zebra_rec_keys_t delkeys = zebra_rec_keys_open();
992 zebra_rec_keys_t sortKeys = zebra_rec_keys_open();
993 if (action == action_insert)
995 yaz_log(YLOG_LOG, "skipped %s %s " ZINT_FORMAT,
996 recordType, pr_fname, (zint) start_offset);
1001 rec = rec_get(zh->reg->records, *sysno);
1004 recordAttr = rec_init_attr(zh->reg->zei, rec);
1006 /* decrease total size */
1007 zebraExplain_recordBytesIncrement(zh->reg->zei,
1008 - recordAttr->recordSize);
1010 zebra_rec_keys_set_buf(delkeys,
1011 rec->info[recInfo_delKeys],
1012 rec->size[recInfo_delKeys],
1014 zebra_rec_keys_set_buf(sortKeys,
1015 rec->info[recInfo_sortKeys],
1016 rec->size[recInfo_sortKeys],
1019 extract_flush_sort_keys(zh, *sysno, 0, sortKeys);
1021 extract_flush_record_keys(zh, *sysno, 0, delkeys,
1022 recordAttr->staticrank);
1024 if (action == action_delete)
1026 /* record going to be deleted */
1028 extract_flush_record_keys2(zh, *sysno, 0, recordAttr->staticrank,
1029 delkeys, recordAttr->staticrank);
1031 if (zebra_rec_keys_empty(delkeys))
1033 yaz_log(YLOG_LOG, "delete %s %s " ZINT_FORMAT, recordType,
1034 pr_fname, (zint) start_offset);
1035 yaz_log(YLOG_WARN, "cannot delete file above, "
1036 "storeKeys false (3)");
1041 yaz_log(YLOG_LOG, "delete %s %s " ZINT_FORMAT, recordType,
1042 pr_fname, (zint) start_offset);
1043 zh->records_deleted++;
1046 int db_ord = zebraExplain_get_database_ord(zh->reg->zei);
1047 dict_delete_ord(zh->reg->matchDict, db_ord, matchStr);
1049 rec_del(zh->reg->records, &rec);
1051 zebra_rec_keys_close(delkeys);
1052 zebra_rec_keys_close(sortKeys);
1058 { /* update or special_update */
1060 yaz_log(YLOG_LOG, "update %s %s " ZINT_FORMAT, recordType,
1061 pr_fname, (zint) start_offset);
1062 extract_flush_sort_keys(zh, *sysno, 1, zh->reg->sortKeys);
1065 extract_flush_record_keys2(zh, *sysno,
1066 zh->reg->keys, extractCtrl.staticrank,
1067 delkeys, recordAttr->staticrank);
1069 extract_flush_record_keys(zh, *sysno, 1,
1070 zh->reg->keys, extractCtrl.staticrank);
1072 recordAttr->staticrank = extractCtrl.staticrank;
1073 zh->records_updated++;
1075 zebra_rec_keys_close(delkeys);
1076 zebra_rec_keys_close(sortKeys);
1078 /* update file type */
1079 xfree(rec->info[recInfo_fileType]);
1080 rec->info[recInfo_fileType] =
1081 rec_strdup(recordType, &rec->size[recInfo_fileType]);
1083 /* update filename */
1084 xfree(rec->info[recInfo_filename]);
1085 rec->info[recInfo_filename] =
1086 rec_strdup(fname, &rec->size[recInfo_filename]);
1088 /* update delete keys */
1089 xfree(rec->info[recInfo_delKeys]);
1090 if (!zebra_rec_keys_empty(zh->reg->keys) && zh->m_store_keys == 1)
1092 zebra_rec_keys_get_buf(zh->reg->keys,
1093 &rec->info[recInfo_delKeys],
1094 &rec->size[recInfo_delKeys]);
1098 rec->info[recInfo_delKeys] = NULL;
1099 rec->size[recInfo_delKeys] = 0;
1101 /* update sort keys */
1102 xfree(rec->info[recInfo_sortKeys]);
1104 zebra_rec_keys_get_buf(zh->reg->sortKeys,
1105 &rec->info[recInfo_sortKeys],
1106 &rec->size[recInfo_sortKeys]);
1110 recordAttr->recordSize = end_offset - start_offset;
1111 zebraExplain_recordBytesIncrement(zh->reg->zei,
1112 recordAttr->recordSize);
1115 /* set run-number for this record */
1116 recordAttr->runNumber =
1117 zebraExplain_runNumberIncrement(zh->reg->zei, 0);
1119 /* update store data */
1120 xfree(rec->info[recInfo_storeData]);
1122 /* update store data */
1123 if (zh->store_data_buf)
1125 rec->size[recInfo_storeData] = zh->store_data_size;
1126 rec->info[recInfo_storeData] = zh->store_data_buf;
1127 zh->store_data_buf = 0;
1128 recordAttr->recordSize = zh->store_data_size;
1130 else if (zh->m_store_data)
1132 off_t cur_offset = stream->tellf(stream);
1134 rec->size[recInfo_storeData] = recordAttr->recordSize;
1135 rec->info[recInfo_storeData] = (char *)
1136 xmalloc(recordAttr->recordSize);
1137 stream->seekf(stream, start_offset);
1138 stream->readf(stream, rec->info[recInfo_storeData],
1139 recordAttr->recordSize);
1140 stream->seekf(stream, cur_offset);
1144 rec->info[recInfo_storeData] = NULL;
1145 rec->size[recInfo_storeData] = 0;
1147 /* update database name */
1148 xfree(rec->info[recInfo_databaseName]);
1149 rec->info[recInfo_databaseName] =
1150 rec_strdup(zh->basenames[0], &rec->size[recInfo_databaseName]);
1153 recordAttr->recordOffset = start_offset;
1155 /* commit this record */
1156 rec_put(zh->reg->records, &rec);
1161 ZEBRA_RES zebra_extract_explain(void *handle, Record rec, data1_node *n)
1163 ZebraHandle zh = (ZebraHandle) handle;
1164 struct recExtractCtrl extractCtrl;
1166 if (zebraExplain_curDatabase(zh->reg->zei,
1167 rec->info[recInfo_databaseName]))
1170 if (zebraExplain_newDatabase(zh->reg->zei,
1171 rec->info[recInfo_databaseName], 0))
1175 zebra_rec_keys_reset(zh->reg->keys);
1176 zebra_rec_keys_reset(zh->reg->sortKeys);
1178 extractCtrl.init = extract_init;
1179 extractCtrl.tokenAdd = extract_token_add;
1180 extractCtrl.schemaAdd = extract_schema_add;
1181 extractCtrl.dh = zh->reg->dh;
1183 init_extractCtrl(zh, &extractCtrl);
1185 extractCtrl.flagShowRecords = 0;
1186 extractCtrl.match_criteria[0] = '\0';
1187 extractCtrl.staticrank = 0;
1188 extractCtrl.action = action_update;
1190 extractCtrl.handle = handle;
1191 extractCtrl.first_record = 1;
1193 extract_set_store_data_prepare(&extractCtrl);
1196 grs_extract_tree(&extractCtrl, n);
1198 if (rec->size[recInfo_delKeys])
1200 zebra_rec_keys_t delkeys = zebra_rec_keys_open();
1202 zebra_rec_keys_t sortkeys = zebra_rec_keys_open();
1204 zebra_rec_keys_set_buf(delkeys, rec->info[recInfo_delKeys],
1205 rec->size[recInfo_delKeys],
1208 extract_flush_record_keys2(zh, rec->sysno,
1209 zh->reg->keys, 0, delkeys, 0);
1211 extract_flush_record_keys(zh, rec->sysno, 0, delkeys, 0);
1212 extract_flush_record_keys(zh, rec->sysno, 1, zh->reg->keys, 0);
1214 zebra_rec_keys_close(delkeys);
1216 zebra_rec_keys_set_buf(sortkeys, rec->info[recInfo_sortKeys],
1217 rec->size[recInfo_sortKeys],
1220 extract_flush_sort_keys(zh, rec->sysno, 0, sortkeys);
1221 zebra_rec_keys_close(sortkeys);
1226 extract_flush_record_keys2(zh, rec->sysno, zh->reg->keys, 0, 0, 0);
1228 extract_flush_record_keys(zh, rec->sysno, 1, zh->reg->keys, 0);
1231 extract_flush_sort_keys(zh, rec->sysno, 1, zh->reg->sortKeys);
1233 xfree(rec->info[recInfo_delKeys]);
1234 zebra_rec_keys_get_buf(zh->reg->keys,
1235 &rec->info[recInfo_delKeys],
1236 &rec->size[recInfo_delKeys]);
1238 xfree(rec->info[recInfo_sortKeys]);
1239 zebra_rec_keys_get_buf(zh->reg->sortKeys,
1240 &rec->info[recInfo_sortKeys],
1241 &rec->size[recInfo_sortKeys]);
1245 void extract_rec_keys_log(ZebraHandle zh, int is_insert,
1246 zebra_rec_keys_t reckeys,
1249 if (zebra_rec_keys_rewind(reckeys))
1254 NMEM nmem = nmem_create();
1256 while(zebra_rec_keys_read(reckeys, &str, &slen, &key))
1258 char keystr[200]; /* room for zints to print */
1260 int ord = CAST_ZINT_TO_INT(key.mem[0]);
1261 const char *index_type;
1263 const char *string_index;
1265 zebraExplain_lookup_ord(zh->reg->zei, ord, &index_type,
1266 0/* db */, &string_index);
1268 zebra_term_untrans_iconv(zh, nmem, index_type,
1271 for (i = 0; i<key.len; i++)
1273 sprintf(keystr + strlen(keystr), ZINT_FORMAT " ", key.mem[i]);
1276 if (*str < CHR_BASE_CHAR)
1279 char dst_buf[200]; /* room for special chars */
1281 strcpy(dst_buf , "?");
1283 if (!strcmp(str, ""))
1284 strcpy(dst_buf, "alwaysmatches");
1285 if (!strcmp(str, FIRST_IN_FIELD_STR))
1286 strcpy(dst_buf, "firstinfield");
1287 else if (!strcmp(str, CHR_UNKNOWN))
1288 strcpy(dst_buf, "unknown");
1289 else if (!strcmp(str, CHR_SPACE))
1290 strcpy(dst_buf, "space");
1292 for (i = 0; i<slen; i++)
1294 sprintf(dst_buf + strlen(dst_buf), " %d", str[i] & 0xff);
1296 yaz_log(level, "%s%s %s %s", keystr, index_type,
1297 string_index, dst_buf);
1301 yaz_log(level, "%s%s %s \"%s\"", keystr, index_type,
1302 string_index, dst_term);
1310 void extract_rec_keys_adjust(ZebraHandle zh, int is_insert,
1311 zebra_rec_keys_t reckeys)
1313 ZebraExplainInfo zei = zh->reg->zei;
1317 struct ord_stat *next;
1320 if (zebra_rec_keys_rewind(reckeys))
1322 struct ord_stat *ord_list = 0;
1326 struct it_key key_in;
1327 while(zebra_rec_keys_read(reckeys, &str, &slen, &key_in))
1329 int ord = CAST_ZINT_TO_INT(key_in.mem[0]);
1331 for (p = ord_list; p ; p = p->next)
1339 p = xmalloc(sizeof(*p));
1350 struct ord_stat *p1 = p;
1353 zebraExplain_ord_adjust_occurrences(zei, p->ord, p->no, 1);
1355 zebraExplain_ord_adjust_occurrences(zei, p->ord, - p->no, -1);
1362 void extract_flush_record_keys2(ZebraHandle zh, zint sysno,
1363 zebra_rec_keys_t ins_keys, zint ins_rank,
1364 zebra_rec_keys_t del_keys, zint del_rank)
1366 ZebraExplainInfo zei = zh->reg->zei;
1370 if (!zh->reg->key_block)
1372 int mem = 1024*1024 * atoi( res_get_def( zh->res, "memmax", "8"));
1373 const char *key_tmp_dir = res_get_def(zh->res, "keyTmpDir", ".");
1374 int use_threads = atoi(res_get_def(zh->res, "threads", "1"));
1375 zh->reg->key_block = key_block_create(mem, key_tmp_dir, use_threads);
1380 extract_rec_keys_adjust(zh, 1, ins_keys);
1382 zebraExplain_recordCountIncrement(zei, 1);
1383 zebra_rec_keys_rewind(ins_keys);
1387 extract_rec_keys_adjust(zh, 0, del_keys);
1389 zebraExplain_recordCountIncrement(zei, -1);
1390 zebra_rec_keys_rewind(del_keys);
1396 const char *del_str;
1397 struct it_key del_key_in;
1401 const char *ins_str;
1402 struct it_key ins_key_in;
1406 del = zebra_rec_keys_read(del_keys, &del_str, &del_slen,
1409 ins = zebra_rec_keys_read(ins_keys, &ins_str, &ins_slen,
1412 if (del && ins && ins_rank == del_rank
1413 && !key_compare(&del_key_in, &ins_key_in)
1414 && ins_slen == del_slen && !memcmp(del_str, ins_str, del_slen))
1424 key_block_write(zh->reg->key_block, sysno,
1425 &del_key_in, 0, del_str, del_slen,
1426 del_rank, zh->m_staticrank);
1428 key_block_write(zh->reg->key_block, sysno,
1429 &ins_key_in, 1, ins_str, ins_slen,
1430 ins_rank, zh->m_staticrank);
1432 yaz_log(log_level_extract, "normal=%d optimized=%d", normal, optimized);
1436 ZEBRA_RES zebra_rec_keys_to_snippets(ZebraHandle zh,
1437 zebra_rec_keys_t reckeys,
1438 zebra_snippets *snippets)
1440 NMEM nmem = nmem_create();
1441 if (zebra_rec_keys_rewind(reckeys))
1446 while (zebra_rec_keys_read(reckeys, &str, &slen, &key))
1451 const char *index_type;
1453 assert(key.len <= IT_KEY_LEVEL_MAX && key.len > 2);
1454 seqno = key.mem[key.len-1];
1455 ord = CAST_ZINT_TO_INT(key.mem[0]);
1457 zebraExplain_lookup_ord(zh->reg->zei, ord, &index_type,
1458 0/* db */, 0 /* string_index */);
1460 zebra_term_untrans_iconv(zh, nmem, index_type,
1462 zebra_snippets_append(snippets, seqno, 0, ord, dst_term);
1470 void print_rec_keys(ZebraHandle zh, zebra_rec_keys_t reckeys)
1472 yaz_log(YLOG_LOG, "print_rec_keys");
1473 if (zebra_rec_keys_rewind(reckeys))
1478 while (zebra_rec_keys_read(reckeys, &str, &slen, &key))
1480 char dst_buf[IT_MAX_WORD];
1482 const char *index_type;
1483 int ord = CAST_ZINT_TO_INT(key.mem[0]);
1485 assert(key.len <= IT_KEY_LEVEL_MAX && key.len > 2);
1487 zebraExplain_lookup_ord(zh->reg->zei, ord, &index_type, &db, 0);
1489 seqno = key.mem[key.len-1];
1491 zebra_term_untrans(zh, index_type, dst_buf, str);
1493 yaz_log(YLOG_LOG, "ord=%d seqno=" ZINT_FORMAT
1494 " term=%s", ord, seqno, dst_buf);
1499 static void extract_add_index_string(RecWord *p, zinfo_index_category_t cat,
1500 const char *str, int length)
1503 ZebraHandle zh = p->extractCtrl->handle;
1504 ZebraExplainInfo zei = zh->reg->zei;
1507 ch = zebraExplain_lookup_attr_str(zei, cat, p->index_type, p->index_name);
1509 ch = zebraExplain_add_attr_str(zei, cat, p->index_type, p->index_name);
1513 key.mem[i++] = p->record_id;
1514 key.mem[i++] = p->section_id;
1516 if (zh->m_segment_indexing)
1517 key.mem[i++] = p->segment;
1518 key.mem[i++] = p->seqno;
1521 zebra_rec_keys_write(zh->reg->keys, str, length, &key);
1524 static void extract_add_sort_string(RecWord *p, const char *str, int length)
1527 ZebraHandle zh = p->extractCtrl->handle;
1528 ZebraExplainInfo zei = zh->reg->zei;
1530 zinfo_index_category_t cat = zinfo_index_category_sort;
1532 ch = zebraExplain_lookup_attr_str(zei, cat, p->index_type, p->index_name);
1534 ch = zebraExplain_add_attr_str(zei, cat, p->index_type, p->index_name);
1537 key.mem[1] = p->record_id;
1539 zebra_rec_keys_write(zh->reg->sortKeys, str, length, &key);
1542 static void extract_add_staticrank_string(RecWord *p,
1543 const char *str, int length)
1546 struct recExtractCtrl *ctrl = p->extractCtrl;
1548 if (length > sizeof(valz)-1)
1549 length = sizeof(valz)-1;
1551 memcpy(valz, str, length);
1552 valz[length] = '\0';
1553 ctrl->staticrank = atozint(valz);
1556 static void extract_add_string(RecWord *p, zebra_map_t zm,
1557 const char *string, int length)
1564 if (zebra_maps_is_index(zm))
1566 extract_add_index_string(p, zinfo_index_category_index,
1568 if (zebra_maps_is_alwaysmatches(zm))
1571 memcpy(&word, p, sizeof(word));
1574 extract_add_index_string(
1575 &word, zinfo_index_category_alwaysmatches, "", 0);
1578 else if (zebra_maps_is_sort(zm))
1580 extract_add_sort_string(p, string, length);
1582 else if (zebra_maps_is_staticrank(zm))
1584 extract_add_staticrank_string(p, string, length);
1588 static void extract_add_incomplete_field(RecWord *p, zebra_map_t zm)
1590 const char *b = p->term_buf;
1591 int remain = p->term_len;
1593 const char **map = 0;
1596 map = zebra_maps_input(zm, &b, remain, 0);
1600 char buf[IT_MAX_WORD+1];
1604 while (map && *map && **map == *CHR_SPACE)
1606 remain = p->term_len - (b - p->term_buf);
1608 map = zebra_maps_input(zm, &b, remain, 0);
1615 while (map && *map && **map != *CHR_SPACE)
1617 const char *cp = *map;
1619 while (i < IT_MAX_WORD && *cp)
1621 remain = p->term_len - (b - p->term_buf);
1623 map = zebra_maps_input(zm, &b, remain, 0);
1633 if (zebra_maps_is_first_in_field(zm))
1635 /* first in field marker */
1636 extract_add_string(p, zm, FIRST_IN_FIELD_STR, FIRST_IN_FIELD_LEN);
1640 extract_add_string(p, zm, buf, i);
1645 static void extract_add_complete_field(RecWord *p, zebra_map_t zm)
1647 const char *b = p->term_buf;
1648 char buf[IT_MAX_WORD+1];
1649 const char **map = 0;
1650 int i = 0, remain = p->term_len;
1653 map = zebra_maps_input(zm, &b, remain, 1);
1655 while (remain > 0 && i < IT_MAX_WORD)
1657 while (map && *map && **map == *CHR_SPACE)
1659 remain = p->term_len - (b - p->term_buf);
1663 int first = i ? 0 : 1; /* first position */
1664 map = zebra_maps_input(zm, &b, remain, first);
1672 if (i && i < IT_MAX_WORD)
1673 buf[i++] = *CHR_SPACE;
1674 while (map && *map && **map != *CHR_SPACE)
1676 const char *cp = *map;
1678 if (**map == *CHR_CUT)
1684 if (i >= IT_MAX_WORD)
1686 while (i < IT_MAX_WORD && *cp)
1689 remain = p->term_len - (b - p->term_buf);
1692 map = zebra_maps_input(zm, &b, remain, 0);
1700 extract_add_string(p, zm, buf, i);
1703 static void extract_token_add2_index(ZebraHandle zh, zebra_index_type_t type,
1707 const char *res_buf = 0;
1709 int r = zebra_index_type_tokenize(type, p->term_buf, p->term_len,
1710 &res_buf, &res_len);
1711 int cat = zinfo_index_category_index;
1712 int ch = zebraExplain_lookup_attr_str(zh->reg->zei, cat, p->index_type, p->index_name);
1714 ch = zebraExplain_add_attr_str(zh->reg->zei, cat, p->index_type, p->index_name);
1719 key.mem[i++] = p->record_id;
1720 key.mem[i++] = p->section_id;
1722 if (zh->m_segment_indexing)
1723 key.mem[i++] = p->segment;
1724 key.mem[i++] = p->seqno;
1727 yaz_log(YLOG_LOG, "keys_write %.*s", (int) res_len, res_buf);
1728 zebra_rec_keys_write(zh->reg->keys, res_buf, res_len, &key);
1731 r = zebra_index_type_tokenize(type, 0, 0, &res_buf, &res_len);
1735 static void extract_token_add2(RecWord *p)
1737 ZebraHandle zh = p->extractCtrl->handle;
1738 zebra_index_type_t type = zebra_index_type_get(zh->reg->index_types, p->index_type);
1741 if (zebra_index_type_is_index(type))
1743 extract_token_add2_index(zh, type, p);
1745 else if (zebra_index_type_is_sort(type))
1753 /** \brief top-level indexing handler for recctrl system
1754 \param p token data to be indexed
1758 zebra_add_{in}_complete
1761 extract_add_index_string
1763 extract_add_sort_string
1765 extract_add_staticrank_string
1768 static void extract_token_add(RecWord *p)
1770 ZebraHandle zh = p->extractCtrl->handle;
1771 zebra_map_t zm = zebra_map_get_or_add(zh->reg->zebra_maps, p->index_type);
1774 if (log_level_details)
1776 yaz_log(log_level_details, "extract_token_add "
1777 "type=%s index=%s seqno=" ZINT_FORMAT " s=%.*s",
1778 p->index_type, p->index_name,
1779 p->seqno, p->term_len, p->term_buf);
1781 if ((wrbuf = zebra_replace(zm, 0, p->term_buf, p->term_len)))
1783 p->term_buf = wrbuf_buf(wrbuf);
1784 p->term_len = wrbuf_len(wrbuf);
1786 if (zebra_maps_is_complete(zm))
1787 extract_add_complete_field(p, zm);
1789 extract_add_incomplete_field(p, zm);
1792 static void extract_set_store_data_cb(struct recExtractCtrl *p,
1793 void *buf, size_t sz)
1795 ZebraHandle zh = (ZebraHandle) p->handle;
1797 xfree(zh->store_data_buf);
1798 zh->store_data_buf = 0;
1799 zh->store_data_size = 0;
1802 zh->store_data_buf = xmalloc(sz);
1803 zh->store_data_size = sz;
1804 memcpy(zh->store_data_buf, buf, sz);
1808 static void extract_set_store_data_prepare(struct recExtractCtrl *p)
1810 ZebraHandle zh = (ZebraHandle) p->handle;
1811 xfree(zh->store_data_buf);
1812 zh->store_data_buf = 0;
1813 zh->store_data_size = 0;
1814 p->setStoreData = extract_set_store_data_cb;
1817 static void extract_schema_add(struct recExtractCtrl *p, Odr_oid *oid)
1819 ZebraHandle zh = (ZebraHandle) p->handle;
1820 zebraExplain_addSchema(zh->reg->zei, oid);
1823 void extract_flush_sort_keys(ZebraHandle zh, zint sysno,
1824 int cmd, zebra_rec_keys_t reckeys)
1827 yaz_log(YLOG_LOG, "extract_flush_sort_keys cmd=%d sysno=" ZINT_FORMAT,
1829 extract_rec_keys_log(zh, cmd, reckeys, YLOG_LOG);
1832 if (zebra_rec_keys_rewind(reckeys))
1834 zebra_sort_index_t si = zh->reg->sort_index;
1837 struct it_key key_in;
1839 zebra_sort_sysno(si, sysno);
1841 while (zebra_rec_keys_read(reckeys, &str, &slen, &key_in))
1843 int ord = CAST_ZINT_TO_INT(key_in.mem[0]);
1845 zebra_sort_type(si, ord);
1847 zebra_sort_add(si, str, slen);
1849 zebra_sort_delete(si);
1857 * indent-tabs-mode: nil
1859 * vim: shiftwidth=4 tabstop=8 expandtab