172 lines
3.2 KiB
Go

package ocean
import (
"encoding/json"
"os"
"path/filepath"
"strings"
"sync"
)
type Collection[T any] interface {
All() <-chan *T
Clear()
Delete(key string)
Exists(key string) bool
Get(key string) (value *T, ok bool)
Set(key string, value *T)
}
// collection is a hash map of homogeneous data.
type collection[T any] struct {
data sync.Map
name string
directory string
}
// New creates a new collection with the given name.
func New[T any](directories ...string) (*collection[T], error) {
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: directories[len(directories)-1],
directory: directory,
}
return c, c.loadFromDisk()
}
// 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
}
// Get returns the value for the given key.
func (c *collection[T]) Get(key string) (*T, bool) {
value, exists := c.data.Load(key)
return value.(*T), exists
}
// Set sets the value for the given key.
func (c *collection[T]) Set(key string, value *T) {
c.data.Store(key, value)
c.writeFileToDisk(key, value)
}
// Delete deletes a key from the collection.
func (c *collection[T]) Delete(key string) {
c.data.Delete(key)
os.Remove(c.keyFile(key))
}
// Exists returns whether or not the key exists.
func (c *collection[T]) Exists(key string) bool {
_, exists := c.data.Load(key)
return exists
}
// 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
})
}
// keyFile returns the file path for the given key.
func (c *collection[T]) keyFile(key string) string {
return filepath.Join(c.directory, key+".json")
}
// loadFromDisk loads the collection data from the disk.
func (c *collection[T]) loadFromDisk() error {
file, err := os.Open(c.directory)
if err != nil {
return err
}
files, err := file.Readdirnames(0)
for _, name := range files {
fileError := c.loadFileFromDisk(name)
if fileError != nil {
return fileError
}
}
if err != nil {
return err
}
return file.Close()
}
// loadFileFromDisk loads a single file from the disk.
func (c *collection[T]) loadFileFromDisk(name string) error {
file, err := os.Open(filepath.Join(c.directory, name))
if err != nil {
return err
}
value := new(T)
decoder := json.NewDecoder(file)
err = decoder.Decode(value)
if err != nil {
return err
}
key := strings.TrimSuffix(name, filepath.Ext(name))
c.data.Store(key, value)
return file.Close()
}
// writeFileToDisk writes the value for the key to disk as a JSON file.
func (c *collection[T]) writeFileToDisk(key string, value *T) {
fileName := c.keyFile(key)
file, err := os.Create(fileName)
if err != nil {
panic(err)
}
encoder := json.NewEncoder(file)
err = encoder.Encode(value)
if err != nil {
panic(err)
}
err = file.Close()
if err != nil {
panic(err)
}
}