Skip to content

Commit f2541eb

Browse files
committed
add transaction command
1 parent 63b7506 commit f2541eb

12 files changed

+357
-105
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@
44
/vendor
55
/.coverage
66
/coverage.html
7-
.test*
7+
.test*
8+
/play.go

README.md

+6-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
an interactive [buntdb](https://github.com/tidwall/buntdb) shell client
44

5-
![build](https://github.com/Sora233/buntdb-cli/workflows/.github/workflows/ci.yml/badge.svg) [![Coverage Status](https://coveralls.io/repos/github/Sora233/buntdb-cli/badge.svg)](https://coveralls.io/github/Sora233/buntdb-cli)
5+
![build](https://github.com/Sora233/buntdb-cli/workflows/.github/workflows/ci.yml/badge.svg)
6+
[![Coverage Status](https://coveralls.io/repos/github/Sora233/buntdb-cli/badge.svg)](https://coveralls.io/github/Sora233/buntdb-cli)
67

78

89
----
@@ -30,6 +31,10 @@ go get -u -v github.com/Sora233/buntdb-cli
3031
* set
3132
* del
3233
* ttl
34+
* rbegin (begin a readonly transaction)
35+
* rwbegin (begin a read/write transaction)
36+
* commit
37+
* rollback
3338
* show
3439
* keys
3540
* use

cli/completer.go

+18
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package cli
22

33
import (
44
"fmt"
5+
"github.com/Sora233/buntdb-cli/db"
56
"github.com/c-bata/go-prompt"
67
"strings"
78
)
@@ -33,6 +34,18 @@ func cmdCompleter(cmd string) []prompt.Suggest {
3334
{Text: "use", Description: "change db"},
3435
{Text: "exit", Description: "exit buntdb shell client"},
3536
}
37+
tx, _ := db.GetCurrentTransaction()
38+
if tx == nil {
39+
cmds = append(cmds,
40+
prompt.Suggest{Text: "rbegin", Description: "open a readonly transaction"},
41+
prompt.Suggest{Text: "rwbegin", Description: "open a read/write transaction"},
42+
)
43+
} else {
44+
cmds = append(cmds,
45+
prompt.Suggest{Text: "rollback", Description: "rollback a transaction"},
46+
prompt.Suggest{Text: "commit", Description: "commit a transaction"},
47+
)
48+
}
3649
return prompt.FilterHasPrefix(cmds, cmd, true)
3750
}
3851

@@ -44,13 +57,18 @@ func optionCompleter(cmd string, args []string) []prompt.Suggest {
4457
case "get":
4558
case "set":
4659
case "del":
60+
case "ttl":
4761
case "show":
4862
return []prompt.Suggest{
4963
{Text: "index"},
5064
{Text: "db"},
5165
}
5266
case "keys":
5367
case "use":
68+
case "rbegin":
69+
case "rwbegin":
70+
case "rollback":
71+
case "commit":
5472
default:
5573
return []prompt.Suggest{}
5674
}

cli/executor.go

+51-8
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,15 @@ package cli
22

33
import (
44
"fmt"
5+
"github.com/Sora233/buntdb-cli/db"
56
"github.com/alecthomas/kong"
6-
"runtime"
77
"strings"
88
)
99

1010
func BuntdbExecutor(s string) {
1111
s = strings.TrimSpace(s)
12-
if s == "" {
12+
if s == "" || s == "exit" {
1313
return
14-
} else if s == "exit" {
15-
fmt.Println("bye")
16-
runtime.Goexit()
1714
}
1815
args := ArgSplit(s)
1916

@@ -34,8 +31,54 @@ func BuntdbExecutor(s string) {
3431
fmt.Printf("ERR: %v\n", err)
3532
return
3633
}
37-
err = ctx.Run()
38-
if err != nil {
39-
fmt.Printf("ERR: %v\n", err)
34+
cmd := ctx.Selected().Name
35+
if cmd == "rwbegin" || cmd == "rbegin" || cmd == "rollback" || cmd == "commit" {
36+
err = ctx.Run()
37+
if err != nil {
38+
fmt.Printf("ERR: %v\n", err)
39+
}
40+
return
41+
}
42+
tx, rw := db.GetCurrentTransaction()
43+
if ctx.Selected().Name == "use" {
44+
fmt.Printf("tx %v", tx)
45+
err = ctx.Run(tx)
46+
if err != nil {
47+
fmt.Printf("ERR: %v\n", err)
48+
}
49+
return
50+
}
51+
if tx != nil {
52+
if Debug {
53+
fmt.Printf("got current %v transaction\n", db.RWDescribe(rw))
54+
}
55+
err = ctx.Run(tx)
56+
if err != nil {
57+
fmt.Printf("ERR: %v\n", err)
58+
return
59+
}
60+
} else {
61+
if Debug {
62+
fmt.Printf("no transaction, create a rw transaction\n")
63+
}
64+
tx, err := db.Begin(true)
65+
if err != nil {
66+
fmt.Printf("ERR: %v\n", err)
67+
return
68+
}
69+
defer func() {
70+
if Debug {
71+
fmt.Printf("transaction commit\n")
72+
}
73+
err := db.Commit()
74+
if err != nil {
75+
fmt.Printf("ERR: commit error %v\n", err)
76+
}
77+
}()
78+
err = ctx.Run(tx)
79+
if err != nil {
80+
fmt.Printf("ERR: %v\n", err)
81+
return
82+
}
4083
}
4184
}

cli/executor_test.go

+47
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,53 @@ func TestBuntdbExecutor(t *testing.T) {
6161
return nil
6262
})
6363
BuntdbExecutor("del b")
64+
BuntdbExecutor("del a")
65+
66+
BuntdbExecutor("rwbegin")
67+
BuntdbExecutor("set x y")
68+
BuntdbExecutor("set y x")
69+
BuntdbExecutor("commit")
70+
71+
bd.View(func(tx *buntdb.Tx) error {
72+
val, err := tx.Get("x")
73+
assert.Nil(t, err)
74+
assert.Equal(t, "y", val)
75+
val, err = tx.Get("y")
76+
assert.Nil(t, err)
77+
assert.Equal(t, "x", val)
78+
return nil
79+
})
80+
81+
BuntdbExecutor("rwbegin")
82+
BuntdbExecutor("del x")
83+
BuntdbExecutor("del y")
84+
BuntdbExecutor("set x xxx")
85+
BuntdbExecutor("rollback")
86+
bd.View(func(tx *buntdb.Tx) error {
87+
val, err := tx.Get("x")
88+
assert.Nil(t, err)
89+
assert.Equal(t, "y", val)
90+
val, err = tx.Get("y")
91+
assert.Nil(t, err)
92+
assert.Equal(t, "x", val)
93+
return nil
94+
})
95+
96+
BuntdbExecutor("rbegin")
97+
BuntdbExecutor("del x")
98+
BuntdbExecutor("del y")
99+
BuntdbExecutor("commit")
100+
BuntdbExecutor("rollback")
101+
bd.View(func(tx *buntdb.Tx) error {
102+
val, err := tx.Get("x")
103+
assert.Nil(t, err)
104+
assert.Equal(t, "y", val)
105+
val, err = tx.Get("y")
106+
assert.Nil(t, err)
107+
assert.Equal(t, "x", val)
108+
return nil
109+
})
110+
64111
BuntdbExecutor("use testcli-2")
65112
BuntdbExecutor("use -c testcli-2")
66113
assert.Equal(t, db.GetDbPath(), "testcli-2")

0 commit comments

Comments
 (0)