godb

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

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

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

Documentation

Index

Constants

View Source
const (
	B  = 1
	KB = 1024 * B
	MB = 1024 * KB
	GB = 1024 * MB
)
View Source
const (
	// MaxKeySize is the maximum length of a key, in bytes.
	MaxKeySize = 32768

	// MaxValueSize is the maximum length of a value, in bytes.
	MaxValueSize = (1 << 31) - 2
)
View Source
const HeaderSize = 5

Variables

View Source
var (
	// ErrDatabaseNotOpen is returned when a DB instance is accessed before it
	// is opened or after it is closed.
	ErrDatabaseNotOpen = errors.New("database not open")

	// ErrDatabaseOpen is returned when opening a database that is
	// already open.
	ErrDatabaseOpen = errors.New("database already open")

	// ErrInvalid is returned when both meta pages on a database are invalid.
	// This typically occurs when a file is not a bolt database.
	ErrInvalid = errors.New("invalid database")

	// ErrVersionMismatch is returned when the data file was created with a
	// different version of Bolt.
	ErrVersionMismatch = errors.New("version mismatch")

	// ErrChecksum is returned when either meta page checksum does not match.
	ErrChecksum = errors.New("checksum error")

	// ErrTimeout is returned when a database cannot obtain an exclusive lock
	// on the data file after the timeout passed to Open().
	ErrTimeout = errors.New("timeout")
)

These errors can be returned when opening or calling methods on a DB.

View Source
var (
	// ErrTxNotWritable is returned when performing a write operation on a
	// read-only transaction.
	ErrTxNotWritable = errors.New("tx not writable")

	// ErrTxClosed is returned when committing or rolling back a transaction
	// that has already been committed or rolled back.
	ErrTxClosed = errors.New("tx closed")

	// ErrDatabaseReadOnly is returned when a mutating transaction is started on a
	// read-only database.
	ErrDatabaseReadOnly = errors.New("database is in read-only mode")
)

These errors can occur when beginning or committing a Tx.

View Source
var (
	// ErrBucketNotFound is returned when trying to access a bucket that has
	// not been created yet.
	ErrBucketNotFound = errors.New("bucket not found")

	// ErrBucketExists is returned when creating a bucket that already exists.
	ErrBucketExists = errors.New("bucket already exists")

	// ErrBucketNameRequired is returned when creating a bucket with a blank name.
	ErrBucketNameRequired = errors.New("bucket name required")

	// ErrKeyRequired is returned when inserting a zero-length key.
	ErrKeyRequired = errors.New("key required")

	// ErrKeyTooLarge is returned when inserting a key that is larger than MaxKeySize.
	ErrKeyTooLarge = errors.New("key too large")

	// ErrValueTooLarge is returned when inserting a value that is larger than MaxValueSize.
	ErrValueTooLarge = errors.New("value too large")

	// ErrIncompatibleValue is returned when trying create or delete a bucket
	// on an existing non-bucket key or when trying to create or delete a
	// non-bucket key on an existing bucket key.
	ErrIncompatibleValue = errors.New("incompatible value")

	// ErrKeyNotFound is returned when trying to access a key that does not exist.
	// This can occur when trying to delete a key or bucket that does not exist.
	ErrKeyNotFound = errors.New("key not found")
)

These errors can occur when putting or deleting a value or a bucket.

View Source
var (
	ErrInvalidRecord = errors.New("invalid record")
)

Functions

This section is empty.

Types

type Bucket

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

func (*Bucket) Delete

func (b *Bucket) Delete(key []byte) error

func (*Bucket) Get

func (b *Bucket) Get(key []byte) ([]byte, error)

func (*Bucket) Put

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

type DB

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

func Open

func Open(dbpath string, options ...Option) (*DB, error)

Open opens a database at the given path.

func (*DB) Begin

func (db *DB) Begin(writable bool) (*Tx, error)

func (*DB) Close

func (db *DB) Close() error

func (*DB) Compact

func (db *DB) Compact() error

Compact Perform database compression operations, delete deleted records, and optimize storage space

func (*DB) Delete

func (db *DB) Delete(key []byte) error

func (*DB) Get

func (db *DB) Get(key []byte) ([]byte, error)

func (*DB) Put

func (db *DB) Put(key, value []byte) error

func (*DB) ReadAt

func (db *DB) ReadAt(offset int64) (Node, error)

func (*DB) Size

func (db *DB) Size() int64

func (*DB) TriggerCompaction

func (db *DB) TriggerCompaction()

TriggerCompaction 手动触发压缩

func (*DB) Update

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

Update executes a function within a managed read/write transaction. The transaction has been committed when no error is returned. In the event that an error is returned, the transaction will be rolled back. When a non-nil error is returned from the function, the transaction will be rolled back and the that error will be return to the caller of Update().

func (*DB) View

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

View executes a function within a managed read-only transaction. When a non-nil error is returned from the function that error will be return to the caller of View().

type Header [HeaderSize]byte
  • Header format:

+----------+---------------+ |state(1B) | EntrySize(4B) | +----------+---------------+

func (*Header) EncodeState

func (h *Header) EncodeState(t State, s State)

func (Header) EntrySize

func (h Header) EntrySize() uint32

func (Header) IsBucket

func (h Header) IsBucket() bool

内联常用操作

func (Header) IsDeleted

func (h Header) IsDeleted() bool

func (Header) IsExpired

func (h Header) IsExpired() bool

func (Header) IsKV

func (h Header) IsKV() bool

func (Header) IsNormal

func (h Header) IsNormal() bool

func (*Header) SetEntrySize

func (h *Header) SetEntrySize(size uint32)

func (*Header) SetRecord

func (h *Header) SetRecord(s State)

func (*Header) SetType

func (h *Header) SetType(t State)

func (Header) State

func (h Header) State() State

func (Header) StateRecord

func (h Header) StateRecord() State

func (Header) StateType

func (h Header) StateType() State

type Node

type Node interface {
	// io.WriterTo
	Header() Header
	BucketID() uint32
	Size() uint32
	Value() []byte
	MarshalToBuffer(buff *bytebufferpool.ByteBuffer) error
}

type Option

type Option func(*option)

func WithCompactionDisabled

func WithCompactionDisabled() Option

WithCompactionDisabled 禁用自动压缩

func WithCompactionInterval

func WithCompactionInterval(interval time.Duration) Option

WithCompactionInterval 设置自动压缩间隔

func WithFsync

func WithFsync(fsync bool) Option

func WithIncrementSize

func WithIncrementSize(s int64) Option

func WithStorage

func WithStorage(s IOStorage) Option

type State

type State uint8

State 定义了记录的状态信息,由一个字节表示 高4位表示记录类型 (StateType),低4位表示记录状态 (StateRecord)

const (
	TypeNormal   State = 0x00 // 0000 0000 - 普通记录
	TypeBucket   State = 0x10 // 0001 0000 - 桶记录
	TypeKV       State = 0x20 // 0010 0000 - 键值记录
	TypeReserved State = 0x30 // 0011 0000 - 保留类型
	TypeMask     State = 0xF0 // 1111 0000 - 类型掩码
)

StateType 定义,使用高4位

const (
	StateNormal   State = 0x00 // 0000 0000 - 正常状态
	StateDeleted  State = 0x01 // 0000 0001 - 已删除
	StateInvalid  State = 0x02 // 0000 0010 - 无效状态
	StateReserved State = 0x03 // 0000 0011 - 保留状态
	StateMask     State = 0x0F // 0000 1111 - 状态掩码
)

StateRecord 定义,使用低4位

func CombineState

func CombineState(typ State, state State) State

type Tx

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

func (*Tx) Bucket

func (tx *Tx) Bucket(name []byte) *Bucket

Bucket returns a bucket by name. if the bucket does not exist it will be created and returned. The bucket is not persisted until the transaction

func (*Tx) Commit

func (tx *Tx) Commit() error

func (*Tx) CreateBucket

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

func (*Tx) DeleteBucket

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

func (*Tx) OpenBucket

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

func (*Tx) Rollback

func (tx *Tx) Rollback() error

Directories

Path Synopsis
internal
bio

Jump to

Keyboard shortcuts

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