Tkrzw
Public Types | Public Member Functions | Static Public Member Functions | Static Public Attributes | 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 , OP_ADD = 3 }
 Enumeration for operation types. More...
 

Public Member Functions

 HashRecord (File *file, int32_t crc_width, 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, int32_t min_read_size)
 Reads the metadata and the key. More...
 
Status ReadBody ()
 Reads the body data and fill all the properties. More...
 
void SetBodyWithNullValue ()
 Sets the body data with the value filled with null codes. More...
 
Status CheckCRC ()
 Check the CRC. 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, int32_t min_read_size, 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 crc_width, Compressor *compressor, int32_t offset_width, int32_t align_pow, int32_t min_read_size, 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 crc_width, 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...
 

Static Public Attributes

static constexpr int64_t MAX_KEY_SIZE = 8 * (1LL << 20) - 1
 The maximum key size. More...
 
static constexpr int64_t MAX_VALUE_SIZE = 2 * (1LL << 30) - 1
 The maximum value size. More...
 
static constexpr int64_t META_MIN_READ_SIZE = 48
 The minimum size to read the meta data. 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 the value of an existing record.

OP_REMOVE 

Operation to remove an existing record.

OP_ADD 

Operation to add a new record.

Constructor & Destructor Documentation

◆ HashRecord()

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

Constructor.

Parameters
fileThe pointer to the file object.
crc_widthThe width of the CRC value.
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,
int32_t  min_read_size 
)

Reads the metadata and the key.

Parameters
offsetThe offset of the record.
min_read_sizeThe minimum reading size.
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 ( )

Reads the body data and fill all the properties.

Returns
The result status.

◆ SetBodyWithNullValue()

void tkrzw::HashRecord::SetBodyWithNullValue ( )

Sets the body data with the value filled with null codes.

◆ CheckCRC()

Status tkrzw::HashRecord::CheckCRC ( )

Check the CRC.

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,
int32_t  min_read_size,
int64_t *  next_offset 
)

Finds the next record offset by heuristics.

Parameters
offsetThe current offset.
min_read_sizeThe minimum reading size.
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  crc_width,
Compressor compressor,
int32_t  offset_width,
int32_t  align_pow,
int32_t  min_read_size,
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.
crc_widthThe width of the CRC value.
compressorThe compressor object.
offset_widthThe offset width.
align_powThe alignment power.
min_read_sizeThe minimum reading size.
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  crc_width,
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.
crc_widthThe width of the CRC value.
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.

Member Data Documentation

◆ MAX_KEY_SIZE

constexpr int64_t tkrzw::HashRecord::MAX_KEY_SIZE = 8 * (1LL << 20) - 1
staticconstexpr

The maximum key size.

◆ MAX_VALUE_SIZE

constexpr int64_t tkrzw::HashRecord::MAX_VALUE_SIZE = 2 * (1LL << 30) - 1
staticconstexpr

The maximum value size.

◆ META_MIN_READ_SIZE

constexpr int64_t tkrzw::HashRecord::META_MIN_READ_SIZE = 48
staticconstexpr

The minimum size to read the meta data.