xapian-core  2.0.0
glass_version.h
Go to the documentation of this file.
1 
4 /* Copyright (C) 2006-2024 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, see
19  * <https://www.gnu.org/licenses/>.
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 {
67  return blocksize;
68  }
69  uint4 get_compress_min() const { return compress_min; }
70  const std::string & get_free_list() const { return fl_serialised; }
71 
72  void set_level(int level_) { level = unsigned(level_); }
74  void set_root_is_fake(bool f) { root_is_fake = f; }
75  void set_sequential(bool f) { sequential = f; }
76  void set_root(glass_block_t root_) { root = root_; }
77  void set_blocksize(unsigned b) {
80  blocksize = b;
81  }
82  void set_free_list(const std::string & s) { fl_serialised = s; }
83 };
84 
85 }
86 
87 using Glass::RootInfo;
88 
96 class GlassVersion {
98 
101 
104 
113  int fd;
114 
119  off_t offset;
120 
122  std::string db_dir;
123 
125 
128 
131 
134 
137 
140 
143 
146 
149 
151  std::string serialised_stats;
152 
153  // Serialise the database stats.
154  void serialise_stats();
155 
156  // Unserialise the database stats.
157  void unserialise_stats();
158 
159  public:
160  explicit GlassVersion(std::string_view db_dir_)
161  : rev(0), fd(-1), offset(0), db_dir(db_dir_), changes(NULL),
162  doccount(0), total_doclen(0), last_docid(0),
165  oldest_changeset(0) { }
166 
167  explicit GlassVersion(int fd_);
168 
169  ~GlassVersion();
170 
172  void create(unsigned blocksize);
173 
174  void set_changes(GlassChanges * changes_) { changes = changes_; }
175 
180  void read();
181 
182  void cancel();
183 
184  const std::string write(glass_revision_number_t new_rev, int flags);
185 
186  bool sync(const std::string & tmpfile,
187  glass_revision_number_t new_rev, int flags);
188 
190 
191  const RootInfo & get_root(Glass::table_type tbl) const {
192  return root[tbl];
193  }
194 
196  return &root[tbl];
197  }
198 
200  const char * get_uuid() const {
201  return uuid.data();
202  }
203 
205  std::string get_uuid_string() const {
206  return uuid.to_string();
207  }
208 
210 
212 
214 
216  return doclen_lbound;
217  }
218 
220  return doclen_ubound;
221  }
222 
224 
227  }
228 
230  return oldest_changeset;
231  }
232 
234  if (total_doclen == 0) return 0;
235  Assert(doclen_lbound != 0);
236  Assert(wdf_ubound != 0);
237  return (doclen_lbound - 1) / wdf_ubound + 1;
238  }
239 
241 
243  oldest_changeset = changeset;
244  }
245 
248  }
249 
251  ++doccount;
253  doclen_ubound = std::max(doclen_ubound, doclen);
254  total_doclen += doclen;
255  }
256 
258  --doccount;
259  total_doclen -= doclen;
260  // If the database no longer contains any postings, we can reset
261  // doclen_lbound, doclen_ubound and wdf_ubound.
262  if (total_doclen == 0) {
263  doclen_lbound = 0;
264  doclen_ubound = 0;
265  wdf_ubound = 0;
266  }
267  }
268 
270  if (wdf > wdf_ubound) wdf_ubound = wdf;
271  }
272 
274 
279  void merge_stats(const GlassVersion & o);
280 
281  bool single_file() const { return db_dir.empty(); }
282 
283  off_t get_offset() const { return offset; }
284 };
285 
286 #endif // XAPIAN_INCLUDED_GLASS_VERSION_H
The GlassVersion class manages the revision files.
Definition: glass_version.h:96
glass_revision_number_t oldest_changeset
Oldest changeset removed when max_changesets is set.
RootInfo * root_to_set(Glass::table_type tbl)
void set_oldest_changeset(glass_revision_number_t changeset) const
Xapian::termcount spelling_wordfreq_ubound
An upper bound on the spelling wordfreq in this database.
const RootInfo & get_root(Glass::table_type tbl) const
void check_wdf(Xapian::termcount wdf)
RootInfo old_root[Glass::MAX_]
Xapian::termcount doclen_ubound
An upper bound on the greatest document length in this database.
off_t offset
Offset into the file at which the version data starts.
void add_document(Xapian::termcount doclen)
const std::string write(glass_revision_number_t new_rev, int flags)
Xapian::docid get_last_docid() const
std::string db_dir
The database directory.
glass_revision_number_t get_revision() const
void delete_document(Xapian::termcount doclen)
void serialise_stats()
Xapian::termcount get_doclength_lower_bound() const
std::string get_uuid_string() const
Return UUID in the standard 36 character string format.
bool single_file() const
void set_spelling_wordfreq_upper_bound(Xapian::termcount ub)
void set_last_docid(Xapian::docid did)
glass_revision_number_t get_oldest_changeset() const
off_t get_offset() const
Xapian::docid last_docid
Greatest document id ever used in this database.
Xapian::termcount get_spelling_wordfreq_upper_bound() const
std::string serialised_stats
The serialised database stats.
Uuid uuid
The UUID of this database.
glass_revision_number_t rev
Definition: glass_version.h:97
Xapian::totallength total_doclen
The total of the lengths of all documents in the database.
const char * get_uuid() const
Return pointer to 16 byte UUID.
bool sync(const std::string &tmpfile, glass_revision_number_t new_rev, int flags)
Xapian::doccount get_doccount() const
void set_changes(GlassChanges *changes_)
void merge_stats(const GlassVersion &o)
Merge the database stats.
Xapian::termcount get_wdf_upper_bound() const
GlassVersion(std::string_view db_dir_)
Xapian::termcount doclen_lbound
A lower bound on the smallest document length in this database.
GlassChanges * changes
Xapian::docid get_next_docid()
Xapian::termcount wdf_ubound
An upper bound on the greatest wdf in this database.
Xapian::doccount doccount
The number of documents in the database.
int fd
File descriptor.
void create(unsigned blocksize)
Create the version file.
RootInfo root[Glass::MAX_]
Definition: glass_version.h:99
void unserialise_stats()
void read()
Read the version file and check it's a version we understand.
Xapian::totallength get_total_doclen() const
Xapian::termcount get_unique_terms_lower_bound() const
Xapian::termcount get_doclength_upper_bound() const
void set_num_entries(glass_tablesize_t n)
Definition: glass_version.h:73
const std::string & get_free_list() const
Definition: glass_version.h:70
void init(unsigned blocksize_, uint4 compress_min_)
void serialise(std::string &s) const
bool unserialise(const char **p, const char *end)
glass_tablesize_t get_num_entries() const
Definition: glass_version.h:61
void set_blocksize(unsigned b)
Definition: glass_version.h:77
std::string fl_serialised
Definition: glass_version.h:50
bool get_sequential() const
Definition: glass_version.h:63
unsigned blocksize
Definition: glass_version.h:47
void set_level(int level_)
Definition: glass_version.h:72
unsigned get_blocksize() const
Definition: glass_version.h:64
void set_sequential(bool f)
Definition: glass_version.h:75
void set_free_list(const std::string &s)
Definition: glass_version.h:82
int get_level() const
Definition: glass_version.h:60
glass_tablesize_t num_entries
Definition: glass_version.h:44
glass_block_t root
Definition: glass_version.h:42
uint4 get_compress_min() const
Definition: glass_version.h:69
void set_root_is_fake(bool f)
Definition: glass_version.h:74
uint4 compress_min
Should be >= 4 or 0 for no compression.
Definition: glass_version.h:49
glass_block_t get_root() const
Definition: glass_version.h:59
bool get_root_is_fake() const
Definition: glass_version.h:62
void set_root(glass_block_t root_)
Definition: glass_version.h:76
Definition: uuids.h:28
std::string to_string() const
Definition: uuids.cc:120
const char * data() const
Definition: uuids.h:60
PositionList * p
Glass changesets.
Definitions, types, etc for use inside glass.
unsigned long long glass_tablesize_t
How many entries there are in a table.
Definition: glass_defs.h:71
#define GLASS_MIN_BLOCKSIZE
Minimum B-tree block size.
Definition: glass_defs.h:33
uint4 glass_block_t
A block number in a glass Btree file.
Definition: glass_defs.h:65
uint4 glass_revision_number_t
The revision number of a glass database.
Definition: glass_defs.h:68
#define GLASS_MAX_BLOCKSIZE
Maximum B-tree block size.
Definition: glass_defs.h:36
Types used internally.
uint32_t uint4
Definition: internaltypes.h:31
Return the smaller of two numbers which isn't zero.
constexpr std::enable_if_t< std::is_unsigned_v< T >, T > min_non_zero(const T &a, const T &b)
Return the smaller of two unsigned integers which isn't zero.
Definition: min_non_zero.h:39
table_type
Definition: glass_defs.h:53
@ MAX_
Definition: glass_defs.h:60
unsigned XAPIAN_TERMCOUNT_BASE_TYPE termcount
A counts of terms.
Definition: types.h:64
unsigned XAPIAN_DOCID_BASE_TYPE doccount
A count of documents.
Definition: types.h:37
unsigned XAPIAN_DOCID_BASE_TYPE docid
A unique identifier for a document.
Definition: types.h:51
XAPIAN_TOTALLENGTH_TYPE totallength
The total length of all documents in a database.
Definition: types.h:114
Various assertion macros.
#define AssertRel(A, REL, B)
Definition: omassert.h:123
#define Assert(COND)
Definition: omassert.h:122
typedefs for Xapian
Class for handling UUIDs.