diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..3c94c19 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,9 @@ +root = true + +[*] +indent_style = tab +indent_size = 4 +end_of_line = lf +charset = utf-8 +trim_trailing_whitespace = true +insert_final_newline = false diff --git a/.gitignore b/.gitignore index 420c61b..342a59c 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,7 @@ # But not these files... !/.gitignore +!/.editorconfig !*.go !go.sum diff --git a/Collection.go b/Collection.go index c7fcf1c..ff869bd 100644 --- a/Collection.go +++ b/Collection.go @@ -3,7 +3,11 @@ package ocean import "sync" type Collection[T any] interface { + All() <-chan T + 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. @@ -38,3 +42,19 @@ func (c *collection[T]) Exists(key string) bool { _, exists := c.data.Load(key) return exists } + +// 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 +} diff --git a/Collection_test.go b/Collection_test.go index 8cf4ecc..e891706 100644 --- a/Collection_test.go +++ b/Collection_test.go @@ -7,29 +7,51 @@ import ( "git.akyoto.dev/go/ocean" ) -func TestNewCollection(t *testing.T) { +func TestCollectionGet(t *testing.T) { + users := ocean.NewCollection[string]("User") + users.Set("1", "abc") + user, exists := users.Get("1") + + assert.True(t, exists) + assert.NotNil(t, user) +} + +func TestCollectionAll(t *testing.T) { + users := ocean.NewCollection[string]("User") + users.Set("1", "abc") + users.Set("2", "def") + count := 0 + + for range users.All() { + count++ + } + + assert.Equal(t, count, 2) +} + +func TestInteraction(t *testing.T) { users := ocean.NewCollection[string]("User") - assert.False(t, users.Exists("Hello")) - assert.False(t, users.Exists("World")) + assert.True(t, !users.Exists("1")) + assert.True(t, !users.Exists("2")) - users.Set("Hello", "Hello data") + users.Set("1", "abc") - assert.True(t, users.Exists("Hello")) - assert.False(t, users.Exists("World")) + assert.True(t, users.Exists("1")) + assert.True(t, !users.Exists("2")) - users.Set("World", "World data") + users.Set("2", "def") - assert.True(t, users.Exists("Hello")) - assert.True(t, users.Exists("World")) + assert.True(t, users.Exists("1")) + assert.True(t, users.Exists("2")) - users.Delete("Hello") + users.Delete("1") - assert.False(t, users.Exists("Hello")) - assert.True(t, users.Exists("World")) + assert.True(t, !users.Exists("1")) + assert.True(t, users.Exists("2")) - users.Delete("World") + users.Delete("2") - assert.False(t, users.Exists("Hello")) - assert.False(t, users.Exists("World")) + assert.True(t, !users.Exists("1")) + assert.True(t, !users.Exists("2")) } diff --git a/README.md b/README.md index 69b93cc..f02806e 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,6 @@ # ocean -In-memory key value store that writes JSON to disk. +In-memory key value store that saves your data to simple JSON files. +No background processes, no schemas, just plain old files. +If you like, you can execute a search on your entire data with classic UNIX tools. +Writing to disk is async, so the performance of your code is only limited by your CPU and RAM speed.