Removed file extension
This commit is contained in:
parent
808ff00e3b
commit
9b9aef76eb
@ -5,7 +5,6 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"reflect"
|
"reflect"
|
||||||
"strings"
|
|
||||||
"sync"
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -105,7 +104,7 @@ 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+".json")
|
return filepath.Join(c.directory, key)
|
||||||
}
|
}
|
||||||
|
|
||||||
// loadFromDisk loads the collection data from the disk.
|
// loadFromDisk loads the collection data from the disk.
|
||||||
@ -118,8 +117,8 @@ func (c *collection[T]) loadFromDisk() error {
|
|||||||
|
|
||||||
files, err := file.Readdirnames(0)
|
files, err := file.Readdirnames(0)
|
||||||
|
|
||||||
for _, name := range files {
|
for _, key := range files {
|
||||||
fileError := c.loadFileFromDisk(name)
|
fileError := c.loadFileFromDisk(key)
|
||||||
|
|
||||||
if fileError != nil {
|
if fileError != nil {
|
||||||
return fileError
|
return fileError
|
||||||
@ -134,8 +133,8 @@ func (c *collection[T]) loadFromDisk() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// loadFileFromDisk loads a single file from the disk.
|
// loadFileFromDisk loads a single file from the disk.
|
||||||
func (c *collection[T]) loadFileFromDisk(name string) error {
|
func (c *collection[T]) loadFileFromDisk(key string) error {
|
||||||
file, err := os.Open(filepath.Join(c.directory, name))
|
file, err := os.Open(filepath.Join(c.directory, key))
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@ -146,10 +145,10 @@ func (c *collection[T]) loadFileFromDisk(name string) error {
|
|||||||
err = decoder.Decode(value)
|
err = decoder.Decode(value)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
file.Close()
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
key := strings.TrimSuffix(name, filepath.Ext(name))
|
|
||||||
c.data.Store(key, value)
|
c.data.Store(key, value)
|
||||||
return file.Close()
|
return file.Close()
|
||||||
}
|
}
|
||||||
@ -167,6 +166,7 @@ func (c *collection[T]) writeFileToDisk(key string, value *T) error {
|
|||||||
err = encoder.Encode(value)
|
err = encoder.Encode(value)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
file.Close()
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -44,6 +44,8 @@ You can add as many directory hierarchies as you need but I recommend using a si
|
|||||||
|
|
||||||
## Limitations
|
## Limitations
|
||||||
|
|
||||||
This storage mechanism is suitable for small to medium data volume.
|
* Keys cannot be empty and they cannot contain a directory separator like `/`.
|
||||||
|
|
||||||
It is not suited for big data, however the package is pretty lightweight so you can combine it with a big data store.
|
* This storage mechanism is only suitable for small to medium data volume.
|
||||||
|
|
||||||
|
Ocean isn't meant to be used for big data, however the package is very lightweight so you can combine it with a big data store.
|
||||||
|
Loading…
Reference in New Issue
Block a user