From 135b2edb9b57034c9b5f5e83749aca7d11fe5c1c Mon Sep 17 00:00:00 2001 From: Steven Allen Date: Thu, 4 Oct 2018 16:26:36 -0700 Subject: [PATCH 1/2] testing: sweet sanity --- flatfs_test.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/flatfs_test.go b/flatfs_test.go index a16867e..9665802 100644 --- a/flatfs_test.go +++ b/flatfs_test.go @@ -256,8 +256,8 @@ func testHasNotFound(dirFunc mkShardFunc, t *testing.T) { if err != nil { t.Fatalf("Has fail: %v\n", err) } - if g, e := found, false; g != e { - t.Fatalf("wrong Has: %v != %v", g, e) + if found { + t.Fatal("Has should have returned false") } } @@ -282,8 +282,8 @@ func testHasFound(dirFunc mkShardFunc, t *testing.T) { if err != nil { t.Fatalf("Has fail: %v\n", err) } - if g, e := found, true; g != e { - t.Fatalf("wrong Has: %v != %v", g, e) + if !found { + t.Fatal("Has should have returned true") } } From 1df2c1e01578eea8211d380ced7aefed9c47fe4d Mon Sep 17 00:00:00 2001 From: Steven Allen Date: Thu, 4 Oct 2018 16:32:08 -0700 Subject: [PATCH 2/2] add GetSize function --- flatfs.go | 12 ++++++++++++ flatfs_test.go | 44 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) diff --git a/flatfs.go b/flatfs.go index cea61bd..a608e21 100644 --- a/flatfs.go +++ b/flatfs.go @@ -577,6 +577,18 @@ func (fs *Datastore) Has(key datastore.Key) (exists bool, err error) { } } +func (fs *Datastore) GetSize(key datastore.Key) (size int, err error) { + _, path := fs.encode(key) + switch s, err := os.Stat(path); { + case err == nil: + return int(s.Size()), nil + case os.IsNotExist(err): + return -1, datastore.ErrNotFound + default: + return -1, err + } +} + // Delete removes a key/value from the Datastore. Please read // the Put() explanation about the handling of concurrent write // operations to the same key. diff --git a/flatfs_test.go b/flatfs_test.go index 9665802..f5d1168 100644 --- a/flatfs_test.go +++ b/flatfs_test.go @@ -289,6 +289,50 @@ func testHasFound(dirFunc mkShardFunc, t *testing.T) { func TestHasFound(t *testing.T) { tryAllShardFuncs(t, testHasFound) } +func testGetSizeFound(dirFunc mkShardFunc, t *testing.T) { + temp, cleanup := tempdir(t) + defer cleanup() + + fs, err := flatfs.CreateOrOpen(temp, dirFunc(2), false) + if err != nil { + t.Fatalf("New fail: %v\n", err) + } + defer fs.Close() + + _, err = fs.GetSize(datastore.NewKey("quux")) + if err != datastore.ErrNotFound { + t.Fatalf("GetSize should have returned ErrNotFound, got: %v\n", err) + } +} + +func TestGetSizeFound(t *testing.T) { tryAllShardFuncs(t, testGetSizeFound) } + +func testGetSizeNotFound(dirFunc mkShardFunc, t *testing.T) { + temp, cleanup := tempdir(t) + defer cleanup() + + fs, err := flatfs.CreateOrOpen(temp, dirFunc(2), false) + if err != nil { + t.Fatalf("New fail: %v\n", err) + } + defer fs.Close() + + err = fs.Put(datastore.NewKey("quux"), []byte("foobar")) + if err != nil { + t.Fatalf("Put fail: %v\n", err) + } + + size, err := fs.GetSize(datastore.NewKey("quux")) + if err != nil { + t.Fatalf("GetSize failed with: %v\n", err) + } + if size != len("foobar") { + t.Fatalf("GetSize returned wrong size: got %d, expected %d", size, len("foobar")) + } +} + +func TestGetSizeNotFound(t *testing.T) { tryAllShardFuncs(t, testGetSizeNotFound) } + func testDeleteNotFound(dirFunc mkShardFunc, t *testing.T) { temp, cleanup := tempdir(t) defer cleanup()