24 #ifndef OM_HGUARD_CHERT_TABLE_H
25 #define OM_HGUARD_CHERT_TABLE_H
101 #define CHERT_BTREE_MAX_KEY_LEN 252
127 inline int getK(
const unsigned char *p,
int c) {
return getint1(p, c); }
128 inline void setD(
unsigned char *p,
int c,
int x) {
setint2(p, c, x); }
170 explicit Key(
const uint8_t * p_) :
p(p_) { }
172 void read(std::string * key)
const {
173 key->assign(
reinterpret_cast<const char *
>(
p +
K1),
length());
219 tag->append(
reinterpret_cast<const char *
>(
p + cd), l);
251 int i = truncate_size;
254 int newkey_len = newkey.
length();
256 int newsize =
I2 +
K1 + i +
C2;
292 std::string::size_type key_len = key_.length();
297 std::string msg(
"Key too long: length was ");
299 msg +=
" bytes, maximum length of a key is "
305 std::memmove(
p +
I2 +
K1, key_.data(), key_len);
309 void set_tag(
int cd,
const char *start,
int len,
bool compressed) {
310 std::memmove(
p + cd, start, len);
312 if (compressed) *
p |= 0x80;
376 ChertTable(
const char * tablename_,
const std::string & path_,
392 void close(
bool permanent =
false);
459 const std::string * changes_tail = NULL);
488 bool get_exact_entry(
const std::string & key, std::string & tag)
const;
501 bool key_exists(
const std::string &key)
const;
511 bool read_tag(
Cursor * C_, std::string *tag,
bool keep_compressed)
const;
529 void add(
const std::string &key, std::string tag,
bool already_compressed =
false);
546 bool del(
const std::string &key);
690 bool create_db =
false);
709 void form_key(
const std::string & key)
const;
Btree base file implementation.
Interface to Btree cursors.
void SET_LEVEL(uint8_t *b, int x)
const int BYTES_PER_BLOCK_NUMBER
void setint4(unsigned char *p, int c, int x)
void SET_MAX_FREE(uint8_t *b, int x)
int getK(const unsigned char *p, int c)
int TOTAL_FREE(const uint8_t *b)
#define CHERT_BTREE_MAX_KEY_LEN
The largest possible value of a key_len.
void setint2(unsigned char *p, int c, int x)
int GET_LEVEL(const uint8_t *b)
unsigned REVISION(const uint8_t *b)
void SET_DIR_END(uint8_t *b, int x)
const size_t CHERT_MAX_ITEM_SIZE
void SET_REVISION(uint8_t *b, uint4 rev)
const int BTREE_CURSOR_LEVELS
int getint2(const unsigned char *p, int c)
int DIR_END(const uint8_t *b)
int MAX_FREE(const uint8_t *b)
const size_t BLOCK_CAPACITY
Even for items of at maximum size, it must be possible to get this number of items in a block.
int getint4(const unsigned char *p, int c)
void setD(unsigned char *p, int c, int x)
void SET_TOTAL_FREE(uint8_t *b, int x)
void setint1(unsigned char *p, int c, int x)
int getint1(const unsigned char *p, int c)
Types used by chert backend and the Btree manager.
unsigned long long chert_tablesize_t
A type used to store the number of entries in a table.
unsigned int chert_revision_number_t
A type used to store a revision number for a table.
A cursor pointing to a position in a Btree table, for reading several entries in order,...
Class managing a Btree table in a Chert database.
void open()
Open the btree at the latest revision.
void set_overwritten() const
bool next(Cursor *C_, int j) const
bool do_open_to_read(bool revision_supplied, chert_revision_number_t revision_)
Perform the opening operation to read.
ChertCursor * cursor_get() const
Get a cursor for reading from the table.
bool full_compaction
set to true when full compaction is to be achieved
int mid_point(uint8_t *p) const
mid_point(p) finds the directory entry in c that determines the approximate mid point of the data in ...
bool cursor_created_since_last_modification
Flag for tracking when cursors need to rebuild.
bool readahead_key(const string &key) const
bool basic_open(bool revision_supplied, chert_revision_number_t revision)
bool sequential
true iff the data has been written in a single write in sequential order.
unsigned int get_block_size() const
Get the block size.
bool next_default(Cursor *C_, int j) const
uint4 changed_n
the last block to be changed by an addition
void commit(chert_revision_number_t revision, int changes_fd=-1, const std::string *changes_tail=NULL)
Commit any outstanding changes to the table.
void read_block(uint4 n, uint8_t *p) const
read_block(n, p) reads block n of the DB file to address p.
void set_max_item_size(size_t block_capacity)
Set the maximum item size given the block capacity.
bool writable
Set to true when the database is opened to write.
void set_block_size(unsigned int block_size_)
Set the block size.
static void throw_database_closed()
Throw an exception indicating that the database is closed.
chert_tablesize_t get_entry_count() const
Return a count of the number of entries in the table.
void flush_db()
Flush any outstanding changes to the DB file of the table.
uint4 root
the root block of the B-tree
bool key_exists(const std::string &key) const
Check if a key exists in the Btree.
unsigned long cursor_version
Version count for tracking when cursors need to rebuild.
bool really_empty() const
Return true if there are no entries in the table.
ChertTable_base base
For writing back as file baseA or baseB.
z_stream * inflate_zstream
Zlib state object for inflating.
int changed_c
directory offset corresponding to last block to be changed by an addition
bool both_bases
set to true if baseA and baseB both exist as valid bases.
bool get_exact_entry(const std::string &key, std::string &tag) const
Read an entry from the table, if and only if it is exactly that being asked for.
bool empty() const
Return true if there are no entries in the table.
int add_kt(bool found)
add_kt(found) adds the item (key-tag pair) at B->kt into the B-tree, using cursor C.
bool del(const std::string &key)
Delete an entry from the table.
bool is_open() const
Return true if this table is open.
int seq_count
count of the number of successive instances of purely sequential addition, starting at SEQ_START_POIN...
void write_changed_blocks(int changes_fd)
Append the list of blocks changed to a changeset file.
bool next_for_sequential(Cursor *C_, int dummy) const
bool is_modified() const
Determine whether the object contains uncommitted modifications.
void split_root(uint4 split_n)
Btree needs to gain a new level to insert more items: so split root block and construct a new one.
chert_revision_number_t revision_number
revision number of the opened B-tree.
chert_revision_number_t get_open_revision_number() const
Get the revision number at which this table is currently open.
void form_key(const std::string &key) const
void set_full_compaction(bool parity)
int level
number of levels, counting from 0
void add_item_to_block(uint8_t *p, Item_wr kt, int c)
add_item_to_block(p, kt_, c) adds item kt_ to the block at p.
ChertTable(const ChertTable &)
Copying not allowed.
bool do_open_to_write(bool revision_supplied, chert_revision_number_t revision_, bool create_db=false)
Perform the opening operation to write.
std::string name
The path name of the B tree.
uint8_t * split_p
Buffer used when splitting a block.
size_t max_item_size
maximum size of an item (key-tag pair)
void write_block(uint4 n, const uint8_t *p) const
write_block(n, p) writes block n in the DB file from address p.
char other_base_letter() const
Item_wr kt
buffer of size block_size for making up key-tag items
chert_revision_number_t latest_revision_number
Revision number of the other base, or zero if there is only one base file.
bool read_tag(Cursor *C_, std::string *tag, bool keep_compressed) const
Read the tag value for the key pointed to by cursor C_.
chert_tablesize_t item_count
keeps a count of the number of items in the B-tree.
bool find(Cursor *) const
find(C_) searches for the key of B->kt in the B-tree.
~ChertTable()
Close the Btree.
void erase()
Erase this table from disk.
bool prev_for_sequential(Cursor *C_, int dummy) const
void delete_item(int j, bool repeatedly)
ChertTable::delete_item(j, repeatedly) is (almost) the converse of add_item.
unsigned int block_size
block size of the B tree in bytes
bool prev_default(Cursor *C_, int j) const
static int find_in_block(const uint8_t *p, Key key, bool leaf, int c)
find_in_block(p, key, leaf, c) searches for the key in the block at p.
char base_letter
the value 'A' or 'B' of the current base
int compress_strategy
DONT_COMPRESS or Z_DEFAULT_STRATEGY, Z_FILTERED, Z_HUFFMAN_ONLY, Z_RLE.
bool exists() const
Determine whether the btree exists on disk.
void cancel()
Cancel any outstanding changes.
void alter()
Btree::alter(); is called when the B-tree is to be altered.
int handle
File descriptor of the table.
void add(const std::string &key, std::string tag, bool already_compressed=false)
Add a key/tag pair to the table, replacing any existing pair with the same key.
Cursor C[BTREE_CURSOR_LEVELS]
bool lazy
If true, don't create the table until it's needed.
bool prev(Cursor *C_, int j) const
void lazy_alloc_inflate_zstream() const
Allocate the zstream for inflating, if not already allocated.
void create_and_open(unsigned int blocksize)
Create a new empty btree structure on disk and open it at the initial revision.
void enter_key(int j, Key prevkey, Key newkey)
enter_key(j, prevkey, newkey) is called after a block split.
z_stream * deflate_zstream
Zlib state object for deflating.
uint8_t * buffer
buffer of size block_size for reforming blocks
void add_item(Item_wr kt, int j)
ChertTable::add_item(kt_, j) adds item kt_ to the block at cursor level C[j].
void compact(uint8_t *p)
compact(p) compact the block at p by shuffling all the items up to the end.
void block_to_cursor(Cursor *C_, int j, uint4 n) const
const char * tablename
The name of the table (used when writing changesets).
bool faked_root_block
true if the root block is faked (not written to disk).
uint4 last_readahead
Last block readahead_key() preread.
ChertTable & operator=(const ChertTable &)
Assignment not allowed.
bool Btree_modified
Set to true the first time the B-tree is modified.
void lazy_alloc_deflate_zstream() const
Allocate the zstream for deflating, if not already allocated.
static uint4 block_given_by(const uint8_t *p, int c)
block_given_by(p, c) finds the item at block address p, directory offset c, and returns its tag value...
void close(bool permanent=false)
Close the Btree.
chert_revision_number_t get_latest_revision_number() const
Get the latest revision number stored in this table.
int components_of() const
bool get_compressed() const
int size() const
I in diagram above.
void append_chunk(std::string *tag) const
uint4 block_given_by() const
Get this item's tag as a block number (this block should not be at level 0).
void set_tag(int cd, const char *start, int len, bool compressed)
void form_key(const std::string &key_)
void set_block_given_by(uint4 n)
Set this item's tag to point to block n (this block should not be at level 0).
Item_wr(uint8_t *p_, int c)
void form_null_key(uint4 n)
Form an item with a null key and with block number n in the tag.
void set_key_and_block(Key newkey, int truncate_size, uint4 n)
void set_component_of(int i)
void set_components_of(int m)
Item(const uint8_t *p_, int c)
char operator[](size_t i) const
bool operator<(Key key2) const
Compares this key with key2.
bool operator>=(Key key2) const
void read(std::string *key) const
bool operator==(Key key2) const
const uint8_t * get_address() const
bool operator<=(Key key2) const
bool operator>(Key key2) const
bool operator!=(Key key2) const
DatabaseError indicates some sort of database related error.
InvalidArgumentError indicates an invalid parameter value was passed to the API.
Hierarchy of classes which Xapian can throw as exceptions.
string str(int value)
Convert int to std::string.
int revision()
Report the revision of the library which the program is linked with.
XAPIAN_REVISION_TYPE rev
Revision number of a database.
Define the XAPIAN_NORETURN macro.
Various assertion macros.
#define AssertRel(A, REL, B)
Convert types to std::string.
Various handy helpers which std::string really should provide.
#define STRINGIZE(X)
The STRINGIZE macro converts its parameter into a string constant.
functions for reading and writing different width words
void aligned_write4(unsigned char *ptr, T value)
uint32_t unaligned_read4(const unsigned char *ptr)
void unaligned_write2(unsigned char *ptr, T value)
uint16_t unaligned_read2(const unsigned char *ptr)
void unaligned_write4(unsigned char *ptr, T value)
uint32_t aligned_read4(const unsigned char *ptr)