Buffers used during file match got bigger.
[idzebra-moved-to-github.git] / index / extract.c
index 479e8bd..53dc896 100644 (file)
@@ -1,10 +1,46 @@
 /*
- * Copyright (C) 1994-1995, Index Data I/S 
+ * Copyright (C) 1994-1996, Index Data I/S 
  * All rights reserved.
  * Sebastian Hammer, Adam Dickmeiss
  *
  * $Log: extract.c,v $
- * Revision 1.54  1996-05-01 13:46:35  adam
+ * Revision 1.64  1996-11-08 11:10:16  adam
+ * Buffers used during file match got bigger.
+ * Compressed ISAM support everywhere.
+ * Bug fixes regarding masking characters in queries.
+ * Redesigned Regexp-2 queries.
+ *
+ * Revision 1.63  1996/10/29 14:09:39  adam
+ * Use of cisam system - enabled if setting isamc is 1.
+ *
+ * Revision 1.62  1996/10/11 10:57:01  adam
+ * New module recctrl. Used to manage records (extract/retrieval).
+ * Several files have been moved to the recctrl sub directory.
+ *
+ * Revision 1.61  1996/06/06 12:08:37  quinn
+ * Added showRecord function
+ *
+ * Revision 1.60  1996/06/04  10:18:12  adam
+ * Search/scan uses character mapping module.
+ *
+ * Revision 1.59  1996/05/14  15:47:07  adam
+ * Cleanup of various buffer size entities.
+ *
+ * Revision 1.58  1996/05/14  06:16:38  adam
+ * Compact use/set bytes used in search service.
+ *
+ * Revision 1.57  1996/05/13 14:23:04  adam
+ * Work on compaction of set/use bytes in dictionary.
+ *
+ * Revision 1.56  1996/05/09  09:54:42  adam
+ * Server supports maps from one logical attributes to a list of physical
+ * attributes.
+ * The extraction process doesn't make space consuming 'any' keys.
+ *
+ * Revision 1.55  1996/05/09  07:28:55  quinn
+ * Work towards phrases and multiple registers
+ *
+ * Revision 1.54  1996/05/01  13:46:35  adam
  * First work on multiple records in one file.
  * New option, -offset, to the "unread" command in the filter module.
  *
 #include <assert.h>
 #include <unistd.h>
 #include <fcntl.h>
-#include <ctype.h>
 
-#include <alexutil.h>
 #include <recctrl.h>
 #include "index.h"
 
-#include "recindex.h"
+#include "zinfo.h"
 
 static Dict matchDict;
 
@@ -222,6 +256,8 @@ static int records_updated = 0;
 static int records_deleted = 0;
 static int records_processed = 0;
 
+static ZebTargetInfo *zti = NULL;
+
 static void logRecord (int showFlag)
 {
     if (!showFlag)
@@ -236,6 +272,8 @@ static void logRecord (int showFlag)
 
 void key_open (int mem)
 {
+    if (!mem)
+        mem = atoi(res_get_def (common_resource, "memMax", "4"))*1024*1024;
     if (mem < 50000)
         mem = 50000;
     key_buf = xmalloc (mem);
@@ -252,12 +290,15 @@ void key_open (int mem)
     }
     assert (!records);
     records = rec_open (1);
+#if 1
+    zti = zebTargetInfo_open (records, 1);
+#endif
 }
 
 struct encode_info {
     int  sysno;
     int  seqno;
-    char buf[512];
+    char buf[768];
 };
 
 void encode_key_init (struct encode_info *i)
@@ -314,20 +355,39 @@ void encode_key_write (char *k, struct encode_info *i, FILE *outf)
     }
 }
 
+static int key_y_len;
+
+static int key_y_compare (const void *p1, const void *p2)
+{
+    int r;
+
+    if ((r = key_compare (*(char**) p1 + key_y_len + 1,
+                          *(char**) p2 + key_y_len + 1)))
+         return r;
+    return *(*(char**) p1 + key_y_len) - *(*(char**) p2 + key_y_len);
+}
+
+static int key_x_compare (const void *p1, const void *p2)
+{
+    return strcmp (*(char**) p1, *(char**) p2);
+}
+
 void key_flush (void)
 {
     FILE *outf;
     char out_fname[200];
     char *prevcp, *cp;
     struct encode_info encode_info;
+    int i;
     
     if (ptr_i <= 0)
         return;
 
     key_file_no++;
     logf (LOG_LOG, "sorting section %d", key_file_no);
+#if 1
     qsort (key_buf + ptr_top-ptr_i, ptr_i, sizeof(char*), key_qsort_compare);
-    sprintf (out_fname, TEMP_FNAME, key_file_no);
+    getFnameTmp (out_fname, key_file_no);
 
     if (!(outf = fopen (out_fname, "w")))
     {
@@ -351,6 +411,42 @@ void key_flush (void)
         else
             encode_key_write (cp + strlen(cp), &encode_info, outf);
     }
+#else
+    qsort (key_buf + ptr_top-ptr_i, ptr_i, sizeof(char*), key_x_compare);
+    getFnameTmp (out_fname, key_file_no);
+
+    if (!(outf = fopen (out_fname, "w")))
+    {
+        logf (LOG_FATAL|LOG_ERRNO, "fopen (4) %s", out_fname);
+        exit (1);
+    }
+    logf (LOG_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
+            logf (LOG_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);
+            }
+            if (!i)
+                break;
+            prevcp = key_buf[ptr_top-ptr_i];
+        }
+#endif
     if (fclose (outf))
     {
         logf (LOG_FATAL|LOG_ERRNO, "fclose %s", out_fname);
@@ -365,6 +461,9 @@ int key_close (void)
 {
     key_flush ();
     xfree (key_buf);
+#if 1
+    zebTargetInfo_close (zti, 1);
+#endif
     rec_close (&records);
     dict_close (matchDict);
 
@@ -418,32 +517,35 @@ static void addRecordKey (const RecWord *p)
     else
         reckeys.prevAttrUse = attrUse;
 
+    *dst++ = lead;
+
+    if (!(lead & 1))
+    {
+        memcpy (dst, &attrSet, sizeof(attrSet));
+        dst += sizeof(attrSet);
+    }
+    if (!(lead & 2))
+    {
+        memcpy (dst, &attrUse, sizeof(attrUse));
+        dst += sizeof(attrUse);
+    }
     switch (p->which)
     {
-    case Word_String:
-        *dst++ = lead;
-
-        if (!(lead & 1))
-        {
-            memcpy (dst, &attrSet, sizeof(attrSet));
-            dst += sizeof(attrSet);
-        }
-        if (!(lead & 2))
-        {
-            memcpy (dst, &attrUse, sizeof(attrUse));
-            dst += sizeof(attrUse);
-        }
-        for (i = 0; p->u.string[i]; i++)
-            *dst++ = p->u.string[i];
-        *dst++ = '\0';
-
-        memcpy (dst, &p->seqno, sizeof(p->seqno));
-        dst += sizeof(p->seqno);
-
-        break;
-    default:
-        return;
+        case Word_String:
+            *dst++ = 'w';
+            break;
+        case Word_Phrase:
+            *dst++ = 'p';
+            break;
+        case Word_Numeric:
+            *dst++ = 'n';
     }
+    for (i = 0; p->u.string[i]; i++)
+        *dst++ = p->u.string[i];
+    *dst++ = '\0';
+
+    memcpy (dst, &p->seqno, sizeof(p->seqno));
+    dst += sizeof(p->seqno);
     reckeys.buf_used = dst - reckeys.buf;
 }
 
@@ -453,6 +555,12 @@ static void flushRecordKeys (SYSNO sysno, int cmd, struct recKeys *reckeys,
     char attrSet = -1;
     short attrUse = -1;
     int off = 0;
+
+    if (zebTargetInfo_curDatabase (zti, databaseName))
+    {
+        if (zebTargetInfo_newDatabase (zti, databaseName))
+            abort ();
+    }
     while (off < reckeys->buf_used)
     {
         const char *src = reckeys->buf + off;
@@ -475,10 +583,14 @@ static void flushRecordKeys (SYSNO sysno, int cmd, struct recKeys *reckeys,
             key_flush ();
         ++ptr_i;
         key_buf[ptr_top-ptr_i] = (char*)key_buf + key_buf_used;
-        key_buf_used += index_word_prefix ((char*)key_buf + key_buf_used,
-                                           attrSet, attrUse, databaseName);
+
+        lead = zebTargetInfo_lookupSU (zti, attrSet, attrUse);
+        if (lead < 0)
+            lead = zebTargetInfo_addSU (zti, attrSet, attrUse);
+        assert (lead > 0);
+        ((char*) key_buf) [key_buf_used++] = lead;
         while (*src)
-            ((char*)key_buf) [key_buf_used++] = index_char_cvt (*src++);
+            ((char*)key_buf) [key_buf_used++] = *src++;
         src++;
         ((char*)key_buf) [key_buf_used++] = '\0';
         
@@ -547,21 +659,6 @@ static const char **searchRecordKey (struct recKeys *reckeys,
     return ws;
 }
 
-static void addRecordKeyAny (const RecWord *p)
-{
-    if (p->attrSet != 1 || p->attrUse != 1016)
-    {
-        RecWord w;
-
-        memcpy (&w, p, sizeof(w));
-        w.attrSet = 1;
-        w.attrUse = 1016;
-        addRecordKey (&w);
-    }
-    if (p->attrSet != -1)
-        addRecordKey (p);
-}
-
 struct file_read_info {
     off_t file_max;
     off_t file_offset;
@@ -813,7 +910,7 @@ static int recordExtract (SYSNO *sysno, const char *fname,
         extractCtrl.fh = fi;
         extractCtrl.subType = subType;
         extractCtrl.init = wordInit;
-        extractCtrl.add = addRecordKeyAny;
+        extractCtrl.add = addRecordKey;
 
         reckeys.buf_used = 0;
         reckeys.prevAttrUse = -1;
@@ -824,6 +921,8 @@ static int recordExtract (SYSNO *sysno, const char *fname,
         extractCtrl.readf = file_read;
         extractCtrl.seekf = file_seek;
         extractCtrl.endf = file_end;
+        extractCtrl.map_chrs_input = map_chrs_input;
+        extractCtrl.flagShowRecords = rGroup->flagShowRecords;
         r = (*recType->extract)(&extractCtrl);
 
         if (r)      
@@ -878,7 +977,7 @@ static int recordExtract (SYSNO *sysno, const char *fname,
         }
         logInfo.op = "add";
         if (rGroup->fileVerboseFlag)
-            logf (LOG_LOG, "add %s %s %ld", rGroup->recordType,
+            logf (LOG_LOG, "add %s %s+%ld", rGroup->recordType,
                   fname, (long) recordOffset);
         rec = rec_new (records);
         *sysno = rec->sysno;