135 lines
2.5 KiB
Go
135 lines
2.5 KiB
Go
package ocean
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"reflect"
|
|
"sync"
|
|
)
|
|
|
|
type Collection[T any] interface {
|
|
All() <-chan *T
|
|
Clear()
|
|
Delete(key string)
|
|
Exists(key string) bool
|
|
Filter(func(*T) bool) <-chan *T
|
|
Get(key string) (value *T, err error)
|
|
Set(key string, value *T)
|
|
Sync()
|
|
}
|
|
|
|
// collection is a hash map of homogeneous data.
|
|
type collection[T any] struct {
|
|
data sync.Map
|
|
storage Storage[T]
|
|
name string
|
|
root string
|
|
}
|
|
|
|
// New creates a new collection with the given name.
|
|
func New[T any](directories ...string) (*collection[T], error) {
|
|
name := reflect.TypeOf((*T)(nil)).Elem().Name()
|
|
home, err := os.UserHomeDir()
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
directories = append([]string{home, ".ocean"}, directories...)
|
|
directory := filepath.Join(directories...)
|
|
err = os.MkdirAll(directory, 0700)
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
c := &collection[T]{
|
|
name: name,
|
|
root: directory,
|
|
storage: &FileStorage[T]{},
|
|
}
|
|
|
|
return c, c.storage.Init(c)
|
|
}
|
|
|
|
// All returns a channel of all objects in the collection.
|
|
func (c *collection[T]) All() <-chan *T {
|
|
channel := make(chan *T)
|
|
|
|
go func() {
|
|
c.data.Range(func(key, value any) bool {
|
|
channel <- value.(*T)
|
|
return true
|
|
})
|
|
|
|
close(channel)
|
|
}()
|
|
|
|
return channel
|
|
}
|
|
|
|
// Clear deletes all objects from the collection.
|
|
func (c *collection[T]) Clear() {
|
|
c.data.Range(func(key, value any) bool {
|
|
c.Delete(key.(string))
|
|
return true
|
|
})
|
|
}
|
|
|
|
// Delete deletes a key from the collection.
|
|
func (c *collection[T]) Delete(key string) {
|
|
c.data.Delete(key)
|
|
c.storage.Delete(key)
|
|
}
|
|
|
|
// Exists returns whether or not the key exists.
|
|
func (c *collection[T]) Exists(key string) bool {
|
|
_, exists := c.data.Load(key)
|
|
return exists
|
|
}
|
|
|
|
// Get returns the value for the given key.
|
|
func (c *collection[T]) Get(key string) (*T, error) {
|
|
value, exists := c.data.Load(key)
|
|
|
|
if !exists {
|
|
return nil, &KeyNotFoundError{Key: key}
|
|
}
|
|
|
|
return value.(*T), nil
|
|
}
|
|
|
|
// Filter returns a channel of all objects that pass the given filter function.
|
|
func (c *collection[T]) Filter(filter func(*T) bool) <-chan *T {
|
|
channel := make(chan *T)
|
|
|
|
go func() {
|
|
c.data.Range(func(key, value any) bool {
|
|
if filter(value.(*T)) {
|
|
channel <- value.(*T)
|
|
}
|
|
|
|
return true
|
|
})
|
|
|
|
close(channel)
|
|
}()
|
|
|
|
return channel
|
|
}
|
|
|
|
// Set sets the value for the given key.
|
|
func (c *collection[T]) Set(key string, value *T) {
|
|
c.data.Store(key, value)
|
|
err := c.storage.Set(key, value)
|
|
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
|
|
// Sync waits for all disk writes to finish before it returns.
|
|
func (c *collection[T]) Sync() {
|
|
c.storage.Sync()
|
|
}
|