1 /* $Id: extract.c,v 1.196 2005-10-28 07:25:30 adam Exp $
2 Copyright (C) 1995-2005
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
38 #if _FILE_OFFSET_BITS == 64
39 #define PRINTF_OFF_T "%Ld"
41 #define PRINTF_OFF_T "%ld"
44 #define USE_SHELLSORT 0
47 static void shellsort(void *ar, int r, size_t s,
48 int (*cmp)(const void *a, const void *b))
53 static const int incs[16] = { 1391376, 463792, 198768, 86961, 33936,
54 13776, 4592, 1968, 861, 336,
55 112, 48, 21, 7, 3, 1 };
56 for ( k = 0; k < 16; k++)
57 for (h = incs[k], i = h; i < r; i++)
61 while (j > h && (*cmp)(a + s*(j-h), v) > 0)
63 memcpy (a + s*j, a + s*(j-h), s);
71 static void logRecord (ZebraHandle zh)
73 ++zh->records_processed;
74 if (!(zh->records_processed % 1000))
76 yaz_log (YLOG_LOG, "Records: "ZINT_FORMAT" i/u/d "
77 ZINT_FORMAT"/"ZINT_FORMAT"/"ZINT_FORMAT,
78 zh->records_processed, zh->records_inserted, zh->records_updated,
83 static void extract_set_store_data_prepare(struct recExtractCtrl *p);
85 static void extract_init (struct recExtractCtrl *p, RecWord *w)
87 w->zebra_maps = p->zebra_maps;
91 w->attrSet = VAL_BIB1;
101 static void searchRecordKey(ZebraHandle zh,
103 zebra_rec_keys_t reckeys,
105 const struct recKeys *reckeys,
107 int attrSetS, int attrUseS,
108 const char **ws, int ws_length)
112 void *decode_handle = iscz1_start();
120 for (i = 0; i<ws_length; i++)
123 ch = zebraExplain_lookup_attr_su_any_index(zh->reg->zei,
129 if (zebra_rec_keys_rewind(reckeys))
136 while (zebra_rec_keys_read(reckeys, &str, &slen, &key))
138 assert(key.len <= 4 && key.len > 2);
140 seqno = key.mem[key.len-1];
142 if (key.mem[0] == ch)
148 woff = seqno - startSeq;
149 if (woff >= 0 && woff < ws_length)
155 while (off < reckeys->buf_used)
157 const char *src = reckeys->buf + off;
159 char *dst = (char*) &key;
161 iscz1_decode(decode_handle, &dst, &src);
162 assert(key.len <= 4 && key.len > 2);
164 seqno = (int) key.mem[key.len-1];
166 if (key.mem[0] == ch)
172 woff = seqno - startSeq;
173 if (woff >= 0 && woff < ws_length)
179 off = src - reckeys->buf;
181 iscz1_stop(decode_handle);
182 assert (off == reckeys->buf_used);
186 struct file_read_info {
187 off_t file_max; /* maximum offset so far */
188 off_t file_offset; /* current offset */
189 off_t file_moffset; /* offset of rec/rec boundary */
194 static struct file_read_info *file_read_start (int fd)
196 struct file_read_info *fi = (struct file_read_info *)
197 xmalloc (sizeof(*fi));
201 fi->file_moffset = 0;
207 static void file_read_stop (struct file_read_info *fi)
212 static off_t file_seek (void *handle, off_t offset)
214 struct file_read_info *p = (struct file_read_info *) handle;
215 p->file_offset = offset;
216 return lseek (p->fd, offset, SEEK_SET);
219 static off_t file_tell (void *handle)
221 struct file_read_info *p = (struct file_read_info *) handle;
222 return p->file_offset;
225 static int file_read (void *handle, char *buf, size_t count)
227 struct file_read_info *p = (struct file_read_info *) handle;
230 r = read (fd, buf, count);
234 if (p->file_offset > p->file_max)
235 p->file_max = p->file_offset;
240 static void file_end (void *handle, off_t offset)
242 struct file_read_info *p = (struct file_read_info *) handle;
244 if (offset != p->file_moffset)
246 p->file_moffset = offset;
251 static char *fileMatchStr (ZebraHandle zh,
253 zebra_rec_keys_t reckeys,
255 struct recKeys *reckeys,
257 const char *fname, const char *spec)
259 static char dstBuf[2048]; /* static here ??? */
261 const char *s = spec;
265 while (*s == ' ' || *s == '\t')
272 char attset_str[64], attname_str[64];
273 data1_attset *attset;
276 int attSet = 1, attUse = 1;
280 for (i = 0; *s && *s != ',' && *s != ')'; s++)
282 attset_str[i++] = *s;
283 attset_str[i] = '\0';
288 for (i = 0; *s && *s != ')'; s++)
290 attname_str[i++] = *s;
291 attname_str[i] = '\0';
294 if ((attset = data1_get_attset (zh->reg->dh, attset_str)))
297 attSet = attset->reference;
298 att = data1_getattbyname(zh->reg->dh, attset, attname_str);
302 attUse = atoi (attname_str);
304 searchRecordKey (zh, reckeys, attSet, attUse, ws, 32);
308 for (i = 0; i<32; i++)
313 yaz_log (YLOG_WARN, "Missing ) in match criteria %s in group %s",
314 spec, zh->m_group ? zh->m_group : "none");
319 for (i = 0; i<32; i++)
320 if (matchFlag[i] && ws[i])
328 dst += strlen(ws[i]);
332 yaz_log (YLOG_WARN, "Record didn't contain match"
333 " fields in (%s,%s)", attset_str, attname_str);
341 const char *spec_src = NULL;
342 const char *s1 = ++s;
343 while (*s1 && *s1 != ' ' && *s1 != '\t')
349 memcpy (special, s, spec_len);
350 special[spec_len] = '\0';
353 if (!strcmp (special, "group"))
354 spec_src = zh->m_group;
355 else if (!strcmp (special, "database"))
356 spec_src = zh->basenames[0];
357 else if (!strcmp (special, "filename")) {
360 else if (!strcmp (special, "type"))
361 spec_src = zh->m_record_type;
366 strcpy (dst, spec_src);
367 dst += strlen (spec_src);
370 else if (*s == '\"' || *s == '\'')
372 int stopMarker = *s++;
376 while (*s && *s != stopMarker)
379 tmpString[i++] = *s++;
384 strcpy (dst, tmpString);
385 dst += strlen (tmpString);
389 yaz_log (YLOG_WARN, "Syntax error in match criteria %s in group %s",
390 spec, zh->m_group ? zh->m_group : "none");
397 yaz_log (YLOG_WARN, "No match criteria for record %s in group %s",
398 fname, zh->m_group ? zh->m_group : "none");
405 struct recordLogInfo {
408 struct recordGroup *rGroup;
413 void create_rec_keys_codec(struct recKeys *keys)
416 iscz1_reset(keys->codec_handle);
420 static void init_extractCtrl(ZebraHandle zh, struct recExtractCtrl *ctrl)
423 for (i = 0; i<256; i++)
425 if (zebra_maps_is_positioned(zh->reg->zebra_maps, i))
430 ctrl->zebra_maps = zh->reg->zebra_maps;
431 ctrl->flagShowRecords = !zh->m_flag_rw;
434 static int file_extract_record(ZebraHandle zh,
435 SYSNO *sysno, const char *fname,
437 struct file_read_info *fi,
440 void *recTypeClientData)
442 RecordAttr *recordAttr;
444 const char *matchStr = 0;
447 off_t recordOffset = 0;
448 struct recExtractCtrl extractCtrl;
450 /* announce database */
451 if (zebraExplain_curDatabase (zh->reg->zei, zh->basenames[0]))
453 if (zebraExplain_newDatabase (zh->reg->zei, zh->basenames[0],
454 zh->m_explain_database))
460 /* we are going to read from a file, so prepare the extraction */
462 zebra_rec_keys_reset(zh->reg->keys);
464 create_rec_keys_codec(&zh->reg->keys);
469 zebra_rec_keys_reset(zh->reg->sortKeys);
471 create_rec_keys_codec(&zh->reg->sortKeys);
474 zh->reg->sortKeys.buf_used = 0;
476 recordOffset = fi->file_moffset;
477 extractCtrl.handle = zh;
478 extractCtrl.offset = fi->file_moffset;
479 extractCtrl.readf = file_read;
480 extractCtrl.seekf = file_seek;
481 extractCtrl.tellf = file_tell;
482 extractCtrl.endf = file_end;
484 extractCtrl.init = extract_init;
485 extractCtrl.tokenAdd = extract_token_add;
486 extractCtrl.schemaAdd = extract_schema_add;
487 extractCtrl.dh = zh->reg->dh;
488 extractCtrl.match_criteria[0] = '\0';
489 extractCtrl.staticrank = 0;
491 extractCtrl.first_record = fi->file_offset ? 0 : 1;
493 extract_set_store_data_prepare(&extractCtrl);
495 init_extractCtrl(zh, &extractCtrl);
498 printf ("File: %s " PRINTF_OFF_T "\n", fname, recordOffset);
502 sprintf (msg, "%s:" PRINTF_OFF_T , fname, recordOffset);
503 yaz_log_init_prefix2 (msg);
506 r = (*recType->extract)(recTypeClientData, &extractCtrl);
508 yaz_log_init_prefix2 (0);
509 if (r == RECCTRL_EXTRACT_EOF)
511 else if (r == RECCTRL_EXTRACT_ERROR_GENERIC)
513 /* error occured during extraction ... */
515 zh->records_processed < zh->m_file_verbose_limit)
517 yaz_log (YLOG_WARN, "fail %s %s " PRINTF_OFF_T, zh->m_record_type,
518 fname, recordOffset);
522 else if (r == RECCTRL_EXTRACT_ERROR_NO_SUCH_FILTER)
524 /* error occured during extraction ... */
526 zh->records_processed < zh->m_file_verbose_limit)
528 yaz_log (YLOG_WARN, "no filter for %s %s "
529 PRINTF_OFF_T, zh->m_record_type,
530 fname, recordOffset);
534 if (extractCtrl.match_criteria[0])
535 matchStr = extractCtrl.match_criteria;
538 /* perform match if sysno not known and if match criteria is specified */
544 if (matchStr == 0 && zh->m_record_id && *zh->m_record_id)
547 matchStr = fileMatchStr (zh, zh->reg->keys, fname,
550 matchStr = fileMatchStr (zh, &zh->reg->keys, fname,
555 yaz_log(YLOG_WARN, "Bad match criteria");
561 char *rinfo = dict_lookup (zh->reg->matchDict, matchStr);
564 assert(*rinfo == sizeof(*sysno));
565 memcpy (sysno, rinfo+1, sizeof(*sysno));
571 && zebra_rec_keys_empty(zh->reg->keys)
573 && zh->reg->keys.buf_used == 0
577 /* the extraction process returned no information - the record
578 is probably empty - unless flagShowRecords is in use */
582 if (zh->records_processed < zh->m_file_verbose_limit)
583 yaz_log (YLOG_WARN, "empty %s %s " PRINTF_OFF_T, zh->m_record_type,
584 fname, recordOffset);
593 yaz_log (YLOG_LOG, "delete %s %s " PRINTF_OFF_T, zh->m_record_type,
594 fname, recordOffset);
595 yaz_log (YLOG_WARN, "cannot delete record above (seems new)");
598 if (zh->records_processed < zh->m_file_verbose_limit)
599 yaz_log (YLOG_LOG, "add %s %s " PRINTF_OFF_T, zh->m_record_type,
600 fname, recordOffset);
601 rec = rec_new (zh->reg->records);
605 recordAttr = rec_init_attr (zh->reg->zei, rec);
606 recordAttr->staticrank = extractCtrl.staticrank;
610 dict_insert (zh->reg->matchDict, matchStr, sizeof(*sysno), sysno);
612 extract_flushSortKeys (zh, *sysno, 1, &zh->reg->sortKeys);
614 extract_flushRecordKeys (zh, *sysno, 1, zh->reg->keys,
615 recordAttr->staticrank);
617 extract_flushRecordKeys (zh, *sysno, 1, &zh->reg->keys,
618 recordAttr->staticrank);
620 zh->records_inserted++;
624 /* record already exists */
626 zebra_rec_keys_t delkeys = zebra_rec_keys_open();
629 struct recKeys delkeys;
633 zebra_rec_keys_t sortKeys = zebra_rec_keys_open();
635 struct recKeys sortKeys;
638 struct sortKeys sortKeys;
641 rec = rec_get (zh->reg->records, *sysno);
644 recordAttr = rec_init_attr (zh->reg->zei, rec);
647 zebra_rec_keys_set_buf(delkeys,
648 rec->info[recInfo_delKeys],
649 rec->size[recInfo_delKeys],
652 /* flush old keys for sort&search etc. */
653 delkeys.buf_used = rec->size[recInfo_delKeys];
654 delkeys.buf = rec->info[recInfo_delKeys];
657 #if NEW_REC_KEYS && NATTR
658 zebra_rec_keys_set_buf(sortKeys,
659 rec->info[recInfo_sortKeys],
660 rec->size[recInfo_sortKeys],
662 extract_flushSortKeys (zh, *sysno, 0, sortKeys);
664 sortKeys.buf_used = rec->size[recInfo_sortKeys];
665 sortKeys.buf = rec->info[recInfo_sortKeys];
666 extract_flushSortKeys (zh, *sysno, 0, &sortKeys);
670 extract_flushRecordKeys (zh, *sysno, 0, delkeys,
671 recordAttr->staticrank); /* old values */
673 extract_flushRecordKeys (zh, *sysno, 0, &delkeys,
674 recordAttr->staticrank); /* old values */
678 /* record going to be deleted */
681 zebra_rec_keys_empty(delkeys)
687 yaz_log (YLOG_LOG, "delete %s %s " PRINTF_OFF_T,
688 zh->m_record_type, fname, recordOffset);
689 yaz_log (YLOG_WARN, "cannot delete file above, storeKeys false");
693 if (zh->records_processed < zh->m_file_verbose_limit)
694 yaz_log (YLOG_LOG, "delete %s %s " PRINTF_OFF_T,
695 zh->m_record_type, fname, recordOffset);
696 zh->records_deleted++;
698 dict_delete (zh->reg->matchDict, matchStr);
699 rec_del (zh->reg->records, &rec);
707 /* record going to be updated */
710 zebra_rec_keys_empty(delkeys)
716 yaz_log (YLOG_LOG, "update %s %s " PRINTF_OFF_T,
717 zh->m_record_type, fname, recordOffset);
718 yaz_log (YLOG_WARN, "cannot update file above, storeKeys false");
722 /* flush new keys for sort&search etc */
723 if (zh->records_processed < zh->m_file_verbose_limit)
724 yaz_log (YLOG_LOG, "update %s %s " PRINTF_OFF_T,
725 zh->m_record_type, fname, recordOffset);
726 recordAttr->staticrank = extractCtrl.staticrank;
727 extract_flushSortKeys (zh, *sysno, 1, &zh->reg->sortKeys);
729 extract_flushRecordKeys (zh, *sysno, 1, zh->reg->keys,
730 recordAttr->staticrank);
732 extract_flushRecordKeys (zh, *sysno, 1, &zh->reg->keys,
733 recordAttr->staticrank);
735 zh->records_updated++;
739 zebra_rec_keys_close(delkeys);
741 #if NATTR && NEW_REC_KWYS
742 zebra_rec_keys_close(sortKeys);
745 /* update file type */
746 xfree (rec->info[recInfo_fileType]);
747 rec->info[recInfo_fileType] =
748 rec_strdup (zh->m_record_type, &rec->size[recInfo_fileType]);
750 /* update filename */
751 xfree (rec->info[recInfo_filename]);
752 rec->info[recInfo_filename] =
753 rec_strdup (fname, &rec->size[recInfo_filename]);
755 /* update delete keys */
756 xfree (rec->info[recInfo_delKeys]);
759 !zebra_rec_keys_empty(zh->reg->keys)
761 zh->reg->keys.buf_used > 0
763 && zh->m_store_keys == 1)
766 zebra_rec_keys_get_buf(zh->reg->keys,
767 &rec->info[recInfo_delKeys],
768 &rec->size[recInfo_delKeys]);
771 rec->size[recInfo_delKeys] = zh->reg->keys.buf_used;
772 rec->info[recInfo_delKeys] = zh->reg->keys.buf;
773 zh->reg->keys.buf = NULL;
774 zh->reg->keys.buf_max = 0;
779 rec->info[recInfo_delKeys] = NULL;
780 rec->size[recInfo_delKeys] = 0;
783 /* update sort keys */
784 xfree (rec->info[recInfo_sortKeys]);
786 rec->size[recInfo_sortKeys] = zh->reg->sortKeys.buf_used;
787 rec->info[recInfo_sortKeys] = zh->reg->sortKeys.buf;
788 zh->reg->sortKeys.buf = NULL;
789 zh->reg->sortKeys.buf_max = 0;
791 /* save file size of original record */
792 zebraExplain_recordBytesIncrement (zh->reg->zei,
793 - recordAttr->recordSize);
794 recordAttr->recordSize = fi->file_moffset - recordOffset;
795 if (!recordAttr->recordSize)
796 recordAttr->recordSize = fi->file_max - recordOffset;
797 zebraExplain_recordBytesIncrement (zh->reg->zei,
798 recordAttr->recordSize);
800 /* set run-number for this record */
801 recordAttr->runNumber = zebraExplain_runNumberIncrement (zh->reg->zei,
804 /* update store data */
805 xfree (rec->info[recInfo_storeData]);
806 if (zh->store_data_buf)
808 rec->size[recInfo_storeData] = zh->store_data_size;
809 rec->info[recInfo_storeData] = zh->store_data_buf;
810 zh->store_data_buf = 0;
812 else if (zh->m_store_data)
814 rec->size[recInfo_storeData] = recordAttr->recordSize;
815 rec->info[recInfo_storeData] = (char *)
816 xmalloc (recordAttr->recordSize);
817 if (lseek (fi->fd, recordOffset, SEEK_SET) < 0)
819 yaz_log (YLOG_ERRNO|YLOG_FATAL, "seek to " PRINTF_OFF_T " in %s",
820 recordOffset, fname);
823 if (read (fi->fd, rec->info[recInfo_storeData], recordAttr->recordSize)
824 < recordAttr->recordSize)
826 yaz_log (YLOG_ERRNO|YLOG_FATAL, "read %d bytes of %s",
827 recordAttr->recordSize, fname);
833 rec->info[recInfo_storeData] = NULL;
834 rec->size[recInfo_storeData] = 0;
836 /* update database name */
837 xfree (rec->info[recInfo_databaseName]);
838 rec->info[recInfo_databaseName] =
839 rec_strdup (zh->basenames[0], &rec->size[recInfo_databaseName]);
842 recordAttr->recordOffset = recordOffset;
844 /* commit this record */
845 rec_put (zh->reg->records, &rec);
850 int fileExtract (ZebraHandle zh, SYSNO *sysno, const char *fname,
857 struct file_read_info *fi;
858 const char *original_record_type = 0;
860 void *recTypeClientData;
862 if (!zh->m_group || !*zh->m_group)
865 sprintf (gprefix, "%s.", zh->m_group);
867 yaz_log (YLOG_DEBUG, "fileExtract %s", fname);
869 /* determine file extension */
871 for (i = strlen(fname); --i >= 0; )
874 else if (fname[i] == '.')
876 strcpy (ext, fname+i+1);
879 /* determine file type - depending on extension */
880 original_record_type = zh->m_record_type;
881 if (!zh->m_record_type)
883 sprintf (ext_res, "%srecordType.%s", gprefix, ext);
884 zh->m_record_type = res_get (zh->res, ext_res);
886 if (!zh->m_record_type)
888 if (zh->records_processed < zh->m_file_verbose_limit)
889 yaz_log (YLOG_LOG, "? %s", fname);
892 /* determine match criteria */
893 if (!zh->m_record_id)
895 sprintf (ext_res, "%srecordId.%s", gprefix, ext);
896 zh->m_record_id = res_get (zh->res, ext_res);
900 recType_byName (zh->reg->recTypes, zh->res, zh->m_record_type,
901 &recTypeClientData)))
903 yaz_log(YLOG_WARN, "No such record type: %s", zh->m_record_type);
907 switch(recType->version)
912 yaz_log(YLOG_WARN, "Bad filter version: %s", zh->m_record_type);
914 if (sysno && deleteFlag)
920 if (zh->path_reg && !yaz_is_abspath (fname))
922 strcpy (full_rep, zh->path_reg);
923 strcat (full_rep, "/");
924 strcat (full_rep, fname);
927 strcpy (full_rep, fname);
930 if ((fd = open (full_rep, O_BINARY|O_RDONLY)) == -1)
932 yaz_log (YLOG_WARN|YLOG_ERRNO, "open %s", full_rep);
933 zh->m_record_type = original_record_type;
937 fi = file_read_start (fd);
940 fi->file_moffset = fi->file_offset;
941 fi->file_more = 0; /* file_end not called (yet) */
942 r = file_extract_record (zh, sysno, fname, deleteFlag, fi, 1,
943 recType, recTypeClientData);
945 { /* file_end has been called so reset offset .. */
946 fi->file_offset = fi->file_moffset;
947 lseek(fi->fd, fi->file_moffset, SEEK_SET);
954 zh->m_record_type = original_record_type;
959 If sysno is provided, then it's used to identify the reocord.
960 If not, and match_criteria is provided, then sysno is guessed
961 If not, and a record is provided, then sysno is got from there
964 ZEBRA_RES buffer_extract_record(ZebraHandle zh,
965 const char *buf, size_t buf_size,
968 const char *recordType,
970 const char *match_criteria,
975 RecordAttr *recordAttr;
976 struct recExtractCtrl extractCtrl;
978 const char *matchStr = 0;
979 RecType recType = NULL;
982 long recordOffset = 0;
983 struct zebra_fetch_control fc;
984 const char *pr_fname = fname; /* filename to print .. */
985 int show_progress = zh->records_processed < zh->m_file_verbose_limit ? 1:0;
988 pr_fname = "<no file>"; /* make it printable if file is omitted */
991 fc.record_int_buf = buf;
992 fc.record_int_len = buf_size;
993 fc.record_int_pos = 0;
995 fc.record_offset = 0;
997 extractCtrl.offset = 0;
998 extractCtrl.readf = zebra_record_int_read;
999 extractCtrl.seekf = zebra_record_int_seek;
1000 extractCtrl.tellf = zebra_record_int_tell;
1001 extractCtrl.endf = zebra_record_int_end;
1002 extractCtrl.first_record = 1;
1003 extractCtrl.fh = &fc;
1006 zebra_rec_keys_reset(zh->reg->keys);
1008 create_rec_keys_codec(&zh->reg->keys);
1012 zebra_rec_keys_reset(zh->reg->sortKeys);
1014 create_rec_keys_codec(&zh->reg->sortKeys);
1017 zh->reg->sortKeys.buf_used = 0;
1019 if (zebraExplain_curDatabase (zh->reg->zei, zh->basenames[0]))
1021 if (zebraExplain_newDatabase (zh->reg->zei, zh->basenames[0],
1022 zh->m_explain_database))
1026 if (recordType && *recordType)
1028 yaz_log (YLOG_DEBUG, "Record type explicitly specified: %s", recordType);
1029 recType = recType_byName (zh->reg->recTypes, zh->res, recordType,
1034 if (!(zh->m_record_type))
1036 yaz_log (YLOG_WARN, "No such record type defined");
1039 yaz_log (YLOG_DEBUG, "Get record type from rgroup: %s",zh->m_record_type);
1040 recType = recType_byName (zh->reg->recTypes, zh->res,
1041 zh->m_record_type, &clientData);
1042 recordType = zh->m_record_type;
1047 yaz_log (YLOG_WARN, "No such record type: %s", zh->m_record_type);
1051 extractCtrl.init = extract_init;
1052 extractCtrl.tokenAdd = extract_token_add;
1053 extractCtrl.schemaAdd = extract_schema_add;
1054 extractCtrl.dh = zh->reg->dh;
1055 extractCtrl.handle = zh;
1056 extractCtrl.match_criteria[0] = '\0';
1057 extractCtrl.staticrank = 0;
1059 init_extractCtrl(zh, &extractCtrl);
1061 extract_set_store_data_prepare(&extractCtrl);
1063 r = (*recType->extract)(clientData, &extractCtrl);
1065 if (r == RECCTRL_EXTRACT_EOF)
1067 else if (r == RECCTRL_EXTRACT_ERROR_GENERIC)
1069 /* error occured during extraction ... */
1070 yaz_log (YLOG_WARN, "extract error: generic");
1073 else if (r == RECCTRL_EXTRACT_ERROR_NO_SUCH_FILTER)
1075 /* error occured during extraction ... */
1076 yaz_log (YLOG_WARN, "extract error: no such filter");
1079 /* match criteria */
1082 if (extractCtrl.match_criteria[0])
1083 match_criteria = extractCtrl.match_criteria;
1087 if (match_criteria && *match_criteria) {
1088 matchStr = match_criteria;
1090 if (zh->m_record_id && *zh->m_record_id) {
1092 matchStr = fileMatchStr (zh, zh->reg->keys, pr_fname,
1095 matchStr = fileMatchStr (zh, &zh->reg->keys, pr_fname,
1100 yaz_log (YLOG_WARN, "Bad match criteria (recordID)");
1106 rinfo = dict_lookup (zh->reg->matchDict, matchStr);
1109 assert(*rinfo == sizeof(*sysno));
1110 memcpy (sysno, rinfo+1, sizeof(*sysno));
1116 zebra_rec_keys_empty(zh->reg->keys)
1118 zh->reg->keys.buf_used == 0
1122 /* the extraction process returned no information - the record
1123 is probably empty - unless flagShowRecords is in use */
1134 yaz_log (YLOG_LOG, "delete %s %s %ld", recordType,
1135 pr_fname, (long) recordOffset);
1136 yaz_log (YLOG_WARN, "cannot delete record above (seems new)");
1140 yaz_log (YLOG_LOG, "add %s %s %ld", recordType, pr_fname,
1141 (long) recordOffset);
1142 rec = rec_new (zh->reg->records);
1144 *sysno = rec->sysno;
1146 recordAttr = rec_init_attr (zh->reg->zei, rec);
1147 recordAttr->staticrank = extractCtrl.staticrank;
1151 dict_insert (zh->reg->matchDict, matchStr,
1152 sizeof(*sysno), sysno);
1154 extract_flushSortKeys (zh, *sysno, 1, &zh->reg->sortKeys);
1158 print_rec_keys(zh, zh->reg->keys);
1160 extract_flushRecordKeys (zh, *sysno, 1, zh->reg->keys,
1161 recordAttr->staticrank);
1164 print_rec_keys(zh, &zh->reg->keys);
1166 extract_flushRecordKeys (zh, *sysno, 1, &zh->reg->keys,
1167 recordAttr->staticrank);
1170 zh->records_inserted++;
1174 /* record already exists */
1176 zebra_rec_keys_t delkeys = zebra_rec_keys_open();
1178 struct recKeys delkeys;
1181 struct recKeys sortKeys;
1183 struct sortKeys sortKeys;
1189 yaz_log (YLOG_LOG, "skipped %s %s %ld",
1190 recordType, pr_fname, (long) recordOffset);
1195 rec = rec_get (zh->reg->records, *sysno);
1198 recordAttr = rec_init_attr (zh->reg->zei, rec);
1201 zebra_rec_keys_set_buf(delkeys,
1202 rec->info[recInfo_delKeys],
1203 rec->size[recInfo_delKeys],
1206 delkeys.buf_used = rec->size[recInfo_delKeys];
1207 delkeys.buf = rec->info[recInfo_delKeys];
1209 sortKeys.buf_used = rec->size[recInfo_sortKeys];
1210 sortKeys.buf = rec->info[recInfo_sortKeys];
1212 #if NEW_REC_KEYS && NATTR
1213 extract_flushSortKeys (zh, *sysno, 0, sortKeys);
1215 extract_flushSortKeys (zh, *sysno, 0, &sortKeys);
1218 extract_flushRecordKeys (zh, *sysno, 0, delkeys,
1219 recordAttr->staticrank);
1221 extract_flushRecordKeys (zh, *sysno, 0, &delkeys,
1222 recordAttr->staticrank);
1226 /* record going to be deleted */
1229 zebra_rec_keys_empty(delkeys)
1237 yaz_log (YLOG_LOG, "delete %s %s %ld", recordType,
1238 pr_fname, (long) recordOffset);
1239 yaz_log (YLOG_WARN, "cannot delete file above, "
1246 yaz_log (YLOG_LOG, "delete %s %s %ld", recordType,
1247 pr_fname, (long) recordOffset);
1248 zh->records_deleted++;
1250 dict_delete (zh->reg->matchDict, matchStr);
1251 rec_del (zh->reg->records, &rec);
1259 /* record going to be updated */
1262 zebra_rec_keys_empty(delkeys)
1270 yaz_log (YLOG_LOG, "update %s %s %ld", recordType,
1271 pr_fname, (long) recordOffset);
1272 yaz_log (YLOG_WARN, "cannot update file above, storeKeys false");
1278 yaz_log (YLOG_LOG, "update %s %s %ld", recordType,
1279 pr_fname, (long) recordOffset);
1280 recordAttr->staticrank = extractCtrl.staticrank;
1281 extract_flushSortKeys (zh, *sysno, 1, &zh->reg->sortKeys);
1283 extract_flushRecordKeys (zh, *sysno, 1, zh->reg->keys,
1284 recordAttr->staticrank);
1286 extract_flushRecordKeys (zh, *sysno, 1, &zh->reg->keys,
1287 recordAttr->staticrank);
1289 zh->records_updated++;
1293 zebra_rec_keys_close(delkeys);
1295 #if NEW_REC_KEYS && NATTR
1296 zebra_rec_keys_close(sortKeys);
1299 /* update file type */
1300 xfree (rec->info[recInfo_fileType]);
1301 rec->info[recInfo_fileType] =
1302 rec_strdup (recordType, &rec->size[recInfo_fileType]);
1304 /* update filename */
1305 xfree (rec->info[recInfo_filename]);
1306 rec->info[recInfo_filename] =
1307 rec_strdup (fname, &rec->size[recInfo_filename]);
1309 /* update delete keys */
1310 xfree (rec->info[recInfo_delKeys]);
1313 !zebra_rec_keys_empty(zh->reg->keys)
1315 zh->reg->keys.buf_used > 0
1317 && zh->m_store_keys == 1)
1320 zebra_rec_keys_get_buf(zh->reg->keys,
1321 &rec->info[recInfo_delKeys],
1322 &rec->size[recInfo_delKeys]);
1325 rec->size[recInfo_delKeys] = zh->reg->keys.buf_used;
1326 rec->info[recInfo_delKeys] = zh->reg->keys.buf;
1327 zh->reg->keys.buf = NULL;
1328 zh->reg->keys.buf_max = 0;
1333 rec->info[recInfo_delKeys] = NULL;
1334 rec->size[recInfo_delKeys] = 0;
1336 /* update sort keys */
1337 xfree (rec->info[recInfo_sortKeys]);
1339 rec->size[recInfo_sortKeys] = zh->reg->sortKeys.buf_used;
1340 rec->info[recInfo_sortKeys] = zh->reg->sortKeys.buf;
1341 zh->reg->sortKeys.buf = NULL;
1342 zh->reg->sortKeys.buf_max = 0;
1344 /* save file size of original record */
1345 zebraExplain_recordBytesIncrement (zh->reg->zei,
1346 - recordAttr->recordSize);
1348 recordAttr->recordSize = fi->file_moffset - recordOffset;
1349 if (!recordAttr->recordSize)
1350 recordAttr->recordSize = fi->file_max - recordOffset;
1352 recordAttr->recordSize = buf_size;
1354 zebraExplain_recordBytesIncrement (zh->reg->zei,
1355 recordAttr->recordSize);
1357 /* set run-number for this record */
1358 recordAttr->runNumber =
1359 zebraExplain_runNumberIncrement (zh->reg->zei, 0);
1361 /* update store data */
1362 xfree (rec->info[recInfo_storeData]);
1364 /* update store data */
1365 if (zh->store_data_buf)
1367 rec->size[recInfo_storeData] = zh->store_data_size;
1368 rec->info[recInfo_storeData] = zh->store_data_buf;
1369 zh->store_data_buf = 0;
1371 else if (zh->m_store_data)
1373 rec->size[recInfo_storeData] = recordAttr->recordSize;
1374 rec->info[recInfo_storeData] = (char *)
1375 xmalloc (recordAttr->recordSize);
1376 memcpy (rec->info[recInfo_storeData], buf, recordAttr->recordSize);
1380 rec->info[recInfo_storeData] = NULL;
1381 rec->size[recInfo_storeData] = 0;
1383 /* update database name */
1384 xfree (rec->info[recInfo_databaseName]);
1385 rec->info[recInfo_databaseName] =
1386 rec_strdup (zh->basenames[0], &rec->size[recInfo_databaseName]);
1389 recordAttr->recordOffset = recordOffset;
1391 /* commit this record */
1392 rec_put (zh->reg->records, &rec);
1397 int explain_extract (void *handle, Record rec, data1_node *n)
1399 ZebraHandle zh = (ZebraHandle) handle;
1400 struct recExtractCtrl extractCtrl;
1402 if (zebraExplain_curDatabase (zh->reg->zei,
1403 rec->info[recInfo_databaseName]))
1406 if (zebraExplain_newDatabase (zh->reg->zei,
1407 rec->info[recInfo_databaseName], 0))
1412 zebra_rec_keys_reset(zh->reg->keys);
1414 create_rec_keys_codec(&zh->reg->keys);
1419 zebra_rec_keys_reset(zh->reg->sortKeys);
1421 create_rec_keys_codec(&zh->reg->sortKeys);
1424 zh->reg->sortKeys.buf_used = 0;
1426 extractCtrl.init = extract_init;
1427 extractCtrl.tokenAdd = extract_token_add;
1428 extractCtrl.schemaAdd = extract_schema_add;
1429 extractCtrl.dh = zh->reg->dh;
1431 init_extractCtrl(zh, &extractCtrl);
1433 extractCtrl.flagShowRecords = 0;
1434 extractCtrl.match_criteria[0] = '\0';
1435 extractCtrl.staticrank = 0;
1436 extractCtrl.handle = handle;
1437 extractCtrl.first_record = 1;
1439 extract_set_store_data_prepare(&extractCtrl);
1442 grs_extract_tree(&extractCtrl, n);
1444 if (rec->size[recInfo_delKeys])
1447 zebra_rec_keys_t delkeys = zebra_rec_keys_open();
1449 struct recKeys delkeys;
1454 zebra_rec_keys_t sortkeys = zzebra_rec_keys_open();
1456 struct recKeys sortkeys;
1459 struct sortKeys sortkeys;
1463 zebra_rec_keys_set_buf(delkeys, rec->info[recInfo_delKeys],
1464 rec->size[recInfo_delKeys],
1466 extract_flushRecordKeys (zh, rec->sysno, 0, delkeys, 0);
1467 zebra_rec_keys_close(delkeys);
1469 delkeys.buf_used = rec->size[recInfo_delKeys];
1470 delkeys.buf = rec->info[recInfo_delKeys];
1471 extract_flushRecordKeys (zh, rec->sysno, 0, &delkeys, 0);
1473 #if NATTR && NEW_REC_KEYS
1474 zebra_rec_keys_set_buf(sortkeys, rec->info[recInfo_sortKeys],
1475 rec->size[recInfo_sortKeys],
1478 extract_flushSortKeys (zh, rec->sysno, 0, sortkeys);
1479 zebra_rec_keys_close(sortkeys);
1481 sortkeys.buf_used = rec->size[recInfo_sortKeys];
1482 sortkeys.buf = rec->info[recInfo_sortKeys];
1483 extract_flushSortKeys (zh, rec->sysno, 0, &sortkeys);
1487 extract_flushRecordKeys (zh, rec->sysno, 1, zh->reg->keys, 0);
1489 extract_flushRecordKeys (zh, rec->sysno, 1, &zh->reg->keys, 0);
1491 #if NATTR && NEW_REC_KEYS
1492 extract_flushSortKeys (zh, rec->sysno, 1, zh->reg->sortKeys);
1494 extract_flushSortKeys (zh, rec->sysno, 1, &zh->reg->sortKeys);
1497 xfree (rec->info[recInfo_delKeys]);
1499 zebra_rec_keys_get_buf(zh->reg->keys,
1500 &rec->info[recInfo_delKeys],
1501 &rec->size[recInfo_delKeys]);
1503 rec->size[recInfo_delKeys] = zh->reg->keys.buf_used;
1504 rec->info[recInfo_delKeys] = zh->reg->keys.buf;
1505 zh->reg->keys.buf = NULL;
1506 zh->reg->keys.buf_max = 0;
1509 xfree (rec->info[recInfo_sortKeys]);
1510 rec->size[recInfo_sortKeys] = zh->reg->sortKeys.buf_used;
1511 rec->info[recInfo_sortKeys] = zh->reg->sortKeys.buf;
1512 zh->reg->sortKeys.buf = NULL;
1513 zh->reg->sortKeys.buf_max = 0;
1518 void extract_flushRecordKeys (ZebraHandle zh, SYSNO sysno,
1521 zebra_rec_keys_t reckeys,
1523 struct recKeys *reckeys,
1529 void *decode_handle = iscz1_start();
1533 ZebraExplainInfo zei = zh->reg->zei;
1535 if (!zh->reg->key_buf)
1537 int mem= 1024*1024* atoi( res_get_def( zh->res, "memmax", "8"));
1540 yaz_log(YLOG_WARN, "Invalid memory setting, using default 8 MB");
1543 /* FIXME: That "8" should be in a default settings include */
1544 /* not hard-coded here! -H */
1545 zh->reg->key_buf = (char**) xmalloc (mem);
1546 zh->reg->ptr_top = mem/sizeof(char*);
1548 zh->reg->key_buf_used = 0;
1549 zh->reg->key_file_no = 0;
1551 zebraExplain_recordCountIncrement (zei, cmd ? 1 : -1);
1554 if (zebra_rec_keys_rewind(reckeys))
1558 struct it_key key_in;
1559 while(zebra_rec_keys_read(reckeys, &str, &slen, &key_in))
1562 struct it_key key_out;
1563 zint *keyp = key_out.mem;
1565 assert(key_in.len == 4);
1567 /* check for buffer overflow */
1568 if (zh->reg->key_buf_used + 1024 >
1569 (zh->reg->ptr_top -zh->reg->ptr_i)*sizeof(char*))
1570 extract_flushWriteKeys (zh, 0);
1573 assert(zh->reg->ptr_i > 0);
1574 (zh->reg->key_buf)[zh->reg->ptr_top - zh->reg->ptr_i] =
1575 (char*)zh->reg->key_buf + zh->reg->key_buf_used;
1577 /* encode the ordinal value (field/use/attribute) .. */
1578 ch = (int) key_in.mem[0];
1579 zh->reg->key_buf_used +=
1580 key_SU_encode(ch, (char*)zh->reg->key_buf +
1581 zh->reg->key_buf_used);
1583 /* copy the 0-terminated stuff from str to output */
1584 memcpy((char*)zh->reg->key_buf + zh->reg->key_buf_used, str, slen);
1585 zh->reg->key_buf_used += slen;
1586 ((char*)zh->reg->key_buf)[(zh->reg->key_buf_used)++] = '\0';
1588 /* the delete/insert indicator */
1589 ((char*)zh->reg->key_buf)[(zh->reg->key_buf_used)++] = cmd;
1591 if (zh->m_staticrank) /* rank config enabled ? */
1593 *keyp++ = staticrank;
1599 if (key_in.mem[1]) /* filter specified record ID */
1600 *keyp++ = key_in.mem[1];
1603 *keyp++ = key_in.mem[2]; /* section_id */
1604 *keyp++ = key_in.mem[3]; /* sequence .. */
1606 memcpy((char*)zh->reg->key_buf + zh->reg->key_buf_used,
1607 &key_out, sizeof(key_out));
1608 (zh->reg->key_buf_used) += sizeof(key_out);
1612 while (off < reckeys->buf_used)
1614 const char *src = reckeys->buf + off;
1615 struct it_key key_in;
1616 struct it_key key_out;
1617 char *dst = (char*) &key_in;
1618 zint *keyp = key_out.mem;
1620 iscz1_decode(decode_handle, &dst, &src);
1621 assert(key_in.len == 4);
1623 if (zh->reg->key_buf_used + 1024 >
1624 (zh->reg->ptr_top -zh->reg->ptr_i)*sizeof(char*))
1625 extract_flushWriteKeys(zh, 0);
1627 assert(zh->reg->ptr_i > 0);
1628 (zh->reg->key_buf)[zh->reg->ptr_top - zh->reg->ptr_i] =
1629 (char*)zh->reg->key_buf + zh->reg->key_buf_used;
1631 ch = (int) key_in.mem[0]; /* ordinal for field/use/attribute */
1633 zh->reg->key_buf_used +=
1634 key_SU_encode(ch, ((char*)zh->reg->key_buf) +
1635 zh->reg->key_buf_used);
1637 ((char*)zh->reg->key_buf) [(zh->reg->key_buf_used)++] = *src++;
1639 ((char*)(zh->reg->key_buf))[(zh->reg->key_buf_used)++] = '\0';
1640 ((char*)(zh->reg->key_buf))[(zh->reg->key_buf_used)++] = cmd;
1642 if (zh->m_staticrank) /* rank config enabled ? */
1644 *keyp++ = staticrank;
1650 if (key_in.mem[1]) /* filter specified record ID */
1651 *keyp++ = key_in.mem[1];
1654 *keyp++ = key_in.mem[2]; /* section_id */
1655 *keyp++ = key_in.mem[3]; /* sequence .. */
1657 memcpy ((char*)zh->reg->key_buf + zh->reg->key_buf_used,
1658 &key_out, sizeof(key_out));
1659 (zh->reg->key_buf_used) += sizeof(key_out);
1660 off = src - reckeys->buf;
1662 assert (off == reckeys->buf_used);
1663 iscz1_stop(decode_handle);
1667 void extract_flushWriteKeys (ZebraHandle zh, int final)
1668 /* optimizing: if final=1, and no files written yet */
1669 /* push the keys directly to merge, sidestepping the */
1670 /* temp file altogether. Speeds small updates */
1673 char out_fname[200];
1675 struct encode_info encode_info;
1676 int ptr_i = zh->reg->ptr_i;
1681 if (!zh->reg->key_buf || ptr_i <= 0)
1683 yaz_log (YLOG_DEBUG, " nothing to flush section=%d buf=%p i=%d",
1684 zh->reg->key_file_no, zh->reg->key_buf, ptr_i);
1685 yaz_log (YLOG_DEBUG, " buf=%p ",
1687 yaz_log (YLOG_DEBUG, " ptr=%d ",zh->reg->ptr_i);
1688 yaz_log (YLOG_DEBUG, " reg=%p ",zh->reg);
1693 (zh->reg->key_file_no)++;
1694 yaz_log (YLOG_LOG, "sorting section %d", (zh->reg->key_file_no));
1695 yaz_log (YLOG_DEBUG, " sort_buff at %p n=%d",
1696 zh->reg->key_buf + zh->reg->ptr_top - ptr_i,ptr_i);
1698 qsort (zh->reg->key_buf + zh->reg->ptr_top - ptr_i, ptr_i,
1699 sizeof(char*), key_qsort_compare);
1701 /* zebra.cfg: tempfiles:
1702 Y: always use temp files (old way)
1703 A: use temp files, if more than one (auto)
1704 = if this is both the last and the first
1705 N: never bother with temp files (new) */
1707 temp_policy=toupper(res_get_def(zh->res,"tempfiles","auto")[0]);
1708 if (temp_policy != 'Y' && temp_policy != 'N' && temp_policy != 'A') {
1709 yaz_log (YLOG_WARN, "Illegal tempfiles setting '%c'. using 'Auto' ",
1714 if ( ( temp_policy =='N' ) || /* always from memory */
1715 ( ( temp_policy =='A' ) && /* automatic */
1716 (zh->reg->key_file_no == 1) && /* this is first time */
1717 (final) ) ) /* and last (=only) time */
1718 { /* go directly from memory */
1719 zh->reg->key_file_no =0; /* signal not to read files */
1720 zebra_index_merge(zh);
1722 zh->reg->key_buf_used = 0;
1726 /* Not doing directly from memory, write into a temp file */
1727 extract_get_fname_tmp (zh, out_fname, zh->reg->key_file_no);
1729 if (!(outf = fopen (out_fname, "wb")))
1731 yaz_log (YLOG_FATAL|YLOG_ERRNO, "fopen %s", out_fname);
1734 yaz_log (YLOG_LOG, "writing section %d", zh->reg->key_file_no);
1735 prevcp = cp = (zh->reg->key_buf)[zh->reg->ptr_top - ptr_i];
1737 encode_key_init (&encode_info);
1738 encode_key_write (cp, &encode_info, outf);
1742 cp = (zh->reg->key_buf)[zh->reg->ptr_top - ptr_i];
1743 if (strcmp (cp, prevcp))
1745 encode_key_flush ( &encode_info, outf);
1746 encode_key_init (&encode_info);
1747 encode_key_write (cp, &encode_info, outf);
1751 encode_key_write (cp + strlen(cp), &encode_info, outf);
1753 encode_key_flush ( &encode_info, outf);
1755 qsort (key_buf + ptr_top-ptr_i, ptr_i, sizeof(char*), key_x_compare);
1756 extract_get_fname_tmp (out_fname, key_file_no);
1758 if (!(outf = fopen (out_fname, "wb")))
1760 yaz_log (YLOG_FATAL|YLOG_ERRNO, "fopen %s", out_fname);
1763 yaz_log (YLOG_LOG, "writing section %d", key_file_no);
1765 prevcp = key_buf[ptr_top-i];
1767 if (!--i || strcmp (prevcp, key_buf[ptr_top-i]))
1769 key_y_len = strlen(prevcp)+1;
1771 yaz_log (YLOG_LOG, "key_y_len: %2d %02x %02x %s",
1772 key_y_len, prevcp[0], prevcp[1], 2+prevcp);
1774 qsort (key_buf + ptr_top-ptr_i, ptr_i - i,
1775 sizeof(char*), key_y_compare);
1776 cp = key_buf[ptr_top-ptr_i];
1778 encode_key_init (&encode_info);
1779 encode_key_write (cp, &encode_info, outf);
1782 cp = key_buf[ptr_top-ptr_i];
1783 encode_key_write (cp+key_y_len, &encode_info, outf);
1785 encode_key_flush ( &encode_info, outf);
1788 prevcp = key_buf[ptr_top-ptr_i];
1793 yaz_log (YLOG_FATAL|YLOG_ERRNO, "fclose %s", out_fname);
1796 yaz_log (YLOG_LOG, "finished section %d", zh->reg->key_file_no);
1798 zh->reg->key_buf_used = 0;
1802 void extract_add_it_key (ZebraHandle zh,
1803 zebra_rec_keys_t *keys,
1805 const char *str, int slen, struct it_key *key)
1807 zebra_rec_keys_write(*keys, reg_type, str, slen, key);
1810 void extract_add_it_key (ZebraHandle zh,
1811 struct recKeys *keys,
1813 const char *str, int slen, struct it_key *key)
1816 const char *src = (char*) key;
1818 if (keys->buf_used+1024 > keys->buf_max)
1820 char *b = (char *) xmalloc (keys->buf_max += 128000);
1821 if (keys->buf_used > 0)
1822 memcpy (b, keys->buf, keys->buf_used);
1826 dst = keys->buf + keys->buf_used;
1828 iscz1_encode(keys->codec_handle, &dst, &src);
1833 memcpy (dst, str, slen);
1836 keys->buf_used = dst - keys->buf;
1840 ZEBRA_RES zebra_snippets_rec_keys(ZebraHandle zh,
1842 zebra_rec_keys_t reckeys,
1844 struct recKeys *reckeys,
1846 zebra_snippets *snippets)
1848 NMEM nmem = nmem_create();
1851 if (zebra_rec_keys_rewind(reckeys))
1856 while (zebra_rec_keys_read(reckeys, &str, &slen, &key))
1858 char dst_buf[IT_MAX_WORD];
1859 char *dst_term = dst_buf;
1862 assert(key.len <= 4 && key.len > 2);
1863 seqno = (int) key.mem[key.len-1];
1866 zebraExplain_lookup_ord(zh->reg->zei, ord, &index_type,
1867 0/* db */, 0/* set */, 0/* use */);
1869 zebra_term_untrans_iconv(zh, nmem, index_type,
1871 zebra_snippets_append(snippets, seqno, ord, dst_term);
1878 void *decode_handle = iscz1_start();
1879 assert(reckeys->buf);
1880 while (off < reckeys->buf_used)
1882 const char *src = reckeys->buf + off;
1884 char *dst = (char*) &key;
1885 char dst_buf[IT_MAX_WORD];
1886 char *dst_term = dst_buf;
1887 int index_type = 0, ord;
1889 iscz1_decode(decode_handle, &dst, &src);
1890 assert(key.len <= 4 && key.len > 2);
1892 seqno = (int) key.mem[key.len-1];
1895 zebraExplain_lookup_ord(zh->reg->zei, ord, &index_type,
1896 0/* db */, 0/* set */, 0/* use */);
1898 zebra_term_untrans_iconv(zh, nmem, index_type,
1900 zebra_snippets_append(snippets, seqno, ord, dst_term);
1903 off = src - reckeys->buf;
1906 iscz1_stop(decode_handle);
1912 void print_rec_keys(ZebraHandle zh,
1914 zebra_rec_keys_t reckeys
1916 struct recKeys *reckeys
1921 yaz_log(YLOG_LOG, "print_rec_keys");
1922 if (zebra_rec_keys_rewind(reckeys))
1927 while (zebra_rec_keys_read(reckeys, &str, &slen, &key))
1929 char dst_buf[IT_MAX_WORD];
1933 assert(key.len <= 4 && key.len > 2);
1935 zebraExplain_lookup_ord(zh->reg->zei,
1936 key.mem[0], &index_type, &db, 0, 0);
1938 seqno = (int) key.mem[key.len-1];
1940 zebra_term_untrans(zh, index_type, dst_buf, str);
1942 yaz_log(YLOG_LOG, "ord=" ZINT_FORMAT " seqno=%d term=%s",
1943 key.mem[0], seqno, dst_buf);
1949 void *decode_handle = iscz1_start();
1950 yaz_log(YLOG_LOG, "print_rec_keys buf=%p sz=%d", reckeys->buf,
1952 assert(reckeys->buf);
1953 while (off < reckeys->buf_used)
1955 const char *src = reckeys->buf + off;
1957 char *dst = (char*) &key;
1958 char dst_buf[IT_MAX_WORD];
1962 iscz1_decode(decode_handle, &dst, &src);
1963 assert(key.len <= 4 && key.len > 2);
1965 seqno = (int) key.mem[key.len-1];
1967 zebraExplain_lookup_ord(zh->reg->zei,
1968 key.mem[0], &index_type, &db, 0, 0);
1970 zebra_term_untrans(zh, index_type, dst_buf, src);
1972 yaz_log(YLOG_LOG, "ord=" ZINT_FORMAT " seqno=%d term=%s",
1973 key.mem[0], seqno, dst_buf);
1976 off = src - reckeys->buf;
1978 iscz1_stop(decode_handle);
1982 void extract_add_index_string (RecWord *p, const char *str, int length)
1986 ZebraHandle zh = p->extractCtrl->handle;
1987 ZebraExplainInfo zei = zh->reg->zei;
1992 ch = zebraExplain_lookup_attr_str(zei, p->index_type, p->index_name);
1994 ch = zebraExplain_add_attr_str(zei, p->index_type, p->index_name);
2001 ch = zebraExplain_lookup_attr_su(zei, p->index_type,
2002 p->attrSet, p->attrUse);
2004 ch = zebraExplain_add_attr_su(zei, p->index_type,
2005 p->attrSet, p->attrUse);
2010 key.mem[1] = p->record_id;
2011 key.mem[2] = p->section_id;
2012 key.mem[3] = p->seqno;
2015 /* just for debugging .. */
2016 yaz_log(YLOG_LOG, "add: set=%d use=%d "
2017 "record_id=%lld section_id=%lld seqno=%lld",
2018 p->attrSet, p->attrUse, p->record_id, p->section_id, p->seqno);
2021 extract_add_it_key(p->extractCtrl->handle,
2028 static void extract_add_sort_string (RecWord *p, const char *str, int length)
2032 ZebraHandle zh = p->extractCtrl->handle;
2033 ZebraExplainInfo zei = zh->reg->zei;
2038 ch = zebraExplain_lookup_attr_str(zei, p->index_type, p->index_name);
2040 ch = zebraExplain_add_attr_str(zei, p->index_type, p->index_name);
2048 key.mem[1] = p->record_id;
2049 key.mem[2] = p->section_id;
2050 key.mem[3] = p->seqno;
2052 extract_add_it_key(p->extractCtrl->handle,
2058 static void extract_add_sort_string (RecWord *p, const char *str, int length)
2060 ZebraHandle zh = p->extractCtrl->handle;
2061 struct sortKeys *sk = &zh->reg->sortKeys;
2064 while (off < sk->buf_used)
2068 off += key_SU_decode(&set, (unsigned char *) sk->buf + off);
2069 off += key_SU_decode(&use, (unsigned char *) sk->buf + off);
2070 off += key_SU_decode(&slen, (unsigned char *) sk->buf + off);
2072 if (p->attrSet == set && p->attrUse == use)
2075 assert (off == sk->buf_used);
2077 if (sk->buf_used + IT_MAX_WORD > sk->buf_max)
2081 b = (char *) xmalloc (sk->buf_max += 128000);
2082 if (sk->buf_used > 0)
2083 memcpy (b, sk->buf, sk->buf_used);
2087 off += key_SU_encode(p->attrSet, sk->buf + off);
2088 off += key_SU_encode(p->attrUse, sk->buf + off);
2089 off += key_SU_encode(length, sk->buf + off);
2090 memcpy (sk->buf + off, str, length);
2091 sk->buf_used = off + length;
2095 void extract_add_string (RecWord *p, const char *string, int length)
2097 assert (length > 0);
2098 if (zebra_maps_is_sort (p->zebra_maps, p->index_type))
2099 extract_add_sort_string (p, string, length);
2101 extract_add_index_string (p, string, length);
2104 static void extract_add_incomplete_field (RecWord *p)
2106 const char *b = p->term_buf;
2107 int remain = p->term_len;
2108 const char **map = 0;
2110 yaz_log(YLOG_DEBUG, "Incomplete field, w='%.*s'", p->term_len, p->term_buf);
2113 map = zebra_maps_input(p->zebra_maps, p->index_type, &b, remain, 0);
2117 char buf[IT_MAX_WORD+1];
2121 while (map && *map && **map == *CHR_SPACE)
2123 remain = p->term_len - (b - p->term_buf);
2125 map = zebra_maps_input(p->zebra_maps, p->index_type, &b,
2133 while (map && *map && **map != *CHR_SPACE)
2135 const char *cp = *map;
2137 while (i < IT_MAX_WORD && *cp)
2139 remain = p->term_len - (b - p->term_buf);
2141 map = zebra_maps_input(p->zebra_maps, p->index_type, &b, remain, 0);
2147 extract_add_string (p, buf, i);
2152 static void extract_add_complete_field (RecWord *p)
2154 const char *b = p->term_buf;
2155 char buf[IT_MAX_WORD+1];
2156 const char **map = 0;
2157 int i = 0, remain = p->term_len;
2159 yaz_log(YLOG_DEBUG, "Complete field, w='%.*s'",
2160 p->term_len, p->term_buf);
2163 map = zebra_maps_input (p->zebra_maps, p->index_type, &b, remain, 1);
2165 while (remain > 0 && i < IT_MAX_WORD)
2167 while (map && *map && **map == *CHR_SPACE)
2169 remain = p->term_len - (b - p->term_buf);
2173 int first = i ? 0 : 1; /* first position */
2174 map = zebra_maps_input(p->zebra_maps, p->index_type, &b, remain, first);
2182 if (i && i < IT_MAX_WORD)
2183 buf[i++] = *CHR_SPACE;
2184 while (map && *map && **map != *CHR_SPACE)
2186 const char *cp = *map;
2188 if (**map == *CHR_CUT)
2194 if (i >= IT_MAX_WORD)
2196 yaz_log(YLOG_DEBUG, "Adding string to index '%d'", **map);
2197 while (i < IT_MAX_WORD && *cp)
2200 remain = p->term_len - (b - p->term_buf);
2203 map = zebra_maps_input (p->zebra_maps, p->index_type, &b,
2212 extract_add_string (p, buf, i);
2215 void extract_token_add (RecWord *p)
2219 yaz_log (YLOG_LOG, "token_add "
2220 "reg_type=%c attrSet=%d attrUse=%d seqno=%d s=%.*s",
2221 p->reg_type, p->attrSet, p->attrUse, p->seqno, p->length,
2224 if ((wrbuf = zebra_replace(p->zebra_maps, p->index_type, 0,
2225 p->term_buf, p->term_len)))
2227 p->term_buf = wrbuf_buf(wrbuf);
2228 p->term_len = wrbuf_len(wrbuf);
2230 if (zebra_maps_is_complete (p->zebra_maps, p->index_type))
2231 extract_add_complete_field (p);
2233 extract_add_incomplete_field(p);
2236 static void extract_set_store_data_cb(struct recExtractCtrl *p,
2237 void *buf, size_t sz)
2239 ZebraHandle zh = (ZebraHandle) p->handle;
2241 xfree(zh->store_data_buf);
2242 zh->store_data_buf = 0;
2243 zh->store_data_size = 0;
2246 zh->store_data_buf = xmalloc(sz);
2247 zh->store_data_size = sz;
2248 memcpy(zh->store_data_buf, buf, sz);
2252 static void extract_set_store_data_prepare(struct recExtractCtrl *p)
2254 ZebraHandle zh = (ZebraHandle) p->handle;
2255 xfree(zh->store_data_buf);
2256 zh->store_data_buf = 0;
2257 zh->store_data_size = 0;
2258 p->setStoreData = extract_set_store_data_cb;
2261 void extract_schema_add (struct recExtractCtrl *p, Odr_oid *oid)
2263 ZebraHandle zh = (ZebraHandle) p->handle;
2264 zebraExplain_addSchema (zh->reg->zei, oid);
2268 void extract_flushSortKeys (ZebraHandle zh, SYSNO sysno,
2269 int cmd, struct recKeys *reckeys)
2271 SortIdx sortIdx = zh->reg->sortIdx;
2272 void *decode_handle = iscz1_start();
2276 while (off < reckeys->buf_used)
2278 const char *src = reckeys->buf + off;
2280 char *dst = (char*) &key;
2282 iscz1_decode(decode_handle, &dst, &src);
2283 assert(key.len == 4);
2285 ch = (int) key.mem[0]; /* ordinal for field/use/attribute */
2287 sortIdx_type(sortIdx, ch);
2289 sortIdx_add(sortIdx, src, strlen(src));
2291 sortIdx_add(sortIdx, "", 1);
2296 off = src - reckeys->buf;
2298 assert (off == reckeys->buf_used);
2299 iscz1_stop(decode_handle);
2302 void extract_flushSortKeys (ZebraHandle zh, SYSNO sysno,
2303 int cmd, struct sortKeys *sk)
2305 SortIdx sortIdx = zh->reg->sortIdx;
2308 sortIdx_sysno (sortIdx, sysno);
2310 while (off < sk->buf_used)
2314 off += key_SU_decode(&set, (unsigned char *) sk->buf + off);
2315 off += key_SU_decode(&use, (unsigned char *) sk->buf + off);
2316 off += key_SU_decode(&slen, (unsigned char *) sk->buf + off);
2318 sortIdx_type(sortIdx, use);
2320 sortIdx_add(sortIdx, sk->buf + off, slen);
2322 sortIdx_add(sortIdx, "", 1);
2328 void encode_key_init (struct encode_info *i)
2337 i->encode_handle = iscz1_start();
2343 /* this is the old encode_key_write
2344 * may be deleted once we are confident that the new works
2347 void encode_key_write (char *k, struct encode_info *i, FILE *outf)
2350 char *bp = i->buf, *bp0;
2351 const char *src = (char *) &key;
2353 /* copy term to output buf */
2354 while ((*bp++ = *k++))
2356 /* and copy & align key so we can mangle */
2357 memcpy (&key, k+1, sizeof(struct it_key)); /* *k is insert/delete */
2360 iscz1_encode(i->encode_handle, &bp, &src);
2361 *bp0 = (*k * 128) + bp - bp0 - 1; /* length and insert/delete combined */
2362 if (fwrite (i->buf, bp - i->buf, 1, outf) != 1)
2364 yaz_log (YLOG_FATAL|YLOG_ERRNO, "fwrite");
2369 void encode_key_flush (struct encode_info *i, FILE *outf)
2370 { /* dummy routine */
2371 iscz1_stop(i->encode_handle);
2376 /* new encode_key_write
2377 * The idea is to buffer one more key, and compare them
2378 * If we are going to delete and insert the same key,
2379 * we may as well not bother. Should make a difference in
2380 * updates with small modifications (appending to a mbox)
2382 void encode_key_write (char *k, struct encode_info *i, FILE *outf)
2387 if (*k) /* first time for new key */
2390 while ((*bp++ = *k++))
2392 i->keylen= bp - i->buf -1;
2393 assert(i->keylen+1+sizeof(struct it_key) < ENCODE_BUFLEN);
2397 bp=i->buf + i->keylen;
2402 memcpy (&key, k+1, sizeof(struct it_key));
2403 if (0==i->prevsys) /* no previous filter, fill up */
2405 i->prevsys=key.sysno;
2406 i->prevseq=key.seqno;
2409 else if ( (i->prevsys==key.sysno) &&
2410 (i->prevseq==key.seqno) &&
2412 { /* same numbers, diff cmd, they cancel out */
2416 { /* different stuff, write previous, move buf */
2417 bp = encode_key_int ( (i->prevsys - i->sysno) * 2 + i->prevcmd, bp);
2418 if (i->sysno != i->prevsys)
2420 i->sysno = i->prevsys;
2423 else if (!i->seqno && !i->prevseq && i->cmd == i->prevcmd)
2425 return; /* ??? Filters some sort of duplicates away */
2426 /* ??? Can this ever happen -H 15oct02 */
2428 bp = encode_key_int (i->prevseq - i->seqno, bp);
2429 i->seqno = i->prevseq;
2430 i->cmd = i->prevcmd;
2431 if (fwrite (i->buf, bp - i->buf, 1, outf) != 1)
2433 yaz_log (YLOG_FATAL|YLOG_ERRNO, "fwrite");
2436 i->keylen=0; /* ok, it's written, forget it */
2437 i->prevsys=key.sysno;
2438 i->prevseq=key.seqno;
2443 void encode_key_flush (struct encode_info *i, FILE *outf)
2444 { /* flush the last key from i */
2445 char *bp =i->buf + i->keylen;
2448 return; /* nothing to flush */
2451 bp = encode_key_int ( (i->prevsys - i->sysno) * 2 + i->prevcmd, bp);
2452 if (i->sysno != i->prevsys)
2454 i->sysno = i->prevsys;
2457 else if (!i->seqno && !i->prevseq && i->cmd == i->prevcmd)
2459 return; /* ??? Filters some sort of duplicates away */
2460 /* ??? Can this ever happen -H 15oct02 */
2462 bp = encode_key_int (i->prevseq - i->seqno, bp);
2463 i->seqno = i->prevseq;
2464 i->cmd = i->prevcmd;
2465 if (fwrite (i->buf, bp - i->buf, 1, outf) != 1)
2467 yaz_log (YLOG_FATAL|YLOG_ERRNO, "fwrite");
2470 i->keylen=0; /* ok, it's written, forget it */
2471 i->prevsys=0; /* forget the values too */