xapian-core  1.4.26
Public Member Functions | Static Public Member Functions | Protected Member Functions | Static Protected Member Functions | Protected Attributes | Private Member Functions | Friends | List of all members
GlassTable Class Reference

Class managing a Btree table in a Glass database. More...

#include <glass_table.h>

+ Inheritance diagram for GlassTable:
+ Collaboration diagram for GlassTable:

Public Member Functions

 GlassTable (const char *tablename_, const std::string &path_, bool readonly_, bool lazy=false)
 Create a new Btree object. More...
 
 GlassTable (const char *tablename_, int fd, off_t offset_, bool readonly_, bool lazy=false)
 
 ~GlassTable ()
 Close the Btree. More...
 
void close (bool permanent=false)
 Close the Btree. More...
 
bool readahead_key (const string &key) const
 
bool exists () const
 Determine whether the btree exists on disk. More...
 
void open (int flags_, const RootInfo &root_info, glass_revision_number_t rev)
 Open the btree. More...
 
bool is_open () const
 Return true if this table is open. More...
 
bool is_writable () const
 Return true if this table is writable. More...
 
void flush_db ()
 Flush any outstanding changes to the DB file of the table. More...
 
void commit (glass_revision_number_t revision, RootInfo *root_info)
 Commit any outstanding changes to the table. More...
 
bool sync ()
 
void cancel (const RootInfo &root_info, glass_revision_number_t rev)
 Cancel any outstanding changes. More...
 
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. More...
 
bool key_exists (const std::string &key) const
 Check if a key exists in the Btree. More...
 
bool read_tag (Glass::Cursor *C_, std::string *tag, bool keep_compressed) const
 Read the tag value for the key pointed to by cursor C_. More...
 
void add (const std::string &key, const std::string &tag, bool already_compressed=false)
 Add a key/tag pair to the table, replacing any existing pair with the same key. More...
 
bool del (const std::string &key)
 Delete an entry from the table. More...
 
int get_flags () const
 
void create_and_open (int flags_, const RootInfo &root_info)
 Create a new empty btree structure on disk and open it at the initial revision. More...
 
void set_full_compaction (bool parity)
 
glass_revision_number_t get_open_revision_number () const
 Get the revision number at which this table is currently open. More...
 
glass_tablesize_t get_entry_count () const
 Return a count of the number of entries in the table. More...
 
bool empty () const
 Return true if there are no entries in the table. More...
 
GlassCursorcursor_get () const
 Get a cursor for reading from the table. More...
 
bool is_modified () const
 Determine whether the object contains uncommitted modifications. More...
 
void set_max_item_size (size_t block_capacity)
 Set the maximum item size given the block capacity. More...
 
void set_changes (GlassChanges *changes)
 Set the GlassChanges object to write changed blocks to. More...
 
string get_path () const
 

Static Public Member Functions

static void throw_database_closed ()
 Throw an exception indicating that the database is closed. More...
 

Protected Member Functions

bool find (Glass::Cursor *) const
 find(C_) searches for the key of B->kt in the B-tree. More...
 
int delete_kt ()
 
void read_block (uint4 n, uint8_t *p) const
 read_block(n, p) reads block n of the DB file to address p. More...
 
void write_block (uint4 n, const uint8_t *p, bool appending=false) const
 write_block(n, p, appending) writes block n in the DB file from address p. More...
 
void set_overwritten () const
 
void block_to_cursor (Glass::Cursor *C_, int j, uint4 n) const
 
void alter ()
 Btree::alter(); is called when the B-tree is to be altered. More...
 
void compact (uint8_t *p)
 compact(p) compact the block at p by shuffling all the items up to the end. More...
 
void enter_key_above_leaf (Glass::LeafItem previtem, Glass::LeafItem newitem)
 enter_key_above_leaf(previtem, newitem) is called after a leaf block split. More...
 
void enter_key_above_branch (int j, Glass::BItem newitem)
 enter_key_above_branch(j, newkey) is called after a branch block split. More...
 
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 the block at p. More...
 
void add_item_to_leaf (uint8_t *p, Glass::LeafItem kt, int c)
 add_item_to_leaf(p, kt_, c) adds item kt_ to the leaf block at p. More...
 
void add_item_to_branch (uint8_t *p, Glass::BItem kt, int c)
 add_item_to_branch(p, kt_, c) adds item kt_ to the branch block at p. More...
 
void add_leaf_item (Glass::LeafItem kt)
 GlassTable::add_leaf_item(kt_) adds item kt_ to the leaf block. More...
 
void add_branch_item (Glass::BItem kt, int j)
 GlassTable::add_item(kt_, j) adds item kt_ to the block at cursor level C[j]. More...
 
void delete_leaf_item (bool repeatedly)
 GlassTable::delete_leaf_item(repeatedly) is (almost) the converse of add_leaf_item. More...
 
void delete_branch_item (int j)
 GlassTable::delete_branch_item(j, repeatedly) is (almost) the converse of add_branch_item. More...
 
int add_kt (bool found)
 add_kt(found) adds the item (key-tag pair) at B->kt into the B-tree, using cursor C. More...
 
void read_root ()
 
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. More...
 
void form_key (const std::string &key) const
 
bool single_file () const
 
bool prev (Glass::Cursor *C_, int j) const
 
bool next (Glass::Cursor *C_, int j) const
 
bool prev_default (Glass::Cursor *C_, int j) const
 
bool next_default (Glass::Cursor *C_, int j) const
 
bool prev_for_sequential (Glass::Cursor *C_, int dummy) const
 
bool next_for_sequential (Glass::Cursor *C_, int dummy) const
 

Static Protected Member Functions

static int find_in_leaf (const uint8_t *p, Glass::LeafItem item, int c, bool &exact)
 find_in_leaf(p, key, c, exact) searches for the key in the leaf block at p. More...
 
static int find_in_branch (const uint8_t *p, Glass::LeafItem item, int c)
 
static int find_in_branch (const uint8_t *p, Glass::BItem item, int c)
 
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 as an integer. More...
 

Protected Attributes

const char * tablename
 The name of the table (used when writing changesets). More...
 
glass_revision_number_t revision_number
 revision number of the opened B-tree. More...
 
glass_tablesize_t item_count
 keeps a count of the number of items in the B-tree. More...
 
unsigned int block_size
 block size of the B tree in bytes More...
 
int flags
 Flags like DB_NO_SYNC and DB_DANGEROUS. More...
 
bool faked_root_block
 true if the root block is faked (not written to disk). More...
 
bool sequential
 true iff the data has been written in a single write in sequential order. More...
 
int handle
 File descriptor of the table. More...
 
int level
 number of levels, counting from 0 More...
 
uint4 root
 the root block of the B-tree More...
 
Glass::LeafItem_wr kt
 buffer of size block_size for making up key-tag items More...
 
uint8_t * buffer
 buffer of size block_size for reforming blocks More...
 
GlassFreeList free_list
 List of free blocks. More...
 
std::string name
 The path name of the B tree. More...
 
int seq_count
 count of the number of successive instances of purely sequential addition, starting at SEQ_START_POINT (neg) and going up to zero. More...
 
uint4 changed_n
 the last block to be changed by an addition More...
 
int changed_c
 directory offset corresponding to last block to be changed by an addition More...
 
size_t max_item_size
 maximum size of an item (key-tag pair) More...
 
bool Btree_modified
 Set to true the first time the B-tree is modified. More...
 
bool full_compaction
 set to true when full compaction is to be achieved More...
 
bool writable
 Set to true when the database is opened to write. More...
 
bool cursor_created_since_last_modification
 Flag for tracking when cursors need to rebuild. More...
 
unsigned long cursor_version
 Version count for tracking when cursors need to rebuild. More...
 
GlassChangeschanges_obj
 The GlassChanges object to write block changes to. More...
 
Glass::Cursor C [GLASS_BTREE_CURSOR_LEVELS]
 
uint8_t * split_p
 Buffer used when splitting a block. More...
 
uint4 compress_min
 Minimum size tag to try compressing (0 for no compression). More...
 
CompressionStream comp_stream
 
bool lazy
 If true, don't create the table until it's needed. More...
 
uint4 last_readahead
 Last block readahead_key() preread. More...
 
off_t offset
 offset to start of table in file. More...
 

Private Member Functions

 GlassTable (const GlassTable &)
 Copying not allowed. More...
 
GlassTableoperator= (const GlassTable &)
 Assignment not allowed. More...
 
void basic_open (const RootInfo *root_info, glass_revision_number_t rev)
 
void do_open_to_read (const RootInfo *root_info, glass_revision_number_t rev)
 Perform the opening operation to read. More...
 
void do_open_to_write (const RootInfo *root_info, glass_revision_number_t rev=0)
 Perform the opening operation to write. More...
 

Friends

class GlassCursor
 
class GlassFreeList
 

Detailed Description

Class managing a Btree table in a Glass database.

A table is a store holding a set of key/tag pairs.

A key is used to access a block of data in a glass table.

Keys are of limited length.

Keys may not be empty (each Btree has a special empty key for internal use).

A tag is a piece of data associated with a given key. The contents of the tag are opaque to the Btree.

Tags may be of arbitrary length (the Btree imposes a very large limit). Note though that they will be loaded into memory in their entirety, so should not be permitted to grow without bound in normal usage.

Tags which are null strings are valid, and are different from a tag simply not being in the table.

Definition at line 425 of file glass_table.h.

Constructor & Destructor Documentation

◆ GlassTable() [1/3]

GlassTable::GlassTable ( const GlassTable )
private

Copying not allowed.

◆ GlassTable() [2/3]

GlassTable::GlassTable ( const char *  tablename_,
const std::string &  path_,
bool  readonly_,
bool  lazy = false 
)

Create a new Btree object.

This does not create the table on disk - the create_and_open() method must be called to create the table on disk.

This also does not open the table - either the create_and_open() or open() methods must be called before use is made of the table.

Parameters
tablename_The name of the table (used in changesets).
path_Path at which the table is stored.
readonly_whether to open the table for read only access.
lazyIf true, don't create the table until it's needed.

Definition at line 1698 of file glass_table.cc.

References LOGCALL_CTOR.

◆ GlassTable() [3/3]

GlassTable::GlassTable ( const char *  tablename_,
int  fd,
off_t  offset_,
bool  readonly_,
bool  lazy = false 
)

Definition at line 1733 of file glass_table.cc.

References LOGCALL_CTOR.

◆ ~GlassTable()

GlassTable::~GlassTable ( )

Close the Btree.

Any outstanding changes (ie, changes made without commit() having subsequently been called) will be lost.

Definition at line 1807 of file glass_table.cc.

References close(), and LOGCALL_DTOR.

Member Function Documentation

◆ add()

void GlassTable::add ( const std::string &  key,
const std::string &  tag,
bool  already_compressed = false 
)

Add a key/tag pair to the table, replacing any existing pair with the same key.

If an error occurs during the operation, an exception will be thrown.

If key is empty, then the null item is replaced.

e.g. btree.add("TODAY", "Mon 9 Oct 2000");

Parameters
keyThe key to store in the table.
tagThe tag to store in the table.
already_compressedtrue if tag is already compressed, for example because it is being opaquely copied (default: false).

Definition at line 1279 of file glass_table.cc.

References Assert, AssertEq, BYTE_PAIR_RANGE, C, Glass::D2, Glass::I2, Glass::RootInfo::init(), Glass::K1, LOGCALL_VOID, throw_database_closed(), Glass::TOTAL_FREE(), and Glass::X2.

Referenced by GlassDocDataTable::add_document_data(), Glass::PostlistChunkWriter::flush(), GlassCompact::merge_docid_keyed(), GlassCompact::merge_positions(), GlassCompact::merge_postlists(), GlassCompact::merge_spellings(), GlassCompact::merge_synonyms(), GlassDocDataTable::replace_document_data(), GlassWritableDatabase::set_metadata(), and Glass::ValueUpdater::write_tag().

◆ add_branch_item()

void GlassTable::add_branch_item ( Glass::BItem  kt,
int  j 
)
protected

GlassTable::add_item(kt_, j) adds item kt_ to the block at cursor level C[j].

If there is not enough room the block splits and the item is then added to the appropriate half.

Definition at line 942 of file glass_table.cc.

References Assert, AssertRel, Glass::BItem_base< T >::block_given_by(), C, Glass::D2, Glass::DIR_END(), Glass::DIR_START, Glass::BItem_wr::form_null_key(), Glass::BItem_base< T >::key(), Glass::Key::length(), LOGCALL_VOID, Glass::MAX_FREE(), Glass::SET_DIR_END(), Glass::SET_TOTAL_FREE(), Glass::BItem_base< T >::size(), and Glass::TOTAL_FREE().

◆ add_item_to_branch()

void GlassTable::add_item_to_branch ( uint8_t *  p,
Glass::BItem  kt,
int  c 
)
protected

add_item_to_branch(p, kt_, c) adds item kt_ to the branch block at p.

c is the offset in the directory that needs to be expanded to accommodate the new entry for the item. We know before this is called that there is enough contiguous room for the item in the block, so it's just a matter of shuffling up any directory entries after where we're inserting and copying in the item.

Definition at line 811 of file glass_table.cc.

References Assert, AssertRel, Glass::D2, Glass::DIR_END(), Glass::DIR_START, Glass::BItem_base< T >::get_address(), LOGCALL_VOID, Glass::MAX_FREE(), Glass::SET_DIR_END(), Glass::SET_MAX_FREE(), Glass::SET_TOTAL_FREE(), Glass::BItem_wr::setD(), Glass::BItem_base< T >::size(), and Glass::TOTAL_FREE().

◆ add_item_to_leaf()

void GlassTable::add_item_to_leaf ( uint8_t *  p,
Glass::LeafItem  kt,
int  c 
)
protected

add_item_to_leaf(p, kt_, c) adds item kt_ to the leaf block at p.

c is the offset in the directory that needs to be expanded to accommodate the new entry for the item. We know before this is called that there is enough contiguous room for the item in the block, so it's just a matter of shuffling up any directory entries after where we're inserting and copying in the item.

Definition at line 771 of file glass_table.cc.

References Assert, AssertRel, Glass::D2, Glass::DIR_END(), Glass::DIR_START, Glass::LeafItem_base< T >::get_address(), LOGCALL_VOID, Glass::MAX_FREE(), Glass::SET_DIR_END(), Glass::SET_MAX_FREE(), Glass::SET_TOTAL_FREE(), Glass::LeafItem_wr::setD(), Glass::LeafItem_base< T >::size(), and Glass::TOTAL_FREE().

◆ add_kt()

int GlassTable::add_kt ( bool  found)
protected

add_kt(found) adds the item (key-tag pair) at B->kt into the B-tree, using cursor C.

found == find() is handed over as a parameter from Btree::add. Btree::alter() prepares for the alteration to the B-tree. Then there are a number of cases to consider:

If an item with the same key is in the B-tree (found is true), the new kt replaces it.

If then kt is smaller, or the same size as, the item it replaces, kt is put in the same place as the item it replaces, and the TOTAL_FREE measure is reduced.

If kt is larger than the item it replaces it is put in the MAX_FREE space if there is room, and the directory entry and space counts are adjusted accordingly.

  • But if there is not room we do it the long way: the old item is deleted with delete_leaf_item and kt is added in with add_item.

If the key of kt is not in the B-tree (found is false), the new kt is added in with add_item.

Returns: 0 : added kt 1 : replaced kt 2 : replaced kt and it was the final one

Definition at line 1149 of file glass_table.cc.

References Assert, AssertRel, C, Glass::D2, Glass::DIR_END(), Glass::DIR_START, Glass::LeafItem_base< T >::get_address(), Glass::LeafItem_base< T >::last_component(), LOGCALL, Glass::MAX_FREE(), RETURN, SEQ_START_POINT, Glass::SET_MAX_FREE(), Glass::SET_TOTAL_FREE(), Glass::LeafItem_wr::setD(), Glass::LeafItem_base< T >::size(), and Glass::TOTAL_FREE().

◆ add_leaf_item()

void GlassTable::add_leaf_item ( Glass::LeafItem  kt)
protected

GlassTable::add_leaf_item(kt_) adds item kt_ to the leaf block.

If there is not enough room the block splits and the item is then added to the appropriate half.

Definition at line 847 of file glass_table.cc.

References Assert, AssertRel, C, Glass::D2, Glass::DIR_END(), Glass::DIR_START, LOGCALL_VOID, Glass::MAX_FREE(), Glass::SET_DIR_END(), Glass::LeafItem_base< T >::size(), and Glass::TOTAL_FREE().

◆ alter()

void GlassTable::alter ( )
protected

Btree::alter(); is called when the B-tree is to be altered.

It causes new blocks to be forced for the current set of blocks in the cursor.

The point is that if a block at level 0 is to be altered it may get a new number. Then the pointer to this block from level 1 will need changing. So the block at level 1 needs altering and may get a new block number. Then the pointer to this block from level 2 will need changing ... and so on back to the root.

The clever bit here is spotting the cases when we can make an early exit from this process. If C[j].rewrite is true, C[j+k].rewrite will be true for k = 1,2 ... We have been through all this before, and there is no need to do it again. If C[j].n was free at the start of the transaction, we can copy it back to the same place without violating the integrity of the B-tree. We don't then need a new n and can return. The corresponding C[j].rewrite may be true or false in that case.

Definition at line 374 of file glass_table.cc.

References Assert, C, Xapian::DB_DANGEROUS, LOGCALL_VOID, Glass::REVISION(), Glass::BItem_wr::set_block_given_by(), and Glass::SET_REVISION().

◆ basic_open()

void GlassTable::basic_open ( const RootInfo root_info,
glass_revision_number_t  rev 
)
private

◆ block_given_by()

static uint4 GlassTable::block_given_by ( const uint8_t *  p,
int  c 
)
staticprotected

block_given_by(p, c) finds the item at block address p, directory offset c, and returns its tag value as an integer.

Referenced by next_default(), and prev_default().

◆ block_to_cursor()

void GlassTable::block_to_cursor ( Glass::Cursor C_,
int  j,
uint4  n 
) const
protected

◆ cancel()

void GlassTable::cancel ( const RootInfo root_info,
glass_revision_number_t  rev 
)

◆ close()

void GlassTable::close ( bool  permanent = false)

Close the Btree.

This closes and frees any of the btree structures which have been created and opened.

Parameters
permanentIf true, the Btree will not reopen on demand.

Definition at line 1812 of file glass_table.cc.

References buffer, C, Glass::Cursor::destroy(), Glass::LeafItem_base< T >::get_address(), handle, kt, level, LOGCALL_VOID, single_file(), and split_p.

Referenced by GlassDatabase::close(), commit(), create_and_open(), open(), and ~GlassTable().

◆ commit()

void GlassTable::commit ( glass_revision_number_t  revision,
RootInfo root_info 
)

Commit any outstanding changes to the table.

Commit changes made by calling add() and del() to the Btree.

If an error occurs during the operation, this will be signalled by an exception. In case of error, changes made will not be committed to the Btree - they will be discarded.

Parameters
new_revisionThe new revision number to store. This must be greater than the current revision number. FIXME: If we support rewinding to a previous revision, maybe this needs to be greater than any previously used revision.
root_infoInformation about the root is returned in this.

Definition at line 1866 of file glass_table.cc.

References Assert, block_size, Btree_modified, C, changed_c, changed_n, close(), GlassFreeList::commit(), DIR_START, faked_root_block, free_list, Glass::Cursor::get_n(), handle, Glass::Cursor::init(), item_count, level, LOGCALL_VOID, GlassFreeList::pack(), read_root(), Xapian::revision(), revision_number, root, seq_count, SEQ_START_POINT, sequential, Glass::RootInfo::set_blocksize(), Glass::RootInfo::set_free_list(), Glass::RootInfo::set_level(), Glass::RootInfo::set_num_entries(), GlassFreeList::set_revision(), Glass::RootInfo::set_root(), Glass::RootInfo::set_root_is_fake(), Glass::RootInfo::set_sequential(), throw_database_closed(), and writable.

Referenced by GlassDatabase::compact(), GlassCompact::multimerge_postlists(), and GlassDatabase::set_revision_number().

◆ compact()

void GlassTable::compact ( uint8_t *  p)
protected

◆ create_and_open()

void GlassTable::create_and_open ( int  flags_,
const RootInfo root_info 
)

Create a new empty btree structure on disk and open it at the initial revision.

The table must be writable - it doesn't make sense to create a table that is read-only!

The block size must be less than 64K, where K = 1024. It is unwise to use a small block size (less than 1024 perhaps), so we enforce a minimum block size of 2K.

Example:

// File will be "X." + GLASS_TABLE_EXTENSION (i.e. "X.glass") Btree btree("X."); btree.create_and_open(0, root_info);

Parameters
root_infoRootInfo object
Exceptions
Xapian::DatabaseCreateErrorif the table can't be created.
Xapian::InvalidArgumentErrorif the requested blocksize is unsuitable.

Definition at line 1776 of file glass_table.cc.

References Assert, block_size, BYTE_PAIR_RANGE, close(), compress_min, do_open_to_write(), flags, Glass::RootInfo::get_blocksize(), Glass::RootInfo::get_compress_min(), GLASS_TABLE_EXTENSION, handle, io_unlink(), lazy, LOGCALL_VOID, throw_database_closed(), and writable.

Referenced by GlassDatabase::compact(), GlassDatabase::create_and_open_tables(), and GlassCompact::multimerge_postlists().

◆ cursor_get()

GlassCursor * GlassTable::cursor_get ( ) const

◆ del()

bool GlassTable::del ( const std::string &  key)

Delete an entry from the table.

The entry will be removed from the table, if it exists. If it does not exist, no action will be taken. The item with an empty key can't be removed, and false is returned.

If an error occurs during the operation, this will be signalled by an exception.

e.g. bool deleted = btree.del("TODAY")

Parameters
keyThe key to remove from the table.
Returns
true if an entry was removed; false if it did not exist.

Definition at line 1394 of file glass_table.cc.

References Assert, GLASS_BTREE_MAX_KEY_LEN, LOGCALL, RETURN, and throw_database_closed().

Referenced by GlassDocDataTable::delete_document_data(), Glass::PostlistChunkWriter::flush(), GlassWritableDatabase::set_metadata(), and Glass::ValueUpdater::write_tag().

◆ delete_branch_item()

void GlassTable::delete_branch_item ( int  j)
protected

GlassTable::delete_branch_item(j, repeatedly) is (almost) the converse of add_branch_item.

The process repeats at the next level when a block has been completely emptied, freeing the block and taking out the pointer to it. Emptied root blocks are also removed, which reduces the number of levels in the B-tree.

Definition at line 1074 of file glass_table.cc.

References Assert, AssertRel, BLK_UNUSED, Glass::BItem_base< T >::block_given_by(), C, Glass::D2, Glass::DIR_END(), Glass::DIR_START, LOGCALL_VOID, Glass::MAX_FREE(), Glass::SET_DIR_END(), Glass::SET_MAX_FREE(), Glass::SET_TOTAL_FREE(), Glass::BItem_base< T >::size(), and Glass::TOTAL_FREE().

◆ delete_kt()

int GlassTable::delete_kt ( )
protected

◆ delete_leaf_item()

void GlassTable::delete_leaf_item ( bool  repeatedly)
protected

GlassTable::delete_leaf_item(repeatedly) is (almost) the converse of add_leaf_item.

If repeatedly is true, the process repeats at the next level when a block has been completely emptied, freeing the block and taking out the pointer to it. Emptied root blocks are also removed, which reduces the number of levels in the B-tree.

Definition at line 1039 of file glass_table.cc.

References Assert, AssertRel, BLK_UNUSED, C, Glass::D2, Glass::DIR_END(), Glass::DIR_START, LOGCALL_VOID, Glass::MAX_FREE(), Glass::SET_DIR_END(), Glass::SET_MAX_FREE(), Glass::SET_TOTAL_FREE(), Glass::LeafItem_base< T >::size(), and Glass::TOTAL_FREE().

◆ do_open_to_read()

void GlassTable::do_open_to_read ( const RootInfo root_info,
glass_revision_number_t  rev 
)
private

Perform the opening operation to read.

Definition at line 1980 of file glass_table.cc.

References basic_open(), GLASS_TABLE_EXTENSION, handle, io_open_block_rd(), lazy, LOGCALL, name, read_root(), revision_number, single_file(), and throw_database_closed().

Referenced by open().

◆ do_open_to_write()

void GlassTable::do_open_to_write ( const RootInfo root_info,
glass_revision_number_t  rev = 0 
)
private

Perform the opening operation to write.

Definition at line 1659 of file glass_table.cc.

References Glass::DIR_START, GLASS_TABLE_EXTENSION, io_open_block_wr(), LOGCALL_VOID, name, SEQ_START_POINT, throw_database_closed(), and zeroed_new().

Referenced by create_and_open(), and open().

◆ empty()

bool GlassTable::empty ( ) const
inline

◆ enter_key_above_branch()

void GlassTable::enter_key_above_branch ( int  j,
Glass::BItem  newitem 
)
protected

enter_key_above_branch(j, newkey) is called after a branch block split.

It enters in the block at level C[j] a separating key for the block at level C[j - 1]. The key itself is newkey.

This code looks longer than it really is. If j exceeds the number of B-tree levels the root block has split and we have to construct a new one, but this is a rare event.

The key is constructed in b, with block number C[j - 1].n as tag, and this is added in with add_item. add_item may itself cause a block split, with a further call to enter_key. Hence the recursion.

Definition at line 703 of file glass_table.cc.

References Assert, AssertEq, AssertRel, Glass::BYTES_PER_BLOCK_NUMBER, C, Glass::D2, Glass::K1, Glass::BItem_base< T >::key(), LOGCALL_VOID, Glass::BItem_wr::set_key_and_block(), and Glass::X2.

◆ enter_key_above_leaf()

void GlassTable::enter_key_above_leaf ( Glass::LeafItem  previtem,
Glass::LeafItem  newitem 
)
protected

enter_key_above_leaf(previtem, newitem) is called after a leaf block split.

It enters in the block at level C[1] a separating key for the block at level C[0]. The key itself is newitem.key(). previtem is the preceding item, and at level 1 newitem.key() can be trimmed down to the first point of difference to previtem.key() for entry in C[j].

This code looks longer than it really is. If j exceeds the number of B-tree levels the root block has split and we have to construct a new one, but this is a rare event.

The key is constructed in b, with block number C[0].n as tag, and this is added in with add_item. add_item may itself cause a block split, with a further call to enter_key. Hence the recursion.

Definition at line 647 of file glass_table.cc.

References Assert, AssertEq, AssertRel, Glass::BYTES_PER_BLOCK_NUMBER, C, Glass::compare(), Glass::LeafItem_base< T >::component_of(), Glass::D2, Glass::K1, Glass::LeafItem_base< T >::key(), Glass::Key::length(), LOGCALL_VOID, Glass::BItem_wr::set_truncated_key_and_block(), and Glass::X2.

◆ exists()

bool GlassTable::exists ( ) const

Determine whether the btree exists on disk.

Definition at line 1769 of file glass_table.cc.

References file_exists(), GLASS_TABLE_EXTENSION, LOGCALL, and single_file().

Referenced by GlassDatabase::database_exists().

◆ find()

bool GlassTable::find ( Glass::Cursor C_) const
protected

find(C_) searches for the key of B->kt in the B-tree.

Result is true if found, false otherwise. When false, the B_tree cursor is positioned at the last key in the B-tree <= the search key. Goes to first (null) item in B-tree when key length == 0.

Definition at line 530 of file glass_table.cc.

References LOGCALL, and RETURN.

◆ find_in_branch() [1/2]

int GlassTable::find_in_branch ( const uint8_t *  p,
Glass::LeafItem  item,
int  c 
)
staticprotected

◆ find_in_branch() [2/2]

int GlassTable::find_in_branch ( const uint8_t *  p,
Glass::BItem  item,
int  c 
)
staticprotected

◆ find_in_leaf()

int GlassTable::find_in_leaf ( const uint8_t *  p,
Glass::LeafItem  item,
int  c,
bool &  exact 
)
staticprotected

find_in_leaf(p, key, c, exact) searches for the key in the leaf block at p.

What we get is the directory entry to the last key <= the key being searched for.

The lookup is by binary chop, with i and j set to the left and right ends of the search area. In sequential addition, c will often be the answer, so we test the keys round c and move i and j towards c if possible.

exact is set to true if the match was exact (otherwise exact is unchanged).

Definition at line 418 of file glass_table.cc.

References Assert, AssertRel, Glass::compare(), Glass::D2, Glass::DIR_END(), Glass::DIR_START, Glass::LeafItem_base< T >::get_address(), LOGCALL_STATIC, and RETURN.

◆ flush_db()

void GlassTable::flush_db ( )

Flush any outstanding changes to the DB file of the table.

This must be called before commit, to ensure that the DB file is ready to be switched to a new version by the commit.

Definition at line 1845 of file glass_table.cc.

References Assert, C, faked_root_block, handle, level, LOGCALL_VOID, throw_database_closed(), writable, and write_block().

Referenced by GlassDatabase::compact(), GlassSynonymTable::flush_db(), GlassSpellingTable::flush_db(), GlassCompact::multimerge_postlists(), and GlassDatabase::set_revision_number().

◆ form_key()

void GlassTable::form_key ( const std::string &  key) const
protected

Definition at line 1249 of file glass_table.cc.

References LOGCALL_VOID.

◆ get_entry_count()

glass_tablesize_t GlassTable::get_entry_count ( ) const
inline

Return a count of the number of entries in the table.

The count does not include the ever-present item with null key.

Use empty() if you only want to know if the table is empty or not.

Returns
The number of entries in the table.

Definition at line 676 of file glass_table.h.

Referenced by Inverter::has_positions(), and main().

◆ get_exact_entry()

bool GlassTable::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.

If the key is found in the table, then the tag is copied to tag. If the key is not found tag is left unchanged.

The result is true iff the specified key is found in the Btree.

Parameters
keyThe key to look for in the table.
tagA tag object to fill with the value if found.
Returns
true if key is found in table, false if key is not found in table.

Definition at line 1473 of file glass_table.cc.

References Assert, C, GLASS_BTREE_MAX_KEY_LEN, LOGCALL, RETURN, and throw_database_closed().

Referenced by Glass::PostlistChunkWriter::flush(), GlassDocDataTable::get_document_data(), GlassPostListTable::get_freqs(), GlassDatabase::get_metadata(), GlassTermList::GlassTermList(), and Inverter::store_positions().

◆ get_flags()

int GlassTable::get_flags ( ) const
inline

◆ get_open_revision_number()

glass_revision_number_t GlassTable::get_open_revision_number ( ) const
inline

Get the revision number at which this table is currently open.

It is possible that there are other, more recent or older revisions available.

Returns
the current revision number.

Definition at line 663 of file glass_table.h.

◆ get_path()

string GlassTable::get_path ( ) const
inline

◆ is_modified()

bool GlassTable::is_modified ( ) const
inline

Determine whether the object contains uncommitted modifications.

Returns
true if there have been modifications since the last the last call to commit().

Definition at line 697 of file glass_table.h.

Referenced by GlassDatabase::apply(), GlassDatabase::compact(), GlassWritableDatabase::has_uncommitted_changes(), GlassSynonymTable::is_modified(), and GlassSpellingTable::is_modified().

◆ is_open()

bool GlassTable::is_open ( ) const
inline

◆ is_writable()

bool GlassTable::is_writable ( ) const
inline

Return true if this table is writable.

Definition at line 509 of file glass_table.h.

References GlassFreeList::commit(), and Xapian::revision().

Referenced by GlassPostList::open_nearby_postlist().

◆ key_exists()

bool GlassTable::key_exists ( const std::string &  key) const

Check if a key exists in the Btree.

This is just like get_exact_entry() except it doesn't read the tag value so is more efficient if you only want to check that the key exists.

Parameters
keyThe key to look for in the table.
Returns
true if key is found in table, false if key is not found in table.

Definition at line 1496 of file glass_table.cc.

References Assert, C, GLASS_BTREE_MAX_KEY_LEN, LOGCALL, and RETURN.

◆ mid_point()

int GlassTable::mid_point ( uint8_t *  p) const
protected

mid_point(p) finds the directory entry in c that determines the approximate mid point of the data in the block at p.

Definition at line 734 of file glass_table.cc.

References Assert, Glass::D2, Glass::DIR_END(), Glass::DIR_START, Glass::GET_LEVEL(), LOGCALL, RETURN, Glass::LeafItem_base< T >::size(), Glass::BItem_base< T >::size(), and Glass::TOTAL_FREE().

◆ next()

bool GlassTable::next ( Glass::Cursor C_,
int  j 
) const
inlineprotected

Definition at line 860 of file glass_table.h.

References dummy.

Referenced by Glass::PostlistChunkReader::is_at_end().

◆ next_default()

bool GlassTable::next_default ( Glass::Cursor C_,
int  j 
) const
protected

◆ next_for_sequential()

bool GlassTable::next_for_sequential ( Glass::Cursor C_,
int  dummy 
) const
protected

◆ open()

void GlassTable::open ( int  flags_,
const RootInfo root_info,
glass_revision_number_t  rev 
)

Open the btree.

Parameters
flags_flags for opening
root_inforoot block info
Exceptions
Xapian::DatabaseCorruptErrorwill be thrown if the table is in a corrupt state.
Xapian::DatabaseOpeningErrorwill be thrown if the table cannot be opened (but is not corrupt - eg, permission problems, not present, etc).

Definition at line 2010 of file glass_table.cc.

References block_size, close(), do_open_to_read(), do_open_to_write(), flags, Glass::RootInfo::get_blocksize(), Glass::RootInfo::get_root(), LOGCALL_VOID, root, and writable.

Referenced by GlassDatabase::compact(), main(), GlassDatabase::modifications_failed(), GlassPostListTable::open(), and GlassDatabase::open_tables().

◆ operator=()

GlassTable& GlassTable::operator= ( const GlassTable )
private

Assignment not allowed.

◆ prev()

bool GlassTable::prev ( Glass::Cursor C_,
int  j 
) const
inlineprotected

Definition at line 854 of file glass_table.h.

◆ prev_default()

bool GlassTable::prev_default ( Glass::Cursor C_,
int  j 
) const
protected

◆ prev_for_sequential()

bool GlassTable::prev_for_sequential ( Glass::Cursor C_,
int  dummy 
) const
protected

◆ read_block()

void GlassTable::read_block ( uint4  n,
uint8_t *  p 
) const
protected

◆ read_root()

void GlassTable::read_root ( )
protected

◆ read_tag()

bool GlassTable::read_tag ( Glass::Cursor C_,
std::string *  tag,
bool  keep_compressed 
) const

Read the tag value for the key pointed to by cursor C_.

Parameters
keep_compressedDon't uncompress the tag - e.g. useful if it's just being opaquely copied.
Returns
true if current_tag holds compressed data (always false if keep_compressed was false).

Definition at line 1509 of file glass_table.cc.

References Glass::LeafItem_base< T >::append_chunk(), Glass::LeafItem_base< T >::decompress_chunk(), Glass::LeafItem_base< T >::get_compressed(), Glass::LeafItem_base< T >::last_component(), LOGCALL, and RETURN.

◆ readahead_key()

bool GlassTable::readahead_key ( const string &  key) const

◆ set_changes()

void GlassTable::set_changes ( GlassChanges changes)
inline

Set the GlassChanges object to write changed blocks to.

The GlassChanges object is not owned by the table, so the table must not delete it.

Definition at line 721 of file glass_table.h.

References throw_database_closed().

Referenced by GlassDatabase::apply(), GlassDatabase::modifications_failed(), and GlassDatabase::open_tables().

◆ set_full_compaction()

void GlassTable::set_full_compaction ( bool  parity)

Definition at line 1553 of file glass_table.cc.

References Assert, and LOGCALL_VOID.

Referenced by GlassDatabase::compact().

◆ set_max_item_size()

void GlassTable::set_max_item_size ( size_t  block_capacity)
inline

Set the maximum item size given the block capacity.

At least this many items of maximum size must fit into a block. The default is BLOCK_CAPACITY (which is currently 4).

Definition at line 704 of file glass_table.h.

References Glass::BLOCK_CAPACITY, Glass::D2, Glass::DIR_START, and Glass::MAX_ITEM_SIZE.

Referenced by GlassDatabase::compact().

◆ set_overwritten()

void GlassTable::set_overwritten ( ) const
protected

Definition at line 289 of file glass_table.cc.

References LOGCALL_VOID.

Referenced by next_for_sequential(), and prev_for_sequential().

◆ single_file()

bool GlassTable::single_file ( ) const
inlineprotected

Definition at line 849 of file glass_table.h.

Referenced by close(), do_open_to_read(), and exists().

◆ split_root()

void GlassTable::split_root ( uint4  split_n)
protected

Btree needs to gain a new level to insert more items: so split root block and construct a new one.

Definition at line 600 of file glass_table.cc.

References C, Glass::DIR_START, Glass::BItem_wr::form_null_key(), GLASS_BTREE_CURSOR_LEVELS, LOGCALL_VOID, Glass::SET_DIR_END(), Glass::SET_LEVEL(), Glass::SET_REVISION(), and STRINGIZE.

◆ sync()

bool GlassTable::sync ( )
inline

Definition at line 535 of file glass_table.h.

References Xapian::DB_NO_SYNC, and io_sync().

Referenced by GlassDatabase::compact(), and GlassDatabase::set_revision_number().

◆ throw_database_closed()

void GlassTable::throw_database_closed ( )
static

◆ write_block()

void GlassTable::write_block ( uint4  n,
const uint8_t *  p,
bool  appending = false 
) const
protected

write_block(n, p, appending) writes block n in the DB file from address p.

If appending is true (not specified it defaults to false), then this indicates that we've added data to a block in space which was previously unused, and are writing the block back in place - we use this to add free list entries (the information about where the freelist data for a revision begins and ends is stored in the "iamglass" file). We don't currently use this flag for much, but it signifies that we don't risk invalidating any existing revisions, which may be useful information.

Definition at line 191 of file glass_table.cc.

References Assert, AssertEqParanoid, AssertRel, Xapian::DB_DANGEROUS, Glass::DOCDATA, io_write_block(), LOGCALL_VOID, pack_uint(), Glass::POSITION, Glass::POSTLIST, Glass::REVISION(), Glass::SPELLING, Glass::SYNONYM, and Glass::TERMLIST.

Referenced by flush_db(), and GlassFreeList::write_block().

Friends And Related Function Documentation

◆ GlassCursor

friend class GlassCursor
friend

Definition at line 426 of file glass_table.h.

◆ GlassFreeList

friend class GlassFreeList
friend

Definition at line 427 of file glass_table.h.

Member Data Documentation

◆ block_size

unsigned int GlassTable::block_size
protected

block size of the B tree in bytes

Definition at line 767 of file glass_table.h.

Referenced by cancel(), commit(), create_and_open(), next_default(), next_for_sequential(), open(), prev_default(), and prev_for_sequential().

◆ Btree_modified

bool GlassTable::Btree_modified
mutableprotected

Set to true the first time the B-tree is modified.

Definition at line 829 of file glass_table.h.

Referenced by cancel(), and commit().

◆ buffer

uint8_t* GlassTable::buffer
protected

buffer of size block_size for reforming blocks

Definition at line 802 of file glass_table.h.

Referenced by close().

◆ C

Glass::Cursor GlassTable::C[GLASS_BTREE_CURSOR_LEVELS]
mutableprotected

Definition at line 884 of file glass_table.h.

Referenced by cancel(), close(), commit(), flush_db(), next_for_sequential(), and prev_for_sequential().

◆ changed_c

int GlassTable::changed_c
protected

directory offset corresponding to last block to be changed by an addition

Definition at line 823 of file glass_table.h.

Referenced by cancel(), and commit().

◆ changed_n

uint4 GlassTable::changed_n
protected

the last block to be changed by an addition

Definition at line 819 of file glass_table.h.

Referenced by cancel(), and commit().

◆ changes_obj

GlassChanges* GlassTable::changes_obj
protected

The GlassChanges object to write block changes to.

If NULL, no changes will be written.

Definition at line 847 of file glass_table.h.

◆ comp_stream

CompressionStream GlassTable::comp_stream
mutableprotected

Definition at line 896 of file glass_table.h.

◆ compress_min

uint4 GlassTable::compress_min
protected

Minimum size tag to try compressing (0 for no compression).

Definition at line 894 of file glass_table.h.

Referenced by create_and_open().

◆ cursor_created_since_last_modification

bool GlassTable::cursor_created_since_last_modification
mutableprotected

Flag for tracking when cursors need to rebuild.

Definition at line 838 of file glass_table.h.

Referenced by cancel().

◆ cursor_version

unsigned long GlassTable::cursor_version
protected

Version count for tracking when cursors need to rebuild.

Definition at line 841 of file glass_table.h.

Referenced by cancel().

◆ faked_root_block

bool GlassTable::faked_root_block
protected

true if the root block is faked (not written to disk).

false otherwise. This is true when the btree hasn't been modified yet.

Definition at line 776 of file glass_table.h.

Referenced by cancel(), commit(), and flush_db().

◆ flags

int GlassTable::flags
protected

Flags like DB_NO_SYNC and DB_DANGEROUS.

Definition at line 770 of file glass_table.h.

Referenced by cancel(), create_and_open(), and open().

◆ free_list

GlassFreeList GlassTable::free_list
protected

List of free blocks.

Definition at line 805 of file glass_table.h.

Referenced by cancel(), commit(), and next_for_sequential().

◆ full_compaction

bool GlassTable::full_compaction
protected

set to true when full compaction is to be achieved

Definition at line 832 of file glass_table.h.

◆ handle

int GlassTable::handle
protected

File descriptor of the table.

If close() has been called, this will be -2.

If the table is lazily created and doesn't yet exist, this will be -1 (for a multi-file database) or -3-fd (for a single-file database).

Definition at line 790 of file glass_table.h.

Referenced by cancel(), close(), commit(), create_and_open(), do_open_to_read(), and flush_db().

◆ item_count

glass_tablesize_t GlassTable::item_count
protected

keeps a count of the number of items in the B-tree.

Definition at line 764 of file glass_table.h.

Referenced by cancel(), and commit().

◆ kt

Glass::LeafItem_wr GlassTable::kt
mutableprotected

buffer of size block_size for making up key-tag items

Definition at line 799 of file glass_table.h.

Referenced by close().

◆ last_readahead

uint4 GlassTable::last_readahead
mutableprotected

Last block readahead_key() preread.

Definition at line 902 of file glass_table.h.

◆ lazy

bool GlassTable::lazy
protected

If true, don't create the table until it's needed.

Definition at line 899 of file glass_table.h.

Referenced by create_and_open(), and do_open_to_read().

◆ level

int GlassTable::level
protected

number of levels, counting from 0

Definition at line 793 of file glass_table.h.

Referenced by cancel(), close(), commit(), flush_db(), next_default(), next_for_sequential(), prev_default(), and prev_for_sequential().

◆ max_item_size

size_t GlassTable::max_item_size
protected

maximum size of an item (key-tag pair)

Definition at line 826 of file glass_table.h.

◆ name

std::string GlassTable::name
protected

The path name of the B tree.

For a single-file database, this will be empty.

Definition at line 811 of file glass_table.h.

Referenced by do_open_to_read().

◆ offset

off_t GlassTable::offset
protected

offset to start of table in file.

Definition at line 905 of file glass_table.h.

◆ revision_number

glass_revision_number_t GlassTable::revision_number
protected

revision number of the opened B-tree.

Definition at line 761 of file glass_table.h.

Referenced by cancel(), commit(), do_open_to_read(), next_for_sequential(), and prev_for_sequential().

◆ root

uint4 GlassTable::root
protected

the root block of the B-tree

Definition at line 796 of file glass_table.h.

Referenced by cancel(), commit(), and open().

◆ seq_count

int GlassTable::seq_count
protected

count of the number of successive instances of purely sequential addition, starting at SEQ_START_POINT (neg) and going up to zero.

Definition at line 816 of file glass_table.h.

Referenced by cancel(), and commit().

◆ sequential

bool GlassTable::sequential
protected

true iff the data has been written in a single write in sequential order.

Definition at line 781 of file glass_table.h.

Referenced by cancel(), and commit().

◆ split_p

uint8_t* GlassTable::split_p
protected

Buffer used when splitting a block.

This buffer holds the split off part of the block. It's only used when updating (in GlassTable::add_item().

Definition at line 891 of file glass_table.h.

Referenced by close().

◆ tablename

const char* GlassTable::tablename
protected

The name of the table (used when writing changesets).

Definition at line 758 of file glass_table.h.

◆ writable

bool GlassTable::writable
protected

Set to true when the database is opened to write.

Definition at line 835 of file glass_table.h.

Referenced by cancel(), commit(), create_and_open(), flush_db(), next_for_sequential(), open(), and prev_for_sequential().


The documentation for this class was generated from the following files: