1 /* $Id: extract.c,v 1.239 2006-11-21 14:32:38 adam Exp $
2 Copyright (C) 1995-2006
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_flushRecordKeys (ZebraHandle zh, SYSNO sysno,
55 int cmd, zebra_rec_keys_t reckeys,
57 static void extract_flushSortKeys (ZebraHandle zh, SYSNO 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 logRecord (ZebraHandle zh)
64 ++zh->records_processed;
65 if (!(zh->records_processed % 1000))
67 yaz_log(YLOG_LOG, "Records: "ZINT_FORMAT" i/u/d "
68 ZINT_FORMAT"/"ZINT_FORMAT"/"ZINT_FORMAT,
69 zh->records_processed, zh->records_inserted,
70 zh->records_updated, zh->records_deleted);
74 static void extract_add_index_string (RecWord *p,
75 zinfo_index_category_t cat,
76 const char *str, int length);
78 static void extract_set_store_data_prepare(struct recExtractCtrl *p);
80 static void extract_init(struct recExtractCtrl *p, RecWord *w)
83 w->index_name = "any";
91 static void searchRecordKey(ZebraHandle zh,
92 zebra_rec_keys_t reckeys,
93 const char *index_name,
94 const char **ws, int ws_length)
98 zinfo_index_category_t cat = zinfo_index_category_index;
100 for (i = 0; i<ws_length; i++)
104 ch = zebraExplain_lookup_attr_str(zh->reg->zei, cat, '0', index_name);
106 ch = zebraExplain_lookup_attr_str(zh->reg->zei, cat, 'p', index_name);
108 ch = zebraExplain_lookup_attr_str(zh->reg->zei, cat, 'w', index_name);
113 if (zebra_rec_keys_rewind(reckeys))
120 while (zebra_rec_keys_read(reckeys, &str, &slen, &key))
122 assert(key.len <= IT_KEY_LEVEL_MAX && key.len > 2);
124 seqno = key.mem[key.len-1];
126 if (key.mem[0] == ch)
132 woff = seqno - startSeq;
133 if (woff >= 0 && woff < ws_length)
140 #define FILE_MATCH_BLANK "\t "
142 static char *fileMatchStr (ZebraHandle zh,
143 zebra_rec_keys_t reckeys,
144 const char *fname, const char *spec)
146 static char dstBuf[2048]; /* static here ??? */
148 const char *s = spec;
152 for (; *s && strchr(FILE_MATCH_BLANK, *s); s++)
159 char attset_str[64], attname_str[64];
163 for (s++; strchr(FILE_MATCH_BLANK, *s); s++)
165 for (i = 0; *s && *s != ',' && *s != ')' &&
166 !strchr(FILE_MATCH_BLANK, *s); s++)
167 if (i+1 < sizeof(attset_str))
168 attset_str[i++] = *s;
169 attset_str[i] = '\0';
171 for (; strchr(FILE_MATCH_BLANK, *s); s++)
174 strcpy(attname_str, attset_str);
177 for (s++; strchr(FILE_MATCH_BLANK, *s); s++)
179 for (i = 0; *s && *s != ')' &&
180 !strchr(FILE_MATCH_BLANK, *s); s++)
181 if (i+1 < sizeof(attname_str))
182 attname_str[i++] = *s;
183 attname_str[i] = '\0';
186 searchRecordKey (zh, reckeys, attname_str, ws, 32);
190 yaz_log (YLOG_WARN, "Missing ) in match criteria %s in group %s",
191 spec, zh->m_group ? zh->m_group : "none");
196 for (i = 0; i<32; i++)
205 dst += strlen(ws[i]);
209 yaz_log (YLOG_WARN, "Record didn't contain match"
210 " fields in (%s,%s)", attset_str, attname_str);
218 const char *spec_src = NULL;
219 const char *s1 = ++s;
220 while (*s1 && !strchr(FILE_MATCH_BLANK, *s1))
224 if (spec_len > sizeof(special)-1)
225 spec_len = sizeof(special)-1;
226 memcpy (special, s, spec_len);
227 special[spec_len] = '\0';
230 if (!strcmp (special, "group"))
231 spec_src = zh->m_group;
232 else if (!strcmp (special, "database"))
233 spec_src = zh->basenames[0];
234 else if (!strcmp (special, "filename")) {
237 else if (!strcmp (special, "type"))
238 spec_src = zh->m_record_type;
243 strcpy (dst, spec_src);
244 dst += strlen (spec_src);
247 else if (*s == '\"' || *s == '\'')
249 int stopMarker = *s++;
253 while (*s && *s != stopMarker)
255 if (i+1 < sizeof(tmpString))
256 tmpString[i++] = *s++;
261 strcpy (dst, tmpString);
262 dst += strlen (tmpString);
266 yaz_log (YLOG_WARN, "Syntax error in match criteria %s in group %s",
267 spec, zh->m_group ? zh->m_group : "none");
274 yaz_log (YLOG_WARN, "No match criteria for record %s in group %s",
275 fname, zh->m_group ? zh->m_group : "none");
282 struct recordLogInfo {
285 struct recordGroup *rGroup;
288 static void init_extractCtrl(ZebraHandle zh, struct recExtractCtrl *ctrl)
291 for (i = 0; i<256; i++)
293 if (zebra_maps_is_positioned(zh->reg->zebra_maps, i))
298 ctrl->flagShowRecords = !zh->m_flag_rw;
301 static void all_matches_add(struct recExtractCtrl *ctrl)
304 extract_init(ctrl, &word);
305 word.index_name = "_ALLRECORDS";
306 word.index_type = 'w';
308 extract_add_index_string (&word, zinfo_index_category_alwaysmatches,
312 ZEBRA_RES zebra_extract_file(ZebraHandle zh, SYSNO *sysno, const char *fname,
315 ZEBRA_RES r = ZEBRA_OK;
320 struct file_read_info *fi = 0;
321 const char *original_record_type = 0;
323 void *recTypeClientData;
324 struct ZebraRecStream stream, *streamp;
326 zebra_init_log_level();
328 if (!zh->m_group || !*zh->m_group)
331 sprintf (gprefix, "%s.", zh->m_group);
333 yaz_log(log_level_extract, "zebra_extract_file %s", fname);
335 /* determine file extension */
337 for (i = strlen(fname); --i >= 0; )
340 else if (fname[i] == '.')
342 strcpy (ext, fname+i+1);
345 /* determine file type - depending on extension */
346 original_record_type = zh->m_record_type;
347 if (!zh->m_record_type)
349 sprintf (ext_res, "%srecordType.%s", gprefix, ext);
350 zh->m_record_type = res_get (zh->res, ext_res);
352 if (!zh->m_record_type)
354 if (zh->records_processed < zh->m_file_verbose_limit)
355 yaz_log (YLOG_LOG, "? %s", fname);
358 /* determine match criteria */
359 if (!zh->m_record_id)
361 sprintf (ext_res, "%srecordId.%s", gprefix, ext);
362 zh->m_record_id = res_get (zh->res, ext_res);
366 recType_byName (zh->reg->recTypes, zh->res, zh->m_record_type,
367 &recTypeClientData)))
369 yaz_log(YLOG_WARN, "No such record type: %s", zh->m_record_type);
373 switch(recType->version)
378 yaz_log(YLOG_WARN, "Bad filter version: %s", zh->m_record_type);
380 if (sysno && deleteFlag)
389 if (zh->path_reg && !yaz_is_abspath (fname))
391 strcpy (full_rep, zh->path_reg);
392 strcat (full_rep, "/");
393 strcat (full_rep, fname);
396 strcpy (full_rep, fname);
398 if ((fd = open (full_rep, O_BINARY|O_RDONLY)) == -1)
400 yaz_log (YLOG_WARN|YLOG_ERRNO, "open %s", full_rep);
401 zh->m_record_type = original_record_type;
405 zebra_create_stream_fd(streamp, fd, 0);
409 r = zebra_extract_record_stream(zh, streamp,
414 0, /*match_criteria */
416 1, /* force_update */
417 1, /* allow_update */
418 recType, recTypeClientData);
429 stream.destroy(streamp);
430 zh->m_record_type = original_record_type;
435 If sysno is provided, then it's used to identify the reocord.
436 If not, and match_criteria is provided, then sysno is guessed
437 If not, and a record is provided, then sysno is got from there
441 ZEBRA_RES zebra_buffer_extract_record(ZebraHandle zh,
442 const char *buf, size_t buf_size,
445 const char *recordType,
447 const char *match_criteria,
452 struct ZebraRecStream stream;
457 if (recordType && *recordType)
459 yaz_log(log_level_extract,
460 "Record type explicitly specified: %s", recordType);
461 recType = recType_byName (zh->reg->recTypes, zh->res, recordType,
466 if (!(zh->m_record_type))
468 yaz_log (YLOG_WARN, "No such record type defined");
471 yaz_log(log_level_extract, "Get record type from rgroup: %s",
473 recType = recType_byName (zh->reg->recTypes, zh->res,
474 zh->m_record_type, &clientData);
475 recordType = zh->m_record_type;
480 yaz_log (YLOG_WARN, "No such record type: %s", recordType);
486 zebra_create_stream_mem(&stream, buf, buf_size);
488 res = zebra_extract_record_stream(zh, &stream,
497 recType, clientData);
498 stream.destroy(&stream);
503 ZEBRA_RES zebra_extract_record_stream(ZebraHandle zh,
504 struct ZebraRecStream *stream,
507 const char *recordType,
509 const char *match_criteria,
514 void *recTypeClientData)
518 RecordAttr *recordAttr;
519 struct recExtractCtrl extractCtrl;
521 const char *matchStr = 0;
523 off_t start_offset = 0;
524 const char *pr_fname = fname; /* filename to print .. */
525 int show_progress = zh->records_processed < zh->m_file_verbose_limit ? 1:0;
527 zebra_init_log_level();
530 pr_fname = "<no file>"; /* make it printable if file is omitted */
532 zebra_rec_keys_reset(zh->reg->keys);
533 zebra_rec_keys_reset(zh->reg->sortKeys);
535 if (zebraExplain_curDatabase (zh->reg->zei, zh->basenames[0]))
537 if (zebraExplain_newDatabase (zh->reg->zei, zh->basenames[0],
538 zh->m_explain_database))
544 off_t null_offset = 0;
545 extractCtrl.stream = stream;
547 start_offset = stream->tellf(stream);
549 extractCtrl.first_record = start_offset ? 0 : 1;
551 stream->endf(stream, &null_offset);;
553 extractCtrl.init = extract_init;
554 extractCtrl.tokenAdd = extract_token_add;
555 extractCtrl.schemaAdd = extract_schema_add;
556 extractCtrl.dh = zh->reg->dh;
557 extractCtrl.handle = zh;
558 extractCtrl.match_criteria[0] = '\0';
559 extractCtrl.staticrank = 0;
562 init_extractCtrl(zh, &extractCtrl);
564 extract_set_store_data_prepare(&extractCtrl);
566 r = (*recType->extract)(recTypeClientData, &extractCtrl);
568 if (r == RECCTRL_EXTRACT_EOF)
570 else if (r == RECCTRL_EXTRACT_ERROR_GENERIC)
572 /* error occured during extraction ... */
573 yaz_log (YLOG_WARN, "extract error: generic");
576 else if (r == RECCTRL_EXTRACT_ERROR_NO_SUCH_FILTER)
578 /* error occured during extraction ... */
579 yaz_log (YLOG_WARN, "extract error: no such filter");
583 all_matches_add(&extractCtrl);
585 if (extractCtrl.match_criteria[0])
586 match_criteria = extractCtrl.match_criteria;
592 if (match_criteria && *match_criteria) {
593 matchStr = match_criteria;
595 if (zh->m_record_id && *zh->m_record_id) {
596 matchStr = fileMatchStr (zh, zh->reg->keys, pr_fname,
600 yaz_log (YLOG_WARN, "Bad match criteria (recordID)");
607 int db_ord = zebraExplain_get_database_ord(zh->reg->zei);
608 char *rinfo = dict_lookup_ord(zh->reg->matchDict, db_ord,
612 assert(*rinfo == sizeof(*sysno));
613 memcpy (sysno, rinfo+1, sizeof(*sysno));
617 if (zebra_rec_keys_empty(zh->reg->keys))
619 /* the extraction process returned no information - the record
620 is probably empty - unless flagShowRecords is in use */
630 yaz_log (YLOG_LOG, "delete %s %s " ZINT_FORMAT, recordType,
631 pr_fname, (zint) start_offset);
632 yaz_log (YLOG_WARN, "cannot delete record above (seems new)");
636 yaz_log (YLOG_LOG, "add %s %s " ZINT_FORMAT, recordType, pr_fname,
637 (zint) start_offset);
638 rec = rec_new (zh->reg->records);
642 recordAttr = rec_init_attr (zh->reg->zei, rec);
643 recordAttr->staticrank = extractCtrl.staticrank;
647 int db_ord = zebraExplain_get_database_ord(zh->reg->zei);
648 dict_insert_ord(zh->reg->matchDict, db_ord, matchStr,
649 sizeof(*sysno), sysno);
653 extract_flushSortKeys (zh, *sysno, 1, zh->reg->sortKeys);
654 extract_flushRecordKeys (zh, *sysno, 1, zh->reg->keys,
655 recordAttr->staticrank);
656 zh->records_inserted++;
660 /* record already exists */
661 zebra_rec_keys_t delkeys = zebra_rec_keys_open();
662 zebra_rec_keys_t sortKeys = zebra_rec_keys_open();
665 yaz_log (YLOG_LOG, "skipped %s %s " ZINT_FORMAT,
666 recordType, pr_fname, (zint) start_offset);
671 rec = rec_get (zh->reg->records, *sysno);
674 recordAttr = rec_init_attr (zh->reg->zei, rec);
676 zebra_rec_keys_set_buf(delkeys,
677 rec->info[recInfo_delKeys],
678 rec->size[recInfo_delKeys],
680 zebra_rec_keys_set_buf(sortKeys,
681 rec->info[recInfo_sortKeys],
682 rec->size[recInfo_sortKeys],
685 extract_flushSortKeys (zh, *sysno, 0, sortKeys);
686 extract_flushRecordKeys (zh, *sysno, 0, delkeys,
687 recordAttr->staticrank);
690 /* record going to be deleted */
691 if (zebra_rec_keys_empty(delkeys))
693 yaz_log(YLOG_LOG, "delete %s %s " ZINT_FORMAT, recordType,
694 pr_fname, (zint) start_offset);
695 yaz_log(YLOG_WARN, "cannot delete file above, "
696 "storeKeys false (3)");
701 yaz_log(YLOG_LOG, "delete %s %s " ZINT_FORMAT, recordType,
702 pr_fname, (zint) start_offset);
703 zh->records_deleted++;
706 int db_ord = zebraExplain_get_database_ord(zh->reg->zei);
707 dict_delete_ord(zh->reg->matchDict, db_ord, matchStr);
709 rec_del (zh->reg->records, &rec);
718 yaz_log(YLOG_LOG, "update %s %s " ZINT_FORMAT, recordType,
719 pr_fname, (zint) ZINT_FORMAT);
720 recordAttr->staticrank = extractCtrl.staticrank;
721 extract_flushSortKeys (zh, *sysno, 1, zh->reg->sortKeys);
722 extract_flushRecordKeys (zh, *sysno, 1, zh->reg->keys,
723 recordAttr->staticrank);
724 zh->records_updated++;
726 zebra_rec_keys_close(delkeys);
727 zebra_rec_keys_close(sortKeys);
729 /* update file type */
730 xfree (rec->info[recInfo_fileType]);
731 rec->info[recInfo_fileType] =
732 rec_strdup (recordType, &rec->size[recInfo_fileType]);
734 /* update filename */
735 xfree (rec->info[recInfo_filename]);
736 rec->info[recInfo_filename] =
737 rec_strdup (fname, &rec->size[recInfo_filename]);
739 /* update delete keys */
740 xfree (rec->info[recInfo_delKeys]);
741 if (!zebra_rec_keys_empty(zh->reg->keys) && zh->m_store_keys == 1)
743 zebra_rec_keys_get_buf(zh->reg->keys,
744 &rec->info[recInfo_delKeys],
745 &rec->size[recInfo_delKeys]);
749 rec->info[recInfo_delKeys] = NULL;
750 rec->size[recInfo_delKeys] = 0;
752 /* update sort keys */
753 xfree (rec->info[recInfo_sortKeys]);
755 zebra_rec_keys_get_buf(zh->reg->sortKeys,
756 &rec->info[recInfo_sortKeys],
757 &rec->size[recInfo_sortKeys]);
759 /* save file size of original record */
760 zebraExplain_recordBytesIncrement (zh->reg->zei,
761 - recordAttr->recordSize);
764 off_t end_offset = stream->endf(stream, 0);
767 end_offset = stream->tellf(stream);
769 stream->seekf(stream, end_offset);
771 recordAttr->recordSize = end_offset - start_offset;
772 zebraExplain_recordBytesIncrement(zh->reg->zei,
773 recordAttr->recordSize);
776 /* set run-number for this record */
777 recordAttr->runNumber =
778 zebraExplain_runNumberIncrement (zh->reg->zei, 0);
780 /* update store data */
781 xfree (rec->info[recInfo_storeData]);
783 /* update store data */
784 if (zh->store_data_buf)
786 rec->size[recInfo_storeData] = zh->store_data_size;
787 rec->info[recInfo_storeData] = zh->store_data_buf;
788 zh->store_data_buf = 0;
789 recordAttr->recordSize = zh->store_data_size;
791 else if (zh->m_store_data)
793 off_t cur_offset = stream->tellf(stream);
795 rec->size[recInfo_storeData] = recordAttr->recordSize;
796 rec->info[recInfo_storeData] = (char *)
797 xmalloc (recordAttr->recordSize);
798 stream->seekf(stream, start_offset);
799 stream->readf(stream, rec->info[recInfo_storeData],
800 recordAttr->recordSize);
801 stream->seekf(stream, cur_offset);
805 rec->info[recInfo_storeData] = NULL;
806 rec->size[recInfo_storeData] = 0;
808 /* update database name */
809 xfree (rec->info[recInfo_databaseName]);
810 rec->info[recInfo_databaseName] =
811 rec_strdup (zh->basenames[0], &rec->size[recInfo_databaseName]);
814 recordAttr->recordOffset = start_offset;
816 /* commit this record */
817 rec_put (zh->reg->records, &rec);
822 ZEBRA_RES zebra_extract_explain(void *handle, Record rec, data1_node *n)
824 ZebraHandle zh = (ZebraHandle) handle;
825 struct recExtractCtrl extractCtrl;
827 if (zebraExplain_curDatabase (zh->reg->zei,
828 rec->info[recInfo_databaseName]))
831 if (zebraExplain_newDatabase (zh->reg->zei,
832 rec->info[recInfo_databaseName], 0))
836 zebra_rec_keys_reset(zh->reg->keys);
837 zebra_rec_keys_reset(zh->reg->sortKeys);
839 extractCtrl.init = extract_init;
840 extractCtrl.tokenAdd = extract_token_add;
841 extractCtrl.schemaAdd = extract_schema_add;
842 extractCtrl.dh = zh->reg->dh;
844 init_extractCtrl(zh, &extractCtrl);
846 extractCtrl.flagShowRecords = 0;
847 extractCtrl.match_criteria[0] = '\0';
848 extractCtrl.staticrank = 0;
849 extractCtrl.handle = handle;
850 extractCtrl.first_record = 1;
852 extract_set_store_data_prepare(&extractCtrl);
855 grs_extract_tree(&extractCtrl, n);
857 if (rec->size[recInfo_delKeys])
859 zebra_rec_keys_t delkeys = zebra_rec_keys_open();
861 zebra_rec_keys_t sortkeys = zebra_rec_keys_open();
863 zebra_rec_keys_set_buf(delkeys, rec->info[recInfo_delKeys],
864 rec->size[recInfo_delKeys],
866 extract_flushRecordKeys (zh, rec->sysno, 0, delkeys, 0);
867 zebra_rec_keys_close(delkeys);
869 zebra_rec_keys_set_buf(sortkeys, rec->info[recInfo_sortKeys],
870 rec->size[recInfo_sortKeys],
873 extract_flushSortKeys (zh, rec->sysno, 0, sortkeys);
874 zebra_rec_keys_close(sortkeys);
876 extract_flushRecordKeys (zh, rec->sysno, 1, zh->reg->keys, 0);
877 extract_flushSortKeys (zh, rec->sysno, 1, zh->reg->sortKeys);
879 xfree (rec->info[recInfo_delKeys]);
880 zebra_rec_keys_get_buf(zh->reg->keys,
881 &rec->info[recInfo_delKeys],
882 &rec->size[recInfo_delKeys]);
884 xfree (rec->info[recInfo_sortKeys]);
885 zebra_rec_keys_get_buf(zh->reg->sortKeys,
886 &rec->info[recInfo_sortKeys],
887 &rec->size[recInfo_sortKeys]);
891 void extract_rec_keys_log(ZebraHandle zh, int is_insert,
892 zebra_rec_keys_t reckeys,
895 if (zebra_rec_keys_rewind(reckeys))
900 NMEM nmem = nmem_create();
902 while(zebra_rec_keys_read(reckeys, &str, &slen, &key))
904 char keystr[200]; /* room for zints to print */
906 int ord = CAST_ZINT_TO_INT(key.mem[0]);
908 const char *string_index;
910 zebraExplain_lookup_ord(zh->reg->zei, ord, &index_type,
911 0/* db */, &string_index);
913 zebra_term_untrans_iconv(zh, nmem, index_type,
916 for (i = 0; i<key.len; i++)
918 sprintf(keystr + strlen(keystr), ZINT_FORMAT " ", key.mem[i]);
921 if (*str < CHR_BASE_CHAR)
924 char dst_buf[200]; /* room for special chars */
926 strcpy(dst_buf , "?");
928 if (!strcmp(str, ""))
929 strcpy(dst_buf, "alwaysmatches");
930 if (!strcmp(str, FIRST_IN_FIELD_STR))
931 strcpy(dst_buf, "firstinfield");
932 else if (!strcmp(str, CHR_UNKNOWN))
933 strcpy(dst_buf, "unknown");
934 else if (!strcmp(str, CHR_SPACE))
935 strcpy(dst_buf, "space");
937 for (i = 0; i<slen; i++)
939 sprintf(dst_buf + strlen(dst_buf), " %d", str[i] & 0xff);
941 yaz_log(level, "%s%c %s %s", keystr, index_type,
942 string_index, dst_buf);
946 yaz_log(level, "%s%c %s \"%s\"", keystr, index_type,
947 string_index, dst_term);
955 void extract_rec_keys_adjust(ZebraHandle zh, int is_insert,
956 zebra_rec_keys_t reckeys)
958 ZebraExplainInfo zei = zh->reg->zei;
962 struct ord_stat *next;
965 if (zebra_rec_keys_rewind(reckeys))
967 struct ord_stat *ord_list = 0;
971 struct it_key key_in;
972 while(zebra_rec_keys_read(reckeys, &str, &slen, &key_in))
974 int ord = CAST_ZINT_TO_INT(key_in.mem[0]);
976 for (p = ord_list; p ; p = p->next)
984 p = xmalloc(sizeof(*p));
995 struct ord_stat *p1 = p;
998 zebraExplain_ord_adjust_occurrences(zei, p->ord, p->no, 1);
1000 zebraExplain_ord_adjust_occurrences(zei, p->ord, - p->no, -1);
1007 void extract_flushRecordKeys(ZebraHandle zh, SYSNO sysno, int cmd,
1008 zebra_rec_keys_t reckeys,
1011 ZebraExplainInfo zei = zh->reg->zei;
1013 extract_rec_keys_adjust(zh, cmd, reckeys);
1015 if (log_level_details)
1017 yaz_log(log_level_details, "Keys for record " ZINT_FORMAT " %s",
1018 sysno, cmd ? "insert" : "delete");
1019 extract_rec_keys_log(zh, cmd, reckeys, log_level_details);
1022 if (!zh->reg->key_block)
1024 int mem= 1024*1024* atoi( res_get_def( zh->res, "memmax", "8"));
1025 const char *key_tmp_dir = res_get_def (zh->res, "keyTmpDir", ".");
1026 zh->reg->key_block = key_block_create(mem, key_tmp_dir);
1028 zebraExplain_recordCountIncrement (zei, cmd ? 1 : -1);
1030 if (zebra_rec_keys_rewind(reckeys))
1034 struct it_key key_in;
1035 while(zebra_rec_keys_read(reckeys, &str, &slen, &key_in))
1037 key_block_write(zh->reg->key_block, sysno,
1038 &key_in, cmd, str, slen,
1039 staticrank, zh->m_staticrank);
1045 ZEBRA_RES zebra_rec_keys_to_snippets(ZebraHandle zh,
1046 zebra_rec_keys_t reckeys,
1047 zebra_snippets *snippets)
1049 NMEM nmem = nmem_create();
1050 if (zebra_rec_keys_rewind(reckeys))
1055 while (zebra_rec_keys_read(reckeys, &str, &slen, &key))
1057 char dst_buf[IT_MAX_WORD];
1058 char *dst_term = dst_buf;
1063 assert(key.len <= IT_KEY_LEVEL_MAX && key.len > 2);
1064 seqno = key.mem[key.len-1];
1065 ord = CAST_ZINT_TO_INT(key.mem[0]);
1067 zebraExplain_lookup_ord(zh->reg->zei, ord, &index_type,
1068 0/* db */, 0 /* string_index */);
1070 zebra_term_untrans_iconv(zh, nmem, index_type,
1072 zebra_snippets_append(snippets, seqno, ord, dst_term);
1080 void print_rec_keys(ZebraHandle zh, zebra_rec_keys_t reckeys)
1082 yaz_log(YLOG_LOG, "print_rec_keys");
1083 if (zebra_rec_keys_rewind(reckeys))
1088 while (zebra_rec_keys_read(reckeys, &str, &slen, &key))
1090 char dst_buf[IT_MAX_WORD];
1093 int ord = CAST_ZINT_TO_INT(key.mem[0]);
1095 assert(key.len <= IT_KEY_LEVEL_MAX && key.len > 2);
1097 zebraExplain_lookup_ord(zh->reg->zei, ord, &index_type, &db, 0);
1099 seqno = key.mem[key.len-1];
1101 zebra_term_untrans(zh, index_type, dst_buf, str);
1103 yaz_log(YLOG_LOG, "ord=%d seqno=" ZINT_FORMAT
1104 " term=%s", ord, seqno, dst_buf);
1109 static void extract_add_index_string(RecWord *p, zinfo_index_category_t cat,
1110 const char *str, int length)
1113 ZebraHandle zh = p->extractCtrl->handle;
1114 ZebraExplainInfo zei = zh->reg->zei;
1117 ch = zebraExplain_lookup_attr_str(zei, cat, p->index_type, p->index_name);
1119 ch = zebraExplain_add_attr_str(zei, cat, p->index_type, p->index_name);
1123 key.mem[i++] = p->record_id;
1124 key.mem[i++] = p->section_id;
1126 if (zh->m_segment_indexing)
1127 key.mem[i++] = p->segment;
1128 key.mem[i++] = p->seqno;
1131 zebra_rec_keys_write(zh->reg->keys, str, length, &key);
1134 static void extract_add_sort_string(RecWord *p, const char *str, int length)
1137 ZebraHandle zh = p->extractCtrl->handle;
1138 ZebraExplainInfo zei = zh->reg->zei;
1140 zinfo_index_category_t cat = zinfo_index_category_sort;
1142 ch = zebraExplain_lookup_attr_str(zei, cat, p->index_type, p->index_name);
1144 ch = zebraExplain_add_attr_str(zei, cat, p->index_type, p->index_name);
1147 key.mem[1] = p->record_id;
1149 zebra_rec_keys_write(zh->reg->sortKeys, str, length, &key);
1152 static void extract_add_string(RecWord *p, const char *string, int length)
1154 ZebraHandle zh = p->extractCtrl->handle;
1155 assert (length > 0);
1160 if (zebra_maps_is_sort(zh->reg->zebra_maps, p->index_type))
1161 extract_add_sort_string(p, string, length);
1164 extract_add_index_string(p, zinfo_index_category_index,
1166 if (zebra_maps_is_alwaysmatches(zh->reg->zebra_maps, p->index_type))
1169 memcpy(&word, p, sizeof(word));
1172 extract_add_index_string(
1173 &word, zinfo_index_category_alwaysmatches, "", 0);
1178 static void extract_add_incomplete_field(RecWord *p)
1180 ZebraHandle zh = p->extractCtrl->handle;
1181 const char *b = p->term_buf;
1182 int remain = p->term_len;
1183 const char **map = 0;
1186 map = zebra_maps_input(zh->reg->zebra_maps, p->index_type, &b, remain, 0);
1190 if (zebra_maps_is_first_in_field(zh->reg->zebra_maps, p->index_type))
1192 /* first in field marker */
1193 extract_add_string(p, FIRST_IN_FIELD_STR, FIRST_IN_FIELD_LEN);
1199 char buf[IT_MAX_WORD+1];
1203 while (map && *map && **map == *CHR_SPACE)
1205 remain = p->term_len - (b - p->term_buf);
1207 map = zebra_maps_input(zh->reg->zebra_maps, p->index_type, &b,
1215 while (map && *map && **map != *CHR_SPACE)
1217 const char *cp = *map;
1219 while (i < IT_MAX_WORD && *cp)
1221 remain = p->term_len - (b - p->term_buf);
1223 map = zebra_maps_input(zh->reg->zebra_maps, p->index_type, &b, remain, 0);
1229 extract_add_string (p, buf, i);
1234 static void extract_add_complete_field (RecWord *p)
1236 ZebraHandle zh = p->extractCtrl->handle;
1237 const char *b = p->term_buf;
1238 char buf[IT_MAX_WORD+1];
1239 const char **map = 0;
1240 int i = 0, remain = p->term_len;
1243 map = zebra_maps_input (zh->reg->zebra_maps, p->index_type, &b, remain, 1);
1245 while (remain > 0 && i < IT_MAX_WORD)
1247 while (map && *map && **map == *CHR_SPACE)
1249 remain = p->term_len - (b - p->term_buf);
1253 int first = i ? 0 : 1; /* first position */
1254 map = zebra_maps_input(zh->reg->zebra_maps, p->index_type, &b, remain, first);
1262 if (i && i < IT_MAX_WORD)
1263 buf[i++] = *CHR_SPACE;
1264 while (map && *map && **map != *CHR_SPACE)
1266 const char *cp = *map;
1268 if (**map == *CHR_CUT)
1274 if (i >= IT_MAX_WORD)
1276 while (i < IT_MAX_WORD && *cp)
1279 remain = p->term_len - (b - p->term_buf);
1282 map = zebra_maps_input (zh->reg->zebra_maps, p->index_type, &b,
1291 extract_add_string (p, buf, i);
1294 static void extract_token_add(RecWord *p)
1296 ZebraHandle zh = p->extractCtrl->handle;
1299 if (log_level_extract)
1301 yaz_log(log_level_extract, "extract_token_add "
1302 "type=%c index=%s seqno=" ZINT_FORMAT " s=%.*s",
1303 p->index_type, p->index_name,
1304 p->seqno, p->term_len, p->term_buf);
1306 if ((wrbuf = zebra_replace(zh->reg->zebra_maps, p->index_type, 0,
1307 p->term_buf, p->term_len)))
1309 p->term_buf = wrbuf_buf(wrbuf);
1310 p->term_len = wrbuf_len(wrbuf);
1312 if (zebra_maps_is_complete (zh->reg->zebra_maps, p->index_type))
1313 extract_add_complete_field (p);
1315 extract_add_incomplete_field(p);
1318 static void extract_set_store_data_cb(struct recExtractCtrl *p,
1319 void *buf, size_t sz)
1321 ZebraHandle zh = (ZebraHandle) p->handle;
1323 xfree(zh->store_data_buf);
1324 zh->store_data_buf = 0;
1325 zh->store_data_size = 0;
1328 zh->store_data_buf = xmalloc(sz);
1329 zh->store_data_size = sz;
1330 memcpy(zh->store_data_buf, buf, sz);
1334 static void extract_set_store_data_prepare(struct recExtractCtrl *p)
1336 ZebraHandle zh = (ZebraHandle) p->handle;
1337 xfree(zh->store_data_buf);
1338 zh->store_data_buf = 0;
1339 zh->store_data_size = 0;
1340 p->setStoreData = extract_set_store_data_cb;
1343 static void extract_schema_add(struct recExtractCtrl *p, Odr_oid *oid)
1345 ZebraHandle zh = (ZebraHandle) p->handle;
1346 zebraExplain_addSchema (zh->reg->zei, oid);
1349 void extract_flushSortKeys(ZebraHandle zh, SYSNO sysno,
1350 int cmd, zebra_rec_keys_t reckeys)
1352 if (zebra_rec_keys_rewind(reckeys))
1354 SortIdx sortIdx = zh->reg->sortIdx;
1357 struct it_key key_in;
1359 sortIdx_sysno (sortIdx, sysno);
1361 while (zebra_rec_keys_read(reckeys, &str, &slen, &key_in))
1363 int ord = CAST_ZINT_TO_INT(key_in.mem[0]);
1365 sortIdx_type(sortIdx, ord);
1367 sortIdx_add(sortIdx, str, slen);
1369 sortIdx_add(sortIdx, "", 1);
1377 * indent-tabs-mode: nil
1379 * vim: shiftwidth=4 tabstop=8 expandtab