Added file system persistence
This commit is contained in:
parent
1ac0c91d31
commit
e88f91e9d3
@ -1,6 +1,11 @@
|
|||||||
package ocean
|
package ocean
|
||||||
|
|
||||||
import "sync"
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"os"
|
||||||
|
"path"
|
||||||
|
"sync"
|
||||||
|
)
|
||||||
|
|
||||||
type Collection[T any] interface {
|
type Collection[T any] interface {
|
||||||
All() <-chan T
|
All() <-chan T
|
||||||
@ -14,11 +19,30 @@ type Collection[T any] interface {
|
|||||||
type collection[T any] struct {
|
type collection[T any] struct {
|
||||||
data sync.Map
|
data sync.Map
|
||||||
name string
|
name string
|
||||||
|
directory string
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewCollection creates a new collection with the given name.
|
// NewCollection creates a new collection with the given name.
|
||||||
func NewCollection[T any](name string) *collection[T] {
|
func NewCollection[T any](namespace string, name string) (*collection[T], error) {
|
||||||
return &collection[T]{name: name}
|
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.
|
// 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.
|
// Set sets the value for the given key.
|
||||||
func (c *collection[T]) Set(key string, value T) {
|
func (c *collection[T]) Set(key string, value T) {
|
||||||
c.data.Store(key, value)
|
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.
|
// Delete deletes a key from the collection.
|
||||||
|
@ -8,7 +8,9 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func TestCollectionGet(t *testing.T) {
|
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")
|
users.Set("1", "abc")
|
||||||
user, exists := users.Get("1")
|
user, exists := users.Get("1")
|
||||||
|
|
||||||
@ -17,7 +19,9 @@ func TestCollectionGet(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestCollectionAll(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("1", "abc")
|
||||||
users.Set("2", "def")
|
users.Set("2", "def")
|
||||||
count := 0
|
count := 0
|
||||||
@ -30,7 +34,8 @@ func TestCollectionAll(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestInteraction(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("1"))
|
||||||
assert.True(t, !users.Exists("2"))
|
assert.True(t, !users.Exists("2"))
|
||||||
|
Loading…
Reference in New Issue
Block a user