Skip to content

Commit a837bd5

Browse files
committed
store: Introduce Info() for datastore information
Signed-off-by: Simarpreet Singh <simar@linux.com>
1 parent 17fa71e commit a837bd5

File tree

8 files changed

+50
-6
lines changed

8 files changed

+50
-6
lines changed

bbolt/bbolt.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package bbolt
22

33
import (
44
"errors"
5+
"os"
56

67
"github.com/simar7/gokv/types"
78

@@ -247,3 +248,15 @@ func (s Store) Scan(input types.ScanInput) (types.ScanOutput, error) {
247248
func (s Store) Close() error {
248249
return s.db.Close()
249250
}
251+
252+
func (s Store) Info() (types.StoreInfo, error) {
253+
f, err := os.Stat(s.dbPath)
254+
if err != nil {
255+
return types.StoreInfo{}, err
256+
}
257+
258+
return types.StoreInfo{
259+
Name: s.rbc.Name,
260+
Size: f.Size(),
261+
}, nil
262+
}

bbolt/bbolt_test.go

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,13 @@ import (
77
"sync"
88
"testing"
99

10-
"github.com/simar7/gokv/encoding"
11-
12-
"github.com/simar7/gokv/util"
13-
bolt "go.etcd.io/bbolt"
10+
h "github.com/dustin/go-humanize"
1411

12+
"github.com/simar7/gokv/encoding"
1513
"github.com/simar7/gokv/types"
16-
14+
"github.com/simar7/gokv/util"
1715
"github.com/stretchr/testify/assert"
16+
bolt "go.etcd.io/bbolt"
1817
)
1918

2019
func setupStoreWithCodec(codec encoding.Codec) (*Store, *os.File, error) {
@@ -369,3 +368,17 @@ func TestStore_Scan(t *testing.T) {
369368
assert.Empty(t, scanOut)
370369
})
371370
}
371+
372+
func TestStore_Info(t *testing.T) {
373+
s, f, err := setupStore()
374+
defer func() {
375+
_ = f.Close()
376+
_ = os.RemoveAll(f.Name())
377+
}()
378+
assert.NoError(t, err)
379+
380+
actualInfo, err := s.Info()
381+
assert.NoError(t, err)
382+
assert.Equal(t, "gokvbbolt", actualInfo.Name)
383+
assert.Equal(t, "32 KiB", h.IBytes(uint64(actualInfo.Size)))
384+
}

dynamodb/dynamodb.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ var (
2525

2626
var (
2727
ErrMissingTableName = errors.New("table name is required")
28-
//ErrNotImplemented = errors.New("function not implemented")
28+
ErrNotImplemented = errors.New("function not implemented")
2929
)
3030

3131
type Options struct {
@@ -245,3 +245,7 @@ func (s Store) Scan(input types.ScanInput) (types.ScanOutput, error) {
245245

246246
return scanOuput, nil
247247
}
248+
249+
func (s Store) Info() (types.StoreInfo, error) {
250+
return types.StoreInfo{}, ErrNotImplemented
251+
}

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ require (
66
github.com/alicebob/miniredis/v2 v2.11.0
77
github.com/aws/aws-sdk-go v1.25.31
88
github.com/davecgh/go-spew v1.1.0
9+
github.com/dustin/go-humanize v1.0.0
910
github.com/gomodule/redigo v2.0.0+incompatible
1011
github.com/stretchr/testify v1.4.0
1112
go.etcd.io/bbolt v1.3.3

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5P
99
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
1010
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
1111
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
12+
github.com/dustin/go-humanize v1.0.0 h1:VSnTsYCnlFHaM2/igO1h6X3HA71jcobQuxemgkq4zYo=
13+
github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk=
1214
github.com/gomodule/redigo v1.7.1-0.20190322064113-39e2c31b7ca3/go.mod h1:B4C85qUVwatsJoIUNIfCRsp7qO0iAmpGFZ4EELWSbC4=
1315
github.com/gomodule/redigo v2.0.0+incompatible h1:K/R+8tc58AaqLkqG2Ol3Qk+DR/TlNuhuh457pBFPtt0=
1416
github.com/gomodule/redigo v2.0.0+incompatible/go.mod h1:B4C85qUVwatsJoIUNIfCRsp7qO0iAmpGFZ4EELWSbC4=

redis/redis.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ var (
1717
ErrInvalidAddress = errors.New("invalid redis address specified")
1818
ErrRedisInitFailed = errors.New("redis initialization failed")
1919
ErrKeyNotFound = errors.New("key not found")
20+
ErrNotImplemented = errors.New("function not implemented")
2021
)
2122

2223
type Options struct {
@@ -243,3 +244,7 @@ func (s Store) getAllKeys() ([]string, error) {
243244
}
244245
return keys, nil
245246
}
247+
248+
func (s Store) Info() (types.StoreInfo, error) {
249+
return types.StoreInfo{}, ErrNotImplemented
250+
}

store.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ type Store interface {
99
Delete(input types.DeleteItemInput) error
1010
Close() error
1111
Scan(input types.ScanInput) (types.ScanOutput, error)
12+
Info() (types.StoreInfo, error)
1213
}

types/types.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,8 @@ type ScanOutput struct {
3131
Keys []string `dynamodbav:"k"`
3232
Values [][]byte `dynamodbav:"v"`
3333
}
34+
35+
type StoreInfo struct {
36+
Name string
37+
Size int64
38+
}

0 commit comments

Comments
 (0)