1 /* $Id: extract.c,v 1.251 2007-03-13 13:46:11 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 static void zebra_init_log_level(void)
45 if (!log_level_initialized)
47 log_level_initialized = 1;
49 log_level_extract = yaz_log_module_level("extract");
50 log_level_details = yaz_log_module_level("indexdetails");
54 static void extract_flush_record_keys(ZebraHandle zh, zint sysno,
55 int cmd, zebra_rec_keys_t reckeys,
57 static void extract_flush_sort_keys(ZebraHandle zh, zint sysno,
58 int cmd, zebra_rec_keys_t skp);
59 static void extract_schema_add (struct recExtractCtrl *p, Odr_oid *oid);
60 static void extract_token_add (RecWord *p);
62 static void check_log_limit(ZebraHandle zh)
64 if (zh->records_processed + zh->records_skipped == zh->m_file_verbose_limit)
66 yaz_log(YLOG_LOG, "More than %d file log entries. Omitting rest",
67 zh->m_file_verbose_limit);
71 static void logRecord (ZebraHandle zh)
74 ++zh->records_processed;
75 if (!(zh->records_processed % 1000))
77 yaz_log(YLOG_LOG, "Records: "ZINT_FORMAT" i/u/d "
78 ZINT_FORMAT"/"ZINT_FORMAT"/"ZINT_FORMAT,
79 zh->records_processed, zh->records_inserted,
80 zh->records_updated, zh->records_deleted);
84 static void extract_add_index_string (RecWord *p,
85 zinfo_index_category_t cat,
86 const char *str, int length);
88 static void extract_set_store_data_prepare(struct recExtractCtrl *p);
90 static void extract_init(struct recExtractCtrl *p, RecWord *w)
93 w->index_name = "any";
101 static void searchRecordKey(ZebraHandle zh,
102 zebra_rec_keys_t reckeys,
103 const char *index_name,
104 const char **ws, int ws_length)
108 zinfo_index_category_t cat = zinfo_index_category_index;
110 for (i = 0; i<ws_length; i++)
114 ch = zebraExplain_lookup_attr_str(zh->reg->zei, cat, '0', index_name);
116 ch = zebraExplain_lookup_attr_str(zh->reg->zei, cat, 'p', index_name);
118 ch = zebraExplain_lookup_attr_str(zh->reg->zei, cat, 'w', index_name);
123 if (zebra_rec_keys_rewind(reckeys))
130 while (zebra_rec_keys_read(reckeys, &str, &slen, &key))
132 assert(key.len <= IT_KEY_LEVEL_MAX && key.len > 2);
134 seqno = key.mem[key.len-1];
136 if (key.mem[0] == ch)
142 woff = seqno - startSeq;
143 if (woff >= 0 && woff < ws_length)
150 #define FILE_MATCH_BLANK "\t "
152 static char *get_match_from_spec(ZebraHandle zh,
153 zebra_rec_keys_t reckeys,
154 const char *fname, const char *spec)
156 static char dstBuf[2048]; /* static here ??? */
158 const char *s = spec;
162 for (; *s && strchr(FILE_MATCH_BLANK, *s); s++)
169 char attset_str[64], attname_str[64];
173 for (s++; strchr(FILE_MATCH_BLANK, *s); s++)
175 for (i = 0; *s && *s != ',' && *s != ')' &&
176 !strchr(FILE_MATCH_BLANK, *s); s++)
177 if (i+1 < sizeof(attset_str))
178 attset_str[i++] = *s;
179 attset_str[i] = '\0';
181 for (; strchr(FILE_MATCH_BLANK, *s); s++)
184 strcpy(attname_str, attset_str);
187 for (s++; strchr(FILE_MATCH_BLANK, *s); s++)
189 for (i = 0; *s && *s != ')' &&
190 !strchr(FILE_MATCH_BLANK, *s); s++)
191 if (i+1 < sizeof(attname_str))
192 attname_str[i++] = *s;
193 attname_str[i] = '\0';
196 searchRecordKey (zh, reckeys, attname_str, ws, 32);
200 yaz_log (YLOG_WARN, "Missing ) in match criteria %s in group %s",
201 spec, zh->m_group ? zh->m_group : "none");
206 for (i = 0; i<32; i++)
215 dst += strlen(ws[i]);
219 yaz_log (YLOG_WARN, "Record didn't contain match"
220 " fields in (%s,%s)", attset_str, attname_str);
228 const char *spec_src = NULL;
229 const char *s1 = ++s;
230 while (*s1 && !strchr(FILE_MATCH_BLANK, *s1))
234 if (spec_len > sizeof(special)-1)
235 spec_len = sizeof(special)-1;
236 memcpy (special, s, spec_len);
237 special[spec_len] = '\0';
240 if (!strcmp (special, "group"))
241 spec_src = zh->m_group;
242 else if (!strcmp (special, "database"))
243 spec_src = zh->basenames[0];
244 else if (!strcmp (special, "filename")) {
247 else if (!strcmp (special, "type"))
248 spec_src = zh->m_record_type;
253 strcpy (dst, spec_src);
254 dst += strlen (spec_src);
257 else if (*s == '\"' || *s == '\'')
259 int stopMarker = *s++;
263 while (*s && *s != stopMarker)
265 if (i+1 < sizeof(tmpString))
266 tmpString[i++] = *s++;
271 strcpy (dst, tmpString);
272 dst += strlen (tmpString);
276 yaz_log (YLOG_WARN, "Syntax error in match criteria %s in group %s",
277 spec, zh->m_group ? zh->m_group : "none");
284 yaz_log (YLOG_WARN, "No match criteria for record %s in group %s",
285 fname, zh->m_group ? zh->m_group : "none");
292 struct recordLogInfo {
295 struct recordGroup *rGroup;
298 static void init_extractCtrl(ZebraHandle zh, struct recExtractCtrl *ctrl)
301 for (i = 0; i<256; i++)
303 if (zebra_maps_is_positioned(zh->reg->zebra_maps, i))
308 ctrl->flagShowRecords = !zh->m_flag_rw;
311 static void all_matches_add(struct recExtractCtrl *ctrl)
314 extract_init(ctrl, &word);
315 word.index_name = "_ALLRECORDS";
316 word.index_type = 'w';
318 extract_add_index_string (&word, zinfo_index_category_alwaysmatches,
322 ZEBRA_RES zebra_extract_records_stream(ZebraHandle zh,
323 struct ZebraRecStream *stream,
326 const char *recordType,
328 const char *match_criteria,
333 void *recTypeClientData);
336 ZEBRA_RES zebra_extract_file(ZebraHandle zh, zint *sysno, const char *fname,
339 ZEBRA_RES r = ZEBRA_OK;
344 struct file_read_info *fi = 0;
345 const char *original_record_type = 0;
347 void *recTypeClientData;
348 struct ZebraRecStream stream, *streamp;
350 zebra_init_log_level();
352 if (!zh->m_group || !*zh->m_group)
355 sprintf (gprefix, "%s.", zh->m_group);
357 yaz_log(log_level_extract, "zebra_extract_file %s", fname);
359 /* determine file extension */
361 for (i = strlen(fname); --i >= 0; )
364 else if (fname[i] == '.')
366 strcpy (ext, fname+i+1);
369 /* determine file type - depending on extension */
370 original_record_type = zh->m_record_type;
371 if (!zh->m_record_type)
373 sprintf (ext_res, "%srecordType.%s", gprefix, ext);
374 zh->m_record_type = res_get (zh->res, ext_res);
376 if (!zh->m_record_type)
379 if (zh->records_processed + zh->records_skipped
380 < zh->m_file_verbose_limit)
381 yaz_log (YLOG_LOG, "? %s", fname);
382 zh->records_skipped++;
385 /* determine match criteria */
386 if (!zh->m_record_id)
388 sprintf (ext_res, "%srecordId.%s", gprefix, ext);
389 zh->m_record_id = res_get (zh->res, ext_res);
393 recType_byName (zh->reg->recTypes, zh->res, zh->m_record_type,
394 &recTypeClientData)))
396 yaz_log(YLOG_WARN, "No such record type: %s", zh->m_record_type);
400 switch(recType->version)
405 yaz_log(YLOG_WARN, "Bad filter version: %s", zh->m_record_type);
407 if (sysno && deleteFlag)
416 if (zh->path_reg && !yaz_is_abspath (fname))
418 strcpy (full_rep, zh->path_reg);
419 strcat (full_rep, "/");
420 strcat (full_rep, fname);
423 strcpy (full_rep, fname);
425 if ((fd = open (full_rep, O_BINARY|O_RDONLY)) == -1)
427 yaz_log (YLOG_WARN|YLOG_ERRNO, "open %s", full_rep);
428 zh->m_record_type = original_record_type;
432 zebra_create_stream_fd(streamp, fd, 0);
434 r = zebra_extract_records_stream(zh, streamp,
439 0, /*match_criteria */
441 1, /* force_update */
442 1, /* allow_update */
443 recType, recTypeClientData);
445 stream.destroy(streamp);
446 zh->m_record_type = original_record_type;
451 If sysno is provided, then it's used to identify the reocord.
452 If not, and match_criteria is provided, then sysno is guessed
453 If not, and a record is provided, then sysno is got from there
457 ZEBRA_RES zebra_buffer_extract_record(ZebraHandle zh,
458 const char *buf, size_t buf_size,
461 const char *recordType,
463 const char *match_criteria,
468 struct ZebraRecStream stream;
473 if (recordType && *recordType)
475 yaz_log(log_level_extract,
476 "Record type explicitly specified: %s", recordType);
477 recType = recType_byName (zh->reg->recTypes, zh->res, recordType,
482 if (!(zh->m_record_type))
484 yaz_log (YLOG_WARN, "No such record type defined");
487 yaz_log(log_level_extract, "Get record type from rgroup: %s",
489 recType = recType_byName (zh->reg->recTypes, zh->res,
490 zh->m_record_type, &clientData);
491 recordType = zh->m_record_type;
496 yaz_log (YLOG_WARN, "No such record type: %s", recordType);
500 zebra_create_stream_mem(&stream, buf, buf_size);
502 res = zebra_extract_records_stream(zh, &stream,
511 recType, clientData);
512 stream.destroy(&stream);
516 ZEBRA_RES zebra_extract_records_stream(ZebraHandle zh,
517 struct ZebraRecStream *stream,
520 const char *recordType,
522 const char *match_criteria,
527 void *recTypeClientData)
529 ZEBRA_RES res = ZEBRA_OK;
533 res = zebra_extract_record_stream(zh, stream,
542 recType, recTypeClientData, &more);
557 ZEBRA_RES zebra_extract_record_stream(ZebraHandle zh,
558 struct ZebraRecStream *stream,
561 const char *recordType,
563 const char *match_criteria,
568 void *recTypeClientData,
573 RecordAttr *recordAttr;
574 struct recExtractCtrl extractCtrl;
576 const char *matchStr = 0;
578 off_t start_offset = 0, end_offset = 0;
579 const char *pr_fname = fname; /* filename to print .. */
580 int show_progress = zh->records_processed + zh->records_skipped
581 < zh->m_file_verbose_limit ? 1:0;
583 zebra_init_log_level();
586 pr_fname = "<no file>"; /* make it printable if file is omitted */
588 zebra_rec_keys_reset(zh->reg->keys);
589 zebra_rec_keys_reset(zh->reg->sortKeys);
591 if (zebraExplain_curDatabase (zh->reg->zei, zh->basenames[0]))
593 if (zebraExplain_newDatabase (zh->reg->zei, zh->basenames[0],
594 zh->m_explain_database))
600 off_t null_offset = 0;
601 extractCtrl.stream = stream;
603 start_offset = stream->tellf(stream);
605 extractCtrl.first_record = start_offset ? 0 : 1;
607 stream->endf(stream, &null_offset);;
609 extractCtrl.init = extract_init;
610 extractCtrl.tokenAdd = extract_token_add;
611 extractCtrl.schemaAdd = extract_schema_add;
612 extractCtrl.dh = zh->reg->dh;
613 extractCtrl.handle = zh;
614 extractCtrl.match_criteria[0] = '\0';
615 extractCtrl.staticrank = 0;
617 init_extractCtrl(zh, &extractCtrl);
619 extract_set_store_data_prepare(&extractCtrl);
621 r = (*recType->extract)(recTypeClientData, &extractCtrl);
625 case RECCTRL_EXTRACT_EOF:
627 case RECCTRL_EXTRACT_ERROR_GENERIC:
628 /* error occured during extraction ... */
629 yaz_log (YLOG_WARN, "extract error: generic");
631 case RECCTRL_EXTRACT_ERROR_NO_SUCH_FILTER:
632 /* error occured during extraction ... */
633 yaz_log (YLOG_WARN, "extract error: no such filter");
635 case RECCTRL_EXTRACT_SKIP:
637 yaz_log (YLOG_LOG, "skip %s %s " ZINT_FORMAT,
638 recordType, pr_fname, (zint) start_offset);
641 end_offset = stream->endf(stream, 0);
643 stream->seekf(stream, end_offset);
646 case RECCTRL_EXTRACT_OK:
649 yaz_log (YLOG_WARN, "extract error: unknown error: %d", r);
652 end_offset = stream->endf(stream, 0);
654 stream->seekf(stream, end_offset);
656 end_offset = stream->tellf(stream);
658 all_matches_add(&extractCtrl);
660 if (extractCtrl.match_criteria[0])
661 match_criteria = extractCtrl.match_criteria;
669 if (match_criteria && *match_criteria) {
670 matchStr = match_criteria;
672 if (zh->m_record_id && *zh->m_record_id) {
673 matchStr = get_match_from_spec(zh, zh->reg->keys, pr_fname,
677 yaz_log (YLOG_LOG, "error %s %s " ZINT_FORMAT, recordType,
678 pr_fname, (zint) start_offset);
685 int db_ord = zebraExplain_get_database_ord(zh->reg->zei);
686 char *rinfo = dict_lookup_ord(zh->reg->matchDict, db_ord,
690 assert(*rinfo == sizeof(*sysno));
691 memcpy (sysno, rinfo+1, sizeof(*sysno));
695 if (zebra_rec_keys_empty(zh->reg->keys))
697 /* the extraction process returned no information - the record
698 is probably empty - unless flagShowRecords is in use */
708 yaz_log (YLOG_LOG, "delete %s %s " ZINT_FORMAT, recordType,
709 pr_fname, (zint) start_offset);
710 yaz_log (YLOG_WARN, "cannot delete record above (seems new)");
713 else if (!force_update)
715 yaz_log (YLOG_LOG, "update %s %s " ZINT_FORMAT, recordType,
716 pr_fname, (zint) start_offset);
717 yaz_log (YLOG_WARN, "cannot update record above (seems new)");
721 yaz_log (YLOG_LOG, "add %s %s " ZINT_FORMAT, recordType, pr_fname,
722 (zint) start_offset);
723 rec = rec_new (zh->reg->records);
727 recordAttr = rec_init_attr (zh->reg->zei, rec);
728 if (extractCtrl.staticrank < 0)
730 yaz_log(YLOG_WARN, "Negative staticrank for record. Set to 0");
731 extractCtrl.staticrank = 0;
733 recordAttr->staticrank = extractCtrl.staticrank;
737 int db_ord = zebraExplain_get_database_ord(zh->reg->zei);
738 dict_insert_ord(zh->reg->matchDict, db_ord, matchStr,
739 sizeof(*sysno), sysno);
742 extract_flush_sort_keys(zh, *sysno, 1, zh->reg->sortKeys);
743 extract_flush_record_keys(zh, *sysno, 1, zh->reg->keys,
744 recordAttr->staticrank);
745 zh->records_inserted++;
749 /* record already exists */
750 zebra_rec_keys_t delkeys = zebra_rec_keys_open();
751 zebra_rec_keys_t sortKeys = zebra_rec_keys_open();
754 yaz_log (YLOG_LOG, "skipped %s %s " ZINT_FORMAT,
755 recordType, pr_fname, (zint) start_offset);
760 rec = rec_get (zh->reg->records, *sysno);
763 recordAttr = rec_init_attr (zh->reg->zei, rec);
765 /* decrease total size */
766 zebraExplain_recordBytesIncrement (zh->reg->zei,
767 - recordAttr->recordSize);
769 zebra_rec_keys_set_buf(delkeys,
770 rec->info[recInfo_delKeys],
771 rec->size[recInfo_delKeys],
773 zebra_rec_keys_set_buf(sortKeys,
774 rec->info[recInfo_sortKeys],
775 rec->size[recInfo_sortKeys],
778 extract_flush_sort_keys(zh, *sysno, 0, sortKeys);
779 extract_flush_record_keys(zh, *sysno, 0, delkeys,
780 recordAttr->staticrank);
783 /* record going to be deleted */
784 if (zebra_rec_keys_empty(delkeys))
786 yaz_log(YLOG_LOG, "delete %s %s " ZINT_FORMAT, recordType,
787 pr_fname, (zint) start_offset);
788 yaz_log(YLOG_WARN, "cannot delete file above, "
789 "storeKeys false (3)");
794 yaz_log(YLOG_LOG, "delete %s %s " ZINT_FORMAT, recordType,
795 pr_fname, (zint) start_offset);
796 zh->records_deleted++;
799 int db_ord = zebraExplain_get_database_ord(zh->reg->zei);
800 dict_delete_ord(zh->reg->matchDict, db_ord, matchStr);
802 rec_del (zh->reg->records, &rec);
811 yaz_log(YLOG_LOG, "update %s %s " ZINT_FORMAT, recordType,
812 pr_fname, (zint) start_offset);
813 recordAttr->staticrank = extractCtrl.staticrank;
814 extract_flush_sort_keys(zh, *sysno, 1, zh->reg->sortKeys);
815 extract_flush_record_keys(zh, *sysno, 1, zh->reg->keys,
816 recordAttr->staticrank);
817 zh->records_updated++;
819 zebra_rec_keys_close(delkeys);
820 zebra_rec_keys_close(sortKeys);
822 /* update file type */
823 xfree (rec->info[recInfo_fileType]);
824 rec->info[recInfo_fileType] =
825 rec_strdup (recordType, &rec->size[recInfo_fileType]);
827 /* update filename */
828 xfree (rec->info[recInfo_filename]);
829 rec->info[recInfo_filename] =
830 rec_strdup (fname, &rec->size[recInfo_filename]);
832 /* update delete keys */
833 xfree (rec->info[recInfo_delKeys]);
834 if (!zebra_rec_keys_empty(zh->reg->keys) && zh->m_store_keys == 1)
836 zebra_rec_keys_get_buf(zh->reg->keys,
837 &rec->info[recInfo_delKeys],
838 &rec->size[recInfo_delKeys]);
842 rec->info[recInfo_delKeys] = NULL;
843 rec->size[recInfo_delKeys] = 0;
845 /* update sort keys */
846 xfree (rec->info[recInfo_sortKeys]);
848 zebra_rec_keys_get_buf(zh->reg->sortKeys,
849 &rec->info[recInfo_sortKeys],
850 &rec->size[recInfo_sortKeys]);
854 recordAttr->recordSize = end_offset - start_offset;
855 zebraExplain_recordBytesIncrement(zh->reg->zei,
856 recordAttr->recordSize);
859 /* set run-number for this record */
860 recordAttr->runNumber =
861 zebraExplain_runNumberIncrement (zh->reg->zei, 0);
863 /* update store data */
864 xfree (rec->info[recInfo_storeData]);
866 /* update store data */
867 if (zh->store_data_buf)
869 rec->size[recInfo_storeData] = zh->store_data_size;
870 rec->info[recInfo_storeData] = zh->store_data_buf;
871 zh->store_data_buf = 0;
872 recordAttr->recordSize = zh->store_data_size;
874 else if (zh->m_store_data)
876 off_t cur_offset = stream->tellf(stream);
878 rec->size[recInfo_storeData] = recordAttr->recordSize;
879 rec->info[recInfo_storeData] = (char *)
880 xmalloc (recordAttr->recordSize);
881 stream->seekf(stream, start_offset);
882 stream->readf(stream, rec->info[recInfo_storeData],
883 recordAttr->recordSize);
884 stream->seekf(stream, cur_offset);
888 rec->info[recInfo_storeData] = NULL;
889 rec->size[recInfo_storeData] = 0;
891 /* update database name */
892 xfree (rec->info[recInfo_databaseName]);
893 rec->info[recInfo_databaseName] =
894 rec_strdup (zh->basenames[0], &rec->size[recInfo_databaseName]);
897 recordAttr->recordOffset = start_offset;
899 /* commit this record */
900 rec_put (zh->reg->records, &rec);
905 ZEBRA_RES zebra_extract_explain(void *handle, Record rec, data1_node *n)
907 ZebraHandle zh = (ZebraHandle) handle;
908 struct recExtractCtrl extractCtrl;
910 if (zebraExplain_curDatabase (zh->reg->zei,
911 rec->info[recInfo_databaseName]))
914 if (zebraExplain_newDatabase (zh->reg->zei,
915 rec->info[recInfo_databaseName], 0))
919 zebra_rec_keys_reset(zh->reg->keys);
920 zebra_rec_keys_reset(zh->reg->sortKeys);
922 extractCtrl.init = extract_init;
923 extractCtrl.tokenAdd = extract_token_add;
924 extractCtrl.schemaAdd = extract_schema_add;
925 extractCtrl.dh = zh->reg->dh;
927 init_extractCtrl(zh, &extractCtrl);
929 extractCtrl.flagShowRecords = 0;
930 extractCtrl.match_criteria[0] = '\0';
931 extractCtrl.staticrank = 0;
932 extractCtrl.handle = handle;
933 extractCtrl.first_record = 1;
935 extract_set_store_data_prepare(&extractCtrl);
938 grs_extract_tree(&extractCtrl, n);
940 if (rec->size[recInfo_delKeys])
942 zebra_rec_keys_t delkeys = zebra_rec_keys_open();
944 zebra_rec_keys_t sortkeys = zebra_rec_keys_open();
946 zebra_rec_keys_set_buf(delkeys, rec->info[recInfo_delKeys],
947 rec->size[recInfo_delKeys],
949 extract_flush_record_keys(zh, rec->sysno, 0, delkeys, 0);
950 zebra_rec_keys_close(delkeys);
952 zebra_rec_keys_set_buf(sortkeys, rec->info[recInfo_sortKeys],
953 rec->size[recInfo_sortKeys],
956 extract_flush_sort_keys(zh, rec->sysno, 0, sortkeys);
957 zebra_rec_keys_close(sortkeys);
959 extract_flush_record_keys(zh, rec->sysno, 1, zh->reg->keys, 0);
960 extract_flush_sort_keys(zh, rec->sysno, 1, zh->reg->sortKeys);
962 xfree (rec->info[recInfo_delKeys]);
963 zebra_rec_keys_get_buf(zh->reg->keys,
964 &rec->info[recInfo_delKeys],
965 &rec->size[recInfo_delKeys]);
967 xfree (rec->info[recInfo_sortKeys]);
968 zebra_rec_keys_get_buf(zh->reg->sortKeys,
969 &rec->info[recInfo_sortKeys],
970 &rec->size[recInfo_sortKeys]);
974 void extract_rec_keys_log(ZebraHandle zh, int is_insert,
975 zebra_rec_keys_t reckeys,
978 if (zebra_rec_keys_rewind(reckeys))
983 NMEM nmem = nmem_create();
985 while(zebra_rec_keys_read(reckeys, &str, &slen, &key))
987 char keystr[200]; /* room for zints to print */
989 int ord = CAST_ZINT_TO_INT(key.mem[0]);
991 const char *string_index;
993 zebraExplain_lookup_ord(zh->reg->zei, ord, &index_type,
994 0/* db */, &string_index);
996 zebra_term_untrans_iconv(zh, nmem, index_type,
999 for (i = 0; i<key.len; i++)
1001 sprintf(keystr + strlen(keystr), ZINT_FORMAT " ", key.mem[i]);
1004 if (*str < CHR_BASE_CHAR)
1007 char dst_buf[200]; /* room for special chars */
1009 strcpy(dst_buf , "?");
1011 if (!strcmp(str, ""))
1012 strcpy(dst_buf, "alwaysmatches");
1013 if (!strcmp(str, FIRST_IN_FIELD_STR))
1014 strcpy(dst_buf, "firstinfield");
1015 else if (!strcmp(str, CHR_UNKNOWN))
1016 strcpy(dst_buf, "unknown");
1017 else if (!strcmp(str, CHR_SPACE))
1018 strcpy(dst_buf, "space");
1020 for (i = 0; i<slen; i++)
1022 sprintf(dst_buf + strlen(dst_buf), " %d", str[i] & 0xff);
1024 yaz_log(level, "%s%c %s %s", keystr, index_type,
1025 string_index, dst_buf);
1029 yaz_log(level, "%s%c %s \"%s\"", keystr, index_type,
1030 string_index, dst_term);
1038 void extract_rec_keys_adjust(ZebraHandle zh, int is_insert,
1039 zebra_rec_keys_t reckeys)
1041 ZebraExplainInfo zei = zh->reg->zei;
1045 struct ord_stat *next;
1048 if (zebra_rec_keys_rewind(reckeys))
1050 struct ord_stat *ord_list = 0;
1054 struct it_key key_in;
1055 while(zebra_rec_keys_read(reckeys, &str, &slen, &key_in))
1057 int ord = CAST_ZINT_TO_INT(key_in.mem[0]);
1059 for (p = ord_list; p ; p = p->next)
1067 p = xmalloc(sizeof(*p));
1078 struct ord_stat *p1 = p;
1081 zebraExplain_ord_adjust_occurrences(zei, p->ord, p->no, 1);
1083 zebraExplain_ord_adjust_occurrences(zei, p->ord, - p->no, -1);
1090 void extract_flush_record_keys(ZebraHandle zh, zint sysno, int cmd,
1091 zebra_rec_keys_t reckeys,
1094 ZebraExplainInfo zei = zh->reg->zei;
1096 extract_rec_keys_adjust(zh, cmd, reckeys);
1098 if (log_level_details)
1100 yaz_log(log_level_details, "Keys for record " ZINT_FORMAT " %s",
1101 sysno, cmd ? "insert" : "delete");
1102 extract_rec_keys_log(zh, cmd, reckeys, log_level_details);
1105 if (!zh->reg->key_block)
1107 int mem = 1024*1024 * atoi( res_get_def( zh->res, "memmax", "8"));
1108 const char *key_tmp_dir = res_get_def (zh->res, "keyTmpDir", ".");
1109 int use_threads = atoi(res_get_def (zh->res, "threads", "1"));
1110 zh->reg->key_block = key_block_create(mem, key_tmp_dir, use_threads);
1112 zebraExplain_recordCountIncrement (zei, cmd ? 1 : -1);
1114 if (zebra_rec_keys_rewind(reckeys))
1118 struct it_key key_in;
1119 while(zebra_rec_keys_read(reckeys, &str, &slen, &key_in))
1121 key_block_write(zh->reg->key_block, sysno,
1122 &key_in, cmd, str, slen,
1123 staticrank, zh->m_staticrank);
1129 ZEBRA_RES zebra_rec_keys_to_snippets(ZebraHandle zh,
1130 zebra_rec_keys_t reckeys,
1131 zebra_snippets *snippets)
1133 NMEM nmem = nmem_create();
1134 if (zebra_rec_keys_rewind(reckeys))
1139 while (zebra_rec_keys_read(reckeys, &str, &slen, &key))
1146 assert(key.len <= IT_KEY_LEVEL_MAX && key.len > 2);
1147 seqno = key.mem[key.len-1];
1148 ord = CAST_ZINT_TO_INT(key.mem[0]);
1150 zebraExplain_lookup_ord(zh->reg->zei, ord, &index_type,
1151 0/* db */, 0 /* string_index */);
1153 zebra_term_untrans_iconv(zh, nmem, index_type,
1155 zebra_snippets_append(snippets, seqno, ord, dst_term);
1163 void print_rec_keys(ZebraHandle zh, zebra_rec_keys_t reckeys)
1165 yaz_log(YLOG_LOG, "print_rec_keys");
1166 if (zebra_rec_keys_rewind(reckeys))
1171 while (zebra_rec_keys_read(reckeys, &str, &slen, &key))
1173 char dst_buf[IT_MAX_WORD];
1176 int ord = CAST_ZINT_TO_INT(key.mem[0]);
1178 assert(key.len <= IT_KEY_LEVEL_MAX && key.len > 2);
1180 zebraExplain_lookup_ord(zh->reg->zei, ord, &index_type, &db, 0);
1182 seqno = key.mem[key.len-1];
1184 zebra_term_untrans(zh, index_type, dst_buf, str);
1186 yaz_log(YLOG_LOG, "ord=%d seqno=" ZINT_FORMAT
1187 " term=%s", ord, seqno, dst_buf);
1192 static void extract_add_index_string(RecWord *p, zinfo_index_category_t cat,
1193 const char *str, int length)
1196 ZebraHandle zh = p->extractCtrl->handle;
1197 ZebraExplainInfo zei = zh->reg->zei;
1200 ch = zebraExplain_lookup_attr_str(zei, cat, p->index_type, p->index_name);
1202 ch = zebraExplain_add_attr_str(zei, cat, p->index_type, p->index_name);
1206 key.mem[i++] = p->record_id;
1207 key.mem[i++] = p->section_id;
1209 if (zh->m_segment_indexing)
1210 key.mem[i++] = p->segment;
1211 key.mem[i++] = p->seqno;
1214 zebra_rec_keys_write(zh->reg->keys, str, length, &key);
1217 static void extract_add_sort_string(RecWord *p, const char *str, int length)
1220 ZebraHandle zh = p->extractCtrl->handle;
1221 ZebraExplainInfo zei = zh->reg->zei;
1223 zinfo_index_category_t cat = zinfo_index_category_sort;
1225 ch = zebraExplain_lookup_attr_str(zei, cat, p->index_type, p->index_name);
1227 ch = zebraExplain_add_attr_str(zei, cat, p->index_type, p->index_name);
1230 key.mem[1] = p->record_id;
1232 zebra_rec_keys_write(zh->reg->sortKeys, str, length, &key);
1235 static void extract_add_staticrank_string(RecWord *p,
1236 const char *str, int length)
1239 struct recExtractCtrl *ctrl = p->extractCtrl;
1241 if (length > sizeof(valz)-1)
1242 length = sizeof(valz)-1;
1244 memcpy(valz, str, length);
1245 valz[length] = '\0';
1246 ctrl->staticrank = atozint(valz);
1249 static void extract_add_string(RecWord *p, const char *string, int length)
1251 ZebraHandle zh = p->extractCtrl->handle;
1252 assert (length > 0);
1257 if (zebra_maps_is_index(zh->reg->zebra_maps, p->index_type))
1259 extract_add_index_string(p, zinfo_index_category_index,
1261 if (zebra_maps_is_alwaysmatches(zh->reg->zebra_maps, p->index_type))
1264 memcpy(&word, p, sizeof(word));
1267 extract_add_index_string(
1268 &word, zinfo_index_category_alwaysmatches, "", 0);
1271 else if (zebra_maps_is_sort(zh->reg->zebra_maps, p->index_type))
1273 extract_add_sort_string(p, string, length);
1275 else if (zebra_maps_is_staticrank(zh->reg->zebra_maps, p->index_type))
1277 extract_add_staticrank_string(p, string, length);
1281 static void extract_add_incomplete_field(RecWord *p)
1283 ZebraHandle zh = p->extractCtrl->handle;
1284 const char *b = p->term_buf;
1285 int remain = p->term_len;
1287 const char **map = 0;
1290 map = zebra_maps_input(zh->reg->zebra_maps, p->index_type, &b, remain, 0);
1294 char buf[IT_MAX_WORD+1];
1298 while (map && *map && **map == *CHR_SPACE)
1300 remain = p->term_len - (b - p->term_buf);
1302 map = zebra_maps_input(zh->reg->zebra_maps, p->index_type, &b,
1310 while (map && *map && **map != *CHR_SPACE)
1312 const char *cp = *map;
1314 while (i < IT_MAX_WORD && *cp)
1316 remain = p->term_len - (b - p->term_buf);
1318 map = zebra_maps_input(zh->reg->zebra_maps, p->index_type, &b, remain, 0);
1328 if (zebra_maps_is_first_in_field(zh->reg->zebra_maps, p->index_type))
1330 /* first in field marker */
1331 extract_add_string(p, FIRST_IN_FIELD_STR, FIRST_IN_FIELD_LEN);
1335 extract_add_string (p, buf, i);
1340 static void extract_add_complete_field (RecWord *p)
1342 ZebraHandle zh = p->extractCtrl->handle;
1343 const char *b = p->term_buf;
1344 char buf[IT_MAX_WORD+1];
1345 const char **map = 0;
1346 int i = 0, remain = p->term_len;
1349 map = zebra_maps_input (zh->reg->zebra_maps, p->index_type, &b, remain, 1);
1351 while (remain > 0 && i < IT_MAX_WORD)
1353 while (map && *map && **map == *CHR_SPACE)
1355 remain = p->term_len - (b - p->term_buf);
1359 int first = i ? 0 : 1; /* first position */
1360 map = zebra_maps_input(zh->reg->zebra_maps, p->index_type, &b, remain, first);
1368 if (i && i < IT_MAX_WORD)
1369 buf[i++] = *CHR_SPACE;
1370 while (map && *map && **map != *CHR_SPACE)
1372 const char *cp = *map;
1374 if (**map == *CHR_CUT)
1380 if (i >= IT_MAX_WORD)
1382 while (i < IT_MAX_WORD && *cp)
1385 remain = p->term_len - (b - p->term_buf);
1388 map = zebra_maps_input (zh->reg->zebra_maps, p->index_type, &b,
1397 extract_add_string (p, buf, i);
1400 static void extract_token_add(RecWord *p)
1402 ZebraHandle zh = p->extractCtrl->handle;
1405 if (log_level_extract)
1407 yaz_log(log_level_extract, "extract_token_add "
1408 "type=%c index=%s seqno=" ZINT_FORMAT " s=%.*s",
1409 p->index_type, p->index_name,
1410 p->seqno, p->term_len, p->term_buf);
1412 if ((wrbuf = zebra_replace(zh->reg->zebra_maps, p->index_type, 0,
1413 p->term_buf, p->term_len)))
1415 p->term_buf = wrbuf_buf(wrbuf);
1416 p->term_len = wrbuf_len(wrbuf);
1418 if (zebra_maps_is_complete (zh->reg->zebra_maps, p->index_type))
1419 extract_add_complete_field (p);
1421 extract_add_incomplete_field(p);
1424 static void extract_set_store_data_cb(struct recExtractCtrl *p,
1425 void *buf, size_t sz)
1427 ZebraHandle zh = (ZebraHandle) p->handle;
1429 xfree(zh->store_data_buf);
1430 zh->store_data_buf = 0;
1431 zh->store_data_size = 0;
1434 zh->store_data_buf = xmalloc(sz);
1435 zh->store_data_size = sz;
1436 memcpy(zh->store_data_buf, buf, sz);
1440 static void extract_set_store_data_prepare(struct recExtractCtrl *p)
1442 ZebraHandle zh = (ZebraHandle) p->handle;
1443 xfree(zh->store_data_buf);
1444 zh->store_data_buf = 0;
1445 zh->store_data_size = 0;
1446 p->setStoreData = extract_set_store_data_cb;
1449 static void extract_schema_add(struct recExtractCtrl *p, Odr_oid *oid)
1451 ZebraHandle zh = (ZebraHandle) p->handle;
1452 zebraExplain_addSchema (zh->reg->zei, oid);
1455 void extract_flush_sort_keys(ZebraHandle zh, zint sysno,
1456 int cmd, zebra_rec_keys_t reckeys)
1459 yaz_log(YLOG_LOG, "extract_flush_sort_keys cmd=%d sysno=" ZINT_FORMAT,
1461 extract_rec_keys_log(zh, cmd, reckeys, YLOG_LOG);
1464 if (zebra_rec_keys_rewind(reckeys))
1466 zebra_sort_index_t si = zh->reg->sort_index;
1469 struct it_key key_in;
1471 zebra_sort_sysno(si, sysno);
1473 while (zebra_rec_keys_read(reckeys, &str, &slen, &key_in))
1475 int ord = CAST_ZINT_TO_INT(key_in.mem[0]);
1477 zebra_sort_type(si, ord);
1479 zebra_sort_add(si, str, slen);
1481 zebra_sort_delete(si);
1489 * indent-tabs-mode: nil
1491 * vim: shiftwidth=4 tabstop=8 expandtab