172 lines
3.2 KiB
Go
Raw Normal View History

2023-07-04 22:20:26 +00:00
package ocean
2023-07-05 21:59:49 +00:00
import (
"encoding/json"
"os"
2023-07-06 12:10:12 +00:00
"path/filepath"
"strings"
2023-07-05 21:59:49 +00:00
"sync"
)
2023-07-04 22:20:26 +00:00
2023-07-05 15:41:53 +00:00
type Collection[T any] interface {
2023-07-06 12:10:12 +00:00
All() <-chan *T
Clear()
2023-07-05 20:02:16 +00:00
Delete(key string)
Exists(key string) bool
2023-07-06 12:10:12 +00:00
Get(key string) (value *T, ok bool)
Set(key string, value *T)
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-05 15:41:53 +00:00
type collection[T any] struct {
2023-07-05 21:59:49 +00:00
data sync.Map
name string
directory string
2023-07-04 22:20:26 +00:00
}
2023-07-06 12:27:48 +00:00
// New creates a new collection with the given name.
func New[T any](directories ...string) (*collection[T], error) {
2023-07-05 21:59:49 +00:00
home, err := os.UserHomeDir()
if err != nil {
return nil, err
}
2023-07-06 12:27:48 +00:00
directories = append([]string{home, ".ocean"}, directories...)
directory := filepath.Join(directories...)
2023-07-05 21:59:49 +00:00
err = os.MkdirAll(directory, 0700)
if err != nil {
return nil, err
}
c := &collection[T]{
2023-07-06 12:27:48 +00:00
name: directories[len(directories)-1],
2023-07-05 21:59:49 +00:00
directory: directory,
}
2023-07-06 12:10:12 +00:00
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
2023-07-05 15:23:50 +00:00
}
// Get returns the value for the given key.
2023-07-06 12:10:12 +00:00
func (c *collection[T]) Get(key string) (*T, bool) {
2023-07-05 15:41:53 +00:00
value, exists := c.data.Load(key)
2023-07-06 12:10:12 +00:00
return value.(*T), exists
2023-07-05 15:23:50 +00:00
}
// Set sets the value for the given key.
2023-07-06 12:10:12 +00:00
func (c *collection[T]) Set(key string, value *T) {
2023-07-05 15:23:50 +00:00
c.data.Store(key, value)
2023-07-06 12:10:12 +00:00
c.writeFileToDisk(key, value)
}
2023-07-05 21:59:49 +00:00
2023-07-06 12:10:12 +00:00
// 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)
2023-07-05 21:59:49 +00:00
if err != nil {
2023-07-06 12:10:12 +00:00
return err
2023-07-05 21:59:49 +00:00
}
2023-07-06 12:10:12 +00:00
files, err := file.Readdirnames(0)
2023-07-05 21:59:49 +00:00
2023-07-06 12:10:12 +00:00
for _, name := range files {
fileError := c.loadFileFromDisk(name)
if fileError != nil {
return fileError
}
}
2023-07-05 21:59:49 +00:00
if err != nil {
2023-07-06 12:10:12 +00:00
return err
2023-07-05 21:59:49 +00:00
}
2023-07-05 15:23:50 +00:00
2023-07-06 12:10:12 +00:00
return file.Close()
2023-07-04 22:20:26 +00:00
}
2023-07-06 12:10:12 +00:00
// 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()
2023-07-05 15:23:50 +00:00
}
2023-07-05 20:02:16 +00:00
2023-07-06 12:10:12 +00:00
// 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)
2023-07-05 20:02:16 +00:00
2023-07-06 12:10:12 +00:00
if err != nil {
panic(err)
}
2023-07-05 20:02:16 +00:00
2023-07-06 12:10:12 +00:00
encoder := json.NewEncoder(file)
err = encoder.Encode(value)
2023-07-05 20:02:16 +00:00
2023-07-06 12:10:12 +00:00
if err != nil {
panic(err)
}
err = file.Close()
if err != nil {
panic(err)
}
2023-07-05 20:02:16 +00:00
}