Documentation ¶
Index ¶
- func HeuristicOptimalDistribution(expectedItemsCount int) (int, int)
- func HeuristicOptimalShardsCount() int
- type Generic
- func (sm Generic[K, V]) Delete(key K)
- func (sm Generic[K, V]) DeleteWhere(predicate func(K, V) bool)
- func (sm Generic[K, V]) DeleteWhereKeys(predicate func(K) bool)
- func (sm Generic[K, V]) Len() int
- func (sm Generic[K, V]) Load(key K) (V, bool)
- func (sm Generic[K, V]) LoadAndDelete(key K) (V, bool)
- func (sm Generic[K, V]) LoadOrCreate(key K, generator func() V) (V, bool)
- func (sm Generic[K, V]) LockShard(id int)
- func (sm Generic[K, V]) RLockShard(id int)
- func (sm Generic[K, V]) RUnlockShard(id int)
- func (sm Generic[K, V]) Range(cb func(K, V) bool)
- func (sm Generic[K, V]) RangeKeys(cb func(K) bool)
- func (sm Generic[K, V]) ShardID(key K) int
- func (sm Generic[K, V]) ShardsCount() int
- func (sm Generic[K, V]) Store(key K, value V)
- func (sm Generic[K, V]) UnblockedGet(key K) (V, bool)
- func (sm Generic[K, V]) UnblockedSet(key K, value V)
- func (sm Generic[K, V]) UnblockedShardRange(shardID int, cb func(key K, value V) bool)
- func (sm Generic[K, V]) UnlockShard(id int)
- type GenericComparable
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func HeuristicOptimalDistribution ¶
HeuristicOptimalDistribution returns shards count, and shard size for given map size. Use with caution, It's point for change, and could be changed in the future.
func HeuristicOptimalShardsCount ¶
func HeuristicOptimalShardsCount() int
HeuristicOptimalShardsCount returns shards count. Use with caution, It's point for change, and could be changed in the future.
Types ¶
type Generic ¶
type Generic[K comparable, V any] struct { // contains filtered or unexported fields }
Generic stores data in N shards, with rw mutex for each.
func NewGeneric ¶
func NewGeneric[K comparable, V any](shardsCount, defaultSize int, shardDetector func(key K) int) Generic[K, V]
NewGeneric creates generic RWLocked Sharded map. shardDetector should be idempotent function.
func NewInteger ¶
func NewInteger[K constraints.Integer, V any](shardsCount, defaultSize int) Generic[K, V]
NewInteger creates sharded rwlock maps with shard detection based on key division to shards count modulo.
func (Generic[K, V]) Delete ¶
func (sm Generic[K, V]) Delete(key K)
Delete deletes the value for a key.
func (Generic[K, V]) DeleteWhere ¶ added in v1.1.1
DeleteWhere deletes values according to predicate.
func (Generic[K, V]) DeleteWhereKeys ¶ added in v1.1.1
DeleteWhereKeys deletes values according to predicate.
func (Generic[K, V]) Load ¶
Load returns the value stored in the map for a key, or nil if no value is present. The ok result indicates whether value was found in the map.
func (Generic[K, V]) LoadAndDelete ¶
LoadAndDelete deletes the value for a key, returning the previous value if any. The loaded result reports whether the key was present.
func (Generic[K, V]) LoadOrCreate ¶
LoadOrCreate returns the existing value for the key if present. Otherwise, it calls generator func, stores and returns the generator's result. Generator will not be called if key present. The loaded result is true if the value was loaded, false if stored.
func (Generic[K, V]) LockShard ¶
LockShard locks shard with given id. Could be useful with Unblocked* functions. Other calls to sm could be locked. Use with caution, only when benchmark shows significant performance changes.
func (Generic[K, V]) RLockShard ¶
RLockShard locks for read shard with given id. Could be useful with Unblocked* functions. Other calls to sm could be rlocked. Use with caution, only when benchmark shows significant performance changes.
func (Generic[K, V]) RUnlockShard ¶
RUnlockShard unlocks for read shard with given id.
func (Generic[K, V]) Range ¶
Range calls cb sequentially for each key and value present in the map. If cb returns false, range stops the iteration.
Range performs like sync.Range: does not correspond to any consistent snapshot of the Map's contents: no key will be visited more than once, but if the value for any key is stored or deleted concurrently (including by cb), Range may reflect any mapping for that key from any point during the Range call. Range does not block other methods on the receiver; even cb itself may call any method on sm.
Range may be O(N) with the number of elements in the map even if f returns false after a constant number of calls.
func (Generic[K, V]) RangeKeys ¶ added in v1.1.2
RangeKeys works similar like Range, but iterates by keys only.
func (Generic[K, V]) ShardsCount ¶
ShardsCount returns shards count, given on initialisation.
func (Generic[K, V]) Store ¶
func (sm Generic[K, V]) Store(key K, value V)
Store sets the value for a key.
func (Generic[K, V]) UnblockedGet ¶
UnblockedGet returns value, without locks. Use with caution, only when lock or rlock were taken for shard.
func (Generic[K, V]) UnblockedSet ¶
func (sm Generic[K, V]) UnblockedSet(key K, value V)
UnblockedSet sets value, without locks. Use with caution, only when lock were taken for shard.
func (Generic[K, V]) UnblockedShardRange ¶
UnblockedShardRange calls cb sequentially for each key and value present in the maps shard. Use with caution, only when lock or rlock were taken for shard.
func (Generic[K, V]) UnlockShard ¶
UnlockShard unlocks shard with given id.
type GenericComparable ¶
type GenericComparable[K comparable, V comparable] struct { Generic[K, V] }
GenericComparable stores data in N shards, with rw mutex for each. Additional CompareAndSwap method added for comparable values.
func NewGenericComparable ¶
func NewGenericComparable[K comparable, V comparable](shardsCount, defaultSize int, shardDetector func(key K) int) GenericComparable[K, V]
NewGenericComparable creates generic RWLocked Sharded map for comparable values. shardDetector should be idempotent function.
func NewIntegerComparable ¶
func NewIntegerComparable[K constraints.Integer, V comparable](shardsCount, defaultSize int) GenericComparable[K, V]
NewIntegerComparable creates sharded rwlock maps with comparable values.
func (GenericComparable[K, V]) CompareAndSwap ¶
func (sm GenericComparable[K, V]) CompareAndSwap(key K, old, new V) (V, bool)
CompareAndSwap executes the compare-and-swap operation for the Key & Value pair. If and only if key exists, and value for key equals old, value will be changed to new. Otherwise, returns current value. The ok result indicates whether value was changed to new in the map.