Improved directory storage

This commit is contained in:
Eduard Urbach 2023-07-12 16:05:09 +02:00
parent 0c6db9d1f3
commit 22391ed036
Signed by: akyoto
GPG Key ID: C874F672B1AF20C0
4 changed files with 56 additions and 9 deletions

View File

@ -44,6 +44,10 @@ for user := range users.All() {
## Storage systems
### nil
You can specify `nil` as the storage system which will keep data in RAM only.
### storage.File
`storage.File` uses a single file to store all records.
@ -59,6 +63,10 @@ You should use `storage.File` if you have a permanently running process such as
Make sure you `defer collection.Sync()` to ensure that queued writes will be handled when the process ends.
### storage.Directory
`storage.Directory` creates a directory for your records and saves each record in a separate file. The performance of this method heavily depends on the file system you are using.
## Benchmarks
```

View File

@ -20,7 +20,7 @@ func (ds *Directory[T]) Init(c ocean.StorageData) error {
ds.directory = filepath.Join(c.Root(), c.Name())
err := os.Mkdir(ds.directory, 0700)
if err != nil {
if err != nil && !os.IsExist(err) {
return err
}
@ -70,8 +70,8 @@ func (ds *Directory[T]) read() error {
// readFile loads a single file from the disk.
func (ds *Directory[T]) readFile(fileName string) error {
fileName = filepath.Join(ds.directory, fileName)
file, err := os.Open(fileName)
filePath := filepath.Join(ds.directory, fileName)
file, err := os.Open(filePath)
if err != nil {
return err

42
storage/Directory_test.go Normal file
View File

@ -0,0 +1,42 @@
package storage_test
import (
"testing"
"git.akyoto.dev/go/assert"
"git.akyoto.dev/go/ocean"
"git.akyoto.dev/go/ocean/storage"
)
var _ ocean.Storage[string] = (*storage.Directory[string])(nil)
func TestDirectoryPersistence(t *testing.T) {
users, err := ocean.New[User]("test", &storage.Directory[User]{})
assert.Nil(t, err)
defer users.Sync()
defer users.Clear()
users.Set("1", &User{Name: "User 1"})
users.Set("2", &User{Name: "User 2"})
users.Set("3", &User{Name: "User 3"})
users.Sync()
reload, err := ocean.New[User]("test", &storage.Directory[User]{})
assert.Nil(t, err)
user, err := reload.Get("1")
assert.Nil(t, err)
assert.NotNil(t, user)
assert.Equal(t, user.Name, "User 1")
user, err = reload.Get("2")
assert.Nil(t, err)
assert.NotNil(t, user)
assert.Equal(t, user.Name, "User 2")
user, err = reload.Get("3")
assert.Nil(t, err)
assert.NotNil(t, user)
assert.Equal(t, user.Name, "User 3")
}

View File

@ -8,16 +8,13 @@ import (
"git.akyoto.dev/go/ocean/storage"
)
var _ ocean.Storage[string] = (*storage.File[string])(nil)
type User struct {
Name string `json:"name"`
}
func TestInterface(t *testing.T) {
var _ ocean.Storage[string] = (*storage.File[string])(nil)
var _ ocean.Storage[string] = (*storage.Directory[string])(nil)
}
func TestPersistence(t *testing.T) {
func TestFilePersistence(t *testing.T) {
users, err := ocean.New[User]("test", &storage.File[User]{})
assert.Nil(t, err)