1 /* $Id: extract.c,v 1.265 2007-10-29 16:57:52 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]);
1275 const char *index_type;
1277 const char *string_index;
1279 zebraExplain_lookup_ord(zh->reg->zei, ord, &index_type,
1280 0/* db */, &string_index);
1282 zebra_term_untrans_iconv(zh, nmem, *index_type,
1285 for (i = 0; i<key.len; i++)
1287 sprintf(keystr + strlen(keystr), ZINT_FORMAT " ", key.mem[i]);
1290 if (*str < CHR_BASE_CHAR)
1293 char dst_buf[200]; /* room for special chars */
1295 strcpy(dst_buf , "?");
1297 if (!strcmp(str, ""))
1298 strcpy(dst_buf, "alwaysmatches");
1299 if (!strcmp(str, FIRST_IN_FIELD_STR))
1300 strcpy(dst_buf, "firstinfield");
1301 else if (!strcmp(str, CHR_UNKNOWN))
1302 strcpy(dst_buf, "unknown");
1303 else if (!strcmp(str, CHR_SPACE))
1304 strcpy(dst_buf, "space");
1306 for (i = 0; i<slen; i++)
1308 sprintf(dst_buf + strlen(dst_buf), " %d", str[i] & 0xff);
1310 yaz_log(level, "%s%s %s %s", keystr, index_type,
1311 string_index, dst_buf);
1315 yaz_log(level, "%s%s %s \"%s\"", keystr, index_type,
1316 string_index, dst_term);
1324 void extract_rec_keys_adjust(ZebraHandle zh, int is_insert,
1325 zebra_rec_keys_t reckeys)
1327 ZebraExplainInfo zei = zh->reg->zei;
1331 struct ord_stat *next;
1334 if (zebra_rec_keys_rewind(reckeys))
1336 struct ord_stat *ord_list = 0;
1340 struct it_key key_in;
1341 while(zebra_rec_keys_read(reckeys, &str, &slen, &key_in))
1343 int ord = CAST_ZINT_TO_INT(key_in.mem[0]);
1345 for (p = ord_list; p ; p = p->next)
1353 p = xmalloc(sizeof(*p));
1364 struct ord_stat *p1 = p;
1367 zebraExplain_ord_adjust_occurrences(zei, p->ord, p->no, 1);
1369 zebraExplain_ord_adjust_occurrences(zei, p->ord, - p->no, -1);
1376 void extract_flush_record_keys2(ZebraHandle zh, zint sysno,
1377 zebra_rec_keys_t ins_keys, zint ins_rank,
1378 zebra_rec_keys_t del_keys, zint del_rank)
1380 ZebraExplainInfo zei = zh->reg->zei;
1384 if (!zh->reg->key_block)
1386 int mem = 1024*1024 * atoi( res_get_def( zh->res, "memmax", "8"));
1387 const char *key_tmp_dir = res_get_def(zh->res, "keyTmpDir", ".");
1388 int use_threads = atoi(res_get_def(zh->res, "threads", "1"));
1389 zh->reg->key_block = key_block_create(mem, key_tmp_dir, use_threads);
1394 extract_rec_keys_adjust(zh, 1, ins_keys);
1396 zebraExplain_recordCountIncrement(zei, 1);
1397 zebra_rec_keys_rewind(ins_keys);
1401 extract_rec_keys_adjust(zh, 0, del_keys);
1403 zebraExplain_recordCountIncrement(zei, -1);
1404 zebra_rec_keys_rewind(del_keys);
1410 const char *del_str;
1411 struct it_key del_key_in;
1415 const char *ins_str;
1416 struct it_key ins_key_in;
1420 del = zebra_rec_keys_read(del_keys, &del_str, &del_slen,
1423 ins = zebra_rec_keys_read(ins_keys, &ins_str, &ins_slen,
1426 if (del && ins && ins_rank == del_rank
1427 && !key_compare(&del_key_in, &ins_key_in)
1428 && ins_slen == del_slen && !memcmp(del_str, ins_str, del_slen))
1438 key_block_write(zh->reg->key_block, sysno,
1439 &del_key_in, 0, del_str, del_slen,
1440 del_rank, zh->m_staticrank);
1442 key_block_write(zh->reg->key_block, sysno,
1443 &ins_key_in, 1, ins_str, ins_slen,
1444 ins_rank, zh->m_staticrank);
1446 yaz_log(log_level_extract, "normal=%d optimized=%d", normal, optimized);
1449 void extract_flush_record_keys(ZebraHandle zh, zint sysno, int cmd,
1450 zebra_rec_keys_t reckeys,
1453 ZebraExplainInfo zei = zh->reg->zei;
1455 extract_rec_keys_adjust(zh, cmd, reckeys);
1457 if (log_level_details)
1459 yaz_log(log_level_details, "Keys for record " ZINT_FORMAT " %s",
1460 sysno, cmd ? "insert" : "delete");
1461 extract_rec_keys_log(zh, cmd, reckeys, log_level_details);
1464 if (!zh->reg->key_block)
1466 int mem = 1024*1024 * atoi( res_get_def( zh->res, "memmax", "8"));
1467 const char *key_tmp_dir = res_get_def(zh->res, "keyTmpDir", ".");
1468 int use_threads = atoi(res_get_def(zh->res, "threads", "1"));
1469 zh->reg->key_block = key_block_create(mem, key_tmp_dir, use_threads);
1471 zebraExplain_recordCountIncrement(zei, cmd ? 1 : -1);
1474 yaz_log(YLOG_LOG, "sysno=" ZINT_FORMAT " cmd=%d", sysno, cmd);
1475 print_rec_keys(zh, reckeys);
1477 if (zebra_rec_keys_rewind(reckeys))
1481 struct it_key key_in;
1482 while(zebra_rec_keys_read(reckeys, &str, &slen, &key_in))
1484 key_block_write(zh->reg->key_block, sysno,
1485 &key_in, cmd, str, slen,
1486 staticrank, zh->m_staticrank);
1491 ZEBRA_RES zebra_rec_keys_to_snippets(ZebraHandle zh,
1492 zebra_rec_keys_t reckeys,
1493 zebra_snippets *snippets)
1495 NMEM nmem = nmem_create();
1496 if (zebra_rec_keys_rewind(reckeys))
1501 while (zebra_rec_keys_read(reckeys, &str, &slen, &key))
1506 const char *index_type;
1508 assert(key.len <= IT_KEY_LEVEL_MAX && key.len > 2);
1509 seqno = key.mem[key.len-1];
1510 ord = CAST_ZINT_TO_INT(key.mem[0]);
1512 zebraExplain_lookup_ord(zh->reg->zei, ord, &index_type,
1513 0/* db */, 0 /* string_index */);
1515 zebra_term_untrans_iconv(zh, nmem, *index_type,
1517 zebra_snippets_append(snippets, seqno, 0, ord, dst_term);
1525 void print_rec_keys(ZebraHandle zh, zebra_rec_keys_t reckeys)
1527 yaz_log(YLOG_LOG, "print_rec_keys");
1528 if (zebra_rec_keys_rewind(reckeys))
1533 while (zebra_rec_keys_read(reckeys, &str, &slen, &key))
1535 char dst_buf[IT_MAX_WORD];
1537 const char *index_type;
1538 int ord = CAST_ZINT_TO_INT(key.mem[0]);
1540 assert(key.len <= IT_KEY_LEVEL_MAX && key.len > 2);
1542 zebraExplain_lookup_ord(zh->reg->zei, ord, &index_type, &db, 0);
1544 seqno = key.mem[key.len-1];
1546 zebra_term_untrans(zh, *index_type, dst_buf, str);
1548 yaz_log(YLOG_LOG, "ord=%d seqno=" ZINT_FORMAT
1549 " term=%s", ord, seqno, dst_buf);
1554 static void extract_add_index_string(RecWord *p, zinfo_index_category_t cat,
1555 const char *str, int length)
1558 ZebraHandle zh = p->extractCtrl->handle;
1559 ZebraExplainInfo zei = zh->reg->zei;
1562 ch = zebraExplain_lookup_attr_str(zei, cat, p->index_type, p->index_name);
1564 ch = zebraExplain_add_attr_str(zei, cat, p->index_type, p->index_name);
1568 key.mem[i++] = p->record_id;
1569 key.mem[i++] = p->section_id;
1571 if (zh->m_segment_indexing)
1572 key.mem[i++] = p->segment;
1573 key.mem[i++] = p->seqno;
1576 zebra_rec_keys_write(zh->reg->keys, str, length, &key);
1579 static void extract_add_sort_string(RecWord *p, const char *str, int length)
1582 ZebraHandle zh = p->extractCtrl->handle;
1583 ZebraExplainInfo zei = zh->reg->zei;
1585 zinfo_index_category_t cat = zinfo_index_category_sort;
1587 ch = zebraExplain_lookup_attr_str(zei, cat, p->index_type, p->index_name);
1589 ch = zebraExplain_add_attr_str(zei, cat, p->index_type, p->index_name);
1592 key.mem[1] = p->record_id;
1594 zebra_rec_keys_write(zh->reg->sortKeys, str, length, &key);
1597 static void extract_add_staticrank_string(RecWord *p,
1598 const char *str, int length)
1601 struct recExtractCtrl *ctrl = p->extractCtrl;
1603 if (length > sizeof(valz)-1)
1604 length = sizeof(valz)-1;
1606 memcpy(valz, str, length);
1607 valz[length] = '\0';
1608 ctrl->staticrank = atozint(valz);
1611 static void extract_add_string(RecWord *p, const char *string, int length)
1613 ZebraHandle zh = p->extractCtrl->handle;
1619 if (zebra_maps_is_index(zh->reg->zebra_maps, *p->index_type))
1621 extract_add_index_string(p, zinfo_index_category_index,
1623 if (zebra_maps_is_alwaysmatches(zh->reg->zebra_maps, *p->index_type))
1626 memcpy(&word, p, sizeof(word));
1629 extract_add_index_string(
1630 &word, zinfo_index_category_alwaysmatches, "", 0);
1633 else if (zebra_maps_is_sort(zh->reg->zebra_maps, *p->index_type))
1635 extract_add_sort_string(p, string, length);
1637 else if (zebra_maps_is_staticrank(zh->reg->zebra_maps, *p->index_type))
1639 extract_add_staticrank_string(p, string, length);
1643 static void extract_add_incomplete_field(RecWord *p)
1645 ZebraHandle zh = p->extractCtrl->handle;
1646 const char *b = p->term_buf;
1647 int remain = p->term_len;
1649 const char **map = 0;
1652 map = zebra_maps_input(zh->reg->zebra_maps, *p->index_type, &b, remain, 0);
1656 char buf[IT_MAX_WORD+1];
1660 while (map && *map && **map == *CHR_SPACE)
1662 remain = p->term_len - (b - p->term_buf);
1664 map = zebra_maps_input(zh->reg->zebra_maps, *p->index_type, &b,
1672 while (map && *map && **map != *CHR_SPACE)
1674 const char *cp = *map;
1676 while (i < IT_MAX_WORD && *cp)
1678 remain = p->term_len - (b - p->term_buf);
1680 map = zebra_maps_input(zh->reg->zebra_maps, *p->index_type, &b, remain, 0);
1690 if (zebra_maps_is_first_in_field(zh->reg->zebra_maps, *p->index_type))
1692 /* first in field marker */
1693 extract_add_string(p, FIRST_IN_FIELD_STR, FIRST_IN_FIELD_LEN);
1697 extract_add_string(p, buf, i);
1702 static void extract_add_complete_field(RecWord *p)
1704 ZebraHandle zh = p->extractCtrl->handle;
1705 const char *b = p->term_buf;
1706 char buf[IT_MAX_WORD+1];
1707 const char **map = 0;
1708 int i = 0, remain = p->term_len;
1711 map = zebra_maps_input(zh->reg->zebra_maps, *p->index_type, &b, remain, 1);
1713 while (remain > 0 && i < IT_MAX_WORD)
1715 while (map && *map && **map == *CHR_SPACE)
1717 remain = p->term_len - (b - p->term_buf);
1721 int first = i ? 0 : 1; /* first position */
1722 map = zebra_maps_input(zh->reg->zebra_maps, *p->index_type, &b, remain, first);
1730 if (i && i < IT_MAX_WORD)
1731 buf[i++] = *CHR_SPACE;
1732 while (map && *map && **map != *CHR_SPACE)
1734 const char *cp = *map;
1736 if (**map == *CHR_CUT)
1742 if (i >= IT_MAX_WORD)
1744 while (i < IT_MAX_WORD && *cp)
1747 remain = p->term_len - (b - p->term_buf);
1750 map = zebra_maps_input(zh->reg->zebra_maps, *p->index_type, &b,
1759 extract_add_string(p, buf, i);
1762 static void extract_token_add2_index(ZebraHandle zh, zebra_index_type_t type,
1766 const char *res_buf = 0;
1768 int r = zebra_index_type_tokenize(type, p->term_buf, p->term_len,
1769 &res_buf, &res_len);
1770 int cat = zinfo_index_category_index;
1771 int ch = zebraExplain_lookup_attr_str(zh->reg->zei, cat, p->index_type, p->index_name);
1773 ch = zebraExplain_add_attr_str(zh->reg->zei, cat, p->index_type, p->index_name);
1778 key.mem[i++] = p->record_id;
1779 key.mem[i++] = p->section_id;
1781 if (zh->m_segment_indexing)
1782 key.mem[i++] = p->segment;
1783 key.mem[i++] = p->seqno;
1786 yaz_log(YLOG_LOG, "keys_write %.*s", (int) res_len, res_buf);
1787 zebra_rec_keys_write(zh->reg->keys, res_buf, res_len, &key);
1790 r = zebra_index_type_tokenize(type, 0, 0, &res_buf, &res_len);
1794 static void extract_token_add2(RecWord *p)
1796 ZebraHandle zh = p->extractCtrl->handle;
1797 zebra_index_type_t type = zebra_index_type_get(zh->reg->index_types, p->index_type);
1800 if (zebra_index_type_is_index(type))
1802 extract_token_add2_index(zh, type, p);
1804 else if (zebra_index_type_is_sort(type))
1812 /** \brief top-level indexing handler for recctrl system
1813 \param p token data to be indexed
1817 zebra_add_{in}_complete
1820 extract_add_index_string
1822 extract_add_sort_string
1824 extract_add_staticrank_string
1827 static void extract_token_add(RecWord *p)
1829 ZebraHandle zh = p->extractCtrl->handle;
1832 if (log_level_details)
1834 yaz_log(log_level_details, "extract_token_add "
1835 "type=%s index=%s seqno=" ZINT_FORMAT " s=%.*s",
1836 p->index_type, p->index_name,
1837 p->seqno, p->term_len, p->term_buf);
1839 if ((wrbuf = zebra_replace(zh->reg->zebra_maps, *p->index_type, 0,
1840 p->term_buf, p->term_len)))
1842 p->term_buf = wrbuf_buf(wrbuf);
1843 p->term_len = wrbuf_len(wrbuf);
1845 if (zebra_maps_is_complete(zh->reg->zebra_maps, *p->index_type))
1846 extract_add_complete_field(p);
1848 extract_add_incomplete_field(p);
1851 static void extract_set_store_data_cb(struct recExtractCtrl *p,
1852 void *buf, size_t sz)
1854 ZebraHandle zh = (ZebraHandle) p->handle;
1856 xfree(zh->store_data_buf);
1857 zh->store_data_buf = 0;
1858 zh->store_data_size = 0;
1861 zh->store_data_buf = xmalloc(sz);
1862 zh->store_data_size = sz;
1863 memcpy(zh->store_data_buf, buf, sz);
1867 static void extract_set_store_data_prepare(struct recExtractCtrl *p)
1869 ZebraHandle zh = (ZebraHandle) p->handle;
1870 xfree(zh->store_data_buf);
1871 zh->store_data_buf = 0;
1872 zh->store_data_size = 0;
1873 p->setStoreData = extract_set_store_data_cb;
1876 static void extract_schema_add(struct recExtractCtrl *p, Odr_oid *oid)
1878 ZebraHandle zh = (ZebraHandle) p->handle;
1879 zebraExplain_addSchema(zh->reg->zei, oid);
1882 void extract_flush_sort_keys(ZebraHandle zh, zint sysno,
1883 int cmd, zebra_rec_keys_t reckeys)
1886 yaz_log(YLOG_LOG, "extract_flush_sort_keys cmd=%d sysno=" ZINT_FORMAT,
1888 extract_rec_keys_log(zh, cmd, reckeys, YLOG_LOG);
1891 if (zebra_rec_keys_rewind(reckeys))
1893 zebra_sort_index_t si = zh->reg->sort_index;
1896 struct it_key key_in;
1898 zebra_sort_sysno(si, sysno);
1900 while (zebra_rec_keys_read(reckeys, &str, &slen, &key_in))
1902 int ord = CAST_ZINT_TO_INT(key_in.mem[0]);
1904 zebra_sort_type(si, ord);
1906 zebra_sort_add(si, str, slen);
1908 zebra_sort_delete(si);
1916 * indent-tabs-mode: nil
1918 * vim: shiftwidth=4 tabstop=8 expandtab