Zebra version corresponds to YAZ version 1.4.
[idzebra-moved-to-github.git] / index / zserver.c
index faae520..9c59eae 100644 (file)
@@ -1,10 +1,40 @@
 /*
- * Copyright (C) 1995-1996, Index Data I/S 
+ * Copyright (C) 1995-1997, Index Data I/S 
  * All rights reserved.
  * Sebastian Hammer, Adam Dickmeiss
  *
  * $Log: zserver.c,v $
- * Revision 1.40  1996-06-04 10:19:02  adam
+ * Revision 1.48  1997-09-17 12:19:19  adam
+ * Zebra version corresponds to YAZ version 1.4.
+ * Changed Zebra server so that it doesn't depend on global common_resource.
+ *
+ * Revision 1.47  1997/09/04 13:58:36  adam
+ * New retrieve/extract method tellf (added).
+ * Added O_BINARY for open calls.
+ *
+ * Revision 1.46  1997/07/28 08:30:47  adam
+ * Server returns diagnostic 14 when record doesn't exist.
+ *
+ * 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
 #include <unistd.h>
 #include <fcntl.h>
 
-#include <common.h>
 #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;
-    int state = zebraServerLockGetState(&lastChange);
+    int state = zebraServerLockGetState(zi->res, &lastChange);
 
     switch (state)
     {
@@ -172,7 +214,10 @@ static int register_lock (ZServerInfo *zi)
     default:
         state = 0;
     }
-    zebraServerLock (state);
+    zebraServerLock (zi->res, state);
+#if USE_TIMES
+    times (&tms1);
+#endif
     if (zi->registerState == state)
     {
         if (zi->registerChange >= lastChange)
@@ -189,20 +234,35 @@ static int register_lock (ZServerInfo *zi)
     if (zi->records)
     {
         zebTargetInfo_close (zi->zti, 0);
-        dict_close (zi->wordDict);
-        is_close (zi->wordIsam);
+        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);
+    bf_cache (zi->bfs, state ? res_get (zi->res, "shadow") : NULL);
     zi->registerState = state;
-    zi->records = rec_open (0);
-    if (!(zi->wordDict = dict_open (FNAME_WORD_DICT, 40, 0)))
-        return -1;
-    if (!(zi->wordIsam = is_open (FNAME_WORD_ISAM, key_compare, 0,
-                                  sizeof (struct it_key))))
+    zi->records = rec_open (zi->bfs, 0);
+    if (!(zi->dict = dict_open (zi->bfs, FNAME_DICT, 40, 0)))
         return -1;
+    zi->isam = NULL;
+    zi->isamc = NULL;
+    if (res_get_match (zi->res, "isam", "c", NULL))
+    {
+        if (!(zi->isamc = isc_open (zi->bfs, FNAME_ISAMC,
+                                   0, key_isamc_m(zi->res))))
+            return -1;
+
+    }
+    else
+    {
+        if (!(zi->isam = is_open (zi->bfs, FNAME_ISAM, key_compare, 0,
+                                  sizeof (struct it_key), zi->res)))
+            return -1;
+    }
     zi->zti = zebTargetInfo_open (zi->records, 0);
-    init_charmap ();
+    init_charmap (zi->res);
     return 0;
 }
 
@@ -210,9 +270,15 @@ 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");
+        char *s = res_get (zi->res, "debugRequestWait");
         if (s)
             waitSec = atoi (s);
         else
@@ -226,125 +292,140 @@ static void register_unlock (ZServerInfo *zi)
 
 bend_initresult *bend_init (bend_initrequest *q)
 {
-    static bend_initresult r;
-    static char *name = "zserver";
+    bend_initresult *r = odr_malloc (q->stream, sizeof(*r));
+    ZServerInfo *zi = xmalloc (sizeof(*zi));
+    struct statserv_options_block *sob;
 
-    r.errcode = 0;
-    r.errstring = 0;
-    r.handle = name;
+    r->errcode = 0;
+    r->errstring = 0;
+    r->handle = zi;
 
     logf (LOG_DEBUG, "bend_init");
 
-    if (!common_resource)
+    sob = statserv_getcontrol ();
+    logf (LOG_LOG, "Reading resources from %s", sob->configname);
+    if (!(zi->res = res_open (sob->configname)))
     {
-        struct statserv_options_block *sob;
-
-        sob = statserv_getcontrol ();
-        logf (LOG_LOG, "Reading resources from %s", sob->configname);
-        if (!(common_resource = res_open (sob->configname)))
-        {
-            logf (LOG_FATAL, "Cannot open resource `%s'", sob->configname);
-            exit (1);
-        }
-        bf_lockDir (res_get (common_resource, "lockPath"));
-        data1_tabpath = res_get(common_resource, "profilePath");
+       logf (LOG_FATAL, "Cannot open resource `%s'", sob->configname);
+       exit (1);
     }
-    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;
+    zi->dh = data1_create ();
+    zi->bfs = bfs_create (res_get (zi->res, "register"));
+    bf_lockDir (zi->bfs, res_get (zi->res, "lockDir"));
+    data1_set_tabpath (zi->dh, res_get(zi->res, "profilePath"));
+    zi->sets = NULL;
+    zi->registerState = -1;  /* trigger open of registers! */
+    zi->registerChange = 0;
+
+    zi->records = NULL;
+    zi->odr = odr_createmem (ODR_ENCODE);
+    zi->registered_sets = NULL;
+    return r;
 }
 
 bend_searchresult *bend_search (void *handle, bend_searchrequest *q, int *fd)
 {
-    static bend_searchresult r;
+    ZServerInfo *zi = handle;
+    bend_searchresult *r = odr_malloc (q->stream, sizeof(*r));
 
-    r.errcode = 0;
-    r.errstring = 0;
-    r.hits = 0;
+    r->errcode = 0;
+    r->errstring = 0;
+    r->hits = 0;
 
-    register_lock (&server_info);
-    odr_reset (server_info.odr);
-    server_info.errCode = 0;
-    server_info.errString = NULL;
+    register_lock (zi);
+    odr_reset (zi->odr);
+    zi->errCode = 0;
+    zi->errString = NULL;
 
     logf (LOG_LOG, "ResultSet '%s'", q->setname);
     switch (q->query->which)
     {
     case Z_Query_type_1: case Z_Query_type_101:
-        r.errcode = rpn_search (&server_info, q->query->u.type_1,
+        r->errcode = rpn_search (zi, q->query->u.type_1,
                                 q->num_bases, q->basenames, q->setname,
-                                &r.hits);
-        r.errstring = server_info.errString;
+                                &r->hits);
+        r->errstring = zi->errString;
         break;
     default:
-        r.errcode = 107;
+        r->errcode = 107;
     }
-    register_unlock (&server_info);
-    return &r;
+    register_unlock (zi);
+    return r;
 }
 
-static int record_offset;
+struct fetch_control {
+    int record_offset;
+    int record_int_pos;
+    char *record_int_buf;
+    int record_int_len;
+    int fd;
+};
 
 static int record_ext_read (void *fh, char *buf, size_t count)
 {
-    return read (*((int*) fh), buf, count);
+    struct fetch_control *fc = fh;
+    return read (fc->fd, buf, count);
 }
 
 static off_t record_ext_seek (void *fh, off_t offset)
 {
-    return lseek (*((int*) fh), offset + record_offset, SEEK_SET);
+    struct fetch_control *fc = fh;
+    return lseek (fc->fd, offset + fc->record_offset, SEEK_SET);
 }
 
-static int record_int_pos;
-static char *record_int_buf;
-static int record_int_len;
+static off_t record_ext_tell (void *fh)
+{
+    struct fetch_control *fc = fh;
+    return lseek (fc->fd, 0, SEEK_CUR) - fc->record_offset;
+}
 
 static off_t record_int_seek (void *fh, off_t offset)
 {
-    return (off_t) (record_int_pos = offset);
+    struct fetch_control *fc = fh;
+    return (off_t) (fc->record_int_pos = offset);
+}
+
+static off_t record_int_tell (void *fh)
+{
+    struct fetch_control *fc = fh;
+    return (off_t) fc->record_int_pos;
 }
 
 static int record_int_read (void *fh, char *buf, size_t count)
 {
-    int l = record_int_len - record_int_pos;
+    struct fetch_control *fc = fh;
+    int l = fc->record_int_len - fc->record_int_pos;
     if (l <= 0)
         return 0;
     l = (l < count) ? l : count;
-    memcpy (buf, record_int_buf + record_int_pos, l);
-    record_int_pos += l;
+    memcpy (buf, fc->record_int_buf + fc->record_int_pos, l);
+    fc->record_int_pos += l;
     return l;
 }
 
 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];
+    struct fetch_control fc;
 
     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;
+        return 14;
     }
     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)))
     {
@@ -353,34 +434,33 @@ static int record_fetch (ZServerInfo *zi, int sysno, int score, ODR stream,
         exit (1);
     }
     logf (LOG_DEBUG, "retrieve localno=%d score=%d", sysno, score);
+    retrieveCtrl.fh = &fc;
+    fc.fd = -1;
     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;
-        logf (LOG_DEBUG, "Internal retrieve. %d bytes", record_int_len);
+        retrieveCtrl.tellf = record_int_tell;
+        fc.record_int_len = rec->size[recInfo_storeData];
+        fc.record_int_buf = rec->info[recInfo_storeData];
+        fc.record_int_pos = 0;
+        logf (LOG_DEBUG, "Internal retrieve. %d bytes", fc.record_int_len);
     }
     else 
     {
-        if ((fd = open (fname, O_RDONLY)) == -1)
+        if ((fc.fd = open (fname, O_BINARY|O_RDONLY)) == -1)
         {
-            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;
-            *rec_lenp = strlen (msg);
+            logf (LOG_WARN|LOG_ERRNO, "Retrieve fail; missing file: %s",
+                 fname);
             rec_rm (&rec);
-            return 0;     /* or 14: System error in presenting records */
+            return 14;
         }
+        memcpy (&fc.record_offset, rec->info[recInfo_offset],
+                sizeof(fc.record_offset));
 
-        memcpy (&record_offset, rec->info[recInfo_offset],
-                sizeof(record_offset));
-
-        retrieveCtrl.fh = &fd;
         retrieveCtrl.readf = record_ext_read;
         retrieveCtrl.seekf = record_ext_seek;
+        retrieveCtrl.tellf = record_ext_tell;
 
         record_ext_seek (retrieveCtrl.fh, 0);
     }
@@ -391,12 +471,13 @@ static int record_fetch (ZServerInfo *zi, int sysno, int score, ODR stream,
     retrieveCtrl.input_format = retrieveCtrl.output_format = input_format;
     retrieveCtrl.comp = comp;
     retrieveCtrl.diagnostic = 0;
+    retrieveCtrl.dh = zi->dh;
     (*rt->retrieve)(&retrieveCtrl);
     *output_format = retrieveCtrl.output_format;
     *rec_bufp = retrieveCtrl.rec_buf;
     *rec_lenp = retrieveCtrl.rec_len;
-    if (fd != -1)
-        close (fd);
+    if (fc.fd != -1)
+        close (fc.fd);
     rec_rm (&rec);
 
     return retrieveCtrl.diagnostic;
@@ -404,82 +485,93 @@ static int record_fetch (ZServerInfo *zi, int sysno, int score, ODR stream,
 
 bend_fetchresult *bend_fetch (void *handle, bend_fetchrequest *q, int *num)
 {
-    static bend_fetchresult r;
+    ZServerInfo *zi = handle;
+    bend_fetchresult *r = odr_malloc (q->stream, sizeof(*r));
     int positions[2];
     ZServerSetSysno *records;
 
-    register_lock (&server_info);
+    register_lock (zi);
 
-    r.errstring = 0;
-    r.last_in_set = 0;
-    r.basename = "base";
+    r->errstring = 0;
+    r->last_in_set = 0;
+    r->basename = "base";
 
-    odr_reset (server_info.odr);
-    server_info.errCode = 0;
+    odr_reset (zi->odr);
+    zi->errCode = 0;
 
     positions[0] = q->number;
-    records = resultSetSysnoGet (&server_info, q->setname, 1, positions);
+    records = resultSetSysnoGet (zi, q->setname, 1, positions);
     if (!records)
     {
         logf (LOG_DEBUG, "resultSetRecordGet, error");
-        r.errcode = 13;
-        register_unlock (&server_info);
-        return &r;
+        r->errcode = 13;
+        register_unlock (zi);
+        return r;
     }
     if (!records[0].sysno)
     {
-        r.errcode = 13;
+        r->errcode = 13;
         logf (LOG_DEBUG, "Out of range. pos=%d", q->number);
-        register_unlock (&server_info);
-        return &r;
+        register_unlock (zi);
+        return r;
     }
-    r.errcode = record_fetch (&server_info, records[0].sysno,
+    r->errcode = record_fetch (zi, records[0].sysno,
                               records[0].score, q->stream, q->format,
-                              q->comp, &r.format, &r.record, &r.len);
-    resultSetSysnoDel (&server_info, records, 1);
-    register_unlock (&server_info);
-    return &r;
+                              q->comp, &r->format, &r->record, &r->len,
+                              &r->basename);
+    resultSetSysnoDel (zi, records, 1);
+    register_unlock (zi);
+    return r;
 }
 
 bend_deleteresult *bend_delete (void *handle, bend_deleterequest *q, int *num)
 {
-    register_lock (&server_info);
-    register_unlock (&server_info);
+    ZServerInfo *zi = handle;
+    register_lock (zi);
+    register_unlock (zi);
     return 0;
 }
 
 bend_scanresult *bend_scan (void *handle, bend_scanrequest *q, int *num)
 {
-    static bend_scanresult r;
+    ZServerInfo *zi = handle;
+    bend_scanresult *r = odr_malloc (q->stream, sizeof(*r));
     int status;
 
-    register_lock (&server_info);
-    odr_reset (server_info.odr);
-    server_info.errCode = 0;
-    server_info.errString = 0;
+    register_lock (zi);
+    odr_reset (zi->odr);
+    zi->errCode = 0;
+    zi->errString = 0;
 
-    r.term_position = q->term_position;
-    r.num_entries = q->num_entries;
-    r.errcode = rpn_scan (&server_info, q->term,
+    r->term_position = q->term_position;
+    r->num_entries = q->num_entries;
+    r->errcode = rpn_scan (zi, q->term,
                           q->attributeset,
                           q->num_bases, q->basenames,
-                          &r.term_position,
-                          &r.num_entries, &r.entries, &status);
-    r.errstring = server_info.errString;
-    r.status = status;
-    register_unlock (&server_info);
-    return &r;
+                          &r->term_position,
+                          &r->num_entries, &r->entries, &status);
+    r->errstring = zi->errString;
+    r->status = status;
+    register_unlock (zi);
+    return r;
 }
 
 void bend_close (void *handle)
 {
-    if (server_info.records)
+    ZServerInfo *zi = handle;
+    if (zi->records)
     {
-        dict_close (server_info.wordDict);
-        is_close (server_info.wordIsam);
-        rec_close (&server_info.records);
-        register_unlock (&server_info);
+        resultSetDestroy (zi);
+        dict_close (zi->dict);
+        if (zi->isam)
+            is_close (zi->isam);
+        if (zi->isamc)
+            isc_close (zi->isamc);
+        rec_close (&zi->records);
+        register_unlock (zi);
     }
+    bfs_destroy (zi->bfs);
+    data1_destroy (zi->dh);
     return;
 }