diff --git a/diskv_wrapper.go b/diskv_wrapper.go index a77b3ff..86dc604 100644 --- a/diskv_wrapper.go +++ b/diskv_wrapper.go @@ -1,12 +1,21 @@ package diskv_wrapper import ( - "strings" + "errors" "github.com/peterbourgon/diskv/v3" ) -func Init(basePath string) *diskv.Diskv { +type DiskvWrapper struct { + Disk *diskv.Diskv +} + +// New will initialize diskv, if basepath is empty it will return nil, error +func New(basePath string) (*DiskvWrapper, error) { + if basePath == "" { + return nil, errors.New("please provide basepath for main store") + } + disk := diskv.New(diskv.Options{ BasePath: basePath, AdvancedTransform: AdvanceTransform, @@ -14,11 +23,21 @@ func Init(basePath string) *diskv.Diskv { CacheSizeMax: 1024 * 1024, }) - return disk + wrapper := &DiskvWrapper{ + Disk: disk, + } + + return wrapper, nil } -func GetKeyVersion(d *diskv.Diskv, version string, key string) string { - path := strings.Join([]string{version, key}, "/") +// Init can be used as single method that will return *diskv.Diskv +func Init(basePath string) *diskv.Diskv { + disk := diskv.New(diskv.Options{ + BasePath: basePath, + AdvancedTransform: AdvanceTransform, + InverseTransform: InverseTransform, + CacheSizeMax: 1024 * 1024, + }) - return path + return disk } diff --git a/example/main.go b/example/main.go index 7e8cc4d..c890c3e 100644 --- a/example/main.go +++ b/example/main.go @@ -56,7 +56,7 @@ func main() { return } - path := diskv_wrapper.GetKeyVersion(currentDisk, version, key) + path := diskv_wrapper.WritePath(currentDisk, version, key) currentDisk.WriteString(path, value) diff --git a/tests/diskv_wrapper_test.go b/tests/diskv_wrapper_test.go index debc3f8..32aaadc 100644 --- a/tests/diskv_wrapper_test.go +++ b/tests/diskv_wrapper_test.go @@ -9,23 +9,45 @@ import ( var ( disk *diskv.Diskv = diskv_wrapper.Init("disk_test") - version string = "v1" + version string = "v1-test" key string = "versionkeytest" data string = "this is string of data" - path string = diskv_wrapper.GetKeyVersion(disk, version, key) + path string = diskv_wrapper.WritePath(disk, version, key) ) func write_disk_version(t *testing.T) { err := diskv_wrapper.WriteVersion(disk, version) if err != nil { - t.Errorf("Write version error") + t.Errorf("Expected can write version") } } func write_data_string_version(t *testing.T) { err := diskv_wrapper.Write(disk, path, data) if err != nil { - t.Errorf("Write data error") + t.Errorf("Expected can write data") + } +} + +func TestUsingNew(t *testing.T) { + vKeyForNew := "version" + + diskv, err := diskv_wrapper.New("disk_test_new") + if err != nil { + t.Errorf("Expected can create new diskv: %v", err) + } + + diskv_wrapper.WriteVersion(diskv.Disk, "v1") + + versionUseOnNew := diskv_wrapper.VersionUse(diskv.Disk) + + if versionUseOnNew != "v1" { + t.Errorf("Expected key is %v", vKeyForNew) + } + + errorErase := diskv.Disk.EraseAll() + if errorErase != nil { + t.Errorf("Expected erasing %v data: %v", vKeyForNew, errorErase) } } diff --git a/transformer.go b/transformer.go index 7fde248..2af3404 100644 --- a/transformer.go +++ b/transformer.go @@ -6,6 +6,7 @@ import ( "github.com/peterbourgon/diskv/v3" ) +// AdvanceTransform to transform key it provided for *diskv and used as Option in *diskv func AdvanceTransform(key string) *diskv.PathKey { path := strings.Split(key, "/") last := len(path) - 1 @@ -15,6 +16,7 @@ func AdvanceTransform(key string) *diskv.PathKey { } } +// InverseTransform will inverse all written keys on disk. Used by *diskv to determine all keys on disk. func InverseTransform(pathKey *diskv.PathKey) (key string) { var path, file []string file = []string{pathKey.FileName[:len(pathKey.FileName)]} diff --git a/writer.go b/writer.go index 2e22f0e..b54ae94 100644 --- a/writer.go +++ b/writer.go @@ -1,9 +1,12 @@ package diskv_wrapper import ( + "strings" + "github.com/peterbourgon/diskv/v3" ) +// WriteVersion used to write version file under disk. It contain version info that used in current. func WriteVersion(d *diskv.Diskv, version string) error { err := d.WriteString("version", version) if err != nil { @@ -12,6 +15,14 @@ func WriteVersion(d *diskv.Diskv, version string) error { return err } +// WritePath will return a key to be written under `vx` folder +func WritePath(d *diskv.Diskv, version string, key string) string { + path := strings.Join([]string{version, key}, "/") + + return path +} + +// Write is function to fill the data under keys func Write(d *diskv.Diskv, key string, value string) error { err := d.WriteString(key, value) if err != nil {