Reduce overheads of PostList positional data support
[xapian.git] / xapian-core / backends / glass / glass_positionlist.h
blob08d67e97765b570a2ea2dfd739991dfad667fa45
1 /** @file glass_positionlist.h
2 * @brief A position list in a glass database.
3 */
4 /* Copyright (C) 2005,2006,2008,2009,2010,2011,2013,2016,2017 Olly Betts
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 of the
9 * License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
19 * USA
22 #ifndef XAPIAN_HGUARD_GLASS_POSITIONLIST_H
23 #define XAPIAN_HGUARD_GLASS_POSITIONLIST_H
25 #include <xapian/types.h>
27 #include "bitstream.h"
28 #include "glass_cursor.h"
29 #include "glass_lazytable.h"
30 #include "pack.h"
31 #include "backends/positionlist.h"
33 #include <string>
35 using namespace std;
37 class GlassPositionListTable : public GlassLazyTable {
38 public:
39 static string make_key(Xapian::docid did, const string & term) {
40 string key;
41 pack_string_preserving_sort(key, term);
42 pack_uint_preserving_sort(key, did);
43 return key;
46 /** Create a new GlassPositionListTable object.
48 * This method does not create or open the table on disk - you
49 * must call the create() or open() methods respectively!
51 * @param dbdir The directory the glass database is stored in.
52 * @param readonly true if we're opening read-only, else false.
54 GlassPositionListTable(const string & dbdir, bool readonly)
55 : GlassLazyTable("position", dbdir + "/position.", readonly) { }
57 GlassPositionListTable(int fd, off_t offset_, bool readonly_)
58 : GlassLazyTable("position", fd, offset_, readonly_) { }
60 /** Pack a position list into a string.
62 * @param s The string to append the position list data to.
64 void pack(string & s, const Xapian::VecCOW<Xapian::termpos> & vec) const;
66 /** Set the position list for term tname in document did.
68 void set_positionlist(Xapian::docid did, const string & tname,
69 const string & s) {
70 add(make_key(did, tname), s);
73 /// Delete the position list for term tname in document did.
74 void delete_positionlist(Xapian::docid did, const string & tname) {
75 del(make_key(did, tname));
78 /// Return the number of entries in specified position list.
79 Xapian::termcount positionlist_count(Xapian::docid did,
80 const string & term) const;
83 /** Base-class for a position list in a glass database. */
84 class GlassBasePositionList : public PositionList {
85 /// Copying is not allowed.
86 GlassBasePositionList(const GlassBasePositionList&) = delete;
88 /// Assignment is not allowed.
89 GlassBasePositionList& operator=(const GlassBasePositionList&) = delete;
91 protected:
92 /// Interpolative decoder.
93 BitReader rd;
95 /// Current entry.
96 Xapian::termpos current_pos;
98 /// Last entry.
99 Xapian::termpos last;
101 /// Number of entries.
102 Xapian::termcount size;
104 /// Have we started iterating yet?
105 bool have_started;
107 public:
108 /// Default constructor.
109 GlassBasePositionList() {}
111 /// Returns size of position list.
112 Xapian::termcount get_approx_size() const;
114 /** Returns current position.
116 * Either next() or skip_to() must have been called before this
117 * method can be called.
119 Xapian::termpos get_position() const;
121 /// Advance to the next term position in the list.
122 bool next();
124 /// Advance to the first term position which is at least termpos.
125 bool skip_to(Xapian::termpos termpos);
128 /** A position list in a glass database. */
129 class GlassPositionList : public GlassBasePositionList {
130 /// The encoded positional data being read by rd.
131 std::string pos_data;
133 /// Copying is not allowed.
134 GlassPositionList(const GlassPositionList&) = delete;
136 /// Assignment is not allowed.
137 GlassPositionList& operator=(const GlassPositionList&) = delete;
139 public:
140 /// Construct and initialise with data.
141 explicit
142 GlassPositionList(const string& data);
144 /// Construct and initialise with data.
145 GlassPositionList(const GlassTable* table,
146 Xapian::docid did,
147 const string& term);
150 /** A reusable position list in a glass database. */
151 class GlassRePositionList : public GlassBasePositionList {
152 /// Cursor for locating multiple entries efficiently.
153 GlassCursor cursor;
155 /// Copying is not allowed.
156 GlassRePositionList(const GlassRePositionList&) = delete;
158 /// Assignment is not allowed.
159 GlassRePositionList& operator=(const GlassRePositionList&) = delete;
161 public:
162 /// Constructor.
163 explicit
164 GlassRePositionList(const GlassTable* table)
165 : cursor(table) {}
167 /** Fill list with data, and move the position to the start. */
168 void read_data(Xapian::docid did,
169 const string& term);
172 #endif /* XAPIAN_HGUARD_GLASS_POSITIONLIST_H */