2007.
[idzebra-moved-to-github.git] / index / zinfo.c
index 5146554..01a8bdd 100644 (file)
@@ -1,5 +1,5 @@
-/* $Id: zinfo.c,v 1.65 2006-05-19 23:20:24 adam Exp $
-   Copyright (C) 1995-2006
+/* $Id: zinfo.c,v 1.76 2007-01-15 15:10:17 adam Exp $
+   Copyright (C) 1995-2007
    Index Data ApS
 
 This file is part of the Zebra server.
@@ -15,9 +15,9 @@ FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 for more details.
 
 You should have received a copy of the GNU General Public License
-along with Zebra; see the file LICENSE.zebra.  If not, write to the
-Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
-02111-1307, USA.
+along with this program; if not, write to the Free Software
+Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+
 */
 
 #include <sys/types.h>
@@ -33,6 +33,7 @@ Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
 
 struct zebSUInfo {
     int index_type;
+    zinfo_index_category_t cat;
 #define ZEB_SU_SET_USE 1
 #define ZEB_SU_STR 2
     int which;
@@ -52,7 +53,7 @@ struct zebSUInfoB {
 typedef struct zebAccessObjectB *zebAccessObject;
 struct zebAccessObjectB {
     void *handle;
-    SYSNO sysno;
+    zint sysno;
     Odr_oid *oid;
     zebAccessObject next;
 };
@@ -65,7 +66,7 @@ struct zebAccessInfoB {
 
 typedef struct {
     struct zebSUInfoB *SUInfo;
-    SYSNO sysno;
+    zint sysno;
     int dirty;
     int readFlag;
     data1_node *data1_tree;
@@ -78,7 +79,7 @@ struct zebDatabaseInfoB {
     data1_node *data1_database;
     zint recordCount;    /* records in db */
     zint recordBytes;    /* size of records */
-    SYSNO sysno;         /* sysno of database info */
+    zint sysno;          /* sysno of database info */
     int readFlag;        /* 1: read is needed when referenced; 0 if not */
     int dirty;           /* 1: database is dirty: write is needed */
     struct zebDatabaseInfoB *next;
@@ -93,7 +94,7 @@ struct zebraExplainAttset {
 
 struct zebraCategoryListInfo {
     int dirty;
-    SYSNO sysno;
+    zint sysno;
     data1_node *data1_categoryList;
 };
 
@@ -114,7 +115,7 @@ struct zebraExplainInfo {
     struct zebDatabaseInfoB *curDatabaseInfo;
     zebAccessInfo accessInfo;
     char date[15]; /* YYYY MMDD HH MM SS */
-    int (*updateFunc)(void *handle, Record drec, data1_node *n);
+    ZebraExplainUpdateFunc *updateFunc;
     void *updateHandle;
 };
 
@@ -142,7 +143,7 @@ static void zebraExplain_writeCategoryList(ZebraExplainInfo zei,
                                            int key_flush);
 
 
-static Record createRecord(Records records, SYSNO *sysno)
+static Record createRecord(Records records, zint *sysno)
 {
     Record rec;
     if (*sysno)
@@ -336,7 +337,7 @@ ZebraExplainInfo zebraExplain_open(
     Res res,
     int writeFlag,
     void *updateHandle,
-    int (*updateFunc)(void *handle, Record drec, data1_node *n))
+    ZebraExplainUpdateFunc *updateFunc)
 {
     Record trec;
     ZebraExplainInfo zei;
@@ -359,6 +360,9 @@ ZebraExplainInfo zebraExplain_open(
     zei->records = records;
     zei->nmem = nmem;
     zei->dh = dh;
+    
+    data1_get_absyn (zei->dh, "explain", DATA1_XPATH_INDEXING_DISABLE);
+
     zei->attsets = NULL;
     zei->res = res;
     zei->categoryList = (struct zebraCategoryListInfo *)
@@ -487,7 +491,7 @@ ZebraExplainInfo zebraExplain_open(
             yaz_log(YLOG_DEBUG, "read runnumber=" ZINT_FORMAT, zei->runNumber);
            *zdip = NULL;
        }
-       rec_rm(&trec);
+       rec_free(&trec);
     }
     else  /* create initial targetInfo */
     {
@@ -539,7 +543,7 @@ ZebraExplainInfo zebraExplain_open(
            trec->size[recInfo_storeData] = sgml_len;
                
            rec_put(records, &trec);
-           rec_rm(&trec);
+           rec_free(&trec);
        }
        
        zebraExplain_newDatabase(zei, "IR-Explain-1", 0);
@@ -590,6 +594,7 @@ static void zebraExplain_readAttributeDetails(ZebraExplainInfo zei,
        data1_node *node_str = NULL;
        data1_node *node_ordinal = NULL;
        data1_node *node_type = NULL;
+       data1_node *node_cat = NULL;
         data1_node *node_doc_occurrences = NULL;
         data1_node *node_term_occurrences = NULL;
        data1_node *np2;
@@ -607,6 +612,8 @@ static void zebraExplain_readAttributeDetails(ZebraExplainInfo zei,
                node_ordinal = np2->child;
            else if (!strcmp(np2->u.tag.tag, "type"))
                node_type = np2->child;
+           else if (!strcmp(np2->u.tag.tag, "cat"))
+               node_cat = np2->child;
            else if (!strcmp(np2->u.tag.tag, "dococcurrences"))
                node_doc_occurrences = np2->child;
            else if (!strcmp(np2->u.tag.tag, "termoccurrences"))
@@ -629,6 +636,31 @@ static void zebraExplain_readAttributeDetails(ZebraExplainInfo zei,
            yaz_log(YLOG_WARN, "Missing attribute 'type' in attribute info");
            (*zsuip)->info.index_type = 'w';
        }
+        if (node_cat && node_cat->u.data.len > 0)
+        {
+            zinfo_index_category_t cat;
+
+            data1_node *np = node_cat;
+            if (!strncmp(np->u.data.data, "index", np->u.data.len))
+                cat = zinfo_index_category_index;
+            else if (!strncmp(np->u.data.data, "sort", np->u.data.len))
+                cat = zinfo_index_category_sort;
+            else if (!strncmp(np->u.data.data, "alwaysmatches", 
+                              np->u.data.len))
+                cat = zinfo_index_category_alwaysmatches;
+            else if (!strncmp(np->u.data.data, "anchor", 
+                              np->u.data.len))
+                cat = zinfo_index_category_anchor;
+            else
+            {
+                yaz_log(YLOG_WARN, "Bad index cateogry '%.*s'",
+                        np->u.data.len, np->u.data.data);
+                cat = zinfo_index_category_index;
+            }
+            (*zsuip)->info.cat = cat;
+        }
+        else
+            (*zsuip)->info.cat = zinfo_index_category_index;
 
         if (node_doc_occurrences)
         {
@@ -661,7 +693,7 @@ static void zebraExplain_readAttributeDetails(ZebraExplainInfo zei,
     }
     *zsuip = NULL;
     zad->readFlag = 0;
-    rec_rm (&rec);
+    rec_free(&rec);
 }
 
 static void zebraExplain_readDatabase (ZebraExplainInfo zei,
@@ -706,7 +738,7 @@ static void zebraExplain_readDatabase (ZebraExplainInfo zei,
                                    np->child->u.data.len);
     }
     zdi->readFlag = 0;
-    rec_rm (&rec);
+    rec_free(&rec);
 }
 
 int zebraExplain_removeDatabase(ZebraExplainInfo zei, void *update_handle)
@@ -730,12 +762,12 @@ int zebraExplain_removeDatabase(ZebraExplainInfo zei, void *update_handle)
                
                rec = rec_get(zei->records, zad->sysno);
                (*zei->updateFunc)(zei->updateHandle, rec, 0);
-               rec_rm(&rec);
+               rec_free(&rec);
            }
            /* remove database record keys and delete it */
            rec = rec_get (zei->records, zdi->sysno);
            (*zei->updateFunc)(zei->updateHandle, rec, 0);
-           rec_rm(&rec);
+           rec_free(&rec);
 
            /* remove from list */
            *zdip = zdi->next;
@@ -823,8 +855,7 @@ static void zebraExplain_updateAccessInfo (ZebraExplainInfo zei, data1_node *n,
     if (!c)
     {
         data1_pr_tree (zei->dh, n, stdout);
-        exit (0);
-        assert (c);
+        zebra_exit("zebraExplain_updateAccessInfo");
     }
 
     if ((p = accessInfo->attributeSetIds))
@@ -932,6 +963,8 @@ int zebraExplain_newDatabase (ZebraExplainInfo zei, const char *database,
 
     zebraExplain_initCommonInfo (zei, node_adinfo);
 
+    data1_mk_tag_data_text(zei->dh, node_adinfo, "name", database, zei->nmem);
+
     return 0;
 }
 
@@ -945,7 +978,7 @@ static void zebraExplain_writeCategoryList (ZebraExplainInfo zei,
     int i;
     Record drec;
     data1_node *node_ci, *node_categoryList;
-    SYSNO sysno = 0;
+    zint sysno = 0;
     static char *category[] = {
        "CategoryList",
        "TargetInfo",
@@ -1017,6 +1050,7 @@ static void zebraExplain_writeAttributeDetails (ZebraExplainInfo zei,
     zad->dirty = 0;
 #if ZINFO_DEBUG
     yaz_log(YLOG_LOG, "zebraExplain_writeAttributeDetails");    
+    data1_pr_tree(zei->dh, zad->data1_tree, stderr);
 #endif
 
     drec = createRecord (zei->records, &zad->sysno);
@@ -1028,16 +1062,14 @@ static void zebraExplain_writeAttributeDetails (ZebraExplainInfo zei,
                                   "/attributeDetails");
     zebraExplain_updateCommonInfo (zei, node_adinfo);
 
-    data1_mk_tag_data_text (zei->dh, node_adinfo, "name",
-                           databaseName, zei->nmem);
+    /* zebra info (private) .. no children yet.. so se don't index zebraInfo */
+    node_zebra = data1_mk_tag_uni (zei->dh, zei->nmem,
+                                "zebraInfo", node_adinfo);
 
     /* extract *searchable* keys from it. We do this here, because
        record count, etc. is affected */
     if (key_flush)
        (*zei->updateFunc)(zei->updateHandle, drec, zad->data1_tree);
-    /* zebra info (private) */
-    node_zebra = data1_mk_tag_uni (zei->dh, zei->nmem,
-                                "zebraInfo", node_adinfo);
     node_list = data1_mk_tag_uni (zei->dh, zei->nmem,
                                 "attrlist", node_zebra);
     for (zsui = zad->SUInfo; zsui; zsui = zsui->next)
@@ -1064,6 +1096,21 @@ static void zebraExplain_writeAttributeDetails (ZebraExplainInfo zei,
                                 zsui->info.doc_occurrences, zei->nmem);
         data1_mk_tag_data_zint (zei->dh, node_attr, "termoccurrences",
                                 zsui->info.term_occurrences, zei->nmem);
+        switch(zsui->info.cat)
+        {
+        case zinfo_index_category_index:
+           data1_mk_tag_data_text (zei->dh, node_attr, "cat",
+                                   "index", zei->nmem); break;
+        case zinfo_index_category_sort:
+           data1_mk_tag_data_text (zei->dh, node_attr, "cat",
+                                   "sort", zei->nmem); break;
+        case zinfo_index_category_alwaysmatches:
+           data1_mk_tag_data_text (zei->dh, node_attr, "cat",
+                                   "alwaysmatches", zei->nmem); break;
+        case zinfo_index_category_anchor:
+           data1_mk_tag_data_text (zei->dh, node_attr, "cat",
+                                   "anchor", zei->nmem); break;
+        }
     }
     /* convert to "SGML" and write it */
 #if ZINFO_DEBUG
@@ -1106,10 +1153,6 @@ static void zebraExplain_writeDatabase (ZebraExplainInfo zei,
     zebraExplain_updateCommonInfo (zei, node_dbinfo);
     zebraExplain_updateAccessInfo (zei, node_dbinfo, zdi->accessInfo);
 
-    /* extract *searchable* keys from it. We do this here, because
-       record count, etc. is affected */
-    if (key_flush)
-       (*zei->updateFunc)(zei->updateHandle, drec, zdi->data1_database);
     /* record count */
     node_count = data1_mk_tag_uni (zei->dh, zei->nmem,
                                 "recordCount", node_dbinfo);
@@ -1119,6 +1162,11 @@ static void zebraExplain_writeDatabase (ZebraExplainInfo zei,
     /* zebra info (private) */
     node_zebra = data1_mk_tag_uni (zei->dh, zei->nmem,
                                 "zebraInfo", node_dbinfo);
+
+    /* extract *searchable* keys from it. We do this here, because
+       record count, etc. is affected */
+    if (key_flush)
+       (*zei->updateFunc)(zei->updateHandle, drec, zdi->data1_database);
     data1_mk_tag_data_zint (zei->dh, node_zebra,
                           "recordBytes", zdi->recordBytes, zei->nmem);
 
@@ -1260,12 +1308,12 @@ static void zebraExplain_writeTarget (ZebraExplainInfo zei, int key_flush)
     zebraExplain_updateCommonInfo (zei, node_tgtinfo);
     zebraExplain_updateAccessInfo (zei, node_tgtinfo, zei->accessInfo);
 
+    node_zebra = data1_mk_tag_uni (zei->dh, zei->nmem,
+                                "zebraInfo", node_tgtinfo);
     /* convert to "SGML" and write it */
     if (key_flush)
        (*zei->updateFunc)(zei->updateHandle, trec, zei->data1_target);
 
-    node_zebra = data1_mk_tag_uni (zei->dh, zei->nmem,
-                                "zebraInfo", node_tgtinfo);
     data1_mk_tag_data_text (zei->dh, node_zebra, "version",
                               ZEBRAVER, zei->nmem);
     node_list = data1_mk_tag (zei->dh, zei->nmem,
@@ -1303,7 +1351,9 @@ static void zebraExplain_writeTarget (ZebraExplainInfo zei, int key_flush)
     rec_put (zei->records, &trec);
 }
 
-int zebraExplain_lookup_attr_str(ZebraExplainInfo zei, int index_type,
+int zebraExplain_lookup_attr_str(ZebraExplainInfo zei, 
+                                 zinfo_index_category_t cat,
+                                 int index_type,
                                 const char *str)
 {
     struct zebSUInfoB **zsui;
@@ -1311,9 +1361,10 @@ int zebraExplain_lookup_attr_str(ZebraExplainInfo zei, int index_type,
     assert (zei->curDatabaseInfo);
     for (zsui = &zei->curDatabaseInfo->attributeDetails->SUInfo;
         *zsui; zsui = &(*zsui)->next)
-        if ((*zsui)->info.index_type == index_type
-            && (*zsui)->info.which == ZEB_SU_STR 
-            && !yaz_matchstr((*zsui)->info.u.str, str))
+        if ( (index_type == -1 || (*zsui)->info.index_type == index_type)
+             && (*zsui)->info.cat == cat
+             && (*zsui)->info.which == ZEB_SU_STR 
+             && !yaz_matchstr((*zsui)->info.u.str, str))
         {
             struct zebSUInfoB *zsui_this = *zsui;
 
@@ -1486,6 +1537,7 @@ void zebraExplain_addAttributeSet (ZebraExplainInfo zei, int set)
 }
 
 struct zebSUInfoB *zebraExplain_add_sui_info(ZebraExplainInfo zei,
+                                             zinfo_index_category_t cat,
                                              int index_type)
 {
     struct zebSUInfoB *zsui;
@@ -1497,16 +1549,19 @@ struct zebSUInfoB *zebraExplain_add_sui_info(ZebraExplainInfo zei,
     zei->curDatabaseInfo->attributeDetails->dirty = 1;
     zei->dirty = 1;
     zsui->info.index_type = index_type;
+    zsui->info.cat = cat;
     zsui->info.doc_occurrences = 0;
     zsui->info.term_occurrences = 0;
     zsui->info.ordinal = (zei->ordinalSU)++;
     return zsui;
 }
 
-int zebraExplain_add_attr_str(ZebraExplainInfo zei, int index_type,
+int zebraExplain_add_attr_str(ZebraExplainInfo zei, 
+                              zinfo_index_category_t cat,
+                              int index_type,
                              const char *index_name)
 {
-    struct zebSUInfoB *zsui = zebraExplain_add_sui_info(zei, index_type);
+    struct zebSUInfoB *zsui = zebraExplain_add_sui_info(zei, cat, index_type);
 
     zsui->info.which = ZEB_SU_STR;
     zsui->info.u.str = nmem_strdup(zei->nmem, index_name);
@@ -1558,6 +1613,8 @@ RecordAttr *rec_init_attr (ZebraExplainInfo zei, Record rec)
     if (rec->info[recInfo_attr])
        return (RecordAttr *) rec->info[recInfo_attr];
     recordAttr = (RecordAttr *) xmalloc (sizeof(*recordAttr));
+
+    memset(recordAttr, '\0', sizeof(*recordAttr));
     rec->info[recInfo_attr] = (char *) recordAttr;
     rec->size[recInfo_attr] = sizeof(*recordAttr);