Documentation ¶
Overview ¶
Package common contains utilities that are used accross various Babble packages.
Index ¶
- Variables
- func DecodeFromString(hexString string) ([]byte, error)
- func EncodeToString(hexBytes []byte) string
- func IsStore(err error, t StoreErrType) bool
- func Median(input []int64) (median int64)
- func NewTestEntry(t testing.TB, level logrus.Level) *logrus.Entry
- func NewTestLogger(t testing.TB, level logrus.Level) *logrus.Logger
- type EvictCallback
- type LRU
- func (c *LRU) Add(key, value interface{}) bool
- func (c *LRU) Contains(key interface{}) (ok bool)
- func (c *LRU) Get(key interface{}) (value interface{}, ok bool)
- func (c *LRU) GetOldest() (interface{}, interface{}, bool)
- func (c *LRU) Keys() []interface{}
- func (c *LRU) Len() int
- func (c *LRU) Peek(key interface{}) (value interface{}, ok bool)
- func (c *LRU) Purge()
- func (c *LRU) Remove(key interface{}) bool
- func (c *LRU) RemoveOldest() (interface{}, interface{}, bool)
- type RollingIndex
- type RollingIndexMap
- func (rim *RollingIndexMap) AddKey(key uint32) error
- func (rim *RollingIndexMap) Get(key uint32, skipIndex int) ([]interface{}, error)
- func (rim *RollingIndexMap) GetItem(key uint32, index int) (interface{}, error)
- func (rim *RollingIndexMap) GetLast(key uint32) (interface{}, error)
- func (rim *RollingIndexMap) Known() map[uint32]int
- func (rim *RollingIndexMap) Set(key uint32, item interface{}, index int) error
- type StoreErr
- type StoreErrType
- type Trilean
Constants ¶
This section is empty.
Variables ¶
var TestLogLevel = logrus.InfoLevel
TestLogLevel is the level used in tests by default.
Functions ¶
func DecodeFromString ¶ added in v0.5.0
DecodeFromString converts a hex string with 0X prefix to a byte slice
func EncodeToString ¶ added in v0.5.0
EncodeToString returns the UPPERCASE string representation of hexBytes with the 0X prefix
func IsStore ¶ added in v0.5.5
func IsStore(err error, t StoreErrType) bool
IsStore checks that an error is of type StoreErr and that it's code matches the provided StoreErr code.
func NewTestEntry ¶ added in v0.5.2
NewTestEntry returns a logrus Entry for testing.
Types ¶
type EvictCallback ¶
type EvictCallback func(key interface{}, value interface{})
EvictCallback is used to get a callback when a cache entry is evicted
type LRU ¶
type LRU struct {
// contains filtered or unexported fields
}
LRU implements a non-thread safe fixed size LRU cache
func NewLRU ¶
func NewLRU(size int, onEvict EvictCallback) *LRU
NewLRU constructs an LRU of the given size
func (*LRU) Contains ¶
Contains checks if a key is in the cache, without updating the recent-ness or deleting it for being stale.
func (*LRU) Keys ¶
func (c *LRU) Keys() []interface{}
Keys returns a slice of the keys in the cache, from oldest to newest.
func (*LRU) Peek ¶
Peek returns the key value (or undefined if not found) without updating the "recently used"-ness of the key.
func (*LRU) Remove ¶
Remove removes the provided key from the cache, returning if the key was contained.
func (*LRU) RemoveOldest ¶
RemoveOldest removes the oldest item from the cache.
type RollingIndex ¶
type RollingIndex struct {
// contains filtered or unexported fields
}
RollingIndex is a FIFO cache that evicts half of it's items when it reaches full capacity. It contains items in strict sequential order where index is a property of the items. It prevents skipping indexes when inserting new items.
func NewRollingIndex ¶
func NewRollingIndex(name string, size int) *RollingIndex
NewRollingIndex creates a new RollingIndex that contains up to size items.
func (*RollingIndex) Get ¶
func (r *RollingIndex) Get(skipIndex int) ([]interface{}, error)
Get returns all the items with index greater than skipIndex or a TooLate error if some items have been evicted.
func (*RollingIndex) GetItem ¶
func (r *RollingIndex) GetItem(index int) (interface{}, error)
GetItem retrieves an item by index. It returns a TooLate error if the item was evicted, or a KeyNotFound error if the item is not found.
func (*RollingIndex) GetLastWindow ¶
func (r *RollingIndex) GetLastWindow() (lastWindow []interface{}, lastIndex int)
GetLastWindow returns all the items contained in the cache and the index of the last item.
func (*RollingIndex) Set ¶
func (r *RollingIndex) Set(item interface{}, index int) error
Set inserts an item and evicts the earlier half of the cache if it reached full capacity. It returns a SkippedIndex error if the item's index is greater than last index + 1. It allows setting items in place if the index corresponds to an existing item.
type RollingIndexMap ¶
type RollingIndexMap struct {
// contains filtered or unexported fields
}
RollingIndexMap is a collection of RollingIndexes.
func NewRollingIndexMap ¶
func NewRollingIndexMap(name string, size int) *RollingIndexMap
NewRollingIndexMap creates a new RollingIndexMap where each RollingIndex has the specified size.
func (*RollingIndexMap) AddKey ¶ added in v0.4.1
func (rim *RollingIndexMap) AddKey(key uint32) error
AddKey adds a new RollingIndex to the map and returns a KeyAlreadyExists if the key already exists.
func (*RollingIndexMap) Get ¶
func (rim *RollingIndexMap) Get(key uint32, skipIndex int) ([]interface{}, error)
Get returns all the items with index greater than skipIndex from the RollingIndex indentified by key.
func (*RollingIndexMap) GetItem ¶
func (rim *RollingIndexMap) GetItem(key uint32, index int) (interface{}, error)
GetItem returns specific item from a specific RollingIndex.
func (*RollingIndexMap) GetLast ¶
func (rim *RollingIndexMap) GetLast(key uint32) (interface{}, error)
GetLast returns the last item from a RolllingIndex indentified by key.
func (*RollingIndexMap) Known ¶
func (rim *RollingIndexMap) Known() map[uint32]int
Known returns a mapping of key to last known index.
type StoreErr ¶
type StoreErr struct {
// contains filtered or unexported fields
}
StoreErr is a generic error type that encodes errors when accessing objects in the hashgraph store.
func NewStoreErr ¶
func NewStoreErr(dataType string, errType StoreErrType, key string) StoreErr
NewStoreErr creates a StoreErr pertaining to an object indentified by it's dataType and key. The errType parameter determines the nature of the error.
type StoreErrType ¶
type StoreErrType uint32
StoreErrType encodes the nature of a StoreErr
const ( // KeyNotFound signifies an item is not found. KeyNotFound StoreErrType = iota // TooLate signifies that an item is no longer in the store because it was // evicted from the cache. TooLate // SkippedIndex signifies that an attempt was made to insert an // non-sequential item in a cache. SkippedIndex // UnknownParticipant signifies that an attempt was made to retrieve objects // associated to a non-existant participant. UnknownParticipant // Empty signifies that a cache is empty. Empty // KeyAlreadyExists Signifies that an attempt was made to insert an item // already present in a cache. KeyAlreadyExists )