xapian-core  1.4.19
glass_version.h
Go to the documentation of this file.
1 
4 /* Copyright (C) 2006,2007,2008,2009,2010,2013,2014,2015,2016,2018 Olly Betts
5  * Copyright (C) 2011 Dan Colish
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21 
22 #ifndef XAPIAN_INCLUDED_GLASS_VERSION_H
23 #define XAPIAN_INCLUDED_GLASS_VERSION_H
24 
25 #include "glass_changes.h"
26 #include "glass_defs.h"
27 
28 #include "omassert.h"
29 
30 #include <algorithm>
31 #include <cstring>
32 #include <string>
33 
34 #include "backends/uuids.h"
35 #include "internaltypes.h"
36 #include "min_non_zero.h"
37 #include "xapian/types.h"
38 
39 namespace Glass {
40 
41 class RootInfo {
43  unsigned level;
46  bool sequential;
47  unsigned blocksize;
50  std::string fl_serialised;
51 
52  public:
53  void init(unsigned blocksize_, uint4 compress_min_);
54 
55  void serialise(std::string &s) const;
56 
57  bool unserialise(const char ** p, const char * end);
58 
59  glass_block_t get_root() const { return root; }
60  int get_level() const { return int(level); }
62  bool get_root_is_fake() const { return root_is_fake; }
63  bool get_sequential() const { return sequential; }
64  unsigned get_blocksize() const {
65  AssertRel(blocksize,>=,2048);
66  return blocksize;
67  }
68  uint4 get_compress_min() const { return compress_min; }
69  const std::string & get_free_list() const { return fl_serialised; }
70 
71  void set_level(int level_) { level = unsigned(level_); }
72  void set_num_entries(glass_tablesize_t n) { num_entries = n; }
73  void set_root_is_fake(bool f) { root_is_fake = f; }
74  void set_sequential(bool f) { sequential = f; }
75  void set_root(glass_block_t root_) { root = root_; }
76  void set_blocksize(unsigned b) {
77  AssertRel(b,>=,2048);
78  blocksize = b;
79  }
80  void set_free_list(const std::string & s) { fl_serialised = s; }
81 };
82 
83 }
84 
85 using Glass::RootInfo;
86 
94 class GlassVersion {
96 
98  RootInfo old_root[Glass::MAX_];
99 
102 
111  int fd;
112 
117  off_t offset;
118 
120  std::string db_dir;
121 
123 
126 
129 
132 
135 
138 
141 
144 
147 
149  std::string serialised_stats;
150 
151  // Serialise the database stats.
152  void serialise_stats();
153 
154  // Unserialise the database stats.
155  void unserialise_stats();
156 
157  public:
158  explicit GlassVersion(const std::string & db_dir_)
159  : rev(0), fd(-1), offset(0), db_dir(db_dir_), changes(NULL),
160  doccount(0), total_doclen(0), last_docid(0),
161  doclen_lbound(0), doclen_ubound(0),
162  wdf_ubound(0), spelling_wordfreq_ubound(0),
163  oldest_changeset(0) { }
164 
165  explicit GlassVersion(int fd_);
166 
167  ~GlassVersion();
168 
170  void create(unsigned blocksize);
171 
172  void set_changes(GlassChanges * changes_) { changes = changes_; }
173 
178  void read();
179 
180  void cancel();
181 
182  const std::string write(glass_revision_number_t new_rev, int flags);
183 
184  bool sync(const std::string & tmpfile,
185  glass_revision_number_t new_rev, int flags);
186 
188 
189  const RootInfo & get_root(Glass::table_type tbl) const {
190  return root[tbl];
191  }
192 
194  return &root[tbl];
195  }
196 
198  const char * get_uuid() const {
199  return uuid.data();
200  }
201 
203  std::string get_uuid_string() const {
204  return uuid.to_string();
205  }
206 
208 
209  Xapian::totallength get_total_doclen() const { return total_doclen; }
210 
211  Xapian::docid get_last_docid() const { return last_docid; }
212 
214  return doclen_lbound;
215  }
216 
218  return doclen_ubound;
219  }
220 
221  Xapian::termcount get_wdf_upper_bound() const { return wdf_ubound; }
222 
224  return spelling_wordfreq_ubound;
225  }
226 
228  return oldest_changeset;
229  }
230 
231  void set_last_docid(Xapian::docid did) { last_docid = did; }
232 
234  oldest_changeset = changeset;
235  }
236 
238  spelling_wordfreq_ubound = ub;
239  }
240 
242  ++doccount;
243  doclen_lbound = min_non_zero(doclen_lbound, doclen);
244  doclen_ubound = std::max(doclen_ubound, doclen);
245  total_doclen += doclen;
246  }
247 
249  --doccount;
250  total_doclen -= doclen;
251  // If the database no longer contains any postings, we can reset
252  // doclen_lbound, doclen_ubound and wdf_ubound.
253  if (total_doclen == 0) {
254  doclen_lbound = 0;
255  doclen_ubound = 0;
256  wdf_ubound = 0;
257  }
258  }
259 
261  if (wdf > wdf_ubound) wdf_ubound = wdf;
262  }
263 
264  Xapian::docid get_next_docid() { return ++last_docid; }
265 
270  void merge_stats(const GlassVersion & o);
271 
272  bool single_file() const { return db_dir.empty(); }
273 
274  off_t get_offset() const { return offset; }
275 };
276 
277 #endif // XAPIAN_INCLUDED_GLASS_VERSION_H
const RootInfo & get_root(Glass::table_type tbl) const
uint4 get_compress_min() const
Definition: glass_version.h:68
Xapian::termcount get_doclength_upper_bound() const
Xapian::docid get_next_docid()
typedefs for Xapian
bool get_sequential() const
Definition: glass_version.h:63
glass_revision_number_t oldest_changeset
Oldest changeset removed when max_changesets is set.
XAPIAN_REVISION_TYPE rev
Revision number of a database.
Definition: types.h:133
table_type
Definition: glass_defs.h:46
Xapian::termcount wdf_ubound
An upper bound on the greatest wdf in this database.
off_t offset
Offset into the file at which the version data starts.
#define AssertRel(A, REL, B)
Definition: omassert.h:123
Uuid uuid
The UUID of this database.
uint4 glass_revision_number_t
The revision number of a glass database.
Definition: glass_defs.h:61
XAPIAN_TOTALLENGTH_TYPE totallength
The total length of all documents in a database.
Definition: types.h:139
glass_tablesize_t num_entries
Definition: glass_version.h:44
void set_blocksize(unsigned b)
Definition: glass_version.h:76
The GlassVersion class manages the revision files.
Definition: glass_version.h:94
GlassVersion(const std::string &db_dir_)
glass_revision_number_t rev
Definition: glass_version.h:95
GlassChanges * changes
Definitions, types, etc for use inside glass.
off_t get_offset() const
void check_wdf(Xapian::termcount wdf)
void set_level(int level_)
Definition: glass_version.h:71
unsigned long long glass_tablesize_t
How many entries there are in a table.
Definition: glass_defs.h:64
Xapian::docid get_last_docid() const
unsigned XAPIAN_TERMCOUNT_BASE_TYPE termcount
A counts of terms.
Definition: types.h:72
unsigned blocksize
Definition: glass_version.h:47
void set_num_entries(glass_tablesize_t n)
Definition: glass_version.h:72
Xapian::termcount get_wdf_upper_bound() const
Xapian::totallength total_doclen
The total of the lengths of all documents in the database.
RootInfo * root_to_set(Glass::table_type tbl)
uint4 compress_min
Should be >= 4 or 0 for no compression.
Definition: glass_version.h:49
uint32_t uint4
Definition: internaltypes.h:32
bool unserialise(const char **p, const char *end)
void set_changes(GlassChanges *changes_)
Glass changesets.
int fd
File descriptor.
void add_document(Xapian::termcount doclen)
Xapian::termcount get_doclength_lower_bound() const
std::string to_string() const
Definition: uuids.cc:120
glass_tablesize_t get_num_entries() const
Definition: glass_version.h:61
bool get_root_is_fake() const
Definition: glass_version.h:62
void set_oldest_changeset(glass_revision_number_t changeset) const
std::string serialised_stats
The serialised database stats.
void delete_document(Xapian::termcount doclen)
const char * get_uuid() const
Return pointer to 16 byte UUID.
string serialise_stats(const Xapian::Weight::Internal &stats)
Serialise a stats object.
Definition: serialise.cc:42
void set_spelling_wordfreq_upper_bound(Xapian::termcount ub)
void init(unsigned blocksize_, uint4 compress_min_)
glass_block_t root
Definition: glass_version.h:42
void unserialise_stats(const char *p, const char *p_end, Xapian::Weight::Internal &stat)
Unserialise a serialised stats object.
Definition: serialise.cc:71
unsigned get_blocksize() const
Definition: glass_version.h:64
unsigned XAPIAN_DOCID_BASE_TYPE doccount
A count of documents.
Definition: types.h:38
glass_revision_number_t get_oldest_changeset() const
glass_block_t get_root() const
Definition: glass_version.h:59
Xapian::termcount spelling_wordfreq_ubound
An upper bound on the spelling wordfreq in this database.
Xapian::totallength get_total_doclen() const
Xapian::termcount get_spelling_wordfreq_upper_bound() const
void set_root_is_fake(bool f)
Definition: glass_version.h:73
Definition: uuids.h:28
Class for handling UUIDs.
Xapian::termcount doclen_ubound
An upper bound on the greatest document length in this database.
bool single_file() const
Xapian::docid last_docid
Greatest document id ever used in this database.
Various assertion macros.
constexpr std::enable_if< std::is_unsigned< T >::value, T >::type min_non_zero(const T &a, const T &b)
Return the smaller of two unsigned integers which isn&#39;t zero.
Definition: min_non_zero.h:36
const char * data() const
Definition: uuids.h:60
Xapian::termcount doclen_lbound
A lower bound on the smallest document length in this database.
unsigned XAPIAN_DOCID_BASE_TYPE docid
A unique identifier for a document.
Definition: types.h:52
glass_revision_number_t get_revision() const
void set_root(glass_block_t root_)
Definition: glass_version.h:75
void set_sequential(bool f)
Definition: glass_version.h:74
void serialise(std::string &s) const
std::string fl_serialised
Definition: glass_version.h:50
std::string db_dir
The database directory.
std::string get_uuid_string() const
Return UUID in the standard 36 character string format.
const std::string & get_free_list() const
Definition: glass_version.h:69
void set_last_docid(Xapian::docid did)
void set_free_list(const std::string &s)
Definition: glass_version.h:80
int get_level() const
Definition: glass_version.h:60
uint4 glass_block_t
A block number in a glass Btree file.
Definition: glass_defs.h:58
Types used internally.
Xapian::doccount doccount
The number of documents in the database.
Return the smaller of two numbers which isn&#39;t zero.
Xapian::doccount get_doccount() const