Added TextCollection::Save() and TextCollection::Load() functionality
[SXSI/TextCollection.git] / TextCollection.h
1 /******************************************************************************
2  *   Copyright (C) 2008 by Niko Valimaki <nvalimak@cs.helsinki.fi>            *
3  *   Text collection interface for an in-memory XQuery/XPath engine           *
4  *                                                                            *
5  *   This program is free software; you can redistribute it and/or modify     *
6  *   it under the terms of the GNU Lesser General Public License as published *
7  *   by the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) any later version.                                      *
9  *                                                                            *
10  *   This program is distributed in the hope that it will be useful,          *
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of           *
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the            *
13  *   GNU Lesser General Public License for more details.                      *
14  *                                                                            *
15  *   You should have received a copy of the GNU Lesser General Public License *
16  *   along with this program; if not, write to the                            *
17  *   Free Software Foundation, Inc.,                                          *
18  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.                *
19  ******************************************************************************/ 
20
21 #ifndef _SXSI_TextCollection_h_
22 #define _SXSI_TextCollection_h_
23
24 #include "Tools.h" // Defines ulong and uchar.
25 #include <vector>
26 #include <utility> // Defines std::pair.
27
28 // Default samplerate for suffix array samples
29 #define TEXTCOLLECTION_DEFAULT_SAMPLERATE 64
30
31 namespace SXSI
32 {
33     /**
34      * General interface for a text collection
35      *
36      * Class is virtual, make objects by calling 
37      * the static method InitTextCollection().
38      */
39     class TextCollection
40     {
41     public:
42         // Type of document identifier
43         typedef int DocId;
44         // Type for text position (FIXME ulong or long?)
45         typedef ulong TextPosition;
46
47         /**
48          * Init an instance of a text collection object
49          *
50          * Returns a pointer to an object implementing this interface.
51          */
52         static TextCollection * InitTextCollection(unsigned samplerate = TEXTCOLLECTION_DEFAULT_SAMPLERATE);
53         /**
54          * Load from a file
55          *
56          * New samplerate can be given, otherwise will use the one specified in the save file!
57          * Note: This is not a static method; call InitTextCollection() first to get the object handle.
58          *
59          * Throws an exception if std::fread() fails.
60          * 
61          */
62         virtual void Load(FILE *, unsigned samplerate = 0) = 0;
63         /**
64          * Save data structure into a file
65          * 
66          * Throws an exception if std::fwrite() fails.
67          */
68         virtual void Save(FILE *) const = 0;
69         /**
70          * Virtual destructor
71          */
72         virtual ~TextCollection() { };
73         /** 
74          * Insert text
75          *
76          * Must be a zero-terminated string from alphabet [1,255].
77          * Can not be called after makeStatic().
78          * The i'th text insertion gets an identifier value i-1.
79          * In other words, document identifiers start from 0.
80          */
81         virtual void InsertText(uchar const *) = 0;
82         /**
83          * Make static
84          *
85          * Convert to a static collection; reduces space and time complexities.
86          * New texts can not be inserted after this operation.
87          */
88         virtual void MakeStatic() = 0;
89         
90         /**
91          * Displaying content
92          *
93          * Returns the i'th text in the collection.
94          * The numbering starts from 0.
95          */
96         virtual uchar* GetText(DocId) const = 0;
97         /**
98          * Returns substring [i, j] of k'th text
99          *
100          * Note: Parameters i and j are text positions inside the k'th text.
101          */
102         virtual uchar* GetText(DocId, TextPosition, TextPosition) const = 0;
103         /**
104          * Returns backwards (reverse) iterator to the end of i'th text
105          * 
106          * Note: Do we need this?
107          * Forward iterator would be really in-efficient compared to
108          * getText(k) and getText(k, i, j).
109          *
110          * TODO Define and implement const_reverse_iterator.
111          */
112         //const_reverse_iterator rend(DocId) const;
113         
114         /**
115          * Existential queries
116          */
117         // Is there a text prefixed by given string?
118         virtual bool IsPrefix(uchar const *) const = 0;
119         // Is there a text having given string as a suffix?
120         virtual bool IsSuffix(uchar const *) const = 0;
121         // Is there a text that equals given string?
122         virtual bool IsEqual(uchar const *) const = 0;
123         // Does a text contain given string?
124         virtual bool IsContains(uchar const *) const = 0;
125         // Is there a text that is lexicographically less than given string?
126         virtual bool IsLessThan(uchar const *) const = 0;
127
128         /**
129          * Counting queries 
130          * 
131          * Result is the number of documents.
132          */
133         virtual unsigned CountPrefix(uchar const *) const = 0;
134         virtual unsigned CountSuffix(uchar const *) const = 0;
135         virtual unsigned CountEqual(uchar const *) const = 0;
136         virtual unsigned CountContains(uchar const *) const = 0;
137         virtual unsigned CountLessThan(uchar const *) const = 0;
138
139         /**
140          * Document reporting queries
141          *
142          * Result is a vector of document id's in some undefined order.
143          */
144         // Data type for results
145         typedef std::vector<DocId> document_result;
146         virtual document_result Prefix(uchar const *) const = 0;
147         virtual document_result Suffix(uchar const *) const = 0;
148         virtual document_result Equal(uchar const *) const = 0;
149         virtual document_result Contains(uchar const *) const = 0;
150         virtual document_result LessThan(uchar const *) const = 0;
151
152         /**
153          * Full reporting queries
154          *
155          * Result is a vector of pairs <doc id, offset> in some undefined order.
156          */
157         // Data type for results
158         typedef std::vector<std::pair<DocId, TextPosition> > full_result;
159         virtual full_result FullContains(uchar const *) const = 0;
160
161     protected:
162         // Protected constructor; call the static function InitTextCollection().
163         TextCollection() { };
164
165         // No copy constructor or assignment
166         TextCollection(TextCollection const&);
167         TextCollection& operator = (TextCollection const&);
168     };
169 }
170 #endif