Documentation
¶
Overview ¶
Package cmap provides thread-safe map.
Index ¶
- type IterCb
- type Map
- func (m Map[K, V]) Clear()
- func (m Map[K, V]) Count() int
- func (m Map[K, V]) Get(key K) (V, bool)
- func (m Map[K, V]) Has(key K) bool
- func (m Map[K, V]) IsEmpty() bool
- func (m Map[K, V]) Items() map[K]V
- func (m Map[K, V]) Iter(fn IterCb[K, V])
- func (m Map[K, V]) Keys() []K
- func (m Map[K, V]) MarshalJSON() ([]byte, error)
- func (m Map[K, V]) Pop(key K) (value V, exists bool)
- func (m Map[K, V]) Remove(key K)
- func (m Map[K, V]) RemoveCb(key K, cb RemoveCb[K, V]) bool
- func (m Map[K, V]) RemoveFunc(fn RemoveFunc[K, V])
- func (m Map[K, V]) Seq() iter.Seq2[K, V]
- func (m Map[K, V]) Set(key K, value V)
- func (m Map[K, V]) SetIfAbsent(key K, value V) bool
- func (m *Map[K, V]) UnmarshalJSON(b []byte) (err error)
- func (m Map[K, V]) Update(key K, cb UpdateCb[V]) (res V, updated bool)
- func (m Map[K, V]) Upsert(key K, cb UpsertCb[V]) (res V)
- type Option
- type RemoveCb
- type RemoveFunc
- type ShardingFunc
- type UpdateCb
- type UpsertCb
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type IterCb ¶
type IterCb[K comparable, V any] func(key K, value 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 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 dived to several map shards.
func New ¶
func New[K comparable, V any](opts ...Option) Map[K, V]
New creates a new concurrent map.
func (Map[K, V]) Iter ¶
Iter is a callback based iterator, cheapest way to read all elements in a map.
func (Map[K, V]) MarshalJSON ¶
MarshalJSON encodes the map into a json object.
func (Map[K, V]) RemoveCb ¶
RemoveCb removes an element from the map using cb. Returns the value returned by cb.
func (Map[K, V]) RemoveFunc ¶
func (m Map[K, V]) RemoveFunc(fn RemoveFunc[K, V])
RemoveFunc removes any element from the map for which fn returns true.
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.
func (*Map[K, V]) UnmarshalJSON ¶
UnmarshalJSON decodes a json object into the map.
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 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 ¶
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.
type ShardingFunc ¶
type ShardingFunc[K comparable] func(key K) uint64
ShardingFunc is a function for sharding a map.