Updated WIN32 code specific sections. Changed header.
[idzebra-moved-to-github.git] / index / extract.c
index f3bceae..ebd04ac 100644 (file)
@@ -1,10 +1,40 @@
 /*
- * Copyright (C) 1994-1998, Index Data I/S 
+ * Copyright (C) 1994-1999, Index Data 
  * All rights reserved.
  * Sebastian Hammer, Adam Dickmeiss
  *
  * $Log: extract.c,v $
- * Revision 1.82  1998-05-20 10:12:15  adam
+ * Revision 1.90  1999-02-02 14:50:52  adam
+ * Updated WIN32 code specific sections. Changed header.
+ *
+ * Revision 1.89  1998/10/28 10:54:38  adam
+ * SDRKit integration.
+ *
+ * Revision 1.88  1998/10/16 08:14:29  adam
+ * Updated record control system.
+ *
+ * Revision 1.87  1998/10/15 13:10:33  adam
+ * Fixed bug in Zebra that caused it to stop indexing when empty
+ * record was read.
+ *
+ * Revision 1.86  1998/10/13 20:33:53  adam
+ * Fixed one log message and change use ordinal to be an unsigned char.
+ *
+ * Revision 1.85  1998/09/22 10:03:41  adam
+ * Changed result sets to be persistent in the sense that they can
+ * be re-searched if needed.
+ * Fixed memory leak in rsm_or.
+ *
+ * Revision 1.84  1998/06/11 15:42:22  adam
+ * Changed the way use attributes are specified in the recordId
+ * specification.
+ *
+ * Revision 1.83  1998/06/08 14:43:10  adam
+ * Added suport for EXPLAIN Proxy servers - added settings databasePath
+ * and explainDatabase to facilitate this. Increased maximum number
+ * of databases and attributes in one register.
+ *
+ * Revision 1.82  1998/05/20 10:12:15  adam
  * Implemented automatic EXPLAIN database maintenance.
  * Modified Zebra to work with ASN.1 compiled version of YAZ.
  *
  */
 #include <stdio.h>
 #include <assert.h>
-#ifdef WINDOWS
+#ifdef WIN32
 #include <io.h>
 #else
 #include <unistd.h>
 
 #include "zinfo.h"
 
+#ifndef ZEBRASDR
+#define ZEBRASDR 0
+#endif
+
+#if ZEBRASDR
+#include "zebrasdr.h"
+#endif
+
 static Dict matchDict;
 
 static Records records = NULL;
@@ -598,8 +636,8 @@ static struct recKeys {
 static void addIndexString (RecWord *p, const char *string, int length)
 {
     char *dst;
-    char attrSet;
-    short attrUse;
+    unsigned char attrSet;
+    unsigned short attrUse;
     int lead = 0;
     int diff = 0;
     int *pseqno = &p->seqnos[p->reg_type];
@@ -673,9 +711,10 @@ static void addSortString (RecWord *p, const char *string, int length)
     sk->next = sortKeys;
     sortKeys = sk;
 
-    sk->string = xmalloc (p->length);
-    sk->length = p->length;
-    memcpy (sk->string, p->string, p->length);
+    sk->string = xmalloc (length);
+    sk->length = length;
+    memcpy (sk->string, string, length);
+
     sk->attrSet = p->attrSet;
     sk->attrUse = p->attrUse;
 }
@@ -806,8 +845,8 @@ static void flushSortKeys (SYSNO sysno, int cmd)
 
 static void flushRecordKeys (SYSNO sysno, int cmd, struct recKeys *reckeys)
 {
-    char attrSet = -1;
-    short attrUse = -1;
+    unsigned char attrSet = (unsigned char) -1;
+    unsigned short attrUse = (unsigned short) -1;
     int seqno = 0;
     int off = 0;
 
@@ -839,7 +878,8 @@ static void flushRecordKeys (SYSNO sysno, int cmd, struct recKeys *reckeys)
         if (ch < 0)
             ch = zebraExplain_addSU (zti, attrSet, attrUse);
         assert (ch > 0);
-        ((char*) key_buf) [key_buf_used++] = ch;
+       key_buf_used += key_SU_code (ch, ((char*)key_buf) + key_buf_used);
+
         while (*src)
             ((char*)key_buf) [key_buf_used++] = *src++;
         src++;
@@ -924,11 +964,13 @@ static const char **searchRecordKey (struct recKeys *reckeys,
 }
 
 struct file_read_info {
-    off_t file_max;
-    off_t file_offset;
-    off_t file_moffset;
+    off_t file_max;        /* maximum offset so far */
+    off_t file_offset;      /* current offset */
+    off_t file_moffset;     /* offset of rec/rec boundary */
     int file_more;
     int fd;
+    char *sdrbuf;
+    int sdrmax;
 };
 
 static struct file_read_info *file_read_start (int fd)
@@ -938,6 +980,8 @@ static struct file_read_info *file_read_start (int fd)
     fi->fd = fd;
     fi->file_max = 0;
     fi->file_moffset = 0;
+    fi->sdrbuf = 0;
+    fi->sdrmax = 0;
     return fi;
 }
 
@@ -950,6 +994,8 @@ static off_t file_seek (void *handle, off_t offset)
 {
     struct file_read_info *p = handle;
     p->file_offset = offset;
+    if (p->sdrbuf)
+       return offset;
     return lseek (p->fd, offset, SEEK_SET);
 }
 
@@ -964,7 +1010,16 @@ static int file_read (void *handle, char *buf, size_t count)
     struct file_read_info *p = handle;
     int fd = p->fd;
     int r;
-    r = read (fd, buf, count);
+    if (p->sdrbuf)
+    {
+       r = count;
+       if (r > p->sdrmax - p->file_offset)
+           r = p->sdrmax - p->file_offset;
+       if (r)
+           memcpy (buf, p->sdrbuf + p->file_offset, r);
+    }
+    else
+       r = read (fd, buf, count);
     if (r > 0)
     {
         p->file_offset += r;
@@ -979,7 +1034,7 @@ static void file_begin (void *handle)
     struct file_read_info *p = handle;
 
     p->file_offset = p->file_moffset;
-    if (p->file_moffset)
+    if (!p->sdrbuf && p->file_moffset)
         lseek (p->fd, p->file_moffset, SEEK_SET);
     p->file_more = 0;
 }
@@ -993,26 +1048,13 @@ static void file_end (void *handle, off_t offset)
     p->file_moffset = offset;
 }
 
-static int atois (const char **s)
-{
-    int val = 0, c;
-    while ( (c=**s) >= '0' && c <= '9')
-    {
-        val = val*10 + c - '0';
-        ++(*s);
-    }
-    return val;
-}
-
 static char *fileMatchStr (struct recKeys *reckeys, struct recordGroup *rGroup,
-                           const char *fname,
-                           const char *spec)
+                           const char *fname, const char *spec)
 {
     static char dstBuf[2048];
     char *dst = dstBuf;
     const char *s = spec;
     static const char **w;
-    int i;
 
     while (1)
     {
@@ -1022,21 +1064,39 @@ static char *fileMatchStr (struct recKeys *reckeys, struct recordGroup *rGroup,
             break;
         if (*s == '(')
         {
+           char attset_str[64], attname_str[64];
+           data1_attset *attset;
+           int i;
             char matchFlag[32];
-            int attrSet, attrUse;
+            int attSet = 1, attUse = 1;
             int first = 1;
 
             s++;
-            attrSet = atois (&s);
-            if (*s != ',')
-            {
-                logf (LOG_WARN, "Missing , in match criteria %s in group %s",
-                      spec, rGroup->groupName ? rGroup->groupName : "none");
-                return NULL;
-            }
-            s++;
-            attrUse = atois (&s);
-            w = searchRecordKey (reckeys, attrSet, attrUse);
+           for (i = 0; *s && *s != ',' && *s != ')'; s++)
+               if (i < 63)
+                   attset_str[i++] = *s;
+           attset_str[i] = '\0';
+
+           if (*s == ',')
+           {
+               s++;
+               for (i = 0; *s && *s != ')'; s++)
+                   if (i < 63)
+                       attname_str[i++] = *s;
+               attname_str[i] = '\0';
+           }
+           
+           if ((attset = data1_get_attset (rGroup->dh, attset_str)))
+           {
+               data1_att *att;
+               attSet = attset->reference;
+               att = data1_getattbyname(rGroup->dh, attset, attname_str);
+               if (att)
+                   attUse = att->value;
+               else
+                   attUse = atoi (attname_str);
+           }
+            w = searchRecordKey (reckeys, attSet, attUse);
             assert (w);
 
             if (*s == ')')
@@ -1066,7 +1126,7 @@ static char *fileMatchStr (struct recKeys *reckeys, struct recordGroup *rGroup,
             if (first)
             {
                 logf (LOG_WARN, "Record didn't contain match"
-                      " fields in (%d,%d)", attrSet, attrUse);
+                      " fields in (%s,%s)", attset_str, attname_str);
                 return NULL;
             }
         }
@@ -1233,7 +1293,7 @@ static int recordExtract (SYSNO *sysno, const char *fname,
                 return 1;
             logf (LOG_WARN, "No keys generated for file %s", fname);
             logf (LOG_WARN, " The file is probably empty");
-            return 0;
+            return 1;
         }
     }
 
@@ -1406,8 +1466,8 @@ static int recordExtract (SYSNO *sysno, const char *fname,
         rec->info[recInfo_storeData] = xmalloc (recordAttr->recordSize);
         if (lseek (fi->fd, recordOffset, SEEK_SET) < 0)
         {
-            logf (LOG_ERRNO|LOG_FATAL, "seek to %ld in %s", fname,
-                  (long) recordOffset);
+            logf (LOG_ERRNO|LOG_FATAL, "seek to %ld in %s",
+                  (long) recordOffset, fname);
             exit (1);
         }
         if (read (fi->fd, rec->info[recInfo_storeData], recordAttr->recordSize)
@@ -1460,12 +1520,10 @@ int fileExtract (SYSNO *sysno, const char *fname,
     logf (LOG_DEBUG, "fileExtract %s", fname);
 
     /* determine file extension */
+    *ext = '\0';
     for (i = strlen(fname); --i >= 0; )
         if (fname[i] == '/')
-        {
-            strcpy (ext, "");
             break;
-        }
         else if (fname[i] == '.')
         {
             strcpy (ext, fname+i+1);
@@ -1478,21 +1536,19 @@ int fileExtract (SYSNO *sysno, const char *fname,
         if (!(rGroup->recordType = res_get (common_resource, ext_res)))
         {
             sprintf (ext_res, "%srecordType", gprefix);
-            if (!(rGroup->recordType = res_get (common_resource, ext_res)))
-            {
-                if (records_processed < rGroup->fileVerboseLimit)
-                    logf (LOG_LOG, "? %s", fname);
-                return 0;
-            }
+            rGroup->recordType = res_get (common_resource, ext_res);
         }
     }
     if (!rGroup->recordType)
     {
         if (records_processed < rGroup->fileVerboseLimit)
-            logf (LOG_LOG, "? record %s", fname);
+            logf (LOG_LOG, "? %s", fname);
         return 0;
     }
-    if (!(recType = recType_byName (rGroup->recordType, subType)))
+    if (!*rGroup->recordType)
+       return 0;
+    if (!(recType =
+         recType_byName (rGroup->recTypes, rGroup->recordType, subType)))
     {
         logf (LOG_WARN, "No such record type: %s", rGroup->recordType);
         return 0;
@@ -1518,9 +1574,17 @@ int fileExtract (SYSNO *sysno, const char *fname,
     if (!rGroup->databaseName)
         rGroup->databaseName = "Default";
 
+    /* determine if explain database */
+    
+    sprintf (ext_res, "%sexplainDatabase", gprefix);
+    rGroup->explainDatabase =
+       atoi (res_get_def (common_resource, ext_res, "0"));
+
+    /* announce database */
     if (zebraExplain_curDatabase (zti, rGroup->databaseName))
     {
-        if (zebraExplain_newDatabase (zti, rGroup->databaseName))
+        if (zebraExplain_newDatabase (zti, rGroup->databaseName,
+                                     rGroup->explainDatabase))
             abort ();
     }
 
@@ -1555,6 +1619,57 @@ int fileExtract (SYSNO *sysno, const char *fname,
     if (rGroup->flagStoreKeys == -1)
         rGroup->flagStoreKeys = 0;
 
+#if ZEBRASDR
+    if (1)
+    {
+       ZebraSdrHandle h;
+       char xname[128], *xp;
+
+       strncpy (xname, fname, 127);
+       if ((xp = strchr (xname, '.')))
+           *xp = '\0';
+
+        h = zebraSdr_open (xname);
+       if (!h)
+       {
+           logf (LOG_WARN, "sdr open %s", xname);
+           return 0;
+       }
+       for (;;)
+       {
+           unsigned char *buf;
+           char sdr_name[128];
+           int r, segmentno;
+
+           segmentno = zebraSdr_segment (h, 0);
+           sprintf (sdr_name, "%%%s.%d", xname, segmentno);
+           logf (LOG_LOG, "SDR: %s", sdr_name);
+
+#if 1
+           if (segmentno > 20)
+               break;
+#endif
+           r = zebraSdr_read (h, &buf);
+
+           if (!r)
+               break;
+
+            fi = file_read_start (0);
+           fi->sdrbuf = buf;
+           fi->sdrmax = r;
+           do
+           {
+               file_begin (fi);
+               r = recordExtract (sysno, sdr_name, rGroup, deleteFlag, fi,
+                           recType, subType);
+           } while (r && !sysno && fi->file_more);
+           file_read_stop (fi);
+           free (buf);
+       }
+       zebraSdr_close (h);
+       return 1;
+    }
+#endif
     if (sysno && deleteFlag)
         fd = -1;
     else
@@ -1586,7 +1701,7 @@ static int explain_extract (void *handle, Record rec, data1_node *n)
 
     if (zebraExplain_curDatabase (zti, rec->info[recInfo_databaseName]))
     {
-        if (zebraExplain_newDatabase (zti, rec->info[recInfo_databaseName]))
+        if (zebraExplain_newDatabase (zti, rec->info[recInfo_databaseName], 0))
             abort ();
     }