common

package module
v0.0.0-...-c6040da Latest Latest
Warning

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

Go to latest
Published: Jul 28, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

View Source
const InternalKeyTrailerLen = 8

Variables

View Source
var (
	ObjectTypeFromString = map[string]ObjectType{
		"manifest": TypeManifest,
		"sst":      TypeTable,
		"wal":      TypeWAL,
		"LOCK":     TypeLock,
	}
	ObjectTypeToString = map[ObjectType]string{
		TypeManifest: "manifest",
		TypeTable:    "sst",
		TypeWAL:      "wal",
		TypeLock:     "LOCK",
	}
)
View Source
var BlockKindStrings = map[BlockKind]string{
	BlockKindData:      "data",
	BlockKindIndex:     "index",
	BlockKindFilter:    "filter",
	BlockKindMetaIntex: "meta-index",
}
View Source
var DefaultLogger defaultLogger

DefaultLogger logs to the Go stdlib logs.

Functions

func GetFileName

func GetFileName(objType ObjectType, num DiskfileNum) string

func ParseFileName

func ParseFileName(name string) (ObjectType, DiskfileNum, bool)

Types

type BlockKind

type BlockKind byte
const (
	BlockKindUnknown BlockKind = iota
	BlockKindData
	BlockKindIndex
	BlockKindFilter
	BlockKindMetaIntex
)

type BoundaryKind

type BoundaryKind uint8
const (
	Exclusive BoundaryKind = iota
	Inclusive
)

The two possible values of BoundaryKind.

Note that we prefer Exclusive to be the zero value, so that zero UserKeyBounds are not valid.

type BufferPoolFetcher

type BufferPoolFetcher struct {
	Fetcher
	// contains filtered or unexported fields
}

func (*BufferPoolFetcher) Load

func (b *BufferPoolFetcher) Load() []byte

func (*BufferPoolFetcher) Release

func (b *BufferPoolFetcher) Release()

func (*BufferPoolFetcher) Reserve

func (b *BufferPoolFetcher) Reserve(size int)

func (*BufferPoolFetcher) Set

func (b *BufferPoolFetcher) Set(val []byte)

type ChecksumType

type ChecksumType byte
const (
	UnknownChecksum ChecksumType = iota
	CRC32Checksum
)

type DefaultComparer

type DefaultComparer struct{}

func NewComparer

func NewComparer() *DefaultComparer

func (DefaultComparer) AbbreviatedKey

func (c DefaultComparer) AbbreviatedKey(key []byte) []byte

func (DefaultComparer) Compare

func (c DefaultComparer) Compare(a, b []byte) int

func (DefaultComparer) Name

func (c DefaultComparer) Name() string

func (DefaultComparer) Separator

func (c DefaultComparer) Separator(a, b []byte) []byte

func (DefaultComparer) Split

func (c DefaultComparer) Split(b []byte) int

func (DefaultComparer) Successor

func (c DefaultComparer) Successor(b []byte) []byte

type DiskfileNum

type DiskfileNum int64

type Fetcher

type Fetcher interface {
	Load() []byte
	// Release freed associated resources. Release should always success
	// and can be called multiple times without causing error.
	Release()
}

Fetcher uses to fetch and/or release the data

type IChecksum

type IChecksum interface {
	Checksum(block []byte, auxiliary byte) uint32
}

func NewChecksumer

func NewChecksumer(ct ChecksumType) IChecksum

type IComparer

type IComparer interface {
	// Compare returns -1, 0, or +1 depending on whether a is 'less than', 'equal
	// to' or 'greater than' b.
	//
	// A key a is less than b if a's prefix is byte-wise less than b's prefix, or if
	// the prefixes are equal and a's suffix is less than b's suffix
	Compare(a, b []byte) int

	// Separator returns a sequence of bytes x such that a <= x && x < b,
	// where 'less than' is consistent with Compare.
	// Trivial implementation is just simply returns "a", however we try to return
	// a shorter "x" to reduce the SSTable size
	//
	// Use case: Use for creating an index key that separate 2 blocks
	Separator(a, b []byte) []byte

	// Successor returns a sequence of bytes x such that x >= b, where
	// 'less than' is consistent with Compare.
	// Trivial implementation is just simply return "b", however we try to return
	// a shorter "x" to reduce the SSTable size
	//
	// Use case: Use to create an index key of the last block in a SSTable
	Successor(b []byte) []byte

	// Split return the prefix of a given key
	//
	// Use case: Uses to separate the actual user key and MVCC id
	// in the internalKey.UserKey
	Split(b []byte) int

	// Name of the comparer will be persisted within the Manifest
	Name() string

	// AbbreviatedKey returns a fixed length prefix of a user key such that
	//
	//	AbbreviatedKey(a) < AbbreviatedKey(b) implies a < b, and
	//	AbbreviatedKey(a) > AbbreviatedKey(b) implies a > b.
	//
	// If AbbreviatedKey(a) == AbbreviatedKey(b), an additional comparison is
	// required to determine if the two keys are actually equal.
	//
	// This helps optimize indexed batch comparisons for cache locality. If a Split
	// function is specified, AbbreviatedKey usually returns the first eight bytes
	// of the user key prefix in the order that gives the correct ordering.
	AbbreviatedKey(key []byte) []byte
}

IComparer defines a total ordering over the space of []byte keys: a 'less than' relationship.

type InternalIterator

type InternalIterator[T any] interface {
	InternalSeeker[T]

	// First moves the iterator the first key/value pair.
	First() *T

	// Last moves the iterator the last key/value pair.
	Last() *T

	// Next moves the iterator to the next key/value pair
	Next() *T

	// Prev moves the iterator to the previous key/value pair.
	Prev() *T

	// Close closes the iterator and returns any accumulated error. Exhausting
	// all the key/value pairs in a table is not considered to be an error.
	Close() error
	IsClosed() bool
}

InternalIterator iterates over a DB's key/value pairs in key order. Implementations may vary depending on the TableFormat being written.

type InternalKV

type InternalKV struct {
	K InternalKey
	V InternalLazyValue
}

func (*InternalKV) Compare

func (kv *InternalKV) Compare(comparer IComparer, another *InternalKV) int

type InternalKey

type InternalKey struct {
	UserKey []byte
	Trailer InternalKeyTrailer
}

InternalKey or internal key. Due to the LSM structure, keys are never updated in place, but overwritten with new versions. An Internal InternalKey is composed of the user specified key, a sequence number (7 bytes) and a kind (1 byte).

+-------------+------------+----------+
| UserKey (N) | SeqNum (7) | Kind (1) |
+-------------+------------+----------+

In the MVCC model, a same key can have multiple versions. So the UserKey is composed by "[prefix - actual user key][suffix - MVCC id]" To find the prefix of a key, we should implement the comparer.Split()

func DeserializeKey

func DeserializeKey(key []byte) *InternalKey

func MakeKey

func MakeKey(userKey []byte, num SeqNum, kind KeyKind) InternalKey

func MakeMetaIndexKey

func MakeMetaIndexKey(blkKind BlockKind) InternalKey

func (*InternalKey) Compare

func (k *InternalKey) Compare(comparer IComparer, other *InternalKey) int

func (*InternalKey) KeyKind

func (k *InternalKey) KeyKind() KeyKind

func (*InternalKey) ReadMetaIndexKey

func (k *InternalKey) ReadMetaIndexKey() BlockKind

func (*InternalKey) Separator

func (k *InternalKey) Separator(comparer IComparer, other *InternalKey) *InternalKey

func (*InternalKey) SeqNum

func (k *InternalKey) SeqNum() SeqNum

func (*InternalKey) SerializeTo

func (k *InternalKey) SerializeTo(buf []byte)

SerializeTo serialise an internal key into give buffer. Caller must ensure buf has enough size to hold

func (*InternalKey) Size

func (k *InternalKey) Size() int

func (*InternalKey) Successor

func (k *InternalKey) Successor(comparer IComparer) *InternalKey

type InternalKeyTrailer

type InternalKeyTrailer uint64

InternalKeyTrailer encodes a [SeqNum (7) + InternalKeyKind (1)].

type InternalLazyValue

type InternalLazyValue struct {
	ValueSource   ValueSource
	BufferFetcher *BufferPoolFetcher
	CacheFetcher  Fetcher
}

InternalLazyValue either points to a block in the block cache (Fetcher != nil), or a buffer that exists outside the block cache allocated from a BufferPool.

The value of the InternalLazyValue might not yet dereference, until caller explicitly load the value.

func NewBlankInternalLazyValue

func NewBlankInternalLazyValue(s ValueSource) InternalLazyValue

func (*InternalLazyValue) Compare

func (iv *InternalLazyValue) Compare(comparer IComparer, lv *InternalLazyValue) int

func (*InternalLazyValue) Release

func (iv *InternalLazyValue) Release()

Release releases the allocated memory to store the value

func (*InternalLazyValue) ReserveBuffer

func (iv *InternalLazyValue) ReserveBuffer(pool *predictable_size.PredictablePool, size int)

ReserveBuffer uses for reserving a define allocated memory from a buffer pool

func (*InternalLazyValue) SetBufferValue

func (iv *InternalLazyValue) SetBufferValue(value []byte) error

SetBufferValue used for manually update the buffered value of the BufferFetcher A caller must use the Reserve function before using this function to load the data into the reserved buffer

func (*InternalLazyValue) SetCacheFetcher

func (iv *InternalLazyValue) SetCacheFetcher(fetcher Fetcher) error

func (*InternalLazyValue) Value

func (iv *InternalLazyValue) Value() []byte

Value loads value

type InternalSeeker

type InternalSeeker[T any] interface {
	// SeekGTE moves the iterator to the first key/value pair whose key ≥ to the given key.
	SeekGTE(key []byte) *T

	// SeekPrefixGTE moves the iterator to the first key/value pair whose key >= to the given key.
	// that has the defined prefix for faster looking up
	SeekPrefixGTE(prefix, key []byte) *T

	// SeekLTE moves the iterator to the last key/value pair whose key ≤ to the given key.
	SeekLTE(key []byte) *T
}

Inspired by :

https://github.com/facebook/rocksdb/wiki/Iterator
https://github.com/facebook/rocksdb/wiki/Iterator-Implementation

InternalSeeker defines an interface for seeking within an sstable. Implementations may vary depending on the TableFormat being read.

type InternalWriter

type InternalWriter interface {
	// Add adds a key-value pair to the sstable.
	Add(key InternalKey, value []byte) error
	// Close finishes writing the table and closes the underlying file that the table was written to.
	Close() error
}

InternalWriter defines an interface for sstable writers. Implementations may vary depending on the TableFormat being written.

type KeyKind

type KeyKind byte

KeyKind enumerates the kind of key: a deletion tombstone, a set value, a merged value, etc.

const (
	KeyKindUnknown KeyKind = iota
	KeyKindDelete
	KeyKindSet
	KeyKindMerge
	KeyKindSeparator
	KeyKindMetaIndex
)

type Logger

type Logger interface {
	Infof(format string, args ...any)
	Errorf(format string, args ...any)
	Fatalf(format string, args ...any)
}

Logger defines an interface for writing log messages.

type ObjectType

type ObjectType byte
const (
	TypeManifest ObjectType = iota
	TypeTable
	TypeWAL
	TypeLock
)

type SeqNum

type SeqNum uint64

SeqNum is a sequence number defining precedence among identical keys. A key with a higher sequence number takes precedence over a key with an equal user key of a lower sequence number. Basically this is an auto increment number will be assigned to every user key when it is written to the DB

See db/batch.go about the batches, and db/commit.go to understand how batches and SeqNum are committed to the log

const (
	// SeqNumMax is the largest valid sequence number.
	SeqNumMax SeqNum = 1<<56 - 1
)

type UserKeyBound

type UserKeyBound struct {
	Start []byte
	End   UserKeyBoundary
}

UserKeyBound is a user key interval with an inclusive start boundary and with an end boundary that can be either inclusive or exclusive.

func (*UserKeyBound) Union

func (u *UserKeyBound) Union(cmp IComparer, other UserKeyBound) UserKeyBound

type UserKeyBoundary

type UserKeyBoundary struct {
	Key  []byte
	Kind BoundaryKind
}

UserKeyBoundary represents the endpoint of a bound which can be exclusive or inclusive.

func (*UserKeyBoundary) Compare

func (eb *UserKeyBoundary) Compare(cmp IComparer, other UserKeyBoundary) int

type ValueSource

type ValueSource byte
const (
	ValueFromUnknown ValueSource = iota
	ValueFromBuffer
	ValueFromCache
)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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