Some more debugging and comments.
authorkim <kim@3cdefd35-fc62-479d-8e8d-bae585ffb9ca>
Thu, 29 Jan 2009 08:12:33 +0000 (08:12 +0000)
committerkim <kim@3cdefd35-fc62-479d-8e8d-bae585ffb9ca>
Thu, 29 Jan 2009 08:12:33 +0000 (08:12 +0000)
git-svn-id: svn+ssh://idea.nguyen.vg/svn/sxsi/trunk/XMLTree@91 3cdefd35-fc62-479d-8e8d-bae585ffb9ca

XMLTree.cpp
XMLTree.h

index a696d7f..b93e75a 100644 (file)
@@ -1,7 +1,7 @@
 #include "XMLTree.h"\r
 #include <cstring>\r
 \r
- // functions to convert tag positions to the corresponding tree node and viceversa. \r
+// functions to convert tag positions to the corresponding tree node and viceversa. \r
 // These are implemented in order to be able to change the tree and Tags representations, \r
 // without affecting the code so much.\r
 // Current implementation corresponds to balanced-parentheses representation for\r
@@ -17,7 +17,8 @@ inline int node2tagpos(treeNode x) {
    return (int)x;\r
 }\r
 \r
-// to prevent suprious "unused result" warnings\r
+\r
+//KIM OJO to prevent suprious "unused result" warnings\r
 \r
 inline void ufread(void *ptr, size_t size, size_t nmemb, FILE *stream){\r
   size_t res;\r
@@ -27,6 +28,7 @@ inline void ufread(void *ptr, size_t size, size_t nmemb, FILE *stream){
 \r
   return;\r
 }\r
+\r
 inline void ufwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream){\r
   size_t res;\r
   res = fwrite(ptr,size,nmemb,stream);\r
@@ -35,6 +37,36 @@ inline void ufwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream){
   return;\r
 }\r
 \r
+// OJO to fail cleanly while doing a realloc\r
+// if we can't realloc we are pretty much screwed anyway but\r
+// it makes the code clearer to not have a bunch of if (!ptr) { printf("..."); exit(1); };\r
+inline void * urealloc(void *ptr, size_t size){\r
+\r
+  void * dest = realloc(ptr,size);\r
+  //don't fail if we requested size 0\r
+  if (dest == NULL && size > 0 )\r
+    throw std::bad_alloc();\r
+  return dest;\r
+\r
+}\r
+\r
+inline void * ucalloc(size_t nmemb, size_t size){\r
+\r
+  void * dest = calloc(nmemb,size);\r
+  //don't fail if we requested size 0\r
+  if (dest == NULL && nmemb > 0 && size > 0 )\r
+    throw std::bad_alloc();\r
+  return dest;\r
+\r
+}\r
+\r
+inline void * umalloc(size_t size){\r
+  void * dest = malloc(size);\r
+  if (dest == NULL && size > 0)\r
+    throw std::bad_alloc();\r
+  return dest;\r
+}\r
+\r
 // Save: saves XML tree data structure to file. \r
 void XMLTree::Save(unsigned char *filename) \r
  {\r
@@ -85,52 +117,53 @@ XMLTree *XMLTree::Load(unsigned char *filename, int sample_rate_text)
  {\r
 \r
     FILE *fp;\r
-    char filenameaux[1024];\r
+    char buffer[1024];\r
     XMLTree *XML_Tree;\r
     int i;\r
     \r
     // first load the tree topology\r
-    sprintf(filenameaux, "%s.srx", filename);\r
-    fp = fopen(filenameaux, "r");\r
+    sprintf(buffer, "%s.srx", filename);\r
+    fp = fopen(buffer, "r");\r
     if (fp == NULL) {\r
-       printf("Error: cannot open file %s to load the tree structure of XML collection\n", filenameaux);\r
+       printf("Error: cannot open file %s to load the tree structure of XML collection\n", buffer);\r
        exit(1);\r
     } \r
 \r
     XML_Tree = new XMLTree();\r
 \r
-    XML_Tree->Par = (bp *)malloc(sizeof(bp));\r
+    XML_Tree->Par = (bp *)umalloc(sizeof(bp));\r
 \r
     loadTree(XML_Tree->Par, fp); \r
     \r
     // stores the table with tag names\r
     ufread(&XML_Tree->ntagnames, sizeof(int), 1, fp);\r
-    XML_Tree->TagName = (unsigned char **)malloc(XML_Tree->ntagnames*sizeof(unsigned char *));\r
+    XML_Tree->TagName = (unsigned char **)umalloc(XML_Tree->ntagnames*sizeof(unsigned char *));\r
 \r
     for (i=0; i<XML_Tree->ntagnames;i++) {\r
       \r
-      // Kim is it needed ?\r
+      // OJO Kim is it needed ?\r
       int k = feof(fp);\r
-       // fscanf chokes ont "\n" which is the case for the root element\r
-       char * r = fgets(filenameaux,1023,fp);\r
-       //       int r = fscanf(fp, "<%s>\n",filenameaux);\r
+\r
+      \r
+       // fscanf chokes on "\n" which is the case for the root element\r
+       char * r = fgets(buffer,1023,fp);\r
+       //       int r = fscanf(fp, "%s\n",buffer);\r
        if (r==NULL)\r
         throw "Cannot read tag list";\r
 \r
-\r
-       int len = strlen((const char*)filenameaux);\r
-       XML_Tree->TagName[i] = (unsigned char *)calloc(len,sizeof(char));\r
-\r
-       //XML_Tree->TagName[i] = (unsigned char *)malloc(sizeof(unsigned char)*(strlen((const char *)filenameaux)+1));\r
-       //the - 1 removes the trailing \n\r
-       strncpy((char *)XML_Tree->TagName[i], (const char *)filenameaux,len - 1);\r
+       // strlen is actually the right size, since there is a trailing '\n'\r
+       int len = strlen((const char*)buffer);\r
+       XML_Tree->TagName[i] = (unsigned char *)ucalloc(len,sizeof(char));\r
+       strncpy((char *)XML_Tree->TagName[i], (const char *)buffer,len - 1);\r
     }\r
        \r
     // loads the flags\r
+\r
     ufread(&(XML_Tree->indexing_empty_texts), sizeof(bool), 1, fp);\r
     ufread(&(XML_Tree->initialized), sizeof(bool), 1, fp);\r
     ufread(&(XML_Tree->finished), sizeof(bool), 1, fp);\r
     ufread(&(XML_Tree->disable_tc), sizeof(bool), 1, fp);\r
+\r
     if (!(XML_Tree->indexing_empty_texts)) XML_Tree->EBVector = static_bitsequence_rrr02::load(fp);\r
 \r
     // loads the tags\r
@@ -297,7 +330,6 @@ int XMLTree::Postorder(treeNode x)
        fprintf(stderr, "Error: data structure has not been constructed properly\n");\r
        exit(1);\r
     }\r
-\r
     return postorder_rank(Par, x);\r
  }\r
 \r
@@ -591,10 +623,15 @@ treeNode XMLTree::ParentNode(DocID d)
        fprintf(stderr, "Error: data structure has not been constructed properly\n");\r
        exit(1);\r
     }\r
-\r
-    int s;\r
+    \r
+    if (d == NULLT)\r
+      return NULLT;\r
+    \r
+    int s = d;\r
+    // Kim : I added the d+1. before that, else was select1(d)\r
+    // and gave wrong results but I'm really poking a dead bear with a stick here.\r
     if (indexing_empty_texts) s = d;\r
-    else s = EBVector->select1(d);\r
+    else s = EBVector->select1(d+1);\r
     \r
     if (inspect(Par,s) == CP) // is a closing parenthesis\r
        return parent(Par, find_open(Par, s));\r
@@ -619,48 +656,25 @@ int XMLTree::OpenDocument(bool empty_texts, int sample_rate_text,bool dtc)
     \r
     indexing_empty_texts = empty_texts;\r
     \r
-    par_aux = (pb *)malloc(sizeof(pb)*parArraySize);\r
-    if (!par_aux) {\r
-       fprintf(stderr, "Error: not enough memory\n");\r
-       return NULLT;\r
-    }\r
+    par_aux = (pb *)umalloc(sizeof(pb)*parArraySize);\r
     \r
-    tags_aux = (TagType *) malloc(sizeof(TagType));\r
-    if (!tags_aux) {\r
-       fprintf(stderr, "Error: not enough memory\n");\r
-       return NULLT;\r
-    }\r
+    tags_aux = (TagType *) umalloc(sizeof(TagType));\r
     \r
-    TagName = (unsigned char **) malloc(2*sizeof(unsigned char*));\r
-    if (!TagName){\r
-       fprintf(stderr, "Error: not enough memory\n");\r
-       return NULLT;\r
-    }\r
+    TagName = (unsigned char **) umalloc(2*sizeof(unsigned char*));\r
 \r
-    TagName[0] = (unsigned char *) malloc(4*sizeof(unsigned char));\r
-    strcpy((char *) TagName[0], "<@>");\r
+    TagName[0] = (unsigned char *) umalloc(4*sizeof(unsigned char));\r
 \r
-    if (!TagName[0]){\r
-      fprintf(stderr, "Error: not enough memory\n");\r
-      return NULLT;\r
-    }\r
+    strcpy((char *) TagName[0], "<@>");\r
 \r
-    TagName[1] = (unsigned char *) malloc(4*sizeof(unsigned char));\r
-    if (!TagName[1]){\r
-      fprintf(stderr, "Error: not enough memory\n");\r
-      return NULLT;\r
-    }\r
+    TagName[1] = (unsigned char *) umalloc(4*sizeof(unsigned char));\r
 \r
     strcpy((char *) TagName[1], "<$>");\r
 \r
 \r
-    if (!indexing_empty_texts) {\r
-       empty_texts_aux = (unsigned int *)malloc(sizeof(unsigned int));\r
-       if (!empty_texts_aux) {\r
-          fprintf(stderr, "Error: not enough memory\n");\r
-          return NULLT;\r
-       }\r
-    }\r
+    if (!indexing_empty_texts) \r
+      empty_texts_aux = (unsigned int *)umalloc(sizeof(unsigned int));\r
+       \r
+    \r
     \r
     Text = TextCollection::InitTextCollection((unsigned)sample_rate_text);\r
     \r
@@ -679,14 +693,10 @@ int XMLTree::CloseDocument()
     }\r
     \r
     // closing parenthesis for the tree root\r
-    par_aux = (pb *)realloc(par_aux, sizeof(pb)*(1+npar/(8*sizeof(pb))));\r
-    if (!par_aux) {\r
-       fprintf(stderr, "Error: not enough memory\n");\r
-       return NULLT;    \r
-    }\r
+    par_aux = (pb *)urealloc(par_aux, sizeof(pb)*(1+npar/(8*sizeof(pb))));\r
     \r
     // creates the data structure for the tree topology\r
-    Par = (bp *)malloc(sizeof(bp));\r
+    Par = (bp *)umalloc(sizeof(bp));\r
     bp_construct(Par, npar, par_aux, OPT_DEGREE|0);    \r
     // creates structure for tags\r
     static_bitsequence_builder * bmb = new static_bitsequence_builder_brw32(20);\r
@@ -714,7 +724,9 @@ int XMLTree::CloseDocument()
        empty_texts_aux = NULL;\r
     }\r
    \r
+    // OJO was leaked before, found by valgrind\r
     free(tags_aux);\r
+\r
     tags_aux = NULL;\r
 \r
     finished = true;\r
@@ -737,15 +749,10 @@ int XMLTree::NewOpenTag(unsigned char *tagname)
     \r
     // inserts a new opening parentheses in the bit sequence\r
     if (sizeof(pb)*8*parArraySize == npar) { // no space left for the new parenthesis\r
-       par_aux = (pb *)realloc(par_aux, sizeof(pb)*2*parArraySize);\r
+       par_aux = (pb *)urealloc(par_aux, sizeof(pb)*2*parArraySize);\r
        parArraySize *= 2;\r
     }\r
     \r
-    if (!par_aux) {\r
-       fprintf(stderr, "Error: not enough memory\n");\r
-       return NULLT;    \r
-    }\r
-\r
     setbit(par_aux,npar,OP);  // marks a new opening parenthesis\r
 \r
     // transforms the tagname into a tag identifier. If the tag is new, we insert\r
@@ -759,7 +766,7 @@ int XMLTree::NewOpenTag(unsigned char *tagname)
       found_attributes=true;\r
 \r
     if (i==ntagnames) { // the tag is a new one, then we insert it\r
-       TagName = (unsigned char **)realloc(TagName, sizeof(char *)*(ntagnames+1));\r
+       TagName = (unsigned char **)urealloc(TagName, sizeof(char *)*(ntagnames+1));\r
        \r
        if (!TagName) {\r
           fprintf(stderr, "Error: not enough memory\n");\r
@@ -767,14 +774,10 @@ int XMLTree::NewOpenTag(unsigned char *tagname)
        }\r
        \r
        ntagnames++;\r
-       TagName[i] = (unsigned char *)malloc(sizeof(unsigned char)*(strlen((const char *)tagname)+1));\r
+       TagName[i] = (unsigned char *)umalloc(sizeof(unsigned char)*(strlen((const char *)tagname)+1));\r
        strcpy((char *)TagName[i], (const char *)tagname);\r
     } \r
-    tags_aux = (TagType *) realloc(tags_aux, sizeof(TagType)*(npar + 1));\r
-    if (!tags_aux) {\r
-       fprintf(stderr, "Error: not enough memory\n");\r
-       return NULLT;\r
-    }\r
+    tags_aux = (TagType *) urealloc(tags_aux, sizeof(TagType)*(npar + 1));\r
 \r
     tags_aux[npar] = i; // inserts the new tag id within the preorder sequence of tags\r
     \r
@@ -799,14 +802,10 @@ int XMLTree::NewClosingTag(unsigned char *tagname)
     \r
     // inserts a new closing parentheses in the bit sequence\r
     if (sizeof(pb)*8*parArraySize == npar) { // no space left for the new parenthesis\r
-       par_aux = (pb *)realloc(par_aux, sizeof(pb)*2*parArraySize);\r
+       par_aux = (pb *)urealloc(par_aux, sizeof(pb)*2*parArraySize);\r
        parArraySize *= 2;\r
     }\r
     \r
-    if (!par_aux) {\r
-       fprintf(stderr, "Error: not enough memory\n");\r
-       return NULLT;    \r
-    }\r
     setbit(par_aux,npar,CP);  // marks a new closing opening parenthesis\r
 \r
     // transforms the tagname into a tag identifier. If the tag is new, we insert\r
@@ -815,25 +814,16 @@ int XMLTree::NewClosingTag(unsigned char *tagname)
        if (strcmp((const char *)tagname,(const char *)TagName[i])==0) break;\r
  \r
     if (i==ntagnames) { // the tag is a new one, then we insert it\r
-       TagName = (unsigned char **)realloc(TagName, sizeof(char *)*(ntagnames+1));\r
+       TagName = (unsigned char **)urealloc(TagName, sizeof(char *)*(ntagnames+1));\r
        \r
-       if (!TagName) {\r
-          fprintf(stderr, "Error: not enough memory\n");\r
-          return NULLT;\r
-       }\r
        \r
        ntagnames++;\r
-       TagName[i] = (unsigned char *)malloc(sizeof(char)*(strlen((const char *)tagname)+2));\r
+       TagName[i] = (unsigned char *)umalloc(sizeof(char)*(strlen((const char *)tagname)+2));\r
        TagName[i][0] = '/';\r
        strcpy((char *)&(TagName[i][1]), (const char *)tagname);\r
     } \r
 \r
-    tags_aux = (TagType *)realloc(tags_aux, sizeof(TagType)*(npar + 1));\r
-\r
-    if (!tags_aux) {\r
-       fprintf(stderr, "Error: not enough memory\n");\r
-       return NULLT;\r
-    }\r
+    tags_aux = (TagType *)urealloc(tags_aux, sizeof(TagType)*(npar + 1));\r
 \r
     tags_aux[npar] = i; // inserts the new tag id within the preorder sequence of tags\r
     \r
@@ -860,13 +850,8 @@ int XMLTree::NewText(unsigned char *s)
     };\r
 \r
     if (!indexing_empty_texts) {\r
-       empty_texts_aux = (unsigned int *)realloc(empty_texts_aux, sizeof(pb)*(1+(npar-1)/(8*sizeof(pb))));\r
-       if (!empty_texts_aux) {\r
-          fprintf(stderr, "Error: not enough memory\n");\r
-          return NULLT;\r
-       }\r
-       \r
-       bitset(empty_texts_aux, npar-1);  // marks the non-empty text with a 1 in the bit vector\r
+       empty_texts_aux = (unsigned int *)urealloc(empty_texts_aux, sizeof(pb)*(1+(npar-1)/(8*sizeof(pb))));\r
+              bitset(empty_texts_aux, npar-1);  // marks the non-empty text with a 1 in the bit vector\r
     }\r
     \r
     Text->InsertText(s);\r
@@ -888,11 +873,7 @@ int XMLTree::NewEmptyText()
     }\r
 \r
     if (!indexing_empty_texts) {\r
-       empty_texts_aux = (unsigned int *)realloc(empty_texts_aux, sizeof(pb)*(1+(npar-1)/(8*sizeof(pb))));\r
-       if (!empty_texts_aux) {\r
-          fprintf(stderr, "Error: not enough memory\n");\r
-          return NULLT;\r
-       }\r
+       empty_texts_aux = (unsigned int *)urealloc(empty_texts_aux, sizeof(pb)*(1+(npar-1)/(8*sizeof(pb))));\r
        \r
        bitclean(empty_texts_aux, npar-1);  // marks the empty text with a 0 in the bit vector\r
     }\r
@@ -922,23 +903,33 @@ unsigned char *XMLTree::GetTagName(TagType tagid)
     unsigned char *s;\r
 \r
     if (tagid >= ntagnames) return NULL; // invalid tag identifier\r
-    s = (unsigned char *)malloc((strlen((const char *)TagName[tagid])+1)*sizeof(unsigned char));\r
+    s = (unsigned char *)umalloc((strlen((const char *)TagName[tagid])+1)*sizeof(unsigned char));\r
     strcpy((char *)s, (const char *)TagName[tagid]);\r
     return s;\r
  }\r
 \r
 \r
+//KIM : OJO need the two following methods\r
+\r
+const unsigned char *XMLTree::GetTagNameByRef(TagType tagid)\r
+ {\r
+    if (tagid >= ntagnames) return NULL; // invalid tag identifier\r
+    return ((const unsigned char*)  TagName[tagid]);\r
+ }\r
+\r
+\r
+\r
 TagType XMLTree::RegisterTag(unsigned char *tagname)\r
 {\r
   if (!finished)\r
     return NULLT;\r
   \r
-\r
   TagType id = XMLTree::GetTagId(tagname);\r
   if (id == NULLT){\r
     id = ntagnames;\r
     ntagnames = ntagnames + 1;    \r
-    TagName = (unsigned char **) realloc(TagName,ntagnames*(sizeof(unsigned char*)));\r
+    TagName = (unsigned char **) urealloc(TagName,ntagnames*(sizeof(unsigned char*)));\r
+    TagName[id] = (unsigned char *) umalloc(sizeof(unsigned char)*strlen( (const char*) tagname)+1);\r
     strcpy((char*)TagName[id], (const char *)tagname);  \r
   };\r
 \r
index 759aff8..7c08fcc 100644 (file)
--- a/XMLTree.h
+++ b/XMLTree.h
@@ -26,6 +26,7 @@
 #include <stdlib.h>\r
 #include <cstring>\r
 \r
+//KIM : OJO\r
 //clash between TextCollection/Tools.h and libcds/includes/basics.h\r
 #undef W\r
 #undef WW\r
@@ -61,6 +62,13 @@ typedef struct {
 } range;\r
 \r
 \r
+//KIM : OJO\r
+// I know this class implements the working draft that we have but the logics seem flawed here...\r
+// We should have two classes. One XMLTreeBuilder and one XMLTree.\r
+// XMLTreeBuilder would have OpenDocument, NewOpenTag,... and CloseDocument would return an XMLTree\r
+// XMLTree would have only an initialized structure. If find it really ugly to check (!finished) or (!initialized)\r
+// in every function (FirstChild....).\r
+\r
 class XMLTree {\r
    /** Balanced parentheses representation of the tree */\r
    bp *Par;\r
@@ -96,7 +104,17 @@ class XMLTree {
    int parArraySize;\r
    int ntagnames;\r
    unsigned int *empty_texts_aux;\r
+\r
+   // KIM : OJO\r
+   // I added those two. The TagName array should always contains two special tags\r
+   // <@> for attributes and <$> for PCDATA.\r
+   // <$> can never be in a document (since we handle the text differently)\r
+   // but <@> can be returned by the parser. This boolean is needed for the construction\r
+   // of the Tag bitmap to know if <@> must be taken into account or not\r
    bool found_attributes;\r
+\r
+   // KIM : OJO\r
+   // Allows to disable the TextCollection for benchmarkin purposes\r
    bool disable_tc;\r
    \r
 public:\r
@@ -226,6 +244,7 @@ public:
     * (i.e. everything is considered an empty text *)\r
     * Returns a non-zero value upon success, NULLT in case of \r
     * error. */\r
+\r
    int OpenDocument(bool empty_texts, int sample_rate_text, bool dtc);\r
 \r
    /** CloseDocument(): finishes the construction of the data structure for \r
@@ -265,6 +284,19 @@ public:
    unsigned char *GetTagName(TagType tagid);\r
 \r
 \r
+   // OJO\r
+   /** GetTagName(tagid): returns the tag name of a given tag identifier.     \r
+    *  The result is just a reference and should not be freed by the caller.\r
+    */\r
+   const unsigned char *GetTagNameByRef(TagType tagid);\r
+\r
+   //OJO\r
+   /** RegisterTag adds a new tag to the tag collection this is needed\r
+    * if the query contains a tag which is not in the document, we need\r
+    * to give this new tag a fresh id and store it somewhere. A logical\r
+    * choice is here.\r
+    * We might want to use a hashtable instead of an array though.\r
+    */\r
    TagType RegisterTag(unsigned char *tagname);\r
 \r
    bool EmptyText(DocID i) {\r