Documentation
¶
Overview ¶
Package cmap provides thread-safe map.
Index ¶
- type ComputeOp
- type Item
- type Map
- func (m *Map[K, V]) Clear()
- func (m *Map[K, V]) Compute(key K, fn func(oldValue V, exists bool) (newValue V, op ComputeOp)) (actual V, ok bool)
- func (m *Map[K, V]) Get(key K) (V, bool)
- func (m *Map[K, V]) IsEmpty() bool
- func (m *Map[K, V]) Items() map[K]V
- func (m *Map[K, V]) Iter(fn func(*Item[K, V]) bool)
- func (m *Map[K, V]) IterMut(fn func(*Item[K, V]) bool)
- func (m *Map[K, V]) Keys() []K
- func (m *Map[K, V]) Len() int
- func (m *Map[K, V]) Remove(key K) (V, bool)
- func (m *Map[K, V]) Seq() iter.Seq[*Item[K, V]]
- func (m *Map[K, V]) SeqMut() iter.Seq[*Item[K, V]]
- func (m *Map[K, V]) Set(key K, value V)
- func (m *Map[K, V]) SetIfAbsent(key K, value V) (V, bool)
- type Option
- type ShardingFunc
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ComputeOp ¶ added in v0.3.0
type ComputeOp int
ComputeOp is an operation to be applied to the value stored under a key by Compute.
type Item ¶ added in v0.3.0
type Item[K comparable, V any] struct { // contains filtered or unexported fields }
Item is a key-value pair stored in a Map, passed to the callback of Iter, IterMut, Seq and SeqMut.
An Item is only valid inside the iteration callback it was passed to: the underlying entry may be changed or removed by other goroutines after the callback returns.
func (*Item[K, V]) Delete ¶ added in v0.3.0
func (it *Item[K, V]) Delete()
Delete removes the item from the map.
It only takes effect when the item was obtained from a mutable iteration (IterMut or SeqMut); otherwise it is a no-op.
type Map ¶
type Map[K comparable, V any] struct { // contains filtered or unexported fields }
Map is a thread-safe map. To avoid lock bottlenecks this map is divided into several map shards, each guarded by its own mutex.
func New ¶
func New[K comparable, V any](opts ...Option) *Map[K, V]
New creates a new concurrent map.
func (*Map[K, V]) Compute ¶ added in v0.3.0
func (m *Map[K, V]) Compute(key K, fn func(oldValue V, exists bool) (newValue V, op ComputeOp)) (actual V, ok bool)
Compute atomically updates the value stored under the given key. fn is called with the current value and whether the key is present in the map, and must return the new value together with the operation to apply:
- KeepOp leaves the stored value (or the key absence) unchanged;
- SetOp stores newValue under the key;
- RemoveOp deletes the key from the map.
Compute returns the resulting value for the key and whether the key is present after the operation.
fn is invoked while the shard containing the key is locked; it must not call methods of the same map, as that may deadlock.
func (*Map[K, V]) Iter ¶
Iter iterates over all items in the map in an unspecified order, calling fn for each item until fn returns false or every item has been visited.
Iteration holds a read lock on each shard, so the map must not be modified from within fn (Item.Delete is a no-op here); use IterMut instead.
func (*Map[K, V]) IterMut ¶ added in v0.3.0
IterMut is like Iter, but holds a write lock on each shard, so items can be removed from the map with Item.Delete.
As with Iter, the map must not be modified from within fn by any other means.
func (*Map[K, V]) Seq ¶
Seq returns an iterator over the items of the map, suitable for use with range. It is equivalent to Iter.
func (*Map[K, V]) SeqMut ¶ added in v0.3.0
SeqMut returns a mutable iterator over the items of the map, suitable for use with range. It is equivalent to IterMut and allows items to be removed with Item.Delete.
func (*Map[K, V]) Set ¶
func (m *Map[K, V]) Set(key K, value V)
Set sets the given value under the specified key.
func (*Map[K, V]) SetIfAbsent ¶
SetIfAbsent sets the given value under the specified key if no value was associated with it.
type Option ¶
type Option func(*options)
Option is used to configure concurrent map.
func WithShardCount ¶
WithShardCount allows to set the number of shards in a map.
func WithShardingFunc ¶
func WithShardingFunc[K comparable](fn ShardingFunc[K]) Option
WithShardingFunc allows to set the sharding function of a map.
type ShardingFunc ¶
type ShardingFunc[K comparable] func(key K) uint64
ShardingFunc is a function for sharding a map.