X-Git-Url: http://git.nguyen.vg/gitweb/?a=blobdiff_plain;f=XMLTree.cpp;h=cdfe95296d6e4310ac04356d18e374af0688600d;hb=39ee9d4c866fd5e4ba478ef65dc7e714e80c0b91;hp=a696d7f62507ce8a10b1ea256b3c0902ebc3d168;hpb=2220c6ca1fb8ebefd48d6f5d9a6be3afc7325da5;p=SXSI%2FXMLTree.git diff --git a/XMLTree.cpp b/XMLTree.cpp index a696d7f..cdfe952 100644 --- a/XMLTree.cpp +++ b/XMLTree.cpp @@ -1,7 +1,7 @@ #include "XMLTree.h" #include - // functions to convert tag positions to the corresponding tree node and viceversa. +// functions to convert tag positions to the corresponding tree node and viceversa. // These are implemented in order to be able to change the tree and Tags representations, // without affecting the code so much. // Current implementation corresponds to balanced-parentheses representation for @@ -17,7 +17,8 @@ inline int node2tagpos(treeNode x) { return (int)x; } -// to prevent suprious "unused result" warnings + +//KIM OJO to prevent suprious "unused result" warnings inline void ufread(void *ptr, size_t size, size_t nmemb, FILE *stream){ size_t res; @@ -27,6 +28,7 @@ inline void ufread(void *ptr, size_t size, size_t nmemb, FILE *stream){ return; } + inline void ufwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream){ size_t res; res = fwrite(ptr,size,nmemb,stream); @@ -35,6 +37,36 @@ inline void ufwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream){ return; } +// OJO to fail cleanly while doing a realloc +// if we can't realloc we are pretty much screwed anyway but +// it makes the code clearer to not have a bunch of if (!ptr) { printf("..."); exit(1); }; +inline void * urealloc(void *ptr, size_t size){ + + void * dest = realloc(ptr,size); + //don't fail if we requested size 0 + if (dest == NULL && size > 0 ) + throw std::bad_alloc(); + return dest; + +} + +inline void * ucalloc(size_t nmemb, size_t size){ + + void * dest = calloc(nmemb,size); + //don't fail if we requested size 0 + if (dest == NULL && nmemb > 0 && size > 0 ) + throw std::bad_alloc(); + return dest; + +} + +inline void * umalloc(size_t size){ + void * dest = malloc(size); + if (dest == NULL && size > 0) + throw std::bad_alloc(); + return dest; +} + // Save: saves XML tree data structure to file. void XMLTree::Save(unsigned char *filename) { @@ -73,7 +105,15 @@ void XMLTree::Save(unsigned char *filename) // stores the texts if (!disable_tc) Text->Save(fp); - + if (!disable_tc){ + int st = CachedText.size(); + ufwrite(&st, sizeof(int),1,fp); + for (int i = 0; i< CachedText.size(); ++i){ + st = CachedText.at(i).size(); + ufwrite(&st, sizeof(int),1,fp); + ufwrite(CachedText.at(i).c_str(),sizeof(char),(1+strlen(CachedText.at(i).c_str())),fp); + }; + }; fclose(fp); } @@ -85,66 +125,111 @@ XMLTree *XMLTree::Load(unsigned char *filename, int sample_rate_text) { FILE *fp; - char filenameaux[1024]; + char buffer[1024]; XMLTree *XML_Tree; int i; - + size_t s_tree = 0; + long s_text = 0; + size_t s_tags = 0; + // first load the tree topology - sprintf(filenameaux, "%s.srx", filename); - fp = fopen(filenameaux, "r"); + sprintf(buffer, "%s.srx", filename); + fp = fopen(buffer, "r"); if (fp == NULL) { - printf("Error: cannot open file %s to load the tree structure of XML collection\n", filenameaux); + printf("Error: cannot open file %s to load the tree structure of XML collection\n", buffer); exit(1); } XML_Tree = new XMLTree(); - XML_Tree->Par = (bp *)malloc(sizeof(bp)); + XML_Tree->Par = (bp *)umalloc(sizeof(bp)); loadTree(XML_Tree->Par, fp); - + + s_tree += sizeof(bp); + // stores the table with tag names ufread(&XML_Tree->ntagnames, sizeof(int), 1, fp); - XML_Tree->TagName = (unsigned char **)malloc(XML_Tree->ntagnames*sizeof(unsigned char *)); + + s_tree += sizeof(int); + + XML_Tree->TagName = (unsigned char **)umalloc(XML_Tree->ntagnames*sizeof(unsigned char *)); + + s_tags += sizeof(unsigned char*)*XML_Tree->ntagnames; + for (i=0; intagnames;i++) { - // Kim is it needed ? + // OJO Kim is it needed ? int k = feof(fp); - // fscanf chokes ont "\n" which is the case for the root element - char * r = fgets(filenameaux,1023,fp); - // int r = fscanf(fp, "<%s>\n",filenameaux); + + + // fscanf chokes on "\n" which is the case for the root element + char * r = fgets(buffer,1023,fp); + // int r = fscanf(fp, "%s\n",buffer); if (r==NULL) throw "Cannot read tag list"; - - int len = strlen((const char*)filenameaux); - XML_Tree->TagName[i] = (unsigned char *)calloc(len,sizeof(char)); - - //XML_Tree->TagName[i] = (unsigned char *)malloc(sizeof(unsigned char)*(strlen((const char *)filenameaux)+1)); - //the - 1 removes the trailing \n - strncpy((char *)XML_Tree->TagName[i], (const char *)filenameaux,len - 1); + // strlen is actually the right size, since there is a trailing '\n' + int len = strlen((const char*)buffer); + XML_Tree->TagName[i] = (unsigned char *)ucalloc(len,sizeof(char)); + strncpy((char *)XML_Tree->TagName[i], (const char *)buffer,len - 1); + s_tags+= len*sizeof(char); } // loads the flags + ufread(&(XML_Tree->indexing_empty_texts), sizeof(bool), 1, fp); ufread(&(XML_Tree->initialized), sizeof(bool), 1, fp); ufread(&(XML_Tree->finished), sizeof(bool), 1, fp); ufread(&(XML_Tree->disable_tc), sizeof(bool), 1, fp); - if (!(XML_Tree->indexing_empty_texts)) XML_Tree->EBVector = static_bitsequence_rrr02::load(fp); + + s_tree+=sizeof(bool)*4; + if (!(XML_Tree->indexing_empty_texts)) XML_Tree->EBVector = static_bitsequence_rrr02::load(fp); + + s_tree+= XML_Tree->EBVector->size(); + // loads the tags XML_Tree->Tags = static_sequence::load(fp); + s_tree+= XML_Tree->Tags->size(); + + s_text = ftell(fp); // loads the texts if (!XML_Tree->disable_tc){ XML_Tree->Text = TextCollection::InitTextCollection(sample_rate_text); XML_Tree->Text->Load(fp,sample_rate_text); + int sst; + int st; + ufread(&sst, sizeof(int),1,fp); + for (int i=0;iCachedText.push_back(cppstr); + free(str); + }; + } else XML_Tree->Text = NULL; + s_text = ftell(fp) - s_text; + + + + fclose(fp); + + std::cerr << "Tree part is " << s_tree/1024 << " Kbytes,\n" + << "with node->tagid part " << XML_Tree->Tags->size()/1024 << "Kbytes \n" + << "size of Tag part : " << XML_Tree->Tags->length () << " elements\n" + << "sizof(unsigned int)* " << XML_Tree->Tags->length () << " = " << + sizeof(unsigned int) * XML_Tree->Tags->length () / 1024 << " Kbytes\n" + << "Tag part is " << s_tags/1024 << " Kbytes,\n" + << "Text collection is " << s_text/1024 << " Kbytes \n"; return XML_Tree; } @@ -207,6 +292,9 @@ int XMLTree::SubtreeTags(treeNode x, TagType tag) fprintf(stderr, "Error: data structure has not been constructed properly\n"); exit(1); } + if (x == Root()) + x = first_child(Par,x); + int s = x + 2*subtree_size(Par, x) - 1; @@ -297,7 +385,6 @@ int XMLTree::Postorder(treeNode x) fprintf(stderr, "Error: data structure has not been constructed properly\n"); exit(1); } - return postorder_rank(Par, x); } @@ -308,7 +395,7 @@ TagType XMLTree::Tag(treeNode x) fprintf(stderr, "Error: data structure has not been constructed properly\n"); exit(1); } - + return Tags->access(node2tagpos(x)); } @@ -436,6 +523,9 @@ treeNode XMLTree::TaggedDesc(treeNode x, TagType tag) int r, s; treeNode y; + if (isleaf(Par,x)) + return NULLT; + r = (int) Tags->rank(tag, node2tagpos(x)); s = (int) Tags->select(tag, r+1); if (s == -1) return NULLT; // there is no such node @@ -444,6 +534,28 @@ treeNode XMLTree::TaggedDesc(treeNode x, TagType tag) else return y; } +// TaggedNext(x,tag): returns the first node tagged tag with larger preorder than x +// Returns NULLT if there is none. +treeNode XMLTree::TaggedNext(treeNode x, TagType tag) + { + if (!finished) { + fprintf(stderr, "Error: data structure has not been constructed properly\n"); + exit(1); + } + + int r, s; + treeNode y; + if (x==NULLT) + return NULLT; + + r = (int) Tags->rank(tag, node2tagpos(x)); + s = (int) Tags->select(tag, r+1); + if (s == -1) return NULLT; // there is no such node + y = tagpos2node(s); // transforms the tag position into a node position + return (y<=x ? NULLT : y); + } + + // TaggedPrec(x,tag): returns the first node tagged tag with smaller preorder than x and not an // ancestor of x. Returns NULLT if there is none. treeNode XMLTree::TaggedPrec(treeNode x, TagType tag) @@ -479,6 +591,9 @@ treeNode XMLTree::TaggedFoll(treeNode x, TagType tag) } int r, s; + if (x ==NULLT || x == Root()|| (next_sibling(Par,x) == -1 )) + return NULLT; + r = (int) Tags->rank(tag, node2tagpos(next_sibling(Par, x))-1); s = (int) Tags->select(tag, r+1); // select returns -1 in case that there is no r+1-th tag. if (s==-1) return NULLT; @@ -591,10 +706,16 @@ treeNode XMLTree::ParentNode(DocID d) fprintf(stderr, "Error: data structure has not been constructed properly\n"); exit(1); } - + + if (d == NULLT) + return NULLT; + int s; + // OJO : Kim : I added the d+1. before that, else branch was + // EBVector->select1(d) + // and gave wrong results (I'm really poking a bear with a stick here). if (indexing_empty_texts) s = d; - else s = EBVector->select1(d); + else s = EBVector->select1(d+1); if (inspect(Par,s) == CP) // is a closing parenthesis return parent(Par, find_open(Par, s)); @@ -619,48 +740,25 @@ int XMLTree::OpenDocument(bool empty_texts, int sample_rate_text,bool dtc) indexing_empty_texts = empty_texts; - par_aux = (pb *)malloc(sizeof(pb)*parArraySize); - if (!par_aux) { - fprintf(stderr, "Error: not enough memory\n"); - return NULLT; - } + par_aux = (pb *)umalloc(sizeof(pb)*parArraySize); - tags_aux = (TagType *) malloc(sizeof(TagType)); - if (!tags_aux) { - fprintf(stderr, "Error: not enough memory\n"); - return NULLT; - } + tags_aux = (TagType *) umalloc(sizeof(TagType)); - TagName = (unsigned char **) malloc(2*sizeof(unsigned char*)); - if (!TagName){ - fprintf(stderr, "Error: not enough memory\n"); - return NULLT; - } + TagName = (unsigned char **) umalloc(2*sizeof(unsigned char*)); - TagName[0] = (unsigned char *) malloc(4*sizeof(unsigned char)); - strcpy((char *) TagName[0], "<@>"); + TagName[0] = (unsigned char *) umalloc(4*sizeof(unsigned char)); - if (!TagName[0]){ - fprintf(stderr, "Error: not enough memory\n"); - return NULLT; - } + strcpy((char *) TagName[0], "<@>"); - TagName[1] = (unsigned char *) malloc(4*sizeof(unsigned char)); - if (!TagName[1]){ - fprintf(stderr, "Error: not enough memory\n"); - return NULLT; - } + TagName[1] = (unsigned char *) umalloc(4*sizeof(unsigned char)); strcpy((char *) TagName[1], "<$>"); - if (!indexing_empty_texts) { - empty_texts_aux = (unsigned int *)malloc(sizeof(unsigned int)); - if (!empty_texts_aux) { - fprintf(stderr, "Error: not enough memory\n"); - return NULLT; - } - } + if (!indexing_empty_texts) + empty_texts_aux = (unsigned int *)umalloc(sizeof(unsigned int)); + + Text = TextCollection::InitTextCollection((unsigned)sample_rate_text); @@ -679,14 +777,10 @@ int XMLTree::CloseDocument() } // closing parenthesis for the tree root - par_aux = (pb *)realloc(par_aux, sizeof(pb)*(1+npar/(8*sizeof(pb)))); - if (!par_aux) { - fprintf(stderr, "Error: not enough memory\n"); - return NULLT; - } + par_aux = (pb *)urealloc(par_aux, sizeof(pb)*(1+npar/(8*sizeof(pb)))); // creates the data structure for the tree topology - Par = (bp *)malloc(sizeof(bp)); + Par = (bp *)umalloc(sizeof(bp)); bp_construct(Par, npar, par_aux, OPT_DEGREE|0); // creates structure for tags static_bitsequence_builder * bmb = new static_bitsequence_builder_brw32(20); @@ -714,7 +808,9 @@ int XMLTree::CloseDocument() empty_texts_aux = NULL; } + // OJO was leaked before, found by valgrind free(tags_aux); + tags_aux = NULL; finished = true; @@ -737,15 +833,10 @@ int XMLTree::NewOpenTag(unsigned char *tagname) // inserts a new opening parentheses in the bit sequence if (sizeof(pb)*8*parArraySize == npar) { // no space left for the new parenthesis - par_aux = (pb *)realloc(par_aux, sizeof(pb)*2*parArraySize); + par_aux = (pb *)urealloc(par_aux, sizeof(pb)*2*parArraySize); parArraySize *= 2; } - if (!par_aux) { - fprintf(stderr, "Error: not enough memory\n"); - return NULLT; - } - setbit(par_aux,npar,OP); // marks a new opening parenthesis // transforms the tagname into a tag identifier. If the tag is new, we insert @@ -759,7 +850,7 @@ int XMLTree::NewOpenTag(unsigned char *tagname) found_attributes=true; if (i==ntagnames) { // the tag is a new one, then we insert it - TagName = (unsigned char **)realloc(TagName, sizeof(char *)*(ntagnames+1)); + TagName = (unsigned char **)urealloc(TagName, sizeof(char *)*(ntagnames+1)); if (!TagName) { fprintf(stderr, "Error: not enough memory\n"); @@ -767,19 +858,15 @@ int XMLTree::NewOpenTag(unsigned char *tagname) } ntagnames++; - TagName[i] = (unsigned char *)malloc(sizeof(unsigned char)*(strlen((const char *)tagname)+1)); + TagName[i] = (unsigned char *)umalloc(sizeof(unsigned char)*(strlen((const char *)tagname)+1)); strcpy((char *)TagName[i], (const char *)tagname); } - tags_aux = (TagType *) realloc(tags_aux, sizeof(TagType)*(npar + 1)); - if (!tags_aux) { - fprintf(stderr, "Error: not enough memory\n"); - return NULLT; - } + tags_aux = (TagType *) urealloc(tags_aux, sizeof(TagType)*(npar + 1)); tags_aux[npar] = i; // inserts the new tag id within the preorder sequence of tags npar++; - + return 1; } @@ -799,14 +886,10 @@ int XMLTree::NewClosingTag(unsigned char *tagname) // inserts a new closing parentheses in the bit sequence if (sizeof(pb)*8*parArraySize == npar) { // no space left for the new parenthesis - par_aux = (pb *)realloc(par_aux, sizeof(pb)*2*parArraySize); + par_aux = (pb *)urealloc(par_aux, sizeof(pb)*2*parArraySize); parArraySize *= 2; } - if (!par_aux) { - fprintf(stderr, "Error: not enough memory\n"); - return NULLT; - } setbit(par_aux,npar,CP); // marks a new closing opening parenthesis // transforms the tagname into a tag identifier. If the tag is new, we insert @@ -815,25 +898,16 @@ int XMLTree::NewClosingTag(unsigned char *tagname) if (strcmp((const char *)tagname,(const char *)TagName[i])==0) break; if (i==ntagnames) { // the tag is a new one, then we insert it - TagName = (unsigned char **)realloc(TagName, sizeof(char *)*(ntagnames+1)); + TagName = (unsigned char **)urealloc(TagName, sizeof(char *)*(ntagnames+1)); - if (!TagName) { - fprintf(stderr, "Error: not enough memory\n"); - return NULLT; - } ntagnames++; - TagName[i] = (unsigned char *)malloc(sizeof(char)*(strlen((const char *)tagname)+2)); + TagName[i] = (unsigned char *)umalloc(sizeof(char)*(strlen((const char *)tagname)+2)); TagName[i][0] = '/'; strcpy((char *)&(TagName[i][1]), (const char *)tagname); } - tags_aux = (TagType *)realloc(tags_aux, sizeof(TagType)*(npar + 1)); - - if (!tags_aux) { - fprintf(stderr, "Error: not enough memory\n"); - return NULLT; - } + tags_aux = (TagType *)urealloc(tags_aux, sizeof(TagType)*(npar + 1)); tags_aux[npar] = i; // inserts the new tag id within the preorder sequence of tags @@ -860,16 +934,13 @@ int XMLTree::NewText(unsigned char *s) }; if (!indexing_empty_texts) { - empty_texts_aux = (unsigned int *)realloc(empty_texts_aux, sizeof(pb)*(1+(npar-1)/(8*sizeof(pb)))); - if (!empty_texts_aux) { - fprintf(stderr, "Error: not enough memory\n"); - return NULLT; - } - - bitset(empty_texts_aux, npar-1); // marks the non-empty text with a 1 in the bit vector + empty_texts_aux = (unsigned int *)urealloc(empty_texts_aux, sizeof(pb)*(1+(npar-1)/(8*sizeof(pb)))); + bitset(empty_texts_aux, npar-1); // marks the non-empty text with a 1 in the bit vector } Text->InsertText(s); + string cpps = (char*) s; + CachedText.push_back(cpps); return 1; // success } @@ -888,11 +959,7 @@ int XMLTree::NewEmptyText() } if (!indexing_empty_texts) { - empty_texts_aux = (unsigned int *)realloc(empty_texts_aux, sizeof(pb)*(1+(npar-1)/(8*sizeof(pb)))); - if (!empty_texts_aux) { - fprintf(stderr, "Error: not enough memory\n"); - return NULLT; - } + empty_texts_aux = (unsigned int *)urealloc(empty_texts_aux, sizeof(pb)*(1+(npar-1)/(8*sizeof(pb)))); bitclean(empty_texts_aux, npar-1); // marks the empty text with a 0 in the bit vector } @@ -922,23 +989,33 @@ unsigned char *XMLTree::GetTagName(TagType tagid) unsigned char *s; if (tagid >= ntagnames) return NULL; // invalid tag identifier - s = (unsigned char *)malloc((strlen((const char *)TagName[tagid])+1)*sizeof(unsigned char)); + s = (unsigned char *)umalloc((strlen((const char *)TagName[tagid])+1)*sizeof(unsigned char)); strcpy((char *)s, (const char *)TagName[tagid]); return s; } +//KIM : OJO need the two following methods + +const unsigned char *XMLTree::GetTagNameByRef(TagType tagid) + { + if (tagid >= ntagnames) return NULL; // invalid tag identifier + return ((const unsigned char*) TagName[tagid]); + } + + + TagType XMLTree::RegisterTag(unsigned char *tagname) { if (!finished) return NULLT; - TagType id = XMLTree::GetTagId(tagname); if (id == NULLT){ id = ntagnames; ntagnames = ntagnames + 1; - TagName = (unsigned char **) realloc(TagName,ntagnames*(sizeof(unsigned char*))); + TagName = (unsigned char **) urealloc(TagName,ntagnames*(sizeof(unsigned char*))); + TagName[id] = (unsigned char *) umalloc(sizeof(unsigned char)*strlen( (const char*) tagname)+1); strcpy((char*)TagName[id], (const char *)tagname); };