1 /* $Id: extract.c,v 1.264 2007-10-29 13:43:57 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_record_keys(ZebraHandle zh, zint sysno,
71 int cmd, zebra_rec_keys_t reckeys,
73 static void extract_flush_sort_keys(ZebraHandle zh, zint sysno,
74 int cmd, zebra_rec_keys_t skp);
75 static void extract_schema_add(struct recExtractCtrl *p, Odr_oid *oid);
76 static void extract_token_add(RecWord *p);
77 static void extract_token_add2(RecWord *p);
79 static void check_log_limit(ZebraHandle zh)
81 if (zh->records_processed + zh->records_skipped == zh->m_file_verbose_limit)
83 yaz_log(YLOG_LOG, "More than %d file log entries. Omitting rest",
84 zh->m_file_verbose_limit);
88 static void logRecord(ZebraHandle zh)
91 ++zh->records_processed;
92 if (!(zh->records_processed % 1000))
94 yaz_log(YLOG_LOG, "Records: "ZINT_FORMAT" i/u/d "
95 ZINT_FORMAT"/"ZINT_FORMAT"/"ZINT_FORMAT,
96 zh->records_processed, zh->records_inserted,
97 zh->records_updated, zh->records_deleted);
101 static void init_extractCtrl(ZebraHandle zh, struct recExtractCtrl *ctrl)
104 for (i = 0; i<256; i++)
106 if (zebra_maps_is_positioned(zh->reg->zebra_maps, i))
111 ctrl->flagShowRecords = !zh->m_flag_rw;
115 static void extract_add_index_string(RecWord *p,
116 zinfo_index_category_t cat,
117 const char *str, int length);
119 static void extract_set_store_data_prepare(struct recExtractCtrl *p);
121 static void extract_init(struct recExtractCtrl *p, RecWord *w)
124 w->index_name = "any";
132 struct snip_rec_info {
134 zebra_snippets *snippets;
138 static void snippet_add_complete_field(RecWord *p, int ord)
140 struct snip_rec_info *h = p->extractCtrl->handle;
141 ZebraHandle zh = h->zh;
143 const char *b = p->term_buf;
144 char buf[IT_MAX_WORD+1];
145 const char **map = 0;
146 int i = 0, remain = p->term_len;
147 const char *start = b;
148 const char *last = 0;
151 map = zebra_maps_input(zh->reg->zebra_maps, p->index_type, &b, remain, 1);
153 while (remain > 0 && i < IT_MAX_WORD)
155 while (map && *map && **map == *CHR_SPACE)
157 remain = p->term_len - (b - p->term_buf);
160 start = b; /* set to first non-ws area */
163 int first = i ? 0 : 1; /* first position */
165 map = zebra_maps_input(zh->reg->zebra_maps, p->index_type,
174 if (i && i < IT_MAX_WORD)
175 buf[i++] = *CHR_SPACE;
176 while (map && *map && **map != *CHR_SPACE)
178 const char *cp = *map;
180 if (**map == *CHR_CUT)
186 if (i >= IT_MAX_WORD)
188 while (i < IT_MAX_WORD && *cp)
192 remain = p->term_len - (b - p->term_buf);
195 map = zebra_maps_input(zh->reg->zebra_maps, p->index_type, &b,
204 if (last && start != last)
205 zebra_snippets_appendn(h->snippets, p->seqno, 0, ord,
206 start, last - start);
209 static void snippet_add_incomplete_field(RecWord *p, int ord)
211 struct snip_rec_info *h = p->extractCtrl->handle;
212 ZebraHandle zh = h->zh;
213 const char *b = p->term_buf;
214 int remain = p->term_len;
216 const char **map = 0;
217 const char *start = b;
218 const char *last = b;
221 map = zebra_maps_input(zh->reg->zebra_maps, p->index_type, &b, remain, 0);
225 char buf[IT_MAX_WORD+1];
229 while (map && *map && **map == *CHR_SPACE)
231 remain = p->term_len - (b - p->term_buf);
234 map = zebra_maps_input(zh->reg->zebra_maps, p->index_type, &b,
243 zebra_snippets_appendn(h->snippets, p->seqno, 1, ord,
244 start, last - start);
250 while (map && *map && **map != *CHR_SPACE)
252 const char *cp = *map;
254 while (i < IT_MAX_WORD && *cp)
256 remain = p->term_len - (b - p->term_buf);
259 map = zebra_maps_input(zh->reg->zebra_maps, p->index_type, &b, remain, 0);
269 if (zebra_maps_is_first_in_field(zh->reg->zebra_maps, p->index_type))
271 /* first in field marker */
276 zebra_snippets_appendn(h->snippets, p->seqno, 0, ord,
277 start, last - start);
284 static void snippet_token_add(RecWord *p)
286 struct snip_rec_info *h = p->extractCtrl->handle;
287 ZebraHandle zh = h->zh;
289 if (zebra_maps_is_index(zh->reg->zebra_maps, p->index_type))
291 ZebraExplainInfo zei = zh->reg->zei;
292 int ch = zebraExplain_lookup_attr_str(
293 zei, zinfo_index_category_index, p->index_type, p->index_name);
295 if(zebra_maps_is_complete (h->zh->reg->zebra_maps, p->index_type))
296 snippet_add_complete_field(p, ch);
298 snippet_add_incomplete_field(p, ch);
302 static void snippet_schema_add(
303 struct recExtractCtrl *p, Odr_oid *oid)
308 void extract_snippet(ZebraHandle zh, zebra_snippets *sn,
309 struct ZebraRecStream *stream,
310 RecType rt, void *recTypeClientData)
312 struct recExtractCtrl extractCtrl;
313 struct snip_rec_info info;
316 extractCtrl.stream = stream;
317 extractCtrl.first_record = 1;
318 extractCtrl.init = extract_init;
319 extractCtrl.tokenAdd = snippet_token_add;
320 extractCtrl.schemaAdd = snippet_schema_add;
324 extractCtrl.dh = zh->reg->dh;
328 extractCtrl.handle = &info;
329 extractCtrl.match_criteria[0] = '\0';
330 extractCtrl.staticrank = 0;
331 extractCtrl.action = action_insert;
333 init_extractCtrl(zh, &extractCtrl);
335 extractCtrl.setStoreData = 0;
337 r = (*rt->extract)(recTypeClientData, &extractCtrl);
341 static void searchRecordKey(ZebraHandle zh,
342 zebra_rec_keys_t reckeys,
343 const char *index_name,
344 const char **ws, int ws_length)
348 zinfo_index_category_t cat = zinfo_index_category_index;
350 for (i = 0; i<ws_length; i++)
354 ch = zebraExplain_lookup_attr_str(zh->reg->zei, cat, '0', index_name);
356 ch = zebraExplain_lookup_attr_str(zh->reg->zei, cat, 'p', index_name);
358 ch = zebraExplain_lookup_attr_str(zh->reg->zei, cat, 'w', index_name);
363 if (zebra_rec_keys_rewind(reckeys))
370 while (zebra_rec_keys_read(reckeys, &str, &slen, &key))
372 assert(key.len <= IT_KEY_LEVEL_MAX && key.len > 2);
374 seqno = key.mem[key.len-1];
376 if (key.mem[0] == ch)
382 woff = seqno - startSeq;
383 if (woff >= 0 && woff < ws_length)
390 #define FILE_MATCH_BLANK "\t "
392 static char *get_match_from_spec(ZebraHandle zh,
393 zebra_rec_keys_t reckeys,
394 const char *fname, const char *spec)
396 static char dstBuf[2048]; /* static here ??? */
398 const char *s = spec;
402 for (; *s && strchr(FILE_MATCH_BLANK, *s); s++)
409 char attset_str[64], attname_str[64];
413 for (s++; strchr(FILE_MATCH_BLANK, *s); s++)
415 for (i = 0; *s && *s != ',' && *s != ')' &&
416 !strchr(FILE_MATCH_BLANK, *s); s++)
417 if (i+1 < sizeof(attset_str))
418 attset_str[i++] = *s;
419 attset_str[i] = '\0';
421 for (; strchr(FILE_MATCH_BLANK, *s); s++)
424 strcpy(attname_str, attset_str);
427 for (s++; strchr(FILE_MATCH_BLANK, *s); s++)
429 for (i = 0; *s && *s != ')' &&
430 !strchr(FILE_MATCH_BLANK, *s); s++)
431 if (i+1 < sizeof(attname_str))
432 attname_str[i++] = *s;
433 attname_str[i] = '\0';
436 searchRecordKey(zh, reckeys, attname_str, ws, 32);
440 yaz_log(YLOG_WARN, "Missing ) in match criteria %s in group %s",
441 spec, zh->m_group ? zh->m_group : "none");
446 for (i = 0; i<32; i++)
455 dst += strlen(ws[i]);
459 yaz_log(YLOG_WARN, "Record didn't contain match"
460 " fields in (%s,%s)", attset_str, attname_str);
468 const char *spec_src = NULL;
469 const char *s1 = ++s;
470 while (*s1 && !strchr(FILE_MATCH_BLANK, *s1))
474 if (spec_len > sizeof(special)-1)
475 spec_len = sizeof(special)-1;
476 memcpy(special, s, spec_len);
477 special[spec_len] = '\0';
480 if (!strcmp(special, "group"))
481 spec_src = zh->m_group;
482 else if (!strcmp(special, "database"))
483 spec_src = zh->basenames[0];
484 else if (!strcmp(special, "filename")) {
487 else if (!strcmp(special, "type"))
488 spec_src = zh->m_record_type;
493 strcpy(dst, spec_src);
494 dst += strlen(spec_src);
497 else if (*s == '\"' || *s == '\'')
499 int stopMarker = *s++;
503 while (*s && *s != stopMarker)
505 if (i+1 < sizeof(tmpString))
506 tmpString[i++] = *s++;
511 strcpy(dst, tmpString);
512 dst += strlen(tmpString);
516 yaz_log(YLOG_WARN, "Syntax error in match criteria %s in group %s",
517 spec, zh->m_group ? zh->m_group : "none");
524 yaz_log(YLOG_WARN, "No match criteria for record %s in group %s",
525 fname, zh->m_group ? zh->m_group : "none");
532 struct recordLogInfo {
535 struct recordGroup *rGroup;
538 static void all_matches_add(struct recExtractCtrl *ctrl)
541 extract_init(ctrl, &word);
542 word.index_name = "_ALLRECORDS";
543 word.index_type = 'w';
545 extract_add_index_string(&word, zinfo_index_category_alwaysmatches,
549 ZEBRA_RES zebra_extract_records_stream(ZebraHandle zh,
550 struct ZebraRecStream *stream,
551 enum zebra_recctrl_action_t action,
553 const char *recordType,
555 const char *match_criteria,
558 void *recTypeClientData);
561 ZEBRA_RES zebra_extract_file(ZebraHandle zh, zint *sysno, const char *fname,
564 ZEBRA_RES r = ZEBRA_OK;
569 struct file_read_info *fi = 0;
570 const char *original_record_type = 0;
572 void *recTypeClientData;
573 struct ZebraRecStream stream, *streamp;
575 zebra_init_log_level();
577 if (!zh->m_group || !*zh->m_group)
580 sprintf(gprefix, "%s.", zh->m_group);
582 yaz_log(log_level_extract, "zebra_extract_file %s", fname);
584 /* determine file extension */
586 for (i = strlen(fname); --i >= 0; )
589 else if (fname[i] == '.')
591 strcpy(ext, fname+i+1);
594 /* determine file type - depending on extension */
595 original_record_type = zh->m_record_type;
596 if (!zh->m_record_type)
598 sprintf(ext_res, "%srecordType.%s", gprefix, ext);
599 zh->m_record_type = res_get(zh->res, ext_res);
601 if (!zh->m_record_type)
604 if (zh->records_processed + zh->records_skipped
605 < zh->m_file_verbose_limit)
606 yaz_log(YLOG_LOG, "? %s", fname);
607 zh->records_skipped++;
610 /* determine match criteria */
611 if (!zh->m_record_id)
613 sprintf(ext_res, "%srecordId.%s", gprefix, ext);
614 zh->m_record_id = res_get(zh->res, ext_res);
618 recType_byName(zh->reg->recTypes, zh->res, zh->m_record_type,
619 &recTypeClientData)))
621 yaz_log(YLOG_WARN, "No such record type: %s", zh->m_record_type);
625 switch(recType->version)
630 yaz_log(YLOG_WARN, "Bad filter version: %s", zh->m_record_type);
632 if (sysno && deleteFlag)
641 if (zh->path_reg && !yaz_is_abspath(fname))
643 strcpy(full_rep, zh->path_reg);
644 strcat(full_rep, "/");
645 strcat(full_rep, fname);
648 strcpy(full_rep, fname);
650 if ((fd = open(full_rep, O_BINARY|O_RDONLY)) == -1)
652 yaz_log(YLOG_WARN|YLOG_ERRNO, "open %s", full_rep);
653 zh->m_record_type = original_record_type;
657 zebra_create_stream_fd(streamp, fd, 0);
659 r = zebra_extract_records_stream(zh, streamp,
661 action_delete : action_update,
665 0, /*match_criteria */
667 recType, recTypeClientData);
669 stream.destroy(streamp);
670 zh->m_record_type = original_record_type;
675 If sysno is provided, then it's used to identify the reocord.
676 If not, and match_criteria is provided, then sysno is guessed
677 If not, and a record is provided, then sysno is got from there
681 ZEBRA_RES zebra_buffer_extract_record(ZebraHandle zh,
682 const char *buf, size_t buf_size,
683 enum zebra_recctrl_action_t action,
685 const char *recordType,
687 const char *match_criteria,
690 struct ZebraRecStream stream;
695 if (recordType && *recordType)
697 yaz_log(log_level_extract,
698 "Record type explicitly specified: %s", recordType);
699 recType = recType_byName(zh->reg->recTypes, zh->res, recordType,
704 if (!(zh->m_record_type))
706 yaz_log(YLOG_WARN, "No such record type defined");
709 yaz_log(log_level_extract, "Get record type from rgroup: %s",
711 recType = recType_byName(zh->reg->recTypes, zh->res,
712 zh->m_record_type, &clientData);
713 recordType = zh->m_record_type;
718 yaz_log(YLOG_WARN, "No such record type: %s", recordType);
722 zebra_create_stream_mem(&stream, buf, buf_size);
724 res = zebra_extract_records_stream(zh, &stream,
731 recType, clientData);
732 stream.destroy(&stream);
736 ZEBRA_RES zebra_extract_records_stream(ZebraHandle zh,
737 struct ZebraRecStream *stream,
738 enum zebra_recctrl_action_t action,
740 const char *recordType,
742 const char *match_criteria,
745 void *recTypeClientData)
747 ZEBRA_RES res = ZEBRA_OK;
751 res = zebra_extract_record_stream(zh, stream,
758 recType, recTypeClientData, &more);
773 static WRBUF wrbuf_hex_str(const char *cstr)
776 WRBUF w = wrbuf_alloc();
777 for (i = 0; cstr[i]; i++)
779 if (cstr[i] < ' ' || cstr[i] > 126)
780 wrbuf_printf(w, "\\%02X", cstr[i] & 0xff);
782 wrbuf_putc(w, cstr[i]);
787 ZEBRA_RES zebra_extract_record_stream(ZebraHandle zh,
788 struct ZebraRecStream *stream,
789 enum zebra_recctrl_action_t action,
791 const char *recordType,
793 const char *match_criteria,
796 void *recTypeClientData,
801 RecordAttr *recordAttr;
802 struct recExtractCtrl extractCtrl;
804 const char *matchStr = 0;
806 off_t start_offset = 0, end_offset = 0;
807 const char *pr_fname = fname; /* filename to print .. */
808 int show_progress = zh->records_processed + zh->records_skipped
809 < zh->m_file_verbose_limit ? 1:0;
811 zebra_init_log_level();
814 pr_fname = "<no file>"; /* make it printable if file is omitted */
816 zebra_rec_keys_reset(zh->reg->keys);
817 zebra_rec_keys_reset(zh->reg->sortKeys);
819 if (zebraExplain_curDatabase(zh->reg->zei, zh->basenames[0]))
821 if (zebraExplain_newDatabase(zh->reg->zei, zh->basenames[0],
822 zh->m_explain_database))
828 off_t null_offset = 0;
829 extractCtrl.stream = stream;
831 start_offset = stream->tellf(stream);
833 extractCtrl.first_record = start_offset ? 0 : 1;
835 stream->endf(stream, &null_offset);;
837 extractCtrl.init = extract_init;
838 if (zh->reg->index_types)
840 extractCtrl.tokenAdd = extract_token_add2;
844 extractCtrl.tokenAdd = extract_token_add;
846 extractCtrl.schemaAdd = extract_schema_add;
847 extractCtrl.dh = zh->reg->dh;
848 extractCtrl.handle = zh;
849 extractCtrl.match_criteria[0] = '\0';
850 extractCtrl.staticrank = 0;
851 extractCtrl.action = action;
853 init_extractCtrl(zh, &extractCtrl);
855 extract_set_store_data_prepare(&extractCtrl);
857 r = (*recType->extract)(recTypeClientData, &extractCtrl);
859 if (action == action_update)
861 action = extractCtrl.action;
866 case RECCTRL_EXTRACT_EOF:
868 case RECCTRL_EXTRACT_ERROR_GENERIC:
869 /* error occured during extraction ... */
870 yaz_log(YLOG_WARN, "extract error: generic");
872 case RECCTRL_EXTRACT_ERROR_NO_SUCH_FILTER:
873 /* error occured during extraction ... */
874 yaz_log(YLOG_WARN, "extract error: no such filter");
876 case RECCTRL_EXTRACT_SKIP:
878 yaz_log(YLOG_LOG, "skip %s %s " ZINT_FORMAT,
879 recordType, pr_fname, (zint) start_offset);
882 end_offset = stream->endf(stream, 0);
884 stream->seekf(stream, end_offset);
887 case RECCTRL_EXTRACT_OK:
890 yaz_log(YLOG_WARN, "extract error: unknown error: %d", r);
893 end_offset = stream->endf(stream, 0);
895 stream->seekf(stream, end_offset);
897 end_offset = stream->tellf(stream);
899 all_matches_add(&extractCtrl);
901 if (extractCtrl.match_criteria[0])
902 match_criteria = extractCtrl.match_criteria;
910 if (match_criteria && *match_criteria) {
911 matchStr = match_criteria;
913 if (zh->m_record_id && *zh->m_record_id) {
914 matchStr = get_match_from_spec(zh, zh->reg->keys, pr_fname,
918 yaz_log(YLOG_LOG, "error %s %s " ZINT_FORMAT, recordType,
919 pr_fname, (zint) start_offset);
926 int db_ord = zebraExplain_get_database_ord(zh->reg->zei);
927 char *rinfo = dict_lookup_ord(zh->reg->matchDict, db_ord,
931 if (log_level_extract)
933 WRBUF w = wrbuf_hex_str(matchStr);
934 yaz_log(log_level_extract, "matchStr: %s", wrbuf_cstr(w));
939 assert(*rinfo == sizeof(*sysno));
940 memcpy(sysno, rinfo+1, sizeof(*sysno));
944 if (zebra_rec_keys_empty(zh->reg->keys))
946 /* the extraction process returned no information - the record
947 is probably empty - unless flagShowRecords is in use */
955 if (action == action_delete)
957 yaz_log(YLOG_LOG, "delete %s %s " ZINT_FORMAT, recordType,
958 pr_fname, (zint) start_offset);
959 yaz_log(YLOG_WARN, "cannot delete record above (seems new)");
962 else if (action == action_replace)
964 yaz_log(YLOG_LOG, "update %s %s " ZINT_FORMAT, recordType,
965 pr_fname, (zint) start_offset);
966 yaz_log(YLOG_WARN, "cannot update record above (seems new)");
970 yaz_log(YLOG_LOG, "add %s %s " ZINT_FORMAT, recordType, pr_fname,
971 (zint) start_offset);
972 rec = rec_new(zh->reg->records);
976 recordAttr = rec_init_attr(zh->reg->zei, rec);
977 if (extractCtrl.staticrank < 0)
979 yaz_log(YLOG_WARN, "Negative staticrank for record. Set to 0");
980 extractCtrl.staticrank = 0;
985 int db_ord = zebraExplain_get_database_ord(zh->reg->zei);
986 dict_insert_ord(zh->reg->matchDict, db_ord, matchStr,
987 sizeof(*sysno), sysno);
990 extract_flush_sort_keys(zh, *sysno, 1, zh->reg->sortKeys);
992 extract_flush_record_keys2(zh, *sysno,
993 zh->reg->keys, extractCtrl.staticrank,
994 0, recordAttr->staticrank);
996 extract_flush_record_keys(zh, *sysno, 1, zh->reg->keys,
997 extractCtrl.staticrank);
999 recordAttr->staticrank = extractCtrl.staticrank;
1000 zh->records_inserted++;
1004 /* record already exists */
1005 zebra_rec_keys_t delkeys = zebra_rec_keys_open();
1006 zebra_rec_keys_t sortKeys = zebra_rec_keys_open();
1007 if (action == action_insert)
1009 yaz_log(YLOG_LOG, "skipped %s %s " ZINT_FORMAT,
1010 recordType, pr_fname, (zint) start_offset);
1015 rec = rec_get(zh->reg->records, *sysno);
1018 recordAttr = rec_init_attr(zh->reg->zei, rec);
1020 /* decrease total size */
1021 zebraExplain_recordBytesIncrement(zh->reg->zei,
1022 - recordAttr->recordSize);
1024 zebra_rec_keys_set_buf(delkeys,
1025 rec->info[recInfo_delKeys],
1026 rec->size[recInfo_delKeys],
1028 zebra_rec_keys_set_buf(sortKeys,
1029 rec->info[recInfo_sortKeys],
1030 rec->size[recInfo_sortKeys],
1033 extract_flush_sort_keys(zh, *sysno, 0, sortKeys);
1035 extract_flush_record_keys(zh, *sysno, 0, delkeys,
1036 recordAttr->staticrank);
1038 if (action == action_delete)
1040 /* record going to be deleted */
1042 extract_flush_record_keys2(zh, *sysno, 0, recordAttr->staticrank,
1043 delkeys, recordAttr->staticrank);
1045 if (zebra_rec_keys_empty(delkeys))
1047 yaz_log(YLOG_LOG, "delete %s %s " ZINT_FORMAT, recordType,
1048 pr_fname, (zint) start_offset);
1049 yaz_log(YLOG_WARN, "cannot delete file above, "
1050 "storeKeys false (3)");
1055 yaz_log(YLOG_LOG, "delete %s %s " ZINT_FORMAT, recordType,
1056 pr_fname, (zint) start_offset);
1057 zh->records_deleted++;
1060 int db_ord = zebraExplain_get_database_ord(zh->reg->zei);
1061 dict_delete_ord(zh->reg->matchDict, db_ord, matchStr);
1063 rec_del(zh->reg->records, &rec);
1065 zebra_rec_keys_close(delkeys);
1066 zebra_rec_keys_close(sortKeys);
1072 { /* update or special_update */
1074 yaz_log(YLOG_LOG, "update %s %s " ZINT_FORMAT, recordType,
1075 pr_fname, (zint) start_offset);
1076 extract_flush_sort_keys(zh, *sysno, 1, zh->reg->sortKeys);
1079 extract_flush_record_keys2(zh, *sysno,
1080 zh->reg->keys, extractCtrl.staticrank,
1081 delkeys, recordAttr->staticrank);
1083 extract_flush_record_keys(zh, *sysno, 1,
1084 zh->reg->keys, extractCtrl.staticrank);
1086 recordAttr->staticrank = extractCtrl.staticrank;
1087 zh->records_updated++;
1089 zebra_rec_keys_close(delkeys);
1090 zebra_rec_keys_close(sortKeys);
1092 /* update file type */
1093 xfree(rec->info[recInfo_fileType]);
1094 rec->info[recInfo_fileType] =
1095 rec_strdup(recordType, &rec->size[recInfo_fileType]);
1097 /* update filename */
1098 xfree(rec->info[recInfo_filename]);
1099 rec->info[recInfo_filename] =
1100 rec_strdup(fname, &rec->size[recInfo_filename]);
1102 /* update delete keys */
1103 xfree(rec->info[recInfo_delKeys]);
1104 if (!zebra_rec_keys_empty(zh->reg->keys) && zh->m_store_keys == 1)
1106 zebra_rec_keys_get_buf(zh->reg->keys,
1107 &rec->info[recInfo_delKeys],
1108 &rec->size[recInfo_delKeys]);
1112 rec->info[recInfo_delKeys] = NULL;
1113 rec->size[recInfo_delKeys] = 0;
1115 /* update sort keys */
1116 xfree(rec->info[recInfo_sortKeys]);
1118 zebra_rec_keys_get_buf(zh->reg->sortKeys,
1119 &rec->info[recInfo_sortKeys],
1120 &rec->size[recInfo_sortKeys]);
1124 recordAttr->recordSize = end_offset - start_offset;
1125 zebraExplain_recordBytesIncrement(zh->reg->zei,
1126 recordAttr->recordSize);
1129 /* set run-number for this record */
1130 recordAttr->runNumber =
1131 zebraExplain_runNumberIncrement(zh->reg->zei, 0);
1133 /* update store data */
1134 xfree(rec->info[recInfo_storeData]);
1136 /* update store data */
1137 if (zh->store_data_buf)
1139 rec->size[recInfo_storeData] = zh->store_data_size;
1140 rec->info[recInfo_storeData] = zh->store_data_buf;
1141 zh->store_data_buf = 0;
1142 recordAttr->recordSize = zh->store_data_size;
1144 else if (zh->m_store_data)
1146 off_t cur_offset = stream->tellf(stream);
1148 rec->size[recInfo_storeData] = recordAttr->recordSize;
1149 rec->info[recInfo_storeData] = (char *)
1150 xmalloc(recordAttr->recordSize);
1151 stream->seekf(stream, start_offset);
1152 stream->readf(stream, rec->info[recInfo_storeData],
1153 recordAttr->recordSize);
1154 stream->seekf(stream, cur_offset);
1158 rec->info[recInfo_storeData] = NULL;
1159 rec->size[recInfo_storeData] = 0;
1161 /* update database name */
1162 xfree(rec->info[recInfo_databaseName]);
1163 rec->info[recInfo_databaseName] =
1164 rec_strdup(zh->basenames[0], &rec->size[recInfo_databaseName]);
1167 recordAttr->recordOffset = start_offset;
1169 /* commit this record */
1170 rec_put(zh->reg->records, &rec);
1175 ZEBRA_RES zebra_extract_explain(void *handle, Record rec, data1_node *n)
1177 ZebraHandle zh = (ZebraHandle) handle;
1178 struct recExtractCtrl extractCtrl;
1180 if (zebraExplain_curDatabase(zh->reg->zei,
1181 rec->info[recInfo_databaseName]))
1184 if (zebraExplain_newDatabase(zh->reg->zei,
1185 rec->info[recInfo_databaseName], 0))
1189 zebra_rec_keys_reset(zh->reg->keys);
1190 zebra_rec_keys_reset(zh->reg->sortKeys);
1192 extractCtrl.init = extract_init;
1193 extractCtrl.tokenAdd = extract_token_add;
1194 extractCtrl.schemaAdd = extract_schema_add;
1195 extractCtrl.dh = zh->reg->dh;
1197 init_extractCtrl(zh, &extractCtrl);
1199 extractCtrl.flagShowRecords = 0;
1200 extractCtrl.match_criteria[0] = '\0';
1201 extractCtrl.staticrank = 0;
1202 extractCtrl.action = action_update;
1204 extractCtrl.handle = handle;
1205 extractCtrl.first_record = 1;
1207 extract_set_store_data_prepare(&extractCtrl);
1210 grs_extract_tree(&extractCtrl, n);
1212 if (rec->size[recInfo_delKeys])
1214 zebra_rec_keys_t delkeys = zebra_rec_keys_open();
1216 zebra_rec_keys_t sortkeys = zebra_rec_keys_open();
1218 zebra_rec_keys_set_buf(delkeys, rec->info[recInfo_delKeys],
1219 rec->size[recInfo_delKeys],
1222 extract_flush_record_keys2(zh, rec->sysno,
1223 zh->reg->keys, 0, delkeys, 0);
1225 extract_flush_record_keys(zh, rec->sysno, 0, delkeys, 0);
1226 extract_flush_record_keys(zh, rec->sysno, 1, zh->reg->keys, 0);
1228 zebra_rec_keys_close(delkeys);
1230 zebra_rec_keys_set_buf(sortkeys, rec->info[recInfo_sortKeys],
1231 rec->size[recInfo_sortKeys],
1234 extract_flush_sort_keys(zh, rec->sysno, 0, sortkeys);
1235 zebra_rec_keys_close(sortkeys);
1240 extract_flush_record_keys2(zh, rec->sysno, zh->reg->keys, 0, 0, 0);
1242 extract_flush_record_keys(zh, rec->sysno, 1, zh->reg->keys, 0);
1245 extract_flush_sort_keys(zh, rec->sysno, 1, zh->reg->sortKeys);
1247 xfree(rec->info[recInfo_delKeys]);
1248 zebra_rec_keys_get_buf(zh->reg->keys,
1249 &rec->info[recInfo_delKeys],
1250 &rec->size[recInfo_delKeys]);
1252 xfree(rec->info[recInfo_sortKeys]);
1253 zebra_rec_keys_get_buf(zh->reg->sortKeys,
1254 &rec->info[recInfo_sortKeys],
1255 &rec->size[recInfo_sortKeys]);
1259 void extract_rec_keys_log(ZebraHandle zh, int is_insert,
1260 zebra_rec_keys_t reckeys,
1263 if (zebra_rec_keys_rewind(reckeys))
1268 NMEM nmem = nmem_create();
1270 while(zebra_rec_keys_read(reckeys, &str, &slen, &key))
1272 char keystr[200]; /* room for zints to print */
1274 int ord = CAST_ZINT_TO_INT(key.mem[0]);
1276 const char *string_index;
1278 zebraExplain_lookup_ord(zh->reg->zei, ord, &index_type,
1279 0/* db */, &string_index);
1281 zebra_term_untrans_iconv(zh, nmem, index_type,
1284 for (i = 0; i<key.len; i++)
1286 sprintf(keystr + strlen(keystr), ZINT_FORMAT " ", key.mem[i]);
1289 if (*str < CHR_BASE_CHAR)
1292 char dst_buf[200]; /* room for special chars */
1294 strcpy(dst_buf , "?");
1296 if (!strcmp(str, ""))
1297 strcpy(dst_buf, "alwaysmatches");
1298 if (!strcmp(str, FIRST_IN_FIELD_STR))
1299 strcpy(dst_buf, "firstinfield");
1300 else if (!strcmp(str, CHR_UNKNOWN))
1301 strcpy(dst_buf, "unknown");
1302 else if (!strcmp(str, CHR_SPACE))
1303 strcpy(dst_buf, "space");
1305 for (i = 0; i<slen; i++)
1307 sprintf(dst_buf + strlen(dst_buf), " %d", str[i] & 0xff);
1309 yaz_log(level, "%s%c %s %s", keystr, index_type,
1310 string_index, dst_buf);
1314 yaz_log(level, "%s%c %s \"%s\"", keystr, index_type,
1315 string_index, dst_term);
1323 void extract_rec_keys_adjust(ZebraHandle zh, int is_insert,
1324 zebra_rec_keys_t reckeys)
1326 ZebraExplainInfo zei = zh->reg->zei;
1330 struct ord_stat *next;
1333 if (zebra_rec_keys_rewind(reckeys))
1335 struct ord_stat *ord_list = 0;
1339 struct it_key key_in;
1340 while(zebra_rec_keys_read(reckeys, &str, &slen, &key_in))
1342 int ord = CAST_ZINT_TO_INT(key_in.mem[0]);
1344 for (p = ord_list; p ; p = p->next)
1352 p = xmalloc(sizeof(*p));
1363 struct ord_stat *p1 = p;
1366 zebraExplain_ord_adjust_occurrences(zei, p->ord, p->no, 1);
1368 zebraExplain_ord_adjust_occurrences(zei, p->ord, - p->no, -1);
1375 void extract_flush_record_keys2(ZebraHandle zh, zint sysno,
1376 zebra_rec_keys_t ins_keys, zint ins_rank,
1377 zebra_rec_keys_t del_keys, zint del_rank)
1379 ZebraExplainInfo zei = zh->reg->zei;
1383 if (!zh->reg->key_block)
1385 int mem = 1024*1024 * atoi( res_get_def( zh->res, "memmax", "8"));
1386 const char *key_tmp_dir = res_get_def(zh->res, "keyTmpDir", ".");
1387 int use_threads = atoi(res_get_def(zh->res, "threads", "1"));
1388 zh->reg->key_block = key_block_create(mem, key_tmp_dir, use_threads);
1393 extract_rec_keys_adjust(zh, 1, ins_keys);
1395 zebraExplain_recordCountIncrement(zei, 1);
1396 zebra_rec_keys_rewind(ins_keys);
1400 extract_rec_keys_adjust(zh, 0, del_keys);
1402 zebraExplain_recordCountIncrement(zei, -1);
1403 zebra_rec_keys_rewind(del_keys);
1409 const char *del_str;
1410 struct it_key del_key_in;
1414 const char *ins_str;
1415 struct it_key ins_key_in;
1419 del = zebra_rec_keys_read(del_keys, &del_str, &del_slen,
1422 ins = zebra_rec_keys_read(ins_keys, &ins_str, &ins_slen,
1425 if (del && ins && ins_rank == del_rank
1426 && !key_compare(&del_key_in, &ins_key_in)
1427 && ins_slen == del_slen && !memcmp(del_str, ins_str, del_slen))
1437 key_block_write(zh->reg->key_block, sysno,
1438 &del_key_in, 0, del_str, del_slen,
1439 del_rank, zh->m_staticrank);
1441 key_block_write(zh->reg->key_block, sysno,
1442 &ins_key_in, 1, ins_str, ins_slen,
1443 ins_rank, zh->m_staticrank);
1445 yaz_log(log_level_extract, "normal=%d optimized=%d", normal, optimized);
1448 void extract_flush_record_keys(ZebraHandle zh, zint sysno, int cmd,
1449 zebra_rec_keys_t reckeys,
1452 ZebraExplainInfo zei = zh->reg->zei;
1454 extract_rec_keys_adjust(zh, cmd, reckeys);
1456 if (log_level_details)
1458 yaz_log(log_level_details, "Keys for record " ZINT_FORMAT " %s",
1459 sysno, cmd ? "insert" : "delete");
1460 extract_rec_keys_log(zh, cmd, reckeys, log_level_details);
1463 if (!zh->reg->key_block)
1465 int mem = 1024*1024 * atoi( res_get_def( zh->res, "memmax", "8"));
1466 const char *key_tmp_dir = res_get_def(zh->res, "keyTmpDir", ".");
1467 int use_threads = atoi(res_get_def(zh->res, "threads", "1"));
1468 zh->reg->key_block = key_block_create(mem, key_tmp_dir, use_threads);
1470 zebraExplain_recordCountIncrement(zei, cmd ? 1 : -1);
1473 yaz_log(YLOG_LOG, "sysno=" ZINT_FORMAT " cmd=%d", sysno, cmd);
1474 print_rec_keys(zh, reckeys);
1476 if (zebra_rec_keys_rewind(reckeys))
1480 struct it_key key_in;
1481 while(zebra_rec_keys_read(reckeys, &str, &slen, &key_in))
1483 key_block_write(zh->reg->key_block, sysno,
1484 &key_in, cmd, str, slen,
1485 staticrank, zh->m_staticrank);
1490 ZEBRA_RES zebra_rec_keys_to_snippets(ZebraHandle zh,
1491 zebra_rec_keys_t reckeys,
1492 zebra_snippets *snippets)
1494 NMEM nmem = nmem_create();
1495 if (zebra_rec_keys_rewind(reckeys))
1500 while (zebra_rec_keys_read(reckeys, &str, &slen, &key))
1507 assert(key.len <= IT_KEY_LEVEL_MAX && key.len > 2);
1508 seqno = key.mem[key.len-1];
1509 ord = CAST_ZINT_TO_INT(key.mem[0]);
1511 zebraExplain_lookup_ord(zh->reg->zei, ord, &index_type,
1512 0/* db */, 0 /* string_index */);
1514 zebra_term_untrans_iconv(zh, nmem, index_type,
1516 zebra_snippets_append(snippets, seqno, 0, ord, dst_term);
1524 void print_rec_keys(ZebraHandle zh, zebra_rec_keys_t reckeys)
1526 yaz_log(YLOG_LOG, "print_rec_keys");
1527 if (zebra_rec_keys_rewind(reckeys))
1532 while (zebra_rec_keys_read(reckeys, &str, &slen, &key))
1534 char dst_buf[IT_MAX_WORD];
1537 int ord = CAST_ZINT_TO_INT(key.mem[0]);
1539 assert(key.len <= IT_KEY_LEVEL_MAX && key.len > 2);
1541 zebraExplain_lookup_ord(zh->reg->zei, ord, &index_type, &db, 0);
1543 seqno = key.mem[key.len-1];
1545 zebra_term_untrans(zh, index_type, dst_buf, str);
1547 yaz_log(YLOG_LOG, "ord=%d seqno=" ZINT_FORMAT
1548 " term=%s", ord, seqno, dst_buf);
1553 static void extract_add_index_string(RecWord *p, zinfo_index_category_t cat,
1554 const char *str, int length)
1557 ZebraHandle zh = p->extractCtrl->handle;
1558 ZebraExplainInfo zei = zh->reg->zei;
1561 ch = zebraExplain_lookup_attr_str(zei, cat, p->index_type, p->index_name);
1563 ch = zebraExplain_add_attr_str(zei, cat, p->index_type, p->index_name);
1567 key.mem[i++] = p->record_id;
1568 key.mem[i++] = p->section_id;
1570 if (zh->m_segment_indexing)
1571 key.mem[i++] = p->segment;
1572 key.mem[i++] = p->seqno;
1575 zebra_rec_keys_write(zh->reg->keys, str, length, &key);
1578 static void extract_add_sort_string(RecWord *p, const char *str, int length)
1581 ZebraHandle zh = p->extractCtrl->handle;
1582 ZebraExplainInfo zei = zh->reg->zei;
1584 zinfo_index_category_t cat = zinfo_index_category_sort;
1586 ch = zebraExplain_lookup_attr_str(zei, cat, p->index_type, p->index_name);
1588 ch = zebraExplain_add_attr_str(zei, cat, p->index_type, p->index_name);
1591 key.mem[1] = p->record_id;
1593 zebra_rec_keys_write(zh->reg->sortKeys, str, length, &key);
1596 static void extract_add_staticrank_string(RecWord *p,
1597 const char *str, int length)
1600 struct recExtractCtrl *ctrl = p->extractCtrl;
1602 if (length > sizeof(valz)-1)
1603 length = sizeof(valz)-1;
1605 memcpy(valz, str, length);
1606 valz[length] = '\0';
1607 ctrl->staticrank = atozint(valz);
1610 static void extract_add_string(RecWord *p, const char *string, int length)
1612 ZebraHandle zh = p->extractCtrl->handle;
1618 if (zebra_maps_is_index(zh->reg->zebra_maps, p->index_type))
1620 extract_add_index_string(p, zinfo_index_category_index,
1622 if (zebra_maps_is_alwaysmatches(zh->reg->zebra_maps, p->index_type))
1625 memcpy(&word, p, sizeof(word));
1628 extract_add_index_string(
1629 &word, zinfo_index_category_alwaysmatches, "", 0);
1632 else if (zebra_maps_is_sort(zh->reg->zebra_maps, p->index_type))
1634 extract_add_sort_string(p, string, length);
1636 else if (zebra_maps_is_staticrank(zh->reg->zebra_maps, p->index_type))
1638 extract_add_staticrank_string(p, string, length);
1642 static void extract_add_incomplete_field(RecWord *p)
1644 ZebraHandle zh = p->extractCtrl->handle;
1645 const char *b = p->term_buf;
1646 int remain = p->term_len;
1648 const char **map = 0;
1651 map = zebra_maps_input(zh->reg->zebra_maps, p->index_type, &b, remain, 0);
1655 char buf[IT_MAX_WORD+1];
1659 while (map && *map && **map == *CHR_SPACE)
1661 remain = p->term_len - (b - p->term_buf);
1663 map = zebra_maps_input(zh->reg->zebra_maps, p->index_type, &b,
1671 while (map && *map && **map != *CHR_SPACE)
1673 const char *cp = *map;
1675 while (i < IT_MAX_WORD && *cp)
1677 remain = p->term_len - (b - p->term_buf);
1679 map = zebra_maps_input(zh->reg->zebra_maps, p->index_type, &b, remain, 0);
1689 if (zebra_maps_is_first_in_field(zh->reg->zebra_maps, p->index_type))
1691 /* first in field marker */
1692 extract_add_string(p, FIRST_IN_FIELD_STR, FIRST_IN_FIELD_LEN);
1696 extract_add_string(p, buf, i);
1701 static void extract_add_complete_field(RecWord *p)
1703 ZebraHandle zh = p->extractCtrl->handle;
1704 const char *b = p->term_buf;
1705 char buf[IT_MAX_WORD+1];
1706 const char **map = 0;
1707 int i = 0, remain = p->term_len;
1710 map = zebra_maps_input(zh->reg->zebra_maps, p->index_type, &b, remain, 1);
1712 while (remain > 0 && i < IT_MAX_WORD)
1714 while (map && *map && **map == *CHR_SPACE)
1716 remain = p->term_len - (b - p->term_buf);
1720 int first = i ? 0 : 1; /* first position */
1721 map = zebra_maps_input(zh->reg->zebra_maps, p->index_type, &b, remain, first);
1729 if (i && i < IT_MAX_WORD)
1730 buf[i++] = *CHR_SPACE;
1731 while (map && *map && **map != *CHR_SPACE)
1733 const char *cp = *map;
1735 if (**map == *CHR_CUT)
1741 if (i >= IT_MAX_WORD)
1743 while (i < IT_MAX_WORD && *cp)
1746 remain = p->term_len - (b - p->term_buf);
1749 map = zebra_maps_input(zh->reg->zebra_maps, p->index_type, &b,
1758 extract_add_string(p, buf, i);
1761 static void extract_token_add2_index(ZebraHandle zh, zebra_index_type_t type,
1765 const char *res_buf = 0;
1767 int r = zebra_index_type_tokenize(type, p->term_buf, p->term_len,
1768 &res_buf, &res_len);
1769 int cat = zinfo_index_category_index;
1770 int ch = zebraExplain_lookup_attr_str(zh->reg->zei, cat, p->index_type, p->index_name);
1772 ch = zebraExplain_add_attr_str(zh->reg->zei, cat, p->index_type, p->index_name);
1777 key.mem[i++] = p->record_id;
1778 key.mem[i++] = p->section_id;
1780 if (zh->m_segment_indexing)
1781 key.mem[i++] = p->segment;
1782 key.mem[i++] = p->seqno;
1785 yaz_log(YLOG_LOG, "keys_write %.*s", (int) res_len, res_buf);
1786 zebra_rec_keys_write(zh->reg->keys, res_buf, res_len, &key);
1789 r = zebra_index_type_tokenize(type, 0, 0, &res_buf, &res_len);
1793 static void extract_token_add2(RecWord *p)
1795 zebra_index_type_t type;
1796 ZebraHandle zh = p->extractCtrl->handle;
1798 type_tmp[0] = p->index_type;
1800 type = zebra_index_type_get(zh->reg->index_types, type_tmp);
1803 if (zebra_index_type_is_index(type))
1805 extract_token_add2_index(zh, type, p);
1807 else if (zebra_index_type_is_sort(type))
1815 /** \brief top-level indexing handler for recctrl system
1816 \param p token data to be indexed
1820 zebra_add_{in}_complete
1823 extract_add_index_string
1825 extract_add_sort_string
1827 extract_add_staticrank_string
1830 static void extract_token_add(RecWord *p)
1832 ZebraHandle zh = p->extractCtrl->handle;
1835 if (log_level_details)
1837 yaz_log(log_level_details, "extract_token_add "
1838 "type=%c index=%s seqno=" ZINT_FORMAT " s=%.*s",
1839 p->index_type, p->index_name,
1840 p->seqno, p->term_len, p->term_buf);
1842 if ((wrbuf = zebra_replace(zh->reg->zebra_maps, p->index_type, 0,
1843 p->term_buf, p->term_len)))
1845 p->term_buf = wrbuf_buf(wrbuf);
1846 p->term_len = wrbuf_len(wrbuf);
1848 if (zebra_maps_is_complete(zh->reg->zebra_maps, p->index_type))
1849 extract_add_complete_field(p);
1851 extract_add_incomplete_field(p);
1854 static void extract_set_store_data_cb(struct recExtractCtrl *p,
1855 void *buf, size_t sz)
1857 ZebraHandle zh = (ZebraHandle) p->handle;
1859 xfree(zh->store_data_buf);
1860 zh->store_data_buf = 0;
1861 zh->store_data_size = 0;
1864 zh->store_data_buf = xmalloc(sz);
1865 zh->store_data_size = sz;
1866 memcpy(zh->store_data_buf, buf, sz);
1870 static void extract_set_store_data_prepare(struct recExtractCtrl *p)
1872 ZebraHandle zh = (ZebraHandle) p->handle;
1873 xfree(zh->store_data_buf);
1874 zh->store_data_buf = 0;
1875 zh->store_data_size = 0;
1876 p->setStoreData = extract_set_store_data_cb;
1879 static void extract_schema_add(struct recExtractCtrl *p, Odr_oid *oid)
1881 ZebraHandle zh = (ZebraHandle) p->handle;
1882 zebraExplain_addSchema(zh->reg->zei, oid);
1885 void extract_flush_sort_keys(ZebraHandle zh, zint sysno,
1886 int cmd, zebra_rec_keys_t reckeys)
1889 yaz_log(YLOG_LOG, "extract_flush_sort_keys cmd=%d sysno=" ZINT_FORMAT,
1891 extract_rec_keys_log(zh, cmd, reckeys, YLOG_LOG);
1894 if (zebra_rec_keys_rewind(reckeys))
1896 zebra_sort_index_t si = zh->reg->sort_index;
1899 struct it_key key_in;
1901 zebra_sort_sysno(si, sysno);
1903 while (zebra_rec_keys_read(reckeys, &str, &slen, &key_in))
1905 int ord = CAST_ZINT_TO_INT(key_in.mem[0]);
1907 zebra_sort_type(si, ord);
1909 zebra_sort_add(si, str, slen);
1911 zebra_sort_delete(si);
1919 * indent-tabs-mode: nil
1921 * vim: shiftwidth=4 tabstop=8 expandtab