1 /* $Id: extract.c,v 1.236 2006-11-14 08:12:08 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 #define ENCODE_BUFLEN 768
43 char buf[ENCODE_BUFLEN];
46 static int log_level_extract = 0;
47 static int log_level_details = 0;
48 static int log_level_initialized = 0;
50 static void zebra_init_log_level(void)
52 if (!log_level_initialized)
54 log_level_initialized = 1;
56 log_level_extract = yaz_log_module_level("extract");
57 log_level_details = yaz_log_module_level("indexdetails");
61 static void extract_flushRecordKeys (ZebraHandle zh, SYSNO sysno,
62 int cmd, zebra_rec_keys_t reckeys,
64 static void extract_flushSortKeys (ZebraHandle zh, SYSNO sysno,
65 int cmd, zebra_rec_keys_t skp);
66 static void extract_schema_add (struct recExtractCtrl *p, Odr_oid *oid);
67 static void extract_token_add (RecWord *p);
69 static void encode_key_init (struct encode_info *i);
70 static void encode_key_write (char *k, struct encode_info *i, FILE *outf);
71 static void encode_key_flush (struct encode_info *i, FILE *outf);
73 #define USE_SHELLSORT 0
76 static void shellsort(void *ar, int r, size_t s,
77 int (*cmp)(const void *a, const void *b))
82 static const int incs[16] = { 1391376, 463792, 198768, 86961, 33936,
83 13776, 4592, 1968, 861, 336,
84 112, 48, 21, 7, 3, 1 };
85 for ( k = 0; k < 16; k++)
86 for (h = incs[k], i = h; i < r; i++)
90 while (j > h && (*cmp)(a + s*(j-h), v) > 0)
92 memcpy (a + s*j, a + s*(j-h), s);
100 static void logRecord (ZebraHandle zh)
102 ++zh->records_processed;
103 if (!(zh->records_processed % 1000))
105 yaz_log(YLOG_LOG, "Records: "ZINT_FORMAT" i/u/d "
106 ZINT_FORMAT"/"ZINT_FORMAT"/"ZINT_FORMAT,
107 zh->records_processed, zh->records_inserted,
108 zh->records_updated, zh->records_deleted);
112 static void extract_add_index_string (RecWord *p,
113 zinfo_index_category_t cat,
114 const char *str, int length);
116 static void extract_set_store_data_prepare(struct recExtractCtrl *p);
118 static void extract_init(struct recExtractCtrl *p, RecWord *w)
121 w->index_name = "any";
129 static void searchRecordKey(ZebraHandle zh,
130 zebra_rec_keys_t reckeys,
131 const char *index_name,
132 const char **ws, int ws_length)
136 zinfo_index_category_t cat = zinfo_index_category_index;
138 for (i = 0; i<ws_length; i++)
142 ch = zebraExplain_lookup_attr_str(zh->reg->zei, cat, '0', index_name);
144 ch = zebraExplain_lookup_attr_str(zh->reg->zei, cat, 'p', index_name);
146 ch = zebraExplain_lookup_attr_str(zh->reg->zei, cat, 'w', index_name);
151 if (zebra_rec_keys_rewind(reckeys))
158 while (zebra_rec_keys_read(reckeys, &str, &slen, &key))
160 assert(key.len <= IT_KEY_LEVEL_MAX && key.len > 2);
162 seqno = key.mem[key.len-1];
164 if (key.mem[0] == ch)
170 woff = seqno - startSeq;
171 if (woff >= 0 && woff < ws_length)
178 #define FILE_MATCH_BLANK "\t "
180 static char *fileMatchStr (ZebraHandle zh,
181 zebra_rec_keys_t reckeys,
182 const char *fname, const char *spec)
184 static char dstBuf[2048]; /* static here ??? */
186 const char *s = spec;
190 for (; *s && strchr(FILE_MATCH_BLANK, *s); s++)
197 char attset_str[64], attname_str[64];
201 for (s++; strchr(FILE_MATCH_BLANK, *s); s++)
203 for (i = 0; *s && *s != ',' && *s != ')' &&
204 !strchr(FILE_MATCH_BLANK, *s); s++)
205 if (i+1 < sizeof(attset_str))
206 attset_str[i++] = *s;
207 attset_str[i] = '\0';
209 for (; strchr(FILE_MATCH_BLANK, *s); s++)
212 strcpy(attname_str, attset_str);
215 for (s++; strchr(FILE_MATCH_BLANK, *s); s++)
217 for (i = 0; *s && *s != ')' &&
218 !strchr(FILE_MATCH_BLANK, *s); s++)
219 if (i+1 < sizeof(attname_str))
220 attname_str[i++] = *s;
221 attname_str[i] = '\0';
224 searchRecordKey (zh, reckeys, attname_str, ws, 32);
228 yaz_log (YLOG_WARN, "Missing ) in match criteria %s in group %s",
229 spec, zh->m_group ? zh->m_group : "none");
234 for (i = 0; i<32; i++)
243 dst += strlen(ws[i]);
247 yaz_log (YLOG_WARN, "Record didn't contain match"
248 " fields in (%s,%s)", attset_str, attname_str);
256 const char *spec_src = NULL;
257 const char *s1 = ++s;
258 while (*s1 && !strchr(FILE_MATCH_BLANK, *s1))
262 if (spec_len > sizeof(special)-1)
263 spec_len = sizeof(special)-1;
264 memcpy (special, s, spec_len);
265 special[spec_len] = '\0';
268 if (!strcmp (special, "group"))
269 spec_src = zh->m_group;
270 else if (!strcmp (special, "database"))
271 spec_src = zh->basenames[0];
272 else if (!strcmp (special, "filename")) {
275 else if (!strcmp (special, "type"))
276 spec_src = zh->m_record_type;
281 strcpy (dst, spec_src);
282 dst += strlen (spec_src);
285 else if (*s == '\"' || *s == '\'')
287 int stopMarker = *s++;
291 while (*s && *s != stopMarker)
293 if (i+1 < sizeof(tmpString))
294 tmpString[i++] = *s++;
299 strcpy (dst, tmpString);
300 dst += strlen (tmpString);
304 yaz_log (YLOG_WARN, "Syntax error in match criteria %s in group %s",
305 spec, zh->m_group ? zh->m_group : "none");
312 yaz_log (YLOG_WARN, "No match criteria for record %s in group %s",
313 fname, zh->m_group ? zh->m_group : "none");
320 struct recordLogInfo {
323 struct recordGroup *rGroup;
326 static void init_extractCtrl(ZebraHandle zh, struct recExtractCtrl *ctrl)
329 for (i = 0; i<256; i++)
331 if (zebra_maps_is_positioned(zh->reg->zebra_maps, i))
336 ctrl->flagShowRecords = !zh->m_flag_rw;
339 static void all_matches_add(struct recExtractCtrl *ctrl)
342 extract_init(ctrl, &word);
343 word.index_name = "_ALLRECORDS";
344 word.index_type = 'w';
346 extract_add_index_string (&word, zinfo_index_category_alwaysmatches,
350 ZEBRA_RES zebra_extract_file(ZebraHandle zh, SYSNO *sysno, const char *fname,
353 ZEBRA_RES r = ZEBRA_OK;
358 struct file_read_info *fi = 0;
359 const char *original_record_type = 0;
361 void *recTypeClientData;
362 struct ZebraRecStream stream, *streamp;
364 zebra_init_log_level();
366 if (!zh->m_group || !*zh->m_group)
369 sprintf (gprefix, "%s.", zh->m_group);
371 yaz_log(log_level_extract, "zebra_extract_file %s", fname);
373 /* determine file extension */
375 for (i = strlen(fname); --i >= 0; )
378 else if (fname[i] == '.')
380 strcpy (ext, fname+i+1);
383 /* determine file type - depending on extension */
384 original_record_type = zh->m_record_type;
385 if (!zh->m_record_type)
387 sprintf (ext_res, "%srecordType.%s", gprefix, ext);
388 zh->m_record_type = res_get (zh->res, ext_res);
390 if (!zh->m_record_type)
392 if (zh->records_processed < zh->m_file_verbose_limit)
393 yaz_log (YLOG_LOG, "? %s", fname);
396 /* determine match criteria */
397 if (!zh->m_record_id)
399 sprintf (ext_res, "%srecordId.%s", gprefix, ext);
400 zh->m_record_id = res_get (zh->res, ext_res);
404 recType_byName (zh->reg->recTypes, zh->res, zh->m_record_type,
405 &recTypeClientData)))
407 yaz_log(YLOG_WARN, "No such record type: %s", zh->m_record_type);
411 switch(recType->version)
416 yaz_log(YLOG_WARN, "Bad filter version: %s", zh->m_record_type);
418 if (sysno && deleteFlag)
427 if (zh->path_reg && !yaz_is_abspath (fname))
429 strcpy (full_rep, zh->path_reg);
430 strcat (full_rep, "/");
431 strcat (full_rep, fname);
434 strcpy (full_rep, fname);
436 if ((fd = open (full_rep, O_BINARY|O_RDONLY)) == -1)
438 yaz_log (YLOG_WARN|YLOG_ERRNO, "open %s", full_rep);
439 zh->m_record_type = original_record_type;
443 zebra_create_stream_fd(streamp, fd, 0);
447 r = zebra_extract_record_stream(zh, streamp,
452 0, /*match_criteria */
454 1, /* force_update */
455 1, /* allow_update */
456 recType, recTypeClientData);
467 stream.destroy(streamp);
468 zh->m_record_type = original_record_type;
473 If sysno is provided, then it's used to identify the reocord.
474 If not, and match_criteria is provided, then sysno is guessed
475 If not, and a record is provided, then sysno is got from there
479 ZEBRA_RES zebra_buffer_extract_record(ZebraHandle zh,
480 const char *buf, size_t buf_size,
483 const char *recordType,
485 const char *match_criteria,
490 struct ZebraRecStream stream;
495 if (recordType && *recordType)
497 yaz_log(log_level_extract,
498 "Record type explicitly specified: %s", recordType);
499 recType = recType_byName (zh->reg->recTypes, zh->res, recordType,
504 if (!(zh->m_record_type))
506 yaz_log (YLOG_WARN, "No such record type defined");
509 yaz_log(log_level_extract, "Get record type from rgroup: %s",
511 recType = recType_byName (zh->reg->recTypes, zh->res,
512 zh->m_record_type, &clientData);
513 recordType = zh->m_record_type;
518 yaz_log (YLOG_WARN, "No such record type: %s", recordType);
524 zebra_create_stream_mem(&stream, buf, buf_size);
526 res = zebra_extract_record_stream(zh, &stream,
535 recType, clientData);
536 stream.destroy(&stream);
541 ZEBRA_RES zebra_extract_record_stream(ZebraHandle zh,
542 struct ZebraRecStream *stream,
545 const char *recordType,
547 const char *match_criteria,
552 void *recTypeClientData)
556 RecordAttr *recordAttr;
557 struct recExtractCtrl extractCtrl;
559 const char *matchStr = 0;
561 off_t start_offset = 0;
562 const char *pr_fname = fname; /* filename to print .. */
563 int show_progress = zh->records_processed < zh->m_file_verbose_limit ? 1:0;
565 zebra_init_log_level();
568 pr_fname = "<no file>"; /* make it printable if file is omitted */
570 zebra_rec_keys_reset(zh->reg->keys);
571 zebra_rec_keys_reset(zh->reg->sortKeys);
573 if (zebraExplain_curDatabase (zh->reg->zei, zh->basenames[0]))
575 if (zebraExplain_newDatabase (zh->reg->zei, zh->basenames[0],
576 zh->m_explain_database))
582 off_t null_offset = 0;
583 extractCtrl.stream = stream;
585 start_offset = stream->tellf(stream);
587 extractCtrl.first_record = start_offset ? 0 : 1;
589 stream->endf(stream, &null_offset);;
591 extractCtrl.init = extract_init;
592 extractCtrl.tokenAdd = extract_token_add;
593 extractCtrl.schemaAdd = extract_schema_add;
594 extractCtrl.dh = zh->reg->dh;
595 extractCtrl.handle = zh;
596 extractCtrl.match_criteria[0] = '\0';
597 extractCtrl.staticrank = 0;
600 init_extractCtrl(zh, &extractCtrl);
602 extract_set_store_data_prepare(&extractCtrl);
604 r = (*recType->extract)(recTypeClientData, &extractCtrl);
606 if (r == RECCTRL_EXTRACT_EOF)
608 else if (r == RECCTRL_EXTRACT_ERROR_GENERIC)
610 /* error occured during extraction ... */
611 yaz_log (YLOG_WARN, "extract error: generic");
614 else if (r == RECCTRL_EXTRACT_ERROR_NO_SUCH_FILTER)
616 /* error occured during extraction ... */
617 yaz_log (YLOG_WARN, "extract error: no such filter");
621 all_matches_add(&extractCtrl);
623 if (extractCtrl.match_criteria[0])
624 match_criteria = extractCtrl.match_criteria;
630 if (match_criteria && *match_criteria) {
631 matchStr = match_criteria;
633 if (zh->m_record_id && *zh->m_record_id) {
634 matchStr = fileMatchStr (zh, zh->reg->keys, pr_fname,
638 yaz_log (YLOG_WARN, "Bad match criteria (recordID)");
645 int db_ord = zebraExplain_get_database_ord(zh->reg->zei);
646 char *rinfo = dict_lookup_ord(zh->reg->matchDict, db_ord,
650 assert(*rinfo == sizeof(*sysno));
651 memcpy (sysno, rinfo+1, sizeof(*sysno));
655 if (zebra_rec_keys_empty(zh->reg->keys))
657 /* the extraction process returned no information - the record
658 is probably empty - unless flagShowRecords is in use */
668 yaz_log (YLOG_LOG, "delete %s %s " ZINT_FORMAT, recordType,
669 pr_fname, (zint) start_offset);
670 yaz_log (YLOG_WARN, "cannot delete record above (seems new)");
674 yaz_log (YLOG_LOG, "add %s %s " ZINT_FORMAT, recordType, pr_fname,
675 (zint) start_offset);
676 rec = rec_new (zh->reg->records);
680 recordAttr = rec_init_attr (zh->reg->zei, rec);
681 recordAttr->staticrank = extractCtrl.staticrank;
685 int db_ord = zebraExplain_get_database_ord(zh->reg->zei);
686 dict_insert_ord(zh->reg->matchDict, db_ord, matchStr,
687 sizeof(*sysno), sysno);
691 extract_flushSortKeys (zh, *sysno, 1, zh->reg->sortKeys);
692 extract_flushRecordKeys (zh, *sysno, 1, zh->reg->keys,
693 recordAttr->staticrank);
694 zh->records_inserted++;
698 /* record already exists */
699 zebra_rec_keys_t delkeys = zebra_rec_keys_open();
700 zebra_rec_keys_t sortKeys = zebra_rec_keys_open();
703 yaz_log (YLOG_LOG, "skipped %s %s " ZINT_FORMAT,
704 recordType, pr_fname, (zint) start_offset);
709 rec = rec_get (zh->reg->records, *sysno);
712 recordAttr = rec_init_attr (zh->reg->zei, rec);
714 zebra_rec_keys_set_buf(delkeys,
715 rec->info[recInfo_delKeys],
716 rec->size[recInfo_delKeys],
718 zebra_rec_keys_set_buf(sortKeys,
719 rec->info[recInfo_sortKeys],
720 rec->size[recInfo_sortKeys],
723 extract_flushSortKeys (zh, *sysno, 0, sortKeys);
724 extract_flushRecordKeys (zh, *sysno, 0, delkeys,
725 recordAttr->staticrank);
728 /* record going to be deleted */
729 if (zebra_rec_keys_empty(delkeys))
731 yaz_log(YLOG_LOG, "delete %s %s " ZINT_FORMAT, recordType,
732 pr_fname, (zint) start_offset);
733 yaz_log(YLOG_WARN, "cannot delete file above, "
734 "storeKeys false (3)");
739 yaz_log(YLOG_LOG, "delete %s %s " ZINT_FORMAT, recordType,
740 pr_fname, (zint) start_offset);
741 zh->records_deleted++;
744 int db_ord = zebraExplain_get_database_ord(zh->reg->zei);
745 dict_delete_ord(zh->reg->matchDict, db_ord, matchStr);
747 rec_del (zh->reg->records, &rec);
756 yaz_log(YLOG_LOG, "update %s %s " ZINT_FORMAT, recordType,
757 pr_fname, (zint) ZINT_FORMAT);
758 recordAttr->staticrank = extractCtrl.staticrank;
759 extract_flushSortKeys (zh, *sysno, 1, zh->reg->sortKeys);
760 extract_flushRecordKeys (zh, *sysno, 1, zh->reg->keys,
761 recordAttr->staticrank);
762 zh->records_updated++;
764 zebra_rec_keys_close(delkeys);
765 zebra_rec_keys_close(sortKeys);
767 /* update file type */
768 xfree (rec->info[recInfo_fileType]);
769 rec->info[recInfo_fileType] =
770 rec_strdup (recordType, &rec->size[recInfo_fileType]);
772 /* update filename */
773 xfree (rec->info[recInfo_filename]);
774 rec->info[recInfo_filename] =
775 rec_strdup (fname, &rec->size[recInfo_filename]);
777 /* update delete keys */
778 xfree (rec->info[recInfo_delKeys]);
779 if (!zebra_rec_keys_empty(zh->reg->keys) && zh->m_store_keys == 1)
781 zebra_rec_keys_get_buf(zh->reg->keys,
782 &rec->info[recInfo_delKeys],
783 &rec->size[recInfo_delKeys]);
787 rec->info[recInfo_delKeys] = NULL;
788 rec->size[recInfo_delKeys] = 0;
790 /* update sort keys */
791 xfree (rec->info[recInfo_sortKeys]);
793 zebra_rec_keys_get_buf(zh->reg->sortKeys,
794 &rec->info[recInfo_sortKeys],
795 &rec->size[recInfo_sortKeys]);
797 /* save file size of original record */
798 zebraExplain_recordBytesIncrement (zh->reg->zei,
799 - recordAttr->recordSize);
802 off_t end_offset = stream->endf(stream, 0);
805 end_offset = stream->tellf(stream);
807 stream->seekf(stream, end_offset);
809 recordAttr->recordSize = end_offset - start_offset;
810 zebraExplain_recordBytesIncrement(zh->reg->zei,
811 recordAttr->recordSize);
814 /* set run-number for this record */
815 recordAttr->runNumber =
816 zebraExplain_runNumberIncrement (zh->reg->zei, 0);
818 /* update store data */
819 xfree (rec->info[recInfo_storeData]);
821 /* update store data */
822 if (zh->store_data_buf)
824 rec->size[recInfo_storeData] = zh->store_data_size;
825 rec->info[recInfo_storeData] = zh->store_data_buf;
826 zh->store_data_buf = 0;
828 else if (zh->m_store_data)
830 off_t cur_offset = stream->tellf(stream);
832 rec->size[recInfo_storeData] = recordAttr->recordSize;
833 rec->info[recInfo_storeData] = (char *)
834 xmalloc (recordAttr->recordSize);
835 stream->seekf(stream, start_offset);
836 stream->readf(stream, rec->info[recInfo_storeData],
837 recordAttr->recordSize);
838 stream->seekf(stream, cur_offset);
842 rec->info[recInfo_storeData] = NULL;
843 rec->size[recInfo_storeData] = 0;
845 /* update database name */
846 xfree (rec->info[recInfo_databaseName]);
847 rec->info[recInfo_databaseName] =
848 rec_strdup (zh->basenames[0], &rec->size[recInfo_databaseName]);
851 recordAttr->recordOffset = start_offset;
853 /* commit this record */
854 rec_put (zh->reg->records, &rec);
859 ZEBRA_RES zebra_extract_explain(void *handle, Record rec, data1_node *n)
861 ZebraHandle zh = (ZebraHandle) handle;
862 struct recExtractCtrl extractCtrl;
864 if (zebraExplain_curDatabase (zh->reg->zei,
865 rec->info[recInfo_databaseName]))
868 if (zebraExplain_newDatabase (zh->reg->zei,
869 rec->info[recInfo_databaseName], 0))
873 zebra_rec_keys_reset(zh->reg->keys);
874 zebra_rec_keys_reset(zh->reg->sortKeys);
876 extractCtrl.init = extract_init;
877 extractCtrl.tokenAdd = extract_token_add;
878 extractCtrl.schemaAdd = extract_schema_add;
879 extractCtrl.dh = zh->reg->dh;
881 init_extractCtrl(zh, &extractCtrl);
883 extractCtrl.flagShowRecords = 0;
884 extractCtrl.match_criteria[0] = '\0';
885 extractCtrl.staticrank = 0;
886 extractCtrl.handle = handle;
887 extractCtrl.first_record = 1;
889 extract_set_store_data_prepare(&extractCtrl);
892 grs_extract_tree(&extractCtrl, n);
894 if (rec->size[recInfo_delKeys])
896 zebra_rec_keys_t delkeys = zebra_rec_keys_open();
898 zebra_rec_keys_t sortkeys = zebra_rec_keys_open();
900 zebra_rec_keys_set_buf(delkeys, rec->info[recInfo_delKeys],
901 rec->size[recInfo_delKeys],
903 extract_flushRecordKeys (zh, rec->sysno, 0, delkeys, 0);
904 zebra_rec_keys_close(delkeys);
906 zebra_rec_keys_set_buf(sortkeys, rec->info[recInfo_sortKeys],
907 rec->size[recInfo_sortKeys],
910 extract_flushSortKeys (zh, rec->sysno, 0, sortkeys);
911 zebra_rec_keys_close(sortkeys);
913 extract_flushRecordKeys (zh, rec->sysno, 1, zh->reg->keys, 0);
914 extract_flushSortKeys (zh, rec->sysno, 1, zh->reg->sortKeys);
916 xfree (rec->info[recInfo_delKeys]);
917 zebra_rec_keys_get_buf(zh->reg->keys,
918 &rec->info[recInfo_delKeys],
919 &rec->size[recInfo_delKeys]);
921 xfree (rec->info[recInfo_sortKeys]);
922 zebra_rec_keys_get_buf(zh->reg->sortKeys,
923 &rec->info[recInfo_sortKeys],
924 &rec->size[recInfo_sortKeys]);
928 void extract_rec_keys_log(ZebraHandle zh, int is_insert,
929 zebra_rec_keys_t reckeys,
932 if (zebra_rec_keys_rewind(reckeys))
937 NMEM nmem = nmem_create();
939 while(zebra_rec_keys_read(reckeys, &str, &slen, &key))
941 char keystr[200]; /* room for zints to print */
943 int ord = CAST_ZINT_TO_INT(key.mem[0]);
945 const char *string_index;
947 zebraExplain_lookup_ord(zh->reg->zei, ord, &index_type,
948 0/* db */, &string_index);
950 zebra_term_untrans_iconv(zh, nmem, index_type,
953 for (i = 0; i<key.len; i++)
955 sprintf(keystr + strlen(keystr), ZINT_FORMAT " ", key.mem[i]);
958 if (*str < CHR_BASE_CHAR)
961 char dst_buf[200]; /* room for special chars */
963 strcpy(dst_buf , "?");
965 if (!strcmp(str, ""))
966 strcpy(dst_buf, "alwaysmatches");
967 if (!strcmp(str, FIRST_IN_FIELD_STR))
968 strcpy(dst_buf, "firstinfield");
969 else if (!strcmp(str, CHR_UNKNOWN))
970 strcpy(dst_buf, "unknown");
971 else if (!strcmp(str, CHR_SPACE))
972 strcpy(dst_buf, "space");
974 for (i = 0; i<slen; i++)
976 sprintf(dst_buf + strlen(dst_buf), " %d", str[i] & 0xff);
978 yaz_log(level, "%s%c %s %s", keystr, index_type,
979 string_index, dst_buf);
983 yaz_log(level, "%s%c %s \"%s\"", keystr, index_type,
984 string_index, dst_term);
992 void extract_rec_keys_adjust(ZebraHandle zh, int is_insert,
993 zebra_rec_keys_t reckeys)
995 ZebraExplainInfo zei = zh->reg->zei;
999 struct ord_stat *next;
1002 if (zebra_rec_keys_rewind(reckeys))
1004 struct ord_stat *ord_list = 0;
1008 struct it_key key_in;
1009 while(zebra_rec_keys_read(reckeys, &str, &slen, &key_in))
1011 int ord = CAST_ZINT_TO_INT(key_in.mem[0]);
1013 for (p = ord_list; p ; p = p->next)
1021 p = xmalloc(sizeof(*p));
1032 struct ord_stat *p1 = p;
1035 zebraExplain_ord_adjust_occurrences(zei, p->ord, p->no, 1);
1037 zebraExplain_ord_adjust_occurrences(zei, p->ord, - p->no, -1);
1044 void extract_flushRecordKeys (ZebraHandle zh, SYSNO sysno,
1046 zebra_rec_keys_t reckeys,
1049 ZebraExplainInfo zei = zh->reg->zei;
1051 extract_rec_keys_adjust(zh, cmd, reckeys);
1053 if (log_level_details)
1055 yaz_log(log_level_details, "Keys for record " ZINT_FORMAT " %s",
1056 sysno, cmd ? "insert" : "delete");
1057 extract_rec_keys_log(zh, cmd, reckeys, log_level_details);
1060 if (!zh->reg->key_buf)
1062 int mem= 1024*1024* atoi( res_get_def( zh->res, "memmax", "8"));
1065 yaz_log(YLOG_WARN, "Invalid memory setting, using default 8 MB");
1068 /* FIXME: That "8" should be in a default settings include */
1069 /* not hard-coded here! -H */
1070 zh->reg->key_buf = (char**) xmalloc (mem);
1071 zh->reg->ptr_top = mem/sizeof(char*);
1073 zh->reg->key_buf_used = 0;
1074 zh->reg->key_file_no = 0;
1076 zebraExplain_recordCountIncrement (zei, cmd ? 1 : -1);
1078 if (zebra_rec_keys_rewind(reckeys))
1082 struct it_key key_in;
1083 while(zebra_rec_keys_read(reckeys, &str, &slen, &key_in))
1087 struct it_key key_out;
1089 assert(key_in.len >= 2);
1090 assert(key_in.len <= IT_KEY_LEVEL_MAX);
1092 /* check for buffer overflow */
1093 if (zh->reg->key_buf_used + 1024 >
1094 (zh->reg->ptr_top -zh->reg->ptr_i)*sizeof(char*))
1095 extract_flushWriteKeys (zh, 0);
1098 assert(zh->reg->ptr_i > 0);
1099 (zh->reg->key_buf)[zh->reg->ptr_top - zh->reg->ptr_i] =
1100 (char*)zh->reg->key_buf + zh->reg->key_buf_used;
1102 /* key_in.mem[0] ord/ch */
1103 /* key_in.mem[1] filter specified record ID */
1105 /* encode the ordinal value (field/use/attribute) .. */
1106 ch = CAST_ZINT_TO_INT(key_in.mem[0]);
1107 zh->reg->key_buf_used +=
1108 key_SU_encode(ch, (char*)zh->reg->key_buf +
1109 zh->reg->key_buf_used);
1111 /* copy the 0-terminated stuff from str to output */
1112 memcpy((char*)zh->reg->key_buf + zh->reg->key_buf_used, str, slen);
1113 zh->reg->key_buf_used += slen;
1114 ((char*)zh->reg->key_buf)[(zh->reg->key_buf_used)++] = '\0';
1116 /* the delete/insert indicator */
1117 ((char*)zh->reg->key_buf)[(zh->reg->key_buf_used)++] = cmd;
1119 if (zh->m_staticrank) /* rank config enabled ? */
1123 yaz_log(YLOG_WARN, "staticrank = %ld. Setting to 0",
1127 key_out.mem[j++] = staticrank;
1130 if (key_in.mem[1]) /* filter specified record ID */
1131 key_out.mem[j++] = key_in.mem[1];
1133 key_out.mem[j++] = sysno;
1134 for (i = 2; i < key_in.len; i++)
1135 key_out.mem[j++] = key_in.mem[i];
1138 memcpy((char*)zh->reg->key_buf + zh->reg->key_buf_used,
1139 &key_out, sizeof(key_out));
1140 (zh->reg->key_buf_used) += sizeof(key_out);
1145 void extract_flushWriteKeys (ZebraHandle zh, int final)
1146 /* optimizing: if final=1, and no files written yet */
1147 /* push the keys directly to merge, sidestepping the */
1148 /* temp file altogether. Speeds small updates */
1151 char out_fname[200];
1153 struct encode_info encode_info;
1154 int ptr_i = zh->reg->ptr_i;
1159 if (!zh->reg->key_buf || ptr_i <= 0)
1161 yaz_log(log_level_extract, " nothing to flush section=%d buf=%p i=%d",
1162 zh->reg->key_file_no, zh->reg->key_buf, ptr_i);
1166 (zh->reg->key_file_no)++;
1167 yaz_log (YLOG_LOG, "sorting section %d", (zh->reg->key_file_no));
1168 yaz_log(log_level_extract, " sort_buff at %p n=%d",
1169 zh->reg->key_buf + zh->reg->ptr_top - ptr_i,ptr_i);
1171 qsort (zh->reg->key_buf + zh->reg->ptr_top - ptr_i, ptr_i,
1172 sizeof(char*), key_qsort_compare);
1174 /* zebra.cfg: tempfiles:
1175 Y: always use temp files (old way)
1176 A: use temp files, if more than one (auto)
1177 = if this is both the last and the first
1178 N: never bother with temp files (new) */
1180 temp_policy=toupper(res_get_def(zh->res,"tempfiles","auto")[0]);
1181 if (temp_policy != 'Y' && temp_policy != 'N' && temp_policy != 'A') {
1182 yaz_log (YLOG_WARN, "Illegal tempfiles setting '%c'. using 'Auto' ",
1187 if ( ( temp_policy =='N' ) || /* always from memory */
1188 ( ( temp_policy =='A' ) && /* automatic */
1189 (zh->reg->key_file_no == 1) && /* this is first time */
1190 (final) ) ) /* and last (=only) time */
1191 { /* go directly from memory */
1192 zh->reg->key_file_no =0; /* signal not to read files */
1193 zebra_index_merge(zh);
1195 zh->reg->key_buf_used = 0;
1199 /* Not doing directly from memory, write into a temp file */
1200 extract_get_fname_tmp (zh, out_fname, zh->reg->key_file_no);
1202 if (!(outf = fopen (out_fname, "wb")))
1204 yaz_log (YLOG_FATAL|YLOG_ERRNO, "fopen %s", out_fname);
1205 zebra_exit("extract_flushWriteKeys");
1207 yaz_log (YLOG_LOG, "writing section %d", zh->reg->key_file_no);
1208 prevcp = cp = (zh->reg->key_buf)[zh->reg->ptr_top - ptr_i];
1210 encode_key_init (&encode_info);
1211 encode_key_write (cp, &encode_info, outf);
1215 cp = (zh->reg->key_buf)[zh->reg->ptr_top - ptr_i];
1216 if (strcmp (cp, prevcp))
1218 encode_key_flush ( &encode_info, outf);
1219 encode_key_init (&encode_info);
1220 encode_key_write (cp, &encode_info, outf);
1224 encode_key_write (cp + strlen(cp), &encode_info, outf);
1226 encode_key_flush ( &encode_info, outf);
1228 qsort (key_buf + ptr_top-ptr_i, ptr_i, sizeof(char*), key_x_compare);
1229 extract_get_fname_tmp (out_fname, key_file_no);
1231 if (!(outf = fopen (out_fname, "wb")))
1233 yaz_log (YLOG_FATAL|YLOG_ERRNO, "fopen %s", out_fname);
1234 zebra_exit("extract_flushWriteKeys");
1236 yaz_log (YLOG_LOG, "writing section %d", key_file_no);
1238 prevcp = key_buf[ptr_top-i];
1240 if (!--i || strcmp (prevcp, key_buf[ptr_top-i]))
1242 key_y_len = strlen(prevcp)+1;
1244 yaz_log (YLOG_LOG, "key_y_len: %2d %02x %02x %s",
1245 key_y_len, prevcp[0], prevcp[1], 2+prevcp);
1247 qsort (key_buf + ptr_top-ptr_i, ptr_i - i,
1248 sizeof(char*), key_y_compare);
1249 cp = key_buf[ptr_top-ptr_i];
1251 encode_key_init (&encode_info);
1252 encode_key_write (cp, &encode_info, outf);
1255 cp = key_buf[ptr_top-ptr_i];
1256 encode_key_write (cp+key_y_len, &encode_info, outf);
1258 encode_key_flush ( &encode_info, outf);
1261 prevcp = key_buf[ptr_top-ptr_i];
1266 yaz_log (YLOG_FATAL|YLOG_ERRNO, "fclose %s", out_fname);
1267 zebra_exit("extract_flushWriteKeys");
1269 yaz_log (YLOG_LOG, "finished section %d", zh->reg->key_file_no);
1271 zh->reg->key_buf_used = 0;
1274 ZEBRA_RES zebra_rec_keys_to_snippets(ZebraHandle zh,
1275 zebra_rec_keys_t reckeys,
1276 zebra_snippets *snippets)
1278 NMEM nmem = nmem_create();
1279 if (zebra_rec_keys_rewind(reckeys))
1284 while (zebra_rec_keys_read(reckeys, &str, &slen, &key))
1286 char dst_buf[IT_MAX_WORD];
1287 char *dst_term = dst_buf;
1292 assert(key.len <= IT_KEY_LEVEL_MAX && key.len > 2);
1293 seqno = key.mem[key.len-1];
1294 ord = CAST_ZINT_TO_INT(key.mem[0]);
1296 zebraExplain_lookup_ord(zh->reg->zei, ord, &index_type,
1297 0/* db */, 0 /* string_index */);
1299 zebra_term_untrans_iconv(zh, nmem, index_type,
1301 zebra_snippets_append(snippets, seqno, ord, dst_term);
1309 void print_rec_keys(ZebraHandle zh, zebra_rec_keys_t reckeys)
1311 yaz_log(YLOG_LOG, "print_rec_keys");
1312 if (zebra_rec_keys_rewind(reckeys))
1317 while (zebra_rec_keys_read(reckeys, &str, &slen, &key))
1319 char dst_buf[IT_MAX_WORD];
1322 int ord = CAST_ZINT_TO_INT(key.mem[0]);
1324 assert(key.len <= IT_KEY_LEVEL_MAX && key.len > 2);
1326 zebraExplain_lookup_ord(zh->reg->zei, ord, &index_type, &db, 0);
1328 seqno = key.mem[key.len-1];
1330 zebra_term_untrans(zh, index_type, dst_buf, str);
1332 yaz_log(YLOG_LOG, "ord=%d seqno=" ZINT_FORMAT
1333 " term=%s", ord, seqno, dst_buf);
1338 static void extract_add_index_string(RecWord *p, zinfo_index_category_t cat,
1339 const char *str, int length)
1342 ZebraHandle zh = p->extractCtrl->handle;
1343 ZebraExplainInfo zei = zh->reg->zei;
1346 ch = zebraExplain_lookup_attr_str(zei, cat, p->index_type, p->index_name);
1348 ch = zebraExplain_add_attr_str(zei, cat, p->index_type, p->index_name);
1352 key.mem[i++] = p->record_id;
1353 key.mem[i++] = p->section_id;
1355 if (zh->m_segment_indexing)
1356 key.mem[i++] = p->segment;
1357 key.mem[i++] = p->seqno;
1360 zebra_rec_keys_write(zh->reg->keys, str, length, &key);
1363 static void extract_add_sort_string(RecWord *p, const char *str, int length)
1366 ZebraHandle zh = p->extractCtrl->handle;
1367 ZebraExplainInfo zei = zh->reg->zei;
1369 zinfo_index_category_t cat = zinfo_index_category_sort;
1371 ch = zebraExplain_lookup_attr_str(zei, cat, p->index_type, p->index_name);
1373 ch = zebraExplain_add_attr_str(zei, cat, p->index_type, p->index_name);
1376 key.mem[1] = p->record_id;
1378 zebra_rec_keys_write(zh->reg->sortKeys, str, length, &key);
1381 static void extract_add_string(RecWord *p, const char *string, int length)
1383 ZebraHandle zh = p->extractCtrl->handle;
1384 assert (length > 0);
1389 if (zebra_maps_is_sort(zh->reg->zebra_maps, p->index_type))
1390 extract_add_sort_string(p, string, length);
1393 extract_add_index_string(p, zinfo_index_category_index,
1395 if (zebra_maps_is_alwaysmatches(zh->reg->zebra_maps, p->index_type))
1398 memcpy(&word, p, sizeof(word));
1401 extract_add_index_string(
1402 &word, zinfo_index_category_alwaysmatches, "", 0);
1407 static void extract_add_incomplete_field(RecWord *p)
1409 ZebraHandle zh = p->extractCtrl->handle;
1410 const char *b = p->term_buf;
1411 int remain = p->term_len;
1412 const char **map = 0;
1415 map = zebra_maps_input(zh->reg->zebra_maps, p->index_type, &b, remain, 0);
1419 if (zebra_maps_is_first_in_field(zh->reg->zebra_maps, p->index_type))
1421 /* first in field marker */
1422 extract_add_string(p, FIRST_IN_FIELD_STR, FIRST_IN_FIELD_LEN);
1428 char buf[IT_MAX_WORD+1];
1432 while (map && *map && **map == *CHR_SPACE)
1434 remain = p->term_len - (b - p->term_buf);
1436 map = zebra_maps_input(zh->reg->zebra_maps, p->index_type, &b,
1444 while (map && *map && **map != *CHR_SPACE)
1446 const char *cp = *map;
1448 while (i < IT_MAX_WORD && *cp)
1450 remain = p->term_len - (b - p->term_buf);
1452 map = zebra_maps_input(zh->reg->zebra_maps, p->index_type, &b, remain, 0);
1458 extract_add_string (p, buf, i);
1463 static void extract_add_complete_field (RecWord *p)
1465 ZebraHandle zh = p->extractCtrl->handle;
1466 const char *b = p->term_buf;
1467 char buf[IT_MAX_WORD+1];
1468 const char **map = 0;
1469 int i = 0, remain = p->term_len;
1472 map = zebra_maps_input (zh->reg->zebra_maps, p->index_type, &b, remain, 1);
1474 while (remain > 0 && i < IT_MAX_WORD)
1476 while (map && *map && **map == *CHR_SPACE)
1478 remain = p->term_len - (b - p->term_buf);
1482 int first = i ? 0 : 1; /* first position */
1483 map = zebra_maps_input(zh->reg->zebra_maps, p->index_type, &b, remain, first);
1491 if (i && i < IT_MAX_WORD)
1492 buf[i++] = *CHR_SPACE;
1493 while (map && *map && **map != *CHR_SPACE)
1495 const char *cp = *map;
1497 if (**map == *CHR_CUT)
1503 if (i >= IT_MAX_WORD)
1505 while (i < IT_MAX_WORD && *cp)
1508 remain = p->term_len - (b - p->term_buf);
1511 map = zebra_maps_input (zh->reg->zebra_maps, p->index_type, &b,
1520 extract_add_string (p, buf, i);
1523 static void extract_token_add(RecWord *p)
1525 ZebraHandle zh = p->extractCtrl->handle;
1528 if (log_level_extract)
1530 yaz_log(log_level_extract, "extract_token_add "
1531 "type=%c index=%s seqno=" ZINT_FORMAT " s=%.*s",
1532 p->index_type, p->index_name,
1533 p->seqno, p->term_len, p->term_buf);
1535 if ((wrbuf = zebra_replace(zh->reg->zebra_maps, p->index_type, 0,
1536 p->term_buf, p->term_len)))
1538 p->term_buf = wrbuf_buf(wrbuf);
1539 p->term_len = wrbuf_len(wrbuf);
1541 if (zebra_maps_is_complete (zh->reg->zebra_maps, p->index_type))
1542 extract_add_complete_field (p);
1544 extract_add_incomplete_field(p);
1547 static void extract_set_store_data_cb(struct recExtractCtrl *p,
1548 void *buf, size_t sz)
1550 ZebraHandle zh = (ZebraHandle) p->handle;
1552 xfree(zh->store_data_buf);
1553 zh->store_data_buf = 0;
1554 zh->store_data_size = 0;
1557 zh->store_data_buf = xmalloc(sz);
1558 zh->store_data_size = sz;
1559 memcpy(zh->store_data_buf, buf, sz);
1563 static void extract_set_store_data_prepare(struct recExtractCtrl *p)
1565 ZebraHandle zh = (ZebraHandle) p->handle;
1566 xfree(zh->store_data_buf);
1567 zh->store_data_buf = 0;
1568 zh->store_data_size = 0;
1569 p->setStoreData = extract_set_store_data_cb;
1572 static void extract_schema_add (struct recExtractCtrl *p, Odr_oid *oid)
1574 ZebraHandle zh = (ZebraHandle) p->handle;
1575 zebraExplain_addSchema (zh->reg->zei, oid);
1578 void extract_flushSortKeys (ZebraHandle zh, SYSNO sysno,
1579 int cmd, zebra_rec_keys_t reckeys)
1581 if (zebra_rec_keys_rewind(reckeys))
1583 SortIdx sortIdx = zh->reg->sortIdx;
1586 struct it_key key_in;
1588 sortIdx_sysno (sortIdx, sysno);
1590 while (zebra_rec_keys_read(reckeys, &str, &slen, &key_in))
1592 int ord = CAST_ZINT_TO_INT(key_in.mem[0]);
1594 sortIdx_type(sortIdx, ord);
1596 sortIdx_add(sortIdx, str, slen);
1598 sortIdx_add(sortIdx, "", 1);
1603 static void encode_key_init(struct encode_info *i)
1605 i->encode_handle = iscz1_start();
1606 i->decode_handle = iscz1_start();
1609 static void encode_key_write (char *k, struct encode_info *i, FILE *outf)
1612 char *bp = i->buf, *bp0;
1613 const char *src = (char *) &key;
1615 /* copy term to output buf */
1616 while ((*bp++ = *k++))
1618 /* and copy & align key so we can mangle */
1619 memcpy (&key, k+1, sizeof(struct it_key)); /* *k is insert/delete */
1623 key_logdump_txt(YLOG_LOG, &key, *k ? "i" : "d");
1625 assert(key.mem[0] >= 0);
1628 iscz1_encode(i->encode_handle, &bp, &src);
1630 *bp0 = (*k * 128) + bp - bp0 - 1; /* length and insert/delete combined */
1631 if (fwrite (i->buf, bp - i->buf, 1, outf) != 1)
1633 yaz_log (YLOG_FATAL|YLOG_ERRNO, "fwrite");
1634 zebra_exit("encode_key_write");
1642 const char *src = bp0+1;
1643 char *dst = (char*) &key2;
1644 iscz1_decode(i->decode_handle, &dst, &src);
1646 key_logdump_txt(YLOG_LOG, &key2, *k ? "i" : "d");
1648 assert(key2.mem[1]);
1653 static void encode_key_flush (struct encode_info *i, FILE *outf)
1655 iscz1_stop(i->encode_handle);
1656 iscz1_stop(i->decode_handle);
1662 * indent-tabs-mode: nil
1664 * vim: shiftwidth=4 tabstop=8 expandtab