Skip to content

Commit

Permalink
store: Introduce DeleteBucket()
Browse files Browse the repository at this point in the history
Signed-off-by: Simarpreet Singh <simar@linux.com>
  • Loading branch information
simar7 committed Dec 12, 2019
1 parent a837bd5 commit 75014a5
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 0 deletions.
14 changes: 14 additions & 0 deletions bbolt/bbolt.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,20 @@ func (s Store) Delete(input types.DeleteItemInput) error {
})
}

func (s Store) DeleteBucket(input types.DeleteBucketInput) error {
if err := util.CheckBucketName(input.BucketName); err != nil {
return err
}

return s.db.Update(func(tx *bolt.Tx) error {
var b *bolt.Bucket
if b = tx.Bucket([]byte(s.rbc.Name)); b == nil {
return ErrBucketNotFound
}
return b.DeleteBucket([]byte(input.BucketName))
})
}

func (s Store) Scan(input types.ScanInput) (types.ScanOutput, error) {
if err := util.CheckBucketName(input.BucketName); err != nil {
return types.ScanOutput{}, err
Expand Down
54 changes: 54 additions & 0 deletions bbolt/bbolt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -382,3 +382,57 @@ func TestStore_Info(t *testing.T) {
assert.Equal(t, "gokvbbolt", actualInfo.Name)
assert.Equal(t, "32 KiB", h.IBytes(uint64(actualInfo.Size)))
}

func TestStore_DeleteBucket(t *testing.T) {
s, f, err := setupStore()
defer func() {
_ = f.Close()
_ = os.RemoveAll(f.Name())
}()
assert.NoError(t, err)

// set
assert.NoError(t, s.Set(types.SetItemInput{
Key: "foo",
Value: "bar",
BucketName: "subbucket",
}))

// delete bucket
assert.NoError(t, s.DeleteBucket(types.DeleteBucketInput{
BucketName: "subbucket"},
))

// verify
var actualValue string
found, err := s.Get(types.GetItemInput{
BucketName: "subbucket",
Key: "foo",
Value: &actualValue,
})
assert.Equal(t, ErrBucketNotFound, err)
assert.False(t, found)
assert.Empty(t, actualValue)

t.Run("sad path, empty input bucket name", func(t *testing.T) {
s, f, err := setupStore()
defer func() {
_ = f.Close()
_ = os.RemoveAll(f.Name())
}()
assert.NoError(t, err)
assert.Equal(t, util.ErrEmptyBucketName, s.DeleteBucket(types.DeleteBucketInput{}))
})

t.Run("sad path, bucket not found", func(t *testing.T) {
s, f, err := setupStore()
defer func() {
_ = f.Close()
_ = os.RemoveAll(f.Name())
}()
assert.NoError(t, err)
assert.Equal(t, ErrBucketNotFound, s.DeleteBucket(types.DeleteBucketInput{
BucketName: "badbucket",
}))
})
}
1 change: 1 addition & 0 deletions store.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ type Store interface {
BatchSet(input types.BatchSetItemInput) error
Get(input types.GetItemInput) (found bool, err error)
Delete(input types.DeleteItemInput) error
DeleteBucket(input types.DeleteBucketInput) error
Close() error
Scan(input types.ScanInput) (types.ScanOutput, error)
Info() (types.StoreInfo, error)
Expand Down
4 changes: 4 additions & 0 deletions types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ type DeleteItemInput struct {
Key string
}

type DeleteBucketInput struct {
BucketName string
}

type ScanInput struct {
BucketName string
}
Expand Down

0 comments on commit 75014a5

Please sign in to comment.