Skip to content

Commit

Permalink
Merge pull request #48 from ipfs/feat/get-size
Browse files Browse the repository at this point in the history
add GetSize function
  • Loading branch information
Stebalien committed Oct 5, 2018
2 parents d2629a2 + 1df2c1e commit 91e8764
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 4 deletions.
12 changes: 12 additions & 0 deletions flatfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
52 changes: 48 additions & 4 deletions flatfs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}

Expand All @@ -282,13 +282,57 @@ 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")
}
}

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()
Expand Down

0 comments on commit 91e8764

Please sign in to comment.