Fixed bug regarding online updates on WIN32.
[idzebra-moved-to-github.git] / index / zebraapi.c
index 90fe482..a2c2d55 100644 (file)
@@ -3,7 +3,22 @@
  * All rights reserved.
  *
  * $Log: zebraapi.c,v $
- * Revision 1.37  2000-10-17 12:37:09  adam
+ * Revision 1.41  2000-12-01 17:59:08  adam
+ * Fixed bug regarding online updates on WIN32.
+ * When zebra.cfg is not available the server will not abort.
+ *
+ * Revision 1.40  2000/11/29 15:21:31  adam
+ * Fixed problem with passwd db.
+ *
+ * Revision 1.39  2000/11/29 14:24:01  adam
+ * Script configure uses yaz pthreads options. Added locking for
+ * zebra_register_{lock,unlock}.
+ *
+ * Revision 1.38  2000/11/08 13:46:58  adam
+ * Fixed scan: server could break if bad attribute/database was selected.
+ * Work on remote update.
+ *
+ * Revision 1.37  2000/10/17 12:37:09  adam
  * Fixed notification of live-updates. Fixed minor problem with mf_init
  * where it didn't handle shadow area file names correctly.
  *
@@ -169,7 +184,7 @@ static void zebra_register_unlock (ZebraHandle zh);
 static int zebra_register_activate (ZebraService zh, int rw);
 static int zebra_register_deactivate (ZebraService zh);
 
-static int zebra_register_lock (ZebraHandle zh)
+static int zebra_register_lock (ZebraHandle zh, int rw)
 {
     time_t lastChange;
     int state;
@@ -185,6 +200,8 @@ static int zebra_register_lock (ZebraHandle zh)
     times (&zh->tms1);
 #endif
 
+    zebra_mutex_cond_lock (&zh->service->session_lock);
+
     state = zebra_server_lock_get_state(zh->service, &lastChange);
 
     zebra_server_lock (zh->service, state);
@@ -197,17 +214,23 @@ static int zebra_register_lock (ZebraHandle zh)
     default:
         state = 0;
     }
-    if (zh->service->registerState == state)
+    if (rw)
+       logf (LOG_LOG, "Register in read/write mode");
+    else if (zh->service->registerState == state)
     {
-        if (zh->service->registerChange >= lastChange)
-            return 0;
-        logf (LOG_LOG, "Register completely updated since last access");
+        logf (LOG_LOG, "registerChange = %ld lastChange = %ld",
+            (long) zh->service->registerChange, (long)lastChange);
+       if (zh->service->registerChange >= lastChange)
+        {
+           return 0;
+        }
+       logf (LOG_LOG, "Register completely updated since last access");
     }
     else if (zh->service->registerState == -1)
-        logf (LOG_LOG, "Reading register using state %d pid=%ld", state,
-              (long) getpid());
+       logf (LOG_LOG, "Reading register using state %d pid=%ld", state,
+             (long) getpid());
     else
-        logf (LOG_LOG, "Register has changed state from %d to %d",
+       logf (LOG_LOG, "Register has changed state from %d to %d",
               zh->service->registerState, state);
     zh->service->registerChange = lastChange;
 
@@ -215,7 +238,7 @@ static int zebra_register_lock (ZebraHandle zh)
 
     zh->service->registerState = state;
 
-    zebra_register_activate (zh->service, 0);
+    zebra_register_activate (zh->service, rw);
     return 0;
 }
 
@@ -224,6 +247,7 @@ static void zebra_register_unlock (ZebraHandle zh)
 {
     if (zh->service->registerState != -1)
         zebra_server_unlock (zh->service, zh->service->registerState);
+    zebra_mutex_cond_unlock (&zh->service->session_lock);
 #if HAVE_SYS_TIMES_H
     times (&zh->tms2);
     logf (LOG_LOG, "user/system: %ld/%ld",
@@ -242,6 +266,7 @@ ZebraHandle zebra_open (ZebraService zs)
        return 0;
 
     zh = (ZebraHandle) xmalloc (sizeof(*zh));
+    yaz_log (LOG_LOG, "zebra_open zs=%p returns %p", zs, zh);
 
     zh->service = zs;
     zh->sets = 0;
@@ -280,18 +305,33 @@ ZebraService zebra_start (const char *configName)
     if (!(zh->res = res_open (zh->configName)))
     {
        logf (LOG_WARN, "Failed to read resources `%s'", zh->configName);
-       return zh;
+//     return zh;
     }
     zebra_chdir (zh);
     zebra_server_lock_init (zh);
     zebra_mutex_cond_init (&zh->session_lock);
+    if (!res_get (zh->res, "passwd"))
+       zh->passwd_db = NULL;
+    else
+    {
+       zh->passwd_db = passwd_db_open ();
+       if (!zh->passwd_db)
+           logf (LOG_WARN|LOG_ERRNO, "passwd_db_open failed");
+       else
+           passwd_db_file (zh->passwd_db, res_get (zh->res, "passwd"));
+    }
+
     return zh;
 }
 
 static int zebra_register_activate (ZebraService zh, int rw)
 {
     if (zh->active > 1)
+    {
+       yaz_log (LOG_LOG, "zebra_register_activate (ignored since active=%d)",
+                zh->active);
        return 0;
+    }
     yaz_log (LOG_LOG, "zebra_register_activate shadow=%s",
             zh->registerState ? "yes" : "no");
 
@@ -327,17 +367,6 @@ static int zebra_register_activate (ZebraService zh, int rw)
     
     zebraRankInstall (zh, rank1_class);
 
-    if (!res_get (zh->res, "passwd"))
-       zh->passwd_db = NULL;
-    else
-    {
-       zh->passwd_db = passwd_db_open ();
-       if (!zh->passwd_db)
-           logf (LOG_WARN|LOG_ERRNO, "passwd_db_open failed");
-       else
-           passwd_db_file (zh->passwd_db, res_get (zh->res, "passwd"));
-    }
-
     if (!(zh->records = rec_open (zh->bfs, rw, 0)))
     {
        logf (LOG_WARN, "rec_open");
@@ -410,11 +439,6 @@ static int zebra_register_activate (ZebraService zh, int rw)
 
 void zebra_admin_shutdown (ZebraHandle zh)
 {
-    zebra_register_lock (zh);
-    zebraExplain_flush (zh->service->zei, 1, zh);
-    extract_index (zh);
-
-    zebra_register_unlock (zh);
     zebra_mutex_cond_lock (&zh->service->session_lock);
     zh->service->stop_flag = 1;
     if (!zh->service->sessions)
@@ -433,62 +457,72 @@ void zebra_admin_start (ZebraHandle zh)
     zebra_mutex_cond_unlock (&zs->session_lock);
 }
 
-static int zebra_register_deactivate (ZebraService zh)
+static int zebra_register_deactivate (ZebraService zs)
 {
-    zh->stop_flag = 0;
-    if (zh->active <= 1)
+    zs->stop_flag = 0;
+    if (zs->active <= 1)
+    {
+       yaz_log(LOG_LOG, "zebra_register_deactivate (ignored since active=%d)",
+               zs->active);
        return 0;
+    }
     yaz_log(LOG_LOG, "zebra_register_deactivate");
-    zebra_chdir (zh);
-    if (zh->records)
+    zebra_chdir (zs);
+    if (zs->records)
     {
-        zebraExplain_close (zh->zei, 0);
-        dict_close (zh->dict);
-       sortIdx_close (zh->sortIdx);
-       if (zh->isams)
-           isams_close (zh->isams);
+        zebraExplain_close (zs->zei, 0);
+        dict_close (zs->dict);
+       sortIdx_close (zs->sortIdx);
+       if (zs->isams)
+           isams_close (zs->isams);
 #if ZMBOL
-        if (zh->isam)
-            is_close (zh->isam);
-        if (zh->isamc)
-            isc_close (zh->isamc);
-        if (zh->isamd)
-            isamd_close (zh->isamd);
+        if (zs->isam)
+            is_close (zs->isam);
+        if (zs->isamc)
+            isc_close (zs->isamc);
+        if (zs->isamd)
+            isamd_close (zs->isamd);
 #endif
-        rec_close (&zh->records);
+        rec_close (&zs->records);
     }
-    recTypes_destroy (zh->recTypes);
-    zebra_maps_close (zh->zebra_maps);
-    zebraRankDestroy (zh);
-    bfs_destroy (zh->bfs);
-    data1_destroy (zh->dh);
-
-    if (zh->passwd_db)
-       passwd_db_close (zh->passwd_db);
-    zh->active = 1;
+    recTypes_destroy (zs->recTypes);
+    zebra_maps_close (zs->zebra_maps);
+    zebraRankDestroy (zs);
+    bfs_destroy (zs->bfs);
+    data1_destroy (zs->dh);
+
+    if (zs->passwd_db)
+       passwd_db_close (zs->passwd_db);
+    zs->active = 1;
     return 0;
 }
 
-void zebra_stop(ZebraService zh)
+void zebra_stop(ZebraService zs)
 {
-    if (!zh)
+    if (!zs)
        return ;
     yaz_log (LOG_LOG, "zebra_stop");
 
-    assert (!zh->sessions);
+    zebra_mutex_cond_lock (&zs->session_lock);
+    while (zs->sessions)
+        zebra_close (zs->sessions);
 
-    zebra_mutex_cond_destroy (&zh->session_lock);
+    zebra_mutex_cond_unlock (&zs->session_lock);
 
-    zebra_register_deactivate(zh);
-    res_close (zh->res);
-    xfree (zh->configName);
-    xfree (zh);
+    zebra_mutex_cond_destroy (&zs->session_lock);
+
+    zebra_register_deactivate(zs);
+    res_close (zs->res);
+    xfree (zs->configName);
+    xfree (zs);
 }
 
 void zebra_close (ZebraHandle zh)
 {
     ZebraService zs = zh->service;
     struct zebra_session **sp;
+
+    yaz_log (LOG_LOG, "zebra_close zh=%p", zh);
     if (!zh)
        return ;
     resultSetDestroy (zh, -1, 0, 0);
@@ -590,7 +624,7 @@ void zebra_search_rpn (ZebraHandle zh, ODR stream, ODR decode,
                       const char *setname)
 {
     zh->hits = 0;
-    if (zebra_register_lock (zh))
+    if (zebra_register_lock (zh, 0))
        return;
     map_basenames (zh, stream, &num_bases, &basenames);
     resultSetAddRPN (zh, stream, decode, query, num_bases, basenames, setname);
@@ -608,7 +642,7 @@ void zebra_records_retrieve (ZebraHandle zh, ODR stream,
     ZebraPosSet poset;
     int i, *pos_array;
 
-    if (zebra_register_lock (zh))
+    if (zebra_register_lock (zh, 0))
        return;
     pos_array = (int *) xmalloc (num_recs * sizeof(*pos_array));
     for (i = 0; i<num_recs; i++)
@@ -656,7 +690,7 @@ void zebra_scan (ZebraHandle zh, ODR stream, Z_AttributesPlusTerm *zapt,
                 int *position, int *num_entries, ZebraScanEntry **entries,
                 int *is_partial)
 {
-    if (zebra_register_lock (zh))
+    if (zebra_register_lock (zh, 0))
     {
        *entries = 0;
        *num_entries = 0;
@@ -674,7 +708,7 @@ void zebra_sort (ZebraHandle zh, ODR stream,
                 const char *output_setname, Z_SortKeySpecList *sort_sequence,
                 int *sort_status)
 {
-    if (zebra_register_lock (zh))
+    if (zebra_register_lock (zh, 0))
        return;
     resultSetSort (zh, stream->mem, num_input_setnames, input_setnames,
                   output_setname, sort_sequence, sort_status);
@@ -686,7 +720,7 @@ int zebra_deleleResultSet(ZebraHandle zh, int function,
                          int *statuses)
 {
     int i, status;
-    if (zebra_register_lock (zh))
+    if (zebra_register_lock (zh, 0))
        return Z_DeleteStatus_systemProblemAtTarget;
     switch (function)
     {
@@ -739,24 +773,24 @@ int zebra_auth (ZebraService zh, const char *user, const char *pass)
 
 void zebra_admin_import_begin (ZebraHandle zh, const char *database)
 {
-    if (zebra_register_lock (zh))
+    if (zebra_register_lock (zh, 1))
        return;
     xfree (zh->admin_databaseName);
     zh->admin_databaseName = xstrdup(database);
-    zebra_register_unlock(zh);
 }
 
 void zebra_admin_import_end (ZebraHandle zh)
 {
     zebraExplain_flush (zh->service->zei, 1, zh);
     extract_index (zh);
+    zebra_register_unlock (zh);
 }
 
 void zebra_admin_import_segment (ZebraHandle zh, Z_Segment *segment)
 {
     int sysno;
     int i;
-    if (zebra_register_lock (zh))
+    if (zh->service->active < 2)
        return;
     for (i = 0; i<segment->num_segmentRecords; i++)
     {
@@ -788,13 +822,12 @@ void zebra_admin_import_segment (ZebraHandle zh, Z_Segment *segment)
            }
        }
     }
-    zebra_register_unlock(zh);
 }
 
 void zebra_admin_create (ZebraHandle zh, const char *database)
 {
     ZebraService zs = zh->service;
-    if (zebra_register_lock(zh))
+    if (zebra_register_lock(zh, 1))
     {
        zh->errCode = 1019;
        return;
@@ -805,6 +838,8 @@ void zebra_admin_create (ZebraHandle zh, const char *database)
        zh->errCode = 224;
        zh->errString = "Database already exist";
     }
+    zebraExplain_flush (zh->service->zei, 1, zh);
+    extract_index (zh);
     zebra_register_unlock(zh);
 }