store

package
v0.0.0-...-9de1cbb Latest Latest
Warning

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

Go to latest
Published: Feb 12, 2020 License: MIT Imports: 21 Imported by: 2

Documentation

Overview

Package store provides a bolt 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.

Index

Constants

This section is empty.

Variables

View Source
var ErrBucketNotFound = errors.New("bucket not found")

ErrBucketNotFound bucket not found error

View Source
var ErrNotLeader = raft.ErrNotLeader

ErrNotLeader not leader error

Functions

This section is empty.

Types

type Cache

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

Cache implements a non-thread safe fixed size LRU cache.

func NewCache

func NewCache(maxSize uint64, onEvict EvictCallback, logger log.Logger) *Cache

NewCache constructs an LRU cache of the given size.

func (*Cache) Add

func (c *Cache) Add(bucketName []byte, key []byte, value []byte) bool

Add adds a value to the cache. Returns true if an eviction occurred.

func (*Cache) Contains

func (c *Cache) Contains(bucket, key []byte) (ok bool)

Contains checks if a key is in the cache, without updating the recent-ness or deleting it for being stale.

func (*Cache) Get

func (c *Cache) Get(bucket, key []byte) (value []byte, ok bool)

Get looks up a key's value from the cache.

func (*Cache) GetOldest

func (c *Cache) GetOldest() ([]byte, []byte, []byte, bool)

GetOldest returns the oldest entry.

func (*Cache) Keys

func (c *Cache) Keys() []string

Keys returns a slice of the keys in the cache, from oldest to newest.

func (*Cache) Len

func (c *Cache) Len() int

Len returns the number of items in the cache.

func (*Cache) Open

func (c *Cache) Open()

Open cache

func (*Cache) Peek

func (c *Cache) Peek(bucket, key []byte) (value []byte, ok bool)

Peek returns the key value (or undefined if not found) without updating the "recently used"-ness of the key.

func (*Cache) Purge

func (c *Cache) Purge()

Purge is used to completely clear the cache.

func (*Cache) Remove

func (c *Cache) Remove(bucket, key []byte) bool

Remove removes the provided key from the cache, returning if the key was contained.

func (*Cache) RemoveBucket

func (c *Cache) RemoveBucket(bucket []byte) bool

RemoveBucket remove Bucket removes the provided bucket from the cache, returning if the bucket was contained.

func (*Cache) RemoveOldest

func (c *Cache) RemoveOldest() ([]byte, []byte, []byte, bool)

RemoveOldest removes the oldest item from the cache.

func (*Cache) Statistics

func (c *Cache) Statistics(tags map[string]string) []model.Statistic

Statistics returns statistics for periodic monitoring.

type CacheStatistics

type CacheStatistics struct {
	MemSizeBytes      int64
	PurgeCount        int64
	AddCount          int64
	GetCount          int64
	HitCount          int64
	MissCount         int64
	RemoveCount       int64
	RemoveBucketCount int64
	RemoveOldestCount int64
	KeysCount         int64
}

CacheStatistics hold statistics related to the cache.

type ClusterState

type ClusterState int

ClusterState defines the possible Raft states the current node can be in

const (
	Leader ClusterState = iota
	Follower
	Candidate
	Shutdown
	Unknown
)

Represents the Raft cluster states

type EvictCallback

type EvictCallback func(bucket []byte, key []byte, value []byte)

EvictCallback is used to get a callback when a cache entry is evicted.

type LodaSession

type LodaSession struct {
	//SessionMap store all client token
	SessionMap map[interface{}]interface{}
	// Mux locks SessionMap
	Mux sync.Mutex
}

LodaSession struct

func NewSession

func NewSession() *LodaSession

NewSession returns a new session

func (*LodaSession) Delete

func (ls *LodaSession) Delete(k interface{})

Delete removes the session value associated to the given key.

func (*LodaSession) Get

func (ls *LodaSession) Get(i interface{}) interface{}

Get returns the session value associated to the given key.

func (*LodaSession) Set

func (ls *LodaSession) Set(k, v interface{})

Set sets the session value associated to the given key.

type Session

type Session interface {
	// Get returns the session value associated to the given key.
	Get(key interface{}) interface{}
	// Set sets the session value associated to the given key.
	Set(key interface{}, val interface{})
	// Delete removes the session value associated to the given key.
	Delete(key interface{})
}

Session interface key is user token, value is user id.

type Store

type Store struct {
	Dir string

	// TODO: maybe need to config
	SnapshotThreshold uint64
	HeartbeatTimeout  time.Duration
	// contains filtered or unexported fields
}

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

func New

func New(path string, tn Transport, logger log.Logger) *Store

New returns a new Store.

func (*Store) APIPeers

func (s *Store) APIPeers() (map[string]string, error)

APIPeers return the map of Raft addresses to API addresses. Delete apiPeer record not in the cluster.

func (*Store) Addr

func (s *Store) Addr() string

Addr returns the address of the store.

func (*Store) Backup

func (s *Store) Backup() ([]byte, error)

Backup returns a snapshot of the store.

func (*Store) Batch

func (s *Store) Batch(rows []model.Row) error

Batch update the values for the given keys.

func (*Store) Close

func (s *Store) Close(wait bool) error

Close closes the store. If wait is true, waits for a graceful shutdown.

func (*Store) CreateBucket

func (s *Store) CreateBucket(name []byte) error

CreateBucket create a bucket.

func (*Store) CreateBucketIfNotExist

func (s *Store) CreateBucketIfNotExist(name []byte) error

CreateBucketIfNotExist create a bucket if not exist.

func (*Store) DelSession

func (s *Store) DelSession(k interface{}) error

DelSession delete a session by given key.

func (*Store) GetSession

func (s *Store) GetSession(k interface{}) interface{}

GetSession get a session.

func (*Store) IsLeader

func (s *Store) IsLeader() bool

IsLeader is used to determine if the current node is cluster leader.

func (*Store) Join

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

Join joins a node, located at addr, to this store. The node must be ready to respond to Raft communications at that address.

func (*Store) Leader

func (s *Store) Leader() string

Leader returns the current leader. Returns a blank string if there is no leader.

func (*Store) Nodes

func (s *Store) Nodes() ([]string, error)

Nodes returns the list of current peers.

func (*Store) Open

func (s *Store) Open(enableSingle bool) 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.

func (*Store) Path

func (s *Store) Path() string

Path returns the path to the store's storage directory.

func (*Store) Peer

func (s *Store) Peer(addr string) string

Peer returns the API address for the given addr. If there is no peer for the address, it returns the empty string.

func (*Store) Remove

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

Remove removes a node from the store, specified by addr. NOTE: raft Bug will cause the claster cannot add peer any more.

func (*Store) RemoveBucket

func (s *Store) RemoveBucket(name []byte) error

RemoveBucket remove a bucket.

func (*Store) RemoveKey

func (s *Store) RemoveKey(bucket, key []byte) error

RemoveKey remove a key from store

func (*Store) Restore

func (s *Store) Restore(backupfile string) error

Restore restores backup data file.

func (*Store) SetSession

func (s *Store) SetSession(k, v interface{}) error

SetSession set a session.

func (*Store) State

func (s *Store) State() ClusterState

State returns the current node's Raft state.

func (*Store) Statistics

func (s *Store) Statistics(tags map[string]string) []model.Statistic

Statistics returns statistics for periodic monitoring.

func (*Store) Update

func (s *Store) Update(bucket []byte, key []byte, value []byte) error

Update the value for the given key.

func (*Store) UpdateAPIPeers

func (s *Store) UpdateAPIPeers(peers map[string]string) error

UpdateAPIPeers updates the cluster-wide peer information.

func (*Store) View

func (s *Store) View(bucket, key []byte) ([]byte, error)

View returns the value for the given key.

func (*Store) ViewPrefix

func (s *Store) ViewPrefix(bucket, keyPrefix []byte) (map[string][]byte, error)

ViewPrefix views bucket by keyPerfix.

func (*Store) WaitForLeader

func (s *Store) WaitForLeader(timeout time.Duration) (string, error)

WaitForLeader blocks until a leader is detected, or the timeout expires.

type Transport

type Transport interface {
	net.Listener

	// Dial is used to create a new outgoing connection
	Dial(address string, timeout time.Duration) (net.Conn, error)
}

Transport is the interface the network service must provide.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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