Documentation
¶
Index ¶
- Constants
- Variables
- func EntryCompare(a, b *Entry) int
- func ListSSTables(dir string) ([][]*SSTable, error)
- func SSTablePath(dir string, level int, id int) string
- type BlockCache
- type BloomFilter
- func (bf *BloomFilter) Add(key []byte)
- func (bf *BloomFilter) Contains(key []byte) bool
- func (bf *BloomFilter) EstimateFalsePositiveRate(itemCount int) float64
- func (bf *BloomFilter) HashCount() int
- func (bf *BloomFilter) MarshalBinary() []byte
- func (bf *BloomFilter) MayContain(key []byte) bool
- func (bf *BloomFilter) Merge(other *BloomFilter) error
- func (bf *BloomFilter) Reset()
- func (bf *BloomFilter) Size() int
- func (bf *BloomFilter) UnmarshalBinary(data []byte) error
- type BloomFilterError
- type CompactionPlan
- type CompactionStats
- type CompactionStrategy
- type Compactor
- type Entry
- type IndexEntry
- type LSMOptions
- type LSMStats
- type LSMStatsSnapshot
- type LSMStorage
- func (lsm *LSMStorage) Close() error
- func (lsm *LSMStorage) Delete(key []byte) error
- func (lsm *LSMStorage) Get(key []byte) ([]byte, bool)
- func (lsm *LSMStorage) GetStats() LSMStatsSnapshot
- func (lsm *LSMStorage) PrintStats()
- func (lsm *LSMStorage) Put(key, value []byte) error
- func (lsm *LSMStorage) Scan(start, end []byte) (map[string][]byte, error)
- func (lsm *LSMStorage) Sync() error
- type LeveledCompactionStrategy
- type MappedSSTable
- type MemTable
- func (mt *MemTable) Clear()
- func (mt *MemTable) Delete(key []byte) error
- func (mt *MemTable) Get(key []byte) (*Entry, bool)
- func (mt *MemTable) IsFull() bool
- func (mt *MemTable) Iterator() []*Entry
- func (mt *MemTable) Put(key, value []byte) error
- func (mt *MemTable) Scan(start, end []byte) []*Entry
- func (mt *MemTable) Size() int
- type MergeIterator
- type SSTable
- type SSTableHeader
- type SSTableIterator
Constants ¶
const ( SSTableMagic = 0x53535442 // "SSTB" SSTableVersion = 1 IndexInterval = 128 // Create index entry every N keys )
Variables ¶
var ErrIncompatibleFilters = &BloomFilterError{"incompatible bloom filters"}
Functions ¶
func ListSSTables ¶
ListSSTables returns all SSTable files in a directory. Returns partial results even if some SSTables fail to open, along with an error describing the failures. Callers should check both the result and error.
Types ¶
type BlockCache ¶
type BlockCache struct {
// contains filtered or unexported fields
}
BlockCache is an LRU cache for frequently accessed data blocks
func NewBlockCache ¶
func NewBlockCache(capacity int) *BlockCache
NewBlockCache creates a new LRU block cache
func (*BlockCache) Delete ¶
func (bc *BlockCache) Delete(key string)
Delete removes an entry from the cache
func (*BlockCache) Get ¶
func (bc *BlockCache) Get(key string) ([]byte, bool)
Get retrieves a value from the cache
func (*BlockCache) Put ¶
func (bc *BlockCache) Put(key string, value []byte)
Put adds a value to the cache
func (*BlockCache) Size ¶
func (bc *BlockCache) Size() int
Size returns the current number of entries
func (*BlockCache) Stats ¶
func (bc *BlockCache) Stats() (hits, misses int64, hitRate float64)
Stats returns cache statistics
type BloomFilter ¶
type BloomFilter struct {
// contains filtered or unexported fields
}
BloomFilter is a probabilistic data structure for set membership testing - False positives possible (may say key exists when it doesn't) - False negatives impossible (if it says key doesn't exist, it definitely doesn't)
func NewBloomFilter ¶
func NewBloomFilter(expectedItems int, falsePositiveRate float64) *BloomFilter
NewBloomFilter creates a Bloom filter optimized for the given parameters expectedItems: number of items to store falsePositiveRate: desired false positive rate (e.g., 0.01 for 1%)
func (*BloomFilter) Contains ¶
func (bf *BloomFilter) Contains(key []byte) bool
Contains is an alias for MayContain for better API usability
func (*BloomFilter) EstimateFalsePositiveRate ¶
func (bf *BloomFilter) EstimateFalsePositiveRate(itemCount int) float64
EstimateFalsePositiveRate estimates current false positive rate
func (*BloomFilter) HashCount ¶
func (bf *BloomFilter) HashCount() int
HashCount returns the number of hash functions
func (*BloomFilter) MarshalBinary ¶
func (bf *BloomFilter) MarshalBinary() []byte
MarshalBinary serializes the Bloom filter
func (*BloomFilter) MayContain ¶
func (bf *BloomFilter) MayContain(key []byte) bool
MayContain checks if a key might be in the set Returns true if key might exist (with false positive rate) Returns false if key definitely doesn't exist
func (*BloomFilter) Merge ¶
func (bf *BloomFilter) Merge(other *BloomFilter) error
Merge combines another Bloom filter into this one (OR operation) Both filters must have the same size and hash count
func (*BloomFilter) Size ¶
func (bf *BloomFilter) Size() int
Size returns the size of the filter in bits
func (*BloomFilter) UnmarshalBinary ¶
func (bf *BloomFilter) UnmarshalBinary(data []byte) error
UnmarshalBinary deserializes the Bloom filter
type BloomFilterError ¶
type BloomFilterError struct {
// contains filtered or unexported fields
}
func (*BloomFilterError) Error ¶
func (e *BloomFilterError) Error() string
type CompactionPlan ¶
CompactionPlan describes which SSTables to compact
type CompactionStats ¶
type CompactionStats struct {
TotalCompactions int64
BytesRead int64
BytesWritten int64
KeysRemoved int64 // Deleted or duplicate keys
}
CompactionStats tracks compaction metrics
type CompactionStrategy ¶
type CompactionStrategy interface {
SelectCompaction(levels [][]*SSTable) *CompactionPlan
}
CompactionStrategy defines how SSTables are compacted
type Compactor ¶
type Compactor struct {
// contains filtered or unexported fields
}
Compactor performs SSTable compaction
func NewCompactor ¶
func NewCompactor(dataDir string, strategy CompactionStrategy) *Compactor
NewCompactor creates a new compactor
func (*Compactor) CleanupOldSSTables ¶
CleanupOldSSTables removes old SSTables after compaction. Continues on errors to avoid leaving the LSM tree in an inconsistent state. Returns an error aggregating all deletion failures.
type Entry ¶
type Entry struct {
Key []byte
Value []byte
Timestamp int64
Deleted bool // Tombstone for deletions
}
Entry represents a key-value pair with metadata
type IndexEntry ¶
IndexEntry represents an entry in the sparse index
type LSMOptions ¶
type LSMOptions struct {
DataDir string
MemTableSize int // Bytes (default 4MB)
CompactionStrategy CompactionStrategy
EnableAutoCompaction bool
}
LSMOptions configures LSM storage
func DefaultLSMOptions ¶
func DefaultLSMOptions(dataDir string) LSMOptions
DefaultLSMOptions returns default LSM configuration
type LSMStats ¶
type LSMStats struct {
// High-frequency counters use atomics (lock-free)
WriteCount atomic.Int64
ReadCount atomic.Int64
FlushCount atomic.Int64
CompactionCount atomic.Int64
BytesWritten atomic.Int64
BytesRead atomic.Int64
MemTableSize int
SSTableCount int
Level0FileCount int
// contains filtered or unexported fields
}
LSMStats tracks LSM storage statistics using lock-free atomic counters for high-frequency operations (reads/writes) to avoid contention.
type LSMStatsSnapshot ¶
type LSMStatsSnapshot struct {
WriteCount int64
ReadCount int64
FlushCount int64
CompactionCount int64
BytesWritten int64
BytesRead int64
MemTableSize int
SSTableCount int
Level0FileCount int
}
LSMStatsSnapshot is a point-in-time snapshot of LSM statistics
type LSMStorage ¶
type LSMStorage struct {
// contains filtered or unexported fields
}
LSMStorage is the main LSM-tree storage engine
func NewLSMStorage ¶
func NewLSMStorage(opts LSMOptions) (*LSMStorage, error)
NewLSMStorage creates a new LSM storage engine
func (*LSMStorage) Close ¶
func (lsm *LSMStorage) Close() error
Close flushes pending writes and stops background workers
func (*LSMStorage) Delete ¶
func (lsm *LSMStorage) Delete(key []byte) error
Delete removes a key (writes tombstone)
func (*LSMStorage) Get ¶
func (lsm *LSMStorage) Get(key []byte) ([]byte, bool)
Get retrieves a value by key
func (*LSMStorage) GetStats ¶
func (lsm *LSMStorage) GetStats() LSMStatsSnapshot
GetStats returns current statistics as a snapshot
func (*LSMStorage) PrintStats ¶
func (lsm *LSMStorage) PrintStats()
PrintStats prints storage statistics
func (*LSMStorage) Put ¶
func (lsm *LSMStorage) Put(key, value []byte) error
Put writes a key-value pair
func (*LSMStorage) Scan ¶
func (lsm *LSMStorage) Scan(start, end []byte) (map[string][]byte, error)
Scan returns all key-value pairs in range [start, end)
func (*LSMStorage) Sync ¶
func (lsm *LSMStorage) Sync() error
Sync forces a flush of the current memtable to disk
type LeveledCompactionStrategy ¶
type LeveledCompactionStrategy struct {
Level0FileLimit int // Max files in L0 before compaction
LevelSizeRatio float64 // Size ratio between levels (default 10.0)
MaxLevels int // Maximum number of levels
}
LeveledCompactionStrategy implements leveled compaction (like LevelDB/RocksDB) - Level 0: Multiple overlapping SSTables (from MemTable flushes) - Level 1+: Non-overlapping SSTables, size increases by 10x per level
func DefaultLeveledCompaction ¶
func DefaultLeveledCompaction() *LeveledCompactionStrategy
DefaultLeveledCompaction returns default leveled compaction config
func (*LeveledCompactionStrategy) SelectCompaction ¶
func (lcs *LeveledCompactionStrategy) SelectCompaction(levels [][]*SSTable) *CompactionPlan
SelectCompaction determines which SSTables to compact
type MappedSSTable ¶
type MappedSSTable struct {
// contains filtered or unexported fields
}
MappedSSTable is a memory-mapped SSTable for fast reads
func OpenMappedSSTable ¶
func OpenMappedSSTable(path string) (*MappedSSTable, error)
OpenMappedSSTable opens an SSTable using memory-mapped I/O
func (*MappedSSTable) Close ¶
func (sst *MappedSSTable) Close() error
Close closes the memory-mapped file
func (*MappedSSTable) Get ¶
func (sst *MappedSSTable) Get(key []byte) (*Entry, bool)
Get retrieves a value by key using memory-mapped I/O
func (*MappedSSTable) Iterator ¶
func (sst *MappedSSTable) Iterator() ([]*Entry, error)
Iterator returns all entries
type MemTable ¶
type MemTable struct {
// contains filtered or unexported fields
}
MemTable is an in-memory write buffer using a sorted map
type MergeIterator ¶
type MergeIterator struct {
// contains filtered or unexported fields
}
MergeIterator merges multiple sorted iterators
func NewMergeIterator ¶
func NewMergeIterator(sstables []*SSTable) (*MergeIterator, error)
NewMergeIterator creates an iterator that merges multiple SSTables
func (*MergeIterator) Next ¶
func (mi *MergeIterator) Next() (*Entry, bool)
Next returns the next entry in sorted order across all iterators
type SSTable ¶
type SSTable struct {
// contains filtered or unexported fields
}
SSTable represents a Sorted String Table on disk
func NewSSTable ¶
NewSSTable creates a new SSTable from MemTable entries
func OpenSSTable ¶
OpenSSTable opens an existing SSTable
type SSTableHeader ¶
SSTableHeader represents the header of an SSTable file
type SSTableIterator ¶
type SSTableIterator struct {
// contains filtered or unexported fields
}
SSTableIterator iterates over an SSTable
func NewSSTableIterator ¶
func NewSSTableIterator(sst *SSTable) (*SSTableIterator, error)
NewSSTableIterator creates an iterator for an SSTable
func (*SSTableIterator) Next ¶
func (it *SSTableIterator) Next() (*Entry, bool)
Next advances the iterator
func (*SSTableIterator) Peek ¶
func (it *SSTableIterator) Peek() (*Entry, bool)
Peek returns current entry without advancing