.
[SXSI/XMLTree.git] / XMLTree.h
1 /******************************************************************************\r
2  *   Copyright (C) 2008 by Diego Arroyuelo                                    *\r
3  *   Interface for the in-memory XQuery/XPath engine                          *\r
4  *                                                                            *\r
5  *   This program is free software; you can redistribute it and/or modify     *\r
6  *   it under the terms of the GNU Lesser General Public License as published *\r
7  *   by the Free Software Foundation; either version 2 of the License, or     *\r
8  *   (at your option) any later version.                                      *\r
9  *                                                                            *\r
10  *   This program is distributed in the hope that it will be useful,          *\r
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of           *\r
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            *\r
13  *   GNU Lesser General Public License for more details.                      *\r
14  *                                                                            *\r
15  *   You should have received a copy of the GNU Lesser General Public License *\r
16  *   along with this program; if not, write to the                            *\r
17  *   Free Software Foundation, Inc.,                                          *\r
18  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.                *\r
19  ******************************************************************************/\r
20 \r
21 #ifndef XMLTREE_H_\r
22 #define XMLTREE_H_\r
23 #include <unordered_set>\r
24 #include <unordered_map>\r
25 #include "TextCollection/TextCollectionBuilder.h"\r
26 #include <stdio.h>\r
27 #include <stdlib.h>\r
28 #include <cstring>\r
29 \r
30 \r
31 #undef W\r
32 #undef WW\r
33 #undef Wminusone\r
34 \r
35 #include "bp.h"\r
36 #include <libcds/includes/basics.h>\r
37 #include <static_bitsequence.h>\r
38 #include <alphabet_mapper.h>\r
39 #include <static_sequence.h>\r
40 using SXSI::TextCollection;\r
41 using SXSI::TextCollectionBuilder;\r
42 \r
43 \r
44 // this constant is used to efficiently compute the child operation in the tree\r
45 #define OPTD 10\r
46 \r
47 #define NULLT -1\r
48 \r
49 #define PERM_SAMPLE 10\r
50 \r
51 \r
52 typedef int treeNode;\r
53 typedef int TagType; \r
54 typedef int DocID;  \r
55 \r
56 typedef struct {\r
57    int min;\r
58    int max;\r
59 } range;\r
60 \r
61 // Encoding of the XML Document :\r
62 // The following TAGs and IDs are fixed, "" is the tag of the root.\r
63 // a TextNode is represented by a leaf <<$>></<$>> The DocId in the TextCollection\r
64 // of that leaf is kept in a bit sequence.\r
65 // a TextNode below an attribute is likewise represented by a leaf <<@$>><</@$>>\r
66 // An element <e a1="v1" a2="v2" ... an="vn" > ...</e> the representation is:\r
67 // <e><<@>> <<@>a1> <<$@>>DocID(v1)</<$@>></<@>a1> ... </<@>> .... </e>\r
68 // Hence the attributes (if any) are always below the first child of their element,\r
69 // as the children of a fake node <@>.\r
70 \r
71 \r
72 #define DOCUMENT_OPEN_TAG ""\r
73 #define DOCUMENT_TAG_ID 0\r
74 #define ATTRIBUTE_OPEN_TAG "<@>"\r
75 #define ATTRIBUTE_TAG_ID 1\r
76 #define PCDATA_OPEN_TAG "<$>"\r
77 #define PCDATA_TAG_ID 2\r
78 #define ATTRIBUTE_DATA_OPEN_TAG "<@$>"\r
79 #define ATTRIBUTE_DATA_TAG_ID 3\r
80 #define DOCUMENT_CLOSE_TAG "/"\r
81 #define ATTRIBUTE_CLOSE_TAG "/<@>"\r
82 #define PCDATA_CLOSE_TAG "/<$>"\r
83 #define ATTRIBUTE_DATA_CLOSE_TAG "/<@$>"\r
84 \r
85 \r
86 typedef std::unordered_set<int> TagIdSet;\r
87 typedef std::unordered_map<string,int> TagIdMap;\r
88 typedef TagIdMap::const_iterator TagIdMapIT;\r
89 \r
90 #define REGISTER_TAG(v,h,t) do { (h)->insert(std::make_pair((t),(v)->size()));\\r
91     (v)->push_back(t); } while (false)\r
92 \r
93 \r
94 // returns NULLT if the test is true\r
95 #define NULLT_IF(x)  do { if (x) return NULLT; } while (0)\r
96 \r
97 \r
98 class XMLTreeBuilder;\r
99 \r
100 class XMLTree {\r
101 \r
102   // Only the builder can access the constructor\r
103   friend class XMLTreeBuilder;\r
104 \r
105  private:\r
106    /** Balanced parentheses representation of the tree */\r
107    bp *Par;\r
108  \r
109    /** Mapping from tag identifer to tag name */  \r
110    vector<string> *TagName;\r
111    TagIdMap * tIdMap;\r
112   \r
113    /** Bit vector indicating with a 1 the positions of the non-empty texts. */\r
114    static_bitsequence *EBVector;  \r
115                       \r
116    /** Tag sequence represented with a data structure for rank and select */\r
117    static_sequence *Tags;\r
118    uint * tags_fix;\r
119    uint tags_blen, tags_len;\r
120 \r
121    /** The texts in the XML document */\r
122    TextCollection *Text;\r
123 \r
124    // Allows to disable the TextCollection for benchmarkin purposes\r
125    bool disable_tc;\r
126    \r
127    FILE* stream;\r
128    int   stream_fd;\r
129 \r
130    /** Data structure constructors */\r
131    XMLTree(){;};\r
132 \r
133    // non const pointer are freed by this method.\r
134   XMLTree( pb * const par, uint npar,  vector<string> * const TN,  TagIdMap * const tim, uint *empty_texts_bmp, TagType *tags,\r
135            TextCollection * const TC, bool dis_tc);\r
136 \r
137 public: \r
138    /** Data structure destructor */\r
139    ~XMLTree();\r
140    \r
141    /** root(): returns the tree root. */\r
142    treeNode Root();\r
143 \r
144    /** Size() :  Number of parenthesis */\r
145    unsigned int Size(){\r
146      return tags_len/2;\r
147    }\r
148 \r
149    /** SubtreeSize(x): the number of nodes (and attributes) in the subtree of \r
150     * node x. */\r
151    int SubtreeSize(treeNode x);\r
152   \r
153    /** SubtreeTags(x,tag): the number of occurrences of tag within the subtree \r
154     * of node x. */\r
155    int SubtreeTags(treeNode x, TagType tag);\r
156    \r
157    /** SubtreeElements(x) of element nodes in the subtree of x\r
158     */\r
159    int SubtreeElements(treeNode x);\r
160 \r
161    /** IsLeaf(x): returns whether node x is leaf or not. In the succinct \r
162     * representation this is just a bit inspection. */\r
163 \r
164    bool IsLeaf(treeNode x);\r
165 \r
166    /** IsAncestor(x,y): returns whether node x is ancestor of node y. */\r
167 \r
168    bool IsAncestor(treeNode x, treeNode y);\r
169   \r
170    /** IsChild(x,y): returns whether node x is parent of node y. */\r
171    bool IsChild(treeNode x, treeNode y);\r
172 \r
173    /** IsFirstChild(x): returns whether node x is the first child of its parent. */\r
174    /* OCAML */\r
175    bool IsFirstChild(treeNode x);\r
176      \r
177    /** NumChildren(x): number of children of node x. Constant time with the \r
178     * data structure of Sadakane. */\r
179    int NumChildren(treeNode x);\r
180 \r
181    /** ChildNumber(x): returns i if node x is the i-th children of its \r
182     * parent. */\r
183    int ChildNumber(treeNode x);\r
184 \r
185    /** Depth(x): depth of node x, a simple binary rank on the parentheses \r
186     * sequence. */\r
187    int Depth(treeNode x);\r
188    \r
189    /** Preorder(x): returns the preorder number of node x, just regarding tree \r
190     * nodes (and not texts). */ \r
191    int Preorder(treeNode x);\r
192    \r
193    /** Postorder(x): returns the postorder number of node x, just regarding \r
194     * tree nodes (and not texts). */\r
195    int Postorder(treeNode x);\r
196       \r
197    /** Tag(x): returns the tag identifier of node x. */\r
198    TagType Tag(treeNode x) {\r
199      if (tags_blen == 8)\r
200        return  (TagType) (((uchar*)tags_fix)[(int) x]);\r
201      else\r
202        return (TagType) get_field(tags_fix,tags_blen, (int) x);\r
203    }\r
204 \r
205    /** DocIds(x): returns the range (i.e., a pair of integers) of document \r
206     * identifiers that descend from node x. */\r
207    range DocIds(treeNode x);\r
208 \r
209    /** Parent(x): returns the parent node of node x. */\r
210    treeNode Parent(treeNode x);\r
211    /* Assumes x is neither 0 nor -1 */\r
212    \r
213    /** Child(x,i): returns the i-th child of node x, assuming it exists. */   \r
214    treeNode Child(treeNode x, int i);\r
215 \r
216    /** FirstChild(x): returns the first child of node x, or NULLT if the node is a leaf\r
217     */\r
218    treeNode FirstChild(treeNode x);\r
219 \r
220    /** FirstElement(x): returns the first non text, non attribute child of node x, or NULLT\r
221     *    if none.\r
222     */\r
223    treeNode FirstElement(treeNode x);\r
224 \r
225    /** LastChild(x): returns the last child of node x.  */\r
226    treeNode LastChild(treeNode x);\r
227    \r
228    /** NextSibling(x): returns the next sibling of node x, or NULLT if none \r
229     * exists. */\r
230 \r
231    treeNode NextSibling(treeNode x);\r
232 \r
233    /** NextElement(x): returns the first non text, non attribute sibling of node x, or NULLT\r
234     *    if none.\r
235     */\r
236    treeNode NextElement(treeNode x);\r
237    \r
238    /** PrevSibling(x): returns the previous sibling of node x, assuming it \r
239     * exists. */\r
240 \r
241    treeNode PrevSibling(treeNode x);\r
242    \r
243    /** TaggedChild(x,tag): returns the first child of node x tagged tag, or \r
244     * NULLT if there is none. Because of the balanced-parentheses representation \r
245     * of the tree, this operation is not supported efficiently, just iterating \r
246     * among the children of node x until finding the desired child. */\r
247    treeNode TaggedChild(treeNode x, TagType tag);\r
248    \r
249    treeNode SelectChild(treeNode x, TagIdSet * tags);\r
250 \r
251    /** TaggedFollowingSibling(x,tag): returns the first sibling of node x tagged tag, or \r
252     *  NULLT if there is none. */\r
253    treeNode TaggedFollowingSibling(treeNode x, TagType tag);\r
254    \r
255    treeNode SelectFollowingSibling(treeNode x, TagIdSet * tags);\r
256 \r
257    /** TaggedDesc(x,tag): returns the first node tagged tag with larger \r
258     * preorder than x and within the subtree of x. Returns NULT if there \r
259     * is none. */\r
260    treeNode TaggedDescendant(treeNode x, TagType tag);\r
261 \r
262    treeNode SelectDescendant(treeNode x, TagIdSet * tags);\r
263 \r
264    /** TaggedPrec(x,tag): returns the first node tagged tag with smaller \r
265     * preorder than x and not an ancestor of x. Returns NULLT if there \r
266     * is none. */\r
267    treeNode TaggedPreceding(treeNode x, TagType tag);\r
268   \r
269    /** TaggedFoll(x,tag): returns the first node tagged tag with larger \r
270     * preorder than x and not in the subtree of x. Returns NULLT if there \r
271     * is none. */\r
272    treeNode TaggedFollowing(treeNode x, TagType tag);\r
273 \r
274    treeNode TaggedFollowingBelow(treeNode x, TagType tag,treeNode ancestor);     \r
275 \r
276    treeNode SelectFollowingBelow(treeNode x, TagIdSet * tags, treeNode ancestor);\r
277 \r
278    treeNode TaggedFollowingBefore(treeNode x, TagType tag,treeNode closing);\r
279 \r
280    treeNode SelectFollowingBefore(treeNode x, TagIdSet * tags, treeNode closing);\r
281 \r
282    /** TaggedAncestor(x, tag): returns the closest ancestor of x tagged \r
283      * tag. Return NULLT is there is none. */\r
284    treeNode TaggedAncestor(treeNode x, TagType tag);\r
285  \r
286    /** PrevText(x): returns the document identifier of the text to the left of \r
287     * node x, or NULLT if x is the root node. */\r
288    DocID PrevText(treeNode x);\r
289    \r
290    /** NextText(x): returns the document identifier of the text to the right of \r
291     * node x, or NULLT if x is the root node. */\r
292    DocID NextText(treeNode x);\r
293    \r
294    /** MyText(x): returns the document identifier of the text below node x, or \r
295     * NULLT if x is not a leaf node. */\r
296    DocID MyText(treeNode x);\r
297    DocID MyTextUnsafe(treeNode x);\r
298 \r
299    /** TextXMLId(d): returns the preorder of document with identifier d in the \r
300     * tree consisting of all tree nodes and all text nodes. */\r
301    int TextXMLId(DocID d);\r
302    \r
303    /** NodeXMLId(x): returns the preorder of node x in the tree consisting of \r
304     * all tree nodes and all text nodes. */\r
305    int NodeXMLId(treeNode x);\r
306    \r
307    /** ParentNode(d): returns the parent node of document identifier d. */\r
308    treeNode ParentNode(DocID d);\r
309    \r
310    treeNode PrevNode(DocID d);\r
311 \r
312    /** GetTagId(tagname): returns the tag identifier corresponding to a given \r
313     * tag name. Returns NULLT in case that the tag name does not exists. */\r
314    TagType GetTagId(unsigned char *tagname);\r
315 \r
316    /** GetTagName(tagid): returns the tag name of a given tag identifier. \r
317     * Returns NULL in case that the tag identifier is not valid.*/\r
318    unsigned char *GetTagName(TagType tagid);\r
319 \r
320    /** GetTagName(tagid): returns the tag name of a given tag identifier.     \r
321     *  The result is just a reference and should not be freed by the caller.\r
322     */\r
323    const unsigned char *GetTagNameByRef(TagType tagid);\r
324 \r
325    /** RegisterTag adds a new tag to the tag collection this is needed\r
326     * if the query contains a tag which is not in the document, we need\r
327     * to give this new tag a fresh id and store it somewhere. A logical\r
328     * choice is here.\r
329     * We might want to use a hashtable instead of an array though.\r
330     */\r
331    TagType RegisterTag(unsigned char *tagname);\r
332 \r
333    bool EmptyText(DocID i) {\r
334        return Text->EmptyText(i);\r
335    }\r
336 \r
337    /** Prefix(s): search for texts prefixed by string s. */\r
338    TextCollection::document_result Prefix(uchar const *s) {\r
339       return Text->Prefix(s);\r
340    }\r
341 \r
342    /** Suffix(s): search for texts having string s as a suffix. */\r
343    TextCollection::document_result Suffix(uchar const *s) {\r
344       return Text->Suffix(s);\r
345    }\r
346 \r
347    /** Equal(s): search for texts equal to string s. */\r
348    TextCollection::document_result Equals(uchar const *s) {\r
349       return Text->Equal(s);\r
350    }\r
351 \r
352    /** Contains(s): search for texts containing string s.  */\r
353    TextCollection::document_result Contains(uchar const *s) {\r
354       return Text->Contains(s);\r
355    }\r
356 \r
357    /** LessThan(s): returns document identifiers for the texts that\r
358     * are lexicographically smaller than string s. */\r
359    TextCollection::document_result LessThan(uchar const *s) {\r
360       return Text->LessThan(s);\r
361    }\r
362    \r
363    /** IsPrefix(x): returns true if there is a text prefixed by string s. */\r
364    bool IsPrefix(uchar const *s) {\r
365       return Text->IsPrefix(s);\r
366    }          \r
367    \r
368    /** IsSuffix(s): returns true if there is a text having string s as a \r
369     * suffix.*/\r
370    bool IsSuffix(uchar const *s) {\r
371       return Text->IsSuffix(s);\r
372    }\r
373    \r
374    /** IsEqual(s): returns true if there is a text that equals given \r
375     * string s. */\r
376    bool IsEqual(uchar const *s) {\r
377       return Text->IsEqual(s);\r
378    }\r
379    \r
380    /** IsContains(s): returns true if there is a text containing string s. */\r
381    bool IsContains(uchar const *s) {\r
382       return Text->IsContains(s);\r
383    }\r
384    \r
385    /** IsLessThan(s): returns true if there is at least a text that is \r
386     * lexicographically smaller than string s. */\r
387    bool IsLessThan(uchar const *s) {\r
388       return Text->IsLessThan(s);\r
389    }\r
390    \r
391    /** Count(s): Global counting  */\r
392    unsigned Count(uchar const *s) {\r
393       return Text->Count(s);\r
394    }\r
395 \r
396    /** CountPrefix(s): counting version of Prefix(s). */\r
397    unsigned CountPrefix(uchar const *s) {\r
398       return Text->CountPrefix(s);\r
399    }\r
400    \r
401    /** CountSuffix(s): counting version of Suffix(s). */\r
402    unsigned CountSuffix(uchar const *s) {\r
403       return Text->CountSuffix(s);\r
404    }\r
405    \r
406    /** CountEqual(s): counting version of Equal(s). */\r
407    unsigned CountEqual(uchar const *s) {\r
408       return Text->CountEqual(s);\r
409    }\r
410    \r
411    /** CountContains(s): counting version of Contains(s). */\r
412    unsigned CountContains(uchar const *s) {\r
413       return Text->CountContains(s);\r
414    }\r
415    \r
416    /** CountLessThan(s): counting version of LessThan(s). */\r
417    unsigned CountLessThan(uchar const *s) {\r
418       return Text->CountLessThan(s);\r
419    }\r
420    \r
421    /** GetText(d): returns the text corresponding to document with\r
422     * id d. */\r
423    uchar* GetText(DocID d) {\r
424      \r
425        uchar * s = Text->GetText(d);\r
426        return (s[0] == 1 ? (uchar*)"" : s);\r
427    }\r
428 \r
429    /** GetText(i, j): returns the texts corresponding to documents with\r
430     * ids i, i+1, ..., j. Texts are separated by '\0' character.  */\r
431    //   uchar* GetText(DocID i, DocID j) {\r
432    //  uchar * s = Text->GetText(i, j);\r
433    // return (s[0] == 1 ? (uchar*)"" : s);\r
434    //}\r
435 \r
436    TextCollection *getTextCollection() {\r
437       return Text;\r
438    }\r
439    \r
440    /** Save: saves XML tree data structure to file. */\r
441    void Save(int fd);\r
442       \r
443    /** Load: loads XML tree data structure from file. sample_rate_text \r
444     * indicates the sample rate for the text search data structure. */\r
445    static XMLTree *Load(int fd,bool load_tc, int sample_factor);   \r
446 \r
447    void insertTag(TagType tag, uint position);\r
448    \r
449    void print_stats();\r
450 \r
451    \r
452    /** Parenthesis functions */\r
453    treeNode Closing(treeNode x);\r
454 \r
455    bool IsOpen(treeNode x);\r
456 \r
457 \r
458    /** Print procedure */\r
459    void Print(int fd,treeNode x);\r
460 \r
461 };\r
462 #endif\r
463 \r