1 /* $Id: extract.c,v 1.216 2006-05-19 23:20:24 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 Zebra; see the file LICENSE.zebra. If not, write to the
19 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
40 #define PRINTF_OFF_T "%I64d"
43 #if SIZEOF_OFF_T == SIZEOF_LONG_LONG
44 #define PRINTF_OFF_T "%lld"
46 #define PRINTF_OFF_T "%ld"
52 #define USE_SHELLSORT 0
55 static void shellsort(void *ar, int r, size_t s,
56 int (*cmp)(const void *a, const void *b))
61 static const int incs[16] = { 1391376, 463792, 198768, 86961, 33936,
62 13776, 4592, 1968, 861, 336,
63 112, 48, 21, 7, 3, 1 };
64 for ( k = 0; k < 16; k++)
65 for (h = incs[k], i = h; i < r; i++)
69 while (j > h && (*cmp)(a + s*(j-h), v) > 0)
71 memcpy (a + s*j, a + s*(j-h), s);
79 static void logRecord (ZebraHandle zh)
81 ++zh->records_processed;
82 if (!(zh->records_processed % 1000))
84 yaz_log (YLOG_LOG, "Records: "ZINT_FORMAT" i/u/d "
85 ZINT_FORMAT"/"ZINT_FORMAT"/"ZINT_FORMAT,
86 zh->records_processed, zh->records_inserted, zh->records_updated,
91 static void extract_add_index_string (RecWord *p, const char *str, int length);
93 static void extract_set_store_data_prepare(struct recExtractCtrl *p);
95 static void extract_init (struct recExtractCtrl *p, RecWord *w)
97 w->zebra_maps = p->zebra_maps;
99 w->index_name = "any";
106 static void searchRecordKey(ZebraHandle zh,
107 zebra_rec_keys_t reckeys,
108 const char *index_name,
109 const char **ws, int ws_length)
114 for (i = 0; i<ws_length; i++)
118 ch = zebraExplain_lookup_attr_str(zh->reg->zei, '0', index_name);
120 ch = zebraExplain_lookup_attr_str(zh->reg->zei, 'p', index_name);
122 ch = zebraExplain_lookup_attr_str(zh->reg->zei, 'w', index_name);
127 if (zebra_rec_keys_rewind(reckeys))
134 while (zebra_rec_keys_read(reckeys, &str, &slen, &key))
136 assert(key.len <= 4 && key.len > 2);
138 seqno = key.mem[key.len-1];
140 if (key.mem[0] == ch)
146 woff = seqno - startSeq;
147 if (woff >= 0 && woff < ws_length)
154 struct file_read_info {
155 off_t file_max; /* maximum offset so far */
156 off_t file_offset; /* current offset */
157 off_t file_moffset; /* offset of rec/rec boundary */
162 static struct file_read_info *file_read_start (int fd)
164 struct file_read_info *fi = (struct file_read_info *)
165 xmalloc (sizeof(*fi));
169 fi->file_moffset = 0;
175 static void file_read_stop (struct file_read_info *fi)
180 static off_t file_seek (void *handle, off_t offset)
182 struct file_read_info *p = (struct file_read_info *) handle;
183 p->file_offset = offset;
184 return lseek (p->fd, offset, SEEK_SET);
187 static off_t file_tell (void *handle)
189 struct file_read_info *p = (struct file_read_info *) handle;
190 return p->file_offset;
193 static int file_read (void *handle, char *buf, size_t count)
195 struct file_read_info *p = (struct file_read_info *) handle;
198 r = read (fd, buf, count);
202 if (p->file_offset > p->file_max)
203 p->file_max = p->file_offset;
208 static void file_end (void *handle, off_t offset)
210 struct file_read_info *p = (struct file_read_info *) handle;
212 if (offset != p->file_moffset)
214 p->file_moffset = offset;
219 #define FILE_MATCH_BLANK "\t "
221 static char *fileMatchStr (ZebraHandle zh,
222 zebra_rec_keys_t reckeys,
223 const char *fname, const char *spec)
225 static char dstBuf[2048]; /* static here ??? */
227 const char *s = spec;
231 for (; *s && strchr(FILE_MATCH_BLANK, *s); s++)
238 char attset_str[64], attname_str[64];
242 for (s++; strchr(FILE_MATCH_BLANK, *s); s++)
244 for (i = 0; *s && *s != ',' && *s != ')' &&
245 !strchr(FILE_MATCH_BLANK, *s); s++)
246 if (i+1 < sizeof(attset_str))
247 attset_str[i++] = *s;
248 attset_str[i] = '\0';
250 for (; strchr(FILE_MATCH_BLANK, *s); s++)
253 strcpy(attname_str, attset_str);
256 for (s++; strchr(FILE_MATCH_BLANK, *s); s++)
258 for (i = 0; *s && *s != ')' &&
259 !strchr(FILE_MATCH_BLANK, *s); s++)
260 if (i+1 < sizeof(attname_str))
261 attname_str[i++] = *s;
262 attname_str[i] = '\0';
265 searchRecordKey (zh, reckeys, attname_str, ws, 32);
269 yaz_log (YLOG_WARN, "Missing ) in match criteria %s in group %s",
270 spec, zh->m_group ? zh->m_group : "none");
275 for (i = 0; i<32; i++)
284 dst += strlen(ws[i]);
288 yaz_log (YLOG_WARN, "Record didn't contain match"
289 " fields in (%s,%s)", attset_str, attname_str);
297 const char *spec_src = NULL;
298 const char *s1 = ++s;
299 while (*s1 && !strchr(FILE_MATCH_BLANK, *s1))
303 if (spec_len > sizeof(special)-1)
304 spec_len = sizeof(special)-1;
305 memcpy (special, s, spec_len);
306 special[spec_len] = '\0';
309 if (!strcmp (special, "group"))
310 spec_src = zh->m_group;
311 else if (!strcmp (special, "database"))
312 spec_src = zh->basenames[0];
313 else if (!strcmp (special, "filename")) {
316 else if (!strcmp (special, "type"))
317 spec_src = zh->m_record_type;
322 strcpy (dst, spec_src);
323 dst += strlen (spec_src);
326 else if (*s == '\"' || *s == '\'')
328 int stopMarker = *s++;
332 while (*s && *s != stopMarker)
334 if (i+1 < sizeof(tmpString))
335 tmpString[i++] = *s++;
340 strcpy (dst, tmpString);
341 dst += strlen (tmpString);
345 yaz_log (YLOG_WARN, "Syntax error in match criteria %s in group %s",
346 spec, zh->m_group ? zh->m_group : "none");
353 yaz_log (YLOG_WARN, "No match criteria for record %s in group %s",
354 fname, zh->m_group ? zh->m_group : "none");
361 struct recordLogInfo {
364 struct recordGroup *rGroup;
367 static void init_extractCtrl(ZebraHandle zh, struct recExtractCtrl *ctrl)
370 for (i = 0; i<256; i++)
372 if (zebra_maps_is_positioned(zh->reg->zebra_maps, i))
377 ctrl->zebra_maps = zh->reg->zebra_maps;
378 ctrl->flagShowRecords = !zh->m_flag_rw;
381 static void all_matches_add(struct recExtractCtrl *ctrl)
384 extract_init(ctrl, &word);
385 word.index_name = "allrecords";
386 word.index_type = 'w';
388 extract_add_index_string (&word, "", 0);
391 static ZEBRA_RES file_extract_record(ZebraHandle zh,
392 SYSNO *sysno, const char *fname,
394 struct file_read_info *fi,
397 void *recTypeClientData)
399 const char *match_str_to_print = "";
400 RecordAttr *recordAttr;
402 const char *matchStr = 0;
405 off_t recordOffset = 0;
406 struct recExtractCtrl extractCtrl;
408 /* announce database */
409 if (zebraExplain_curDatabase (zh->reg->zei, zh->basenames[0]))
411 if (zebraExplain_newDatabase (zh->reg->zei, zh->basenames[0],
412 zh->m_explain_database))
418 /* we are going to read from a file, so prepare the extraction */
419 zebra_rec_keys_reset(zh->reg->keys);
421 zebra_rec_keys_reset(zh->reg->sortKeys);
422 recordOffset = fi->file_moffset;
423 extractCtrl.handle = zh;
424 extractCtrl.offset = fi->file_moffset;
425 extractCtrl.readf = file_read;
426 extractCtrl.seekf = file_seek;
427 extractCtrl.tellf = file_tell;
428 extractCtrl.endf = file_end;
430 extractCtrl.init = extract_init;
431 extractCtrl.tokenAdd = extract_token_add;
432 extractCtrl.schemaAdd = extract_schema_add;
433 extractCtrl.dh = zh->reg->dh;
434 extractCtrl.match_criteria[0] = '\0';
435 extractCtrl.staticrank = 0;
437 extractCtrl.first_record = fi->file_offset ? 0 : 1;
439 extract_set_store_data_prepare(&extractCtrl);
441 init_extractCtrl(zh, &extractCtrl);
444 printf ("File: %s " PRINTF_OFF_T "\n", fname, recordOffset);
448 sprintf (msg, "%s:" PRINTF_OFF_T , fname, recordOffset);
449 yaz_log_init_prefix2 (msg);
452 r = (*recType->extract)(recTypeClientData, &extractCtrl);
454 yaz_log_init_prefix2 (0);
455 if (r == RECCTRL_EXTRACT_EOF)
457 else if (r == RECCTRL_EXTRACT_ERROR_GENERIC)
459 /* error occured during extraction ... */
461 zh->records_processed < zh->m_file_verbose_limit)
463 yaz_log (YLOG_WARN, "fail %s %s " PRINTF_OFF_T, zh->m_record_type,
464 fname, recordOffset);
468 else if (r == RECCTRL_EXTRACT_ERROR_NO_SUCH_FILTER)
470 /* error occured during extraction ... */
472 zh->records_processed < zh->m_file_verbose_limit)
474 yaz_log (YLOG_WARN, "no filter for %s %s "
475 PRINTF_OFF_T, zh->m_record_type,
476 fname, recordOffset);
480 all_matches_add(&extractCtrl);
481 if (extractCtrl.match_criteria[0])
482 matchStr = extractCtrl.match_criteria;
485 /* if matchStr is set now - we assume it's printable .
486 For internal matchStr (see below) we don't print */
488 match_str_to_print = matchStr;
490 /* perform internal match if sysno not known and if match criteria is
497 if (matchStr == 0 && zh->m_record_id && *zh->m_record_id)
499 matchStr = fileMatchStr (zh, zh->reg->keys, fname,
503 yaz_log(YLOG_WARN, "Bad match criteria");
505 if (zebra_rec_keys_empty(zh->reg->keys))
507 yaz_log(YLOG_WARN, "And no index keys");
514 int db_ord = zebraExplain_get_database_ord(zh->reg->zei);
515 char *rinfo = dict_lookup_ord(zh->reg->matchDict, db_ord,
519 assert(*rinfo == sizeof(*sysno));
520 memcpy (sysno, rinfo+1, sizeof(*sysno));
524 if (! *sysno && zebra_rec_keys_empty(zh->reg->keys) )
526 /* the extraction process returned no information - the record
527 is probably empty - unless flagShowRecords is in use */
531 if (zh->records_processed < zh->m_file_verbose_limit)
532 yaz_log (YLOG_WARN, "empty %s %s " PRINTF_OFF_T, zh->m_record_type,
533 fname, recordOffset);
542 yaz_log (YLOG_LOG, "delete %s %s " PRINTF_OFF_T, zh->m_record_type,
543 fname, recordOffset);
544 yaz_log (YLOG_WARN, "cannot delete record above (seems new)");
548 rec = rec_new (zh->reg->records);
552 if (zh->records_processed < zh->m_file_verbose_limit)
554 yaz_log(YLOG_LOG, "add %s %s " PRINTF_OFF_T
555 " " ZINT_FORMAT " %s" ,
557 fname, recordOffset, *sysno, match_str_to_print);
559 recordAttr = rec_init_attr (zh->reg->zei, rec);
560 recordAttr->staticrank = extractCtrl.staticrank;
564 int db_ord = zebraExplain_get_database_ord(zh->reg->zei);
565 dict_insert_ord(zh->reg->matchDict, db_ord, matchStr,
566 sizeof(*sysno), sysno);
570 extract_flushSortKeys (zh, *sysno, 1, zh->reg->sortKeys);
571 extract_flushRecordKeys (zh, *sysno, 1, zh->reg->keys,
572 recordAttr->staticrank);
573 zh->records_inserted++;
577 /* record already exists */
578 zebra_rec_keys_t delkeys = zebra_rec_keys_open();
580 zebra_rec_keys_t sortKeys = zebra_rec_keys_open();
582 rec = rec_get (zh->reg->records, *sysno);
585 recordAttr = rec_init_attr (zh->reg->zei, rec);
587 zebra_rec_keys_set_buf(delkeys,
588 rec->info[recInfo_delKeys],
589 rec->size[recInfo_delKeys],
592 zebra_rec_keys_set_buf(sortKeys,
593 rec->info[recInfo_sortKeys],
594 rec->size[recInfo_sortKeys],
596 extract_flushSortKeys (zh, *sysno, 0, sortKeys);
597 extract_flushRecordKeys (zh, *sysno, 0, delkeys,
598 recordAttr->staticrank); /* old values */
601 /* record going to be deleted */
602 if (zebra_rec_keys_empty(delkeys))
604 yaz_log (YLOG_LOG, "delete %s %s " PRINTF_OFF_T
606 zh->m_record_type, fname, recordOffset, *sysno);
607 yaz_log (YLOG_WARN, "cannot delete file above, storeKeys false (1)");
611 if (zh->records_processed < zh->m_file_verbose_limit)
613 yaz_log(YLOG_LOG, "delete %s %s " PRINTF_OFF_T
614 " " ZINT_FORMAT " %s" ,
616 fname, recordOffset, *sysno, match_str_to_print);
618 zh->records_deleted++;
621 int db_ord = zebraExplain_get_database_ord(zh->reg->zei);
622 dict_delete_ord(zh->reg->matchDict, db_ord, matchStr);
624 rec_del (zh->reg->records, &rec);
632 /* flush new keys for sort&search etc */
633 if (zh->records_processed < zh->m_file_verbose_limit)
635 yaz_log(YLOG_LOG, "update %s %s " PRINTF_OFF_T
636 " " ZINT_FORMAT " %s" ,
638 fname, recordOffset, *sysno, match_str_to_print);
640 recordAttr->staticrank = extractCtrl.staticrank;
641 extract_flushSortKeys (zh, *sysno, 1, zh->reg->sortKeys);
642 extract_flushRecordKeys (zh, *sysno, 1, zh->reg->keys,
643 recordAttr->staticrank);
644 zh->records_updated++;
646 zebra_rec_keys_close(delkeys);
647 zebra_rec_keys_close(sortKeys);
649 /* update file type */
650 xfree (rec->info[recInfo_fileType]);
651 rec->info[recInfo_fileType] =
652 rec_strdup (zh->m_record_type, &rec->size[recInfo_fileType]);
654 /* update filename */
655 xfree (rec->info[recInfo_filename]);
656 rec->info[recInfo_filename] =
657 rec_strdup (fname, &rec->size[recInfo_filename]);
659 /* update delete keys */
660 xfree (rec->info[recInfo_delKeys]);
661 if (!zebra_rec_keys_empty(zh->reg->keys) && zh->m_store_keys == 1)
663 zebra_rec_keys_get_buf(zh->reg->keys,
664 &rec->info[recInfo_delKeys],
665 &rec->size[recInfo_delKeys]);
669 rec->info[recInfo_delKeys] = NULL;
670 rec->size[recInfo_delKeys] = 0;
673 /* update sort keys */
674 xfree (rec->info[recInfo_sortKeys]);
676 zebra_rec_keys_get_buf(zh->reg->sortKeys,
677 &rec->info[recInfo_sortKeys],
678 &rec->size[recInfo_sortKeys]);
680 /* save file size of original record */
681 zebraExplain_recordBytesIncrement (zh->reg->zei,
682 - recordAttr->recordSize);
683 recordAttr->recordSize = fi->file_moffset - recordOffset;
684 if (!recordAttr->recordSize)
685 recordAttr->recordSize = fi->file_max - recordOffset;
686 zebraExplain_recordBytesIncrement (zh->reg->zei,
687 recordAttr->recordSize);
689 /* set run-number for this record */
690 recordAttr->runNumber = zebraExplain_runNumberIncrement (zh->reg->zei,
693 /* update store data */
694 xfree (rec->info[recInfo_storeData]);
695 if (zh->store_data_buf)
697 rec->size[recInfo_storeData] = zh->store_data_size;
698 rec->info[recInfo_storeData] = zh->store_data_buf;
699 zh->store_data_buf = 0;
701 else if (zh->m_store_data)
703 rec->size[recInfo_storeData] = recordAttr->recordSize;
704 rec->info[recInfo_storeData] = (char *)
705 xmalloc (recordAttr->recordSize);
706 if (lseek (fi->fd, recordOffset, SEEK_SET) < 0)
708 yaz_log (YLOG_ERRNO|YLOG_FATAL, "seek to " PRINTF_OFF_T " in %s",
709 recordOffset, fname);
712 if (read (fi->fd, rec->info[recInfo_storeData], recordAttr->recordSize)
713 < recordAttr->recordSize)
715 yaz_log (YLOG_ERRNO|YLOG_FATAL, "read %d bytes of %s",
716 recordAttr->recordSize, fname);
722 rec->info[recInfo_storeData] = NULL;
723 rec->size[recInfo_storeData] = 0;
725 /* update database name */
726 xfree (rec->info[recInfo_databaseName]);
727 rec->info[recInfo_databaseName] =
728 rec_strdup (zh->basenames[0], &rec->size[recInfo_databaseName]);
731 recordAttr->recordOffset = recordOffset;
733 /* commit this record */
734 rec_put (zh->reg->records, &rec);
739 ZEBRA_RES zebra_extract_file(ZebraHandle zh, SYSNO *sysno, const char *fname,
742 ZEBRA_RES r = ZEBRA_OK;
747 struct file_read_info *fi;
748 const char *original_record_type = 0;
750 void *recTypeClientData;
752 if (!zh->m_group || !*zh->m_group)
755 sprintf (gprefix, "%s.", zh->m_group);
757 yaz_log (YLOG_DEBUG, "fileExtract %s", fname);
759 /* determine file extension */
761 for (i = strlen(fname); --i >= 0; )
764 else if (fname[i] == '.')
766 strcpy (ext, fname+i+1);
769 /* determine file type - depending on extension */
770 original_record_type = zh->m_record_type;
771 if (!zh->m_record_type)
773 sprintf (ext_res, "%srecordType.%s", gprefix, ext);
774 zh->m_record_type = res_get (zh->res, ext_res);
776 if (!zh->m_record_type)
778 if (zh->records_processed < zh->m_file_verbose_limit)
779 yaz_log (YLOG_LOG, "? %s", fname);
782 /* determine match criteria */
783 if (!zh->m_record_id)
785 sprintf (ext_res, "%srecordId.%s", gprefix, ext);
786 zh->m_record_id = res_get (zh->res, ext_res);
790 recType_byName (zh->reg->recTypes, zh->res, zh->m_record_type,
791 &recTypeClientData)))
793 yaz_log(YLOG_WARN, "No such record type: %s", zh->m_record_type);
797 switch(recType->version)
802 yaz_log(YLOG_WARN, "Bad filter version: %s", zh->m_record_type);
804 if (sysno && deleteFlag)
810 if (zh->path_reg && !yaz_is_abspath (fname))
812 strcpy (full_rep, zh->path_reg);
813 strcat (full_rep, "/");
814 strcat (full_rep, fname);
817 strcpy (full_rep, fname);
819 if ((fd = open (full_rep, O_BINARY|O_RDONLY)) == -1)
821 yaz_log (YLOG_WARN|YLOG_ERRNO, "open %s", full_rep);
822 zh->m_record_type = original_record_type;
826 fi = file_read_start (fd);
829 fi->file_moffset = fi->file_offset;
830 fi->file_more = 0; /* file_end not called (yet) */
831 r = file_extract_record (zh, sysno, fname, deleteFlag, fi, 1,
832 recType, recTypeClientData);
834 { /* file_end has been called so reset offset .. */
835 fi->file_offset = fi->file_moffset;
836 lseek(fi->fd, fi->file_moffset, SEEK_SET);
850 zh->m_record_type = original_record_type;
855 If sysno is provided, then it's used to identify the reocord.
856 If not, and match_criteria is provided, then sysno is guessed
857 If not, and a record is provided, then sysno is got from there
860 ZEBRA_RES buffer_extract_record(ZebraHandle zh,
861 const char *buf, size_t buf_size,
864 const char *recordType,
866 const char *match_criteria,
872 RecordAttr *recordAttr;
873 struct recExtractCtrl extractCtrl;
875 const char *matchStr = 0;
876 RecType recType = NULL;
879 long recordOffset = 0;
880 struct zebra_fetch_control fc;
881 const char *pr_fname = fname; /* filename to print .. */
882 int show_progress = zh->records_processed < zh->m_file_verbose_limit ? 1:0;
885 pr_fname = "<no file>"; /* make it printable if file is omitted */
888 fc.record_int_buf = buf;
889 fc.record_int_len = buf_size;
890 fc.record_int_pos = 0;
892 fc.record_offset = 0;
894 extractCtrl.offset = 0;
895 extractCtrl.readf = zebra_record_int_read;
896 extractCtrl.seekf = zebra_record_int_seek;
897 extractCtrl.tellf = zebra_record_int_tell;
898 extractCtrl.endf = zebra_record_int_end;
899 extractCtrl.first_record = 1;
900 extractCtrl.fh = &fc;
902 zebra_rec_keys_reset(zh->reg->keys);
903 zebra_rec_keys_reset(zh->reg->sortKeys);
905 if (zebraExplain_curDatabase (zh->reg->zei, zh->basenames[0]))
907 if (zebraExplain_newDatabase (zh->reg->zei, zh->basenames[0],
908 zh->m_explain_database))
912 if (recordType && *recordType)
914 yaz_log (YLOG_DEBUG, "Record type explicitly specified: %s", recordType);
915 recType = recType_byName (zh->reg->recTypes, zh->res, recordType,
920 if (!(zh->m_record_type))
922 yaz_log (YLOG_WARN, "No such record type defined");
925 yaz_log (YLOG_DEBUG, "Get record type from rgroup: %s",zh->m_record_type);
926 recType = recType_byName (zh->reg->recTypes, zh->res,
927 zh->m_record_type, &clientData);
928 recordType = zh->m_record_type;
933 yaz_log (YLOG_WARN, "No such record type: %s", recordType);
937 extractCtrl.init = extract_init;
938 extractCtrl.tokenAdd = extract_token_add;
939 extractCtrl.schemaAdd = extract_schema_add;
940 extractCtrl.dh = zh->reg->dh;
941 extractCtrl.handle = zh;
942 extractCtrl.match_criteria[0] = '\0';
943 extractCtrl.staticrank = 0;
945 init_extractCtrl(zh, &extractCtrl);
947 extract_set_store_data_prepare(&extractCtrl);
949 r = (*recType->extract)(clientData, &extractCtrl);
951 if (r == RECCTRL_EXTRACT_EOF)
953 else if (r == RECCTRL_EXTRACT_ERROR_GENERIC)
955 /* error occured during extraction ... */
956 yaz_log (YLOG_WARN, "extract error: generic");
959 else if (r == RECCTRL_EXTRACT_ERROR_NO_SUCH_FILTER)
961 /* error occured during extraction ... */
962 yaz_log (YLOG_WARN, "extract error: no such filter");
966 all_matches_add(&extractCtrl);
968 if (extractCtrl.match_criteria[0])
969 match_criteria = extractCtrl.match_criteria;
975 if (match_criteria && *match_criteria) {
976 matchStr = match_criteria;
978 if (zh->m_record_id && *zh->m_record_id) {
979 matchStr = fileMatchStr (zh, zh->reg->keys, pr_fname,
983 yaz_log (YLOG_WARN, "Bad match criteria (recordID)");
990 int db_ord = zebraExplain_get_database_ord(zh->reg->zei);
991 char *rinfo = dict_lookup_ord(zh->reg->matchDict, db_ord,
995 assert(*rinfo == sizeof(*sysno));
996 memcpy (sysno, rinfo+1, sizeof(*sysno));
1000 if (zebra_rec_keys_empty(zh->reg->keys))
1002 /* the extraction process returned no information - the record
1003 is probably empty - unless flagShowRecords is in use */
1013 yaz_log (YLOG_LOG, "delete %s %s %ld", recordType,
1014 pr_fname, (long) recordOffset);
1015 yaz_log (YLOG_WARN, "cannot delete record above (seems new)");
1019 yaz_log (YLOG_LOG, "add %s %s %ld", recordType, pr_fname,
1020 (long) recordOffset);
1021 rec = rec_new (zh->reg->records);
1023 *sysno = rec->sysno;
1025 recordAttr = rec_init_attr (zh->reg->zei, rec);
1026 recordAttr->staticrank = extractCtrl.staticrank;
1030 int db_ord = zebraExplain_get_database_ord(zh->reg->zei);
1031 dict_insert_ord(zh->reg->matchDict, db_ord, matchStr,
1032 sizeof(*sysno), sysno);
1036 extract_flushSortKeys (zh, *sysno, 1, zh->reg->sortKeys);
1037 extract_flushRecordKeys (zh, *sysno, 1, zh->reg->keys,
1038 recordAttr->staticrank);
1039 zh->records_inserted++;
1043 /* record already exists */
1044 zebra_rec_keys_t delkeys = zebra_rec_keys_open();
1045 zebra_rec_keys_t sortKeys = zebra_rec_keys_open();
1048 yaz_log (YLOG_LOG, "skipped %s %s %ld",
1049 recordType, pr_fname, (long) recordOffset);
1054 rec = rec_get (zh->reg->records, *sysno);
1057 recordAttr = rec_init_attr (zh->reg->zei, rec);
1059 zebra_rec_keys_set_buf(delkeys,
1060 rec->info[recInfo_delKeys],
1061 rec->size[recInfo_delKeys],
1063 zebra_rec_keys_set_buf(sortKeys,
1064 rec->info[recInfo_sortKeys],
1065 rec->size[recInfo_sortKeys],
1068 extract_flushSortKeys (zh, *sysno, 0, sortKeys);
1069 extract_flushRecordKeys (zh, *sysno, 0, delkeys,
1070 recordAttr->staticrank);
1073 /* record going to be deleted */
1074 if (zebra_rec_keys_empty(delkeys))
1076 yaz_log (YLOG_LOG, "delete %s %s %ld", recordType,
1077 pr_fname, (long) recordOffset);
1078 yaz_log (YLOG_WARN, "cannot delete file above, "
1079 "storeKeys false (3)");
1084 yaz_log (YLOG_LOG, "delete %s %s %ld", recordType,
1085 pr_fname, (long) recordOffset);
1086 zh->records_deleted++;
1089 int db_ord = zebraExplain_get_database_ord(zh->reg->zei);
1090 dict_delete_ord(zh->reg->matchDict, db_ord, matchStr);
1092 rec_del (zh->reg->records, &rec);
1101 yaz_log (YLOG_LOG, "update %s %s %ld", recordType,
1102 pr_fname, (long) recordOffset);
1103 recordAttr->staticrank = extractCtrl.staticrank;
1104 extract_flushSortKeys (zh, *sysno, 1, zh->reg->sortKeys);
1105 extract_flushRecordKeys (zh, *sysno, 1, zh->reg->keys,
1106 recordAttr->staticrank);
1107 zh->records_updated++;
1109 zebra_rec_keys_close(delkeys);
1110 zebra_rec_keys_close(sortKeys);
1112 /* update file type */
1113 xfree (rec->info[recInfo_fileType]);
1114 rec->info[recInfo_fileType] =
1115 rec_strdup (recordType, &rec->size[recInfo_fileType]);
1117 /* update filename */
1118 xfree (rec->info[recInfo_filename]);
1119 rec->info[recInfo_filename] =
1120 rec_strdup (fname, &rec->size[recInfo_filename]);
1122 /* update delete keys */
1123 xfree (rec->info[recInfo_delKeys]);
1124 if (!zebra_rec_keys_empty(zh->reg->keys) && zh->m_store_keys == 1)
1126 zebra_rec_keys_get_buf(zh->reg->keys,
1127 &rec->info[recInfo_delKeys],
1128 &rec->size[recInfo_delKeys]);
1132 rec->info[recInfo_delKeys] = NULL;
1133 rec->size[recInfo_delKeys] = 0;
1135 /* update sort keys */
1136 xfree (rec->info[recInfo_sortKeys]);
1138 zebra_rec_keys_get_buf(zh->reg->sortKeys,
1139 &rec->info[recInfo_sortKeys],
1140 &rec->size[recInfo_sortKeys]);
1142 /* save file size of original record */
1143 zebraExplain_recordBytesIncrement (zh->reg->zei,
1144 - recordAttr->recordSize);
1146 recordAttr->recordSize = fi->file_moffset - recordOffset;
1147 if (!recordAttr->recordSize)
1148 recordAttr->recordSize = fi->file_max - recordOffset;
1150 recordAttr->recordSize = buf_size;
1152 zebraExplain_recordBytesIncrement (zh->reg->zei,
1153 recordAttr->recordSize);
1155 /* set run-number for this record */
1156 recordAttr->runNumber =
1157 zebraExplain_runNumberIncrement (zh->reg->zei, 0);
1159 /* update store data */
1160 xfree (rec->info[recInfo_storeData]);
1162 /* update store data */
1163 if (zh->store_data_buf)
1165 rec->size[recInfo_storeData] = zh->store_data_size;
1166 rec->info[recInfo_storeData] = zh->store_data_buf;
1167 zh->store_data_buf = 0;
1169 else if (zh->m_store_data)
1171 rec->size[recInfo_storeData] = recordAttr->recordSize;
1172 rec->info[recInfo_storeData] = (char *)
1173 xmalloc (recordAttr->recordSize);
1174 memcpy (rec->info[recInfo_storeData], buf, recordAttr->recordSize);
1178 rec->info[recInfo_storeData] = NULL;
1179 rec->size[recInfo_storeData] = 0;
1181 /* update database name */
1182 xfree (rec->info[recInfo_databaseName]);
1183 rec->info[recInfo_databaseName] =
1184 rec_strdup (zh->basenames[0], &rec->size[recInfo_databaseName]);
1187 recordAttr->recordOffset = recordOffset;
1189 /* commit this record */
1190 rec_put (zh->reg->records, &rec);
1195 int explain_extract (void *handle, Record rec, data1_node *n)
1197 ZebraHandle zh = (ZebraHandle) handle;
1198 struct recExtractCtrl extractCtrl;
1200 if (zebraExplain_curDatabase (zh->reg->zei,
1201 rec->info[recInfo_databaseName]))
1204 if (zebraExplain_newDatabase (zh->reg->zei,
1205 rec->info[recInfo_databaseName], 0))
1209 zebra_rec_keys_reset(zh->reg->keys);
1210 zebra_rec_keys_reset(zh->reg->sortKeys);
1212 extractCtrl.init = extract_init;
1213 extractCtrl.tokenAdd = extract_token_add;
1214 extractCtrl.schemaAdd = extract_schema_add;
1215 extractCtrl.dh = zh->reg->dh;
1217 init_extractCtrl(zh, &extractCtrl);
1219 extractCtrl.flagShowRecords = 0;
1220 extractCtrl.match_criteria[0] = '\0';
1221 extractCtrl.staticrank = 0;
1222 extractCtrl.handle = handle;
1223 extractCtrl.first_record = 1;
1225 extract_set_store_data_prepare(&extractCtrl);
1228 grs_extract_tree(&extractCtrl, n);
1230 if (rec->size[recInfo_delKeys])
1232 zebra_rec_keys_t delkeys = zebra_rec_keys_open();
1234 zebra_rec_keys_t sortkeys = zebra_rec_keys_open();
1236 zebra_rec_keys_set_buf(delkeys, rec->info[recInfo_delKeys],
1237 rec->size[recInfo_delKeys],
1239 extract_flushRecordKeys (zh, rec->sysno, 0, delkeys, 0);
1240 zebra_rec_keys_close(delkeys);
1242 zebra_rec_keys_set_buf(sortkeys, rec->info[recInfo_sortKeys],
1243 rec->size[recInfo_sortKeys],
1246 extract_flushSortKeys (zh, rec->sysno, 0, sortkeys);
1247 zebra_rec_keys_close(sortkeys);
1249 extract_flushRecordKeys (zh, rec->sysno, 1, zh->reg->keys, 0);
1250 extract_flushSortKeys (zh, rec->sysno, 1, zh->reg->sortKeys);
1252 xfree (rec->info[recInfo_delKeys]);
1253 zebra_rec_keys_get_buf(zh->reg->keys,
1254 &rec->info[recInfo_delKeys],
1255 &rec->size[recInfo_delKeys]);
1257 xfree (rec->info[recInfo_sortKeys]);
1258 zebra_rec_keys_get_buf(zh->reg->sortKeys,
1259 &rec->info[recInfo_sortKeys],
1260 &rec->size[recInfo_sortKeys]);
1265 void extract_rec_keys_adjust(ZebraHandle zh, int is_insert,
1266 zebra_rec_keys_t reckeys)
1268 ZebraExplainInfo zei = zh->reg->zei;
1272 struct ord_stat *next;
1275 if (zebra_rec_keys_rewind(reckeys))
1277 struct ord_stat *ord_list = 0;
1281 struct it_key key_in;
1282 while(zebra_rec_keys_read(reckeys, &str, &slen, &key_in))
1284 int ord = key_in.mem[0];
1286 for (p = ord_list; p ; p = p->next)
1294 p = xmalloc(sizeof(*p));
1305 struct ord_stat *p1 = p;
1308 zebraExplain_ord_adjust_occurrences(zei, p->ord, p->no, 1);
1310 zebraExplain_ord_adjust_occurrences(zei, p->ord, - p->no, -1);
1317 void extract_flushRecordKeys (ZebraHandle zh, SYSNO sysno,
1319 zebra_rec_keys_t reckeys,
1322 ZebraExplainInfo zei = zh->reg->zei;
1324 extract_rec_keys_adjust(zh, cmd, reckeys);
1326 if (!zh->reg->key_buf)
1328 int mem= 1024*1024* atoi( res_get_def( zh->res, "memmax", "8"));
1331 yaz_log(YLOG_WARN, "Invalid memory setting, using default 8 MB");
1334 /* FIXME: That "8" should be in a default settings include */
1335 /* not hard-coded here! -H */
1336 zh->reg->key_buf = (char**) xmalloc (mem);
1337 zh->reg->ptr_top = mem/sizeof(char*);
1339 zh->reg->key_buf_used = 0;
1340 zh->reg->key_file_no = 0;
1342 zebraExplain_recordCountIncrement (zei, cmd ? 1 : -1);
1344 if (zebra_rec_keys_rewind(reckeys))
1348 struct it_key key_in;
1349 while(zebra_rec_keys_read(reckeys, &str, &slen, &key_in))
1352 struct it_key key_out;
1353 zint *keyp = key_out.mem;
1355 assert(key_in.len == 4);
1357 /* check for buffer overflow */
1358 if (zh->reg->key_buf_used + 1024 >
1359 (zh->reg->ptr_top -zh->reg->ptr_i)*sizeof(char*))
1360 extract_flushWriteKeys (zh, 0);
1363 assert(zh->reg->ptr_i > 0);
1364 (zh->reg->key_buf)[zh->reg->ptr_top - zh->reg->ptr_i] =
1365 (char*)zh->reg->key_buf + zh->reg->key_buf_used;
1367 /* encode the ordinal value (field/use/attribute) .. */
1368 ch = (int) key_in.mem[0];
1369 zh->reg->key_buf_used +=
1370 key_SU_encode(ch, (char*)zh->reg->key_buf +
1371 zh->reg->key_buf_used);
1373 /* copy the 0-terminated stuff from str to output */
1374 memcpy((char*)zh->reg->key_buf + zh->reg->key_buf_used, str, slen);
1375 zh->reg->key_buf_used += slen;
1376 ((char*)zh->reg->key_buf)[(zh->reg->key_buf_used)++] = '\0';
1378 /* the delete/insert indicator */
1379 ((char*)zh->reg->key_buf)[(zh->reg->key_buf_used)++] = cmd;
1381 if (zh->m_staticrank) /* rank config enabled ? */
1385 yaz_log(YLOG_WARN, "staticrank = %ld. Setting to 0",
1389 *keyp++ = staticrank;
1395 if (key_in.mem[1]) /* filter specified record ID */
1396 *keyp++ = key_in.mem[1];
1399 *keyp++ = key_in.mem[2]; /* section_id */
1400 *keyp++ = key_in.mem[3]; /* sequence .. */
1402 memcpy((char*)zh->reg->key_buf + zh->reg->key_buf_used,
1403 &key_out, sizeof(key_out));
1404 (zh->reg->key_buf_used) += sizeof(key_out);
1409 void extract_flushWriteKeys (ZebraHandle zh, int final)
1410 /* optimizing: if final=1, and no files written yet */
1411 /* push the keys directly to merge, sidestepping the */
1412 /* temp file altogether. Speeds small updates */
1415 char out_fname[200];
1417 struct encode_info encode_info;
1418 int ptr_i = zh->reg->ptr_i;
1423 if (!zh->reg->key_buf || ptr_i <= 0)
1425 yaz_log (YLOG_DEBUG, " nothing to flush section=%d buf=%p i=%d",
1426 zh->reg->key_file_no, zh->reg->key_buf, ptr_i);
1427 yaz_log (YLOG_DEBUG, " buf=%p ",
1429 yaz_log (YLOG_DEBUG, " ptr=%d ",zh->reg->ptr_i);
1430 yaz_log (YLOG_DEBUG, " reg=%p ",zh->reg);
1435 (zh->reg->key_file_no)++;
1436 yaz_log (YLOG_LOG, "sorting section %d", (zh->reg->key_file_no));
1437 yaz_log (YLOG_DEBUG, " sort_buff at %p n=%d",
1438 zh->reg->key_buf + zh->reg->ptr_top - ptr_i,ptr_i);
1440 qsort (zh->reg->key_buf + zh->reg->ptr_top - ptr_i, ptr_i,
1441 sizeof(char*), key_qsort_compare);
1443 /* zebra.cfg: tempfiles:
1444 Y: always use temp files (old way)
1445 A: use temp files, if more than one (auto)
1446 = if this is both the last and the first
1447 N: never bother with temp files (new) */
1449 temp_policy=toupper(res_get_def(zh->res,"tempfiles","auto")[0]);
1450 if (temp_policy != 'Y' && temp_policy != 'N' && temp_policy != 'A') {
1451 yaz_log (YLOG_WARN, "Illegal tempfiles setting '%c'. using 'Auto' ",
1456 if ( ( temp_policy =='N' ) || /* always from memory */
1457 ( ( temp_policy =='A' ) && /* automatic */
1458 (zh->reg->key_file_no == 1) && /* this is first time */
1459 (final) ) ) /* and last (=only) time */
1460 { /* go directly from memory */
1461 zh->reg->key_file_no =0; /* signal not to read files */
1462 zebra_index_merge(zh);
1464 zh->reg->key_buf_used = 0;
1468 /* Not doing directly from memory, write into a temp file */
1469 extract_get_fname_tmp (zh, out_fname, zh->reg->key_file_no);
1471 if (!(outf = fopen (out_fname, "wb")))
1473 yaz_log (YLOG_FATAL|YLOG_ERRNO, "fopen %s", out_fname);
1476 yaz_log (YLOG_LOG, "writing section %d", zh->reg->key_file_no);
1477 prevcp = cp = (zh->reg->key_buf)[zh->reg->ptr_top - ptr_i];
1479 encode_key_init (&encode_info);
1480 encode_key_write (cp, &encode_info, outf);
1484 cp = (zh->reg->key_buf)[zh->reg->ptr_top - ptr_i];
1485 if (strcmp (cp, prevcp))
1487 encode_key_flush ( &encode_info, outf);
1488 encode_key_init (&encode_info);
1489 encode_key_write (cp, &encode_info, outf);
1493 encode_key_write (cp + strlen(cp), &encode_info, outf);
1495 encode_key_flush ( &encode_info, outf);
1497 qsort (key_buf + ptr_top-ptr_i, ptr_i, sizeof(char*), key_x_compare);
1498 extract_get_fname_tmp (out_fname, key_file_no);
1500 if (!(outf = fopen (out_fname, "wb")))
1502 yaz_log (YLOG_FATAL|YLOG_ERRNO, "fopen %s", out_fname);
1505 yaz_log (YLOG_LOG, "writing section %d", key_file_no);
1507 prevcp = key_buf[ptr_top-i];
1509 if (!--i || strcmp (prevcp, key_buf[ptr_top-i]))
1511 key_y_len = strlen(prevcp)+1;
1513 yaz_log (YLOG_LOG, "key_y_len: %2d %02x %02x %s",
1514 key_y_len, prevcp[0], prevcp[1], 2+prevcp);
1516 qsort (key_buf + ptr_top-ptr_i, ptr_i - i,
1517 sizeof(char*), key_y_compare);
1518 cp = key_buf[ptr_top-ptr_i];
1520 encode_key_init (&encode_info);
1521 encode_key_write (cp, &encode_info, outf);
1524 cp = key_buf[ptr_top-ptr_i];
1525 encode_key_write (cp+key_y_len, &encode_info, outf);
1527 encode_key_flush ( &encode_info, outf);
1530 prevcp = key_buf[ptr_top-ptr_i];
1535 yaz_log (YLOG_FATAL|YLOG_ERRNO, "fclose %s", out_fname);
1538 yaz_log (YLOG_LOG, "finished section %d", zh->reg->key_file_no);
1540 zh->reg->key_buf_used = 0;
1543 ZEBRA_RES zebra_snippets_rec_keys(ZebraHandle zh,
1544 zebra_rec_keys_t reckeys,
1545 zebra_snippets *snippets)
1547 NMEM nmem = nmem_create();
1548 if (zebra_rec_keys_rewind(reckeys))
1553 while (zebra_rec_keys_read(reckeys, &str, &slen, &key))
1555 char dst_buf[IT_MAX_WORD];
1556 char *dst_term = dst_buf;
1559 assert(key.len <= 4 && key.len > 2);
1560 seqno = (int) key.mem[key.len-1];
1563 zebraExplain_lookup_ord(zh->reg->zei, ord, &index_type,
1564 0/* db */, 0 /* string_index */);
1566 zebra_term_untrans_iconv(zh, nmem, index_type,
1568 zebra_snippets_append(snippets, seqno, ord, dst_term);
1576 void print_rec_keys(ZebraHandle zh, zebra_rec_keys_t reckeys)
1578 yaz_log(YLOG_LOG, "print_rec_keys");
1579 if (zebra_rec_keys_rewind(reckeys))
1584 while (zebra_rec_keys_read(reckeys, &str, &slen, &key))
1586 char dst_buf[IT_MAX_WORD];
1590 assert(key.len <= 4 && key.len > 2);
1592 zebraExplain_lookup_ord(zh->reg->zei,
1593 key.mem[0], &index_type, &db, 0);
1595 seqno = (int) key.mem[key.len-1];
1597 zebra_term_untrans(zh, index_type, dst_buf, str);
1599 yaz_log(YLOG_LOG, "ord=" ZINT_FORMAT " seqno=%d term=%s",
1600 key.mem[0], seqno, dst_buf);
1605 void extract_add_index_string(RecWord *p, const char *str, int length)
1609 ZebraHandle zh = p->extractCtrl->handle;
1610 ZebraExplainInfo zei = zh->reg->zei;
1616 ch = zebraExplain_lookup_attr_str(zei, p->index_type, p->index_name);
1618 ch = zebraExplain_add_attr_str(zei, p->index_type, p->index_name);
1622 key.mem[1] = p->record_id;
1623 key.mem[2] = p->section_id;
1624 key.mem[3] = p->seqno;
1633 for (i = 0; i<length && i < 20; i++)
1634 sprintf(strz+strlen(strz), "%02X", str[i] & 0xff);
1635 /* just for debugging .. */
1636 yaz_log(YLOG_LOG, "add: set=%d use=%d "
1637 "record_id=%lld section_id=%lld seqno=%lld %s",
1638 p->attrSet, p->attrUse, p->record_id, p->section_id, p->seqno,
1643 zebra_rec_keys_write(zh->reg->keys, str, length, &key);
1646 static void extract_add_sort_string(RecWord *p, const char *str, int length)
1650 ZebraHandle zh = p->extractCtrl->handle;
1651 ZebraExplainInfo zei = zh->reg->zei;
1657 ch = zebraExplain_lookup_attr_str(zei, p->index_type, p->index_name);
1659 ch = zebraExplain_add_attr_str(zei, p->index_type, p->index_name);
1662 key.mem[1] = p->record_id;
1663 key.mem[2] = p->section_id;
1664 key.mem[3] = p->seqno;
1673 for (i = 0; i<length && i < 20; i++)
1674 sprintf(strz+strlen(strz), "%02X", str[i] & 0xff);
1675 /* just for debugging .. */
1676 yaz_log(YLOG_LOG, "add: set=%d use=%d "
1677 "record_id=%lld section_id=%lld seqno=%lld %s",
1678 p->attrSet, p->attrUse, p->record_id, p->section_id, p->seqno,
1682 zebra_rec_keys_write(zh->reg->sortKeys, str, length, &key);
1685 void extract_add_string (RecWord *p, const char *string, int length)
1687 assert (length > 0);
1688 if (zebra_maps_is_sort (p->zebra_maps, p->index_type))
1689 extract_add_sort_string (p, string, length);
1691 extract_add_index_string (p, string, length);
1694 static void extract_add_incomplete_field (RecWord *p)
1696 const char *b = p->term_buf;
1697 int remain = p->term_len;
1698 const char **map = 0;
1700 yaz_log(YLOG_DEBUG, "Incomplete field, w='%.*s'", p->term_len, p->term_buf);
1703 map = zebra_maps_input(p->zebra_maps, p->index_type, &b, remain, 0);
1707 char buf[IT_MAX_WORD+1];
1711 while (map && *map && **map == *CHR_SPACE)
1713 remain = p->term_len - (b - p->term_buf);
1715 map = zebra_maps_input(p->zebra_maps, p->index_type, &b,
1723 while (map && *map && **map != *CHR_SPACE)
1725 const char *cp = *map;
1727 while (i < IT_MAX_WORD && *cp)
1729 remain = p->term_len - (b - p->term_buf);
1731 map = zebra_maps_input(p->zebra_maps, p->index_type, &b, remain, 0);
1737 extract_add_string (p, buf, i);
1742 static void extract_add_complete_field (RecWord *p)
1744 const char *b = p->term_buf;
1745 char buf[IT_MAX_WORD+1];
1746 const char **map = 0;
1747 int i = 0, remain = p->term_len;
1749 yaz_log(YLOG_DEBUG, "Complete field, w='%.*s'",
1750 p->term_len, p->term_buf);
1753 map = zebra_maps_input (p->zebra_maps, p->index_type, &b, remain, 1);
1755 while (remain > 0 && i < IT_MAX_WORD)
1757 while (map && *map && **map == *CHR_SPACE)
1759 remain = p->term_len - (b - p->term_buf);
1763 int first = i ? 0 : 1; /* first position */
1764 map = zebra_maps_input(p->zebra_maps, p->index_type, &b, remain, first);
1772 if (i && i < IT_MAX_WORD)
1773 buf[i++] = *CHR_SPACE;
1774 while (map && *map && **map != *CHR_SPACE)
1776 const char *cp = *map;
1778 if (**map == *CHR_CUT)
1784 if (i >= IT_MAX_WORD)
1786 yaz_log(YLOG_DEBUG, "Adding string to index '%d'", **map);
1787 while (i < IT_MAX_WORD && *cp)
1790 remain = p->term_len - (b - p->term_buf);
1793 map = zebra_maps_input (p->zebra_maps, p->index_type, &b,
1802 extract_add_string (p, buf, i);
1805 void extract_token_add (RecWord *p)
1809 yaz_log (YLOG_LOG, "token_add "
1810 "reg_type=%c attrSet=%d attrUse=%d seqno=%d s=%.*s",
1811 p->reg_type, p->attrSet, p->attrUse, p->seqno, p->length,
1814 if ((wrbuf = zebra_replace(p->zebra_maps, p->index_type, 0,
1815 p->term_buf, p->term_len)))
1817 p->term_buf = wrbuf_buf(wrbuf);
1818 p->term_len = wrbuf_len(wrbuf);
1820 if (zebra_maps_is_complete (p->zebra_maps, p->index_type))
1821 extract_add_complete_field (p);
1823 extract_add_incomplete_field(p);
1826 static void extract_set_store_data_cb(struct recExtractCtrl *p,
1827 void *buf, size_t sz)
1829 ZebraHandle zh = (ZebraHandle) p->handle;
1831 xfree(zh->store_data_buf);
1832 zh->store_data_buf = 0;
1833 zh->store_data_size = 0;
1836 zh->store_data_buf = xmalloc(sz);
1837 zh->store_data_size = sz;
1838 memcpy(zh->store_data_buf, buf, sz);
1842 static void extract_set_store_data_prepare(struct recExtractCtrl *p)
1844 ZebraHandle zh = (ZebraHandle) p->handle;
1845 xfree(zh->store_data_buf);
1846 zh->store_data_buf = 0;
1847 zh->store_data_size = 0;
1848 p->setStoreData = extract_set_store_data_cb;
1851 void extract_schema_add (struct recExtractCtrl *p, Odr_oid *oid)
1853 ZebraHandle zh = (ZebraHandle) p->handle;
1854 zebraExplain_addSchema (zh->reg->zei, oid);
1857 void extract_flushSortKeys (ZebraHandle zh, SYSNO sysno,
1858 int cmd, zebra_rec_keys_t reckeys)
1860 if (zebra_rec_keys_rewind(reckeys))
1862 SortIdx sortIdx = zh->reg->sortIdx;
1865 struct it_key key_in;
1867 sortIdx_sysno (sortIdx, sysno);
1869 while (zebra_rec_keys_read(reckeys, &str, &slen, &key_in))
1871 int ord = (int) key_in.mem[0];
1873 sortIdx_type(sortIdx, ord);
1875 sortIdx_add(sortIdx, str, slen);
1877 sortIdx_add(sortIdx, "", 1);
1882 void encode_key_init (struct encode_info *i)
1891 i->encode_handle = iscz1_start();
1892 i->decode_handle = iscz1_start();
1898 /* this is the old encode_key_write
1899 * may be deleted once we are confident that the new works
1902 void encode_key_write (char *k, struct encode_info *i, FILE *outf)
1905 char *bp = i->buf, *bp0;
1906 const char *src = (char *) &key;
1908 /* copy term to output buf */
1909 while ((*bp++ = *k++))
1911 /* and copy & align key so we can mangle */
1912 memcpy (&key, k+1, sizeof(struct it_key)); /* *k is insert/delete */
1916 key_logdump_txt(YLOG_LOG, &key, *k ? "i" : "d");
1918 assert(key.mem[0] >= 0);
1921 iscz1_encode(i->encode_handle, &bp, &src);
1923 *bp0 = (*k * 128) + bp - bp0 - 1; /* length and insert/delete combined */
1924 if (fwrite (i->buf, bp - i->buf, 1, outf) != 1)
1926 yaz_log (YLOG_FATAL|YLOG_ERRNO, "fwrite");
1935 const char *src = bp0+1;
1936 char *dst = (char*) &key2;
1937 iscz1_decode(i->decode_handle, &dst, &src);
1939 key_logdump_txt(YLOG_LOG, &key2, *k ? "i" : "d");
1941 assert(key2.mem[1]);
1946 void encode_key_flush (struct encode_info *i, FILE *outf)
1948 iscz1_stop(i->encode_handle);
1949 iscz1_stop(i->decode_handle);
1954 /* new encode_key_write
1955 * The idea is to buffer one more key, and compare them
1956 * If we are going to delete and insert the same key,
1957 * we may as well not bother. Should make a difference in
1958 * updates with small modifications (appending to a mbox)
1960 void encode_key_write (char *k, struct encode_info *i, FILE *outf)
1965 if (*k) /* first time for new key */
1968 while ((*bp++ = *k++))
1970 i->keylen= bp - i->buf -1;
1971 assert(i->keylen+1+sizeof(struct it_key) < ENCODE_BUFLEN);
1975 bp=i->buf + i->keylen;
1980 memcpy (&key, k+1, sizeof(struct it_key));
1981 if (0==i->prevsys) /* no previous filter, fill up */
1983 i->prevsys=key.sysno;
1984 i->prevseq=key.seqno;
1987 else if ( (i->prevsys==key.sysno) &&
1988 (i->prevseq==key.seqno) &&
1990 { /* same numbers, diff cmd, they cancel out */
1994 { /* different stuff, write previous, move buf */
1995 bp = encode_key_int ( (i->prevsys - i->sysno) * 2 + i->prevcmd, bp);
1996 if (i->sysno != i->prevsys)
1998 i->sysno = i->prevsys;
2001 else if (!i->seqno && !i->prevseq && i->cmd == i->prevcmd)
2003 return; /* ??? Filters some sort of duplicates away */
2004 /* ??? Can this ever happen -H 15oct02 */
2006 bp = encode_key_int (i->prevseq - i->seqno, bp);
2007 i->seqno = i->prevseq;
2008 i->cmd = i->prevcmd;
2009 if (fwrite (i->buf, bp - i->buf, 1, outf) != 1)
2011 yaz_log (YLOG_FATAL|YLOG_ERRNO, "fwrite");
2014 i->keylen=0; /* ok, it's written, forget it */
2015 i->prevsys=key.sysno;
2016 i->prevseq=key.seqno;
2021 void encode_key_flush (struct encode_info *i, FILE *outf)
2022 { /* flush the last key from i */
2023 char *bp =i->buf + i->keylen;
2026 return; /* nothing to flush */
2029 bp = encode_key_int ( (i->prevsys - i->sysno) * 2 + i->prevcmd, bp);
2030 if (i->sysno != i->prevsys)
2032 i->sysno = i->prevsys;
2035 else if (!i->seqno && !i->prevseq && i->cmd == i->prevcmd)
2037 return; /* ??? Filters some sort of duplicates away */
2038 /* ??? Can this ever happen -H 15oct02 */
2040 bp = encode_key_int (i->prevseq - i->seqno, bp);
2041 i->seqno = i->prevseq;
2042 i->cmd = i->prevcmd;
2043 if (fwrite (i->buf, bp - i->buf, 1, outf) != 1)
2045 yaz_log (YLOG_FATAL|YLOG_ERRNO, "fwrite");
2048 i->keylen=0; /* ok, it's written, forget it */
2049 i->prevsys=0; /* forget the values too */
2056 * indent-tabs-mode: nil
2058 * vim: shiftwidth=4 tabstop=8 expandtab