X-Git-Url: http://git.nguyen.vg/gitweb/?a=blobdiff_plain;f=XMLTree.cpp;h=dfda61afa9467dd8049037bfef8866eec4ee82d1;hb=fadbf5f42541aa09769cbe406f5756fa042fe2d9;hp=22ba4bb9a9e993dd7cdb4c48fbdb9987acd9f75a;hpb=184fd5131d257a334c29b0e55b1240fb29dc796b;p=SXSI%2FXMLTree.git diff --git a/XMLTree.cpp b/XMLTree.cpp index 22ba4bb..dfda61a 100644 --- a/XMLTree.cpp +++ b/XMLTree.cpp @@ -1,5 +1,65 @@ -#include "XMLTree.h" +#include "basics.h" #include +#include +#include "XMLTree.h" +#include +#include +#include +#include + +static double tLoading = 0; + +static unsigned int cLoading = 0; +static struct timeval tmpv1; +static struct timeval tmpv2; +static string mem1; +static string mem2; + +void read_procmem(string& memstr) { + std::string buf; + pid_t pid = getpid(); + std::stringstream path; + path << "/proc/" << pid << "/status"; + std::ifstream infile; + infile.open (path.str().c_str(), std::ifstream::in); + while (infile.good()){ + getline(infile,buf); + if (infile.eof()) { + memstr = "Could not read memory"; + return; + }; + int idx = buf.find("VmRSS"); + if (idx == 0){ + memstr = buf; + return; + }; + }; + memstr = "Could not read memory"; + return; + +} + +#define STARTTIMER() do { \ + gettimeofday(&tmpv1,NULL); \ + read_procmem(mem1); \ + } while (0) \ + +#define STOPTIMER(x) do { \ + read_procmem(mem2); \ + gettimeofday(&tmpv2,NULL); \ + (t##x) = ((tmpv2.tv_sec - tmpv1.tv_sec) * 1000000.0 + \ + (tmpv2.tv_usec - tmpv1.tv_usec))/1000.0; \ + (c##x)= (c##x)+1; \ + } while (0) + +#define PRINTTIME(s,x) do { \ + std::cerr << (s) << " : " << (t##x) << "ms" << std::endl; \ + std::cerr << "Mem use before: " << mem1 << std::endl; \ + std::cerr << "Mem use after: " << mem2 << std::endl; \ + std::cerr.flush(); \ + } while (0) \ + + // 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. @@ -7,169 +67,254 @@ // the tree, and storing 2 tags per tree node (opening and closing tags). // tag position -> tree node -inline treeNode tagpos2node(int t) { - return (treeNode)t; -} +inline treeNode tagpos2node(int t) + { + return (treeNode) t; + } // tree node -> tag position -inline int node2tagpos(treeNode x) { - return (int)x; +inline int node2tagpos(treeNode x) +{ + return (int)x; } -// Save: saves XML tree data structure to file. -void XMLTree::Save(unsigned char *filename) +// returns NULLT if the test is true +#define NULLT_IF(x) do { if (x) return NULLT; } while (0) + + +XMLTree::XMLTree( pb * const par, uint npar, vector * const TN, TagIdMap * const tim, uint *empty_texts_bmp, TagType *tags, + TextCollection * const TC, bool dis_tc) + { + // creates the data structure for the tree topology + Par = (bp *)umalloc(sizeof(bp)); + bp_construct(Par, npar, (pb*) par, OPT_DEGREE|0); + + // creates structure for tags - FILE *fp; - char filenameaux[1024]; + TagName = (vector*)TN; + tIdMap = (TagIdMap *) tim; + + uint max_tag = TN->size() - 1; + + static_bitsequence_builder *bmb = new static_bitsequence_builder_sdarray(); + alphabet_mapper *am = new alphabet_mapper_none(); + Tags = new static_sequence_bs((uint*)tags,npar,am,bmb); + + cout << "Tags test: " << Tags->test((uint*)tags,npar) << endl; + + tags_blen = bits(max_tag); + tags_len = (uint)npar; + tags_fix = new uint[uint_len(tags_blen,tags_len)]; + for(uint i=0;i<(uint)npar;i++) + set_field(tags_fix,tags_blen,i,tags[i]); + + delete bmb; + free(tags); + tags = NULL; + + Text = (TextCollection*) TC; + + + EBVector = new static_bitsequence_rrr02(empty_texts_bmp,npar,32); + free(empty_texts_bmp); + empty_texts_bmp = NULL; + + + disable_tc = dis_tc; + } + + +// ~XMLTree: frees memory of XML tree. +XMLTree::~XMLTree() + { int i; + + destroyTree(Par); + free(Par); // frees the memory of struct Par - sprintf(filenameaux, "%s.srx", filename); - fp = fopen(filenameaux, "w"); - if (fp == NULL) { - printf("Error: cannot create file %s to store the tree structure of XML collection\n", filenameaux); - exit(1); - } + delete tIdMap; + tIdMap = NULL; + delete TagName; + TagName = NULL; + + delete Tags; + Tags = NULL; + + delete Text; + Text = NULL; + + delete EBVector; + EBVector = NULL; + + + } + + +void XMLTree::print_stats() + { + uint total_space = Tags->size()+sizeof(static_sequence*); + total_space += sizeof(uint*)+sizeof(uint)*(2+uint_len(tags_blen,tags_len)); + cout << "Space usage for XMLTree:" << endl + << " - tags static_sequence: " << Tags->size()+sizeof(static_sequence*) << endl + << " - tags access array: " << sizeof(uint*)+sizeof(uint)*(2+uint_len(tags_blen,tags_len)) << endl + << " ... add Diego structures ... " << endl + << " *total* " << total_space << endl; + } + +// Save: saves XML tree data structure to file. +void XMLTree::Save(int fd) + { + FILE *fp; + char filenameaux[1024]; + int i; + + fp = fdopen(fd, "wa"); // first stores the tree topology saveTree(Par, fp); - + // stores the table with tag names - fwrite(&ntagnames, sizeof(int), 1, fp); - for (i=0; isave(fp); + int ntags = TagName->size(); + + ufwrite(&ntags, sizeof(int), 1, fp); + for (i = 0; iat(i).c_str()); + // stores the tags Tags->save(fp); + ufwrite(&tags_blen,sizeof(uint),1,fp); + ufwrite(&tags_len,sizeof(uint),1,fp); + ufwrite(tags_fix,sizeof(uint),uint_len(tags_blen,tags_len),fp); + // flags + ufwrite(&disable_tc, sizeof(bool),1,fp); + + //text positions + EBVector->save(fp); + // stores the texts - Text->Save(fp); + if (!disable_tc) { + Text->Save(fp); + }; - fclose(fp); } // Load: loads XML tree data structure from file. Returns // a pointer to the loaded data structure -XMLTree *XMLTree::Load(unsigned char *filename, int sample_rate_text) +XMLTree *XMLTree::Load(int fd) { - FILE *fp; - char filenameaux[1024]; + char buffer[1024]; XMLTree *XML_Tree; int i; - - // first load the tree topology - sprintf(filenameaux, "%s.srx", filename); - fp = fopen(filenameaux, "r"); - if (fp == NULL) { - printf("Error: cannot open file %s to load the tree structure of XML collection\n", filenameaux); - exit(1); - } - XML_Tree = new XMLTree(); - XML_Tree->Par = (bp *)malloc(sizeof(bp)); - loadTree(XML_Tree->Par, fp); - - // stores the table with tag names - fread(&XML_Tree->ntagnames, sizeof(int), 1, fp); + fp = fdopen(fd, "r"); - XML_Tree->TagName = (unsigned char **)malloc(XML_Tree->ntagnames*sizeof(unsigned char *)); + XML_Tree = new XMLTree(); + STARTTIMER(); + // Load the tree structure + XML_Tree->Par = (bp *)umalloc(sizeof(bp)); - for (i=0; intagnames;i++) { - int k = feof(fp); - fscanf(fp, "%s\n",filenameaux); - XML_Tree->TagName[i] = (unsigned char *)malloc(sizeof(unsigned char)*(strlen((const char *)filenameaux)+1)); - strcpy((char *)XML_Tree->TagName[i], (const char *)filenameaux); - } - - // loads the flags - fread(&(XML_Tree->indexing_empty_texts), sizeof(bool), 1, fp); - fread(&(XML_Tree->initialized), sizeof(bool), 1, fp); - fread(&(XML_Tree->finished), sizeof(bool), 1, fp); + loadTree(XML_Tree->Par, fp); + STOPTIMER(Loading); + PRINTTIME("Loading parenthesis struct", Loading); + STARTTIMER(); + + XML_Tree->TagName = new vector(); + XML_Tree->tIdMap = new std::unordered_map(); - if (!(XML_Tree->indexing_empty_texts)) XML_Tree->EBVector = static_bitsequence_rrr02::load(fp); + string s; + int ntags; + + // Load the tag names + ufread(&ntags, sizeof(int), 1, fp); + + for (i=0; iTagName->push_back(s); + XML_Tree->tIdMap->insert(std::make_pair(s,i)); + + }; + STOPTIMER(Loading); + PRINTTIME("Loading tag names struct", Loading); + STARTTIMER(); - // loads the tags + // loads the tag structure XML_Tree->Tags = static_sequence::load(fp); + ufread(&XML_Tree->tags_blen,sizeof(uint),1,fp); + ufread(&XML_Tree->tags_len,sizeof(uint),1,fp); + XML_Tree->tags_fix = new uint[uint_len(XML_Tree->tags_blen,XML_Tree->tags_len)]; + ufread(XML_Tree->tags_fix,sizeof(uint),uint_len(XML_Tree->tags_blen,XML_Tree->tags_len),fp); + + // TODO ask francisco about this + /// FIXME:UGLY tests! + uint * seq = new uint[XML_Tree->tags_len]; + for(uint i=0;itags_len;i++) + seq[i] = get_field(XML_Tree->tags_fix,XML_Tree->tags_blen,i); + //cout << "Tags test: " << XML_Tree->Tags->test(seq,XML_Tree->tags_len) << endl; + XML_Tree->Tags->test(seq,XML_Tree->tags_len); + delete [] seq; + /// End ugly tests + + STOPTIMER(Loading); + PRINTTIME("Loading tag struct", Loading); + STARTTIMER(); - // loads the texts - XML_Tree->Text->Load(fp,sample_rate_text); - - fclose(fp); + // loads the flags - return XML_Tree; - } + ufread(&(XML_Tree->disable_tc), sizeof(bool), 1, fp); + XML_Tree->EBVector = static_bitsequence_rrr02::load(fp); -// ~XMLTree: frees memory of XML tree. -XMLTree::~XMLTree() - { - int i; - destroyTree(Par); - free(Par); // frees the memory of struct Par - - for (i=0; i~static_bitsequence_rrr02(); - delete EBVector; - EBVector = NULL; + // Not used + int sample_rate_text = 64; + // loads the texts + if (!XML_Tree->disable_tc){ + XML_Tree->Text = TextCollection::Load(fp,sample_rate_text); } + else XML_Tree->Text = NULL; + STOPTIMER(Loading); + PRINTTIME("Loading TextCollection", Loading); + STARTTIMER(); - //Tags->~static_sequence_wvtree(); - delete Tags; - Tags = NULL; - - //Text->~TextCollection(); - delete Text; - Text = NULL; - - initialized = false; - finished = false; + return XML_Tree; } + // root(): returns the tree root. -treeNode XMLTree::Root() +inline treeNode XMLTree::Root() { - if (!finished) { - fprintf(stderr, "Error: data structure has not been constructed properly\n"); - exit(1); - } - return root_node(Par); + return 0; //root_node(Par); } // SubtreeSize(x): the number of nodes (and attributes) in the subtree of node x. int XMLTree::SubtreeSize(treeNode x) { - if (!finished) { - fprintf(stderr, "Error: data structure has not been constructed properly\n"); - exit(1); - } return subtree_size(Par, x); } // SubtreeTags(x,tag): the number of occurrences of tag within the subtree of node x. int XMLTree::SubtreeTags(treeNode x, TagType tag) { - if (!finished) { - 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; @@ -186,57 +331,39 @@ bool XMLTree::IsLeaf(treeNode x) // IsAncestor(x,y): returns whether node x is ancestor of node y. bool XMLTree::IsAncestor(treeNode x, treeNode y) { - if (!finished) { - fprintf(stderr, "Error: data structure has not been constructed properly\n"); - exit(1); - } - return is_ancestor(Par, x, y); } // IsChild(x,y): returns whether node x is parent of node y. bool XMLTree::IsChild(treeNode x, treeNode y) { - if (!finished) { - fprintf(stderr, "Error: data structure has not been constructed properly\n"); - exit(1); - } - if (!is_ancestor(Par, x, y)) return false; return depth(Par, x) == (depth(Par, y) + 1); } +// IsFirstChild(x): returns whether node x is the first child of its parent. +bool XMLTree::IsFirstChild(treeNode x) + { + return ((x != NULLT)&&(x==Root() || prev_sibling(Par,x) == (treeNode)-1)); + } + + // NumChildren(x): number of children of node x. Constant time with the data structure // of Sadakane. int XMLTree::NumChildren(treeNode x) { - if (!finished) { - fprintf(stderr, "Error: data structure has not been constructed properly\n"); - exit(1); - } - return degree(Par, x); } // ChildNumber(x): returns i if node x is the i-th children of its parent. int XMLTree::ChildNumber(treeNode x) { - if (!finished) { - fprintf(stderr, "Error: data structure has not been constructed properly\n"); - exit(1); - } - return child_rank(Par, x); } // Depth(x): depth of node x, a simple binary rank on the parentheses sequence. int XMLTree::Depth(treeNode x) { - if (!finished) { - fprintf(stderr, "Error: data structure has not been constructed properly\n"); - exit(1); - } - return depth(Par, x); } @@ -244,11 +371,6 @@ int XMLTree::Depth(treeNode x) // nodes (i.e., tags, it disregards the texts in the tree). int XMLTree::Preorder(treeNode x) { - if (!finished) { - fprintf(stderr, "Error: data structure has not been constructed properly\n"); - exit(1); - } - return preorder_rank(Par, x); } @@ -256,61 +378,44 @@ int XMLTree::Preorder(treeNode x) // nodes (i.e., tags, it disregards the texts in the tree). int XMLTree::Postorder(treeNode x) { - if (!finished) { - fprintf(stderr, "Error: data structure has not been constructed properly\n"); - exit(1); - } - return postorder_rank(Par, x); } // Tag(x): returns the tag identifier of node x. TagType XMLTree::Tag(treeNode x) { - if (!finished) { - fprintf(stderr, "Error: data structure has not been constructed properly\n"); - exit(1); - } - - return Tags->access(node2tagpos(x)); + return get_field(tags_fix,tags_blen,node2tagpos(x)); } // DocIds(x): returns the range of text identifiers that descend from node x. // returns {NULLT, NULLT} when there are no texts descending from x. range XMLTree::DocIds(treeNode x) { - if (!finished) { - fprintf(stderr, "Error: data structure has not been constructed properly\n"); - exit(1); - } - range r; - if (indexing_empty_texts) { // faster, no rank needed - r.min = x; - r.max = x+2*subtree_size(Par, x)-2; + if (x == NULLT) { + r.min = NULLT; + r.max = NULLT; + return r; + }; + + + int min = EBVector->rank1(x-1); + int max = EBVector->rank1(x+2*subtree_size(Par, x)-2); + if (min==max) { // range is empty, no texts within the subtree of x + r.min = NULLT; + r.max = NULLT; } - else { // we are not indexing empty texts, we need rank - int min = EBVector->rank1(x-1); - int max = EBVector->rank1(x+2*subtree_size(Par, x)-2); - if (min==max) { // range is empty, no texts within the subtree of x - r.min = NULLT; - r.max = NULLT; - } - else { // the range is non-empty, there are texts within the subtree of x - r.min = min+1; - r.max = max; - } + else { // the range is non-empty, there are texts within the subtree of x + r.min = min+1; + r.max = max; } return r; + } // Parent(x): returns the parent node of node x. treeNode XMLTree::Parent(treeNode x) { - if (!finished) { - fprintf(stderr, "Error: data structure has not been constructed properly\n"); - exit(1); - } if (x == Root()) return NULLT; else @@ -320,102 +425,153 @@ treeNode XMLTree::Parent(treeNode x) // Child(x,i): returns the i-th child of node x, assuming it exists. treeNode XMLTree::Child(treeNode x, int i) { - if (!finished) { - fprintf(stderr, "Error: data structure has not been constructed properly\n"); - exit(1); - } - if (i <= OPTD) return naive_child(Par, x, i); else return child(Par, x, i); - } +} // FirstChild(x): returns the first child of node x, assuming it exists. Very fast in BP. treeNode XMLTree::FirstChild(treeNode x) { - if (!finished) { - fprintf(stderr, "Error: data structure has not been constructed properly\n"); - exit(1); - } + NULLT_IF(x==NULLT); + return first_child(Par, x); + } + +treeNode XMLTree::FirstElement(treeNode x) + { + NULLT_IF(x==NULLT); + treeNode fc = first_child(Par, x); + //<$> is 2 + return ((fc == NULLT || Tag(fc) != PCDATA_TAG_ID) ? fc : next_sibling(Par,fc)); - return first_child(Par, x); } +treeNode XMLTree::NextElement(treeNode x) +{ + NULLT_IF(x==NULLT); + treeNode ns = next_sibling(Par, x); + return ((ns == NULLT || Tag(ns) != PCDATA_TAG_ID) ? ns : next_sibling(Par,ns)); +} + +// LastChild(x): returns the last child of node x. +treeNode XMLTree::LastChild(treeNode x) + { + NULLT_IF(x==NULLT || x == Root() || isleaf(Par,x)); + return find_open(Par, find_close(Par, x)-1); + } + + // NextSibling(x): returns the next sibling of node x, assuming it exists. treeNode XMLTree::NextSibling(treeNode x) { - if (!finished) { - fprintf(stderr, "Error: data structure has not been constructed properly\n"); - exit(1); - } - if (x == Root()) - return NULLT; - - return next_sibling(Par, x); + NULLT_IF(x==NULLT || x == Root() ); + return next_sibling(Par, x); } // PrevSibling(x): returns the previous sibling of node x, assuming it exists. treeNode XMLTree::PrevSibling(treeNode x) { - if (!finished) { - fprintf(stderr, "Error: data structure has not been constructed properly\n"); - exit(1); - } - - return prev_sibling(Par, x); + NULLT_IF(x==NULLT || x == Root()); + return prev_sibling(Par, x); } -// TaggedChild(x,i,tag): returns the i-th child of node x tagged tag, or NULLT if there is none. +// TaggedChild(x,tag): returns the first child of node x tagged tag, or NULLT if there is none. // Because of the balanced-parentheses representation of the tree, this operation is not supported // efficiently, just iterating among the children of node x until finding the desired child. -treeNode XMLTree::TaggedChild(treeNode x, int i, TagType tag) +treeNode XMLTree::TaggedChild(treeNode x, TagType tag) { - if (!finished) { - fprintf(stderr, "Error: data structure has not been constructed properly\n"); - exit(1); - } - - treeNode child; - child = first_child(Par, x); // starts at first child of node x - if (child==(treeNode)-1) return NULLT; // node x is a leaf, there is no such child - while (child!=(treeNode)-1) { - if (Tags->access(node2tagpos(child)) == tag) { // current child is labeled with tag of interest - i--; - if (i==0) return child; // we have seen i children of x tagged tag, this is the one we are looking for - } - child = next_sibling(Par, x); // OK, let's try with the next child - } - return NULLT; // no such child was found + NULLT_IF(x==NULLT || isleaf(Par,x)); + + treeNode child; + child = first_child(Par, x); // starts at first child of node x + if (get_field(tags_fix,tags_blen,node2tagpos(child)) == tag) + return child; + else + return TaggedFollSibling(child,tag); } +// TaggedSibling(x,tag): returns the first sibling of node x tagged tag, or NULLT if there is none. +treeNode XMLTree::TaggedFollSibling(treeNode x, TagType tag) +{ + NULLT_IF(x==NULLT); + treeNode sibling = next_sibling(Par, x); + while (sibling != NULLT) { + if (get_field(tags_fix,tags_blen,node2tagpos(sibling)) == tag) // current sibling is labeled with tag of interest + return sibling; + sibling = next_sibling(Par, sibling); // OK, let's try with the next sibling + } + return NULLT; // no such sibling was found +} + +treeNode XMLTree::SelectChild(treeNode x, std::unordered_set *tags) +{ + + NULLT_IF(x==NULLT || isleaf(Par,x)); + int i; + treeNode child = first_child(Par, x); + TagType t = get_field(tags_fix, tags_blen, node2tagpos(child)); + std::unordered_set::const_iterator tagit = tags->find(t); + if (tagit != tags->end()) return child; + return SelectFollSibling(child,tags); +} + + +treeNode XMLTree::SelectFollSibling(treeNode x, std::unordered_set *tags) +{ + + NULLT_IF(x==NULLT); + int i; + TagType t; + treeNode sibling = next_sibling(Par, x); + std::unordered_set::const_iterator tagit; + while (sibling != NULLT) { + t = get_field(tags_fix, tags_blen, node2tagpos(sibling)); + tagit = tags->find(t); + if (tagit != tags->end()) return sibling; + sibling = next_sibling(Par, sibling); + } + return NULLT; + } + + // TaggedDesc(x,tag): returns the first node tagged tag with larger preorder than x and within // the subtree of x. Returns NULLT if there is none. treeNode XMLTree::TaggedDesc(treeNode x, TagType tag) { - if (!finished) { - fprintf(stderr, "Error: data structure has not been constructed properly\n"); - exit(1); - } + NULLT_IF(x==NULLT || isleaf(Par,x)); - int r, s; - treeNode y; - 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 - if (!is_ancestor(Par, x, y)) return NULLT; // the next node tagged tag (in preorder) is not within the subtree of x. - else return y; + int s = (int) Tags->select_next(tag,node2tagpos(x)); + NULLT_IF (s == -1); + + treeNode y = tagpos2node(s); // transforms the tag position into a node position + + return (is_ancestor(Par,x,y) ? y : NULLT); + } + + +treeNode XMLTree::SelectDesc(treeNode x, std::unordered_set *tags) + { + NULLT_IF (x ==NULLT || isleaf(Par,x)); + int i; + treeNode min = NULLT; + treeNode fc = first_child(Par,x); + treeNode aux; + std::unordered_set::const_iterator tagit; + for (tagit = tags->begin(); tagit != tags->end(); tagit++) { + aux = TaggedDesc(x, (TagType) *tagit); + if (aux == fc) return fc; + if (aux == NULLT) continue; + if ((min == NULLT) || (aux < min)) min = aux; + }; + return min; } + + // 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) - { - if (!finished) { - fprintf(stderr, "Error: data structure has not been constructed properly\n"); - exit(1); - } - + { int r, s; treeNode node_s, root; r = (int)Tags->rank(tag, node2tagpos(x)-1); @@ -432,100 +588,109 @@ treeNode XMLTree::TaggedPrec(treeNode x, TagType tag) return NULLT; // there is no such node } + // TaggedFoll(x,tag): returns the first node tagged tag with larger preorder than x and not in // the subtree of x. Returns NULLT if there is none. -treeNode XMLTree::TaggedFoll(treeNode x, TagType tag) +treeNode XMLTree::TaggedFoll(treeNode x, TagType tag) { - if (!finished) { - fprintf(stderr, "Error: data structure has not been constructed properly\n"); - exit(1); - } + NULLT_IF (x ==NULLT || x == Root()); + + return tagpos2node(Tags->select_next(tag,find_close(Par, x))); - int r, s; - 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; - else return tagpos2node(s); - } + } + +// TaggedFollBelow(x,tag,root): returns the first node tagged tag with larger preorder than x +// and not in the subtree of x. Returns NULLT if there is none. +treeNode XMLTree::TaggedFollBelow(treeNode x, TagType tag, treeNode root) +{ + + NULLT_IF (x == NULLT || x == Root()); + + treeNode s = tagpos2node(Tags->select_next(tag, find_close(Par, x))); + + if (root == Root()) return s; + NULLT_IF (s == NULLT || s >= find_close(Par, root)); + + return s; +} -// PrevText(x): returns the document identifier of the text to the left -// of node x, or NULLT if x is the root node or the text is empty. -// Assumes Doc ids start from 0. -DocID XMLTree::PrevText(treeNode x) +/* Here we inline TaggedFoll to find the min globally, and only at the end + we check if the min is below the context node */ +treeNode XMLTree::SelectFollBelow(treeNode x, std::unordered_set *tags, treeNode root) { - if (!finished) { - fprintf(stderr, "Error: data structure has not been constructed properly\n"); - exit(1); - } - if (x == Root()) return NULLT; - if (indexing_empty_texts) // faster, no rank needed - return (DocID)x-1; - else { // we are not indexing empty texts, rank is needed - if (EBVector->access(x-1) == 0) - return (DocID)NULLT; // there is no text to the left of node (text is empty) - else - return (DocID)EBVector->rank1(x-1)-1; //-1 because document ids start from 0 - } + NULLT_IF(x==NULLT || x==Root()); + int i; + treeNode min = NULLT; + treeNode ns = next_sibling(Par, x); + treeNode aux; + std::unordered_set::const_iterator tagit; + for (tagit = tags->begin(); tagit != tags->end(); tagit++) { + + aux = tagpos2node(Tags->select_next(*tagit, find_close(Par, x))); + + // The next sibling of x is guaranteed to be below ctx + // and is the node with lowest preorder which is after ctx. + // if we find it, we return early; + + if (aux == ns ) return ns; + if (aux == NULLT) continue; + if ((min == NULLT) || (aux < min)) min = aux; + }; + + // found the smallest node in preorder which is after x. + // if ctx is the root node, just return what we found. + + if (root == Root()) return min; + // else check whether if is in below the ctx node + + NULLT_IF (min == NULLT || min >= find_close(Par, root)); + + return min; + } -// NextText(x): returns the document identifier of the text to the right -// of node x, or NULLT if x is the root node. Assumes Doc ids start from 0. -DocID XMLTree::NextText(treeNode x) - { - if (!finished) { - fprintf(stderr, "Error: data structure has not been constructed properly\n"); - exit(1); - } - if (x == Root()) return NULLT; - if (indexing_empty_texts) // faster, no rank needed - return (DocID)x+2*subtree_size(Par, x)-1; - else { // we are not indexing empty texts, rank is needed - int p = x+2*subtree_size(Par, x)-1; - if (EBVector->access(p) == 0) // there is no text to the right of node - return (DocID)NULLT; - else - return (DocID)EBVector->rank1(p)-1; //-1 because document ids start from 0 +// TaggedAncestor(x, tag): returns the closest ancestor of x tagged tag. Return +// NULLT is there is none. +treeNode XMLTree::TaggedAncestor(treeNode x, TagType tag) + { + if (x == NULLT || x == Root()) + return NULLT; + + treeNode s = parent(Par, x), r = Root(); + while (s != r) { + if (get_field(tags_fix,tags_blen,node2tagpos(s)) /*Tags->access(node2tagpos(s))*/ == tag) return s; + s = parent(Par, s); } + return NULLT; } + + // MyText(x): returns the document identifier of the text below node x, // or NULLT if x is not a leaf node or the text is empty. Assumes Doc // ids start from 0. DocID XMLTree::MyText(treeNode x) - { - if (!finished) { - fprintf(stderr, "Error: data structure has not been constructed properly\n"); - exit(1); - } +{ + TagType tag = Tag(x); + // seems faster than testing EBVector->access(x); - if (!IsLeaf(x)) return NULLT; - if (indexing_empty_texts) // faster, no rank needed - return (DocID)x; - else { // we are not indexing empty texts, rank is needed - if (EBVector->access(x) == 0) // there is no text below node x - return (DocID)NULLT; - else - return (DocID)EBVector->rank1(x)-1; //-1 because document ids start from 0 - } - } + if (tag == PCDATA_TAG_ID || tag == ATTRIBUTE_DATA_TAG_ID) + return (DocID) (EBVector->rank1(x)-1); //-1 because document ids start from 0 + else + return (DocID) NULLT; + +} // TextXMLId(d): returns the preorder of document with identifier d in the tree consisting of // all tree nodes and all text nodes. Assumes that the tree root has preorder 1. int XMLTree::TextXMLId(DocID d) { - if (!finished) { - fprintf(stderr, "Error: data structure has not been constructed properly\n"); - exit(1); - } - - if (indexing_empty_texts) - return d + rank_open(Par, d)+1; // +1 because root has preorder 1 - else { // slower, needs rank and select - int s = EBVector->select1(d+1); - return rank_open(Par, s) + d + 1; // +1 because root has preorder 1 - } + NULLT_IF(d == NULLT); + int s = EBVector->select1(d+1); + return rank_open(Par, s) + d + 1; // +1 because root has preorder 1 + } // NodeXMLId(x): returns the preorder of node x in the tree consisting @@ -533,332 +698,66 @@ int XMLTree::TextXMLId(DocID d) // preorder 0; int XMLTree::NodeXMLId(treeNode x) { - if (!finished) { - fprintf(stderr, "Error: data structure has not been constructed properly\n"); - exit(1); - } - - if (indexing_empty_texts) - return x - 1 + rank_open(Par, x); - else { - if (x == Root()) return 1; // root node has preorder 1 - else - return rank_open(Par, x) + EBVector->rank1(x-1); - } + NULLT_IF(x == NULLT); + if (x == Root()) return 1; // root node has preorder 1 + return rank_open(Par, x) + EBVector->rank1(x-1); } // ParentNode(d): returns the parent node of document identifier d. treeNode XMLTree::ParentNode(DocID d) - { - if (!finished) { - fprintf(stderr, "Error: data structure has not been constructed properly\n"); - exit(1); - } - - int s; - if (indexing_empty_texts) s = d; - else s = EBVector->select1(d); - - if (inspect(Par,s) == CP) // is a closing parenthesis - return parent(Par, find_open(Par, s)); - else // is an opening parenthesis - return (treeNode)s; - + { + NULLT_IF (d == NULLT); + return (treeNode) EBVector->select1(d+1); } - -// OpenDocument(empty_texts): it starts the construction of the data structure for -// the XML document. Parameter empty_texts indicates whether we index empty texts -// in document or not. Returns a non-zero value upon success, NULLT in case of error. -int XMLTree::OpenDocument(bool empty_texts, int sample_rate_text) - { - initialized = true; - finished = false; - npar = 0; - parArraySize = 1; - ntagnames = 0; - - indexing_empty_texts = empty_texts; - - par_aux = (pb *)malloc(sizeof(pb)*parArraySize); - if (!par_aux) { - fprintf(stderr, "Error: not enough memory\n"); - return NULLT; - } - - tags_aux = (TagType *) malloc(sizeof(TagType)); - if (!tags_aux) { - fprintf(stderr, "Error: not enough memory\n"); - return NULLT; - } - - TagName = NULL; - - 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; - } - } - - Text = TextCollection::InitTextCollection((unsigned)sample_rate_text); - - return 1; // indicates success in the initialization of the data structure - } - -// CloseDocument(): it finishes the construction of the data structure for the XML -// document. Tree and tags are represented in the final form, dynamic data -// structures are made static, and the flag "finished" is set to true. After that, -// the data structure can be queried. -int XMLTree::CloseDocument() +// GetTagId: returns the tag identifier corresponding to a given tag name. +// Returns NULLT in case that the tag name does not exists. +TagType XMLTree::GetTagId(unsigned char *tagname) { - if (!initialized) { // data structure has not been initialized properly - fprintf(stderr, "Error: data structure has not been initialized properly (by calling method OpenDocument)\n"); - return NULLT; - } - - // 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; - } - - // creates the data structure for the tree topology - Par = (bp *)malloc(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); - static_permutation_builder * pmb = new static_permutation_builder_mrrr(PERM_SAMPLE, bmb); - static_sequence_builder * ssb = new static_sequence_builder_gmr_chunk(bmb, pmb); - - Tags = new static_sequence_gmr((uint *) tags_aux, (uint) npar-1,2*ntagnames, bmb, ssb); - - delete bmb; - delete pmb; - delete ssb; - // makes the text collection static - Text->MakeStatic(); + + string s = (char *) tagname; + TagIdMapIT it = tIdMap->find(s); + return (TagType) ((it != tIdMap->end()) ? it->second : -1); - // creates the data structure marking the non-empty texts (just in the case it is necessary) - if (!indexing_empty_texts) - EBVector = new static_bitsequence_rrr02((uint *)empty_texts_aux,(ulong)npar,(uint)32); - - finished = true; - - return 1; // indicates success in the inicialization } -// NewOpenTag(tagname): indicates the event of finding a new opening tag in the document. -// Tag name is given. Returns a non-zero value upon success, and returns NULLT -// in case of failing when trying to insert the new tag. -int XMLTree::NewOpenTag(unsigned char *tagname) +// GetTagName(tagid): returns the tag name of a given tag identifier. +// Returns NULL in case that the tag identifier is not valid. +unsigned char *XMLTree::GetTagName(TagType tagid) { - int i; - - if (!initialized) { // data structure has not been initialized properly - fprintf(stderr, "Error: you cannot insert a new opening tag without first calling method OpenDocument first\n"); - return NULLT; - } - - // 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); - 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 - // it in the table. - for (i=0; i= TagName->size()) + return (unsigned char *) ""; + strcpy((char *)s, TagName->at(tagid).c_str()); + return (s == NULL ? (unsigned char*) "" : s); } -// NewClosingTag(tagname): indicates the event of finding a new closing tag in the document. -// Tag name is given. Returns a non-zero value upon success, and returns NULLT -// in case of failing when trying to insert the new tag. -int XMLTree::NewClosingTag(unsigned char *tagname) +const unsigned char *XMLTree::GetTagNameByRef(TagType tagid) { - int i; - - if (!initialized) { // data structure has not been initialized properly - fprintf(stderr, "Error: you cannot insert a new closing tag without first calling method OpenDocument first\n"); - return NULLT; - } - - // 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); - 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 - // it in the table. - for (i=0; i= TagName->size()) + return (unsigned char *) ""; + + return (const unsigned char *) TagName->at(tagid).c_str(); + } -// NewText(s): indicates the event of finding a new (non-empty) text s in the document. -// The new text is inserted within the text collection. Returns a non-zero value upon -// success, NULLT in case of error. -int XMLTree::NewText(unsigned char *s) - { - if (!initialized) { // data structure has not been initialized properly - fprintf(stderr, "Error: you cannot insert a new text without first calling method OpenDocument first\n"); - return NULLT; - } - - 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 - } - - Text->InsertText(s); - - return 1; // success - } - -// NewEmptyText(): indicates the event of finding a new empty text in the document. -// In case of indexing empty and non-empty texts, we insert the empty texts into the -// text collection. In case of indexing only non-empty texts, it just indicates an -// empty text in the bit vector of empty texts. Returns a non-zero value upon -// success, NULLT in case of error. -int XMLTree::NewEmptyText() - { - unsigned char c = 0; - if (!initialized) { // data structure has not been initialized properly - fprintf(stderr, "Error: you cannot insert a new empty text without first calling method OpenDocument first\n"); - return NULLT; - } - 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; - } - - bitclean(empty_texts_aux, npar-1); // marks the empty text with a 0 in the bit vector - } - else Text->InsertText(&c); // we insert the empty text just in case we index all the texts +TagType XMLTree::RegisterTag(unsigned char *tagname) + { + TagType id = XMLTree::GetTagId(tagname); + if (id == NULLT) { + string s = (char *) tagname; + REGISTER_TAG(TagName,tIdMap,s); + + }; - return 1; // success + return id; } -// GetTagId: returns the tag identifier corresponding to a given tag name. -// Returns NULLT in case that the tag name does not exists. -TagType XMLTree::GetTagId(unsigned char *tagname) - { - int i; - // this should be changed for more efficient processing - for (i=0; i= ntagnames) return NULL; // invalid tag identifier - s = (unsigned char *)malloc((strlen((const char *)TagName[tagid])+1)*sizeof(unsigned char)); - strcpy((char *)s, (const char *)TagName[tagid]); - return s; - } - - -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*))); - strcpy((char*)TagName[id], (const char *)tagname); - }; - - return id; -}