Added file system persistence
This commit is contained in:
parent
1ac0c91d31
commit
e88f91e9d3
@ -1,6 +1,11 @@
|
||||
package ocean
|
||||
|
||||
import "sync"
|
||||
import (
|
||||
"encoding/json"
|
||||
"os"
|
||||
"path"
|
||||
"sync"
|
||||
)
|
||||
|
||||
type Collection[T any] interface {
|
||||
All() <-chan T
|
||||
@ -12,13 +17,32 @@ type Collection[T any] interface {
|
||||
|
||||
// collection is a hash map of homogeneous data.
|
||||
type collection[T any] struct {
|
||||
data sync.Map
|
||||
name string
|
||||
data sync.Map
|
||||
name string
|
||||
directory string
|
||||
}
|
||||
|
||||
// NewCollection creates a new collection with the given name.
|
||||
func NewCollection[T any](name string) *collection[T] {
|
||||
return &collection[T]{name: name}
|
||||
func NewCollection[T any](namespace string, name string) (*collection[T], error) {
|
||||
home, err := os.UserHomeDir()
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
directory := path.Join(home, ".ocean", namespace, name)
|
||||
err = os.MkdirAll(directory, 0700)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
c := &collection[T]{
|
||||
name: name,
|
||||
directory: directory,
|
||||
}
|
||||
|
||||
return c, err
|
||||
}
|
||||
|
||||
// Get returns the value for the given key.
|
||||
@ -30,6 +54,22 @@ func (c *collection[T]) Get(key string) (T, bool) {
|
||||
// Set sets the value for the given key.
|
||||
func (c *collection[T]) Set(key string, value T) {
|
||||
c.data.Store(key, value)
|
||||
|
||||
fileName := path.Join(c.directory, key+".json")
|
||||
file, err := os.Create(fileName)
|
||||
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
encoder := json.NewEncoder(file)
|
||||
encoder.Encode(value)
|
||||
|
||||
err = file.Close()
|
||||
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
||||
// Delete deletes a key from the collection.
|
||||
|
@ -8,7 +8,9 @@ import (
|
||||
)
|
||||
|
||||
func TestCollectionGet(t *testing.T) {
|
||||
users := ocean.NewCollection[string]("User")
|
||||
users, err := ocean.NewCollection[string]("test", "User")
|
||||
assert.Nil(t, err)
|
||||
|
||||
users.Set("1", "abc")
|
||||
user, exists := users.Get("1")
|
||||
|
||||
@ -17,7 +19,9 @@ func TestCollectionGet(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestCollectionAll(t *testing.T) {
|
||||
users := ocean.NewCollection[string]("User")
|
||||
users, err := ocean.NewCollection[string]("test", "User")
|
||||
assert.Nil(t, err)
|
||||
|
||||
users.Set("1", "abc")
|
||||
users.Set("2", "def")
|
||||
count := 0
|
||||
@ -30,7 +34,8 @@ func TestCollectionAll(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestInteraction(t *testing.T) {
|
||||
users := ocean.NewCollection[string]("User")
|
||||
users, err := ocean.NewCollection[string]("test", "User")
|
||||
assert.Nil(t, err)
|
||||
|
||||
assert.True(t, !users.Exists("1"))
|
||||
assert.True(t, !users.Exists("2"))
|
||||
|
Loading…
Reference in New Issue
Block a user