Skip to content

Commit

Permalink
impl dbsize command
Browse files Browse the repository at this point in the history
  • Loading branch information
Sora233 committed Feb 1, 2022
1 parent a735175 commit 323fac9
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 9 deletions.
10 changes: 4 additions & 6 deletions cli/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,11 @@ func BuntdbExecutor(s string) {
return
case any:
tx, _ := db.GetCurrentTransaction()
if cmd == "use" {
err = ctx.Run(tx)
if err != nil {
fmt.Printf("ERR: %v\n", err)
}
return
err = ctx.Run(tx)
if err != nil {
fmt.Printf("ERR: %v\n", err)
}
return
case nonNil:
tx, rw, closeTx := db.GetCurrentOrNewTransaction()
if Debug {
Expand Down
6 changes: 6 additions & 0 deletions cli/executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ func TestBuntdbExecutor(t *testing.T) {
assert.Equal(t, "b", val)
return nil
})
BuntdbExecutor("dbsize")
BuntdbExecutor("set a c")
bd.View(func(tx *buntdb.Tx) error {
val, err := tx.Get("a")
Expand Down Expand Up @@ -95,6 +96,7 @@ func TestBuntdbExecutor(t *testing.T) {
BuntdbExecutor("rwbegin")
BuntdbExecutor("set x y")
BuntdbExecutor("set y x")
BuntdbExecutor("dbsize")
BuntdbExecutor("commit")

bd.View(func(tx *buntdb.Tx) error {
Expand All @@ -111,6 +113,7 @@ func TestBuntdbExecutor(t *testing.T) {
BuntdbExecutor("del x")
BuntdbExecutor("del y")
BuntdbExecutor("set x xxx")
BuntdbExecutor("dbsize")
BuntdbExecutor("rollback")
bd.View(func(tx *buntdb.Tx) error {
val, err := tx.Get("x")
Expand All @@ -126,6 +129,7 @@ func TestBuntdbExecutor(t *testing.T) {
BuntdbExecutor("del x")
BuntdbExecutor("del y")
BuntdbExecutor("shrink")
BuntdbExecutor("dbsize")
BuntdbExecutor("save testcli_save")
_, err = os.Lstat("testcli_save")
assert.Nil(t, err)
Expand All @@ -140,6 +144,7 @@ func TestBuntdbExecutor(t *testing.T) {
assert.Equal(t, "x", val)
return nil
})
BuntdbExecutor("dbsize")
BuntdbExecutor("shrink")
BuntdbExecutor("set a xy")
BuntdbExecutor("save testcli_save")
Expand All @@ -156,6 +161,7 @@ func TestBuntdbExecutor(t *testing.T) {
BuntdbExecutor("use :memory:")
BuntdbExecutor("exit")
BuntdbExecutor("")
BuntdbExecutor("dbsize")

os.Remove("testcli_save")
os.Remove("testcli")
Expand Down
37 changes: 34 additions & 3 deletions cli/grammar.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"time"
)

var null = "<nil>"
const null = "<nil>"

type GetGrammar struct {
Key string `arg:"" help:"the key to get"`
Expand Down Expand Up @@ -262,13 +262,43 @@ type HelpGrammar struct {
}

func (h *HelpGrammar) Run(ctx *kong.Context) (err error) {
commands := "get\nset\ndel\nttl\nrbegin (begin a readonly transaction)\nrwbegin (begin a read/write transaction)\ncommit\nrollback\nshow\nkeys\nsearch\nuse\nshrink"
commands := "get\n" +
"set\n" +
"del\n" +
"ttl\n" +
"rbegin (begin a readonly transaction)\n" +
"rwbegin (begin a read/write transaction)\n" +
"commit\n" +
"rollback\n" +
"show\n" +
"keys\n" +
"search\n" +
"use\n" +
"shrink\n" +
"size"
_, err = fmt.Fprintln(ctx.Stdout,
"Commands available:\n----------\n"+commands+"\n----------\nFor more help, try running a command with the -h switch.",
)
return
}

type DBSizeGrammar struct {
}

func (h *DBSizeGrammar) Run(ctx *kong.Context, tx *buntdb.Tx) (err error) {
var count int
err = tx.Ascend("", func(key, value string) bool {
count++
return true
})
if err != nil {
fmt.Fprintf(ctx.Stdout, "ERR: Ascend error %v\n", err)
return
}
fmt.Fprintf(ctx.Stdout, "%v\n", count)
return nil
}

type Grammar struct {
Get GetGrammar `cmd:"" help:"get a value from key, return the value if key exists, or <nil> if non-exists."`
Set SetGrammar `cmd:"" help:"set a key-value [ttl], return the old value, or <nil> if old value doesn't exist."`
Expand All @@ -285,7 +315,8 @@ type Grammar struct {
Save SaveGrammar `cmd:"" help:"save the db to file"`
Drop DropGrammar `cmd:"" help:"drop command"`
Search SearchGrammar `cmd:"" help:"Search for a string contained in any values"`
Help HelpGrammar `cmd:""`
Help HelpGrammar `cmd:"" help:"show available command"`
DBSize DBSizeGrammar `cmd:"" name:"dbsize" help:"show db size"`
Exit bool `kong:"-"`
}

Expand Down

0 comments on commit 323fac9

Please sign in to comment.