1 /* $Id: extract.c,v 1.213 2006-05-17 17:46:45 adam Exp $
2 Copyright (C) 1995-2006
5 This file is part of the Zebra server.
7 Zebra is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with Zebra; see the file LICENSE.zebra. If not, write to the
19 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
40 #define PRINTF_OFF_T "%I64d"
43 #if SIZEOF_OFF_T == SIZEOF_LONG_LONG
44 #define PRINTF_OFF_T "%lld"
46 #define PRINTF_OFF_T "%ld"
52 #define USE_SHELLSORT 0
55 static void shellsort(void *ar, int r, size_t s,
56 int (*cmp)(const void *a, const void *b))
61 static const int incs[16] = { 1391376, 463792, 198768, 86961, 33936,
62 13776, 4592, 1968, 861, 336,
63 112, 48, 21, 7, 3, 1 };
64 for ( k = 0; k < 16; k++)
65 for (h = incs[k], i = h; i < r; i++)
69 while (j > h && (*cmp)(a + s*(j-h), v) > 0)
71 memcpy (a + s*j, a + s*(j-h), s);
79 static void logRecord (ZebraHandle zh)
81 ++zh->records_processed;
82 if (!(zh->records_processed % 1000))
84 yaz_log (YLOG_LOG, "Records: "ZINT_FORMAT" i/u/d "
85 ZINT_FORMAT"/"ZINT_FORMAT"/"ZINT_FORMAT,
86 zh->records_processed, zh->records_inserted, zh->records_updated,
91 static void extract_add_index_string (RecWord *p, const char *str, int length);
93 static void extract_set_store_data_prepare(struct recExtractCtrl *p);
95 static void extract_init (struct recExtractCtrl *p, RecWord *w)
97 w->zebra_maps = p->zebra_maps;
101 w->attrSet = VAL_BIB1;
111 static void searchRecordKey(ZebraHandle zh,
112 zebra_rec_keys_t reckeys,
113 int attrSetS, int attrUseS,
114 const char **ws, int ws_length)
119 for (i = 0; i<ws_length; i++)
122 ch = zebraExplain_lookup_attr_su_any_index(zh->reg->zei,
127 if (zebra_rec_keys_rewind(reckeys))
134 while (zebra_rec_keys_read(reckeys, &str, &slen, &key))
136 assert(key.len <= 4 && key.len > 2);
138 seqno = key.mem[key.len-1];
140 if (key.mem[0] == ch)
146 woff = seqno - startSeq;
147 if (woff >= 0 && woff < ws_length)
154 struct file_read_info {
155 off_t file_max; /* maximum offset so far */
156 off_t file_offset; /* current offset */
157 off_t file_moffset; /* offset of rec/rec boundary */
162 static struct file_read_info *file_read_start (int fd)
164 struct file_read_info *fi = (struct file_read_info *)
165 xmalloc (sizeof(*fi));
169 fi->file_moffset = 0;
175 static void file_read_stop (struct file_read_info *fi)
180 static off_t file_seek (void *handle, off_t offset)
182 struct file_read_info *p = (struct file_read_info *) handle;
183 p->file_offset = offset;
184 return lseek (p->fd, offset, SEEK_SET);
187 static off_t file_tell (void *handle)
189 struct file_read_info *p = (struct file_read_info *) handle;
190 return p->file_offset;
193 static int file_read (void *handle, char *buf, size_t count)
195 struct file_read_info *p = (struct file_read_info *) handle;
198 r = read (fd, buf, count);
202 if (p->file_offset > p->file_max)
203 p->file_max = p->file_offset;
208 static void file_end (void *handle, off_t offset)
210 struct file_read_info *p = (struct file_read_info *) handle;
212 if (offset != p->file_moffset)
214 p->file_moffset = offset;
219 #define FILE_MATCH_BLANK "\t "
221 static char *fileMatchStr (ZebraHandle zh,
222 zebra_rec_keys_t reckeys,
223 const char *fname, const char *spec)
225 static char dstBuf[2048]; /* static here ??? */
227 const char *s = spec;
231 for (; *s && strchr(FILE_MATCH_BLANK, *s); s++)
238 char attset_str[64], attname_str[64];
239 data1_attset *attset;
241 int attSet = 1, attUse = 1;
244 for (s++; strchr(FILE_MATCH_BLANK, *s); s++)
246 for (i = 0; *s && *s != ',' && *s != ')' &&
247 !strchr(FILE_MATCH_BLANK, *s); s++)
248 if (i+1 < sizeof(attset_str))
249 attset_str[i++] = *s;
250 attset_str[i] = '\0';
252 for (; strchr(FILE_MATCH_BLANK, *s); s++)
256 for (s++; strchr(FILE_MATCH_BLANK, *s); s++)
258 for (i = 0; *s && *s != ')' &&
259 !strchr(FILE_MATCH_BLANK, *s); s++)
260 if (i+1 < sizeof(attname_str))
261 attname_str[i++] = *s;
262 attname_str[i] = '\0';
265 if ((attset = data1_get_attset (zh->reg->dh, attset_str)))
268 attSet = attset->reference;
269 att = data1_getattbyname(zh->reg->dh, attset, attname_str);
273 attUse = atoi (attname_str);
275 searchRecordKey (zh, reckeys, attSet, attUse, ws, 32);
279 yaz_log (YLOG_WARN, "Missing ) in match criteria %s in group %s",
280 spec, zh->m_group ? zh->m_group : "none");
285 for (i = 0; i<32; i++)
294 dst += strlen(ws[i]);
298 yaz_log (YLOG_WARN, "Record didn't contain match"
299 " fields in (%s,%s)", attset_str, attname_str);
307 const char *spec_src = NULL;
308 const char *s1 = ++s;
309 while (*s1 && !strchr(FILE_MATCH_BLANK, *s1))
313 if (spec_len > sizeof(special)-1)
314 spec_len = sizeof(special)-1;
315 memcpy (special, s, spec_len);
316 special[spec_len] = '\0';
319 if (!strcmp (special, "group"))
320 spec_src = zh->m_group;
321 else if (!strcmp (special, "database"))
322 spec_src = zh->basenames[0];
323 else if (!strcmp (special, "filename")) {
326 else if (!strcmp (special, "type"))
327 spec_src = zh->m_record_type;
332 strcpy (dst, spec_src);
333 dst += strlen (spec_src);
336 else if (*s == '\"' || *s == '\'')
338 int stopMarker = *s++;
342 while (*s && *s != stopMarker)
344 if (i+1 < sizeof(tmpString))
345 tmpString[i++] = *s++;
350 strcpy (dst, tmpString);
351 dst += strlen (tmpString);
355 yaz_log (YLOG_WARN, "Syntax error in match criteria %s in group %s",
356 spec, zh->m_group ? zh->m_group : "none");
363 yaz_log (YLOG_WARN, "No match criteria for record %s in group %s",
364 fname, zh->m_group ? zh->m_group : "none");
371 struct recordLogInfo {
374 struct recordGroup *rGroup;
377 static void init_extractCtrl(ZebraHandle zh, struct recExtractCtrl *ctrl)
380 for (i = 0; i<256; i++)
382 if (zebra_maps_is_positioned(zh->reg->zebra_maps, i))
387 ctrl->zebra_maps = zh->reg->zebra_maps;
388 ctrl->flagShowRecords = !zh->m_flag_rw;
391 static void all_matches_add(struct recExtractCtrl *ctrl)
394 extract_init(ctrl, &word);
395 word.index_name = "allrecords";
396 word.index_type = 'w';
398 extract_add_index_string (&word, "", 0);
401 static ZEBRA_RES file_extract_record(ZebraHandle zh,
402 SYSNO *sysno, const char *fname,
404 struct file_read_info *fi,
407 void *recTypeClientData)
409 const char *match_str_to_print = "";
410 RecordAttr *recordAttr;
412 const char *matchStr = 0;
415 off_t recordOffset = 0;
416 struct recExtractCtrl extractCtrl;
418 /* announce database */
419 if (zebraExplain_curDatabase (zh->reg->zei, zh->basenames[0]))
421 if (zebraExplain_newDatabase (zh->reg->zei, zh->basenames[0],
422 zh->m_explain_database))
428 /* we are going to read from a file, so prepare the extraction */
429 zebra_rec_keys_reset(zh->reg->keys);
432 zebra_rec_keys_reset(zh->reg->sortKeys);
434 zh->reg->sortKeys.buf_used = 0;
436 recordOffset = fi->file_moffset;
437 extractCtrl.handle = zh;
438 extractCtrl.offset = fi->file_moffset;
439 extractCtrl.readf = file_read;
440 extractCtrl.seekf = file_seek;
441 extractCtrl.tellf = file_tell;
442 extractCtrl.endf = file_end;
444 extractCtrl.init = extract_init;
445 extractCtrl.tokenAdd = extract_token_add;
446 extractCtrl.schemaAdd = extract_schema_add;
447 extractCtrl.dh = zh->reg->dh;
448 extractCtrl.match_criteria[0] = '\0';
449 extractCtrl.staticrank = 0;
451 extractCtrl.first_record = fi->file_offset ? 0 : 1;
453 extract_set_store_data_prepare(&extractCtrl);
455 init_extractCtrl(zh, &extractCtrl);
458 printf ("File: %s " PRINTF_OFF_T "\n", fname, recordOffset);
462 sprintf (msg, "%s:" PRINTF_OFF_T , fname, recordOffset);
463 yaz_log_init_prefix2 (msg);
466 r = (*recType->extract)(recTypeClientData, &extractCtrl);
468 yaz_log_init_prefix2 (0);
469 if (r == RECCTRL_EXTRACT_EOF)
471 else if (r == RECCTRL_EXTRACT_ERROR_GENERIC)
473 /* error occured during extraction ... */
475 zh->records_processed < zh->m_file_verbose_limit)
477 yaz_log (YLOG_WARN, "fail %s %s " PRINTF_OFF_T, zh->m_record_type,
478 fname, recordOffset);
482 else if (r == RECCTRL_EXTRACT_ERROR_NO_SUCH_FILTER)
484 /* error occured during extraction ... */
486 zh->records_processed < zh->m_file_verbose_limit)
488 yaz_log (YLOG_WARN, "no filter for %s %s "
489 PRINTF_OFF_T, zh->m_record_type,
490 fname, recordOffset);
494 all_matches_add(&extractCtrl);
495 if (extractCtrl.match_criteria[0])
496 matchStr = extractCtrl.match_criteria;
499 /* if matchStr is set now - we assume it's printable .
500 For internal matchStr (see below) we don't print */
502 match_str_to_print = matchStr;
504 /* perform internal match if sysno not known and if match criteria is
511 if (matchStr == 0 && zh->m_record_id && *zh->m_record_id)
513 matchStr = fileMatchStr (zh, zh->reg->keys, fname,
517 yaz_log(YLOG_WARN, "Bad match criteria");
523 int db_ord = zebraExplain_get_database_ord(zh->reg->zei);
524 char *rinfo = dict_lookup_ord(zh->reg->matchDict, db_ord,
528 assert(*rinfo == sizeof(*sysno));
529 memcpy (sysno, rinfo+1, sizeof(*sysno));
533 if (! *sysno && zebra_rec_keys_empty(zh->reg->keys) )
535 /* the extraction process returned no information - the record
536 is probably empty - unless flagShowRecords is in use */
540 if (zh->records_processed < zh->m_file_verbose_limit)
541 yaz_log (YLOG_WARN, "empty %s %s " PRINTF_OFF_T, zh->m_record_type,
542 fname, recordOffset);
551 yaz_log (YLOG_LOG, "delete %s %s " PRINTF_OFF_T, zh->m_record_type,
552 fname, recordOffset);
553 yaz_log (YLOG_WARN, "cannot delete record above (seems new)");
557 rec = rec_new (zh->reg->records);
561 if (zh->records_processed < zh->m_file_verbose_limit)
563 yaz_log(YLOG_LOG, "add %s %s " PRINTF_OFF_T
564 " " ZINT_FORMAT " %s" ,
566 fname, recordOffset, *sysno, match_str_to_print);
568 recordAttr = rec_init_attr (zh->reg->zei, rec);
569 recordAttr->staticrank = extractCtrl.staticrank;
573 int db_ord = zebraExplain_get_database_ord(zh->reg->zei);
574 dict_insert_ord(zh->reg->matchDict, db_ord, matchStr,
575 sizeof(*sysno), sysno);
580 extract_flushSortKeys (zh, *sysno, 1, zh->reg->sortKeys);
582 extract_flushSortKeys (zh, *sysno, 1, &zh->reg->sortKeys);
584 extract_flushRecordKeys (zh, *sysno, 1, zh->reg->keys,
585 recordAttr->staticrank);
586 zh->records_inserted++;
590 /* record already exists */
591 zebra_rec_keys_t delkeys = zebra_rec_keys_open();
594 zebra_rec_keys_t sortKeys = zebra_rec_keys_open();
596 struct sortKeys sortKeys;
599 rec = rec_get (zh->reg->records, *sysno);
602 recordAttr = rec_init_attr (zh->reg->zei, rec);
604 zebra_rec_keys_set_buf(delkeys,
605 rec->info[recInfo_delKeys],
606 rec->size[recInfo_delKeys],
610 zebra_rec_keys_set_buf(sortKeys,
611 rec->info[recInfo_sortKeys],
612 rec->size[recInfo_sortKeys],
614 extract_flushSortKeys (zh, *sysno, 0, sortKeys);
616 sortKeys.buf_used = rec->size[recInfo_sortKeys];
617 sortKeys.buf = rec->info[recInfo_sortKeys];
618 extract_flushSortKeys (zh, *sysno, 0, &sortKeys);
621 extract_flushRecordKeys (zh, *sysno, 0, delkeys,
622 recordAttr->staticrank); /* old values */
625 /* record going to be deleted */
626 if (zebra_rec_keys_empty(delkeys))
628 yaz_log (YLOG_LOG, "delete %s %s " PRINTF_OFF_T
630 zh->m_record_type, fname, recordOffset, *sysno);
631 yaz_log (YLOG_WARN, "cannot delete file above, storeKeys false (1)");
635 if (zh->records_processed < zh->m_file_verbose_limit)
637 yaz_log(YLOG_LOG, "delete %s %s " PRINTF_OFF_T
638 " " ZINT_FORMAT " %s" ,
640 fname, recordOffset, *sysno, match_str_to_print);
642 zh->records_deleted++;
645 int db_ord = zebraExplain_get_database_ord(zh->reg->zei);
646 dict_delete_ord(zh->reg->matchDict, db_ord, matchStr);
648 rec_del (zh->reg->records, &rec);
656 /* flush new keys for sort&search etc */
657 if (zh->records_processed < zh->m_file_verbose_limit)
659 yaz_log(YLOG_LOG, "update %s %s " PRINTF_OFF_T
660 " " ZINT_FORMAT " %s" ,
662 fname, recordOffset, *sysno, match_str_to_print);
664 recordAttr->staticrank = extractCtrl.staticrank;
666 extract_flushSortKeys (zh, *sysno, 1, zh->reg->sortKeys);
668 extract_flushSortKeys (zh, *sysno, 1, &zh->reg->sortKeys);
670 extract_flushRecordKeys (zh, *sysno, 1, zh->reg->keys,
671 recordAttr->staticrank);
672 zh->records_updated++;
674 zebra_rec_keys_close(delkeys);
676 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]);
707 zebra_rec_keys_get_buf(zh->reg->sortKeys,
708 &rec->info[recInfo_sortKeys],
709 &rec->size[recInfo_sortKeys]);
711 rec->size[recInfo_sortKeys] = zh->reg->sortKeys.buf_used;
712 rec->info[recInfo_sortKeys] = zh->reg->sortKeys.buf;
713 zh->reg->sortKeys.buf = NULL;
714 zh->reg->sortKeys.buf_max = 0;
717 /* save file size of original record */
718 zebraExplain_recordBytesIncrement (zh->reg->zei,
719 - recordAttr->recordSize);
720 recordAttr->recordSize = fi->file_moffset - recordOffset;
721 if (!recordAttr->recordSize)
722 recordAttr->recordSize = fi->file_max - recordOffset;
723 zebraExplain_recordBytesIncrement (zh->reg->zei,
724 recordAttr->recordSize);
726 /* set run-number for this record */
727 recordAttr->runNumber = zebraExplain_runNumberIncrement (zh->reg->zei,
730 /* update store data */
731 xfree (rec->info[recInfo_storeData]);
732 if (zh->store_data_buf)
734 rec->size[recInfo_storeData] = zh->store_data_size;
735 rec->info[recInfo_storeData] = zh->store_data_buf;
736 zh->store_data_buf = 0;
738 else if (zh->m_store_data)
740 rec->size[recInfo_storeData] = recordAttr->recordSize;
741 rec->info[recInfo_storeData] = (char *)
742 xmalloc (recordAttr->recordSize);
743 if (lseek (fi->fd, recordOffset, SEEK_SET) < 0)
745 yaz_log (YLOG_ERRNO|YLOG_FATAL, "seek to " PRINTF_OFF_T " in %s",
746 recordOffset, fname);
749 if (read (fi->fd, rec->info[recInfo_storeData], recordAttr->recordSize)
750 < recordAttr->recordSize)
752 yaz_log (YLOG_ERRNO|YLOG_FATAL, "read %d bytes of %s",
753 recordAttr->recordSize, fname);
759 rec->info[recInfo_storeData] = NULL;
760 rec->size[recInfo_storeData] = 0;
762 /* update database name */
763 xfree (rec->info[recInfo_databaseName]);
764 rec->info[recInfo_databaseName] =
765 rec_strdup (zh->basenames[0], &rec->size[recInfo_databaseName]);
768 recordAttr->recordOffset = recordOffset;
770 /* commit this record */
771 rec_put (zh->reg->records, &rec);
776 ZEBRA_RES zebra_extract_file(ZebraHandle zh, SYSNO *sysno, const char *fname,
779 ZEBRA_RES r = ZEBRA_OK;
784 struct file_read_info *fi;
785 const char *original_record_type = 0;
787 void *recTypeClientData;
789 if (!zh->m_group || !*zh->m_group)
792 sprintf (gprefix, "%s.", zh->m_group);
794 yaz_log (YLOG_DEBUG, "fileExtract %s", fname);
796 /* determine file extension */
798 for (i = strlen(fname); --i >= 0; )
801 else if (fname[i] == '.')
803 strcpy (ext, fname+i+1);
806 /* determine file type - depending on extension */
807 original_record_type = zh->m_record_type;
808 if (!zh->m_record_type)
810 sprintf (ext_res, "%srecordType.%s", gprefix, ext);
811 zh->m_record_type = res_get (zh->res, ext_res);
813 if (!zh->m_record_type)
815 if (zh->records_processed < zh->m_file_verbose_limit)
816 yaz_log (YLOG_LOG, "? %s", fname);
819 /* determine match criteria */
820 if (!zh->m_record_id)
822 sprintf (ext_res, "%srecordId.%s", gprefix, ext);
823 zh->m_record_id = res_get (zh->res, ext_res);
827 recType_byName (zh->reg->recTypes, zh->res, zh->m_record_type,
828 &recTypeClientData)))
830 yaz_log(YLOG_WARN, "No such record type: %s", zh->m_record_type);
834 switch(recType->version)
839 yaz_log(YLOG_WARN, "Bad filter version: %s", zh->m_record_type);
841 if (sysno && deleteFlag)
847 if (zh->path_reg && !yaz_is_abspath (fname))
849 strcpy (full_rep, zh->path_reg);
850 strcat (full_rep, "/");
851 strcat (full_rep, fname);
854 strcpy (full_rep, fname);
856 if ((fd = open (full_rep, O_BINARY|O_RDONLY)) == -1)
858 yaz_log (YLOG_WARN|YLOG_ERRNO, "open %s", full_rep);
859 zh->m_record_type = original_record_type;
863 fi = file_read_start (fd);
866 fi->file_moffset = fi->file_offset;
867 fi->file_more = 0; /* file_end not called (yet) */
868 r = file_extract_record (zh, sysno, fname, deleteFlag, fi, 1,
869 recType, recTypeClientData);
871 { /* file_end has been called so reset offset .. */
872 fi->file_offset = fi->file_moffset;
873 lseek(fi->fd, fi->file_moffset, SEEK_SET);
887 zh->m_record_type = original_record_type;
892 If sysno is provided, then it's used to identify the reocord.
893 If not, and match_criteria is provided, then sysno is guessed
894 If not, and a record is provided, then sysno is got from there
897 ZEBRA_RES buffer_extract_record(ZebraHandle zh,
898 const char *buf, size_t buf_size,
901 const char *recordType,
903 const char *match_criteria,
909 RecordAttr *recordAttr;
910 struct recExtractCtrl extractCtrl;
912 const char *matchStr = 0;
913 RecType recType = NULL;
916 long recordOffset = 0;
917 struct zebra_fetch_control fc;
918 const char *pr_fname = fname; /* filename to print .. */
919 int show_progress = zh->records_processed < zh->m_file_verbose_limit ? 1:0;
922 pr_fname = "<no file>"; /* make it printable if file is omitted */
925 fc.record_int_buf = buf;
926 fc.record_int_len = buf_size;
927 fc.record_int_pos = 0;
929 fc.record_offset = 0;
931 extractCtrl.offset = 0;
932 extractCtrl.readf = zebra_record_int_read;
933 extractCtrl.seekf = zebra_record_int_seek;
934 extractCtrl.tellf = zebra_record_int_tell;
935 extractCtrl.endf = zebra_record_int_end;
936 extractCtrl.first_record = 1;
937 extractCtrl.fh = &fc;
939 zebra_rec_keys_reset(zh->reg->keys);
942 zebra_rec_keys_reset(zh->reg->sortKeys);
944 zh->reg->sortKeys.buf_used = 0;
946 if (zebraExplain_curDatabase (zh->reg->zei, zh->basenames[0]))
948 if (zebraExplain_newDatabase (zh->reg->zei, zh->basenames[0],
949 zh->m_explain_database))
953 if (recordType && *recordType)
955 yaz_log (YLOG_DEBUG, "Record type explicitly specified: %s", recordType);
956 recType = recType_byName (zh->reg->recTypes, zh->res, recordType,
961 if (!(zh->m_record_type))
963 yaz_log (YLOG_WARN, "No such record type defined");
966 yaz_log (YLOG_DEBUG, "Get record type from rgroup: %s",zh->m_record_type);
967 recType = recType_byName (zh->reg->recTypes, zh->res,
968 zh->m_record_type, &clientData);
969 recordType = zh->m_record_type;
974 yaz_log (YLOG_WARN, "No such record type: %s", recordType);
978 extractCtrl.init = extract_init;
979 extractCtrl.tokenAdd = extract_token_add;
980 extractCtrl.schemaAdd = extract_schema_add;
981 extractCtrl.dh = zh->reg->dh;
982 extractCtrl.handle = zh;
983 extractCtrl.match_criteria[0] = '\0';
984 extractCtrl.staticrank = 0;
986 init_extractCtrl(zh, &extractCtrl);
988 extract_set_store_data_prepare(&extractCtrl);
990 r = (*recType->extract)(clientData, &extractCtrl);
992 if (r == RECCTRL_EXTRACT_EOF)
994 else if (r == RECCTRL_EXTRACT_ERROR_GENERIC)
996 /* error occured during extraction ... */
997 yaz_log (YLOG_WARN, "extract error: generic");
1000 else if (r == RECCTRL_EXTRACT_ERROR_NO_SUCH_FILTER)
1002 /* error occured during extraction ... */
1003 yaz_log (YLOG_WARN, "extract error: no such filter");
1007 all_matches_add(&extractCtrl);
1009 if (extractCtrl.match_criteria[0])
1010 match_criteria = extractCtrl.match_criteria;
1016 if (match_criteria && *match_criteria) {
1017 matchStr = match_criteria;
1019 if (zh->m_record_id && *zh->m_record_id) {
1020 matchStr = fileMatchStr (zh, zh->reg->keys, pr_fname,
1024 yaz_log (YLOG_WARN, "Bad match criteria (recordID)");
1031 int db_ord = zebraExplain_get_database_ord(zh->reg->zei);
1032 char *rinfo = dict_lookup_ord(zh->reg->matchDict, db_ord,
1036 assert(*rinfo == sizeof(*sysno));
1037 memcpy (sysno, rinfo+1, sizeof(*sysno));
1041 if (zebra_rec_keys_empty(zh->reg->keys))
1043 /* the extraction process returned no information - the record
1044 is probably empty - unless flagShowRecords is in use */
1054 yaz_log (YLOG_LOG, "delete %s %s %ld", recordType,
1055 pr_fname, (long) recordOffset);
1056 yaz_log (YLOG_WARN, "cannot delete record above (seems new)");
1060 yaz_log (YLOG_LOG, "add %s %s %ld", recordType, pr_fname,
1061 (long) recordOffset);
1062 rec = rec_new (zh->reg->records);
1064 *sysno = rec->sysno;
1066 recordAttr = rec_init_attr (zh->reg->zei, rec);
1067 recordAttr->staticrank = extractCtrl.staticrank;
1071 int db_ord = zebraExplain_get_database_ord(zh->reg->zei);
1072 dict_insert_ord(zh->reg->matchDict, db_ord, matchStr,
1073 sizeof(*sysno), sysno);
1078 extract_flushSortKeys (zh, *sysno, 1, zh->reg->sortKeys);
1080 extract_flushSortKeys (zh, *sysno, 1, &zh->reg->sortKeys);
1084 print_rec_keys(zh, zh->reg->keys);
1086 extract_flushRecordKeys (zh, *sysno, 1, zh->reg->keys,
1087 recordAttr->staticrank);
1088 zh->records_inserted++;
1092 /* record already exists */
1093 zebra_rec_keys_t delkeys = zebra_rec_keys_open();
1095 zebra_rec_keys_t sortKeys = zebra_rec_keys_open();
1097 struct sortKeys sortKeys;
1102 yaz_log (YLOG_LOG, "skipped %s %s %ld",
1103 recordType, pr_fname, (long) recordOffset);
1108 rec = rec_get (zh->reg->records, *sysno);
1111 recordAttr = rec_init_attr (zh->reg->zei, rec);
1113 zebra_rec_keys_set_buf(delkeys,
1114 rec->info[recInfo_delKeys],
1115 rec->size[recInfo_delKeys],
1118 zebra_rec_keys_set_buf(sortKeys,
1119 rec->info[recInfo_sortKeys],
1120 rec->size[recInfo_sortKeys],
1123 sortKeys.buf_used = rec->size[recInfo_sortKeys];
1124 sortKeys.buf = rec->info[recInfo_sortKeys];
1128 extract_flushSortKeys (zh, *sysno, 0, sortKeys);
1130 extract_flushSortKeys (zh, *sysno, 0, &sortKeys);
1132 extract_flushRecordKeys (zh, *sysno, 0, delkeys,
1133 recordAttr->staticrank);
1136 /* record going to be deleted */
1137 if (zebra_rec_keys_empty(delkeys))
1139 yaz_log (YLOG_LOG, "delete %s %s %ld", recordType,
1140 pr_fname, (long) recordOffset);
1141 yaz_log (YLOG_WARN, "cannot delete file above, "
1142 "storeKeys false (3)");
1147 yaz_log (YLOG_LOG, "delete %s %s %ld", recordType,
1148 pr_fname, (long) recordOffset);
1149 zh->records_deleted++;
1152 int db_ord = zebraExplain_get_database_ord(zh->reg->zei);
1153 dict_delete_ord(zh->reg->matchDict, db_ord, matchStr);
1155 rec_del (zh->reg->records, &rec);
1164 yaz_log (YLOG_LOG, "update %s %s %ld", recordType,
1165 pr_fname, (long) recordOffset);
1166 recordAttr->staticrank = extractCtrl.staticrank;
1168 extract_flushSortKeys (zh, *sysno, 1, zh->reg->sortKeys);
1170 extract_flushSortKeys (zh, *sysno, 1, &zh->reg->sortKeys);
1172 extract_flushRecordKeys (zh, *sysno, 1, zh->reg->keys,
1173 recordAttr->staticrank);
1174 zh->records_updated++;
1176 zebra_rec_keys_close(delkeys);
1178 zebra_rec_keys_close(sortKeys);
1181 /* update file type */
1182 xfree (rec->info[recInfo_fileType]);
1183 rec->info[recInfo_fileType] =
1184 rec_strdup (recordType, &rec->size[recInfo_fileType]);
1186 /* update filename */
1187 xfree (rec->info[recInfo_filename]);
1188 rec->info[recInfo_filename] =
1189 rec_strdup (fname, &rec->size[recInfo_filename]);
1191 /* update delete keys */
1192 xfree (rec->info[recInfo_delKeys]);
1193 if (!zebra_rec_keys_empty(zh->reg->keys) && zh->m_store_keys == 1)
1195 zebra_rec_keys_get_buf(zh->reg->keys,
1196 &rec->info[recInfo_delKeys],
1197 &rec->size[recInfo_delKeys]);
1201 rec->info[recInfo_delKeys] = NULL;
1202 rec->size[recInfo_delKeys] = 0;
1204 /* update sort keys */
1205 xfree (rec->info[recInfo_sortKeys]);
1208 zebra_rec_keys_get_buf(zh->reg->sortKeys,
1209 &rec->info[recInfo_sortKeys],
1210 &rec->size[recInfo_sortKeys]);
1212 rec->size[recInfo_sortKeys] = zh->reg->sortKeys.buf_used;
1213 rec->info[recInfo_sortKeys] = zh->reg->sortKeys.buf;
1214 zh->reg->sortKeys.buf = NULL;
1215 zh->reg->sortKeys.buf_max = 0;
1218 /* save file size of original record */
1219 zebraExplain_recordBytesIncrement (zh->reg->zei,
1220 - recordAttr->recordSize);
1222 recordAttr->recordSize = fi->file_moffset - recordOffset;
1223 if (!recordAttr->recordSize)
1224 recordAttr->recordSize = fi->file_max - recordOffset;
1226 recordAttr->recordSize = buf_size;
1228 zebraExplain_recordBytesIncrement (zh->reg->zei,
1229 recordAttr->recordSize);
1231 /* set run-number for this record */
1232 recordAttr->runNumber =
1233 zebraExplain_runNumberIncrement (zh->reg->zei, 0);
1235 /* update store data */
1236 xfree (rec->info[recInfo_storeData]);
1238 /* update store data */
1239 if (zh->store_data_buf)
1241 rec->size[recInfo_storeData] = zh->store_data_size;
1242 rec->info[recInfo_storeData] = zh->store_data_buf;
1243 zh->store_data_buf = 0;
1245 else if (zh->m_store_data)
1247 rec->size[recInfo_storeData] = recordAttr->recordSize;
1248 rec->info[recInfo_storeData] = (char *)
1249 xmalloc (recordAttr->recordSize);
1250 memcpy (rec->info[recInfo_storeData], buf, recordAttr->recordSize);
1254 rec->info[recInfo_storeData] = NULL;
1255 rec->size[recInfo_storeData] = 0;
1257 /* update database name */
1258 xfree (rec->info[recInfo_databaseName]);
1259 rec->info[recInfo_databaseName] =
1260 rec_strdup (zh->basenames[0], &rec->size[recInfo_databaseName]);
1263 recordAttr->recordOffset = recordOffset;
1265 /* commit this record */
1266 rec_put (zh->reg->records, &rec);
1271 int explain_extract (void *handle, Record rec, data1_node *n)
1273 ZebraHandle zh = (ZebraHandle) handle;
1274 struct recExtractCtrl extractCtrl;
1276 if (zebraExplain_curDatabase (zh->reg->zei,
1277 rec->info[recInfo_databaseName]))
1280 if (zebraExplain_newDatabase (zh->reg->zei,
1281 rec->info[recInfo_databaseName], 0))
1285 zebra_rec_keys_reset(zh->reg->keys);
1288 zebra_rec_keys_reset(zh->reg->sortKeys);
1290 zh->reg->sortKeys.buf_used = 0;
1292 extractCtrl.init = extract_init;
1293 extractCtrl.tokenAdd = extract_token_add;
1294 extractCtrl.schemaAdd = extract_schema_add;
1295 extractCtrl.dh = zh->reg->dh;
1297 init_extractCtrl(zh, &extractCtrl);
1299 extractCtrl.flagShowRecords = 0;
1300 extractCtrl.match_criteria[0] = '\0';
1301 extractCtrl.staticrank = 0;
1302 extractCtrl.handle = handle;
1303 extractCtrl.first_record = 1;
1305 extract_set_store_data_prepare(&extractCtrl);
1308 grs_extract_tree(&extractCtrl, n);
1310 if (rec->size[recInfo_delKeys])
1312 zebra_rec_keys_t delkeys = zebra_rec_keys_open();
1315 zebra_rec_keys_t sortkeys = zebra_rec_keys_open();
1317 struct sortKeys sortkeys;
1320 zebra_rec_keys_set_buf(delkeys, rec->info[recInfo_delKeys],
1321 rec->size[recInfo_delKeys],
1323 extract_flushRecordKeys (zh, rec->sysno, 0, delkeys, 0);
1324 zebra_rec_keys_close(delkeys);
1326 zebra_rec_keys_set_buf(sortkeys, rec->info[recInfo_sortKeys],
1327 rec->size[recInfo_sortKeys],
1330 extract_flushSortKeys (zh, rec->sysno, 0, sortkeys);
1331 zebra_rec_keys_close(sortkeys);
1333 sortkeys.buf_used = rec->size[recInfo_sortKeys];
1334 sortkeys.buf = rec->info[recInfo_sortKeys];
1335 extract_flushSortKeys (zh, rec->sysno, 0, &sortkeys);
1338 extract_flushRecordKeys (zh, rec->sysno, 1, zh->reg->keys, 0);
1340 extract_flushSortKeys (zh, rec->sysno, 1, zh->reg->sortKeys);
1342 extract_flushSortKeys (zh, rec->sysno, 1, &zh->reg->sortKeys);
1345 xfree (rec->info[recInfo_delKeys]);
1346 zebra_rec_keys_get_buf(zh->reg->keys,
1347 &rec->info[recInfo_delKeys],
1348 &rec->size[recInfo_delKeys]);
1350 xfree (rec->info[recInfo_sortKeys]);
1352 zebra_rec_keys_get_buf(zh->reg->sortKeys,
1353 &rec->info[recInfo_sortKeys],
1354 &rec->size[recInfo_sortKeys]);
1356 rec->size[recInfo_sortKeys] = zh->reg->sortKeys.buf_used;
1357 rec->info[recInfo_sortKeys] = zh->reg->sortKeys.buf;
1358 zh->reg->sortKeys.buf = NULL;
1359 zh->reg->sortKeys.buf_max = 0;
1365 void extract_rec_keys_adjust(ZebraHandle zh, int is_insert,
1366 zebra_rec_keys_t reckeys)
1368 ZebraExplainInfo zei = zh->reg->zei;
1372 struct ord_stat *next;
1375 if (zebra_rec_keys_rewind(reckeys))
1377 struct ord_stat *ord_list = 0;
1381 struct it_key key_in;
1382 while(zebra_rec_keys_read(reckeys, &str, &slen, &key_in))
1384 int ord = key_in.mem[0];
1386 for (p = ord_list; p ; p = p->next)
1394 p = xmalloc(sizeof(*p));
1405 struct ord_stat *p1 = p;
1408 zebraExplain_ord_adjust_occurrences(zei, p->ord, p->no, 1);
1410 zebraExplain_ord_adjust_occurrences(zei, p->ord, - p->no, -1);
1417 void extract_flushRecordKeys (ZebraHandle zh, SYSNO sysno,
1419 zebra_rec_keys_t reckeys,
1422 ZebraExplainInfo zei = zh->reg->zei;
1424 extract_rec_keys_adjust(zh, cmd, reckeys);
1426 if (!zh->reg->key_buf)
1428 int mem= 1024*1024* atoi( res_get_def( zh->res, "memmax", "8"));
1431 yaz_log(YLOG_WARN, "Invalid memory setting, using default 8 MB");
1434 /* FIXME: That "8" should be in a default settings include */
1435 /* not hard-coded here! -H */
1436 zh->reg->key_buf = (char**) xmalloc (mem);
1437 zh->reg->ptr_top = mem/sizeof(char*);
1439 zh->reg->key_buf_used = 0;
1440 zh->reg->key_file_no = 0;
1442 zebraExplain_recordCountIncrement (zei, cmd ? 1 : -1);
1444 if (zebra_rec_keys_rewind(reckeys))
1448 struct it_key key_in;
1449 while(zebra_rec_keys_read(reckeys, &str, &slen, &key_in))
1452 struct it_key key_out;
1453 zint *keyp = key_out.mem;
1455 assert(key_in.len == 4);
1457 /* check for buffer overflow */
1458 if (zh->reg->key_buf_used + 1024 >
1459 (zh->reg->ptr_top -zh->reg->ptr_i)*sizeof(char*))
1460 extract_flushWriteKeys (zh, 0);
1463 assert(zh->reg->ptr_i > 0);
1464 (zh->reg->key_buf)[zh->reg->ptr_top - zh->reg->ptr_i] =
1465 (char*)zh->reg->key_buf + zh->reg->key_buf_used;
1467 /* encode the ordinal value (field/use/attribute) .. */
1468 ch = (int) key_in.mem[0];
1469 zh->reg->key_buf_used +=
1470 key_SU_encode(ch, (char*)zh->reg->key_buf +
1471 zh->reg->key_buf_used);
1473 /* copy the 0-terminated stuff from str to output */
1474 memcpy((char*)zh->reg->key_buf + zh->reg->key_buf_used, str, slen);
1475 zh->reg->key_buf_used += slen;
1476 ((char*)zh->reg->key_buf)[(zh->reg->key_buf_used)++] = '\0';
1478 /* the delete/insert indicator */
1479 ((char*)zh->reg->key_buf)[(zh->reg->key_buf_used)++] = cmd;
1481 if (zh->m_staticrank) /* rank config enabled ? */
1485 yaz_log(YLOG_WARN, "staticrank = %ld. Setting to 0",
1489 *keyp++ = staticrank;
1495 if (key_in.mem[1]) /* filter specified record ID */
1496 *keyp++ = key_in.mem[1];
1499 *keyp++ = key_in.mem[2]; /* section_id */
1500 *keyp++ = key_in.mem[3]; /* sequence .. */
1502 memcpy((char*)zh->reg->key_buf + zh->reg->key_buf_used,
1503 &key_out, sizeof(key_out));
1504 (zh->reg->key_buf_used) += sizeof(key_out);
1509 void extract_flushWriteKeys (ZebraHandle zh, int final)
1510 /* optimizing: if final=1, and no files written yet */
1511 /* push the keys directly to merge, sidestepping the */
1512 /* temp file altogether. Speeds small updates */
1515 char out_fname[200];
1517 struct encode_info encode_info;
1518 int ptr_i = zh->reg->ptr_i;
1523 if (!zh->reg->key_buf || ptr_i <= 0)
1525 yaz_log (YLOG_DEBUG, " nothing to flush section=%d buf=%p i=%d",
1526 zh->reg->key_file_no, zh->reg->key_buf, ptr_i);
1527 yaz_log (YLOG_DEBUG, " buf=%p ",
1529 yaz_log (YLOG_DEBUG, " ptr=%d ",zh->reg->ptr_i);
1530 yaz_log (YLOG_DEBUG, " reg=%p ",zh->reg);
1535 (zh->reg->key_file_no)++;
1536 yaz_log (YLOG_LOG, "sorting section %d", (zh->reg->key_file_no));
1537 yaz_log (YLOG_DEBUG, " sort_buff at %p n=%d",
1538 zh->reg->key_buf + zh->reg->ptr_top - ptr_i,ptr_i);
1540 qsort (zh->reg->key_buf + zh->reg->ptr_top - ptr_i, ptr_i,
1541 sizeof(char*), key_qsort_compare);
1543 /* zebra.cfg: tempfiles:
1544 Y: always use temp files (old way)
1545 A: use temp files, if more than one (auto)
1546 = if this is both the last and the first
1547 N: never bother with temp files (new) */
1549 temp_policy=toupper(res_get_def(zh->res,"tempfiles","auto")[0]);
1550 if (temp_policy != 'Y' && temp_policy != 'N' && temp_policy != 'A') {
1551 yaz_log (YLOG_WARN, "Illegal tempfiles setting '%c'. using 'Auto' ",
1556 if ( ( temp_policy =='N' ) || /* always from memory */
1557 ( ( temp_policy =='A' ) && /* automatic */
1558 (zh->reg->key_file_no == 1) && /* this is first time */
1559 (final) ) ) /* and last (=only) time */
1560 { /* go directly from memory */
1561 zh->reg->key_file_no =0; /* signal not to read files */
1562 zebra_index_merge(zh);
1564 zh->reg->key_buf_used = 0;
1568 /* Not doing directly from memory, write into a temp file */
1569 extract_get_fname_tmp (zh, out_fname, zh->reg->key_file_no);
1571 if (!(outf = fopen (out_fname, "wb")))
1573 yaz_log (YLOG_FATAL|YLOG_ERRNO, "fopen %s", out_fname);
1576 yaz_log (YLOG_LOG, "writing section %d", zh->reg->key_file_no);
1577 prevcp = cp = (zh->reg->key_buf)[zh->reg->ptr_top - ptr_i];
1579 encode_key_init (&encode_info);
1580 encode_key_write (cp, &encode_info, outf);
1584 cp = (zh->reg->key_buf)[zh->reg->ptr_top - ptr_i];
1585 if (strcmp (cp, prevcp))
1587 encode_key_flush ( &encode_info, outf);
1588 encode_key_init (&encode_info);
1589 encode_key_write (cp, &encode_info, outf);
1593 encode_key_write (cp + strlen(cp), &encode_info, outf);
1595 encode_key_flush ( &encode_info, outf);
1597 qsort (key_buf + ptr_top-ptr_i, ptr_i, sizeof(char*), key_x_compare);
1598 extract_get_fname_tmp (out_fname, key_file_no);
1600 if (!(outf = fopen (out_fname, "wb")))
1602 yaz_log (YLOG_FATAL|YLOG_ERRNO, "fopen %s", out_fname);
1605 yaz_log (YLOG_LOG, "writing section %d", key_file_no);
1607 prevcp = key_buf[ptr_top-i];
1609 if (!--i || strcmp (prevcp, key_buf[ptr_top-i]))
1611 key_y_len = strlen(prevcp)+1;
1613 yaz_log (YLOG_LOG, "key_y_len: %2d %02x %02x %s",
1614 key_y_len, prevcp[0], prevcp[1], 2+prevcp);
1616 qsort (key_buf + ptr_top-ptr_i, ptr_i - i,
1617 sizeof(char*), key_y_compare);
1618 cp = key_buf[ptr_top-ptr_i];
1620 encode_key_init (&encode_info);
1621 encode_key_write (cp, &encode_info, outf);
1624 cp = key_buf[ptr_top-ptr_i];
1625 encode_key_write (cp+key_y_len, &encode_info, outf);
1627 encode_key_flush ( &encode_info, outf);
1630 prevcp = key_buf[ptr_top-ptr_i];
1635 yaz_log (YLOG_FATAL|YLOG_ERRNO, "fclose %s", out_fname);
1638 yaz_log (YLOG_LOG, "finished section %d", zh->reg->key_file_no);
1640 zh->reg->key_buf_used = 0;
1643 ZEBRA_RES zebra_snippets_rec_keys(ZebraHandle zh,
1644 zebra_rec_keys_t reckeys,
1645 zebra_snippets *snippets)
1647 NMEM nmem = nmem_create();
1648 if (zebra_rec_keys_rewind(reckeys))
1653 while (zebra_rec_keys_read(reckeys, &str, &slen, &key))
1655 char dst_buf[IT_MAX_WORD];
1656 char *dst_term = dst_buf;
1659 assert(key.len <= 4 && key.len > 2);
1660 seqno = (int) key.mem[key.len-1];
1663 zebraExplain_lookup_ord(zh->reg->zei, ord, &index_type,
1664 0/* db */, 0/* set */, 0/* use */,
1665 0 /* string_index */);
1667 zebra_term_untrans_iconv(zh, nmem, index_type,
1669 zebra_snippets_append(snippets, seqno, ord, dst_term);
1677 void print_rec_keys(ZebraHandle zh, zebra_rec_keys_t reckeys)
1679 yaz_log(YLOG_LOG, "print_rec_keys");
1680 if (zebra_rec_keys_rewind(reckeys))
1685 while (zebra_rec_keys_read(reckeys, &str, &slen, &key))
1687 char dst_buf[IT_MAX_WORD];
1691 assert(key.len <= 4 && key.len > 2);
1693 zebraExplain_lookup_ord(zh->reg->zei,
1694 key.mem[0], &index_type, &db, 0, 0, 0);
1696 seqno = (int) key.mem[key.len-1];
1698 zebra_term_untrans(zh, index_type, dst_buf, str);
1700 yaz_log(YLOG_LOG, "ord=" ZINT_FORMAT " seqno=%d term=%s",
1701 key.mem[0], seqno, dst_buf);
1706 void extract_add_index_string(RecWord *p, const char *str, int length)
1710 ZebraHandle zh = p->extractCtrl->handle;
1711 ZebraExplainInfo zei = zh->reg->zei;
1716 ch = zebraExplain_lookup_attr_str(zei, p->index_type, p->index_name);
1718 ch = zebraExplain_add_attr_str(zei, p->index_type, p->index_name);
1725 ch = zebraExplain_lookup_attr_su(zei, p->index_type,
1726 p->attrSet, p->attrUse);
1728 ch = zebraExplain_add_attr_su(zei, p->index_type,
1729 p->attrSet, p->attrUse);
1734 key.mem[1] = p->record_id;
1735 key.mem[2] = p->section_id;
1736 key.mem[3] = p->seqno;
1745 for (i = 0; i<length && i < 20; i++)
1746 sprintf(strz+strlen(strz), "%02X", str[i] & 0xff);
1747 /* just for debugging .. */
1748 yaz_log(YLOG_LOG, "add: set=%d use=%d "
1749 "record_id=%lld section_id=%lld seqno=%lld %s",
1750 p->attrSet, p->attrUse, p->record_id, p->section_id, p->seqno,
1755 zebra_rec_keys_write(zh->reg->keys, str, length, &key);
1759 static void extract_add_sort_string (RecWord *p, const char *str, int length)
1763 ZebraHandle zh = p->extractCtrl->handle;
1764 ZebraExplainInfo zei = zh->reg->zei;
1769 ch = zebraExplain_lookup_attr_str(zei, p->index_type, p->index_name);
1771 ch = zebraExplain_add_attr_str(zei, p->index_type, p->index_name);
1779 key.mem[1] = p->record_id;
1780 key.mem[2] = p->section_id;
1781 key.mem[3] = p->seqno;
1783 zebra_rec_keys_write(zh->reg->sortKeys, str, length, &key);
1786 static void extract_add_sort_string (RecWord *p, const char *str, int length)
1788 ZebraHandle zh = p->extractCtrl->handle;
1789 struct sortKeys *sk = &zh->reg->sortKeys;
1792 while (off < sk->buf_used)
1796 off += key_SU_decode(&set, (unsigned char *) sk->buf + off);
1797 off += key_SU_decode(&use, (unsigned char *) sk->buf + off);
1798 off += key_SU_decode(&slen, (unsigned char *) sk->buf + off);
1800 if (p->attrSet == set && p->attrUse == use)
1803 assert (off == sk->buf_used);
1805 if (sk->buf_used + IT_MAX_WORD > sk->buf_max)
1809 b = (char *) xmalloc (sk->buf_max += 128000);
1810 if (sk->buf_used > 0)
1811 memcpy (b, sk->buf, sk->buf_used);
1815 off += key_SU_encode(p->attrSet, sk->buf + off);
1816 off += key_SU_encode(p->attrUse, sk->buf + off);
1817 off += key_SU_encode(length, sk->buf + off);
1818 memcpy (sk->buf + off, str, length);
1819 sk->buf_used = off + length;
1823 void extract_add_string (RecWord *p, const char *string, int length)
1825 assert (length > 0);
1826 if (zebra_maps_is_sort (p->zebra_maps, p->index_type))
1827 extract_add_sort_string (p, string, length);
1829 extract_add_index_string (p, string, length);
1832 static void extract_add_incomplete_field (RecWord *p)
1834 const char *b = p->term_buf;
1835 int remain = p->term_len;
1836 const char **map = 0;
1838 yaz_log(YLOG_DEBUG, "Incomplete field, w='%.*s'", p->term_len, p->term_buf);
1841 map = zebra_maps_input(p->zebra_maps, p->index_type, &b, remain, 0);
1845 char buf[IT_MAX_WORD+1];
1849 while (map && *map && **map == *CHR_SPACE)
1851 remain = p->term_len - (b - p->term_buf);
1853 map = zebra_maps_input(p->zebra_maps, p->index_type, &b,
1861 while (map && *map && **map != *CHR_SPACE)
1863 const char *cp = *map;
1865 while (i < IT_MAX_WORD && *cp)
1867 remain = p->term_len - (b - p->term_buf);
1869 map = zebra_maps_input(p->zebra_maps, p->index_type, &b, remain, 0);
1875 extract_add_string (p, buf, i);
1880 static void extract_add_complete_field (RecWord *p)
1882 const char *b = p->term_buf;
1883 char buf[IT_MAX_WORD+1];
1884 const char **map = 0;
1885 int i = 0, remain = p->term_len;
1887 yaz_log(YLOG_DEBUG, "Complete field, w='%.*s'",
1888 p->term_len, p->term_buf);
1891 map = zebra_maps_input (p->zebra_maps, p->index_type, &b, remain, 1);
1893 while (remain > 0 && i < IT_MAX_WORD)
1895 while (map && *map && **map == *CHR_SPACE)
1897 remain = p->term_len - (b - p->term_buf);
1901 int first = i ? 0 : 1; /* first position */
1902 map = zebra_maps_input(p->zebra_maps, p->index_type, &b, remain, first);
1910 if (i && i < IT_MAX_WORD)
1911 buf[i++] = *CHR_SPACE;
1912 while (map && *map && **map != *CHR_SPACE)
1914 const char *cp = *map;
1916 if (**map == *CHR_CUT)
1922 if (i >= IT_MAX_WORD)
1924 yaz_log(YLOG_DEBUG, "Adding string to index '%d'", **map);
1925 while (i < IT_MAX_WORD && *cp)
1928 remain = p->term_len - (b - p->term_buf);
1931 map = zebra_maps_input (p->zebra_maps, p->index_type, &b,
1940 extract_add_string (p, buf, i);
1943 void extract_token_add (RecWord *p)
1947 yaz_log (YLOG_LOG, "token_add "
1948 "reg_type=%c attrSet=%d attrUse=%d seqno=%d s=%.*s",
1949 p->reg_type, p->attrSet, p->attrUse, p->seqno, p->length,
1952 if ((wrbuf = zebra_replace(p->zebra_maps, p->index_type, 0,
1953 p->term_buf, p->term_len)))
1955 p->term_buf = wrbuf_buf(wrbuf);
1956 p->term_len = wrbuf_len(wrbuf);
1958 if (zebra_maps_is_complete (p->zebra_maps, p->index_type))
1959 extract_add_complete_field (p);
1961 extract_add_incomplete_field(p);
1964 static void extract_set_store_data_cb(struct recExtractCtrl *p,
1965 void *buf, size_t sz)
1967 ZebraHandle zh = (ZebraHandle) p->handle;
1969 xfree(zh->store_data_buf);
1970 zh->store_data_buf = 0;
1971 zh->store_data_size = 0;
1974 zh->store_data_buf = xmalloc(sz);
1975 zh->store_data_size = sz;
1976 memcpy(zh->store_data_buf, buf, sz);
1980 static void extract_set_store_data_prepare(struct recExtractCtrl *p)
1982 ZebraHandle zh = (ZebraHandle) p->handle;
1983 xfree(zh->store_data_buf);
1984 zh->store_data_buf = 0;
1985 zh->store_data_size = 0;
1986 p->setStoreData = extract_set_store_data_cb;
1989 void extract_schema_add (struct recExtractCtrl *p, Odr_oid *oid)
1991 ZebraHandle zh = (ZebraHandle) p->handle;
1992 zebraExplain_addSchema (zh->reg->zei, oid);
1996 #error not done yet with zebra_rec_keys_t
1997 void extract_flushSortKeys (ZebraHandle zh, SYSNO sysno,
1998 int cmd, struct recKeys *reckeys)
2000 SortIdx sortIdx = zh->reg->sortIdx;
2001 void *decode_handle = iscz1_start();
2005 while (off < reckeys->buf_used)
2007 const char *src = reckeys->buf + off;
2009 char *dst = (char*) &key;
2011 iscz1_decode(decode_handle, &dst, &src);
2012 assert(key.len == 4);
2014 ch = (int) key.mem[0]; /* ordinal for field/use/attribute */
2016 sortIdx_type(sortIdx, ch);
2018 sortIdx_add(sortIdx, src, strlen(src));
2020 sortIdx_add(sortIdx, "", 1);
2025 off = src - reckeys->buf;
2027 assert (off == reckeys->buf_used);
2028 iscz1_stop(decode_handle);
2031 void extract_flushSortKeys (ZebraHandle zh, SYSNO sysno,
2032 int cmd, struct sortKeys *sk)
2034 SortIdx sortIdx = zh->reg->sortIdx;
2037 sortIdx_sysno (sortIdx, sysno);
2039 while (off < sk->buf_used)
2043 off += key_SU_decode(&set, (unsigned char *) sk->buf + off);
2044 off += key_SU_decode(&use, (unsigned char *) sk->buf + off);
2045 off += key_SU_decode(&slen, (unsigned char *) sk->buf + off);
2047 sortIdx_type(sortIdx, use);
2049 sortIdx_add(sortIdx, sk->buf + off, slen);
2051 sortIdx_add(sortIdx, "", 1);
2057 void encode_key_init (struct encode_info *i)
2066 i->encode_handle = iscz1_start();
2067 i->decode_handle = iscz1_start();
2073 /* this is the old encode_key_write
2074 * may be deleted once we are confident that the new works
2077 void encode_key_write (char *k, struct encode_info *i, FILE *outf)
2080 char *bp = i->buf, *bp0;
2081 const char *src = (char *) &key;
2083 /* copy term to output buf */
2084 while ((*bp++ = *k++))
2086 /* and copy & align key so we can mangle */
2087 memcpy (&key, k+1, sizeof(struct it_key)); /* *k is insert/delete */
2091 key_logdump_txt(YLOG_LOG, &key, *k ? "i" : "d");
2093 assert(key.mem[0] >= 0);
2096 iscz1_encode(i->encode_handle, &bp, &src);
2098 *bp0 = (*k * 128) + bp - bp0 - 1; /* length and insert/delete combined */
2099 if (fwrite (i->buf, bp - i->buf, 1, outf) != 1)
2101 yaz_log (YLOG_FATAL|YLOG_ERRNO, "fwrite");
2110 const char *src = bp0+1;
2111 char *dst = (char*) &key2;
2112 iscz1_decode(i->decode_handle, &dst, &src);
2114 key_logdump_txt(YLOG_LOG, &key2, *k ? "i" : "d");
2116 assert(key2.mem[1]);
2121 void encode_key_flush (struct encode_info *i, FILE *outf)
2123 iscz1_stop(i->encode_handle);
2124 iscz1_stop(i->decode_handle);
2129 /* new encode_key_write
2130 * The idea is to buffer one more key, and compare them
2131 * If we are going to delete and insert the same key,
2132 * we may as well not bother. Should make a difference in
2133 * updates with small modifications (appending to a mbox)
2135 void encode_key_write (char *k, struct encode_info *i, FILE *outf)
2140 if (*k) /* first time for new key */
2143 while ((*bp++ = *k++))
2145 i->keylen= bp - i->buf -1;
2146 assert(i->keylen+1+sizeof(struct it_key) < ENCODE_BUFLEN);
2150 bp=i->buf + i->keylen;
2155 memcpy (&key, k+1, sizeof(struct it_key));
2156 if (0==i->prevsys) /* no previous filter, fill up */
2158 i->prevsys=key.sysno;
2159 i->prevseq=key.seqno;
2162 else if ( (i->prevsys==key.sysno) &&
2163 (i->prevseq==key.seqno) &&
2165 { /* same numbers, diff cmd, they cancel out */
2169 { /* different stuff, write previous, move buf */
2170 bp = encode_key_int ( (i->prevsys - i->sysno) * 2 + i->prevcmd, bp);
2171 if (i->sysno != i->prevsys)
2173 i->sysno = i->prevsys;
2176 else if (!i->seqno && !i->prevseq && i->cmd == i->prevcmd)
2178 return; /* ??? Filters some sort of duplicates away */
2179 /* ??? Can this ever happen -H 15oct02 */
2181 bp = encode_key_int (i->prevseq - i->seqno, bp);
2182 i->seqno = i->prevseq;
2183 i->cmd = i->prevcmd;
2184 if (fwrite (i->buf, bp - i->buf, 1, outf) != 1)
2186 yaz_log (YLOG_FATAL|YLOG_ERRNO, "fwrite");
2189 i->keylen=0; /* ok, it's written, forget it */
2190 i->prevsys=key.sysno;
2191 i->prevseq=key.seqno;
2196 void encode_key_flush (struct encode_info *i, FILE *outf)
2197 { /* flush the last key from i */
2198 char *bp =i->buf + i->keylen;
2201 return; /* nothing to flush */
2204 bp = encode_key_int ( (i->prevsys - i->sysno) * 2 + i->prevcmd, bp);
2205 if (i->sysno != i->prevsys)
2207 i->sysno = i->prevsys;
2210 else if (!i->seqno && !i->prevseq && i->cmd == i->prevcmd)
2212 return; /* ??? Filters some sort of duplicates away */
2213 /* ??? Can this ever happen -H 15oct02 */
2215 bp = encode_key_int (i->prevseq - i->seqno, bp);
2216 i->seqno = i->prevseq;
2217 i->cmd = i->prevcmd;
2218 if (fwrite (i->buf, bp - i->buf, 1, outf) != 1)
2220 yaz_log (YLOG_FATAL|YLOG_ERRNO, "fwrite");
2223 i->keylen=0; /* ok, it's written, forget it */
2224 i->prevsys=0; /* forget the values too */
2231 * indent-tabs-mode: nil
2233 * vim: shiftwidth=4 tabstop=8 expandtab