1 /* $Id: extract.c,v 1.132 2002-11-26 22:18:34 adam Exp $
2 Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002
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
37 #if _FILE_OFFSET_BITS == 64
38 #define PRINTF_OFF_T "%Ld"
40 #define PRINTF_OFF_T "%ld"
43 #define USE_SHELLSORT 0
46 static void shellsort(void *ar, int r, size_t s,
47 int (*cmp)(const void *a, const void *b))
52 static const int incs[16] = { 1391376, 463792, 198768, 86961, 33936,
53 13776, 4592, 1968, 861, 336,
54 112, 48, 21, 7, 3, 1 };
55 for ( k = 0; k < 16; k++)
56 for (h = incs[k], i = h; i < r; i++)
60 while (j > h && (*cmp)(a + s*(j-h), v) > 0)
62 memcpy (a + s*j, a + s*(j-h), s);
70 static void logRecord (ZebraHandle zh)
72 ++zh->records_processed;
73 if (!(zh->records_processed % 1000))
75 logf (LOG_LOG, "Records: %7d i/u/d %d/%d/%d",
76 zh->records_processed, zh->records_inserted, zh->records_updated,
81 static void extract_init (struct recExtractCtrl *p, RecWord *w)
83 w->zebra_maps = p->zebra_maps;
85 w->attrSet = VAL_BIB1;
91 static const char **searchRecordKey (ZebraHandle zh,
92 struct recKeys *reckeys,
93 int attrSetS, int attrUseS)
95 static const char *ws[32];
107 for (i = 0; i<32; i++)
111 chS = zebraExplain_lookupSU (zh->reg->zei, attrSetS, attrUseS);
115 while (off < reckeys->buf_used)
118 const char *src = reckeys->buf + off;
126 memcpy (&ch, src, sizeof(ch));
132 memcpy (&attrSet, src, sizeof(attrSet));
133 src += sizeof(attrSet);
137 memcpy (&attrUse, src, sizeof(attrUse));
138 src += sizeof(attrUse);
145 seqno += ((lead>>2) & 15)-1;
148 memcpy (&seqno, src, sizeof(seqno));
149 src += sizeof(seqno);
155 attrUseS == attrUse && attrSetS == attrSet
164 woff = seqno - startSeq;
165 if (woff >= 0 && woff < 31)
169 off = src - reckeys->buf;
171 assert (off == reckeys->buf_used);
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 */
185 static struct file_read_info *file_read_start (int fd)
187 struct file_read_info *fi = (struct file_read_info *)
188 xmalloc (sizeof(*fi));
192 fi->file_moffset = 0;
198 static void file_read_stop (struct file_read_info *fi)
203 static off_t file_seek (void *handle, off_t offset)
205 struct file_read_info *p = (struct file_read_info *) handle;
206 p->file_offset = offset;
209 return lseek (p->fd, offset, SEEK_SET);
212 static off_t file_tell (void *handle)
214 struct file_read_info *p = (struct file_read_info *) handle;
215 return p->file_offset;
218 static int file_read (void *handle, char *buf, size_t count)
220 struct file_read_info *p = (struct file_read_info *) handle;
226 if (r > p->sdrmax - p->file_offset)
227 r = p->sdrmax - p->file_offset;
229 memcpy (buf, p->sdrbuf + p->file_offset, r);
232 r = read (fd, buf, count);
236 if (p->file_offset > p->file_max)
237 p->file_max = p->file_offset;
242 static void file_begin (void *handle)
244 struct file_read_info *p = (struct file_read_info *) handle;
246 p->file_offset = p->file_moffset;
247 if (!p->sdrbuf && p->file_moffset)
248 lseek (p->fd, p->file_moffset, SEEK_SET);
252 static void file_end (void *handle, off_t offset)
254 struct file_read_info *p = (struct file_read_info *) handle;
256 assert (p->file_more == 0);
258 p->file_moffset = offset;
261 static char *fileMatchStr (ZebraHandle zh,
262 struct recKeys *reckeys, struct recordGroup *rGroup,
263 const char *fname, const char *spec)
265 static char dstBuf[2048]; /* static here ??? */
267 const char *s = spec;
268 static const char **w;
272 while (*s == ' ' || *s == '\t')
278 char attset_str[64], attname_str[64];
279 data1_attset *attset;
282 int attSet = 1, attUse = 1;
286 for (i = 0; *s && *s != ',' && *s != ')'; s++)
288 attset_str[i++] = *s;
289 attset_str[i] = '\0';
294 for (i = 0; *s && *s != ')'; s++)
296 attname_str[i++] = *s;
297 attname_str[i] = '\0';
300 if ((attset = data1_get_attset (zh->reg->dh, attset_str)))
303 attSet = attset->reference;
304 att = data1_getattbyname(zh->reg->dh, attset, attname_str);
308 attUse = atoi (attname_str);
310 w = searchRecordKey (zh, reckeys, attSet, attUse);
315 for (i = 0; i<32; i++)
320 logf (LOG_WARN, "Missing ) in match criteria %s in group %s",
321 spec, rGroup->groupName ? rGroup->groupName : "none");
326 for (i = 0; i<32; i++)
327 if (matchFlag[i] && w[i])
339 logf (LOG_WARN, "Record didn't contain match"
340 " fields in (%s,%s)", attset_str, attname_str);
348 const char *spec_src = NULL;
349 const char *s1 = ++s;
350 while (*s1 && *s1 != ' ' && *s1 != '\t')
356 memcpy (special, s, spec_len);
357 special[spec_len] = '\0';
360 if (!strcmp (special, "group"))
361 spec_src = rGroup->groupName;
362 else if (!strcmp (special, "database"))
363 spec_src = rGroup->databaseName;
364 else if (!strcmp (special, "filename")) {
367 else if (!strcmp (special, "type"))
368 spec_src = rGroup->recordType;
373 strcpy (dst, spec_src);
374 dst += strlen (spec_src);
377 else if (*s == '\"' || *s == '\'')
379 int stopMarker = *s++;
383 while (*s && *s != stopMarker)
386 tmpString[i++] = *s++;
391 strcpy (dst, tmpString);
392 dst += strlen (tmpString);
396 logf (LOG_WARN, "Syntax error in match criteria %s in group %s",
397 spec, rGroup->groupName ? rGroup->groupName : "none");
404 logf (LOG_WARN, "No match criteria for record %s in group %s",
405 fname, rGroup->groupName ? rGroup->groupName : "none");
412 struct recordLogInfo {
415 struct recordGroup *rGroup;
418 static int recordExtract (ZebraHandle zh,
419 SYSNO *sysno, const char *fname,
420 struct recordGroup *rGroup, int deleteFlag,
421 struct file_read_info *fi,
422 RecType recType, char *subType, void *clientData)
424 RecordAttr *recordAttr;
429 off_t recordOffset = 0;
433 struct recExtractCtrl extractCtrl;
435 /* we are going to read from a file, so prepare the extraction */
438 zh->reg->keys.buf_used = 0;
439 zh->reg->keys.prevAttrUse = -1;
440 zh->reg->keys.prevAttrSet = -1;
441 zh->reg->keys.prevSeqNo = 0;
442 zh->reg->sortKeys.buf_used = 0;
443 zh->reg->sortKeys.buf_max = 0;
444 zh->reg->sortKeys.buf = 0;
446 recordOffset = fi->file_moffset;
447 extractCtrl.offset = fi->file_moffset;
448 extractCtrl.readf = file_read;
449 extractCtrl.seekf = file_seek;
450 extractCtrl.tellf = file_tell;
451 extractCtrl.endf = file_end;
453 extractCtrl.subType = subType;
454 extractCtrl.init = extract_init;
455 extractCtrl.tokenAdd = extract_token_add;
456 extractCtrl.schemaAdd = extract_schema_add;
457 extractCtrl.dh = zh->reg->dh;
458 extractCtrl.handle = zh;
459 for (i = 0; i<256; i++)
461 if (zebra_maps_is_positioned(zh->reg->zebra_maps, i))
462 extractCtrl.seqno[i] = 1;
464 extractCtrl.seqno[i] = 0;
466 extractCtrl.zebra_maps = zh->reg->zebra_maps;
467 extractCtrl.flagShowRecords = !rGroup->flagRw;
470 printf ("File: %s " PRINTF_OFF_T "\n", fname, recordOffset);
474 sprintf (msg, "%s:" PRINTF_OFF_T , fname, recordOffset);
475 yaz_log_init_prefix2 (msg);
478 r = (*recType->extract)(clientData, &extractCtrl);
480 yaz_log_init_prefix2 (0);
481 if (r == RECCTRL_EXTRACT_EOF)
483 else if (r == RECCTRL_EXTRACT_ERROR_GENERIC)
485 /* error occured during extraction ... */
486 if (rGroup->flagRw &&
487 zh->records_processed < rGroup->fileVerboseLimit)
489 logf (LOG_WARN, "fail %s %s " PRINTF_OFF_T, rGroup->recordType,
490 fname, recordOffset);
494 else if (r == RECCTRL_EXTRACT_ERROR_NO_SUCH_FILTER)
496 /* error occured during extraction ... */
497 if (rGroup->flagRw &&
498 zh->records_processed < rGroup->fileVerboseLimit)
500 logf (LOG_WARN, "no filter for %s %s "
501 PRINTF_OFF_T, rGroup->recordType,
502 fname, recordOffset);
506 if (zh->reg->keys.buf_used == 0)
508 /* the extraction process returned no information - the record
509 is probably empty - unless flagShowRecords is in use */
513 logf (LOG_WARN, "empty %s %s " PRINTF_OFF_T, rGroup->recordType,
514 fname, recordOffset);
519 /* perform match if sysno not known and if match criteria is specified */
526 if (rGroup->recordId && *rGroup->recordId)
530 matchStr = fileMatchStr (zh, &zh->reg->keys, rGroup, fname,
534 rinfo = dict_lookup (zh->reg->matchDict, matchStr);
536 memcpy (sysno, rinfo+1, sizeof(*sysno));
540 logf (LOG_WARN, "Bad match criteria");
551 logf (LOG_LOG, "delete %s %s " PRINTF_OFF_T, rGroup->recordType,
552 fname, recordOffset);
553 logf (LOG_WARN, "cannot delete record above (seems new)");
556 if (zh->records_processed < rGroup->fileVerboseLimit)
557 logf (LOG_LOG, "add %s %s " PRINTF_OFF_T, rGroup->recordType,
558 fname, recordOffset);
559 rec = rec_new (zh->reg->records);
563 recordAttr = rec_init_attr (zh->reg->zei, rec);
567 dict_insert (zh->reg->matchDict, matchStr, sizeof(*sysno), sysno);
569 extract_flushSortKeys (zh, *sysno, 1, &zh->reg->sortKeys);
570 extract_flushRecordKeys (zh, *sysno, 1, &zh->reg->keys);
572 zh->records_inserted++;
576 /* record already exists */
577 struct recKeys delkeys;
578 struct sortKeys sortKeys;
580 rec = rec_get (zh->reg->records, *sysno);
583 recordAttr = rec_init_attr (zh->reg->zei, rec);
585 if (recordAttr->runNumber ==
586 zebraExplain_runNumberIncrement (zh->reg->zei, 0))
588 yaz_log (LOG_LOG, "run number = %d", recordAttr->runNumber);
589 yaz_log (LOG_LOG, "skipped %s %s " PRINTF_OFF_T,
590 rGroup->recordType, fname, recordOffset);
591 extract_flushSortKeys (zh, *sysno, -1, &zh->reg->sortKeys);
596 delkeys.buf_used = rec->size[recInfo_delKeys];
597 delkeys.buf = rec->info[recInfo_delKeys];
599 sortKeys.buf_used = rec->size[recInfo_sortKeys];
600 sortKeys.buf = rec->info[recInfo_sortKeys];
602 extract_flushSortKeys (zh, *sysno, 0, &sortKeys);
603 extract_flushRecordKeys (zh, *sysno, 0, &delkeys);
606 /* record going to be deleted */
607 if (!delkeys.buf_used)
609 logf (LOG_LOG, "delete %s %s " PRINTF_OFF_T,
610 rGroup->recordType, fname, recordOffset);
611 logf (LOG_WARN, "cannot delete file above, storeKeys false");
615 if (zh->records_processed < rGroup->fileVerboseLimit)
616 logf (LOG_LOG, "delete %s %s " PRINTF_OFF_T,
617 rGroup->recordType, fname, recordOffset);
618 zh->records_deleted++;
620 dict_delete (zh->reg->matchDict, matchStr);
621 rec_del (zh->reg->records, &rec);
629 /* record going to be updated */
630 if (!delkeys.buf_used)
632 logf (LOG_LOG, "update %s %s " PRINTF_OFF_T,
633 rGroup->recordType, fname, recordOffset);
634 logf (LOG_WARN, "cannot update file above, storeKeys false");
638 if (zh->records_processed < rGroup->fileVerboseLimit)
639 logf (LOG_LOG, "update %s %s " PRINTF_OFF_T,
640 rGroup->recordType, fname, recordOffset);
641 extract_flushSortKeys (zh, *sysno, 1, &zh->reg->sortKeys);
642 extract_flushRecordKeys (zh, *sysno, 1, &zh->reg->keys);
643 zh->records_updated++;
647 /* update file type */
648 xfree (rec->info[recInfo_fileType]);
649 rec->info[recInfo_fileType] =
650 rec_strdup (rGroup->recordType, &rec->size[recInfo_fileType]);
652 /* update filename */
653 xfree (rec->info[recInfo_filename]);
654 rec->info[recInfo_filename] =
655 rec_strdup (fname, &rec->size[recInfo_filename]);
657 /* update delete keys */
658 xfree (rec->info[recInfo_delKeys]);
659 if (zh->reg->keys.buf_used > 0 && rGroup->flagStoreKeys == 1)
661 rec->size[recInfo_delKeys] = zh->reg->keys.buf_used;
662 rec->info[recInfo_delKeys] = zh->reg->keys.buf;
663 zh->reg->keys.buf = NULL;
664 zh->reg->keys.buf_max = 0;
668 rec->info[recInfo_delKeys] = NULL;
669 rec->size[recInfo_delKeys] = 0;
672 /* update sort keys */
673 xfree (rec->info[recInfo_sortKeys]);
675 rec->size[recInfo_sortKeys] = zh->reg->sortKeys.buf_used;
676 rec->info[recInfo_sortKeys] = zh->reg->sortKeys.buf;
677 zh->reg->sortKeys.buf = NULL;
678 zh->reg->sortKeys.buf_max = 0;
680 /* save file size of original record */
681 zebraExplain_recordBytesIncrement (zh->reg->zei,
682 - recordAttr->recordSize);
683 recordAttr->recordSize = fi->file_moffset - recordOffset;
684 if (!recordAttr->recordSize)
685 recordAttr->recordSize = fi->file_max - recordOffset;
686 zebraExplain_recordBytesIncrement (zh->reg->zei,
687 recordAttr->recordSize);
689 /* set run-number for this record */
690 recordAttr->runNumber = zebraExplain_runNumberIncrement (zh->reg->zei,
693 /* update store data */
694 xfree (rec->info[recInfo_storeData]);
695 if (rGroup->flagStoreData == 1)
697 rec->size[recInfo_storeData] = recordAttr->recordSize;
698 rec->info[recInfo_storeData] = (char *)
699 xmalloc (recordAttr->recordSize);
700 if (lseek (fi->fd, recordOffset, SEEK_SET) < 0)
702 logf (LOG_ERRNO|LOG_FATAL, "seek to " PRINTF_OFF_T " in %s",
703 recordOffset, fname);
706 if (read (fi->fd, rec->info[recInfo_storeData], recordAttr->recordSize)
707 < recordAttr->recordSize)
709 logf (LOG_ERRNO|LOG_FATAL, "read %d bytes of %s",
710 recordAttr->recordSize, fname);
716 rec->info[recInfo_storeData] = NULL;
717 rec->size[recInfo_storeData] = 0;
719 /* update database name */
720 xfree (rec->info[recInfo_databaseName]);
721 rec->info[recInfo_databaseName] =
722 rec_strdup (rGroup->databaseName, &rec->size[recInfo_databaseName]);
725 recordAttr->recordOffset = recordOffset;
727 /* commit this record */
728 rec_put (zh->reg->records, &rec);
733 int fileExtract (ZebraHandle zh, SYSNO *sysno, const char *fname,
734 const struct recordGroup *rGroupP, int deleteFlag)
742 struct recordGroup rGroupM;
743 struct recordGroup *rGroup = &rGroupM;
744 struct file_read_info *fi;
747 memcpy (rGroup, rGroupP, sizeof(*rGroupP));
749 if (!rGroup->groupName || !*rGroup->groupName)
752 sprintf (gprefix, "%s.", rGroup->groupName);
754 logf (LOG_DEBUG, "fileExtract %s", fname);
756 /* determine file extension */
758 for (i = strlen(fname); --i >= 0; )
761 else if (fname[i] == '.')
763 strcpy (ext, fname+i+1);
766 /* determine file type - depending on extension */
767 if (!rGroup->recordType)
769 sprintf (ext_res, "%srecordType.%s", gprefix, ext);
770 if (!(rGroup->recordType = res_get (zh->res, ext_res)))
772 sprintf (ext_res, "%srecordType", gprefix);
773 rGroup->recordType = res_get (zh->res, ext_res);
776 if (!rGroup->recordType)
778 if (zh->records_processed < rGroup->fileVerboseLimit)
779 logf (LOG_LOG, "? %s", fname);
782 if (!*rGroup->recordType)
785 recType_byName (zh->reg->recTypes, rGroup->recordType, subType,
788 logf (LOG_WARN, "No such record type: %s", rGroup->recordType);
792 /* determine match criteria */
793 if (!rGroup->recordId)
795 sprintf (ext_res, "%srecordId.%s", gprefix, ext);
796 rGroup->recordId = res_get (zh->res, ext_res);
799 /* determine database name */
800 if (!rGroup->databaseName)
802 sprintf (ext_res, "%sdatabase.%s", gprefix, ext);
803 if (!(rGroup->databaseName = res_get (zh->res, ext_res)))
805 sprintf (ext_res, "%sdatabase", gprefix);
806 rGroup->databaseName = res_get (zh->res, ext_res);
809 if (!rGroup->databaseName)
810 rGroup->databaseName = "Default";
812 /* determine if explain database */
814 sprintf (ext_res, "%sexplainDatabase", gprefix);
815 rGroup->explainDatabase =
816 atoi (res_get_def (zh->res, ext_res, "0"));
818 /* announce database */
819 if (zebraExplain_curDatabase (zh->reg->zei, rGroup->databaseName))
821 if (zebraExplain_newDatabase (zh->reg->zei, rGroup->databaseName,
822 rGroup->explainDatabase))
826 if (rGroup->flagStoreData == -1)
829 sprintf (ext_res, "%sstoreData.%s", gprefix, ext);
830 if (!(sval = res_get (zh->res, ext_res)))
832 sprintf (ext_res, "%sstoreData", gprefix);
833 sval = res_get (zh->res, ext_res);
836 rGroup->flagStoreData = atoi (sval);
838 if (rGroup->flagStoreData == -1)
839 rGroup->flagStoreData = 0;
841 if (rGroup->flagStoreKeys == -1)
845 sprintf (ext_res, "%sstoreKeys.%s", gprefix, ext);
846 sval = res_get (zh->res, ext_res);
849 sprintf (ext_res, "%sstoreKeys", gprefix);
850 sval = res_get (zh->res, ext_res);
853 sval = res_get (zh->res, "storeKeys");
855 rGroup->flagStoreKeys = atoi (sval);
857 if (rGroup->flagStoreKeys == -1)
858 rGroup->flagStoreKeys = 0;
860 if (sysno && deleteFlag)
866 if (zh->path_reg && !yaz_is_abspath (fname))
868 strcpy (full_rep, zh->path_reg);
869 strcat (full_rep, "/");
870 strcat (full_rep, fname);
873 strcpy (full_rep, fname);
876 if ((fd = open (full_rep, O_BINARY|O_RDONLY)) == -1)
878 logf (LOG_WARN|LOG_ERRNO, "open %s", full_rep);
882 fi = file_read_start (fd);
886 r = recordExtract (zh, sysno, fname, rGroup, deleteFlag, fi,
887 recType, subType, clientData);
888 } while (r && !sysno && fi->file_more);
894 int extract_rec_in_mem (ZebraHandle zh, const char *recordType,
895 const char *buf, size_t buf_size,
896 const char *databaseName, int delete_flag,
897 int test_mode, int *sysno,
898 int store_keys, int store_data,
899 const char *match_criteria)
901 struct recordGroup rGroup;
902 rGroup.groupName = NULL;
903 rGroup.databaseName = (char *)databaseName;
905 rGroup.recordId = NULL;
906 rGroup.recordType = (char *)recordType;
907 rGroup.flagStoreData = store_data;
908 rGroup.flagStoreKeys = store_keys;
910 rGroup.databaseNamePath = 0;
911 rGroup.explainDatabase = 0;
912 rGroup.fileVerboseLimit = 100000;
913 rGroup.followLinks = -1;
914 return (bufferExtractRecord (zh,
924 If sysno is provided, then it's used to identify the reocord.
925 If not, and match_criteria is provided, then sysno is guessed
926 If not, and a record is provided, then sysno is got from there
929 int bufferExtractRecord (ZebraHandle zh,
930 const char *buf, size_t buf_size,
931 struct recordGroup *rGroup,
935 const char *match_criteria,
939 RecordAttr *recordAttr;
940 struct recExtractCtrl extractCtrl;
948 long recordOffset = 0;
949 struct zebra_fetch_control fc;
952 fc.record_int_buf = buf;
953 fc.record_int_len = buf_size;
954 fc.record_int_pos = 0;
956 fc.record_offset = 0;
958 extractCtrl.offset = 0;
959 extractCtrl.readf = zebra_record_int_read;
960 extractCtrl.seekf = zebra_record_int_seek;
961 extractCtrl.tellf = zebra_record_int_tell;
962 extractCtrl.endf = zebra_record_int_end;
963 extractCtrl.fh = &fc;
965 /* announce database */
966 if (zebraExplain_curDatabase (zh->reg->zei, rGroup->databaseName))
968 if (zebraExplain_newDatabase (zh->reg->zei, rGroup->databaseName, 0))
972 if (!(rGroup->recordType)) {
973 logf (LOG_WARN, "No such record type defined");
978 recType_byName (zh->reg->recTypes, rGroup->recordType, subType,
981 logf (LOG_WARN, "No such record type: %s", rGroup->recordType);
984 zh->reg->keys.buf_used = 0;
985 zh->reg->keys.prevAttrUse = -1;
986 zh->reg->keys.prevAttrSet = -1;
987 zh->reg->keys.prevSeqNo = 0;
988 zh->reg->sortKeys.buf_used = 0;
989 zh->reg->sortKeys.buf_max = 0;
990 zh->reg->sortKeys.buf = 0;
992 extractCtrl.subType = subType;
993 extractCtrl.init = extract_init;
994 extractCtrl.tokenAdd = extract_token_add;
995 extractCtrl.schemaAdd = extract_schema_add;
996 extractCtrl.dh = zh->reg->dh;
997 extractCtrl.handle = zh;
998 extractCtrl.zebra_maps = zh->reg->zebra_maps;
999 extractCtrl.flagShowRecords = 0;
1000 for (i = 0; i<256; i++)
1002 if (zebra_maps_is_positioned(zh->reg->zebra_maps, i))
1003 extractCtrl.seqno[i] = 1;
1005 extractCtrl.seqno[i] = 0;
1008 r = (*recType->extract)(clientData, &extractCtrl);
1010 if (r == RECCTRL_EXTRACT_EOF)
1012 else if (r == RECCTRL_EXTRACT_ERROR_GENERIC)
1014 /* error occured during extraction ... */
1015 yaz_log (LOG_WARN, "extract error: generic");
1018 else if (r == RECCTRL_EXTRACT_ERROR_NO_SUCH_FILTER)
1020 /* error occured during extraction ... */
1021 yaz_log (LOG_WARN, "extract error: no such filter");
1024 if (zh->reg->keys.buf_used == 0)
1026 /* the extraction process returned no information - the record
1027 is probably empty - unless flagShowRecords is in use */
1030 logf (LOG_WARN, "No keys generated for record");
1031 logf (LOG_WARN, " The file is probably empty");
1034 /* match criteria */
1039 if (strlen(match_criteria) > 0) {
1040 matchStr = (char *)match_criteria;
1042 if (rGroup->recordId && *rGroup->recordId) {
1043 matchStr = fileMatchStr (zh, &zh->reg->keys, rGroup, fname,
1048 rinfo = dict_lookup (zh->reg->matchDict, matchStr);
1050 memcpy (sysno, rinfo+1, sizeof(*sysno));
1052 logf (LOG_WARN, "Bad match criteria (recordID)");
1063 logf (LOG_LOG, "delete %s %s %ld", rGroup->recordType,
1064 fname, (long) recordOffset);
1065 logf (LOG_WARN, "cannot delete record above (seems new)");
1068 logf (LOG_LOG, "add %s %s %ld", rGroup->recordType, fname,
1069 (long) recordOffset);
1070 rec = rec_new (zh->reg->records);
1072 *sysno = rec->sysno;
1074 recordAttr = rec_init_attr (zh->reg->zei, rec);
1078 dict_insert (zh->reg->matchDict, matchStr,
1079 sizeof(*sysno), sysno);
1081 extract_flushSortKeys (zh, *sysno, 1, &zh->reg->sortKeys);
1082 extract_flushRecordKeys (zh, *sysno, 1, &zh->reg->keys);
1084 zh->records_inserted++;
1088 /* record already exists */
1089 struct recKeys delkeys;
1090 struct sortKeys sortKeys;
1092 rec = rec_get (zh->reg->records, *sysno);
1095 recordAttr = rec_init_attr (zh->reg->zei, rec);
1097 if (recordAttr->runNumber ==
1098 zebraExplain_runNumberIncrement (zh->reg->zei, 0))
1100 logf (LOG_LOG, "skipped %s %s %ld", rGroup->recordType,
1101 fname, (long) recordOffset);
1102 extract_flushSortKeys (zh, *sysno, -1, &zh->reg->sortKeys);
1107 delkeys.buf_used = rec->size[recInfo_delKeys];
1108 delkeys.buf = rec->info[recInfo_delKeys];
1110 sortKeys.buf_used = rec->size[recInfo_sortKeys];
1111 sortKeys.buf = rec->info[recInfo_sortKeys];
1113 extract_flushSortKeys (zh, *sysno, 0, &sortKeys);
1114 extract_flushRecordKeys (zh, *sysno, 0, &delkeys);
1117 /* record going to be deleted */
1118 if (!delkeys.buf_used)
1120 logf (LOG_LOG, "delete %s %s %ld", rGroup->recordType,
1121 fname, (long) recordOffset);
1122 logf (LOG_WARN, "cannot delete file above, storeKeys false");
1126 logf (LOG_LOG, "delete %s %s %ld", rGroup->recordType,
1127 fname, (long) recordOffset);
1128 zh->records_deleted++;
1130 dict_delete (zh->reg->matchDict, matchStr);
1131 rec_del (zh->reg->records, &rec);
1139 /* record going to be updated */
1140 if (!delkeys.buf_used)
1142 logf (LOG_LOG, "update %s %s %ld", rGroup->recordType,
1143 fname, (long) recordOffset);
1144 logf (LOG_WARN, "cannot update file above, storeKeys false");
1148 logf (LOG_LOG, "update %s %s %ld", rGroup->recordType,
1149 fname, (long) recordOffset);
1150 extract_flushSortKeys (zh, *sysno, 1, &zh->reg->sortKeys);
1151 extract_flushRecordKeys (zh, *sysno, 1, &zh->reg->keys);
1152 zh->records_updated++;
1156 /* update file type */
1157 xfree (rec->info[recInfo_fileType]);
1158 rec->info[recInfo_fileType] =
1159 rec_strdup (rGroup->recordType, &rec->size[recInfo_fileType]);
1161 /* update filename */
1162 xfree (rec->info[recInfo_filename]);
1163 rec->info[recInfo_filename] =
1164 rec_strdup (fname, &rec->size[recInfo_filename]);
1166 /* update delete keys */
1167 xfree (rec->info[recInfo_delKeys]);
1168 if (zh->reg->keys.buf_used > 0 && rGroup->flagStoreKeys == 1)
1170 rec->size[recInfo_delKeys] = zh->reg->keys.buf_used;
1171 rec->info[recInfo_delKeys] = zh->reg->keys.buf;
1172 zh->reg->keys.buf = NULL;
1173 zh->reg->keys.buf_max = 0;
1177 rec->info[recInfo_delKeys] = NULL;
1178 rec->size[recInfo_delKeys] = 0;
1181 /* update sort keys */
1182 xfree (rec->info[recInfo_sortKeys]);
1184 rec->size[recInfo_sortKeys] = zh->reg->sortKeys.buf_used;
1185 rec->info[recInfo_sortKeys] = zh->reg->sortKeys.buf;
1186 zh->reg->sortKeys.buf = NULL;
1187 zh->reg->sortKeys.buf_max = 0;
1189 /* save file size of original record */
1190 zebraExplain_recordBytesIncrement (zh->reg->zei,
1191 - recordAttr->recordSize);
1193 recordAttr->recordSize = fi->file_moffset - recordOffset;
1194 if (!recordAttr->recordSize)
1195 recordAttr->recordSize = fi->file_max - recordOffset;
1197 recordAttr->recordSize = buf_size;
1199 zebraExplain_recordBytesIncrement (zh->reg->zei,
1200 recordAttr->recordSize);
1202 /* set run-number for this record */
1203 recordAttr->runNumber =
1204 zebraExplain_runNumberIncrement (zh->reg->zei, 0);
1206 /* update store data */
1207 xfree (rec->info[recInfo_storeData]);
1208 if (rGroup->flagStoreData == 1)
1210 rec->size[recInfo_storeData] = recordAttr->recordSize;
1211 rec->info[recInfo_storeData] = (char *)
1212 xmalloc (recordAttr->recordSize);
1214 memcpy (rec->info[recInfo_storeData], buf, recordAttr->recordSize);
1216 if (lseek (fi->fd, recordOffset, SEEK_SET) < 0)
1218 logf (LOG_ERRNO|LOG_FATAL, "seek to %ld in %s",
1219 (long) recordOffset, fname);
1222 if (read (fi->fd, rec->info[recInfo_storeData], recordAttr->recordSize)
1223 < recordAttr->recordSize)
1225 logf (LOG_ERRNO|LOG_FATAL, "read %d bytes of %s",
1226 recordAttr->recordSize, fname);
1233 rec->info[recInfo_storeData] = NULL;
1234 rec->size[recInfo_storeData] = 0;
1236 /* update database name */
1237 xfree (rec->info[recInfo_databaseName]);
1238 rec->info[recInfo_databaseName] =
1239 rec_strdup (rGroup->databaseName, &rec->size[recInfo_databaseName]);
1242 recordAttr->recordOffset = recordOffset;
1244 /* commit this record */
1245 rec_put (zh->reg->records, &rec);
1252 int explain_extract (void *handle, Record rec, data1_node *n)
1254 ZebraHandle zh = (ZebraHandle) handle;
1255 struct recExtractCtrl extractCtrl;
1258 if (zebraExplain_curDatabase (zh->reg->zei,
1259 rec->info[recInfo_databaseName]))
1262 if (zebraExplain_newDatabase (zh->reg->zei,
1263 rec->info[recInfo_databaseName], 0))
1267 zh->reg->keys.buf_used = 0;
1268 zh->reg->keys.prevAttrUse = -1;
1269 zh->reg->keys.prevAttrSet = -1;
1270 zh->reg->keys.prevSeqNo = 0;
1271 zh->reg->sortKeys.buf_used = 0;
1273 extractCtrl.init = extract_init;
1274 extractCtrl.tokenAdd = extract_token_add;
1275 extractCtrl.schemaAdd = extract_schema_add;
1276 extractCtrl.dh = zh->reg->dh;
1277 for (i = 0; i<256; i++)
1278 extractCtrl.seqno[i] = 0;
1279 extractCtrl.zebra_maps = zh->reg->zebra_maps;
1280 extractCtrl.flagShowRecords = 0;
1281 extractCtrl.handle = handle;
1283 grs_extract_tree(&extractCtrl, n);
1285 if (rec->size[recInfo_delKeys])
1287 struct recKeys delkeys;
1288 struct sortKeys sortkeys;
1290 delkeys.buf_used = rec->size[recInfo_delKeys];
1291 delkeys.buf = rec->info[recInfo_delKeys];
1293 sortkeys.buf_used = rec->size[recInfo_sortKeys];
1294 sortkeys.buf = rec->info[recInfo_sortKeys];
1296 extract_flushSortKeys (zh, rec->sysno, 0, &sortkeys);
1297 extract_flushRecordKeys (zh, rec->sysno, 0, &delkeys);
1299 extract_flushRecordKeys (zh, rec->sysno, 1, &zh->reg->keys);
1300 extract_flushSortKeys (zh, rec->sysno, 1, &zh->reg->sortKeys);
1302 xfree (rec->info[recInfo_delKeys]);
1303 rec->size[recInfo_delKeys] = zh->reg->keys.buf_used;
1304 rec->info[recInfo_delKeys] = zh->reg->keys.buf;
1305 zh->reg->keys.buf = NULL;
1306 zh->reg->keys.buf_max = 0;
1308 xfree (rec->info[recInfo_sortKeys]);
1309 rec->size[recInfo_sortKeys] = zh->reg->sortKeys.buf_used;
1310 rec->info[recInfo_sortKeys] = zh->reg->sortKeys.buf;
1311 zh->reg->sortKeys.buf = NULL;
1312 zh->reg->sortKeys.buf_max = 0;
1317 void extract_flushRecordKeys (ZebraHandle zh, SYSNO sysno,
1318 int cmd, struct recKeys *reckeys)
1322 unsigned char attrSet = (unsigned char) -1;
1323 unsigned short attrUse = (unsigned short) -1;
1328 ZebraExplainInfo zei = zh->reg->zei;
1330 if (!zh->reg->key_buf)
1332 int mem= 1024*1024* atoi( res_get_def( zh->res, "memmax", "8"));
1335 logf(LOG_WARN, "Invalid memory setting, using default 8 MB");
1338 /* FIXME: That "8" should be in a default settings include */
1339 /* not hard-coded here! -H */
1340 zh->reg->key_buf = (char**) xmalloc (mem);
1341 zh->reg->ptr_top = mem/sizeof(char*);
1343 zh->reg->key_buf_used = 0;
1344 zh->reg->key_file_no = 0;
1346 zebraExplain_recordCountIncrement (zei, cmd ? 1 : -1);
1347 while (off < reckeys->buf_used)
1349 const char *src = reckeys->buf + off;
1358 memcpy (&ch, src, sizeof(ch));
1364 memcpy (&attrSet, src, sizeof(attrSet));
1365 src += sizeof(attrSet);
1369 memcpy (&attrUse, src, sizeof(attrUse));
1370 src += sizeof(attrUse);
1373 if (zh->reg->key_buf_used + 1024 >
1374 (zh->reg->ptr_top -zh->reg->ptr_i)*sizeof(char*))
1375 extract_flushWriteKeys (zh);
1377 (zh->reg->key_buf)[zh->reg->ptr_top - zh->reg->ptr_i] =
1378 (char*)zh->reg->key_buf + zh->reg->key_buf_used;
1381 ch = zebraExplain_lookupSU (zei, attrSet, attrUse);
1383 ch = zebraExplain_addSU (zei, attrSet, attrUse);
1386 zh->reg->key_buf_used +=
1387 key_SU_encode (ch,((char*)zh->reg->key_buf) +
1388 zh->reg->key_buf_used);
1391 ((char*)zh->reg->key_buf) [(zh->reg->key_buf_used)++] = *src++;
1393 ((char*)(zh->reg->key_buf))[(zh->reg->key_buf_used)++] = '\0';
1394 ((char*)(zh->reg->key_buf))[(zh->reg->key_buf_used)++] = cmd;
1397 seqno += ((lead>>2) & 15)-1;
1400 memcpy (&seqno, src, sizeof(seqno));
1401 src += sizeof(seqno);
1405 memcpy ((char*)zh->reg->key_buf + zh->reg->key_buf_used, &key, sizeof(key));
1406 (zh->reg->key_buf_used) += sizeof(key);
1407 off = src - reckeys->buf;
1409 assert (off == reckeys->buf_used);
1412 void extract_flushWriteKeys (ZebraHandle zh)
1415 char out_fname[200];
1417 struct encode_info encode_info;
1418 int ptr_i = zh->reg->ptr_i;
1422 if (!zh->reg->key_buf || ptr_i <= 0)
1425 (zh->reg->key_file_no)++;
1426 logf (LOG_LOG, "sorting section %d", (zh->reg->key_file_no));
1428 qsort (zh->reg->key_buf + zh->reg->ptr_top - ptr_i, ptr_i,
1429 sizeof(char*), key_qsort_compare);
1430 extract_get_fname_tmp (zh, out_fname, zh->reg->key_file_no);
1432 if (!(outf = fopen (out_fname, "wb")))
1434 logf (LOG_FATAL|LOG_ERRNO, "fopen %s", out_fname);
1437 logf (LOG_LOG, "writing section %d", zh->reg->key_file_no);
1438 prevcp = cp = (zh->reg->key_buf)[zh->reg->ptr_top - ptr_i];
1440 encode_key_init (&encode_info);
1441 encode_key_write (cp, &encode_info, outf);
1445 cp = (zh->reg->key_buf)[zh->reg->ptr_top - ptr_i];
1446 if (strcmp (cp, prevcp))
1448 encode_key_flush ( &encode_info, outf);
1449 encode_key_init (&encode_info);
1450 encode_key_write (cp, &encode_info, outf);
1454 encode_key_write (cp + strlen(cp), &encode_info, outf);
1456 encode_key_flush ( &encode_info, outf);
1458 qsort (key_buf + ptr_top-ptr_i, ptr_i, sizeof(char*), key_x_compare);
1459 extract_get_fname_tmp (out_fname, key_file_no);
1461 if (!(outf = fopen (out_fname, "wb")))
1463 logf (LOG_FATAL|LOG_ERRNO, "fopen %s", out_fname);
1466 logf (LOG_LOG, "writing section %d", key_file_no);
1468 prevcp = key_buf[ptr_top-i];
1470 if (!--i || strcmp (prevcp, key_buf[ptr_top-i]))
1472 key_y_len = strlen(prevcp)+1;
1474 logf (LOG_LOG, "key_y_len: %2d %02x %02x %s",
1475 key_y_len, prevcp[0], prevcp[1], 2+prevcp);
1477 qsort (key_buf + ptr_top-ptr_i, ptr_i - i,
1478 sizeof(char*), key_y_compare);
1479 cp = key_buf[ptr_top-ptr_i];
1481 encode_key_init (&encode_info);
1482 encode_key_write (cp, &encode_info, outf);
1485 cp = key_buf[ptr_top-ptr_i];
1486 encode_key_write (cp+key_y_len, &encode_info, outf);
1488 encode_key_flush ( &encode_info, outf);
1491 prevcp = key_buf[ptr_top-ptr_i];
1496 logf (LOG_FATAL|LOG_ERRNO, "fclose %s", out_fname);
1499 logf (LOG_LOG, "finished section %d", zh->reg->key_file_no);
1501 zh->reg->key_buf_used = 0;
1504 void extract_add_index_string (RecWord *p, const char *string,
1508 unsigned char attrSet;
1509 unsigned short attrUse;
1512 int *pseqno = &p->seqno;
1513 ZebraHandle zh = p->extractCtrl->handle;
1514 ZebraExplainInfo zei = zh->reg->zei;
1515 struct recKeys *keys = &zh->reg->keys;
1517 if (keys->buf_used+1024 > keys->buf_max)
1521 b = (char *) xmalloc (keys->buf_max += 128000);
1522 if (keys->buf_used > 0)
1523 memcpy (b, keys->buf, keys->buf_used);
1527 dst = keys->buf + keys->buf_used;
1529 attrSet = p->attrSet;
1530 if (keys->buf_used > 0 && keys->prevAttrSet == attrSet)
1533 keys->prevAttrSet = attrSet;
1534 attrUse = p->attrUse;
1535 if (keys->buf_used > 0 && keys->prevAttrUse == attrUse)
1538 keys->prevAttrUse = attrUse;
1540 diff = 1 + *pseqno - keys->prevSeqNo;
1541 if (diff >= 1 && diff <= 15)
1542 lead |= (diff << 2);
1546 keys->prevSeqNo = *pseqno;
1553 int ch = zebraExplain_lookupSU (zei, attrSet, attrUse);
1556 ch = zebraExplain_addSU (zei, attrSet, attrUse);
1557 yaz_log (LOG_DEBUG, "addSU set=%d use=%d SU=%d",
1558 attrSet, attrUse, ch);
1561 memcpy (dst, &ch, sizeof(ch));
1567 memcpy (dst, &attrSet, sizeof(attrSet));
1568 dst += sizeof(attrSet);
1572 memcpy (dst, &attrUse, sizeof(attrUse));
1573 dst += sizeof(attrUse);
1576 *dst++ = p->reg_type;
1577 memcpy (dst, string, length);
1583 memcpy (dst, pseqno, sizeof(*pseqno));
1584 dst += sizeof(*pseqno);
1586 keys->buf_used = dst - keys->buf;
1589 static void extract_add_sort_string (RecWord *p, const char *string,
1592 ZebraHandle zh = p->extractCtrl->handle;
1593 struct sortKeys *sk = &zh->reg->sortKeys;
1596 while (off < sk->buf_used)
1600 off += key_SU_decode(&set, sk->buf + off);
1601 off += key_SU_decode(&use, sk->buf + off);
1602 off += key_SU_decode(&slen, sk->buf + off);
1604 if (p->attrSet == set && p->attrUse == use)
1607 assert (off == sk->buf_used);
1609 if (sk->buf_used + IT_MAX_WORD > sk->buf_max)
1613 b = (char *) xmalloc (sk->buf_max += 128000);
1614 if (sk->buf_used > 0)
1615 memcpy (b, sk->buf, sk->buf_used);
1619 off += key_SU_encode(p->attrSet, sk->buf + off);
1620 off += key_SU_encode(p->attrUse, sk->buf + off);
1621 off += key_SU_encode(length, sk->buf + off);
1622 memcpy (sk->buf + off, string, length);
1623 sk->buf_used = off + length;
1626 void extract_add_string (RecWord *p, const char *string, int length)
1628 assert (length > 0);
1629 if (zebra_maps_is_sort (p->zebra_maps, p->reg_type))
1630 extract_add_sort_string (p, string, length);
1632 extract_add_index_string (p, string, length);
1635 static void extract_add_incomplete_field (RecWord *p)
1637 const char *b = p->string;
1638 int remain = p->length;
1639 const char **map = 0;
1642 map = zebra_maps_input(p->zebra_maps, p->reg_type, &b, remain);
1646 char buf[IT_MAX_WORD+1];
1650 while (map && *map && **map == *CHR_SPACE)
1652 remain = p->length - (b - p->string);
1654 map = zebra_maps_input(p->zebra_maps, p->reg_type, &b, remain);
1661 while (map && *map && **map != *CHR_SPACE)
1663 const char *cp = *map;
1665 while (i < IT_MAX_WORD && *cp)
1667 remain = p->length - (b - p->string);
1669 map = zebra_maps_input(p->zebra_maps, p->reg_type, &b, remain);
1675 extract_add_string (p, buf, i);
1680 static void extract_add_complete_field (RecWord *p)
1682 const char *b = p->string;
1683 char buf[IT_MAX_WORD+1];
1684 const char **map = 0;
1685 int i = 0, remain = p->length;
1688 map = zebra_maps_input (p->zebra_maps, p->reg_type, &b, remain);
1690 while (remain > 0 && i < IT_MAX_WORD)
1692 while (map && *map && **map == *CHR_SPACE)
1694 remain = p->length - (b - p->string);
1696 map = zebra_maps_input(p->zebra_maps, p->reg_type, &b, remain);
1703 if (i && i < IT_MAX_WORD)
1704 buf[i++] = *CHR_SPACE;
1705 while (map && *map && **map != *CHR_SPACE)
1707 const char *cp = *map;
1709 if (i >= IT_MAX_WORD)
1711 while (i < IT_MAX_WORD && *cp)
1713 remain = p->length - (b - p->string);
1715 map = zebra_maps_input (p->zebra_maps, p->reg_type, &b,
1723 extract_add_string (p, buf, i);
1726 void extract_token_add (RecWord *p)
1729 if ((wrbuf = zebra_replace(p->zebra_maps, p->reg_type, 0,
1730 p->string, p->length)))
1732 p->string = wrbuf_buf(wrbuf);
1733 p->length = wrbuf_len(wrbuf);
1735 if (zebra_maps_is_complete (p->zebra_maps, p->reg_type))
1736 extract_add_complete_field (p);
1738 extract_add_incomplete_field(p);
1741 void extract_schema_add (struct recExtractCtrl *p, Odr_oid *oid)
1743 ZebraHandle zh = (ZebraHandle) (p->handle);
1744 zebraExplain_addSchema (zh->reg->zei, oid);
1747 void extract_flushSortKeys (ZebraHandle zh, SYSNO sysno,
1748 int cmd, struct sortKeys *sk)
1750 SortIdx sortIdx = zh->reg->sortIdx;
1753 sortIdx_sysno (sortIdx, sysno);
1755 while (off < sk->buf_used)
1757 int set, use, slen, l;
1759 off += key_SU_decode(&set, sk->buf + off);
1760 off += key_SU_decode(&use, sk->buf + off);
1761 off += key_SU_decode(&slen, sk->buf + off);
1763 sortIdx_type(sortIdx, use);
1765 sortIdx_add(sortIdx, sk->buf + off, slen);
1767 sortIdx_add(sortIdx, "", 1);
1772 void encode_key_init (struct encode_info *i)
1783 char *encode_key_int (int d, char *bp)
1787 else if (d <= 16383)
1789 *bp++ = 64 + (d>>8);
1792 else if (d <= 4194303)
1794 *bp++ = 128 + (d>>16);
1795 *bp++ = (d>>8) & 255;
1800 *bp++ = 192 + (d>>24);
1801 *bp++ = (d>>16) & 255;
1802 *bp++ = (d>>8) & 255;
1809 /* this is the old encode_key_write
1810 * may be deleted once we are confident that the new works
1813 void encode_key_write (char *k, struct encode_info *i, FILE *outf)
1818 while ((*bp++ = *k++))
1820 memcpy (&key, k+1, sizeof(struct it_key));
1821 bp = encode_key_int ( (key.sysno - i->sysno) * 2 + *k, bp);
1822 if (i->sysno != key.sysno)
1824 i->sysno = key.sysno;
1827 else if (!i->seqno && !key.seqno && i->cmd == *k)
1829 bp = encode_key_int (key.seqno - i->seqno, bp);
1830 i->seqno = key.seqno;
1832 if (fwrite (i->buf, bp - i->buf, 1, outf) != 1)
1834 logf (LOG_FATAL|LOG_ERRNO, "fwrite");
1839 void encode_key_flush (struct encode_info *i, FILE *outf)
1840 { /* dummy routine */
1845 /* new encode_key_write
1846 * The idea is to buffer one more key, and compare them
1847 * If we are going to delete and insert the same key,
1848 * we may as well not bother. Should make a difference in
1849 * updates with small modifications (appending to a mbox)
1851 void encode_key_write (char *k, struct encode_info *i, FILE *outf)
1856 if (*k) /* first time for new key */
1859 while ((*bp++ = *k++))
1861 i->keylen= bp - i->buf -1;
1862 assert(i->keylen+1+sizeof(struct it_key) < ENCODE_BUFLEN);
1866 bp=i->buf + i->keylen;
1871 memcpy (&key, k+1, sizeof(struct it_key));
1872 if (0==i->prevsys) /* no previous filter, fill up */
1874 i->prevsys=key.sysno;
1875 i->prevseq=key.seqno;
1878 else if ( (i->prevsys==key.sysno) &&
1879 (i->prevseq==key.seqno) &&
1881 { /* same numbers, diff cmd, they cancel out */
1885 { /* different stuff, write previous, move buf */
1886 bp = encode_key_int ( (i->prevsys - i->sysno) * 2 + i->prevcmd, bp);
1887 if (i->sysno != i->prevsys)
1889 i->sysno = i->prevsys;
1892 else if (!i->seqno && !i->prevseq && i->cmd == i->prevcmd)
1894 return; /* ??? Filters some sort of duplicates away */
1895 /* ??? Can this ever happen -H 15oct02 */
1897 bp = encode_key_int (i->prevseq - i->seqno, bp);
1898 i->seqno = i->prevseq;
1899 i->cmd = i->prevcmd;
1900 if (fwrite (i->buf, bp - i->buf, 1, outf) != 1)
1902 logf (LOG_FATAL|LOG_ERRNO, "fwrite");
1905 i->keylen=0; /* ok, it's written, forget it */
1906 i->prevsys=key.sysno;
1907 i->prevseq=key.seqno;
1912 void encode_key_flush (struct encode_info *i, FILE *outf)
1913 { /* flush the last key from i */
1914 char *bp =i->buf + i->keylen;
1917 return; /* nothing to flush */
1920 bp = encode_key_int ( (i->prevsys - i->sysno) * 2 + i->prevcmd, bp);
1921 if (i->sysno != i->prevsys)
1923 i->sysno = i->prevsys;
1926 else if (!i->seqno && !i->prevseq && i->cmd == i->prevcmd)
1928 return; /* ??? Filters some sort of duplicates away */
1929 /* ??? Can this ever happen -H 15oct02 */
1931 bp = encode_key_int (i->prevseq - i->seqno, bp);
1932 i->seqno = i->prevseq;
1933 i->cmd = i->prevcmd;
1934 if (fwrite (i->buf, bp - i->buf, 1, outf) != 1)
1936 logf (LOG_FATAL|LOG_ERRNO, "fwrite");
1939 i->keylen=0; /* ok, it's written, forget it */
1940 i->prevsys=0; /* forget the values too */