+
+void extract_flushRecordKeys (ZebraHandle zh, SYSNO sysno,
+ int cmd, struct recKeys *reckeys)
+{
+ void *decode_handle = iscz1_start();
+ int off = 0;
+ int ch = 0;
+ ZebraExplainInfo zei = zh->reg->zei;
+
+ if (!zh->reg->key_buf)
+ {
+ int mem= 1024*1024* atoi( res_get_def( zh->res, "memmax", "8"));
+ if (mem <= 0)
+ {
+ yaz_log(YLOG_WARN, "Invalid memory setting, using default 8 MB");
+ mem= 1024*1024*8;
+ }
+ /* FIXME: That "8" should be in a default settings include */
+ /* not hard-coded here! -H */
+ zh->reg->key_buf = (char**) xmalloc (mem);
+ zh->reg->ptr_top = mem/sizeof(char*);
+ zh->reg->ptr_i = 0;
+ zh->reg->key_buf_used = 0;
+ zh->reg->key_file_no = 0;
+ }
+ zebraExplain_recordCountIncrement (zei, cmd ? 1 : -1);
+
+ while (off < reckeys->buf_used)
+ {
+ const char *src = reckeys->buf + off;
+ struct it_key key;
+ char *dst = (char*) &key;
+ int attrSet, attrUse;
+
+ iscz1_decode(decode_handle, &dst, &src);
+ assert(key.len == 4);
+
+ attrSet = (int) key.mem[0] >> 16;
+ attrUse = (int) key.mem[0] & 65535;
+
+ if (zh->reg->key_buf_used + 1024 >
+ (zh->reg->ptr_top -zh->reg->ptr_i)*sizeof(char*))
+ extract_flushWriteKeys (zh,0);
+ ++(zh->reg->ptr_i);
+ assert(zh->reg->ptr_i > 0);
+ (zh->reg->key_buf)[zh->reg->ptr_top - zh->reg->ptr_i] =
+ (char*)zh->reg->key_buf + zh->reg->key_buf_used;
+
+ ch = zebraExplain_lookupSU (zei, attrSet, attrUse);
+ if (ch < 0)
+ ch = zebraExplain_addSU (zei, attrSet, attrUse);
+
+ assert (ch > 0);
+ zh->reg->key_buf_used +=
+ key_SU_encode (ch,((char*)zh->reg->key_buf) +
+ zh->reg->key_buf_used);
+ while (*src)
+ ((char*)zh->reg->key_buf) [(zh->reg->key_buf_used)++] = *src++;
+ src++;
+ ((char*)(zh->reg->key_buf))[(zh->reg->key_buf_used)++] = '\0';
+ ((char*)(zh->reg->key_buf))[(zh->reg->key_buf_used)++] = cmd;
+
+ key.len = 3;
+ if (key.mem[1]) /* filter specify record ID */
+ key.mem[0] = key.mem[1];
+ else
+ key.mem[0] = sysno;
+ key.mem[1] = key.mem[2]; /* section_id */
+ key.mem[2] = key.mem[3]; /* sequence .. */
+
+ memcpy ((char*)zh->reg->key_buf + zh->reg->key_buf_used,
+ &key, sizeof(key));
+ (zh->reg->key_buf_used) += sizeof(key);
+ off = src - reckeys->buf;
+ }
+ assert (off == reckeys->buf_used);
+ iscz1_stop(decode_handle);
+}
+
+void extract_flushWriteKeys (ZebraHandle zh, int final)
+ /* optimizing: if final=1, and no files written yet */
+ /* push the keys directly to merge, sidestepping the */
+ /* temp file altogether. Speeds small updates */
+{
+ FILE *outf;
+ char out_fname[200];
+ char *prevcp, *cp;
+ struct encode_info encode_info;
+ int ptr_i = zh->reg->ptr_i;
+ int temp_policy;
+#if SORT_EXTRA
+ int i;
+#endif
+ if (!zh->reg->key_buf || ptr_i <= 0)
+ {
+ yaz_log (YLOG_DEBUG, " nothing to flush section=%d buf=%p i=%d",
+ zh->reg->key_file_no, zh->reg->key_buf, ptr_i);
+ yaz_log (YLOG_DEBUG, " buf=%p ",
+ zh->reg->key_buf);
+ yaz_log (YLOG_DEBUG, " ptr=%d ",zh->reg->ptr_i);
+ yaz_log (YLOG_DEBUG, " reg=%p ",zh->reg);
+
+ return;
+ }
+
+ (zh->reg->key_file_no)++;
+ yaz_log (YLOG_LOG, "sorting section %d", (zh->reg->key_file_no));
+ yaz_log (YLOG_DEBUG, " sort_buff at %p n=%d",
+ zh->reg->key_buf + zh->reg->ptr_top - ptr_i,ptr_i);
+#if !SORT_EXTRA
+ qsort (zh->reg->key_buf + zh->reg->ptr_top - ptr_i, ptr_i,
+ sizeof(char*), key_qsort_compare);
+
+ /* zebra.cfg: tempfiles:
+ Y: always use temp files (old way)
+ A: use temp files, if more than one (auto)
+ = if this is both the last and the first
+ N: never bother with temp files (new) */
+
+ temp_policy=toupper(res_get_def(zh->res,"tempfiles","auto")[0]);
+ if (temp_policy != 'Y' && temp_policy != 'N' && temp_policy != 'A') {
+ yaz_log (YLOG_WARN, "Illegal tempfiles setting '%c'. using 'Auto' ",
+ temp_policy);
+ temp_policy='A';
+ }
+
+ if ( ( temp_policy =='N' ) || /* always from memory */
+ ( ( temp_policy =='A' ) && /* automatic */
+ (zh->reg->key_file_no == 1) && /* this is first time */
+ (final) ) ) /* and last (=only) time */
+ { /* go directly from memory */
+ zh->reg->key_file_no =0; /* signal not to read files */
+ zebra_index_merge(zh);
+ zh->reg->ptr_i = 0;
+ zh->reg->key_buf_used = 0;
+ return;
+ }
+
+ /* Not doing directly from memory, write into a temp file */
+ extract_get_fname_tmp (zh, out_fname, zh->reg->key_file_no);
+
+ if (!(outf = fopen (out_fname, "wb")))
+ {
+ yaz_log (YLOG_FATAL|YLOG_ERRNO, "fopen %s", out_fname);
+ exit (1);
+ }
+ yaz_log (YLOG_LOG, "writing section %d", zh->reg->key_file_no);
+ prevcp = cp = (zh->reg->key_buf)[zh->reg->ptr_top - ptr_i];
+
+ encode_key_init (&encode_info);
+ encode_key_write (cp, &encode_info, outf);
+
+ while (--ptr_i > 0)
+ {
+ cp = (zh->reg->key_buf)[zh->reg->ptr_top - ptr_i];
+ if (strcmp (cp, prevcp))
+ {
+ encode_key_flush ( &encode_info, outf);
+ encode_key_init (&encode_info);
+ encode_key_write (cp, &encode_info, outf);
+ prevcp = cp;
+ }
+ else
+ encode_key_write (cp + strlen(cp), &encode_info, outf);
+ }
+ encode_key_flush ( &encode_info, outf);
+#else
+ qsort (key_buf + ptr_top-ptr_i, ptr_i, sizeof(char*), key_x_compare);
+ extract_get_fname_tmp (out_fname, key_file_no);
+
+ if (!(outf = fopen (out_fname, "wb")))
+ {
+ yaz_log (YLOG_FATAL|YLOG_ERRNO, "fopen %s", out_fname);
+ exit (1);
+ }
+ yaz_log (YLOG_LOG, "writing section %d", key_file_no);
+ i = ptr_i;
+ prevcp = key_buf[ptr_top-i];
+ while (1)
+ if (!--i || strcmp (prevcp, key_buf[ptr_top-i]))
+ {
+ key_y_len = strlen(prevcp)+1;
+#if 0
+ yaz_log (YLOG_LOG, "key_y_len: %2d %02x %02x %s",
+ key_y_len, prevcp[0], prevcp[1], 2+prevcp);
+#endif
+ qsort (key_buf + ptr_top-ptr_i, ptr_i - i,
+ sizeof(char*), key_y_compare);
+ cp = key_buf[ptr_top-ptr_i];
+ --key_y_len;
+ encode_key_init (&encode_info);
+ encode_key_write (cp, &encode_info, outf);
+ while (--ptr_i > i)
+ {
+ cp = key_buf[ptr_top-ptr_i];
+ encode_key_write (cp+key_y_len, &encode_info, outf);
+ }
+ encode_key_flush ( &encode_info, outf);
+ if (!i)
+ break;
+ prevcp = key_buf[ptr_top-ptr_i];
+ }
+#endif
+ if (fclose (outf))
+ {
+ yaz_log (YLOG_FATAL|YLOG_ERRNO, "fclose %s", out_fname);
+ exit (1);
+ }
+ yaz_log (YLOG_LOG, "finished section %d", zh->reg->key_file_no);
+ zh->reg->ptr_i = 0;
+ zh->reg->key_buf_used = 0;
+}
+
+void extract_add_it_key (ZebraHandle zh,
+ int reg_type,
+ const char *str, int slen, struct it_key *key)
+{
+ char *dst;
+ struct recKeys *keys = &zh->reg->keys;
+ const char *src = (char*) key;
+
+ if (keys->buf_used+1024 > keys->buf_max)
+ {
+ char *b;
+
+ b = (char *) xmalloc (keys->buf_max += 128000);
+ if (keys->buf_used > 0)
+ memcpy (b, keys->buf, keys->buf_used);
+ xfree (keys->buf);
+ keys->buf = b;
+ }
+ dst = keys->buf + keys->buf_used;
+
+ iscz1_encode(keys->codec_handle, &dst, &src);
+
+ *dst++ = reg_type;
+ memcpy (dst, str, slen);
+ dst += slen;
+ *dst++ = '\0';
+ keys->buf_used = dst - keys->buf;
+}
+
+void extract_add_index_string (RecWord *p, const char *str, int length)
+{
+ struct it_key key;
+ key.len = 4;
+ key.mem[0] = p->attrSet * 65536 + p->attrUse;
+ key.mem[1] = p->record_id;
+ key.mem[2] = p->section_id;
+ key.mem[3] = p->seqno;
+
+#if 0
+ /* just for debugging .. */
+ yaz_log(YLOG_LOG, "add: set=%d use=%d "
+ "record_id=%lld section_id=%lld seqno=%lld",
+ p->attrSet, p->attrUse, p->record_id, p->section_id, p->seqno);
+#endif
+
+ extract_add_it_key(p->extractCtrl->handle, p->reg_type, str,
+ length, &key);
+}
+
+static void extract_add_sort_string (RecWord *p, const char *str,
+ int length)
+{
+ ZebraHandle zh = p->extractCtrl->handle;
+ struct sortKeys *sk = &zh->reg->sortKeys;
+ int off = 0;
+
+ while (off < sk->buf_used)
+ {
+ int set, use, slen;
+
+ off += key_SU_decode(&set, sk->buf + off);
+ off += key_SU_decode(&use, sk->buf + off);
+ off += key_SU_decode(&slen, sk->buf + off);
+ off += slen;
+ if (p->attrSet == set && p->attrUse == use)
+ return;
+ }
+ assert (off == sk->buf_used);
+
+ if (sk->buf_used + IT_MAX_WORD > sk->buf_max)
+ {
+ char *b;
+
+ b = (char *) xmalloc (sk->buf_max += 128000);
+ if (sk->buf_used > 0)
+ memcpy (b, sk->buf, sk->buf_used);
+ xfree (sk->buf);
+ sk->buf = b;
+ }
+ off += key_SU_encode(p->attrSet, sk->buf + off);
+ off += key_SU_encode(p->attrUse, sk->buf + off);
+ off += key_SU_encode(length, sk->buf + off);
+ memcpy (sk->buf + off, str, length);
+ sk->buf_used = off + length;
+}
+
+void extract_add_string (RecWord *p, const char *string, int length)
+{
+ assert (length > 0);
+ if (zebra_maps_is_sort (p->zebra_maps, p->reg_type))
+ extract_add_sort_string (p, string, length);
+ else
+ extract_add_index_string (p, string, length);
+}
+
+static void extract_add_incomplete_field (RecWord *p)
+{
+ const char *b = p->string;
+ int remain = p->length;
+ const char **map = 0;
+
+ yaz_log(YLOG_DEBUG, "Incomplete field, w='%.*s'", p->length, p->string);
+
+ if (remain > 0)
+ map = zebra_maps_input(p->zebra_maps, p->reg_type, &b, remain, 0);
+
+ while (map)
+ {
+ char buf[IT_MAX_WORD+1];
+ int i, remain;
+
+ /* Skip spaces */
+ while (map && *map && **map == *CHR_SPACE)
+ {
+ remain = p->length - (b - p->string);
+ if (remain > 0)
+ map = zebra_maps_input(p->zebra_maps, p->reg_type, &b, remain, 0);
+ else
+ map = 0;
+ }
+ if (!map)
+ break;
+ i = 0;
+ while (map && *map && **map != *CHR_SPACE)
+ {
+ const char *cp = *map;
+
+ while (i < IT_MAX_WORD && *cp)
+ buf[i++] = *(cp++);
+ remain = p->length - (b - p->string);
+ if (remain > 0)
+ map = zebra_maps_input(p->zebra_maps, p->reg_type, &b, remain, 0);
+ else
+ map = 0;
+ }
+ if (!i)
+ return;
+ extract_add_string (p, buf, i);
+ p->seqno++;
+ }
+}
+
+static void extract_add_complete_field (RecWord *p)
+{
+ const char *b = p->string;
+ char buf[IT_MAX_WORD+1];
+ const char **map = 0;
+ int i = 0, remain = p->length;
+ int first; /* first position */
+
+ yaz_log(YLOG_DEBUG, "Complete field, w='%.*s'", p->length, p->string);
+
+ if (remain > 0)
+ map = zebra_maps_input (p->zebra_maps, p->reg_type, &b, remain, 1);
+
+ while (remain > 0 && i < IT_MAX_WORD)
+ {
+ while (map && *map && **map == *CHR_SPACE)
+ {
+ remain = p->length - (b - p->string);
+
+ if (remain > 0)
+ {
+ first = i ? 0 : 1;
+ map = zebra_maps_input(p->zebra_maps, p->reg_type, &b, remain, first);
+ }
+ else
+ map = 0;
+ }
+ if (!map)
+ break;
+
+ if (i && i < IT_MAX_WORD)
+ buf[i++] = *CHR_SPACE;
+ while (map && *map && **map != *CHR_SPACE)
+ {
+ const char *cp = *map;
+
+ if (**map == *CHR_CUT)
+ {
+ i = 0;
+ }
+ else
+ {
+ if (i >= IT_MAX_WORD)
+ break;
+ yaz_log(YLOG_DEBUG, "Adding string to index '%d'", **map);
+ while (i < IT_MAX_WORD && *cp)
+ buf[i++] = *(cp++);
+ }
+ remain = p->length - (b - p->string);
+ if (remain > 0)
+ {
+ map = zebra_maps_input (p->zebra_maps, p->reg_type, &b,
+ remain, 0);
+ }
+ else
+ map = 0;
+ }
+ }
+ if (!i)
+ return;
+ extract_add_string (p, buf, i);
+}
+
+void extract_token_add (RecWord *p)
+{
+ WRBUF wrbuf;
+#if 0
+ yaz_log (YLOG_LOG, "token_add "
+ "reg_type=%c attrSet=%d attrUse=%d seqno=%d s=%.*s",
+ p->reg_type, p->attrSet, p->attrUse, p->seqno, p->length,
+ p->string);
+#endif
+ if ((wrbuf = zebra_replace(p->zebra_maps, p->reg_type, 0,
+ p->string, p->length)))
+ {
+ p->string = wrbuf_buf(wrbuf);
+ p->length = wrbuf_len(wrbuf);
+ }
+ if (zebra_maps_is_complete (p->zebra_maps, p->reg_type))
+ extract_add_complete_field (p);
+ else
+ extract_add_incomplete_field(p);
+}
+
+void extract_schema_add (struct recExtractCtrl *p, Odr_oid *oid)
+{
+ ZebraHandle zh = (ZebraHandle) (p->handle);
+ zebraExplain_addSchema (zh->reg->zei, oid);
+}
+
+void extract_flushSortKeys (ZebraHandle zh, SYSNO sysno,
+ int cmd, struct sortKeys *sk)
+{
+ SortIdx sortIdx = zh->reg->sortIdx;
+ int off = 0;
+
+ sortIdx_sysno (sortIdx, sysno);
+
+ while (off < sk->buf_used)
+ {
+ int set, use, slen;
+
+ off += key_SU_decode(&set, sk->buf + off);
+ off += key_SU_decode(&use, sk->buf + off);
+ off += key_SU_decode(&slen, sk->buf + off);
+
+ sortIdx_type(sortIdx, use);
+ if (cmd == 1)
+ sortIdx_add(sortIdx, sk->buf + off, slen);
+ else
+ sortIdx_add(sortIdx, "", 1);
+ off += slen;
+ }
+}
+
+void encode_key_init (struct encode_info *i)
+{
+ i->sysno = 0;
+ i->seqno = 0;
+ i->cmd = -1;
+ i->prevsys=0;
+ i->prevseq=0;
+ i->prevcmd=-1;
+ i->keylen=0;
+ i->encode_handle = iscz1_start();
+}
+
+#define OLDENCODE 1
+
+#ifdef OLDENCODE
+/* this is the old encode_key_write
+ * may be deleted once we are confident that the new works
+ * HL 15-oct-2002
+ */
+void encode_key_write (char *k, struct encode_info *i, FILE *outf)
+{
+ struct it_key key;
+ char *bp = i->buf, *bp0;
+ const char *src = (char *) &key;
+
+ /* copy term to output buf */
+ while ((*bp++ = *k++))
+ ;
+ /* and copy & align key so we can mangle */
+ memcpy (&key, k+1, sizeof(struct it_key)); /* *k is insert/delete */
+
+ bp0 = bp++;
+ iscz1_encode(i->encode_handle, &bp, &src);
+ *bp0 = (*k * 128) + bp - bp0 - 1; /* length and insert/delete combined */
+ if (fwrite (i->buf, bp - i->buf, 1, outf) != 1)
+ {
+ yaz_log (YLOG_FATAL|YLOG_ERRNO, "fwrite");
+ exit (1);
+ }
+}
+
+void encode_key_flush (struct encode_info *i, FILE *outf)
+{ /* dummy routine */
+ iscz1_stop(i->encode_handle);
+}
+
+#else
+
+/* new encode_key_write
+ * The idea is to buffer one more key, and compare them
+ * If we are going to delete and insert the same key,
+ * we may as well not bother. Should make a difference in
+ * updates with small modifications (appending to a mbox)
+ */
+void encode_key_write (char *k, struct encode_info *i, FILE *outf)
+{
+ struct it_key key;
+ char *bp;
+
+ if (*k) /* first time for new key */
+ {
+ bp = i->buf;
+ while ((*bp++ = *k++))
+ ;
+ i->keylen= bp - i->buf -1;
+ assert(i->keylen+1+sizeof(struct it_key) < ENCODE_BUFLEN);
+ }
+ else
+ {
+ bp=i->buf + i->keylen;
+ *bp++=0;
+ k++;
+ }
+
+ memcpy (&key, k+1, sizeof(struct it_key));
+ if (0==i->prevsys) /* no previous filter, fill up */
+ {
+ i->prevsys=key.sysno;
+ i->prevseq=key.seqno;
+ i->prevcmd=*k;
+ }
+ else if ( (i->prevsys==key.sysno) &&
+ (i->prevseq==key.seqno) &&
+ (i->prevcmd!=*k) )
+ { /* same numbers, diff cmd, they cancel out */
+ i->prevsys=0;
+ }
+ else
+ { /* different stuff, write previous, move buf */
+ bp = encode_key_int ( (i->prevsys - i->sysno) * 2 + i->prevcmd, bp);
+ if (i->sysno != i->prevsys)
+ {
+ i->sysno = i->prevsys;
+ i->seqno = 0;
+ }
+ else if (!i->seqno && !i->prevseq && i->cmd == i->prevcmd)
+ {
+ return; /* ??? Filters some sort of duplicates away */
+ /* ??? Can this ever happen -H 15oct02 */
+ }
+ bp = encode_key_int (i->prevseq - i->seqno, bp);
+ i->seqno = i->prevseq;
+ i->cmd = i->prevcmd;
+ if (fwrite (i->buf, bp - i->buf, 1, outf) != 1)
+ {
+ yaz_log (YLOG_FATAL|YLOG_ERRNO, "fwrite");
+ exit (1);
+ }
+ i->keylen=0; /* ok, it's written, forget it */
+ i->prevsys=key.sysno;
+ i->prevseq=key.seqno;
+ i->prevcmd=*k;
+ }
+}
+
+void encode_key_flush (struct encode_info *i, FILE *outf)
+{ /* flush the last key from i */
+ char *bp =i->buf + i->keylen;
+ if (0==i->prevsys)
+ {
+ return; /* nothing to flush */
+ }
+ *bp++=0;
+ bp = encode_key_int ( (i->prevsys - i->sysno) * 2 + i->prevcmd, bp);
+ if (i->sysno != i->prevsys)
+ {
+ i->sysno = i->prevsys;
+ i->seqno = 0;
+ }
+ else if (!i->seqno && !i->prevseq && i->cmd == i->prevcmd)
+ {
+ return; /* ??? Filters some sort of duplicates away */
+ /* ??? Can this ever happen -H 15oct02 */
+ }
+ bp = encode_key_int (i->prevseq - i->seqno, bp);
+ i->seqno = i->prevseq;
+ i->cmd = i->prevcmd;
+ if (fwrite (i->buf, bp - i->buf, 1, outf) != 1)
+ {
+ yaz_log (YLOG_FATAL|YLOG_ERRNO, "fwrite");
+ exit (1);
+ }
+ i->keylen=0; /* ok, it's written, forget it */
+ i->prevsys=0; /* forget the values too */
+ i->prevseq=0;
+}
+#endif