1 /* $Id: extract.c,v 1.276 2007-12-20 11:21:29 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);
75 static void check_log_limit(ZebraHandle zh)
77 if (zh->records_processed + zh->records_skipped == zh->m_file_verbose_limit)
79 yaz_log(YLOG_LOG, "More than %d file log entries. Omitting rest",
80 zh->m_file_verbose_limit);
84 static void logRecord(ZebraHandle zh)
87 ++zh->records_processed;
88 if (!(zh->records_processed % 1000))
90 yaz_log(YLOG_LOG, "Records: "ZINT_FORMAT" i/u/d "
91 ZINT_FORMAT"/"ZINT_FORMAT"/"ZINT_FORMAT,
92 zh->records_processed, zh->records_inserted,
93 zh->records_updated, zh->records_deleted);
97 static void init_extractCtrl(ZebraHandle zh, struct recExtractCtrl *ctrl)
99 ctrl->flagShowRecords = !zh->m_flag_rw;
103 static void extract_add_index_string(RecWord *p,
104 zinfo_index_category_t cat,
105 const char *str, int length);
107 static void extract_set_store_data_prepare(struct recExtractCtrl *p);
109 static void extract_init(struct recExtractCtrl *p, RecWord *w)
112 w->index_name = "any";
120 struct snip_rec_info {
122 zebra_snippets *snippets;
126 static void snippet_add_complete_field(RecWord *p, int ord,
129 struct snip_rec_info *h = p->extractCtrl->handle;
131 const char *b = p->term_buf;
132 char buf[IT_MAX_WORD+1];
133 const char **map = 0;
134 int i = 0, remain = p->term_len;
135 const char *start = b;
136 const char *last = 0;
139 map = zebra_maps_input(zm, &b, remain, 1);
141 while (remain > 0 && i < IT_MAX_WORD)
143 while (map && *map && **map == *CHR_SPACE)
145 remain = p->term_len - (b - p->term_buf);
148 start = b; /* set to first non-ws area */
151 int first = i ? 0 : 1; /* first position */
153 map = zebra_maps_input(zm, &b, remain, first);
161 if (i && i < IT_MAX_WORD)
162 buf[i++] = *CHR_SPACE;
163 while (map && *map && **map != *CHR_SPACE)
165 const char *cp = *map;
167 if (**map == *CHR_CUT)
173 if (i >= IT_MAX_WORD)
175 while (i < IT_MAX_WORD && *cp)
179 remain = p->term_len - (b - p->term_buf);
182 map = zebra_maps_input(zm, &b, remain, 0);
190 if (last && start != last)
191 zebra_snippets_appendn(h->snippets, p->seqno, 0, ord,
192 start, last - start);
195 static void snippet_add_incomplete_field(RecWord *p, int ord, zebra_map_t zm)
197 struct snip_rec_info *h = p->extractCtrl->handle;
198 const char *b = p->term_buf;
199 int remain = p->term_len;
201 const char **map = 0;
202 const char *start = b;
203 const char *last = b;
206 map = zebra_maps_input(zm, &b, remain, 0);
210 char buf[IT_MAX_WORD+1];
214 while (map && *map && **map == *CHR_SPACE)
216 remain = p->term_len - (b - p->term_buf);
219 map = zebra_maps_input(zm, &b, remain, 0);
227 zebra_snippets_appendn(h->snippets, p->seqno, 1, ord,
228 start, last - start);
234 while (map && *map && **map != *CHR_SPACE)
236 const char *cp = *map;
238 while (i < IT_MAX_WORD && *cp)
240 remain = p->term_len - (b - p->term_buf);
243 map = zebra_maps_input(zm, &b, remain, 0);
253 if (zebra_maps_is_first_in_field(zm))
255 /* first in field marker */
260 zebra_snippets_appendn(h->snippets, p->seqno, 0, ord,
261 start, last - start);
268 static void snippet_add_icu(RecWord *p, int ord, zebra_map_t zm)
270 struct snip_rec_info *h = p->extractCtrl->handle;
272 const char *res_buf = 0;
275 const char *display_buf = 0;
276 size_t display_len = 0;
278 zebra_map_tokenize_start(zm, p->term_buf, p->term_len);
279 while (zebra_map_tokenize_next(zm, &res_buf, &res_len,
280 &display_buf, &display_len))
282 zebra_snippets_appendn(h->snippets, p->seqno, 0, ord,
283 display_buf, display_len);
288 static void snippet_token_add(RecWord *p)
290 struct snip_rec_info *h = p->extractCtrl->handle;
291 ZebraHandle zh = h->zh;
292 zebra_map_t zm = zebra_map_get(zh->reg->zebra_maps, p->index_type);
294 if (zm && zebra_maps_is_index(zm))
296 ZebraExplainInfo zei = zh->reg->zei;
297 int ch = zebraExplain_lookup_attr_str(
298 zei, zinfo_index_category_index, p->index_type, p->index_name);
300 if (zebra_maps_is_icu(zm))
301 snippet_add_icu(p, ch, zm);
304 if (zebra_maps_is_complete(zm))
305 snippet_add_complete_field(p, ch, zm);
307 snippet_add_incomplete_field(p, ch, zm);
312 static void snippet_schema_add(
313 struct recExtractCtrl *p, Odr_oid *oid)
318 void extract_snippet(ZebraHandle zh, zebra_snippets *sn,
319 struct ZebraRecStream *stream,
320 RecType rt, void *recTypeClientData)
322 struct recExtractCtrl extractCtrl;
323 struct snip_rec_info info;
326 extractCtrl.stream = stream;
327 extractCtrl.first_record = 1;
328 extractCtrl.init = extract_init;
329 extractCtrl.tokenAdd = snippet_token_add;
330 extractCtrl.schemaAdd = snippet_schema_add;
334 extractCtrl.dh = zh->reg->dh;
338 extractCtrl.handle = &info;
339 extractCtrl.match_criteria[0] = '\0';
340 extractCtrl.staticrank = 0;
341 extractCtrl.action = action_insert;
343 init_extractCtrl(zh, &extractCtrl);
345 extractCtrl.setStoreData = 0;
347 r = (*rt->extract)(recTypeClientData, &extractCtrl);
351 static void searchRecordKey(ZebraHandle zh,
352 zebra_rec_keys_t reckeys,
353 const char *index_name,
354 const char **ws, int ws_length)
358 zinfo_index_category_t cat = zinfo_index_category_index;
360 for (i = 0; i<ws_length; i++)
364 ch = zebraExplain_lookup_attr_str(zh->reg->zei, cat, "0", index_name);
366 ch = zebraExplain_lookup_attr_str(zh->reg->zei, cat, "p", index_name);
368 ch = zebraExplain_lookup_attr_str(zh->reg->zei, cat, "w", index_name);
373 if (zebra_rec_keys_rewind(reckeys))
380 while (zebra_rec_keys_read(reckeys, &str, &slen, &key))
382 assert(key.len <= IT_KEY_LEVEL_MAX && key.len > 2);
384 seqno = key.mem[key.len-1];
386 if (key.mem[0] == ch)
392 woff = seqno - startSeq;
393 if (woff >= 0 && woff < ws_length)
400 #define FILE_MATCH_BLANK "\t "
402 static char *get_match_from_spec(ZebraHandle zh,
403 zebra_rec_keys_t reckeys,
404 const char *fname, const char *spec)
406 static char dstBuf[2048]; /* static here ??? */
408 const char *s = spec;
412 for (; *s && strchr(FILE_MATCH_BLANK, *s); s++)
419 char attset_str[64], attname_str[64];
423 for (s++; strchr(FILE_MATCH_BLANK, *s); s++)
425 for (i = 0; *s && *s != ',' && *s != ')' &&
426 !strchr(FILE_MATCH_BLANK, *s); s++)
427 if (i+1 < sizeof(attset_str))
428 attset_str[i++] = *s;
429 attset_str[i] = '\0';
431 for (; strchr(FILE_MATCH_BLANK, *s); s++)
434 strcpy(attname_str, attset_str);
437 for (s++; strchr(FILE_MATCH_BLANK, *s); s++)
439 for (i = 0; *s && *s != ')' &&
440 !strchr(FILE_MATCH_BLANK, *s); s++)
441 if (i+1 < sizeof(attname_str))
442 attname_str[i++] = *s;
443 attname_str[i] = '\0';
446 searchRecordKey(zh, reckeys, attname_str, ws, 32);
450 yaz_log(YLOG_WARN, "Missing ) in match criteria %s in group %s",
451 spec, zh->m_group ? zh->m_group : "none");
456 for (i = 0; i<32; i++)
465 dst += strlen(ws[i]);
469 yaz_log(YLOG_WARN, "Record didn't contain match"
470 " fields in (%s,%s)", attset_str, attname_str);
478 const char *spec_src = NULL;
479 const char *s1 = ++s;
480 while (*s1 && !strchr(FILE_MATCH_BLANK, *s1))
484 if (spec_len > sizeof(special)-1)
485 spec_len = sizeof(special)-1;
486 memcpy(special, s, spec_len);
487 special[spec_len] = '\0';
490 if (!strcmp(special, "group"))
491 spec_src = zh->m_group;
492 else if (!strcmp(special, "database"))
493 spec_src = zh->basenames[0];
494 else if (!strcmp(special, "filename")) {
497 else if (!strcmp(special, "type"))
498 spec_src = zh->m_record_type;
503 strcpy(dst, spec_src);
504 dst += strlen(spec_src);
507 else if (*s == '\"' || *s == '\'')
509 int stopMarker = *s++;
513 while (*s && *s != stopMarker)
515 if (i+1 < sizeof(tmpString))
516 tmpString[i++] = *s++;
521 strcpy(dst, tmpString);
522 dst += strlen(tmpString);
526 yaz_log(YLOG_WARN, "Syntax error in match criteria %s in group %s",
527 spec, zh->m_group ? zh->m_group : "none");
534 yaz_log(YLOG_WARN, "No match criteria for record %s in group %s",
535 fname, zh->m_group ? zh->m_group : "none");
542 struct recordLogInfo {
545 struct recordGroup *rGroup;
548 /** \brief add the always-matches index entry and map to real record ID
549 \param ctrl record control
550 \param record_id custom record ID
551 \param sysno system record ID
553 This function serves two purposes.. It adds the always matches
554 entry and makes a pointer from the custom record ID (if defined)
555 back to the system record ID (sysno)
556 See zebra_recid_to_sysno .
558 static void all_matches_add(struct recExtractCtrl *ctrl, zint record_id,
562 extract_init(ctrl, &word);
563 word.record_id = record_id;
564 /* we use the seqno as placeholder for a way to get back to
565 record database from _ALLRECORDS.. This is used if a custom
566 RECORD was defined */
568 word.index_name = "_ALLRECORDS";
569 word.index_type = "w";
571 extract_add_index_string(&word, zinfo_index_category_alwaysmatches,
575 ZEBRA_RES zebra_extract_records_stream(ZebraHandle zh,
576 struct ZebraRecStream *stream,
577 enum zebra_recctrl_action_t action,
579 const char *recordType,
581 const char *match_criteria,
584 void *recTypeClientData);
587 ZEBRA_RES zebra_extract_file(ZebraHandle zh, zint *sysno, const char *fname,
588 enum zebra_recctrl_action_t action)
590 ZEBRA_RES r = ZEBRA_OK;
595 struct file_read_info *fi = 0;
596 const char *original_record_type = 0;
598 void *recTypeClientData;
599 struct ZebraRecStream stream, *streamp;
601 zebra_init_log_level();
603 if (!zh->m_group || !*zh->m_group)
606 sprintf(gprefix, "%s.", zh->m_group);
608 yaz_log(log_level_extract, "zebra_extract_file %s", fname);
610 /* determine file extension */
612 for (i = strlen(fname); --i >= 0; )
615 else if (fname[i] == '.')
617 strcpy(ext, fname+i+1);
620 /* determine file type - depending on extension */
621 original_record_type = zh->m_record_type;
622 if (!zh->m_record_type)
624 sprintf(ext_res, "%srecordType.%s", gprefix, ext);
625 zh->m_record_type = res_get(zh->res, ext_res);
627 if (!zh->m_record_type)
630 if (zh->records_processed + zh->records_skipped
631 < zh->m_file_verbose_limit)
632 yaz_log(YLOG_LOG, "? %s", fname);
633 zh->records_skipped++;
636 /* determine match criteria */
637 if (!zh->m_record_id)
639 sprintf(ext_res, "%srecordId.%s", gprefix, ext);
640 zh->m_record_id = res_get(zh->res, ext_res);
644 recType_byName(zh->reg->recTypes, zh->res, zh->m_record_type,
645 &recTypeClientData)))
647 yaz_log(YLOG_WARN, "No such record type: %s", zh->m_record_type);
651 switch(recType->version)
656 yaz_log(YLOG_WARN, "Bad filter version: %s", zh->m_record_type);
658 if (sysno && (action == action_delete || action == action_a_delete))
667 if (zh->path_reg && !yaz_is_abspath(fname))
669 strcpy(full_rep, zh->path_reg);
670 strcat(full_rep, "/");
671 strcat(full_rep, fname);
674 strcpy(full_rep, fname);
676 if ((fd = open(full_rep, O_BINARY|O_RDONLY)) == -1)
678 yaz_log(YLOG_WARN|YLOG_ERRNO, "open %s", full_rep);
679 zh->m_record_type = original_record_type;
683 zebra_create_stream_fd(streamp, fd, 0);
685 r = zebra_extract_records_stream(zh, streamp,
690 0, /*match_criteria */
692 recType, recTypeClientData);
694 stream.destroy(streamp);
695 zh->m_record_type = original_record_type;
700 If sysno is provided, then it's used to identify the reocord.
701 If not, and match_criteria is provided, then sysno is guessed
702 If not, and a record is provided, then sysno is got from there
706 ZEBRA_RES zebra_buffer_extract_record(ZebraHandle zh,
707 const char *buf, size_t buf_size,
708 enum zebra_recctrl_action_t action,
710 const char *recordType,
712 const char *match_criteria,
715 struct ZebraRecStream stream;
720 if (recordType && *recordType)
722 yaz_log(log_level_extract,
723 "Record type explicitly specified: %s", recordType);
724 recType = recType_byName(zh->reg->recTypes, zh->res, recordType,
729 if (!(zh->m_record_type))
731 yaz_log(YLOG_WARN, "No such record type defined");
734 yaz_log(log_level_extract, "Get record type from rgroup: %s",
736 recType = recType_byName(zh->reg->recTypes, zh->res,
737 zh->m_record_type, &clientData);
738 recordType = zh->m_record_type;
743 yaz_log(YLOG_WARN, "No such record type: %s", recordType);
747 zebra_create_stream_mem(&stream, buf, buf_size);
749 res = zebra_extract_records_stream(zh, &stream,
756 recType, clientData);
757 stream.destroy(&stream);
761 ZEBRA_RES zebra_extract_records_stream(ZebraHandle zh,
762 struct ZebraRecStream *stream,
763 enum zebra_recctrl_action_t action,
765 const char *recordType,
767 const char *match_criteria,
770 void *recTypeClientData)
772 ZEBRA_RES res = ZEBRA_OK;
776 res = zebra_extract_record_stream(zh, stream,
783 recType, recTypeClientData, &more);
798 static WRBUF wrbuf_hex_str(const char *cstr)
801 WRBUF w = wrbuf_alloc();
802 for (i = 0; cstr[i]; i++)
804 if (cstr[i] < ' ' || cstr[i] > 126)
805 wrbuf_printf(w, "\\%02X", cstr[i] & 0xff);
807 wrbuf_putc(w, cstr[i]);
812 ZEBRA_RES zebra_extract_record_stream(ZebraHandle zh,
813 struct ZebraRecStream *stream,
814 enum zebra_recctrl_action_t action,
816 const char *recordType,
818 const char *match_criteria,
821 void *recTypeClientData,
826 RecordAttr *recordAttr;
827 struct recExtractCtrl extractCtrl;
829 const char *matchStr = 0;
831 off_t start_offset = 0, end_offset = 0;
832 const char *pr_fname = fname; /* filename to print .. */
833 int show_progress = zh->records_processed + zh->records_skipped
834 < zh->m_file_verbose_limit ? 1:0;
836 zebra_init_log_level();
839 pr_fname = "<no file>"; /* make it printable if file is omitted */
841 zebra_rec_keys_reset(zh->reg->keys);
842 zebra_rec_keys_reset(zh->reg->sortKeys);
844 if (zebraExplain_curDatabase(zh->reg->zei, zh->basenames[0]))
846 if (zebraExplain_newDatabase(zh->reg->zei, zh->basenames[0],
847 zh->m_explain_database))
853 off_t null_offset = 0;
854 extractCtrl.stream = stream;
856 start_offset = stream->tellf(stream);
858 extractCtrl.first_record = start_offset ? 0 : 1;
860 stream->endf(stream, &null_offset);;
862 extractCtrl.init = extract_init;
863 extractCtrl.tokenAdd = extract_token_add;
864 extractCtrl.schemaAdd = extract_schema_add;
865 extractCtrl.dh = zh->reg->dh;
866 extractCtrl.handle = zh;
867 extractCtrl.match_criteria[0] = '\0';
868 extractCtrl.staticrank = 0;
869 extractCtrl.action = action;
871 init_extractCtrl(zh, &extractCtrl);
873 extract_set_store_data_prepare(&extractCtrl);
875 r = (*recType->extract)(recTypeClientData, &extractCtrl);
877 if (action == action_update)
879 action = extractCtrl.action;
884 case RECCTRL_EXTRACT_EOF:
886 case RECCTRL_EXTRACT_ERROR_GENERIC:
887 /* error occured during extraction ... */
888 yaz_log(YLOG_WARN, "extract error: generic");
890 case RECCTRL_EXTRACT_ERROR_NO_SUCH_FILTER:
891 /* error occured during extraction ... */
892 yaz_log(YLOG_WARN, "extract error: no such filter");
894 case RECCTRL_EXTRACT_SKIP:
896 yaz_log(YLOG_LOG, "skip %s %s " ZINT_FORMAT,
897 recordType, pr_fname, (zint) start_offset);
900 end_offset = stream->endf(stream, 0);
902 stream->seekf(stream, end_offset);
905 case RECCTRL_EXTRACT_OK:
908 yaz_log(YLOG_WARN, "extract error: unknown error: %d", r);
911 end_offset = stream->endf(stream, 0);
913 stream->seekf(stream, end_offset);
915 end_offset = stream->tellf(stream);
917 if (extractCtrl.match_criteria[0])
918 match_criteria = extractCtrl.match_criteria;
926 if (match_criteria && *match_criteria) {
927 matchStr = match_criteria;
929 if (zh->m_record_id && *zh->m_record_id) {
930 matchStr = get_match_from_spec(zh, zh->reg->keys, pr_fname,
934 yaz_log(YLOG_LOG, "error %s %s " ZINT_FORMAT, recordType,
935 pr_fname, (zint) start_offset);
942 int db_ord = zebraExplain_get_database_ord(zh->reg->zei);
943 char *rinfo = dict_lookup_ord(zh->reg->matchDict, db_ord,
947 if (log_level_extract)
949 WRBUF w = wrbuf_hex_str(matchStr);
950 yaz_log(log_level_extract, "matchStr: %s", wrbuf_cstr(w));
955 assert(*rinfo == sizeof(*sysno));
956 memcpy(sysno, rinfo+1, sizeof(*sysno));
961 if (zebra_rec_keys_empty(zh->reg->keys))
963 /* the extraction process returned no information - the record
964 is probably empty - unless flagShowRecords is in use */
971 /* new record AKA does not exist already */
972 if (action == action_delete)
974 yaz_log(YLOG_LOG, "delete %s %s " ZINT_FORMAT, recordType,
975 pr_fname, (zint) start_offset);
976 yaz_log(YLOG_WARN, "cannot delete record above (seems new)");
979 else if (action == action_a_delete)
982 yaz_log(YLOG_LOG, "adelete %s %s " ZINT_FORMAT, recordType,
983 pr_fname, (zint) start_offset);
986 else if (action == action_replace)
988 yaz_log(YLOG_LOG, "update %s %s " ZINT_FORMAT, recordType,
989 pr_fname, (zint) start_offset);
990 yaz_log(YLOG_WARN, "cannot update record above (seems new)");
994 yaz_log(YLOG_LOG, "add %s %s " ZINT_FORMAT, recordType, pr_fname,
995 (zint) start_offset);
996 rec = rec_new(zh->reg->records);
1003 all_matches_add(&extractCtrl,
1004 zebra_rec_keys_get_custom_record_id(zh->reg->keys),
1009 recordAttr = rec_init_attr(zh->reg->zei, rec);
1010 if (extractCtrl.staticrank < 0)
1012 yaz_log(YLOG_WARN, "Negative staticrank for record. Set to 0");
1013 extractCtrl.staticrank = 0;
1018 int db_ord = zebraExplain_get_database_ord(zh->reg->zei);
1019 dict_insert_ord(zh->reg->matchDict, db_ord, matchStr,
1020 sizeof(*sysno), sysno);
1023 extract_flush_sort_keys(zh, *sysno, 1, zh->reg->sortKeys);
1025 extract_flush_record_keys2(zh, *sysno,
1026 zh->reg->keys, extractCtrl.staticrank,
1027 0, recordAttr->staticrank);
1029 extract_flush_record_keys(zh, *sysno, 1, zh->reg->keys,
1030 extractCtrl.staticrank);
1032 recordAttr->staticrank = extractCtrl.staticrank;
1033 zh->records_inserted++;
1037 /* record already exists */
1038 zebra_rec_keys_t delkeys = zebra_rec_keys_open();
1039 zebra_rec_keys_t sortKeys = zebra_rec_keys_open();
1040 if (action == action_insert)
1042 yaz_log(YLOG_LOG, "skipped %s %s " ZINT_FORMAT,
1043 recordType, pr_fname, (zint) start_offset);
1048 rec = rec_get(zh->reg->records, *sysno);
1053 all_matches_add(&extractCtrl,
1054 zebra_rec_keys_get_custom_record_id(zh->reg->keys),
1058 recordAttr = rec_init_attr(zh->reg->zei, rec);
1060 /* decrease total size */
1061 zebraExplain_recordBytesIncrement(zh->reg->zei,
1062 - recordAttr->recordSize);
1064 zebra_rec_keys_set_buf(delkeys,
1065 rec->info[recInfo_delKeys],
1066 rec->size[recInfo_delKeys],
1068 zebra_rec_keys_set_buf(sortKeys,
1069 rec->info[recInfo_sortKeys],
1070 rec->size[recInfo_sortKeys],
1073 extract_flush_sort_keys(zh, *sysno, 0, sortKeys);
1075 extract_flush_record_keys(zh, *sysno, 0, delkeys,
1076 recordAttr->staticrank);
1078 if (action == action_delete || action == action_a_delete)
1080 /* record going to be deleted */
1082 extract_flush_record_keys2(zh, *sysno, 0, recordAttr->staticrank,
1083 delkeys, recordAttr->staticrank);
1085 if (zebra_rec_keys_empty(delkeys))
1087 yaz_log(YLOG_LOG, "delete %s %s " ZINT_FORMAT, recordType,
1088 pr_fname, (zint) start_offset);
1089 yaz_log(YLOG_WARN, "cannot delete file above, "
1090 "storeKeys false (3)");
1095 yaz_log(YLOG_LOG, "delete %s %s " ZINT_FORMAT, recordType,
1096 pr_fname, (zint) start_offset);
1097 zh->records_deleted++;
1100 int db_ord = zebraExplain_get_database_ord(zh->reg->zei);
1101 dict_delete_ord(zh->reg->matchDict, db_ord, matchStr);
1103 rec_del(zh->reg->records, &rec);
1105 zebra_rec_keys_close(delkeys);
1106 zebra_rec_keys_close(sortKeys);
1112 { /* update or special_update */
1114 yaz_log(YLOG_LOG, "update %s %s " ZINT_FORMAT, recordType,
1115 pr_fname, (zint) start_offset);
1116 extract_flush_sort_keys(zh, *sysno, 1, zh->reg->sortKeys);
1119 extract_flush_record_keys2(zh, *sysno,
1120 zh->reg->keys, extractCtrl.staticrank,
1121 delkeys, recordAttr->staticrank);
1123 extract_flush_record_keys(zh, *sysno, 1,
1124 zh->reg->keys, extractCtrl.staticrank);
1126 recordAttr->staticrank = extractCtrl.staticrank;
1127 zh->records_updated++;
1129 zebra_rec_keys_close(delkeys);
1130 zebra_rec_keys_close(sortKeys);
1132 /* update file type */
1133 xfree(rec->info[recInfo_fileType]);
1134 rec->info[recInfo_fileType] =
1135 rec_strdup(recordType, &rec->size[recInfo_fileType]);
1137 /* update filename */
1138 xfree(rec->info[recInfo_filename]);
1139 rec->info[recInfo_filename] =
1140 rec_strdup(fname, &rec->size[recInfo_filename]);
1142 /* update delete keys */
1143 xfree(rec->info[recInfo_delKeys]);
1144 if (!zebra_rec_keys_empty(zh->reg->keys) && zh->m_store_keys == 1)
1146 zebra_rec_keys_get_buf(zh->reg->keys,
1147 &rec->info[recInfo_delKeys],
1148 &rec->size[recInfo_delKeys]);
1152 rec->info[recInfo_delKeys] = NULL;
1153 rec->size[recInfo_delKeys] = 0;
1155 /* update sort keys */
1156 xfree(rec->info[recInfo_sortKeys]);
1158 zebra_rec_keys_get_buf(zh->reg->sortKeys,
1159 &rec->info[recInfo_sortKeys],
1160 &rec->size[recInfo_sortKeys]);
1164 recordAttr->recordSize = end_offset - start_offset;
1165 zebraExplain_recordBytesIncrement(zh->reg->zei,
1166 recordAttr->recordSize);
1169 /* set run-number for this record */
1170 recordAttr->runNumber =
1171 zebraExplain_runNumberIncrement(zh->reg->zei, 0);
1173 /* update store data */
1174 xfree(rec->info[recInfo_storeData]);
1176 /* update store data */
1177 if (zh->store_data_buf)
1179 rec->size[recInfo_storeData] = zh->store_data_size;
1180 rec->info[recInfo_storeData] = zh->store_data_buf;
1181 zh->store_data_buf = 0;
1182 recordAttr->recordSize = zh->store_data_size;
1184 else if (zh->m_store_data)
1186 off_t cur_offset = stream->tellf(stream);
1188 rec->size[recInfo_storeData] = recordAttr->recordSize;
1189 rec->info[recInfo_storeData] = (char *)
1190 xmalloc(recordAttr->recordSize);
1191 stream->seekf(stream, start_offset);
1192 stream->readf(stream, rec->info[recInfo_storeData],
1193 recordAttr->recordSize);
1194 stream->seekf(stream, cur_offset);
1198 rec->info[recInfo_storeData] = NULL;
1199 rec->size[recInfo_storeData] = 0;
1201 /* update database name */
1202 xfree(rec->info[recInfo_databaseName]);
1203 rec->info[recInfo_databaseName] =
1204 rec_strdup(zh->basenames[0], &rec->size[recInfo_databaseName]);
1207 recordAttr->recordOffset = start_offset;
1209 /* commit this record */
1210 rec_put(zh->reg->records, &rec);
1215 ZEBRA_RES zebra_extract_explain(void *handle, Record rec, data1_node *n)
1217 ZebraHandle zh = (ZebraHandle) handle;
1218 struct recExtractCtrl extractCtrl;
1220 if (zebraExplain_curDatabase(zh->reg->zei,
1221 rec->info[recInfo_databaseName]))
1224 if (zebraExplain_newDatabase(zh->reg->zei,
1225 rec->info[recInfo_databaseName], 0))
1229 zebra_rec_keys_reset(zh->reg->keys);
1230 zebra_rec_keys_reset(zh->reg->sortKeys);
1232 extractCtrl.init = extract_init;
1233 extractCtrl.tokenAdd = extract_token_add;
1234 extractCtrl.schemaAdd = extract_schema_add;
1235 extractCtrl.dh = zh->reg->dh;
1237 init_extractCtrl(zh, &extractCtrl);
1239 extractCtrl.flagShowRecords = 0;
1240 extractCtrl.match_criteria[0] = '\0';
1241 extractCtrl.staticrank = 0;
1242 extractCtrl.action = action_update;
1244 extractCtrl.handle = handle;
1245 extractCtrl.first_record = 1;
1247 extract_set_store_data_prepare(&extractCtrl);
1250 grs_extract_tree(&extractCtrl, n);
1252 if (rec->size[recInfo_delKeys])
1254 zebra_rec_keys_t delkeys = zebra_rec_keys_open();
1256 zebra_rec_keys_t sortkeys = zebra_rec_keys_open();
1258 zebra_rec_keys_set_buf(delkeys, rec->info[recInfo_delKeys],
1259 rec->size[recInfo_delKeys],
1262 extract_flush_record_keys2(zh, rec->sysno,
1263 zh->reg->keys, 0, delkeys, 0);
1265 extract_flush_record_keys(zh, rec->sysno, 0, delkeys, 0);
1266 extract_flush_record_keys(zh, rec->sysno, 1, zh->reg->keys, 0);
1268 zebra_rec_keys_close(delkeys);
1270 zebra_rec_keys_set_buf(sortkeys, rec->info[recInfo_sortKeys],
1271 rec->size[recInfo_sortKeys],
1274 extract_flush_sort_keys(zh, rec->sysno, 0, sortkeys);
1275 zebra_rec_keys_close(sortkeys);
1280 extract_flush_record_keys2(zh, rec->sysno, zh->reg->keys, 0, 0, 0);
1282 extract_flush_record_keys(zh, rec->sysno, 1, zh->reg->keys, 0);
1285 extract_flush_sort_keys(zh, rec->sysno, 1, zh->reg->sortKeys);
1287 xfree(rec->info[recInfo_delKeys]);
1288 zebra_rec_keys_get_buf(zh->reg->keys,
1289 &rec->info[recInfo_delKeys],
1290 &rec->size[recInfo_delKeys]);
1292 xfree(rec->info[recInfo_sortKeys]);
1293 zebra_rec_keys_get_buf(zh->reg->sortKeys,
1294 &rec->info[recInfo_sortKeys],
1295 &rec->size[recInfo_sortKeys]);
1299 void extract_rec_keys_log(ZebraHandle zh, int is_insert,
1300 zebra_rec_keys_t reckeys,
1303 if (zebra_rec_keys_rewind(reckeys))
1308 NMEM nmem = nmem_create();
1310 while(zebra_rec_keys_read(reckeys, &str, &slen, &key))
1312 char keystr[200]; /* room for zints to print */
1314 int ord = CAST_ZINT_TO_INT(key.mem[0]);
1315 const char *index_type;
1317 const char *string_index;
1319 zebraExplain_lookup_ord(zh->reg->zei, ord, &index_type,
1320 0/* db */, &string_index);
1322 zebra_term_untrans_iconv(zh, nmem, index_type,
1325 for (i = 0; i<key.len; i++)
1327 sprintf(keystr + strlen(keystr), ZINT_FORMAT " ", key.mem[i]);
1330 if (*str < CHR_BASE_CHAR)
1333 char dst_buf[200]; /* room for special chars */
1335 strcpy(dst_buf , "?");
1337 if (!strcmp(str, ""))
1338 strcpy(dst_buf, "alwaysmatches");
1339 if (!strcmp(str, FIRST_IN_FIELD_STR))
1340 strcpy(dst_buf, "firstinfield");
1341 else if (!strcmp(str, CHR_UNKNOWN))
1342 strcpy(dst_buf, "unknown");
1343 else if (!strcmp(str, CHR_SPACE))
1344 strcpy(dst_buf, "space");
1346 for (i = 0; i<slen; i++)
1348 sprintf(dst_buf + strlen(dst_buf), " %d", str[i] & 0xff);
1350 yaz_log(level, "%s%s %s %s", keystr, index_type,
1351 string_index, dst_buf);
1355 yaz_log(level, "%s%s %s \"%s\"", keystr, index_type,
1356 string_index, dst_term);
1364 void extract_rec_keys_adjust(ZebraHandle zh, int is_insert,
1365 zebra_rec_keys_t reckeys)
1367 ZebraExplainInfo zei = zh->reg->zei;
1371 struct ord_stat *next;
1374 if (zebra_rec_keys_rewind(reckeys))
1376 struct ord_stat *ord_list = 0;
1380 struct it_key key_in;
1381 while(zebra_rec_keys_read(reckeys, &str, &slen, &key_in))
1383 int ord = CAST_ZINT_TO_INT(key_in.mem[0]);
1385 for (p = ord_list; p ; p = p->next)
1393 p = xmalloc(sizeof(*p));
1404 struct ord_stat *p1 = p;
1407 zebraExplain_ord_adjust_occurrences(zei, p->ord, p->no, 1);
1409 zebraExplain_ord_adjust_occurrences(zei, p->ord, - p->no, -1);
1416 void extract_flush_record_keys2(ZebraHandle zh, zint sysno,
1417 zebra_rec_keys_t ins_keys, zint ins_rank,
1418 zebra_rec_keys_t del_keys, zint del_rank)
1420 ZebraExplainInfo zei = zh->reg->zei;
1424 if (!zh->reg->key_block)
1426 int mem = 1024*1024 * atoi( res_get_def( zh->res, "memmax", "8"));
1427 const char *key_tmp_dir = res_get_def(zh->res, "keyTmpDir", ".");
1428 int use_threads = atoi(res_get_def(zh->res, "threads", "1"));
1429 zh->reg->key_block = key_block_create(mem, key_tmp_dir, use_threads);
1434 extract_rec_keys_adjust(zh, 1, ins_keys);
1436 zebraExplain_recordCountIncrement(zei, 1);
1437 zebra_rec_keys_rewind(ins_keys);
1441 extract_rec_keys_adjust(zh, 0, del_keys);
1443 zebraExplain_recordCountIncrement(zei, -1);
1444 zebra_rec_keys_rewind(del_keys);
1450 const char *del_str;
1451 struct it_key del_key_in;
1455 const char *ins_str;
1456 struct it_key ins_key_in;
1460 del = zebra_rec_keys_read(del_keys, &del_str, &del_slen,
1463 ins = zebra_rec_keys_read(ins_keys, &ins_str, &ins_slen,
1466 if (del && ins && ins_rank == del_rank
1467 && !key_compare(&del_key_in, &ins_key_in)
1468 && ins_slen == del_slen && !memcmp(del_str, ins_str, del_slen))
1478 key_block_write(zh->reg->key_block, sysno,
1479 &del_key_in, 0, del_str, del_slen,
1480 del_rank, zh->m_staticrank);
1482 key_block_write(zh->reg->key_block, sysno,
1483 &ins_key_in, 1, ins_str, ins_slen,
1484 ins_rank, zh->m_staticrank);
1486 yaz_log(log_level_extract, "normal=%d optimized=%d", normal, optimized);
1490 ZEBRA_RES zebra_rec_keys_to_snippets1(ZebraHandle zh,
1491 zebra_rec_keys_t reckeys,
1492 zebra_snippets *snippets)
1494 NMEM nmem = nmem_create();
1495 if (zebra_rec_keys_rewind(reckeys))
1500 while (zebra_rec_keys_read(reckeys, &str, &slen, &key))
1505 const char *index_type;
1507 assert(key.len <= IT_KEY_LEVEL_MAX && key.len > 2);
1508 seqno = key.mem[key.len-1];
1509 ord = CAST_ZINT_TO_INT(key.mem[0]);
1511 zebraExplain_lookup_ord(zh->reg->zei, ord, &index_type,
1512 0/* db */, 0 /* string_index */);
1514 zebra_term_untrans_iconv(zh, nmem, index_type,
1516 zebra_snippets_append(snippets, seqno, 0, ord, dst_term);
1524 void print_rec_keys(ZebraHandle zh, zebra_rec_keys_t reckeys)
1526 yaz_log(YLOG_LOG, "print_rec_keys");
1527 if (zebra_rec_keys_rewind(reckeys))
1532 while (zebra_rec_keys_read(reckeys, &str, &slen, &key))
1534 char dst_buf[IT_MAX_WORD];
1536 const char *index_type;
1537 int ord = CAST_ZINT_TO_INT(key.mem[0]);
1539 assert(key.len <= IT_KEY_LEVEL_MAX && key.len > 2);
1541 zebraExplain_lookup_ord(zh->reg->zei, ord, &index_type, &db, 0);
1543 seqno = key.mem[key.len-1];
1545 zebra_term_untrans(zh, index_type, dst_buf, str);
1547 yaz_log(YLOG_LOG, "ord=%d seqno=" ZINT_FORMAT
1548 " term=%s", ord, seqno, dst_buf);
1553 static void extract_add_index_string(RecWord *p, zinfo_index_category_t cat,
1554 const char *str, int length)
1557 ZebraHandle zh = p->extractCtrl->handle;
1558 ZebraExplainInfo zei = zh->reg->zei;
1561 ch = zebraExplain_lookup_attr_str(zei, cat, p->index_type, p->index_name);
1563 ch = zebraExplain_add_attr_str(zei, cat, p->index_type, p->index_name);
1567 key.mem[i++] = p->record_id;
1568 key.mem[i++] = p->section_id;
1570 if (zh->m_segment_indexing)
1571 key.mem[i++] = p->segment;
1572 key.mem[i++] = p->seqno;
1575 zebra_rec_keys_write(zh->reg->keys, str, length, &key);
1578 static void extract_add_sort_string(RecWord *p, const char *str, int length)
1581 ZebraHandle zh = p->extractCtrl->handle;
1582 ZebraExplainInfo zei = zh->reg->zei;
1584 zinfo_index_category_t cat = zinfo_index_category_sort;
1586 ch = zebraExplain_lookup_attr_str(zei, cat, p->index_type, p->index_name);
1588 ch = zebraExplain_add_attr_str(zei, cat, p->index_type, p->index_name);
1591 key.mem[1] = p->record_id;
1593 zebra_rec_keys_write(zh->reg->sortKeys, str, length, &key);
1596 static void extract_add_staticrank_string(RecWord *p,
1597 const char *str, int length)
1600 struct recExtractCtrl *ctrl = p->extractCtrl;
1602 if (length > sizeof(valz)-1)
1603 length = sizeof(valz)-1;
1605 memcpy(valz, str, length);
1606 valz[length] = '\0';
1607 ctrl->staticrank = atozint(valz);
1610 static void extract_add_string(RecWord *p, zebra_map_t zm,
1611 const char *string, int length)
1618 if (zebra_maps_is_index(zm))
1620 extract_add_index_string(p, zinfo_index_category_index,
1622 if (zebra_maps_is_alwaysmatches(zm))
1625 memcpy(&word, p, sizeof(word));
1628 extract_add_index_string(
1629 &word, zinfo_index_category_alwaysmatches, "", 0);
1632 else if (zebra_maps_is_sort(zm))
1634 extract_add_sort_string(p, string, length);
1636 else if (zebra_maps_is_staticrank(zm))
1638 extract_add_staticrank_string(p, string, length);
1642 static void extract_add_incomplete_field(RecWord *p, zebra_map_t zm)
1644 const char *b = p->term_buf;
1645 int remain = p->term_len;
1647 const char **map = 0;
1650 map = zebra_maps_input(zm, &b, remain, 0);
1654 char buf[IT_MAX_WORD+1];
1658 while (map && *map && **map == *CHR_SPACE)
1660 remain = p->term_len - (b - p->term_buf);
1662 map = zebra_maps_input(zm, &b, remain, 0);
1669 while (map && *map && **map != *CHR_SPACE)
1671 const char *cp = *map;
1673 while (i < IT_MAX_WORD && *cp)
1675 remain = p->term_len - (b - p->term_buf);
1677 map = zebra_maps_input(zm, &b, remain, 0);
1687 if (zebra_maps_is_first_in_field(zm))
1689 /* first in field marker */
1690 extract_add_string(p, zm, FIRST_IN_FIELD_STR, FIRST_IN_FIELD_LEN);
1694 extract_add_string(p, zm, buf, i);
1699 static void extract_add_complete_field(RecWord *p, zebra_map_t zm)
1701 const char *b = p->term_buf;
1702 char buf[IT_MAX_WORD+1];
1703 const char **map = 0;
1704 int i = 0, remain = p->term_len;
1707 map = zebra_maps_input(zm, &b, remain, 1);
1709 while (remain > 0 && i < IT_MAX_WORD)
1711 while (map && *map && **map == *CHR_SPACE)
1713 remain = p->term_len - (b - p->term_buf);
1717 int first = i ? 0 : 1; /* first position */
1718 map = zebra_maps_input(zm, &b, remain, first);
1726 if (i && i < IT_MAX_WORD)
1727 buf[i++] = *CHR_SPACE;
1728 while (map && *map && **map != *CHR_SPACE)
1730 const char *cp = *map;
1732 if (**map == *CHR_CUT)
1738 if (i >= IT_MAX_WORD)
1740 while (i < IT_MAX_WORD && *cp)
1743 remain = p->term_len - (b - p->term_buf);
1746 map = zebra_maps_input(zm, &b, remain, 0);
1754 extract_add_string(p, zm, buf, i);
1757 static void extract_add_icu(RecWord *p, zebra_map_t zm)
1759 const char *res_buf = 0;
1762 zebra_map_tokenize_start(zm, p->term_buf, p->term_len);
1763 while (zebra_map_tokenize_next(zm, &res_buf, &res_len, 0, 0))
1765 extract_add_string(p, zm, res_buf, res_len);
1771 /** \brief top-level indexing handler for recctrl system
1772 \param p token data to be indexed
1776 extract_add_{in}_complete / extract_add_icu
1779 extract_add_index_string
1781 extract_add_sort_string
1783 extract_add_staticrank_string
1786 static void extract_token_add(RecWord *p)
1788 ZebraHandle zh = p->extractCtrl->handle;
1789 zebra_map_t zm = zebra_map_get_or_add(zh->reg->zebra_maps, p->index_type);
1792 if (log_level_details)
1794 yaz_log(log_level_details, "extract_token_add "
1795 "type=%s index=%s seqno=" ZINT_FORMAT " s=%.*s",
1796 p->index_type, p->index_name,
1797 p->seqno, p->term_len, p->term_buf);
1799 if ((wrbuf = zebra_replace(zm, 0, p->term_buf, p->term_len)))
1801 p->term_buf = wrbuf_buf(wrbuf);
1802 p->term_len = wrbuf_len(wrbuf);
1804 if (zebra_maps_is_icu(zm))
1806 extract_add_icu(p, zm);
1810 if (zebra_maps_is_complete(zm))
1811 extract_add_complete_field(p, zm);
1813 extract_add_incomplete_field(p, zm);
1817 static void extract_set_store_data_cb(struct recExtractCtrl *p,
1818 void *buf, size_t sz)
1820 ZebraHandle zh = (ZebraHandle) p->handle;
1822 xfree(zh->store_data_buf);
1823 zh->store_data_buf = 0;
1824 zh->store_data_size = 0;
1827 zh->store_data_buf = xmalloc(sz);
1828 zh->store_data_size = sz;
1829 memcpy(zh->store_data_buf, buf, sz);
1833 static void extract_set_store_data_prepare(struct recExtractCtrl *p)
1835 ZebraHandle zh = (ZebraHandle) p->handle;
1836 xfree(zh->store_data_buf);
1837 zh->store_data_buf = 0;
1838 zh->store_data_size = 0;
1839 p->setStoreData = extract_set_store_data_cb;
1842 static void extract_schema_add(struct recExtractCtrl *p, Odr_oid *oid)
1844 ZebraHandle zh = (ZebraHandle) p->handle;
1845 zebraExplain_addSchema(zh->reg->zei, oid);
1848 void extract_flush_sort_keys(ZebraHandle zh, zint sysno,
1849 int cmd, zebra_rec_keys_t reckeys)
1852 yaz_log(YLOG_LOG, "extract_flush_sort_keys cmd=%d sysno=" ZINT_FORMAT,
1854 extract_rec_keys_log(zh, cmd, reckeys, YLOG_LOG);
1857 if (zebra_rec_keys_rewind(reckeys))
1859 zebra_sort_index_t si = zh->reg->sort_index;
1862 struct it_key key_in;
1864 zebra_sort_sysno(si, sysno);
1866 while (zebra_rec_keys_read(reckeys, &str, &slen, &key_in))
1868 int ord = CAST_ZINT_TO_INT(key_in.mem[0]);
1870 zebra_sort_type(si, ord);
1872 zebra_sort_add(si, str, slen);
1874 zebra_sort_delete(si);
1882 * indent-tabs-mode: nil
1884 * vim: shiftwidth=4 tabstop=8 expandtab