sharded

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Jul 31, 2025 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

View Source
const ActiveShards uint64 = 2047 // 2047 active shards
View Source
const NumOfShards uint64 = 2049 // 2048 total shards (one for collisions)

Variables

This section is empty.

Functions

func MapShardKey

func MapShardKey(key uint64) uint64

MapShardKey calculates the shard index for a given key.

Types

type Map

type Map[V Value] struct {
	// contains filtered or unexported fields
}

Map is a sharded concurrent map for high-performance caches.

func NewMap

func NewMap[V Value](ctx context.Context, defaultLen int) *Map[V]

NewMap creates a new sharded map with preallocated shards and a default per-shard map capacity.

func (*Map[V]) Get

func (smap *Map[V]) Get(key uint64) (value V, ok bool)

Get fetches a value and its releaser from the correct shard. found==false means the value is absent.

func (*Map[V]) Len

func (smap *Map[V]) Len() int64

Len returns the total number of elements in all shards (O(NumOfShards)).

func (*Map[V]) Mem

func (smap *Map[V]) Mem() int64

func (*Map[V]) RealLen added in v1.0.1

func (smap *Map[V]) RealLen() int64

func (*Map[V]) RealMem

func (smap *Map[V]) RealMem() int64

func (*Map[V]) Remove

func (smap *Map[V]) Remove(key uint64) (freedBytes int64, hit bool)

Remove deletes a value by key, returning how much memory was freed and a pointer to its LRU/list element.

func (*Map[V]) Rnd added in v0.9.3

func (smap *Map[V]) Rnd() (value V, ok bool)

func (*Map[V]) Set

func (smap *Map[V]) Set(key uint64, value V)

Set inserts or updates a value in the correct shard. Returns a releaser for ref counting.

func (*Map[V]) Shard

func (smap *Map[V]) Shard(key uint64) *Shard[V]

Shard returns the shard that stores the given key.

func (*Map[V]) WalkShards

func (smap *Map[V]) WalkShards(ctx context.Context, fn func(key uint64, shard *Shard[V]))

WalkShards launches fn concurrently for each shard (key, *Shard[V]). The callback runs in a separate goroutine for each shard; fn should be goroutine-safe.

type Shard

type Shard[V Value] struct {
	*sync.RWMutex // Shard-level RWMutex for concurrency
	// contains filtered or unexported fields
}

Shard is a single partition of the sharded map. Each shard is an independent concurrent map with its own lock and refCounted pool for releasers.

func NewShard

func NewShard[V Value](id uint64, defaultLen int) *Shard[V]

NewShard creates a new shard with its own lock, value map, and releaser pool.

func (*Shard[V]) Clear added in v0.9.7

func (shard *Shard[V]) Clear()

func (*Shard[V]) Get

func (shard *Shard[V]) Get(key uint64) (val V, ok bool)

Get retrieves a value and returns a releaser for it, incrementing its refCount. Returns (value, releaser, true) if found; otherwise (zero, nil, false).

func (*Shard[V]) GetRand added in v0.9.5

func (shard *Shard[V]) GetRand() (val V, ok bool)

func (*Shard[V]) ID

func (shard *Shard[V]) ID() uint64

ID returns the numeric index of this shard.

func (*Shard[V]) Len

func (shard *Shard[V]) Len() int64

func (*Shard[V]) Remove

func (shard *Shard[V]) Remove(key uint64) (freedBytes int64, hit bool)

Remove removes a value from the shard, decrements counters, and may trigger full resource cleanup. Returns (memory_freed, pointer_to_list_element, was_found).

func (*Shard[V]) Set

func (shard *Shard[V]) Set(key uint64, new V)

Set inserts or updates a value by key, resets refCount, and updates counters. Returns a releaser for the inserted value.

func (*Shard[V]) Walk

func (shard *Shard[V]) Walk(ctx context.Context, fn func(uint64, V) bool, lockRead bool)

Walk applies fn to all key/value pairs in the shard, optionally locking for writing.

func (*Shard[V]) Weight

func (shard *Shard[V]) Weight() int64

Weight returns an approximate total memory usage for this shard (including overhead).

type Value

type Value interface {
	types.Keyed
	types.Sized
}

Value must implement all cache entry interfaces: keying, sizing, and releasability.

Jump to

Keyboard shortcuts

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