1 /* $Id: extract.c,v 1.269 2007-11-08 21:21:58 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 static void all_matches_add(struct recExtractCtrl *ctrl)
526 extract_init(ctrl, &word);
527 word.index_name = "_ALLRECORDS";
528 word.index_type = "w";
530 extract_add_index_string(&word, zinfo_index_category_alwaysmatches,
534 ZEBRA_RES zebra_extract_records_stream(ZebraHandle zh,
535 struct ZebraRecStream *stream,
536 enum zebra_recctrl_action_t action,
538 const char *recordType,
540 const char *match_criteria,
543 void *recTypeClientData);
546 ZEBRA_RES zebra_extract_file(ZebraHandle zh, zint *sysno, const char *fname,
549 ZEBRA_RES r = ZEBRA_OK;
554 struct file_read_info *fi = 0;
555 const char *original_record_type = 0;
557 void *recTypeClientData;
558 struct ZebraRecStream stream, *streamp;
560 zebra_init_log_level();
562 if (!zh->m_group || !*zh->m_group)
565 sprintf(gprefix, "%s.", zh->m_group);
567 yaz_log(log_level_extract, "zebra_extract_file %s", fname);
569 /* determine file extension */
571 for (i = strlen(fname); --i >= 0; )
574 else if (fname[i] == '.')
576 strcpy(ext, fname+i+1);
579 /* determine file type - depending on extension */
580 original_record_type = zh->m_record_type;
581 if (!zh->m_record_type)
583 sprintf(ext_res, "%srecordType.%s", gprefix, ext);
584 zh->m_record_type = res_get(zh->res, ext_res);
586 if (!zh->m_record_type)
589 if (zh->records_processed + zh->records_skipped
590 < zh->m_file_verbose_limit)
591 yaz_log(YLOG_LOG, "? %s", fname);
592 zh->records_skipped++;
595 /* determine match criteria */
596 if (!zh->m_record_id)
598 sprintf(ext_res, "%srecordId.%s", gprefix, ext);
599 zh->m_record_id = res_get(zh->res, ext_res);
603 recType_byName(zh->reg->recTypes, zh->res, zh->m_record_type,
604 &recTypeClientData)))
606 yaz_log(YLOG_WARN, "No such record type: %s", zh->m_record_type);
610 switch(recType->version)
615 yaz_log(YLOG_WARN, "Bad filter version: %s", zh->m_record_type);
617 if (sysno && deleteFlag)
626 if (zh->path_reg && !yaz_is_abspath(fname))
628 strcpy(full_rep, zh->path_reg);
629 strcat(full_rep, "/");
630 strcat(full_rep, fname);
633 strcpy(full_rep, fname);
635 if ((fd = open(full_rep, O_BINARY|O_RDONLY)) == -1)
637 yaz_log(YLOG_WARN|YLOG_ERRNO, "open %s", full_rep);
638 zh->m_record_type = original_record_type;
642 zebra_create_stream_fd(streamp, fd, 0);
644 r = zebra_extract_records_stream(zh, streamp,
646 action_delete : action_update,
650 0, /*match_criteria */
652 recType, recTypeClientData);
654 stream.destroy(streamp);
655 zh->m_record_type = original_record_type;
660 If sysno is provided, then it's used to identify the reocord.
661 If not, and match_criteria is provided, then sysno is guessed
662 If not, and a record is provided, then sysno is got from there
666 ZEBRA_RES zebra_buffer_extract_record(ZebraHandle zh,
667 const char *buf, size_t buf_size,
668 enum zebra_recctrl_action_t action,
670 const char *recordType,
672 const char *match_criteria,
675 struct ZebraRecStream stream;
680 if (recordType && *recordType)
682 yaz_log(log_level_extract,
683 "Record type explicitly specified: %s", recordType);
684 recType = recType_byName(zh->reg->recTypes, zh->res, recordType,
689 if (!(zh->m_record_type))
691 yaz_log(YLOG_WARN, "No such record type defined");
694 yaz_log(log_level_extract, "Get record type from rgroup: %s",
696 recType = recType_byName(zh->reg->recTypes, zh->res,
697 zh->m_record_type, &clientData);
698 recordType = zh->m_record_type;
703 yaz_log(YLOG_WARN, "No such record type: %s", recordType);
707 zebra_create_stream_mem(&stream, buf, buf_size);
709 res = zebra_extract_records_stream(zh, &stream,
716 recType, clientData);
717 stream.destroy(&stream);
721 ZEBRA_RES zebra_extract_records_stream(ZebraHandle zh,
722 struct ZebraRecStream *stream,
723 enum zebra_recctrl_action_t action,
725 const char *recordType,
727 const char *match_criteria,
730 void *recTypeClientData)
732 ZEBRA_RES res = ZEBRA_OK;
736 res = zebra_extract_record_stream(zh, stream,
743 recType, recTypeClientData, &more);
758 static WRBUF wrbuf_hex_str(const char *cstr)
761 WRBUF w = wrbuf_alloc();
762 for (i = 0; cstr[i]; i++)
764 if (cstr[i] < ' ' || cstr[i] > 126)
765 wrbuf_printf(w, "\\%02X", cstr[i] & 0xff);
767 wrbuf_putc(w, cstr[i]);
772 ZEBRA_RES zebra_extract_record_stream(ZebraHandle zh,
773 struct ZebraRecStream *stream,
774 enum zebra_recctrl_action_t action,
776 const char *recordType,
778 const char *match_criteria,
781 void *recTypeClientData,
786 RecordAttr *recordAttr;
787 struct recExtractCtrl extractCtrl;
789 const char *matchStr = 0;
791 off_t start_offset = 0, end_offset = 0;
792 const char *pr_fname = fname; /* filename to print .. */
793 int show_progress = zh->records_processed + zh->records_skipped
794 < zh->m_file_verbose_limit ? 1:0;
796 zebra_init_log_level();
799 pr_fname = "<no file>"; /* make it printable if file is omitted */
801 zebra_rec_keys_reset(zh->reg->keys);
802 zebra_rec_keys_reset(zh->reg->sortKeys);
804 if (zebraExplain_curDatabase(zh->reg->zei, zh->basenames[0]))
806 if (zebraExplain_newDatabase(zh->reg->zei, zh->basenames[0],
807 zh->m_explain_database))
813 off_t null_offset = 0;
814 extractCtrl.stream = stream;
816 start_offset = stream->tellf(stream);
818 extractCtrl.first_record = start_offset ? 0 : 1;
820 stream->endf(stream, &null_offset);;
822 extractCtrl.init = extract_init;
823 extractCtrl.tokenAdd = extract_token_add;
824 extractCtrl.schemaAdd = extract_schema_add;
825 extractCtrl.dh = zh->reg->dh;
826 extractCtrl.handle = zh;
827 extractCtrl.match_criteria[0] = '\0';
828 extractCtrl.staticrank = 0;
829 extractCtrl.action = action;
831 init_extractCtrl(zh, &extractCtrl);
833 extract_set_store_data_prepare(&extractCtrl);
835 r = (*recType->extract)(recTypeClientData, &extractCtrl);
837 if (action == action_update)
839 action = extractCtrl.action;
844 case RECCTRL_EXTRACT_EOF:
846 case RECCTRL_EXTRACT_ERROR_GENERIC:
847 /* error occured during extraction ... */
848 yaz_log(YLOG_WARN, "extract error: generic");
850 case RECCTRL_EXTRACT_ERROR_NO_SUCH_FILTER:
851 /* error occured during extraction ... */
852 yaz_log(YLOG_WARN, "extract error: no such filter");
854 case RECCTRL_EXTRACT_SKIP:
856 yaz_log(YLOG_LOG, "skip %s %s " ZINT_FORMAT,
857 recordType, pr_fname, (zint) start_offset);
860 end_offset = stream->endf(stream, 0);
862 stream->seekf(stream, end_offset);
865 case RECCTRL_EXTRACT_OK:
868 yaz_log(YLOG_WARN, "extract error: unknown error: %d", r);
871 end_offset = stream->endf(stream, 0);
873 stream->seekf(stream, end_offset);
875 end_offset = stream->tellf(stream);
877 all_matches_add(&extractCtrl);
879 if (extractCtrl.match_criteria[0])
880 match_criteria = extractCtrl.match_criteria;
888 if (match_criteria && *match_criteria) {
889 matchStr = match_criteria;
891 if (zh->m_record_id && *zh->m_record_id) {
892 matchStr = get_match_from_spec(zh, zh->reg->keys, pr_fname,
896 yaz_log(YLOG_LOG, "error %s %s " ZINT_FORMAT, recordType,
897 pr_fname, (zint) start_offset);
904 int db_ord = zebraExplain_get_database_ord(zh->reg->zei);
905 char *rinfo = dict_lookup_ord(zh->reg->matchDict, db_ord,
909 if (log_level_extract)
911 WRBUF w = wrbuf_hex_str(matchStr);
912 yaz_log(log_level_extract, "matchStr: %s", wrbuf_cstr(w));
917 assert(*rinfo == sizeof(*sysno));
918 memcpy(sysno, rinfo+1, sizeof(*sysno));
922 if (zebra_rec_keys_empty(zh->reg->keys))
924 /* the extraction process returned no information - the record
925 is probably empty - unless flagShowRecords is in use */
933 if (action == action_delete)
935 yaz_log(YLOG_LOG, "delete %s %s " ZINT_FORMAT, recordType,
936 pr_fname, (zint) start_offset);
937 yaz_log(YLOG_WARN, "cannot delete record above (seems new)");
940 else if (action == action_replace)
942 yaz_log(YLOG_LOG, "update %s %s " ZINT_FORMAT, recordType,
943 pr_fname, (zint) start_offset);
944 yaz_log(YLOG_WARN, "cannot update record above (seems new)");
948 yaz_log(YLOG_LOG, "add %s %s " ZINT_FORMAT, recordType, pr_fname,
949 (zint) start_offset);
950 rec = rec_new(zh->reg->records);
954 recordAttr = rec_init_attr(zh->reg->zei, rec);
955 if (extractCtrl.staticrank < 0)
957 yaz_log(YLOG_WARN, "Negative staticrank for record. Set to 0");
958 extractCtrl.staticrank = 0;
963 int db_ord = zebraExplain_get_database_ord(zh->reg->zei);
964 dict_insert_ord(zh->reg->matchDict, db_ord, matchStr,
965 sizeof(*sysno), sysno);
968 extract_flush_sort_keys(zh, *sysno, 1, zh->reg->sortKeys);
970 extract_flush_record_keys2(zh, *sysno,
971 zh->reg->keys, extractCtrl.staticrank,
972 0, recordAttr->staticrank);
974 extract_flush_record_keys(zh, *sysno, 1, zh->reg->keys,
975 extractCtrl.staticrank);
977 recordAttr->staticrank = extractCtrl.staticrank;
978 zh->records_inserted++;
982 /* record already exists */
983 zebra_rec_keys_t delkeys = zebra_rec_keys_open();
984 zebra_rec_keys_t sortKeys = zebra_rec_keys_open();
985 if (action == action_insert)
987 yaz_log(YLOG_LOG, "skipped %s %s " ZINT_FORMAT,
988 recordType, pr_fname, (zint) start_offset);
993 rec = rec_get(zh->reg->records, *sysno);
996 recordAttr = rec_init_attr(zh->reg->zei, rec);
998 /* decrease total size */
999 zebraExplain_recordBytesIncrement(zh->reg->zei,
1000 - recordAttr->recordSize);
1002 zebra_rec_keys_set_buf(delkeys,
1003 rec->info[recInfo_delKeys],
1004 rec->size[recInfo_delKeys],
1006 zebra_rec_keys_set_buf(sortKeys,
1007 rec->info[recInfo_sortKeys],
1008 rec->size[recInfo_sortKeys],
1011 extract_flush_sort_keys(zh, *sysno, 0, sortKeys);
1013 extract_flush_record_keys(zh, *sysno, 0, delkeys,
1014 recordAttr->staticrank);
1016 if (action == action_delete)
1018 /* record going to be deleted */
1020 extract_flush_record_keys2(zh, *sysno, 0, recordAttr->staticrank,
1021 delkeys, recordAttr->staticrank);
1023 if (zebra_rec_keys_empty(delkeys))
1025 yaz_log(YLOG_LOG, "delete %s %s " ZINT_FORMAT, recordType,
1026 pr_fname, (zint) start_offset);
1027 yaz_log(YLOG_WARN, "cannot delete file above, "
1028 "storeKeys false (3)");
1033 yaz_log(YLOG_LOG, "delete %s %s " ZINT_FORMAT, recordType,
1034 pr_fname, (zint) start_offset);
1035 zh->records_deleted++;
1038 int db_ord = zebraExplain_get_database_ord(zh->reg->zei);
1039 dict_delete_ord(zh->reg->matchDict, db_ord, matchStr);
1041 rec_del(zh->reg->records, &rec);
1043 zebra_rec_keys_close(delkeys);
1044 zebra_rec_keys_close(sortKeys);
1050 { /* update or special_update */
1052 yaz_log(YLOG_LOG, "update %s %s " ZINT_FORMAT, recordType,
1053 pr_fname, (zint) start_offset);
1054 extract_flush_sort_keys(zh, *sysno, 1, zh->reg->sortKeys);
1057 extract_flush_record_keys2(zh, *sysno,
1058 zh->reg->keys, extractCtrl.staticrank,
1059 delkeys, recordAttr->staticrank);
1061 extract_flush_record_keys(zh, *sysno, 1,
1062 zh->reg->keys, extractCtrl.staticrank);
1064 recordAttr->staticrank = extractCtrl.staticrank;
1065 zh->records_updated++;
1067 zebra_rec_keys_close(delkeys);
1068 zebra_rec_keys_close(sortKeys);
1070 /* update file type */
1071 xfree(rec->info[recInfo_fileType]);
1072 rec->info[recInfo_fileType] =
1073 rec_strdup(recordType, &rec->size[recInfo_fileType]);
1075 /* update filename */
1076 xfree(rec->info[recInfo_filename]);
1077 rec->info[recInfo_filename] =
1078 rec_strdup(fname, &rec->size[recInfo_filename]);
1080 /* update delete keys */
1081 xfree(rec->info[recInfo_delKeys]);
1082 if (!zebra_rec_keys_empty(zh->reg->keys) && zh->m_store_keys == 1)
1084 zebra_rec_keys_get_buf(zh->reg->keys,
1085 &rec->info[recInfo_delKeys],
1086 &rec->size[recInfo_delKeys]);
1090 rec->info[recInfo_delKeys] = NULL;
1091 rec->size[recInfo_delKeys] = 0;
1093 /* update sort keys */
1094 xfree(rec->info[recInfo_sortKeys]);
1096 zebra_rec_keys_get_buf(zh->reg->sortKeys,
1097 &rec->info[recInfo_sortKeys],
1098 &rec->size[recInfo_sortKeys]);
1102 recordAttr->recordSize = end_offset - start_offset;
1103 zebraExplain_recordBytesIncrement(zh->reg->zei,
1104 recordAttr->recordSize);
1107 /* set run-number for this record */
1108 recordAttr->runNumber =
1109 zebraExplain_runNumberIncrement(zh->reg->zei, 0);
1111 /* update store data */
1112 xfree(rec->info[recInfo_storeData]);
1114 /* update store data */
1115 if (zh->store_data_buf)
1117 rec->size[recInfo_storeData] = zh->store_data_size;
1118 rec->info[recInfo_storeData] = zh->store_data_buf;
1119 zh->store_data_buf = 0;
1120 recordAttr->recordSize = zh->store_data_size;
1122 else if (zh->m_store_data)
1124 off_t cur_offset = stream->tellf(stream);
1126 rec->size[recInfo_storeData] = recordAttr->recordSize;
1127 rec->info[recInfo_storeData] = (char *)
1128 xmalloc(recordAttr->recordSize);
1129 stream->seekf(stream, start_offset);
1130 stream->readf(stream, rec->info[recInfo_storeData],
1131 recordAttr->recordSize);
1132 stream->seekf(stream, cur_offset);
1136 rec->info[recInfo_storeData] = NULL;
1137 rec->size[recInfo_storeData] = 0;
1139 /* update database name */
1140 xfree(rec->info[recInfo_databaseName]);
1141 rec->info[recInfo_databaseName] =
1142 rec_strdup(zh->basenames[0], &rec->size[recInfo_databaseName]);
1145 recordAttr->recordOffset = start_offset;
1147 /* commit this record */
1148 rec_put(zh->reg->records, &rec);
1153 ZEBRA_RES zebra_extract_explain(void *handle, Record rec, data1_node *n)
1155 ZebraHandle zh = (ZebraHandle) handle;
1156 struct recExtractCtrl extractCtrl;
1158 if (zebraExplain_curDatabase(zh->reg->zei,
1159 rec->info[recInfo_databaseName]))
1162 if (zebraExplain_newDatabase(zh->reg->zei,
1163 rec->info[recInfo_databaseName], 0))
1167 zebra_rec_keys_reset(zh->reg->keys);
1168 zebra_rec_keys_reset(zh->reg->sortKeys);
1170 extractCtrl.init = extract_init;
1171 extractCtrl.tokenAdd = extract_token_add;
1172 extractCtrl.schemaAdd = extract_schema_add;
1173 extractCtrl.dh = zh->reg->dh;
1175 init_extractCtrl(zh, &extractCtrl);
1177 extractCtrl.flagShowRecords = 0;
1178 extractCtrl.match_criteria[0] = '\0';
1179 extractCtrl.staticrank = 0;
1180 extractCtrl.action = action_update;
1182 extractCtrl.handle = handle;
1183 extractCtrl.first_record = 1;
1185 extract_set_store_data_prepare(&extractCtrl);
1188 grs_extract_tree(&extractCtrl, n);
1190 if (rec->size[recInfo_delKeys])
1192 zebra_rec_keys_t delkeys = zebra_rec_keys_open();
1194 zebra_rec_keys_t sortkeys = zebra_rec_keys_open();
1196 zebra_rec_keys_set_buf(delkeys, rec->info[recInfo_delKeys],
1197 rec->size[recInfo_delKeys],
1200 extract_flush_record_keys2(zh, rec->sysno,
1201 zh->reg->keys, 0, delkeys, 0);
1203 extract_flush_record_keys(zh, rec->sysno, 0, delkeys, 0);
1204 extract_flush_record_keys(zh, rec->sysno, 1, zh->reg->keys, 0);
1206 zebra_rec_keys_close(delkeys);
1208 zebra_rec_keys_set_buf(sortkeys, rec->info[recInfo_sortKeys],
1209 rec->size[recInfo_sortKeys],
1212 extract_flush_sort_keys(zh, rec->sysno, 0, sortkeys);
1213 zebra_rec_keys_close(sortkeys);
1218 extract_flush_record_keys2(zh, rec->sysno, zh->reg->keys, 0, 0, 0);
1220 extract_flush_record_keys(zh, rec->sysno, 1, zh->reg->keys, 0);
1223 extract_flush_sort_keys(zh, rec->sysno, 1, zh->reg->sortKeys);
1225 xfree(rec->info[recInfo_delKeys]);
1226 zebra_rec_keys_get_buf(zh->reg->keys,
1227 &rec->info[recInfo_delKeys],
1228 &rec->size[recInfo_delKeys]);
1230 xfree(rec->info[recInfo_sortKeys]);
1231 zebra_rec_keys_get_buf(zh->reg->sortKeys,
1232 &rec->info[recInfo_sortKeys],
1233 &rec->size[recInfo_sortKeys]);
1237 void extract_rec_keys_log(ZebraHandle zh, int is_insert,
1238 zebra_rec_keys_t reckeys,
1241 if (zebra_rec_keys_rewind(reckeys))
1246 NMEM nmem = nmem_create();
1248 while(zebra_rec_keys_read(reckeys, &str, &slen, &key))
1250 char keystr[200]; /* room for zints to print */
1252 int ord = CAST_ZINT_TO_INT(key.mem[0]);
1253 const char *index_type;
1255 const char *string_index;
1257 zebraExplain_lookup_ord(zh->reg->zei, ord, &index_type,
1258 0/* db */, &string_index);
1260 zebra_term_untrans_iconv(zh, nmem, index_type,
1263 for (i = 0; i<key.len; i++)
1265 sprintf(keystr + strlen(keystr), ZINT_FORMAT " ", key.mem[i]);
1268 if (*str < CHR_BASE_CHAR)
1271 char dst_buf[200]; /* room for special chars */
1273 strcpy(dst_buf , "?");
1275 if (!strcmp(str, ""))
1276 strcpy(dst_buf, "alwaysmatches");
1277 if (!strcmp(str, FIRST_IN_FIELD_STR))
1278 strcpy(dst_buf, "firstinfield");
1279 else if (!strcmp(str, CHR_UNKNOWN))
1280 strcpy(dst_buf, "unknown");
1281 else if (!strcmp(str, CHR_SPACE))
1282 strcpy(dst_buf, "space");
1284 for (i = 0; i<slen; i++)
1286 sprintf(dst_buf + strlen(dst_buf), " %d", str[i] & 0xff);
1288 yaz_log(level, "%s%s %s %s", keystr, index_type,
1289 string_index, dst_buf);
1293 yaz_log(level, "%s%s %s \"%s\"", keystr, index_type,
1294 string_index, dst_term);
1302 void extract_rec_keys_adjust(ZebraHandle zh, int is_insert,
1303 zebra_rec_keys_t reckeys)
1305 ZebraExplainInfo zei = zh->reg->zei;
1309 struct ord_stat *next;
1312 if (zebra_rec_keys_rewind(reckeys))
1314 struct ord_stat *ord_list = 0;
1318 struct it_key key_in;
1319 while(zebra_rec_keys_read(reckeys, &str, &slen, &key_in))
1321 int ord = CAST_ZINT_TO_INT(key_in.mem[0]);
1323 for (p = ord_list; p ; p = p->next)
1331 p = xmalloc(sizeof(*p));
1342 struct ord_stat *p1 = p;
1345 zebraExplain_ord_adjust_occurrences(zei, p->ord, p->no, 1);
1347 zebraExplain_ord_adjust_occurrences(zei, p->ord, - p->no, -1);
1354 void extract_flush_record_keys2(ZebraHandle zh, zint sysno,
1355 zebra_rec_keys_t ins_keys, zint ins_rank,
1356 zebra_rec_keys_t del_keys, zint del_rank)
1358 ZebraExplainInfo zei = zh->reg->zei;
1362 if (!zh->reg->key_block)
1364 int mem = 1024*1024 * atoi( res_get_def( zh->res, "memmax", "8"));
1365 const char *key_tmp_dir = res_get_def(zh->res, "keyTmpDir", ".");
1366 int use_threads = atoi(res_get_def(zh->res, "threads", "1"));
1367 zh->reg->key_block = key_block_create(mem, key_tmp_dir, use_threads);
1372 extract_rec_keys_adjust(zh, 1, ins_keys);
1374 zebraExplain_recordCountIncrement(zei, 1);
1375 zebra_rec_keys_rewind(ins_keys);
1379 extract_rec_keys_adjust(zh, 0, del_keys);
1381 zebraExplain_recordCountIncrement(zei, -1);
1382 zebra_rec_keys_rewind(del_keys);
1388 const char *del_str;
1389 struct it_key del_key_in;
1393 const char *ins_str;
1394 struct it_key ins_key_in;
1398 del = zebra_rec_keys_read(del_keys, &del_str, &del_slen,
1401 ins = zebra_rec_keys_read(ins_keys, &ins_str, &ins_slen,
1404 if (del && ins && ins_rank == del_rank
1405 && !key_compare(&del_key_in, &ins_key_in)
1406 && ins_slen == del_slen && !memcmp(del_str, ins_str, del_slen))
1416 key_block_write(zh->reg->key_block, sysno,
1417 &del_key_in, 0, del_str, del_slen,
1418 del_rank, zh->m_staticrank);
1420 key_block_write(zh->reg->key_block, sysno,
1421 &ins_key_in, 1, ins_str, ins_slen,
1422 ins_rank, zh->m_staticrank);
1424 yaz_log(log_level_extract, "normal=%d optimized=%d", normal, optimized);
1428 ZEBRA_RES zebra_rec_keys_to_snippets(ZebraHandle zh,
1429 zebra_rec_keys_t reckeys,
1430 zebra_snippets *snippets)
1432 NMEM nmem = nmem_create();
1433 if (zebra_rec_keys_rewind(reckeys))
1438 while (zebra_rec_keys_read(reckeys, &str, &slen, &key))
1443 const char *index_type;
1445 assert(key.len <= IT_KEY_LEVEL_MAX && key.len > 2);
1446 seqno = key.mem[key.len-1];
1447 ord = CAST_ZINT_TO_INT(key.mem[0]);
1449 zebraExplain_lookup_ord(zh->reg->zei, ord, &index_type,
1450 0/* db */, 0 /* string_index */);
1452 zebra_term_untrans_iconv(zh, nmem, index_type,
1454 zebra_snippets_append(snippets, seqno, 0, ord, dst_term);
1462 void print_rec_keys(ZebraHandle zh, zebra_rec_keys_t reckeys)
1464 yaz_log(YLOG_LOG, "print_rec_keys");
1465 if (zebra_rec_keys_rewind(reckeys))
1470 while (zebra_rec_keys_read(reckeys, &str, &slen, &key))
1472 char dst_buf[IT_MAX_WORD];
1474 const char *index_type;
1475 int ord = CAST_ZINT_TO_INT(key.mem[0]);
1477 assert(key.len <= IT_KEY_LEVEL_MAX && key.len > 2);
1479 zebraExplain_lookup_ord(zh->reg->zei, ord, &index_type, &db, 0);
1481 seqno = key.mem[key.len-1];
1483 zebra_term_untrans(zh, index_type, dst_buf, str);
1485 yaz_log(YLOG_LOG, "ord=%d seqno=" ZINT_FORMAT
1486 " term=%s", ord, seqno, dst_buf);
1491 static void extract_add_index_string(RecWord *p, zinfo_index_category_t cat,
1492 const char *str, int length)
1495 ZebraHandle zh = p->extractCtrl->handle;
1496 ZebraExplainInfo zei = zh->reg->zei;
1499 ch = zebraExplain_lookup_attr_str(zei, cat, p->index_type, p->index_name);
1501 ch = zebraExplain_add_attr_str(zei, cat, p->index_type, p->index_name);
1505 key.mem[i++] = p->record_id;
1506 key.mem[i++] = p->section_id;
1508 if (zh->m_segment_indexing)
1509 key.mem[i++] = p->segment;
1510 key.mem[i++] = p->seqno;
1513 zebra_rec_keys_write(zh->reg->keys, str, length, &key);
1516 static void extract_add_sort_string(RecWord *p, const char *str, int length)
1519 ZebraHandle zh = p->extractCtrl->handle;
1520 ZebraExplainInfo zei = zh->reg->zei;
1522 zinfo_index_category_t cat = zinfo_index_category_sort;
1524 ch = zebraExplain_lookup_attr_str(zei, cat, p->index_type, p->index_name);
1526 ch = zebraExplain_add_attr_str(zei, cat, p->index_type, p->index_name);
1529 key.mem[1] = p->record_id;
1531 zebra_rec_keys_write(zh->reg->sortKeys, str, length, &key);
1534 static void extract_add_staticrank_string(RecWord *p,
1535 const char *str, int length)
1538 struct recExtractCtrl *ctrl = p->extractCtrl;
1540 if (length > sizeof(valz)-1)
1541 length = sizeof(valz)-1;
1543 memcpy(valz, str, length);
1544 valz[length] = '\0';
1545 ctrl->staticrank = atozint(valz);
1548 static void extract_add_string(RecWord *p, zebra_map_t zm,
1549 const char *string, int length)
1556 if (zebra_maps_is_index(zm))
1558 extract_add_index_string(p, zinfo_index_category_index,
1560 if (zebra_maps_is_alwaysmatches(zm))
1563 memcpy(&word, p, sizeof(word));
1566 extract_add_index_string(
1567 &word, zinfo_index_category_alwaysmatches, "", 0);
1570 else if (zebra_maps_is_sort(zm))
1572 extract_add_sort_string(p, string, length);
1574 else if (zebra_maps_is_staticrank(zm))
1576 extract_add_staticrank_string(p, string, length);
1580 static void extract_add_incomplete_field(RecWord *p, zebra_map_t zm)
1582 const char *b = p->term_buf;
1583 int remain = p->term_len;
1585 const char **map = 0;
1588 map = zebra_maps_input(zm, &b, remain, 0);
1592 char buf[IT_MAX_WORD+1];
1596 while (map && *map && **map == *CHR_SPACE)
1598 remain = p->term_len - (b - p->term_buf);
1600 map = zebra_maps_input(zm, &b, remain, 0);
1607 while (map && *map && **map != *CHR_SPACE)
1609 const char *cp = *map;
1611 while (i < IT_MAX_WORD && *cp)
1613 remain = p->term_len - (b - p->term_buf);
1615 map = zebra_maps_input(zm, &b, remain, 0);
1625 if (zebra_maps_is_first_in_field(zm))
1627 /* first in field marker */
1628 extract_add_string(p, zm, FIRST_IN_FIELD_STR, FIRST_IN_FIELD_LEN);
1632 extract_add_string(p, zm, buf, i);
1637 static void extract_add_complete_field(RecWord *p, zebra_map_t zm)
1639 const char *b = p->term_buf;
1640 char buf[IT_MAX_WORD+1];
1641 const char **map = 0;
1642 int i = 0, remain = p->term_len;
1645 map = zebra_maps_input(zm, &b, remain, 1);
1647 while (remain > 0 && i < IT_MAX_WORD)
1649 while (map && *map && **map == *CHR_SPACE)
1651 remain = p->term_len - (b - p->term_buf);
1655 int first = i ? 0 : 1; /* first position */
1656 map = zebra_maps_input(zm, &b, remain, first);
1664 if (i && i < IT_MAX_WORD)
1665 buf[i++] = *CHR_SPACE;
1666 while (map && *map && **map != *CHR_SPACE)
1668 const char *cp = *map;
1670 if (**map == *CHR_CUT)
1676 if (i >= IT_MAX_WORD)
1678 while (i < IT_MAX_WORD && *cp)
1681 remain = p->term_len - (b - p->term_buf);
1684 map = zebra_maps_input(zm, &b, remain, 0);
1692 extract_add_string(p, zm, buf, i);
1695 static void extract_add_icu(RecWord *p, zebra_map_t zm)
1698 const char *res_buf = 0;
1700 ZebraHandle zh = p->extractCtrl->handle;
1701 int r = zebra_map_tokenize(zm, p->term_buf, p->term_len,
1702 &res_buf, &res_len);
1703 int cat = zinfo_index_category_index;
1704 int ch = zebraExplain_lookup_attr_str(zh->reg->zei, cat, p->index_type, p->index_name);
1706 ch = zebraExplain_add_attr_str(zh->reg->zei, cat, p->index_type, p->index_name);
1711 key.mem[i++] = p->record_id;
1712 key.mem[i++] = p->section_id;
1714 if (zh->m_segment_indexing)
1715 key.mem[i++] = p->segment;
1716 key.mem[i++] = p->seqno;
1719 zebra_rec_keys_write(zh->reg->keys, res_buf, res_len, &key);
1722 r = zebra_map_tokenize(zm, 0, 0, &res_buf, &res_len);
1727 /** \brief top-level indexing handler for recctrl system
1728 \param p token data to be indexed
1732 zebra_add_{in}_complete
1735 extract_add_index_string
1737 extract_add_sort_string
1739 extract_add_staticrank_string
1742 static void extract_token_add(RecWord *p)
1744 ZebraHandle zh = p->extractCtrl->handle;
1745 zebra_map_t zm = zebra_map_get_or_add(zh->reg->zebra_maps, p->index_type);
1748 if (log_level_details)
1750 yaz_log(log_level_details, "extract_token_add "
1751 "type=%s index=%s seqno=" ZINT_FORMAT " s=%.*s",
1752 p->index_type, p->index_name,
1753 p->seqno, p->term_len, p->term_buf);
1755 if ((wrbuf = zebra_replace(zm, 0, p->term_buf, p->term_len)))
1757 p->term_buf = wrbuf_buf(wrbuf);
1758 p->term_len = wrbuf_len(wrbuf);
1760 if (zebra_maps_is_icu(zm))
1762 extract_add_icu(p, zm);
1766 if (zebra_maps_is_complete(zm))
1767 extract_add_complete_field(p, zm);
1769 extract_add_incomplete_field(p, zm);
1773 static void extract_set_store_data_cb(struct recExtractCtrl *p,
1774 void *buf, size_t sz)
1776 ZebraHandle zh = (ZebraHandle) p->handle;
1778 xfree(zh->store_data_buf);
1779 zh->store_data_buf = 0;
1780 zh->store_data_size = 0;
1783 zh->store_data_buf = xmalloc(sz);
1784 zh->store_data_size = sz;
1785 memcpy(zh->store_data_buf, buf, sz);
1789 static void extract_set_store_data_prepare(struct recExtractCtrl *p)
1791 ZebraHandle zh = (ZebraHandle) p->handle;
1792 xfree(zh->store_data_buf);
1793 zh->store_data_buf = 0;
1794 zh->store_data_size = 0;
1795 p->setStoreData = extract_set_store_data_cb;
1798 static void extract_schema_add(struct recExtractCtrl *p, Odr_oid *oid)
1800 ZebraHandle zh = (ZebraHandle) p->handle;
1801 zebraExplain_addSchema(zh->reg->zei, oid);
1804 void extract_flush_sort_keys(ZebraHandle zh, zint sysno,
1805 int cmd, zebra_rec_keys_t reckeys)
1808 yaz_log(YLOG_LOG, "extract_flush_sort_keys cmd=%d sysno=" ZINT_FORMAT,
1810 extract_rec_keys_log(zh, cmd, reckeys, YLOG_LOG);
1813 if (zebra_rec_keys_rewind(reckeys))
1815 zebra_sort_index_t si = zh->reg->sort_index;
1818 struct it_key key_in;
1820 zebra_sort_sysno(si, sysno);
1822 while (zebra_rec_keys_read(reckeys, &str, &slen, &key_in))
1824 int ord = CAST_ZINT_TO_INT(key_in.mem[0]);
1826 zebra_sort_type(si, ord);
1828 zebra_sort_add(si, str, slen);
1830 zebra_sort_delete(si);
1838 * indent-tabs-mode: nil
1840 * vim: shiftwidth=4 tabstop=8 expandtab