2 /******************************************************************************
\r
3 * Copyright (C) 2008 by Diego Arroyuelo *
\r
4 * Interface for the in-memory XQuery/XPath engine *
\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
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
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
24 #include <unordered_set>
\r
25 #include <unordered_map>
\r
26 #include "TextCollection/TextCollectionBuilder.h"
\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
45 // this constant is used to efficiently compute the child operation in the tree
\r
50 #define PERM_SAMPLE 10
\r
53 typedef int treeNode;
\r
54 typedef int TagType;
\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
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
88 typedef std::unordered_map<string,int> TagIdMap;
\r
89 typedef TagIdMap::const_iterator TagIdMapIT;
\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
95 class XMLTreeBuilder;
\r
99 // Only the builder can access the constructor
\r
100 friend class XMLTreeBuilder;
\r
103 /** Balanced parentheses representation of the tree */
\r
106 /** Mapping from tag identifer to tag name */
\r
107 vector<string> *TagName;
\r
110 /** Bit vector indicating with a 1 the positions of the non-empty texts. */
\r
111 static_bitsequence *EBVector;
\r
113 /** Tag sequence represented with a data structure for rank and select */
\r
114 static_sequence *Tags;
\r
116 uint tags_blen, tags_len;
\r
118 /** The texts in the XML document */
\r
119 TextCollection *Text;
\r
121 /** The texts in the XML document (cached for faster display) */
\r
122 vector<string> *CachedText;
\r
124 // Allows to disable the TextCollection for benchmarkin purposes
\r
128 /** Data structure constructors */
\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
136 /** Data structure destructor */
\r
139 /** root(): returns the tree root. */
\r
142 /** SubtreeSize(x): the number of nodes (and attributes) in the subtree of
\r
144 int SubtreeSize(treeNode x);
\r
146 /** SubtreeTags(x,tag): the number of occurrences of tag within the subtree
\r
148 int SubtreeTags(treeNode x, TagType tag);
\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
154 /** IsAncestor(x,y): returns whether node x is ancestor of node y. */
\r
155 bool IsAncestor(treeNode x, treeNode y);
\r
157 /** IsChild(x,y): returns whether node x is parent of node y. */
\r
158 bool IsChild(treeNode x, treeNode y);
\r
160 /** IsFirstChild(x): returns whether node x is the first child of its parent. */
\r
161 bool IsFirstChild(treeNode x);
\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
167 /** ChildNumber(x): returns i if node x is the i-th children of its
\r
169 inline int ChildNumber(treeNode x);
\r
171 /** Depth(x): depth of node x, a simple binary rank on the parentheses
\r
173 int Depth(treeNode x);
\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
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
183 /** Tag(x): returns the tag identifier of node x. */
\r
184 TagType Tag(treeNode x);
\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
190 /** Parent(x): returns the parent node of node x. */
\r
191 treeNode Parent(treeNode x);
\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
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
201 /** LastChild(x): returns the last child of node x. */
\r
202 treeNode LastChild(treeNode x);
\r
204 /** NextSibling(x): returns the next sibling of node x, assuming it
\r
206 treeNode NextSibling(treeNode x);
\r
207 treeNode NextElement(treeNode x);
\r
209 /** PrevSibling(x): returns the previous sibling of node x, assuming it
\r
211 treeNode PrevSibling(treeNode x);
\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
219 treeNode SelectChild(treeNode x, std::unordered_set<int> * tags);
\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
225 treeNode SelectFollSibling(treeNode x, std::unordered_set<int> * tags);
\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
230 treeNode TaggedDesc(treeNode x, TagType tag);
\r
232 treeNode SelectDesc(treeNode x, std::unordered_set<int> * tags);
\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
238 treeNode TaggedPrec(treeNode x, TagType tag);
\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
243 treeNode TaggedFoll(treeNode x, TagType tag);
\r
245 treeNode TaggedFollBelow(treeNode x, TagType tag,treeNode root);
\r
247 treeNode SelectFollBelow(treeNode x, std::unordered_set<int> * tags, treeNode root);
\r
249 /** TaggedFollowingSibling(x,tag) */
\r
250 treeNode TaggedFollowingSibling(treeNode x, TagType tag);
\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
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
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
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
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
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
276 /** ParentNode(d): returns the parent node of document identifier d. */
\r
277 treeNode ParentNode(DocID d);
\r
279 treeNode PrevNode(DocID d);
\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
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
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
292 const unsigned char *GetTagNameByRef(TagType tagid);
\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
298 * We might want to use a hashtable instead of an array though.
\r
300 TagType RegisterTag(unsigned char *tagname);
\r
302 bool EmptyText(DocID i) {
\r
303 return Text->EmptyText(i);
\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
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
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
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
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
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
337 /** IsSuffix(s): returns true if there is a text having string s as a
\r
339 bool IsSuffix(uchar const *s) {
\r
340 return Text->IsSuffix(s);
\r
343 /** IsEqual(s): returns true if there is a text that equals given
\r
345 bool IsEqual(uchar const *s) {
\r
346 return Text->IsEqual(s);
\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
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
360 /** Count(s): Global counting */
\r
361 unsigned Count(uchar const *s) {
\r
362 return Text->Count(s);
\r
365 /** CountPrefix(s): counting version of Prefix(s). */
\r
366 unsigned CountPrefix(uchar const *s) {
\r
367 return Text->CountPrefix(s);
\r
370 /** CountSuffix(s): counting version of Suffix(s). */
\r
371 unsigned CountSuffix(uchar const *s) {
\r
372 return Text->CountSuffix(s);
\r
375 /** CountEqual(s): counting version of Equal(s). */
\r
376 unsigned CountEqual(uchar const *s) {
\r
377 return Text->CountEqual(s);
\r
380 /** CountContains(s): counting version of Contains(s). */
\r
381 unsigned CountContains(uchar const *s) {
\r
382 return Text->CountContains(s);
\r
385 /** CountLessThan(s): counting version of LessThan(s). */
\r
386 unsigned CountLessThan(uchar const *s) {
\r
387 return Text->CountLessThan(s);
\r
390 /** GetText(d): returns the text corresponding to document with
\r
392 uchar* GetText(DocID d) {
\r
393 uchar * s = Text->GetText(d);
\r
394 return (s[0] == 1 ? (uchar*)"" : s);
\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
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
410 TextCollection *getTextCollection() {
\r
414 /** Save: saves XML tree data structure to file. */
\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
421 void insertTag(TagType tag, uint position);
\r
423 void print_stats();
\r