1 /* $Id: extract.c,v 1.225 2006-08-14 10:40:15 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 this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
39 #define ENCODE_BUFLEN 768
43 char buf[ENCODE_BUFLEN];
46 static int log_level = 0;
47 static int log_level_initialized = 1;
49 static void zebra_init_log_level()
51 if (!log_level_initialized)
53 log_level = yaz_log_module_level("extract");
54 log_level_initialized = 1;
58 static void extract_flushRecordKeys (ZebraHandle zh, SYSNO sysno,
59 int cmd, zebra_rec_keys_t reckeys,
61 static void extract_flushSortKeys (ZebraHandle zh, SYSNO sysno,
62 int cmd, zebra_rec_keys_t skp);
63 static void extract_schema_add (struct recExtractCtrl *p, Odr_oid *oid);
64 static void extract_token_add (RecWord *p);
66 static void encode_key_init (struct encode_info *i);
67 static void encode_key_write (char *k, struct encode_info *i, FILE *outf);
68 static void encode_key_flush (struct encode_info *i, FILE *outf);
70 #define USE_SHELLSORT 0
73 static void shellsort(void *ar, int r, size_t s,
74 int (*cmp)(const void *a, const void *b))
79 static const int incs[16] = { 1391376, 463792, 198768, 86961, 33936,
80 13776, 4592, 1968, 861, 336,
81 112, 48, 21, 7, 3, 1 };
82 for ( k = 0; k < 16; k++)
83 for (h = incs[k], i = h; i < r; i++)
87 while (j > h && (*cmp)(a + s*(j-h), v) > 0)
89 memcpy (a + s*j, a + s*(j-h), s);
97 static void logRecord (ZebraHandle zh)
99 ++zh->records_processed;
100 if (!(zh->records_processed % 1000))
102 yaz_log(YLOG_LOG, "Records: "ZINT_FORMAT" i/u/d "
103 ZINT_FORMAT"/"ZINT_FORMAT"/"ZINT_FORMAT,
104 zh->records_processed, zh->records_inserted,
105 zh->records_updated, zh->records_deleted);
109 static void extract_add_index_string (RecWord *p,
110 zinfo_index_category_t cat,
111 const char *str, int length);
113 static void extract_set_store_data_prepare(struct recExtractCtrl *p);
115 static void extract_init (struct recExtractCtrl *p, RecWord *w)
117 w->zebra_maps = p->zebra_maps;
119 w->index_name = "any";
126 static void searchRecordKey(ZebraHandle zh,
127 zebra_rec_keys_t reckeys,
128 const char *index_name,
129 const char **ws, int ws_length)
133 zinfo_index_category_t cat = zinfo_index_category_index;
135 for (i = 0; i<ws_length; i++)
139 ch = zebraExplain_lookup_attr_str(zh->reg->zei, cat, '0', index_name);
141 ch = zebraExplain_lookup_attr_str(zh->reg->zei, cat, 'p', index_name);
143 ch = zebraExplain_lookup_attr_str(zh->reg->zei, cat, 'w', index_name);
148 if (zebra_rec_keys_rewind(reckeys))
155 while (zebra_rec_keys_read(reckeys, &str, &slen, &key))
157 assert(key.len <= 4 && key.len > 2);
159 seqno = key.mem[key.len-1];
161 if (key.mem[0] == ch)
167 woff = seqno - startSeq;
168 if (woff >= 0 && woff < ws_length)
175 struct file_read_info {
176 off_t file_max; /* maximum offset so far */
177 off_t file_offset; /* current offset */
178 off_t file_moffset; /* offset of rec/rec boundary */
183 static struct file_read_info *file_read_start (int fd)
185 struct file_read_info *fi = (struct file_read_info *)
186 xmalloc (sizeof(*fi));
190 fi->file_moffset = 0;
196 static void file_read_stop (struct file_read_info *fi)
201 static off_t file_seek (void *handle, off_t offset)
203 struct file_read_info *p = (struct file_read_info *) handle;
204 p->file_offset = offset;
205 return lseek (p->fd, offset, SEEK_SET);
208 static off_t file_tell (void *handle)
210 struct file_read_info *p = (struct file_read_info *) handle;
211 return p->file_offset;
214 static int file_read (void *handle, char *buf, size_t count)
216 struct file_read_info *p = (struct file_read_info *) handle;
219 r = read (fd, buf, count);
223 if (p->file_offset > p->file_max)
224 p->file_max = p->file_offset;
229 static void file_end (void *handle, off_t offset)
231 struct file_read_info *p = (struct file_read_info *) handle;
233 if (offset != p->file_moffset)
235 p->file_moffset = offset;
240 #define FILE_MATCH_BLANK "\t "
242 static char *fileMatchStr (ZebraHandle zh,
243 zebra_rec_keys_t reckeys,
244 const char *fname, const char *spec)
246 static char dstBuf[2048]; /* static here ??? */
248 const char *s = spec;
252 for (; *s && strchr(FILE_MATCH_BLANK, *s); s++)
259 char attset_str[64], attname_str[64];
263 for (s++; strchr(FILE_MATCH_BLANK, *s); s++)
265 for (i = 0; *s && *s != ',' && *s != ')' &&
266 !strchr(FILE_MATCH_BLANK, *s); s++)
267 if (i+1 < sizeof(attset_str))
268 attset_str[i++] = *s;
269 attset_str[i] = '\0';
271 for (; strchr(FILE_MATCH_BLANK, *s); s++)
274 strcpy(attname_str, attset_str);
277 for (s++; strchr(FILE_MATCH_BLANK, *s); s++)
279 for (i = 0; *s && *s != ')' &&
280 !strchr(FILE_MATCH_BLANK, *s); s++)
281 if (i+1 < sizeof(attname_str))
282 attname_str[i++] = *s;
283 attname_str[i] = '\0';
286 searchRecordKey (zh, reckeys, attname_str, ws, 32);
290 yaz_log (YLOG_WARN, "Missing ) in match criteria %s in group %s",
291 spec, zh->m_group ? zh->m_group : "none");
296 for (i = 0; i<32; i++)
305 dst += strlen(ws[i]);
309 yaz_log (YLOG_WARN, "Record didn't contain match"
310 " fields in (%s,%s)", attset_str, attname_str);
318 const char *spec_src = NULL;
319 const char *s1 = ++s;
320 while (*s1 && !strchr(FILE_MATCH_BLANK, *s1))
324 if (spec_len > sizeof(special)-1)
325 spec_len = sizeof(special)-1;
326 memcpy (special, s, spec_len);
327 special[spec_len] = '\0';
330 if (!strcmp (special, "group"))
331 spec_src = zh->m_group;
332 else if (!strcmp (special, "database"))
333 spec_src = zh->basenames[0];
334 else if (!strcmp (special, "filename")) {
337 else if (!strcmp (special, "type"))
338 spec_src = zh->m_record_type;
343 strcpy (dst, spec_src);
344 dst += strlen (spec_src);
347 else if (*s == '\"' || *s == '\'')
349 int stopMarker = *s++;
353 while (*s && *s != stopMarker)
355 if (i+1 < sizeof(tmpString))
356 tmpString[i++] = *s++;
361 strcpy (dst, tmpString);
362 dst += strlen (tmpString);
366 yaz_log (YLOG_WARN, "Syntax error in match criteria %s in group %s",
367 spec, zh->m_group ? zh->m_group : "none");
374 yaz_log (YLOG_WARN, "No match criteria for record %s in group %s",
375 fname, zh->m_group ? zh->m_group : "none");
382 struct recordLogInfo {
385 struct recordGroup *rGroup;
388 static void init_extractCtrl(ZebraHandle zh, struct recExtractCtrl *ctrl)
391 for (i = 0; i<256; i++)
393 if (zebra_maps_is_positioned(zh->reg->zebra_maps, i))
398 ctrl->zebra_maps = zh->reg->zebra_maps;
399 ctrl->flagShowRecords = !zh->m_flag_rw;
402 static void all_matches_add(struct recExtractCtrl *ctrl)
405 extract_init(ctrl, &word);
406 word.index_name = "_ALLRECORDS";
407 word.index_type = 'w';
409 extract_add_index_string (&word, zinfo_index_category_alwaysmatches,
413 static ZEBRA_RES file_extract_record(ZebraHandle zh,
414 SYSNO *sysno, const char *fname,
416 struct file_read_info *fi,
419 void *recTypeClientData)
421 const char *match_str_to_print = "";
422 RecordAttr *recordAttr;
424 const char *matchStr = 0;
427 off_t recordOffset = 0;
428 struct recExtractCtrl extractCtrl;
430 /* announce database */
431 if (zebraExplain_curDatabase (zh->reg->zei, zh->basenames[0]))
433 if (zebraExplain_newDatabase (zh->reg->zei, zh->basenames[0],
434 zh->m_explain_database))
440 /* we are going to read from a file, so prepare the extraction */
441 zebra_rec_keys_reset(zh->reg->keys);
443 zebra_rec_keys_reset(zh->reg->sortKeys);
444 recordOffset = fi->file_moffset;
445 extractCtrl.handle = zh;
446 extractCtrl.offset = fi->file_moffset;
447 extractCtrl.readf = file_read;
448 extractCtrl.seekf = file_seek;
449 extractCtrl.tellf = file_tell;
450 extractCtrl.endf = file_end;
452 extractCtrl.init = extract_init;
453 extractCtrl.tokenAdd = extract_token_add;
454 extractCtrl.schemaAdd = extract_schema_add;
455 extractCtrl.dh = zh->reg->dh;
456 extractCtrl.match_criteria[0] = '\0';
457 extractCtrl.staticrank = 0;
459 extractCtrl.first_record = fi->file_offset ? 0 : 1;
461 extract_set_store_data_prepare(&extractCtrl);
463 init_extractCtrl(zh, &extractCtrl);
466 printf ("File: %s " ZINT_FORMAT "\n", fname, (zint)recordOffset);
470 sprintf (msg, "%s:" ZINT_FORMAT , fname, (zint)recordOffset);
471 yaz_log_init_prefix2 (msg);
474 r = (*recType->extract)(recTypeClientData, &extractCtrl);
476 yaz_log_init_prefix2 (0);
477 if (r == RECCTRL_EXTRACT_EOF)
479 else if (r == RECCTRL_EXTRACT_ERROR_GENERIC)
481 /* error occured during extraction ... */
483 zh->records_processed < zh->m_file_verbose_limit)
485 yaz_log (YLOG_WARN, "fail %s %s " ZINT_FORMAT,
487 fname, (zint) recordOffset);
491 else if (r == RECCTRL_EXTRACT_ERROR_NO_SUCH_FILTER)
493 /* error occured during extraction ... */
495 zh->records_processed < zh->m_file_verbose_limit)
497 yaz_log (YLOG_WARN, "no filter for %s %s "
498 ZINT_FORMAT, zh->m_record_type,
499 fname, (zint) recordOffset);
503 all_matches_add(&extractCtrl);
504 if (extractCtrl.match_criteria[0])
505 matchStr = extractCtrl.match_criteria;
508 /* if matchStr is set now - we assume it's printable .
509 For internal matchStr (see below) we don't print */
511 match_str_to_print = matchStr;
513 /* perform internal match if sysno not known and if match criteria is
520 if (matchStr == 0 && zh->m_record_id && *zh->m_record_id)
522 matchStr = fileMatchStr (zh, zh->reg->keys, fname,
526 yaz_log(YLOG_WARN, "Bad match criteria");
528 if (zebra_rec_keys_empty(zh->reg->keys))
530 yaz_log(YLOG_WARN, "And no index keys");
537 int db_ord = zebraExplain_get_database_ord(zh->reg->zei);
538 char *rinfo = dict_lookup_ord(zh->reg->matchDict, db_ord,
542 assert(*rinfo == sizeof(*sysno));
543 memcpy (sysno, rinfo+1, sizeof(*sysno));
547 if (! *sysno && zebra_rec_keys_empty(zh->reg->keys) )
549 /* the extraction process returned no information - the record
550 is probably empty - unless flagShowRecords is in use */
554 if (zh->records_processed < zh->m_file_verbose_limit)
555 yaz_log(YLOG_WARN, "empty %s %s " ZINT_FORMAT, zh->m_record_type,
556 fname, (zint)recordOffset);
565 yaz_log(YLOG_LOG, "delete %s %s " ZINT_FORMAT, zh->m_record_type,
566 fname, (zint)recordOffset);
567 yaz_log(YLOG_WARN, "cannot delete record above (seems new)");
571 rec = rec_new (zh->reg->records);
575 if (zh->records_processed < zh->m_file_verbose_limit)
577 yaz_log(YLOG_LOG, "add %s %s " ZINT_FORMAT
578 " " ZINT_FORMAT " %s" ,
580 fname, (zint) recordOffset, *sysno, match_str_to_print);
582 recordAttr = rec_init_attr (zh->reg->zei, rec);
583 recordAttr->staticrank = extractCtrl.staticrank;
587 int db_ord = zebraExplain_get_database_ord(zh->reg->zei);
588 dict_insert_ord(zh->reg->matchDict, db_ord, matchStr,
589 sizeof(*sysno), sysno);
593 extract_flushSortKeys (zh, *sysno, 1, zh->reg->sortKeys);
594 extract_flushRecordKeys (zh, *sysno, 1, zh->reg->keys,
595 recordAttr->staticrank);
596 zh->records_inserted++;
600 /* record already exists */
601 zebra_rec_keys_t delkeys = zebra_rec_keys_open();
603 zebra_rec_keys_t sortKeys = zebra_rec_keys_open();
605 rec = rec_get (zh->reg->records, *sysno);
608 recordAttr = rec_init_attr (zh->reg->zei, rec);
610 zebra_rec_keys_set_buf(delkeys,
611 rec->info[recInfo_delKeys],
612 rec->size[recInfo_delKeys],
615 zebra_rec_keys_set_buf(sortKeys,
616 rec->info[recInfo_sortKeys],
617 rec->size[recInfo_sortKeys],
619 extract_flushSortKeys (zh, *sysno, 0, sortKeys);
620 extract_flushRecordKeys (zh, *sysno, 0, delkeys,
621 recordAttr->staticrank); /* old values */
624 /* record going to be deleted */
625 if (zebra_rec_keys_empty(delkeys))
627 yaz_log(YLOG_LOG, "delete %s %s " ZINT_FORMAT
629 zh->m_record_type, fname, (zint)recordOffset, *sysno);
630 yaz_log(YLOG_WARN, "cannot delete file above, storeKeys false (1)");
634 if (zh->records_processed < zh->m_file_verbose_limit)
636 yaz_log(YLOG_LOG, "delete %s %s " ZINT_FORMAT
637 " " ZINT_FORMAT " %s" ,
638 zh->m_record_type, fname, (zint) recordOffset,
639 *sysno, match_str_to_print);
641 zh->records_deleted++;
644 int db_ord = zebraExplain_get_database_ord(zh->reg->zei);
645 dict_delete_ord(zh->reg->matchDict, db_ord, matchStr);
647 rec_del (zh->reg->records, &rec);
655 /* flush new keys for sort&search etc */
656 if (zh->records_processed < zh->m_file_verbose_limit)
658 yaz_log(YLOG_LOG, "update %s %s " ZINT_FORMAT
659 " " ZINT_FORMAT " %s" ,
660 zh->m_record_type, fname, (zint) recordOffset,
661 *sysno, match_str_to_print);
663 recordAttr->staticrank = extractCtrl.staticrank;
664 extract_flushSortKeys (zh, *sysno, 1, zh->reg->sortKeys);
665 extract_flushRecordKeys (zh, *sysno, 1, zh->reg->keys,
666 recordAttr->staticrank);
667 zh->records_updated++;
669 zebra_rec_keys_close(delkeys);
670 zebra_rec_keys_close(sortKeys);
672 /* update file type */
673 xfree (rec->info[recInfo_fileType]);
674 rec->info[recInfo_fileType] =
675 rec_strdup (zh->m_record_type, &rec->size[recInfo_fileType]);
677 /* update filename */
678 xfree (rec->info[recInfo_filename]);
679 rec->info[recInfo_filename] =
680 rec_strdup (fname, &rec->size[recInfo_filename]);
682 /* update delete keys */
683 xfree (rec->info[recInfo_delKeys]);
684 if (!zebra_rec_keys_empty(zh->reg->keys) && zh->m_store_keys == 1)
686 zebra_rec_keys_get_buf(zh->reg->keys,
687 &rec->info[recInfo_delKeys],
688 &rec->size[recInfo_delKeys]);
692 rec->info[recInfo_delKeys] = NULL;
693 rec->size[recInfo_delKeys] = 0;
696 /* update sort keys */
697 xfree (rec->info[recInfo_sortKeys]);
699 zebra_rec_keys_get_buf(zh->reg->sortKeys,
700 &rec->info[recInfo_sortKeys],
701 &rec->size[recInfo_sortKeys]);
703 /* save file size of original record */
704 zebraExplain_recordBytesIncrement (zh->reg->zei,
705 - recordAttr->recordSize);
706 recordAttr->recordSize = fi->file_moffset - recordOffset;
707 if (!recordAttr->recordSize)
708 recordAttr->recordSize = fi->file_max - recordOffset;
709 zebraExplain_recordBytesIncrement (zh->reg->zei,
710 recordAttr->recordSize);
712 /* set run-number for this record */
713 recordAttr->runNumber = zebraExplain_runNumberIncrement (zh->reg->zei,
716 /* update store data */
717 xfree (rec->info[recInfo_storeData]);
718 if (zh->store_data_buf)
720 rec->size[recInfo_storeData] = zh->store_data_size;
721 rec->info[recInfo_storeData] = zh->store_data_buf;
722 zh->store_data_buf = 0;
724 else if (zh->m_store_data)
726 rec->size[recInfo_storeData] = recordAttr->recordSize;
727 rec->info[recInfo_storeData] = (char *)
728 xmalloc (recordAttr->recordSize);
729 if (lseek (fi->fd, recordOffset, SEEK_SET) < 0)
731 yaz_log(YLOG_ERRNO|YLOG_FATAL, "seek to " ZINT_FORMAT " in %s",
732 (zint)recordOffset, fname);
735 if (read (fi->fd, rec->info[recInfo_storeData], recordAttr->recordSize)
736 < recordAttr->recordSize)
738 yaz_log (YLOG_ERRNO|YLOG_FATAL, "read %d bytes of %s",
739 recordAttr->recordSize, fname);
745 rec->info[recInfo_storeData] = NULL;
746 rec->size[recInfo_storeData] = 0;
748 /* update database name */
749 xfree (rec->info[recInfo_databaseName]);
750 rec->info[recInfo_databaseName] =
751 rec_strdup (zh->basenames[0], &rec->size[recInfo_databaseName]);
754 recordAttr->recordOffset = recordOffset;
756 /* commit this record */
757 rec_put (zh->reg->records, &rec);
762 ZEBRA_RES zebra_extract_file(ZebraHandle zh, SYSNO *sysno, const char *fname,
765 ZEBRA_RES r = ZEBRA_OK;
770 struct file_read_info *fi;
771 const char *original_record_type = 0;
773 void *recTypeClientData;
775 zebra_init_log_level();
777 if (!zh->m_group || !*zh->m_group)
780 sprintf (gprefix, "%s.", zh->m_group);
782 yaz_log(log_level, "zebra_extract_file %s", fname);
784 /* determine file extension */
786 for (i = strlen(fname); --i >= 0; )
789 else if (fname[i] == '.')
791 strcpy (ext, fname+i+1);
794 /* determine file type - depending on extension */
795 original_record_type = zh->m_record_type;
796 if (!zh->m_record_type)
798 sprintf (ext_res, "%srecordType.%s", gprefix, ext);
799 zh->m_record_type = res_get (zh->res, ext_res);
801 if (!zh->m_record_type)
803 if (zh->records_processed < zh->m_file_verbose_limit)
804 yaz_log (YLOG_LOG, "? %s", fname);
807 /* determine match criteria */
808 if (!zh->m_record_id)
810 sprintf (ext_res, "%srecordId.%s", gprefix, ext);
811 zh->m_record_id = res_get (zh->res, ext_res);
815 recType_byName (zh->reg->recTypes, zh->res, zh->m_record_type,
816 &recTypeClientData)))
818 yaz_log(YLOG_WARN, "No such record type: %s", zh->m_record_type);
822 switch(recType->version)
827 yaz_log(YLOG_WARN, "Bad filter version: %s", zh->m_record_type);
829 if (sysno && deleteFlag)
835 if (zh->path_reg && !yaz_is_abspath (fname))
837 strcpy (full_rep, zh->path_reg);
838 strcat (full_rep, "/");
839 strcat (full_rep, fname);
842 strcpy (full_rep, fname);
844 if ((fd = open (full_rep, O_BINARY|O_RDONLY)) == -1)
846 yaz_log (YLOG_WARN|YLOG_ERRNO, "open %s", full_rep);
847 zh->m_record_type = original_record_type;
851 fi = file_read_start (fd);
854 fi->file_moffset = fi->file_offset;
855 fi->file_more = 0; /* file_end not called (yet) */
856 r = file_extract_record (zh, sysno, fname, deleteFlag, fi, 1,
857 recType, recTypeClientData);
859 { /* file_end has been called so reset offset .. */
860 fi->file_offset = fi->file_moffset;
861 lseek(fi->fd, fi->file_moffset, SEEK_SET);
875 zh->m_record_type = original_record_type;
880 If sysno is provided, then it's used to identify the reocord.
881 If not, and match_criteria is provided, then sysno is guessed
882 If not, and a record is provided, then sysno is got from there
885 ZEBRA_RES zebra_buffer_extract_record(ZebraHandle zh,
886 const char *buf, size_t buf_size,
889 const char *recordType,
891 const char *match_criteria,
897 RecordAttr *recordAttr;
898 struct recExtractCtrl extractCtrl;
900 const char *matchStr = 0;
901 RecType recType = NULL;
904 long recordOffset = 0;
905 struct zebra_fetch_control fc;
906 const char *pr_fname = fname; /* filename to print .. */
907 int show_progress = zh->records_processed < zh->m_file_verbose_limit ? 1:0;
909 zebra_init_log_level();
912 pr_fname = "<no file>"; /* make it printable if file is omitted */
915 fc.record_int_buf = buf;
916 fc.record_int_len = buf_size;
917 fc.record_int_pos = 0;
919 fc.record_offset = 0;
921 extractCtrl.offset = 0;
922 extractCtrl.readf = zebra_record_int_read;
923 extractCtrl.seekf = zebra_record_int_seek;
924 extractCtrl.tellf = zebra_record_int_tell;
925 extractCtrl.endf = zebra_record_int_end;
926 extractCtrl.first_record = 1;
927 extractCtrl.fh = &fc;
929 zebra_rec_keys_reset(zh->reg->keys);
930 zebra_rec_keys_reset(zh->reg->sortKeys);
932 if (zebraExplain_curDatabase (zh->reg->zei, zh->basenames[0]))
934 if (zebraExplain_newDatabase (zh->reg->zei, zh->basenames[0],
935 zh->m_explain_database))
939 if (recordType && *recordType)
941 yaz_log(log_level, "Record type explicitly specified: %s", recordType);
942 recType = recType_byName (zh->reg->recTypes, zh->res, recordType,
947 if (!(zh->m_record_type))
949 yaz_log (YLOG_WARN, "No such record type defined");
952 yaz_log(log_level, "Get record type from rgroup: %s",
954 recType = recType_byName (zh->reg->recTypes, zh->res,
955 zh->m_record_type, &clientData);
956 recordType = zh->m_record_type;
961 yaz_log (YLOG_WARN, "No such record type: %s", recordType);
965 extractCtrl.init = extract_init;
966 extractCtrl.tokenAdd = extract_token_add;
967 extractCtrl.schemaAdd = extract_schema_add;
968 extractCtrl.dh = zh->reg->dh;
969 extractCtrl.handle = zh;
970 extractCtrl.match_criteria[0] = '\0';
971 extractCtrl.staticrank = 0;
973 init_extractCtrl(zh, &extractCtrl);
975 extract_set_store_data_prepare(&extractCtrl);
977 r = (*recType->extract)(clientData, &extractCtrl);
979 if (r == RECCTRL_EXTRACT_EOF)
981 else if (r == RECCTRL_EXTRACT_ERROR_GENERIC)
983 /* error occured during extraction ... */
984 yaz_log (YLOG_WARN, "extract error: generic");
987 else if (r == RECCTRL_EXTRACT_ERROR_NO_SUCH_FILTER)
989 /* error occured during extraction ... */
990 yaz_log (YLOG_WARN, "extract error: no such filter");
994 all_matches_add(&extractCtrl);
996 if (extractCtrl.match_criteria[0])
997 match_criteria = extractCtrl.match_criteria;
1003 if (match_criteria && *match_criteria) {
1004 matchStr = match_criteria;
1006 if (zh->m_record_id && *zh->m_record_id) {
1007 matchStr = fileMatchStr (zh, zh->reg->keys, pr_fname,
1011 yaz_log (YLOG_WARN, "Bad match criteria (recordID)");
1018 int db_ord = zebraExplain_get_database_ord(zh->reg->zei);
1019 char *rinfo = dict_lookup_ord(zh->reg->matchDict, db_ord,
1023 assert(*rinfo == sizeof(*sysno));
1024 memcpy (sysno, rinfo+1, sizeof(*sysno));
1028 if (zebra_rec_keys_empty(zh->reg->keys))
1030 /* the extraction process returned no information - the record
1031 is probably empty - unless flagShowRecords is in use */
1041 yaz_log (YLOG_LOG, "delete %s %s %ld", recordType,
1042 pr_fname, (long) recordOffset);
1043 yaz_log (YLOG_WARN, "cannot delete record above (seems new)");
1047 yaz_log (YLOG_LOG, "add %s %s %ld", recordType, pr_fname,
1048 (long) recordOffset);
1049 rec = rec_new (zh->reg->records);
1051 *sysno = rec->sysno;
1053 recordAttr = rec_init_attr (zh->reg->zei, rec);
1054 recordAttr->staticrank = extractCtrl.staticrank;
1058 int db_ord = zebraExplain_get_database_ord(zh->reg->zei);
1059 dict_insert_ord(zh->reg->matchDict, db_ord, matchStr,
1060 sizeof(*sysno), sysno);
1064 extract_flushSortKeys (zh, *sysno, 1, zh->reg->sortKeys);
1065 extract_flushRecordKeys (zh, *sysno, 1, zh->reg->keys,
1066 recordAttr->staticrank);
1067 zh->records_inserted++;
1071 /* record already exists */
1072 zebra_rec_keys_t delkeys = zebra_rec_keys_open();
1073 zebra_rec_keys_t sortKeys = zebra_rec_keys_open();
1076 yaz_log (YLOG_LOG, "skipped %s %s %ld",
1077 recordType, pr_fname, (long) recordOffset);
1082 rec = rec_get (zh->reg->records, *sysno);
1085 recordAttr = rec_init_attr (zh->reg->zei, rec);
1087 zebra_rec_keys_set_buf(delkeys,
1088 rec->info[recInfo_delKeys],
1089 rec->size[recInfo_delKeys],
1091 zebra_rec_keys_set_buf(sortKeys,
1092 rec->info[recInfo_sortKeys],
1093 rec->size[recInfo_sortKeys],
1096 extract_flushSortKeys (zh, *sysno, 0, sortKeys);
1097 extract_flushRecordKeys (zh, *sysno, 0, delkeys,
1098 recordAttr->staticrank);
1101 /* record going to be deleted */
1102 if (zebra_rec_keys_empty(delkeys))
1104 yaz_log(YLOG_LOG, "delete %s %s %ld", recordType,
1105 pr_fname, (long) recordOffset);
1106 yaz_log(YLOG_WARN, "cannot delete file above, "
1107 "storeKeys false (3)");
1112 yaz_log(YLOG_LOG, "delete %s %s %ld", recordType,
1113 pr_fname, (long) recordOffset);
1114 zh->records_deleted++;
1117 int db_ord = zebraExplain_get_database_ord(zh->reg->zei);
1118 dict_delete_ord(zh->reg->matchDict, db_ord, matchStr);
1120 rec_del (zh->reg->records, &rec);
1129 yaz_log(YLOG_LOG, "update %s %s %ld", recordType,
1130 pr_fname, (long) recordOffset);
1131 recordAttr->staticrank = extractCtrl.staticrank;
1132 extract_flushSortKeys (zh, *sysno, 1, zh->reg->sortKeys);
1133 extract_flushRecordKeys (zh, *sysno, 1, zh->reg->keys,
1134 recordAttr->staticrank);
1135 zh->records_updated++;
1137 zebra_rec_keys_close(delkeys);
1138 zebra_rec_keys_close(sortKeys);
1140 /* update file type */
1141 xfree (rec->info[recInfo_fileType]);
1142 rec->info[recInfo_fileType] =
1143 rec_strdup (recordType, &rec->size[recInfo_fileType]);
1145 /* update filename */
1146 xfree (rec->info[recInfo_filename]);
1147 rec->info[recInfo_filename] =
1148 rec_strdup (fname, &rec->size[recInfo_filename]);
1150 /* update delete keys */
1151 xfree (rec->info[recInfo_delKeys]);
1152 if (!zebra_rec_keys_empty(zh->reg->keys) && zh->m_store_keys == 1)
1154 zebra_rec_keys_get_buf(zh->reg->keys,
1155 &rec->info[recInfo_delKeys],
1156 &rec->size[recInfo_delKeys]);
1160 rec->info[recInfo_delKeys] = NULL;
1161 rec->size[recInfo_delKeys] = 0;
1163 /* update sort keys */
1164 xfree (rec->info[recInfo_sortKeys]);
1166 zebra_rec_keys_get_buf(zh->reg->sortKeys,
1167 &rec->info[recInfo_sortKeys],
1168 &rec->size[recInfo_sortKeys]);
1170 /* save file size of original record */
1171 zebraExplain_recordBytesIncrement (zh->reg->zei,
1172 - recordAttr->recordSize);
1174 recordAttr->recordSize = fi->file_moffset - recordOffset;
1175 if (!recordAttr->recordSize)
1176 recordAttr->recordSize = fi->file_max - recordOffset;
1178 recordAttr->recordSize = buf_size;
1180 zebraExplain_recordBytesIncrement (zh->reg->zei,
1181 recordAttr->recordSize);
1183 /* set run-number for this record */
1184 recordAttr->runNumber =
1185 zebraExplain_runNumberIncrement (zh->reg->zei, 0);
1187 /* update store data */
1188 xfree (rec->info[recInfo_storeData]);
1190 /* update store data */
1191 if (zh->store_data_buf)
1193 rec->size[recInfo_storeData] = zh->store_data_size;
1194 rec->info[recInfo_storeData] = zh->store_data_buf;
1195 zh->store_data_buf = 0;
1197 else if (zh->m_store_data)
1199 rec->size[recInfo_storeData] = recordAttr->recordSize;
1200 rec->info[recInfo_storeData] = (char *)
1201 xmalloc (recordAttr->recordSize);
1202 memcpy (rec->info[recInfo_storeData], buf, recordAttr->recordSize);
1206 rec->info[recInfo_storeData] = NULL;
1207 rec->size[recInfo_storeData] = 0;
1209 /* update database name */
1210 xfree (rec->info[recInfo_databaseName]);
1211 rec->info[recInfo_databaseName] =
1212 rec_strdup (zh->basenames[0], &rec->size[recInfo_databaseName]);
1215 recordAttr->recordOffset = recordOffset;
1217 /* commit this record */
1218 rec_put (zh->reg->records, &rec);
1223 ZEBRA_RES zebra_extract_explain(void *handle, Record rec, data1_node *n)
1225 ZebraHandle zh = (ZebraHandle) handle;
1226 struct recExtractCtrl extractCtrl;
1228 if (zebraExplain_curDatabase (zh->reg->zei,
1229 rec->info[recInfo_databaseName]))
1232 if (zebraExplain_newDatabase (zh->reg->zei,
1233 rec->info[recInfo_databaseName], 0))
1237 zebra_rec_keys_reset(zh->reg->keys);
1238 zebra_rec_keys_reset(zh->reg->sortKeys);
1240 extractCtrl.init = extract_init;
1241 extractCtrl.tokenAdd = extract_token_add;
1242 extractCtrl.schemaAdd = extract_schema_add;
1243 extractCtrl.dh = zh->reg->dh;
1245 init_extractCtrl(zh, &extractCtrl);
1247 extractCtrl.flagShowRecords = 0;
1248 extractCtrl.match_criteria[0] = '\0';
1249 extractCtrl.staticrank = 0;
1250 extractCtrl.handle = handle;
1251 extractCtrl.first_record = 1;
1253 extract_set_store_data_prepare(&extractCtrl);
1256 grs_extract_tree(&extractCtrl, n);
1258 if (rec->size[recInfo_delKeys])
1260 zebra_rec_keys_t delkeys = zebra_rec_keys_open();
1262 zebra_rec_keys_t sortkeys = zebra_rec_keys_open();
1264 zebra_rec_keys_set_buf(delkeys, rec->info[recInfo_delKeys],
1265 rec->size[recInfo_delKeys],
1267 extract_flushRecordKeys (zh, rec->sysno, 0, delkeys, 0);
1268 zebra_rec_keys_close(delkeys);
1270 zebra_rec_keys_set_buf(sortkeys, rec->info[recInfo_sortKeys],
1271 rec->size[recInfo_sortKeys],
1274 extract_flushSortKeys (zh, rec->sysno, 0, sortkeys);
1275 zebra_rec_keys_close(sortkeys);
1277 extract_flushRecordKeys (zh, rec->sysno, 1, zh->reg->keys, 0);
1278 extract_flushSortKeys (zh, rec->sysno, 1, zh->reg->sortKeys);
1280 xfree (rec->info[recInfo_delKeys]);
1281 zebra_rec_keys_get_buf(zh->reg->keys,
1282 &rec->info[recInfo_delKeys],
1283 &rec->size[recInfo_delKeys]);
1285 xfree (rec->info[recInfo_sortKeys]);
1286 zebra_rec_keys_get_buf(zh->reg->sortKeys,
1287 &rec->info[recInfo_sortKeys],
1288 &rec->size[recInfo_sortKeys]);
1292 void extract_rec_keys_adjust(ZebraHandle zh, int is_insert,
1293 zebra_rec_keys_t reckeys)
1295 ZebraExplainInfo zei = zh->reg->zei;
1299 struct ord_stat *next;
1302 if (zebra_rec_keys_rewind(reckeys))
1304 struct ord_stat *ord_list = 0;
1308 struct it_key key_in;
1309 while(zebra_rec_keys_read(reckeys, &str, &slen, &key_in))
1311 int ord = CAST_ZINT_TO_INT(key_in.mem[0]);
1313 for (p = ord_list; p ; p = p->next)
1321 p = xmalloc(sizeof(*p));
1332 struct ord_stat *p1 = p;
1335 zebraExplain_ord_adjust_occurrences(zei, p->ord, p->no, 1);
1337 zebraExplain_ord_adjust_occurrences(zei, p->ord, - p->no, -1);
1344 void extract_flushRecordKeys (ZebraHandle zh, SYSNO sysno,
1346 zebra_rec_keys_t reckeys,
1349 ZebraExplainInfo zei = zh->reg->zei;
1351 extract_rec_keys_adjust(zh, cmd, reckeys);
1353 if (!zh->reg->key_buf)
1355 int mem= 1024*1024* atoi( res_get_def( zh->res, "memmax", "8"));
1358 yaz_log(YLOG_WARN, "Invalid memory setting, using default 8 MB");
1361 /* FIXME: That "8" should be in a default settings include */
1362 /* not hard-coded here! -H */
1363 zh->reg->key_buf = (char**) xmalloc (mem);
1364 zh->reg->ptr_top = mem/sizeof(char*);
1366 zh->reg->key_buf_used = 0;
1367 zh->reg->key_file_no = 0;
1369 zebraExplain_recordCountIncrement (zei, cmd ? 1 : -1);
1371 if (zebra_rec_keys_rewind(reckeys))
1375 struct it_key key_in;
1376 while(zebra_rec_keys_read(reckeys, &str, &slen, &key_in))
1379 struct it_key key_out;
1380 zint *keyp = key_out.mem;
1382 assert(key_in.len == 4);
1384 /* check for buffer overflow */
1385 if (zh->reg->key_buf_used + 1024 >
1386 (zh->reg->ptr_top -zh->reg->ptr_i)*sizeof(char*))
1387 extract_flushWriteKeys (zh, 0);
1390 assert(zh->reg->ptr_i > 0);
1391 (zh->reg->key_buf)[zh->reg->ptr_top - zh->reg->ptr_i] =
1392 (char*)zh->reg->key_buf + zh->reg->key_buf_used;
1394 /* encode the ordinal value (field/use/attribute) .. */
1395 ch = CAST_ZINT_TO_INT(key_in.mem[0]);
1396 zh->reg->key_buf_used +=
1397 key_SU_encode(ch, (char*)zh->reg->key_buf +
1398 zh->reg->key_buf_used);
1400 /* copy the 0-terminated stuff from str to output */
1401 memcpy((char*)zh->reg->key_buf + zh->reg->key_buf_used, str, slen);
1402 zh->reg->key_buf_used += slen;
1403 ((char*)zh->reg->key_buf)[(zh->reg->key_buf_used)++] = '\0';
1405 /* the delete/insert indicator */
1406 ((char*)zh->reg->key_buf)[(zh->reg->key_buf_used)++] = cmd;
1408 if (zh->m_staticrank) /* rank config enabled ? */
1412 yaz_log(YLOG_WARN, "staticrank = %ld. Setting to 0",
1416 *keyp++ = staticrank;
1422 if (key_in.mem[1]) /* filter specified record ID */
1423 *keyp++ = key_in.mem[1];
1426 *keyp++ = key_in.mem[2]; /* section_id */
1427 *keyp++ = key_in.mem[3]; /* sequence .. */
1429 memcpy((char*)zh->reg->key_buf + zh->reg->key_buf_used,
1430 &key_out, sizeof(key_out));
1431 (zh->reg->key_buf_used) += sizeof(key_out);
1436 void extract_flushWriteKeys (ZebraHandle zh, int final)
1437 /* optimizing: if final=1, and no files written yet */
1438 /* push the keys directly to merge, sidestepping the */
1439 /* temp file altogether. Speeds small updates */
1442 char out_fname[200];
1444 struct encode_info encode_info;
1445 int ptr_i = zh->reg->ptr_i;
1450 if (!zh->reg->key_buf || ptr_i <= 0)
1452 yaz_log(log_level, " nothing to flush section=%d buf=%p i=%d",
1453 zh->reg->key_file_no, zh->reg->key_buf, ptr_i);
1457 (zh->reg->key_file_no)++;
1458 yaz_log (YLOG_LOG, "sorting section %d", (zh->reg->key_file_no));
1459 yaz_log(log_level, " sort_buff at %p n=%d",
1460 zh->reg->key_buf + zh->reg->ptr_top - ptr_i,ptr_i);
1462 qsort (zh->reg->key_buf + zh->reg->ptr_top - ptr_i, ptr_i,
1463 sizeof(char*), key_qsort_compare);
1465 /* zebra.cfg: tempfiles:
1466 Y: always use temp files (old way)
1467 A: use temp files, if more than one (auto)
1468 = if this is both the last and the first
1469 N: never bother with temp files (new) */
1471 temp_policy=toupper(res_get_def(zh->res,"tempfiles","auto")[0]);
1472 if (temp_policy != 'Y' && temp_policy != 'N' && temp_policy != 'A') {
1473 yaz_log (YLOG_WARN, "Illegal tempfiles setting '%c'. using 'Auto' ",
1478 if ( ( temp_policy =='N' ) || /* always from memory */
1479 ( ( temp_policy =='A' ) && /* automatic */
1480 (zh->reg->key_file_no == 1) && /* this is first time */
1481 (final) ) ) /* and last (=only) time */
1482 { /* go directly from memory */
1483 zh->reg->key_file_no =0; /* signal not to read files */
1484 zebra_index_merge(zh);
1486 zh->reg->key_buf_used = 0;
1490 /* Not doing directly from memory, write into a temp file */
1491 extract_get_fname_tmp (zh, out_fname, zh->reg->key_file_no);
1493 if (!(outf = fopen (out_fname, "wb")))
1495 yaz_log (YLOG_FATAL|YLOG_ERRNO, "fopen %s", out_fname);
1498 yaz_log (YLOG_LOG, "writing section %d", zh->reg->key_file_no);
1499 prevcp = cp = (zh->reg->key_buf)[zh->reg->ptr_top - ptr_i];
1501 encode_key_init (&encode_info);
1502 encode_key_write (cp, &encode_info, outf);
1506 cp = (zh->reg->key_buf)[zh->reg->ptr_top - ptr_i];
1507 if (strcmp (cp, prevcp))
1509 encode_key_flush ( &encode_info, outf);
1510 encode_key_init (&encode_info);
1511 encode_key_write (cp, &encode_info, outf);
1515 encode_key_write (cp + strlen(cp), &encode_info, outf);
1517 encode_key_flush ( &encode_info, outf);
1519 qsort (key_buf + ptr_top-ptr_i, ptr_i, sizeof(char*), key_x_compare);
1520 extract_get_fname_tmp (out_fname, key_file_no);
1522 if (!(outf = fopen (out_fname, "wb")))
1524 yaz_log (YLOG_FATAL|YLOG_ERRNO, "fopen %s", out_fname);
1527 yaz_log (YLOG_LOG, "writing section %d", key_file_no);
1529 prevcp = key_buf[ptr_top-i];
1531 if (!--i || strcmp (prevcp, key_buf[ptr_top-i]))
1533 key_y_len = strlen(prevcp)+1;
1535 yaz_log (YLOG_LOG, "key_y_len: %2d %02x %02x %s",
1536 key_y_len, prevcp[0], prevcp[1], 2+prevcp);
1538 qsort (key_buf + ptr_top-ptr_i, ptr_i - i,
1539 sizeof(char*), key_y_compare);
1540 cp = key_buf[ptr_top-ptr_i];
1542 encode_key_init (&encode_info);
1543 encode_key_write (cp, &encode_info, outf);
1546 cp = key_buf[ptr_top-ptr_i];
1547 encode_key_write (cp+key_y_len, &encode_info, outf);
1549 encode_key_flush ( &encode_info, outf);
1552 prevcp = key_buf[ptr_top-ptr_i];
1557 yaz_log (YLOG_FATAL|YLOG_ERRNO, "fclose %s", out_fname);
1560 yaz_log (YLOG_LOG, "finished section %d", zh->reg->key_file_no);
1562 zh->reg->key_buf_used = 0;
1565 ZEBRA_RES zebra_snippets_rec_keys(ZebraHandle zh,
1566 zebra_rec_keys_t reckeys,
1567 zebra_snippets *snippets)
1569 NMEM nmem = nmem_create();
1570 if (zebra_rec_keys_rewind(reckeys))
1575 while (zebra_rec_keys_read(reckeys, &str, &slen, &key))
1577 char dst_buf[IT_MAX_WORD];
1578 char *dst_term = dst_buf;
1583 assert(key.len <= 4 && key.len > 2);
1584 seqno = key.mem[key.len-1];
1585 ord = CAST_ZINT_TO_INT(key.mem[0]);
1587 zebraExplain_lookup_ord(zh->reg->zei, ord, &index_type,
1588 0/* db */, 0 /* string_index */);
1590 zebra_term_untrans_iconv(zh, nmem, index_type,
1592 zebra_snippets_append(snippets, seqno, ord, dst_term);
1600 void print_rec_keys(ZebraHandle zh, zebra_rec_keys_t reckeys)
1602 yaz_log(YLOG_LOG, "print_rec_keys");
1603 if (zebra_rec_keys_rewind(reckeys))
1608 while (zebra_rec_keys_read(reckeys, &str, &slen, &key))
1610 char dst_buf[IT_MAX_WORD];
1613 int ord = CAST_ZINT_TO_INT(key.mem[0]);
1615 assert(key.len <= 4 && key.len > 2);
1617 zebraExplain_lookup_ord(zh->reg->zei, ord, &index_type, &db, 0);
1619 seqno = key.mem[key.len-1];
1621 zebra_term_untrans(zh, index_type, dst_buf, str);
1623 yaz_log(YLOG_LOG, "ord=%d seqno=" ZINT_FORMAT
1624 " term=%s", ord, seqno, dst_buf);
1629 static void extract_add_index_string(RecWord *p, zinfo_index_category_t cat,
1630 const char *str, int length)
1634 ZebraHandle zh = p->extractCtrl->handle;
1635 ZebraExplainInfo zei = zh->reg->zei;
1641 ch = zebraExplain_lookup_attr_str(zei, cat, p->index_type, p->index_name);
1643 ch = zebraExplain_add_attr_str(zei, cat, p->index_type, p->index_name);
1647 key.mem[1] = p->record_id;
1648 key.mem[2] = p->section_id;
1649 key.mem[3] = p->seqno;
1658 for (i = 0; i<length && i < 20; i++)
1659 sprintf(strz+strlen(strz), "%02X", str[i] & 0xff);
1660 /* just for debugging .. */
1661 yaz_log(YLOG_LOG, "add: set=%d use=%d "
1662 "record_id=%lld section_id=%lld seqno=%lld %s",
1663 p->attrSet, p->attrUse, p->record_id, p->section_id, p->seqno,
1668 zebra_rec_keys_write(zh->reg->keys, str, length, &key);
1671 static void extract_add_sort_string(RecWord *p, const char *str, int length)
1675 ZebraHandle zh = p->extractCtrl->handle;
1676 ZebraExplainInfo zei = zh->reg->zei;
1678 zinfo_index_category_t cat = zinfo_index_category_sort;
1684 ch = zebraExplain_lookup_attr_str(zei, cat, p->index_type, p->index_name);
1686 ch = zebraExplain_add_attr_str(zei, cat, p->index_type, p->index_name);
1689 key.mem[1] = p->record_id;
1690 key.mem[2] = p->section_id;
1691 key.mem[3] = p->seqno;
1700 for (i = 0; i<length && i < 20; i++)
1701 sprintf(strz+strlen(strz), "%02X", str[i] & 0xff);
1702 /* just for debugging .. */
1703 yaz_log(YLOG_LOG, "add: set=%d use=%d "
1704 "record_id=%lld section_id=%lld seqno=%lld %s",
1705 p->attrSet, p->attrUse, p->record_id, p->section_id, p->seqno,
1709 zebra_rec_keys_write(zh->reg->sortKeys, str, length, &key);
1712 static void extract_add_string (RecWord *p, const char *string, int length)
1714 assert (length > 0);
1715 if (zebra_maps_is_sort (p->zebra_maps, p->index_type))
1716 extract_add_sort_string (p, string, length);
1719 extract_add_index_string(p, zinfo_index_category_index,
1721 if (zebra_maps_is_alwaysmatches(p->zebra_maps, p->index_type))
1724 memcpy(&word, p, sizeof(word));
1727 extract_add_index_string(
1728 &word, zinfo_index_category_alwaysmatches, "", 0);
1733 static void extract_add_incomplete_field (RecWord *p)
1735 const char *b = p->term_buf;
1736 int remain = p->term_len;
1737 const char **map = 0;
1740 map = zebra_maps_input(p->zebra_maps, p->index_type, &b, remain, 0);
1744 char buf[IT_MAX_WORD+1];
1748 while (map && *map && **map == *CHR_SPACE)
1750 remain = p->term_len - (b - p->term_buf);
1752 map = zebra_maps_input(p->zebra_maps, p->index_type, &b,
1760 while (map && *map && **map != *CHR_SPACE)
1762 const char *cp = *map;
1764 while (i < IT_MAX_WORD && *cp)
1766 remain = p->term_len - (b - p->term_buf);
1768 map = zebra_maps_input(p->zebra_maps, p->index_type, &b, remain, 0);
1774 extract_add_string (p, buf, i);
1779 static void extract_add_complete_field (RecWord *p)
1781 const char *b = p->term_buf;
1782 char buf[IT_MAX_WORD+1];
1783 const char **map = 0;
1784 int i = 0, remain = p->term_len;
1787 map = zebra_maps_input (p->zebra_maps, p->index_type, &b, remain, 1);
1789 while (remain > 0 && i < IT_MAX_WORD)
1791 while (map && *map && **map == *CHR_SPACE)
1793 remain = p->term_len - (b - p->term_buf);
1797 int first = i ? 0 : 1; /* first position */
1798 map = zebra_maps_input(p->zebra_maps, p->index_type, &b, remain, first);
1806 if (i && i < IT_MAX_WORD)
1807 buf[i++] = *CHR_SPACE;
1808 while (map && *map && **map != *CHR_SPACE)
1810 const char *cp = *map;
1812 if (**map == *CHR_CUT)
1818 if (i >= IT_MAX_WORD)
1820 while (i < IT_MAX_WORD && *cp)
1823 remain = p->term_len - (b - p->term_buf);
1826 map = zebra_maps_input (p->zebra_maps, p->index_type, &b,
1835 extract_add_string (p, buf, i);
1838 static void extract_token_add(RecWord *p)
1842 yaz_log(log_level, "extract_token_add "
1843 "type=%c index=%s seqno=" ZINT_FORMAT " s=%.*s",
1844 p->index_type, p->index_name,
1845 p->seqno, p->term_len, p->term_buf);
1846 if ((wrbuf = zebra_replace(p->zebra_maps, p->index_type, 0,
1847 p->term_buf, p->term_len)))
1849 p->term_buf = wrbuf_buf(wrbuf);
1850 p->term_len = wrbuf_len(wrbuf);
1852 if (zebra_maps_is_complete (p->zebra_maps, p->index_type))
1853 extract_add_complete_field (p);
1855 extract_add_incomplete_field(p);
1858 static void extract_set_store_data_cb(struct recExtractCtrl *p,
1859 void *buf, size_t sz)
1861 ZebraHandle zh = (ZebraHandle) p->handle;
1863 xfree(zh->store_data_buf);
1864 zh->store_data_buf = 0;
1865 zh->store_data_size = 0;
1868 zh->store_data_buf = xmalloc(sz);
1869 zh->store_data_size = sz;
1870 memcpy(zh->store_data_buf, buf, sz);
1874 static void extract_set_store_data_prepare(struct recExtractCtrl *p)
1876 ZebraHandle zh = (ZebraHandle) p->handle;
1877 xfree(zh->store_data_buf);
1878 zh->store_data_buf = 0;
1879 zh->store_data_size = 0;
1880 p->setStoreData = extract_set_store_data_cb;
1883 static void extract_schema_add (struct recExtractCtrl *p, Odr_oid *oid)
1885 ZebraHandle zh = (ZebraHandle) p->handle;
1886 zebraExplain_addSchema (zh->reg->zei, oid);
1889 void extract_flushSortKeys (ZebraHandle zh, SYSNO sysno,
1890 int cmd, zebra_rec_keys_t reckeys)
1892 if (zebra_rec_keys_rewind(reckeys))
1894 SortIdx sortIdx = zh->reg->sortIdx;
1897 struct it_key key_in;
1899 sortIdx_sysno (sortIdx, sysno);
1901 while (zebra_rec_keys_read(reckeys, &str, &slen, &key_in))
1903 int ord = CAST_ZINT_TO_INT(key_in.mem[0]);
1905 sortIdx_type(sortIdx, ord);
1907 sortIdx_add(sortIdx, str, slen);
1909 sortIdx_add(sortIdx, "", 1);
1914 static void encode_key_init(struct encode_info *i)
1916 i->encode_handle = iscz1_start();
1917 i->decode_handle = iscz1_start();
1920 static void encode_key_write (char *k, struct encode_info *i, FILE *outf)
1923 char *bp = i->buf, *bp0;
1924 const char *src = (char *) &key;
1926 /* copy term to output buf */
1927 while ((*bp++ = *k++))
1929 /* and copy & align key so we can mangle */
1930 memcpy (&key, k+1, sizeof(struct it_key)); /* *k is insert/delete */
1934 key_logdump_txt(YLOG_LOG, &key, *k ? "i" : "d");
1936 assert(key.mem[0] >= 0);
1939 iscz1_encode(i->encode_handle, &bp, &src);
1941 *bp0 = (*k * 128) + bp - bp0 - 1; /* length and insert/delete combined */
1942 if (fwrite (i->buf, bp - i->buf, 1, outf) != 1)
1944 yaz_log (YLOG_FATAL|YLOG_ERRNO, "fwrite");
1953 const char *src = bp0+1;
1954 char *dst = (char*) &key2;
1955 iscz1_decode(i->decode_handle, &dst, &src);
1957 key_logdump_txt(YLOG_LOG, &key2, *k ? "i" : "d");
1959 assert(key2.mem[1]);
1964 static void encode_key_flush (struct encode_info *i, FILE *outf)
1966 iscz1_stop(i->encode_handle);
1967 iscz1_stop(i->decode_handle);
1973 * indent-tabs-mode: nil
1975 * vim: shiftwidth=4 tabstop=8 expandtab