blobman

package
v0.0.0-...-575026b Latest Latest
Warning

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

Go to latest
Published: Sep 30, 2025 License: GPL-3.0 Imports: 18 Imported by: 0

Documentation

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

	CRC    uint32
	CRCNeg 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 FileKey

type FileKey struct {
	// Shard uniquely identifies the shard
	ShardID ShardID
	// Gen uniquely identifies the generation of the file within the shard
	Gen uint32
}

FileKey uniquely identifies a {Table, Pack} file within the system.

func (FileKey) PackPath

func (fk FileKey) PackPath() string

func (FileKey) TablePath

func (fk FileKey) TablePath() string

type Key

type Key [2]uint64

Key is a 128 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 [16]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.

func (Key) ShardID

func (k Key) ShardID(numBits int) ShardID

func (Key) ShiftIn

func (k Key) ShiftIn(i int) Key

ShiftIn shifts the key into 0. The lowest bits are discarded, zeros are shifted in to the highest bits.

func (Key) Uint16

func (k Key) Uint16(i int) uint16

Uint16 returns the 16 bit integer at the given index. Indexes are interpretted modulo 8. Uint16(0) is bits [0, 15], Uint16(1) is bits [16, 31].

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 16. 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 CreateShard

func CreateShard(root *os.Root, prefix ShardID, maxTableSize, maxPackSize uint32) (*Shard, error)

CreateShard creates a new shard in the filesystem. If the shard already exists, than os.ErrExist is returned. The root should be the global root.

func OpenShard

func OpenShard(root *os.Root, prefix ShardID, maxTableSize, maxPackSize uint32) (*Shard, error)

OpenShard opens a shard that already exists in the filesystem. The root should be the global root.

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) 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)

type ShardID

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

ShardID is a prefix of at most 120 bits. ShardID takes up 128 bits. A prefix refers to a set of keys.

func NewShardID

func NewShardID(data [15]byte, numBits uint8) ShardID

func (ShardID) Child

func (sh ShardID) Child(k uint8) ShardID

Child appends the given index to the shard id and returns the new shard id.

func (ShardID) Data

func (p ShardID) Data() (ret [15]byte)

func (ShardID) Len

func (p ShardID) Len() int

func (ShardID) Path

func (p ShardID) Path() string

func (ShardID) ShiftIn

func (p ShardID) ShiftIn(i int) ShardID

type Store

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

Store stores blobs on in the filesystem.

func New

func New(root *os.Root) *Store

func (*Store) Close

func (db *Store) Close() error

func (*Store) Delete

func (db *Store) Delete(key Key) error

Delete overwrites any tables containing key with a tombstone.

func (*Store) Flush

func (db *Store) Flush() error

func (*Store) Get

func (db *Store) Get(key Key, fn func(data []byte)) (bool, error)

Get finds key if it exists and calls fn with the data. The data must not be used outside the callback.

func (*Store) Maintain

func (db *Store) Maintain() error

Maintain performs background maintenance tasks on the trie.

func (*Store) Put

func (db *Store) Put(key Key, data []byte) (bool, error)

Put finds a spot for key, and writes data to it. If the key already exists, then the write is ignored and false is returned. If some data with key does not exist in the system after this call, then an error is returned.

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