common

package
v0.8.5 Latest Latest
Warning

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

Go to latest
Published: Jul 10, 2021 License: MIT Imports: 7 Imported by: 5

Documentation

Overview

Package common contains utilities that are used accross various Babble packages.

Index

Constants

This section is empty.

Variables

View Source
var TestLogLevel = logrus.InfoLevel

TestLogLevel is the level used in tests by default.

Functions

func DecodeFromString added in v0.5.0

func DecodeFromString(hexString string) ([]byte, error)

DecodeFromString converts a hex string with 0X prefix to a byte slice

func EncodeToString added in v0.5.0

func EncodeToString(hexBytes []byte) string

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 Median added in v0.8.2

func Median(input []int64) (median int64)

Median gets the median number in a slice of numbers

func NewTestEntry added in v0.5.2

func NewTestEntry(t testing.TB, level logrus.Level) *logrus.Entry

NewTestEntry returns a logrus Entry for testing.

func NewTestLogger

func NewTestLogger(t testing.TB, level logrus.Level) *logrus.Logger

NewTestLogger return a logrus Logger 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) Add

func (c *LRU) Add(key, value interface{}) bool

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

func (*LRU) Contains

func (c *LRU) Contains(key interface{}) (ok bool)

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

func (*LRU) Get

func (c *LRU) Get(key interface{}) (value interface{}, ok bool)

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

func (*LRU) GetOldest

func (c *LRU) GetOldest() (interface{}, interface{}, bool)

GetOldest returns the oldest entry

func (*LRU) Keys

func (c *LRU) Keys() []interface{}

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

func (*LRU) Len

func (c *LRU) Len() int

Len returns the number of items in the cache.

func (*LRU) Peek

func (c *LRU) Peek(key interface{}) (value interface{}, ok bool)

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

func (*LRU) Purge

func (c *LRU) Purge()

Purge is used to completely clear the cache

func (*LRU) Remove

func (c *LRU) Remove(key interface{}) bool

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

func (*LRU) RemoveOldest

func (c *LRU) RemoveOldest() (interface{}, interface{}, bool)

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.

func (*RollingIndexMap) Set

func (rim *RollingIndexMap) Set(key uint32, item interface{}, index int) error

Set inserts or updates an item into a RollingIndex identified by key.

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.

func (StoreErr) Error

func (e StoreErr) Error() string

Error returns an error's message.

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
)

type Trilean added in v0.5.0

type Trilean int

Trilean is a boolean that can also be undefined

const (
	// Undefined means the value has not been defined yet
	Undefined Trilean = iota
	// True means the value is defined and true
	True
	// False means the value is defined and false
	False
)

func (Trilean) String added in v0.5.0

func (t Trilean) String() string

String returns the string representation of Trilean

Jump to

Keyboard shortcuts

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