-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
158 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
*.local | ||
config.toml | ||
fs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package kv | ||
|
||
import ( | ||
"context" | ||
"os" | ||
"path" | ||
|
||
"go.uber.org/zap" | ||
"golang.org/x/sync/errgroup" | ||
) | ||
|
||
type FSStore struct { | ||
fsPath string | ||
} | ||
|
||
func NewFSStore(logger *zap.Logger, fsPath string) *FSStore { | ||
return &FSStore{ | ||
fsPath: fsPath, | ||
} | ||
} | ||
|
||
func (s *FSStore) Start(ctx context.Context, g *errgroup.Group) error { | ||
return nil | ||
} | ||
|
||
func (s *FSStore) Get(ctx context.Context, ns Namespace, key string) (string, error) { | ||
path := path.Join(s.fsPath, string(ns), key) | ||
|
||
b, err := os.ReadFile(path) | ||
if err != nil { | ||
if os.IsNotExist(err) { | ||
err := s.Touch(path) | ||
if err != nil { | ||
return "", err | ||
} | ||
return "", nil | ||
} | ||
return "", err | ||
} | ||
return string(b), nil | ||
} | ||
|
||
func (s *FSStore) Set(ctx context.Context, ns Namespace, key string, value string) error { | ||
path := path.Join(s.fsPath, string(ns), key) | ||
|
||
err := s.Touch(path) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
err = os.WriteFile(path, []byte(value), 0644) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (s *FSStore) Touch(fpath string) error { | ||
baseDir := path.Dir(fpath) | ||
if err := os.MkdirAll(baseDir, 0755); err != nil { | ||
return err | ||
} | ||
|
||
file, err := os.OpenFile(fpath, os.O_RDONLY|os.O_CREATE, 0644) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
err = file.Close() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package kv | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
. "github.com/smartystreets/goconvey/convey" | ||
|
||
"go.uber.org/zap" | ||
) | ||
|
||
func TestSpec(t *testing.T) { | ||
Convey("Given a fresh context", t, func() { | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
defer cancel() | ||
test_path := t.TempDir() | ||
logger := zap.NewExample() | ||
store := NewFSStore(logger, test_path) | ||
ns := Namespace("namespace1") | ||
|
||
Convey("Values can be set", func() { | ||
key := "key1" | ||
value := "value1" | ||
err := store.Set(ctx, ns, key, value) | ||
So(err, ShouldEqual, nil) | ||
}) | ||
Convey("When reading a set value", func() { | ||
key := "key2" | ||
value := "value1" | ||
store.Set(ctx, ns, key, value) | ||
returned, err := store.Get(ctx, ns, key) | ||
Convey("The application does not error", func() { | ||
So(err, ShouldEqual, nil) | ||
}) | ||
Convey("The value read is equal to the value set", func() { | ||
So(returned, ShouldEqual, value) | ||
}) | ||
}) | ||
Convey("When reading a value that was not set", func() { | ||
key := "key3" | ||
returned, err := store.Get(ctx, ns, key) | ||
Convey("The application does not error", func() { | ||
So(err, ShouldEqual, nil) | ||
}) | ||
Convey("The value is empty", func() { | ||
So(returned, ShouldEqual, "") | ||
}) | ||
}) | ||
}) | ||
} |