2023-07-04 22:20:26 +00:00
|
|
|
package ocean
|
|
|
|
|
|
|
|
import "sync"
|
|
|
|
|
|
|
|
type Collection interface {
|
2023-07-05 15:23:50 +00:00
|
|
|
Get(key string) (value any, ok bool)
|
2023-07-04 22:20:26 +00:00
|
|
|
}
|
|
|
|
|
2023-07-05 15:23:50 +00:00
|
|
|
// collection is a hash map of homogeneous data.
|
2023-07-04 22:20:26 +00:00
|
|
|
type collection struct {
|
|
|
|
data sync.Map
|
|
|
|
name string
|
|
|
|
}
|
|
|
|
|
2023-07-05 15:23:50 +00:00
|
|
|
// Force interface implementation
|
|
|
|
var _ Collection = (*collection)(nil)
|
|
|
|
|
|
|
|
// NewCollection creates a new collection with the given name.
|
|
|
|
func NewCollection(name string) *collection {
|
|
|
|
return &collection{name: name}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get returns the value for the given key.
|
|
|
|
func (c *collection) Get(key string) (value any, ok bool) {
|
|
|
|
return c.data.Load(key)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set sets the value for the given key.
|
|
|
|
func (c *collection) Set(key string, value any) {
|
|
|
|
c.data.Store(key, value)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete deletes a key from the collection.
|
|
|
|
func (c *collection) Delete(key string) {
|
|
|
|
c.data.Delete(key)
|
2023-07-04 22:20:26 +00:00
|
|
|
}
|
|
|
|
|
2023-07-05 15:23:50 +00:00
|
|
|
// Exists returns whether or not the key exists.
|
|
|
|
func (c *collection) Exists(key string) bool {
|
|
|
|
_, exists := c.data.Load(key)
|
|
|
|
return exists
|
|
|
|
}
|