\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
\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
\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
}\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
\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
\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
}*/\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
*/\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
// 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
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
\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
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
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
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
// 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);
/** 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;
// 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(""", fd);
+ break;
case '&':
- buffer->append("&");
+ _dputs("&", fd);
break;
case '\'':
- buffer->append("'");
- break;
- case '"':
- buffer->append(""");
+ _dputs("'", fd);
break;
case '<':
- buffer->append("<");
+ _dputs("<", fd);
break;
case '>':
- buffer->append(">");
+ _dputs(">", 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);
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 */
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. */
/** 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
}
/** 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);
/** 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. */