Updated WIN32 code specific sections. Changed header.
[idzebra-moved-to-github.git] / index / zinfo.c
index 2e6dae9..31f35b8 100644 (file)
@@ -1,10 +1,26 @@
 /*
- * Copyright (C) 1994-1998, Index Data I/S 
+ * Copyright (C) 1994-1999, Index Data
  * All rights reserved.
  * Sebastian Hammer, Adam Dickmeiss
  *
  * $Log: zinfo.c,v $
- * Revision 1.10  1998-06-08 14:43:15  adam
+ * Revision 1.15  1999-01-25 13:47:54  adam
+ * Fixed bug.
+ *
+ * Revision 1.14  1998/11/04 16:31:32  adam
+ * Fixed bug regarding recordBytes in databaseInfo.
+ *
+ * Revision 1.13  1998/11/03 10:17:09  adam
+ * Fixed bug regarding creation of some data1 nodes for Explain records.
+ *
+ * Revision 1.12  1998/10/13 20:37:11  adam
+ * Changed the way attribute sets are saved in Explain database to
+ * reflect "dynamic" OIDs.
+ *
+ * Revision 1.11  1998/06/09 12:16:48  adam
+ * Implemented auto-generation of CategoryList records.
+ *
+ * Revision 1.10  1998/06/08 14:43:15  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.
@@ -105,6 +121,12 @@ struct zebraExplainAttset {
     struct zebraExplainAttset *next;
 };
 
+struct zebraCategoryListInfo {
+    int dirty;
+    int sysno;
+    data1_node *data1_categoryList;
+};
+
 struct zebraExplainInfo {
     int  ordinalSU;
     int  runNumber;
@@ -115,6 +137,7 @@ struct zebraExplainInfo {
     struct zebraExplainAttset *attsets;
     NMEM nmem;
     data1_node *data1_target;
+    struct zebraCategoryListInfo *categoryList;
     struct zebDatabaseInfoB *databaseInfo;
     struct zebDatabaseInfoB *curDatabaseInfo;
     zebAccessInfo accessInfo;
@@ -257,9 +280,9 @@ static data1_node *data1_make_tagdata_text (data1_handle dh, data1_node *at,
     {
        data1_node *node_data = node->child;
        node_data->u.data.what = DATA1I_text;
-       node_data->u.data.data = node_data->lbuf;
-       strcpy (node_data->u.data.data, str);
-       node_data->u.data.len = strlen (node_data->u.data.data);
+       node_data->u.data.len = strlen (str);
+       node_data->u.data.data = data1_insert_string (dh, node_data,
+                                                     nmem, str);
        return node_data;
     }
 }
@@ -275,6 +298,10 @@ static void zebraExplain_writeTarget (ZebraExplainInfo zei, int key_flush);
 static void zebraExplain_writeAttributeSet (ZebraExplainInfo zei,
                                            zebAccessObject o,
                                            int key_flush);
+static void zebraExplain_writeCategoryList (ZebraExplainInfo zei,
+                                           struct zebraCategoryListInfo *zcl,
+                                           int key_flush);
+
 
 static Record createRecord (Records records, int *sysno)
 {
@@ -315,7 +342,9 @@ void zebraExplain_close (ZebraExplainInfo zei, int writeFlag,
                                                zdi->databaseName, 1);
        }
        zebraExplain_writeTarget (zei, 1);
-       
+       zebraExplain_writeCategoryList (zei,
+                                       zei->categoryList,
+                                       1);
        assert (zei->accessInfo);
        for (o = zei->accessInfo->attributeSetIds; o; o = o->next)
            if (!o->sysno)
@@ -427,6 +456,11 @@ ZebraExplainInfo zebraExplain_open (
     zei->dh = dh;
     zei->attsets = NULL;
     zei->res = res;
+    zei->categoryList = nmem_malloc (zei->nmem,
+                                    sizeof(*zei->categoryList));
+    zei->categoryList->sysno = 0;
+    zei->categoryList->dirty = 0;
+    zei->categoryList->data1_categoryList = NULL;
 
     time (&our_time);
     tm = localtime (&our_time);
@@ -541,7 +575,6 @@ ZebraExplainInfo zebraExplain_open (
                                            "targetInfo");
            assert (node_tgtinfo);
 
-
            zebraExplain_initCommonInfo (zei, node_tgtinfo);
            zebraExplain_initAccessInfo (zei, node_tgtinfo);
 
@@ -562,6 +595,23 @@ ZebraExplainInfo zebraExplain_open (
        *zdip = NULL;
        rec_rm (&trec);
        zebraExplain_newDatabase (zei, "IR-Explain-1", 0);
+
+       if (!zei->categoryList->dirty)
+       {
+           struct zebraCategoryListInfo *zcl = zei->categoryList;
+           data1_node *node_cl;
+
+           zcl->dirty = 1;
+           zcl->data1_categoryList =
+               data1_read_sgml (zei->dh, zei->nmem,
+                                "<explain><categoryList>CategoryList\n"
+                                "/></>\n");
+           node_cl = data1_search_tag (zei->dh,
+                                       zcl->data1_categoryList->child,
+                                       "categoryList");
+           assert (node_cl);
+           zebraExplain_initCommonInfo (zei, node_cl);
+       }
     }
     return zei;
 }
@@ -590,6 +640,9 @@ static void zebraExplain_readAttributeDetails (ZebraExplainInfo zei,
        data1_node *node_use = NULL;
        data1_node *node_ordinal = NULL;
        data1_node *np2;
+       char oid_str[128];
+       int oid_str_len;
+
        if (np->which != DATA1N_tag || strcmp (np->u.tag.tag, "attr"))
            continue;
        for (np2 = np->child; np2; np2 = np2->next)
@@ -605,10 +658,16 @@ static void zebraExplain_readAttributeDetails (ZebraExplainInfo zei,
                node_ordinal = np2->child;
        }
        assert (node_set && node_use && node_ordinal);
+
+       oid_str_len = node_set->u.data.len;
+       if (oid_str_len >= sizeof(oid_str))
+           oid_str_len = sizeof(oid_str)-1;
+       memcpy (oid_str, node_set->u.data.data, oid_str_len);
+       oid_str[oid_str_len] = '\0';
        
         *zsuip = nmem_malloc (zei->nmem, sizeof(**zsuip));
-       (*zsuip)->info.set = atoi_n (node_set->u.data.data,
-                                    node_set->u.data.len);
+       (*zsuip)->info.set = oid_getvalbyname (oid_str);
+
        (*zsuip)->info.use = atoi_n (node_use->u.data.data,
                                     node_use->u.data.len);
        (*zsuip)->info.ordinal = atoi_n (node_ordinal->u.data.data,
@@ -639,9 +698,10 @@ static void zebraExplain_readDatabase (ZebraExplainInfo zei,
 
     node_zebra = data1_search_tag (zei->dh, node_dbinfo->child,
                                 "zebraInfo");
-    np  = data1_search_tag (zei->dh, node_dbinfo->child,
-                           "recordBytes");
-    if (np && np->child && np->child->which == DATA1N_data)
+    if (node_zebra
+       && (np = data1_search_tag (zei->dh, node_zebra->child,
+                                  "recordBytes")) 
+       && np->child && np->child->which == DATA1N_data)
        zdi->recordBytes = atoi_n (np->child->u.data.data,
                                   np->child->u.data.len);
     if ((np = data1_search_tag (zei->dh, node_dbinfo->child,
@@ -849,6 +909,68 @@ static void writeAttributeValueDetails (ZebraExplainInfo zei,
     }
 }
 
+static void zebraExplain_writeCategoryList (ZebraExplainInfo zei,
+                                           struct zebraCategoryListInfo *zcl,
+                                           int key_flush)
+{
+    char *sgml_buf;
+    int sgml_len;
+    int i;
+    Record drec;
+    data1_node *node_ci, *node_categoryList;
+    int sysno = 0;
+    static char *category[] = {
+       "CategoryList",
+       "TargetInfo",
+       "DatabaseInfo",
+       "AttributeDetails",
+       NULL
+    };
+
+    assert (zcl);
+    if (!zcl->dirty)
+       return ;
+    zcl->dirty = 1;
+    node_categoryList = zcl->data1_categoryList;
+
+#if ZINFO_DEBUG
+    logf (LOG_LOG, "zebraExplain_writeCategoryList");
+#endif
+
+    drec = createRecord (zei->records, &sysno);
+
+    node_ci = data1_search_tag (zei->dh, node_categoryList->child,
+                               "categoryList");
+    assert (node_ci);
+    node_ci = data1_add_tag (zei->dh, node_ci, "categories", zei->nmem);
+    assert (node_ci);
+    
+    for (i = 0; category[i]; i++)
+    {
+       data1_node *node_cat = data1_add_tag (zei->dh, node_ci, 
+                                             "category", zei->nmem);
+
+       data1_add_tagdata_text (zei->dh, node_cat, "name",
+                               category[i], zei->nmem);
+    }
+    /* extract *searchable* keys from it. We do this here, because
+       record count, etc. is affected */
+    if (key_flush)
+       (*zei->updateFunc)(zei->updateHandle, drec, node_categoryList);
+
+    /* convert to "SGML" and write it */
+#if ZINFO_DEBUG
+    data1_pr_tree (zei->dh, node_categoryList, stderr);
+#endif
+    sgml_buf = data1_nodetoidsgml(zei->dh, node_categoryList,
+                                 0, &sgml_len);
+    drec->info[recInfo_storeData] = xmalloc (sgml_len);
+    memcpy (drec->info[recInfo_storeData], sgml_buf, sgml_len);
+    drec->size[recInfo_storeData] = sgml_len;
+    
+    rec_put (zei->records, &drec);
+}
+
 static void zebraExplain_writeAttributeDetails (ZebraExplainInfo zei,
                                                zebAttributeDetails zad,
                                                const char *databaseName,
@@ -923,15 +1045,15 @@ static void zebraExplain_writeAttributeDetails (ZebraExplainInfo zei,
                data1_node *node_abt, *node_atd, *node_atvs;
                data1_add_tagdata_oid (zei->dh, node_asd, "oid",
                                       oid, zei->nmem);
-
+               
                node_abt = data1_add_tag (zei->dh, node_asd,
                                          "attributesByType", zei->nmem);
                node_atd = data1_add_tag (zei->dh, node_abt,
                                          "attributeTypeDetails", zei->nmem);
                data1_add_tagdata_int (zei->dh, node_atd,
-                                       "type", 1, zei->nmem);
+                                      "type", 1, zei->nmem);
                node_atvs = data1_add_tag (zei->dh, node_atd, 
-                                         "attributeValues", zei->nmem);
+                                          "attributeValues", zei->nmem);
                writeAttributeValueDetails (zei, zad, node_atvs, attset);
            }
        }
@@ -943,15 +1065,23 @@ static void zebraExplain_writeAttributeDetails (ZebraExplainInfo zei,
                                 "attrlist", zei->nmem);
     for (zsui = zad->SUInfo; zsui; zsui = zsui->next)
     {
+       struct oident oident;
+       int oid[OID_SIZE];
        data1_node *node_attr;
-       node_attr = data1_add_tag (zei->dh, node_list,
-                                     "attr", zei->nmem);
-       data1_add_tagdata_int (zei->dh, node_attr, "set",
-                                 zsui->info.set, zei->nmem);
+       
+       node_attr = data1_add_tag (zei->dh, node_list, "attr", zei->nmem);
+       
+       oident.proto = PROTO_Z3950;
+       oident.oclass = CLASS_ATTSET;
+       oident.value = zsui->info.set;
+       oid_ent_to_oid (&oident, oid);
+       
+       data1_add_tagdata_text (zei->dh, node_attr, "set",
+                               oident.desc, zei->nmem);
        data1_add_tagdata_int (zei->dh, node_attr, "use",
-                                 zsui->info.use, zei->nmem);
+                              zsui->info.use, zei->nmem);
        data1_add_tagdata_int (zei->dh, node_attr, "ordinal",
-                                 zsui->info.ordinal, zei->nmem);
+                              zsui->info.ordinal, zei->nmem);
     }
     /* convert to "SGML" and write it */
 #if ZINFO_DEBUG