Tkrzw
Public Types | Public Member Functions | Static Public Member Functions | List of all members
tkrzw::HashRecord Class Referencefinal

Key and value record structure in the file hash database. More...

#include <tkrzw_dbm_hash_impl.h>

Public Types

enum  OperationType : int32_t { OP_VOID = 0, OP_SET = 1, OP_REMOVE = 2 }
 Enumeration for operation types. More...
 

Public Member Functions

 HashRecord (File *file, int32_t offset_width, int32_t align_pow)
 Constructor. More...
 
 ~HashRecord ()
 Destructor. More...
 
OperationType GetOperationType () const
 Gets the operation type of the record. More...
 
std::string_view GetKey () const
 Gets the key data. More...
 
std::string_view GetValue () const
 Gets the value data. More...
 
int64_t GetChildOffset () const
 Gets the offset of the child record. More...
 
int32_t GetWholeSize () const
 Gets the whole size of the record. More...
 
Status ReadMetadataKey (int64_t offset)
 Read the metadata and the key. More...
 
Status ReadBody ()
 Read the body data and fill all the properties. More...
 
void SetData (OperationType type, int32_t ideal_whole_size, const char *key_ptr, int32_t key_size, const char *value_ptr, int32_t value_size, int64_t child_offset)
 Sets the actual data of the record. More...
 
Status Write (int64_t offset, int64_t *new_offset) const
 Writes the record in the file. More...
 
Status WriteChildOffset (int64_t offset, int64_t child_offset)
 Writes the child offset of the record in the file. More...
 
Status FindNextOffset (int64_t offset, int64_t *next_offset)
 Finds the next record offset by heuristics. More...
 

Static Public Member Functions

static Status ReplayOperations (File *file, DBM::RecordProcessor *proc, int64_t record_base, int32_t offset_width, int32_t align_pow, bool skip_broken_records, int64_t end_offset)
 Replays operations applied on a hash database file. More...
 
static Status ExtractOffsets (File *in_file, File *out_file, int64_t record_base, int32_t offset_width, int32_t align_pow, bool skip_broken_records, int64_t end_offset)
 Extracts a sequence of offsets from a file. More...
 

Detailed Description

Key and value record structure in the file hash database.

Member Enumeration Documentation

◆ OperationType

Enumeration for operation types.

Enumerator
OP_VOID 

Operation to do nothing.

OP_SET 

Operation to set a record.

OP_REMOVE 

Operation to remove a record.

Constructor & Destructor Documentation

◆ HashRecord()

tkrzw::HashRecord::HashRecord ( File file,
int32_t  offset_width,
int32_t  align_pow 
)

Constructor.

Parameters
fileThe pointer to the file object.
offset_widthThe width of the offset data.
align_powThe alignment power.

◆ ~HashRecord()

tkrzw::HashRecord::~HashRecord ( )

Destructor.

Member Function Documentation

◆ GetOperationType()

OperationType tkrzw::HashRecord::GetOperationType ( ) const

Gets the operation type of the record.

Returns
The operation type of the record.

◆ GetKey()

std::string_view tkrzw::HashRecord::GetKey ( ) const

Gets the key data.

Returns
The key data.

◆ GetValue()

std::string_view tkrzw::HashRecord::GetValue ( ) const

Gets the value data.

Returns
The value data. The data might be nullptr if the body hasn't been read.

◆ GetChildOffset()

int64_t tkrzw::HashRecord::GetChildOffset ( ) const

Gets the offset of the child record.

Returns
The offset of the child record.

◆ GetWholeSize()

int32_t tkrzw::HashRecord::GetWholeSize ( ) const

Gets the whole size of the record.

Returns
The whole size of the record. It might be zero if the body hasn't been read.

◆ ReadMetadataKey()

Status tkrzw::HashRecord::ReadMetadataKey ( int64_t  offset)

Read the metadata and the key.

Parameters
offsetThe offset of the record.
Returns
The result status. If successful, the key data is always read. However the value data and the whole size is not always read. To read them, call ReadBody.

◆ ReadBody()

Status tkrzw::HashRecord::ReadBody ( )

Read the body data and fill all the properties.

Returns
The result status.

◆ SetData()

void tkrzw::HashRecord::SetData ( OperationType  type,
int32_t  ideal_whole_size,
const char *  key_ptr,
int32_t  key_size,
const char *  value_ptr,
int32_t  value_size,
int64_t  child_offset 
)

Sets the actual data of the record.

Parameters
typeAn operation type.
ideal_whole_sizeThe ideal size of the storage space.
key_ptrThe pointer to a key buffer.
key_sizeThe size of the key buffer.
value_ptrThe pointer to a value buffer.
value_sizeThe size of the value buffer.
child_offsetThe offset of the child record or zero for nothing.

◆ Write()

Status tkrzw::HashRecord::Write ( int64_t  offset,
int64_t *  new_offset 
) const

Writes the record in the file.

Parameters
offsetThe offset of the record. If it is negative, the data is appended at the end of the file.
new_offsetThe pointer to an integer to store the offset of the appended data. If can be nullptr if the given offset is not negative.
Returns
The result status.

◆ WriteChildOffset()

Status tkrzw::HashRecord::WriteChildOffset ( int64_t  offset,
int64_t  child_offset 
)

Writes the child offset of the record in the file.

Parameters
offsetThe offset of the record to update.
child_offsetThe offset of the child record.
Returns
The result status.

◆ FindNextOffset()

Status tkrzw::HashRecord::FindNextOffset ( int64_t  offset,
int64_t *  next_offset 
)

Finds the next record offset by heuristics.

Parameters
offsetThe current offset.
next_offsetThe pointer to an integer to store the next offset.
Returns
The result status.

◆ ReplayOperations()

static Status tkrzw::HashRecord::ReplayOperations ( File file,
DBM::RecordProcessor proc,
int64_t  record_base,
int32_t  offset_width,
int32_t  align_pow,
bool  skip_broken_records,
int64_t  end_offset 
)
static

Replays operations applied on a hash database file.

Parameters
fileA file object having opened the database file.
procThe pointer to the processor object.
record_baseThe record base offset.
offset_widthThe offset width.
align_powThe alignment power.
skip_broken_recordsIf true, the operation continues even if there are broken records which can be skipped.
end_offsetThe exclusive end offset of records to read. Negative means unlimited.
Returns
The result status. For each setting operation, ProcessFull of the processer is called. For each removing operation, ProcessEmpty of the processor is called. If they return a value other than NOOP, the iteration is cancelled.

◆ ExtractOffsets()

static Status tkrzw::HashRecord::ExtractOffsets ( File in_file,
File out_file,
int64_t  record_base,
int32_t  offset_width,
int32_t  align_pow,
bool  skip_broken_records,
int64_t  end_offset 
)
static

Extracts a sequence of offsets from a file.

Parameters
in_fileA file object having opened the input database file.
out_fileA file object having opened as a writer to store the output.
record_baseThe record base offset.
offset_widthThe offset width.
align_powThe alignment power.
skip_broken_recordsIf true, the operation continues even if there are broken records which can be skipped.
end_offsetThe exclusive end offset of records to read. Negative means unlimited.
Returns
The result status.