1 /* $Id: extract.c,v 1.253 2007-03-14 14:16:14 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,
324 enum zebra_recctrl_action_t action,
326 const char *recordType,
328 const char *match_criteria,
331 void *recTypeClientData);
334 ZEBRA_RES zebra_extract_file(ZebraHandle zh, zint *sysno, const char *fname,
337 ZEBRA_RES r = ZEBRA_OK;
342 struct file_read_info *fi = 0;
343 const char *original_record_type = 0;
345 void *recTypeClientData;
346 struct ZebraRecStream stream, *streamp;
348 zebra_init_log_level();
350 if (!zh->m_group || !*zh->m_group)
353 sprintf (gprefix, "%s.", zh->m_group);
355 yaz_log(log_level_extract, "zebra_extract_file %s", fname);
357 /* determine file extension */
359 for (i = strlen(fname); --i >= 0; )
362 else if (fname[i] == '.')
364 strcpy (ext, fname+i+1);
367 /* determine file type - depending on extension */
368 original_record_type = zh->m_record_type;
369 if (!zh->m_record_type)
371 sprintf (ext_res, "%srecordType.%s", gprefix, ext);
372 zh->m_record_type = res_get (zh->res, ext_res);
374 if (!zh->m_record_type)
377 if (zh->records_processed + zh->records_skipped
378 < zh->m_file_verbose_limit)
379 yaz_log (YLOG_LOG, "? %s", fname);
380 zh->records_skipped++;
383 /* determine match criteria */
384 if (!zh->m_record_id)
386 sprintf (ext_res, "%srecordId.%s", gprefix, ext);
387 zh->m_record_id = res_get (zh->res, ext_res);
391 recType_byName (zh->reg->recTypes, zh->res, zh->m_record_type,
392 &recTypeClientData)))
394 yaz_log(YLOG_WARN, "No such record type: %s", zh->m_record_type);
398 switch(recType->version)
403 yaz_log(YLOG_WARN, "Bad filter version: %s", zh->m_record_type);
405 if (sysno && deleteFlag)
414 if (zh->path_reg && !yaz_is_abspath (fname))
416 strcpy (full_rep, zh->path_reg);
417 strcat (full_rep, "/");
418 strcat (full_rep, fname);
421 strcpy (full_rep, fname);
423 if ((fd = open (full_rep, O_BINARY|O_RDONLY)) == -1)
425 yaz_log (YLOG_WARN|YLOG_ERRNO, "open %s", full_rep);
426 zh->m_record_type = original_record_type;
430 zebra_create_stream_fd(streamp, fd, 0);
432 r = zebra_extract_records_stream(zh, streamp,
434 action_delete : action_update,
438 0, /*match_criteria */
440 recType, recTypeClientData);
442 stream.destroy(streamp);
443 zh->m_record_type = original_record_type;
448 If sysno is provided, then it's used to identify the reocord.
449 If not, and match_criteria is provided, then sysno is guessed
450 If not, and a record is provided, then sysno is got from there
454 ZEBRA_RES zebra_buffer_extract_record(ZebraHandle zh,
455 const char *buf, size_t buf_size,
456 enum zebra_recctrl_action_t action,
458 const char *recordType,
460 const char *match_criteria,
463 struct ZebraRecStream stream;
468 if (recordType && *recordType)
470 yaz_log(log_level_extract,
471 "Record type explicitly specified: %s", recordType);
472 recType = recType_byName (zh->reg->recTypes, zh->res, recordType,
477 if (!(zh->m_record_type))
479 yaz_log (YLOG_WARN, "No such record type defined");
482 yaz_log(log_level_extract, "Get record type from rgroup: %s",
484 recType = recType_byName (zh->reg->recTypes, zh->res,
485 zh->m_record_type, &clientData);
486 recordType = zh->m_record_type;
491 yaz_log (YLOG_WARN, "No such record type: %s", recordType);
495 zebra_create_stream_mem(&stream, buf, buf_size);
497 res = zebra_extract_records_stream(zh, &stream,
504 recType, clientData);
505 stream.destroy(&stream);
509 ZEBRA_RES zebra_extract_records_stream(ZebraHandle zh,
510 struct ZebraRecStream *stream,
511 enum zebra_recctrl_action_t action,
513 const char *recordType,
515 const char *match_criteria,
518 void *recTypeClientData)
520 ZEBRA_RES res = ZEBRA_OK;
524 res = zebra_extract_record_stream(zh, stream,
531 recType, recTypeClientData, &more);
546 ZEBRA_RES zebra_extract_record_stream(ZebraHandle zh,
547 struct ZebraRecStream *stream,
548 enum zebra_recctrl_action_t action,
550 const char *recordType,
552 const char *match_criteria,
555 void *recTypeClientData,
560 RecordAttr *recordAttr;
561 struct recExtractCtrl extractCtrl;
563 const char *matchStr = 0;
565 off_t start_offset = 0, end_offset = 0;
566 const char *pr_fname = fname; /* filename to print .. */
567 int show_progress = zh->records_processed + zh->records_skipped
568 < zh->m_file_verbose_limit ? 1:0;
570 zebra_init_log_level();
573 pr_fname = "<no file>"; /* make it printable if file is omitted */
575 zebra_rec_keys_reset(zh->reg->keys);
576 zebra_rec_keys_reset(zh->reg->sortKeys);
578 if (zebraExplain_curDatabase (zh->reg->zei, zh->basenames[0]))
580 if (zebraExplain_newDatabase (zh->reg->zei, zh->basenames[0],
581 zh->m_explain_database))
587 off_t null_offset = 0;
588 extractCtrl.stream = stream;
590 start_offset = stream->tellf(stream);
592 extractCtrl.first_record = start_offset ? 0 : 1;
594 stream->endf(stream, &null_offset);;
596 extractCtrl.init = extract_init;
597 extractCtrl.tokenAdd = extract_token_add;
598 extractCtrl.schemaAdd = extract_schema_add;
599 extractCtrl.dh = zh->reg->dh;
600 extractCtrl.handle = zh;
601 extractCtrl.match_criteria[0] = '\0';
602 extractCtrl.staticrank = 0;
603 extractCtrl.action = action;
605 init_extractCtrl(zh, &extractCtrl);
607 extract_set_store_data_prepare(&extractCtrl);
609 r = (*recType->extract)(recTypeClientData, &extractCtrl);
611 yaz_log(YLOG_LOG, "Old action=%d new action=%d", action,
613 if (action == action_update)
615 action = extractCtrl.action;
620 case RECCTRL_EXTRACT_EOF:
622 case RECCTRL_EXTRACT_ERROR_GENERIC:
623 /* error occured during extraction ... */
624 yaz_log (YLOG_WARN, "extract error: generic");
626 case RECCTRL_EXTRACT_ERROR_NO_SUCH_FILTER:
627 /* error occured during extraction ... */
628 yaz_log (YLOG_WARN, "extract error: no such filter");
630 case RECCTRL_EXTRACT_SKIP:
632 yaz_log (YLOG_LOG, "skip %s %s " ZINT_FORMAT,
633 recordType, pr_fname, (zint) start_offset);
636 end_offset = stream->endf(stream, 0);
638 stream->seekf(stream, end_offset);
641 case RECCTRL_EXTRACT_OK:
644 yaz_log (YLOG_WARN, "extract error: unknown error: %d", r);
647 end_offset = stream->endf(stream, 0);
649 stream->seekf(stream, end_offset);
651 end_offset = stream->tellf(stream);
653 all_matches_add(&extractCtrl);
655 if (extractCtrl.match_criteria[0])
656 match_criteria = extractCtrl.match_criteria;
664 if (match_criteria && *match_criteria) {
665 matchStr = match_criteria;
667 if (zh->m_record_id && *zh->m_record_id) {
668 matchStr = get_match_from_spec(zh, zh->reg->keys, pr_fname,
672 yaz_log (YLOG_LOG, "error %s %s " ZINT_FORMAT, recordType,
673 pr_fname, (zint) start_offset);
680 int db_ord = zebraExplain_get_database_ord(zh->reg->zei);
681 char *rinfo = dict_lookup_ord(zh->reg->matchDict, db_ord,
685 assert(*rinfo == sizeof(*sysno));
686 memcpy (sysno, rinfo+1, sizeof(*sysno));
690 if (zebra_rec_keys_empty(zh->reg->keys))
692 /* the extraction process returned no information - the record
693 is probably empty - unless flagShowRecords is in use */
701 if (action == action_delete)
703 yaz_log (YLOG_LOG, "delete %s %s " ZINT_FORMAT, recordType,
704 pr_fname, (zint) start_offset);
705 yaz_log (YLOG_WARN, "cannot delete record above (seems new)");
708 else if (action == action_replace)
710 yaz_log (YLOG_LOG, "update %s %s " ZINT_FORMAT, recordType,
711 pr_fname, (zint) start_offset);
712 yaz_log (YLOG_WARN, "cannot update record above (seems new)");
716 yaz_log (YLOG_LOG, "add %s %s " ZINT_FORMAT, recordType, pr_fname,
717 (zint) start_offset);
718 rec = rec_new (zh->reg->records);
722 recordAttr = rec_init_attr (zh->reg->zei, rec);
723 if (extractCtrl.staticrank < 0)
725 yaz_log(YLOG_WARN, "Negative staticrank for record. Set to 0");
726 extractCtrl.staticrank = 0;
728 recordAttr->staticrank = extractCtrl.staticrank;
732 int db_ord = zebraExplain_get_database_ord(zh->reg->zei);
733 dict_insert_ord(zh->reg->matchDict, db_ord, matchStr,
734 sizeof(*sysno), sysno);
737 extract_flush_sort_keys(zh, *sysno, 1, zh->reg->sortKeys);
738 extract_flush_record_keys(zh, *sysno, 1, zh->reg->keys,
739 recordAttr->staticrank);
740 zh->records_inserted++;
744 /* record already exists */
745 zebra_rec_keys_t delkeys = zebra_rec_keys_open();
746 zebra_rec_keys_t sortKeys = zebra_rec_keys_open();
747 if (action == action_insert)
749 yaz_log (YLOG_LOG, "skipped %s %s " ZINT_FORMAT,
750 recordType, pr_fname, (zint) start_offset);
755 rec = rec_get (zh->reg->records, *sysno);
758 recordAttr = rec_init_attr (zh->reg->zei, rec);
760 /* decrease total size */
761 zebraExplain_recordBytesIncrement (zh->reg->zei,
762 - recordAttr->recordSize);
764 zebra_rec_keys_set_buf(delkeys,
765 rec->info[recInfo_delKeys],
766 rec->size[recInfo_delKeys],
768 zebra_rec_keys_set_buf(sortKeys,
769 rec->info[recInfo_sortKeys],
770 rec->size[recInfo_sortKeys],
773 extract_flush_sort_keys(zh, *sysno, 0, sortKeys);
774 extract_flush_record_keys(zh, *sysno, 0, delkeys,
775 recordAttr->staticrank);
776 if (action == action_delete)
778 /* record going to be deleted */
779 if (zebra_rec_keys_empty(delkeys))
781 yaz_log(YLOG_LOG, "delete %s %s " ZINT_FORMAT, recordType,
782 pr_fname, (zint) start_offset);
783 yaz_log(YLOG_WARN, "cannot delete file above, "
784 "storeKeys false (3)");
789 yaz_log(YLOG_LOG, "delete %s %s " ZINT_FORMAT, recordType,
790 pr_fname, (zint) start_offset);
791 zh->records_deleted++;
794 int db_ord = zebraExplain_get_database_ord(zh->reg->zei);
795 dict_delete_ord(zh->reg->matchDict, db_ord, matchStr);
797 rec_del (zh->reg->records, &rec);
804 { /* update or special_update */
806 yaz_log(YLOG_LOG, "update %s %s " ZINT_FORMAT, recordType,
807 pr_fname, (zint) start_offset);
808 recordAttr->staticrank = extractCtrl.staticrank;
809 extract_flush_sort_keys(zh, *sysno, 1, zh->reg->sortKeys);
810 extract_flush_record_keys(zh, *sysno, 1, zh->reg->keys,
811 recordAttr->staticrank);
812 zh->records_updated++;
814 zebra_rec_keys_close(delkeys);
815 zebra_rec_keys_close(sortKeys);
817 /* update file type */
818 xfree (rec->info[recInfo_fileType]);
819 rec->info[recInfo_fileType] =
820 rec_strdup (recordType, &rec->size[recInfo_fileType]);
822 /* update filename */
823 xfree (rec->info[recInfo_filename]);
824 rec->info[recInfo_filename] =
825 rec_strdup (fname, &rec->size[recInfo_filename]);
827 /* update delete keys */
828 xfree (rec->info[recInfo_delKeys]);
829 if (!zebra_rec_keys_empty(zh->reg->keys) && zh->m_store_keys == 1)
831 zebra_rec_keys_get_buf(zh->reg->keys,
832 &rec->info[recInfo_delKeys],
833 &rec->size[recInfo_delKeys]);
837 rec->info[recInfo_delKeys] = NULL;
838 rec->size[recInfo_delKeys] = 0;
840 /* update sort keys */
841 xfree (rec->info[recInfo_sortKeys]);
843 zebra_rec_keys_get_buf(zh->reg->sortKeys,
844 &rec->info[recInfo_sortKeys],
845 &rec->size[recInfo_sortKeys]);
849 recordAttr->recordSize = end_offset - start_offset;
850 zebraExplain_recordBytesIncrement(zh->reg->zei,
851 recordAttr->recordSize);
854 /* set run-number for this record */
855 recordAttr->runNumber =
856 zebraExplain_runNumberIncrement (zh->reg->zei, 0);
858 /* update store data */
859 xfree (rec->info[recInfo_storeData]);
861 /* update store data */
862 if (zh->store_data_buf)
864 rec->size[recInfo_storeData] = zh->store_data_size;
865 rec->info[recInfo_storeData] = zh->store_data_buf;
866 zh->store_data_buf = 0;
867 recordAttr->recordSize = zh->store_data_size;
869 else if (zh->m_store_data)
871 off_t cur_offset = stream->tellf(stream);
873 rec->size[recInfo_storeData] = recordAttr->recordSize;
874 rec->info[recInfo_storeData] = (char *)
875 xmalloc (recordAttr->recordSize);
876 stream->seekf(stream, start_offset);
877 stream->readf(stream, rec->info[recInfo_storeData],
878 recordAttr->recordSize);
879 stream->seekf(stream, cur_offset);
883 rec->info[recInfo_storeData] = NULL;
884 rec->size[recInfo_storeData] = 0;
886 /* update database name */
887 xfree (rec->info[recInfo_databaseName]);
888 rec->info[recInfo_databaseName] =
889 rec_strdup (zh->basenames[0], &rec->size[recInfo_databaseName]);
892 recordAttr->recordOffset = start_offset;
894 /* commit this record */
895 rec_put (zh->reg->records, &rec);
900 ZEBRA_RES zebra_extract_explain(void *handle, Record rec, data1_node *n)
902 ZebraHandle zh = (ZebraHandle) handle;
903 struct recExtractCtrl extractCtrl;
905 if (zebraExplain_curDatabase (zh->reg->zei,
906 rec->info[recInfo_databaseName]))
909 if (zebraExplain_newDatabase (zh->reg->zei,
910 rec->info[recInfo_databaseName], 0))
914 zebra_rec_keys_reset(zh->reg->keys);
915 zebra_rec_keys_reset(zh->reg->sortKeys);
917 extractCtrl.init = extract_init;
918 extractCtrl.tokenAdd = extract_token_add;
919 extractCtrl.schemaAdd = extract_schema_add;
920 extractCtrl.dh = zh->reg->dh;
922 init_extractCtrl(zh, &extractCtrl);
924 extractCtrl.flagShowRecords = 0;
925 extractCtrl.match_criteria[0] = '\0';
926 extractCtrl.staticrank = 0;
927 extractCtrl.action = action_update;
929 extractCtrl.handle = handle;
930 extractCtrl.first_record = 1;
932 extract_set_store_data_prepare(&extractCtrl);
935 grs_extract_tree(&extractCtrl, n);
937 if (rec->size[recInfo_delKeys])
939 zebra_rec_keys_t delkeys = zebra_rec_keys_open();
941 zebra_rec_keys_t sortkeys = zebra_rec_keys_open();
943 zebra_rec_keys_set_buf(delkeys, rec->info[recInfo_delKeys],
944 rec->size[recInfo_delKeys],
946 extract_flush_record_keys(zh, rec->sysno, 0, delkeys, 0);
947 zebra_rec_keys_close(delkeys);
949 zebra_rec_keys_set_buf(sortkeys, rec->info[recInfo_sortKeys],
950 rec->size[recInfo_sortKeys],
953 extract_flush_sort_keys(zh, rec->sysno, 0, sortkeys);
954 zebra_rec_keys_close(sortkeys);
956 extract_flush_record_keys(zh, rec->sysno, 1, zh->reg->keys, 0);
957 extract_flush_sort_keys(zh, rec->sysno, 1, zh->reg->sortKeys);
959 xfree (rec->info[recInfo_delKeys]);
960 zebra_rec_keys_get_buf(zh->reg->keys,
961 &rec->info[recInfo_delKeys],
962 &rec->size[recInfo_delKeys]);
964 xfree (rec->info[recInfo_sortKeys]);
965 zebra_rec_keys_get_buf(zh->reg->sortKeys,
966 &rec->info[recInfo_sortKeys],
967 &rec->size[recInfo_sortKeys]);
971 void extract_rec_keys_log(ZebraHandle zh, int is_insert,
972 zebra_rec_keys_t reckeys,
975 if (zebra_rec_keys_rewind(reckeys))
980 NMEM nmem = nmem_create();
982 while(zebra_rec_keys_read(reckeys, &str, &slen, &key))
984 char keystr[200]; /* room for zints to print */
986 int ord = CAST_ZINT_TO_INT(key.mem[0]);
988 const char *string_index;
990 zebraExplain_lookup_ord(zh->reg->zei, ord, &index_type,
991 0/* db */, &string_index);
993 zebra_term_untrans_iconv(zh, nmem, index_type,
996 for (i = 0; i<key.len; i++)
998 sprintf(keystr + strlen(keystr), ZINT_FORMAT " ", key.mem[i]);
1001 if (*str < CHR_BASE_CHAR)
1004 char dst_buf[200]; /* room for special chars */
1006 strcpy(dst_buf , "?");
1008 if (!strcmp(str, ""))
1009 strcpy(dst_buf, "alwaysmatches");
1010 if (!strcmp(str, FIRST_IN_FIELD_STR))
1011 strcpy(dst_buf, "firstinfield");
1012 else if (!strcmp(str, CHR_UNKNOWN))
1013 strcpy(dst_buf, "unknown");
1014 else if (!strcmp(str, CHR_SPACE))
1015 strcpy(dst_buf, "space");
1017 for (i = 0; i<slen; i++)
1019 sprintf(dst_buf + strlen(dst_buf), " %d", str[i] & 0xff);
1021 yaz_log(level, "%s%c %s %s", keystr, index_type,
1022 string_index, dst_buf);
1026 yaz_log(level, "%s%c %s \"%s\"", keystr, index_type,
1027 string_index, dst_term);
1035 void extract_rec_keys_adjust(ZebraHandle zh, int is_insert,
1036 zebra_rec_keys_t reckeys)
1038 ZebraExplainInfo zei = zh->reg->zei;
1042 struct ord_stat *next;
1045 if (zebra_rec_keys_rewind(reckeys))
1047 struct ord_stat *ord_list = 0;
1051 struct it_key key_in;
1052 while(zebra_rec_keys_read(reckeys, &str, &slen, &key_in))
1054 int ord = CAST_ZINT_TO_INT(key_in.mem[0]);
1056 for (p = ord_list; p ; p = p->next)
1064 p = xmalloc(sizeof(*p));
1075 struct ord_stat *p1 = p;
1078 zebraExplain_ord_adjust_occurrences(zei, p->ord, p->no, 1);
1080 zebraExplain_ord_adjust_occurrences(zei, p->ord, - p->no, -1);
1087 void extract_flush_record_keys(ZebraHandle zh, zint sysno, int cmd,
1088 zebra_rec_keys_t reckeys,
1091 ZebraExplainInfo zei = zh->reg->zei;
1093 extract_rec_keys_adjust(zh, cmd, reckeys);
1095 if (log_level_details)
1097 yaz_log(log_level_details, "Keys for record " ZINT_FORMAT " %s",
1098 sysno, cmd ? "insert" : "delete");
1099 extract_rec_keys_log(zh, cmd, reckeys, log_level_details);
1102 if (!zh->reg->key_block)
1104 int mem = 1024*1024 * atoi( res_get_def( zh->res, "memmax", "8"));
1105 const char *key_tmp_dir = res_get_def (zh->res, "keyTmpDir", ".");
1106 int use_threads = atoi(res_get_def (zh->res, "threads", "1"));
1107 zh->reg->key_block = key_block_create(mem, key_tmp_dir, use_threads);
1109 zebraExplain_recordCountIncrement (zei, cmd ? 1 : -1);
1111 if (zebra_rec_keys_rewind(reckeys))
1115 struct it_key key_in;
1116 while(zebra_rec_keys_read(reckeys, &str, &slen, &key_in))
1118 key_block_write(zh->reg->key_block, sysno,
1119 &key_in, cmd, str, slen,
1120 staticrank, zh->m_staticrank);
1126 ZEBRA_RES zebra_rec_keys_to_snippets(ZebraHandle zh,
1127 zebra_rec_keys_t reckeys,
1128 zebra_snippets *snippets)
1130 NMEM nmem = nmem_create();
1131 if (zebra_rec_keys_rewind(reckeys))
1136 while (zebra_rec_keys_read(reckeys, &str, &slen, &key))
1143 assert(key.len <= IT_KEY_LEVEL_MAX && key.len > 2);
1144 seqno = key.mem[key.len-1];
1145 ord = CAST_ZINT_TO_INT(key.mem[0]);
1147 zebraExplain_lookup_ord(zh->reg->zei, ord, &index_type,
1148 0/* db */, 0 /* string_index */);
1150 zebra_term_untrans_iconv(zh, nmem, index_type,
1152 zebra_snippets_append(snippets, seqno, ord, dst_term);
1160 void print_rec_keys(ZebraHandle zh, zebra_rec_keys_t reckeys)
1162 yaz_log(YLOG_LOG, "print_rec_keys");
1163 if (zebra_rec_keys_rewind(reckeys))
1168 while (zebra_rec_keys_read(reckeys, &str, &slen, &key))
1170 char dst_buf[IT_MAX_WORD];
1173 int ord = CAST_ZINT_TO_INT(key.mem[0]);
1175 assert(key.len <= IT_KEY_LEVEL_MAX && key.len > 2);
1177 zebraExplain_lookup_ord(zh->reg->zei, ord, &index_type, &db, 0);
1179 seqno = key.mem[key.len-1];
1181 zebra_term_untrans(zh, index_type, dst_buf, str);
1183 yaz_log(YLOG_LOG, "ord=%d seqno=" ZINT_FORMAT
1184 " term=%s", ord, seqno, dst_buf);
1189 static void extract_add_index_string(RecWord *p, zinfo_index_category_t cat,
1190 const char *str, int length)
1193 ZebraHandle zh = p->extractCtrl->handle;
1194 ZebraExplainInfo zei = zh->reg->zei;
1197 ch = zebraExplain_lookup_attr_str(zei, cat, p->index_type, p->index_name);
1199 ch = zebraExplain_add_attr_str(zei, cat, p->index_type, p->index_name);
1203 key.mem[i++] = p->record_id;
1204 key.mem[i++] = p->section_id;
1206 if (zh->m_segment_indexing)
1207 key.mem[i++] = p->segment;
1208 key.mem[i++] = p->seqno;
1211 zebra_rec_keys_write(zh->reg->keys, str, length, &key);
1214 static void extract_add_sort_string(RecWord *p, const char *str, int length)
1217 ZebraHandle zh = p->extractCtrl->handle;
1218 ZebraExplainInfo zei = zh->reg->zei;
1220 zinfo_index_category_t cat = zinfo_index_category_sort;
1222 ch = zebraExplain_lookup_attr_str(zei, cat, p->index_type, p->index_name);
1224 ch = zebraExplain_add_attr_str(zei, cat, p->index_type, p->index_name);
1227 key.mem[1] = p->record_id;
1229 zebra_rec_keys_write(zh->reg->sortKeys, str, length, &key);
1232 static void extract_add_staticrank_string(RecWord *p,
1233 const char *str, int length)
1236 struct recExtractCtrl *ctrl = p->extractCtrl;
1238 if (length > sizeof(valz)-1)
1239 length = sizeof(valz)-1;
1241 memcpy(valz, str, length);
1242 valz[length] = '\0';
1243 ctrl->staticrank = atozint(valz);
1246 static void extract_add_string(RecWord *p, const char *string, int length)
1248 ZebraHandle zh = p->extractCtrl->handle;
1249 assert (length > 0);
1254 if (zebra_maps_is_index(zh->reg->zebra_maps, p->index_type))
1256 extract_add_index_string(p, zinfo_index_category_index,
1258 if (zebra_maps_is_alwaysmatches(zh->reg->zebra_maps, p->index_type))
1261 memcpy(&word, p, sizeof(word));
1264 extract_add_index_string(
1265 &word, zinfo_index_category_alwaysmatches, "", 0);
1268 else if (zebra_maps_is_sort(zh->reg->zebra_maps, p->index_type))
1270 extract_add_sort_string(p, string, length);
1272 else if (zebra_maps_is_staticrank(zh->reg->zebra_maps, p->index_type))
1274 extract_add_staticrank_string(p, string, length);
1278 static void extract_add_incomplete_field(RecWord *p)
1280 ZebraHandle zh = p->extractCtrl->handle;
1281 const char *b = p->term_buf;
1282 int remain = p->term_len;
1284 const char **map = 0;
1287 map = zebra_maps_input(zh->reg->zebra_maps, p->index_type, &b, remain, 0);
1291 char buf[IT_MAX_WORD+1];
1295 while (map && *map && **map == *CHR_SPACE)
1297 remain = p->term_len - (b - p->term_buf);
1299 map = zebra_maps_input(zh->reg->zebra_maps, p->index_type, &b,
1307 while (map && *map && **map != *CHR_SPACE)
1309 const char *cp = *map;
1311 while (i < IT_MAX_WORD && *cp)
1313 remain = p->term_len - (b - p->term_buf);
1315 map = zebra_maps_input(zh->reg->zebra_maps, p->index_type, &b, remain, 0);
1325 if (zebra_maps_is_first_in_field(zh->reg->zebra_maps, p->index_type))
1327 /* first in field marker */
1328 extract_add_string(p, FIRST_IN_FIELD_STR, FIRST_IN_FIELD_LEN);
1332 extract_add_string (p, buf, i);
1337 static void extract_add_complete_field (RecWord *p)
1339 ZebraHandle zh = p->extractCtrl->handle;
1340 const char *b = p->term_buf;
1341 char buf[IT_MAX_WORD+1];
1342 const char **map = 0;
1343 int i = 0, remain = p->term_len;
1346 map = zebra_maps_input (zh->reg->zebra_maps, p->index_type, &b, remain, 1);
1348 while (remain > 0 && i < IT_MAX_WORD)
1350 while (map && *map && **map == *CHR_SPACE)
1352 remain = p->term_len - (b - p->term_buf);
1356 int first = i ? 0 : 1; /* first position */
1357 map = zebra_maps_input(zh->reg->zebra_maps, p->index_type, &b, remain, first);
1365 if (i && i < IT_MAX_WORD)
1366 buf[i++] = *CHR_SPACE;
1367 while (map && *map && **map != *CHR_SPACE)
1369 const char *cp = *map;
1371 if (**map == *CHR_CUT)
1377 if (i >= IT_MAX_WORD)
1379 while (i < IT_MAX_WORD && *cp)
1382 remain = p->term_len - (b - p->term_buf);
1385 map = zebra_maps_input (zh->reg->zebra_maps, p->index_type, &b,
1394 extract_add_string (p, buf, i);
1397 static void extract_token_add(RecWord *p)
1399 ZebraHandle zh = p->extractCtrl->handle;
1402 if (log_level_extract)
1404 yaz_log(log_level_extract, "extract_token_add "
1405 "type=%c index=%s seqno=" ZINT_FORMAT " s=%.*s",
1406 p->index_type, p->index_name,
1407 p->seqno, p->term_len, p->term_buf);
1409 if ((wrbuf = zebra_replace(zh->reg->zebra_maps, p->index_type, 0,
1410 p->term_buf, p->term_len)))
1412 p->term_buf = wrbuf_buf(wrbuf);
1413 p->term_len = wrbuf_len(wrbuf);
1415 if (zebra_maps_is_complete (zh->reg->zebra_maps, p->index_type))
1416 extract_add_complete_field (p);
1418 extract_add_incomplete_field(p);
1421 static void extract_set_store_data_cb(struct recExtractCtrl *p,
1422 void *buf, size_t sz)
1424 ZebraHandle zh = (ZebraHandle) p->handle;
1426 xfree(zh->store_data_buf);
1427 zh->store_data_buf = 0;
1428 zh->store_data_size = 0;
1431 zh->store_data_buf = xmalloc(sz);
1432 zh->store_data_size = sz;
1433 memcpy(zh->store_data_buf, buf, sz);
1437 static void extract_set_store_data_prepare(struct recExtractCtrl *p)
1439 ZebraHandle zh = (ZebraHandle) p->handle;
1440 xfree(zh->store_data_buf);
1441 zh->store_data_buf = 0;
1442 zh->store_data_size = 0;
1443 p->setStoreData = extract_set_store_data_cb;
1446 static void extract_schema_add(struct recExtractCtrl *p, Odr_oid *oid)
1448 ZebraHandle zh = (ZebraHandle) p->handle;
1449 zebraExplain_addSchema (zh->reg->zei, oid);
1452 void extract_flush_sort_keys(ZebraHandle zh, zint sysno,
1453 int cmd, zebra_rec_keys_t reckeys)
1456 yaz_log(YLOG_LOG, "extract_flush_sort_keys cmd=%d sysno=" ZINT_FORMAT,
1458 extract_rec_keys_log(zh, cmd, reckeys, YLOG_LOG);
1461 if (zebra_rec_keys_rewind(reckeys))
1463 zebra_sort_index_t si = zh->reg->sort_index;
1466 struct it_key key_in;
1468 zebra_sort_sysno(si, sysno);
1470 while (zebra_rec_keys_read(reckeys, &str, &slen, &key_in))
1472 int ord = CAST_ZINT_TO_INT(key_in.mem[0]);
1474 zebra_sort_type(si, ord);
1476 zebra_sort_add(si, str, slen);
1478 zebra_sort_delete(si);
1486 * indent-tabs-mode: nil
1488 * vim: shiftwidth=4 tabstop=8 expandtab