kevo/pkg/memtable/iterator_adapter.go
Jeremy Tregunna 6fc3be617d
Some checks failed
Go Tests / Run Tests (1.24.2) (push) Has been cancelled
feat: Initial release of kevo storage engine.
Adds a complete LSM-based storage engine with these features:
- Single-writer based architecture for the storage engine
- WAL for durability, and hey it's configurable
- MemTable with skip list implementation for fast read/writes
- SSTable with block-based structure for on-disk level-based storage
- Background compaction with tiered strategy
- ACID transactions
- Good documentation (I hope)
2025-04-20 14:06:50 -06:00

91 lines
1.9 KiB
Go

package memtable
// No imports needed
// IteratorAdapter adapts a memtable.Iterator to the common Iterator interface
type IteratorAdapter struct {
iter *Iterator
}
// NewIteratorAdapter creates a new adapter for a memtable iterator
func NewIteratorAdapter(iter *Iterator) *IteratorAdapter {
return &IteratorAdapter{iter: iter}
}
// SeekToFirst positions the iterator at the first key
func (a *IteratorAdapter) SeekToFirst() {
a.iter.SeekToFirst()
}
// SeekToLast positions the iterator at the last key
func (a *IteratorAdapter) SeekToLast() {
a.iter.SeekToFirst()
// If no items, return early
if !a.iter.Valid() {
return
}
// Store the last key we've seen
var lastKey []byte
// Scan to find the last element
for a.iter.Valid() {
lastKey = a.iter.Key()
a.iter.Next()
}
// Re-position at the last key we found
if lastKey != nil {
a.iter.Seek(lastKey)
}
}
// Seek positions the iterator at the first key >= target
func (a *IteratorAdapter) Seek(target []byte) bool {
a.iter.Seek(target)
return a.iter.Valid()
}
// Next advances the iterator to the next key
func (a *IteratorAdapter) Next() bool {
if !a.Valid() {
return false
}
a.iter.Next()
return a.iter.Valid()
}
// Key returns the current key
func (a *IteratorAdapter) Key() []byte {
if !a.Valid() {
return nil
}
return a.iter.Key()
}
// Value returns the current value
func (a *IteratorAdapter) Value() []byte {
if !a.Valid() {
return nil
}
// Check if this is a tombstone (deletion marker)
if a.iter.IsTombstone() {
// This ensures that during compaction, we know this is a deletion marker
return nil
}
return a.iter.Value()
}
// Valid returns true if the iterator is positioned at a valid entry
func (a *IteratorAdapter) Valid() bool {
return a.iter != nil && a.iter.Valid()
}
// IsTombstone returns true if the current entry is a deletion marker
func (a *IteratorAdapter) IsTombstone() bool {
return a.iter != nil && a.iter.IsTombstone()
}