Persistency works much better now. New command: status - history-like
[egate.git] / kernel / persist.c
index e341536..219024f 100644 (file)
@@ -2,7 +2,10 @@
  * Europagate, 1995
  *
  * $Log: persist.c,v $
- * Revision 1.1  1995/04/19 07:31:10  adam
+ * Revision 1.2  1995/04/19 10:46:19  adam
+ * Persistency works much better now. New command: status - history-like
+ *
+ * Revision 1.1  1995/04/19  07:31:10  adam
  * First work on Z39.50 persistence.
  *
  */
 
 #include "kernel.h"
 
+static int fgetsx (char *buf, int size, FILE *inf)
+{
+    char *cp;
+
+    if (!fgets (buf, size, inf))
+        return 0;
+    if ((cp = strchr (buf, '\n')))
+        *cp = '\0';
+    return 1;
+}
+
 static int set_change;
 
 static int obtain_set (ZASS zass, struct gw_user_set *set)
@@ -32,6 +46,7 @@ static int obtain_set (ZASS zass, struct gw_user_set *set)
         set_change = 1;
     set->present_flag = 1;
     set->hits = p->num;
+    gw_log (GW_LOG_DEBUG, KERNEL_LOG, "Set %s researched", set->name);
     return 0;
 }
 
@@ -55,9 +70,17 @@ static int obtain_sets (ZASS zass, struct ccl_rpn_node *rpn,
     case CCL_RPN_SET:
         set = user_set_search (rpn->u.setname);
        if (!set)
+        {
+            gw_log (GW_LOG_DEBUG, KERNEL_LOG, "Set %s not there at all",
+                    rpn->u.setname);
            return 1;
+        }
        if (set->present_flag)
+        {
+            gw_log (GW_LOG_DEBUG, KERNEL_LOG, "Set %s already there",
+                    rpn->u.setname);
            return 0;
+        }
     }
     return obtain_set (zass, set);
 }
@@ -87,9 +110,12 @@ const struct zass_presentent *zass_p_present (ZASS zass,
        return NULL;
     if (!set->present_flag)
     {
-        int r = obtain_set (zass, set);
-       if (r)
-           return NULL;
+        const struct zass_searchent *p;
+
+        p = zass_p_search (zass, set->rpn, result_set, set->database,
+                           info.sets);
+        if (!p)
+            return NULL;
     }
     return zass_present (zass, (char*) result_set, offset, number);
 }
@@ -98,13 +124,11 @@ struct ccl_rpn_node *load_rpn (char *buf, FILE *inf)
 {
     struct ccl_rpn_node *rpn;
     struct ccl_rpn_attr **attrp;
-    char *cp;
     int type, value, no_read;
+    char *cp;
 
-    if (!fgets (buf, 1024, inf))
+    if (!fgetsx (buf, 1024, inf))
         return NULL;
-    if ((cp = strchr (buf, '\n')))
-        *cp = '\0';
     switch (*buf)
     {
     case 'A':
@@ -147,7 +171,7 @@ struct ccl_rpn_node *load_rpn (char *buf, FILE *inf)
 
        rpn->u.t.term = gw_strdup (buf+2);
        attrp = &rpn->u.t.attr_list;
-        if (!fgets (buf, 1024, inf))
+        if (!fgetsx (buf, 1024, inf))
             return NULL;
        cp = buf;
        while (sscanf (cp, "%d %d%n", &type, &value, &no_read) > 1)
@@ -189,33 +213,46 @@ int load_p_state (int userid)
                "Couldn't open %s", fname);
         return -1;
     }
-    gw_log (GW_LOG_FATAL, KERNEL_LOG, 
+    gw_log (GW_LOG_DEBUG, KERNEL_LOG, 
        "Reading persistence file %s", fname);
-    if (!fgets (fline, 1024, inf))
+
+    if (!fgetsx (fline, 1024, inf))
         return -1;
     if (sscanf (fline, "%s", info.target) != 1)
         *info.target = '\0';
-    gw_log (GW_LOG_FATAL, KERNEL_LOG, 
-           "Reading persistence file %s (2)", fname);
     read_kernel_res ();
-    gw_log (GW_LOG_FATAL, KERNEL_LOG, "reopen_target start");
+
+    if (!fgetsx (fline, 1024, inf))
+        return -1;
+    free (info.database);
+    info.database = gw_strdup (fline);
+
+    if (!fgetsx (fline, 1024, inf))
+        return -1;
+    if (sscanf (fline, "%d", &info.setno) != 1)
+        return -1;
+    gw_log (GW_LOG_DEBUG, KERNEL_LOG, 
+           "Reading persistence file %s (2)", fname);
     reopen_target ();
-    gw_log (GW_LOG_FATAL, KERNEL_LOG, "reopen_target end");
-    while (fgets (fline, 1024, inf))
+    while (fgetsx (fline, 1024, inf))
     {
-        gw_log (GW_LOG_FATAL, KERNEL_LOG, 
+        gw_log (GW_LOG_DEBUG, KERNEL_LOG, 
               "Reading persistence file %s (3)", fname);
         if (sscanf (fline, "%s %d %s", resultname, &hits, database) != 3)
            return -1;
-       gw_log (GW_LOG_FATAL, KERNEL_LOG,
+        if (!fgetsx (fline, 1024, inf))        /* search string */
+            return -1;
+       gw_log (GW_LOG_DEBUG, KERNEL_LOG,
                "Adding %s, %d hits, database %s",
                resultname, hits, database);
-       set = user_set_add (resultname, hits, database, NULL, 0);
+       gw_log (GW_LOG_DEBUG, KERNEL_LOG, "Search string %s", fline);
+       set = user_set_add (resultname, hits, database, NULL, 0, fline);
        set->rpn = load_rpn (fline, inf);
        ccl_pr_tree (set->rpn, stderr);
-       fgets (fline, 1024, inf);
+       fgetsx (fline, 1024, inf);
     }
     fclose (inf);
+    gw_log (GW_LOG_DEBUG, KERNEL_LOG, "Finished reading %s", fname);
     return 0;
 }
 
@@ -256,11 +293,21 @@ static void save_rpn (struct ccl_rpn_node *rpn, FILE *of)
     }
 }
 
+static void save_sets (FILE *of, struct gw_user_set *sp)
+{
+    if (!sp)
+        return;
+    save_sets (of, sp->prev);
+    fprintf (of, "%s %d %s\n%s\n", sp->name, sp->hits, sp->database,
+             sp->search_str);
+    save_rpn (sp->rpn, of);
+    fprintf (of, "X\n");
+}
+
 int save_p_state (int userid)
 {
     FILE *of;
     char fname[128];
-    struct gw_user_set *sp;
 
     sprintf (fname, "persist.%d", userid);
 
@@ -272,14 +319,8 @@ int save_p_state (int userid)
         return -1;
     }
     gw_log (GW_LOG_DEBUG, KERNEL_LOG, "Writing persistence file %s", fname);
-    fprintf (of, "%s\n", info.target);
-    for (sp = info.sets; sp; sp = sp->prev)
-    {
-        fprintf (of, "%s %d %s\n", sp->name, sp->hits, sp->database);
-       save_rpn (sp->rpn, of);
-       fprintf (of, "X\n");
-    }
+    fprintf (of, "%s\n%s\n%d\n", info.target, info.database, info.setno);
+    save_sets (of, info.sets);
     fclose (of);
     return 0;
 }
-