shard

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Apr 6, 2023 License: MIT Imports: 4 Imported by: 1

README

shard

Test

make

Benchmark

cd bench
make

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Map

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

Map is a hashmap. Like map[string]any, but sharded and thread-safe.

func New

func New[V any]() (m *Map[V])

New returns a new hashmap with the specified capacity.

func NewWithCapcity

func NewWithCapcity[V any](capcity int) (m *Map[V])

New returns a new hashmap with the specified capacity.

func (*Map[V]) Clear

func (m *Map[V]) Clear()

Clear out all values from map

func (*Map[V]) Delete

func (m *Map[V]) Delete(key string) (prev V, deleted bool)

Delete deletes a value for a key. Returns the deleted value, or false when no value was assigned.

func (*Map[V]) DeleteAccept

func (m *Map[V]) DeleteAccept(
	key string,
	accept func(prev V, replaced bool) bool,
) (prev V, deleted bool)

DeleteAccept deletes a value for a key. The "accept" function can be used to inspect the previous value, if any, and accept or reject the change. It's also provides a safe way to block other others from writing to the same shard while inspecting. Returns the deleted value, or false when no value was assigned.

func (*Map[V]) Get

func (m *Map[V]) Get(key string) (value V, ok bool)

Get returns a value for a key. Returns false when no value has been assign for key.

func (*Map[V]) Has

func (m *Map[V]) Has(key string) (ok bool)

Looks up an item under specified key

func (*Map[V]) Len

func (m *Map[V]) Len() (length int)

Len returns the number of values in map.

func (*Map[V]) Range

func (m *Map[V]) Range(callback func(key string, value V) bool)

Range calls the provided callback function for each key-value pair in the map until the callback returns false or all pairs have been processed.

func (*Map[V]) Set

func (m *Map[V]) Set(key string, value V) (prev any, replaced bool)

Set assigns a value to a key. Returns the previous value, or false when no value was assigned.

func (*Map[V]) SetAccept

func (m *Map[V]) SetAccept(
	key string, value V,
	accept func(prev V, replaced bool) bool,
) (prev V, replaced bool)

SetAccept assigns a value to a key. The "accept" function can be used to inspect the previous value, if any, and accept or reject the change. It's also provides a safe way to block other others from writing to the same shard while inspecting. Returns the previous value, or false when no value was assigned.

Jump to

Keyboard shortcuts

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