store

package
v0.0.0-...-36ee774 Latest Latest
Warning

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

Go to latest
Published: Nov 6, 2023 License: MIT Imports: 17 Imported by: 0

Documentation

Overview

Package store provides a simple distributed key-value store. The keys and associated values are changed via distributed consensus, meaning that the values are changed only when a majority of nodes in the cluster agree on the new value.

Distributed consensus is provided via the Raft algorithm, specifically the Hashicorp implementation.

Index

Constants

This section is empty.

Variables

View Source
var ErrInvalidMessage = errors.New("invalid message")
View Source
var (

	// ErrKeyNotFound
	// An error indicating a given key does not exist
	ErrKeyNotFound = errors.New("not found")
)

Functions

func Build

func Build(message *Message) []byte

func BytesToString

func BytesToString(b []byte) string

func StringToBytes

func StringToBytes(s string) []byte

Types

type BoltStore

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

BoltStore provides access to BoltDB for Raft to store and retrieve log entries. It also provides key/value storage, and can be used as a LogStore and StableStore.

func NewBolt

func NewBolt(options Options) (*BoltStore, error)

NewBolt New uses the supplied options to open the BoltDB and prepare it for use as a raft backend.

func NewBoltStore

func NewBoltStore(path string) (*BoltStore, error)

NewBoltStore takes a file path and returns a connected Raft backend.

func (*BoltStore) Close

func (b *BoltStore) Close() error

Close is used to gracefully close the DB connection.

func (*BoltStore) Count

func (b *BoltStore) Count() uint64

Count Close is used to gracefully close the DB connection.

func (*BoltStore) DeleteRange

func (b *BoltStore) DeleteRange(min, max uint64) error

DeleteRange is used to delete logs within a given range inclusively.

func (*BoltStore) FirstIndex

func (b *BoltStore) FirstIndex() (uint64, error)

FirstIndex returns the first known index from the Raft log.

func (*BoltStore) Get

func (b *BoltStore) Get(k []byte) ([]byte, error)

Get is used to retrieve a value from the k/v store by key

func (*BoltStore) GetLog

func (b *BoltStore) GetLog(idx uint64, log *raft.Log) error

GetLog is used to retrieve a log from BoltDB at a given index.

func (*BoltStore) GetUint64

func (b *BoltStore) GetUint64(key []byte) (uint64, error)

GetUint64 is like Get, but handles uint64 values

func (*BoltStore) LastIndex

func (b *BoltStore) LastIndex() (uint64, error)

LastIndex returns the last known index from the Raft log.

func (*BoltStore) RemoveAll

func (b *BoltStore) RemoveAll() error

func (*BoltStore) Set

func (b *BoltStore) Set(k, v []byte) error

Set is used to set a key/value set outside of the raft log

func (*BoltStore) SetUint64

func (b *BoltStore) SetUint64(key []byte, val uint64) error

SetUint64 is like Set, but handles uint64 values

func (*BoltStore) StoreLog

func (b *BoltStore) StoreLog(log *raft.Log) error

StoreLog is used to store a single raft log

func (*BoltStore) StoreLogs

func (b *BoltStore) StoreLogs(logs []*raft.Log) error

StoreLogs is used to store a set of raft logs

func (*BoltStore) Sync

func (b *BoltStore) Sync() error

Sync performs an fsync on the database file handle. This is not necessary under normal operation unless NoSync is enabled, in which this forces the database file to sync against the disk.

type KV

type KV struct {
	Key   string              `json:"key"`
	Value jsoniter.RawMessage `json:"value"`
}

type Message

type Message struct {
	Op    Op
	Key   string
	Value []byte
}

func Parse

func Parse(data []byte) (*Message, error)

func ParseMulti

func ParseMulti(data []byte) ([]*Message, error)

type Op

type Op byte
const (
	Delete Op = iota
	Set
	Clear
)

type Options

type Options struct {
	// Path is the file path to the BoltDB to use
	Path string

	// BoltOptions contains any specific BoltDB options you might
	// want to specify [e.g. open timeout]
	BoltOptions *bolt.Options

	// NoSync causes the database to skip fsync calls after each
	// write to the log. This is unsafe, so it should be used
	// with caution.
	NoSync bool
}

Options contains all the configuration used to open the BoltDB

type Store

type Store struct {
	RaftDir  string
	RaftAddr string

	OnLeaderChange func(leader raft.LeaderObservation)
	OnPeerChange   func(leader raft.PeerObservation)
	OnKeyChange    func(op *Message)
	Ready          chan bool
	IsReady        bool
	// contains filtered or unexported fields
}

Store is a simple key-value store, where all changes are made via Raft consensus.

func New

func New(dataDir, raftAddr string) *Store

New returns a new Store.

func (*Store) All

func (s *Store) All() []*KV

func (*Store) BootstrapCluster

func (s *Store) BootstrapCluster(ok bool)

BootstrapCluster MUST SET ONE

func (*Store) Clear

func (s *Store) Clear() error

Clear clears all KV pairs in the store.

func (*Store) Delete

func (s *Store) Delete(key string) error

Delete deletes the given key.

func (*Store) Get

func (s *Store) Get(key string) ([]byte, error)

Get returns the value for the given key.

func (*Store) Join

func (s *Store) Join(addr string) error

func (*Store) Leave

func (s *Store) Leave(addr string) error

func (*Store) Open

func (s *Store) Open() error

Open opens the store. If enableSingle is set, and there are no existing peers, then this node becomes the first node, and therefore leader, of the cluster. localID should be the server identifier for this node.

func (*Store) Raft

func (s *Store) Raft() *raft.Raft

func (*Store) Set

func (s *Store) Set(key string, value []byte) error

Set sets the value for the given key.

func (*Store) Shutdown

func (s *Store) Shutdown() raft.Future

Jump to

Keyboard shortcuts

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