New module recctrl. Used to manage records (extract/retrieval).
[idzebra-moved-to-github.git] / index / extract.c
index 6f7686c..ff5081c 100644 (file)
@@ -1,10 +1,63 @@
 /*
- * 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.47  1996-01-17 14:57:48  adam
+ * 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.
+ *
+ * Revision 1.53  1996/04/26  12:09:43  adam
+ * Added a few comments.
+ *
+ * Revision 1.52  1996/04/25  13:27:57  adam
+ * Function recordExtract modified so that files with no keys (possibly empty)
+ * are ignored.
+ *
+ * Revision 1.51  1996/03/19  11:08:42  adam
+ * Bug fix: Log preamble wasn't always turned off after recordExtract.
+ *
+ * Revision 1.50  1996/02/12  18:45:36  adam
+ * New fileVerboseFlag in record group control.
+ *
+ * Revision 1.49  1996/02/05  12:29:57  adam
+ * Logging reduced a bit.
+ * The remaining running time is estimated during register merge.
+ *
+ * Revision 1.48  1996/02/01  20:53:26  adam
+ * The temporary per-record keys are compacted a little, and duplication
+ * of the per-records keys are avoided when they are saved in the record
+ * information buffer.
+ *
+ * Revision 1.47  1996/01/17  14:57:48  adam
  * Prototype changed for reader functions in extract/retrieve. File
  *  is identified by 'void *' instead of 'int.
  *
 #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;
 
@@ -194,6 +246,21 @@ static int key_file_no;
 static int records_inserted = 0;
 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)
+        ++records_processed;
+    if (showFlag || !(records_processed % 1000))
+    {
+        logf (LOG_LOG, "Records: %7d i/u/d %d/%d/%d", 
+              records_processed, records_inserted, records_updated,
+              records_deleted);
+    }
+}
 
 void key_open (int mem)
 {
@@ -213,12 +280,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)
@@ -326,12 +396,13 @@ int key_close (void)
 {
     key_flush ();
     xfree (key_buf);
+#if 1
+    zebTargetInfo_close (zti, 1);
+#endif
     rec_close (&records);
     dict_close (matchDict);
 
-    logf (LOG_LOG, "Records inserted %6d", records_inserted);
-    logf (LOG_LOG, "Records updated  %6d", records_updated);
-    logf (LOG_LOG, "Records deleted  %6d", records_deleted);
+    logRecord (1);
     return key_file_no;
 }
 
@@ -346,6 +417,8 @@ struct recKeys {
     int buf_used;
     int buf_max;
     char *buf;
+    char prevAttrSet;
+    short prevAttrUse;
 } reckeys;
 
 static void addRecordKey (const RecWord *p)
@@ -354,68 +427,105 @@ static void addRecordKey (const RecWord *p)
     char attrSet;
     short attrUse;
     size_t i;
+    int lead = 0;
 
     if (reckeys.buf_used+1024 > reckeys.buf_max)
     {
         char *b;
 
-        b = xmalloc (reckeys.buf_max += 65000);
+        b = xmalloc (reckeys.buf_max += 128000);
         if (reckeys.buf_used > 0)
             memcpy (b, reckeys.buf, reckeys.buf_used);
         xfree (reckeys.buf);
         reckeys.buf = b;
     }
     dst = reckeys.buf + reckeys.buf_used;
-    switch (p->which)
+
+    attrSet = p->attrSet;
+    if (reckeys.buf_used > 0 && reckeys.prevAttrSet == attrSet)
+        lead |= 1;
+    else
+        reckeys.prevAttrSet = attrSet;
+    attrUse = p->attrUse;
+    if (reckeys.buf_used > 0 && reckeys.prevAttrUse == attrUse)
+        lead |= 2;
+    else
+        reckeys.prevAttrUse = attrUse;
+
+    *dst++ = lead;
+
+    if (!(lead & 1))
     {
-    case Word_String:
-        attrSet = p->attrSet;
         memcpy (dst, &attrSet, sizeof(attrSet));
         dst += sizeof(attrSet);
-
-        attrUse = p->attrUse;
+    }
+    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;
     }
+    switch (p->which)
+    {
+        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;
 }
 
 static void flushRecordKeys (SYSNO sysno, int cmd, struct recKeys *reckeys, 
                              const char *databaseName)
 {
+    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;
-        char attrSet;
-        short attrUse;
         struct it_key key;
-        
-        memcpy (&attrSet, src, sizeof(attrSet));
-        src += sizeof(attrSet);
-
-        memcpy (&attrUse, src, sizeof(attrUse));
-        src += sizeof(attrUse);
+        int lead;
+    
+        lead = *src++;
 
+        if (!(lead & 1))
+        {
+            memcpy (&attrSet, src, sizeof(attrSet));
+            src += sizeof(attrSet);
+        }
+        if (!(lead & 2))
+        {
+            memcpy (&attrUse, src, sizeof(attrUse));
+            src += sizeof(attrUse);
+        }
         if (key_buf_used + 1024 > (ptr_top-ptr_i)*sizeof(char*))
             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';
         
@@ -484,30 +594,12 @@ 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);
-}
-
-#define FILE_READ_BUFSIZE 4096
 struct file_read_info {
-    int file_noread;
+    off_t file_max;
+    off_t file_offset;
+    off_t file_moffset;
+    int file_more;
     int fd;
-#if FILE_READ_BUFSIZE
-    char *file_buf;
-    int file_offset;
-    int file_bufsize;
-#endif
 };
 
 static struct file_read_info *file_read_start (int fd)
@@ -515,82 +607,56 @@ static struct file_read_info *file_read_start (int fd)
     struct file_read_info *fi = xmalloc (sizeof(*fi));
 
     fi->fd = fd;
-    fi->file_noread = 0;
-#if FILE_READ_BUFSIZE
-    fi->file_offset = 0;
-    fi->file_buf = xmalloc (FILE_READ_BUFSIZE);
-    fi->file_bufsize = read (fd, fi->file_buf, FILE_READ_BUFSIZE);
-#endif
+    fi->file_max = 0;
+    fi->file_moffset = 0;
     return fi;
 }
 
 static void file_read_stop (struct file_read_info *fi)
 {
     assert (fi);
-#if FILE_READ_BUFSIZE
-    xfree (fi->file_buf);
-    fi->file_buf = NULL;
-#endif
     xfree (fi);
 }
 
+static off_t file_seek (void *handle, off_t offset)
+{
+    struct file_read_info *p = handle;
+    p->file_offset = offset;
+    return lseek (p->fd, offset, SEEK_SET);
+}
+
 static int file_read (void *handle, char *buf, size_t count)
 {
     struct file_read_info *p = handle;
     int fd = p->fd;
-#if FILE_READ_BUFSIZE
-    int l = p->file_bufsize - p->file_offset;
-
-    if (count > l)
-    {
-        int r;
-        if (l > 0)
-            memcpy (buf, p->file_buf + p->file_offset, l);
-        count = count-l;
-        if (count > FILE_READ_BUFSIZE)
-        {
-            if ((r = read (fd, buf + l, count)) == -1)
-            {
-                logf (LOG_FATAL|LOG_ERRNO, "read");
-                exit (1);
-            }
-            p->file_bufsize = 0;
-            p->file_offset = 0;
-            p->file_noread += l+r;
-            return l+r;
-        }
-        p->file_bufsize = r = read (fd, p->file_buf, FILE_READ_BUFSIZE);
-        if (r == -1)
-        {
-            logf (LOG_FATAL|LOG_ERRNO, "read");
-            exit (1);
-        }
-        else if (r <= count)
-        {
-            p->file_offset = r;
-            memcpy (buf + l, p->file_buf, r);
-            p->file_noread += l+r;
-            return l+r;
-        }
-        else
-        {
-            p->file_offset = count;
-            memcpy (buf + l, p->file_buf, count - l);
-            p->file_noread += count;
-            return count;
-        }
-    }
-    memcpy (buf, p->file_buf + p->file_offset, count);
-    p->file_offset += count;
-    p->file_noread += count;
-    return count;
-#else
     int r;
     r = read (fd, buf, count);
     if (r > 0)
-        p->file_noread += r;
+    {
+        p->file_offset += r;
+        if (p->file_offset > p->file_max)
+            p->file_max = p->file_offset;
+    }
     return r;
-#endif
+}
+
+static void file_begin (void *handle)
+{
+    struct file_read_info *p = handle;
+
+    p->file_offset = p->file_moffset;
+    if (p->file_moffset)
+        lseek (p->fd, p->file_moffset, SEEK_SET);
+    p->file_more = 0;
+}
+
+static void file_end (void *handle, off_t offset)
+{
+    struct file_read_info *p = handle;
+
+    assert (p->file_more == 0);
+    p->file_more = 1;
+    p->file_moffset = offset;
 }
 
 static int atois (const char **s)
@@ -665,8 +731,8 @@ static char *fileMatchStr (struct recKeys *reckeys, struct recordGroup *rGroup,
                 }
             if (first)
             {
-                logf (LOG_WARN, "Record in file %s didn't contain match"
-                      " fields in (%d,%d)", fname, attrSet, attrUse);
+                logf (LOG_WARN, "Record didn't contain match"
+                      " fields in (%d,%d)", attrSet, attrUse);
                 return NULL;
             }
         }
@@ -736,6 +802,25 @@ static char *fileMatchStr (struct recKeys *reckeys, struct recordGroup *rGroup,
     return dstBuf;
 }
 
+struct recordLogInfo {
+    const char *fname;
+    char *op;
+    struct recordGroup *rGroup;
+};
+     
+static void recordLogPreamble (int level, const char *msg, void *info)
+{
+    struct recordLogInfo *p = info;
+    FILE *outf = log_file ();
+
+    if (level & LOG_LOG)
+        return ;
+    if (p->op) 
+        fprintf (outf, "%s of ", p->op);
+    fprintf (outf, "%s type %s\n", p->rGroup->recordType, p->fname);
+    log_event_start (NULL, NULL);
+}
+
 static int recordExtract (SYSNO *sysno, const char *fname,
                           struct recordGroup *rGroup, int deleteFlag,
                           struct file_read_info *fi, RecType recType,
@@ -745,25 +830,49 @@ static int recordExtract (SYSNO *sysno, const char *fname,
     int r;
     char *matchStr;
     SYSNO sysnotmp;
+    off_t recordOffset = 0;
     Record rec;
+    struct recordLogInfo logInfo;
 
+    logInfo.fname = fname;
+    logInfo.op = NULL;
+    logInfo.rGroup = rGroup;
+    log_event_start (recordLogPreamble, &logInfo);
+    
     if (fi->fd != -1)
     {
+        /* we are going to read from a file, so prepare the extraction */
         extractCtrl.fh = fi;
-        /* extract keys */
         extractCtrl.subType = subType;
         extractCtrl.init = wordInit;
-        extractCtrl.add = addRecordKeyAny;
+        extractCtrl.add = addRecordKey;
 
         reckeys.buf_used = 0;
+        reckeys.prevAttrUse = -1;
+        reckeys.prevAttrSet = -1;
+
+        recordOffset = fi->file_moffset;
+        extractCtrl.offset = recordOffset;
         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)      
         {
+            /* error occured during extraction ... */
             logf (LOG_WARN, "Couldn't extract file %s, code %d", fname, r);
             return 0;
         }
+        if (reckeys.buf_used == 0)
+        {
+            /* the extraction process returned no information - the record
+               is probably empty */
+            logf (LOG_WARN, "Empty file %s", fname);
+            return 0;
+        }
     }
 
     /* perform match if sysno not known and if match criteria is specified */
@@ -787,21 +896,24 @@ static int recordExtract (SYSNO *sysno, const char *fname,
             }
             else
             {
-                logf (LOG_WARN, "Record not inserted");
+                logf (LOG_WARN, "Bad match criteria");
                 return 0;
             }
         }
     }
 
-    /* new record ? */
     if (! *sysno)
     {
+        /* new record */
         if (deleteFlag)
         {
-            logf (LOG_LOG, "? %s", fname);
+            logf (LOG_LOG, "Cannot delete new record");
             return 1;
         }
-        logf (LOG_LOG, "add %s %s", rGroup->recordType, fname);
+        logInfo.op = "add";
+        if (rGroup->fileVerboseFlag)
+            logf (LOG_LOG, "add %s %s+%ld", rGroup->recordType,
+                  fname, (long) recordOffset);
         rec = rec_new (records);
         *sysno = rec->sysno;
 
@@ -815,6 +927,7 @@ static int recordExtract (SYSNO *sysno, const char *fname,
     }
     else
     {
+        /* record already exists */
         struct recKeys delkeys;
 
         rec = rec_get (records, *sysno);
@@ -824,52 +937,68 @@ static int recordExtract (SYSNO *sysno, const char *fname,
         flushRecordKeys (*sysno, 0, &delkeys, rec->info[recInfo_databaseName]);
         if (deleteFlag)
         {
+            /* record going to be deleted */
+            logInfo.op = "delete";
             if (!delkeys.buf_used)
             {
-                logf (LOG_WARN, "cannot delete %s: storeKeys false",
-                      fname);
+                logf (LOG_WARN, "cannot delete; storeKeys false");
             }
             else
             {
-                logf (LOG_LOG, "delete %s %s", rGroup->recordType, fname);
+                if (rGroup->fileVerboseFlag)
+                    logf (LOG_LOG, "delete %s %s %ld", rGroup->recordType,
+                          fname, (long) recordOffset);
                 records_deleted++;
                 if (matchStr)
                     dict_delete (matchDict, matchStr);
                 rec_del (records, &rec);
             }
+            logRecord (0);
             return 1;
         }
         else
         {
+            /* record going to be updated */
+            logInfo.op = "update";
             if (!delkeys.buf_used)
             {
-                logf (LOG_WARN, "cannot update %s: storeKeys false",
-                      fname);
+                logf (LOG_WARN, "cannot update; storeKeys false");
             }
             else
             {
-                logf (LOG_LOG, "update %s %s", rGroup->recordType,
-                      fname);
+                if (rGroup->fileVerboseFlag)
+                    logf (LOG_LOG, "update %s %s %ld", rGroup->recordType,
+                          fname, (long) recordOffset);
                 flushRecordKeys (*sysno, 1, &reckeys, rGroup->databaseName); 
                 records_updated++;
             }
         }
     }
+    /* update file type */
     xfree (rec->info[recInfo_fileType]);
     rec->info[recInfo_fileType] =
         rec_strdup (rGroup->recordType, &rec->size[recInfo_fileType]);
 
+    /* update filename */
     xfree (rec->info[recInfo_filename]);
     rec->info[recInfo_filename] =
         rec_strdup (fname, &rec->size[recInfo_filename]);
 
+    /* update delete keys */
     xfree (rec->info[recInfo_delKeys]);
     if (reckeys.buf_used > 0 && rGroup->flagStoreKeys == 1)
     {
+#if 1
+        rec->size[recInfo_delKeys] = reckeys.buf_used;
+        rec->info[recInfo_delKeys] = reckeys.buf;
+        reckeys.buf = NULL;
+        reckeys.buf_max = 0;
+#else
         rec->info[recInfo_delKeys] = xmalloc (reckeys.buf_used);
         rec->size[recInfo_delKeys] = reckeys.buf_used;
         memcpy (rec->info[recInfo_delKeys], reckeys.buf,
                 rec->size[recInfo_delKeys]);
+#endif
     }
     else
     {
@@ -877,30 +1006,24 @@ static int recordExtract (SYSNO *sysno, const char *fname,
         rec->size[recInfo_delKeys] = 0;
     }
 
+    /* update store data */
     xfree (rec->info[recInfo_storeData]);
     if (rGroup->flagStoreData == 1)
     {
-        rec->size[recInfo_storeData] = fi->file_noread;
-        rec->info[recInfo_storeData] = xmalloc (fi->file_noread);
-#if FILE_READ_BUFSIZE
-        if (fi->file_noread < FILE_READ_BUFSIZE)
-           memcpy (rec->info[recInfo_storeData], fi->file_buf,
-                    fi->file_noread);
-        else
-#endif
+        rec->size[recInfo_storeData] = fi->file_max;
+        rec->info[recInfo_storeData] = xmalloc (fi->file_max);
+        if (lseek (fi->fd, recordOffset, SEEK_SET) < 0)
         {
-            if (lseek (fi->fd, 0L, SEEK_SET) < 0)
-            {
-                logf (LOG_ERRNO|LOG_FATAL, "seek to 0 in %s", fname);
-                exit (1);
-            }
-            if (read (fi->fd, rec->info[recInfo_storeData], fi->file_noread) 
-                < fi->file_noread)
-            {
-                logf (LOG_ERRNO|LOG_FATAL, "read %d bytes of %s",
-                      fi->file_noread, fname);
-                exit (1);
-            }
+            logf (LOG_ERRNO|LOG_FATAL, "seek to %ld in %s", fname,
+                  (long) recordOffset);
+            exit (1);
+        }
+        if (read (fi->fd, rec->info[recInfo_storeData], fi->file_max)
+            < fi->file_max)
+        {
+            logf (LOG_ERRNO|LOG_FATAL, "read %d bytes of %s",
+                  fi->file_max, fname);
+            exit (1);
         }
     }
     else
@@ -908,18 +1031,28 @@ static int recordExtract (SYSNO *sysno, const char *fname,
         rec->info[recInfo_storeData] = NULL;
         rec->size[recInfo_storeData] = 0;
     }
+    /* update database name */
     xfree (rec->info[recInfo_databaseName]);
     rec->info[recInfo_databaseName] =
         rec_strdup (rGroup->databaseName, &rec->size[recInfo_databaseName]); 
 
+    /* update offset */
+    xfree (rec->info[recInfo_offset]);
+
+    rec->size[recInfo_offset] = sizeof(recordOffset);
+    rec->info[recInfo_offset] = xmalloc (sizeof(recordOffset));
+    memcpy (rec->info[recInfo_offset], &recordOffset, sizeof(recordOffset));
+    
+    /* commit this record */
     rec_put (records, &rec);
+    logRecord (0);
     return 1;
 }
 
 int fileExtract (SYSNO *sysno, const char *fname, 
                  const struct recordGroup *rGroupP, int deleteFlag)
 {
-    int i, fd;
+    int r, i, fd;
     char gprefix[128];
     char ext[128];
     char ext_res[128];
@@ -959,14 +1092,16 @@ int fileExtract (SYSNO *sysno, const char *fname,
             sprintf (ext_res, "%srecordType", gprefix);
             if (!(rGroup->recordType = res_get (common_resource, ext_res)))
             {
-                logf (LOG_LOG, "? %s", fname);
+                if (rGroup->fileVerboseFlag)
+                    logf (LOG_LOG, "? %s", fname);
                 return 0;
             }
         }
     }
     if (!rGroup->recordType)
     {
-        logf (LOG_LOG, "? record %s", fname);
+        if (rGroup->fileVerboseFlag)
+            logf (LOG_LOG, "? record %s", fname);
         return 0;
     }
     if (!(recType = recType_byName (rGroup->recordType, subType)))
@@ -1037,10 +1172,16 @@ int fileExtract (SYSNO *sysno, const char *fname,
         }
     }
     fi = file_read_start (fd);
-    recordExtract (sysno, fname, rGroup, deleteFlag, fi, recType, subType);
+    do
+    {
+        file_begin (fi);
+        r = recordExtract (sysno, fname, rGroup, deleteFlag, fi,
+                           recType, subType);
+    } while (r && !sysno && fi->file_more);
+    log_event_start (NULL, NULL);
     file_read_stop (fi);
     if (fd != -1)
         close (fd);
-    return 1;
+    return r;
 }