record

package
v0.1.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 3, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

View Source
const DiskRecordHeaderSizeBytes = 20

CRC (4) + Timestamp (8) + KeySize (4) + ValueSize (4)

View Source
const HintRecordHeaderSizeBytes = 24

KeySize (4) + FileNameSize (4) + Offset (4) + ValueSize (4) + Timestamp (8)

Variables

This section is empty.

Functions

func CalculateCRC

func CalculateCRC(key, value []byte) uint32

CalculateCRC computes the CRC32 checksum of the key-value pair using IEEE polynomial.

func EncodeHintRecordToBytes

func EncodeHintRecordToBytes(hintRecord *HintRecord) ([]byte, error)

EncodeHintRecordToBytes serializes a HintRecord into its on-disk binary form.

Hint records are written sequentially into hint files and are used only during startup for faster KeyDir reconstruction.

func EncodeRecordToBytes

func EncodeRecordToBytes(record *DiskRecord) ([]byte, error)

EncodeRecordToBytes serializes a DiskRecord into its on-disk binary form.

The returned byte slice is suitable for direct append-only writing to a Bitcask datafile.

func ValidateCRC

func ValidateCRC(key, value []byte, checksum uint32) bool

ValidateCRC returns true if the provided checksum matches the computed CRC32 of the key-value pair

Types

type DiskRecord

type DiskRecord struct {
	CRC       uint32 // CRC checksum of Key and Value
	Timestamp int64  // Unix timestamp in nanoseconds
	KeySize   uint32 // Length of Key in bytes
	ValueSize uint32 // Length of Value in bytes (0 indicates tombstone)
	Key       []byte // Raw key bytes
	Value     []byte // Raw value bytes
}

DiskRecord represents a single append-only record stored in a Bitcask datafile.

DiskRecords are written sequentially and never modified in place. Newer versions of a key supersede older ones based on the Timestamp.

Layout on disk (little-endian):

CRC (4 bytes)
Timestamp (8 bytes)
KeySize (4 bytes)
ValueSize (4 bytes)
Key (KeySize bytes)
Value (ValueSize bytes)

A record with ValueSize == 0 represents a tombstone (logical delete).

func CreateRecord

func CreateRecord(key, value string) DiskRecord

CreateRecord constructs a new DiskRecord for the given key and value.

It computes the CRC over the key and value and assigns the current time as the record timestamp.

func CreateTombstoneRecord

func CreateTombstoneRecord(key string) DiskRecord

CreateTombstoneRecord constructs a DiskRecord representing a logical delete (tombstone) for the given key.

Tombstone records have ValueSize == 0 and are used to invalidate older versions of the key during reads and compaction.

func DecodeRecordFromBytes

func DecodeRecordFromBytes(data []byte) (*DiskRecord, error)

DecodeRecordFromBytes deserializes a DiskRecord from its binary form.

The input byte slice must contain a complete record, including the header, key, and value. Tombstone records (ValueSize == 0) are rejected.

type HintRecord

type HintRecord struct {
	KeySize      uint32 // Length of Key in bytes
	FileNameSize uint32 // Length of FileName in bytes
	Offset       uint32 // Byte offset of the record in the datafile
	ValueSize    uint32 // Length of Value in bytes
	Timestamp    int64  // Timestamp of the record
	Key          []byte // Raw key bytes
	FileName     []byte // Datafile name containing the record
}

HintRecord represents a compact index entry stored in a hint file.

Hint files are advisory structures used to reconstruct the in-memory KeyDir quickly on startup without scanning full datafiles.

Layout on disk (little-endian):

KeySize (4 bytes)
FileNameSize (4 bytes)
Offset (4 bytes)
ValueSize (4 bytes)
Timestamp (8 bytes)
Key (KeySize bytes)
FileName (FileNameSize bytes)

func DecodeHintRecordFromBytes

func DecodeHintRecordFromBytes(data []byte) (*HintRecord, error)

DecodeHintRecordFromBytes deserializes a HintRecord from its binary form.

The input byte slice must contain a complete hint record, including the header, key, and file name.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL