Documentation ¶
Index ¶
- Constants
- type CMap
- func (cm *CMap) Delete(key interface{})
- func (cm *CMap) DeleteAndGet(key interface{}) interface{}
- func (cm *CMap) ForEach(fn func(key interface{}, val interface{}) bool) bool
- func (cm *CMap) ForEachLocked(fn func(key interface{}, val interface{}) bool) bool
- func (cm *CMap) Get(key interface{}) (val interface{})
- func (cm *CMap) GetOK(key interface{}) (val interface{}, ok bool)
- func (cm *CMap) Has(key interface{}) bool
- func (cm *CMap) Iter(ctx context.Context, buffer int) <-chan *KV
- func (cm *CMap) IterLocked(ctx context.Context, buffer int) <-chan *KV
- func (cm *CMap) Keys() []interface{}
- func (cm *CMap) Len() int
- func (cm *CMap) NumShards() int
- func (cm *CMap) Set(key interface{}, val interface{})
- func (cm *CMap) SetIfNotExists(key interface{}, val interface{}) (set bool)
- func (cm *CMap) ShardDistribution() []float64
- func (cm *CMap) ShardForKey(key interface{}) *LMap
- func (cm *CMap) Swap(key interface{}, val interface{}) interface{}
- func (cm *CMap) Update(key interface{}, fn func(oldval interface{}) (newval interface{}))
- type KV
- type LMap
- func (lm *LMap) Delete(key interface{})
- func (lm *LMap) DeleteAndGet(key interface{}) (v interface{})
- func (lm *LMap) ForEach(keys []interface{}, fn func(key interface{}, val interface{}) bool) bool
- func (lm *LMap) ForEachLocked(fn func(key interface{}, val interface{}) bool) bool
- func (lm *LMap) Get(key interface{}) (v interface{})
- func (lm *LMap) GetOK(key interface{}) (v interface{}, ok bool)
- func (lm *LMap) Has(key interface{}) (ok bool)
- func (lm *LMap) Keys(buf []interface{}) []interface{}
- func (lm *LMap) Len() (ln int)
- func (lm *LMap) Set(key interface{}, v interface{})
- func (lm *LMap) SetIfNotExists(key interface{}, val interface{}) (set bool)
- func (lm *LMap) Swap(key interface{}, newV interface{}) (oldV interface{})
- func (lm *LMap) Update(key interface{}, fn func(oldVal interface{}) (newVal interface{}))
Constants ¶
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 NewSize ¶
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 interface{})
Delete is the equivalent of `delete(map, key)`.
func (*CMap) DeleteAndGet ¶
func (cm *CMap) DeleteAndGet(key interface{}) interface{}
DeleteAndGet is the equivalent of `oldVal := map[key]; delete(map, key)`.
func (*CMap) ForEach ¶
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 ¶
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 interface{}) (val interface{})
Get is the equivalent of `val := map[key]`.
func (*CMap) Iter ¶
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 ¶
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() []interface{}
Keys returns a slice of all the keys of the map.
func (*CMap) Set ¶
func (cm *CMap) Set(key interface{}, val interface{})
Set is the equivalent of `map[key] = val`.
func (*CMap) SetIfNotExists ¶
SetIfNotExists will only assign val to key if it wasn't already set. Use `Update` if you need more logic.
func (*CMap) ShardDistribution ¶
ShardDistribution returns the distribution of data amoung all shards. Useful for debugging the efficiency of a hash.
func (*CMap) ShardForKey ¶
ShardForKey returns the LMap that may hold the specific key.
func (*CMap) Swap ¶
func (cm *CMap) Swap(key interface{}, val interface{}) interface{}
Swap is the equivalent of `oldVal, map[key] = map[key], newVal`.
func (*CMap) Update ¶
func (cm *CMap) Update(key interface{}, 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`.
type KV ¶
type KV struct { Key interface{} 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 NewLMapSize ¶
NewLMapSize is the equivalent of `m := make(map[interface{}]interface{}, cap)`
func (*LMap) Delete ¶
func (lm *LMap) Delete(key interface{})
Delete is the equivalent of `delete(map, key)`.
func (*LMap) DeleteAndGet ¶
func (lm *LMap) DeleteAndGet(key interface{}) (v interface{})
DeleteAndGet is the equivalent of `oldVal := map[key]; delete(map, key)`.
func (*LMap) ForEach ¶
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 ¶
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 interface{}) (v interface{})
Get is the equivalent of `val := map[key]`.
func (*LMap) Keys ¶
func (lm *LMap) Keys(buf []interface{}) []interface{}
Keys appends all the keys in the map to buf and returns buf. buf may be nil.
func (*LMap) Set ¶
func (lm *LMap) Set(key interface{}, v interface{})
Set is the equivalent of `map[key] = val`.
func (*LMap) SetIfNotExists ¶
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 interface{}, newV interface{}) (oldV interface{})
Swap is the equivalent of `oldVal, map[key] = map[key], newVal`.
func (*LMap) Update ¶
func (lm *LMap) Update(key interface{}, 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`.