1 /******************************************************************************
2 * Copyright (C) 2008 by Diego Arroyuelo *
3 * Interface for the in-memory XQuery/XPath engine *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU Lesser General Public License as published *
7 * by the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU Lesser General Public License for more details. *
15 * You should have received a copy of the GNU Lesser General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ******************************************************************************/
25 #include <unordered_set>
26 #include <unordered_map>
28 #include <TextCollection/TextCollectionBuilder.h>
35 #include <bp/bp-darray.h>
36 #include <libcds/includes/basics.h>
37 #include <libcds/includes/static_bitsequence.h>
38 #include <libcds/includes/alphabet_mapper.h>
39 #include <libcds/includes/static_sequence.h>
41 using SXSI::TextCollection;
42 using SXSI::TextCollectionBuilder;
45 // this constant is used to efficiently compute the child operation in the tree
50 #define PERM_SAMPLE 10
62 // Encoding of the XML Document :
63 // The following TAGs and IDs are fixed, "" is the tag of the root.
64 // a TextNode is represented by a leaf <<$>></<$>> The DocId in the TextCollection
65 // of that leaf is kept in a bit sequence.
66 // a TextNode below an attribute is likewise represented by a leaf <<@$>><</@$>>
67 // An element <e a1="v1" a2="v2" ... an="vn" > ...</e> the representation is:
68 // <e><<@>> <<@>a1> <<$@>>DocID(v1)</<$@>></<@>a1> ... </<@>> .... </e>
69 // Hence the attributes (if any) are always below the first child of their element,
70 // as the children of a fake node <@>.
73 #define DOCUMENT_OPEN_TAG ""
74 #define DOCUMENT_TAG_ID 0
75 #define ATTRIBUTE_OPEN_TAG "<@>"
76 #define ATTRIBUTE_TAG_ID 1
77 #define PCDATA_OPEN_TAG "<$>"
78 #define PCDATA_TAG_ID 2
79 #define ATTRIBUTE_DATA_OPEN_TAG "<@$>"
80 #define ATTRIBUTE_DATA_TAG_ID 3
81 #define CLOSING_TAG "</>"
82 #define CLOSING_TAG_ID 4
83 #define DOCUMENT_CLOSE_TAG "/"
84 #define ATTRIBUTE_CLOSE_TAG "/<@>"
85 #define PCDATA_CLOSE_TAG "/<$>"
86 #define ATTRIBUTE_DATA_CLOSE_TAG "/<@$>"
89 typedef std::unordered_set<int> TagIdSet;
90 typedef std::unordered_map<std::string,int> TagIdMap;
91 typedef TagIdMap::const_iterator TagIdMapIT;
93 #define REGISTER_TAG(v,h,t) do { (h)->insert(std::make_pair((t),(v)->size()));\
94 (v)->push_back(t); } while (false)
96 // returns NULLT if the test is true
97 #define NULLT_IF(x) do { if (x) return NULLT; } while (0)
98 #define IS_NIL(x) ((x) < 0)
100 // Direct calls to sarray library
102 #define BUFFER_ALLOC (8192 * 2)
103 #define BUFFER_SIZE (BUFFER_ALLOC / 2)
105 // tag position -> tree node
106 static treeNode tagpos2node(int t)
110 // tree node -> tag position
111 static int node2tagpos(treeNode x)
117 class XMLTreeBuilder;
121 // Only the builder can access the constructor
122 friend class XMLTreeBuilder;
125 /** Balanced parentheses representation of the tree */
128 /** Mapping from tag identifer to tag name */
129 std::vector<std::string> *TagName;
132 /** Bit vector indicating with a 1 the positions of the non-empty texts. */
133 static_bitsequence *EBVector;
135 /** Tag sequence represented with a data structure for rank and select */
136 static_sequence *Tags;
138 uint tags_blen, tags_len;
140 /** The texts in the XML document */
141 TextCollection *Text;
143 // Allows to disable the TextCollection for benchmarkin purposes
145 SXSI::TextCollectionBuilder::index_type_t text_index_type;
148 std::vector<std::string *> *print_stack;
151 void _real_flush(int fd, size_t size) {
152 if (size == 0) return;
155 written = write(fd, buffer->data(), size);
156 if ((written < 0) && (errno == EAGAIN || errno == EINTR))
165 size_t size = buffer->size();
166 if (size < BUFFER_SIZE) return;
167 _real_flush(fd, size);
170 void _dput_str(std::string s, int fd){
175 void _dputs(const char* s, int fd){
180 void _dputc(const char c, int fd){
181 buffer->push_back(c);
184 size_t _dprintf(const char* s, int fd){
185 if (s == NULL) return 0;
188 for (i = 0; (c = s[i]); i++) {
191 _dputs(""", fd);
197 _dputs("'", fd);
213 void PrintNode(treeNode n, int fd);
214 /** Data structure constructors */
215 XMLTree(){ buffer = 0;};
217 // non const pointer are freed by this method.
218 XMLTree( pb * const par,
220 std::vector<std::string> * const TN,
221 TagIdMap * const tim, uint *empty_texts_bmp,
223 TextCollectionBuilder * const TCB, bool dis_tc,
224 TextCollectionBuilder::index_type_t _index_type );
227 /** Data structure destructor */
230 /** root(): returns the tree root. */
231 treeNode Root() { return 0; }
233 /** Size() : Number of parenthesis */
239 /** NumTags() : Number of distinct tags */
240 unsigned int NumTags() {
241 return TagName->size();
244 int TagsBinaryLength(){ return tags_blen; };
245 unsigned int TagStructLength(){ return uint_len(tags_blen,tags_len); };
246 unsigned int * TagStruct() { return tags_fix; };
249 /** SubtreeSize(x): the number of nodes (and attributes) in the subtree of
251 int SubtreeSize(treeNode x) { return bp_subtree_size(Par, x); }
253 /** SubtreeTags(x,tag): the number of occurrences of tag within the subtree
255 int SubtreeTags(treeNode x, TagType tag){
256 //int s = x + 2*subtree_size(Par, x) - 1;
257 treeNode y = bp_find_close(Par, x);
261 for(int i = x; i < y; i++)
262 count += (Tag(i) == tag);
266 return (Tags->rank(tag, y) - Tags->rank(tag, x));
269 /** SubtreeElements(x) of element nodes in the subtree of x
271 int SubtreeElements(treeNode x);
273 /** IsLeaf(x): returns whether node x is leaf or not. In the succinct
274 * representation this is just a bit inspection. */
276 bool IsLeaf(treeNode x);
278 /** IsAncestor(x,y): returns whether node x is ancestor of node y. */
280 bool IsAncestor(treeNode x, treeNode y);
283 /** IsRigthDescendant returns true if y is a descendant of x and y is
284 not a descendant of the first child of x */
285 bool IsRightDescendant(treeNode x, treeNode y) {
286 if (x <= Root()) return false;
287 treeNode z = bp_parent_close(Par, x);
288 treeNode c = bp_find_close(Par, x);
289 return (y > c && y < z );
293 /** IsChild(x,y): returns whether node x is parent of node y. */
294 bool IsChild(treeNode x, treeNode y);
296 /** IsFirstChild(x): returns whether node x is the first child of its parent. */
298 bool IsFirstChild(treeNode x) {
299 return ((x != NULLT)&&(x==Root() || bp_prev_sibling(Par,x) == (treeNode)-1));
303 bool IsFirstChild(treeNode x) {
304 return (x <= Root()) || (bp_inspect(Par,x-1) == OP);
307 /** NumChildren(x): number of children of node x. Constant time with the
308 * data structure of Sadakane. */
309 int NumChildren(treeNode x);
311 /** ChildNumber(x): returns i if node x is the i-th children of its
313 int ChildNumber(treeNode x);
315 /** Depth(x): depth of node x, a simple binary rank on the parentheses
317 int Depth(treeNode x);
319 /** Preorder(x): returns the preorder number of node x, just regarding tree
320 * nodes (and not texts). */
321 int Preorder(treeNode x);
323 /** Postorder(x): returns the postorder number of node x, just regarding
324 * tree nodes (and not texts). */
325 int Postorder(treeNode x);
328 /** DocIds(x): returns the range (i.e., a pair of integers) of document
329 * identifiers that descend from node x. */
330 range DocIds(treeNode x);
332 /** Parent(x): returns the parent node of node x. */
333 treeNode Parent(treeNode x) {
334 return (x == Root()) ? NULLT : bp_parent(Par, x);
337 treeNode BinaryParent(treeNode x){
341 treeNode prev = x - 1;
342 return (bp_inspect(Par, prev) == OP) ? prev : bp_find_open(Par, prev);
346 /* Assumes x is neither 0 nor -1 */
348 /** Child(x,i): returns the i-th child of node x, assuming it exists. */
349 treeNode Child(treeNode x, int i);
353 /** LastChild(x): returns the last child of node x. */
354 treeNode LastChild(treeNode x) {
355 NULLT_IF(x == NULLT || bp_isleaf(Par,x));
356 return bp_find_open(Par, bp_find_close(Par, x)-1);
359 /** PrevSibling(x): returns the previous sibling of node x, assuming it
362 treeNode PrevSibling(treeNode x)
365 return bp_prev_sibling(Par, x);
369 /** TaggedChild(x,tag): returns the first child of node x tagged tag, or
370 * NULLT if there is none. Because of the balanced-parentheses representation
371 * of the tree, this operation is not supported efficiently, just iterating
372 * among the children of node x until finding the desired child. */
375 treeNode SelectChild(treeNode x, TagIdSet * tags);
377 /** TaggedFollowingSibling(x,tag): returns the first sibling of node x tagged tag, or
378 * NULLT if there is none. */
380 treeNode SelectFollowingSibling(treeNode x, TagIdSet * tags);
385 treeNode SelectDescendant(treeNode x, TagIdSet * tags) {
386 NULLT_IF (x == NULLT);
388 if (bp_inspect(Par, fc) == CP) return NULLT;
389 treeNode min = NULLT;
392 TagIdSet::const_iterator tagit;
393 for (tagit = tags->begin(); tagit != tags->end(); ++tagit) {
394 aux = TaggedDescendant(x, (TagType) *tagit);
395 if (((unsigned int) aux) < ((unsigned int) min)) min = aux;
400 /** TaggedPrec(x,tag): returns the first node tagged tag with smaller
401 * preorder than x and not an ancestor of x. Returns NULLT if there
403 treeNode TaggedPreceding(treeNode x, TagType tag);
405 /** TaggedFoll(x,tag): returns the first node tagged tag with larger
406 * preorder than x and not in the subtree of x. Returns NULLT if there
408 treeNode TaggedFollowing(treeNode x, TagType tag);
412 treeNode SelectFollowingBelow(treeNode x, TagIdSet * tags, treeNode ancestor);
414 // treeNode TaggedFollowingBefore(treeNode x, TagType tag,treeNode closing);
416 treeNode SelectFollowingBefore(treeNode x, TagIdSet * tags, treeNode ancestor_closing)
421 treeNode close = bp_find_close(Par,x);
424 treeNode min = NULLT;
428 TagIdSet::const_iterator tagit;
429 for (tagit = tags->begin(); tagit != tags->end(); ++tagit) {
431 aux = tagpos2node(Tags->select_next(*tagit, close));
433 if (((unsigned int) aux) < ((unsigned int) min)) min = aux;
437 return (min < ancestor_closing) ? min : NULLT;
441 /** TaggedAncestor(x, tag): returns the closest ancestor of x tagged
442 * tag. Return NULLT is there is none. */
443 treeNode TaggedAncestor(treeNode x, TagType tag);
445 /** PrevText(x): returns the document identifier of the text to the left of
446 * node x, or NULLT if x is the root node. */
447 DocID PrevText(treeNode x);
449 /** NextText(x): returns the document identifier of the text to the right of
450 * node x, or NULLT if x is the root node. */
451 DocID NextText(treeNode x);
453 /** MyText(x): returns the document identifier of the text below node x, or
454 * NULLT if x is not a leaf node. */
455 DocID MyText(treeNode x);
456 DocID MyTextUnsafe(treeNode x);
458 /** TextXMLId(d): returns the preorder of document with identifier d in the
459 * tree consisting of all tree nodes and all text nodes. */
460 int TextXMLId(DocID d);
462 /** NodeXMLId(x): returns the preorder of node x in the tree consisting of
463 * all tree nodes and all text nodes. */
464 int NodeXMLId(treeNode x);
466 /** ParentNode(d): returns the parent node of document identifier d. */
467 treeNode ParentNode(DocID d);
469 treeNode PrevNode(DocID d);
471 /** GetTagId(tagname): returns the tag identifier corresponding to a given
472 * tag name. Returns NULLT in case that the tag name does not exists. */
473 TagType GetTagId(unsigned char *tagname);
475 /** GetTagName(tagid): returns the tag name of a given tag identifier.
476 * Returns NULL in case that the tag identifier is not valid.*/
477 unsigned char *GetTagName(TagType tagid);
479 /** GetTagName(tagid): returns the tag name of a given tag identifier.
480 * The result is just a reference and should not be freed by the caller.
482 const unsigned char *GetTagNameByRef(TagType tagid);
484 /** RegisterTag adds a new tag to the tag collection this is needed
485 * if the query contains a tag which is not in the document, we need
486 * to give this new tag a fresh id and store it somewhere. A logical
488 * We might want to use a hashtable instead of an array though.
490 TagType RegisterTag(unsigned char *tagname);
492 bool EmptyText(DocID i) {
493 return Text->EmptyText(i);
496 /** Prefix(s): search for texts prefixed by string s. */
497 TextCollection::document_result Prefix(uchar const *s) {
498 return Text->Prefix(s);
501 /** Suffix(s): search for texts having string s as a suffix. */
502 TextCollection::document_result Suffix(uchar const *s) {
503 return Text->Suffix(s);
506 /** Equal(s): search for texts equal to string s. */
507 TextCollection::document_result Equals(uchar const *s) {
508 return Text->Equal(s);
511 /** Contains(s): search for texts containing string s. */
512 TextCollection::document_result Contains(uchar const *s) {
513 return Text->Contains(s);
516 /** LessThan(s): returns document identifiers for the texts that
517 * are lexicographically smaller than string s. */
518 TextCollection::document_result LessThan(uchar const *s) {
519 return Text->LessThan(s);
522 /** IsPrefix(x): returns true if there is a text prefixed by string s. */
523 bool IsPrefix(uchar const *s) {
524 return Text->IsPrefix(s);
527 /** IsSuffix(s): returns true if there is a text having string s as a
529 bool IsSuffix(uchar const *s) {
530 return Text->IsSuffix(s);
533 /** IsEqual(s): returns true if there is a text that equals given
535 bool IsEqual(uchar const *s) {
536 return Text->IsEqual(s);
539 /** IsContains(s): returns true if there is a text containing string s. */
540 bool IsContains(uchar const *s) {
541 return Text->IsContains(s);
544 /** IsLessThan(s): returns true if there is at least a text that is
545 * lexicographically smaller than string s. */
546 bool IsLessThan(uchar const *s) {
547 return Text->IsLessThan(s);
550 /** Count(s): Global counting */
551 unsigned Count(uchar const *s) {
552 return Text->Count(s);
555 /** CountPrefix(s): counting version of Prefix(s). */
556 unsigned CountPrefix(uchar const *s) {
557 return Text->CountPrefix(s);
560 /** CountSuffix(s): counting version of Suffix(s). */
561 unsigned CountSuffix(uchar const *s) {
562 return Text->CountSuffix(s);
565 /** CountEqual(s): counting version of Equal(s). */
566 unsigned CountEqual(uchar const *s) {
567 return Text->CountEqual(s);
570 /** CountContains(s): counting version of Contains(s). */
571 unsigned CountContains(uchar const *s) {
572 return Text->CountContains(s);
575 /** CountLessThan(s): counting version of LessThan(s). */
576 unsigned CountLessThan(uchar const *s) {
577 return Text->CountLessThan(s);
580 /** GetText(d): returns the text corresponding to document with
582 uchar* GetText(DocID d) {
584 uchar * s = Text->GetText(d);
585 return (s[0] == 1 ? (s+1) : s);
588 /** GetText(i, j): returns the texts corresponding to documents with
589 * ids i, i+1, ..., j. Texts are separated by '\0' character. */
590 // uchar* GetText(DocID i, DocID j) {
591 // uchar * s = Text->GetText(i, j);
592 // return (s[0] == 1 ? (uchar*)"" : s);
595 TextCollection *getTextCollection() {
599 /** Save: saves XML tree data structure to file. */
600 void Save(int fd, char* name );
602 /** Load: loads XML tree data structure from file. sample_rate_text
603 * indicates the sample rate for the text search data structure. */
604 static XMLTree *Load(int fd, bool load_tc, int sample_factor, char * name);
606 void insertTag(TagType tag, uint position);
611 /** Parenthesis functions */
612 treeNode Closing(treeNode x);
614 bool IsOpen(treeNode x);
617 /** Print procedure */
618 void Print(int fd,treeNode x, bool no_text);
619 void Print(int fd,treeNode x) { Print(fd,x,false); }
620 void Flush(int fd){ if (buffer) _real_flush(fd, buffer->size()); }
622 // The following are inlined here for speed
623 /** Tag(x): returns the tag identifier of node x. */
625 inline TagType Tag(treeNode x) const throw () {
627 return (TagType) (((uchar*)tags_fix)[(int) x]);
629 return get_field(tags_fix, tags_blen, x);
632 size_t idxlen = x * tags_blen;
633 size_t j = idxlen % W;
634 size_t i = idxlen / W;
635 size_t offset = W - tags_blen;
636 size_t offset2 = offset - j;
637 size_t w = tags_fix[i];
638 return (offset2 >= 0)
639 ? ( w << offset2 ) >> offset
640 : ( w >> j) | (tags_fix[i+1] << (W+offset2)) >> offset;
645 /** FirstChild(x): returns the first child of node x, or NULLT if the node is a leaf
647 treeNode FirstChild(treeNode x) {
649 return bp_first_child(Par, x);
653 /** FirstElement(x): returns the first non text, non attribute child of node x, or NULLT
656 treeNode FirstElement(treeNode node){
658 NULLT_IF(node == NULLT);
659 treeNode x = bp_first_child(Par, node);
660 NULLT_IF(x == NULLT);
663 case ATTRIBUTE_TAG_ID:
664 x = bp_next_sibling(Par,x);
665 if (x == NULLT || Tag(x) != PCDATA_TAG_ID) return x;
669 return (bp_inspect(Par,x)==OP)? x : NULLT;
677 /** NextSibling(x): returns the next sibling of node x, or NULLT if none
680 treeNode NextSibling(treeNode x) {
682 return bp_next_sibling(Par, x);
685 /** NextElement(x): returns the first non text, non attribute sibling of node x, or NULLT
688 treeNode NextElement(treeNode x)
691 x = bp_next_sibling(Par, x);
692 NULLT_IF(x == NULLT);
693 if (Tag(x) == PCDATA_TAG_ID){
695 return (bp_inspect(Par, y) == OP) ? y : NULLT;
699 /** TaggedDesc(x,tag): returns the first node tagged tag with larger
700 * preorder than x and within the subtree of x. Returns NULT if there
702 inline treeNode TaggedNext(treeNode x, TagType tag)
704 return tagpos2node(Tags->select_next(tag,node2tagpos(x)));
706 inline treeNode TaggedDescendant(treeNode x, TagType tag)
709 int s = (int) Tags->select_next(tag,node2tagpos(x));
712 treeNode y = tagpos2node(s); // transforms the tag position into a node position
714 return (bp_is_ancestor(Par,x,y) ? y : NULLT);
717 inline treeNode TaggedFollowingBelow(treeNode x, TagType tag, treeNode ancestor)
719 treeNode close = bp_find_close(Par, x);
720 treeNode s = tagpos2node(Tags->select_next(tag, close));
722 if (ancestor == Root() || s == NULLT || s < bp_find_close(Par,ancestor)) return s;
726 inline treeNode TaggedFollowingBefore(treeNode x, TagType tag, treeNode ancestor_closing)
728 treeNode close = bp_find_close(Par, x);
729 treeNode s = tagpos2node(Tags->select_next(tag, close));
731 if (ancestor_closing == Root() || s == NULLT || s < ancestor_closing) return s;
735 inline treeNode NextNodeBefore(treeNode x, treeNode ancestor_closing)
737 treeNode close = bp_find_close(Par, x);
738 int rank = bp_rank_open(Par, close);
739 treeNode y = bp_select_open(Par, rank+1);
740 return (y < ancestor_closing) ? y : NULLT;
743 // TaggedSibling(x,tag): returns the first sibling of node x tagged tag, or NULLT if there is none.
744 treeNode TaggedFollowingSibling(treeNode x, TagType tag)
747 treeNode sibling = x;
749 while ((sibling = bp_next_sibling(Par, sibling)) != NULLT) {
751 if (ctag == tag) return sibling;
756 treeNode TaggedChild(treeNode x, TagType tag)
759 NULLT_IF(x==NULLT || bp_isleaf(Par,x));
761 child = bp_first_child(Par, x);
763 if (Tag(child) == tag)
766 return TaggedFollowingSibling(child, tag);