cmap

package
v1.1.3 Latest Latest
Warning

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

Go to latest
Published: Jan 23, 2022 License: Apache-2.0 Imports: 3 Imported by: 0

README

Concurrent Map (CMAP)

This packages attempts to provide a simple to use concurrent map with pluggable hashing.

The default settings should be sufficient for most usage.

For usage examples please refer here

Documentation

Overview

Package cmap please refer to README.md

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// ErrNoSuchItem indicated no such item exists in the key
	ErrNoSuchItem = errors.New("no such item")
)

Functions

This section is empty.

Types

type Map

type Map struct {
	// contains filtered or unexported fields
}

Map is a concurrent map

Example (DefaultSharding)
package main

import (
	"fmt"

	"github.com/corsc/go-commons/concurrency/cmap"
)

func main() {
	myMap := cmap.New()

	val, err := myMap.Get("key")
	fmt.Printf("Value/Err: %v/%v\n", val, err)

	err = myMap.Set("key", "foo")
	fmt.Printf("Err: %v\n", err)

	val, err = myMap.Get("key")
	fmt.Printf("Value/Err: %v/%v\n", val, err)

}
Output:

Value/Err: <nil>/no such item
Err: <nil>
Value/Err: foo/<nil>

func New

func New(manager ...ShardManager) *Map

New returns a initialized concurrent map

func (*Map) Count

func (c *Map) Count() int64

Count will return the total number of items in the map

func (*Map) Get

func (c *Map) Get(key string) (interface{}, error)

Get will attempt to return the requested key or an error. `ErrNoSuchItem` indicates the item does not exist

func (*Map) GetElseSet

func (c *Map) GetElseSet(key string, newValue interface{}) (interface{}, error)

GetElseSet will return the existing value in the map or will set the value using `newValue`. Regardless, this method will return the map item value or an error.

func (*Map) Has

func (c *Map) Has(key string) bool

Has will return true if the key exists in the map or false

Note: this method will silently fail on errors

func (*Map) Iterator

func (c *Map) Iterator() chan Tuple

Iterator will return a iterator (snapshot) of the map

func (*Map) Remove

func (c *Map) Remove(key string)

Remove will remove the key from the map (if exists)

Note: this method will silently fail on errors

func (*Map) Set

func (c *Map) Set(key string, newValue interface{}) error

Set will set the supplied value into the map

type ShardManager

type ShardManager interface {
	// Return the total number of shards in this concurrent map
	GetTotalShards() int64

	// Return the shard number for the supplied key
	GetShardNo(key string) (int64, error)
}

ShardManager controls how many shards exist and how keys are hashed

type ShardManagerFNV

type ShardManagerFNV struct {
	TotalShards int64
}

ShardManagerFNV implements manager using `hash/fnv.Hash32` to hash the keys

func (*ShardManagerFNV) GetShardNo

func (sm *ShardManagerFNV) GetShardNo(key string) (int64, error)

GetShardNo implements manager

func (*ShardManagerFNV) GetTotalShards

func (sm *ShardManagerFNV) GetTotalShards() int64

GetTotalShards implements manager

type Tuple

type Tuple struct {
	Key   string
	Value interface{}
}

Tuple is 1 key/value pair from the map

Jump to

Keyboard shortcuts

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