Skip to content

A Go package that implements a thread-safe, sharded map with configurable concurrency for optimized key-value storage operations.

License

Notifications You must be signed in to change notification settings

openfort-xyz/shardedmap

Repository files navigation

shardedmap

Coverage Go Report Card

import "go.openfort.xyz/shardedmap"

A Go package that implements a thread-safe, sharded map with configurable concurrency for optimized key-value storage operations.

Index

Variables

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

type MapShard

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
}

type Option

Option is a functional option for configuring a sharded map.

type Option func(*options)

func WithShardCount

func WithShardCount(shardCount int) Option

WithShardCount sets the number of shards in the sharded map.

type ShardedMap

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

func NewShardedMap[K comparable, V any](opts ...Option) *ShardedMap[K, V]

NewShardedMap initializes a ShardedMap with the specified options or defaults.

func (*ShardedMap[K, V]) Clear

func (m *ShardedMap[K, V]) Clear()

Clear removes all key-value pairs from the map.

func (*ShardedMap[K, V]) Count

func (m *ShardedMap[K, V]) Count() int32

Count returns the number of elements in the map across all shards.

func (*ShardedMap[K, V]) Delete

func (m *ShardedMap[K, V]) Delete(key K)

Delete removes a key from the map.

func (*ShardedMap[K, V]) Get

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 (*ShardedMap[K, V]) Keys

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 (*ShardedMap[K, V]) Set

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.

About

A Go package that implements a thread-safe, sharded map with configurable concurrency for optimized key-value storage operations.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages