1 /* $Id: extract.c,v 1.241 2006-11-21 22:17:49 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_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 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 *get_match_from_spec(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, zint *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 = get_match_from_spec(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);
652 extract_flush_sort_keys(zh, *sysno, 1, zh->reg->sortKeys);
653 extract_flush_record_keys(zh, *sysno, 1, zh->reg->keys,
654 recordAttr->staticrank);
655 zh->records_inserted++;
659 /* record already exists */
660 zebra_rec_keys_t delkeys = zebra_rec_keys_open();
661 zebra_rec_keys_t sortKeys = zebra_rec_keys_open();
664 yaz_log (YLOG_LOG, "skipped %s %s " ZINT_FORMAT,
665 recordType, pr_fname, (zint) start_offset);
670 rec = rec_get (zh->reg->records, *sysno);
673 recordAttr = rec_init_attr (zh->reg->zei, rec);
675 zebra_rec_keys_set_buf(delkeys,
676 rec->info[recInfo_delKeys],
677 rec->size[recInfo_delKeys],
679 zebra_rec_keys_set_buf(sortKeys,
680 rec->info[recInfo_sortKeys],
681 rec->size[recInfo_sortKeys],
684 extract_flush_sort_keys(zh, *sysno, 0, sortKeys);
685 extract_flush_record_keys(zh, *sysno, 0, delkeys,
686 recordAttr->staticrank);
689 /* record going to be deleted */
690 if (zebra_rec_keys_empty(delkeys))
692 yaz_log(YLOG_LOG, "delete %s %s " ZINT_FORMAT, recordType,
693 pr_fname, (zint) start_offset);
694 yaz_log(YLOG_WARN, "cannot delete file above, "
695 "storeKeys false (3)");
700 yaz_log(YLOG_LOG, "delete %s %s " ZINT_FORMAT, recordType,
701 pr_fname, (zint) start_offset);
702 zh->records_deleted++;
705 int db_ord = zebraExplain_get_database_ord(zh->reg->zei);
706 dict_delete_ord(zh->reg->matchDict, db_ord, matchStr);
708 rec_del (zh->reg->records, &rec);
717 yaz_log(YLOG_LOG, "update %s %s " ZINT_FORMAT, recordType,
718 pr_fname, (zint) ZINT_FORMAT);
719 recordAttr->staticrank = extractCtrl.staticrank;
720 extract_flush_sort_keys(zh, *sysno, 1, zh->reg->sortKeys);
721 extract_flush_record_keys(zh, *sysno, 1, zh->reg->keys,
722 recordAttr->staticrank);
723 zh->records_updated++;
725 zebra_rec_keys_close(delkeys);
726 zebra_rec_keys_close(sortKeys);
728 /* update file type */
729 xfree (rec->info[recInfo_fileType]);
730 rec->info[recInfo_fileType] =
731 rec_strdup (recordType, &rec->size[recInfo_fileType]);
733 /* update filename */
734 xfree (rec->info[recInfo_filename]);
735 rec->info[recInfo_filename] =
736 rec_strdup (fname, &rec->size[recInfo_filename]);
738 /* update delete keys */
739 xfree (rec->info[recInfo_delKeys]);
740 if (!zebra_rec_keys_empty(zh->reg->keys) && zh->m_store_keys == 1)
742 zebra_rec_keys_get_buf(zh->reg->keys,
743 &rec->info[recInfo_delKeys],
744 &rec->size[recInfo_delKeys]);
748 rec->info[recInfo_delKeys] = NULL;
749 rec->size[recInfo_delKeys] = 0;
751 /* update sort keys */
752 xfree (rec->info[recInfo_sortKeys]);
754 zebra_rec_keys_get_buf(zh->reg->sortKeys,
755 &rec->info[recInfo_sortKeys],
756 &rec->size[recInfo_sortKeys]);
758 /* save file size of original record */
759 zebraExplain_recordBytesIncrement (zh->reg->zei,
760 - recordAttr->recordSize);
763 off_t end_offset = stream->endf(stream, 0);
766 end_offset = stream->tellf(stream);
768 stream->seekf(stream, end_offset);
770 recordAttr->recordSize = end_offset - start_offset;
771 zebraExplain_recordBytesIncrement(zh->reg->zei,
772 recordAttr->recordSize);
775 /* set run-number for this record */
776 recordAttr->runNumber =
777 zebraExplain_runNumberIncrement (zh->reg->zei, 0);
779 /* update store data */
780 xfree (rec->info[recInfo_storeData]);
782 /* update store data */
783 if (zh->store_data_buf)
785 rec->size[recInfo_storeData] = zh->store_data_size;
786 rec->info[recInfo_storeData] = zh->store_data_buf;
787 zh->store_data_buf = 0;
788 recordAttr->recordSize = zh->store_data_size;
790 else if (zh->m_store_data)
792 off_t cur_offset = stream->tellf(stream);
794 rec->size[recInfo_storeData] = recordAttr->recordSize;
795 rec->info[recInfo_storeData] = (char *)
796 xmalloc (recordAttr->recordSize);
797 stream->seekf(stream, start_offset);
798 stream->readf(stream, rec->info[recInfo_storeData],
799 recordAttr->recordSize);
800 stream->seekf(stream, cur_offset);
804 rec->info[recInfo_storeData] = NULL;
805 rec->size[recInfo_storeData] = 0;
807 /* update database name */
808 xfree (rec->info[recInfo_databaseName]);
809 rec->info[recInfo_databaseName] =
810 rec_strdup (zh->basenames[0], &rec->size[recInfo_databaseName]);
813 recordAttr->recordOffset = start_offset;
815 /* commit this record */
816 rec_put (zh->reg->records, &rec);
821 ZEBRA_RES zebra_extract_explain(void *handle, Record rec, data1_node *n)
823 ZebraHandle zh = (ZebraHandle) handle;
824 struct recExtractCtrl extractCtrl;
826 if (zebraExplain_curDatabase (zh->reg->zei,
827 rec->info[recInfo_databaseName]))
830 if (zebraExplain_newDatabase (zh->reg->zei,
831 rec->info[recInfo_databaseName], 0))
835 zebra_rec_keys_reset(zh->reg->keys);
836 zebra_rec_keys_reset(zh->reg->sortKeys);
838 extractCtrl.init = extract_init;
839 extractCtrl.tokenAdd = extract_token_add;
840 extractCtrl.schemaAdd = extract_schema_add;
841 extractCtrl.dh = zh->reg->dh;
843 init_extractCtrl(zh, &extractCtrl);
845 extractCtrl.flagShowRecords = 0;
846 extractCtrl.match_criteria[0] = '\0';
847 extractCtrl.staticrank = 0;
848 extractCtrl.handle = handle;
849 extractCtrl.first_record = 1;
851 extract_set_store_data_prepare(&extractCtrl);
854 grs_extract_tree(&extractCtrl, n);
856 if (rec->size[recInfo_delKeys])
858 zebra_rec_keys_t delkeys = zebra_rec_keys_open();
860 zebra_rec_keys_t sortkeys = zebra_rec_keys_open();
862 zebra_rec_keys_set_buf(delkeys, rec->info[recInfo_delKeys],
863 rec->size[recInfo_delKeys],
865 extract_flush_record_keys(zh, rec->sysno, 0, delkeys, 0);
866 zebra_rec_keys_close(delkeys);
868 zebra_rec_keys_set_buf(sortkeys, rec->info[recInfo_sortKeys],
869 rec->size[recInfo_sortKeys],
872 extract_flush_sort_keys(zh, rec->sysno, 0, sortkeys);
873 zebra_rec_keys_close(sortkeys);
875 extract_flush_record_keys(zh, rec->sysno, 1, zh->reg->keys, 0);
876 extract_flush_sort_keys(zh, rec->sysno, 1, zh->reg->sortKeys);
878 xfree (rec->info[recInfo_delKeys]);
879 zebra_rec_keys_get_buf(zh->reg->keys,
880 &rec->info[recInfo_delKeys],
881 &rec->size[recInfo_delKeys]);
883 xfree (rec->info[recInfo_sortKeys]);
884 zebra_rec_keys_get_buf(zh->reg->sortKeys,
885 &rec->info[recInfo_sortKeys],
886 &rec->size[recInfo_sortKeys]);
890 void extract_rec_keys_log(ZebraHandle zh, int is_insert,
891 zebra_rec_keys_t reckeys,
894 if (zebra_rec_keys_rewind(reckeys))
899 NMEM nmem = nmem_create();
901 while(zebra_rec_keys_read(reckeys, &str, &slen, &key))
903 char keystr[200]; /* room for zints to print */
905 int ord = CAST_ZINT_TO_INT(key.mem[0]);
907 const char *string_index;
909 zebraExplain_lookup_ord(zh->reg->zei, ord, &index_type,
910 0/* db */, &string_index);
912 zebra_term_untrans_iconv(zh, nmem, index_type,
915 for (i = 0; i<key.len; i++)
917 sprintf(keystr + strlen(keystr), ZINT_FORMAT " ", key.mem[i]);
920 if (*str < CHR_BASE_CHAR)
923 char dst_buf[200]; /* room for special chars */
925 strcpy(dst_buf , "?");
927 if (!strcmp(str, ""))
928 strcpy(dst_buf, "alwaysmatches");
929 if (!strcmp(str, FIRST_IN_FIELD_STR))
930 strcpy(dst_buf, "firstinfield");
931 else if (!strcmp(str, CHR_UNKNOWN))
932 strcpy(dst_buf, "unknown");
933 else if (!strcmp(str, CHR_SPACE))
934 strcpy(dst_buf, "space");
936 for (i = 0; i<slen; i++)
938 sprintf(dst_buf + strlen(dst_buf), " %d", str[i] & 0xff);
940 yaz_log(level, "%s%c %s %s", keystr, index_type,
941 string_index, dst_buf);
945 yaz_log(level, "%s%c %s \"%s\"", keystr, index_type,
946 string_index, dst_term);
954 void extract_rec_keys_adjust(ZebraHandle zh, int is_insert,
955 zebra_rec_keys_t reckeys)
957 ZebraExplainInfo zei = zh->reg->zei;
961 struct ord_stat *next;
964 if (zebra_rec_keys_rewind(reckeys))
966 struct ord_stat *ord_list = 0;
970 struct it_key key_in;
971 while(zebra_rec_keys_read(reckeys, &str, &slen, &key_in))
973 int ord = CAST_ZINT_TO_INT(key_in.mem[0]);
975 for (p = ord_list; p ; p = p->next)
983 p = xmalloc(sizeof(*p));
994 struct ord_stat *p1 = p;
997 zebraExplain_ord_adjust_occurrences(zei, p->ord, p->no, 1);
999 zebraExplain_ord_adjust_occurrences(zei, p->ord, - p->no, -1);
1006 void extract_flush_record_keys(ZebraHandle zh, zint sysno, int cmd,
1007 zebra_rec_keys_t reckeys,
1010 ZebraExplainInfo zei = zh->reg->zei;
1012 extract_rec_keys_adjust(zh, cmd, reckeys);
1014 if (log_level_details)
1016 yaz_log(log_level_details, "Keys for record " ZINT_FORMAT " %s",
1017 sysno, cmd ? "insert" : "delete");
1018 extract_rec_keys_log(zh, cmd, reckeys, log_level_details);
1021 if (!zh->reg->key_block)
1023 int mem = 1024*1024 * atoi( res_get_def( zh->res, "memmax", "8"));
1024 const char *key_tmp_dir = res_get_def (zh->res, "keyTmpDir", ".");
1025 int use_threads = atoi(res_get_def (zh->res, "threads", "1"));
1026 zh->reg->key_block = key_block_create(mem, key_tmp_dir, use_threads);
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_flush_sort_keys(ZebraHandle zh, zint 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