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