Added file extensions
This commit is contained in:
parent
038b6fa31f
commit
bb3a571b01
@ -5,6 +5,7 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"reflect"
|
"reflect"
|
||||||
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -133,18 +134,19 @@ func (c *collection[T]) Clear() {
|
|||||||
|
|
||||||
// keyFile returns the file path for the given key.
|
// keyFile returns the file path for the given key.
|
||||||
func (c *collection[T]) keyFile(key string) string {
|
func (c *collection[T]) keyFile(key string) string {
|
||||||
return filepath.Join(c.directory, key)
|
return filepath.Join(c.directory, key+".json")
|
||||||
}
|
}
|
||||||
|
|
||||||
// loadFromDisk loads the collection data from the disk.
|
// loadFromDisk loads the collection data from the disk.
|
||||||
func (c *collection[T]) loadFromDisk() error {
|
func (c *collection[T]) loadFromDisk() error {
|
||||||
file, err := os.Open(c.directory)
|
dir, err := os.Open(c.directory)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
files, err := file.Readdirnames(0)
|
defer dir.Close()
|
||||||
|
files, err := dir.Readdirnames(0)
|
||||||
|
|
||||||
for _, key := range files {
|
for _, key := range files {
|
||||||
fileError := c.loadFileFromDisk(key)
|
fileError := c.loadFileFromDisk(key)
|
||||||
@ -154,16 +156,12 @@ func (c *collection[T]) loadFromDisk() error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
return file.Close()
|
|
||||||
}
|
|
||||||
|
|
||||||
// loadFileFromDisk loads a single file from the disk.
|
// loadFileFromDisk loads a single file from the disk.
|
||||||
func (c *collection[T]) loadFileFromDisk(key string) error {
|
func (c *collection[T]) loadFileFromDisk(fileName string) error {
|
||||||
file, err := os.Open(filepath.Join(c.directory, key))
|
file, err := os.Open(filepath.Join(c.directory, fileName))
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@ -178,6 +176,7 @@ func (c *collection[T]) loadFileFromDisk(key string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
key := strings.TrimSuffix(fileName, ".json")
|
||||||
c.data.Store(key, value)
|
c.data.Store(key, value)
|
||||||
return file.Close()
|
return file.Close()
|
||||||
}
|
}
|
||||||
@ -185,7 +184,7 @@ func (c *collection[T]) loadFileFromDisk(key string) error {
|
|||||||
// writeFileToDisk writes the value for the key to disk as a JSON file.
|
// writeFileToDisk writes the value for the key to disk as a JSON file.
|
||||||
func (c *collection[T]) writeFileToDisk(key string, value *T) error {
|
func (c *collection[T]) writeFileToDisk(key string, value *T) error {
|
||||||
fileName := c.keyFile(key)
|
fileName := c.keyFile(key)
|
||||||
file, err := os.Create(fileName)
|
file, err := os.OpenFile(fileName, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0600)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
package ocean_test
|
package ocean_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strconv"
|
||||||
"sync"
|
"sync"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
@ -196,3 +198,24 @@ func BenchmarkDelete(b *testing.B) {
|
|||||||
|
|
||||||
b.StopTimer()
|
b.StopTimer()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func BenchmarkColdStart100Files(b *testing.B) {
|
||||||
|
users, err := ocean.New[User]("test")
|
||||||
|
assert.Nil(b, err)
|
||||||
|
defer users.Clear()
|
||||||
|
|
||||||
|
for i := 0; i < 100; i++ {
|
||||||
|
users.Set(strconv.Itoa(i), &User{Name: fmt.Sprintf("User %d", i)})
|
||||||
|
}
|
||||||
|
|
||||||
|
b.ReportAllocs()
|
||||||
|
b.ResetTimer()
|
||||||
|
|
||||||
|
for n := 0; n < b.N; n++ {
|
||||||
|
again, err := ocean.New[User]("test")
|
||||||
|
assert.Nil(b, err)
|
||||||
|
assert.NotNil(b, again)
|
||||||
|
}
|
||||||
|
|
||||||
|
b.StopTimer()
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user