Documentation ¶
Index ¶
- Constants
- func UUIDHashCode(input interface{}) uint32
- type ActionFunc
- type ConcurrentTxMap
- type FallibleOnceMap
- type HashFunc
- type IndexedTakeList
- type Iterator
- type MapEntry
- type MapIterator
- type OnceMap
- type PaginationFn
- type PagingIteratorImpl
- type PredicateFunc
- type Queue
- type ShardedConcurrentTxMap
- func (cmap *ShardedConcurrentTxMap) Contains(key interface{}) bool
- func (cmap *ShardedConcurrentTxMap) Get(key interface{}) (interface{}, bool)
- func (cmap *ShardedConcurrentTxMap) GetAndDo(key interface{}, fn ActionFunc) (interface{}, bool, error)
- func (cmap *ShardedConcurrentTxMap) Iter() MapIterator
- func (cmap *ShardedConcurrentTxMap) Len() int
- func (cmap *ShardedConcurrentTxMap) Put(key interface{}, value interface{})
- func (cmap *ShardedConcurrentTxMap) PutIfNotExist(key interface{}, value interface{}) bool
- func (cmap *ShardedConcurrentTxMap) PutOrDo(key interface{}, value interface{}, fn ActionFunc) (interface{}, bool, error)
- func (cmap *ShardedConcurrentTxMap) Remove(key interface{})
- func (cmap *ShardedConcurrentTxMap) RemoveIf(key interface{}, fn PredicateFunc) bool
- type SortedSetManager
- type SyncMap
Constants ¶
const ( // UUIDStringLength is the length of an UUID represented as a hex string UUIDStringLength = 36 // xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx )
Variables ¶
This section is empty.
Functions ¶
func UUIDHashCode ¶
func UUIDHashCode(input interface{}) uint32
UUIDHashCode is a hash function for hashing string uuid if the uuid is malformed, then the hash function always returns 0 as the hash value
Types ¶
type ActionFunc ¶
type ActionFunc func(key interface{}, value interface{}) error
ActionFunc take a key and value, do calculation and return err
type ConcurrentTxMap ¶
type ConcurrentTxMap interface { // Get returns the value for the given key Get(key interface{}) (interface{}, bool) // Contains returns true if the key exist and false otherwise Contains(key interface{}) bool // Put records the mapping from given key to value Put(key interface{}, value interface{}) // PutIfNotExist records the key value mapping only // if the mapping does not already exist PutIfNotExist(key interface{}, value interface{}) bool // Remove deletes the key from the map Remove(key interface{}) // GetAndDo returns the value corresponding to the key, and apply fn to key value before return value // return (value, value exist or not, error when evaluation fn) GetAndDo(key interface{}, fn ActionFunc) (interface{}, bool, error) // PutOrDo put the key value in the map, if key does not exists, otherwise, call fn with existing key and value // return (value, fn evaluated or not, error when evaluation fn) PutOrDo(key interface{}, value interface{}, fn ActionFunc) (interface{}, bool, error) // RemoveIf deletes the given key from the map if fn return true // return whether the key is removed or not RemoveIf(key interface{}, fn PredicateFunc) bool // Iter returns an iterator to the map Iter() MapIterator // Len returns the number of items in the map Len() int }
ConcurrentTxMap is a generic interface for any implementation of a dictionary or a key value lookup table that is thread safe, and providing functionality to modify key / value pair inside within a transaction
func NewShardedConcurrentTxMap ¶
func NewShardedConcurrentTxMap(initialCap int, hashfn HashFunc) ConcurrentTxMap
NewShardedConcurrentTxMap returns an instance of ShardedConcurrentMap
ShardedConcurrentMap is a thread safe map that maintains upto nShards number of maps internally to allow nShards writers to be acive at the same time. This map *does not* use re-entrant locks, so access to the map during iterator can cause a dead lock.
@param initialSz
The initial size for the map
@param hashfn
The hash function to use for sharding
type FallibleOnceMap ¶ added in v1.24.0
type FallibleOnceMap[K comparable, T any] struct { // contains filtered or unexported fields }
FallibleOnceMap is a concurrent map which lazily constructs its values. Map values are initialized on-the-fly, using a provided construction function only when a key is accessed for the first time. If the construct function returns an error, the value is not cached.
func NewFallibleOnceMap ¶ added in v1.24.0
func NewFallibleOnceMap[K comparable, T any](construct func(K) (T, error)) *FallibleOnceMap[K, T]
NewFallibleOnceMap creates a FallibleOnceMap from a given construct function. construct should be kept light as it is called while holding a lock on the entire map.
func (*FallibleOnceMap[K, T]) Get ¶ added in v1.24.0
func (p *FallibleOnceMap[K, T]) Get(key K) (T, error)
func (*FallibleOnceMap[K, T]) Pop ¶ added in v1.25.0
func (p *FallibleOnceMap[K, T]) Pop(key K) (T, bool)
type HashFunc ¶
type HashFunc func(interface{}) uint32
HashFunc represents a hash function for string
type IndexedTakeList ¶ added in v1.21.0
type IndexedTakeList[K comparable, V any] struct { // contains filtered or unexported fields }
IndexedTakeList holds a set of values that can only be observed by being removed from the set. It is possible for this set to contain duplicate values as long as each value maps to a distinct index.
func NewIndexedTakeList ¶ added in v1.21.0
func NewIndexedTakeList[K comparable, V any]( values []V, indexer func(V) K, ) *IndexedTakeList[K, V]
NewIndexedTakeList constructs a new IndexedTakeSet by applying the provided indexer to each of the provided values.
func (*IndexedTakeList[K, V]) Take ¶ added in v1.21.0
func (itl *IndexedTakeList[K, V]) Take(key K) (V, bool)
Take finds a value in this set by its key and removes it, returning the value.
func (*IndexedTakeList[K, V]) TakeRemaining ¶ added in v1.21.0
func (itl *IndexedTakeList[K, V]) TakeRemaining() []V
TakeRemaining removes all remaining values from this set and returns them.
type Iterator ¶ added in v0.5.8
type Iterator[V any] interface { // HasNext return whether this iterator has next value HasNext() bool // Next returns the next item and error Next() (V, error) }
Iterator represents the interface for iterator
func NewPagingIterator ¶ added in v0.5.8
func NewPagingIterator[V any]( paginationFn PaginationFn[V], ) Iterator[V]
NewPagingIterator create a new paging iterator
func NewPagingIteratorWithToken ¶ added in v1.5.7
func NewPagingIteratorWithToken[V any]( paginationFn PaginationFn[V], pageToken []byte, ) Iterator[V]
NewPagingIteratorWithToken create a new paging iterator with initial token
type MapEntry ¶
type MapEntry struct { // Key represents the key Key interface{} // Value represents the value Value interface{} }
MapEntry represents a key-value entry within the map
type MapIterator ¶
type MapIterator interface { // Close closes the iterator // and releases any allocated resources Close() // Entries returns a channel of MapEntry // objects that can be used in a range loop Entries() <-chan *MapEntry }
MapIterator represents the interface for map iterators
type OnceMap ¶ added in v1.24.0
type OnceMap[K comparable, T any] struct { // contains filtered or unexported fields }
OnceMap is a concurrent map which lazily constructs its values. Map values are initialized on-the-fly, using a provided construction function only when a key is accessed for the first time.
func NewOnceMap ¶ added in v1.24.0
func NewOnceMap[K comparable, T any](construct func(K) T) *OnceMap[K, T]
NewOnceMap creates a OnceMap from a given construct function. construct should be kept light as it is called while holding a lock on the entire map.
type PaginationFn ¶ added in v0.5.8
PaginationFn is the function which get a page of results
type PagingIteratorImpl ¶ added in v0.5.8
type PagingIteratorImpl[V any] struct { // contains filtered or unexported fields }
PagingIteratorImpl is the implementation of PagingIterator
type PredicateFunc ¶
type PredicateFunc func(key interface{}, value interface{}) bool
PredicateFunc take a key and value, do calculation and return boolean
type Queue ¶ added in v0.5.8
type Queue[T any] interface { // Peek returns the first item of the queue Peek() T // Add push an item to the queue Add(item T) // Remove pop an item from the queue Remove() T // IsEmpty indicate if the queue is empty IsEmpty() bool // Len return the size of the queue Len() int }
Queue is the interface for queue
func NewPriorityQueue ¶ added in v0.5.8
NewPriorityQueue create a new priority queue
func NewPriorityQueueWithItems ¶ added in v1.18.0
func NewPriorityQueueWithItems[T any]( compareLess func(this T, other T) bool, items []T, ) Queue[T]
NewPriorityQueueWithItems creats a new priority queue with the provided list of items. PriorityQueue will take ownership of the passed in items, so caller should stop modifying it. The complexity is O(n) where n is the number of items
type ShardedConcurrentTxMap ¶
type ShardedConcurrentTxMap struct {
// contains filtered or unexported fields
}
ShardedConcurrentTxMap is an implementation of ConcurrentMap that internally uses multiple sharded maps to increase parallelism
func (*ShardedConcurrentTxMap) Contains ¶
func (cmap *ShardedConcurrentTxMap) Contains(key interface{}) bool
Contains returns true if the key exist and false otherwise
func (*ShardedConcurrentTxMap) Get ¶
func (cmap *ShardedConcurrentTxMap) Get(key interface{}) (interface{}, bool)
Get returns the value corresponding to the key, if it exist
func (*ShardedConcurrentTxMap) GetAndDo ¶
func (cmap *ShardedConcurrentTxMap) GetAndDo(key interface{}, fn ActionFunc) (interface{}, bool, error)
GetAndDo returns the value corresponding to the key, and apply fn to key value before return value return (value, value exist or not, error when evaluation fn)
func (*ShardedConcurrentTxMap) Iter ¶
func (cmap *ShardedConcurrentTxMap) Iter() MapIterator
Iter returns an iterator to the map. This map does not use re-entrant locks, so access or modification to the map during iteration can cause a dead lock.
func (*ShardedConcurrentTxMap) Len ¶ added in v0.5.8
func (cmap *ShardedConcurrentTxMap) Len() int
Len returns the number of items in the map
func (*ShardedConcurrentTxMap) Put ¶
func (cmap *ShardedConcurrentTxMap) Put(key interface{}, value interface{})
Put records the given key value mapping. Overwrites previous values
func (*ShardedConcurrentTxMap) PutIfNotExist ¶
func (cmap *ShardedConcurrentTxMap) PutIfNotExist(key interface{}, value interface{}) bool
PutIfNotExist records the mapping, if there is no mapping for this key already Returns true if the mapping was recorded, false otherwise
func (*ShardedConcurrentTxMap) PutOrDo ¶
func (cmap *ShardedConcurrentTxMap) PutOrDo(key interface{}, value interface{}, fn ActionFunc) (interface{}, bool, error)
PutOrDo put the key value in the map, if key does not exists, otherwise, call fn with existing key and value return (value, fn evaluated or not, error when evaluation fn)
func (*ShardedConcurrentTxMap) Remove ¶
func (cmap *ShardedConcurrentTxMap) Remove(key interface{})
Remove deletes the given key from the map
func (*ShardedConcurrentTxMap) RemoveIf ¶
func (cmap *ShardedConcurrentTxMap) RemoveIf(key interface{}, fn PredicateFunc) bool
RemoveIf deletes the given key from the map if fn return true
type SortedSetManager ¶ added in v1.24.0
type SortedSetManager[S ~[]E, E, K any] struct { // contains filtered or unexported fields }
SortedSetManager provides CRUD functionality for in-memory sorted sets. Note that there's no Update method because you can just use the SortedSetManager.Get method and update that index directly.
func NewSortedSetManager ¶ added in v1.24.0
func NewSortedSetManager[S ~[]E, E, K any](cmp func(E, K) int, key func(E) K) SortedSetManager[S, E, K]
NewSortedSetManager returns a new SortedSetManager with the given comparison function and key function.
func (SortedSetManager[S, E, K]) Add ¶ added in v1.24.0
func (m SortedSetManager[S, E, K]) Add(set S, e E) (S, bool)
Add adds a new element to the set. If the element is already in the set, it returns the set unchanged and false.
func (SortedSetManager[S, E, K]) Get ¶ added in v1.24.0
func (m SortedSetManager[S, E, K]) Get(set S, key K) int
Get returns the index of the element in the set that compares equal to key or -1 if no such element exists.
func (SortedSetManager[S, E, K]) Paginate ¶ added in v1.24.0
func (m SortedSetManager[S, E, K]) Paginate(set S, gtKey K, n int) (S, *K)
Paginate returns up to n elements in the set that compare greater than gtKey. If there are more than n such elements, it also returns the key of the last element in the page. Otherwise, the second return value is nil.
func (SortedSetManager[S, E, K]) Remove ¶ added in v1.24.0
func (m SortedSetManager[S, E, K]) Remove(set S, key K) (S, bool)
Remove removes an element from the set. If the element is not in the set, it returns the set unchanged and false.
type SyncMap ¶ added in v1.24.0
type SyncMap[K comparable, V any] struct { *sync.RWMutex // contains filtered or unexported fields }
SyncMap implements a simple mutex-wrapped map. We've had bugs where we took the wrong lock when reimplementing this pattern, so it's worth having a single canonical implementation.
func NewSyncMap ¶ added in v1.24.0
func NewSyncMap[K comparable, V any]() SyncMap[K, V]