pebbledb

package
v2.0.0-...-fda440d Latest Latest
Warning

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

Go to latest
Published: May 31, 2024 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Index

Constants

View Source
const (
	VersionSize = 8
	// PruneCommitBatchSize defines the size, in number of key/value pairs, to prune
	// in a single batch.
	PruneCommitBatchSize = 50

	StorePrefixTpl = "s/k:%s/" // s/k:<storeKey>

)

Variables

View Source
var MVCCComparer = &pebble.Comparer{
	Name: "ss_pebbledb_comparator",

	Compare: MVCCKeyCompare,

	AbbreviatedKey: func(k []byte) uint64 {
		key, _, ok := SplitMVCCKey(k)
		if !ok {
			return 0
		}

		return pebble.DefaultComparer.AbbreviatedKey(key)
	},

	Equal: func(a, b []byte) bool {
		return MVCCKeyCompare(a, b) == 0
	},

	Separator: func(dst, a, b []byte) []byte {
		aKey, _, ok := SplitMVCCKey(a)
		if !ok {
			return append(dst, a...)
		}

		bKey, _, ok := SplitMVCCKey(b)
		if !ok {
			return append(dst, a...)
		}

		if bytes.Equal(aKey, bKey) {
			return append(dst, a...)
		}

		n := len(dst)

		dst = pebble.DefaultComparer.Separator(dst, aKey, bKey)

		buf := dst[n:]
		if bytes.Equal(aKey, buf) {
			return append(dst[:n], a...)
		}

		return append(dst, 0)
	},

	ImmediateSuccessor: func(dst, a []byte) []byte {

		return append(append(dst, a...), 0)
	},

	Successor: func(dst, a []byte) []byte {
		aKey, _, ok := SplitMVCCKey(a)
		if !ok {
			return append(dst, a...)
		}

		n := len(dst)

		dst = pebble.DefaultComparer.Successor(dst, aKey)

		buf := dst[n:]
		if bytes.Equal(aKey, buf) {
			return append(dst[:n], a...)
		}

		return append(dst, 0)
	},

	FormatKey: func(k []byte) fmt.Formatter {
		return mvccKeyFormatter{key: k}
	},

	Split: func(k []byte) int {
		key, _, ok := SplitMVCCKey(k)
		if !ok {
			return len(k)
		}

		return len(key) + 1
	},
}

MVCCComparer returns a PebbleDB Comparer with encoding and decoding routines for MVCC control, used to compare and store versioned keys.

Note: This Comparer implementation is largely based on PebbleDB's internal MVCC example, which can be found here: https://github.com/cockroachdb/pebble/blob/master/cmd/pebble/mvcc.go

Functions

func MVCCEncode

func MVCCEncode(key []byte, version uint64) (dst []byte)

<key>\x00[<version>]<#version-bytes>

func MVCCKeyCompare

func MVCCKeyCompare(a, b []byte) int

MVCCKeyCompare compares two MVCC keys.

func SplitMVCCKey

func SplitMVCCKey(mvccKey []byte) (key, version []byte, ok bool)

SplitMVCCKey accepts an MVCC key and returns the "user" key, the MVCC version, and a boolean indicating if the provided key is an MVCC key.

Note, internally, we must make a copy of the provided mvccKey argument, which typically comes from the Key() method as it's not safe.

Types

type Batch

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

func NewBatch

func NewBatch(storage *pebble.DB, version uint64, sync bool) (*Batch, error)

func (*Batch) Delete

func (b *Batch) Delete(storeKey, key []byte) error

func (*Batch) Reset

func (b *Batch) Reset() error

func (*Batch) Set

func (b *Batch) Set(storeKey, key, value []byte) error

func (*Batch) Size

func (b *Batch) Size() int

func (*Batch) Write

func (b *Batch) Write() (err error)

type Database

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

func New

func New(dataDir string) (*Database, error)

func NewWithDB

func NewWithDB(storage *pebble.DB, sync bool) *Database

func (*Database) Close

func (db *Database) Close() error

func (*Database) Get

func (db *Database) Get(storeKey []byte, targetVersion uint64, key []byte) ([]byte, error)

func (*Database) GetLatestVersion

func (db *Database) GetLatestVersion() (uint64, error)

func (*Database) Has

func (db *Database) Has(storeKey []byte, version uint64, key []byte) (bool, error)

func (*Database) Iterator

func (db *Database) Iterator(storeKey []byte, version uint64, start, end []byte) (corestore.Iterator, error)

func (*Database) NewBatch

func (db *Database) NewBatch(version uint64) (store.Batch, error)

func (*Database) Prune

func (db *Database) Prune(version uint64) error

Prune removes all versions of all keys that are <= the given version.

Note, the implementation of this method is inefficient and can be potentially time consuming given the size of the database and when the last pruning occurred (if any). This is because the implementation iterates over all keys in the database in order to delete them.

See: https://github.com/cockroachdb/cockroach/blob/33623e3ee420174a4fd3226d1284b03f0e3caaac/pkg/storage/mvcc.go#L3182

func (*Database) ReverseIterator

func (db *Database) ReverseIterator(storeKey []byte, version uint64, start, end []byte) (corestore.Iterator, error)

func (*Database) SetLatestVersion

func (db *Database) SetLatestVersion(version uint64) error

func (*Database) SetSync

func (db *Database) SetSync(sync bool)

Jump to

Keyboard shortcuts

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