kvstore

package
v0.0.0-...-40ef43a Latest Latest
Warning

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

Go to latest
Published: Mar 4, 2021 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNotLeader is the error returned whn an operation that is
	// only permitted on the leader is attempted on a follower node.
	ErrNotLeader error = fmt.Errorf("operation not permitted on followers")
)

Functions

This section is empty.

Types

type Command

type Command struct {
	Type  CommandType `json:"type"`
	Key   string      `json:"key"`
	Value string      `json:"value,omitempty"`
}

Command is the Finite State Machine command.

type CommandType

type CommandType int8

CommandType represents the type of command.

const (
	// Get is the "Get" command type.
	Get CommandType = iota
	// Set is the "Set" command type.
	Set
	// Delete is the "Delete" command type.
	Delete
)

type KVStore

type KVStore interface {
	// Get retrieves a value from the store, given its key.
	Get(key string) (string, error)
	// Set sets a value into the store, creating it if non existing.
	Set(key string, value string) error
	// Delete removes a key/value pair from the store.
	Delete(key string) error
}

KVStore is the common interface to all key/value stores.

type LocalStore

type LocalStore struct {
	sqlite.Store
}

LocalStore is the non-replicated, local-only SQLite-based implementation of the KVStore interface.

func NewLocalStore

func NewLocalStore(options ...sqlite.Option) (*LocalStore, error)

NewLocalStore creates a new SQLite-based, non-replicated implementation of the KVSTore interface.

func (*LocalStore) Delete

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

Delete removes the key/value pair from the store.

func (*LocalStore) Get

func (s *LocalStore) Get(key string) (string, error)

Get returns the value for the given key.

func (*LocalStore) Set

func (s *LocalStore) Set(key string, value string) error

Set sets a value under the given key; if existing, it is updated, otherwise a new key/value pair is created.

type ReplicatedStore

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

ReplicatedStore is the replicated, Raft-based implementation of the KVStore interface; it achieves this by coordinating the Raft cluster and the local database.

func NewReplicatedStore

func NewReplicatedStore(allowGetOnFollower bool, store *LocalStore, cluster *cluster.Cluster) *ReplicatedStore

NewReplicatedStore allocates a new ReplicatedStore which will funnel the commands through the Raft log, leaving it to the cluster to apply the log entries to the underlying, local version of the KVStore (localStore). If allowGetOnFollower is true, the ReplicatedStore will

func (*ReplicatedStore) Delete

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

Delete deletes the given key.

func (*ReplicatedStore) Get

func (s *ReplicatedStore) Get(key string) (string, error)

Get retrieves the value corresponding to the given key; since the Get command is non-mutating, it needs not go through the Apply() rigmarole; it can be served directly from the LocalStore; if the cluster allows it, the values can be served by the followers, which can speed things up but there is a non-null probability that the follower will serve stale data, when the log has not been acknowledged yet, ot it has been acknowledged but not applied yet to the local store.

func (*ReplicatedStore) Set

func (s *ReplicatedStore) Set(key, value string) error

Set sets the value for the given key.

type ReplicatedStoreFSM

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

ReplicatedStoreFSM is an SQLite-base Raft Finite State Machine.

func NewReplicatedStoreFSM

func NewReplicatedStoreFSM(store *LocalStore) *ReplicatedStoreFSM

func (*ReplicatedStoreFSM) Apply

func (s *ReplicatedStoreFSM) Apply(l *raft.Log) interface{}

Apply log is invoked once a log entry is committed. It returns a value which will be made available in the ApplyFuture returned by Raft.Apply method if that method was called on the same Raft node as the FSM.

func (*ReplicatedStoreFSM) Restore

func (s *ReplicatedStoreFSM) Restore(data io.ReadCloser) error

Restore restores the FSM to a previous state from a snapshot.

func (*ReplicatedStoreFSM) Snapshot

func (s *ReplicatedStoreFSM) Snapshot() (raft.FSMSnapshot, error)

Snapshot returns a snapshot of the key-value store, to support log compaction; the returned ReplicatedStoreFSM...

type SQLiteFSMSnapshot

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

SQLiteFSMSnapshot is a transient object, capable of generating a snaphot of the SQLite DB contents at the moment it was created.

func (*SQLiteFSMSnapshot) Persist

func (s *SQLiteFSMSnapshot) Persist(sink raft.SnapshotSink) error

Persist writes the SQLiteFSMSnapshot contents to the Raft-provided sink.

func (*SQLiteFSMSnapshot) Release

func (s *SQLiteFSMSnapshot) Release()

Release is called when a snapshot can be dismissed, so any resources and locks can be removed.

Jump to

Keyboard shortcuts

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