Bug fix: complete phrase search didn't work.
[idzebra-moved-to-github.git] / index / zserver.c
index 9013c55..acaf62c 100644 (file)
@@ -1,10 +1,64 @@
 /*
- * Copyright (C) 1994-1995, Index Data I/S 
+ * Copyright (C) 1995-1996, Index Data I/S 
  * All rights reserved.
  * Sebastian Hammer, Adam Dickmeiss
  *
  * $Log: zserver.c,v $
- * Revision 1.31  1995-12-08 16:22:56  adam
+ * Revision 1.45  1996-12-23 15:30:45  adam
+ * Work on truncation.
+ * Bug fix: result sets weren't deleted after server shut down.
+ *
+ * Revision 1.44  1996/12/11 12:08:01  adam
+ * Added better compression.
+ *
+ * Revision 1.43  1996/11/15 15:03:58  adam
+ * Logging of execution speed by using the times(2) call.
+ *
+ * Revision 1.42  1996/11/08  11:10:36  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.41  1996/10/29 14:09:56  adam
+ * Use of cisam system - enabled if setting isamc is 1.
+ *
+ * Revision 1.40  1996/06/04 10:19:02  adam
+ * Minor changes - removed include of ctype.h.
+ *
+ * Revision 1.39  1996/05/31  09:07:05  quinn
+ * Work on character-set handling
+ *
+ * Revision 1.38  1996/05/14  11:34:01  adam
+ * Scan support in multiple registers/databases.
+ *
+ * Revision 1.37  1996/05/14  06:16:48  adam
+ * Compact use/set bytes used in search service.
+ *
+ * Revision 1.36  1996/05/01 13:46:37  adam
+ * First work on multiple records in one file.
+ * New option, -offset, to the "unread" command in the filter module.
+ *
+ * Revision 1.35  1996/03/26  16:01:14  adam
+ * New setting lockPath: directory of various lock files.
+ *
+ * Revision 1.34  1996/03/20  09:36:46  adam
+ * Function dict_lookup_grep got extra parameter, init_pos, which marks
+ * from which position in pattern approximate pattern matching should occur.
+ * Approximate pattern matching is used in relevance=re-2.
+ *
+ * Revision 1.33  1996/01/17  14:57:56  adam
+ * Prototype changed for reader functions in extract/retrieve. File
+ *  is identified by 'void *' instead of 'int.
+ *
+ * Revision 1.32  1995/12/11  09:12:58  adam
+ * The rec_get function returns NULL if record doesn't exist - will
+ * happen in the server if the result set records have been deleted since
+ * the creation of the set (i.e. the search).
+ * The server saves a result temporarily if it is 'volatile', i.e. the
+ * set is register dependent.
+ *
+ * Revision 1.31  1995/12/08  16:22:56  adam
  * Work on update while servers are running. Three lock files introduced.
  * The servers reload their registers when necessary, but they don't
  * reestablish result sets yet.
 #include <data1.h>
 #include <recctrl.h>
 #include <dmalloc.h>
+
+#define USE_TIMES 1
+#ifdef __linux__
+#define USE_TIMES 1
+#endif
+
+#if USE_TIMES
+#include <sys/times.h>
+#endif
 #include "zserver.h"
 
 ZServerInfo server_info;
 
+#if USE_TIMES
+static struct tms tms1;
+static struct tms tms2;
+#endif
+
 static int register_lock (ZServerInfo *zi)
 {
     time_t lastChange;
@@ -138,6 +206,9 @@ static int register_lock (ZServerInfo *zi)
         state = 0;
     }
     zebraServerLock (state);
+#if USE_TIMES
+    times (&tms1);
+#endif
     if (zi->registerState == state)
     {
         if (zi->registerChange >= lastChange)
@@ -153,23 +224,58 @@ static int register_lock (ZServerInfo *zi)
     zi->registerChange = lastChange;
     if (zi->records)
     {
-        dict_close (zi->wordDict);
-        is_close (zi->wordIsam);
+        zebTargetInfo_close (zi->zti, 0);
+        dict_close (zi->dict);
+        if (zi->isam)
+            is_close (zi->isam);
+        if (zi->isamc)
+            isc_close (zi->isamc);
         rec_close (&zi->records);
     }
     bf_cache (state);
     zi->registerState = state;
     zi->records = rec_open (0);
-    if (!(zi->wordDict = dict_open (FNAME_WORD_DICT, 40, 0)))
+    if (!(zi->dict = dict_open (FNAME_DICT, 40, 0)))
         return -1;
-    if (!(zi->wordIsam = is_open (FNAME_WORD_ISAM, key_compare, 0,
+    zi->isam = NULL;
+    zi->isamc = NULL;
+    if (res_get_match (common_resource, "isam", "c", NULL))
+    {
+        if (!(zi->isamc = isc_open (FNAME_ISAMC, 0, key_isamc_m())))
+            return -1;
+
+    }
+    else
+    {
+        if (!(zi->isam = is_open (FNAME_ISAM, key_compare, 0,
                                   sizeof (struct it_key))))
-        return -1;
+            return -1;
+    }
+    zi->zti = zebTargetInfo_open (zi->records, 0);
+    init_charmap ();
     return 0;
 }
 
-static int register_unlock (ZServerInfo *zi)
+static void register_unlock (ZServerInfo *zi)
 {
+    static int waitSec = -1;
+
+#if USE_TIMES
+    times (&tms2);
+    logf (LOG_LOG, "user/system: %ld/%ld",
+                       (long) (tms2.tms_utime - tms1.tms_utime),
+                       (long) (tms2.tms_stime - tms1.tms_stime));
+#endif
+    if (waitSec == -1)
+    {
+        char *s = res_get (common_resource, "debugRequestWait");
+        if (s)
+            waitSec = atoi (s);
+        else
+            waitSec = 0;
+    }
+    if (waitSec > 0)
+        sleep (waitSec);
     if (zi->registerState != -1)
         zebraServerUnlock (zi->registerState);
 }
@@ -196,15 +302,14 @@ bend_initresult *bend_init (bend_initrequest *q)
             logf (LOG_FATAL, "Cannot open resource `%s'", sob->configname);
             exit (1);
         }
+        bf_lockDir (res_get (common_resource, "lockDir"));
+        data1_set_tabpath (res_get(common_resource, "profilePath"));
     }
-    data1_tabpath = res_get(common_resource, "profilePath");
     server_info.sets = NULL;
     server_info.registerState = -1;  /* trigger open of registers! */
     server_info.registerChange = 0;
 
     server_info.records = NULL;
-    server_info.wordDict = NULL;
-    server_info.wordIsam = NULL;
     server_info.odr = odr_createmem (ODR_ENCODE);
     return &r;
 }
@@ -225,7 +330,7 @@ bend_searchresult *bend_search (void *handle, bend_searchrequest *q, int *fd)
     logf (LOG_LOG, "ResultSet '%s'", q->setname);
     switch (q->query->which)
     {
-    case Z_Query_type_1:
+    case Z_Query_type_1: case Z_Query_type_101:
         r.errcode = rpn_search (&server_info, q->query->u.type_1,
                                 q->num_bases, q->basenames, q->setname,
                                 &r.hits);
@@ -238,16 +343,28 @@ bend_searchresult *bend_search (void *handle, bend_searchrequest *q, int *fd)
     return &r;
 }
 
-static int record_ext_read (int fd, char *buf, size_t count)
+static int record_offset;
+
+static int record_ext_read (void *fh, char *buf, size_t count)
+{
+    return read (*((int*) fh), buf, count);
+}
+
+static off_t record_ext_seek (void *fh, off_t offset)
 {
-    return read (fd, buf, count);
+    return lseek (*((int*) fh), offset + record_offset, SEEK_SET);
 }
 
 static int record_int_pos;
 static char *record_int_buf;
 static int record_int_len;
 
-static int record_int_read (int fd, char *buf, size_t count)
+static off_t record_int_seek (void *fh, off_t offset)
+{
+    return (off_t) (record_int_pos = offset);
+}
+
+static int record_int_read (void *fh, char *buf, size_t count)
 {
     int l = record_int_len - record_int_pos;
     if (l <= 0)
@@ -261,17 +378,30 @@ static int record_int_read (int fd, char *buf, size_t count)
 static int record_fetch (ZServerInfo *zi, int sysno, int score, ODR stream,
                           oid_value input_format, Z_RecordComposition *comp,
                          oid_value *output_format, char **rec_bufp,
-                         int *rec_lenp)
+                         int *rec_lenp, char **basenamep)
 {
     Record rec;
-    char *fname, *file_type;
+    char *fname, *file_type, *basename;
     RecType rt;
+    int fd = -1;
     struct recRetrieveCtrl retrieveCtrl;
     char subType[128];
 
     rec = rec_get (zi->records, sysno);
+    if (!rec)
+    {
+        char *msg = "Record is deleted\n";
+        *output_format = VAL_SUTRS;
+        *rec_bufp = msg;
+        *rec_lenp = strlen (msg);
+        logf (LOG_DEBUG, "rec_get fail on sysno=%d", sysno);
+        return 0;
+    }
     file_type = rec->info[recInfo_fileType];
     fname = rec->info[recInfo_filename];
+    basename = rec->info[recInfo_databaseName];
+    *basenamep = odr_malloc (stream, strlen(basename)+1);
+    strcpy (*basenamep, basename);
 
     if (!(rt = recType_byName (file_type, subType)))
     {
@@ -283,6 +413,7 @@ static int record_fetch (ZServerInfo *zi, int sysno, int score, ODR stream,
     if (rec->size[recInfo_storeData] > 0)
     {
         retrieveCtrl.readf = record_int_read;
+        retrieveCtrl.seekf = record_int_seek;
         record_int_len = rec->size[recInfo_storeData];
         record_int_buf = rec->info[recInfo_storeData];
         record_int_pos = 0;
@@ -290,9 +421,9 @@ static int record_fetch (ZServerInfo *zi, int sysno, int score, ODR stream,
     }
     else 
     {
-        if ((retrieveCtrl.fd = open (fname, O_RDONLY)) == -1)
+        if ((fd = open (fname, O_RDONLY)) == -1)
         {
-            char *msg = "Record doesn't exist";
+            char *msg = "Record doesn't exist\n";
             logf (LOG_WARN|LOG_ERRNO, "Retrieve: Open record file %s", fname);
             *output_format = VAL_SUTRS;
             *rec_bufp = msg;
@@ -300,7 +431,15 @@ static int record_fetch (ZServerInfo *zi, int sysno, int score, ODR stream,
             rec_rm (&rec);
             return 0;     /* or 14: System error in presenting records */
         }
+
+        memcpy (&record_offset, rec->info[recInfo_offset],
+                sizeof(record_offset));
+
+        retrieveCtrl.fh = &fd;
         retrieveCtrl.readf = record_ext_read;
+        retrieveCtrl.seekf = record_ext_seek;
+
+        record_ext_seek (retrieveCtrl.fh, 0);
     }
     retrieveCtrl.subType = subType;
     retrieveCtrl.localno = sysno;
@@ -313,7 +452,8 @@ static int record_fetch (ZServerInfo *zi, int sysno, int score, ODR stream,
     *output_format = retrieveCtrl.output_format;
     *rec_bufp = retrieveCtrl.rec_buf;
     *rec_lenp = retrieveCtrl.rec_len;
-    close (retrieveCtrl.fd);
+    if (fd != -1)
+        close (fd);
     rec_rm (&rec);
 
     return retrieveCtrl.diagnostic;
@@ -352,7 +492,8 @@ bend_fetchresult *bend_fetch (void *handle, bend_fetchrequest *q, int *num)
     }
     r.errcode = record_fetch (&server_info, records[0].sysno,
                               records[0].score, q->stream, q->format,
-                              q->comp, &r.format, &r.record, &r.len);
+                              q->comp, &r.format, &r.record, &r.len,
+                              &r.basename);
     resultSetSysnoDel (&server_info, records, 1);
     register_unlock (&server_info);
     return &r;
@@ -378,6 +519,7 @@ bend_scanresult *bend_scan (void *handle, bend_scanrequest *q, int *num)
     r.term_position = q->term_position;
     r.num_entries = q->num_entries;
     r.errcode = rpn_scan (&server_info, q->term,
+                          q->attributeset,
                           q->num_bases, q->basenames,
                           &r.term_position,
                           &r.num_entries, &r.entries, &status);
@@ -391,8 +533,12 @@ void bend_close (void *handle)
 {
     if (server_info.records)
     {
-        dict_close (server_info.wordDict);
-        is_close (server_info.wordIsam);
+        resultSetDestroy (&server_info);
+        dict_close (server_info.dict);
+        if (server_info.isam)
+            is_close (server_info.isam);
+        if (server_info.isamc)
+            isc_close (server_info.isamc);
         rec_close (&server_info.records);
         register_unlock (&server_info);
     }