1 /* $Id: extract.c,v 1.220 2006-06-13 12:02:06 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"
51 #define ENCODE_BUFLEN 768
55 char buf[ENCODE_BUFLEN];
58 static int log_level = 0;
59 static int log_level_initialized = 1;
61 static void zebra_init_log_level()
63 if (!log_level_initialized)
65 log_level = yaz_log_module_level("extract");
66 log_level_initialized = 1;
70 static void extract_flushRecordKeys (ZebraHandle zh, SYSNO sysno,
71 int cmd, zebra_rec_keys_t reckeys,
73 static void extract_flushSortKeys (ZebraHandle zh, SYSNO sysno,
74 int cmd, zebra_rec_keys_t skp);
75 static void extract_schema_add (struct recExtractCtrl *p, Odr_oid *oid);
76 static void extract_token_add (RecWord *p);
78 static void encode_key_init (struct encode_info *i);
79 static void encode_key_write (char *k, struct encode_info *i, FILE *outf);
80 static void encode_key_flush (struct encode_info *i, FILE *outf);
82 #define USE_SHELLSORT 0
85 static void shellsort(void *ar, int r, size_t s,
86 int (*cmp)(const void *a, const void *b))
91 static const int incs[16] = { 1391376, 463792, 198768, 86961, 33936,
92 13776, 4592, 1968, 861, 336,
93 112, 48, 21, 7, 3, 1 };
94 for ( k = 0; k < 16; k++)
95 for (h = incs[k], i = h; i < r; i++)
99 while (j > h && (*cmp)(a + s*(j-h), v) > 0)
101 memcpy (a + s*j, a + s*(j-h), s);
104 memcpy (a+s*j, v, s);
109 static void logRecord (ZebraHandle zh)
111 ++zh->records_processed;
112 if (!(zh->records_processed % 1000))
114 yaz_log(YLOG_LOG, "Records: "ZINT_FORMAT" i/u/d "
115 ZINT_FORMAT"/"ZINT_FORMAT"/"ZINT_FORMAT,
116 zh->records_processed, zh->records_inserted,
117 zh->records_updated, zh->records_deleted);
121 static void extract_add_index_string (RecWord *p, const char *str, int length);
123 static void extract_set_store_data_prepare(struct recExtractCtrl *p);
125 static void extract_init (struct recExtractCtrl *p, RecWord *w)
127 w->zebra_maps = p->zebra_maps;
129 w->index_name = "any";
136 static void searchRecordKey(ZebraHandle zh,
137 zebra_rec_keys_t reckeys,
138 const char *index_name,
139 const char **ws, int ws_length)
144 for (i = 0; i<ws_length; i++)
148 ch = zebraExplain_lookup_attr_str(zh->reg->zei, '0', index_name);
150 ch = zebraExplain_lookup_attr_str(zh->reg->zei, 'p', index_name);
152 ch = zebraExplain_lookup_attr_str(zh->reg->zei, 'w', index_name);
157 if (zebra_rec_keys_rewind(reckeys))
164 while (zebra_rec_keys_read(reckeys, &str, &slen, &key))
166 assert(key.len <= 4 && key.len > 2);
168 seqno = key.mem[key.len-1];
170 if (key.mem[0] == ch)
176 woff = seqno - startSeq;
177 if (woff >= 0 && woff < ws_length)
184 struct file_read_info {
185 off_t file_max; /* maximum offset so far */
186 off_t file_offset; /* current offset */
187 off_t file_moffset; /* offset of rec/rec boundary */
192 static struct file_read_info *file_read_start (int fd)
194 struct file_read_info *fi = (struct file_read_info *)
195 xmalloc (sizeof(*fi));
199 fi->file_moffset = 0;
205 static void file_read_stop (struct file_read_info *fi)
210 static off_t file_seek (void *handle, off_t offset)
212 struct file_read_info *p = (struct file_read_info *) handle;
213 p->file_offset = offset;
214 return lseek (p->fd, offset, SEEK_SET);
217 static off_t file_tell (void *handle)
219 struct file_read_info *p = (struct file_read_info *) handle;
220 return p->file_offset;
223 static int file_read (void *handle, char *buf, size_t count)
225 struct file_read_info *p = (struct file_read_info *) handle;
228 r = read (fd, buf, count);
232 if (p->file_offset > p->file_max)
233 p->file_max = p->file_offset;
238 static void file_end (void *handle, off_t offset)
240 struct file_read_info *p = (struct file_read_info *) handle;
242 if (offset != p->file_moffset)
244 p->file_moffset = offset;
249 #define FILE_MATCH_BLANK "\t "
251 static char *fileMatchStr (ZebraHandle zh,
252 zebra_rec_keys_t reckeys,
253 const char *fname, const char *spec)
255 static char dstBuf[2048]; /* static here ??? */
257 const char *s = spec;
261 for (; *s && strchr(FILE_MATCH_BLANK, *s); s++)
268 char attset_str[64], attname_str[64];
272 for (s++; strchr(FILE_MATCH_BLANK, *s); s++)
274 for (i = 0; *s && *s != ',' && *s != ')' &&
275 !strchr(FILE_MATCH_BLANK, *s); s++)
276 if (i+1 < sizeof(attset_str))
277 attset_str[i++] = *s;
278 attset_str[i] = '\0';
280 for (; strchr(FILE_MATCH_BLANK, *s); s++)
283 strcpy(attname_str, attset_str);
286 for (s++; strchr(FILE_MATCH_BLANK, *s); s++)
288 for (i = 0; *s && *s != ')' &&
289 !strchr(FILE_MATCH_BLANK, *s); s++)
290 if (i+1 < sizeof(attname_str))
291 attname_str[i++] = *s;
292 attname_str[i] = '\0';
295 searchRecordKey (zh, reckeys, attname_str, ws, 32);
299 yaz_log (YLOG_WARN, "Missing ) in match criteria %s in group %s",
300 spec, zh->m_group ? zh->m_group : "none");
305 for (i = 0; i<32; i++)
314 dst += strlen(ws[i]);
318 yaz_log (YLOG_WARN, "Record didn't contain match"
319 " fields in (%s,%s)", attset_str, attname_str);
327 const char *spec_src = NULL;
328 const char *s1 = ++s;
329 while (*s1 && !strchr(FILE_MATCH_BLANK, *s1))
333 if (spec_len > sizeof(special)-1)
334 spec_len = sizeof(special)-1;
335 memcpy (special, s, spec_len);
336 special[spec_len] = '\0';
339 if (!strcmp (special, "group"))
340 spec_src = zh->m_group;
341 else if (!strcmp (special, "database"))
342 spec_src = zh->basenames[0];
343 else if (!strcmp (special, "filename")) {
346 else if (!strcmp (special, "type"))
347 spec_src = zh->m_record_type;
352 strcpy (dst, spec_src);
353 dst += strlen (spec_src);
356 else if (*s == '\"' || *s == '\'')
358 int stopMarker = *s++;
362 while (*s && *s != stopMarker)
364 if (i+1 < sizeof(tmpString))
365 tmpString[i++] = *s++;
370 strcpy (dst, tmpString);
371 dst += strlen (tmpString);
375 yaz_log (YLOG_WARN, "Syntax error in match criteria %s in group %s",
376 spec, zh->m_group ? zh->m_group : "none");
383 yaz_log (YLOG_WARN, "No match criteria for record %s in group %s",
384 fname, zh->m_group ? zh->m_group : "none");
391 struct recordLogInfo {
394 struct recordGroup *rGroup;
397 static void init_extractCtrl(ZebraHandle zh, struct recExtractCtrl *ctrl)
400 for (i = 0; i<256; i++)
402 if (zebra_maps_is_positioned(zh->reg->zebra_maps, i))
407 ctrl->zebra_maps = zh->reg->zebra_maps;
408 ctrl->flagShowRecords = !zh->m_flag_rw;
411 static void all_matches_add(struct recExtractCtrl *ctrl)
414 extract_init(ctrl, &word);
415 word.index_name = "allrecords";
416 word.index_type = 'w';
418 extract_add_index_string (&word, "", 0);
421 static ZEBRA_RES file_extract_record(ZebraHandle zh,
422 SYSNO *sysno, const char *fname,
424 struct file_read_info *fi,
427 void *recTypeClientData)
429 const char *match_str_to_print = "";
430 RecordAttr *recordAttr;
432 const char *matchStr = 0;
435 off_t recordOffset = 0;
436 struct recExtractCtrl extractCtrl;
438 /* announce database */
439 if (zebraExplain_curDatabase (zh->reg->zei, zh->basenames[0]))
441 if (zebraExplain_newDatabase (zh->reg->zei, zh->basenames[0],
442 zh->m_explain_database))
448 /* we are going to read from a file, so prepare the extraction */
449 zebra_rec_keys_reset(zh->reg->keys);
451 zebra_rec_keys_reset(zh->reg->sortKeys);
452 recordOffset = fi->file_moffset;
453 extractCtrl.handle = zh;
454 extractCtrl.offset = fi->file_moffset;
455 extractCtrl.readf = file_read;
456 extractCtrl.seekf = file_seek;
457 extractCtrl.tellf = file_tell;
458 extractCtrl.endf = file_end;
460 extractCtrl.init = extract_init;
461 extractCtrl.tokenAdd = extract_token_add;
462 extractCtrl.schemaAdd = extract_schema_add;
463 extractCtrl.dh = zh->reg->dh;
464 extractCtrl.match_criteria[0] = '\0';
465 extractCtrl.staticrank = 0;
467 extractCtrl.first_record = fi->file_offset ? 0 : 1;
469 extract_set_store_data_prepare(&extractCtrl);
471 init_extractCtrl(zh, &extractCtrl);
474 printf ("File: %s " PRINTF_OFF_T "\n", fname, recordOffset);
478 sprintf (msg, "%s:" PRINTF_OFF_T , fname, recordOffset);
479 yaz_log_init_prefix2 (msg);
482 r = (*recType->extract)(recTypeClientData, &extractCtrl);
484 yaz_log_init_prefix2 (0);
485 if (r == RECCTRL_EXTRACT_EOF)
487 else if (r == RECCTRL_EXTRACT_ERROR_GENERIC)
489 /* error occured during extraction ... */
491 zh->records_processed < zh->m_file_verbose_limit)
493 yaz_log (YLOG_WARN, "fail %s %s " PRINTF_OFF_T, zh->m_record_type,
494 fname, recordOffset);
498 else if (r == RECCTRL_EXTRACT_ERROR_NO_SUCH_FILTER)
500 /* error occured during extraction ... */
502 zh->records_processed < zh->m_file_verbose_limit)
504 yaz_log (YLOG_WARN, "no filter for %s %s "
505 PRINTF_OFF_T, zh->m_record_type,
506 fname, recordOffset);
510 all_matches_add(&extractCtrl);
511 if (extractCtrl.match_criteria[0])
512 matchStr = extractCtrl.match_criteria;
515 /* if matchStr is set now - we assume it's printable .
516 For internal matchStr (see below) we don't print */
518 match_str_to_print = matchStr;
520 /* perform internal match if sysno not known and if match criteria is
527 if (matchStr == 0 && zh->m_record_id && *zh->m_record_id)
529 matchStr = fileMatchStr (zh, zh->reg->keys, fname,
533 yaz_log(YLOG_WARN, "Bad match criteria");
535 if (zebra_rec_keys_empty(zh->reg->keys))
537 yaz_log(YLOG_WARN, "And no index keys");
544 int db_ord = zebraExplain_get_database_ord(zh->reg->zei);
545 char *rinfo = dict_lookup_ord(zh->reg->matchDict, db_ord,
549 assert(*rinfo == sizeof(*sysno));
550 memcpy (sysno, rinfo+1, sizeof(*sysno));
554 if (! *sysno && zebra_rec_keys_empty(zh->reg->keys) )
556 /* the extraction process returned no information - the record
557 is probably empty - unless flagShowRecords is in use */
561 if (zh->records_processed < zh->m_file_verbose_limit)
562 yaz_log (YLOG_WARN, "empty %s %s " PRINTF_OFF_T, zh->m_record_type,
563 fname, recordOffset);
572 yaz_log (YLOG_LOG, "delete %s %s " PRINTF_OFF_T, zh->m_record_type,
573 fname, recordOffset);
574 yaz_log (YLOG_WARN, "cannot delete record above (seems new)");
578 rec = rec_new (zh->reg->records);
582 if (zh->records_processed < zh->m_file_verbose_limit)
584 yaz_log(YLOG_LOG, "add %s %s " PRINTF_OFF_T
585 " " ZINT_FORMAT " %s" ,
587 fname, recordOffset, *sysno, match_str_to_print);
589 recordAttr = rec_init_attr (zh->reg->zei, rec);
590 recordAttr->staticrank = extractCtrl.staticrank;
594 int db_ord = zebraExplain_get_database_ord(zh->reg->zei);
595 dict_insert_ord(zh->reg->matchDict, db_ord, matchStr,
596 sizeof(*sysno), sysno);
600 extract_flushSortKeys (zh, *sysno, 1, zh->reg->sortKeys);
601 extract_flushRecordKeys (zh, *sysno, 1, zh->reg->keys,
602 recordAttr->staticrank);
603 zh->records_inserted++;
607 /* record already exists */
608 zebra_rec_keys_t delkeys = zebra_rec_keys_open();
610 zebra_rec_keys_t sortKeys = zebra_rec_keys_open();
612 rec = rec_get (zh->reg->records, *sysno);
615 recordAttr = rec_init_attr (zh->reg->zei, rec);
617 zebra_rec_keys_set_buf(delkeys,
618 rec->info[recInfo_delKeys],
619 rec->size[recInfo_delKeys],
622 zebra_rec_keys_set_buf(sortKeys,
623 rec->info[recInfo_sortKeys],
624 rec->size[recInfo_sortKeys],
626 extract_flushSortKeys (zh, *sysno, 0, sortKeys);
627 extract_flushRecordKeys (zh, *sysno, 0, delkeys,
628 recordAttr->staticrank); /* old values */
631 /* record going to be deleted */
632 if (zebra_rec_keys_empty(delkeys))
634 yaz_log (YLOG_LOG, "delete %s %s " PRINTF_OFF_T
636 zh->m_record_type, fname, recordOffset, *sysno);
637 yaz_log (YLOG_WARN, "cannot delete file above, storeKeys false (1)");
641 if (zh->records_processed < zh->m_file_verbose_limit)
643 yaz_log(YLOG_LOG, "delete %s %s " PRINTF_OFF_T
644 " " ZINT_FORMAT " %s" ,
646 fname, recordOffset, *sysno, match_str_to_print);
648 zh->records_deleted++;
651 int db_ord = zebraExplain_get_database_ord(zh->reg->zei);
652 dict_delete_ord(zh->reg->matchDict, db_ord, matchStr);
654 rec_del (zh->reg->records, &rec);
662 /* flush new keys for sort&search etc */
663 if (zh->records_processed < zh->m_file_verbose_limit)
665 yaz_log(YLOG_LOG, "update %s %s " PRINTF_OFF_T
666 " " ZINT_FORMAT " %s" ,
668 fname, recordOffset, *sysno, match_str_to_print);
670 recordAttr->staticrank = extractCtrl.staticrank;
671 extract_flushSortKeys (zh, *sysno, 1, zh->reg->sortKeys);
672 extract_flushRecordKeys (zh, *sysno, 1, zh->reg->keys,
673 recordAttr->staticrank);
674 zh->records_updated++;
676 zebra_rec_keys_close(delkeys);
677 zebra_rec_keys_close(sortKeys);
679 /* update file type */
680 xfree (rec->info[recInfo_fileType]);
681 rec->info[recInfo_fileType] =
682 rec_strdup (zh->m_record_type, &rec->size[recInfo_fileType]);
684 /* update filename */
685 xfree (rec->info[recInfo_filename]);
686 rec->info[recInfo_filename] =
687 rec_strdup (fname, &rec->size[recInfo_filename]);
689 /* update delete keys */
690 xfree (rec->info[recInfo_delKeys]);
691 if (!zebra_rec_keys_empty(zh->reg->keys) && zh->m_store_keys == 1)
693 zebra_rec_keys_get_buf(zh->reg->keys,
694 &rec->info[recInfo_delKeys],
695 &rec->size[recInfo_delKeys]);
699 rec->info[recInfo_delKeys] = NULL;
700 rec->size[recInfo_delKeys] = 0;
703 /* update sort keys */
704 xfree (rec->info[recInfo_sortKeys]);
706 zebra_rec_keys_get_buf(zh->reg->sortKeys,
707 &rec->info[recInfo_sortKeys],
708 &rec->size[recInfo_sortKeys]);
710 /* save file size of original record */
711 zebraExplain_recordBytesIncrement (zh->reg->zei,
712 - recordAttr->recordSize);
713 recordAttr->recordSize = fi->file_moffset - recordOffset;
714 if (!recordAttr->recordSize)
715 recordAttr->recordSize = fi->file_max - recordOffset;
716 zebraExplain_recordBytesIncrement (zh->reg->zei,
717 recordAttr->recordSize);
719 /* set run-number for this record */
720 recordAttr->runNumber = zebraExplain_runNumberIncrement (zh->reg->zei,
723 /* update store data */
724 xfree (rec->info[recInfo_storeData]);
725 if (zh->store_data_buf)
727 rec->size[recInfo_storeData] = zh->store_data_size;
728 rec->info[recInfo_storeData] = zh->store_data_buf;
729 zh->store_data_buf = 0;
731 else if (zh->m_store_data)
733 rec->size[recInfo_storeData] = recordAttr->recordSize;
734 rec->info[recInfo_storeData] = (char *)
735 xmalloc (recordAttr->recordSize);
736 if (lseek (fi->fd, recordOffset, SEEK_SET) < 0)
738 yaz_log (YLOG_ERRNO|YLOG_FATAL, "seek to " PRINTF_OFF_T " in %s",
739 recordOffset, fname);
742 if (read (fi->fd, rec->info[recInfo_storeData], recordAttr->recordSize)
743 < recordAttr->recordSize)
745 yaz_log (YLOG_ERRNO|YLOG_FATAL, "read %d bytes of %s",
746 recordAttr->recordSize, fname);
752 rec->info[recInfo_storeData] = NULL;
753 rec->size[recInfo_storeData] = 0;
755 /* update database name */
756 xfree (rec->info[recInfo_databaseName]);
757 rec->info[recInfo_databaseName] =
758 rec_strdup (zh->basenames[0], &rec->size[recInfo_databaseName]);
761 recordAttr->recordOffset = recordOffset;
763 /* commit this record */
764 rec_put (zh->reg->records, &rec);
769 ZEBRA_RES zebra_extract_file(ZebraHandle zh, SYSNO *sysno, const char *fname,
772 ZEBRA_RES r = ZEBRA_OK;
777 struct file_read_info *fi;
778 const char *original_record_type = 0;
780 void *recTypeClientData;
782 zebra_init_log_level();
784 if (!zh->m_group || !*zh->m_group)
787 sprintf (gprefix, "%s.", zh->m_group);
789 yaz_log(log_level, "zebra_extract_file %s", fname);
791 /* determine file extension */
793 for (i = strlen(fname); --i >= 0; )
796 else if (fname[i] == '.')
798 strcpy (ext, fname+i+1);
801 /* determine file type - depending on extension */
802 original_record_type = zh->m_record_type;
803 if (!zh->m_record_type)
805 sprintf (ext_res, "%srecordType.%s", gprefix, ext);
806 zh->m_record_type = res_get (zh->res, ext_res);
808 if (!zh->m_record_type)
810 if (zh->records_processed < zh->m_file_verbose_limit)
811 yaz_log (YLOG_LOG, "? %s", fname);
814 /* determine match criteria */
815 if (!zh->m_record_id)
817 sprintf (ext_res, "%srecordId.%s", gprefix, ext);
818 zh->m_record_id = res_get (zh->res, ext_res);
822 recType_byName (zh->reg->recTypes, zh->res, zh->m_record_type,
823 &recTypeClientData)))
825 yaz_log(YLOG_WARN, "No such record type: %s", zh->m_record_type);
829 switch(recType->version)
834 yaz_log(YLOG_WARN, "Bad filter version: %s", zh->m_record_type);
836 if (sysno && deleteFlag)
842 if (zh->path_reg && !yaz_is_abspath (fname))
844 strcpy (full_rep, zh->path_reg);
845 strcat (full_rep, "/");
846 strcat (full_rep, fname);
849 strcpy (full_rep, fname);
851 if ((fd = open (full_rep, O_BINARY|O_RDONLY)) == -1)
853 yaz_log (YLOG_WARN|YLOG_ERRNO, "open %s", full_rep);
854 zh->m_record_type = original_record_type;
858 fi = file_read_start (fd);
861 fi->file_moffset = fi->file_offset;
862 fi->file_more = 0; /* file_end not called (yet) */
863 r = file_extract_record (zh, sysno, fname, deleteFlag, fi, 1,
864 recType, recTypeClientData);
866 { /* file_end has been called so reset offset .. */
867 fi->file_offset = fi->file_moffset;
868 lseek(fi->fd, fi->file_moffset, SEEK_SET);
882 zh->m_record_type = original_record_type;
887 If sysno is provided, then it's used to identify the reocord.
888 If not, and match_criteria is provided, then sysno is guessed
889 If not, and a record is provided, then sysno is got from there
892 ZEBRA_RES zebra_buffer_extract_record(ZebraHandle zh,
893 const char *buf, size_t buf_size,
896 const char *recordType,
898 const char *match_criteria,
904 RecordAttr *recordAttr;
905 struct recExtractCtrl extractCtrl;
907 const char *matchStr = 0;
908 RecType recType = NULL;
911 long recordOffset = 0;
912 struct zebra_fetch_control fc;
913 const char *pr_fname = fname; /* filename to print .. */
914 int show_progress = zh->records_processed < zh->m_file_verbose_limit ? 1:0;
916 zebra_init_log_level();
919 pr_fname = "<no file>"; /* make it printable if file is omitted */
922 fc.record_int_buf = buf;
923 fc.record_int_len = buf_size;
924 fc.record_int_pos = 0;
926 fc.record_offset = 0;
928 extractCtrl.offset = 0;
929 extractCtrl.readf = zebra_record_int_read;
930 extractCtrl.seekf = zebra_record_int_seek;
931 extractCtrl.tellf = zebra_record_int_tell;
932 extractCtrl.endf = zebra_record_int_end;
933 extractCtrl.first_record = 1;
934 extractCtrl.fh = &fc;
936 zebra_rec_keys_reset(zh->reg->keys);
937 zebra_rec_keys_reset(zh->reg->sortKeys);
939 if (zebraExplain_curDatabase (zh->reg->zei, zh->basenames[0]))
941 if (zebraExplain_newDatabase (zh->reg->zei, zh->basenames[0],
942 zh->m_explain_database))
946 if (recordType && *recordType)
948 yaz_log(log_level, "Record type explicitly specified: %s", recordType);
949 recType = recType_byName (zh->reg->recTypes, zh->res, recordType,
954 if (!(zh->m_record_type))
956 yaz_log (YLOG_WARN, "No such record type defined");
959 yaz_log(log_level, "Get record type from rgroup: %s",
961 recType = recType_byName (zh->reg->recTypes, zh->res,
962 zh->m_record_type, &clientData);
963 recordType = zh->m_record_type;
968 yaz_log (YLOG_WARN, "No such record type: %s", recordType);
972 extractCtrl.init = extract_init;
973 extractCtrl.tokenAdd = extract_token_add;
974 extractCtrl.schemaAdd = extract_schema_add;
975 extractCtrl.dh = zh->reg->dh;
976 extractCtrl.handle = zh;
977 extractCtrl.match_criteria[0] = '\0';
978 extractCtrl.staticrank = 0;
980 init_extractCtrl(zh, &extractCtrl);
982 extract_set_store_data_prepare(&extractCtrl);
984 r = (*recType->extract)(clientData, &extractCtrl);
986 if (r == RECCTRL_EXTRACT_EOF)
988 else if (r == RECCTRL_EXTRACT_ERROR_GENERIC)
990 /* error occured during extraction ... */
991 yaz_log (YLOG_WARN, "extract error: generic");
994 else if (r == RECCTRL_EXTRACT_ERROR_NO_SUCH_FILTER)
996 /* error occured during extraction ... */
997 yaz_log (YLOG_WARN, "extract error: no such filter");
1001 all_matches_add(&extractCtrl);
1003 if (extractCtrl.match_criteria[0])
1004 match_criteria = extractCtrl.match_criteria;
1010 if (match_criteria && *match_criteria) {
1011 matchStr = match_criteria;
1013 if (zh->m_record_id && *zh->m_record_id) {
1014 matchStr = fileMatchStr (zh, zh->reg->keys, pr_fname,
1018 yaz_log (YLOG_WARN, "Bad match criteria (recordID)");
1025 int db_ord = zebraExplain_get_database_ord(zh->reg->zei);
1026 char *rinfo = dict_lookup_ord(zh->reg->matchDict, db_ord,
1030 assert(*rinfo == sizeof(*sysno));
1031 memcpy (sysno, rinfo+1, sizeof(*sysno));
1035 if (zebra_rec_keys_empty(zh->reg->keys))
1037 /* the extraction process returned no information - the record
1038 is probably empty - unless flagShowRecords is in use */
1048 yaz_log (YLOG_LOG, "delete %s %s %ld", recordType,
1049 pr_fname, (long) recordOffset);
1050 yaz_log (YLOG_WARN, "cannot delete record above (seems new)");
1054 yaz_log (YLOG_LOG, "add %s %s %ld", recordType, pr_fname,
1055 (long) recordOffset);
1056 rec = rec_new (zh->reg->records);
1058 *sysno = rec->sysno;
1060 recordAttr = rec_init_attr (zh->reg->zei, rec);
1061 recordAttr->staticrank = extractCtrl.staticrank;
1065 int db_ord = zebraExplain_get_database_ord(zh->reg->zei);
1066 dict_insert_ord(zh->reg->matchDict, db_ord, matchStr,
1067 sizeof(*sysno), sysno);
1071 extract_flushSortKeys (zh, *sysno, 1, zh->reg->sortKeys);
1072 extract_flushRecordKeys (zh, *sysno, 1, zh->reg->keys,
1073 recordAttr->staticrank);
1074 zh->records_inserted++;
1078 /* record already exists */
1079 zebra_rec_keys_t delkeys = zebra_rec_keys_open();
1080 zebra_rec_keys_t sortKeys = zebra_rec_keys_open();
1083 yaz_log (YLOG_LOG, "skipped %s %s %ld",
1084 recordType, pr_fname, (long) recordOffset);
1089 rec = rec_get (zh->reg->records, *sysno);
1092 recordAttr = rec_init_attr (zh->reg->zei, rec);
1094 zebra_rec_keys_set_buf(delkeys,
1095 rec->info[recInfo_delKeys],
1096 rec->size[recInfo_delKeys],
1098 zebra_rec_keys_set_buf(sortKeys,
1099 rec->info[recInfo_sortKeys],
1100 rec->size[recInfo_sortKeys],
1103 extract_flushSortKeys (zh, *sysno, 0, sortKeys);
1104 extract_flushRecordKeys (zh, *sysno, 0, delkeys,
1105 recordAttr->staticrank);
1108 /* record going to be deleted */
1109 if (zebra_rec_keys_empty(delkeys))
1111 yaz_log(YLOG_LOG, "delete %s %s %ld", recordType,
1112 pr_fname, (long) recordOffset);
1113 yaz_log(YLOG_WARN, "cannot delete file above, "
1114 "storeKeys false (3)");
1119 yaz_log(YLOG_LOG, "delete %s %s %ld", recordType,
1120 pr_fname, (long) recordOffset);
1121 zh->records_deleted++;
1124 int db_ord = zebraExplain_get_database_ord(zh->reg->zei);
1125 dict_delete_ord(zh->reg->matchDict, db_ord, matchStr);
1127 rec_del (zh->reg->records, &rec);
1136 yaz_log(YLOG_LOG, "update %s %s %ld", recordType,
1137 pr_fname, (long) recordOffset);
1138 recordAttr->staticrank = extractCtrl.staticrank;
1139 extract_flushSortKeys (zh, *sysno, 1, zh->reg->sortKeys);
1140 extract_flushRecordKeys (zh, *sysno, 1, zh->reg->keys,
1141 recordAttr->staticrank);
1142 zh->records_updated++;
1144 zebra_rec_keys_close(delkeys);
1145 zebra_rec_keys_close(sortKeys);
1147 /* update file type */
1148 xfree (rec->info[recInfo_fileType]);
1149 rec->info[recInfo_fileType] =
1150 rec_strdup (recordType, &rec->size[recInfo_fileType]);
1152 /* update filename */
1153 xfree (rec->info[recInfo_filename]);
1154 rec->info[recInfo_filename] =
1155 rec_strdup (fname, &rec->size[recInfo_filename]);
1157 /* update delete keys */
1158 xfree (rec->info[recInfo_delKeys]);
1159 if (!zebra_rec_keys_empty(zh->reg->keys) && zh->m_store_keys == 1)
1161 zebra_rec_keys_get_buf(zh->reg->keys,
1162 &rec->info[recInfo_delKeys],
1163 &rec->size[recInfo_delKeys]);
1167 rec->info[recInfo_delKeys] = NULL;
1168 rec->size[recInfo_delKeys] = 0;
1170 /* update sort keys */
1171 xfree (rec->info[recInfo_sortKeys]);
1173 zebra_rec_keys_get_buf(zh->reg->sortKeys,
1174 &rec->info[recInfo_sortKeys],
1175 &rec->size[recInfo_sortKeys]);
1177 /* save file size of original record */
1178 zebraExplain_recordBytesIncrement (zh->reg->zei,
1179 - recordAttr->recordSize);
1181 recordAttr->recordSize = fi->file_moffset - recordOffset;
1182 if (!recordAttr->recordSize)
1183 recordAttr->recordSize = fi->file_max - recordOffset;
1185 recordAttr->recordSize = buf_size;
1187 zebraExplain_recordBytesIncrement (zh->reg->zei,
1188 recordAttr->recordSize);
1190 /* set run-number for this record */
1191 recordAttr->runNumber =
1192 zebraExplain_runNumberIncrement (zh->reg->zei, 0);
1194 /* update store data */
1195 xfree (rec->info[recInfo_storeData]);
1197 /* update store data */
1198 if (zh->store_data_buf)
1200 rec->size[recInfo_storeData] = zh->store_data_size;
1201 rec->info[recInfo_storeData] = zh->store_data_buf;
1202 zh->store_data_buf = 0;
1204 else if (zh->m_store_data)
1206 rec->size[recInfo_storeData] = recordAttr->recordSize;
1207 rec->info[recInfo_storeData] = (char *)
1208 xmalloc (recordAttr->recordSize);
1209 memcpy (rec->info[recInfo_storeData], buf, recordAttr->recordSize);
1213 rec->info[recInfo_storeData] = NULL;
1214 rec->size[recInfo_storeData] = 0;
1216 /* update database name */
1217 xfree (rec->info[recInfo_databaseName]);
1218 rec->info[recInfo_databaseName] =
1219 rec_strdup (zh->basenames[0], &rec->size[recInfo_databaseName]);
1222 recordAttr->recordOffset = recordOffset;
1224 /* commit this record */
1225 rec_put (zh->reg->records, &rec);
1230 ZEBRA_RES zebra_extract_explain(void *handle, Record rec, data1_node *n)
1232 ZebraHandle zh = (ZebraHandle) handle;
1233 struct recExtractCtrl extractCtrl;
1235 if (zebraExplain_curDatabase (zh->reg->zei,
1236 rec->info[recInfo_databaseName]))
1239 if (zebraExplain_newDatabase (zh->reg->zei,
1240 rec->info[recInfo_databaseName], 0))
1244 zebra_rec_keys_reset(zh->reg->keys);
1245 zebra_rec_keys_reset(zh->reg->sortKeys);
1247 extractCtrl.init = extract_init;
1248 extractCtrl.tokenAdd = extract_token_add;
1249 extractCtrl.schemaAdd = extract_schema_add;
1250 extractCtrl.dh = zh->reg->dh;
1252 init_extractCtrl(zh, &extractCtrl);
1254 extractCtrl.flagShowRecords = 0;
1255 extractCtrl.match_criteria[0] = '\0';
1256 extractCtrl.staticrank = 0;
1257 extractCtrl.handle = handle;
1258 extractCtrl.first_record = 1;
1260 extract_set_store_data_prepare(&extractCtrl);
1263 grs_extract_tree(&extractCtrl, n);
1265 if (rec->size[recInfo_delKeys])
1267 zebra_rec_keys_t delkeys = zebra_rec_keys_open();
1269 zebra_rec_keys_t sortkeys = zebra_rec_keys_open();
1271 zebra_rec_keys_set_buf(delkeys, rec->info[recInfo_delKeys],
1272 rec->size[recInfo_delKeys],
1274 extract_flushRecordKeys (zh, rec->sysno, 0, delkeys, 0);
1275 zebra_rec_keys_close(delkeys);
1277 zebra_rec_keys_set_buf(sortkeys, rec->info[recInfo_sortKeys],
1278 rec->size[recInfo_sortKeys],
1281 extract_flushSortKeys (zh, rec->sysno, 0, sortkeys);
1282 zebra_rec_keys_close(sortkeys);
1284 extract_flushRecordKeys (zh, rec->sysno, 1, zh->reg->keys, 0);
1285 extract_flushSortKeys (zh, rec->sysno, 1, zh->reg->sortKeys);
1287 xfree (rec->info[recInfo_delKeys]);
1288 zebra_rec_keys_get_buf(zh->reg->keys,
1289 &rec->info[recInfo_delKeys],
1290 &rec->size[recInfo_delKeys]);
1292 xfree (rec->info[recInfo_sortKeys]);
1293 zebra_rec_keys_get_buf(zh->reg->sortKeys,
1294 &rec->info[recInfo_sortKeys],
1295 &rec->size[recInfo_sortKeys]);
1299 void extract_rec_keys_adjust(ZebraHandle zh, int is_insert,
1300 zebra_rec_keys_t reckeys)
1302 ZebraExplainInfo zei = zh->reg->zei;
1306 struct ord_stat *next;
1309 if (zebra_rec_keys_rewind(reckeys))
1311 struct ord_stat *ord_list = 0;
1315 struct it_key key_in;
1316 while(zebra_rec_keys_read(reckeys, &str, &slen, &key_in))
1318 int ord = CAST_ZINT_TO_INT(key_in.mem[0]);
1320 for (p = ord_list; p ; p = p->next)
1328 p = xmalloc(sizeof(*p));
1339 struct ord_stat *p1 = p;
1342 zebraExplain_ord_adjust_occurrences(zei, p->ord, p->no, 1);
1344 zebraExplain_ord_adjust_occurrences(zei, p->ord, - p->no, -1);
1351 void extract_flushRecordKeys (ZebraHandle zh, SYSNO sysno,
1353 zebra_rec_keys_t reckeys,
1356 ZebraExplainInfo zei = zh->reg->zei;
1358 extract_rec_keys_adjust(zh, cmd, reckeys);
1360 if (!zh->reg->key_buf)
1362 int mem= 1024*1024* atoi( res_get_def( zh->res, "memmax", "8"));
1365 yaz_log(YLOG_WARN, "Invalid memory setting, using default 8 MB");
1368 /* FIXME: That "8" should be in a default settings include */
1369 /* not hard-coded here! -H */
1370 zh->reg->key_buf = (char**) xmalloc (mem);
1371 zh->reg->ptr_top = mem/sizeof(char*);
1373 zh->reg->key_buf_used = 0;
1374 zh->reg->key_file_no = 0;
1376 zebraExplain_recordCountIncrement (zei, cmd ? 1 : -1);
1378 if (zebra_rec_keys_rewind(reckeys))
1382 struct it_key key_in;
1383 while(zebra_rec_keys_read(reckeys, &str, &slen, &key_in))
1386 struct it_key key_out;
1387 zint *keyp = key_out.mem;
1389 assert(key_in.len == 4);
1391 /* check for buffer overflow */
1392 if (zh->reg->key_buf_used + 1024 >
1393 (zh->reg->ptr_top -zh->reg->ptr_i)*sizeof(char*))
1394 extract_flushWriteKeys (zh, 0);
1397 assert(zh->reg->ptr_i > 0);
1398 (zh->reg->key_buf)[zh->reg->ptr_top - zh->reg->ptr_i] =
1399 (char*)zh->reg->key_buf + zh->reg->key_buf_used;
1401 /* encode the ordinal value (field/use/attribute) .. */
1402 ch = CAST_ZINT_TO_INT(key_in.mem[0]);
1403 zh->reg->key_buf_used +=
1404 key_SU_encode(ch, (char*)zh->reg->key_buf +
1405 zh->reg->key_buf_used);
1407 /* copy the 0-terminated stuff from str to output */
1408 memcpy((char*)zh->reg->key_buf + zh->reg->key_buf_used, str, slen);
1409 zh->reg->key_buf_used += slen;
1410 ((char*)zh->reg->key_buf)[(zh->reg->key_buf_used)++] = '\0';
1412 /* the delete/insert indicator */
1413 ((char*)zh->reg->key_buf)[(zh->reg->key_buf_used)++] = cmd;
1415 if (zh->m_staticrank) /* rank config enabled ? */
1419 yaz_log(YLOG_WARN, "staticrank = %ld. Setting to 0",
1423 *keyp++ = staticrank;
1429 if (key_in.mem[1]) /* filter specified record ID */
1430 *keyp++ = key_in.mem[1];
1433 *keyp++ = key_in.mem[2]; /* section_id */
1434 *keyp++ = key_in.mem[3]; /* sequence .. */
1436 memcpy((char*)zh->reg->key_buf + zh->reg->key_buf_used,
1437 &key_out, sizeof(key_out));
1438 (zh->reg->key_buf_used) += sizeof(key_out);
1443 void extract_flushWriteKeys (ZebraHandle zh, int final)
1444 /* optimizing: if final=1, and no files written yet */
1445 /* push the keys directly to merge, sidestepping the */
1446 /* temp file altogether. Speeds small updates */
1449 char out_fname[200];
1451 struct encode_info encode_info;
1452 int ptr_i = zh->reg->ptr_i;
1457 if (!zh->reg->key_buf || ptr_i <= 0)
1459 yaz_log(log_level, " nothing to flush section=%d buf=%p i=%d",
1460 zh->reg->key_file_no, zh->reg->key_buf, ptr_i);
1461 yaz_log(log_level, " buf=%p ",
1463 yaz_log(log_level, " ptr=%d ",zh->reg->ptr_i);
1464 yaz_log(log_level, " reg=%p ",zh->reg);
1469 (zh->reg->key_file_no)++;
1470 yaz_log (YLOG_LOG, "sorting section %d", (zh->reg->key_file_no));
1471 yaz_log(log_level, " sort_buff at %p n=%d",
1472 zh->reg->key_buf + zh->reg->ptr_top - ptr_i,ptr_i);
1474 qsort (zh->reg->key_buf + zh->reg->ptr_top - ptr_i, ptr_i,
1475 sizeof(char*), key_qsort_compare);
1477 /* zebra.cfg: tempfiles:
1478 Y: always use temp files (old way)
1479 A: use temp files, if more than one (auto)
1480 = if this is both the last and the first
1481 N: never bother with temp files (new) */
1483 temp_policy=toupper(res_get_def(zh->res,"tempfiles","auto")[0]);
1484 if (temp_policy != 'Y' && temp_policy != 'N' && temp_policy != 'A') {
1485 yaz_log (YLOG_WARN, "Illegal tempfiles setting '%c'. using 'Auto' ",
1490 if ( ( temp_policy =='N' ) || /* always from memory */
1491 ( ( temp_policy =='A' ) && /* automatic */
1492 (zh->reg->key_file_no == 1) && /* this is first time */
1493 (final) ) ) /* and last (=only) time */
1494 { /* go directly from memory */
1495 zh->reg->key_file_no =0; /* signal not to read files */
1496 zebra_index_merge(zh);
1498 zh->reg->key_buf_used = 0;
1502 /* Not doing directly from memory, write into a temp file */
1503 extract_get_fname_tmp (zh, out_fname, zh->reg->key_file_no);
1505 if (!(outf = fopen (out_fname, "wb")))
1507 yaz_log (YLOG_FATAL|YLOG_ERRNO, "fopen %s", out_fname);
1510 yaz_log (YLOG_LOG, "writing section %d", zh->reg->key_file_no);
1511 prevcp = cp = (zh->reg->key_buf)[zh->reg->ptr_top - ptr_i];
1513 encode_key_init (&encode_info);
1514 encode_key_write (cp, &encode_info, outf);
1518 cp = (zh->reg->key_buf)[zh->reg->ptr_top - ptr_i];
1519 if (strcmp (cp, prevcp))
1521 encode_key_flush ( &encode_info, outf);
1522 encode_key_init (&encode_info);
1523 encode_key_write (cp, &encode_info, outf);
1527 encode_key_write (cp + strlen(cp), &encode_info, outf);
1529 encode_key_flush ( &encode_info, outf);
1531 qsort (key_buf + ptr_top-ptr_i, ptr_i, sizeof(char*), key_x_compare);
1532 extract_get_fname_tmp (out_fname, key_file_no);
1534 if (!(outf = fopen (out_fname, "wb")))
1536 yaz_log (YLOG_FATAL|YLOG_ERRNO, "fopen %s", out_fname);
1539 yaz_log (YLOG_LOG, "writing section %d", key_file_no);
1541 prevcp = key_buf[ptr_top-i];
1543 if (!--i || strcmp (prevcp, key_buf[ptr_top-i]))
1545 key_y_len = strlen(prevcp)+1;
1547 yaz_log (YLOG_LOG, "key_y_len: %2d %02x %02x %s",
1548 key_y_len, prevcp[0], prevcp[1], 2+prevcp);
1550 qsort (key_buf + ptr_top-ptr_i, ptr_i - i,
1551 sizeof(char*), key_y_compare);
1552 cp = key_buf[ptr_top-ptr_i];
1554 encode_key_init (&encode_info);
1555 encode_key_write (cp, &encode_info, outf);
1558 cp = key_buf[ptr_top-ptr_i];
1559 encode_key_write (cp+key_y_len, &encode_info, outf);
1561 encode_key_flush ( &encode_info, outf);
1564 prevcp = key_buf[ptr_top-ptr_i];
1569 yaz_log (YLOG_FATAL|YLOG_ERRNO, "fclose %s", out_fname);
1572 yaz_log (YLOG_LOG, "finished section %d", zh->reg->key_file_no);
1574 zh->reg->key_buf_used = 0;
1577 ZEBRA_RES zebra_snippets_rec_keys(ZebraHandle zh,
1578 zebra_rec_keys_t reckeys,
1579 zebra_snippets *snippets)
1581 NMEM nmem = nmem_create();
1582 if (zebra_rec_keys_rewind(reckeys))
1587 while (zebra_rec_keys_read(reckeys, &str, &slen, &key))
1589 char dst_buf[IT_MAX_WORD];
1590 char *dst_term = dst_buf;
1595 assert(key.len <= 4 && key.len > 2);
1596 seqno = key.mem[key.len-1];
1597 ord = CAST_ZINT_TO_INT(key.mem[0]);
1599 zebraExplain_lookup_ord(zh->reg->zei, ord, &index_type,
1600 0/* db */, 0 /* string_index */);
1602 zebra_term_untrans_iconv(zh, nmem, index_type,
1604 zebra_snippets_append(snippets, seqno, ord, dst_term);
1612 void print_rec_keys(ZebraHandle zh, zebra_rec_keys_t reckeys)
1614 yaz_log(YLOG_LOG, "print_rec_keys");
1615 if (zebra_rec_keys_rewind(reckeys))
1620 while (zebra_rec_keys_read(reckeys, &str, &slen, &key))
1622 char dst_buf[IT_MAX_WORD];
1625 int ord = CAST_ZINT_TO_INT(key.mem[0]);
1627 assert(key.len <= 4 && key.len > 2);
1629 zebraExplain_lookup_ord(zh->reg->zei, ord, &index_type, &db, 0);
1631 seqno = key.mem[key.len-1];
1633 zebra_term_untrans(zh, index_type, dst_buf, str);
1635 yaz_log(YLOG_LOG, "ord=%d seqno=" ZINT_FORMAT
1636 " term=%s", ord, seqno, dst_buf);
1641 static void extract_add_index_string(RecWord *p, const char *str, int length)
1645 ZebraHandle zh = p->extractCtrl->handle;
1646 ZebraExplainInfo zei = zh->reg->zei;
1652 ch = zebraExplain_lookup_attr_str(zei, p->index_type, p->index_name);
1654 ch = zebraExplain_add_attr_str(zei, p->index_type, p->index_name);
1658 key.mem[1] = p->record_id;
1659 key.mem[2] = p->section_id;
1660 key.mem[3] = p->seqno;
1669 for (i = 0; i<length && i < 20; i++)
1670 sprintf(strz+strlen(strz), "%02X", str[i] & 0xff);
1671 /* just for debugging .. */
1672 yaz_log(YLOG_LOG, "add: set=%d use=%d "
1673 "record_id=%lld section_id=%lld seqno=%lld %s",
1674 p->attrSet, p->attrUse, p->record_id, p->section_id, p->seqno,
1679 zebra_rec_keys_write(zh->reg->keys, str, length, &key);
1682 static void extract_add_sort_string(RecWord *p, const char *str, int length)
1686 ZebraHandle zh = p->extractCtrl->handle;
1687 ZebraExplainInfo zei = zh->reg->zei;
1693 ch = zebraExplain_lookup_attr_str(zei, p->index_type, p->index_name);
1695 ch = zebraExplain_add_attr_str(zei, p->index_type, p->index_name);
1698 key.mem[1] = p->record_id;
1699 key.mem[2] = p->section_id;
1700 key.mem[3] = p->seqno;
1709 for (i = 0; i<length && i < 20; i++)
1710 sprintf(strz+strlen(strz), "%02X", str[i] & 0xff);
1711 /* just for debugging .. */
1712 yaz_log(YLOG_LOG, "add: set=%d use=%d "
1713 "record_id=%lld section_id=%lld seqno=%lld %s",
1714 p->attrSet, p->attrUse, p->record_id, p->section_id, p->seqno,
1718 zebra_rec_keys_write(zh->reg->sortKeys, str, length, &key);
1721 static void extract_add_string (RecWord *p, const char *string, int length)
1723 assert (length > 0);
1724 if (zebra_maps_is_sort (p->zebra_maps, p->index_type))
1725 extract_add_sort_string (p, string, length);
1727 extract_add_index_string (p, string, length);
1730 static void extract_add_incomplete_field (RecWord *p)
1732 const char *b = p->term_buf;
1733 int remain = p->term_len;
1734 const char **map = 0;
1737 map = zebra_maps_input(p->zebra_maps, p->index_type, &b, remain, 0);
1741 char buf[IT_MAX_WORD+1];
1745 while (map && *map && **map == *CHR_SPACE)
1747 remain = p->term_len - (b - p->term_buf);
1749 map = zebra_maps_input(p->zebra_maps, p->index_type, &b,
1757 while (map && *map && **map != *CHR_SPACE)
1759 const char *cp = *map;
1761 while (i < IT_MAX_WORD && *cp)
1763 remain = p->term_len - (b - p->term_buf);
1765 map = zebra_maps_input(p->zebra_maps, p->index_type, &b, remain, 0);
1771 extract_add_string (p, buf, i);
1776 static void extract_add_complete_field (RecWord *p)
1778 const char *b = p->term_buf;
1779 char buf[IT_MAX_WORD+1];
1780 const char **map = 0;
1781 int i = 0, remain = p->term_len;
1784 map = zebra_maps_input (p->zebra_maps, p->index_type, &b, remain, 1);
1786 while (remain > 0 && i < IT_MAX_WORD)
1788 while (map && *map && **map == *CHR_SPACE)
1790 remain = p->term_len - (b - p->term_buf);
1794 int first = i ? 0 : 1; /* first position */
1795 map = zebra_maps_input(p->zebra_maps, p->index_type, &b, remain, first);
1803 if (i && i < IT_MAX_WORD)
1804 buf[i++] = *CHR_SPACE;
1805 while (map && *map && **map != *CHR_SPACE)
1807 const char *cp = *map;
1809 if (**map == *CHR_CUT)
1815 if (i >= IT_MAX_WORD)
1817 while (i < IT_MAX_WORD && *cp)
1820 remain = p->term_len - (b - p->term_buf);
1823 map = zebra_maps_input (p->zebra_maps, p->index_type, &b,
1832 extract_add_string (p, buf, i);
1835 static void extract_token_add(RecWord *p)
1839 yaz_log(log_level, "extract_token_add "
1840 "type=%c index=%s seqno=" ZINT_FORMAT " s=%.*s",
1841 p->index_type, p->index_name,
1842 p->seqno, p->term_len, p->term_buf);
1843 if ((wrbuf = zebra_replace(p->zebra_maps, p->index_type, 0,
1844 p->term_buf, p->term_len)))
1846 p->term_buf = wrbuf_buf(wrbuf);
1847 p->term_len = wrbuf_len(wrbuf);
1849 if (zebra_maps_is_complete (p->zebra_maps, p->index_type))
1850 extract_add_complete_field (p);
1852 extract_add_incomplete_field(p);
1855 static void extract_set_store_data_cb(struct recExtractCtrl *p,
1856 void *buf, size_t sz)
1858 ZebraHandle zh = (ZebraHandle) p->handle;
1860 xfree(zh->store_data_buf);
1861 zh->store_data_buf = 0;
1862 zh->store_data_size = 0;
1865 zh->store_data_buf = xmalloc(sz);
1866 zh->store_data_size = sz;
1867 memcpy(zh->store_data_buf, buf, sz);
1871 static void extract_set_store_data_prepare(struct recExtractCtrl *p)
1873 ZebraHandle zh = (ZebraHandle) p->handle;
1874 xfree(zh->store_data_buf);
1875 zh->store_data_buf = 0;
1876 zh->store_data_size = 0;
1877 p->setStoreData = extract_set_store_data_cb;
1880 static void extract_schema_add (struct recExtractCtrl *p, Odr_oid *oid)
1882 ZebraHandle zh = (ZebraHandle) p->handle;
1883 zebraExplain_addSchema (zh->reg->zei, oid);
1886 void extract_flushSortKeys (ZebraHandle zh, SYSNO sysno,
1887 int cmd, zebra_rec_keys_t reckeys)
1889 if (zebra_rec_keys_rewind(reckeys))
1891 SortIdx sortIdx = zh->reg->sortIdx;
1894 struct it_key key_in;
1896 sortIdx_sysno (sortIdx, sysno);
1898 while (zebra_rec_keys_read(reckeys, &str, &slen, &key_in))
1900 int ord = CAST_ZINT_TO_INT(key_in.mem[0]);
1902 sortIdx_type(sortIdx, ord);
1904 sortIdx_add(sortIdx, str, slen);
1906 sortIdx_add(sortIdx, "", 1);
1911 static void encode_key_init(struct encode_info *i)
1913 i->encode_handle = iscz1_start();
1914 i->decode_handle = iscz1_start();
1917 static void encode_key_write (char *k, struct encode_info *i, FILE *outf)
1920 char *bp = i->buf, *bp0;
1921 const char *src = (char *) &key;
1923 /* copy term to output buf */
1924 while ((*bp++ = *k++))
1926 /* and copy & align key so we can mangle */
1927 memcpy (&key, k+1, sizeof(struct it_key)); /* *k is insert/delete */
1931 key_logdump_txt(YLOG_LOG, &key, *k ? "i" : "d");
1933 assert(key.mem[0] >= 0);
1936 iscz1_encode(i->encode_handle, &bp, &src);
1938 *bp0 = (*k * 128) + bp - bp0 - 1; /* length and insert/delete combined */
1939 if (fwrite (i->buf, bp - i->buf, 1, outf) != 1)
1941 yaz_log (YLOG_FATAL|YLOG_ERRNO, "fwrite");
1950 const char *src = bp0+1;
1951 char *dst = (char*) &key2;
1952 iscz1_decode(i->decode_handle, &dst, &src);
1954 key_logdump_txt(YLOG_LOG, &key2, *k ? "i" : "d");
1956 assert(key2.mem[1]);
1961 static void encode_key_flush (struct encode_info *i, FILE *outf)
1963 iscz1_stop(i->encode_handle);
1964 iscz1_stop(i->decode_handle);
1970 * indent-tabs-mode: nil
1972 * vim: shiftwidth=4 tabstop=8 expandtab