import "go.openfort.xyz/shardedmap"
A Go package that implements a thread-safe, sharded map with configurable concurrency for optimized key-value storage operations.
- Variables
- type MapShard
- type Option
- type ShardedMap
- func NewShardedMap[K comparable, V any](opts ...Option) *ShardedMap[K, V]
- func (m *ShardedMap[K, V]) Clear()
- func (m *ShardedMap[K, V]) Count() int32
- func (m *ShardedMap[K, V]) Delete(key K)
- func (m *ShardedMap[K, V]) Get(key K) (V, bool)
- func (m *ShardedMap[K, V]) Keys() []K
- func (m *ShardedMap[K, V]) Set(key K, value V)
ShardCount is the default number of shards used by the ShardedMap. This can be adjusted based on your concurrency requirements using the WithShardCount option.
var ShardCount = 32
MapShard represents a single shard in the ShardedMap. It contains a map and a read-write mutex to ensure thread-safe operations.
type MapShard[K comparable, V any] struct {
// contains filtered or unexported fields
}
Option is a functional option for configuring a sharded map.
type Option func(*options)
func WithShardCount(shardCount int) Option
WithShardCount sets the number of shards in the sharded map.
ShardedMap is a thread-safe map where keys are distributed across multiple shards.
type ShardedMap[K comparable, V any] struct {
// contains filtered or unexported fields
}
func NewShardedMap[K comparable, V any](opts ...Option) *ShardedMap[K, V]
NewShardedMap initializes a ShardedMap with the specified options or defaults.
func (m *ShardedMap[K, V]) Clear()
Clear removes all key-value pairs from the map.
func (m *ShardedMap[K, V]) Count() int32
Count returns the number of elements in the map across all shards.
func (m *ShardedMap[K, V]) Delete(key K)
Delete removes a key from the map.
func (m *ShardedMap[K, V]) Get(key K) (V, bool)
Get retrieves a value from the map by key. The second return value indicates whether the key was found.
func (m *ShardedMap[K, V]) Keys() []K
Keys returns a slice containing all the keys in the map. This method is thread-safe, but may be relatively slow depending on the map size.
func (m *ShardedMap[K, V]) Set(key K, value V)
Set sets a key-value pair in the map. It overwrites any existing value for the key.