1 /* $Id: extract.c,v 1.250 2007-03-01 10:35:46 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_file(ZebraHandle zh, zint *sysno, const char *fname,
325 ZEBRA_RES r = ZEBRA_OK;
330 struct file_read_info *fi = 0;
331 const char *original_record_type = 0;
333 void *recTypeClientData;
334 struct ZebraRecStream stream, *streamp;
336 zebra_init_log_level();
338 if (!zh->m_group || !*zh->m_group)
341 sprintf (gprefix, "%s.", zh->m_group);
343 yaz_log(log_level_extract, "zebra_extract_file %s", fname);
345 /* determine file extension */
347 for (i = strlen(fname); --i >= 0; )
350 else if (fname[i] == '.')
352 strcpy (ext, fname+i+1);
355 /* determine file type - depending on extension */
356 original_record_type = zh->m_record_type;
357 if (!zh->m_record_type)
359 sprintf (ext_res, "%srecordType.%s", gprefix, ext);
360 zh->m_record_type = res_get (zh->res, ext_res);
362 if (!zh->m_record_type)
365 if (zh->records_processed + zh->records_skipped
366 < zh->m_file_verbose_limit)
367 yaz_log (YLOG_LOG, "? %s", fname);
368 zh->records_skipped++;
371 /* determine match criteria */
372 if (!zh->m_record_id)
374 sprintf (ext_res, "%srecordId.%s", gprefix, ext);
375 zh->m_record_id = res_get (zh->res, ext_res);
379 recType_byName (zh->reg->recTypes, zh->res, zh->m_record_type,
380 &recTypeClientData)))
382 yaz_log(YLOG_WARN, "No such record type: %s", zh->m_record_type);
386 switch(recType->version)
391 yaz_log(YLOG_WARN, "Bad filter version: %s", zh->m_record_type);
393 if (sysno && deleteFlag)
402 if (zh->path_reg && !yaz_is_abspath (fname))
404 strcpy (full_rep, zh->path_reg);
405 strcat (full_rep, "/");
406 strcat (full_rep, fname);
409 strcpy (full_rep, fname);
411 if ((fd = open (full_rep, O_BINARY|O_RDONLY)) == -1)
413 yaz_log (YLOG_WARN|YLOG_ERRNO, "open %s", full_rep);
414 zh->m_record_type = original_record_type;
418 zebra_create_stream_fd(streamp, fd, 0);
423 r = zebra_extract_record_stream(zh, streamp,
428 0, /*match_criteria */
430 1, /* force_update */
431 1, /* allow_update */
432 recType, recTypeClientData, &more);
441 stream.destroy(streamp);
442 zh->m_record_type = original_record_type;
447 If sysno is provided, then it's used to identify the reocord.
448 If not, and match_criteria is provided, then sysno is guessed
449 If not, and a record is provided, then sysno is got from there
453 ZEBRA_RES zebra_buffer_extract_record(ZebraHandle zh,
454 const char *buf, size_t buf_size,
457 const char *recordType,
459 const char *match_criteria,
464 struct ZebraRecStream stream;
470 if (recordType && *recordType)
472 yaz_log(log_level_extract,
473 "Record type explicitly specified: %s", recordType);
474 recType = recType_byName (zh->reg->recTypes, zh->res, recordType,
479 if (!(zh->m_record_type))
481 yaz_log (YLOG_WARN, "No such record type defined");
484 yaz_log(log_level_extract, "Get record type from rgroup: %s",
486 recType = recType_byName (zh->reg->recTypes, zh->res,
487 zh->m_record_type, &clientData);
488 recordType = zh->m_record_type;
493 yaz_log (YLOG_WARN, "No such record type: %s", recordType);
497 zebra_create_stream_mem(&stream, buf, buf_size);
499 res = zebra_extract_record_stream(zh, &stream,
508 recType, clientData, &more);
509 stream.destroy(&stream);
514 ZEBRA_RES zebra_extract_record_stream(ZebraHandle zh,
515 struct ZebraRecStream *stream,
518 const char *recordType,
520 const char *match_criteria,
525 void *recTypeClientData,
530 RecordAttr *recordAttr;
531 struct recExtractCtrl extractCtrl;
533 const char *matchStr = 0;
535 off_t start_offset = 0, end_offset = 0;
536 const char *pr_fname = fname; /* filename to print .. */
537 int show_progress = zh->records_processed + zh->records_skipped
538 < zh->m_file_verbose_limit ? 1:0;
540 zebra_init_log_level();
543 pr_fname = "<no file>"; /* make it printable if file is omitted */
545 zebra_rec_keys_reset(zh->reg->keys);
546 zebra_rec_keys_reset(zh->reg->sortKeys);
548 if (zebraExplain_curDatabase (zh->reg->zei, zh->basenames[0]))
550 if (zebraExplain_newDatabase (zh->reg->zei, zh->basenames[0],
551 zh->m_explain_database))
557 off_t null_offset = 0;
558 extractCtrl.stream = stream;
560 start_offset = stream->tellf(stream);
562 extractCtrl.first_record = start_offset ? 0 : 1;
564 stream->endf(stream, &null_offset);;
566 extractCtrl.init = extract_init;
567 extractCtrl.tokenAdd = extract_token_add;
568 extractCtrl.schemaAdd = extract_schema_add;
569 extractCtrl.dh = zh->reg->dh;
570 extractCtrl.handle = zh;
571 extractCtrl.match_criteria[0] = '\0';
572 extractCtrl.staticrank = 0;
574 init_extractCtrl(zh, &extractCtrl);
576 extract_set_store_data_prepare(&extractCtrl);
578 r = (*recType->extract)(recTypeClientData, &extractCtrl);
582 case RECCTRL_EXTRACT_EOF:
584 case RECCTRL_EXTRACT_ERROR_GENERIC:
585 /* error occured during extraction ... */
586 yaz_log (YLOG_WARN, "extract error: generic");
588 case RECCTRL_EXTRACT_ERROR_NO_SUCH_FILTER:
589 /* error occured during extraction ... */
590 yaz_log (YLOG_WARN, "extract error: no such filter");
592 case RECCTRL_EXTRACT_SKIP:
594 yaz_log (YLOG_LOG, "skip %s %s " ZINT_FORMAT,
595 recordType, pr_fname, (zint) start_offset);
598 end_offset = stream->endf(stream, 0);
600 stream->seekf(stream, end_offset);
603 case RECCTRL_EXTRACT_OK:
606 yaz_log (YLOG_WARN, "extract error: unknown error: %d", r);
609 end_offset = stream->endf(stream, 0);
611 stream->seekf(stream, end_offset);
613 end_offset = stream->tellf(stream);
615 all_matches_add(&extractCtrl);
617 if (extractCtrl.match_criteria[0])
618 match_criteria = extractCtrl.match_criteria;
626 if (match_criteria && *match_criteria) {
627 matchStr = match_criteria;
629 if (zh->m_record_id && *zh->m_record_id) {
630 matchStr = get_match_from_spec(zh, zh->reg->keys, pr_fname,
634 yaz_log (YLOG_LOG, "error %s %s " ZINT_FORMAT, recordType,
635 pr_fname, (zint) start_offset);
642 int db_ord = zebraExplain_get_database_ord(zh->reg->zei);
643 char *rinfo = dict_lookup_ord(zh->reg->matchDict, db_ord,
647 assert(*rinfo == sizeof(*sysno));
648 memcpy (sysno, rinfo+1, sizeof(*sysno));
652 if (zebra_rec_keys_empty(zh->reg->keys))
654 /* the extraction process returned no information - the record
655 is probably empty - unless flagShowRecords is in use */
665 yaz_log (YLOG_LOG, "delete %s %s " ZINT_FORMAT, recordType,
666 pr_fname, (zint) start_offset);
667 yaz_log (YLOG_WARN, "cannot delete record above (seems new)");
671 yaz_log (YLOG_LOG, "add %s %s " ZINT_FORMAT, recordType, pr_fname,
672 (zint) start_offset);
673 rec = rec_new (zh->reg->records);
677 recordAttr = rec_init_attr (zh->reg->zei, rec);
678 if (extractCtrl.staticrank < 0)
680 yaz_log(YLOG_WARN, "Negative staticrank for record. Set to 0");
681 extractCtrl.staticrank = 0;
683 recordAttr->staticrank = extractCtrl.staticrank;
687 int db_ord = zebraExplain_get_database_ord(zh->reg->zei);
688 dict_insert_ord(zh->reg->matchDict, db_ord, matchStr,
689 sizeof(*sysno), sysno);
692 extract_flush_sort_keys(zh, *sysno, 1, zh->reg->sortKeys);
693 extract_flush_record_keys(zh, *sysno, 1, zh->reg->keys,
694 recordAttr->staticrank);
695 zh->records_inserted++;
699 /* record already exists */
700 zebra_rec_keys_t delkeys = zebra_rec_keys_open();
701 zebra_rec_keys_t sortKeys = zebra_rec_keys_open();
704 yaz_log (YLOG_LOG, "skipped %s %s " ZINT_FORMAT,
705 recordType, pr_fname, (zint) start_offset);
710 rec = rec_get (zh->reg->records, *sysno);
713 recordAttr = rec_init_attr (zh->reg->zei, rec);
715 /* decrease total size */
716 zebraExplain_recordBytesIncrement (zh->reg->zei,
717 - recordAttr->recordSize);
719 zebra_rec_keys_set_buf(delkeys,
720 rec->info[recInfo_delKeys],
721 rec->size[recInfo_delKeys],
723 zebra_rec_keys_set_buf(sortKeys,
724 rec->info[recInfo_sortKeys],
725 rec->size[recInfo_sortKeys],
728 extract_flush_sort_keys(zh, *sysno, 0, sortKeys);
729 extract_flush_record_keys(zh, *sysno, 0, delkeys,
730 recordAttr->staticrank);
733 /* record going to be deleted */
734 if (zebra_rec_keys_empty(delkeys))
736 yaz_log(YLOG_LOG, "delete %s %s " ZINT_FORMAT, recordType,
737 pr_fname, (zint) start_offset);
738 yaz_log(YLOG_WARN, "cannot delete file above, "
739 "storeKeys false (3)");
744 yaz_log(YLOG_LOG, "delete %s %s " ZINT_FORMAT, recordType,
745 pr_fname, (zint) start_offset);
746 zh->records_deleted++;
749 int db_ord = zebraExplain_get_database_ord(zh->reg->zei);
750 dict_delete_ord(zh->reg->matchDict, db_ord, matchStr);
752 rec_del (zh->reg->records, &rec);
761 yaz_log(YLOG_LOG, "update %s %s " ZINT_FORMAT, recordType,
762 pr_fname, (zint) start_offset);
763 recordAttr->staticrank = extractCtrl.staticrank;
764 extract_flush_sort_keys(zh, *sysno, 1, zh->reg->sortKeys);
765 extract_flush_record_keys(zh, *sysno, 1, zh->reg->keys,
766 recordAttr->staticrank);
767 zh->records_updated++;
769 zebra_rec_keys_close(delkeys);
770 zebra_rec_keys_close(sortKeys);
772 /* update file type */
773 xfree (rec->info[recInfo_fileType]);
774 rec->info[recInfo_fileType] =
775 rec_strdup (recordType, &rec->size[recInfo_fileType]);
777 /* update filename */
778 xfree (rec->info[recInfo_filename]);
779 rec->info[recInfo_filename] =
780 rec_strdup (fname, &rec->size[recInfo_filename]);
782 /* update delete keys */
783 xfree (rec->info[recInfo_delKeys]);
784 if (!zebra_rec_keys_empty(zh->reg->keys) && zh->m_store_keys == 1)
786 zebra_rec_keys_get_buf(zh->reg->keys,
787 &rec->info[recInfo_delKeys],
788 &rec->size[recInfo_delKeys]);
792 rec->info[recInfo_delKeys] = NULL;
793 rec->size[recInfo_delKeys] = 0;
795 /* update sort keys */
796 xfree (rec->info[recInfo_sortKeys]);
798 zebra_rec_keys_get_buf(zh->reg->sortKeys,
799 &rec->info[recInfo_sortKeys],
800 &rec->size[recInfo_sortKeys]);
804 recordAttr->recordSize = end_offset - start_offset;
805 zebraExplain_recordBytesIncrement(zh->reg->zei,
806 recordAttr->recordSize);
809 /* set run-number for this record */
810 recordAttr->runNumber =
811 zebraExplain_runNumberIncrement (zh->reg->zei, 0);
813 /* update store data */
814 xfree (rec->info[recInfo_storeData]);
816 /* update store data */
817 if (zh->store_data_buf)
819 rec->size[recInfo_storeData] = zh->store_data_size;
820 rec->info[recInfo_storeData] = zh->store_data_buf;
821 zh->store_data_buf = 0;
822 recordAttr->recordSize = zh->store_data_size;
824 else if (zh->m_store_data)
826 off_t cur_offset = stream->tellf(stream);
828 rec->size[recInfo_storeData] = recordAttr->recordSize;
829 rec->info[recInfo_storeData] = (char *)
830 xmalloc (recordAttr->recordSize);
831 stream->seekf(stream, start_offset);
832 stream->readf(stream, rec->info[recInfo_storeData],
833 recordAttr->recordSize);
834 stream->seekf(stream, cur_offset);
838 rec->info[recInfo_storeData] = NULL;
839 rec->size[recInfo_storeData] = 0;
841 /* update database name */
842 xfree (rec->info[recInfo_databaseName]);
843 rec->info[recInfo_databaseName] =
844 rec_strdup (zh->basenames[0], &rec->size[recInfo_databaseName]);
847 recordAttr->recordOffset = start_offset;
849 /* commit this record */
850 rec_put (zh->reg->records, &rec);
855 ZEBRA_RES zebra_extract_explain(void *handle, Record rec, data1_node *n)
857 ZebraHandle zh = (ZebraHandle) handle;
858 struct recExtractCtrl extractCtrl;
860 if (zebraExplain_curDatabase (zh->reg->zei,
861 rec->info[recInfo_databaseName]))
864 if (zebraExplain_newDatabase (zh->reg->zei,
865 rec->info[recInfo_databaseName], 0))
869 zebra_rec_keys_reset(zh->reg->keys);
870 zebra_rec_keys_reset(zh->reg->sortKeys);
872 extractCtrl.init = extract_init;
873 extractCtrl.tokenAdd = extract_token_add;
874 extractCtrl.schemaAdd = extract_schema_add;
875 extractCtrl.dh = zh->reg->dh;
877 init_extractCtrl(zh, &extractCtrl);
879 extractCtrl.flagShowRecords = 0;
880 extractCtrl.match_criteria[0] = '\0';
881 extractCtrl.staticrank = 0;
882 extractCtrl.handle = handle;
883 extractCtrl.first_record = 1;
885 extract_set_store_data_prepare(&extractCtrl);
888 grs_extract_tree(&extractCtrl, n);
890 if (rec->size[recInfo_delKeys])
892 zebra_rec_keys_t delkeys = zebra_rec_keys_open();
894 zebra_rec_keys_t sortkeys = zebra_rec_keys_open();
896 zebra_rec_keys_set_buf(delkeys, rec->info[recInfo_delKeys],
897 rec->size[recInfo_delKeys],
899 extract_flush_record_keys(zh, rec->sysno, 0, delkeys, 0);
900 zebra_rec_keys_close(delkeys);
902 zebra_rec_keys_set_buf(sortkeys, rec->info[recInfo_sortKeys],
903 rec->size[recInfo_sortKeys],
906 extract_flush_sort_keys(zh, rec->sysno, 0, sortkeys);
907 zebra_rec_keys_close(sortkeys);
909 extract_flush_record_keys(zh, rec->sysno, 1, zh->reg->keys, 0);
910 extract_flush_sort_keys(zh, rec->sysno, 1, zh->reg->sortKeys);
912 xfree (rec->info[recInfo_delKeys]);
913 zebra_rec_keys_get_buf(zh->reg->keys,
914 &rec->info[recInfo_delKeys],
915 &rec->size[recInfo_delKeys]);
917 xfree (rec->info[recInfo_sortKeys]);
918 zebra_rec_keys_get_buf(zh->reg->sortKeys,
919 &rec->info[recInfo_sortKeys],
920 &rec->size[recInfo_sortKeys]);
924 void extract_rec_keys_log(ZebraHandle zh, int is_insert,
925 zebra_rec_keys_t reckeys,
928 if (zebra_rec_keys_rewind(reckeys))
933 NMEM nmem = nmem_create();
935 while(zebra_rec_keys_read(reckeys, &str, &slen, &key))
937 char keystr[200]; /* room for zints to print */
939 int ord = CAST_ZINT_TO_INT(key.mem[0]);
941 const char *string_index;
943 zebraExplain_lookup_ord(zh->reg->zei, ord, &index_type,
944 0/* db */, &string_index);
946 zebra_term_untrans_iconv(zh, nmem, index_type,
949 for (i = 0; i<key.len; i++)
951 sprintf(keystr + strlen(keystr), ZINT_FORMAT " ", key.mem[i]);
954 if (*str < CHR_BASE_CHAR)
957 char dst_buf[200]; /* room for special chars */
959 strcpy(dst_buf , "?");
961 if (!strcmp(str, ""))
962 strcpy(dst_buf, "alwaysmatches");
963 if (!strcmp(str, FIRST_IN_FIELD_STR))
964 strcpy(dst_buf, "firstinfield");
965 else if (!strcmp(str, CHR_UNKNOWN))
966 strcpy(dst_buf, "unknown");
967 else if (!strcmp(str, CHR_SPACE))
968 strcpy(dst_buf, "space");
970 for (i = 0; i<slen; i++)
972 sprintf(dst_buf + strlen(dst_buf), " %d", str[i] & 0xff);
974 yaz_log(level, "%s%c %s %s", keystr, index_type,
975 string_index, dst_buf);
979 yaz_log(level, "%s%c %s \"%s\"", keystr, index_type,
980 string_index, dst_term);
988 void extract_rec_keys_adjust(ZebraHandle zh, int is_insert,
989 zebra_rec_keys_t reckeys)
991 ZebraExplainInfo zei = zh->reg->zei;
995 struct ord_stat *next;
998 if (zebra_rec_keys_rewind(reckeys))
1000 struct ord_stat *ord_list = 0;
1004 struct it_key key_in;
1005 while(zebra_rec_keys_read(reckeys, &str, &slen, &key_in))
1007 int ord = CAST_ZINT_TO_INT(key_in.mem[0]);
1009 for (p = ord_list; p ; p = p->next)
1017 p = xmalloc(sizeof(*p));
1028 struct ord_stat *p1 = p;
1031 zebraExplain_ord_adjust_occurrences(zei, p->ord, p->no, 1);
1033 zebraExplain_ord_adjust_occurrences(zei, p->ord, - p->no, -1);
1040 void extract_flush_record_keys(ZebraHandle zh, zint sysno, int cmd,
1041 zebra_rec_keys_t reckeys,
1044 ZebraExplainInfo zei = zh->reg->zei;
1046 extract_rec_keys_adjust(zh, cmd, reckeys);
1048 if (log_level_details)
1050 yaz_log(log_level_details, "Keys for record " ZINT_FORMAT " %s",
1051 sysno, cmd ? "insert" : "delete");
1052 extract_rec_keys_log(zh, cmd, reckeys, log_level_details);
1055 if (!zh->reg->key_block)
1057 int mem = 1024*1024 * atoi( res_get_def( zh->res, "memmax", "8"));
1058 const char *key_tmp_dir = res_get_def (zh->res, "keyTmpDir", ".");
1059 int use_threads = atoi(res_get_def (zh->res, "threads", "1"));
1060 zh->reg->key_block = key_block_create(mem, key_tmp_dir, use_threads);
1062 zebraExplain_recordCountIncrement (zei, cmd ? 1 : -1);
1064 if (zebra_rec_keys_rewind(reckeys))
1068 struct it_key key_in;
1069 while(zebra_rec_keys_read(reckeys, &str, &slen, &key_in))
1071 key_block_write(zh->reg->key_block, sysno,
1072 &key_in, cmd, str, slen,
1073 staticrank, zh->m_staticrank);
1079 ZEBRA_RES zebra_rec_keys_to_snippets(ZebraHandle zh,
1080 zebra_rec_keys_t reckeys,
1081 zebra_snippets *snippets)
1083 NMEM nmem = nmem_create();
1084 if (zebra_rec_keys_rewind(reckeys))
1089 while (zebra_rec_keys_read(reckeys, &str, &slen, &key))
1096 assert(key.len <= IT_KEY_LEVEL_MAX && key.len > 2);
1097 seqno = key.mem[key.len-1];
1098 ord = CAST_ZINT_TO_INT(key.mem[0]);
1100 zebraExplain_lookup_ord(zh->reg->zei, ord, &index_type,
1101 0/* db */, 0 /* string_index */);
1103 zebra_term_untrans_iconv(zh, nmem, index_type,
1105 zebra_snippets_append(snippets, seqno, ord, dst_term);
1113 void print_rec_keys(ZebraHandle zh, zebra_rec_keys_t reckeys)
1115 yaz_log(YLOG_LOG, "print_rec_keys");
1116 if (zebra_rec_keys_rewind(reckeys))
1121 while (zebra_rec_keys_read(reckeys, &str, &slen, &key))
1123 char dst_buf[IT_MAX_WORD];
1126 int ord = CAST_ZINT_TO_INT(key.mem[0]);
1128 assert(key.len <= IT_KEY_LEVEL_MAX && key.len > 2);
1130 zebraExplain_lookup_ord(zh->reg->zei, ord, &index_type, &db, 0);
1132 seqno = key.mem[key.len-1];
1134 zebra_term_untrans(zh, index_type, dst_buf, str);
1136 yaz_log(YLOG_LOG, "ord=%d seqno=" ZINT_FORMAT
1137 " term=%s", ord, seqno, dst_buf);
1142 static void extract_add_index_string(RecWord *p, zinfo_index_category_t cat,
1143 const char *str, int length)
1146 ZebraHandle zh = p->extractCtrl->handle;
1147 ZebraExplainInfo zei = zh->reg->zei;
1150 ch = zebraExplain_lookup_attr_str(zei, cat, p->index_type, p->index_name);
1152 ch = zebraExplain_add_attr_str(zei, cat, p->index_type, p->index_name);
1156 key.mem[i++] = p->record_id;
1157 key.mem[i++] = p->section_id;
1159 if (zh->m_segment_indexing)
1160 key.mem[i++] = p->segment;
1161 key.mem[i++] = p->seqno;
1164 zebra_rec_keys_write(zh->reg->keys, str, length, &key);
1167 static void extract_add_sort_string(RecWord *p, const char *str, int length)
1170 ZebraHandle zh = p->extractCtrl->handle;
1171 ZebraExplainInfo zei = zh->reg->zei;
1173 zinfo_index_category_t cat = zinfo_index_category_sort;
1175 ch = zebraExplain_lookup_attr_str(zei, cat, p->index_type, p->index_name);
1177 ch = zebraExplain_add_attr_str(zei, cat, p->index_type, p->index_name);
1180 key.mem[1] = p->record_id;
1182 zebra_rec_keys_write(zh->reg->sortKeys, str, length, &key);
1185 static void extract_add_staticrank_string(RecWord *p,
1186 const char *str, int length)
1189 struct recExtractCtrl *ctrl = p->extractCtrl;
1191 if (length > sizeof(valz)-1)
1192 length = sizeof(valz)-1;
1194 memcpy(valz, str, length);
1195 valz[length] = '\0';
1196 ctrl->staticrank = atozint(valz);
1199 static void extract_add_string(RecWord *p, const char *string, int length)
1201 ZebraHandle zh = p->extractCtrl->handle;
1202 assert (length > 0);
1207 if (zebra_maps_is_index(zh->reg->zebra_maps, p->index_type))
1209 extract_add_index_string(p, zinfo_index_category_index,
1211 if (zebra_maps_is_alwaysmatches(zh->reg->zebra_maps, p->index_type))
1214 memcpy(&word, p, sizeof(word));
1217 extract_add_index_string(
1218 &word, zinfo_index_category_alwaysmatches, "", 0);
1221 else if (zebra_maps_is_sort(zh->reg->zebra_maps, p->index_type))
1223 extract_add_sort_string(p, string, length);
1225 else if (zebra_maps_is_staticrank(zh->reg->zebra_maps, p->index_type))
1227 extract_add_staticrank_string(p, string, length);
1231 static void extract_add_incomplete_field(RecWord *p)
1233 ZebraHandle zh = p->extractCtrl->handle;
1234 const char *b = p->term_buf;
1235 int remain = p->term_len;
1237 const char **map = 0;
1240 map = zebra_maps_input(zh->reg->zebra_maps, p->index_type, &b, remain, 0);
1244 char buf[IT_MAX_WORD+1];
1248 while (map && *map && **map == *CHR_SPACE)
1250 remain = p->term_len - (b - p->term_buf);
1252 map = zebra_maps_input(zh->reg->zebra_maps, p->index_type, &b,
1260 while (map && *map && **map != *CHR_SPACE)
1262 const char *cp = *map;
1264 while (i < IT_MAX_WORD && *cp)
1266 remain = p->term_len - (b - p->term_buf);
1268 map = zebra_maps_input(zh->reg->zebra_maps, p->index_type, &b, remain, 0);
1278 if (zebra_maps_is_first_in_field(zh->reg->zebra_maps, p->index_type))
1280 /* first in field marker */
1281 extract_add_string(p, FIRST_IN_FIELD_STR, FIRST_IN_FIELD_LEN);
1285 extract_add_string (p, buf, i);
1290 static void extract_add_complete_field (RecWord *p)
1292 ZebraHandle zh = p->extractCtrl->handle;
1293 const char *b = p->term_buf;
1294 char buf[IT_MAX_WORD+1];
1295 const char **map = 0;
1296 int i = 0, remain = p->term_len;
1299 map = zebra_maps_input (zh->reg->zebra_maps, p->index_type, &b, remain, 1);
1301 while (remain > 0 && i < IT_MAX_WORD)
1303 while (map && *map && **map == *CHR_SPACE)
1305 remain = p->term_len - (b - p->term_buf);
1309 int first = i ? 0 : 1; /* first position */
1310 map = zebra_maps_input(zh->reg->zebra_maps, p->index_type, &b, remain, first);
1318 if (i && i < IT_MAX_WORD)
1319 buf[i++] = *CHR_SPACE;
1320 while (map && *map && **map != *CHR_SPACE)
1322 const char *cp = *map;
1324 if (**map == *CHR_CUT)
1330 if (i >= IT_MAX_WORD)
1332 while (i < IT_MAX_WORD && *cp)
1335 remain = p->term_len - (b - p->term_buf);
1338 map = zebra_maps_input (zh->reg->zebra_maps, p->index_type, &b,
1347 extract_add_string (p, buf, i);
1350 static void extract_token_add(RecWord *p)
1352 ZebraHandle zh = p->extractCtrl->handle;
1355 if (log_level_extract)
1357 yaz_log(log_level_extract, "extract_token_add "
1358 "type=%c index=%s seqno=" ZINT_FORMAT " s=%.*s",
1359 p->index_type, p->index_name,
1360 p->seqno, p->term_len, p->term_buf);
1362 if ((wrbuf = zebra_replace(zh->reg->zebra_maps, p->index_type, 0,
1363 p->term_buf, p->term_len)))
1365 p->term_buf = wrbuf_buf(wrbuf);
1366 p->term_len = wrbuf_len(wrbuf);
1368 if (zebra_maps_is_complete (zh->reg->zebra_maps, p->index_type))
1369 extract_add_complete_field (p);
1371 extract_add_incomplete_field(p);
1374 static void extract_set_store_data_cb(struct recExtractCtrl *p,
1375 void *buf, size_t sz)
1377 ZebraHandle zh = (ZebraHandle) p->handle;
1379 xfree(zh->store_data_buf);
1380 zh->store_data_buf = 0;
1381 zh->store_data_size = 0;
1384 zh->store_data_buf = xmalloc(sz);
1385 zh->store_data_size = sz;
1386 memcpy(zh->store_data_buf, buf, sz);
1390 static void extract_set_store_data_prepare(struct recExtractCtrl *p)
1392 ZebraHandle zh = (ZebraHandle) p->handle;
1393 xfree(zh->store_data_buf);
1394 zh->store_data_buf = 0;
1395 zh->store_data_size = 0;
1396 p->setStoreData = extract_set_store_data_cb;
1399 static void extract_schema_add(struct recExtractCtrl *p, Odr_oid *oid)
1401 ZebraHandle zh = (ZebraHandle) p->handle;
1402 zebraExplain_addSchema (zh->reg->zei, oid);
1405 void extract_flush_sort_keys(ZebraHandle zh, zint sysno,
1406 int cmd, zebra_rec_keys_t reckeys)
1409 yaz_log(YLOG_LOG, "extract_flush_sort_keys cmd=%d sysno=" ZINT_FORMAT,
1411 extract_rec_keys_log(zh, cmd, reckeys, YLOG_LOG);
1414 if (zebra_rec_keys_rewind(reckeys))
1416 zebra_sort_index_t si = zh->reg->sort_index;
1419 struct it_key key_in;
1421 zebra_sort_sysno(si, sysno);
1423 while (zebra_rec_keys_read(reckeys, &str, &slen, &key_in))
1425 int ord = CAST_ZINT_TO_INT(key_in.mem[0]);
1427 zebra_sort_type(si, ord);
1429 zebra_sort_add(si, str, slen);
1431 zebra_sort_delete(si);
1439 * indent-tabs-mode: nil
1441 * vim: shiftwidth=4 tabstop=8 expandtab