21 #ifndef XAPIAN_INCLUDED_HONEY_TABLE_H
22 #define XAPIAN_INCLUDED_HONEY_TABLE_H
25 # error config.h must be included first in each C++ source file
28 #define SSTINDEX_ARRAY
32 #define SSTINDEX_BINARY_CHOP_KEY_SIZE 4
33 #define SSTINDEX_BINARY_CHOP_PTR_SIZE 4
34 #define SSTINDEX_BINARY_CHOP_ENTRY_SIZE \
35 (SSTINDEX_BINARY_CHOP_KEY_SIZE + SSTINDEX_BINARY_CHOP_PTR_SIZE)
41 #include <string_view>
48 #include <type_traits>
53 #include <sys/types.h>
154 bool open(
const std::string& path,
bool read_only_) {
166 if (fd < 0)
return false;
182 size_t delta = size_t(pos_ - (
pos -
buf_end));
194 void skip(
size_t delta)
const {
210 if (fd == -1 || fstat(fd, &sbuf) < 0)
return true;
211 return (sbuf.st_size == 0);
236 iov[0].iov_base =
buf;
238 iov[1].iov_base =
const_cast<char*
>(
p);
239 iov[1].iov_len = len;
240 ssize_t n_ = writev(
common->
fd, iov, 2);
241 if (n_ < 0) std::abort();
263 if (len >=
sizeof(
buf)) {
280 if (r <
sizeof(
buf)) {
289 return static_cast<unsigned char>(
buf[
sizeof(
buf) -
buf_end--]);
300 void read(
char*
p,
size_t len)
const {
342 #if defined SSTINDEX_BINARY_CHOP
343 size_t block = size_t(-1);
344 #elif defined SSTINDEX_SKIPLIST
347 #if defined SSTINDEX_BINARY_CHOP || defined SSTINDEX_SKIPLIST
348 std::string last_index_key;
355 #ifdef SSTINDEX_ARRAY
356 unsigned char first,
last =
static_cast<unsigned char>(-1);
362 #ifdef SSTINDEX_ARRAY
364 #elif defined SSTINDEX_BINARY_CHOP
365 data.assign(5,
'\x01');
366 #elif defined SSTINDEX_SKIPLIST
367 data.assign(1,
'\x02');
369 # error SSTINDEX type not specified
374 #ifdef SSTINDEX_ARRAY
381 #ifdef SSTINDEX_ARRAY
382 unsigned char initial = key[0];
390 while (++
last !=
int(initial)) {
397 #elif defined SSTINDEX_BINARY_CHOP
423 last_index_key = key;
424 if (key.back() ==
'\0')
432 if (cur_block == block)
439 std::cout <<
"Adding «" << esc <<
"» -> file offset " << ptr
443 data += last_index_key;
444 size_t c =
data.size();
449 #elif defined SSTINDEX_SKIPLIST
451 if (cur_block == block)
return;
456 data += char(key.size() - reuse);
457 data.append(key, reuse, key.size() - reuse);
458 pack_uint(
data,
static_cast<std::make_unsigned_t<off_t>
>(ptr));
463 last_index_key = key;
465 # error SSTINDEX type not specified
472 #ifdef SSTINDEX_ARRAY
482 for (
unsigned ch =
first; ch <=
last; ++ch) {
483 size_t o = 3 + (ch -
first) * 4;
486 if (
sizeof(off_t) > 4 && ptr > off_t(0xffffffff))
493 #elif defined SSTINDEX_BINARY_CHOP
497 size_t i = last_index_key.size();
505 goto skip_adding_upper_bound;
508 ch =
static_cast<unsigned char>(last_index_key[i]) + 1;
509 last_index_key[i] = ch;
517 data += last_index_key;
518 size_t c =
data.size();
523 skip_adding_upper_bound:
526 data[1] = n_index >> 24;
527 data[2] = n_index >> 16;
528 data[3] = n_index >> 8;
530 #elif defined SSTINDEX_SKIPLIST
533 # error SSTINDEX type not specified
544 size_t s =
data.size();
585 HoneyTable(
const char*,
const std::string& path_,
bool read_only_,
593 HoneyTable(
const char*,
int fd, off_t offset_,
bool read_only_,
596 store(fd, 0, offset_, read_only_),
606 std::cout <<
"*** " <<
path <<
" - index " << index_size <<
" for "
607 <<
index.get_num_entries() <<
" entries" << std::endl;
632 void add(std::string_view key,
635 bool compressed =
false);
637 void add(std::string_view key,
638 std::string_view val,
639 bool compressed =
false) {
640 add(key, val.data(), val.size(), compressed);
671 bool del(
const std::string&) {
684 return stat(
path.c_str(), &sbuf) == 0;
BufferedFileCommon & operator=(const BufferedFileCommon &)=delete
BufferedFileCommon(const BufferedFileCommon &)=delete
BufferedFileCommon(int fd_, off_t offset_)
void skip(size_t delta) const
void write(const char *p, size_t len)
bool open(const std::string &path, bool read_only_)
bool was_forced_closed() const
void force_close(bool fd_owned)
void read(char *p, size_t len) const
void close(bool fd_owned)
BufferedFile(int fd_, off_t offset_, off_t pos_, bool read_only_)
BufferedFileCommon * common
void write(unsigned char ch)
size_t buf_end
Index into buf where buffered data ends.
uint4 read_uint4_be() const
BufferedFile(const BufferedFile &o)
HoneyCursor * cursor_get() const
HoneyTable(const char *, int fd, off_t offset_, bool read_only_, bool lazy_=false)
bool get_exact_entry(std::string_view key, std::string *tag) const
honey_tablesize_t get_entry_count() const
void close(bool permanent)
void create_and_open(int flags_, const Honey::RootInfo &root_info)
void add(std::string_view key, const char *val, size_t val_size, bool compressed=false)
bool del(const std::string &)
honey_tablesize_t get_approx_entry_count() const
Return an approximation of the number of entries in the table.
void cancel(const Honey::RootInfo &, honey_revision_number_t)
const std::string & get_path() const
static void throw_database_closed()
honey_tablesize_t num_entries
off_t offset
Offset to add to pointers in this table.
bool read_key(std::string &key, size_t &val_size, bool &compressed) const
HoneyTable(const char *, const std::string &path_, bool read_only_, bool lazy_=false)
bool get_exact_entry(std::string_view key, std::string &tag) const
void read_val(std::string &val, size_t val_size) const
void commit(honey_revision_number_t, Honey::RootInfo *root_info)
bool key_exists(const std::string &key) const
void open(int flags_, const Honey::RootInfo &root_info, honey_revision_number_t)
void add(std::string_view key, std::string_view val, bool compressed=false)
bool readahead_key(const std::string &) const
off_t write(BufferedFile &store)
void maybe_add_entry(std::string_view key, off_t ptr)
Indicates an attempt to access a closed database.
DatabaseError indicates some sort of database related error.
class wrapper around zlib
Append a string to an object description, escaping invalid UTF-8.
Hierarchy of classes which Xapian can throw as exceptions.
Definitions, types, etc for use inside honey.
unsigned long long honey_tablesize_t
How many entries there are in a table.
#define HONEY_TABLE_EXTENSION
Honey table extension.
uint4 honey_revision_number_t
The revision number of a honey database.
#define SSTINDEX_BINARY_CHOP_ENTRY_SIZE
#define SSTINDEX_BINARY_CHOP_KEY_SIZE
void io_write(int fd, const char *p, size_t n)
Write n bytes from block pointed to by p to file descriptor fd.
int io_open_stream_wr(const char *filename, bool anew)
Open a stream-based file for writing.
size_t io_pread(int fd, char *p, size_t n, off_t o, size_t min)
Read n bytes (or until EOF) into block pointed to by p from file descriptor fd starting at position o...
Wrappers for low-level POSIX I/O routines.
int io_open_stream_rd(const char *filename)
Open a stream-based file for reading.
bool io_sync(int fd)
Ensure all data previously written to file descriptor fd has been written to disk.
#define AssertRel(A, REL, B)
Pack types into strings and unpack them again.
void pack_uint(std::string &s, U value)
Append an encoded unsigned integer to a string.
include <sys/stat.h> with portability enhancements
<unistd.h>, but with compat.
Convert types to std::string.
Various handy string-related helpers.
bool startswith(std::string_view s, char pfx)
std::string::size_type common_prefix_length(std::string_view a, std::string_view b)
void description_append(std::string &desc, std::string_view s)
functions for reading and writing different width words
void unaligned_write4(unsigned char *ptr, T value)