1 /* $Id: extract.c,v 1.263 2007-10-29 09:25:40 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
39 static int log_level_extract = 0;
40 static int log_level_details = 0;
41 static int log_level_initialized = 0;
43 /* 1 if we use eliminitate identical delete/insert keys */
44 /* eventually this the 0-case code will be removed */
47 void extract_flush_record_keys2(ZebraHandle zh, zint sysno,
48 zebra_rec_keys_t ins_keys,
50 zebra_rec_keys_t del_keys,
53 static void zebra_init_log_level(void)
55 if (!log_level_initialized)
57 log_level_initialized = 1;
59 log_level_extract = yaz_log_module_level("extract");
60 log_level_details = yaz_log_module_level("indexdetails");
64 static void extract_flush_record_keys(ZebraHandle zh, zint sysno,
65 int cmd, zebra_rec_keys_t reckeys,
67 static void extract_flush_sort_keys(ZebraHandle zh, zint sysno,
68 int cmd, zebra_rec_keys_t skp);
69 static void extract_schema_add(struct recExtractCtrl *p, Odr_oid *oid);
70 static void extract_token_add(RecWord *p);
72 static void check_log_limit(ZebraHandle zh)
74 if (zh->records_processed + zh->records_skipped == zh->m_file_verbose_limit)
76 yaz_log(YLOG_LOG, "More than %d file log entries. Omitting rest",
77 zh->m_file_verbose_limit);
81 static void logRecord(ZebraHandle zh)
84 ++zh->records_processed;
85 if (!(zh->records_processed % 1000))
87 yaz_log(YLOG_LOG, "Records: "ZINT_FORMAT" i/u/d "
88 ZINT_FORMAT"/"ZINT_FORMAT"/"ZINT_FORMAT,
89 zh->records_processed, zh->records_inserted,
90 zh->records_updated, zh->records_deleted);
94 static void init_extractCtrl(ZebraHandle zh, struct recExtractCtrl *ctrl)
97 for (i = 0; i<256; i++)
99 if (zebra_maps_is_positioned(zh->reg->zebra_maps, i))
104 ctrl->flagShowRecords = !zh->m_flag_rw;
108 static void extract_add_index_string(RecWord *p,
109 zinfo_index_category_t cat,
110 const char *str, int length);
112 static void extract_set_store_data_prepare(struct recExtractCtrl *p);
114 static void extract_init(struct recExtractCtrl *p, RecWord *w)
117 w->index_name = "any";
125 struct snip_rec_info {
127 zebra_snippets *snippets;
131 static void snippet_add_complete_field(RecWord *p, int ord)
133 struct snip_rec_info *h = p->extractCtrl->handle;
134 ZebraHandle zh = h->zh;
136 const char *b = p->term_buf;
137 char buf[IT_MAX_WORD+1];
138 const char **map = 0;
139 int i = 0, remain = p->term_len;
140 const char *start = b;
141 const char *last = 0;
144 map = zebra_maps_input(zh->reg->zebra_maps, p->index_type, &b, remain, 1);
146 while (remain > 0 && i < IT_MAX_WORD)
148 while (map && *map && **map == *CHR_SPACE)
150 remain = p->term_len - (b - p->term_buf);
153 start = b; /* set to first non-ws area */
156 int first = i ? 0 : 1; /* first position */
158 map = zebra_maps_input(zh->reg->zebra_maps, p->index_type,
167 if (i && i < IT_MAX_WORD)
168 buf[i++] = *CHR_SPACE;
169 while (map && *map && **map != *CHR_SPACE)
171 const char *cp = *map;
173 if (**map == *CHR_CUT)
179 if (i >= IT_MAX_WORD)
181 while (i < IT_MAX_WORD && *cp)
185 remain = p->term_len - (b - p->term_buf);
188 map = zebra_maps_input(zh->reg->zebra_maps, p->index_type, &b,
197 if (last && start != last)
198 zebra_snippets_appendn(h->snippets, p->seqno, 0, ord,
199 start, last - start);
202 static void snippet_add_incomplete_field(RecWord *p, int ord)
204 struct snip_rec_info *h = p->extractCtrl->handle;
205 ZebraHandle zh = h->zh;
206 const char *b = p->term_buf;
207 int remain = p->term_len;
209 const char **map = 0;
210 const char *start = b;
211 const char *last = b;
214 map = zebra_maps_input(zh->reg->zebra_maps, p->index_type, &b, remain, 0);
218 char buf[IT_MAX_WORD+1];
222 while (map && *map && **map == *CHR_SPACE)
224 remain = p->term_len - (b - p->term_buf);
227 map = zebra_maps_input(zh->reg->zebra_maps, p->index_type, &b,
236 zebra_snippets_appendn(h->snippets, p->seqno, 1, ord,
237 start, last - start);
243 while (map && *map && **map != *CHR_SPACE)
245 const char *cp = *map;
247 while (i < IT_MAX_WORD && *cp)
249 remain = p->term_len - (b - p->term_buf);
252 map = zebra_maps_input(zh->reg->zebra_maps, p->index_type, &b, remain, 0);
262 if (zebra_maps_is_first_in_field(zh->reg->zebra_maps, p->index_type))
264 /* first in field marker */
269 zebra_snippets_appendn(h->snippets, p->seqno, 0, ord,
270 start, last - start);
277 static void snippet_token_add(RecWord *p)
279 struct snip_rec_info *h = p->extractCtrl->handle;
280 ZebraHandle zh = h->zh;
282 if (zebra_maps_is_index(zh->reg->zebra_maps, p->index_type))
284 ZebraExplainInfo zei = zh->reg->zei;
285 int ch = zebraExplain_lookup_attr_str(
286 zei, zinfo_index_category_index, p->index_type, p->index_name);
288 if(zebra_maps_is_complete (h->zh->reg->zebra_maps, p->index_type))
289 snippet_add_complete_field(p, ch);
291 snippet_add_incomplete_field(p, ch);
295 static void snippet_schema_add(
296 struct recExtractCtrl *p, Odr_oid *oid)
301 void extract_snippet(ZebraHandle zh, zebra_snippets *sn,
302 struct ZebraRecStream *stream,
303 RecType rt, void *recTypeClientData)
305 struct recExtractCtrl extractCtrl;
306 struct snip_rec_info info;
309 extractCtrl.stream = stream;
310 extractCtrl.first_record = 1;
311 extractCtrl.init = extract_init;
312 extractCtrl.tokenAdd = snippet_token_add;
313 extractCtrl.schemaAdd = snippet_schema_add;
317 extractCtrl.dh = zh->reg->dh;
321 extractCtrl.handle = &info;
322 extractCtrl.match_criteria[0] = '\0';
323 extractCtrl.staticrank = 0;
324 extractCtrl.action = action_insert;
326 init_extractCtrl(zh, &extractCtrl);
328 extractCtrl.setStoreData = 0;
330 r = (*rt->extract)(recTypeClientData, &extractCtrl);
334 static void searchRecordKey(ZebraHandle zh,
335 zebra_rec_keys_t reckeys,
336 const char *index_name,
337 const char **ws, int ws_length)
341 zinfo_index_category_t cat = zinfo_index_category_index;
343 for (i = 0; i<ws_length; i++)
347 ch = zebraExplain_lookup_attr_str(zh->reg->zei, cat, '0', index_name);
349 ch = zebraExplain_lookup_attr_str(zh->reg->zei, cat, 'p', index_name);
351 ch = zebraExplain_lookup_attr_str(zh->reg->zei, cat, 'w', index_name);
356 if (zebra_rec_keys_rewind(reckeys))
363 while (zebra_rec_keys_read(reckeys, &str, &slen, &key))
365 assert(key.len <= IT_KEY_LEVEL_MAX && key.len > 2);
367 seqno = key.mem[key.len-1];
369 if (key.mem[0] == ch)
375 woff = seqno - startSeq;
376 if (woff >= 0 && woff < ws_length)
383 #define FILE_MATCH_BLANK "\t "
385 static char *get_match_from_spec(ZebraHandle zh,
386 zebra_rec_keys_t reckeys,
387 const char *fname, const char *spec)
389 static char dstBuf[2048]; /* static here ??? */
391 const char *s = spec;
395 for (; *s && strchr(FILE_MATCH_BLANK, *s); s++)
402 char attset_str[64], attname_str[64];
406 for (s++; strchr(FILE_MATCH_BLANK, *s); s++)
408 for (i = 0; *s && *s != ',' && *s != ')' &&
409 !strchr(FILE_MATCH_BLANK, *s); s++)
410 if (i+1 < sizeof(attset_str))
411 attset_str[i++] = *s;
412 attset_str[i] = '\0';
414 for (; strchr(FILE_MATCH_BLANK, *s); s++)
417 strcpy(attname_str, attset_str);
420 for (s++; strchr(FILE_MATCH_BLANK, *s); s++)
422 for (i = 0; *s && *s != ')' &&
423 !strchr(FILE_MATCH_BLANK, *s); s++)
424 if (i+1 < sizeof(attname_str))
425 attname_str[i++] = *s;
426 attname_str[i] = '\0';
429 searchRecordKey(zh, reckeys, attname_str, ws, 32);
433 yaz_log(YLOG_WARN, "Missing ) in match criteria %s in group %s",
434 spec, zh->m_group ? zh->m_group : "none");
439 for (i = 0; i<32; i++)
448 dst += strlen(ws[i]);
452 yaz_log(YLOG_WARN, "Record didn't contain match"
453 " fields in (%s,%s)", attset_str, attname_str);
461 const char *spec_src = NULL;
462 const char *s1 = ++s;
463 while (*s1 && !strchr(FILE_MATCH_BLANK, *s1))
467 if (spec_len > sizeof(special)-1)
468 spec_len = sizeof(special)-1;
469 memcpy(special, s, spec_len);
470 special[spec_len] = '\0';
473 if (!strcmp(special, "group"))
474 spec_src = zh->m_group;
475 else if (!strcmp(special, "database"))
476 spec_src = zh->basenames[0];
477 else if (!strcmp(special, "filename")) {
480 else if (!strcmp(special, "type"))
481 spec_src = zh->m_record_type;
486 strcpy(dst, spec_src);
487 dst += strlen(spec_src);
490 else if (*s == '\"' || *s == '\'')
492 int stopMarker = *s++;
496 while (*s && *s != stopMarker)
498 if (i+1 < sizeof(tmpString))
499 tmpString[i++] = *s++;
504 strcpy(dst, tmpString);
505 dst += strlen(tmpString);
509 yaz_log(YLOG_WARN, "Syntax error in match criteria %s in group %s",
510 spec, zh->m_group ? zh->m_group : "none");
517 yaz_log(YLOG_WARN, "No match criteria for record %s in group %s",
518 fname, zh->m_group ? zh->m_group : "none");
525 struct recordLogInfo {
528 struct recordGroup *rGroup;
531 static void all_matches_add(struct recExtractCtrl *ctrl)
534 extract_init(ctrl, &word);
535 word.index_name = "_ALLRECORDS";
536 word.index_type = 'w';
538 extract_add_index_string(&word, zinfo_index_category_alwaysmatches,
542 ZEBRA_RES zebra_extract_records_stream(ZebraHandle zh,
543 struct ZebraRecStream *stream,
544 enum zebra_recctrl_action_t action,
546 const char *recordType,
548 const char *match_criteria,
551 void *recTypeClientData);
554 ZEBRA_RES zebra_extract_file(ZebraHandle zh, zint *sysno, const char *fname,
557 ZEBRA_RES r = ZEBRA_OK;
562 struct file_read_info *fi = 0;
563 const char *original_record_type = 0;
565 void *recTypeClientData;
566 struct ZebraRecStream stream, *streamp;
568 zebra_init_log_level();
570 if (!zh->m_group || !*zh->m_group)
573 sprintf(gprefix, "%s.", zh->m_group);
575 yaz_log(log_level_extract, "zebra_extract_file %s", fname);
577 /* determine file extension */
579 for (i = strlen(fname); --i >= 0; )
582 else if (fname[i] == '.')
584 strcpy(ext, fname+i+1);
587 /* determine file type - depending on extension */
588 original_record_type = zh->m_record_type;
589 if (!zh->m_record_type)
591 sprintf(ext_res, "%srecordType.%s", gprefix, ext);
592 zh->m_record_type = res_get(zh->res, ext_res);
594 if (!zh->m_record_type)
597 if (zh->records_processed + zh->records_skipped
598 < zh->m_file_verbose_limit)
599 yaz_log(YLOG_LOG, "? %s", fname);
600 zh->records_skipped++;
603 /* determine match criteria */
604 if (!zh->m_record_id)
606 sprintf(ext_res, "%srecordId.%s", gprefix, ext);
607 zh->m_record_id = res_get(zh->res, ext_res);
611 recType_byName(zh->reg->recTypes, zh->res, zh->m_record_type,
612 &recTypeClientData)))
614 yaz_log(YLOG_WARN, "No such record type: %s", zh->m_record_type);
618 switch(recType->version)
623 yaz_log(YLOG_WARN, "Bad filter version: %s", zh->m_record_type);
625 if (sysno && deleteFlag)
634 if (zh->path_reg && !yaz_is_abspath(fname))
636 strcpy(full_rep, zh->path_reg);
637 strcat(full_rep, "/");
638 strcat(full_rep, fname);
641 strcpy(full_rep, fname);
643 if ((fd = open(full_rep, O_BINARY|O_RDONLY)) == -1)
645 yaz_log(YLOG_WARN|YLOG_ERRNO, "open %s", full_rep);
646 zh->m_record_type = original_record_type;
650 zebra_create_stream_fd(streamp, fd, 0);
652 r = zebra_extract_records_stream(zh, streamp,
654 action_delete : action_update,
658 0, /*match_criteria */
660 recType, recTypeClientData);
662 stream.destroy(streamp);
663 zh->m_record_type = original_record_type;
668 If sysno is provided, then it's used to identify the reocord.
669 If not, and match_criteria is provided, then sysno is guessed
670 If not, and a record is provided, then sysno is got from there
674 ZEBRA_RES zebra_buffer_extract_record(ZebraHandle zh,
675 const char *buf, size_t buf_size,
676 enum zebra_recctrl_action_t action,
678 const char *recordType,
680 const char *match_criteria,
683 struct ZebraRecStream stream;
688 if (recordType && *recordType)
690 yaz_log(log_level_extract,
691 "Record type explicitly specified: %s", recordType);
692 recType = recType_byName(zh->reg->recTypes, zh->res, recordType,
697 if (!(zh->m_record_type))
699 yaz_log(YLOG_WARN, "No such record type defined");
702 yaz_log(log_level_extract, "Get record type from rgroup: %s",
704 recType = recType_byName(zh->reg->recTypes, zh->res,
705 zh->m_record_type, &clientData);
706 recordType = zh->m_record_type;
711 yaz_log(YLOG_WARN, "No such record type: %s", recordType);
715 zebra_create_stream_mem(&stream, buf, buf_size);
717 res = zebra_extract_records_stream(zh, &stream,
724 recType, clientData);
725 stream.destroy(&stream);
729 ZEBRA_RES zebra_extract_records_stream(ZebraHandle zh,
730 struct ZebraRecStream *stream,
731 enum zebra_recctrl_action_t action,
733 const char *recordType,
735 const char *match_criteria,
738 void *recTypeClientData)
740 ZEBRA_RES res = ZEBRA_OK;
744 res = zebra_extract_record_stream(zh, stream,
751 recType, recTypeClientData, &more);
766 static WRBUF wrbuf_hex_str(const char *cstr)
769 WRBUF w = wrbuf_alloc();
770 for (i = 0; cstr[i]; i++)
772 if (cstr[i] < ' ' || cstr[i] > 126)
773 wrbuf_printf(w, "\\%02X", cstr[i] & 0xff);
775 wrbuf_putc(w, cstr[i]);
780 ZEBRA_RES zebra_extract_record_stream(ZebraHandle zh,
781 struct ZebraRecStream *stream,
782 enum zebra_recctrl_action_t action,
784 const char *recordType,
786 const char *match_criteria,
789 void *recTypeClientData,
794 RecordAttr *recordAttr;
795 struct recExtractCtrl extractCtrl;
797 const char *matchStr = 0;
799 off_t start_offset = 0, end_offset = 0;
800 const char *pr_fname = fname; /* filename to print .. */
801 int show_progress = zh->records_processed + zh->records_skipped
802 < zh->m_file_verbose_limit ? 1:0;
804 zebra_init_log_level();
807 pr_fname = "<no file>"; /* make it printable if file is omitted */
809 zebra_rec_keys_reset(zh->reg->keys);
810 zebra_rec_keys_reset(zh->reg->sortKeys);
812 if (zebraExplain_curDatabase(zh->reg->zei, zh->basenames[0]))
814 if (zebraExplain_newDatabase(zh->reg->zei, zh->basenames[0],
815 zh->m_explain_database))
821 off_t null_offset = 0;
822 extractCtrl.stream = stream;
824 start_offset = stream->tellf(stream);
826 extractCtrl.first_record = start_offset ? 0 : 1;
828 stream->endf(stream, &null_offset);;
830 extractCtrl.init = extract_init;
831 extractCtrl.tokenAdd = extract_token_add;
832 extractCtrl.schemaAdd = extract_schema_add;
833 extractCtrl.dh = zh->reg->dh;
834 extractCtrl.handle = zh;
835 extractCtrl.match_criteria[0] = '\0';
836 extractCtrl.staticrank = 0;
837 extractCtrl.action = action;
839 init_extractCtrl(zh, &extractCtrl);
841 extract_set_store_data_prepare(&extractCtrl);
843 r = (*recType->extract)(recTypeClientData, &extractCtrl);
845 if (action == action_update)
847 action = extractCtrl.action;
852 case RECCTRL_EXTRACT_EOF:
854 case RECCTRL_EXTRACT_ERROR_GENERIC:
855 /* error occured during extraction ... */
856 yaz_log(YLOG_WARN, "extract error: generic");
858 case RECCTRL_EXTRACT_ERROR_NO_SUCH_FILTER:
859 /* error occured during extraction ... */
860 yaz_log(YLOG_WARN, "extract error: no such filter");
862 case RECCTRL_EXTRACT_SKIP:
864 yaz_log(YLOG_LOG, "skip %s %s " ZINT_FORMAT,
865 recordType, pr_fname, (zint) start_offset);
868 end_offset = stream->endf(stream, 0);
870 stream->seekf(stream, end_offset);
873 case RECCTRL_EXTRACT_OK:
876 yaz_log(YLOG_WARN, "extract error: unknown error: %d", r);
879 end_offset = stream->endf(stream, 0);
881 stream->seekf(stream, end_offset);
883 end_offset = stream->tellf(stream);
885 all_matches_add(&extractCtrl);
887 if (extractCtrl.match_criteria[0])
888 match_criteria = extractCtrl.match_criteria;
896 if (match_criteria && *match_criteria) {
897 matchStr = match_criteria;
899 if (zh->m_record_id && *zh->m_record_id) {
900 matchStr = get_match_from_spec(zh, zh->reg->keys, pr_fname,
904 yaz_log(YLOG_LOG, "error %s %s " ZINT_FORMAT, recordType,
905 pr_fname, (zint) start_offset);
912 int db_ord = zebraExplain_get_database_ord(zh->reg->zei);
913 char *rinfo = dict_lookup_ord(zh->reg->matchDict, db_ord,
917 if (log_level_extract)
919 WRBUF w = wrbuf_hex_str(matchStr);
920 yaz_log(log_level_extract, "matchStr: %s", wrbuf_cstr(w));
925 assert(*rinfo == sizeof(*sysno));
926 memcpy(sysno, rinfo+1, sizeof(*sysno));
930 if (zebra_rec_keys_empty(zh->reg->keys))
932 /* the extraction process returned no information - the record
933 is probably empty - unless flagShowRecords is in use */
941 if (action == action_delete)
943 yaz_log(YLOG_LOG, "delete %s %s " ZINT_FORMAT, recordType,
944 pr_fname, (zint) start_offset);
945 yaz_log(YLOG_WARN, "cannot delete record above (seems new)");
948 else if (action == action_replace)
950 yaz_log(YLOG_LOG, "update %s %s " ZINT_FORMAT, recordType,
951 pr_fname, (zint) start_offset);
952 yaz_log(YLOG_WARN, "cannot update record above (seems new)");
956 yaz_log(YLOG_LOG, "add %s %s " ZINT_FORMAT, recordType, pr_fname,
957 (zint) start_offset);
958 rec = rec_new(zh->reg->records);
962 recordAttr = rec_init_attr(zh->reg->zei, rec);
963 if (extractCtrl.staticrank < 0)
965 yaz_log(YLOG_WARN, "Negative staticrank for record. Set to 0");
966 extractCtrl.staticrank = 0;
971 int db_ord = zebraExplain_get_database_ord(zh->reg->zei);
972 dict_insert_ord(zh->reg->matchDict, db_ord, matchStr,
973 sizeof(*sysno), sysno);
976 extract_flush_sort_keys(zh, *sysno, 1, zh->reg->sortKeys);
978 extract_flush_record_keys2(zh, *sysno,
979 zh->reg->keys, extractCtrl.staticrank,
980 0, recordAttr->staticrank);
982 extract_flush_record_keys(zh, *sysno, 1, zh->reg->keys,
983 extractCtrl.staticrank);
985 recordAttr->staticrank = extractCtrl.staticrank;
986 zh->records_inserted++;
990 /* record already exists */
991 zebra_rec_keys_t delkeys = zebra_rec_keys_open();
992 zebra_rec_keys_t sortKeys = zebra_rec_keys_open();
993 if (action == action_insert)
995 yaz_log(YLOG_LOG, "skipped %s %s " ZINT_FORMAT,
996 recordType, pr_fname, (zint) start_offset);
1001 rec = rec_get(zh->reg->records, *sysno);
1004 recordAttr = rec_init_attr(zh->reg->zei, rec);
1006 /* decrease total size */
1007 zebraExplain_recordBytesIncrement(zh->reg->zei,
1008 - recordAttr->recordSize);
1010 zebra_rec_keys_set_buf(delkeys,
1011 rec->info[recInfo_delKeys],
1012 rec->size[recInfo_delKeys],
1014 zebra_rec_keys_set_buf(sortKeys,
1015 rec->info[recInfo_sortKeys],
1016 rec->size[recInfo_sortKeys],
1019 extract_flush_sort_keys(zh, *sysno, 0, sortKeys);
1021 extract_flush_record_keys(zh, *sysno, 0, delkeys,
1022 recordAttr->staticrank);
1024 if (action == action_delete)
1026 /* record going to be deleted */
1028 extract_flush_record_keys2(zh, *sysno, 0, recordAttr->staticrank,
1029 delkeys, recordAttr->staticrank);
1031 if (zebra_rec_keys_empty(delkeys))
1033 yaz_log(YLOG_LOG, "delete %s %s " ZINT_FORMAT, recordType,
1034 pr_fname, (zint) start_offset);
1035 yaz_log(YLOG_WARN, "cannot delete file above, "
1036 "storeKeys false (3)");
1041 yaz_log(YLOG_LOG, "delete %s %s " ZINT_FORMAT, recordType,
1042 pr_fname, (zint) start_offset);
1043 zh->records_deleted++;
1046 int db_ord = zebraExplain_get_database_ord(zh->reg->zei);
1047 dict_delete_ord(zh->reg->matchDict, db_ord, matchStr);
1049 rec_del(zh->reg->records, &rec);
1051 zebra_rec_keys_close(delkeys);
1052 zebra_rec_keys_close(sortKeys);
1058 { /* update or special_update */
1060 yaz_log(YLOG_LOG, "update %s %s " ZINT_FORMAT, recordType,
1061 pr_fname, (zint) start_offset);
1062 extract_flush_sort_keys(zh, *sysno, 1, zh->reg->sortKeys);
1065 extract_flush_record_keys2(zh, *sysno,
1066 zh->reg->keys, extractCtrl.staticrank,
1067 delkeys, recordAttr->staticrank);
1069 extract_flush_record_keys(zh, *sysno, 1,
1070 zh->reg->keys, extractCtrl.staticrank);
1072 recordAttr->staticrank = extractCtrl.staticrank;
1073 zh->records_updated++;
1075 zebra_rec_keys_close(delkeys);
1076 zebra_rec_keys_close(sortKeys);
1078 /* update file type */
1079 xfree(rec->info[recInfo_fileType]);
1080 rec->info[recInfo_fileType] =
1081 rec_strdup(recordType, &rec->size[recInfo_fileType]);
1083 /* update filename */
1084 xfree(rec->info[recInfo_filename]);
1085 rec->info[recInfo_filename] =
1086 rec_strdup(fname, &rec->size[recInfo_filename]);
1088 /* update delete keys */
1089 xfree(rec->info[recInfo_delKeys]);
1090 if (!zebra_rec_keys_empty(zh->reg->keys) && zh->m_store_keys == 1)
1092 zebra_rec_keys_get_buf(zh->reg->keys,
1093 &rec->info[recInfo_delKeys],
1094 &rec->size[recInfo_delKeys]);
1098 rec->info[recInfo_delKeys] = NULL;
1099 rec->size[recInfo_delKeys] = 0;
1101 /* update sort keys */
1102 xfree(rec->info[recInfo_sortKeys]);
1104 zebra_rec_keys_get_buf(zh->reg->sortKeys,
1105 &rec->info[recInfo_sortKeys],
1106 &rec->size[recInfo_sortKeys]);
1110 recordAttr->recordSize = end_offset - start_offset;
1111 zebraExplain_recordBytesIncrement(zh->reg->zei,
1112 recordAttr->recordSize);
1115 /* set run-number for this record */
1116 recordAttr->runNumber =
1117 zebraExplain_runNumberIncrement(zh->reg->zei, 0);
1119 /* update store data */
1120 xfree(rec->info[recInfo_storeData]);
1122 /* update store data */
1123 if (zh->store_data_buf)
1125 rec->size[recInfo_storeData] = zh->store_data_size;
1126 rec->info[recInfo_storeData] = zh->store_data_buf;
1127 zh->store_data_buf = 0;
1128 recordAttr->recordSize = zh->store_data_size;
1130 else if (zh->m_store_data)
1132 off_t cur_offset = stream->tellf(stream);
1134 rec->size[recInfo_storeData] = recordAttr->recordSize;
1135 rec->info[recInfo_storeData] = (char *)
1136 xmalloc(recordAttr->recordSize);
1137 stream->seekf(stream, start_offset);
1138 stream->readf(stream, rec->info[recInfo_storeData],
1139 recordAttr->recordSize);
1140 stream->seekf(stream, cur_offset);
1144 rec->info[recInfo_storeData] = NULL;
1145 rec->size[recInfo_storeData] = 0;
1147 /* update database name */
1148 xfree(rec->info[recInfo_databaseName]);
1149 rec->info[recInfo_databaseName] =
1150 rec_strdup(zh->basenames[0], &rec->size[recInfo_databaseName]);
1153 recordAttr->recordOffset = start_offset;
1155 /* commit this record */
1156 rec_put(zh->reg->records, &rec);
1161 ZEBRA_RES zebra_extract_explain(void *handle, Record rec, data1_node *n)
1163 ZebraHandle zh = (ZebraHandle) handle;
1164 struct recExtractCtrl extractCtrl;
1166 if (zebraExplain_curDatabase(zh->reg->zei,
1167 rec->info[recInfo_databaseName]))
1170 if (zebraExplain_newDatabase(zh->reg->zei,
1171 rec->info[recInfo_databaseName], 0))
1175 zebra_rec_keys_reset(zh->reg->keys);
1176 zebra_rec_keys_reset(zh->reg->sortKeys);
1178 extractCtrl.init = extract_init;
1179 extractCtrl.tokenAdd = extract_token_add;
1180 extractCtrl.schemaAdd = extract_schema_add;
1181 extractCtrl.dh = zh->reg->dh;
1183 init_extractCtrl(zh, &extractCtrl);
1185 extractCtrl.flagShowRecords = 0;
1186 extractCtrl.match_criteria[0] = '\0';
1187 extractCtrl.staticrank = 0;
1188 extractCtrl.action = action_update;
1190 extractCtrl.handle = handle;
1191 extractCtrl.first_record = 1;
1193 extract_set_store_data_prepare(&extractCtrl);
1196 grs_extract_tree(&extractCtrl, n);
1198 if (rec->size[recInfo_delKeys])
1200 zebra_rec_keys_t delkeys = zebra_rec_keys_open();
1202 zebra_rec_keys_t sortkeys = zebra_rec_keys_open();
1204 zebra_rec_keys_set_buf(delkeys, rec->info[recInfo_delKeys],
1205 rec->size[recInfo_delKeys],
1208 extract_flush_record_keys2(zh, rec->sysno,
1209 zh->reg->keys, 0, delkeys, 0);
1211 extract_flush_record_keys(zh, rec->sysno, 0, delkeys, 0);
1212 extract_flush_record_keys(zh, rec->sysno, 1, zh->reg->keys, 0);
1214 zebra_rec_keys_close(delkeys);
1216 zebra_rec_keys_set_buf(sortkeys, rec->info[recInfo_sortKeys],
1217 rec->size[recInfo_sortKeys],
1220 extract_flush_sort_keys(zh, rec->sysno, 0, sortkeys);
1221 zebra_rec_keys_close(sortkeys);
1226 extract_flush_record_keys2(zh, rec->sysno, zh->reg->keys, 0, 0, 0);
1228 extract_flush_record_keys(zh, rec->sysno, 1, zh->reg->keys, 0);
1231 extract_flush_sort_keys(zh, rec->sysno, 1, zh->reg->sortKeys);
1233 xfree(rec->info[recInfo_delKeys]);
1234 zebra_rec_keys_get_buf(zh->reg->keys,
1235 &rec->info[recInfo_delKeys],
1236 &rec->size[recInfo_delKeys]);
1238 xfree(rec->info[recInfo_sortKeys]);
1239 zebra_rec_keys_get_buf(zh->reg->sortKeys,
1240 &rec->info[recInfo_sortKeys],
1241 &rec->size[recInfo_sortKeys]);
1245 void extract_rec_keys_log(ZebraHandle zh, int is_insert,
1246 zebra_rec_keys_t reckeys,
1249 if (zebra_rec_keys_rewind(reckeys))
1254 NMEM nmem = nmem_create();
1256 while(zebra_rec_keys_read(reckeys, &str, &slen, &key))
1258 char keystr[200]; /* room for zints to print */
1260 int ord = CAST_ZINT_TO_INT(key.mem[0]);
1262 const char *string_index;
1264 zebraExplain_lookup_ord(zh->reg->zei, ord, &index_type,
1265 0/* db */, &string_index);
1267 zebra_term_untrans_iconv(zh, nmem, index_type,
1270 for (i = 0; i<key.len; i++)
1272 sprintf(keystr + strlen(keystr), ZINT_FORMAT " ", key.mem[i]);
1275 if (*str < CHR_BASE_CHAR)
1278 char dst_buf[200]; /* room for special chars */
1280 strcpy(dst_buf , "?");
1282 if (!strcmp(str, ""))
1283 strcpy(dst_buf, "alwaysmatches");
1284 if (!strcmp(str, FIRST_IN_FIELD_STR))
1285 strcpy(dst_buf, "firstinfield");
1286 else if (!strcmp(str, CHR_UNKNOWN))
1287 strcpy(dst_buf, "unknown");
1288 else if (!strcmp(str, CHR_SPACE))
1289 strcpy(dst_buf, "space");
1291 for (i = 0; i<slen; i++)
1293 sprintf(dst_buf + strlen(dst_buf), " %d", str[i] & 0xff);
1295 yaz_log(level, "%s%c %s %s", keystr, index_type,
1296 string_index, dst_buf);
1300 yaz_log(level, "%s%c %s \"%s\"", keystr, index_type,
1301 string_index, dst_term);
1309 void extract_rec_keys_adjust(ZebraHandle zh, int is_insert,
1310 zebra_rec_keys_t reckeys)
1312 ZebraExplainInfo zei = zh->reg->zei;
1316 struct ord_stat *next;
1319 if (zebra_rec_keys_rewind(reckeys))
1321 struct ord_stat *ord_list = 0;
1325 struct it_key key_in;
1326 while(zebra_rec_keys_read(reckeys, &str, &slen, &key_in))
1328 int ord = CAST_ZINT_TO_INT(key_in.mem[0]);
1330 for (p = ord_list; p ; p = p->next)
1338 p = xmalloc(sizeof(*p));
1349 struct ord_stat *p1 = p;
1352 zebraExplain_ord_adjust_occurrences(zei, p->ord, p->no, 1);
1354 zebraExplain_ord_adjust_occurrences(zei, p->ord, - p->no, -1);
1361 void extract_flush_record_keys2(ZebraHandle zh, zint sysno,
1362 zebra_rec_keys_t ins_keys, zint ins_rank,
1363 zebra_rec_keys_t del_keys, zint del_rank)
1365 ZebraExplainInfo zei = zh->reg->zei;
1369 if (!zh->reg->key_block)
1371 int mem = 1024*1024 * atoi( res_get_def( zh->res, "memmax", "8"));
1372 const char *key_tmp_dir = res_get_def(zh->res, "keyTmpDir", ".");
1373 int use_threads = atoi(res_get_def(zh->res, "threads", "1"));
1374 zh->reg->key_block = key_block_create(mem, key_tmp_dir, use_threads);
1379 extract_rec_keys_adjust(zh, 1, ins_keys);
1381 zebraExplain_recordCountIncrement(zei, 1);
1382 zebra_rec_keys_rewind(ins_keys);
1386 extract_rec_keys_adjust(zh, 0, del_keys);
1388 zebraExplain_recordCountIncrement(zei, -1);
1389 zebra_rec_keys_rewind(del_keys);
1395 const char *del_str;
1396 struct it_key del_key_in;
1400 const char *ins_str;
1401 struct it_key ins_key_in;
1405 del = zebra_rec_keys_read(del_keys, &del_str, &del_slen,
1408 ins = zebra_rec_keys_read(ins_keys, &ins_str, &ins_slen,
1411 if (del && ins && ins_rank == del_rank
1412 && !key_compare(&del_key_in, &ins_key_in)
1413 && ins_slen == del_slen && !memcmp(del_str, ins_str, del_slen))
1423 key_block_write(zh->reg->key_block, sysno,
1424 &del_key_in, 0, del_str, del_slen,
1425 del_rank, zh->m_staticrank);
1427 key_block_write(zh->reg->key_block, sysno,
1428 &ins_key_in, 1, ins_str, ins_slen,
1429 ins_rank, zh->m_staticrank);
1431 yaz_log(log_level_extract, "normal=%d optimized=%d", normal, optimized);
1434 void extract_flush_record_keys(ZebraHandle zh, zint sysno, int cmd,
1435 zebra_rec_keys_t reckeys,
1438 ZebraExplainInfo zei = zh->reg->zei;
1440 extract_rec_keys_adjust(zh, cmd, reckeys);
1442 if (log_level_details)
1444 yaz_log(log_level_details, "Keys for record " ZINT_FORMAT " %s",
1445 sysno, cmd ? "insert" : "delete");
1446 extract_rec_keys_log(zh, cmd, reckeys, log_level_details);
1449 if (!zh->reg->key_block)
1451 int mem = 1024*1024 * atoi( res_get_def( zh->res, "memmax", "8"));
1452 const char *key_tmp_dir = res_get_def(zh->res, "keyTmpDir", ".");
1453 int use_threads = atoi(res_get_def(zh->res, "threads", "1"));
1454 zh->reg->key_block = key_block_create(mem, key_tmp_dir, use_threads);
1456 zebraExplain_recordCountIncrement(zei, cmd ? 1 : -1);
1459 yaz_log(YLOG_LOG, "sysno=" ZINT_FORMAT " cmd=%d", sysno, cmd);
1460 print_rec_keys(zh, reckeys);
1462 if (zebra_rec_keys_rewind(reckeys))
1466 struct it_key key_in;
1467 while(zebra_rec_keys_read(reckeys, &str, &slen, &key_in))
1469 key_block_write(zh->reg->key_block, sysno,
1470 &key_in, cmd, str, slen,
1471 staticrank, zh->m_staticrank);
1476 ZEBRA_RES zebra_rec_keys_to_snippets(ZebraHandle zh,
1477 zebra_rec_keys_t reckeys,
1478 zebra_snippets *snippets)
1480 NMEM nmem = nmem_create();
1481 if (zebra_rec_keys_rewind(reckeys))
1486 while (zebra_rec_keys_read(reckeys, &str, &slen, &key))
1493 assert(key.len <= IT_KEY_LEVEL_MAX && key.len > 2);
1494 seqno = key.mem[key.len-1];
1495 ord = CAST_ZINT_TO_INT(key.mem[0]);
1497 zebraExplain_lookup_ord(zh->reg->zei, ord, &index_type,
1498 0/* db */, 0 /* string_index */);
1500 zebra_term_untrans_iconv(zh, nmem, index_type,
1502 zebra_snippets_append(snippets, seqno, 0, ord, dst_term);
1510 void print_rec_keys(ZebraHandle zh, zebra_rec_keys_t reckeys)
1512 yaz_log(YLOG_LOG, "print_rec_keys");
1513 if (zebra_rec_keys_rewind(reckeys))
1518 while (zebra_rec_keys_read(reckeys, &str, &slen, &key))
1520 char dst_buf[IT_MAX_WORD];
1523 int ord = CAST_ZINT_TO_INT(key.mem[0]);
1525 assert(key.len <= IT_KEY_LEVEL_MAX && key.len > 2);
1527 zebraExplain_lookup_ord(zh->reg->zei, ord, &index_type, &db, 0);
1529 seqno = key.mem[key.len-1];
1531 zebra_term_untrans(zh, index_type, dst_buf, str);
1533 yaz_log(YLOG_LOG, "ord=%d seqno=" ZINT_FORMAT
1534 " term=%s", ord, seqno, dst_buf);
1539 static void extract_add_index_string(RecWord *p, zinfo_index_category_t cat,
1540 const char *str, int length)
1543 ZebraHandle zh = p->extractCtrl->handle;
1544 ZebraExplainInfo zei = zh->reg->zei;
1547 ch = zebraExplain_lookup_attr_str(zei, cat, p->index_type, p->index_name);
1549 ch = zebraExplain_add_attr_str(zei, cat, p->index_type, p->index_name);
1553 key.mem[i++] = p->record_id;
1554 key.mem[i++] = p->section_id;
1556 if (zh->m_segment_indexing)
1557 key.mem[i++] = p->segment;
1558 key.mem[i++] = p->seqno;
1561 zebra_rec_keys_write(zh->reg->keys, str, length, &key);
1564 static void extract_add_sort_string(RecWord *p, const char *str, int length)
1567 ZebraHandle zh = p->extractCtrl->handle;
1568 ZebraExplainInfo zei = zh->reg->zei;
1570 zinfo_index_category_t cat = zinfo_index_category_sort;
1572 ch = zebraExplain_lookup_attr_str(zei, cat, p->index_type, p->index_name);
1574 ch = zebraExplain_add_attr_str(zei, cat, p->index_type, p->index_name);
1577 key.mem[1] = p->record_id;
1579 zebra_rec_keys_write(zh->reg->sortKeys, str, length, &key);
1582 static void extract_add_staticrank_string(RecWord *p,
1583 const char *str, int length)
1586 struct recExtractCtrl *ctrl = p->extractCtrl;
1588 if (length > sizeof(valz)-1)
1589 length = sizeof(valz)-1;
1591 memcpy(valz, str, length);
1592 valz[length] = '\0';
1593 ctrl->staticrank = atozint(valz);
1596 static void extract_add_string(RecWord *p, const char *string, int length)
1598 ZebraHandle zh = p->extractCtrl->handle;
1604 if (zebra_maps_is_index(zh->reg->zebra_maps, p->index_type))
1606 extract_add_index_string(p, zinfo_index_category_index,
1608 if (zebra_maps_is_alwaysmatches(zh->reg->zebra_maps, p->index_type))
1611 memcpy(&word, p, sizeof(word));
1614 extract_add_index_string(
1615 &word, zinfo_index_category_alwaysmatches, "", 0);
1618 else if (zebra_maps_is_sort(zh->reg->zebra_maps, p->index_type))
1620 extract_add_sort_string(p, string, length);
1622 else if (zebra_maps_is_staticrank(zh->reg->zebra_maps, p->index_type))
1624 extract_add_staticrank_string(p, string, length);
1628 static void extract_add_incomplete_field(RecWord *p)
1630 ZebraHandle zh = p->extractCtrl->handle;
1631 const char *b = p->term_buf;
1632 int remain = p->term_len;
1634 const char **map = 0;
1637 map = zebra_maps_input(zh->reg->zebra_maps, p->index_type, &b, remain, 0);
1641 char buf[IT_MAX_WORD+1];
1645 while (map && *map && **map == *CHR_SPACE)
1647 remain = p->term_len - (b - p->term_buf);
1649 map = zebra_maps_input(zh->reg->zebra_maps, p->index_type, &b,
1657 while (map && *map && **map != *CHR_SPACE)
1659 const char *cp = *map;
1661 while (i < IT_MAX_WORD && *cp)
1663 remain = p->term_len - (b - p->term_buf);
1665 map = zebra_maps_input(zh->reg->zebra_maps, p->index_type, &b, remain, 0);
1675 if (zebra_maps_is_first_in_field(zh->reg->zebra_maps, p->index_type))
1677 /* first in field marker */
1678 extract_add_string(p, FIRST_IN_FIELD_STR, FIRST_IN_FIELD_LEN);
1682 extract_add_string(p, buf, i);
1687 static void extract_add_complete_field(RecWord *p)
1689 ZebraHandle zh = p->extractCtrl->handle;
1690 const char *b = p->term_buf;
1691 char buf[IT_MAX_WORD+1];
1692 const char **map = 0;
1693 int i = 0, remain = p->term_len;
1696 map = zebra_maps_input(zh->reg->zebra_maps, p->index_type, &b, remain, 1);
1698 while (remain > 0 && i < IT_MAX_WORD)
1700 while (map && *map && **map == *CHR_SPACE)
1702 remain = p->term_len - (b - p->term_buf);
1706 int first = i ? 0 : 1; /* first position */
1707 map = zebra_maps_input(zh->reg->zebra_maps, p->index_type, &b, remain, first);
1715 if (i && i < IT_MAX_WORD)
1716 buf[i++] = *CHR_SPACE;
1717 while (map && *map && **map != *CHR_SPACE)
1719 const char *cp = *map;
1721 if (**map == *CHR_CUT)
1727 if (i >= IT_MAX_WORD)
1729 while (i < IT_MAX_WORD && *cp)
1732 remain = p->term_len - (b - p->term_buf);
1735 map = zebra_maps_input(zh->reg->zebra_maps, p->index_type, &b,
1744 extract_add_string(p, buf, i);
1747 static void extract_token_add(RecWord *p)
1749 ZebraHandle zh = p->extractCtrl->handle;
1752 if (log_level_details)
1754 yaz_log(log_level_details, "extract_token_add "
1755 "type=%c index=%s seqno=" ZINT_FORMAT " s=%.*s",
1756 p->index_type, p->index_name,
1757 p->seqno, p->term_len, p->term_buf);
1759 if ((wrbuf = zebra_replace(zh->reg->zebra_maps, p->index_type, 0,
1760 p->term_buf, p->term_len)))
1762 p->term_buf = wrbuf_buf(wrbuf);
1763 p->term_len = wrbuf_len(wrbuf);
1765 if (zebra_maps_is_complete(zh->reg->zebra_maps, p->index_type))
1766 extract_add_complete_field(p);
1768 extract_add_incomplete_field(p);
1771 static void extract_set_store_data_cb(struct recExtractCtrl *p,
1772 void *buf, size_t sz)
1774 ZebraHandle zh = (ZebraHandle) p->handle;
1776 xfree(zh->store_data_buf);
1777 zh->store_data_buf = 0;
1778 zh->store_data_size = 0;
1781 zh->store_data_buf = xmalloc(sz);
1782 zh->store_data_size = sz;
1783 memcpy(zh->store_data_buf, buf, sz);
1787 static void extract_set_store_data_prepare(struct recExtractCtrl *p)
1789 ZebraHandle zh = (ZebraHandle) p->handle;
1790 xfree(zh->store_data_buf);
1791 zh->store_data_buf = 0;
1792 zh->store_data_size = 0;
1793 p->setStoreData = extract_set_store_data_cb;
1796 static void extract_schema_add(struct recExtractCtrl *p, Odr_oid *oid)
1798 ZebraHandle zh = (ZebraHandle) p->handle;
1799 zebraExplain_addSchema(zh->reg->zei, oid);
1802 void extract_flush_sort_keys(ZebraHandle zh, zint sysno,
1803 int cmd, zebra_rec_keys_t reckeys)
1806 yaz_log(YLOG_LOG, "extract_flush_sort_keys cmd=%d sysno=" ZINT_FORMAT,
1808 extract_rec_keys_log(zh, cmd, reckeys, YLOG_LOG);
1811 if (zebra_rec_keys_rewind(reckeys))
1813 zebra_sort_index_t si = zh->reg->sort_index;
1816 struct it_key key_in;
1818 zebra_sort_sysno(si, sysno);
1820 while (zebra_rec_keys_read(reckeys, &str, &slen, &key_in))
1822 int ord = CAST_ZINT_TO_INT(key_in.mem[0]);
1824 zebra_sort_type(si, ord);
1826 zebra_sort_add(si, str, slen);
1828 zebra_sort_delete(si);
1836 * indent-tabs-mode: nil
1838 * vim: shiftwidth=4 tabstop=8 expandtab