1 /* $Id: extract.c,v 1.271 2007-12-07 14:09:09 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_token_add(RecWord *p)
270 struct snip_rec_info *h = p->extractCtrl->handle;
271 ZebraHandle zh = h->zh;
272 zebra_map_t zm = zebra_map_get(zh->reg->zebra_maps, p->index_type);
274 if (zm && zebra_maps_is_index(zm))
276 ZebraExplainInfo zei = zh->reg->zei;
277 int ch = zebraExplain_lookup_attr_str(
278 zei, zinfo_index_category_index, p->index_type, p->index_name);
280 if (zebra_maps_is_complete(zm))
281 snippet_add_complete_field(p, ch, zm);
283 snippet_add_incomplete_field(p, ch, zm);
287 static void snippet_schema_add(
288 struct recExtractCtrl *p, Odr_oid *oid)
293 void extract_snippet(ZebraHandle zh, zebra_snippets *sn,
294 struct ZebraRecStream *stream,
295 RecType rt, void *recTypeClientData)
297 struct recExtractCtrl extractCtrl;
298 struct snip_rec_info info;
301 extractCtrl.stream = stream;
302 extractCtrl.first_record = 1;
303 extractCtrl.init = extract_init;
304 extractCtrl.tokenAdd = snippet_token_add;
305 extractCtrl.schemaAdd = snippet_schema_add;
309 extractCtrl.dh = zh->reg->dh;
313 extractCtrl.handle = &info;
314 extractCtrl.match_criteria[0] = '\0';
315 extractCtrl.staticrank = 0;
316 extractCtrl.action = action_insert;
318 init_extractCtrl(zh, &extractCtrl);
320 extractCtrl.setStoreData = 0;
322 r = (*rt->extract)(recTypeClientData, &extractCtrl);
326 static void searchRecordKey(ZebraHandle zh,
327 zebra_rec_keys_t reckeys,
328 const char *index_name,
329 const char **ws, int ws_length)
333 zinfo_index_category_t cat = zinfo_index_category_index;
335 for (i = 0; i<ws_length; i++)
339 ch = zebraExplain_lookup_attr_str(zh->reg->zei, cat, "0", index_name);
341 ch = zebraExplain_lookup_attr_str(zh->reg->zei, cat, "p", index_name);
343 ch = zebraExplain_lookup_attr_str(zh->reg->zei, cat, "w", index_name);
348 if (zebra_rec_keys_rewind(reckeys))
355 while (zebra_rec_keys_read(reckeys, &str, &slen, &key))
357 assert(key.len <= IT_KEY_LEVEL_MAX && key.len > 2);
359 seqno = key.mem[key.len-1];
361 if (key.mem[0] == ch)
367 woff = seqno - startSeq;
368 if (woff >= 0 && woff < ws_length)
375 #define FILE_MATCH_BLANK "\t "
377 static char *get_match_from_spec(ZebraHandle zh,
378 zebra_rec_keys_t reckeys,
379 const char *fname, const char *spec)
381 static char dstBuf[2048]; /* static here ??? */
383 const char *s = spec;
387 for (; *s && strchr(FILE_MATCH_BLANK, *s); s++)
394 char attset_str[64], attname_str[64];
398 for (s++; strchr(FILE_MATCH_BLANK, *s); s++)
400 for (i = 0; *s && *s != ',' && *s != ')' &&
401 !strchr(FILE_MATCH_BLANK, *s); s++)
402 if (i+1 < sizeof(attset_str))
403 attset_str[i++] = *s;
404 attset_str[i] = '\0';
406 for (; strchr(FILE_MATCH_BLANK, *s); s++)
409 strcpy(attname_str, attset_str);
412 for (s++; strchr(FILE_MATCH_BLANK, *s); s++)
414 for (i = 0; *s && *s != ')' &&
415 !strchr(FILE_MATCH_BLANK, *s); s++)
416 if (i+1 < sizeof(attname_str))
417 attname_str[i++] = *s;
418 attname_str[i] = '\0';
421 searchRecordKey(zh, reckeys, attname_str, ws, 32);
425 yaz_log(YLOG_WARN, "Missing ) in match criteria %s in group %s",
426 spec, zh->m_group ? zh->m_group : "none");
431 for (i = 0; i<32; i++)
440 dst += strlen(ws[i]);
444 yaz_log(YLOG_WARN, "Record didn't contain match"
445 " fields in (%s,%s)", attset_str, attname_str);
453 const char *spec_src = NULL;
454 const char *s1 = ++s;
455 while (*s1 && !strchr(FILE_MATCH_BLANK, *s1))
459 if (spec_len > sizeof(special)-1)
460 spec_len = sizeof(special)-1;
461 memcpy(special, s, spec_len);
462 special[spec_len] = '\0';
465 if (!strcmp(special, "group"))
466 spec_src = zh->m_group;
467 else if (!strcmp(special, "database"))
468 spec_src = zh->basenames[0];
469 else if (!strcmp(special, "filename")) {
472 else if (!strcmp(special, "type"))
473 spec_src = zh->m_record_type;
478 strcpy(dst, spec_src);
479 dst += strlen(spec_src);
482 else if (*s == '\"' || *s == '\'')
484 int stopMarker = *s++;
488 while (*s && *s != stopMarker)
490 if (i+1 < sizeof(tmpString))
491 tmpString[i++] = *s++;
496 strcpy(dst, tmpString);
497 dst += strlen(tmpString);
501 yaz_log(YLOG_WARN, "Syntax error in match criteria %s in group %s",
502 spec, zh->m_group ? zh->m_group : "none");
509 yaz_log(YLOG_WARN, "No match criteria for record %s in group %s",
510 fname, zh->m_group ? zh->m_group : "none");
517 struct recordLogInfo {
520 struct recordGroup *rGroup;
523 /** \brief add the always-matches index entry and map to real record ID
524 \param ctrl record control
525 \param record_id custom record ID
526 \param sysno system record ID
528 This function serves two purposes.. It adds the always matches
529 entry and makes a pointer from the custom record ID (if defined)
530 back to the system record ID (sysno)
531 See zebra_recid_to_sysno .
533 static void all_matches_add(struct recExtractCtrl *ctrl, zint record_id,
537 extract_init(ctrl, &word);
538 word.record_id = record_id;
539 /* we use the seqno as placeholder for a way to get back to
540 record database from _ALLRECORDS.. This is used if a custom
541 RECORD was defined */
543 word.index_name = "_ALLRECORDS";
544 word.index_type = "w";
546 extract_add_index_string(&word, zinfo_index_category_alwaysmatches,
550 ZEBRA_RES zebra_extract_records_stream(ZebraHandle zh,
551 struct ZebraRecStream *stream,
552 enum zebra_recctrl_action_t action,
554 const char *recordType,
556 const char *match_criteria,
559 void *recTypeClientData);
562 ZEBRA_RES zebra_extract_file(ZebraHandle zh, zint *sysno, const char *fname,
565 ZEBRA_RES r = ZEBRA_OK;
570 struct file_read_info *fi = 0;
571 const char *original_record_type = 0;
573 void *recTypeClientData;
574 struct ZebraRecStream stream, *streamp;
576 zebra_init_log_level();
578 if (!zh->m_group || !*zh->m_group)
581 sprintf(gprefix, "%s.", zh->m_group);
583 yaz_log(log_level_extract, "zebra_extract_file %s", fname);
585 /* determine file extension */
587 for (i = strlen(fname); --i >= 0; )
590 else if (fname[i] == '.')
592 strcpy(ext, fname+i+1);
595 /* determine file type - depending on extension */
596 original_record_type = zh->m_record_type;
597 if (!zh->m_record_type)
599 sprintf(ext_res, "%srecordType.%s", gprefix, ext);
600 zh->m_record_type = res_get(zh->res, ext_res);
602 if (!zh->m_record_type)
605 if (zh->records_processed + zh->records_skipped
606 < zh->m_file_verbose_limit)
607 yaz_log(YLOG_LOG, "? %s", fname);
608 zh->records_skipped++;
611 /* determine match criteria */
612 if (!zh->m_record_id)
614 sprintf(ext_res, "%srecordId.%s", gprefix, ext);
615 zh->m_record_id = res_get(zh->res, ext_res);
619 recType_byName(zh->reg->recTypes, zh->res, zh->m_record_type,
620 &recTypeClientData)))
622 yaz_log(YLOG_WARN, "No such record type: %s", zh->m_record_type);
626 switch(recType->version)
631 yaz_log(YLOG_WARN, "Bad filter version: %s", zh->m_record_type);
633 if (sysno && deleteFlag)
642 if (zh->path_reg && !yaz_is_abspath(fname))
644 strcpy(full_rep, zh->path_reg);
645 strcat(full_rep, "/");
646 strcat(full_rep, fname);
649 strcpy(full_rep, fname);
651 if ((fd = open(full_rep, O_BINARY|O_RDONLY)) == -1)
653 yaz_log(YLOG_WARN|YLOG_ERRNO, "open %s", full_rep);
654 zh->m_record_type = original_record_type;
658 zebra_create_stream_fd(streamp, fd, 0);
660 r = zebra_extract_records_stream(zh, streamp,
662 action_delete : action_update,
666 0, /*match_criteria */
668 recType, recTypeClientData);
670 stream.destroy(streamp);
671 zh->m_record_type = original_record_type;
676 If sysno is provided, then it's used to identify the reocord.
677 If not, and match_criteria is provided, then sysno is guessed
678 If not, and a record is provided, then sysno is got from there
682 ZEBRA_RES zebra_buffer_extract_record(ZebraHandle zh,
683 const char *buf, size_t buf_size,
684 enum zebra_recctrl_action_t action,
686 const char *recordType,
688 const char *match_criteria,
691 struct ZebraRecStream stream;
696 if (recordType && *recordType)
698 yaz_log(log_level_extract,
699 "Record type explicitly specified: %s", recordType);
700 recType = recType_byName(zh->reg->recTypes, zh->res, recordType,
705 if (!(zh->m_record_type))
707 yaz_log(YLOG_WARN, "No such record type defined");
710 yaz_log(log_level_extract, "Get record type from rgroup: %s",
712 recType = recType_byName(zh->reg->recTypes, zh->res,
713 zh->m_record_type, &clientData);
714 recordType = zh->m_record_type;
719 yaz_log(YLOG_WARN, "No such record type: %s", recordType);
723 zebra_create_stream_mem(&stream, buf, buf_size);
725 res = zebra_extract_records_stream(zh, &stream,
732 recType, clientData);
733 stream.destroy(&stream);
737 ZEBRA_RES zebra_extract_records_stream(ZebraHandle zh,
738 struct ZebraRecStream *stream,
739 enum zebra_recctrl_action_t action,
741 const char *recordType,
743 const char *match_criteria,
746 void *recTypeClientData)
748 ZEBRA_RES res = ZEBRA_OK;
752 res = zebra_extract_record_stream(zh, stream,
759 recType, recTypeClientData, &more);
774 static WRBUF wrbuf_hex_str(const char *cstr)
777 WRBUF w = wrbuf_alloc();
778 for (i = 0; cstr[i]; i++)
780 if (cstr[i] < ' ' || cstr[i] > 126)
781 wrbuf_printf(w, "\\%02X", cstr[i] & 0xff);
783 wrbuf_putc(w, cstr[i]);
788 ZEBRA_RES zebra_extract_record_stream(ZebraHandle zh,
789 struct ZebraRecStream *stream,
790 enum zebra_recctrl_action_t action,
792 const char *recordType,
794 const char *match_criteria,
797 void *recTypeClientData,
802 RecordAttr *recordAttr;
803 struct recExtractCtrl extractCtrl;
805 const char *matchStr = 0;
807 off_t start_offset = 0, end_offset = 0;
808 const char *pr_fname = fname; /* filename to print .. */
809 int show_progress = zh->records_processed + zh->records_skipped
810 < zh->m_file_verbose_limit ? 1:0;
812 zebra_init_log_level();
815 pr_fname = "<no file>"; /* make it printable if file is omitted */
817 zebra_rec_keys_reset(zh->reg->keys);
818 zebra_rec_keys_reset(zh->reg->sortKeys);
820 if (zebraExplain_curDatabase(zh->reg->zei, zh->basenames[0]))
822 if (zebraExplain_newDatabase(zh->reg->zei, zh->basenames[0],
823 zh->m_explain_database))
829 off_t null_offset = 0;
830 extractCtrl.stream = stream;
832 start_offset = stream->tellf(stream);
834 extractCtrl.first_record = start_offset ? 0 : 1;
836 stream->endf(stream, &null_offset);;
838 extractCtrl.init = extract_init;
839 extractCtrl.tokenAdd = extract_token_add;
840 extractCtrl.schemaAdd = extract_schema_add;
841 extractCtrl.dh = zh->reg->dh;
842 extractCtrl.handle = zh;
843 extractCtrl.match_criteria[0] = '\0';
844 extractCtrl.staticrank = 0;
845 extractCtrl.action = action;
847 init_extractCtrl(zh, &extractCtrl);
849 extract_set_store_data_prepare(&extractCtrl);
851 r = (*recType->extract)(recTypeClientData, &extractCtrl);
853 if (action == action_update)
855 action = extractCtrl.action;
860 case RECCTRL_EXTRACT_EOF:
862 case RECCTRL_EXTRACT_ERROR_GENERIC:
863 /* error occured during extraction ... */
864 yaz_log(YLOG_WARN, "extract error: generic");
866 case RECCTRL_EXTRACT_ERROR_NO_SUCH_FILTER:
867 /* error occured during extraction ... */
868 yaz_log(YLOG_WARN, "extract error: no such filter");
870 case RECCTRL_EXTRACT_SKIP:
872 yaz_log(YLOG_LOG, "skip %s %s " ZINT_FORMAT,
873 recordType, pr_fname, (zint) start_offset);
876 end_offset = stream->endf(stream, 0);
878 stream->seekf(stream, end_offset);
881 case RECCTRL_EXTRACT_OK:
884 yaz_log(YLOG_WARN, "extract error: unknown error: %d", r);
887 end_offset = stream->endf(stream, 0);
889 stream->seekf(stream, end_offset);
891 end_offset = stream->tellf(stream);
893 if (extractCtrl.match_criteria[0])
894 match_criteria = extractCtrl.match_criteria;
902 if (match_criteria && *match_criteria) {
903 matchStr = match_criteria;
905 if (zh->m_record_id && *zh->m_record_id) {
906 matchStr = get_match_from_spec(zh, zh->reg->keys, pr_fname,
910 yaz_log(YLOG_LOG, "error %s %s " ZINT_FORMAT, recordType,
911 pr_fname, (zint) start_offset);
918 int db_ord = zebraExplain_get_database_ord(zh->reg->zei);
919 char *rinfo = dict_lookup_ord(zh->reg->matchDict, db_ord,
923 if (log_level_extract)
925 WRBUF w = wrbuf_hex_str(matchStr);
926 yaz_log(log_level_extract, "matchStr: %s", wrbuf_cstr(w));
931 assert(*rinfo == sizeof(*sysno));
932 memcpy(sysno, rinfo+1, sizeof(*sysno));
937 if (zebra_rec_keys_empty(zh->reg->keys))
939 /* the extraction process returned no information - the record
940 is probably empty - unless flagShowRecords is in use */
948 if (action == action_delete)
950 yaz_log(YLOG_LOG, "delete %s %s " ZINT_FORMAT, recordType,
951 pr_fname, (zint) start_offset);
952 yaz_log(YLOG_WARN, "cannot delete record above (seems new)");
955 else if (action == action_replace)
957 yaz_log(YLOG_LOG, "update %s %s " ZINT_FORMAT, recordType,
958 pr_fname, (zint) start_offset);
959 yaz_log(YLOG_WARN, "cannot update record above (seems new)");
963 yaz_log(YLOG_LOG, "add %s %s " ZINT_FORMAT, recordType, pr_fname,
964 (zint) start_offset);
965 rec = rec_new(zh->reg->records);
972 all_matches_add(&extractCtrl,
973 zebra_rec_keys_get_custom_record_id(zh->reg->keys),
978 recordAttr = rec_init_attr(zh->reg->zei, rec);
979 if (extractCtrl.staticrank < 0)
981 yaz_log(YLOG_WARN, "Negative staticrank for record. Set to 0");
982 extractCtrl.staticrank = 0;
987 int db_ord = zebraExplain_get_database_ord(zh->reg->zei);
988 dict_insert_ord(zh->reg->matchDict, db_ord, matchStr,
989 sizeof(*sysno), sysno);
992 extract_flush_sort_keys(zh, *sysno, 1, zh->reg->sortKeys);
994 extract_flush_record_keys2(zh, *sysno,
995 zh->reg->keys, extractCtrl.staticrank,
996 0, recordAttr->staticrank);
998 extract_flush_record_keys(zh, *sysno, 1, zh->reg->keys,
999 extractCtrl.staticrank);
1001 recordAttr->staticrank = extractCtrl.staticrank;
1002 zh->records_inserted++;
1006 /* record already exists */
1007 zebra_rec_keys_t delkeys = zebra_rec_keys_open();
1008 zebra_rec_keys_t sortKeys = zebra_rec_keys_open();
1009 if (action == action_insert)
1011 yaz_log(YLOG_LOG, "skipped %s %s " ZINT_FORMAT,
1012 recordType, pr_fname, (zint) start_offset);
1017 rec = rec_get(zh->reg->records, *sysno);
1022 all_matches_add(&extractCtrl,
1023 zebra_rec_keys_get_custom_record_id(zh->reg->keys),
1027 recordAttr = rec_init_attr(zh->reg->zei, rec);
1029 /* decrease total size */
1030 zebraExplain_recordBytesIncrement(zh->reg->zei,
1031 - recordAttr->recordSize);
1033 zebra_rec_keys_set_buf(delkeys,
1034 rec->info[recInfo_delKeys],
1035 rec->size[recInfo_delKeys],
1037 zebra_rec_keys_set_buf(sortKeys,
1038 rec->info[recInfo_sortKeys],
1039 rec->size[recInfo_sortKeys],
1042 extract_flush_sort_keys(zh, *sysno, 0, sortKeys);
1044 extract_flush_record_keys(zh, *sysno, 0, delkeys,
1045 recordAttr->staticrank);
1047 if (action == action_delete)
1049 /* record going to be deleted */
1051 extract_flush_record_keys2(zh, *sysno, 0, recordAttr->staticrank,
1052 delkeys, recordAttr->staticrank);
1054 if (zebra_rec_keys_empty(delkeys))
1056 yaz_log(YLOG_LOG, "delete %s %s " ZINT_FORMAT, recordType,
1057 pr_fname, (zint) start_offset);
1058 yaz_log(YLOG_WARN, "cannot delete file above, "
1059 "storeKeys false (3)");
1064 yaz_log(YLOG_LOG, "delete %s %s " ZINT_FORMAT, recordType,
1065 pr_fname, (zint) start_offset);
1066 zh->records_deleted++;
1069 int db_ord = zebraExplain_get_database_ord(zh->reg->zei);
1070 dict_delete_ord(zh->reg->matchDict, db_ord, matchStr);
1072 rec_del(zh->reg->records, &rec);
1074 zebra_rec_keys_close(delkeys);
1075 zebra_rec_keys_close(sortKeys);
1081 { /* update or special_update */
1083 yaz_log(YLOG_LOG, "update %s %s " ZINT_FORMAT, recordType,
1084 pr_fname, (zint) start_offset);
1085 extract_flush_sort_keys(zh, *sysno, 1, zh->reg->sortKeys);
1088 extract_flush_record_keys2(zh, *sysno,
1089 zh->reg->keys, extractCtrl.staticrank,
1090 delkeys, recordAttr->staticrank);
1092 extract_flush_record_keys(zh, *sysno, 1,
1093 zh->reg->keys, extractCtrl.staticrank);
1095 recordAttr->staticrank = extractCtrl.staticrank;
1096 zh->records_updated++;
1098 zebra_rec_keys_close(delkeys);
1099 zebra_rec_keys_close(sortKeys);
1101 /* update file type */
1102 xfree(rec->info[recInfo_fileType]);
1103 rec->info[recInfo_fileType] =
1104 rec_strdup(recordType, &rec->size[recInfo_fileType]);
1106 /* update filename */
1107 xfree(rec->info[recInfo_filename]);
1108 rec->info[recInfo_filename] =
1109 rec_strdup(fname, &rec->size[recInfo_filename]);
1111 /* update delete keys */
1112 xfree(rec->info[recInfo_delKeys]);
1113 if (!zebra_rec_keys_empty(zh->reg->keys) && zh->m_store_keys == 1)
1115 zebra_rec_keys_get_buf(zh->reg->keys,
1116 &rec->info[recInfo_delKeys],
1117 &rec->size[recInfo_delKeys]);
1121 rec->info[recInfo_delKeys] = NULL;
1122 rec->size[recInfo_delKeys] = 0;
1124 /* update sort keys */
1125 xfree(rec->info[recInfo_sortKeys]);
1127 zebra_rec_keys_get_buf(zh->reg->sortKeys,
1128 &rec->info[recInfo_sortKeys],
1129 &rec->size[recInfo_sortKeys]);
1133 recordAttr->recordSize = end_offset - start_offset;
1134 zebraExplain_recordBytesIncrement(zh->reg->zei,
1135 recordAttr->recordSize);
1138 /* set run-number for this record */
1139 recordAttr->runNumber =
1140 zebraExplain_runNumberIncrement(zh->reg->zei, 0);
1142 /* update store data */
1143 xfree(rec->info[recInfo_storeData]);
1145 /* update store data */
1146 if (zh->store_data_buf)
1148 rec->size[recInfo_storeData] = zh->store_data_size;
1149 rec->info[recInfo_storeData] = zh->store_data_buf;
1150 zh->store_data_buf = 0;
1151 recordAttr->recordSize = zh->store_data_size;
1153 else if (zh->m_store_data)
1155 off_t cur_offset = stream->tellf(stream);
1157 rec->size[recInfo_storeData] = recordAttr->recordSize;
1158 rec->info[recInfo_storeData] = (char *)
1159 xmalloc(recordAttr->recordSize);
1160 stream->seekf(stream, start_offset);
1161 stream->readf(stream, rec->info[recInfo_storeData],
1162 recordAttr->recordSize);
1163 stream->seekf(stream, cur_offset);
1167 rec->info[recInfo_storeData] = NULL;
1168 rec->size[recInfo_storeData] = 0;
1170 /* update database name */
1171 xfree(rec->info[recInfo_databaseName]);
1172 rec->info[recInfo_databaseName] =
1173 rec_strdup(zh->basenames[0], &rec->size[recInfo_databaseName]);
1176 recordAttr->recordOffset = start_offset;
1178 /* commit this record */
1179 rec_put(zh->reg->records, &rec);
1184 ZEBRA_RES zebra_extract_explain(void *handle, Record rec, data1_node *n)
1186 ZebraHandle zh = (ZebraHandle) handle;
1187 struct recExtractCtrl extractCtrl;
1189 if (zebraExplain_curDatabase(zh->reg->zei,
1190 rec->info[recInfo_databaseName]))
1193 if (zebraExplain_newDatabase(zh->reg->zei,
1194 rec->info[recInfo_databaseName], 0))
1198 zebra_rec_keys_reset(zh->reg->keys);
1199 zebra_rec_keys_reset(zh->reg->sortKeys);
1201 extractCtrl.init = extract_init;
1202 extractCtrl.tokenAdd = extract_token_add;
1203 extractCtrl.schemaAdd = extract_schema_add;
1204 extractCtrl.dh = zh->reg->dh;
1206 init_extractCtrl(zh, &extractCtrl);
1208 extractCtrl.flagShowRecords = 0;
1209 extractCtrl.match_criteria[0] = '\0';
1210 extractCtrl.staticrank = 0;
1211 extractCtrl.action = action_update;
1213 extractCtrl.handle = handle;
1214 extractCtrl.first_record = 1;
1216 extract_set_store_data_prepare(&extractCtrl);
1219 grs_extract_tree(&extractCtrl, n);
1221 if (rec->size[recInfo_delKeys])
1223 zebra_rec_keys_t delkeys = zebra_rec_keys_open();
1225 zebra_rec_keys_t sortkeys = zebra_rec_keys_open();
1227 zebra_rec_keys_set_buf(delkeys, rec->info[recInfo_delKeys],
1228 rec->size[recInfo_delKeys],
1231 extract_flush_record_keys2(zh, rec->sysno,
1232 zh->reg->keys, 0, delkeys, 0);
1234 extract_flush_record_keys(zh, rec->sysno, 0, delkeys, 0);
1235 extract_flush_record_keys(zh, rec->sysno, 1, zh->reg->keys, 0);
1237 zebra_rec_keys_close(delkeys);
1239 zebra_rec_keys_set_buf(sortkeys, rec->info[recInfo_sortKeys],
1240 rec->size[recInfo_sortKeys],
1243 extract_flush_sort_keys(zh, rec->sysno, 0, sortkeys);
1244 zebra_rec_keys_close(sortkeys);
1249 extract_flush_record_keys2(zh, rec->sysno, zh->reg->keys, 0, 0, 0);
1251 extract_flush_record_keys(zh, rec->sysno, 1, zh->reg->keys, 0);
1254 extract_flush_sort_keys(zh, rec->sysno, 1, zh->reg->sortKeys);
1256 xfree(rec->info[recInfo_delKeys]);
1257 zebra_rec_keys_get_buf(zh->reg->keys,
1258 &rec->info[recInfo_delKeys],
1259 &rec->size[recInfo_delKeys]);
1261 xfree(rec->info[recInfo_sortKeys]);
1262 zebra_rec_keys_get_buf(zh->reg->sortKeys,
1263 &rec->info[recInfo_sortKeys],
1264 &rec->size[recInfo_sortKeys]);
1268 void extract_rec_keys_log(ZebraHandle zh, int is_insert,
1269 zebra_rec_keys_t reckeys,
1272 if (zebra_rec_keys_rewind(reckeys))
1277 NMEM nmem = nmem_create();
1279 while(zebra_rec_keys_read(reckeys, &str, &slen, &key))
1281 char keystr[200]; /* room for zints to print */
1283 int ord = CAST_ZINT_TO_INT(key.mem[0]);
1284 const char *index_type;
1286 const char *string_index;
1288 zebraExplain_lookup_ord(zh->reg->zei, ord, &index_type,
1289 0/* db */, &string_index);
1291 zebra_term_untrans_iconv(zh, nmem, index_type,
1294 for (i = 0; i<key.len; i++)
1296 sprintf(keystr + strlen(keystr), ZINT_FORMAT " ", key.mem[i]);
1299 if (*str < CHR_BASE_CHAR)
1302 char dst_buf[200]; /* room for special chars */
1304 strcpy(dst_buf , "?");
1306 if (!strcmp(str, ""))
1307 strcpy(dst_buf, "alwaysmatches");
1308 if (!strcmp(str, FIRST_IN_FIELD_STR))
1309 strcpy(dst_buf, "firstinfield");
1310 else if (!strcmp(str, CHR_UNKNOWN))
1311 strcpy(dst_buf, "unknown");
1312 else if (!strcmp(str, CHR_SPACE))
1313 strcpy(dst_buf, "space");
1315 for (i = 0; i<slen; i++)
1317 sprintf(dst_buf + strlen(dst_buf), " %d", str[i] & 0xff);
1319 yaz_log(level, "%s%s %s %s", keystr, index_type,
1320 string_index, dst_buf);
1324 yaz_log(level, "%s%s %s \"%s\"", keystr, index_type,
1325 string_index, dst_term);
1333 void extract_rec_keys_adjust(ZebraHandle zh, int is_insert,
1334 zebra_rec_keys_t reckeys)
1336 ZebraExplainInfo zei = zh->reg->zei;
1340 struct ord_stat *next;
1343 if (zebra_rec_keys_rewind(reckeys))
1345 struct ord_stat *ord_list = 0;
1349 struct it_key key_in;
1350 while(zebra_rec_keys_read(reckeys, &str, &slen, &key_in))
1352 int ord = CAST_ZINT_TO_INT(key_in.mem[0]);
1354 for (p = ord_list; p ; p = p->next)
1362 p = xmalloc(sizeof(*p));
1373 struct ord_stat *p1 = p;
1376 zebraExplain_ord_adjust_occurrences(zei, p->ord, p->no, 1);
1378 zebraExplain_ord_adjust_occurrences(zei, p->ord, - p->no, -1);
1385 void extract_flush_record_keys2(ZebraHandle zh, zint sysno,
1386 zebra_rec_keys_t ins_keys, zint ins_rank,
1387 zebra_rec_keys_t del_keys, zint del_rank)
1389 ZebraExplainInfo zei = zh->reg->zei;
1393 if (!zh->reg->key_block)
1395 int mem = 1024*1024 * atoi( res_get_def( zh->res, "memmax", "8"));
1396 const char *key_tmp_dir = res_get_def(zh->res, "keyTmpDir", ".");
1397 int use_threads = atoi(res_get_def(zh->res, "threads", "1"));
1398 zh->reg->key_block = key_block_create(mem, key_tmp_dir, use_threads);
1403 extract_rec_keys_adjust(zh, 1, ins_keys);
1405 zebraExplain_recordCountIncrement(zei, 1);
1406 zebra_rec_keys_rewind(ins_keys);
1410 extract_rec_keys_adjust(zh, 0, del_keys);
1412 zebraExplain_recordCountIncrement(zei, -1);
1413 zebra_rec_keys_rewind(del_keys);
1419 const char *del_str;
1420 struct it_key del_key_in;
1424 const char *ins_str;
1425 struct it_key ins_key_in;
1429 del = zebra_rec_keys_read(del_keys, &del_str, &del_slen,
1432 ins = zebra_rec_keys_read(ins_keys, &ins_str, &ins_slen,
1435 if (del && ins && ins_rank == del_rank
1436 && !key_compare(&del_key_in, &ins_key_in)
1437 && ins_slen == del_slen && !memcmp(del_str, ins_str, del_slen))
1447 key_block_write(zh->reg->key_block, sysno,
1448 &del_key_in, 0, del_str, del_slen,
1449 del_rank, zh->m_staticrank);
1451 key_block_write(zh->reg->key_block, sysno,
1452 &ins_key_in, 1, ins_str, ins_slen,
1453 ins_rank, zh->m_staticrank);
1455 yaz_log(log_level_extract, "normal=%d optimized=%d", normal, optimized);
1459 ZEBRA_RES zebra_rec_keys_to_snippets(ZebraHandle zh,
1460 zebra_rec_keys_t reckeys,
1461 zebra_snippets *snippets)
1463 NMEM nmem = nmem_create();
1464 if (zebra_rec_keys_rewind(reckeys))
1469 while (zebra_rec_keys_read(reckeys, &str, &slen, &key))
1474 const char *index_type;
1476 assert(key.len <= IT_KEY_LEVEL_MAX && key.len > 2);
1477 seqno = key.mem[key.len-1];
1478 ord = CAST_ZINT_TO_INT(key.mem[0]);
1480 zebraExplain_lookup_ord(zh->reg->zei, ord, &index_type,
1481 0/* db */, 0 /* string_index */);
1483 zebra_term_untrans_iconv(zh, nmem, index_type,
1485 zebra_snippets_append(snippets, seqno, 0, ord, dst_term);
1493 void print_rec_keys(ZebraHandle zh, zebra_rec_keys_t reckeys)
1495 yaz_log(YLOG_LOG, "print_rec_keys");
1496 if (zebra_rec_keys_rewind(reckeys))
1501 while (zebra_rec_keys_read(reckeys, &str, &slen, &key))
1503 char dst_buf[IT_MAX_WORD];
1505 const char *index_type;
1506 int ord = CAST_ZINT_TO_INT(key.mem[0]);
1508 assert(key.len <= IT_KEY_LEVEL_MAX && key.len > 2);
1510 zebraExplain_lookup_ord(zh->reg->zei, ord, &index_type, &db, 0);
1512 seqno = key.mem[key.len-1];
1514 zebra_term_untrans(zh, index_type, dst_buf, str);
1516 yaz_log(YLOG_LOG, "ord=%d seqno=" ZINT_FORMAT
1517 " term=%s", ord, seqno, dst_buf);
1522 static void extract_add_index_string(RecWord *p, zinfo_index_category_t cat,
1523 const char *str, int length)
1526 ZebraHandle zh = p->extractCtrl->handle;
1527 ZebraExplainInfo zei = zh->reg->zei;
1530 ch = zebraExplain_lookup_attr_str(zei, cat, p->index_type, p->index_name);
1532 ch = zebraExplain_add_attr_str(zei, cat, p->index_type, p->index_name);
1536 key.mem[i++] = p->record_id;
1537 key.mem[i++] = p->section_id;
1539 if (zh->m_segment_indexing)
1540 key.mem[i++] = p->segment;
1541 key.mem[i++] = p->seqno;
1544 zebra_rec_keys_write(zh->reg->keys, str, length, &key);
1547 static void extract_add_sort_string(RecWord *p, const char *str, int length)
1550 ZebraHandle zh = p->extractCtrl->handle;
1551 ZebraExplainInfo zei = zh->reg->zei;
1553 zinfo_index_category_t cat = zinfo_index_category_sort;
1555 ch = zebraExplain_lookup_attr_str(zei, cat, p->index_type, p->index_name);
1557 ch = zebraExplain_add_attr_str(zei, cat, p->index_type, p->index_name);
1560 key.mem[1] = p->record_id;
1562 zebra_rec_keys_write(zh->reg->sortKeys, str, length, &key);
1565 static void extract_add_staticrank_string(RecWord *p,
1566 const char *str, int length)
1569 struct recExtractCtrl *ctrl = p->extractCtrl;
1571 if (length > sizeof(valz)-1)
1572 length = sizeof(valz)-1;
1574 memcpy(valz, str, length);
1575 valz[length] = '\0';
1576 ctrl->staticrank = atozint(valz);
1579 static void extract_add_string(RecWord *p, zebra_map_t zm,
1580 const char *string, int length)
1587 if (zebra_maps_is_index(zm))
1589 extract_add_index_string(p, zinfo_index_category_index,
1591 if (zebra_maps_is_alwaysmatches(zm))
1594 memcpy(&word, p, sizeof(word));
1597 extract_add_index_string(
1598 &word, zinfo_index_category_alwaysmatches, "", 0);
1601 else if (zebra_maps_is_sort(zm))
1603 extract_add_sort_string(p, string, length);
1605 else if (zebra_maps_is_staticrank(zm))
1607 extract_add_staticrank_string(p, string, length);
1611 static void extract_add_incomplete_field(RecWord *p, zebra_map_t zm)
1613 const char *b = p->term_buf;
1614 int remain = p->term_len;
1616 const char **map = 0;
1619 map = zebra_maps_input(zm, &b, remain, 0);
1623 char buf[IT_MAX_WORD+1];
1627 while (map && *map && **map == *CHR_SPACE)
1629 remain = p->term_len - (b - p->term_buf);
1631 map = zebra_maps_input(zm, &b, remain, 0);
1638 while (map && *map && **map != *CHR_SPACE)
1640 const char *cp = *map;
1642 while (i < IT_MAX_WORD && *cp)
1644 remain = p->term_len - (b - p->term_buf);
1646 map = zebra_maps_input(zm, &b, remain, 0);
1656 if (zebra_maps_is_first_in_field(zm))
1658 /* first in field marker */
1659 extract_add_string(p, zm, FIRST_IN_FIELD_STR, FIRST_IN_FIELD_LEN);
1663 extract_add_string(p, zm, buf, i);
1668 static void extract_add_complete_field(RecWord *p, zebra_map_t zm)
1670 const char *b = p->term_buf;
1671 char buf[IT_MAX_WORD+1];
1672 const char **map = 0;
1673 int i = 0, remain = p->term_len;
1676 map = zebra_maps_input(zm, &b, remain, 1);
1678 while (remain > 0 && i < IT_MAX_WORD)
1680 while (map && *map && **map == *CHR_SPACE)
1682 remain = p->term_len - (b - p->term_buf);
1686 int first = i ? 0 : 1; /* first position */
1687 map = zebra_maps_input(zm, &b, remain, first);
1695 if (i && i < IT_MAX_WORD)
1696 buf[i++] = *CHR_SPACE;
1697 while (map && *map && **map != *CHR_SPACE)
1699 const char *cp = *map;
1701 if (**map == *CHR_CUT)
1707 if (i >= IT_MAX_WORD)
1709 while (i < IT_MAX_WORD && *cp)
1712 remain = p->term_len - (b - p->term_buf);
1715 map = zebra_maps_input(zm, &b, remain, 0);
1723 extract_add_string(p, zm, buf, i);
1726 static void extract_add_icu(RecWord *p, zebra_map_t zm)
1729 const char *res_buf = 0;
1731 ZebraHandle zh = p->extractCtrl->handle;
1733 int cat = zinfo_index_category_index;
1734 int ch = zebraExplain_lookup_attr_str(zh->reg->zei, cat,
1735 p->index_type, p->index_name);
1737 ch = zebraExplain_add_attr_str(zh->reg->zei, cat,
1738 p->index_type, p->index_name);
1739 zebra_map_tokenize_start(zm, p->term_buf, p->term_len);
1740 while (zebra_map_tokenize_next(zm, &res_buf, &res_len))
1744 key.mem[i++] = p->record_id;
1745 key.mem[i++] = p->section_id;
1747 if (zh->m_segment_indexing)
1748 key.mem[i++] = p->segment;
1749 key.mem[i++] = p->seqno;
1752 zebra_rec_keys_write(zh->reg->keys, res_buf, res_len, &key);
1759 /** \brief top-level indexing handler for recctrl system
1760 \param p token data to be indexed
1764 zebra_add_{in}_complete
1767 extract_add_index_string
1769 extract_add_sort_string
1771 extract_add_staticrank_string
1774 static void extract_token_add(RecWord *p)
1776 ZebraHandle zh = p->extractCtrl->handle;
1777 zebra_map_t zm = zebra_map_get_or_add(zh->reg->zebra_maps, p->index_type);
1780 if (log_level_details)
1782 yaz_log(log_level_details, "extract_token_add "
1783 "type=%s index=%s seqno=" ZINT_FORMAT " s=%.*s",
1784 p->index_type, p->index_name,
1785 p->seqno, p->term_len, p->term_buf);
1787 if ((wrbuf = zebra_replace(zm, 0, p->term_buf, p->term_len)))
1789 p->term_buf = wrbuf_buf(wrbuf);
1790 p->term_len = wrbuf_len(wrbuf);
1792 if (zebra_maps_is_icu(zm))
1794 extract_add_icu(p, zm);
1798 if (zebra_maps_is_complete(zm))
1799 extract_add_complete_field(p, zm);
1801 extract_add_incomplete_field(p, zm);
1805 static void extract_set_store_data_cb(struct recExtractCtrl *p,
1806 void *buf, size_t sz)
1808 ZebraHandle zh = (ZebraHandle) p->handle;
1810 xfree(zh->store_data_buf);
1811 zh->store_data_buf = 0;
1812 zh->store_data_size = 0;
1815 zh->store_data_buf = xmalloc(sz);
1816 zh->store_data_size = sz;
1817 memcpy(zh->store_data_buf, buf, sz);
1821 static void extract_set_store_data_prepare(struct recExtractCtrl *p)
1823 ZebraHandle zh = (ZebraHandle) p->handle;
1824 xfree(zh->store_data_buf);
1825 zh->store_data_buf = 0;
1826 zh->store_data_size = 0;
1827 p->setStoreData = extract_set_store_data_cb;
1830 static void extract_schema_add(struct recExtractCtrl *p, Odr_oid *oid)
1832 ZebraHandle zh = (ZebraHandle) p->handle;
1833 zebraExplain_addSchema(zh->reg->zei, oid);
1836 void extract_flush_sort_keys(ZebraHandle zh, zint sysno,
1837 int cmd, zebra_rec_keys_t reckeys)
1840 yaz_log(YLOG_LOG, "extract_flush_sort_keys cmd=%d sysno=" ZINT_FORMAT,
1842 extract_rec_keys_log(zh, cmd, reckeys, YLOG_LOG);
1845 if (zebra_rec_keys_rewind(reckeys))
1847 zebra_sort_index_t si = zh->reg->sort_index;
1850 struct it_key key_in;
1852 zebra_sort_sysno(si, sysno);
1854 while (zebra_rec_keys_read(reckeys, &str, &slen, &key_in))
1856 int ord = CAST_ZINT_TO_INT(key_in.mem[0]);
1858 zebra_sort_type(si, ord);
1860 zebra_sort_add(si, str, slen);
1862 zebra_sort_delete(si);
1870 * indent-tabs-mode: nil
1872 * vim: shiftwidth=4 tabstop=8 expandtab