Documentation
¶
Index ¶
- type ConcurrentMap
- func (m ConcurrentMap[K, V]) Clear()
- func (m ConcurrentMap[K, V]) Count() int
- func (m ConcurrentMap[K, V]) Get(key K) (V, bool)
- func (m ConcurrentMap[K, V]) Has(key K) bool
- func (m ConcurrentMap[K, V]) IsEmpty() bool
- func (m ConcurrentMap[K, V]) Items() map[K]V
- func (m ConcurrentMap[K, V]) Iter(fn IterCb[K, V])
- func (m ConcurrentMap[K, V]) Keys() []K
- func (m ConcurrentMap[K, V]) MarshalJSON() ([]byte, error)
- func (m ConcurrentMap[K, V]) Pop(key K) (v V, exists bool)
- func (m ConcurrentMap[K, V]) Remove(key K)
- func (m ConcurrentMap[K, V]) RemoveCb(key K, cb RemoveCb[K, V]) bool
- func (m ConcurrentMap[K, V]) RemoveFunc(fn RemoveFunc[K, V])
- func (m ConcurrentMap[K, V]) Seq() iter.Seq2[K, V]
- func (m ConcurrentMap[K, V]) Set(key K, value V)
- func (m ConcurrentMap[K, V]) SetIfAbsent(key K, value V) bool
- func (m *ConcurrentMap[K, V]) UnmarshalJSON(b []byte) (err error)
- func (m ConcurrentMap[K, V]) Update(key K, value V, cb UpdateCb[V]) (res V, updated bool)
- func (m ConcurrentMap[K, V]) Upsert(key K, value V, cb UpsertCb[V]) (res V)
- type IterCb
- type Option
- type RemoveCb
- type RemoveFunc
- type UpdateCb
- type UpsertCb
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ConcurrentMap ¶
type ConcurrentMap[K comparable, V any] struct { // contains filtered or unexported fields }
ConcurrentMap is a thread-safe map. To avoid lock bottlenecks this map is dived to several map shards.
func New ¶
func New[V any](opts ...Option[string]) ConcurrentMap[string, V]
New creates a new concurrent map.
func (ConcurrentMap[K, V]) Clear ¶
func (m ConcurrentMap[K, V]) Clear()
Clear removes all items from the map.
func (ConcurrentMap[K, V]) Count ¶
func (m ConcurrentMap[K, V]) Count() int
Count returns the number of elements within the map.
func (ConcurrentMap[K, V]) Get ¶
func (m ConcurrentMap[K, V]) Get(key K) (V, bool)
Get retrieves an element from the map under the specified key.
func (ConcurrentMap[K, V]) Has ¶
func (m ConcurrentMap[K, V]) Has(key K) bool
Has checks if an item under the specified key exists.
func (ConcurrentMap[K, V]) IsEmpty ¶
func (m ConcurrentMap[K, V]) IsEmpty() bool
IsEmpty checks if the map is empty.
func (ConcurrentMap[K, V]) Items ¶
func (m ConcurrentMap[K, V]) Items() map[K]V
Items returns all items in the map.
func (ConcurrentMap[K, V]) Iter ¶
func (m ConcurrentMap[K, V]) Iter(fn IterCb[K, V])
Iter is a callback based iterator, cheapest way to read all elements in a map.
func (ConcurrentMap[K, V]) Keys ¶
func (m ConcurrentMap[K, V]) Keys() []K
Keys returns all keys in the map.
func (ConcurrentMap[K, V]) MarshalJSON ¶
func (m ConcurrentMap[K, V]) MarshalJSON() ([]byte, error)
MarshalJSON encodes the map into a json object.
func (ConcurrentMap[K, V]) Pop ¶
func (m ConcurrentMap[K, V]) Pop(key K) (v V, exists bool)
Pop removes an element from the map and returns it.
func (ConcurrentMap[K, V]) Remove ¶
func (m ConcurrentMap[K, V]) Remove(key K)
Remove removes an element from the map.
func (ConcurrentMap[K, V]) RemoveCb ¶
func (m ConcurrentMap[K, V]) RemoveCb(key K, cb RemoveCb[K, V]) bool
RemoveCb removes an element from the map using cb. Returns the value returned by cb.
func (ConcurrentMap[K, V]) RemoveFunc ¶ added in v1.2.0
func (m ConcurrentMap[K, V]) RemoveFunc(fn RemoveFunc[K, V])
RemoveFunc removes any element from the map for which fn returns true.
func (ConcurrentMap[K, V]) Seq ¶
func (m ConcurrentMap[K, V]) Seq() iter.Seq2[K, V]
Seq is handy go1.23 iterator based on Iter() method.
func (ConcurrentMap[K, V]) Set ¶
func (m ConcurrentMap[K, V]) Set(key K, value V)
Set sets the given value under the specified key.
func (ConcurrentMap[K, V]) SetIfAbsent ¶
func (m ConcurrentMap[K, V]) SetIfAbsent(key K, value V) bool
SetIfAbsent sets the given value under the specified key if no value was associated with it.
func (*ConcurrentMap[K, V]) UnmarshalJSON ¶
func (m *ConcurrentMap[K, V]) UnmarshalJSON(b []byte) (err error)
UnmarshalJSON decodes a json object into the map.
func (ConcurrentMap[K, V]) Update ¶ added in v1.1.0
func (m ConcurrentMap[K, V]) Update(key K, value V, cb UpdateCb[V]) (res V, updated bool)
Update updates an existing element using UpdateCb. If the element doesn't exist, returns false. Otherwise returns the updated element and true.
func (ConcurrentMap[K, V]) Upsert ¶
func (m ConcurrentMap[K, V]) Upsert(key K, value V, cb UpsertCb[V]) (res V)
Upsert updates an existing element or inserts a new one using UpsertCb. Returns the updated/inserted element.
type IterCb ¶
type IterCb[K comparable, V any] func(key K, v V) bool
IterCb is an iterator callback called for every element in the map. RLock is held for all calls for a given shard therefore callback sees consistent view of a shard, but not across the shards.
type Option ¶
type Option[K comparable] func(*options[K])
Option is used to configure concurrent map.
func WithShardCount ¶
func WithShardCount[K comparable](n int) Option[K]
WithShardCount allows to set the number of shards in a map.
type RemoveCb ¶
RemoveCb is a callback to remove an element from the map. It is called while lock is held. If it returns true, the element will be removed from the map.
type RemoveFunc ¶ added in v1.2.0
RemoveFunc is a callback to remove elements in the map. Lock is held for all calls for a given shard therefore callback sees consistent view of a shard, but not across the shards. If it returns true, the element will be removed from the map.