Added explanation
This commit is contained in:
parent
309f03ba40
commit
979fd256a7
9
.editorconfig
Normal file
9
.editorconfig
Normal file
@ -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
|
1
.gitignore
vendored
1
.gitignore
vendored
@ -3,6 +3,7 @@
|
|||||||
|
|
||||||
# But not these files...
|
# But not these files...
|
||||||
!/.gitignore
|
!/.gitignore
|
||||||
|
!/.editorconfig
|
||||||
|
|
||||||
!*.go
|
!*.go
|
||||||
!go.sum
|
!go.sum
|
||||||
|
@ -3,7 +3,11 @@ package ocean
|
|||||||
import "sync"
|
import "sync"
|
||||||
|
|
||||||
type Collection[T any] interface {
|
type Collection[T any] interface {
|
||||||
|
All() <-chan T
|
||||||
|
Delete(key string)
|
||||||
|
Exists(key string) bool
|
||||||
Get(key string) (value T, ok bool)
|
Get(key string) (value T, ok bool)
|
||||||
|
Set(key string, value T)
|
||||||
}
|
}
|
||||||
|
|
||||||
// collection is a hash map of homogeneous data.
|
// 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)
|
_, exists := c.data.Load(key)
|
||||||
return exists
|
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
|
||||||
|
}
|
||||||
|
@ -7,29 +7,51 @@ import (
|
|||||||
"git.akyoto.dev/go/ocean"
|
"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")
|
users := ocean.NewCollection[string]("User")
|
||||||
|
|
||||||
assert.False(t, users.Exists("Hello"))
|
assert.True(t, !users.Exists("1"))
|
||||||
assert.False(t, users.Exists("World"))
|
assert.True(t, !users.Exists("2"))
|
||||||
|
|
||||||
users.Set("Hello", "Hello data")
|
users.Set("1", "abc")
|
||||||
|
|
||||||
assert.True(t, users.Exists("Hello"))
|
assert.True(t, users.Exists("1"))
|
||||||
assert.False(t, users.Exists("World"))
|
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("1"))
|
||||||
assert.True(t, users.Exists("World"))
|
assert.True(t, users.Exists("2"))
|
||||||
|
|
||||||
users.Delete("Hello")
|
users.Delete("1")
|
||||||
|
|
||||||
assert.False(t, users.Exists("Hello"))
|
assert.True(t, !users.Exists("1"))
|
||||||
assert.True(t, users.Exists("World"))
|
assert.True(t, users.Exists("2"))
|
||||||
|
|
||||||
users.Delete("World")
|
users.Delete("2")
|
||||||
|
|
||||||
assert.False(t, users.Exists("Hello"))
|
assert.True(t, !users.Exists("1"))
|
||||||
assert.False(t, users.Exists("World"))
|
assert.True(t, !users.Exists("2"))
|
||||||
}
|
}
|
||||||
|
@ -1,3 +1,6 @@
|
|||||||
# ocean
|
# 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.
|
||||||
|
Loading…
Reference in New Issue
Block a user