Store unique ordinal integer for database. This is to begin solving
authorAdam Dickmeiss <adam@indexdata.dk>
Tue, 13 Dec 2005 13:47:35 +0000 (13:47 +0000)
committerAdam Dickmeiss <adam@indexdata.dk>
Tue, 13 Dec 2005 13:47:35 +0000 (13:47 +0000)
problem with drop and record IDs. Bug #447.

index/zinfo.c

index a5c05a9..69a3bad 100644 (file)
@@ -1,4 +1,4 @@
-/* $Id: zinfo.c,v 1.52 2005-12-08 11:10:03 adam Exp $
+/* $Id: zinfo.c,v 1.53 2005-12-13 13:47:35 adam Exp $
    Copyright (C) 1995-2005
    Index Data ApS
 
@@ -75,6 +75,7 @@ typedef struct {
 
 struct zebDatabaseInfoB {
     zebAttributeDetails attributeDetails;
+    int ordinalDatabase;
     char *databaseName;
     data1_node *data1_database;
     zint recordCount;    /* records in db */
@@ -100,6 +101,7 @@ struct zebraCategoryListInfo {
 
 struct zebraExplainInfo {
     int ordinalSU;
+    int ordinalDatabase;
     zint runNumber;
     int dirty;
     int write_flag;
@@ -118,53 +120,53 @@ struct zebraExplainInfo {
     void *updateHandle;
 };
 
-static void zebraExplain_initCommonInfo (ZebraExplainInfo zei, data1_node *n);
-static void zebraExplain_initAccessInfo (ZebraExplainInfo zei, data1_node *n);
+static void zebraExplain_initCommonInfo(ZebraExplainInfo zei, data1_node *n);
+static void zebraExplain_initAccessInfo(ZebraExplainInfo zei, data1_node *n);
 
-static data1_node *read_sgml_rec (data1_handle dh, NMEM nmem, Record rec)
+static data1_node *read_sgml_rec(data1_handle dh, NMEM nmem, Record rec)
 {
-    return data1_read_sgml (dh, nmem, rec->info[recInfo_storeData]);
+    return data1_read_sgml(dh, nmem, rec->info[recInfo_storeData]);
 }
 
-static void zebraExplain_writeDatabase (ZebraExplainInfo zei,
+static void zebraExplain_writeDatabase(ZebraExplainInfo zei,
                                         struct zebDatabaseInfoB *zdi,
                                        int key_flush);
-static void zebraExplain_writeAttributeDetails (ZebraExplainInfo zei,
+static void zebraExplain_writeAttributeDetails(ZebraExplainInfo zei,
                                                zebAttributeDetails zad,
                                                const char *databaseName,
                                                int key_flush);
-static void zebraExplain_writeTarget (ZebraExplainInfo zei, int key_flush);
-static void zebraExplain_writeAttributeSet (ZebraExplainInfo zei,
+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,
+static void zebraExplain_writeCategoryList(ZebraExplainInfo zei,
                                            struct zebraCategoryListInfo *zcl,
                                            int key_flush);
 
 
-static Record createRecord (Records records, SYSNO *sysno)
+static Record createRecord(Records records, SYSNO *sysno)
 {
     Record rec;
     if (*sysno)
     {
-       rec = rec_get (records, *sysno);
-       xfree (rec->info[recInfo_storeData]);
+       rec = rec_get(records, *sysno);
+       xfree(rec->info[recInfo_storeData]);
     }
     else
     {
-       rec = rec_new (records);
+       rec = rec_new(records);
        *sysno = rec->sysno;
        
        rec->info[recInfo_fileType] =
-           rec_strdup ("grs.sgml", &rec->size[recInfo_fileType]);
+           rec_strdup("grs.sgml", &rec->size[recInfo_fileType]);
        rec->info[recInfo_databaseName] =
-           rec_strdup ("IR-Explain-1",
+           rec_strdup("IR-Explain-1",
                        &rec->size[recInfo_databaseName]); 
     }
     return rec;
 }
 
-void zebraExplain_flush (ZebraExplainInfo zei, void *handle)
+void zebraExplain_flush(ZebraExplainInfo zei, void *handle)
 {
     if (!zei)
         return;
@@ -177,43 +179,43 @@ void zebraExplain_flush (ZebraExplainInfo zei, void *handle)
        /* write each database info record */
        for (zdi = zei->databaseInfo; zdi; zdi = zdi->next)
        {
-           zebraExplain_writeDatabase (zei, zdi, 1);
-           zebraExplain_writeAttributeDetails (zei, zdi->attributeDetails,
+           zebraExplain_writeDatabase(zei, zdi, 1);
+           zebraExplain_writeAttributeDetails(zei, zdi->attributeDetails,
                                                zdi->databaseName, 1);
        }
-       zebraExplain_writeTarget (zei, 1);
-       zebraExplain_writeCategoryList (zei,
+       zebraExplain_writeTarget(zei, 1);
+       zebraExplain_writeCategoryList(zei,
                                        zei->categoryList,
                                        1);
-       assert (zei->accessInfo);
+       assert(zei->accessInfo);
        for (o = zei->accessInfo->attributeSetIds; o; o = o->next)
            if (!o->sysno)
-               zebraExplain_writeAttributeSet (zei, o, 1);
+               zebraExplain_writeAttributeSet(zei, o, 1);
        for (o = zei->accessInfo->schemas; o; o = o->next)
            if (!o->sysno)
            {
-/*             zebraExplain_writeSchema (zei, o, 1); */
+/*             zebraExplain_writeSchema(zei, o, 1); */
            }
 
        for (zdi = zei->databaseInfo; zdi; zdi = zdi->next)
        {
-           zebraExplain_writeDatabase (zei, zdi, 0);
-           zebraExplain_writeAttributeDetails (zei, zdi->attributeDetails,
+           zebraExplain_writeDatabase(zei, zdi, 0);
+           zebraExplain_writeAttributeDetails(zei, zdi->attributeDetails,
                                                zdi->databaseName, 0);
        }
-       zebraExplain_writeTarget (zei, 0);
+       zebraExplain_writeTarget(zei, 0);
     }
 }
 
-void zebraExplain_close (ZebraExplainInfo zei)
+void zebraExplain_close(ZebraExplainInfo zei)
 {
 #if ZINFO_DEBUG
-    yaz_log (YLOG_LOG, "zebraExplain_close");
+    yaz_log(YLOG_LOG, "zebraExplain_close");
 #endif
     if (!zei)
        return;
-    zebraExplain_flush (zei, zei->updateHandle);
-    nmem_destroy (zei->nmem);
+    zebraExplain_flush(zei, zei->updateHandle);
+    nmem_destroy(zei->nmem);
 }
 
 void zebraExplain_mergeOids (ZebraExplainInfo zei, data1_node *n,
@@ -228,25 +230,25 @@ void zebraExplain_mergeOids (ZebraExplainInfo zei, data1_node *n,
        Odr_oid *oid;
        zebAccessObject ao;
 
-       if (np->which != DATA1N_tag || strcmp (np->u.tag.tag, "oid"))
+       if (np->which != DATA1N_tag || strcmp(np->u.tag.tag, "oid"))
            continue;
        len = np->child->u.data.len;
        if (len > 63)
            len = 63;
-       memcpy (str, np->child->u.data.data, len);
+       memcpy(str, np->child->u.data.data, len);
        str[len] = '\0';
        
-       oid = odr_getoidbystr_nmem (zei->nmem, str);
+       oid = odr_getoidbystr_nmem(zei->nmem, str);
 
        for (ao = *op; ao; ao = ao->next)
-           if (!oid_oidcmp (oid, ao->oid))
+           if (!oid_oidcmp(oid, ao->oid))
            {
                ao->sysno = 1;
                break;
            }
        if (!ao)
        {
-           ao = (zebAccessObject) nmem_malloc (zei->nmem, sizeof(*ao));
+           ao = (zebAccessObject) nmem_malloc(zei->nmem, sizeof(*ao));
            ao->handle = NULL;
            ao->sysno = 1;
            ao->oid = oid;
@@ -256,7 +258,7 @@ void zebraExplain_mergeOids (ZebraExplainInfo zei, data1_node *n,
     }
 }
 
-void zebraExplain_mergeAccessInfo (ZebraExplainInfo zei, data1_node *n,
+void zebraExplain_mergeAccessInfo(ZebraExplainInfo zei, data1_node *n,
                                   zebAccessInfo *accessInfo)
 {
     data1_node *np;
@@ -264,19 +266,19 @@ void zebraExplain_mergeAccessInfo (ZebraExplainInfo zei, data1_node *n,
     if (!n)
     {
        *accessInfo = (zebAccessInfo)
-           nmem_malloc (zei->nmem, sizeof(**accessInfo));
+           nmem_malloc(zei->nmem, sizeof(**accessInfo));
        (*accessInfo)->attributeSetIds = NULL;
        (*accessInfo)->schemas = NULL;
     }
     else
     {
-       if (!(n = data1_search_tag (zei->dh, n->child, "accessInfo")))
+       if (!(n = data1_search_tag(zei->dh, n->child, "accessInfo")))
            return;
-       if ((np = data1_search_tag (zei->dh, n->child, "attributeSetIds")))
-           zebraExplain_mergeOids (zei, np,
+       if ((np = data1_search_tag(zei->dh, n->child, "attributeSetIds")))
+           zebraExplain_mergeOids(zei, np,
                                    &(*accessInfo)->attributeSetIds);
-       if ((np = data1_search_tag (zei->dh, n->child, "schemas")))
-           zebraExplain_mergeOids (zei, np,
+       if ((np = data1_search_tag(zei->dh, n->child, "schemas")))
+           zebraExplain_mergeOids(zei, np,
                                    &(*accessInfo)->schemas);
     }
 }
@@ -327,7 +329,7 @@ explain:
 nextResultSetPosition = 2
 */
 
-ZebraExplainInfo zebraExplain_open (
+ZebraExplainInfo zebraExplain_open(
     Records records, data1_handle dh,
     Res res,
     int writeFlag,
@@ -339,17 +341,18 @@ ZebraExplainInfo zebraExplain_open (
     struct zebDatabaseInfoB **zdip;
     time_t our_time;
     struct tm *tm;
-    NMEM nmem = nmem_create ();
+    NMEM nmem = nmem_create();
 
 #if ZINFO_DEBUG
-    yaz_log (YLOG_LOG, "zebraExplain_open wr=%d", writeFlag);
+    yaz_log(YLOG_LOG, "zebraExplain_open wr=%d", writeFlag);
 #endif
-    zei = (ZebraExplainInfo) nmem_malloc (nmem, sizeof(*zei));
+    zei = (ZebraExplainInfo) nmem_malloc(nmem, sizeof(*zei));
     zei->databaseInfo = 0;
     zei->write_flag = writeFlag;
     zei->updateHandle = updateHandle;
     zei->updateFunc = updateFunc;
     zei->dirty = 0;
+    zei->ordinalDatabase = 1;
     zei->curDatabaseInfo = NULL;
     zei->records = records;
     zei->nmem = nmem;
@@ -357,20 +360,20 @@ ZebraExplainInfo zebraExplain_open (
     zei->attsets = NULL;
     zei->res = res;
     zei->categoryList = (struct zebraCategoryListInfo *)
-       nmem_malloc (zei->nmem, sizeof(*zei->categoryList));
+       nmem_malloc(zei->nmem, sizeof(*zei->categoryList));
     zei->categoryList->sysno = 0;
     zei->categoryList->dirty = 0;
     zei->categoryList->data1_categoryList = NULL;
 
-    if ( atoi (res_get_def (res, "notimestamps", "0") )== 0)
+    if ( atoi(res_get_def(res, "notimestamps", "0") )== 0)
     {
-        time (&our_time);
-        tm = localtime (&our_time);
-        sprintf (zei->date, "%04d%02d%02d%02d%02d%02d",
+        time(&our_time);
+        tm = localtime(&our_time);
+        sprintf(zei->date, "%04d%02d%02d%02d%02d%02d",
                 tm->tm_year+1900, tm->tm_mon+1,  tm->tm_mday,
                 tm->tm_hour, tm->tm_min, tm->tm_sec);
     } else {
-        sprintf (zei->date, "%04d%02d%02d%02d%02d%02d",
+        sprintf(zei->date, "%04d%02d%02d%02d%02d%02d",
                 0, 0, 0,  0, 0, 0);
     }
     zdip = &zei->databaseInfo;
@@ -379,63 +382,63 @@ ZebraExplainInfo zebraExplain_open (
     zei->ordinalSU = 1;
     zei->runNumber = 0;
 
-    zebraExplain_mergeAccessInfo (zei, 0, &zei->accessInfo);
+    zebraExplain_mergeAccessInfo(zei, 0, &zei->accessInfo);
     if (trec)    /* targetInfo already exists ... */
     {
        data1_node *node_tgtinfo, *node_zebra, *node_list, *np;
 
-       zei->data1_target = read_sgml_rec (zei->dh, zei->nmem, trec);
+       zei->data1_target = read_sgml_rec(zei->dh, zei->nmem, trec);
 #if 0
        if (!zei->data1_target || !zei->data1_target->u.root.absyn)
 #else
        if (!zei->data1_target)
 #endif
        {
-           yaz_log (YLOG_FATAL, "Explain schema missing. Check profilePath");
-           nmem_destroy (zei->nmem);
+           yaz_log(YLOG_FATAL, "Explain schema missing. Check profilePath");
+           nmem_destroy(zei->nmem);
            return 0;
        }
 #if ZINFO_DEBUG
-       data1_pr_tree (zei->dh, zei->data1_target, stderr);
+       data1_pr_tree(zei->dh, zei->data1_target, stderr);
 #endif
-       node_tgtinfo = data1_search_tag (zei->dh, zei->data1_target,
+       node_tgtinfo = data1_search_tag(zei->dh, zei->data1_target,
                                         "/targetInfo");
-       zebraExplain_mergeAccessInfo (zei, node_tgtinfo,
+       zebraExplain_mergeAccessInfo(zei, node_tgtinfo,
                                      &zei->accessInfo);
 
-       node_zebra = data1_search_tag (zei->dh, node_tgtinfo->child,
+       node_zebra = data1_search_tag(zei->dh, node_tgtinfo->child,
                                       "zebraInfo");
        np = 0;
        if (node_zebra)
        {
-           node_list = data1_search_tag (zei->dh, node_zebra->child,
+           node_list = data1_search_tag(zei->dh, node_zebra->child,
                                          "databaseList");
            if (node_list)
                np = node_list->child;
        }
-       for (; np; np = np->next)
+       for(; np; np = np->next)
        {
            data1_node *node_name = NULL;
            data1_node *node_id = NULL;
            data1_node *node_aid = NULL;
            data1_node *np2;
-           if (np->which != DATA1N_tag || strcmp (np->u.tag.tag, "database"))
+           if (np->which != DATA1N_tag || strcmp(np->u.tag.tag, "database"))
                continue;
-           for (np2 = np->child; np2; np2 = np2->next)
+           for(np2 = np->child; np2; np2 = np2->next)
            {
                if (np2->which != DATA1N_tag)
                    continue;
-               if (!strcmp (np2->u.tag.tag, "name"))
+               if (!strcmp(np2->u.tag.tag, "name"))
                    node_name = np2->child;
-               else if (!strcmp (np2->u.tag.tag, "id"))
+               else if (!strcmp(np2->u.tag.tag, "id"))
                    node_id = np2->child;
-               else if (!strcmp (np2->u.tag.tag, "attributeDetailsId"))
+               else if (!strcmp(np2->u.tag.tag, "attributeDetailsId"))
                    node_aid = np2->child;
            }
-           assert (node_id && node_name && node_aid);
+           assert(node_id && node_name && node_aid);
            
-           *zdip = (struct zebDatabaseInfoB *) 
-               nmem_malloc (zei->nmem, sizeof(**zdip));
+           *zdip =(struct zebDatabaseInfoB *) 
+               nmem_malloc(zei->nmem, sizeof(**zdip));
             (*zdip)->readFlag = 1;
             (*zdip)->dirty = 0;
            (*zdip)->data1_database = NULL;
@@ -445,8 +448,8 @@ ZebraExplainInfo zebraExplain_open (
 
            (*zdip)->databaseName = (char *)
                nmem_malloc (zei->nmem, 1+node_name->u.data.len);
-           memcpy ((*zdip)->databaseName, node_name->u.data.data,
-                   node_name->u.data.len);
+           memcpy((*zdip)->databaseName, node_name->u.data.data,
+                  node_name->u.data.len);
            (*zdip)->databaseName[node_name->u.data.len] = '\0';
            (*zdip)->sysno = atoi_zn (node_id->u.data.data,
                                      node_id->u.data.len);
@@ -462,21 +465,27 @@ ZebraExplainInfo zebraExplain_open (
        }
        if (node_zebra)
        {
-           np = data1_search_tag (zei->dh, node_zebra->child,
-                                  "ordinalSU");
+           np = data1_search_tag(zei->dh, node_zebra->child,
+                                 "ordinalSU");
            np = np->child;
            assert (np && np->which == DATA1N_data);
-           zei->ordinalSU = atoi_n (np->u.data.data, np->u.data.len);
+           zei->ordinalSU = atoi_n(np->u.data.data, np->u.data.len);
            
-           np = data1_search_tag (zei->dh, node_zebra->child,
+           np = data1_search_tag(zei->dh, node_zebra->child,
+                                 "ordinalDatabase");
+           np = np->child;
+           assert (np && np->which == DATA1N_data);
+           zei->ordinalDatabase = atoi_n(np->u.data.data, np->u.data.len);
+
+           np = data1_search_tag(zei->dh, node_zebra->child,
                                   "runNumber");
            np = np->child;
            assert (np && np->which == DATA1N_data);
-           zei->runNumber = atoi_zn (np->u.data.data, np->u.data.len);
-            yaz_log (YLOG_DEBUG, "read runnumber=" ZINT_FORMAT, zei->runNumber);
+           zei->runNumber = atoi_zn(np->u.data.data, np->u.data.len);
+            yaz_log(YLOG_DEBUG, "read runnumber=" ZINT_FORMAT, zei->runNumber);
            *zdip = NULL;
        }
-       rec_rm (&trec);
+       rec_rm(&trec);
     }
     else  /* create initial targetInfo */
     {
@@ -489,7 +498,7 @@ ZebraExplainInfo zebraExplain_open (
            int sgml_len;
 
            zei->data1_target =
-               data1_read_sgml (zei->dh, zei->nmem,
+               data1_read_sgml(zei->dh, zei->nmem,
                                 "<explain><targetInfo>TargetInfo\n"
                                 "<name>Zebra</>\n"
                                 "<namedResultSets>1</>\n"
@@ -498,34 +507,34 @@ ZebraExplainInfo zebraExplain_open (
                                 "</></>\n" );
            if (!zei->data1_target)
            {
-               yaz_log (YLOG_FATAL, "Explain schema missing. Check profilePath");
-               nmem_destroy (zei->nmem);
+               yaz_log(YLOG_FATAL, "Explain schema missing. Check profilePath");
+               nmem_destroy(zei->nmem);
                return 0;
            }
-           node_tgtinfo = data1_search_tag (zei->dh, zei->data1_target,
+           node_tgtinfo = data1_search_tag(zei->dh, zei->data1_target,
                                              "/targetInfo");
-           assert (node_tgtinfo);
+           assert(node_tgtinfo);
 
-           zebraExplain_initCommonInfo (zei, node_tgtinfo);
-           zebraExplain_initAccessInfo (zei, node_tgtinfo);
+           zebraExplain_initCommonInfo(zei, node_tgtinfo);
+           zebraExplain_initAccessInfo(zei, node_tgtinfo);
 
            /* write now because we want to be sure about the sysno */
-           trec = rec_new (records);
+           trec = rec_new(records);
            trec->info[recInfo_fileType] =
-               rec_strdup ("grs.sgml", &trec->size[recInfo_fileType]);
+               rec_strdup("grs.sgml", &trec->size[recInfo_fileType]);
            trec->info[recInfo_databaseName] =
-               rec_strdup ("IR-Explain-1", &trec->size[recInfo_databaseName]);
+               rec_strdup("IR-Explain-1", &trec->size[recInfo_databaseName]);
            
            sgml_buf = data1_nodetoidsgml(dh, zei->data1_target, 0, &sgml_len);
-           trec->info[recInfo_storeData] = (char *) xmalloc (sgml_len);
-           memcpy (trec->info[recInfo_storeData], sgml_buf, sgml_len);
+           trec->info[recInfo_storeData] = (char *) xmalloc(sgml_len);
+           memcpy(trec->info[recInfo_storeData], sgml_buf, sgml_len);
            trec->size[recInfo_storeData] = sgml_len;
            
-           rec_put (records, &trec);
-           rec_rm (&trec);
+           rec_put(records, &trec);
+           rec_rm(&trec);
 
        }
-       zebraExplain_newDatabase (zei, "IR-Explain-1", 0);
+       zebraExplain_newDatabase(zei, "IR-Explain-1", 0);
            
        if (!zei->categoryList->dirty)
        {
@@ -534,23 +543,23 @@ ZebraExplainInfo zebraExplain_open (
            
            zcl->dirty = 1;
            zcl->data1_categoryList =
-               data1_read_sgml (zei->dh, zei->nmem,
+               data1_read_sgml(zei->dh, zei->nmem,
                                 "<explain><categoryList>CategoryList\n"
                                 "</></>\n");
        
            if (zcl->data1_categoryList)
            {
-               node_cl = data1_search_tag (zei->dh, zcl->data1_categoryList,
+               node_cl = data1_search_tag(zei->dh, zcl->data1_categoryList,
                                            "/categoryList");
-               assert (node_cl);
-               zebraExplain_initCommonInfo (zei, node_cl);
+               assert(node_cl);
+               zebraExplain_initCommonInfo(zei, node_cl);
            }
        }
     }
     return zei;
 }
 
-static void zebraExplain_readAttributeDetails (ZebraExplainInfo zei,
+static void zebraExplain_readAttributeDetails(ZebraExplainInfo zei,
                                               zebAttributeDetails zad)
 {
     Record rec;
@@ -558,15 +567,15 @@ static void zebraExplain_readAttributeDetails (ZebraExplainInfo zei,
     data1_node *node_adinfo, *node_zebra, *node_list, *np;
 
     assert (zad->sysno);
-    rec = rec_get (zei->records, zad->sysno);
+    rec = rec_get(zei->records, zad->sysno);
 
-    zad->data1_tree = read_sgml_rec (zei->dh, zei->nmem, rec);
+    zad->data1_tree = read_sgml_rec(zei->dh, zei->nmem, rec);
 
-    node_adinfo = data1_search_tag (zei->dh, zad->data1_tree,
+    node_adinfo = data1_search_tag(zei->dh, zad->data1_tree,
                                    "/attributeDetails");
-    node_zebra = data1_search_tag (zei->dh, node_adinfo->child,
+    node_zebra = data1_search_tag(zei->dh, node_adinfo->child,
                                 "zebraInfo");
-    node_list = data1_search_tag (zei->dh, node_zebra->child,
+    node_list = data1_search_tag(zei->dh, node_zebra->child,
                                  "attrlist");
     for (np = node_list->child; np; np = np->next)
     {
@@ -579,28 +588,28 @@ static void zebraExplain_readAttributeDetails (ZebraExplainInfo zei,
        char oid_str[128];
        int oid_str_len;
 
-       if (np->which != DATA1N_tag || strcmp (np->u.tag.tag, "attr"))
+       if (np->which != DATA1N_tag || strcmp(np->u.tag.tag, "attr"))
            continue;
        for (np2 = np->child; np2; np2 = np2->next)
        {
            if (np2->which != DATA1N_tag || !np2->child ||
                np2->child->which != DATA1N_data)
                continue;
-           if (!strcmp (np2->u.tag.tag, "set"))
+           if (!strcmp(np2->u.tag.tag, "set"))
                node_set = np2->child;
-           else if (!strcmp (np2->u.tag.tag, "use"))
+           else if (!strcmp(np2->u.tag.tag, "use"))
                node_use = np2->child;
-           else if (!strcmp (np2->u.tag.tag, "str"))
+           else if (!strcmp(np2->u.tag.tag, "str"))
                node_str = np2->child;
-           else if (!strcmp (np2->u.tag.tag, "ordinal"))
+           else if (!strcmp(np2->u.tag.tag, "ordinal"))
                node_ordinal = np2->child;
-           else if (!strcmp (np2->u.tag.tag, "type"))
+           else if (!strcmp(np2->u.tag.tag, "type"))
                node_type = np2->child;
        }
-       assert (node_ordinal);
+       assert(node_ordinal);
 
         *zsuip = (struct zebSUInfoB *)
-           nmem_malloc (zei->nmem, sizeof(**zsuip));
+           nmem_malloc(zei->nmem, sizeof(**zsuip));
 
        if (node_type && node_type->u.data.len > 0)
            (*zsuip)->info.index_type =  node_type->u.data.data[0];
@@ -617,14 +626,14 @@ static void zebraExplain_readAttributeDetails (ZebraExplainInfo zei,
            oid_str_len = node_set->u.data.len;
            if (oid_str_len >= (int) sizeof(oid_str))
                oid_str_len = sizeof(oid_str)-1;
-           memcpy (oid_str, node_set->u.data.data, oid_str_len);
+           memcpy(oid_str, node_set->u.data.data, oid_str_len);
            oid_str[oid_str_len] = '\0';
 
-           (*zsuip)->info.u.su.set = oid_getvalbyname (oid_str);
+           (*zsuip)->info.u.su.set = oid_getvalbyname(oid_str);
            
-           (*zsuip)->info.u.su.use = atoi_n (node_use->u.data.data,
+           (*zsuip)->info.u.su.use = atoi_n(node_use->u.data.data,
                                         node_use->u.data.len);
-           yaz_log (YLOG_DEBUG, "set=%d use=%d ordinal=%d",
+           yaz_log(YLOG_DEBUG, "set=%d use=%d ordinal=%d",
                     (*zsuip)->info.u.su.set, (*zsuip)->info.u.su.use,
                     (*zsuip)->info.ordinal);
        }
@@ -674,6 +683,14 @@ static void zebraExplain_readDatabase (ZebraExplainInfo zei,
        && np->child && np->child->which == DATA1N_data)
        zdi->recordBytes = atoi_zn (np->child->u.data.data,
                                    np->child->u.data.len);
+
+    if (node_zebra
+       && (np = data1_search_tag (zei->dh, node_zebra->child,
+                                  "ordinalDatabase")) 
+       && np->child && np->child->which == DATA1N_data)
+       zdi->ordinalDatabase = atoi_n(np->child->u.data.data,
+                                     np->child->u.data.len);
+
     if ((np = data1_search_tag (zei->dh, node_dbinfo->child,
                                "recordCount")) &&
        (np = data1_search_tag (zei->dh, np->child,
@@ -748,19 +765,19 @@ int zebraExplain_curDatabase (ZebraExplainInfo zei, const char *database)
     if (!zdi)
         return -1;
 #if ZINFO_DEBUG
-    yaz_log (YLOG_LOG, "zebraExplain_curDatabase: %s", database);
+    yaz_log(YLOG_LOG, "zebraExplain_curDatabase: %s", database);
 #endif
     if (zdi->readFlag)
     {
 #if ZINFO_DEBUG
-       yaz_log (YLOG_LOG, "zebraExplain_readDatabase: %s", database);
+       yaz_log(YLOG_LOG, "zebraExplain_readDatabase: %s", database);
 #endif
         zebraExplain_readDatabase (zei, zdi);
     }
     if (zdi->attributeDetails->readFlag)
     {
 #if ZINFO_DEBUG
-       yaz_log (YLOG_LOG, "zebraExplain_readAttributeDetails: %s", database);
+       yaz_log(YLOG_LOG, "zebraExplain_readAttributeDetails: %s", database);
 #endif
         zebraExplain_readAttributeDetails (zei, zdi->attributeDetails);
     }
@@ -832,7 +849,7 @@ int zebraExplain_newDatabase (ZebraExplainInfo zei, const char *database,
         database_n = database;
 
 #if ZINFO_DEBUG
-    yaz_log (YLOG_LOG, "zebraExplain_newDatabase: %s", database);
+    yaz_log(YLOG_LOG, "zebraExplain_newDatabase: %s", database);
 #endif
     assert (zei);
     for (zdi = zei->databaseInfo; zdi; zdi=zdi->next)
@@ -852,6 +869,8 @@ int zebraExplain_newDatabase (ZebraExplainInfo zei, const char *database,
     zdi->readFlag = 0;
     zdi->databaseName = nmem_strdup (zei->nmem, database_n);
 
+    zdi->ordinalDatabase = zei->ordinalDatabase++;
+
     zebraExplain_mergeAccessInfo (zei, 0, &zdi->accessInfo);
     
     assert (zei->dh);
@@ -963,7 +982,7 @@ static void zebraExplain_writeCategoryList (ZebraExplainInfo zei,
     node_categoryList = zcl->data1_categoryList;
 
 #if ZINFO_DEBUG
-    yaz_log (YLOG_LOG, "zebraExplain_writeCategoryList");
+    yaz_log(YLOG_LOG, "zebraExplain_writeCategoryList");
 #endif
 
     drec = createRecord (zei->records, &sysno);
@@ -1017,7 +1036,7 @@ static void zebraExplain_writeAttributeDetails (ZebraExplainInfo zei,
     
     zad->dirty = 0;
 #if ZINFO_DEBUG
-    yaz_log (YLOG_LOG, "zebraExplain_writeAttributeDetails");    
+    yaz_log(YLOG_LOG, "zebraExplain_writeAttributeDetails");    
 #endif
 
     drec = createRecord (zei->records, &zad->sysno);
@@ -1160,7 +1179,7 @@ static void zebraExplain_writeDatabase (ZebraExplainInfo zei,
 
     zdi->dirty = 0;
 #if ZINFO_DEBUG
-    yaz_log (YLOG_LOG, "zebraExplain_writeDatabase %s", zdi->databaseName);
+    yaz_log(YLOG_LOG, "zebraExplain_writeDatabase %s", zdi->databaseName);
 #endif
     drec = createRecord (zei->records, &zdi->sysno);
     assert (zdi->data1_database);
@@ -1187,6 +1206,10 @@ static void zebraExplain_writeDatabase (ZebraExplainInfo zei,
                                 "zebraInfo", node_dbinfo);
     data1_mk_tag_data_zint (zei->dh, node_zebra,
                           "recordBytes", zdi->recordBytes, zei->nmem);
+
+    data1_mk_tag_data_zint(zei->dh, node_zebra,
+                          "ordinalDatabase", zdi->ordinalDatabase, zei->nmem);
+
     /* convert to "SGML" and write it */
 #if ZINFO_DEBUG
     data1_pr_tree (zei->dh, zdi->data1_database, stderr);
@@ -1244,7 +1267,7 @@ static void zebraExplain_writeAttributeSet (ZebraExplainInfo zei,
        attset = data1_attset_search_id (zei->dh, entp->value);
            
 #if ZINFO_DEBUG
-    yaz_log (YLOG_LOG, "zebraExplain_writeAttributeSet %s",
+    yaz_log(YLOG_LOG, "zebraExplain_writeAttributeSet %s",
          attset ? attset->name : "<unknown>");    
 #endif
 
@@ -1345,6 +1368,9 @@ static void zebraExplain_writeTarget (ZebraExplainInfo zei, int key_flush)
     data1_mk_tag_data_int (zei->dh, node_zebra, "ordinalSU",
                            zei->ordinalSU, zei->nmem);
 
+    data1_mk_tag_data_int (zei->dh, node_zebra, "ordinalDatabase",
+                           zei->ordinalDatabase, zei->nmem);
+
     data1_mk_tag_data_zint (zei->dh, node_zebra, "runNumber",
                            zei->runNumber, zei->nmem);
 
@@ -1580,7 +1606,7 @@ static void att_loadset(void *p, const char *n, const char *name)
 {
     data1_handle dh = (data1_handle) p;
     if (!data1_get_attset (dh, name))
-       yaz_log (YLOG_WARN, "Directive attset failed for %s", name);
+       yaz_log(YLOG_WARN, "Directive attset failed for %s", name);
 }
 
 void zebraExplain_loadAttsets (data1_handle dh, Res res)