1 /* $Id: extract.c,v 1.258 2007-05-08 14:27:23 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 extract_add_index_string (RecWord *p,
95 zinfo_index_category_t cat,
96 const char *str, int length);
98 static void extract_set_store_data_prepare(struct recExtractCtrl *p);
100 static void extract_init(struct recExtractCtrl *p, RecWord *w)
103 w->index_name = "any";
111 static void searchRecordKey(ZebraHandle zh,
112 zebra_rec_keys_t reckeys,
113 const char *index_name,
114 const char **ws, int ws_length)
118 zinfo_index_category_t cat = zinfo_index_category_index;
120 for (i = 0; i<ws_length; i++)
124 ch = zebraExplain_lookup_attr_str(zh->reg->zei, cat, '0', index_name);
126 ch = zebraExplain_lookup_attr_str(zh->reg->zei, cat, 'p', index_name);
128 ch = zebraExplain_lookup_attr_str(zh->reg->zei, cat, 'w', index_name);
133 if (zebra_rec_keys_rewind(reckeys))
140 while (zebra_rec_keys_read(reckeys, &str, &slen, &key))
142 assert(key.len <= IT_KEY_LEVEL_MAX && key.len > 2);
144 seqno = key.mem[key.len-1];
146 if (key.mem[0] == ch)
152 woff = seqno - startSeq;
153 if (woff >= 0 && woff < ws_length)
160 #define FILE_MATCH_BLANK "\t "
162 static char *get_match_from_spec(ZebraHandle zh,
163 zebra_rec_keys_t reckeys,
164 const char *fname, const char *spec)
166 static char dstBuf[2048]; /* static here ??? */
168 const char *s = spec;
172 for (; *s && strchr(FILE_MATCH_BLANK, *s); s++)
179 char attset_str[64], attname_str[64];
183 for (s++; strchr(FILE_MATCH_BLANK, *s); s++)
185 for (i = 0; *s && *s != ',' && *s != ')' &&
186 !strchr(FILE_MATCH_BLANK, *s); s++)
187 if (i+1 < sizeof(attset_str))
188 attset_str[i++] = *s;
189 attset_str[i] = '\0';
191 for (; strchr(FILE_MATCH_BLANK, *s); s++)
194 strcpy(attname_str, attset_str);
197 for (s++; strchr(FILE_MATCH_BLANK, *s); s++)
199 for (i = 0; *s && *s != ')' &&
200 !strchr(FILE_MATCH_BLANK, *s); s++)
201 if (i+1 < sizeof(attname_str))
202 attname_str[i++] = *s;
203 attname_str[i] = '\0';
206 searchRecordKey (zh, reckeys, attname_str, ws, 32);
210 yaz_log (YLOG_WARN, "Missing ) in match criteria %s in group %s",
211 spec, zh->m_group ? zh->m_group : "none");
216 for (i = 0; i<32; i++)
225 dst += strlen(ws[i]);
229 yaz_log (YLOG_WARN, "Record didn't contain match"
230 " fields in (%s,%s)", attset_str, attname_str);
238 const char *spec_src = NULL;
239 const char *s1 = ++s;
240 while (*s1 && !strchr(FILE_MATCH_BLANK, *s1))
244 if (spec_len > sizeof(special)-1)
245 spec_len = sizeof(special)-1;
246 memcpy (special, s, spec_len);
247 special[spec_len] = '\0';
250 if (!strcmp (special, "group"))
251 spec_src = zh->m_group;
252 else if (!strcmp (special, "database"))
253 spec_src = zh->basenames[0];
254 else if (!strcmp (special, "filename")) {
257 else if (!strcmp (special, "type"))
258 spec_src = zh->m_record_type;
263 strcpy (dst, spec_src);
264 dst += strlen (spec_src);
267 else if (*s == '\"' || *s == '\'')
269 int stopMarker = *s++;
273 while (*s && *s != stopMarker)
275 if (i+1 < sizeof(tmpString))
276 tmpString[i++] = *s++;
281 strcpy (dst, tmpString);
282 dst += strlen (tmpString);
286 yaz_log (YLOG_WARN, "Syntax error in match criteria %s in group %s",
287 spec, zh->m_group ? zh->m_group : "none");
294 yaz_log (YLOG_WARN, "No match criteria for record %s in group %s",
295 fname, zh->m_group ? zh->m_group : "none");
302 struct recordLogInfo {
305 struct recordGroup *rGroup;
308 static void init_extractCtrl(ZebraHandle zh, struct recExtractCtrl *ctrl)
311 for (i = 0; i<256; i++)
313 if (zebra_maps_is_positioned(zh->reg->zebra_maps, i))
318 ctrl->flagShowRecords = !zh->m_flag_rw;
321 static void all_matches_add(struct recExtractCtrl *ctrl)
324 extract_init(ctrl, &word);
325 word.index_name = "_ALLRECORDS";
326 word.index_type = 'w';
328 extract_add_index_string (&word, zinfo_index_category_alwaysmatches,
332 ZEBRA_RES zebra_extract_records_stream(ZebraHandle zh,
333 struct ZebraRecStream *stream,
334 enum zebra_recctrl_action_t action,
336 const char *recordType,
338 const char *match_criteria,
341 void *recTypeClientData);
344 ZEBRA_RES zebra_extract_file(ZebraHandle zh, zint *sysno, const char *fname,
347 ZEBRA_RES r = ZEBRA_OK;
352 struct file_read_info *fi = 0;
353 const char *original_record_type = 0;
355 void *recTypeClientData;
356 struct ZebraRecStream stream, *streamp;
358 zebra_init_log_level();
360 if (!zh->m_group || !*zh->m_group)
363 sprintf (gprefix, "%s.", zh->m_group);
365 yaz_log(log_level_extract, "zebra_extract_file %s", fname);
367 /* determine file extension */
369 for (i = strlen(fname); --i >= 0; )
372 else if (fname[i] == '.')
374 strcpy (ext, fname+i+1);
377 /* determine file type - depending on extension */
378 original_record_type = zh->m_record_type;
379 if (!zh->m_record_type)
381 sprintf (ext_res, "%srecordType.%s", gprefix, ext);
382 zh->m_record_type = res_get (zh->res, ext_res);
384 if (!zh->m_record_type)
387 if (zh->records_processed + zh->records_skipped
388 < zh->m_file_verbose_limit)
389 yaz_log (YLOG_LOG, "? %s", fname);
390 zh->records_skipped++;
393 /* determine match criteria */
394 if (!zh->m_record_id)
396 sprintf (ext_res, "%srecordId.%s", gprefix, ext);
397 zh->m_record_id = res_get (zh->res, ext_res);
401 recType_byName (zh->reg->recTypes, zh->res, zh->m_record_type,
402 &recTypeClientData)))
404 yaz_log(YLOG_WARN, "No such record type: %s", zh->m_record_type);
408 switch(recType->version)
413 yaz_log(YLOG_WARN, "Bad filter version: %s", zh->m_record_type);
415 if (sysno && deleteFlag)
424 if (zh->path_reg && !yaz_is_abspath (fname))
426 strcpy (full_rep, zh->path_reg);
427 strcat (full_rep, "/");
428 strcat (full_rep, fname);
431 strcpy (full_rep, fname);
433 if ((fd = open (full_rep, O_BINARY|O_RDONLY)) == -1)
435 yaz_log (YLOG_WARN|YLOG_ERRNO, "open %s", full_rep);
436 zh->m_record_type = original_record_type;
440 zebra_create_stream_fd(streamp, fd, 0);
442 r = zebra_extract_records_stream(zh, streamp,
444 action_delete : action_update,
448 0, /*match_criteria */
450 recType, recTypeClientData);
452 stream.destroy(streamp);
453 zh->m_record_type = original_record_type;
458 If sysno is provided, then it's used to identify the reocord.
459 If not, and match_criteria is provided, then sysno is guessed
460 If not, and a record is provided, then sysno is got from there
464 ZEBRA_RES zebra_buffer_extract_record(ZebraHandle zh,
465 const char *buf, size_t buf_size,
466 enum zebra_recctrl_action_t action,
468 const char *recordType,
470 const char *match_criteria,
473 struct ZebraRecStream stream;
478 if (recordType && *recordType)
480 yaz_log(log_level_extract,
481 "Record type explicitly specified: %s", recordType);
482 recType = recType_byName (zh->reg->recTypes, zh->res, recordType,
487 if (!(zh->m_record_type))
489 yaz_log (YLOG_WARN, "No such record type defined");
492 yaz_log(log_level_extract, "Get record type from rgroup: %s",
494 recType = recType_byName (zh->reg->recTypes, zh->res,
495 zh->m_record_type, &clientData);
496 recordType = zh->m_record_type;
501 yaz_log (YLOG_WARN, "No such record type: %s", recordType);
505 zebra_create_stream_mem(&stream, buf, buf_size);
507 res = zebra_extract_records_stream(zh, &stream,
514 recType, clientData);
515 stream.destroy(&stream);
519 ZEBRA_RES zebra_extract_records_stream(ZebraHandle zh,
520 struct ZebraRecStream *stream,
521 enum zebra_recctrl_action_t action,
523 const char *recordType,
525 const char *match_criteria,
528 void *recTypeClientData)
530 ZEBRA_RES res = ZEBRA_OK;
534 res = zebra_extract_record_stream(zh, stream,
541 recType, recTypeClientData, &more);
556 static WRBUF wrbuf_hex_str(const char *cstr)
559 WRBUF w = wrbuf_alloc();
560 for (i = 0; cstr[i]; i++)
562 if (cstr[i] < ' ' || cstr[i] > 126)
563 wrbuf_printf(w, "\\%02X", cstr[i] & 0xff);
565 wrbuf_putc(w, cstr[i]);
570 ZEBRA_RES zebra_extract_record_stream(ZebraHandle zh,
571 struct ZebraRecStream *stream,
572 enum zebra_recctrl_action_t action,
574 const char *recordType,
576 const char *match_criteria,
579 void *recTypeClientData,
584 RecordAttr *recordAttr;
585 struct recExtractCtrl extractCtrl;
587 const char *matchStr = 0;
589 off_t start_offset = 0, end_offset = 0;
590 const char *pr_fname = fname; /* filename to print .. */
591 int show_progress = zh->records_processed + zh->records_skipped
592 < zh->m_file_verbose_limit ? 1:0;
594 zebra_init_log_level();
597 pr_fname = "<no file>"; /* make it printable if file is omitted */
599 zebra_rec_keys_reset(zh->reg->keys);
600 zebra_rec_keys_reset(zh->reg->sortKeys);
602 if (zebraExplain_curDatabase (zh->reg->zei, zh->basenames[0]))
604 if (zebraExplain_newDatabase (zh->reg->zei, zh->basenames[0],
605 zh->m_explain_database))
611 off_t null_offset = 0;
612 extractCtrl.stream = stream;
614 start_offset = stream->tellf(stream);
616 extractCtrl.first_record = start_offset ? 0 : 1;
618 stream->endf(stream, &null_offset);;
620 extractCtrl.init = extract_init;
621 extractCtrl.tokenAdd = extract_token_add;
622 extractCtrl.schemaAdd = extract_schema_add;
623 extractCtrl.dh = zh->reg->dh;
624 extractCtrl.handle = zh;
625 extractCtrl.match_criteria[0] = '\0';
626 extractCtrl.staticrank = 0;
627 extractCtrl.action = action;
629 init_extractCtrl(zh, &extractCtrl);
631 extract_set_store_data_prepare(&extractCtrl);
633 r = (*recType->extract)(recTypeClientData, &extractCtrl);
635 if (action == action_update)
637 action = extractCtrl.action;
642 case RECCTRL_EXTRACT_EOF:
644 case RECCTRL_EXTRACT_ERROR_GENERIC:
645 /* error occured during extraction ... */
646 yaz_log (YLOG_WARN, "extract error: generic");
648 case RECCTRL_EXTRACT_ERROR_NO_SUCH_FILTER:
649 /* error occured during extraction ... */
650 yaz_log (YLOG_WARN, "extract error: no such filter");
652 case RECCTRL_EXTRACT_SKIP:
654 yaz_log (YLOG_LOG, "skip %s %s " ZINT_FORMAT,
655 recordType, pr_fname, (zint) start_offset);
658 end_offset = stream->endf(stream, 0);
660 stream->seekf(stream, end_offset);
663 case RECCTRL_EXTRACT_OK:
666 yaz_log (YLOG_WARN, "extract error: unknown error: %d", r);
669 end_offset = stream->endf(stream, 0);
671 stream->seekf(stream, end_offset);
673 end_offset = stream->tellf(stream);
675 all_matches_add(&extractCtrl);
677 if (extractCtrl.match_criteria[0])
678 match_criteria = extractCtrl.match_criteria;
686 if (match_criteria && *match_criteria) {
687 matchStr = match_criteria;
689 if (zh->m_record_id && *zh->m_record_id) {
690 matchStr = get_match_from_spec(zh, zh->reg->keys, pr_fname,
694 yaz_log (YLOG_LOG, "error %s %s " ZINT_FORMAT, recordType,
695 pr_fname, (zint) start_offset);
702 int db_ord = zebraExplain_get_database_ord(zh->reg->zei);
703 char *rinfo = dict_lookup_ord(zh->reg->matchDict, db_ord,
707 if (log_level_extract)
709 WRBUF w = wrbuf_hex_str(matchStr);
710 yaz_log(log_level_extract, "matchStr: %s", wrbuf_cstr(w));
715 assert(*rinfo == sizeof(*sysno));
716 memcpy (sysno, rinfo+1, sizeof(*sysno));
720 if (zebra_rec_keys_empty(zh->reg->keys))
722 /* the extraction process returned no information - the record
723 is probably empty - unless flagShowRecords is in use */
731 if (action == action_delete)
733 yaz_log (YLOG_LOG, "delete %s %s " ZINT_FORMAT, recordType,
734 pr_fname, (zint) start_offset);
735 yaz_log (YLOG_WARN, "cannot delete record above (seems new)");
738 else if (action == action_replace)
740 yaz_log (YLOG_LOG, "update %s %s " ZINT_FORMAT, recordType,
741 pr_fname, (zint) start_offset);
742 yaz_log (YLOG_WARN, "cannot update record above (seems new)");
746 yaz_log (YLOG_LOG, "add %s %s " ZINT_FORMAT, recordType, pr_fname,
747 (zint) start_offset);
748 rec = rec_new (zh->reg->records);
752 recordAttr = rec_init_attr (zh->reg->zei, rec);
753 if (extractCtrl.staticrank < 0)
755 yaz_log(YLOG_WARN, "Negative staticrank for record. Set to 0");
756 extractCtrl.staticrank = 0;
761 int db_ord = zebraExplain_get_database_ord(zh->reg->zei);
762 dict_insert_ord(zh->reg->matchDict, db_ord, matchStr,
763 sizeof(*sysno), sysno);
766 extract_flush_sort_keys(zh, *sysno, 1, zh->reg->sortKeys);
768 extract_flush_record_keys2(zh, *sysno,
769 zh->reg->keys, extractCtrl.staticrank,
770 0, recordAttr->staticrank);
772 extract_flush_record_keys(zh, *sysno, 1, zh->reg->keys,
773 extractCtrl.staticrank);
775 recordAttr->staticrank = extractCtrl.staticrank;
776 zh->records_inserted++;
780 /* record already exists */
781 zebra_rec_keys_t delkeys = zebra_rec_keys_open();
782 zebra_rec_keys_t sortKeys = zebra_rec_keys_open();
783 if (action == action_insert)
785 yaz_log (YLOG_LOG, "skipped %s %s " ZINT_FORMAT,
786 recordType, pr_fname, (zint) start_offset);
791 rec = rec_get (zh->reg->records, *sysno);
794 recordAttr = rec_init_attr (zh->reg->zei, rec);
796 /* decrease total size */
797 zebraExplain_recordBytesIncrement (zh->reg->zei,
798 - recordAttr->recordSize);
800 zebra_rec_keys_set_buf(delkeys,
801 rec->info[recInfo_delKeys],
802 rec->size[recInfo_delKeys],
804 zebra_rec_keys_set_buf(sortKeys,
805 rec->info[recInfo_sortKeys],
806 rec->size[recInfo_sortKeys],
809 extract_flush_sort_keys(zh, *sysno, 0, sortKeys);
811 extract_flush_record_keys(zh, *sysno, 0, delkeys,
812 recordAttr->staticrank);
814 if (action == action_delete)
816 /* record going to be deleted */
818 extract_flush_record_keys2(zh, *sysno, 0, recordAttr->staticrank,
819 delkeys, recordAttr->staticrank);
821 if (zebra_rec_keys_empty(delkeys))
823 yaz_log(YLOG_LOG, "delete %s %s " ZINT_FORMAT, recordType,
824 pr_fname, (zint) start_offset);
825 yaz_log(YLOG_WARN, "cannot delete file above, "
826 "storeKeys false (3)");
831 yaz_log(YLOG_LOG, "delete %s %s " ZINT_FORMAT, recordType,
832 pr_fname, (zint) start_offset);
833 zh->records_deleted++;
836 int db_ord = zebraExplain_get_database_ord(zh->reg->zei);
837 dict_delete_ord(zh->reg->matchDict, db_ord, matchStr);
839 rec_del (zh->reg->records, &rec);
841 zebra_rec_keys_close(delkeys);
842 zebra_rec_keys_close(sortKeys);
848 { /* update or special_update */
850 yaz_log(YLOG_LOG, "update %s %s " ZINT_FORMAT, recordType,
851 pr_fname, (zint) start_offset);
852 extract_flush_sort_keys(zh, *sysno, 1, zh->reg->sortKeys);
855 extract_flush_record_keys2(zh, *sysno,
856 zh->reg->keys, extractCtrl.staticrank,
857 delkeys, recordAttr->staticrank);
859 extract_flush_record_keys(zh, *sysno, 1,
860 zh->reg->keys, extractCtrl.staticrank);
862 recordAttr->staticrank = extractCtrl.staticrank;
863 zh->records_updated++;
865 zebra_rec_keys_close(delkeys);
866 zebra_rec_keys_close(sortKeys);
868 /* update file type */
869 xfree (rec->info[recInfo_fileType]);
870 rec->info[recInfo_fileType] =
871 rec_strdup (recordType, &rec->size[recInfo_fileType]);
873 /* update filename */
874 xfree (rec->info[recInfo_filename]);
875 rec->info[recInfo_filename] =
876 rec_strdup (fname, &rec->size[recInfo_filename]);
878 /* update delete keys */
879 xfree (rec->info[recInfo_delKeys]);
880 if (!zebra_rec_keys_empty(zh->reg->keys) && zh->m_store_keys == 1)
882 zebra_rec_keys_get_buf(zh->reg->keys,
883 &rec->info[recInfo_delKeys],
884 &rec->size[recInfo_delKeys]);
888 rec->info[recInfo_delKeys] = NULL;
889 rec->size[recInfo_delKeys] = 0;
891 /* update sort keys */
892 xfree (rec->info[recInfo_sortKeys]);
894 zebra_rec_keys_get_buf(zh->reg->sortKeys,
895 &rec->info[recInfo_sortKeys],
896 &rec->size[recInfo_sortKeys]);
900 recordAttr->recordSize = end_offset - start_offset;
901 zebraExplain_recordBytesIncrement(zh->reg->zei,
902 recordAttr->recordSize);
905 /* set run-number for this record */
906 recordAttr->runNumber =
907 zebraExplain_runNumberIncrement (zh->reg->zei, 0);
909 /* update store data */
910 xfree (rec->info[recInfo_storeData]);
912 /* update store data */
913 if (zh->store_data_buf)
915 rec->size[recInfo_storeData] = zh->store_data_size;
916 rec->info[recInfo_storeData] = zh->store_data_buf;
917 zh->store_data_buf = 0;
918 recordAttr->recordSize = zh->store_data_size;
920 else if (zh->m_store_data)
922 off_t cur_offset = stream->tellf(stream);
924 rec->size[recInfo_storeData] = recordAttr->recordSize;
925 rec->info[recInfo_storeData] = (char *)
926 xmalloc (recordAttr->recordSize);
927 stream->seekf(stream, start_offset);
928 stream->readf(stream, rec->info[recInfo_storeData],
929 recordAttr->recordSize);
930 stream->seekf(stream, cur_offset);
934 rec->info[recInfo_storeData] = NULL;
935 rec->size[recInfo_storeData] = 0;
937 /* update database name */
938 xfree (rec->info[recInfo_databaseName]);
939 rec->info[recInfo_databaseName] =
940 rec_strdup (zh->basenames[0], &rec->size[recInfo_databaseName]);
943 recordAttr->recordOffset = start_offset;
945 /* commit this record */
946 rec_put (zh->reg->records, &rec);
951 ZEBRA_RES zebra_extract_explain(void *handle, Record rec, data1_node *n)
953 ZebraHandle zh = (ZebraHandle) handle;
954 struct recExtractCtrl extractCtrl;
956 if (zebraExplain_curDatabase (zh->reg->zei,
957 rec->info[recInfo_databaseName]))
960 if (zebraExplain_newDatabase (zh->reg->zei,
961 rec->info[recInfo_databaseName], 0))
965 zebra_rec_keys_reset(zh->reg->keys);
966 zebra_rec_keys_reset(zh->reg->sortKeys);
968 extractCtrl.init = extract_init;
969 extractCtrl.tokenAdd = extract_token_add;
970 extractCtrl.schemaAdd = extract_schema_add;
971 extractCtrl.dh = zh->reg->dh;
973 init_extractCtrl(zh, &extractCtrl);
975 extractCtrl.flagShowRecords = 0;
976 extractCtrl.match_criteria[0] = '\0';
977 extractCtrl.staticrank = 0;
978 extractCtrl.action = action_update;
980 extractCtrl.handle = handle;
981 extractCtrl.first_record = 1;
983 extract_set_store_data_prepare(&extractCtrl);
986 grs_extract_tree(&extractCtrl, n);
988 if (rec->size[recInfo_delKeys])
990 zebra_rec_keys_t delkeys = zebra_rec_keys_open();
992 zebra_rec_keys_t sortkeys = zebra_rec_keys_open();
994 zebra_rec_keys_set_buf(delkeys, rec->info[recInfo_delKeys],
995 rec->size[recInfo_delKeys],
998 extract_flush_record_keys2(zh, rec->sysno,
999 zh->reg->keys, 0, delkeys, 0);
1001 extract_flush_record_keys(zh, rec->sysno, 0, delkeys, 0);
1002 extract_flush_record_keys(zh, rec->sysno, 1, zh->reg->keys, 0);
1004 zebra_rec_keys_close(delkeys);
1006 zebra_rec_keys_set_buf(sortkeys, rec->info[recInfo_sortKeys],
1007 rec->size[recInfo_sortKeys],
1010 extract_flush_sort_keys(zh, rec->sysno, 0, sortkeys);
1011 zebra_rec_keys_close(sortkeys);
1016 extract_flush_record_keys2(zh, rec->sysno, zh->reg->keys, 0, 0, 0);
1018 extract_flush_record_keys(zh, rec->sysno, 1, zh->reg->keys, 0);
1021 extract_flush_sort_keys(zh, rec->sysno, 1, zh->reg->sortKeys);
1023 xfree (rec->info[recInfo_delKeys]);
1024 zebra_rec_keys_get_buf(zh->reg->keys,
1025 &rec->info[recInfo_delKeys],
1026 &rec->size[recInfo_delKeys]);
1028 xfree (rec->info[recInfo_sortKeys]);
1029 zebra_rec_keys_get_buf(zh->reg->sortKeys,
1030 &rec->info[recInfo_sortKeys],
1031 &rec->size[recInfo_sortKeys]);
1035 void extract_rec_keys_log(ZebraHandle zh, int is_insert,
1036 zebra_rec_keys_t reckeys,
1039 if (zebra_rec_keys_rewind(reckeys))
1044 NMEM nmem = nmem_create();
1046 while(zebra_rec_keys_read(reckeys, &str, &slen, &key))
1048 char keystr[200]; /* room for zints to print */
1050 int ord = CAST_ZINT_TO_INT(key.mem[0]);
1052 const char *string_index;
1054 zebraExplain_lookup_ord(zh->reg->zei, ord, &index_type,
1055 0/* db */, &string_index);
1057 zebra_term_untrans_iconv(zh, nmem, index_type,
1060 for (i = 0; i<key.len; i++)
1062 sprintf(keystr + strlen(keystr), ZINT_FORMAT " ", key.mem[i]);
1065 if (*str < CHR_BASE_CHAR)
1068 char dst_buf[200]; /* room for special chars */
1070 strcpy(dst_buf , "?");
1072 if (!strcmp(str, ""))
1073 strcpy(dst_buf, "alwaysmatches");
1074 if (!strcmp(str, FIRST_IN_FIELD_STR))
1075 strcpy(dst_buf, "firstinfield");
1076 else if (!strcmp(str, CHR_UNKNOWN))
1077 strcpy(dst_buf, "unknown");
1078 else if (!strcmp(str, CHR_SPACE))
1079 strcpy(dst_buf, "space");
1081 for (i = 0; i<slen; i++)
1083 sprintf(dst_buf + strlen(dst_buf), " %d", str[i] & 0xff);
1085 yaz_log(level, "%s%c %s %s", keystr, index_type,
1086 string_index, dst_buf);
1090 yaz_log(level, "%s%c %s \"%s\"", keystr, index_type,
1091 string_index, dst_term);
1099 void extract_rec_keys_adjust(ZebraHandle zh, int is_insert,
1100 zebra_rec_keys_t reckeys)
1102 ZebraExplainInfo zei = zh->reg->zei;
1106 struct ord_stat *next;
1109 if (zebra_rec_keys_rewind(reckeys))
1111 struct ord_stat *ord_list = 0;
1115 struct it_key key_in;
1116 while(zebra_rec_keys_read(reckeys, &str, &slen, &key_in))
1118 int ord = CAST_ZINT_TO_INT(key_in.mem[0]);
1120 for (p = ord_list; p ; p = p->next)
1128 p = xmalloc(sizeof(*p));
1139 struct ord_stat *p1 = p;
1142 zebraExplain_ord_adjust_occurrences(zei, p->ord, p->no, 1);
1144 zebraExplain_ord_adjust_occurrences(zei, p->ord, - p->no, -1);
1151 void extract_flush_record_keys2(ZebraHandle zh, zint sysno,
1152 zebra_rec_keys_t ins_keys, zint ins_rank,
1153 zebra_rec_keys_t del_keys, zint del_rank)
1155 ZebraExplainInfo zei = zh->reg->zei;
1159 if (!zh->reg->key_block)
1161 int mem = 1024*1024 * atoi( res_get_def( zh->res, "memmax", "8"));
1162 const char *key_tmp_dir = res_get_def (zh->res, "keyTmpDir", ".");
1163 int use_threads = atoi(res_get_def (zh->res, "threads", "1"));
1164 zh->reg->key_block = key_block_create(mem, key_tmp_dir, use_threads);
1169 extract_rec_keys_adjust(zh, 1, ins_keys);
1171 zebraExplain_recordCountIncrement (zei, 1);
1172 zebra_rec_keys_rewind(ins_keys);
1176 extract_rec_keys_adjust(zh, 0, del_keys);
1178 zebraExplain_recordCountIncrement (zei, -1);
1179 zebra_rec_keys_rewind(del_keys);
1185 const char *del_str;
1186 struct it_key del_key_in;
1190 const char *ins_str;
1191 struct it_key ins_key_in;
1195 del = zebra_rec_keys_read(del_keys, &del_str, &del_slen,
1198 ins = zebra_rec_keys_read(ins_keys, &ins_str, &ins_slen,
1201 if (del && ins && ins_rank == del_rank
1202 && !key_compare(&del_key_in, &ins_key_in)
1203 && ins_slen == del_slen && !memcmp(del_str, ins_str, del_slen))
1213 key_block_write(zh->reg->key_block, sysno,
1214 &del_key_in, 0, del_str, del_slen,
1215 del_rank, zh->m_staticrank);
1217 key_block_write(zh->reg->key_block, sysno,
1218 &ins_key_in, 1, ins_str, ins_slen,
1219 ins_rank, zh->m_staticrank);
1221 yaz_log(log_level_extract, "normal=%d optimized=%d", normal, optimized);
1224 void extract_flush_record_keys(ZebraHandle zh, zint sysno, int cmd,
1225 zebra_rec_keys_t reckeys,
1228 ZebraExplainInfo zei = zh->reg->zei;
1230 extract_rec_keys_adjust(zh, cmd, reckeys);
1232 if (log_level_details)
1234 yaz_log(log_level_details, "Keys for record " ZINT_FORMAT " %s",
1235 sysno, cmd ? "insert" : "delete");
1236 extract_rec_keys_log(zh, cmd, reckeys, log_level_details);
1239 if (!zh->reg->key_block)
1241 int mem = 1024*1024 * atoi( res_get_def( zh->res, "memmax", "8"));
1242 const char *key_tmp_dir = res_get_def (zh->res, "keyTmpDir", ".");
1243 int use_threads = atoi(res_get_def (zh->res, "threads", "1"));
1244 zh->reg->key_block = key_block_create(mem, key_tmp_dir, use_threads);
1246 zebraExplain_recordCountIncrement (zei, cmd ? 1 : -1);
1249 yaz_log(YLOG_LOG, "sysno=" ZINT_FORMAT " cmd=%d", sysno, cmd);
1250 print_rec_keys(zh, reckeys);
1252 if (zebra_rec_keys_rewind(reckeys))
1256 struct it_key key_in;
1257 while(zebra_rec_keys_read(reckeys, &str, &slen, &key_in))
1259 key_block_write(zh->reg->key_block, sysno,
1260 &key_in, cmd, str, slen,
1261 staticrank, zh->m_staticrank);
1266 ZEBRA_RES zebra_rec_keys_to_snippets(ZebraHandle zh,
1267 zebra_rec_keys_t reckeys,
1268 zebra_snippets *snippets)
1270 NMEM nmem = nmem_create();
1271 if (zebra_rec_keys_rewind(reckeys))
1276 while (zebra_rec_keys_read(reckeys, &str, &slen, &key))
1283 assert(key.len <= IT_KEY_LEVEL_MAX && key.len > 2);
1284 seqno = key.mem[key.len-1];
1285 ord = CAST_ZINT_TO_INT(key.mem[0]);
1287 zebraExplain_lookup_ord(zh->reg->zei, ord, &index_type,
1288 0/* db */, 0 /* string_index */);
1290 zebra_term_untrans_iconv(zh, nmem, index_type,
1292 zebra_snippets_append(snippets, seqno, ord, dst_term);
1300 void print_rec_keys(ZebraHandle zh, zebra_rec_keys_t reckeys)
1302 yaz_log(YLOG_LOG, "print_rec_keys");
1303 if (zebra_rec_keys_rewind(reckeys))
1308 while (zebra_rec_keys_read(reckeys, &str, &slen, &key))
1310 char dst_buf[IT_MAX_WORD];
1313 int ord = CAST_ZINT_TO_INT(key.mem[0]);
1315 assert(key.len <= IT_KEY_LEVEL_MAX && key.len > 2);
1317 zebraExplain_lookup_ord(zh->reg->zei, ord, &index_type, &db, 0);
1319 seqno = key.mem[key.len-1];
1321 zebra_term_untrans(zh, index_type, dst_buf, str);
1323 yaz_log(YLOG_LOG, "ord=%d seqno=" ZINT_FORMAT
1324 " term=%s", ord, seqno, dst_buf);
1329 static void extract_add_index_string(RecWord *p, zinfo_index_category_t cat,
1330 const char *str, int length)
1333 ZebraHandle zh = p->extractCtrl->handle;
1334 ZebraExplainInfo zei = zh->reg->zei;
1337 ch = zebraExplain_lookup_attr_str(zei, cat, p->index_type, p->index_name);
1339 ch = zebraExplain_add_attr_str(zei, cat, p->index_type, p->index_name);
1343 key.mem[i++] = p->record_id;
1344 key.mem[i++] = p->section_id;
1346 if (zh->m_segment_indexing)
1347 key.mem[i++] = p->segment;
1348 key.mem[i++] = p->seqno;
1351 zebra_rec_keys_write(zh->reg->keys, str, length, &key);
1354 static void extract_add_sort_string(RecWord *p, const char *str, int length)
1357 ZebraHandle zh = p->extractCtrl->handle;
1358 ZebraExplainInfo zei = zh->reg->zei;
1360 zinfo_index_category_t cat = zinfo_index_category_sort;
1362 ch = zebraExplain_lookup_attr_str(zei, cat, p->index_type, p->index_name);
1364 ch = zebraExplain_add_attr_str(zei, cat, p->index_type, p->index_name);
1367 key.mem[1] = p->record_id;
1369 zebra_rec_keys_write(zh->reg->sortKeys, str, length, &key);
1372 static void extract_add_staticrank_string(RecWord *p,
1373 const char *str, int length)
1376 struct recExtractCtrl *ctrl = p->extractCtrl;
1378 if (length > sizeof(valz)-1)
1379 length = sizeof(valz)-1;
1381 memcpy(valz, str, length);
1382 valz[length] = '\0';
1383 ctrl->staticrank = atozint(valz);
1386 static void extract_add_string(RecWord *p, const char *string, int length)
1388 ZebraHandle zh = p->extractCtrl->handle;
1389 assert (length > 0);
1394 if (zebra_maps_is_index(zh->reg->zebra_maps, p->index_type))
1396 extract_add_index_string(p, zinfo_index_category_index,
1398 if (zebra_maps_is_alwaysmatches(zh->reg->zebra_maps, p->index_type))
1401 memcpy(&word, p, sizeof(word));
1404 extract_add_index_string(
1405 &word, zinfo_index_category_alwaysmatches, "", 0);
1408 else if (zebra_maps_is_sort(zh->reg->zebra_maps, p->index_type))
1410 extract_add_sort_string(p, string, length);
1412 else if (zebra_maps_is_staticrank(zh->reg->zebra_maps, p->index_type))
1414 extract_add_staticrank_string(p, string, length);
1418 static void extract_add_incomplete_field(RecWord *p)
1420 ZebraHandle zh = p->extractCtrl->handle;
1421 const char *b = p->term_buf;
1422 int remain = p->term_len;
1424 const char **map = 0;
1427 map = zebra_maps_input(zh->reg->zebra_maps, p->index_type, &b, remain, 0);
1431 char buf[IT_MAX_WORD+1];
1435 while (map && *map && **map == *CHR_SPACE)
1437 remain = p->term_len - (b - p->term_buf);
1439 map = zebra_maps_input(zh->reg->zebra_maps, p->index_type, &b,
1447 while (map && *map && **map != *CHR_SPACE)
1449 const char *cp = *map;
1451 while (i < IT_MAX_WORD && *cp)
1453 remain = p->term_len - (b - p->term_buf);
1455 map = zebra_maps_input(zh->reg->zebra_maps, p->index_type, &b, remain, 0);
1465 if (zebra_maps_is_first_in_field(zh->reg->zebra_maps, p->index_type))
1467 /* first in field marker */
1468 extract_add_string(p, FIRST_IN_FIELD_STR, FIRST_IN_FIELD_LEN);
1472 extract_add_string (p, buf, i);
1477 static void extract_add_complete_field (RecWord *p)
1479 ZebraHandle zh = p->extractCtrl->handle;
1480 const char *b = p->term_buf;
1481 char buf[IT_MAX_WORD+1];
1482 const char **map = 0;
1483 int i = 0, remain = p->term_len;
1486 map = zebra_maps_input (zh->reg->zebra_maps, p->index_type, &b, remain, 1);
1488 while (remain > 0 && i < IT_MAX_WORD)
1490 while (map && *map && **map == *CHR_SPACE)
1492 remain = p->term_len - (b - p->term_buf);
1496 int first = i ? 0 : 1; /* first position */
1497 map = zebra_maps_input(zh->reg->zebra_maps, p->index_type, &b, remain, first);
1505 if (i && i < IT_MAX_WORD)
1506 buf[i++] = *CHR_SPACE;
1507 while (map && *map && **map != *CHR_SPACE)
1509 const char *cp = *map;
1511 if (**map == *CHR_CUT)
1517 if (i >= IT_MAX_WORD)
1519 while (i < IT_MAX_WORD && *cp)
1522 remain = p->term_len - (b - p->term_buf);
1525 map = zebra_maps_input (zh->reg->zebra_maps, p->index_type, &b,
1534 extract_add_string (p, buf, i);
1537 static void extract_token_add(RecWord *p)
1539 ZebraHandle zh = p->extractCtrl->handle;
1542 if (log_level_details)
1544 yaz_log(log_level_details, "extract_token_add "
1545 "type=%c index=%s seqno=" ZINT_FORMAT " s=%.*s",
1546 p->index_type, p->index_name,
1547 p->seqno, p->term_len, p->term_buf);
1549 if ((wrbuf = zebra_replace(zh->reg->zebra_maps, p->index_type, 0,
1550 p->term_buf, p->term_len)))
1552 p->term_buf = wrbuf_buf(wrbuf);
1553 p->term_len = wrbuf_len(wrbuf);
1555 if (zebra_maps_is_complete (zh->reg->zebra_maps, p->index_type))
1556 extract_add_complete_field (p);
1558 extract_add_incomplete_field(p);
1561 static void extract_set_store_data_cb(struct recExtractCtrl *p,
1562 void *buf, size_t sz)
1564 ZebraHandle zh = (ZebraHandle) p->handle;
1566 xfree(zh->store_data_buf);
1567 zh->store_data_buf = 0;
1568 zh->store_data_size = 0;
1571 zh->store_data_buf = xmalloc(sz);
1572 zh->store_data_size = sz;
1573 memcpy(zh->store_data_buf, buf, sz);
1577 static void extract_set_store_data_prepare(struct recExtractCtrl *p)
1579 ZebraHandle zh = (ZebraHandle) p->handle;
1580 xfree(zh->store_data_buf);
1581 zh->store_data_buf = 0;
1582 zh->store_data_size = 0;
1583 p->setStoreData = extract_set_store_data_cb;
1586 static void extract_schema_add(struct recExtractCtrl *p, Odr_oid *oid)
1588 ZebraHandle zh = (ZebraHandle) p->handle;
1589 zebraExplain_addSchema (zh->reg->zei, oid);
1592 void extract_flush_sort_keys(ZebraHandle zh, zint sysno,
1593 int cmd, zebra_rec_keys_t reckeys)
1596 yaz_log(YLOG_LOG, "extract_flush_sort_keys cmd=%d sysno=" ZINT_FORMAT,
1598 extract_rec_keys_log(zh, cmd, reckeys, YLOG_LOG);
1601 if (zebra_rec_keys_rewind(reckeys))
1603 zebra_sort_index_t si = zh->reg->sort_index;
1606 struct it_key key_in;
1608 zebra_sort_sysno(si, sysno);
1610 while (zebra_rec_keys_read(reckeys, &str, &slen, &key_in))
1612 int ord = CAST_ZINT_TO_INT(key_in.mem[0]);
1614 zebra_sort_type(si, ord);
1616 zebra_sort_add(si, str, slen);
1618 zebra_sort_delete(si);
1626 * indent-tabs-mode: nil
1628 * vim: shiftwidth=4 tabstop=8 expandtab