stringcmap

package
v0.0.0-...-ccaef76 Latest Latest
Warning

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

Go to latest
Published: Aug 25, 2017 License: Apache-2.0 Imports: 7 Imported by: 2

Documentation

Index

Constants

View Source
const DefaultShardCount = 1 << 8

DefaultShardCount is the default number of shards to use when New() or NewFromJSON() are called. The default is 256.

Variables

This section is empty.

Functions

This section is empty.

Types

type CMap

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

CMap is a concurrent safe sharded map to scale on multiple cores.

func New

func New() *CMap

New is an alias for NewSize(DefaultShardCount)

func NewSize

func NewSize(shardCount int) *CMap

NewSize returns a CMap with the specific shardSize, note that for performance reasons, shardCount must be a power of 2. Higher shardCount will improve concurrency but will consume more memory.

func (*CMap) Delete

func (cm *CMap) Delete(key string)

Delete is the equivalent of `delete(map, key)`.

func (*CMap) DeleteAndGet

func (cm *CMap) DeleteAndGet(key string) interface{}

DeleteAndGet is the equivalent of `oldVal := map[key]; delete(map, key)`.

func (*CMap) ForEach

func (cm *CMap) ForEach(fn func(key string, val interface{}) bool) bool

ForEach loops over all the key/values in the map. You can break early by returning false. It **is** safe to modify the map while using this iterator, however it uses more memory and is slightly slower.

func (*CMap) ForEachLocked

func (cm *CMap) ForEachLocked(fn func(key string, val interface{}) bool) bool

ForEachLocked loops over all the key/values in the map. You can break early by returning false. It is **NOT* safe to modify the map while using this iterator.

func (*CMap) Get

func (cm *CMap) Get(key string) (val interface{})

Get is the equivalent of `val := map[key]`.

func (*CMap) GetOK

func (cm *CMap) GetOK(key string) (val interface{}, ok bool)

GetOK is the equivalent of `val, ok := map[key]`.

func (*CMap) Has

func (cm *CMap) Has(key string) bool

Has is the equivalent of `_, ok := map[key]`.

func (*CMap) Iter

func (cm *CMap) Iter(ctx context.Context, buffer int) <-chan *KV

Iter returns a channel to be used in for range. Use `context.WithCancel` if you intend to break early or goroutines will leak. It **is** safe to modify the map while using this iterator, however it uses more memory and is slightly slower.

func (*CMap) IterLocked

func (cm *CMap) IterLocked(ctx context.Context, buffer int) <-chan *KV

IterLocked returns a channel to be used in for range. Use `context.WithCancel` if you intend to break early or goroutines will leak and map access will deadlock. It is **NOT* safe to modify the map while using this iterator.

func (*CMap) Keys

func (cm *CMap) Keys() []string

Keys returns a slice of all the keys of the map.

func (*CMap) Len

func (cm *CMap) Len() int

Len returns the length of the map.

func (*CMap) MarshalJSON

func (cm *CMap) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaler.

func (*CMap) NumShards

func (cm *CMap) NumShards() int

NumShards returns the number of shards in the map.

func (*CMap) Set

func (cm *CMap) Set(key string, val interface{})

Set is the equivalent of `map[key] = val`.

func (*CMap) SetIfNotExists

func (cm *CMap) SetIfNotExists(key string, val interface{}) (set bool)

SetIfNotExists will only assign val to key if it wasn't already set. Use `Update` if you need more logic.

func (*CMap) ShardDistribution

func (cm *CMap) ShardDistribution() []float64

ShardDistribution returns the distribution of data amoung all shards. Useful for debugging the efficiency of a hash.

func (*CMap) ShardForKey

func (cm *CMap) ShardForKey(key string) *LMap

ShardForKey returns the LMap that may hold the specific key.

func (*CMap) Swap

func (cm *CMap) Swap(key string, val interface{}) interface{}

Swap is the equivalent of `oldVal, map[key] = map[key], newVal`.

func (*CMap) Update

func (cm *CMap) Update(key string, fn func(oldval interface{}) (newval interface{}))

Update calls `fn` with the key's old value (or nil) and assign the returned value to the key. The shard containing the key will be locked, it is NOT safe to call other cmap funcs inside `fn`.

func (*CMap) WithJSON

func (cm *CMap) WithJSON(fn func(raw json.RawMessage) (interface{}, error)) *MapWithJSON

WithJSON returns a MapWithJSON with the specific unmarshal func.

type KV

type KV struct {
	Key   string
	Value interface{}
}

KV holds the key/value returned when Iter is called.

type LMap

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

LMap is a simple sync.RWMutex locked map. Used by CMap internally for sharding.

func NewLMap

func NewLMap() *LMap

NewLMap returns a new LMap with the cap set to 0.

func NewLMapSize

func NewLMapSize(cap int) *LMap

NewLMapSize is the equivalent of `m := make(map[string]interface{}, cap)`

func (*LMap) Delete

func (lm *LMap) Delete(key string)

Delete is the equivalent of `delete(map, key)`.

func (*LMap) DeleteAndGet

func (lm *LMap) DeleteAndGet(key string) (v interface{})

DeleteAndGet is the equivalent of `oldVal := map[key]; delete(map, key)`.

func (*LMap) ForEach

func (lm *LMap) ForEach(keys []string, fn func(key string, val interface{}) bool) bool

ForEach loops over all the key/values in the map. You can break early by returning an error . It **is** safe to modify the map while using this iterator, however it uses more memory and is slightly slower.

func (*LMap) ForEachLocked

func (lm *LMap) ForEachLocked(fn func(key string, val interface{}) bool) bool

ForEachLocked loops over all the key/values in the map. You can break early by returning false It is **NOT* safe to modify the map while using this iterator.

func (*LMap) Get

func (lm *LMap) Get(key string) (v interface{})

Get is the equivalent of `val := map[key]`.

func (*LMap) GetOK

func (lm *LMap) GetOK(key string) (v interface{}, ok bool)

GetOK is the equivalent of `val, ok := map[key]`.

func (*LMap) Has

func (lm *LMap) Has(key string) (ok bool)

Has is the equivalent of `_, ok := map[key]`.

func (*LMap) Keys

func (lm *LMap) Keys(buf []string) []string

Keys appends all the keys in the map to buf and returns buf. buf may be nil.

func (*LMap) Len

func (lm *LMap) Len() (ln int)

Len returns the length of the map.

func (*LMap) Set

func (lm *LMap) Set(key string, v interface{})

Set is the equivalent of `map[key] = val`.

func (*LMap) SetIfNotExists

func (lm *LMap) SetIfNotExists(key string, val interface{}) (set bool)

SetIfNotExists will only assign val to key if it wasn't already set. Use `Update` if you need more logic.

func (*LMap) Swap

func (lm *LMap) Swap(key string, newV interface{}) (oldV interface{})

Swap is the equivalent of `oldVal, map[key] = map[key], newVal`.

func (*LMap) Update

func (lm *LMap) Update(key string, fn func(oldVal interface{}) (newVal interface{}))

Update calls `fn` with the key's old value (or nil) and assigns the returned value to the key. The shard containing the key will be locked, it is NOT safe to call other cmap funcs inside `fn`.

type MapWithJSON

type MapWithJSON struct {
	*CMap
	UnmarshalValueFn func(raw json.RawMessage) (interface{}, error)
}

MapWithJSON is a CMap with UnmarshalJSON support. Usage:

var mwj MapWithJSON
json.Unmarshal(`{"key":"value"}`, &mwj)

func (*MapWithJSON) UnmarshalJSON

func (mwj *MapWithJSON) UnmarshalJSON(p []byte) error

UnmarshalJSON implements json.Unmarshaler

func (*MapWithJSON) WriteTo

func (mwj *MapWithJSON) WriteTo(w io.Writer) (n int64, err error)

WriteTo implements io.WriterTo, outputs the map as a json object.

Jump to

Keyboard shortcuts

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