...
[SXSI/XMLTree.git] / XMLTree.h
index f605286..1268cb6 100644 (file)
--- a/XMLTree.h
+++ b/XMLTree.h
 \r
 #ifndef XMLTREE_H_\r
 #define XMLTREE_H_\r
-#include "TextCollection/TextCollection.h"\r
+#include "TextCollection/TextCollectionBuilder.h"\r
 #include <stdio.h>\r
 #include <stdlib.h>\r
 #include <cstring>\r
+\r
+//KIM : OJO\r
+//clash between TextCollection/Tools.h and libcds/includes/basics.h\r
+#undef W\r
+#undef WW\r
+#undef Wminusone\r
+\r
 #include "bp.h"\r
 #include <static_bitsequence.h>\r
 #include <alphabet_mapper.h>\r
 #include <static_sequence.h>\r
 using SXSI::TextCollection;\r
+using SXSI::TextCollectionBuilder;\r
 \r
 \r
 // this constant is used to efficiently compute the child operation in the tree\r
@@ -37,6 +45,8 @@ using SXSI::TextCollection;
 \r
 #define NULLT -1\r
 \r
+#define PERM_SAMPLE 10\r
+\r
         // sets bit p in e\r
 #define bitset(e,p) ((e)[(p)/W] |= (1<<((p)%W)))\r
         // cleans bit p in e\r
@@ -53,6 +63,13 @@ typedef struct {
 } range;\r
 \r
 \r
+//KIM : OJO\r
+// I know this class implements the working draft that we have but the logics seem flawed here...\r
+// We should have two classes. One XMLTreeBuilder and one XMLTree.\r
+// XMLTreeBuilder would have OpenDocument, NewOpenTag,... and CloseDocument would return an XMLTree\r
+// XMLTree would have only an initialized structure. If find it really ugly to check (!finished) or (!initialized)\r
+// in every function (FirstChild....).\r
+\r
 class XMLTree {\r
    /** Balanced parentheses representation of the tree */\r
    bp *Par;\r
@@ -67,10 +84,16 @@ class XMLTree {
    static_bitsequence_rrr02 *EBVector;  \r
                      \r
    /** Tag sequence represented with a data structure for rank and select */\r
-   static_sequence_wvtree *Tags;\r
+   static_sequence *Tags;\r
+        uint * tags_fix;\r
+        uint tags_blen, tags_len;\r
 \r
    /** The texts in the XML document */\r
+   TextCollectionBuilder *TextBuilder;\r
    TextCollection *Text;\r
+\r
+   /** The texts in the XML document (cached for faster display) */\r
+   vector<string> CachedText;\r
    \r
    /** Flag indicating whether the whole data structure has been constructed or \r
     * not. If the value is true, you cannot add more texts, nodes, etc. */\r
@@ -85,13 +108,27 @@ class XMLTree {
    pb *par_aux;\r
    TagType *tags_aux;\r
    int npar;\r
+   int parArraySize;\r
    int ntagnames;\r
    unsigned int *empty_texts_aux;\r
+\r
+   // KIM : OJO\r
+   // I added those two. The TagName array should always contains two special tags\r
+   // <@> for attributes and <$> for PCDATA.\r
+   // <$> can never be in a document (since we handle the text differently)\r
+   // but <@> can be returned by the parser. This boolean is needed for the construction\r
+   // of the Tag bitmap to know if <@> must be taken into account or not\r
+   bool found_attributes;\r
+\r
+   // KIM : OJO\r
+   // Allows to disable the TextCollection for benchmarkin purposes\r
+   bool disable_tc;\r
    \r
 public:\r
+        void print_stats();\r
 \r
    /** Data structure constructor */\r
-   XMLTree() {finished = false; initialized = false;}; \r
+   XMLTree() {finished = false; initialized = false; Text = 0; TextBuilder = 0; }; \r
  \r
    /** Data structure destructor */\r
    ~XMLTree();\r
@@ -116,6 +153,9 @@ public:
   \r
    /** IsChild(x,y): returns whether node x is parent of node y. */\r
    bool IsChild(treeNode x, treeNode y);\r
+\r
+   /** IsChild(x,y): returns whether node x is the first child of its parent */\r
+   bool IsFirstChild(treeNode x);\r
    \r
    /** NumChildren(x): number of children of node x. Constant time with the \r
     * data structure of Sadakane. */\r
@@ -154,8 +194,13 @@ public:
     * Very fast in BP. */\r
    treeNode FirstChild(treeNode x);\r
    \r
+   /** LastChild(x): returns the last child of node x. \r
+    * Implemented by Kim naively. */\r
+   treeNode LastChild(treeNode x);\r
+\r
    /** NextSibling(x): returns the next sibling of node x, assuming it \r
     * exists. */\r
+\r
    treeNode NextSibling(treeNode x);\r
    \r
    /** PrevSibling(x): returns the previous sibling of node x, assuming it \r
@@ -173,6 +218,22 @@ public:
     * is none. */\r
    treeNode TaggedDesc(treeNode x, TagType tag);\r
 \r
+\r
+   treeNode TaggedBelow(treeNode x, TagType *childtags, unsigned int ctlen,\r
+                                TagType *desctags, unsigned int dtlen);\r
+   \r
+   treeNode TaggedNext(treeNode x, TagType *childtags, unsigned int ctlen,\r
+                               TagType *folltags, unsigned int flen,treeNode root);\r
+\r
+   treeNode TaggedDescOnly(treeNode x, TagType *desctags, unsigned int dtlen);\r
+   \r
+   treeNode TaggedDescOrFollOnly(treeNode x, TagType *folltags, unsigned int flen,\r
+                          treeNode root);\r
+\r
+   treeNode TaggedFollOnly(treeNode x, TagType *folltags, unsigned int flen,\r
+                          treeNode root);\r
+   \r
+\r
    /** TaggedPrec(x,tag): returns the first node tagged tag with smaller \r
     * preorder than x and not an ancestor of x. Returns NULLT if there \r
     * is none. */\r
@@ -182,7 +243,16 @@ public:
     * preorder than x and not in the subtree of x. Returns NULLT if there \r
     * is none. */\r
    treeNode TaggedFoll(treeNode x, TagType tag);\r
-   \r
+\r
+   treeNode TaggedFollBelow(treeNode x, TagType tag,treeNode root);     \r
+  \r
+   /** TaggedFollowingSibling(x,tag) */\r
+   treeNode TaggedFollowingSibling(treeNode x, TagType tag);\r
+\r
+   /** TaggedAncestor(x, tag): returns the closest ancestor of x tagged \r
+     * tag. Return NULLT is there is none. */\r
+   treeNode TaggedAncestor(treeNode x, TagType tag);\r
\r
    /** PrevText(x): returns the document identifier of the text to the left of \r
     * node x, or NULLT if x is the root node. */\r
    DocID PrevText(treeNode x);\r
@@ -205,15 +275,19 @@ public:
    \r
    /** ParentNode(d): returns the parent node of document identifier d. */\r
    treeNode ParentNode(DocID d);\r
+   treeNode PrevNode(DocID d);\r
 \r
-   /** OpenDocument(empty_texts,sample_rate_text): initilizes the construction\r
+   /** OpenDocument(empty_texts,sample_rate_text,dtc): initilizes the construction\r
     * of the data structure for the XML document. Parameter empty_texts \r
     * indicates whether we index empty texts in document or not. Parameter \r
     * sample_rate_text indicates the sampling rate for the text searching data\r
     * structures (small values get faster searching but a bigger space \r
-    * requirement). Returns a non-zero value upon success, NULLT in case of \r
+    * requirement). dtc disable the use of the TextCollection\r
+    * (i.e. everything is considered an empty text *)\r
+    * Returns a non-zero value upon success, NULLT in case of \r
     * error. */\r
-   int OpenDocument(bool empty_texts, int sample_rate_text);\r
+\r
+   int OpenDocument(bool empty_texts, int sample_rate_text, bool dtc);\r
 \r
    /** CloseDocument(): finishes the construction of the data structure for \r
     * the XML document. Tree and tags are represented in the final form, \r
@@ -251,6 +325,25 @@ public:
     * Returns NULL in case that the tag identifier is not valid.*/\r
    unsigned char *GetTagName(TagType tagid);\r
 \r
+\r
+   // OJO\r
+   /** GetTagName(tagid): returns the tag name of a given tag identifier.     \r
+    *  The result is just a reference and should not be freed by the caller.\r
+    */\r
+   const unsigned char *GetTagNameByRef(TagType tagid);\r
+\r
+   //OJO\r
+   /** RegisterTag adds a new tag to the tag collection this is needed\r
+    * if the query contains a tag which is not in the document, we need\r
+    * to give this new tag a fresh id and store it somewhere. A logical\r
+    * choice is here.\r
+    * We might want to use a hashtable instead of an array though.\r
+    */\r
+   TagType RegisterTag(unsigned char *tagname);\r
+\r
+   bool EmptyText(DocID i) {\r
+       return Text->EmptyText(i);\r
+   }\r
    /** Prefix(s): search for texts prefixed by string s. */\r
    TextCollection::document_result Prefix(uchar const *s) {\r
       return Text->Prefix(s);\r
@@ -304,6 +397,11 @@ public:
    bool IsLessThan(uchar const *s) {\r
       return Text->IsLessThan(s);\r
    }\r
+   \r
+   /** Count(s): Global counting  */\r
+   unsigned Count(uchar const *s) {\r
+      return Text->Count(s);\r
+   }\r
 \r
    /** CountPrefix(s): counting version of Prefix(s). */\r
    unsigned CountPrefix(uchar const *s) {\r
@@ -333,7 +431,13 @@ public:
    /** GetText(d): returns the text corresponding to document with\r
     * id d. */\r
    uchar* GetText(DocID d) {\r
-      return Text->GetText(d);\r
+       return Text->GetText(d);\r
+   }\r
+\r
+   uchar* GetCachedText(DocID d) {\r
+     uchar * str = (uchar*) calloc(sizeof(char),(CachedText.at(d).size() + 1));\r
+     strcpy((char*) str,(const char*) CachedText.at(d).c_str());\r
+     return (uchar*) (str);\r
    }\r
    \r
    TextCollection *getTextCollection() {\r