collection

package
v1.23.1 Latest Latest
Warning

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

Go to latest
Published: Apr 30, 2024 License: MIT Imports: 5 Imported by: 4

Documentation

Index

Constants

View Source
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 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 PaginationFn added in v0.5.8

type PaginationFn[V any] func(paginationToken []byte) ([]V, []byte, error)

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

func (*PagingIteratorImpl[V]) HasNext added in v0.5.8

func (iter *PagingIteratorImpl[V]) HasNext() bool

HasNext return whether has next item or err

func (*PagingIteratorImpl[V]) Next added in v0.5.8

func (iter *PagingIteratorImpl[V]) Next() (V, error)

Next return next item or err

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

func NewPriorityQueue[T any](
	compareLess func(this T, other T) bool,
) Queue[T]

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

Jump to

Keyboard shortcuts

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