mbbolt

package module
v0.0.0-...-35f693f Latest Latest
Warning

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

Go to latest
Published: Aug 11, 2023 License: BSD-3-Clause Imports: 22 Imported by: 0

Documentation

Index

Constants

View Source
const DefaultMMapFlags = syscall.MAP_POPULATE
View Source
const ErrDeleteKey = oerrs.String("delete")

Variables

View Source
var (
	DefaultMarshalFn   = json.Marshal
	DefaultUnmarshalFn = json.Unmarshal
	ErrBucketNotFound  = bbolt.ErrBucketNotFound
)
View Source
var DefaultOptions = &Options{
	Timeout:        time.Second,
	NoFreelistSync: true,
	NoGrowSync:     false,
	FreelistType:   bbolt.FreelistMapType,

	MaxBatchSize:  512,
	MaxBatchDelay: time.Millisecond * 10,

	MmapFlags: DefaultMMapFlags,

	InitialMmapSize: 1 << 29,
}

Functions

func CloseAll

func CloseAll() error

func ConvertDB

func ConvertDB(dst, src DBer, fn ConvertFn) error

func DefaultSegmentByKey

func DefaultSegmentByKey(key string) uint64

func ForEachTx

func ForEachTx[T any](tx *Tx, bucket string, fn func(key []byte, val T) error, filterFn func(k, v []byte) bool, unmarshalFn UnmarshalFn) error

func FramesToString

func FramesToString(frs *runtime.Frames) string

func GetAny

func GetAny[T any](db *DB, bucket, key string, unmarshalFn UnmarshalFn) (out T, err error)

func GetTxAny

func GetTxAny[T any](tx *Tx, bucket, key string, unmarshalFn UnmarshalFn) (out T, err error)

func SegDBUpdateOne

func SegDBUpdateOne[T any](s *SegDB, bucket, key string, fn func(v T) (T, error)) error

Types

type BBoltDB

type BBoltDB = bbolt.DB

bbolt type aliases

type BBoltTx

type BBoltTx = bbolt.Tx

bbolt type aliases

type Bucket

type Bucket = bbolt.Bucket

bbolt type aliases

type Cache

type Cache[T any] struct {
	NoBatch bool
	// contains filtered or unexported fields
}

func CacheOf

func CacheOf[T any](db *DB, bucket string, loadAll bool) *Cache[T]

func (*Cache[T]) Delete

func (c *Cache[T]) Delete(key string) (err error)

func (*Cache[T]) ForEach

func (c *Cache[T]) ForEach(fn func(k string, v T) error) (err error)

func (*Cache[T]) Get

func (c *Cache[T]) Get(key string) (v T, err error)

Use clone if T is a pointer or contains slices/maps/pointers that will be modified.

func (*Cache[T]) Put

func (c *Cache[T]) Put(key string, v T) (err error)

func (*Cache[T]) Stats

func (c *Cache[T]) Stats() (hits, misses int64)

func (*Cache[T]) Sync

func (c *Cache[T]) Sync()

func (*Cache[T]) Update

func (c *Cache[T]) Update(fn func(tx *Tx) (key string, v T, err error)) (err error)

type ConvertFn

type ConvertFn = func(bucket string, k, v []byte) ([]byte, bool)

type Cursor

type Cursor = bbolt.Cursor

bbolt type aliases

type DB

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

func MustOpen

func MustOpen(ctx context.Context, path string, opts *Options) *DB

func Open

func Open(ctx context.Context, path string, opts *Options) (*DB, error)

func (*DB) Backup

func (db *DB) Backup(w io.Writer) (n int64, err error)

func (*DB) BackupToFile

func (db *DB) BackupToFile(fp string) (n int64, err error)

func (*DB) Batch

func (db *DB) Batch(fn func(*Tx) error) error

func (*DB) Begin

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

func (*DB) Buckets

func (db *DB) Buckets() (out []string)

func (*DB) Close

func (db *DB) Close() error

func (*DB) CreateBucket

func (db *DB) CreateBucket(bucket string) error

func (*DB) CreateBucketWithIndex

func (db *DB) CreateBucketWithIndex(bucket string, idx uint64) error

func (*DB) CreateBucketWithIndexBig

func (db *DB) CreateBucketWithIndexBig(bucket string, idx *big.Int) error

func (*DB) CurrentIndex

func (db *DB) CurrentIndex(bucket string) (idx uint64)

func (*DB) Delete

func (db *DB) Delete(bucket, key string) error

func (*DB) ForEachBytes

func (db *DB) ForEachBytes(bucket string, fn func(k, v []byte) error) (err error)

func (*DB) Get

func (db *DB) Get(bucket, key string, out any) (err error)

func (*DB) GetAny

func (db *DB) GetAny(bucket, key string, out any, unmarshalFn UnmarshalFn) error

func (*DB) GetBytes

func (db *DB) GetBytes(bucket, key string) (out []byte, err error)

func (*DB) NextIndex

func (db *DB) NextIndex(bucket string) (idx uint64, err error)

func (*DB) OnSlowUpdate

func (db *DB) OnSlowUpdate(minDuration time.Duration, fn OnSlowUpdateFn)

func (*DB) Path

func (db *DB) Path() string

func (*DB) Put

func (db *DB) Put(bucket, key string, val any) error

func (*DB) PutAny

func (db *DB) PutAny(bucket, key string, val any, marshalFn MarshalFn) error

func (*DB) PutBytes

func (db *DB) PutBytes(bucket, key string, val []byte) error

func (*DB) Raw

func (db *DB) Raw() *BBoltDB

func (*DB) SetMarshaler

func (db *DB) SetMarshaler(marshalFn MarshalFn, unmarshalFn UnmarshalFn)

func (*DB) SetNextIndex

func (db *DB) SetNextIndex(bucket string, seq uint64) error

func (*DB) Update

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

func (*DB) UseBatch

func (db *DB) UseBatch(v bool) (old bool)

func (*DB) View

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

type DBer

type DBer interface {
	CurrentIndex(bucket string) uint64
	NextIndex(bucket string) (uint64, error)
	SetNextIndex(bucket string, index uint64) error
	Buckets() []string
	Get(bucket, key string, v any) error
	ForEachBytes(bucket string, fn func(k, v []byte) error) error
	Put(bucket, key string, v any) error
	Delete(bucket, key string) error
}

type MarshalFn

type MarshalFn = func(any) ([]byte, error)

type MultiDB

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

func NewMultiDB

func NewMultiDB(prefix, ext string, opts *Options) *MultiDB

func (*MultiDB) Backup

func (mdb *MultiDB) Backup(w io.Writer, filter func(name string, db *DB) bool) (n int64, err error)

func (*MultiDB) BackupToDir

func (mdb *MultiDB) BackupToDir(dir string, filter func(name string, db *DB) bool) (n int64, err error)

func (*MultiDB) BackupToFile

func (mdb *MultiDB) BackupToFile(fp string, filter func(name string, db *DB) bool) (n int64, err error)

func (*MultiDB) Close

func (mdb *MultiDB) Close() error

func (*MultiDB) CloseDB

func (mdb *MultiDB) CloseDB(name string) (err error)

func (*MultiDB) ForEachDB

func (mdb *MultiDB) ForEachDB(fn func(name string, db *DB) error) error

func (*MultiDB) Get

func (mdb *MultiDB) Get(ctx context.Context, name string, opts *Options) (db *DB, err error)

func (*MultiDB) MustGet

func (mdb *MultiDB) MustGet(ctx context.Context, name string, opts *Options) *DB

func (*MultiDB) Restore

func (mdb *MultiDB) Restore(ctx context.Context, r io.ReaderAt) (err error)

func (*MultiDB) RestoreFromFile

func (mdb *MultiDB) RestoreFromFile(ctx context.Context, fp string) (err error)

type OnSlowUpdateFn

type OnSlowUpdateFn func(callers *runtime.Frames, took time.Duration)

bbolt type aliases

type Options

type Options struct {
	// OpenFile is used to open files. It defaults to os.OpenFile. This option
	// is useful for writing hermetic tests.
	OpenFile func(string, int, os.FileMode) (*os.File, error)

	// InitDB gets called on initial db open
	InitDB func(db *DB) error

	// FreelistType sets the backend freelist type. There are two options. Array which is simple but endures
	// dramatic performance degradation if database is large and framentation in freelist is common.
	// The alternative one is using hashmap, it is faster in almost all circumstances
	// but it doesn't guarantee that it offers the smallest page id available. In normal case it is safe.
	// The default type is array
	FreelistType bbolt.FreelistType

	// InitialBuckets will create the given slice of buckets on initial db open
	InitialBuckets []string

	// Sets the DB.MmapFlags flag before memory mapping the file.
	MmapFlags int

	// InitialMmapSize is the initial mmap size of the database
	// in bytes. Read transactions won't block write transaction
	// if the InitialMmapSize is large enough to hold database mmap
	// size. (See DB.Begin for more information)
	//
	// If <=0, the initial map size is 0.
	// If initialMmapSize is smaller than the previous database size,
	// it takes no effect.
	InitialMmapSize int

	// PageSize overrides the default OS page size.
	PageSize int

	// Timeout is the amount of time to wait to obtain a file lock.
	// When set to zero it will wait indefinitely. This option is only
	// available on Darwin and Linux.
	Timeout time.Duration

	// Sets the DB.NoGrowSync flag before memory mapping the file.
	NoGrowSync bool

	// Do not sync freelist to disk. This improves the database write performance
	// under normal operation, but requires a full database re-sync during recovery.
	NoFreelistSync bool

	// Open database in read-only mode. Uses flock(..., LOCK_SH |LOCK_NB) to
	// grab a shared lock (UNIX).
	ReadOnly bool

	// NoSync sets the initial value of DB.NoSync. Normally this can just be
	// set directly on the DB itself when returned from Open(), but this option
	// is useful in APIs which expose Options but not the underlying DB.
	NoSync bool

	// Mlock locks database file in memory when set to true.
	// It prevents potential page faults, however
	// used memory can't be reclaimed. (UNIX only)
	Mlock bool

	// MaxBatchSize is the maximum size of a batch. Default value is
	// copied from DefaultMaxBatchSize in Open.
	//
	// If <=0, disables batching.
	MaxBatchSize int

	// MaxBatchDelay is the maximum delay before a batch starts.
	// Default value is copied from DefaultMaxBatchDelay in Open.
	//
	// If <=0, effectively disables batching.
	MaxBatchDelay time.Duration

	MarshalFn   MarshalFn
	UnmarshalFn UnmarshalFn

	// AutoRetry will keep retrying to open the db instead of returning a timeout error
	AutoRetry bool
}

func (*Options) BoltOpts

func (opts *Options) BoltOpts() *bbolt.Options

func (*Options) Clone

func (opts *Options) Clone() *Options

type SegDB

type SegDB struct {
	SegmentFn func(key string) uint64
	// contains filtered or unexported fields
}

func NewSegDB

func NewSegDB(ctx context.Context, prefix, ext string, opts *Options, numSegments int) *SegDB

NewSegDB creates a new segmented database. SegDB uses msgpack by default. WARNING WARNING, if numSegments changes between calls, the keys will be out of sync

func NewSegDBFromFile

func NewSegDBFromFile(ctx context.Context, fp, prefix, ext string, opts *Options) (_ *SegDB, err error)

func (*SegDB) Backup

func (s *SegDB) Backup(w io.Writer) (int64, error)

func (*SegDB) Buckets

func (s *SegDB) Buckets() []string

func (*SegDB) Close

func (s *SegDB) Close() error

func (*SegDB) CurrentIndex

func (s *SegDB) CurrentIndex(bucket string) (idx uint64)

func (*SegDB) CurrentIndexByKey

func (s *SegDB) CurrentIndexByKey(bucket, key string) (idx uint64)

func (*SegDB) Delete

func (s *SegDB) Delete(bucket, key string) error

func (*SegDB) ForEachBytes

func (s *SegDB) ForEachBytes(bucket string, fn func(k, v []byte) error) error

func (*SegDB) ForEachDB

func (s *SegDB) ForEachDB(fn func(db *DB) error) error

func (*SegDB) Get

func (s *SegDB) Get(bucket, key string, v any) error

func (*SegDB) NextIndex

func (s *SegDB) NextIndex(bucket string) (seq uint64, err error)

func (*SegDB) NextIndexByKey

func (s *SegDB) NextIndexByKey(bucket, key string) (seq uint64, err error)

func (*SegDB) Put

func (s *SegDB) Put(bucket, key string, v any) error

func (*SegDB) Restore

func (s *SegDB) Restore(ctx context.Context, r io.ReaderAt) error

func (*SegDB) RestoreFromFile

func (s *SegDB) RestoreFromFile(ctx context.Context, fp string) error

func (*SegDB) SetMarshaler

func (s *SegDB) SetMarshaler(marshalFn MarshalFn, unmarshalFn UnmarshalFn)

func (*SegDB) SetNextIndex

func (s *SegDB) SetNextIndex(bucket string, seq uint64) error

func (*SegDB) SetNextIndexByKey

func (s *SegDB) SetNextIndexByKey(bucket, key string, seq uint64) error

func (*SegDB) UseBatch

func (s *SegDB) UseBatch(v bool) (old bool)

type Tx

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

func (*Tx) Bucket

func (tx *Tx) Bucket(bucket string) *Bucket

func (*Tx) CreateBucketIfNotExists

func (tx *Tx) CreateBucketIfNotExists(bucket string) (*Bucket, error)

func (*Tx) Delete

func (tx *Tx) Delete(bucket, key string) error

func (*Tx) DeleteBucket

func (tx *Tx) DeleteBucket(bucket string) error

func (*Tx) ForEachBytes

func (tx *Tx) ForEachBytes(bucket string, fn func(k, v []byte) error) error

func (*Tx) ForEachUpdate

func (tx *Tx) ForEachUpdate(bucket string, fn func(k, v []byte, setValue func(k, nv []byte)) (err error)) (err error)

ForEachUpdate passes a func to the loop func to allow you to set values inside the loop, this is a workaround seting values inside a foreach loop which isn't allowed.

func (*Tx) GetAny

func (tx *Tx) GetAny(bucket, key string, out any, unmarshalFn UnmarshalFn) error

func (*Tx) GetBytes

func (tx *Tx) GetBytes(bucket, key string, clone bool) (out []byte)

func (*Tx) GetValue

func (tx *Tx) GetValue(bucket, key string, out any) error

func (*Tx) MustBucket

func (tx *Tx) MustBucket(bucket string) *Bucket

func (*Tx) NextIndex

func (tx *Tx) NextIndex(bucket string) (uint64, error)

func (*Tx) NextIndexBig

func (tx *Tx) NextIndexBig(bucket string) (*big.Int, error)

func (*Tx) PutAny

func (tx *Tx) PutAny(bucket, key string, val any, marshalFn MarshalFn) error

func (*Tx) PutBytes

func (tx *Tx) PutBytes(bucket, key string, val []byte) error

func (*Tx) PutValue

func (tx *Tx) PutValue(bucket, key string, val any) error

func (*Tx) Range

func (tx *Tx) Range(bucket string, start []byte, fn func(cursor *Cursor, k, v []byte) error, forward bool) (err error)

func (*Tx) SetNextIndex

func (tx *Tx) SetNextIndex(bucket string, idx uint64) error

type TxBase

type TxBase interface {
	GetBytes(bucket, key string, clone bool) (out []byte)
	ForEachBytes(bucket string, fn func(k, v []byte) error) error
	PutBytes(bucket, key string, val []byte) error
	Delete(bucket, key string) error
	DeleteBucket(bucket string) error
}

type TxStats

type TxStats = bbolt.TxStats

bbolt type aliases

type TypedDB

type TypedDB[T any] struct {
	*DB
}

func DBToTyped

func DBToTyped[T any](db *DB) TypedDB[T]

func OpenMultiTDB

func OpenMultiTDB[T any](ctx context.Context, m *MultiDB, path string, opts *Options) (db TypedDB[T], err error)

func OpenTDB

func OpenTDB[T any](ctx context.Context, path string, opts *Options) (db TypedDB[T], err error)

func (TypedDB[T]) ForEach

func (db TypedDB[T]) ForEach(bucket string, fn func(key string, v T) error) error

func (TypedDB[T]) Get

func (db TypedDB[T]) Get(bucket, key string) (v T, err error)

func (TypedDB[T]) Put

func (db TypedDB[T]) Put(bucket, key string, val T) error

type TypedTx

type TypedTx[T any] struct {
	*Tx
}

func (TypedTx[T]) ForEach

func (tx TypedTx[T]) ForEach(bucket string, fn func(key string, v T) error) error

func (TypedTx[T]) Get

func (tx TypedTx[T]) Get(bucket, key string) (v T, err error)

func (TypedTx[T]) MustGet

func (tx TypedTx[T]) MustGet(bucket, key string, def T) (v T)

func (TypedTx[T]) Put

func (tx TypedTx[T]) Put(bucket, key string, v T) error

type UnmarshalFn

type UnmarshalFn = func([]byte, any) error

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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