sstable

package
v0.0.0-...-0ee2f9b Latest Latest
Warning

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

Go to latest
Published: Jul 12, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Package sstable implements immutable sorted string tables: the on-disk read layer of an LSM tree. A table holds key-sorted entries in CRC-checked data blocks, followed by a block index, a bloom filter, and a fixed-size footer. Values are opaque byte payloads; the encoding of value contents is the caller's responsibility, mirroring internal/wal.

Index

Constants

View Source
const (
	// DefaultBlockSize is the target uncompressed size of a data block.
	DefaultBlockSize = 4 * 1024
	// DefaultBitsPerKey sizes the bloom filter at ~1% false positives.
	DefaultBitsPerKey = 10
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

type Cache struct {
	// contains filtered or unexported fields
}

Cache is a byte-capped LRU over decompressed data blocks, shared by any number of Readers. A hit serves a block without the disk read, CRC check, or decompression a cold read pays. Point lookups populate the cache; iterators only consult it, so full-table scans and compactions cannot evict the hot set. Safe for concurrent use. A nil *Cache is valid and caches nothing.

func NewCache

func NewCache(capacityBytes int64) *Cache

NewCache returns a cache holding up to capacityBytes of decompressed blocks. capacityBytes <= 0 returns nil (caching disabled).

func (*Cache) Stats

func (c *Cache) Stats() CacheStats

Stats returns current counters. Safe on a nil cache (all zeros).

type CacheStats

type CacheStats struct {
	Hits    uint64
	Misses  uint64
	Bytes   int64 // charged bytes currently held, including overhead
	Entries int
}

CacheStats is a point-in-time snapshot of cache effectiveness and size.

type Iterator

type Iterator struct {
	// contains filtered or unexported fields
}

Iterator walks a table's entries in key order:

it := r.Iter()
for it.Next() {
    use(it.Key(), it.Value())
}
if err := it.Err(); err != nil { ... }

Key and Value remain valid only until the next call to Next and must not be modified: they may alias a block shared through the block cache.

func (*Iterator) Err

func (it *Iterator) Err() error

Err returns the first error encountered during iteration, if any.

func (*Iterator) Key

func (it *Iterator) Key() []byte

Key returns the current entry's key. Valid until the next call to Next.

func (*Iterator) Next

func (it *Iterator) Next() bool

Next advances to the next entry. It returns false at the end of the table or on error; check Err to distinguish.

func (*Iterator) Value

func (it *Iterator) Value() []byte

Value returns the current entry's value. Valid until the next call to Next.

type Options

type Options struct {
	// BlockSize is the target uncompressed size of a data block in bytes.
	BlockSize int
	// BitsPerKey sizes the bloom filter; 10 gives ~1% false positives.
	BitsPerKey int
	// DisableCompression stores every data block raw. By default each block
	// is s2-compressed, falling back to raw per block when compression does
	// not shrink it.
	DisableCompression bool
}

Options configure table construction. The zero value selects defaults.

type Reader

type Reader struct {
	// contains filtered or unexported fields
}

Reader serves point lookups and ordered scans from a finished table. The block index and bloom filter live in memory; data blocks are read on demand, one ReadAt per lookup. Safe for concurrent use.

func NewReader

func NewReader(ra io.ReaderAt, size int64) (*Reader, error)

NewReader opens a table from any io.ReaderAt of the given size, verifying the footer, index, and bloom filter CRCs up front. Data block CRCs are verified on each read.

func Open

func Open(path string) (*Reader, error)

Open opens the table file at path. The returned Reader owns the file handle; call Close when done.

func (*Reader) Close

func (r *Reader) Close() error

Close releases the underlying file handle when the Reader was created via Open; otherwise it is a no-op.

func (*Reader) Count

func (r *Reader) Count() uint64

Count returns the number of entries in the table.

func (*Reader) Get

func (r *Reader) Get(key []byte) ([]byte, bool, error)

Get returns the value stored for key. The returned slice does not alias reader-internal state and is safe to retain.

func (*Reader) Iter

func (r *Reader) Iter() *Iterator

Iter returns an iterator over all entries in key order, positioned before the first entry. Not safe for concurrent use, but independent iterators from the same Reader are.

func (*Reader) IterFrom

func (r *Reader) IterFrom(start []byte) *Iterator

IterFrom returns an iterator positioned before the first entry with key >= start, using the block index to skip earlier blocks (the same binary search Get uses). A nil or empty start behaves like Iter.

func (*Reader) Largest

func (r *Reader) Largest() []byte

Largest returns the table's last key, or nil for an empty table.

func (*Reader) SetCache

func (r *Reader) SetCache(c *Cache)

SetCache attaches a shared block cache. Call right after Open/NewReader, before the Reader is used concurrently; a nil cache disables caching.

func (*Reader) Smallest

func (r *Reader) Smallest() []byte

Smallest returns the table's first key, or nil for an empty table.

type Writer

type Writer struct {
	// contains filtered or unexported fields
}

Writer builds an SSTable by streaming sorted entries to w. Keys must be added in strictly increasing byte order. The caller owns durability: write to a temp file, Finish, fsync, then rename into place.

Not safe for concurrent use.

func NewWriter

func NewWriter(w io.Writer, opts Options) *Writer

NewWriter returns a Writer that streams a table to w. A zero Options selects DefaultBlockSize and DefaultBitsPerKey.

func (*Writer) Add

func (w *Writer) Add(key, value []byte) error

Add appends one entry. Keys must be non-empty and strictly greater than the previously added key.

func (*Writer) Finish

func (w *Writer) Finish() error

Finish flushes the final data block and writes the index, bloom filter, and footer. The Writer cannot be used afterwards. Finishing an empty table is valid and produces a table with zero entries.

Jump to

Keyboard shortcuts

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