72 lines
1.4 KiB
Go
72 lines
1.4 KiB
Go
package lib
|
|
|
|
import (
|
|
"sync"
|
|
)
|
|
|
|
// Collection is sync.Map, that stores arbitrary data.
|
|
type Collection struct {
|
|
items sync.Map
|
|
}
|
|
|
|
// Item just interface for stoing any data.
|
|
type item struct {
|
|
data any
|
|
}
|
|
|
|
// NewCollection creates new instance of "collection".
|
|
func NewCollection() *Collection {
|
|
c := &Collection{}
|
|
|
|
return c
|
|
}
|
|
|
|
// Get returns value by given key. Or empty value.
|
|
func (collection *Collection) Get(key any) (any, bool) {
|
|
obj, exists := collection.items.Load(key)
|
|
|
|
if !exists {
|
|
return nil, false
|
|
}
|
|
|
|
it, ok := obj.(item)
|
|
if !ok {
|
|
return nil, false
|
|
}
|
|
|
|
return it.data, true
|
|
}
|
|
|
|
// Set saves value to given "collection".
|
|
func (collection *Collection) Set(key any, value any) {
|
|
collection.items.Store(key, item{
|
|
data: value,
|
|
})
|
|
}
|
|
|
|
// Range apply function f to all keys/values in collection. Returns immediately on first error.
|
|
func (collection *Collection) Range(f func(key, value any) bool) {
|
|
fn := func(key, value any) bool {
|
|
it, ok := value.(item)
|
|
if !ok {
|
|
return true
|
|
}
|
|
|
|
return f(key, it.data)
|
|
}
|
|
|
|
collection.items.Range(fn)
|
|
}
|
|
|
|
// Delete deletes key and corresponding value from collection.
|
|
func (collection *Collection) Delete(key any) {
|
|
collection.items.Delete(key)
|
|
}
|
|
|
|
// Close frees memory occupied by collection and deletes collection itself.
|
|
func (collection *Collection) Close() {
|
|
collection.items = sync.Map{}
|
|
}
|
|
|
|
/* vim: set ft=go noet ai ts=4 sw=4 sts=4: */
|