Skip to content

Commit 323fac9

Browse files
committed
impl dbsize command
1 parent a735175 commit 323fac9

File tree

3 files changed

+44
-9
lines changed

3 files changed

+44
-9
lines changed

cli/executor.go

+4-6
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,11 @@ func BuntdbExecutor(s string) {
6262
return
6363
case any:
6464
tx, _ := db.GetCurrentTransaction()
65-
if cmd == "use" {
66-
err = ctx.Run(tx)
67-
if err != nil {
68-
fmt.Printf("ERR: %v\n", err)
69-
}
70-
return
65+
err = ctx.Run(tx)
66+
if err != nil {
67+
fmt.Printf("ERR: %v\n", err)
7168
}
69+
return
7270
case nonNil:
7371
tx, rw, closeTx := db.GetCurrentOrNewTransaction()
7472
if Debug {

cli/executor_test.go

+6
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ func TestBuntdbExecutor(t *testing.T) {
5353
assert.Equal(t, "b", val)
5454
return nil
5555
})
56+
BuntdbExecutor("dbsize")
5657
BuntdbExecutor("set a c")
5758
bd.View(func(tx *buntdb.Tx) error {
5859
val, err := tx.Get("a")
@@ -95,6 +96,7 @@ func TestBuntdbExecutor(t *testing.T) {
9596
BuntdbExecutor("rwbegin")
9697
BuntdbExecutor("set x y")
9798
BuntdbExecutor("set y x")
99+
BuntdbExecutor("dbsize")
98100
BuntdbExecutor("commit")
99101

100102
bd.View(func(tx *buntdb.Tx) error {
@@ -111,6 +113,7 @@ func TestBuntdbExecutor(t *testing.T) {
111113
BuntdbExecutor("del x")
112114
BuntdbExecutor("del y")
113115
BuntdbExecutor("set x xxx")
116+
BuntdbExecutor("dbsize")
114117
BuntdbExecutor("rollback")
115118
bd.View(func(tx *buntdb.Tx) error {
116119
val, err := tx.Get("x")
@@ -126,6 +129,7 @@ func TestBuntdbExecutor(t *testing.T) {
126129
BuntdbExecutor("del x")
127130
BuntdbExecutor("del y")
128131
BuntdbExecutor("shrink")
132+
BuntdbExecutor("dbsize")
129133
BuntdbExecutor("save testcli_save")
130134
_, err = os.Lstat("testcli_save")
131135
assert.Nil(t, err)
@@ -140,6 +144,7 @@ func TestBuntdbExecutor(t *testing.T) {
140144
assert.Equal(t, "x", val)
141145
return nil
142146
})
147+
BuntdbExecutor("dbsize")
143148
BuntdbExecutor("shrink")
144149
BuntdbExecutor("set a xy")
145150
BuntdbExecutor("save testcli_save")
@@ -156,6 +161,7 @@ func TestBuntdbExecutor(t *testing.T) {
156161
BuntdbExecutor("use :memory:")
157162
BuntdbExecutor("exit")
158163
BuntdbExecutor("")
164+
BuntdbExecutor("dbsize")
159165

160166
os.Remove("testcli_save")
161167
os.Remove("testcli")

cli/grammar.go

+34-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import (
1010
"time"
1111
)
1212

13-
var null = "<nil>"
13+
const null = "<nil>"
1414

1515
type GetGrammar struct {
1616
Key string `arg:"" help:"the key to get"`
@@ -262,13 +262,43 @@ type HelpGrammar struct {
262262
}
263263

264264
func (h *HelpGrammar) Run(ctx *kong.Context) (err error) {
265-
commands := "get\nset\ndel\nttl\nrbegin (begin a readonly transaction)\nrwbegin (begin a read/write transaction)\ncommit\nrollback\nshow\nkeys\nsearch\nuse\nshrink"
265+
commands := "get\n" +
266+
"set\n" +
267+
"del\n" +
268+
"ttl\n" +
269+
"rbegin (begin a readonly transaction)\n" +
270+
"rwbegin (begin a read/write transaction)\n" +
271+
"commit\n" +
272+
"rollback\n" +
273+
"show\n" +
274+
"keys\n" +
275+
"search\n" +
276+
"use\n" +
277+
"shrink\n" +
278+
"size"
266279
_, err = fmt.Fprintln(ctx.Stdout,
267280
"Commands available:\n----------\n"+commands+"\n----------\nFor more help, try running a command with the -h switch.",
268281
)
269282
return
270283
}
271284

285+
type DBSizeGrammar struct {
286+
}
287+
288+
func (h *DBSizeGrammar) Run(ctx *kong.Context, tx *buntdb.Tx) (err error) {
289+
var count int
290+
err = tx.Ascend("", func(key, value string) bool {
291+
count++
292+
return true
293+
})
294+
if err != nil {
295+
fmt.Fprintf(ctx.Stdout, "ERR: Ascend error %v\n", err)
296+
return
297+
}
298+
fmt.Fprintf(ctx.Stdout, "%v\n", count)
299+
return nil
300+
}
301+
272302
type Grammar struct {
273303
Get GetGrammar `cmd:"" help:"get a value from key, return the value if key exists, or <nil> if non-exists."`
274304
Set SetGrammar `cmd:"" help:"set a key-value [ttl], return the old value, or <nil> if old value doesn't exist."`
@@ -285,7 +315,8 @@ type Grammar struct {
285315
Save SaveGrammar `cmd:"" help:"save the db to file"`
286316
Drop DropGrammar `cmd:"" help:"drop command"`
287317
Search SearchGrammar `cmd:"" help:"Search for a string contained in any values"`
288-
Help HelpGrammar `cmd:""`
318+
Help HelpGrammar `cmd:"" help:"show available command"`
319+
DBSize DBSizeGrammar `cmd:"" name:"dbsize" help:"show db size"`
289320
Exit bool `kong:"-"`
290321
}
291322

0 commit comments

Comments
 (0)