1 /* $Id: extract.c,v 1.278 2008-01-26 15:32:51 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 && zebra_maps_is_index(zm))
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);
225 if (start != last && zebra_maps_is_index(zm))
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 */
259 if (start != last && zebra_maps_is_index(zm))
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 if (zebra_maps_is_index(zm))
283 zebra_snippets_appendn(h->snippets, p->seqno, 0, ord,
284 display_buf, display_len);
289 static void snippet_token_add(RecWord *p)
291 struct snip_rec_info *h = p->extractCtrl->handle;
292 ZebraHandle zh = h->zh;
293 zebra_map_t zm = zebra_map_get(zh->reg->zebra_maps, p->index_type);
297 ZebraExplainInfo zei = zh->reg->zei;
298 int ch = zebraExplain_lookup_attr_str(
299 zei, zinfo_index_category_index, p->index_type, p->index_name);
301 if (zebra_maps_is_icu(zm))
302 snippet_add_icu(p, ch, zm);
305 if (zebra_maps_is_complete(zm))
306 snippet_add_complete_field(p, ch, zm);
308 snippet_add_incomplete_field(p, ch, zm);
313 static void snippet_schema_add(
314 struct recExtractCtrl *p, Odr_oid *oid)
319 void extract_snippet(ZebraHandle zh, zebra_snippets *sn,
320 struct ZebraRecStream *stream,
321 RecType rt, void *recTypeClientData)
323 struct recExtractCtrl extractCtrl;
324 struct snip_rec_info info;
327 extractCtrl.stream = stream;
328 extractCtrl.first_record = 1;
329 extractCtrl.init = extract_init;
330 extractCtrl.tokenAdd = snippet_token_add;
331 extractCtrl.schemaAdd = snippet_schema_add;
335 extractCtrl.dh = zh->reg->dh;
339 extractCtrl.handle = &info;
340 extractCtrl.match_criteria[0] = '\0';
341 extractCtrl.staticrank = 0;
342 extractCtrl.action = action_insert;
344 init_extractCtrl(zh, &extractCtrl);
346 extractCtrl.setStoreData = 0;
348 r = (*rt->extract)(recTypeClientData, &extractCtrl);
352 static void searchRecordKey(ZebraHandle zh,
353 zebra_rec_keys_t reckeys,
354 const char *index_name,
355 const char **ws, int ws_length)
359 zinfo_index_category_t cat = zinfo_index_category_index;
361 for (i = 0; i<ws_length; i++)
365 ch = zebraExplain_lookup_attr_str(zh->reg->zei, cat, "0", index_name);
367 ch = zebraExplain_lookup_attr_str(zh->reg->zei, cat, "p", index_name);
369 ch = zebraExplain_lookup_attr_str(zh->reg->zei, cat, "w", index_name);
374 if (zebra_rec_keys_rewind(reckeys))
381 while (zebra_rec_keys_read(reckeys, &str, &slen, &key))
383 assert(key.len <= IT_KEY_LEVEL_MAX && key.len > 2);
385 seqno = key.mem[key.len-1];
387 if (key.mem[0] == ch)
393 woff = seqno - startSeq;
394 if (woff >= 0 && woff < ws_length)
401 #define FILE_MATCH_BLANK "\t "
403 static char *get_match_from_spec(ZebraHandle zh,
404 zebra_rec_keys_t reckeys,
405 const char *fname, const char *spec)
407 static char dstBuf[2048]; /* static here ??? */
409 const char *s = spec;
413 for (; *s && strchr(FILE_MATCH_BLANK, *s); s++)
420 char attset_str[64], attname_str[64];
424 for (s++; strchr(FILE_MATCH_BLANK, *s); s++)
426 for (i = 0; *s && *s != ',' && *s != ')' &&
427 !strchr(FILE_MATCH_BLANK, *s); s++)
428 if (i+1 < sizeof(attset_str))
429 attset_str[i++] = *s;
430 attset_str[i] = '\0';
432 for (; strchr(FILE_MATCH_BLANK, *s); s++)
435 strcpy(attname_str, attset_str);
438 for (s++; strchr(FILE_MATCH_BLANK, *s); s++)
440 for (i = 0; *s && *s != ')' &&
441 !strchr(FILE_MATCH_BLANK, *s); s++)
442 if (i+1 < sizeof(attname_str))
443 attname_str[i++] = *s;
444 attname_str[i] = '\0';
447 searchRecordKey(zh, reckeys, attname_str, ws, 32);
451 yaz_log(YLOG_WARN, "Missing ) in match criteria %s in group %s",
452 spec, zh->m_group ? zh->m_group : "none");
457 for (i = 0; i<32; i++)
466 dst += strlen(ws[i]);
470 yaz_log(YLOG_WARN, "Record didn't contain match"
471 " fields in (%s,%s)", attset_str, attname_str);
479 const char *spec_src = NULL;
480 const char *s1 = ++s;
481 while (*s1 && !strchr(FILE_MATCH_BLANK, *s1))
485 if (spec_len > sizeof(special)-1)
486 spec_len = sizeof(special)-1;
487 memcpy(special, s, spec_len);
488 special[spec_len] = '\0';
491 if (!strcmp(special, "group"))
492 spec_src = zh->m_group;
493 else if (!strcmp(special, "database"))
494 spec_src = zh->basenames[0];
495 else if (!strcmp(special, "filename")) {
498 else if (!strcmp(special, "type"))
499 spec_src = zh->m_record_type;
504 strcpy(dst, spec_src);
505 dst += strlen(spec_src);
508 else if (*s == '\"' || *s == '\'')
510 int stopMarker = *s++;
514 while (*s && *s != stopMarker)
516 if (i+1 < sizeof(tmpString))
517 tmpString[i++] = *s++;
522 strcpy(dst, tmpString);
523 dst += strlen(tmpString);
527 yaz_log(YLOG_WARN, "Syntax error in match criteria %s in group %s",
528 spec, zh->m_group ? zh->m_group : "none");
535 yaz_log(YLOG_WARN, "No match criteria for record %s in group %s",
536 fname, zh->m_group ? zh->m_group : "none");
543 struct recordLogInfo {
546 struct recordGroup *rGroup;
549 /** \brief add the always-matches index entry and map to real record ID
550 \param ctrl record control
551 \param record_id custom record ID
552 \param sysno system record ID
554 This function serves two purposes.. It adds the always matches
555 entry and makes a pointer from the custom record ID (if defined)
556 back to the system record ID (sysno)
557 See zebra_recid_to_sysno .
559 static void all_matches_add(struct recExtractCtrl *ctrl, zint record_id,
563 extract_init(ctrl, &word);
564 word.record_id = record_id;
565 /* we use the seqno as placeholder for a way to get back to
566 record database from _ALLRECORDS.. This is used if a custom
567 RECORD was defined */
569 word.index_name = "_ALLRECORDS";
570 word.index_type = "w";
572 extract_add_index_string(&word, zinfo_index_category_alwaysmatches,
576 ZEBRA_RES zebra_extract_records_stream(ZebraHandle zh,
577 struct ZebraRecStream *stream,
578 enum zebra_recctrl_action_t action,
580 const char *recordType,
582 const char *match_criteria,
585 void *recTypeClientData);
588 ZEBRA_RES zebra_extract_file(ZebraHandle zh, zint *sysno, const char *fname,
589 enum zebra_recctrl_action_t action)
591 ZEBRA_RES r = ZEBRA_OK;
596 struct file_read_info *fi = 0;
597 const char *original_record_type = 0;
599 void *recTypeClientData;
600 struct ZebraRecStream stream, *streamp;
602 zebra_init_log_level();
604 if (!zh->m_group || !*zh->m_group)
607 sprintf(gprefix, "%s.", zh->m_group);
609 yaz_log(log_level_extract, "zebra_extract_file %s", fname);
611 /* determine file extension */
613 for (i = strlen(fname); --i >= 0; )
616 else if (fname[i] == '.')
618 strcpy(ext, fname+i+1);
621 /* determine file type - depending on extension */
622 original_record_type = zh->m_record_type;
623 if (!zh->m_record_type)
625 sprintf(ext_res, "%srecordType.%s", gprefix, ext);
626 zh->m_record_type = res_get(zh->res, ext_res);
628 if (!zh->m_record_type)
631 if (zh->records_processed + zh->records_skipped
632 < zh->m_file_verbose_limit)
633 yaz_log(YLOG_LOG, "? %s", fname);
634 zh->records_skipped++;
637 /* determine match criteria */
638 if (!zh->m_record_id)
640 sprintf(ext_res, "%srecordId.%s", gprefix, ext);
641 zh->m_record_id = res_get(zh->res, ext_res);
645 recType_byName(zh->reg->recTypes, zh->res, zh->m_record_type,
646 &recTypeClientData)))
648 yaz_log(YLOG_WARN, "No such record type: %s", zh->m_record_type);
652 switch(recType->version)
657 yaz_log(YLOG_WARN, "Bad filter version: %s", zh->m_record_type);
659 if (sysno && (action == action_delete || action == action_a_delete))
668 if (zh->path_reg && !yaz_is_abspath(fname))
670 strcpy(full_rep, zh->path_reg);
671 strcat(full_rep, "/");
672 strcat(full_rep, fname);
675 strcpy(full_rep, fname);
677 if ((fd = open(full_rep, O_BINARY|O_RDONLY)) == -1)
679 yaz_log(YLOG_WARN|YLOG_ERRNO, "open %s", full_rep);
680 zh->m_record_type = original_record_type;
684 zebra_create_stream_fd(streamp, fd, 0);
686 r = zebra_extract_records_stream(zh, streamp,
691 0, /*match_criteria */
693 recType, recTypeClientData);
695 stream.destroy(streamp);
696 zh->m_record_type = original_record_type;
701 If sysno is provided, then it's used to identify the reocord.
702 If not, and match_criteria is provided, then sysno is guessed
703 If not, and a record is provided, then sysno is got from there
707 ZEBRA_RES zebra_buffer_extract_record(ZebraHandle zh,
708 const char *buf, size_t buf_size,
709 enum zebra_recctrl_action_t action,
711 const char *recordType,
713 const char *match_criteria,
716 struct ZebraRecStream stream;
721 if (recordType && *recordType)
723 yaz_log(log_level_extract,
724 "Record type explicitly specified: %s", recordType);
725 recType = recType_byName(zh->reg->recTypes, zh->res, recordType,
730 if (!(zh->m_record_type))
732 yaz_log(YLOG_WARN, "No such record type defined");
735 yaz_log(log_level_extract, "Get record type from rgroup: %s",
737 recType = recType_byName(zh->reg->recTypes, zh->res,
738 zh->m_record_type, &clientData);
739 recordType = zh->m_record_type;
744 yaz_log(YLOG_WARN, "No such record type: %s", recordType);
748 zebra_create_stream_mem(&stream, buf, buf_size);
750 res = zebra_extract_records_stream(zh, &stream,
757 recType, clientData);
758 stream.destroy(&stream);
762 ZEBRA_RES zebra_extract_records_stream(ZebraHandle zh,
763 struct ZebraRecStream *stream,
764 enum zebra_recctrl_action_t action,
766 const char *recordType,
768 const char *match_criteria,
771 void *recTypeClientData)
773 ZEBRA_RES res = ZEBRA_OK;
777 res = zebra_extract_record_stream(zh, stream,
784 recType, recTypeClientData, &more);
799 static WRBUF wrbuf_hex_str(const char *cstr)
802 WRBUF w = wrbuf_alloc();
803 for (i = 0; cstr[i]; i++)
805 if (cstr[i] < ' ' || cstr[i] > 126)
806 wrbuf_printf(w, "\\%02X", cstr[i] & 0xff);
808 wrbuf_putc(w, cstr[i]);
813 ZEBRA_RES zebra_extract_record_stream(ZebraHandle zh,
814 struct ZebraRecStream *stream,
815 enum zebra_recctrl_action_t action,
817 const char *recordType,
819 const char *match_criteria,
822 void *recTypeClientData,
827 RecordAttr *recordAttr;
828 struct recExtractCtrl extractCtrl;
830 const char *matchStr = 0;
832 off_t start_offset = 0, end_offset = 0;
833 const char *pr_fname = fname; /* filename to print .. */
834 int show_progress = zh->records_processed + zh->records_skipped
835 < zh->m_file_verbose_limit ? 1:0;
837 zebra_init_log_level();
840 pr_fname = "<no file>"; /* make it printable if file is omitted */
842 zebra_rec_keys_reset(zh->reg->keys);
843 zebra_rec_keys_reset(zh->reg->sortKeys);
845 if (zebraExplain_curDatabase(zh->reg->zei, zh->basenames[0]))
847 if (zebraExplain_newDatabase(zh->reg->zei, zh->basenames[0],
848 zh->m_explain_database))
854 off_t null_offset = 0;
855 extractCtrl.stream = stream;
857 start_offset = stream->tellf(stream);
859 extractCtrl.first_record = start_offset ? 0 : 1;
861 stream->endf(stream, &null_offset);;
863 extractCtrl.init = extract_init;
864 extractCtrl.tokenAdd = extract_token_add;
865 extractCtrl.schemaAdd = extract_schema_add;
866 extractCtrl.dh = zh->reg->dh;
867 extractCtrl.handle = zh;
868 extractCtrl.match_criteria[0] = '\0';
869 extractCtrl.staticrank = 0;
870 extractCtrl.action = action;
872 init_extractCtrl(zh, &extractCtrl);
874 extract_set_store_data_prepare(&extractCtrl);
876 r = (*recType->extract)(recTypeClientData, &extractCtrl);
878 if (action == action_update)
880 action = extractCtrl.action;
885 case RECCTRL_EXTRACT_EOF:
887 case RECCTRL_EXTRACT_ERROR_GENERIC:
888 /* error occured during extraction ... */
889 yaz_log(YLOG_WARN, "extract error: generic");
891 case RECCTRL_EXTRACT_ERROR_NO_SUCH_FILTER:
892 /* error occured during extraction ... */
893 yaz_log(YLOG_WARN, "extract error: no such filter");
895 case RECCTRL_EXTRACT_SKIP:
897 yaz_log(YLOG_LOG, "skip %s %s " ZINT_FORMAT,
898 recordType, pr_fname, (zint) start_offset);
901 end_offset = stream->endf(stream, 0);
903 stream->seekf(stream, end_offset);
906 case RECCTRL_EXTRACT_OK:
909 yaz_log(YLOG_WARN, "extract error: unknown error: %d", r);
912 end_offset = stream->endf(stream, 0);
914 stream->seekf(stream, end_offset);
916 end_offset = stream->tellf(stream);
918 if (extractCtrl.match_criteria[0])
919 match_criteria = extractCtrl.match_criteria;
927 if (match_criteria && *match_criteria) {
928 matchStr = match_criteria;
930 if (zh->m_record_id && *zh->m_record_id) {
931 matchStr = get_match_from_spec(zh, zh->reg->keys, pr_fname,
935 yaz_log(YLOG_LOG, "error %s %s " ZINT_FORMAT, recordType,
936 pr_fname, (zint) start_offset);
943 int db_ord = zebraExplain_get_database_ord(zh->reg->zei);
944 char *rinfo = dict_lookup_ord(zh->reg->matchDict, db_ord,
948 if (log_level_extract)
950 WRBUF w = wrbuf_hex_str(matchStr);
951 yaz_log(log_level_extract, "matchStr: %s", wrbuf_cstr(w));
956 assert(*rinfo == sizeof(*sysno));
957 memcpy(sysno, rinfo+1, sizeof(*sysno));
962 if (zebra_rec_keys_empty(zh->reg->keys))
964 /* the extraction process returned no information - the record
965 is probably empty - unless flagShowRecords is in use */
972 /* new record AKA does not exist already */
973 if (action == action_delete)
975 yaz_log(YLOG_LOG, "delete %s %s " ZINT_FORMAT, recordType,
976 pr_fname, (zint) start_offset);
977 yaz_log(YLOG_WARN, "cannot delete record above (seems new)");
980 else if (action == action_a_delete)
983 yaz_log(YLOG_LOG, "adelete %s %s " ZINT_FORMAT, recordType,
984 pr_fname, (zint) start_offset);
987 else if (action == action_replace)
989 yaz_log(YLOG_LOG, "update %s %s " ZINT_FORMAT, recordType,
990 pr_fname, (zint) start_offset);
991 yaz_log(YLOG_WARN, "cannot update record above (seems new)");
995 yaz_log(YLOG_LOG, "add %s %s " ZINT_FORMAT, recordType, pr_fname,
996 (zint) start_offset);
997 rec = rec_new(zh->reg->records);
1004 all_matches_add(&extractCtrl,
1005 zebra_rec_keys_get_custom_record_id(zh->reg->keys),
1010 recordAttr = rec_init_attr(zh->reg->zei, rec);
1011 if (extractCtrl.staticrank < 0)
1013 yaz_log(YLOG_WARN, "Negative staticrank for record. Set to 0");
1014 extractCtrl.staticrank = 0;
1019 int db_ord = zebraExplain_get_database_ord(zh->reg->zei);
1020 dict_insert_ord(zh->reg->matchDict, db_ord, matchStr,
1021 sizeof(*sysno), sysno);
1024 extract_flush_sort_keys(zh, *sysno, 1, zh->reg->sortKeys);
1026 extract_flush_record_keys2(zh, *sysno,
1027 zh->reg->keys, extractCtrl.staticrank,
1028 0, recordAttr->staticrank);
1030 extract_flush_record_keys(zh, *sysno, 1, zh->reg->keys,
1031 extractCtrl.staticrank);
1033 recordAttr->staticrank = extractCtrl.staticrank;
1034 zh->records_inserted++;
1038 /* record already exists */
1039 zebra_rec_keys_t delkeys = zebra_rec_keys_open();
1040 zebra_rec_keys_t sortKeys = zebra_rec_keys_open();
1041 if (action == action_insert)
1043 yaz_log(YLOG_LOG, "skipped %s %s " ZINT_FORMAT,
1044 recordType, pr_fname, (zint) start_offset);
1049 rec = rec_get(zh->reg->records, *sysno);
1054 all_matches_add(&extractCtrl,
1055 zebra_rec_keys_get_custom_record_id(zh->reg->keys),
1059 recordAttr = rec_init_attr(zh->reg->zei, rec);
1061 /* decrease total size */
1062 zebraExplain_recordBytesIncrement(zh->reg->zei,
1063 - recordAttr->recordSize);
1065 zebra_rec_keys_set_buf(delkeys,
1066 rec->info[recInfo_delKeys],
1067 rec->size[recInfo_delKeys],
1069 zebra_rec_keys_set_buf(sortKeys,
1070 rec->info[recInfo_sortKeys],
1071 rec->size[recInfo_sortKeys],
1074 extract_flush_sort_keys(zh, *sysno, 0, sortKeys);
1076 extract_flush_record_keys(zh, *sysno, 0, delkeys,
1077 recordAttr->staticrank);
1079 if (action == action_delete || action == action_a_delete)
1081 /* record going to be deleted */
1083 extract_flush_record_keys2(zh, *sysno, 0, recordAttr->staticrank,
1084 delkeys, recordAttr->staticrank);
1086 if (zebra_rec_keys_empty(delkeys))
1088 yaz_log(YLOG_LOG, "delete %s %s " ZINT_FORMAT, recordType,
1089 pr_fname, (zint) start_offset);
1090 yaz_log(YLOG_WARN, "cannot delete file above, "
1091 "storeKeys false (3)");
1096 yaz_log(YLOG_LOG, "delete %s %s " ZINT_FORMAT, recordType,
1097 pr_fname, (zint) start_offset);
1098 zh->records_deleted++;
1101 int db_ord = zebraExplain_get_database_ord(zh->reg->zei);
1102 dict_delete_ord(zh->reg->matchDict, db_ord, matchStr);
1104 rec_del(zh->reg->records, &rec);
1106 zebra_rec_keys_close(delkeys);
1107 zebra_rec_keys_close(sortKeys);
1113 { /* update or special_update */
1115 yaz_log(YLOG_LOG, "update %s %s " ZINT_FORMAT, recordType,
1116 pr_fname, (zint) start_offset);
1117 extract_flush_sort_keys(zh, *sysno, 1, zh->reg->sortKeys);
1120 extract_flush_record_keys2(zh, *sysno,
1121 zh->reg->keys, extractCtrl.staticrank,
1122 delkeys, recordAttr->staticrank);
1124 extract_flush_record_keys(zh, *sysno, 1,
1125 zh->reg->keys, extractCtrl.staticrank);
1127 recordAttr->staticrank = extractCtrl.staticrank;
1128 zh->records_updated++;
1130 zebra_rec_keys_close(delkeys);
1131 zebra_rec_keys_close(sortKeys);
1133 /* update file type */
1134 xfree(rec->info[recInfo_fileType]);
1135 rec->info[recInfo_fileType] =
1136 rec_strdup(recordType, &rec->size[recInfo_fileType]);
1138 /* update filename */
1139 xfree(rec->info[recInfo_filename]);
1140 rec->info[recInfo_filename] =
1141 rec_strdup(fname, &rec->size[recInfo_filename]);
1143 /* update delete keys */
1144 xfree(rec->info[recInfo_delKeys]);
1145 if (!zebra_rec_keys_empty(zh->reg->keys) && zh->m_store_keys == 1)
1147 zebra_rec_keys_get_buf(zh->reg->keys,
1148 &rec->info[recInfo_delKeys],
1149 &rec->size[recInfo_delKeys]);
1153 rec->info[recInfo_delKeys] = NULL;
1154 rec->size[recInfo_delKeys] = 0;
1156 /* update sort keys */
1157 xfree(rec->info[recInfo_sortKeys]);
1159 zebra_rec_keys_get_buf(zh->reg->sortKeys,
1160 &rec->info[recInfo_sortKeys],
1161 &rec->size[recInfo_sortKeys]);
1165 recordAttr->recordSize = end_offset - start_offset;
1166 zebraExplain_recordBytesIncrement(zh->reg->zei,
1167 recordAttr->recordSize);
1170 /* set run-number for this record */
1171 recordAttr->runNumber =
1172 zebraExplain_runNumberIncrement(zh->reg->zei, 0);
1174 /* update store data */
1175 xfree(rec->info[recInfo_storeData]);
1177 /* update store data */
1178 if (zh->store_data_buf)
1180 rec->size[recInfo_storeData] = zh->store_data_size;
1181 rec->info[recInfo_storeData] = zh->store_data_buf;
1182 zh->store_data_buf = 0;
1183 recordAttr->recordSize = zh->store_data_size;
1185 else if (zh->m_store_data)
1187 off_t cur_offset = stream->tellf(stream);
1189 rec->size[recInfo_storeData] = recordAttr->recordSize;
1190 rec->info[recInfo_storeData] = (char *)
1191 xmalloc(recordAttr->recordSize);
1192 stream->seekf(stream, start_offset);
1193 stream->readf(stream, rec->info[recInfo_storeData],
1194 recordAttr->recordSize);
1195 stream->seekf(stream, cur_offset);
1199 rec->info[recInfo_storeData] = NULL;
1200 rec->size[recInfo_storeData] = 0;
1202 /* update database name */
1203 xfree(rec->info[recInfo_databaseName]);
1204 rec->info[recInfo_databaseName] =
1205 rec_strdup(zh->basenames[0], &rec->size[recInfo_databaseName]);
1208 recordAttr->recordOffset = start_offset;
1210 /* commit this record */
1211 rec_put(zh->reg->records, &rec);
1216 ZEBRA_RES zebra_extract_explain(void *handle, Record rec, data1_node *n)
1218 ZebraHandle zh = (ZebraHandle) handle;
1219 struct recExtractCtrl extractCtrl;
1221 if (zebraExplain_curDatabase(zh->reg->zei,
1222 rec->info[recInfo_databaseName]))
1225 if (zebraExplain_newDatabase(zh->reg->zei,
1226 rec->info[recInfo_databaseName], 0))
1230 zebra_rec_keys_reset(zh->reg->keys);
1231 zebra_rec_keys_reset(zh->reg->sortKeys);
1233 extractCtrl.init = extract_init;
1234 extractCtrl.tokenAdd = extract_token_add;
1235 extractCtrl.schemaAdd = extract_schema_add;
1236 extractCtrl.dh = zh->reg->dh;
1238 init_extractCtrl(zh, &extractCtrl);
1240 extractCtrl.flagShowRecords = 0;
1241 extractCtrl.match_criteria[0] = '\0';
1242 extractCtrl.staticrank = 0;
1243 extractCtrl.action = action_update;
1245 extractCtrl.handle = handle;
1246 extractCtrl.first_record = 1;
1248 extract_set_store_data_prepare(&extractCtrl);
1251 grs_extract_tree(&extractCtrl, n);
1253 if (rec->size[recInfo_delKeys])
1255 zebra_rec_keys_t delkeys = zebra_rec_keys_open();
1257 zebra_rec_keys_t sortkeys = zebra_rec_keys_open();
1259 zebra_rec_keys_set_buf(delkeys, rec->info[recInfo_delKeys],
1260 rec->size[recInfo_delKeys],
1263 extract_flush_record_keys2(zh, rec->sysno,
1264 zh->reg->keys, 0, delkeys, 0);
1266 extract_flush_record_keys(zh, rec->sysno, 0, delkeys, 0);
1267 extract_flush_record_keys(zh, rec->sysno, 1, zh->reg->keys, 0);
1269 zebra_rec_keys_close(delkeys);
1271 zebra_rec_keys_set_buf(sortkeys, rec->info[recInfo_sortKeys],
1272 rec->size[recInfo_sortKeys],
1275 extract_flush_sort_keys(zh, rec->sysno, 0, sortkeys);
1276 zebra_rec_keys_close(sortkeys);
1281 extract_flush_record_keys2(zh, rec->sysno, zh->reg->keys, 0, 0, 0);
1283 extract_flush_record_keys(zh, rec->sysno, 1, zh->reg->keys, 0);
1286 extract_flush_sort_keys(zh, rec->sysno, 1, zh->reg->sortKeys);
1288 xfree(rec->info[recInfo_delKeys]);
1289 zebra_rec_keys_get_buf(zh->reg->keys,
1290 &rec->info[recInfo_delKeys],
1291 &rec->size[recInfo_delKeys]);
1293 xfree(rec->info[recInfo_sortKeys]);
1294 zebra_rec_keys_get_buf(zh->reg->sortKeys,
1295 &rec->info[recInfo_sortKeys],
1296 &rec->size[recInfo_sortKeys]);
1300 void extract_rec_keys_log(ZebraHandle zh, int is_insert,
1301 zebra_rec_keys_t reckeys,
1304 if (zebra_rec_keys_rewind(reckeys))
1309 NMEM nmem = nmem_create();
1311 while(zebra_rec_keys_read(reckeys, &str, &slen, &key))
1313 char keystr[200]; /* room for zints to print */
1315 int ord = CAST_ZINT_TO_INT(key.mem[0]);
1316 const char *index_type;
1318 const char *string_index;
1320 zebraExplain_lookup_ord(zh->reg->zei, ord, &index_type,
1321 0/* db */, &string_index);
1323 zebra_term_untrans_iconv(zh, nmem, index_type,
1326 for (i = 0; i<key.len; i++)
1328 sprintf(keystr + strlen(keystr), ZINT_FORMAT " ", key.mem[i]);
1331 if (*str < CHR_BASE_CHAR)
1334 char dst_buf[200]; /* room for special chars */
1336 strcpy(dst_buf , "?");
1338 if (!strcmp(str, ""))
1339 strcpy(dst_buf, "alwaysmatches");
1340 if (!strcmp(str, FIRST_IN_FIELD_STR))
1341 strcpy(dst_buf, "firstinfield");
1342 else if (!strcmp(str, CHR_UNKNOWN))
1343 strcpy(dst_buf, "unknown");
1344 else if (!strcmp(str, CHR_SPACE))
1345 strcpy(dst_buf, "space");
1347 for (i = 0; i<slen; i++)
1349 sprintf(dst_buf + strlen(dst_buf), " %d", str[i] & 0xff);
1351 yaz_log(level, "%s%s %s %s", keystr, index_type,
1352 string_index, dst_buf);
1356 yaz_log(level, "%s%s %s \"%s\"", keystr, index_type,
1357 string_index, dst_term);
1365 void extract_rec_keys_adjust(ZebraHandle zh, int is_insert,
1366 zebra_rec_keys_t reckeys)
1368 ZebraExplainInfo zei = zh->reg->zei;
1372 struct ord_stat *next;
1375 if (zebra_rec_keys_rewind(reckeys))
1377 struct ord_stat *ord_list = 0;
1381 struct it_key key_in;
1382 while(zebra_rec_keys_read(reckeys, &str, &slen, &key_in))
1384 int ord = CAST_ZINT_TO_INT(key_in.mem[0]);
1386 for (p = ord_list; p ; p = p->next)
1394 p = xmalloc(sizeof(*p));
1405 struct ord_stat *p1 = p;
1408 zebraExplain_ord_adjust_occurrences(zei, p->ord, p->no, 1);
1410 zebraExplain_ord_adjust_occurrences(zei, p->ord, - p->no, -1);
1417 void extract_flush_record_keys2(ZebraHandle zh, zint sysno,
1418 zebra_rec_keys_t ins_keys, zint ins_rank,
1419 zebra_rec_keys_t del_keys, zint del_rank)
1421 ZebraExplainInfo zei = zh->reg->zei;
1425 if (!zh->reg->key_block)
1427 int mem = 1024*1024 * atoi( res_get_def( zh->res, "memmax", "8"));
1428 const char *key_tmp_dir = res_get_def(zh->res, "keyTmpDir", ".");
1429 int use_threads = atoi(res_get_def(zh->res, "threads", "1"));
1430 zh->reg->key_block = key_block_create(mem, key_tmp_dir, use_threads);
1435 extract_rec_keys_adjust(zh, 1, ins_keys);
1437 zebraExplain_recordCountIncrement(zei, 1);
1438 zebra_rec_keys_rewind(ins_keys);
1442 extract_rec_keys_adjust(zh, 0, del_keys);
1444 zebraExplain_recordCountIncrement(zei, -1);
1445 zebra_rec_keys_rewind(del_keys);
1451 const char *del_str;
1452 struct it_key del_key_in;
1456 const char *ins_str;
1457 struct it_key ins_key_in;
1461 del = zebra_rec_keys_read(del_keys, &del_str, &del_slen,
1464 ins = zebra_rec_keys_read(ins_keys, &ins_str, &ins_slen,
1467 if (del && ins && ins_rank == del_rank
1468 && !key_compare(&del_key_in, &ins_key_in)
1469 && ins_slen == del_slen && !memcmp(del_str, ins_str, del_slen))
1479 key_block_write(zh->reg->key_block, sysno,
1480 &del_key_in, 0, del_str, del_slen,
1481 del_rank, zh->m_staticrank);
1483 key_block_write(zh->reg->key_block, sysno,
1484 &ins_key_in, 1, ins_str, ins_slen,
1485 ins_rank, zh->m_staticrank);
1487 yaz_log(log_level_extract, "normal=%d optimized=%d", normal, optimized);
1491 ZEBRA_RES zebra_rec_keys_to_snippets1(ZebraHandle zh,
1492 zebra_rec_keys_t reckeys,
1493 zebra_snippets *snippets)
1495 NMEM nmem = nmem_create();
1496 if (zebra_rec_keys_rewind(reckeys))
1501 while (zebra_rec_keys_read(reckeys, &str, &slen, &key))
1506 const char *index_type;
1508 assert(key.len <= IT_KEY_LEVEL_MAX && key.len > 2);
1509 seqno = key.mem[key.len-1];
1510 ord = CAST_ZINT_TO_INT(key.mem[0]);
1512 zebraExplain_lookup_ord(zh->reg->zei, ord, &index_type,
1513 0/* db */, 0 /* string_index */);
1515 zebra_term_untrans_iconv(zh, nmem, index_type,
1517 zebra_snippets_append(snippets, seqno, 0, ord, dst_term);
1525 void print_rec_keys(ZebraHandle zh, zebra_rec_keys_t reckeys)
1527 yaz_log(YLOG_LOG, "print_rec_keys");
1528 if (zebra_rec_keys_rewind(reckeys))
1533 while (zebra_rec_keys_read(reckeys, &str, &slen, &key))
1535 char dst_buf[IT_MAX_WORD];
1537 const char *index_type;
1538 int ord = CAST_ZINT_TO_INT(key.mem[0]);
1540 assert(key.len <= IT_KEY_LEVEL_MAX && key.len > 2);
1542 zebraExplain_lookup_ord(zh->reg->zei, ord, &index_type, &db, 0);
1544 seqno = key.mem[key.len-1];
1546 zebra_term_untrans(zh, index_type, dst_buf, str);
1548 yaz_log(YLOG_LOG, "ord=%d seqno=" ZINT_FORMAT
1549 " term=%s", ord, seqno, dst_buf);
1554 static void extract_add_index_string(RecWord *p, zinfo_index_category_t cat,
1555 const char *str, int length)
1558 ZebraHandle zh = p->extractCtrl->handle;
1559 ZebraExplainInfo zei = zh->reg->zei;
1562 ch = zebraExplain_lookup_attr_str(zei, cat, p->index_type, p->index_name);
1564 ch = zebraExplain_add_attr_str(zei, cat, p->index_type, p->index_name);
1568 key.mem[i++] = p->record_id;
1569 key.mem[i++] = p->section_id;
1571 if (zh->m_segment_indexing)
1572 key.mem[i++] = p->segment;
1573 key.mem[i++] = p->seqno;
1576 zebra_rec_keys_write(zh->reg->keys, str, length, &key);
1579 static void extract_add_sort_string(RecWord *p, const char *str, int length)
1582 ZebraHandle zh = p->extractCtrl->handle;
1583 ZebraExplainInfo zei = zh->reg->zei;
1585 zinfo_index_category_t cat = zinfo_index_category_sort;
1587 ch = zebraExplain_lookup_attr_str(zei, cat, p->index_type, p->index_name);
1589 ch = zebraExplain_add_attr_str(zei, cat, p->index_type, p->index_name);
1592 key.mem[1] = p->record_id;
1594 zebra_rec_keys_write(zh->reg->sortKeys, str, length, &key);
1597 static void extract_add_staticrank_string(RecWord *p,
1598 const char *str, int length)
1601 struct recExtractCtrl *ctrl = p->extractCtrl;
1603 if (length > sizeof(valz)-1)
1604 length = sizeof(valz)-1;
1606 memcpy(valz, str, length);
1607 valz[length] = '\0';
1608 ctrl->staticrank = atozint(valz);
1611 static void extract_add_string(RecWord *p, zebra_map_t zm,
1612 const char *string, int length)
1618 if (log_level_details)
1621 WRBUF w = wrbuf_alloc();
1623 wrbuf_write_escaped(w, string, length);
1624 yaz_log(log_level_details, "extract_add_string: %s", wrbuf_cstr(w));
1627 if (zebra_maps_is_index(zm))
1629 extract_add_index_string(p, zinfo_index_category_index,
1631 if (zebra_maps_is_alwaysmatches(zm))
1634 memcpy(&word, p, sizeof(word));
1637 extract_add_index_string(
1638 &word, zinfo_index_category_alwaysmatches, "", 0);
1641 else if (zebra_maps_is_sort(zm))
1643 extract_add_sort_string(p, string, length);
1645 else if (zebra_maps_is_staticrank(zm))
1647 extract_add_staticrank_string(p, string, length);
1651 static void extract_add_incomplete_field(RecWord *p, zebra_map_t zm)
1653 const char *b = p->term_buf;
1654 int remain = p->term_len;
1656 const char **map = 0;
1659 map = zebra_maps_input(zm, &b, remain, 0);
1663 char buf[IT_MAX_WORD+1];
1667 while (map && *map && **map == *CHR_SPACE)
1669 remain = p->term_len - (b - p->term_buf);
1671 map = zebra_maps_input(zm, &b, remain, 0);
1678 while (map && *map && **map != *CHR_SPACE)
1680 const char *cp = *map;
1682 while (i < IT_MAX_WORD && *cp)
1684 remain = p->term_len - (b - p->term_buf);
1686 map = zebra_maps_input(zm, &b, remain, 0);
1696 if (zebra_maps_is_first_in_field(zm))
1698 /* first in field marker */
1699 extract_add_string(p, zm, FIRST_IN_FIELD_STR, FIRST_IN_FIELD_LEN);
1703 extract_add_string(p, zm, buf, i);
1708 static void extract_add_complete_field(RecWord *p, zebra_map_t zm)
1710 const char *b = p->term_buf;
1711 char buf[IT_MAX_WORD+1];
1712 const char **map = 0;
1713 int i = 0, remain = p->term_len;
1716 map = zebra_maps_input(zm, &b, remain, 1);
1718 while (remain > 0 && i < IT_MAX_WORD)
1720 while (map && *map && **map == *CHR_SPACE)
1722 remain = p->term_len - (b - p->term_buf);
1726 int first = i ? 0 : 1; /* first position */
1727 map = zebra_maps_input(zm, &b, remain, first);
1735 if (i && i < IT_MAX_WORD)
1736 buf[i++] = *CHR_SPACE;
1737 while (map && *map && **map != *CHR_SPACE)
1739 const char *cp = *map;
1741 if (**map == *CHR_CUT)
1747 if (i >= IT_MAX_WORD)
1749 while (i < IT_MAX_WORD && *cp)
1752 remain = p->term_len - (b - p->term_buf);
1755 map = zebra_maps_input(zm, &b, remain, 0);
1763 extract_add_string(p, zm, buf, i);
1766 static void extract_add_icu(RecWord *p, zebra_map_t zm)
1768 const char *res_buf = 0;
1771 zebra_map_tokenize_start(zm, p->term_buf, p->term_len);
1772 while (zebra_map_tokenize_next(zm, &res_buf, &res_len, 0, 0))
1774 extract_add_string(p, zm, res_buf, res_len);
1780 /** \brief top-level indexing handler for recctrl system
1781 \param p token data to be indexed
1785 extract_add_{in}_complete / extract_add_icu
1788 extract_add_index_string
1790 extract_add_sort_string
1792 extract_add_staticrank_string
1795 static void extract_token_add(RecWord *p)
1797 ZebraHandle zh = p->extractCtrl->handle;
1798 zebra_map_t zm = zebra_map_get_or_add(zh->reg->zebra_maps, p->index_type);
1801 if (log_level_details)
1803 yaz_log(log_level_details, "extract_token_add "
1804 "type=%s index=%s seqno=" ZINT_FORMAT " s=%.*s",
1805 p->index_type, p->index_name,
1806 p->seqno, p->term_len, p->term_buf);
1808 if ((wrbuf = zebra_replace(zm, 0, p->term_buf, p->term_len)))
1810 p->term_buf = wrbuf_buf(wrbuf);
1811 p->term_len = wrbuf_len(wrbuf);
1813 if (zebra_maps_is_icu(zm))
1815 extract_add_icu(p, zm);
1819 if (zebra_maps_is_complete(zm))
1820 extract_add_complete_field(p, zm);
1822 extract_add_incomplete_field(p, zm);
1826 static void extract_set_store_data_cb(struct recExtractCtrl *p,
1827 void *buf, size_t sz)
1829 ZebraHandle zh = (ZebraHandle) p->handle;
1831 xfree(zh->store_data_buf);
1832 zh->store_data_buf = 0;
1833 zh->store_data_size = 0;
1836 zh->store_data_buf = xmalloc(sz);
1837 zh->store_data_size = sz;
1838 memcpy(zh->store_data_buf, buf, sz);
1842 static void extract_set_store_data_prepare(struct recExtractCtrl *p)
1844 ZebraHandle zh = (ZebraHandle) p->handle;
1845 xfree(zh->store_data_buf);
1846 zh->store_data_buf = 0;
1847 zh->store_data_size = 0;
1848 p->setStoreData = extract_set_store_data_cb;
1851 static void extract_schema_add(struct recExtractCtrl *p, Odr_oid *oid)
1853 ZebraHandle zh = (ZebraHandle) p->handle;
1854 zebraExplain_addSchema(zh->reg->zei, oid);
1857 void extract_flush_sort_keys(ZebraHandle zh, zint sysno,
1858 int cmd, zebra_rec_keys_t reckeys)
1861 yaz_log(YLOG_LOG, "extract_flush_sort_keys cmd=%d sysno=" ZINT_FORMAT,
1863 extract_rec_keys_log(zh, cmd, reckeys, YLOG_LOG);
1866 if (zebra_rec_keys_rewind(reckeys))
1868 zebra_sort_index_t si = zh->reg->sort_index;
1871 struct it_key key_in;
1873 zebra_sort_sysno(si, sysno);
1875 while (zebra_rec_keys_read(reckeys, &str, &slen, &key_in))
1877 int ord = CAST_ZINT_TO_INT(key_in.mem[0]);
1879 zebra_sort_type(si, ord);
1881 zebra_sort_add(si, str, slen);
1883 zebra_sort_delete(si);
1891 * indent-tabs-mode: nil
1893 * vim: shiftwidth=4 tabstop=8 expandtab