Temporary sort files are compressed.
[idzebra-moved-to-github.git] / index / extract.c
index 8464bb4..1830850 100644 (file)
@@ -4,7 +4,25 @@
  * Sebastian Hammer, Adam Dickmeiss
  *
  * $Log: extract.c,v $
- * Revision 1.15  1995-10-02 15:42:53  adam
+ * Revision 1.21  1995-10-10 12:24:38  adam
+ * Temporary sort files are compressed.
+ *
+ * Revision 1.20  1995/10/06  13:52:05  adam
+ * Bug fixes. Handler may abort further scanning.
+ *
+ * Revision 1.19  1995/10/04  12:55:16  adam
+ * Bug fix in ranked search. Use=Any keys inserted.
+ *
+ * Revision 1.18  1995/10/04  09:37:08  quinn
+ * Fixed bug.
+ *
+ * Revision 1.17  1995/10/03  14:28:57  adam
+ * Buffered read in extract works.
+ *
+ * Revision 1.16  1995/10/03  14:28:45  adam
+ * Work on more effecient read handler in extract.
+ *
+ * Revision 1.15  1995/10/02  15:42:53  adam
  * Extract uses file descriptors instead of FILE pointers.
  *
  * Revision 1.14  1995/10/02  15:29:13  adam
@@ -104,12 +122,73 @@ void key_open (int mem)
         exit (1);
     }
 }
+
+struct encode_info {
+    int  sysno;
+    int  seqno;
+    char buf[512];
+};
+
+void encode_key_init (struct encode_info *i)
+{
+    i->sysno = 0;
+    i->seqno = 0;
+}
+
+char *encode_key_int (int d, char *bp)
+{
+    if (d <= 63)
+        *bp++ = d;
+    else if (d <= 16383)
+    {
+        *bp++ = 64 + (d>>8);
+        *bp++ = d  & 255;
+    }
+    else if (d <= 4194303)
+    {
+        *bp++ = 128 + (d>>16);
+        *bp++ = (d>>8) & 255;
+        *bp++ = d & 255;
+    }
+    else
+    {
+        *bp++ = 192 + (d>>24);
+        *bp++ = (d>>16) & 255;
+        *bp++ = (d>>8) & 255;
+        *bp++ = d & 255;
+    }
+    return bp;
+}
+
+void encode_key_write (char *k, struct encode_info *i, FILE *outf)
+{
+    struct it_key key;
+    char *bp = i->buf;
+
+    while ((*bp++ = *k++))
+        ;
+    memcpy (&key, k+1, sizeof(struct it_key));
+    bp = encode_key_int ( (key.sysno - i->sysno) * 2 + *k, bp);
+    if (i->sysno != key.sysno)
+    {
+        i->sysno = key.sysno;
+        i->seqno = 0;
+    }
+    bp = encode_key_int (key.seqno - i->seqno, bp);
+    i->seqno = key.seqno;
+    if (fwrite (i->buf, bp - i->buf, 1, outf) != 1)
+    {
+        logf (LOG_FATAL|LOG_ERRNO, "fwrite");
+        exit (1);
+    }
+}
     
 void key_flush (void)
 {
     FILE *outf;
     char out_fname[200];
     char *prevcp, *cp;
+    struct encode_info encode_info;
     
     if (ptr_i <= 0)
         return;
@@ -127,33 +206,19 @@ void key_flush (void)
     logf (LOG_LOG, "writing section %d", key_file_no);
     prevcp = cp = key_buf[ptr_top-ptr_i];
     
-    if (fwrite (cp, strlen (cp)+2+sizeof(struct it_key), 1, outf) != 1)
-    {
-        logf (LOG_FATAL|LOG_ERRNO, "fwrite %s", out_fname);
-        exit (1);
-    }
+    encode_key_init (&encode_info);
+    encode_key_write (cp, &encode_info, outf);
     while (--ptr_i > 0)
     {
         cp = key_buf[ptr_top-ptr_i];
         if (strcmp (cp, prevcp))
         {
-            if (fwrite (cp, strlen (cp)+2+sizeof(struct it_key), 1,
-                        outf) != 1)
-            {
-                logf (LOG_FATAL|LOG_ERRNO, "fwrite %s", out_fname);
-                exit (1);
-            }
+            encode_key_init (&encode_info);
+            encode_key_write (cp, &encode_info, outf);
             prevcp = cp;
         }
         else
-        {
-            cp = strlen (cp) + cp;
-            if (fwrite (cp, 2+sizeof(struct it_key), 1, outf) != 1)
-            {
-                logf (LOG_FATAL|LOG_ERRNO, "fwrite %s", out_fname);
-                exit (1);
-            }
-        }
+            encode_key_write (cp + strlen(cp), &encode_info, outf);
     }
     if (fclose (outf))
     {
@@ -210,11 +275,89 @@ static void wordAdd (const RecWord *p)
     kused += sizeof(key);
 }
 
+static void wordAddAny (const RecWord *p)
+{
+    if (p->attrSet != 1 || p->attrUse != 1016)
+    {
+        RecWord w;
+
+        memcpy (&w, p, sizeof(w));
+        w.attrSet = 1;
+        w.attrUse = 1016;
+        wordAdd (&w);
+    }
+    wordAdd (p);
+}
+
+
+#define FILE_READ_BUF 1
+#if FILE_READ_BUF
+static char *file_buf;
+static int file_offset;
+static int file_bufsize;
+
+static void file_read_start (int fd)
+{
+    file_offset = 0;
+    file_buf = xmalloc (4096);
+    file_bufsize = read (fd, file_buf, 4096);
+}
+
+static void file_read_stop (int fd)
+{
+    xfree (file_buf);
+}
+
+static int file_read (int fd, char *buf, size_t count)
+{
+    int l = file_bufsize - file_offset;
+
+    if (count > l)
+    {
+        int r;
+        if (l > 0)
+            memcpy (buf, file_buf + file_offset, l);
+        count = count-l;
+        if (count > file_bufsize)
+        {
+            if ((r = read (fd, buf + l, count)) == -1)
+            {
+                logf (LOG_FATAL|LOG_ERRNO, "read");
+                exit (1);
+            }
+            file_bufsize = 0;
+            file_offset = 0;
+            return r;
+        }
+        file_bufsize = r = read (fd, file_buf, 4096);
+        if (r == -1)
+        {
+            logf (LOG_FATAL|LOG_ERRNO, "read");
+            exit (1);
+        }
+        else if (r <= count)
+        {
+            file_offset = r;
+            memcpy (buf + l, file_buf, r);
+            return l + r;
+        }
+        else
+        {
+            file_offset = count;
+            memcpy (buf + l, file_buf, count - l);
+            return count;
+        }
+    }
+    memcpy (buf, file_buf + file_offset, count);
+    file_offset += count;
+    return count;
+}
+#else
 static int file_read (int fd, char *buf, size_t count)
 {
     return read (fd, buf, count);
 }
-
+#endif
 void file_extract (int cmd, const char *fname, const char *kname)
 {
     int i, r;
@@ -262,11 +405,17 @@ void file_extract (int cmd, const char *fname, const char *kname)
     }
     extractCtrl.subType = "";
     extractCtrl.init = wordInit;
-    extractCtrl.add = wordAdd;
+    extractCtrl.add = wordAddAny;
+#if FILE_READ_BUF
+    file_read_start (extractCtrl.fd);
+#endif
     extractCtrl.readf = file_read;
     key_sysno = sysno;
     key_cmd = cmd;
     r = (*rt->extract)(&extractCtrl);
+#if FILE_READ_BUF
+    file_read_stop (extractCtrl.fd);
+#endif
     close (extractCtrl.fd);
     if (r)
         logf (LOG_WARN, "Couldn't extract file %s, code %d", fname, r);