1 /* $Id: extract.c,v 1.266 2007-10-30 19:17:15 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 zebra_map_t zm = zebra_map_get(zh->reg->zebra_maps, i);
107 if (zebra_maps_is_positioned(zm))
112 ctrl->flagShowRecords = !zh->m_flag_rw;
116 static void extract_add_index_string(RecWord *p,
117 zinfo_index_category_t cat,
118 const char *str, int length);
120 static void extract_set_store_data_prepare(struct recExtractCtrl *p);
122 static void extract_init(struct recExtractCtrl *p, RecWord *w)
125 w->index_name = "any";
133 struct snip_rec_info {
135 zebra_snippets *snippets;
139 static void snippet_add_complete_field(RecWord *p, int ord,
142 struct snip_rec_info *h = p->extractCtrl->handle;
144 const char *b = p->term_buf;
145 char buf[IT_MAX_WORD+1];
146 const char **map = 0;
147 int i = 0, remain = p->term_len;
148 const char *start = b;
149 const char *last = 0;
152 map = zebra_maps_input(zm, &b, remain, 1);
154 while (remain > 0 && i < IT_MAX_WORD)
156 while (map && *map && **map == *CHR_SPACE)
158 remain = p->term_len - (b - p->term_buf);
161 start = b; /* set to first non-ws area */
164 int first = i ? 0 : 1; /* first position */
166 map = zebra_maps_input(zm, &b, remain, first);
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(zm, &b, remain, 0);
203 if (last && start != last)
204 zebra_snippets_appendn(h->snippets, p->seqno, 0, ord,
205 start, last - start);
208 static void snippet_add_incomplete_field(RecWord *p, int ord, zebra_map_t zm)
210 struct snip_rec_info *h = p->extractCtrl->handle;
211 const char *b = p->term_buf;
212 int remain = p->term_len;
214 const char **map = 0;
215 const char *start = b;
216 const char *last = b;
219 map = zebra_maps_input(zm, &b, remain, 0);
223 char buf[IT_MAX_WORD+1];
227 while (map && *map && **map == *CHR_SPACE)
229 remain = p->term_len - (b - p->term_buf);
232 map = zebra_maps_input(zm, &b, remain, 0);
240 zebra_snippets_appendn(h->snippets, p->seqno, 1, ord,
241 start, last - start);
247 while (map && *map && **map != *CHR_SPACE)
249 const char *cp = *map;
251 while (i < IT_MAX_WORD && *cp)
253 remain = p->term_len - (b - p->term_buf);
256 map = zebra_maps_input(zm, &b, remain, 0);
266 if (zebra_maps_is_first_in_field(zm))
268 /* first in field marker */
273 zebra_snippets_appendn(h->snippets, p->seqno, 0, ord,
274 start, last - start);
281 static void snippet_token_add(RecWord *p)
283 struct snip_rec_info *h = p->extractCtrl->handle;
284 ZebraHandle zh = h->zh;
285 zebra_map_t zm = zebra_map_get(zh->reg->zebra_maps, *p->index_type);
287 if (zm && zebra_maps_is_index(zm))
289 ZebraExplainInfo zei = zh->reg->zei;
290 int ch = zebraExplain_lookup_attr_str(
291 zei, zinfo_index_category_index, p->index_type, p->index_name);
293 if (zebra_maps_is_complete(zm))
294 snippet_add_complete_field(p, ch, zm);
296 snippet_add_incomplete_field(p, ch, zm);
300 static void snippet_schema_add(
301 struct recExtractCtrl *p, Odr_oid *oid)
306 void extract_snippet(ZebraHandle zh, zebra_snippets *sn,
307 struct ZebraRecStream *stream,
308 RecType rt, void *recTypeClientData)
310 struct recExtractCtrl extractCtrl;
311 struct snip_rec_info info;
314 extractCtrl.stream = stream;
315 extractCtrl.first_record = 1;
316 extractCtrl.init = extract_init;
317 extractCtrl.tokenAdd = snippet_token_add;
318 extractCtrl.schemaAdd = snippet_schema_add;
322 extractCtrl.dh = zh->reg->dh;
326 extractCtrl.handle = &info;
327 extractCtrl.match_criteria[0] = '\0';
328 extractCtrl.staticrank = 0;
329 extractCtrl.action = action_insert;
331 init_extractCtrl(zh, &extractCtrl);
333 extractCtrl.setStoreData = 0;
335 r = (*rt->extract)(recTypeClientData, &extractCtrl);
339 static void searchRecordKey(ZebraHandle zh,
340 zebra_rec_keys_t reckeys,
341 const char *index_name,
342 const char **ws, int ws_length)
346 zinfo_index_category_t cat = zinfo_index_category_index;
348 for (i = 0; i<ws_length; i++)
352 ch = zebraExplain_lookup_attr_str(zh->reg->zei, cat, "0", index_name);
354 ch = zebraExplain_lookup_attr_str(zh->reg->zei, cat, "p", index_name);
356 ch = zebraExplain_lookup_attr_str(zh->reg->zei, cat, "w", index_name);
361 if (zebra_rec_keys_rewind(reckeys))
368 while (zebra_rec_keys_read(reckeys, &str, &slen, &key))
370 assert(key.len <= IT_KEY_LEVEL_MAX && key.len > 2);
372 seqno = key.mem[key.len-1];
374 if (key.mem[0] == ch)
380 woff = seqno - startSeq;
381 if (woff >= 0 && woff < ws_length)
388 #define FILE_MATCH_BLANK "\t "
390 static char *get_match_from_spec(ZebraHandle zh,
391 zebra_rec_keys_t reckeys,
392 const char *fname, const char *spec)
394 static char dstBuf[2048]; /* static here ??? */
396 const char *s = spec;
400 for (; *s && strchr(FILE_MATCH_BLANK, *s); s++)
407 char attset_str[64], attname_str[64];
411 for (s++; strchr(FILE_MATCH_BLANK, *s); s++)
413 for (i = 0; *s && *s != ',' && *s != ')' &&
414 !strchr(FILE_MATCH_BLANK, *s); s++)
415 if (i+1 < sizeof(attset_str))
416 attset_str[i++] = *s;
417 attset_str[i] = '\0';
419 for (; strchr(FILE_MATCH_BLANK, *s); s++)
422 strcpy(attname_str, attset_str);
425 for (s++; strchr(FILE_MATCH_BLANK, *s); s++)
427 for (i = 0; *s && *s != ')' &&
428 !strchr(FILE_MATCH_BLANK, *s); s++)
429 if (i+1 < sizeof(attname_str))
430 attname_str[i++] = *s;
431 attname_str[i] = '\0';
434 searchRecordKey(zh, reckeys, attname_str, ws, 32);
438 yaz_log(YLOG_WARN, "Missing ) in match criteria %s in group %s",
439 spec, zh->m_group ? zh->m_group : "none");
444 for (i = 0; i<32; i++)
453 dst += strlen(ws[i]);
457 yaz_log(YLOG_WARN, "Record didn't contain match"
458 " fields in (%s,%s)", attset_str, attname_str);
466 const char *spec_src = NULL;
467 const char *s1 = ++s;
468 while (*s1 && !strchr(FILE_MATCH_BLANK, *s1))
472 if (spec_len > sizeof(special)-1)
473 spec_len = sizeof(special)-1;
474 memcpy(special, s, spec_len);
475 special[spec_len] = '\0';
478 if (!strcmp(special, "group"))
479 spec_src = zh->m_group;
480 else if (!strcmp(special, "database"))
481 spec_src = zh->basenames[0];
482 else if (!strcmp(special, "filename")) {
485 else if (!strcmp(special, "type"))
486 spec_src = zh->m_record_type;
491 strcpy(dst, spec_src);
492 dst += strlen(spec_src);
495 else if (*s == '\"' || *s == '\'')
497 int stopMarker = *s++;
501 while (*s && *s != stopMarker)
503 if (i+1 < sizeof(tmpString))
504 tmpString[i++] = *s++;
509 strcpy(dst, tmpString);
510 dst += strlen(tmpString);
514 yaz_log(YLOG_WARN, "Syntax error in match criteria %s in group %s",
515 spec, zh->m_group ? zh->m_group : "none");
522 yaz_log(YLOG_WARN, "No match criteria for record %s in group %s",
523 fname, zh->m_group ? zh->m_group : "none");
530 struct recordLogInfo {
533 struct recordGroup *rGroup;
536 static void all_matches_add(struct recExtractCtrl *ctrl)
539 extract_init(ctrl, &word);
540 word.index_name = "_ALLRECORDS";
541 word.index_type = "w";
543 extract_add_index_string(&word, zinfo_index_category_alwaysmatches,
547 ZEBRA_RES zebra_extract_records_stream(ZebraHandle zh,
548 struct ZebraRecStream *stream,
549 enum zebra_recctrl_action_t action,
551 const char *recordType,
553 const char *match_criteria,
556 void *recTypeClientData);
559 ZEBRA_RES zebra_extract_file(ZebraHandle zh, zint *sysno, const char *fname,
562 ZEBRA_RES r = ZEBRA_OK;
567 struct file_read_info *fi = 0;
568 const char *original_record_type = 0;
570 void *recTypeClientData;
571 struct ZebraRecStream stream, *streamp;
573 zebra_init_log_level();
575 if (!zh->m_group || !*zh->m_group)
578 sprintf(gprefix, "%s.", zh->m_group);
580 yaz_log(log_level_extract, "zebra_extract_file %s", fname);
582 /* determine file extension */
584 for (i = strlen(fname); --i >= 0; )
587 else if (fname[i] == '.')
589 strcpy(ext, fname+i+1);
592 /* determine file type - depending on extension */
593 original_record_type = zh->m_record_type;
594 if (!zh->m_record_type)
596 sprintf(ext_res, "%srecordType.%s", gprefix, ext);
597 zh->m_record_type = res_get(zh->res, ext_res);
599 if (!zh->m_record_type)
602 if (zh->records_processed + zh->records_skipped
603 < zh->m_file_verbose_limit)
604 yaz_log(YLOG_LOG, "? %s", fname);
605 zh->records_skipped++;
608 /* determine match criteria */
609 if (!zh->m_record_id)
611 sprintf(ext_res, "%srecordId.%s", gprefix, ext);
612 zh->m_record_id = res_get(zh->res, ext_res);
616 recType_byName(zh->reg->recTypes, zh->res, zh->m_record_type,
617 &recTypeClientData)))
619 yaz_log(YLOG_WARN, "No such record type: %s", zh->m_record_type);
623 switch(recType->version)
628 yaz_log(YLOG_WARN, "Bad filter version: %s", zh->m_record_type);
630 if (sysno && deleteFlag)
639 if (zh->path_reg && !yaz_is_abspath(fname))
641 strcpy(full_rep, zh->path_reg);
642 strcat(full_rep, "/");
643 strcat(full_rep, fname);
646 strcpy(full_rep, fname);
648 if ((fd = open(full_rep, O_BINARY|O_RDONLY)) == -1)
650 yaz_log(YLOG_WARN|YLOG_ERRNO, "open %s", full_rep);
651 zh->m_record_type = original_record_type;
655 zebra_create_stream_fd(streamp, fd, 0);
657 r = zebra_extract_records_stream(zh, streamp,
659 action_delete : action_update,
663 0, /*match_criteria */
665 recType, recTypeClientData);
667 stream.destroy(streamp);
668 zh->m_record_type = original_record_type;
673 If sysno is provided, then it's used to identify the reocord.
674 If not, and match_criteria is provided, then sysno is guessed
675 If not, and a record is provided, then sysno is got from there
679 ZEBRA_RES zebra_buffer_extract_record(ZebraHandle zh,
680 const char *buf, size_t buf_size,
681 enum zebra_recctrl_action_t action,
683 const char *recordType,
685 const char *match_criteria,
688 struct ZebraRecStream stream;
693 if (recordType && *recordType)
695 yaz_log(log_level_extract,
696 "Record type explicitly specified: %s", recordType);
697 recType = recType_byName(zh->reg->recTypes, zh->res, recordType,
702 if (!(zh->m_record_type))
704 yaz_log(YLOG_WARN, "No such record type defined");
707 yaz_log(log_level_extract, "Get record type from rgroup: %s",
709 recType = recType_byName(zh->reg->recTypes, zh->res,
710 zh->m_record_type, &clientData);
711 recordType = zh->m_record_type;
716 yaz_log(YLOG_WARN, "No such record type: %s", recordType);
720 zebra_create_stream_mem(&stream, buf, buf_size);
722 res = zebra_extract_records_stream(zh, &stream,
729 recType, clientData);
730 stream.destroy(&stream);
734 ZEBRA_RES zebra_extract_records_stream(ZebraHandle zh,
735 struct ZebraRecStream *stream,
736 enum zebra_recctrl_action_t action,
738 const char *recordType,
740 const char *match_criteria,
743 void *recTypeClientData)
745 ZEBRA_RES res = ZEBRA_OK;
749 res = zebra_extract_record_stream(zh, stream,
756 recType, recTypeClientData, &more);
771 static WRBUF wrbuf_hex_str(const char *cstr)
774 WRBUF w = wrbuf_alloc();
775 for (i = 0; cstr[i]; i++)
777 if (cstr[i] < ' ' || cstr[i] > 126)
778 wrbuf_printf(w, "\\%02X", cstr[i] & 0xff);
780 wrbuf_putc(w, cstr[i]);
785 ZEBRA_RES zebra_extract_record_stream(ZebraHandle zh,
786 struct ZebraRecStream *stream,
787 enum zebra_recctrl_action_t action,
789 const char *recordType,
791 const char *match_criteria,
794 void *recTypeClientData,
799 RecordAttr *recordAttr;
800 struct recExtractCtrl extractCtrl;
802 const char *matchStr = 0;
804 off_t start_offset = 0, end_offset = 0;
805 const char *pr_fname = fname; /* filename to print .. */
806 int show_progress = zh->records_processed + zh->records_skipped
807 < zh->m_file_verbose_limit ? 1:0;
809 zebra_init_log_level();
812 pr_fname = "<no file>"; /* make it printable if file is omitted */
814 zebra_rec_keys_reset(zh->reg->keys);
815 zebra_rec_keys_reset(zh->reg->sortKeys);
817 if (zebraExplain_curDatabase(zh->reg->zei, zh->basenames[0]))
819 if (zebraExplain_newDatabase(zh->reg->zei, zh->basenames[0],
820 zh->m_explain_database))
826 off_t null_offset = 0;
827 extractCtrl.stream = stream;
829 start_offset = stream->tellf(stream);
831 extractCtrl.first_record = start_offset ? 0 : 1;
833 stream->endf(stream, &null_offset);;
835 extractCtrl.init = extract_init;
836 if (zh->reg->index_types)
838 extractCtrl.tokenAdd = extract_token_add2;
842 extractCtrl.tokenAdd = extract_token_add;
844 extractCtrl.schemaAdd = extract_schema_add;
845 extractCtrl.dh = zh->reg->dh;
846 extractCtrl.handle = zh;
847 extractCtrl.match_criteria[0] = '\0';
848 extractCtrl.staticrank = 0;
849 extractCtrl.action = action;
851 init_extractCtrl(zh, &extractCtrl);
853 extract_set_store_data_prepare(&extractCtrl);
855 r = (*recType->extract)(recTypeClientData, &extractCtrl);
857 if (action == action_update)
859 action = extractCtrl.action;
864 case RECCTRL_EXTRACT_EOF:
866 case RECCTRL_EXTRACT_ERROR_GENERIC:
867 /* error occured during extraction ... */
868 yaz_log(YLOG_WARN, "extract error: generic");
870 case RECCTRL_EXTRACT_ERROR_NO_SUCH_FILTER:
871 /* error occured during extraction ... */
872 yaz_log(YLOG_WARN, "extract error: no such filter");
874 case RECCTRL_EXTRACT_SKIP:
876 yaz_log(YLOG_LOG, "skip %s %s " ZINT_FORMAT,
877 recordType, pr_fname, (zint) start_offset);
880 end_offset = stream->endf(stream, 0);
882 stream->seekf(stream, end_offset);
885 case RECCTRL_EXTRACT_OK:
888 yaz_log(YLOG_WARN, "extract error: unknown error: %d", r);
891 end_offset = stream->endf(stream, 0);
893 stream->seekf(stream, end_offset);
895 end_offset = stream->tellf(stream);
897 all_matches_add(&extractCtrl);
899 if (extractCtrl.match_criteria[0])
900 match_criteria = extractCtrl.match_criteria;
908 if (match_criteria && *match_criteria) {
909 matchStr = match_criteria;
911 if (zh->m_record_id && *zh->m_record_id) {
912 matchStr = get_match_from_spec(zh, zh->reg->keys, pr_fname,
916 yaz_log(YLOG_LOG, "error %s %s " ZINT_FORMAT, recordType,
917 pr_fname, (zint) start_offset);
924 int db_ord = zebraExplain_get_database_ord(zh->reg->zei);
925 char *rinfo = dict_lookup_ord(zh->reg->matchDict, db_ord,
929 if (log_level_extract)
931 WRBUF w = wrbuf_hex_str(matchStr);
932 yaz_log(log_level_extract, "matchStr: %s", wrbuf_cstr(w));
937 assert(*rinfo == sizeof(*sysno));
938 memcpy(sysno, rinfo+1, sizeof(*sysno));
942 if (zebra_rec_keys_empty(zh->reg->keys))
944 /* the extraction process returned no information - the record
945 is probably empty - unless flagShowRecords is in use */
953 if (action == action_delete)
955 yaz_log(YLOG_LOG, "delete %s %s " ZINT_FORMAT, recordType,
956 pr_fname, (zint) start_offset);
957 yaz_log(YLOG_WARN, "cannot delete record above (seems new)");
960 else if (action == action_replace)
962 yaz_log(YLOG_LOG, "update %s %s " ZINT_FORMAT, recordType,
963 pr_fname, (zint) start_offset);
964 yaz_log(YLOG_WARN, "cannot update record above (seems new)");
968 yaz_log(YLOG_LOG, "add %s %s " ZINT_FORMAT, recordType, pr_fname,
969 (zint) start_offset);
970 rec = rec_new(zh->reg->records);
974 recordAttr = rec_init_attr(zh->reg->zei, rec);
975 if (extractCtrl.staticrank < 0)
977 yaz_log(YLOG_WARN, "Negative staticrank for record. Set to 0");
978 extractCtrl.staticrank = 0;
983 int db_ord = zebraExplain_get_database_ord(zh->reg->zei);
984 dict_insert_ord(zh->reg->matchDict, db_ord, matchStr,
985 sizeof(*sysno), sysno);
988 extract_flush_sort_keys(zh, *sysno, 1, zh->reg->sortKeys);
990 extract_flush_record_keys2(zh, *sysno,
991 zh->reg->keys, extractCtrl.staticrank,
992 0, recordAttr->staticrank);
994 extract_flush_record_keys(zh, *sysno, 1, zh->reg->keys,
995 extractCtrl.staticrank);
997 recordAttr->staticrank = extractCtrl.staticrank;
998 zh->records_inserted++;
1002 /* record already exists */
1003 zebra_rec_keys_t delkeys = zebra_rec_keys_open();
1004 zebra_rec_keys_t sortKeys = zebra_rec_keys_open();
1005 if (action == action_insert)
1007 yaz_log(YLOG_LOG, "skipped %s %s " ZINT_FORMAT,
1008 recordType, pr_fname, (zint) start_offset);
1013 rec = rec_get(zh->reg->records, *sysno);
1016 recordAttr = rec_init_attr(zh->reg->zei, rec);
1018 /* decrease total size */
1019 zebraExplain_recordBytesIncrement(zh->reg->zei,
1020 - recordAttr->recordSize);
1022 zebra_rec_keys_set_buf(delkeys,
1023 rec->info[recInfo_delKeys],
1024 rec->size[recInfo_delKeys],
1026 zebra_rec_keys_set_buf(sortKeys,
1027 rec->info[recInfo_sortKeys],
1028 rec->size[recInfo_sortKeys],
1031 extract_flush_sort_keys(zh, *sysno, 0, sortKeys);
1033 extract_flush_record_keys(zh, *sysno, 0, delkeys,
1034 recordAttr->staticrank);
1036 if (action == action_delete)
1038 /* record going to be deleted */
1040 extract_flush_record_keys2(zh, *sysno, 0, recordAttr->staticrank,
1041 delkeys, recordAttr->staticrank);
1043 if (zebra_rec_keys_empty(delkeys))
1045 yaz_log(YLOG_LOG, "delete %s %s " ZINT_FORMAT, recordType,
1046 pr_fname, (zint) start_offset);
1047 yaz_log(YLOG_WARN, "cannot delete file above, "
1048 "storeKeys false (3)");
1053 yaz_log(YLOG_LOG, "delete %s %s " ZINT_FORMAT, recordType,
1054 pr_fname, (zint) start_offset);
1055 zh->records_deleted++;
1058 int db_ord = zebraExplain_get_database_ord(zh->reg->zei);
1059 dict_delete_ord(zh->reg->matchDict, db_ord, matchStr);
1061 rec_del(zh->reg->records, &rec);
1063 zebra_rec_keys_close(delkeys);
1064 zebra_rec_keys_close(sortKeys);
1070 { /* update or special_update */
1072 yaz_log(YLOG_LOG, "update %s %s " ZINT_FORMAT, recordType,
1073 pr_fname, (zint) start_offset);
1074 extract_flush_sort_keys(zh, *sysno, 1, zh->reg->sortKeys);
1077 extract_flush_record_keys2(zh, *sysno,
1078 zh->reg->keys, extractCtrl.staticrank,
1079 delkeys, recordAttr->staticrank);
1081 extract_flush_record_keys(zh, *sysno, 1,
1082 zh->reg->keys, extractCtrl.staticrank);
1084 recordAttr->staticrank = extractCtrl.staticrank;
1085 zh->records_updated++;
1087 zebra_rec_keys_close(delkeys);
1088 zebra_rec_keys_close(sortKeys);
1090 /* update file type */
1091 xfree(rec->info[recInfo_fileType]);
1092 rec->info[recInfo_fileType] =
1093 rec_strdup(recordType, &rec->size[recInfo_fileType]);
1095 /* update filename */
1096 xfree(rec->info[recInfo_filename]);
1097 rec->info[recInfo_filename] =
1098 rec_strdup(fname, &rec->size[recInfo_filename]);
1100 /* update delete keys */
1101 xfree(rec->info[recInfo_delKeys]);
1102 if (!zebra_rec_keys_empty(zh->reg->keys) && zh->m_store_keys == 1)
1104 zebra_rec_keys_get_buf(zh->reg->keys,
1105 &rec->info[recInfo_delKeys],
1106 &rec->size[recInfo_delKeys]);
1110 rec->info[recInfo_delKeys] = NULL;
1111 rec->size[recInfo_delKeys] = 0;
1113 /* update sort keys */
1114 xfree(rec->info[recInfo_sortKeys]);
1116 zebra_rec_keys_get_buf(zh->reg->sortKeys,
1117 &rec->info[recInfo_sortKeys],
1118 &rec->size[recInfo_sortKeys]);
1122 recordAttr->recordSize = end_offset - start_offset;
1123 zebraExplain_recordBytesIncrement(zh->reg->zei,
1124 recordAttr->recordSize);
1127 /* set run-number for this record */
1128 recordAttr->runNumber =
1129 zebraExplain_runNumberIncrement(zh->reg->zei, 0);
1131 /* update store data */
1132 xfree(rec->info[recInfo_storeData]);
1134 /* update store data */
1135 if (zh->store_data_buf)
1137 rec->size[recInfo_storeData] = zh->store_data_size;
1138 rec->info[recInfo_storeData] = zh->store_data_buf;
1139 zh->store_data_buf = 0;
1140 recordAttr->recordSize = zh->store_data_size;
1142 else if (zh->m_store_data)
1144 off_t cur_offset = stream->tellf(stream);
1146 rec->size[recInfo_storeData] = recordAttr->recordSize;
1147 rec->info[recInfo_storeData] = (char *)
1148 xmalloc(recordAttr->recordSize);
1149 stream->seekf(stream, start_offset);
1150 stream->readf(stream, rec->info[recInfo_storeData],
1151 recordAttr->recordSize);
1152 stream->seekf(stream, cur_offset);
1156 rec->info[recInfo_storeData] = NULL;
1157 rec->size[recInfo_storeData] = 0;
1159 /* update database name */
1160 xfree(rec->info[recInfo_databaseName]);
1161 rec->info[recInfo_databaseName] =
1162 rec_strdup(zh->basenames[0], &rec->size[recInfo_databaseName]);
1165 recordAttr->recordOffset = start_offset;
1167 /* commit this record */
1168 rec_put(zh->reg->records, &rec);
1173 ZEBRA_RES zebra_extract_explain(void *handle, Record rec, data1_node *n)
1175 ZebraHandle zh = (ZebraHandle) handle;
1176 struct recExtractCtrl extractCtrl;
1178 if (zebraExplain_curDatabase(zh->reg->zei,
1179 rec->info[recInfo_databaseName]))
1182 if (zebraExplain_newDatabase(zh->reg->zei,
1183 rec->info[recInfo_databaseName], 0))
1187 zebra_rec_keys_reset(zh->reg->keys);
1188 zebra_rec_keys_reset(zh->reg->sortKeys);
1190 extractCtrl.init = extract_init;
1191 extractCtrl.tokenAdd = extract_token_add;
1192 extractCtrl.schemaAdd = extract_schema_add;
1193 extractCtrl.dh = zh->reg->dh;
1195 init_extractCtrl(zh, &extractCtrl);
1197 extractCtrl.flagShowRecords = 0;
1198 extractCtrl.match_criteria[0] = '\0';
1199 extractCtrl.staticrank = 0;
1200 extractCtrl.action = action_update;
1202 extractCtrl.handle = handle;
1203 extractCtrl.first_record = 1;
1205 extract_set_store_data_prepare(&extractCtrl);
1208 grs_extract_tree(&extractCtrl, n);
1210 if (rec->size[recInfo_delKeys])
1212 zebra_rec_keys_t delkeys = zebra_rec_keys_open();
1214 zebra_rec_keys_t sortkeys = zebra_rec_keys_open();
1216 zebra_rec_keys_set_buf(delkeys, rec->info[recInfo_delKeys],
1217 rec->size[recInfo_delKeys],
1220 extract_flush_record_keys2(zh, rec->sysno,
1221 zh->reg->keys, 0, delkeys, 0);
1223 extract_flush_record_keys(zh, rec->sysno, 0, delkeys, 0);
1224 extract_flush_record_keys(zh, rec->sysno, 1, zh->reg->keys, 0);
1226 zebra_rec_keys_close(delkeys);
1228 zebra_rec_keys_set_buf(sortkeys, rec->info[recInfo_sortKeys],
1229 rec->size[recInfo_sortKeys],
1232 extract_flush_sort_keys(zh, rec->sysno, 0, sortkeys);
1233 zebra_rec_keys_close(sortkeys);
1238 extract_flush_record_keys2(zh, rec->sysno, zh->reg->keys, 0, 0, 0);
1240 extract_flush_record_keys(zh, rec->sysno, 1, zh->reg->keys, 0);
1243 extract_flush_sort_keys(zh, rec->sysno, 1, zh->reg->sortKeys);
1245 xfree(rec->info[recInfo_delKeys]);
1246 zebra_rec_keys_get_buf(zh->reg->keys,
1247 &rec->info[recInfo_delKeys],
1248 &rec->size[recInfo_delKeys]);
1250 xfree(rec->info[recInfo_sortKeys]);
1251 zebra_rec_keys_get_buf(zh->reg->sortKeys,
1252 &rec->info[recInfo_sortKeys],
1253 &rec->size[recInfo_sortKeys]);
1257 void extract_rec_keys_log(ZebraHandle zh, int is_insert,
1258 zebra_rec_keys_t reckeys,
1261 if (zebra_rec_keys_rewind(reckeys))
1266 NMEM nmem = nmem_create();
1268 while(zebra_rec_keys_read(reckeys, &str, &slen, &key))
1270 char keystr[200]; /* room for zints to print */
1272 int ord = CAST_ZINT_TO_INT(key.mem[0]);
1273 const char *index_type;
1275 const char *string_index;
1277 zebraExplain_lookup_ord(zh->reg->zei, ord, &index_type,
1278 0/* db */, &string_index);
1280 zebra_term_untrans_iconv(zh, nmem, *index_type,
1283 for (i = 0; i<key.len; i++)
1285 sprintf(keystr + strlen(keystr), ZINT_FORMAT " ", key.mem[i]);
1288 if (*str < CHR_BASE_CHAR)
1291 char dst_buf[200]; /* room for special chars */
1293 strcpy(dst_buf , "?");
1295 if (!strcmp(str, ""))
1296 strcpy(dst_buf, "alwaysmatches");
1297 if (!strcmp(str, FIRST_IN_FIELD_STR))
1298 strcpy(dst_buf, "firstinfield");
1299 else if (!strcmp(str, CHR_UNKNOWN))
1300 strcpy(dst_buf, "unknown");
1301 else if (!strcmp(str, CHR_SPACE))
1302 strcpy(dst_buf, "space");
1304 for (i = 0; i<slen; i++)
1306 sprintf(dst_buf + strlen(dst_buf), " %d", str[i] & 0xff);
1308 yaz_log(level, "%s%s %s %s", keystr, index_type,
1309 string_index, dst_buf);
1313 yaz_log(level, "%s%s %s \"%s\"", keystr, index_type,
1314 string_index, dst_term);
1322 void extract_rec_keys_adjust(ZebraHandle zh, int is_insert,
1323 zebra_rec_keys_t reckeys)
1325 ZebraExplainInfo zei = zh->reg->zei;
1329 struct ord_stat *next;
1332 if (zebra_rec_keys_rewind(reckeys))
1334 struct ord_stat *ord_list = 0;
1338 struct it_key key_in;
1339 while(zebra_rec_keys_read(reckeys, &str, &slen, &key_in))
1341 int ord = CAST_ZINT_TO_INT(key_in.mem[0]);
1343 for (p = ord_list; p ; p = p->next)
1351 p = xmalloc(sizeof(*p));
1362 struct ord_stat *p1 = p;
1365 zebraExplain_ord_adjust_occurrences(zei, p->ord, p->no, 1);
1367 zebraExplain_ord_adjust_occurrences(zei, p->ord, - p->no, -1);
1374 void extract_flush_record_keys2(ZebraHandle zh, zint sysno,
1375 zebra_rec_keys_t ins_keys, zint ins_rank,
1376 zebra_rec_keys_t del_keys, zint del_rank)
1378 ZebraExplainInfo zei = zh->reg->zei;
1382 if (!zh->reg->key_block)
1384 int mem = 1024*1024 * atoi( res_get_def( zh->res, "memmax", "8"));
1385 const char *key_tmp_dir = res_get_def(zh->res, "keyTmpDir", ".");
1386 int use_threads = atoi(res_get_def(zh->res, "threads", "1"));
1387 zh->reg->key_block = key_block_create(mem, key_tmp_dir, use_threads);
1392 extract_rec_keys_adjust(zh, 1, ins_keys);
1394 zebraExplain_recordCountIncrement(zei, 1);
1395 zebra_rec_keys_rewind(ins_keys);
1399 extract_rec_keys_adjust(zh, 0, del_keys);
1401 zebraExplain_recordCountIncrement(zei, -1);
1402 zebra_rec_keys_rewind(del_keys);
1408 const char *del_str;
1409 struct it_key del_key_in;
1413 const char *ins_str;
1414 struct it_key ins_key_in;
1418 del = zebra_rec_keys_read(del_keys, &del_str, &del_slen,
1421 ins = zebra_rec_keys_read(ins_keys, &ins_str, &ins_slen,
1424 if (del && ins && ins_rank == del_rank
1425 && !key_compare(&del_key_in, &ins_key_in)
1426 && ins_slen == del_slen && !memcmp(del_str, ins_str, del_slen))
1436 key_block_write(zh->reg->key_block, sysno,
1437 &del_key_in, 0, del_str, del_slen,
1438 del_rank, zh->m_staticrank);
1440 key_block_write(zh->reg->key_block, sysno,
1441 &ins_key_in, 1, ins_str, ins_slen,
1442 ins_rank, zh->m_staticrank);
1444 yaz_log(log_level_extract, "normal=%d optimized=%d", normal, optimized);
1447 void extract_flush_record_keys(ZebraHandle zh, zint sysno, int cmd,
1448 zebra_rec_keys_t reckeys,
1451 ZebraExplainInfo zei = zh->reg->zei;
1453 extract_rec_keys_adjust(zh, cmd, reckeys);
1455 if (log_level_details)
1457 yaz_log(log_level_details, "Keys for record " ZINT_FORMAT " %s",
1458 sysno, cmd ? "insert" : "delete");
1459 extract_rec_keys_log(zh, cmd, reckeys, log_level_details);
1462 if (!zh->reg->key_block)
1464 int mem = 1024*1024 * atoi( res_get_def( zh->res, "memmax", "8"));
1465 const char *key_tmp_dir = res_get_def(zh->res, "keyTmpDir", ".");
1466 int use_threads = atoi(res_get_def(zh->res, "threads", "1"));
1467 zh->reg->key_block = key_block_create(mem, key_tmp_dir, use_threads);
1469 zebraExplain_recordCountIncrement(zei, cmd ? 1 : -1);
1472 yaz_log(YLOG_LOG, "sysno=" ZINT_FORMAT " cmd=%d", sysno, cmd);
1473 print_rec_keys(zh, reckeys);
1475 if (zebra_rec_keys_rewind(reckeys))
1479 struct it_key key_in;
1480 while(zebra_rec_keys_read(reckeys, &str, &slen, &key_in))
1482 key_block_write(zh->reg->key_block, sysno,
1483 &key_in, cmd, str, slen,
1484 staticrank, zh->m_staticrank);
1489 ZEBRA_RES zebra_rec_keys_to_snippets(ZebraHandle zh,
1490 zebra_rec_keys_t reckeys,
1491 zebra_snippets *snippets)
1493 NMEM nmem = nmem_create();
1494 if (zebra_rec_keys_rewind(reckeys))
1499 while (zebra_rec_keys_read(reckeys, &str, &slen, &key))
1504 const char *index_type;
1506 assert(key.len <= IT_KEY_LEVEL_MAX && key.len > 2);
1507 seqno = key.mem[key.len-1];
1508 ord = CAST_ZINT_TO_INT(key.mem[0]);
1510 zebraExplain_lookup_ord(zh->reg->zei, ord, &index_type,
1511 0/* db */, 0 /* string_index */);
1513 zebra_term_untrans_iconv(zh, nmem, *index_type,
1515 zebra_snippets_append(snippets, seqno, 0, ord, dst_term);
1523 void print_rec_keys(ZebraHandle zh, zebra_rec_keys_t reckeys)
1525 yaz_log(YLOG_LOG, "print_rec_keys");
1526 if (zebra_rec_keys_rewind(reckeys))
1531 while (zebra_rec_keys_read(reckeys, &str, &slen, &key))
1533 char dst_buf[IT_MAX_WORD];
1535 const char *index_type;
1536 int ord = CAST_ZINT_TO_INT(key.mem[0]);
1538 assert(key.len <= IT_KEY_LEVEL_MAX && key.len > 2);
1540 zebraExplain_lookup_ord(zh->reg->zei, ord, &index_type, &db, 0);
1542 seqno = key.mem[key.len-1];
1544 zebra_term_untrans(zh, *index_type, dst_buf, str);
1546 yaz_log(YLOG_LOG, "ord=%d seqno=" ZINT_FORMAT
1547 " term=%s", ord, seqno, dst_buf);
1552 static void extract_add_index_string(RecWord *p, zinfo_index_category_t cat,
1553 const char *str, int length)
1556 ZebraHandle zh = p->extractCtrl->handle;
1557 ZebraExplainInfo zei = zh->reg->zei;
1560 ch = zebraExplain_lookup_attr_str(zei, cat, p->index_type, p->index_name);
1562 ch = zebraExplain_add_attr_str(zei, cat, p->index_type, p->index_name);
1566 key.mem[i++] = p->record_id;
1567 key.mem[i++] = p->section_id;
1569 if (zh->m_segment_indexing)
1570 key.mem[i++] = p->segment;
1571 key.mem[i++] = p->seqno;
1574 zebra_rec_keys_write(zh->reg->keys, str, length, &key);
1577 static void extract_add_sort_string(RecWord *p, const char *str, int length)
1580 ZebraHandle zh = p->extractCtrl->handle;
1581 ZebraExplainInfo zei = zh->reg->zei;
1583 zinfo_index_category_t cat = zinfo_index_category_sort;
1585 ch = zebraExplain_lookup_attr_str(zei, cat, p->index_type, p->index_name);
1587 ch = zebraExplain_add_attr_str(zei, cat, p->index_type, p->index_name);
1590 key.mem[1] = p->record_id;
1592 zebra_rec_keys_write(zh->reg->sortKeys, str, length, &key);
1595 static void extract_add_staticrank_string(RecWord *p,
1596 const char *str, int length)
1599 struct recExtractCtrl *ctrl = p->extractCtrl;
1601 if (length > sizeof(valz)-1)
1602 length = sizeof(valz)-1;
1604 memcpy(valz, str, length);
1605 valz[length] = '\0';
1606 ctrl->staticrank = atozint(valz);
1609 static void extract_add_string(RecWord *p, zebra_map_t zm,
1610 const char *string, int length)
1617 if (zebra_maps_is_index(zm))
1619 extract_add_index_string(p, zinfo_index_category_index,
1621 if (zebra_maps_is_alwaysmatches(zm))
1624 memcpy(&word, p, sizeof(word));
1627 extract_add_index_string(
1628 &word, zinfo_index_category_alwaysmatches, "", 0);
1631 else if (zebra_maps_is_sort(zm))
1633 extract_add_sort_string(p, string, length);
1635 else if (zebra_maps_is_staticrank(zm))
1637 extract_add_staticrank_string(p, string, length);
1641 static void extract_add_incomplete_field(RecWord *p, zebra_map_t zm)
1643 const char *b = p->term_buf;
1644 int remain = p->term_len;
1646 const char **map = 0;
1649 map = zebra_maps_input(zm, &b, remain, 0);
1653 char buf[IT_MAX_WORD+1];
1657 while (map && *map && **map == *CHR_SPACE)
1659 remain = p->term_len - (b - p->term_buf);
1661 map = zebra_maps_input(zm, &b, remain, 0);
1668 while (map && *map && **map != *CHR_SPACE)
1670 const char *cp = *map;
1672 while (i < IT_MAX_WORD && *cp)
1674 remain = p->term_len - (b - p->term_buf);
1676 map = zebra_maps_input(zm, &b, remain, 0);
1686 if (zebra_maps_is_first_in_field(zm))
1688 /* first in field marker */
1689 extract_add_string(p, zm, FIRST_IN_FIELD_STR, FIRST_IN_FIELD_LEN);
1693 extract_add_string(p, zm, buf, i);
1698 static void extract_add_complete_field(RecWord *p, zebra_map_t zm)
1700 const char *b = p->term_buf;
1701 char buf[IT_MAX_WORD+1];
1702 const char **map = 0;
1703 int i = 0, remain = p->term_len;
1706 map = zebra_maps_input(zm, &b, remain, 1);
1708 while (remain > 0 && i < IT_MAX_WORD)
1710 while (map && *map && **map == *CHR_SPACE)
1712 remain = p->term_len - (b - p->term_buf);
1716 int first = i ? 0 : 1; /* first position */
1717 map = zebra_maps_input(zm, &b, remain, first);
1725 if (i && i < IT_MAX_WORD)
1726 buf[i++] = *CHR_SPACE;
1727 while (map && *map && **map != *CHR_SPACE)
1729 const char *cp = *map;
1731 if (**map == *CHR_CUT)
1737 if (i >= IT_MAX_WORD)
1739 while (i < IT_MAX_WORD && *cp)
1742 remain = p->term_len - (b - p->term_buf);
1745 map = zebra_maps_input(zm, &b, remain, 0);
1753 extract_add_string(p, zm, buf, i);
1756 static void extract_token_add2_index(ZebraHandle zh, zebra_index_type_t type,
1760 const char *res_buf = 0;
1762 int r = zebra_index_type_tokenize(type, p->term_buf, p->term_len,
1763 &res_buf, &res_len);
1764 int cat = zinfo_index_category_index;
1765 int ch = zebraExplain_lookup_attr_str(zh->reg->zei, cat, p->index_type, p->index_name);
1767 ch = zebraExplain_add_attr_str(zh->reg->zei, cat, p->index_type, p->index_name);
1772 key.mem[i++] = p->record_id;
1773 key.mem[i++] = p->section_id;
1775 if (zh->m_segment_indexing)
1776 key.mem[i++] = p->segment;
1777 key.mem[i++] = p->seqno;
1780 yaz_log(YLOG_LOG, "keys_write %.*s", (int) res_len, res_buf);
1781 zebra_rec_keys_write(zh->reg->keys, res_buf, res_len, &key);
1784 r = zebra_index_type_tokenize(type, 0, 0, &res_buf, &res_len);
1788 static void extract_token_add2(RecWord *p)
1790 ZebraHandle zh = p->extractCtrl->handle;
1791 zebra_index_type_t type = zebra_index_type_get(zh->reg->index_types, p->index_type);
1794 if (zebra_index_type_is_index(type))
1796 extract_token_add2_index(zh, type, p);
1798 else if (zebra_index_type_is_sort(type))
1806 /** \brief top-level indexing handler for recctrl system
1807 \param p token data to be indexed
1811 zebra_add_{in}_complete
1814 extract_add_index_string
1816 extract_add_sort_string
1818 extract_add_staticrank_string
1821 static void extract_token_add(RecWord *p)
1823 ZebraHandle zh = p->extractCtrl->handle;
1824 zebra_map_t zm = zebra_map_get_or_add(zh->reg->zebra_maps, *p->index_type);
1827 if (log_level_details)
1829 yaz_log(log_level_details, "extract_token_add "
1830 "type=%s index=%s seqno=" ZINT_FORMAT " s=%.*s",
1831 p->index_type, p->index_name,
1832 p->seqno, p->term_len, p->term_buf);
1834 if ((wrbuf = zebra_replace(zm, 0, p->term_buf, p->term_len)))
1836 p->term_buf = wrbuf_buf(wrbuf);
1837 p->term_len = wrbuf_len(wrbuf);
1839 if (zebra_maps_is_complete(zm))
1840 extract_add_complete_field(p, zm);
1842 extract_add_incomplete_field(p, zm);
1845 static void extract_set_store_data_cb(struct recExtractCtrl *p,
1846 void *buf, size_t sz)
1848 ZebraHandle zh = (ZebraHandle) p->handle;
1850 xfree(zh->store_data_buf);
1851 zh->store_data_buf = 0;
1852 zh->store_data_size = 0;
1855 zh->store_data_buf = xmalloc(sz);
1856 zh->store_data_size = sz;
1857 memcpy(zh->store_data_buf, buf, sz);
1861 static void extract_set_store_data_prepare(struct recExtractCtrl *p)
1863 ZebraHandle zh = (ZebraHandle) p->handle;
1864 xfree(zh->store_data_buf);
1865 zh->store_data_buf = 0;
1866 zh->store_data_size = 0;
1867 p->setStoreData = extract_set_store_data_cb;
1870 static void extract_schema_add(struct recExtractCtrl *p, Odr_oid *oid)
1872 ZebraHandle zh = (ZebraHandle) p->handle;
1873 zebraExplain_addSchema(zh->reg->zei, oid);
1876 void extract_flush_sort_keys(ZebraHandle zh, zint sysno,
1877 int cmd, zebra_rec_keys_t reckeys)
1880 yaz_log(YLOG_LOG, "extract_flush_sort_keys cmd=%d sysno=" ZINT_FORMAT,
1882 extract_rec_keys_log(zh, cmd, reckeys, YLOG_LOG);
1885 if (zebra_rec_keys_rewind(reckeys))
1887 zebra_sort_index_t si = zh->reg->sort_index;
1890 struct it_key key_in;
1892 zebra_sort_sysno(si, sysno);
1894 while (zebra_rec_keys_read(reckeys, &str, &slen, &key_in))
1896 int ord = CAST_ZINT_TO_INT(key_in.mem[0]);
1898 zebra_sort_type(si, ord);
1900 zebra_sort_add(si, str, slen);
1902 zebra_sort_delete(si);
1910 * indent-tabs-mode: nil
1912 * vim: shiftwidth=4 tabstop=8 expandtab