storage

package
v0.0.2 Latest Latest
Warning

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

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

Documentation

Index

Constants

View Source
const (
	ValueSimple = 0
	ValueBlob   = 1

	NodePageTypeSize = UInt8Size
	NodeTypeSize     = UInt8Size
	NodeNumItemsSize = UInt16Size
	NodeHeaderSize   = NodePageTypeSize + NodeTypeSize + NodeNumItemsSize

	NodePageTypeOffset = 0
	NodeTypeOffset     = NodePageTypeOffset + NodePageTypeSize
	NodeNumItemsOffset = NodeTypeOffset + NodeTypeSize
)
View Source
const (
	MaxKeySize   = 512
	MaxValueSize = 1024

	BucketRootSize       = UInt64Size
	BucketCounterSize    = UInt64Size
	BucketItemNSize      = UInt64Size
	BucketBlobsNSize     = UInt64Size
	BucketBytesInUseSize = UInt64Size
	BucketTotalSize      = BucketRootSize + BucketCounterSize + BucketItemNSize + BucketBlobsNSize + BucketBytesInUseSize

	BucketRootOffset       = 0
	BucketCounterOffset    = BucketRootOffset + BucketRootSize
	BucketItemNOffset      = BucketCounterOffset + BucketCounterSize
	BucketBlobNOffset      = BucketItemNOffset + BucketItemNSize
	BucketBytesInUseOffset = BucketBlobNOffset + BucketBlobsNSize
)
View Source
const (
	UInt64Size = 8
	UInt32Size = 4
	UInt16Size = 2
	UInt8Size  = 1

	BTreePageSize = 4096
)
View Source
const (
	MetaPage     = 0
	FreeListPage = 1
	NodePage     = 2
	BlobPage     = 3
)
View Source
const (
	OneGigabyte = 1024 * 1024 * 1024
)

Variables

View Source
var (
	ErrKeyTooLarge          = errors.New("key too large")
	ErrValueTooLarge        = errors.New("value too large")
	ErrNotEnoughSpace       = errors.New("not enough space to serialize node")
	ErrNoPagesLeft          = errors.New("no pages left")
	ErrBucketNotFound       = errors.New("bucket not found")
	ErrBucketExists         = errors.New("bucket already exists")
	ErrTxClosed             = errors.New("transaction closed")
	ErrWriteInRxTransaction = errors.New("write in read transaction")
	ErrNodeNotFound         = errors.New("node not found")
	ErrBlobTooLarge         = errors.New("blob too large")
	ErrUnknownItemType      = errors.New("unknown item type")
	ErrBadDbVersion         = errors.New("invalid db version")
	ErrBadDbName            = errors.New("invalid db name")
)

Functions

func DeleteBlob

func DeleteBlob(tx *Tx, startPageNum uint64) (int, error)

func SetLogger

func SetLogger(l *slog.Logger)

func TempFileName

func TempFileName(suffix string) string

func WriteFreelist

func WriteFreelist(dal *Dal, freelist *Freelist) error

func WriteMeta

func WriteMeta(dal *Dal, m *Meta) error

Types

type BNode

type BNode struct {

	// PageNum is the pageNum number of the node in the storage.
	PageNum uint64
	// contains filtered or unexported fields
}

BNode represents a node in a B-Tree. It contains Key-value pairs and child nodes.

func NewBNode

func NewBNode() *BNode

func (*BNode) Deserialize

func (node *BNode) Deserialize(data []byte)

func (*BNode) Find

func (node *BNode) Find(tx *Tx, key []byte, exact bool) (int, *BNode, []int, bool)

func (*BNode) Serialize

func (node *BNode) Serialize(data []byte) error

type Blob

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

Blob represent data as linked page list

func GetBlob

func GetBlob(tx *Tx, startPageNum uint64) (*Blob, error)

func NewBlob

func NewBlob(data []byte) (*Blob, error)

func (*Blob) Save

func (blob *Blob) Save(tx *Tx) (uint64, error)

type Bucket

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

func (*Bucket) Cursor

func (bucket *Bucket) Cursor() *Cursor

func (*Bucket) ForEach

func (bucket *Bucket) ForEach(fn func(k, v []byte) error) error

func (*Bucket) Get

func (bucket *Bucket) Get(key []byte) ([]byte, bool)

func (*Bucket) NextSequence

func (bucket *Bucket) NextSequence() (uint64, error)

func (*Bucket) Put

func (bucket *Bucket) Put(key, value []byte) error

func (*Bucket) Remove

func (bucket *Bucket) Remove(key []byte) error

func (*Bucket) Sequence

func (bucket *Bucket) Sequence() uint64

type BucketStat

type BucketStat struct {
	ItemsN     uint64
	BlobsN     uint64
	BytesInUse uint64
}

type Cursor

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

func (*Cursor) First

func (cursor *Cursor) First() (key []byte, value []byte)

func (*Cursor) Last

func (cursor *Cursor) Last() (key []byte, value []byte)

func (*Cursor) Next

func (cursor *Cursor) Next() ([]byte, []byte)

func (*Cursor) Prev

func (cursor *Cursor) Prev() ([]byte, []byte)

func (*Cursor) Seek

func (cursor *Cursor) Seek(key []byte) ([]byte, []byte)

type DB

type DB struct {
	TxN atomic.Int32
	// contains filtered or unexported fields
}

func Open

func Open(path string, opts *Options) (*DB, error)

func (*DB) Begin

func (db *DB) Begin(write bool) *Tx

func (*DB) Close

func (db *DB) Close() error

func (*DB) GetOptions added in v0.0.2

func (db *DB) GetOptions() *Options

func (*DB) Stat

func (db *DB) Stat() *DBStat

func (*DB) Update

func (db *DB) Update(fn func(tx *Tx) error) error

func (*DB) View

func (db *DB) View(fn func(tx *Tx) error) error

type DBStat

type DBStat struct {
	TotalPageNum  int                    // total number pages
	FreePageN     int                    // total number of free pages
	UsedPageN     int                    // total number of used pages
	ReleasedPageN int                    // total number of released pages
	FreeListPageN int                    // total number of pages allocated for freelist
	TotalDBSize   uint64                 // amount of pages * page size
	AvailDBSize   uint64                 // amount of free pages * page size
	UsedDBSize    uint64                 // amount of used pages * page size
	Buckets       map[string]*BucketStat //
	TxN           int                    // total number of started read transactions
}

type Dal

type Dal struct {
	MinFillPercent float32
	MaxFillPercent float32
	// contains filtered or unexported fields
}

func NewDal

func NewDal(path string, opts *Options) (*Dal, error)

func (*Dal) AllocatePage

func (dal *Dal) AllocatePage() (*Page, error)

func (*Dal) Close

func (dal *Dal) Close() error

func (*Dal) GetPage

func (dal *Dal) GetPage(pageNumber uint64) (*Page, error)

func (*Dal) ReleasePage

func (dal *Dal) ReleasePage(pageNumber uint64) error

func (*Dal) SetPage

func (dal *Dal) SetPage(page *Page) error

func (*Dal) Sync

func (dal *Dal) Sync() error

type Freelist

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

func NewFreelist

func NewFreelist(pageSize uint64, maxPages uint64) *Freelist

func ReadFreelist

func ReadFreelist(dal *Dal) (*Freelist, error)

func (*Freelist) GetNextPageNumber

func (f *Freelist) GetNextPageNumber() (uint64, error)

func (*Freelist) ReleasePage

func (f *Freelist) ReleasePage(pageNum uint64)

type Item

type Item struct {
	Key   []byte
	Value []byte
}

Item represents a Key-value pair stored in a B-Tree node.

type Meta

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

func NewMeta

func NewMeta(pageSize uint64) *Meta

func ReadMeta

func ReadMeta(dal *Dal) (*Meta, error)

func (*Meta) Deserialize

func (m *Meta) Deserialize(data []byte)

func (*Meta) GetDbName

func (m *Meta) GetDbName() string

func (*Meta) GetDbVersion

func (m *Meta) GetDbVersion() (major byte, minor byte)

func (*Meta) GetDbVersionString

func (m *Meta) GetDbVersionString() string

func (*Meta) Serialize

func (m *Meta) Serialize(data []byte)

type Options added in v0.0.2

type Options struct {
	FileMode       os.FileMode
	PageSize       uint64
	EnableRecovery bool
	TxLogPath      string
}

func DefaultOptions added in v0.0.2

func DefaultOptions() *Options

func (*Options) WithFileMode added in v0.0.2

func (o *Options) WithFileMode(mode os.FileMode) *Options

func (*Options) WithPageSize added in v0.0.2

func (o *Options) WithPageSize(pageSize uint64) *Options

func (*Options) WithRecovery added in v0.0.2

func (o *Options) WithRecovery(enable bool) *Options

func (*Options) WithTxLogPath added in v0.0.2

func (o *Options) WithTxLogPath(path string) *Options

type Page

type Page struct {
	PageNumber uint64
	Data       []byte
}

func (*Page) Clear

func (p *Page) Clear()

func (*Page) GetPageType added in v0.0.2

func (p *Page) GetPageType() string

type PageRecoveryCallback added in v0.0.2

type PageRecoveryCallback func(offset uint64, page *Page) error

type Tx

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

func (*Tx) Buckets

func (tx *Tx) Buckets() [][]byte

func (*Tx) Commit

func (tx *Tx) Commit() error

func (*Tx) CreateBucket

func (tx *Tx) CreateBucket(name []byte) (*Bucket, error)

func (*Tx) CreateBucketIfNotExists

func (tx *Tx) CreateBucketIfNotExists(name []byte) (*Bucket, error)

func (*Tx) DeleteBucket

func (tx *Tx) DeleteBucket(name []byte) error

func (*Tx) GetBucket

func (tx *Tx) GetBucket(name []byte) (*Bucket, error)

func (*Tx) Rollback

func (tx *Tx) Rollback()

type TxLog added in v0.0.2

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

func NewTxLog added in v0.0.2

func NewTxLog(filename string, mode os.FileMode) *TxLog

func (*TxLog) Recover added in v0.0.2

func (txlog *TxLog) Recover(callback PageRecoveryCallback) error

func (*TxLog) With added in v0.0.2

func (txlog *TxLog) With(fn func() error) error

Jump to

Keyboard shortcuts

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