Skip to content

Commit

Permalink
fix some function method
Browse files Browse the repository at this point in the history
  • Loading branch information
mkhuda committed Sep 26, 2021
1 parent f709e35 commit c6b8473
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 11 deletions.
31 changes: 25 additions & 6 deletions diskv_wrapper.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,43 @@
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,
InverseTransform: InverseTransform,
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
}
2 changes: 1 addition & 1 deletion example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
30 changes: 26 additions & 4 deletions tests/diskv_wrapper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down
2 changes: 2 additions & 0 deletions transformer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)]}
Expand Down
11 changes: 11 additions & 0 deletions writer.go
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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 {
Expand Down

0 comments on commit c6b8473

Please sign in to comment.