cmap

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 24, 2025 License: MIT Imports: 8 Imported by: 0

README

cmap - Thread-safe map library

Go Reference

Hard fork of concurrent-map with a lot of breaking changes.

This library provides a thread-safe map that is split into individual shards, each of which has a separate RWMutex, which reduces the time spent waiting for locks.

Documentation

Overview

Package cmap provides thread-safe map.

Index

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]) Clear

func (m Map[K, V]) Clear()

Clear removes all items from the map.

func (Map[K, V]) Count

func (m Map[K, V]) Count() int

Count returns the number of elements within the map.

func (Map[K, V]) Get

func (m Map[K, V]) Get(key K) (V, bool)

Get retrieves an element from the map under the specified key.

func (Map[K, V]) Has

func (m Map[K, V]) Has(key K) bool

Has checks if an item under the specified key exists.

func (Map[K, V]) IsEmpty

func (m Map[K, V]) IsEmpty() bool

IsEmpty checks if the map is empty.

func (Map[K, V]) Items

func (m Map[K, V]) Items() map[K]V

Items returns all items in the map.

func (Map[K, V]) Iter

func (m Map[K, V]) Iter(fn IterCb[K, V])

Iter is a callback based iterator, cheapest way to read all elements in a map.

func (Map[K, V]) Keys

func (m Map[K, V]) Keys() []K

Keys returns all keys in the map.

func (Map[K, V]) MarshalJSON

func (m Map[K, V]) MarshalJSON() ([]byte, error)

MarshalJSON encodes the map into a json object.

func (Map[K, V]) Pop

func (m Map[K, V]) Pop(key K) (value V, exists bool)

Pop removes an element from the map and returns it.

func (Map[K, V]) Remove

func (m Map[K, V]) Remove(key K)

Remove removes an element from the map.

func (Map[K, V]) RemoveCb

func (m Map[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 (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]) Seq

func (m Map[K, V]) Seq() iter.Seq2[K, V]

Seq is handy go1.23 iterator based on Iter() method.

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

func (m Map[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 (*Map[K, V]) UnmarshalJSON

func (m *Map[K, V]) UnmarshalJSON(b []byte) (err error)

UnmarshalJSON decodes a json object into the map.

func (Map[K, V]) Update

func (m Map[K, V]) Update(key K, 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 (Map[K, V]) Upsert

func (m Map[K, V]) Upsert(key K, cb UpsertCb[V]) (res V)

Upsert updates an existing element or inserts a new one using UpsertCb. Returns the updated/inserted element.

type Option

type Option func(*options)

Option is used to configure concurrent map.

func WithShardCount

func WithShardCount(n int) Option

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

type RemoveCb[K any, V any] func(value V, exists bool) bool

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

type RemoveFunc[K any, V any] func(key K, value V) bool

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.

type UpdateCb

type UpdateCb[V any] func(value V) V

UpdateCb is a callback to update an element in the map. It is called while lock is held, therefore it MUST NOT try to access other keys in same map, as it can lead to deadlock.

type UpsertCb

type UpsertCb[V any] func(value V, exists bool) V

UpsertCb is a callback to return a new element to be inserted into the map. It is called while lock is held, therefore it MUST NOT try to access other keys in the same map, as it can lead to deadlock.

Jump to

Keyboard shortcuts

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