Adds external flush function,
authorkim <kim@3cdefd35-fc62-479d-8e8d-bae585ffb9ca>
Tue, 19 Apr 2011 08:16:22 +0000 (08:16 +0000)
committerkim <kim@3cdefd35-fc62-479d-8e8d-bae585ffb9ca>
Tue, 19 Apr 2011 08:16:22 +0000 (08:16 +0000)
sanitize builder.

git-svn-id: svn+ssh://idea.nguyen.vg/svn/sxsi/trunk/XMLTree@1053 3cdefd35-fc62-479d-8e8d-bae585ffb9ca

Makefile
XMLTree.cpp
XMLTree.h
XMLTreeBuilder.cpp
XMLTreeBuilder.h

index 05922b4..e972a3c 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -1,4 +1,4 @@
-OPT_FLAGS=-O6 -DPOPCOUNT_TABLE -DHAS_NATIVE_POPCOUNT 
+OPT_FLAGS=-O6 -DPOPCOUNT_TABLE -DHAS_NATIVE_POPCOUNT -fno-PIC
 INC_FLAGS=-I./libcds/includes/ -I.
 CFLAGS= $(INC_FLAGS) $(OPT_FLAGS)
 CXXFLAGS= -std=c++0x $(INC_FLAGS) $(OPT_FLAGS)
index 5033877..40d8055 100644 (file)
@@ -66,9 +66,11 @@ static uint fast_get_field(uint* A,int len, int idx)
 \r
 XMLTree::XMLTree( pb * const par, uint npar,  vector<string> * const TN,  TagIdMap * const tim, \r
                  uint *empty_texts_bmp, TagType *tags,\r
-                 TextCollection * const TC, bool dis_tc)\r
+                 TextCollection * const TC, bool dis_tc,\r
+                 TextCollectionBuilder::index_type_t _index_type )\r
  {\r
    buffer = 0;\r
+   print_stack = 0;\r
     // creates the data structure for the tree topology\r
     Par = (bp *)umalloc(sizeof(bp));\r
     STARTTIMER();\r
@@ -117,8 +119,7 @@ XMLTree::XMLTree( pb * const par, uint npar,  vector<string> * const TN,  TagIdM
 \r
     \r
     disable_tc = dis_tc;\r
-    stream = NULL;\r
-    stream_fd = 0;\r
+    text_index_type = _index_type;    \r
     std::cerr << "Number of distinct tags " << TagName->size() << "\n";\r
     //std::cerr.flush();\r
  }\r
@@ -146,11 +147,6 @@ XMLTree::~XMLTree()
 \r
     delete EBVector;\r
     EBVector = NULL;\r
-    if (stream != NULL){\r
-      fclose(stream);\r
-      stream = NULL;\r
-      stream_fd = 0;\r
-    };\r
 \r
  }\r
 \r
@@ -167,7 +163,7 @@ void XMLTree::print_stats()
  }\r
 \r
 // Save: saves XML tree data structure to file. \r
-void XMLTree::Save(int fd, char *filename\r
+void XMLTree::Save(int fd) \r
  {\r
     FILE *fp;\r
     char filenameaux[1024];\r
@@ -199,14 +195,31 @@ void XMLTree::Save(int fd, char *filename)
     \r
     // stores the texts   \r
     if (!disable_tc) {\r
-        Text->Save(fp, filename);\r
-    };\r
- }\r
 \r
+      ufwrite(&text_index_type, sizeof(TextCollectionBuilder::index_type_t), 1, fp);\r
+     \r
+      const char * pref;\r
+      switch (text_index_type){\r
+      case TextCollectionBuilder::index_type_default:\r
+       pref = "default_";\r
+       break;\r
+      case TextCollectionBuilder::index_type_swcsa:\r
+       pref = "swcsa_";\r
+       break;\r
+      case TextCollectionBuilder::index_type_rlcsa:\r
+       pref = "rlcsa_";\r
+       break;\r
+      };\r
+      \r
+      Text->Save(fp, pref);\r
+          \r
+\r
+    }\r
+ }\r
 \r
 // Load: loads XML tree data structure from file. Returns\r
 // a pointer to the loaded data structure\r
-XMLTree *XMLTree::Load(int fd, char *filename, bool load_tc,int sample_factor) \r
+XMLTree *XMLTree::Load(int fd, bool load_tc,int sample_factor) \r
  {\r
 \r
     FILE *fp;\r
@@ -277,32 +290,44 @@ XMLTree *XMLTree::Load(int fd, char *filename, bool load_tc,int sample_factor)
     \r
     ufread(&(XML_Tree->disable_tc), sizeof(bool), 1, fp);\r
     if (load_tc) {\r
-    XML_Tree->EBVector = static_bitsequence_rrr02::load(fp);\r
-    //XML_Tree->EBVector = static_bitsequence_sdarray::load(fp);\r
+      XML_Tree->EBVector = static_bitsequence_rrr02::load(fp);\r
 \r
-    STOPTIMER(Loading);\r
-    PRINTTIME("Loading text bitvector struct", Loading);\r
-    STARTTIMER();\r
-\r
-    // Not used  \r
-    // loads the texts\r
-    if (!XML_Tree->disable_tc){\r
-        XML_Tree->Text = TextCollection::Load(fp, filename, TextCollection::index_mode_default, sample_factor);\r
-    }\r
-    else XML_Tree->Text = NULL;\r
-    STOPTIMER(Loading);\r
-    PRINTTIME("Loading TextCollection", Loading);\r
-    STARTTIMER(); \r
+      STOPTIMER(Loading);\r
+      PRINTTIME("Loading text bitvector struct", Loading);\r
+      STARTTIMER();\r
+      \r
+      // Not used  \r
+      // loads the texts\r
+      if (!XML_Tree->disable_tc){\r
+       ufread(&(XML_Tree->text_index_type),\r
+              sizeof(TextCollectionBuilder::index_type_t), 1, fp);\r
+       const char * pref;\r
+       switch (!XML_Tree->text_index_type){\r
+       case TextCollectionBuilder::index_type_default:\r
+         pref = "default_";\r
+         break;\r
+       case TextCollectionBuilder::index_type_swcsa:\r
+         pref = "swcsa_";\r
+         break;\r
+       case TextCollectionBuilder::index_type_rlcsa:\r
+         pref = "rlcsa_";\r
+         break;\r
+       };      \r
+        XML_Tree->Text = TextCollection::Load(fp, pref, TextCollection::index_mode_default, sample_factor);\r
+\r
+      }\r
+      else XML_Tree->Text = NULL;\r
+      STOPTIMER(Loading);\r
+      PRINTTIME("Loading TextCollection", Loading);\r
+      STARTTIMER(); \r
     }\r
     else {\r
       XML_Tree->EBVector = NULL;\r
       XML_Tree->Text = NULL;\r
       XML_Tree->disable_tc = true;\r
     };\r
-\r
-    XML_Tree->stream = NULL;\r
-    XML_Tree->stream_fd = 0;\r
     \r
+\r
     return XML_Tree;\r
  }\r
 \r
@@ -500,12 +525,12 @@ treeNode XMLTree::NextElement(treeNode x)
   }*/\r
 \r
 // LastChild(x): returns the last child of node x.\r
-treeNode XMLTree::LastChild(treeNode x)\r
+   /*treeNode XMLTree::LastChild(treeNode x)\r
  {\r
    NULLT_IF(x == NULLT || fast_isleaf(Par,x));\r
    return find_open(Par, fast_find_close(Par, x)-1);\r
  }\r
-\r
+   */\r
 // NextSibling(x): returns the next sibling of node x, assuming it exists.\r
 /*treeNode XMLTree::NextSibling(treeNode x) \r
  {\r
@@ -516,12 +541,12 @@ treeNode XMLTree::LastChild(treeNode x)
 */\r
 \r
 // PrevSibling(x): returns the previous sibling of node x, assuming it exists.\r
-treeNode XMLTree::PrevSibling(treeNode x) \r
+/*treeNode XMLTree::PrevSibling(treeNode x) \r
  {\r
    NULLT_IF(x==NULLT);\r
    return prev_sibling(Par, x);\r
  }\r
-\r
+*/\r
 // TaggedChild(x,tag): returns the first child of node x tagged tag, or NULLT if there is none.\r
 // Because of the balanced-parentheses representation of the tree, this operation is not supported\r
 // efficiently, just iterating among the children of node x until finding the desired child.\r
@@ -801,11 +826,10 @@ DocID XMLTree::MyText(treeNode x)
   // seems faster than testing EBVector->access(x);\r
 \r
   if (tag == PCDATA_TAG_ID || tag == ATTRIBUTE_DATA_TAG_ID)\r
-    //if (EBVector->access(x))\r
-    return (DocID) (EBVector->rank1(x)-1); //-1 because document ids start from 0\r
-  else \r
+    return (DocID) (EBVector->rank1(x)-1);  \r
+  else   \r
     return (DocID) NULLT;\r
-  \r
+    \r
 }\r
 // MyText(x): returns the document identifier of the text below node x, \r
 // or NULLT if x is not a leaf node or the text is empty. Assumes Doc \r
@@ -813,6 +837,7 @@ DocID XMLTree::MyText(treeNode x)
 DocID XMLTree::MyTextUnsafe(treeNode x) \r
 {\r
   return (DocID) (EBVector->rank1(x)-1); //-1 because document ids start from 0\r
+  \r
 }\r
 // TextXMLId(d): returns the preorder of document with identifier d in the tree consisting of\r
 // all tree nodes and all text nodes. Assumes that the tree root has preorder 1.\r
@@ -901,19 +926,21 @@ bool XMLTree::IsOpen(treeNode x) { return fast_inspect(Par,x); }
 \r
 void XMLTree::Print(int fd,treeNode x, bool no_text){\r
   \r
-  if (buffer == 0)\r
-    buffer = new string();\r
-  \r
+  if (buffer == 0) { \r
+    buffer = new string(BUFFER_ALLOC, 0);\r
+    print_stack = new std::vector<string *>();\r
+    print_stack->reserve(256);\r
+  };\r
   treeNode fin = fast_find_close(Par,x);\r
   treeNode n = x;\r
   TagType tag = Tag(n);\r
-  uchar * tagstr;\r
+  \r
   range r = DocIds(x);\r
   treeNode first_idx;\r
   treeNode first_text = (tag == PCDATA_TAG_ID ?  x : ParentNode(r.min-1));\r
   treeNode first_att =  NULLT;\r
   \r
-  if (first_att  == NULLT)\r
+  if (first_att  == NULLT) \r
     first_idx = first_text;\r
   else if (first_text == NULLT)\r
     first_idx = first_att;\r
@@ -923,10 +950,9 @@ void XMLTree::Print(int fd,treeNode x, bool no_text){
    uchar * current_text=NULL;\r
 \r
    if (first_idx != NULLT)\r
-     current_text = GetText(MyText(first_idx));\r
+     current_text = GetText(MyTextUnsafe(first_idx));\r
   \r
    size_t read = 0;\r
-   std::vector<uchar*> st;\r
    while (n <= fin){\r
      if (fast_inspect(Par,n)){\r
        if (tag == PCDATA_TAG_ID) {       \r
@@ -940,14 +966,13 @@ void XMLTree::Print(int fd,treeNode x, bool no_text){
         n+=2; // skip closing $\r
         tag = Tag(n);\r
         \r
-       }\r
-       else {\r
-        _dputc('<',fd);\r
-        tagstr = (uchar*) GetTagNameByRef(tag);\r
-        _dputs((const char*) tagstr, fd);\r
+       } else {\r
+\r
+        _dputc('<',fd);         \r
+        _dput_str((*TagName)[tag], fd);\r
         n++;\r
         if (fast_inspect(Par,n)) {\r
-          st.push_back(tagstr);\r
+          print_stack->push_back(&((*TagName)[tag]));\r
           tag = Tag(n);\r
           if (tag == ATTRIBUTE_TAG_ID){\r
             n++;\r
@@ -987,17 +1012,15 @@ void XMLTree::Print(int fd,treeNode x, bool no_text){
           tag=Tag(n);   \r
         };     \r
        };\r
-     }\r
-     else\r
-       do {\r
+     } else do {\r
         _dputs("</", fd);\r
-        _dputs((const char*)st.back(), fd);\r
+        _dput_str(*(print_stack->back()), fd);\r
         _dputc('>', fd);\r
-        st.pop_back();\r
+        print_stack->pop_back();\r
         n++;\r
-       } while (!(fast_inspect(Par,n) || st.empty()));\r
+       } while (!(fast_inspect(Par,n) || print_stack->empty()));\r
      tag = Tag(n);\r
    };\r
    _dputc('\n', fd);\r
-   _flush(fd);\r
+   //_flush(fd);\r
 }\r
index 5c010a5..4673ccc 100644 (file)
--- a/XMLTree.h
+++ b/XMLTree.h
@@ -96,6 +96,8 @@ typedef TagIdMap::const_iterator TagIdMapIT;
 
 // Direct calls to sarray library
 
+#define BUFFER_ALLOC (8192 * 2)
+#define BUFFER_SIZE (BUFFER_ALLOC / 2)
 static inline int fast_find_close(bp *b,int s)
 {
   return fwd_excess(b,s,-1);
@@ -158,7 +160,7 @@ class XMLTree {
   
    /** Bit vector indicating with a 1 the positions of the non-empty texts. */
    static_bitsequence *EBVector;  
-                     
+
    /** Tag sequence represented with a data structure for rank and select */
    static_sequence *Tags;
    uint * tags_fix;
@@ -169,59 +171,65 @@ class XMLTree {
 
    // Allows to disable the TextCollection for benchmarkin purposes
    bool disable_tc;
+   SXSI::TextCollectionBuilder::index_type_t text_index_type;
    
-   FILE* stream;
-   int   stream_fd; 
-   std::string * buffer;
+   std::string *buffer;
+   std::vector<std::string *> *print_stack;
 
    void _flush(int fd){
           size_t size = buffer->size();
-          size_t written = write(fd, buffer->data(), size);
-          if (written != size)
-                  throw "Cannot flush buffer";
+          if (size < BUFFER_SIZE) return;
+          size_t written;
+          while (1) {
+                  written = write(fd, buffer->data(), size);
+                  if ((written < 0) && (errno == EAGAIN || errno == EINTR))
+                          continue;
+                  break;
+          };      
           buffer->clear();
    }
 
+   void _dput_str(std::string s, int fd){
+          buffer->append(s);
+          _flush(fd);
+   }
+
    void _dputs(const char* s, int fd){
      buffer->append(s);
-     if (buffer->size() >= 131072) _flush(fd);
-     
+     _flush(fd);     
    }
 
    void _dputc(const char c, int fd){
-     buffer->append(1,c);
-     if (buffer->size() >= 131072) _flush(fd);
+     buffer->push_back(c);
    }
 
    size_t _dprintf(const char* s, int fd){
      if (s == NULL) return 0;
-     size_t i = 0;
-     while (1) {
-            switch (s[i]) {
+     size_t i;
+     char c;
+     for (i = 0; (c = s[i]); i++) {
+            switch (c) {
+            case '"':
+                    _dputs("&quot;", fd);
+                    break;
             case '&':
-                    buffer->append("&amp;");
+                    _dputs("&amp;", fd);
                     break;
             case '\'':
-                    buffer->append("&apos;");
-                    break;
-            case '"':
-                    buffer->append("&quot;");
+                    _dputs("&apos;", fd);
                     break;
             case '<':
-                    buffer->append("&lt;");
+                    _dputs("&lt;", fd);
                     break;
             case '>':
-                    buffer->append("&gt;");
+                    _dputs("&gt;", fd);
                     break;
-            case 0:
-                    return i;
             default:
-                    buffer->append(1, s[i]);
+                    _dputc(c, fd);
 
             };
-            if (buffer->size() >= 131072) _flush(fd);
-            ++i;
      };
+     return i;
    }
 
    void PrintNode(treeNode n, int fd);
@@ -229,8 +237,13 @@ class XMLTree {
    XMLTree(){ buffer = 0;};
 
    // non const pointer are freed by this method.
-   XMLTree( pb * const par, uint npar,  std::vector<std::string> * const TN,  TagIdMap * const tim, uint *empty_texts_bmp, TagType *tags,
-          TextCollection * const TC, bool dis_tc);
+   XMLTree( pb * const par,
+           uint npar,
+           std::vector<std::string> * const TN,
+           TagIdMap * const tim, uint *empty_texts_bmp,
+           TagType *tags,
+           TextCollection * const TC, bool dis_tc,
+           TextCollectionBuilder::index_type_t _index_type );
 
 public: 
    /** Data structure destructor */
@@ -329,6 +342,16 @@ public:
     else
       return parent(Par, x);
    };
+
+   treeNode BinaryParent(treeNode x){
+          if (x <= Root())
+                  return NULLT;
+          else {
+                  treeNode prev = x - 1;
+                  return (fast_inspect(Par, prev) == OP) ? prev : find_open(Par, prev);
+          };
+   };
+
    /* Assumes x is neither 0 nor -1 */
    
    /** Child(x,i): returns the i-th child of node x, assuming it exists. */   
@@ -337,14 +360,20 @@ public:
 
 
    /** LastChild(x): returns the last child of node x.  */
-   treeNode LastChild(treeNode x);
+   treeNode LastChild(treeNode x) {
+          NULLT_IF(x == NULLT || fast_isleaf(Par,x));
+          return find_open(Par, fast_find_close(Par, x)-1);
+   }
    
-
-
    /** PrevSibling(x): returns the previous sibling of node x, assuming it 
     * exists. */
 
-   treeNode PrevSibling(treeNode x);
+   treeNode PrevSibling(treeNode x)
+   {
+          NULLT_IF(x==NULLT);
+          return prev_sibling(Par, x);
+   }
+
    
    /** TaggedChild(x,tag): returns the first child of node x tagged tag, or 
     * NULLT if there is none. Because of the balanced-parentheses representation 
@@ -577,11 +606,11 @@ public:
    }
    
    /** Save: saves XML tree data structure to file. */
-   void Save(int fd, char *filename);
+   void Save(int fd );
       
    /** Load: loads XML tree data structure from file. sample_rate_text 
     * indicates the sample rate for the text search data structure. */
-   static XMLTree *Load(int fd, char *filename, bool load_tc, int sample_factor);   
+   static XMLTree *Load(int fd, bool load_tc, int sample_factor);   
 
    void insertTag(TagType tag, uint position);
    
@@ -597,6 +626,7 @@ public:
    /** Print procedure */
    void Print(int fd,treeNode x, bool no_text);
    void Print(int fd,treeNode x) { Print(fd,x,false); }
+   void Flush(int fd){ _flush(fd); }
 
   // The following are inlined here for speed
   /** Tag(x): returns the tag identifier of node x. */
index 3115e4d..9c7e3f6 100644 (file)
@@ -10,12 +10,15 @@ XMLTreeBuilder::~XMLTreeBuilder(){
 // OpenDocument(empty_texts): it starts the construction of the data structure for\r
 // the XML document. Parameter empty_texts indicates whether we index empty texts\r
 // in document or not. Returns a non-zero value upon success, NULLT in case of error.\r
-int XMLTreeBuilder::OpenDocument(bool empty_texts, int sample_rate_text, bool dtc)\r
+int XMLTreeBuilder::OpenDocument(bool empty_texts, \r
+                                int sample_rate_text,\r
+                                bool dtc,\r
+                                TextCollectionBuilder::index_type_t index_type)\r
  {\r
     npar = 0;\r
     parArraySize = 1;\r
     disable_tc = dtc;\r
-\r
+    text_index_type = index_type;\r
     STARTTIMER();\r
    \r
     par_aux = (pb *)umalloc(sizeof(pb)*parArraySize);\r
@@ -39,7 +42,8 @@ int XMLTreeBuilder::OpenDocument(bool empty_texts, int sample_rate_text, bool dt
     if (disable_tc)\r
         TextBuilder = 0;\r
     else \r
-        TextBuilder = TextCollectionBuilder::create((unsigned)sample_rate_text, TextCollectionBuilder::index_type_default);\r
+        TextBuilder = TextCollectionBuilder::create((unsigned)sample_rate_text, index_type);\r
+\r
     Text = 0;\r
     empty_texts_aux = (unsigned int *)ucalloc(sizeof(unsigned int),1);\r
     eta_size = sizeof(unsigned int);\r
@@ -80,7 +84,8 @@ XMLTree *XMLTreeBuilder::CloseDocument()
                             empty_texts_aux,                // freed by the constructor\r
                             tags_aux,                       //freed by the constructor\r
                             Text,\r
-                            disable_tc);\r
+                            disable_tc,\r
+                            text_index_type);\r
     return T; \r
  }\r
 \r
index 055eeda..c7a9036 100644 (file)
@@ -67,7 +67,7 @@ class XMLTreeBuilder {
    int eta_size;\r
    // Allows to disable the TextCollection for benchmarkin purposes\r
    bool disable_tc;\r
-\r
+   TextCollectionBuilder::index_type_t text_index_type;\r
 public:\r
 \r
    XMLTreeBuilder() {;};\r
@@ -82,7 +82,8 @@ public:
     * (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, bool dtc);\r
+   int OpenDocument(bool empty_texts, int sample_rate_text, bool dtc,\r
+                   TextCollectionBuilder::index_type_t index_type);\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