1 /** @file honey_version.cc
2 * @brief HoneyVersion class
4 /* Copyright (C) 2006,2007,2008,2009,2010,2013,2014,2015,2016,2017,2018 Olly Betts
5 * Copyright (C) 2011 Dan Colish
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.
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.
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
24 #include "honey_version.h"
28 #include "honey_defs.h"
32 #include "posixy_wrapper.h"
33 #include "stringutils.h" // For STRINGIZE() and CONST_STRLEN().
35 #include <cstring> // For memcmp().
37 #include "safeerrno.h"
38 #include <sys/types.h>
39 #include "safesysstat.h"
40 #include "safefcntl.h"
41 #include "safeunistd.h"
43 #include "stringutils.h"
45 #include "common/safeuuid.h"
47 #include "xapian/constants.h"
48 #include "xapian/error.h"
52 /// Honey format version (date of change):
53 #define HONEY_FORMAT_VERSION DATE_TO_VERSION(2018,1,31)
54 // 2018,1,31 1.5.0 Special case postlist when termfreq==2
55 // 2018,1,30 More compact postlist chunk headers
56 // 2018,1,23 Elide last-first for single occurrence terms
57 // 2018,1,4 Merge values used and terms used
58 // 2018,1,3 Table start offset in RootInfo
59 // 2017,12,30 Value stats key changes
60 // 2017,12,29 User metadata key changes
61 // 2017,12,5 New Honey backend
63 /// Convert date <-> version number. Dates up to 2141-12-31 fit in 2 bytes.
64 #define DATE_TO_VERSION(Y,M,D) \
65 ((unsigned(Y) - 2014) << 9 | unsigned(M) << 5 | unsigned(D))
66 #define VERSION_TO_YEAR(V) ((unsigned(V) >> 9) + 2014)
67 #define VERSION_TO_MONTH(V) ((unsigned(V) >> 5) & 0x0f)
68 #define VERSION_TO_DAY(V) (unsigned(V) & 0x1f)
70 #define HONEY_VERSION_MAGIC_LEN 14
71 #define HONEY_VERSION_MAGIC_AND_VERSION_LEN 16
73 static const char HONEY_VERSION_MAGIC
[HONEY_VERSION_MAGIC_AND_VERSION_LEN
] = {
74 '\x0f', '\x0d', 'X', 'a', 'p', 'i', 'a', 'n', ' ', 'H', 'o', 'n', 'e', 'y',
75 char((HONEY_FORMAT_VERSION
>> 8) & 0xff), char(HONEY_FORMAT_VERSION
& 0xff)
78 HoneyVersion::HoneyVersion(int fd_
)
79 : rev(0), fd(fd_
), offset(0), db_dir(), changes(NULL
),
80 doccount(0), total_doclen(0), last_docid(0),
81 doclen_lbound(0), doclen_ubound(0),
82 wdf_ubound(0), spelling_wordfreq_ubound(0),
85 offset
= lseek(fd
, 0, SEEK_CUR
);
86 if (rare(offset
== off_t(-1))) {
87 string msg
= "lseek failed on file descriptor ";
89 throw Xapian::DatabaseOpeningError(msg
, errno
);
93 HoneyVersion::~HoneyVersion()
95 // Either this is a single-file database, or this fd is from opening a new
96 // version file in write(), but sync() was never called.
104 LOGCALL_VOID(DB
, "HoneyVersion::read", NO_ARGS
);
108 if (rare(lseek(fd
, offset
, SEEK_SET
) == off_t(-1))) {
109 string msg
= "Failed to rewind file descriptor ";
111 throw Xapian::DatabaseOpeningError(msg
, errno
);
115 string filename
= db_dir
;
116 filename
+= "/iamhoney";
117 fd_in
= posixy_open(filename
.c_str(), O_RDONLY
|O_BINARY
);
118 if (rare(fd_in
< 0)) {
119 string msg
= filename
;
120 msg
+= ": Failed to open honey revision file for reading";
121 throw Xapian::DatabaseOpeningError(msg
, errno
);
128 const char * p
= buf
;
129 const char * end
= p
+ io_read(fd_in
, buf
, sizeof(buf
), 33);
131 if (memcmp(buf
, HONEY_VERSION_MAGIC
, HONEY_VERSION_MAGIC_LEN
) != 0)
132 throw Xapian::DatabaseCorruptError("Rev file magic incorrect");
135 version
= static_cast<unsigned char>(buf
[HONEY_VERSION_MAGIC_LEN
]);
137 version
|= static_cast<unsigned char>(buf
[HONEY_VERSION_MAGIC_LEN
+ 1]);
138 if (version
!= HONEY_FORMAT_VERSION
) {
140 if (!single_file()) {
144 msg
+= "Database is format version ";
145 msg
+= str(VERSION_TO_YEAR(version
) * 10000 +
146 VERSION_TO_MONTH(version
) * 100 +
147 VERSION_TO_DAY(version
));
148 msg
+= " but I only understand ";
149 msg
+= str(VERSION_TO_YEAR(HONEY_FORMAT_VERSION
) * 10000 +
150 VERSION_TO_MONTH(HONEY_FORMAT_VERSION
) * 100 +
151 VERSION_TO_DAY(HONEY_FORMAT_VERSION
));
152 throw Xapian::DatabaseVersionError(msg
);
155 p
+= HONEY_VERSION_MAGIC_AND_VERSION_LEN
;
159 if (!unpack_uint(&p
, end
, &rev
))
160 throw Xapian::DatabaseCorruptError("Rev file failed to decode revision");
162 for (unsigned table_no
= 0; table_no
< Honey::MAX_
; ++table_no
) {
163 if (!root
[table_no
].unserialise(&p
, end
)) {
164 throw Xapian::DatabaseCorruptError("Rev file root_info missing");
166 old_root
[table_no
] = root
[table_no
];
169 // For a single-file database, this will assign extra data. We read
170 // sizeof(buf) above, then skip HONEY_VERSION_MAGIC_AND_VERSION_LEN,
171 // then 16, then the size of the serialised root info.
172 serialised_stats
.assign(p
, end
);
177 HoneyVersion::serialise_stats()
179 serialised_stats
.resize(0);
180 pack_uint(serialised_stats
, doccount
);
181 // last_docid must always be >= doccount.
182 pack_uint(serialised_stats
, last_docid
- doccount
);
183 pack_uint(serialised_stats
, doclen_lbound
);
184 pack_uint(serialised_stats
, wdf_ubound
);
185 // doclen_ubound should always be >= wdf_ubound, so we store the
186 // difference as it may encode smaller. wdf_ubound is likely to
187 // be larger than doclen_lbound.
188 pack_uint(serialised_stats
, doclen_ubound
- wdf_ubound
);
189 pack_uint(serialised_stats
, oldest_changeset
);
190 pack_uint(serialised_stats
, total_doclen
);
191 pack_uint(serialised_stats
, spelling_wordfreq_ubound
);
195 HoneyVersion::unserialise_stats()
197 const char * p
= serialised_stats
.data();
198 const char * end
= p
+ serialised_stats
.size();
206 oldest_changeset
= 0;
207 spelling_wordfreq_ubound
= 0;
211 if (!unpack_uint(&p
, end
, &doccount
) ||
212 !unpack_uint(&p
, end
, &last_docid
) ||
213 !unpack_uint(&p
, end
, &doclen_lbound
) ||
214 !unpack_uint(&p
, end
, &wdf_ubound
) ||
215 !unpack_uint(&p
, end
, &doclen_ubound
) ||
216 !unpack_uint(&p
, end
, &oldest_changeset
) ||
217 !unpack_uint(&p
, end
, &total_doclen
) ||
218 !unpack_uint(&p
, end
, &spelling_wordfreq_ubound
)) {
220 "Bad serialised DB stats (overflowed)" :
221 "Bad serialised DB stats (out of data)";
222 throw Xapian::DatabaseCorruptError(m
);
225 // In the single-file DB case, there will be extra data in
226 // serialised_stats, so suppress this check.
227 if (p
!= end
&& !single_file())
228 throw Xapian::DatabaseCorruptError("Rev file has junk at end");
230 // last_docid must always be >= doccount.
231 last_docid
+= doccount
;
232 // doclen_ubound should always be >= wdf_ubound, so we store the
233 // difference as it may encode smaller. wdf_ubound is likely to
234 // be larger than doclen_lbound.
235 doclen_ubound
+= wdf_ubound
;
239 HoneyVersion::merge_stats(const HoneyVersion
& o
)
241 doccount
+= o
.get_doccount();
242 if (doccount
< o
.get_doccount()) {
243 throw Xapian::DatabaseError("doccount overflowed!");
246 Xapian::termcount o_doclen_lbound
= o
.get_doclength_lower_bound();
247 if (o_doclen_lbound
> 0) {
248 if (doclen_lbound
== 0 || o_doclen_lbound
< doclen_lbound
)
249 doclen_lbound
= o_doclen_lbound
;
252 doclen_ubound
= max(doclen_ubound
, o
.get_doclength_upper_bound());
253 wdf_ubound
= max(wdf_ubound
, o
.get_wdf_upper_bound());
254 total_doclen
+= o
.get_total_doclen();
255 if (total_doclen
< o
.get_total_doclen()) {
256 throw Xapian::DatabaseError("Total document length overflowed!");
259 // The upper bounds might be on the same word, so we must sum them.
260 spelling_wordfreq_ubound
+= o
.get_spelling_wordfreq_upper_bound();
264 HoneyVersion::merge_stats(Xapian::doccount o_doccount
,
265 Xapian::termcount o_doclen_lbound
,
266 Xapian::termcount o_doclen_ubound
,
267 Xapian::termcount o_wdf_ubound
,
268 Xapian::totallength o_total_doclen
,
269 Xapian::termcount o_spelling_wordfreq_ubound
)
271 doccount
+= o_doccount
;
272 if (doccount
< o_doccount
) {
273 throw Xapian::DatabaseError("doccount overflowed!");
276 if (o_doclen_lbound
> 0) {
277 if (doclen_lbound
== 0 || o_doclen_lbound
< doclen_lbound
)
278 doclen_lbound
= o_doclen_lbound
;
281 doclen_ubound
= max(doclen_ubound
, o_doclen_ubound
);
282 wdf_ubound
= max(wdf_ubound
, o_wdf_ubound
);
283 total_doclen
+= o_total_doclen
;
284 if (total_doclen
< o_total_doclen
) {
285 throw Xapian::DatabaseError("Total document length overflowed!");
288 // The upper bounds might be on the same word, so we must sum them.
289 spelling_wordfreq_ubound
+= o_spelling_wordfreq_ubound
;
293 HoneyVersion::cancel()
295 LOGCALL_VOID(DB
, "HoneyVersion::cancel", NO_ARGS
);
296 for (unsigned table_no
= 0; table_no
< Honey::MAX_
; ++table_no
) {
297 root
[table_no
] = old_root
[table_no
];
303 HoneyVersion::write(honey_revision_number_t new_rev
, int flags
)
305 LOGCALL(DB
, const string
, "HoneyVersion::write", new_rev
|flags
);
307 string
s(HONEY_VERSION_MAGIC
, HONEY_VERSION_MAGIC_AND_VERSION_LEN
);
308 s
.append(reinterpret_cast<const char *>(uuid
), 16);
310 pack_uint(s
, new_rev
);
312 for (unsigned table_no
= 0; table_no
< Honey::MAX_
; ++table_no
) {
313 root
[table_no
].serialise(s
);
316 // Serialise database statistics.
318 s
+= serialised_stats
;
321 if (!single_file()) {
323 // In dangerous mode, just write the new version file in place.
324 if (flags
& Xapian::DB_DANGEROUS
)
325 tmpfile
+= "/iamhoney";
329 fd
= posixy_open(tmpfile
.c_str(), O_CREAT
|O_TRUNC
|O_WRONLY
|O_BINARY
, 0666);
331 throw Xapian::DatabaseOpeningError("Couldn't write new rev file: " + tmpfile
,
334 if (flags
& Xapian::DB_DANGEROUS
)
339 io_write(fd
, s
.data(), s
.size());
348 changes_buf
+= '\xfe';
349 pack_uint(changes_buf
, new_rev
);
350 pack_uint(changes_buf
, s
.size());
351 changes
->write_block(changes_buf
);
352 changes
->write_block(s
);
359 HoneyVersion::sync(const string
& tmpfile
,
360 honey_revision_number_t new_rev
, int flags
)
362 Assert(new_rev
> rev
|| rev
== 0);
365 if ((flags
& Xapian::DB_NO_SYNC
) == 0 &&
366 ((flags
& Xapian::DB_FULL_SYNC
) ?
372 int fd_to_close
= fd
;
374 if ((flags
& Xapian::DB_NO_SYNC
) == 0 &&
375 ((flags
& Xapian::DB_FULL_SYNC
) ?
376 !io_full_sync(fd_to_close
) :
377 !io_sync(fd_to_close
))) {
378 int save_errno
= errno
;
379 (void)close(fd_to_close
);
380 if (!tmpfile
.empty())
381 (void)unlink(tmpfile
.c_str());
386 if (close(fd_to_close
) != 0) {
387 if (!tmpfile
.empty()) {
388 int save_errno
= errno
;
389 (void)unlink(tmpfile
.c_str());
395 if (!tmpfile
.empty()) {
396 if (!io_tmp_rename(tmpfile
, db_dir
+ "/iamhoney")) {
402 for (unsigned table_no
= 0; table_no
< Honey::MAX_
; ++table_no
) {
403 old_root
[table_no
] = root
[table_no
];
410 // Only try to compress tags longer than this many bytes.
411 const size_t COMPRESS_MIN
= 4;
413 static const uint4 compress_min_tab
[] = {
415 COMPRESS_MIN
, // DOCDATA
416 COMPRESS_MIN
, // TERMLIST
418 COMPRESS_MIN
, // SPELLING
419 COMPRESS_MIN
// SYNONYM
423 HoneyVersion::create(unsigned blocksize
)
425 AssertRel(blocksize
,>=,HONEY_MIN_BLOCKSIZE
);
427 for (unsigned table_no
= 0; table_no
< Honey::MAX_
; ++table_no
) {
428 root
[table_no
].init(blocksize
, compress_min_tab
[table_no
]);
435 RootInfo::init(unsigned blocksize_
, uint4 compress_min_
)
437 AssertRel(blocksize_
,>=,HONEY_MIN_BLOCKSIZE
);
444 blocksize
= blocksize_
;
445 compress_min
= compress_min_
;
446 fl_serialised
.resize(0);
450 RootInfo::serialise(string
&s
) const
452 AssertRel(offset
, >=, 0);
453 std::make_unsigned
<off_t
>::type uoffset
= offset
;
454 AssertRel(root
, >=, uoffset
);
455 pack_uint(s
, uoffset
);
456 pack_uint(s
, root
- uoffset
);
457 unsigned val
= level
<< 2;
458 if (sequential
) val
|= 0x02;
459 if (root_is_fake
) val
|= 0x01;
461 pack_uint(s
, num_entries
);
462 pack_uint(s
, blocksize
>> 11);
463 pack_uint(s
, compress_min
);
464 pack_string(s
, fl_serialised
);
468 RootInfo::unserialise(const char ** p
, const char * end
)
470 std::make_unsigned
<off_t
>::type uoffset
;
472 if (!unpack_uint(p
, end
, &uoffset
) ||
473 !unpack_uint(p
, end
, &root
) ||
474 !unpack_uint(p
, end
, &val
) ||
475 !unpack_uint(p
, end
, &num_entries
) ||
476 !unpack_uint(p
, end
, &blocksize
) ||
477 !unpack_uint(p
, end
, &compress_min
) ||
478 !unpack_string(p
, end
, fl_serialised
)) return false;
482 sequential
= val
& 0x02;
483 root_is_fake
= val
& 0x01;
485 AssertRel(blocksize
,>=,HONEY_MIN_BLOCKSIZE
);