1 /* $Id: extract.c,v 1.221 2006-06-13 19:40:18 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
39 #define ENCODE_BUFLEN 768
43 char buf[ENCODE_BUFLEN];
46 static int log_level = 0;
47 static int log_level_initialized = 1;
49 static void zebra_init_log_level()
51 if (!log_level_initialized)
53 log_level = yaz_log_module_level("extract");
54 log_level_initialized = 1;
58 static void extract_flushRecordKeys (ZebraHandle zh, SYSNO sysno,
59 int cmd, zebra_rec_keys_t reckeys,
61 static void extract_flushSortKeys (ZebraHandle zh, SYSNO sysno,
62 int cmd, zebra_rec_keys_t skp);
63 static void extract_schema_add (struct recExtractCtrl *p, Odr_oid *oid);
64 static void extract_token_add (RecWord *p);
66 static void encode_key_init (struct encode_info *i);
67 static void encode_key_write (char *k, struct encode_info *i, FILE *outf);
68 static void encode_key_flush (struct encode_info *i, FILE *outf);
70 #define USE_SHELLSORT 0
73 static void shellsort(void *ar, int r, size_t s,
74 int (*cmp)(const void *a, const void *b))
79 static const int incs[16] = { 1391376, 463792, 198768, 86961, 33936,
80 13776, 4592, 1968, 861, 336,
81 112, 48, 21, 7, 3, 1 };
82 for ( k = 0; k < 16; k++)
83 for (h = incs[k], i = h; i < r; i++)
87 while (j > h && (*cmp)(a + s*(j-h), v) > 0)
89 memcpy (a + s*j, a + s*(j-h), s);
97 static void logRecord (ZebraHandle zh)
99 ++zh->records_processed;
100 if (!(zh->records_processed % 1000))
102 yaz_log(YLOG_LOG, "Records: "ZINT_FORMAT" i/u/d "
103 ZINT_FORMAT"/"ZINT_FORMAT"/"ZINT_FORMAT,
104 zh->records_processed, zh->records_inserted,
105 zh->records_updated, zh->records_deleted);
109 static void extract_add_index_string (RecWord *p, const char *str, int length);
111 static void extract_set_store_data_prepare(struct recExtractCtrl *p);
113 static void extract_init (struct recExtractCtrl *p, RecWord *w)
115 w->zebra_maps = p->zebra_maps;
117 w->index_name = "any";
124 static void searchRecordKey(ZebraHandle zh,
125 zebra_rec_keys_t reckeys,
126 const char *index_name,
127 const char **ws, int ws_length)
132 for (i = 0; i<ws_length; i++)
136 ch = zebraExplain_lookup_attr_str(zh->reg->zei, '0', index_name);
138 ch = zebraExplain_lookup_attr_str(zh->reg->zei, 'p', index_name);
140 ch = zebraExplain_lookup_attr_str(zh->reg->zei, 'w', index_name);
145 if (zebra_rec_keys_rewind(reckeys))
152 while (zebra_rec_keys_read(reckeys, &str, &slen, &key))
154 assert(key.len <= 4 && key.len > 2);
156 seqno = key.mem[key.len-1];
158 if (key.mem[0] == ch)
164 woff = seqno - startSeq;
165 if (woff >= 0 && woff < ws_length)
172 struct file_read_info {
173 off_t file_max; /* maximum offset so far */
174 off_t file_offset; /* current offset */
175 off_t file_moffset; /* offset of rec/rec boundary */
180 static struct file_read_info *file_read_start (int fd)
182 struct file_read_info *fi = (struct file_read_info *)
183 xmalloc (sizeof(*fi));
187 fi->file_moffset = 0;
193 static void file_read_stop (struct file_read_info *fi)
198 static off_t file_seek (void *handle, off_t offset)
200 struct file_read_info *p = (struct file_read_info *) handle;
201 p->file_offset = offset;
202 return lseek (p->fd, offset, SEEK_SET);
205 static off_t file_tell (void *handle)
207 struct file_read_info *p = (struct file_read_info *) handle;
208 return p->file_offset;
211 static int file_read (void *handle, char *buf, size_t count)
213 struct file_read_info *p = (struct file_read_info *) handle;
216 r = read (fd, buf, count);
220 if (p->file_offset > p->file_max)
221 p->file_max = p->file_offset;
226 static void file_end (void *handle, off_t offset)
228 struct file_read_info *p = (struct file_read_info *) handle;
230 if (offset != p->file_moffset)
232 p->file_moffset = offset;
237 #define FILE_MATCH_BLANK "\t "
239 static char *fileMatchStr (ZebraHandle zh,
240 zebra_rec_keys_t reckeys,
241 const char *fname, const char *spec)
243 static char dstBuf[2048]; /* static here ??? */
245 const char *s = spec;
249 for (; *s && strchr(FILE_MATCH_BLANK, *s); s++)
256 char attset_str[64], attname_str[64];
260 for (s++; strchr(FILE_MATCH_BLANK, *s); s++)
262 for (i = 0; *s && *s != ',' && *s != ')' &&
263 !strchr(FILE_MATCH_BLANK, *s); s++)
264 if (i+1 < sizeof(attset_str))
265 attset_str[i++] = *s;
266 attset_str[i] = '\0';
268 for (; strchr(FILE_MATCH_BLANK, *s); s++)
271 strcpy(attname_str, attset_str);
274 for (s++; strchr(FILE_MATCH_BLANK, *s); s++)
276 for (i = 0; *s && *s != ')' &&
277 !strchr(FILE_MATCH_BLANK, *s); s++)
278 if (i+1 < sizeof(attname_str))
279 attname_str[i++] = *s;
280 attname_str[i] = '\0';
283 searchRecordKey (zh, reckeys, attname_str, ws, 32);
287 yaz_log (YLOG_WARN, "Missing ) in match criteria %s in group %s",
288 spec, zh->m_group ? zh->m_group : "none");
293 for (i = 0; i<32; i++)
302 dst += strlen(ws[i]);
306 yaz_log (YLOG_WARN, "Record didn't contain match"
307 " fields in (%s,%s)", attset_str, attname_str);
315 const char *spec_src = NULL;
316 const char *s1 = ++s;
317 while (*s1 && !strchr(FILE_MATCH_BLANK, *s1))
321 if (spec_len > sizeof(special)-1)
322 spec_len = sizeof(special)-1;
323 memcpy (special, s, spec_len);
324 special[spec_len] = '\0';
327 if (!strcmp (special, "group"))
328 spec_src = zh->m_group;
329 else if (!strcmp (special, "database"))
330 spec_src = zh->basenames[0];
331 else if (!strcmp (special, "filename")) {
334 else if (!strcmp (special, "type"))
335 spec_src = zh->m_record_type;
340 strcpy (dst, spec_src);
341 dst += strlen (spec_src);
344 else if (*s == '\"' || *s == '\'')
346 int stopMarker = *s++;
350 while (*s && *s != stopMarker)
352 if (i+1 < sizeof(tmpString))
353 tmpString[i++] = *s++;
358 strcpy (dst, tmpString);
359 dst += strlen (tmpString);
363 yaz_log (YLOG_WARN, "Syntax error in match criteria %s in group %s",
364 spec, zh->m_group ? zh->m_group : "none");
371 yaz_log (YLOG_WARN, "No match criteria for record %s in group %s",
372 fname, zh->m_group ? zh->m_group : "none");
379 struct recordLogInfo {
382 struct recordGroup *rGroup;
385 static void init_extractCtrl(ZebraHandle zh, struct recExtractCtrl *ctrl)
388 for (i = 0; i<256; i++)
390 if (zebra_maps_is_positioned(zh->reg->zebra_maps, i))
395 ctrl->zebra_maps = zh->reg->zebra_maps;
396 ctrl->flagShowRecords = !zh->m_flag_rw;
399 static void all_matches_add(struct recExtractCtrl *ctrl)
402 extract_init(ctrl, &word);
403 word.index_name = "allrecords";
404 word.index_type = 'w';
406 extract_add_index_string (&word, "", 0);
409 static ZEBRA_RES file_extract_record(ZebraHandle zh,
410 SYSNO *sysno, const char *fname,
412 struct file_read_info *fi,
415 void *recTypeClientData)
417 const char *match_str_to_print = "";
418 RecordAttr *recordAttr;
420 const char *matchStr = 0;
423 off_t recordOffset = 0;
424 struct recExtractCtrl extractCtrl;
426 /* announce database */
427 if (zebraExplain_curDatabase (zh->reg->zei, zh->basenames[0]))
429 if (zebraExplain_newDatabase (zh->reg->zei, zh->basenames[0],
430 zh->m_explain_database))
436 /* we are going to read from a file, so prepare the extraction */
437 zebra_rec_keys_reset(zh->reg->keys);
439 zebra_rec_keys_reset(zh->reg->sortKeys);
440 recordOffset = fi->file_moffset;
441 extractCtrl.handle = zh;
442 extractCtrl.offset = fi->file_moffset;
443 extractCtrl.readf = file_read;
444 extractCtrl.seekf = file_seek;
445 extractCtrl.tellf = file_tell;
446 extractCtrl.endf = file_end;
448 extractCtrl.init = extract_init;
449 extractCtrl.tokenAdd = extract_token_add;
450 extractCtrl.schemaAdd = extract_schema_add;
451 extractCtrl.dh = zh->reg->dh;
452 extractCtrl.match_criteria[0] = '\0';
453 extractCtrl.staticrank = 0;
455 extractCtrl.first_record = fi->file_offset ? 0 : 1;
457 extract_set_store_data_prepare(&extractCtrl);
459 init_extractCtrl(zh, &extractCtrl);
462 printf ("File: %s " ZINT_FORMAT "\n", fname, (zint)recordOffset);
466 sprintf (msg, "%s:" ZINT_FORMAT , fname, (zint)recordOffset);
467 yaz_log_init_prefix2 (msg);
470 r = (*recType->extract)(recTypeClientData, &extractCtrl);
472 yaz_log_init_prefix2 (0);
473 if (r == RECCTRL_EXTRACT_EOF)
475 else if (r == RECCTRL_EXTRACT_ERROR_GENERIC)
477 /* error occured during extraction ... */
479 zh->records_processed < zh->m_file_verbose_limit)
481 yaz_log (YLOG_WARN, "fail %s %s " ZINT_FORMAT,
483 fname, (zint) recordOffset);
487 else if (r == RECCTRL_EXTRACT_ERROR_NO_SUCH_FILTER)
489 /* error occured during extraction ... */
491 zh->records_processed < zh->m_file_verbose_limit)
493 yaz_log (YLOG_WARN, "no filter for %s %s "
494 ZINT_FORMAT, zh->m_record_type,
495 fname, (zint) recordOffset);
499 all_matches_add(&extractCtrl);
500 if (extractCtrl.match_criteria[0])
501 matchStr = extractCtrl.match_criteria;
504 /* if matchStr is set now - we assume it's printable .
505 For internal matchStr (see below) we don't print */
507 match_str_to_print = matchStr;
509 /* perform internal match if sysno not known and if match criteria is
516 if (matchStr == 0 && zh->m_record_id && *zh->m_record_id)
518 matchStr = fileMatchStr (zh, zh->reg->keys, fname,
522 yaz_log(YLOG_WARN, "Bad match criteria");
524 if (zebra_rec_keys_empty(zh->reg->keys))
526 yaz_log(YLOG_WARN, "And no index keys");
533 int db_ord = zebraExplain_get_database_ord(zh->reg->zei);
534 char *rinfo = dict_lookup_ord(zh->reg->matchDict, db_ord,
538 assert(*rinfo == sizeof(*sysno));
539 memcpy (sysno, rinfo+1, sizeof(*sysno));
543 if (! *sysno && zebra_rec_keys_empty(zh->reg->keys) )
545 /* the extraction process returned no information - the record
546 is probably empty - unless flagShowRecords is in use */
550 if (zh->records_processed < zh->m_file_verbose_limit)
551 yaz_log(YLOG_WARN, "empty %s %s " ZINT_FORMAT, zh->m_record_type,
552 fname, (zint)recordOffset);
561 yaz_log(YLOG_LOG, "delete %s %s " ZINT_FORMAT, zh->m_record_type,
562 fname, (zint)recordOffset);
563 yaz_log(YLOG_WARN, "cannot delete record above (seems new)");
567 rec = rec_new (zh->reg->records);
571 if (zh->records_processed < zh->m_file_verbose_limit)
573 yaz_log(YLOG_LOG, "add %s %s " ZINT_FORMAT
574 " " ZINT_FORMAT " %s" ,
576 fname, (zint) recordOffset, *sysno, match_str_to_print);
578 recordAttr = rec_init_attr (zh->reg->zei, rec);
579 recordAttr->staticrank = extractCtrl.staticrank;
583 int db_ord = zebraExplain_get_database_ord(zh->reg->zei);
584 dict_insert_ord(zh->reg->matchDict, db_ord, matchStr,
585 sizeof(*sysno), sysno);
589 extract_flushSortKeys (zh, *sysno, 1, zh->reg->sortKeys);
590 extract_flushRecordKeys (zh, *sysno, 1, zh->reg->keys,
591 recordAttr->staticrank);
592 zh->records_inserted++;
596 /* record already exists */
597 zebra_rec_keys_t delkeys = zebra_rec_keys_open();
599 zebra_rec_keys_t sortKeys = zebra_rec_keys_open();
601 rec = rec_get (zh->reg->records, *sysno);
604 recordAttr = rec_init_attr (zh->reg->zei, rec);
606 zebra_rec_keys_set_buf(delkeys,
607 rec->info[recInfo_delKeys],
608 rec->size[recInfo_delKeys],
611 zebra_rec_keys_set_buf(sortKeys,
612 rec->info[recInfo_sortKeys],
613 rec->size[recInfo_sortKeys],
615 extract_flushSortKeys (zh, *sysno, 0, sortKeys);
616 extract_flushRecordKeys (zh, *sysno, 0, delkeys,
617 recordAttr->staticrank); /* old values */
620 /* record going to be deleted */
621 if (zebra_rec_keys_empty(delkeys))
623 yaz_log(YLOG_LOG, "delete %s %s " ZINT_FORMAT
625 zh->m_record_type, fname, (zint)recordOffset, *sysno);
626 yaz_log(YLOG_WARN, "cannot delete file above, storeKeys false (1)");
630 if (zh->records_processed < zh->m_file_verbose_limit)
632 yaz_log(YLOG_LOG, "delete %s %s " ZINT_FORMAT
633 " " ZINT_FORMAT " %s" ,
634 zh->m_record_type, fname, (zint) recordOffset,
635 *sysno, match_str_to_print);
637 zh->records_deleted++;
640 int db_ord = zebraExplain_get_database_ord(zh->reg->zei);
641 dict_delete_ord(zh->reg->matchDict, db_ord, matchStr);
643 rec_del (zh->reg->records, &rec);
651 /* flush new keys for sort&search etc */
652 if (zh->records_processed < zh->m_file_verbose_limit)
654 yaz_log(YLOG_LOG, "update %s %s " ZINT_FORMAT
655 " " ZINT_FORMAT " %s" ,
656 zh->m_record_type, fname, (zint) recordOffset,
657 *sysno, match_str_to_print);
659 recordAttr->staticrank = extractCtrl.staticrank;
660 extract_flushSortKeys (zh, *sysno, 1, zh->reg->sortKeys);
661 extract_flushRecordKeys (zh, *sysno, 1, zh->reg->keys,
662 recordAttr->staticrank);
663 zh->records_updated++;
665 zebra_rec_keys_close(delkeys);
666 zebra_rec_keys_close(sortKeys);
668 /* update file type */
669 xfree (rec->info[recInfo_fileType]);
670 rec->info[recInfo_fileType] =
671 rec_strdup (zh->m_record_type, &rec->size[recInfo_fileType]);
673 /* update filename */
674 xfree (rec->info[recInfo_filename]);
675 rec->info[recInfo_filename] =
676 rec_strdup (fname, &rec->size[recInfo_filename]);
678 /* update delete keys */
679 xfree (rec->info[recInfo_delKeys]);
680 if (!zebra_rec_keys_empty(zh->reg->keys) && zh->m_store_keys == 1)
682 zebra_rec_keys_get_buf(zh->reg->keys,
683 &rec->info[recInfo_delKeys],
684 &rec->size[recInfo_delKeys]);
688 rec->info[recInfo_delKeys] = NULL;
689 rec->size[recInfo_delKeys] = 0;
692 /* update sort keys */
693 xfree (rec->info[recInfo_sortKeys]);
695 zebra_rec_keys_get_buf(zh->reg->sortKeys,
696 &rec->info[recInfo_sortKeys],
697 &rec->size[recInfo_sortKeys]);
699 /* save file size of original record */
700 zebraExplain_recordBytesIncrement (zh->reg->zei,
701 - recordAttr->recordSize);
702 recordAttr->recordSize = fi->file_moffset - recordOffset;
703 if (!recordAttr->recordSize)
704 recordAttr->recordSize = fi->file_max - recordOffset;
705 zebraExplain_recordBytesIncrement (zh->reg->zei,
706 recordAttr->recordSize);
708 /* set run-number for this record */
709 recordAttr->runNumber = zebraExplain_runNumberIncrement (zh->reg->zei,
712 /* update store data */
713 xfree (rec->info[recInfo_storeData]);
714 if (zh->store_data_buf)
716 rec->size[recInfo_storeData] = zh->store_data_size;
717 rec->info[recInfo_storeData] = zh->store_data_buf;
718 zh->store_data_buf = 0;
720 else if (zh->m_store_data)
722 rec->size[recInfo_storeData] = recordAttr->recordSize;
723 rec->info[recInfo_storeData] = (char *)
724 xmalloc (recordAttr->recordSize);
725 if (lseek (fi->fd, recordOffset, SEEK_SET) < 0)
727 yaz_log(YLOG_ERRNO|YLOG_FATAL, "seek to " ZINT_FORMAT " in %s",
728 (zint)recordOffset, fname);
731 if (read (fi->fd, rec->info[recInfo_storeData], recordAttr->recordSize)
732 < recordAttr->recordSize)
734 yaz_log (YLOG_ERRNO|YLOG_FATAL, "read %d bytes of %s",
735 recordAttr->recordSize, fname);
741 rec->info[recInfo_storeData] = NULL;
742 rec->size[recInfo_storeData] = 0;
744 /* update database name */
745 xfree (rec->info[recInfo_databaseName]);
746 rec->info[recInfo_databaseName] =
747 rec_strdup (zh->basenames[0], &rec->size[recInfo_databaseName]);
750 recordAttr->recordOffset = recordOffset;
752 /* commit this record */
753 rec_put (zh->reg->records, &rec);
758 ZEBRA_RES zebra_extract_file(ZebraHandle zh, SYSNO *sysno, const char *fname,
761 ZEBRA_RES r = ZEBRA_OK;
766 struct file_read_info *fi;
767 const char *original_record_type = 0;
769 void *recTypeClientData;
771 zebra_init_log_level();
773 if (!zh->m_group || !*zh->m_group)
776 sprintf (gprefix, "%s.", zh->m_group);
778 yaz_log(log_level, "zebra_extract_file %s", fname);
780 /* determine file extension */
782 for (i = strlen(fname); --i >= 0; )
785 else if (fname[i] == '.')
787 strcpy (ext, fname+i+1);
790 /* determine file type - depending on extension */
791 original_record_type = zh->m_record_type;
792 if (!zh->m_record_type)
794 sprintf (ext_res, "%srecordType.%s", gprefix, ext);
795 zh->m_record_type = res_get (zh->res, ext_res);
797 if (!zh->m_record_type)
799 if (zh->records_processed < zh->m_file_verbose_limit)
800 yaz_log (YLOG_LOG, "? %s", fname);
803 /* determine match criteria */
804 if (!zh->m_record_id)
806 sprintf (ext_res, "%srecordId.%s", gprefix, ext);
807 zh->m_record_id = res_get (zh->res, ext_res);
811 recType_byName (zh->reg->recTypes, zh->res, zh->m_record_type,
812 &recTypeClientData)))
814 yaz_log(YLOG_WARN, "No such record type: %s", zh->m_record_type);
818 switch(recType->version)
823 yaz_log(YLOG_WARN, "Bad filter version: %s", zh->m_record_type);
825 if (sysno && deleteFlag)
831 if (zh->path_reg && !yaz_is_abspath (fname))
833 strcpy (full_rep, zh->path_reg);
834 strcat (full_rep, "/");
835 strcat (full_rep, fname);
838 strcpy (full_rep, fname);
840 if ((fd = open (full_rep, O_BINARY|O_RDONLY)) == -1)
842 yaz_log (YLOG_WARN|YLOG_ERRNO, "open %s", full_rep);
843 zh->m_record_type = original_record_type;
847 fi = file_read_start (fd);
850 fi->file_moffset = fi->file_offset;
851 fi->file_more = 0; /* file_end not called (yet) */
852 r = file_extract_record (zh, sysno, fname, deleteFlag, fi, 1,
853 recType, recTypeClientData);
855 { /* file_end has been called so reset offset .. */
856 fi->file_offset = fi->file_moffset;
857 lseek(fi->fd, fi->file_moffset, SEEK_SET);
871 zh->m_record_type = original_record_type;
876 If sysno is provided, then it's used to identify the reocord.
877 If not, and match_criteria is provided, then sysno is guessed
878 If not, and a record is provided, then sysno is got from there
881 ZEBRA_RES zebra_buffer_extract_record(ZebraHandle zh,
882 const char *buf, size_t buf_size,
885 const char *recordType,
887 const char *match_criteria,
893 RecordAttr *recordAttr;
894 struct recExtractCtrl extractCtrl;
896 const char *matchStr = 0;
897 RecType recType = NULL;
900 long recordOffset = 0;
901 struct zebra_fetch_control fc;
902 const char *pr_fname = fname; /* filename to print .. */
903 int show_progress = zh->records_processed < zh->m_file_verbose_limit ? 1:0;
905 zebra_init_log_level();
908 pr_fname = "<no file>"; /* make it printable if file is omitted */
911 fc.record_int_buf = buf;
912 fc.record_int_len = buf_size;
913 fc.record_int_pos = 0;
915 fc.record_offset = 0;
917 extractCtrl.offset = 0;
918 extractCtrl.readf = zebra_record_int_read;
919 extractCtrl.seekf = zebra_record_int_seek;
920 extractCtrl.tellf = zebra_record_int_tell;
921 extractCtrl.endf = zebra_record_int_end;
922 extractCtrl.first_record = 1;
923 extractCtrl.fh = &fc;
925 zebra_rec_keys_reset(zh->reg->keys);
926 zebra_rec_keys_reset(zh->reg->sortKeys);
928 if (zebraExplain_curDatabase (zh->reg->zei, zh->basenames[0]))
930 if (zebraExplain_newDatabase (zh->reg->zei, zh->basenames[0],
931 zh->m_explain_database))
935 if (recordType && *recordType)
937 yaz_log(log_level, "Record type explicitly specified: %s", recordType);
938 recType = recType_byName (zh->reg->recTypes, zh->res, recordType,
943 if (!(zh->m_record_type))
945 yaz_log (YLOG_WARN, "No such record type defined");
948 yaz_log(log_level, "Get record type from rgroup: %s",
950 recType = recType_byName (zh->reg->recTypes, zh->res,
951 zh->m_record_type, &clientData);
952 recordType = zh->m_record_type;
957 yaz_log (YLOG_WARN, "No such record type: %s", recordType);
961 extractCtrl.init = extract_init;
962 extractCtrl.tokenAdd = extract_token_add;
963 extractCtrl.schemaAdd = extract_schema_add;
964 extractCtrl.dh = zh->reg->dh;
965 extractCtrl.handle = zh;
966 extractCtrl.match_criteria[0] = '\0';
967 extractCtrl.staticrank = 0;
969 init_extractCtrl(zh, &extractCtrl);
971 extract_set_store_data_prepare(&extractCtrl);
973 r = (*recType->extract)(clientData, &extractCtrl);
975 if (r == RECCTRL_EXTRACT_EOF)
977 else if (r == RECCTRL_EXTRACT_ERROR_GENERIC)
979 /* error occured during extraction ... */
980 yaz_log (YLOG_WARN, "extract error: generic");
983 else if (r == RECCTRL_EXTRACT_ERROR_NO_SUCH_FILTER)
985 /* error occured during extraction ... */
986 yaz_log (YLOG_WARN, "extract error: no such filter");
990 all_matches_add(&extractCtrl);
992 if (extractCtrl.match_criteria[0])
993 match_criteria = extractCtrl.match_criteria;
999 if (match_criteria && *match_criteria) {
1000 matchStr = match_criteria;
1002 if (zh->m_record_id && *zh->m_record_id) {
1003 matchStr = fileMatchStr (zh, zh->reg->keys, pr_fname,
1007 yaz_log (YLOG_WARN, "Bad match criteria (recordID)");
1014 int db_ord = zebraExplain_get_database_ord(zh->reg->zei);
1015 char *rinfo = dict_lookup_ord(zh->reg->matchDict, db_ord,
1019 assert(*rinfo == sizeof(*sysno));
1020 memcpy (sysno, rinfo+1, sizeof(*sysno));
1024 if (zebra_rec_keys_empty(zh->reg->keys))
1026 /* the extraction process returned no information - the record
1027 is probably empty - unless flagShowRecords is in use */
1037 yaz_log (YLOG_LOG, "delete %s %s %ld", recordType,
1038 pr_fname, (long) recordOffset);
1039 yaz_log (YLOG_WARN, "cannot delete record above (seems new)");
1043 yaz_log (YLOG_LOG, "add %s %s %ld", recordType, pr_fname,
1044 (long) recordOffset);
1045 rec = rec_new (zh->reg->records);
1047 *sysno = rec->sysno;
1049 recordAttr = rec_init_attr (zh->reg->zei, rec);
1050 recordAttr->staticrank = extractCtrl.staticrank;
1054 int db_ord = zebraExplain_get_database_ord(zh->reg->zei);
1055 dict_insert_ord(zh->reg->matchDict, db_ord, matchStr,
1056 sizeof(*sysno), sysno);
1060 extract_flushSortKeys (zh, *sysno, 1, zh->reg->sortKeys);
1061 extract_flushRecordKeys (zh, *sysno, 1, zh->reg->keys,
1062 recordAttr->staticrank);
1063 zh->records_inserted++;
1067 /* record already exists */
1068 zebra_rec_keys_t delkeys = zebra_rec_keys_open();
1069 zebra_rec_keys_t sortKeys = zebra_rec_keys_open();
1072 yaz_log (YLOG_LOG, "skipped %s %s %ld",
1073 recordType, pr_fname, (long) recordOffset);
1078 rec = rec_get (zh->reg->records, *sysno);
1081 recordAttr = rec_init_attr (zh->reg->zei, rec);
1083 zebra_rec_keys_set_buf(delkeys,
1084 rec->info[recInfo_delKeys],
1085 rec->size[recInfo_delKeys],
1087 zebra_rec_keys_set_buf(sortKeys,
1088 rec->info[recInfo_sortKeys],
1089 rec->size[recInfo_sortKeys],
1092 extract_flushSortKeys (zh, *sysno, 0, sortKeys);
1093 extract_flushRecordKeys (zh, *sysno, 0, delkeys,
1094 recordAttr->staticrank);
1097 /* record going to be deleted */
1098 if (zebra_rec_keys_empty(delkeys))
1100 yaz_log(YLOG_LOG, "delete %s %s %ld", recordType,
1101 pr_fname, (long) recordOffset);
1102 yaz_log(YLOG_WARN, "cannot delete file above, "
1103 "storeKeys false (3)");
1108 yaz_log(YLOG_LOG, "delete %s %s %ld", recordType,
1109 pr_fname, (long) recordOffset);
1110 zh->records_deleted++;
1113 int db_ord = zebraExplain_get_database_ord(zh->reg->zei);
1114 dict_delete_ord(zh->reg->matchDict, db_ord, matchStr);
1116 rec_del (zh->reg->records, &rec);
1125 yaz_log(YLOG_LOG, "update %s %s %ld", recordType,
1126 pr_fname, (long) recordOffset);
1127 recordAttr->staticrank = extractCtrl.staticrank;
1128 extract_flushSortKeys (zh, *sysno, 1, zh->reg->sortKeys);
1129 extract_flushRecordKeys (zh, *sysno, 1, zh->reg->keys,
1130 recordAttr->staticrank);
1131 zh->records_updated++;
1133 zebra_rec_keys_close(delkeys);
1134 zebra_rec_keys_close(sortKeys);
1136 /* update file type */
1137 xfree (rec->info[recInfo_fileType]);
1138 rec->info[recInfo_fileType] =
1139 rec_strdup (recordType, &rec->size[recInfo_fileType]);
1141 /* update filename */
1142 xfree (rec->info[recInfo_filename]);
1143 rec->info[recInfo_filename] =
1144 rec_strdup (fname, &rec->size[recInfo_filename]);
1146 /* update delete keys */
1147 xfree (rec->info[recInfo_delKeys]);
1148 if (!zebra_rec_keys_empty(zh->reg->keys) && zh->m_store_keys == 1)
1150 zebra_rec_keys_get_buf(zh->reg->keys,
1151 &rec->info[recInfo_delKeys],
1152 &rec->size[recInfo_delKeys]);
1156 rec->info[recInfo_delKeys] = NULL;
1157 rec->size[recInfo_delKeys] = 0;
1159 /* update sort keys */
1160 xfree (rec->info[recInfo_sortKeys]);
1162 zebra_rec_keys_get_buf(zh->reg->sortKeys,
1163 &rec->info[recInfo_sortKeys],
1164 &rec->size[recInfo_sortKeys]);
1166 /* save file size of original record */
1167 zebraExplain_recordBytesIncrement (zh->reg->zei,
1168 - recordAttr->recordSize);
1170 recordAttr->recordSize = fi->file_moffset - recordOffset;
1171 if (!recordAttr->recordSize)
1172 recordAttr->recordSize = fi->file_max - recordOffset;
1174 recordAttr->recordSize = buf_size;
1176 zebraExplain_recordBytesIncrement (zh->reg->zei,
1177 recordAttr->recordSize);
1179 /* set run-number for this record */
1180 recordAttr->runNumber =
1181 zebraExplain_runNumberIncrement (zh->reg->zei, 0);
1183 /* update store data */
1184 xfree (rec->info[recInfo_storeData]);
1186 /* update store data */
1187 if (zh->store_data_buf)
1189 rec->size[recInfo_storeData] = zh->store_data_size;
1190 rec->info[recInfo_storeData] = zh->store_data_buf;
1191 zh->store_data_buf = 0;
1193 else if (zh->m_store_data)
1195 rec->size[recInfo_storeData] = recordAttr->recordSize;
1196 rec->info[recInfo_storeData] = (char *)
1197 xmalloc (recordAttr->recordSize);
1198 memcpy (rec->info[recInfo_storeData], buf, recordAttr->recordSize);
1202 rec->info[recInfo_storeData] = NULL;
1203 rec->size[recInfo_storeData] = 0;
1205 /* update database name */
1206 xfree (rec->info[recInfo_databaseName]);
1207 rec->info[recInfo_databaseName] =
1208 rec_strdup (zh->basenames[0], &rec->size[recInfo_databaseName]);
1211 recordAttr->recordOffset = recordOffset;
1213 /* commit this record */
1214 rec_put (zh->reg->records, &rec);
1219 ZEBRA_RES zebra_extract_explain(void *handle, Record rec, data1_node *n)
1221 ZebraHandle zh = (ZebraHandle) handle;
1222 struct recExtractCtrl extractCtrl;
1224 if (zebraExplain_curDatabase (zh->reg->zei,
1225 rec->info[recInfo_databaseName]))
1228 if (zebraExplain_newDatabase (zh->reg->zei,
1229 rec->info[recInfo_databaseName], 0))
1233 zebra_rec_keys_reset(zh->reg->keys);
1234 zebra_rec_keys_reset(zh->reg->sortKeys);
1236 extractCtrl.init = extract_init;
1237 extractCtrl.tokenAdd = extract_token_add;
1238 extractCtrl.schemaAdd = extract_schema_add;
1239 extractCtrl.dh = zh->reg->dh;
1241 init_extractCtrl(zh, &extractCtrl);
1243 extractCtrl.flagShowRecords = 0;
1244 extractCtrl.match_criteria[0] = '\0';
1245 extractCtrl.staticrank = 0;
1246 extractCtrl.handle = handle;
1247 extractCtrl.first_record = 1;
1249 extract_set_store_data_prepare(&extractCtrl);
1252 grs_extract_tree(&extractCtrl, n);
1254 if (rec->size[recInfo_delKeys])
1256 zebra_rec_keys_t delkeys = zebra_rec_keys_open();
1258 zebra_rec_keys_t sortkeys = zebra_rec_keys_open();
1260 zebra_rec_keys_set_buf(delkeys, rec->info[recInfo_delKeys],
1261 rec->size[recInfo_delKeys],
1263 extract_flushRecordKeys (zh, rec->sysno, 0, delkeys, 0);
1264 zebra_rec_keys_close(delkeys);
1266 zebra_rec_keys_set_buf(sortkeys, rec->info[recInfo_sortKeys],
1267 rec->size[recInfo_sortKeys],
1270 extract_flushSortKeys (zh, rec->sysno, 0, sortkeys);
1271 zebra_rec_keys_close(sortkeys);
1273 extract_flushRecordKeys (zh, rec->sysno, 1, zh->reg->keys, 0);
1274 extract_flushSortKeys (zh, rec->sysno, 1, zh->reg->sortKeys);
1276 xfree (rec->info[recInfo_delKeys]);
1277 zebra_rec_keys_get_buf(zh->reg->keys,
1278 &rec->info[recInfo_delKeys],
1279 &rec->size[recInfo_delKeys]);
1281 xfree (rec->info[recInfo_sortKeys]);
1282 zebra_rec_keys_get_buf(zh->reg->sortKeys,
1283 &rec->info[recInfo_sortKeys],
1284 &rec->size[recInfo_sortKeys]);
1288 void extract_rec_keys_adjust(ZebraHandle zh, int is_insert,
1289 zebra_rec_keys_t reckeys)
1291 ZebraExplainInfo zei = zh->reg->zei;
1295 struct ord_stat *next;
1298 if (zebra_rec_keys_rewind(reckeys))
1300 struct ord_stat *ord_list = 0;
1304 struct it_key key_in;
1305 while(zebra_rec_keys_read(reckeys, &str, &slen, &key_in))
1307 int ord = CAST_ZINT_TO_INT(key_in.mem[0]);
1309 for (p = ord_list; p ; p = p->next)
1317 p = xmalloc(sizeof(*p));
1328 struct ord_stat *p1 = p;
1331 zebraExplain_ord_adjust_occurrences(zei, p->ord, p->no, 1);
1333 zebraExplain_ord_adjust_occurrences(zei, p->ord, - p->no, -1);
1340 void extract_flushRecordKeys (ZebraHandle zh, SYSNO sysno,
1342 zebra_rec_keys_t reckeys,
1345 ZebraExplainInfo zei = zh->reg->zei;
1347 extract_rec_keys_adjust(zh, cmd, reckeys);
1349 if (!zh->reg->key_buf)
1351 int mem= 1024*1024* atoi( res_get_def( zh->res, "memmax", "8"));
1354 yaz_log(YLOG_WARN, "Invalid memory setting, using default 8 MB");
1357 /* FIXME: That "8" should be in a default settings include */
1358 /* not hard-coded here! -H */
1359 zh->reg->key_buf = (char**) xmalloc (mem);
1360 zh->reg->ptr_top = mem/sizeof(char*);
1362 zh->reg->key_buf_used = 0;
1363 zh->reg->key_file_no = 0;
1365 zebraExplain_recordCountIncrement (zei, cmd ? 1 : -1);
1367 if (zebra_rec_keys_rewind(reckeys))
1371 struct it_key key_in;
1372 while(zebra_rec_keys_read(reckeys, &str, &slen, &key_in))
1375 struct it_key key_out;
1376 zint *keyp = key_out.mem;
1378 assert(key_in.len == 4);
1380 /* check for buffer overflow */
1381 if (zh->reg->key_buf_used + 1024 >
1382 (zh->reg->ptr_top -zh->reg->ptr_i)*sizeof(char*))
1383 extract_flushWriteKeys (zh, 0);
1386 assert(zh->reg->ptr_i > 0);
1387 (zh->reg->key_buf)[zh->reg->ptr_top - zh->reg->ptr_i] =
1388 (char*)zh->reg->key_buf + zh->reg->key_buf_used;
1390 /* encode the ordinal value (field/use/attribute) .. */
1391 ch = CAST_ZINT_TO_INT(key_in.mem[0]);
1392 zh->reg->key_buf_used +=
1393 key_SU_encode(ch, (char*)zh->reg->key_buf +
1394 zh->reg->key_buf_used);
1396 /* copy the 0-terminated stuff from str to output */
1397 memcpy((char*)zh->reg->key_buf + zh->reg->key_buf_used, str, slen);
1398 zh->reg->key_buf_used += slen;
1399 ((char*)zh->reg->key_buf)[(zh->reg->key_buf_used)++] = '\0';
1401 /* the delete/insert indicator */
1402 ((char*)zh->reg->key_buf)[(zh->reg->key_buf_used)++] = cmd;
1404 if (zh->m_staticrank) /* rank config enabled ? */
1408 yaz_log(YLOG_WARN, "staticrank = %ld. Setting to 0",
1412 *keyp++ = staticrank;
1418 if (key_in.mem[1]) /* filter specified record ID */
1419 *keyp++ = key_in.mem[1];
1422 *keyp++ = key_in.mem[2]; /* section_id */
1423 *keyp++ = key_in.mem[3]; /* sequence .. */
1425 memcpy((char*)zh->reg->key_buf + zh->reg->key_buf_used,
1426 &key_out, sizeof(key_out));
1427 (zh->reg->key_buf_used) += sizeof(key_out);
1432 void extract_flushWriteKeys (ZebraHandle zh, int final)
1433 /* optimizing: if final=1, and no files written yet */
1434 /* push the keys directly to merge, sidestepping the */
1435 /* temp file altogether. Speeds small updates */
1438 char out_fname[200];
1440 struct encode_info encode_info;
1441 int ptr_i = zh->reg->ptr_i;
1446 if (!zh->reg->key_buf || ptr_i <= 0)
1448 yaz_log(log_level, " nothing to flush section=%d buf=%p i=%d",
1449 zh->reg->key_file_no, zh->reg->key_buf, ptr_i);
1453 (zh->reg->key_file_no)++;
1454 yaz_log (YLOG_LOG, "sorting section %d", (zh->reg->key_file_no));
1455 yaz_log(log_level, " sort_buff at %p n=%d",
1456 zh->reg->key_buf + zh->reg->ptr_top - ptr_i,ptr_i);
1458 qsort (zh->reg->key_buf + zh->reg->ptr_top - ptr_i, ptr_i,
1459 sizeof(char*), key_qsort_compare);
1461 /* zebra.cfg: tempfiles:
1462 Y: always use temp files (old way)
1463 A: use temp files, if more than one (auto)
1464 = if this is both the last and the first
1465 N: never bother with temp files (new) */
1467 temp_policy=toupper(res_get_def(zh->res,"tempfiles","auto")[0]);
1468 if (temp_policy != 'Y' && temp_policy != 'N' && temp_policy != 'A') {
1469 yaz_log (YLOG_WARN, "Illegal tempfiles setting '%c'. using 'Auto' ",
1474 if ( ( temp_policy =='N' ) || /* always from memory */
1475 ( ( temp_policy =='A' ) && /* automatic */
1476 (zh->reg->key_file_no == 1) && /* this is first time */
1477 (final) ) ) /* and last (=only) time */
1478 { /* go directly from memory */
1479 zh->reg->key_file_no =0; /* signal not to read files */
1480 zebra_index_merge(zh);
1482 zh->reg->key_buf_used = 0;
1486 /* Not doing directly from memory, write into a temp file */
1487 extract_get_fname_tmp (zh, out_fname, zh->reg->key_file_no);
1489 if (!(outf = fopen (out_fname, "wb")))
1491 yaz_log (YLOG_FATAL|YLOG_ERRNO, "fopen %s", out_fname);
1494 yaz_log (YLOG_LOG, "writing section %d", zh->reg->key_file_no);
1495 prevcp = cp = (zh->reg->key_buf)[zh->reg->ptr_top - ptr_i];
1497 encode_key_init (&encode_info);
1498 encode_key_write (cp, &encode_info, outf);
1502 cp = (zh->reg->key_buf)[zh->reg->ptr_top - ptr_i];
1503 if (strcmp (cp, prevcp))
1505 encode_key_flush ( &encode_info, outf);
1506 encode_key_init (&encode_info);
1507 encode_key_write (cp, &encode_info, outf);
1511 encode_key_write (cp + strlen(cp), &encode_info, outf);
1513 encode_key_flush ( &encode_info, outf);
1515 qsort (key_buf + ptr_top-ptr_i, ptr_i, sizeof(char*), key_x_compare);
1516 extract_get_fname_tmp (out_fname, key_file_no);
1518 if (!(outf = fopen (out_fname, "wb")))
1520 yaz_log (YLOG_FATAL|YLOG_ERRNO, "fopen %s", out_fname);
1523 yaz_log (YLOG_LOG, "writing section %d", key_file_no);
1525 prevcp = key_buf[ptr_top-i];
1527 if (!--i || strcmp (prevcp, key_buf[ptr_top-i]))
1529 key_y_len = strlen(prevcp)+1;
1531 yaz_log (YLOG_LOG, "key_y_len: %2d %02x %02x %s",
1532 key_y_len, prevcp[0], prevcp[1], 2+prevcp);
1534 qsort (key_buf + ptr_top-ptr_i, ptr_i - i,
1535 sizeof(char*), key_y_compare);
1536 cp = key_buf[ptr_top-ptr_i];
1538 encode_key_init (&encode_info);
1539 encode_key_write (cp, &encode_info, outf);
1542 cp = key_buf[ptr_top-ptr_i];
1543 encode_key_write (cp+key_y_len, &encode_info, outf);
1545 encode_key_flush ( &encode_info, outf);
1548 prevcp = key_buf[ptr_top-ptr_i];
1553 yaz_log (YLOG_FATAL|YLOG_ERRNO, "fclose %s", out_fname);
1556 yaz_log (YLOG_LOG, "finished section %d", zh->reg->key_file_no);
1558 zh->reg->key_buf_used = 0;
1561 ZEBRA_RES zebra_snippets_rec_keys(ZebraHandle zh,
1562 zebra_rec_keys_t reckeys,
1563 zebra_snippets *snippets)
1565 NMEM nmem = nmem_create();
1566 if (zebra_rec_keys_rewind(reckeys))
1571 while (zebra_rec_keys_read(reckeys, &str, &slen, &key))
1573 char dst_buf[IT_MAX_WORD];
1574 char *dst_term = dst_buf;
1579 assert(key.len <= 4 && key.len > 2);
1580 seqno = key.mem[key.len-1];
1581 ord = CAST_ZINT_TO_INT(key.mem[0]);
1583 zebraExplain_lookup_ord(zh->reg->zei, ord, &index_type,
1584 0/* db */, 0 /* string_index */);
1586 zebra_term_untrans_iconv(zh, nmem, index_type,
1588 zebra_snippets_append(snippets, seqno, ord, dst_term);
1596 void print_rec_keys(ZebraHandle zh, zebra_rec_keys_t reckeys)
1598 yaz_log(YLOG_LOG, "print_rec_keys");
1599 if (zebra_rec_keys_rewind(reckeys))
1604 while (zebra_rec_keys_read(reckeys, &str, &slen, &key))
1606 char dst_buf[IT_MAX_WORD];
1609 int ord = CAST_ZINT_TO_INT(key.mem[0]);
1611 assert(key.len <= 4 && key.len > 2);
1613 zebraExplain_lookup_ord(zh->reg->zei, ord, &index_type, &db, 0);
1615 seqno = key.mem[key.len-1];
1617 zebra_term_untrans(zh, index_type, dst_buf, str);
1619 yaz_log(YLOG_LOG, "ord=%d seqno=" ZINT_FORMAT
1620 " term=%s", ord, seqno, dst_buf);
1625 static void extract_add_index_string(RecWord *p, const char *str, int length)
1629 ZebraHandle zh = p->extractCtrl->handle;
1630 ZebraExplainInfo zei = zh->reg->zei;
1636 ch = zebraExplain_lookup_attr_str(zei, p->index_type, p->index_name);
1638 ch = zebraExplain_add_attr_str(zei, p->index_type, p->index_name);
1642 key.mem[1] = p->record_id;
1643 key.mem[2] = p->section_id;
1644 key.mem[3] = p->seqno;
1653 for (i = 0; i<length && i < 20; i++)
1654 sprintf(strz+strlen(strz), "%02X", str[i] & 0xff);
1655 /* just for debugging .. */
1656 yaz_log(YLOG_LOG, "add: set=%d use=%d "
1657 "record_id=%lld section_id=%lld seqno=%lld %s",
1658 p->attrSet, p->attrUse, p->record_id, p->section_id, p->seqno,
1663 zebra_rec_keys_write(zh->reg->keys, str, length, &key);
1666 static void extract_add_sort_string(RecWord *p, const char *str, int length)
1670 ZebraHandle zh = p->extractCtrl->handle;
1671 ZebraExplainInfo zei = zh->reg->zei;
1677 ch = zebraExplain_lookup_attr_str(zei, p->index_type, p->index_name);
1679 ch = zebraExplain_add_attr_str(zei, p->index_type, p->index_name);
1682 key.mem[1] = p->record_id;
1683 key.mem[2] = p->section_id;
1684 key.mem[3] = p->seqno;
1693 for (i = 0; i<length && i < 20; i++)
1694 sprintf(strz+strlen(strz), "%02X", str[i] & 0xff);
1695 /* just for debugging .. */
1696 yaz_log(YLOG_LOG, "add: set=%d use=%d "
1697 "record_id=%lld section_id=%lld seqno=%lld %s",
1698 p->attrSet, p->attrUse, p->record_id, p->section_id, p->seqno,
1702 zebra_rec_keys_write(zh->reg->sortKeys, str, length, &key);
1705 static void extract_add_string (RecWord *p, const char *string, int length)
1707 assert (length > 0);
1708 if (zebra_maps_is_sort (p->zebra_maps, p->index_type))
1709 extract_add_sort_string (p, string, length);
1711 extract_add_index_string (p, string, length);
1714 static void extract_add_incomplete_field (RecWord *p)
1716 const char *b = p->term_buf;
1717 int remain = p->term_len;
1718 const char **map = 0;
1721 map = zebra_maps_input(p->zebra_maps, p->index_type, &b, remain, 0);
1725 char buf[IT_MAX_WORD+1];
1729 while (map && *map && **map == *CHR_SPACE)
1731 remain = p->term_len - (b - p->term_buf);
1733 map = zebra_maps_input(p->zebra_maps, p->index_type, &b,
1741 while (map && *map && **map != *CHR_SPACE)
1743 const char *cp = *map;
1745 while (i < IT_MAX_WORD && *cp)
1747 remain = p->term_len - (b - p->term_buf);
1749 map = zebra_maps_input(p->zebra_maps, p->index_type, &b, remain, 0);
1755 extract_add_string (p, buf, i);
1760 static void extract_add_complete_field (RecWord *p)
1762 const char *b = p->term_buf;
1763 char buf[IT_MAX_WORD+1];
1764 const char **map = 0;
1765 int i = 0, remain = p->term_len;
1768 map = zebra_maps_input (p->zebra_maps, p->index_type, &b, remain, 1);
1770 while (remain > 0 && i < IT_MAX_WORD)
1772 while (map && *map && **map == *CHR_SPACE)
1774 remain = p->term_len - (b - p->term_buf);
1778 int first = i ? 0 : 1; /* first position */
1779 map = zebra_maps_input(p->zebra_maps, p->index_type, &b, remain, first);
1787 if (i && i < IT_MAX_WORD)
1788 buf[i++] = *CHR_SPACE;
1789 while (map && *map && **map != *CHR_SPACE)
1791 const char *cp = *map;
1793 if (**map == *CHR_CUT)
1799 if (i >= IT_MAX_WORD)
1801 while (i < IT_MAX_WORD && *cp)
1804 remain = p->term_len - (b - p->term_buf);
1807 map = zebra_maps_input (p->zebra_maps, p->index_type, &b,
1816 extract_add_string (p, buf, i);
1819 static void extract_token_add(RecWord *p)
1823 yaz_log(log_level, "extract_token_add "
1824 "type=%c index=%s seqno=" ZINT_FORMAT " s=%.*s",
1825 p->index_type, p->index_name,
1826 p->seqno, p->term_len, p->term_buf);
1827 if ((wrbuf = zebra_replace(p->zebra_maps, p->index_type, 0,
1828 p->term_buf, p->term_len)))
1830 p->term_buf = wrbuf_buf(wrbuf);
1831 p->term_len = wrbuf_len(wrbuf);
1833 if (zebra_maps_is_complete (p->zebra_maps, p->index_type))
1834 extract_add_complete_field (p);
1836 extract_add_incomplete_field(p);
1839 static void extract_set_store_data_cb(struct recExtractCtrl *p,
1840 void *buf, size_t sz)
1842 ZebraHandle zh = (ZebraHandle) p->handle;
1844 xfree(zh->store_data_buf);
1845 zh->store_data_buf = 0;
1846 zh->store_data_size = 0;
1849 zh->store_data_buf = xmalloc(sz);
1850 zh->store_data_size = sz;
1851 memcpy(zh->store_data_buf, buf, sz);
1855 static void extract_set_store_data_prepare(struct recExtractCtrl *p)
1857 ZebraHandle zh = (ZebraHandle) p->handle;
1858 xfree(zh->store_data_buf);
1859 zh->store_data_buf = 0;
1860 zh->store_data_size = 0;
1861 p->setStoreData = extract_set_store_data_cb;
1864 static void extract_schema_add (struct recExtractCtrl *p, Odr_oid *oid)
1866 ZebraHandle zh = (ZebraHandle) p->handle;
1867 zebraExplain_addSchema (zh->reg->zei, oid);
1870 void extract_flushSortKeys (ZebraHandle zh, SYSNO sysno,
1871 int cmd, zebra_rec_keys_t reckeys)
1873 if (zebra_rec_keys_rewind(reckeys))
1875 SortIdx sortIdx = zh->reg->sortIdx;
1878 struct it_key key_in;
1880 sortIdx_sysno (sortIdx, sysno);
1882 while (zebra_rec_keys_read(reckeys, &str, &slen, &key_in))
1884 int ord = CAST_ZINT_TO_INT(key_in.mem[0]);
1886 sortIdx_type(sortIdx, ord);
1888 sortIdx_add(sortIdx, str, slen);
1890 sortIdx_add(sortIdx, "", 1);
1895 static void encode_key_init(struct encode_info *i)
1897 i->encode_handle = iscz1_start();
1898 i->decode_handle = iscz1_start();
1901 static void encode_key_write (char *k, struct encode_info *i, FILE *outf)
1904 char *bp = i->buf, *bp0;
1905 const char *src = (char *) &key;
1907 /* copy term to output buf */
1908 while ((*bp++ = *k++))
1910 /* and copy & align key so we can mangle */
1911 memcpy (&key, k+1, sizeof(struct it_key)); /* *k is insert/delete */
1915 key_logdump_txt(YLOG_LOG, &key, *k ? "i" : "d");
1917 assert(key.mem[0] >= 0);
1920 iscz1_encode(i->encode_handle, &bp, &src);
1922 *bp0 = (*k * 128) + bp - bp0 - 1; /* length and insert/delete combined */
1923 if (fwrite (i->buf, bp - i->buf, 1, outf) != 1)
1925 yaz_log (YLOG_FATAL|YLOG_ERRNO, "fwrite");
1934 const char *src = bp0+1;
1935 char *dst = (char*) &key2;
1936 iscz1_decode(i->decode_handle, &dst, &src);
1938 key_logdump_txt(YLOG_LOG, &key2, *k ? "i" : "d");
1940 assert(key2.mem[1]);
1945 static void encode_key_flush (struct encode_info *i, FILE *outf)
1947 iscz1_stop(i->encode_handle);
1948 iscz1_stop(i->decode_handle);
1954 * indent-tabs-mode: nil
1956 * vim: shiftwidth=4 tabstop=8 expandtab