shard

package
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Dec 15, 2025 License: GPL-3.0 Imports: 14 Imported by: 0

Documentation

Overview

Package shard implements a single Shard in the database A Shard is a single directory on disk, called the Shard's "root". All of the data for a Shard is stored as files, which are the immediate children of the directory. Shards may have subdirectories, which are entirely ignored by the Shard. In practice, the organizing layer above will place child Shard directories beneath the the parent in the filesystem.

Index

Constants

View Source
const (
	TableFileExt = ".slot"
	PackFileExt  = ".pack"
)
View Source
const (
	// TableEntrySize is the size of a row in the table.
	// It actually uses less space than this, but we want to align to 4096 bytes.
	TableEntrySize = 32
	PageSize       = 4096
	EntriesPerPage = PageSize / TableEntrySize
	// DefaultMaxTableLen is the maximum length of a table in rows.
	DefaultMaxTableLen = (1 << 20) / TableEntrySize
)
View Source
const DefaultMaxPackSize = 1 << 26
View Source
const ManifestFilename = "MF"

ManifestFilename is the filename for the manifest. This does not include the shard id, which will be in the directory path.

View Source
const ManifestSize = 32

ManifestSize is the size of the manifest in bytes.

Variables

This section is empty.

Functions

func Copy

func Copy(srcLen uint32, srcTab Table, srcPack Pack, dstTab Table, dstPack Pack) (int, error)

Copy copies entries from src{Tab,Pack} to dst{Tab,Pack}. If there is not enough space available in either the table or the pack, then an error is returned. Copy will not copy tombstones, and will sort all the entries by key before copying.

func CreatePackFile

func CreatePackFile(root *os.Root, gen uint32, maxSize uint32) (*os.File, error)

CreatePackFile creates a file configured for a pack in the filesystem, and returns it.

func CreateTableFile

func CreateTableFile(shardRoot *os.Root, gen uint32, maxSize uint32) (*os.File, error)

CreateTableFile creates a file configured for a table in the filesystem, and returns it. maxSize is the maximum size of the table in bytes, NOT the number of rows.

func IsErrShardFull

func IsErrShardFull(err error) bool

func KeyCompare

func KeyCompare(a, b Key) int

func LoadPackFile

func LoadPackFile(root *os.Root, gen uint32) (*os.File, error)

func LoadTableFile

func LoadTableFile(shardRoot *os.Root, gen uint32) (*os.File, error)

func PackFilename

func PackFilename(gen uint32) string

func SaveManifest

func SaveManifest(shardRoot *os.Root, manifest Manifest) error

func TableFilename

func TableFilename(gen uint32) string

TableFilename returns the filename for a table. This is just the filename, the directory will contain the shard prefix.

Types

type Entry

type Entry struct {
	Key Key

	Offset uint32
	Len    uint32
}

Entry is a single entry in the table.

func (*Entry) IsTombstone

func (ent *Entry) IsTombstone() bool

type ErrShardFull

type ErrShardFull struct{}

func (ErrShardFull) Error

func (e ErrShardFull) Error() string

type Key

type Key [3]uint64

Key is a 192 bit key. The 0th bit is considered the first bit, and that is at k[0] & (1 << 0).

func KeyFromBytes

func KeyFromBytes(b []byte) Key

func (Key) Bytes

func (k Key) Bytes() []byte

func (Key) Data

func (k Key) Data() (ret [24]byte)

func (Key) IsZero

func (k Key) IsZero() bool

func (Key) RotateAway

func (k Key) RotateAway(i int) Key

RotateAway specifies the amount of bits to rotate the key away from the 0th bit. The rotation is performed away from zero (towards more significant bit positions) modulo the key length.

func (Key) Uint64

func (k Key) Uint64(i int) uint64

Uint64 returns the 64 bit integer at the given index. The index is 0 or 1.

func (Key) Uint8

func (k Key) Uint8(i int) uint8

Uint8 returns the 8 bit integer at the given index. Indexes are interpretted modulo 24. Uint8(0) is bits [0, 7], Uint8(1) is bits [8, 15].

func (Key) Uint8Len

func (k Key) Uint8Len() int

Uint8Len returns the number of 8 bit integers in the key.

type Manifest

type Manifest struct {
	// Nonce is the number of times the manifest has been saved.
	Nonce uint64
	// Gen is the Shard's current generation.
	Gen uint32
	// Count is the number of entries in the table with the current generation.
	TableLen uint32
}

Manifest is the source of truth for a Shard's state.

func LoadManifest

func LoadManifest(shardRoot *os.Root) (Manifest, error)

type Pack

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

Pack is an append-only file on disk.

func NewPack

func NewPack(f *os.File, nextOffset uint32) (Pack, error)

NewPack calls f.Stat() to determine the max size Then it mmaps the file.

func (*Pack) Append

func (pk *Pack) Append(data []byte) uint32

Append appends data to the pack and returns the offset of the data.

func (*Pack) CanAppend

func (pk *Pack) CanAppend(dataLen uint32) bool

func (Pack) Close

func (pk Pack) Close() error

Close unmaps the pack and closes the file. It DOES NOT flush the mmap to disk.

func (*Pack) Flush

func (pk *Pack) Flush() error

func (Pack) FreeSpace

func (pk Pack) FreeSpace() uint32

func (*Pack) Get

func (pk *Pack) Get(offset, length uint32, fn func(data []byte)) error

Get reads data from the pack by offset and size.

type Shard

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

Shard is a directory on disk containing table and pack files, potentially across multiple generations. Each shard is independent of every other shard, and there are no consistency guarantees between shards. The Shard has no information about where it is in the trie. Shards only deal with files in their immediate directory, subdirectories (which correspond to children in the trie) are ignored by the Shard.

func New

func New(rootDir *os.Root) *Shard

func (*Shard) Close

func (s *Shard) Close() error

func (*Shard) Flush

func (sh *Shard) Flush() error

Flush causes the Shard's current state to be written to disk. First the pack and table are flushed concurrently. Then once both of them have flushed successfully, the manifest is saved.

func (*Shard) HasSpace

func (s *Shard) HasSpace(dataLen int) bool

func (*Shard) Hydrate

func (sh *Shard) Hydrate(maxTableSize, maxPackSize uint32) error

func (*Shard) LocalAppend

func (s *Shard) LocalAppend(key Key, data []byte) (bool, error)

LocalAppend appends the key and data to the table. It returns an error if the table is full or the pack is full. It returns (false, nil) if the data already exists. It returns (true, nil) if the data was appended successfully.

func (*Shard) LocalDelete

func (s *Shard) LocalDelete(key Key) (bool, error)

LocalDelete deletes the key from the table. It returns (true, nil) if the key was deleted successfully.

func (*Shard) LocalExists

func (sh *Shard) LocalExists(key Key) bool

LocalExists checks if the key exists in this shards local data. Local means that the children and grandchildren are not checked.

func (*Shard) LocalGet

func (sh *Shard) LocalGet(key Key, fn func(data []byte)) (bool, error)

func (*Shard) TableLen

func (sh *Shard) TableLen() uint32

type Table

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

Table is an unordered append-only list of entries. Each entry points into a pack, and all entries are the same size.

func NewTable

func NewTable(f *os.File) (Table, error)

NewTable mmaps a file and returns a Table. count should be the number of rows in the table.

func (Table) Capacity

func (idx Table) Capacity() uint32

Capacity is the total number of rows that can be stored in the table.

func (*Table) Close

func (idx *Table) Close() error

Close unmaps the table and closes the file. It DOES NOT flush the mmap to disk.

func (*Table) Flush

func (idx *Table) Flush() error

func (*Table) SetSlot

func (idx *Table) SetSlot(slot uint32, ent Entry)

func (*Table) Slot

func (idx *Table) Slot(i uint32) (ret Entry)

func (*Table) SlotOffset

func (idx *Table) SlotOffset(i uint32) uint32

SlotOffset returns the offset of the i-th slot in the table. Slot 0 starts at 4 bytes, to make room for the row count at the beginning.

func (*Table) Tombstone

func (idx *Table) Tombstone(slot uint32)

Tombstone writes a tombstone at the given slot.

Jump to

Keyboard shortcuts

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