memstore

package
v1.3.1 Latest Latest
Warning

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

Go to latest
Published: Sep 15, 2021 License: Apache-2.0 Imports: 3 Imported by: 0

README

Using MemStore

Memstore acts like a sorted dictionary that can be flushed into an SSTable representation on disk. It allows you to add, update, retrieve and delete elements by their key, of which both are represented by byte slices.

A simple example below illustrates all functionality of the memstore:

path := "/tmp/sstable-ms-ex/"
defer os.RemoveAll(path)

ms := memstore.NewMemStore()
ms.Add([]byte{1}, []byte{1})
ms.Add([]byte{2}, []byte{2})
ms.Upsert([]byte{1}, []byte{2})
ms.Delete([]byte{2})
ms.DeleteIfExists([]byte{3})
value, _ := ms.Get([]byte{1})
log.Printf("value for key 1: %d", value) // yields 2

size := ms.EstimatedSizeInBytes()
log.Printf("memstore size in bytes: %d", size) // yields 3

ms.Flush(sstables.WriteBasePath(path))

You can get the full example from examples/memstore.go.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var KeyAlreadyExists = errors.New("key already exists")
View Source
var KeyNil = errors.New("key was nil")
View Source
var KeyNotFound = errors.New("key not found")
View Source
var KeyTombstoned = errors.New("key was tombstoned")
View Source
var ValueNil = errors.New("value was nil")

Functions

This section is empty.

Types

type MemStore

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

func (*MemStore) Add

func (m *MemStore) Add(key []byte, value []byte) error

func (*MemStore) Contains

func (m *MemStore) Contains(key []byte) bool

func (*MemStore) Delete

func (m *MemStore) Delete(key []byte) error

func (*MemStore) DeleteIfExists

func (m *MemStore) DeleteIfExists(key []byte) error

func (*MemStore) EstimatedSizeInBytes

func (m *MemStore) EstimatedSizeInBytes() uint64

func (*MemStore) Flush

func (m *MemStore) Flush(writerOptions ...sstables.WriterOption) error

func (*MemStore) FlushWithTombstones added in v1.3.0

func (m *MemStore) FlushWithTombstones(writerOptions ...sstables.WriterOption) error

func (*MemStore) Get

func (m *MemStore) Get(key []byte) ([]byte, error)

func (*MemStore) SStableIterator added in v1.3.0

func (m *MemStore) SStableIterator() sstables.SSTableIteratorI

func (*MemStore) Size added in v1.3.0

func (m *MemStore) Size() int

func (*MemStore) Tombstone added in v1.3.0

func (m *MemStore) Tombstone(key []byte) error

func (*MemStore) Upsert

func (m *MemStore) Upsert(key []byte, value []byte) error

type MemStoreI

type MemStoreI interface {
	// inserts when the key does not exist yet, returns a KeyAlreadyExists error when the key exists.
	// Neither nil key nor values are allowed, KeyNil and ValueNil will be returned accordingly.
	Add(key []byte, value []byte) error
	// returns true when the given key exists, false otherwise
	Contains(key []byte) bool
	// returns the values for the given key, if not exists returns a KeyNotFound error
	// if the key exists (meaning it was added and deleted) it will return KeyTombstoned as an error
	Get(key []byte) ([]byte, error)
	// inserts when the key does not exist yet, updates the current value if the key exists.
	// Neither nil key nor values are allowed, KeyNil and ValueNil will be returned accordingly.
	Upsert(key []byte, value []byte) error
	// deletes the key from the MemStore, returns a KeyNotFound error if the key does not exist.
	// Effectively this will set a tombstone for the given key and set its value to be nil.
	Delete(key []byte) error
	// deletes the key from the MemStore. The semantic is the same as in Delete, however when there is no
	// key in the memstore it will also not set a tombstone for it and there will be no error when the key isn't there.
	// That can be problematic in several database constellation, where you can use Tombstone.
	DeleteIfExists(key []byte) error
	// Tombstone records the given key as if it were deleted. When a given key does not exist it will insert it,
	// when it does it will do a Delete
	Tombstone(key []byte) error
	// returns a rough estimate of size in bytes of this MemStore
	EstimatedSizeInBytes() uint64
	// Flush flushes the current memstore to disk as an SSTable, error if unsuccessful. This excludes tombstoned keys.
	Flush(opts ...sstables.WriterOption) error
	// FlushWithTombstones flushes the current memstore to disk as an SSTable, error if unsuccessful.
	// This includes tombstoned keys and writes their values as nil.
	FlushWithTombstones(opts ...sstables.WriterOption) error
	// returns the current memstore as an sstables.SStableIteratorI to iterate the table in memory.
	// if there is a tombstoned record, the key will be returned but the value will be nil.
	// this is especially useful when you want to merge on-disk sstables with in memory memstores
	SStableIterator() sstables.SSTableIteratorI
	// Returns how many elements are in this memstore. This also includes tombstoned keys.
	Size() int
}

noinspection GoNameStartsWithPackageName

func NewMemStore

func NewMemStore() MemStoreI

type SkipListSStableIterator added in v1.3.0

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

func (SkipListSStableIterator) Next added in v1.3.0

func (s SkipListSStableIterator) Next() ([]byte, []byte, error)

type ValueStruct

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

func (ValueStruct) GetValue

func (v ValueStruct) GetValue() []byte

Jump to

Keyboard shortcuts

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