Skip to content

Commit 0c59fa9

Browse files
committed
feat: implement new simple transaction algorithm
If `os.Mkdir("$VOLTPATH/trx/lock", 0755)` returns nil error, It means it could start transaction successfully.
1 parent 5bd8d6a commit 0c59fa9

File tree

10 files changed

+166
-96
lines changed

10 files changed

+166
-96
lines changed

dsl/execute.go

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ package dsl
22

33
import (
44
"context"
5-
"errors"
65

6+
"github.com/pkg/errors"
7+
"github.com/vim-volt/volt/dsl/op"
78
"github.com/vim-volt/volt/dsl/types"
89
)
910

@@ -21,17 +22,17 @@ const (
2122

2223
// Execute executes given expr with given ctx.
2324
func Execute(ctx context.Context, expr *types.Expr) (val types.Value, rollback func(), err error) {
24-
ctx = context.WithValue(ctx, CtxTrxIDKey, genNewTrxID())
25-
if ctx.Value(CtxLockJSONKey) == nil {
26-
return nil, func() {}, errors.New("no lock.json key in context")
27-
}
28-
if ctx.Value(CtxConfigKey) == nil {
29-
return nil, func() {}, errors.New("no config.toml key in context")
25+
for _, st := range []struct {
26+
key CtxKeyType
27+
name string
28+
}{
29+
{CtxLockJSONKey, "lock.json"},
30+
{CtxConfigKey, "config.toml"},
31+
{CtxTrxIDKey, "transaction ID"},
32+
} {
33+
if ctx.Value(st.key) == nil {
34+
return nil, op.NoRollback, errors.Errorf("no %s key in context", st.name)
35+
}
3036
}
3137
return expr.Eval(ctx)
3238
}
33-
34-
func genNewTrxID() types.TrxID {
35-
// TODO: Get unallocated transaction ID looking $VOLTPATH/trx/ directory
36-
return types.TrxID(0)
37-
}

dsl/types/trxid.go

Lines changed: 0 additions & 6 deletions
This file was deleted.

migration/lockjson.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,17 @@ Description
3232
To suppress this, running this command simply reads and writes migrated structure to lock.json.`
3333
}
3434

35-
func (*lockjsonMigrater) Migrate(lockJSON *lockjson.LockJSON, cfg *config.Config) error {
35+
func (*lockjsonMigrater) Migrate(lockJSON *lockjson.LockJSON, cfg *config.Config) (result error) {
3636
// Begin transaction
37-
err := transaction.Create()
37+
trx, err := transaction.Start()
3838
if err != nil {
3939
return err
4040
}
41-
defer transaction.Remove()
41+
defer func() {
42+
if err := trx.Done(); err != nil {
43+
result = err
44+
}
45+
}()
4246

4347
// Write to lock.json
4448
err = lockJSON.Write()

migration/plugconf-config-func.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
package migration
22

33
import (
4-
"errors"
54
"io/ioutil"
65
"os"
76
"path/filepath"
87
"strings"
98

9+
"github.com/pkg/errors"
1010
"github.com/vim-volt/volt/config"
1111
"github.com/vim-volt/volt/lockjson"
1212
"github.com/vim-volt/volt/logger"
@@ -40,7 +40,7 @@ Description
4040
All plugconf files are replaced with new contents.`
4141
}
4242

43-
func (*plugconfConfigMigrater) Migrate(lockJSON *lockjson.LockJSON, cfg *config.Config) error {
43+
func (*plugconfConfigMigrater) Migrate(lockJSON *lockjson.LockJSON, cfg *config.Config) (result error) {
4444
results, parseErr := plugconf.ParseMultiPlugconf(lockJSON.Repos)
4545
if parseErr.HasErrs() {
4646
logger.Error("Please fix the following errors before migration:")
@@ -84,17 +84,20 @@ func (*plugconfConfigMigrater) Migrate(lockJSON *lockjson.LockJSON, cfg *config.
8484
}
8585

8686
// Begin transaction
87-
err := transaction.Create()
87+
trx, err := transaction.Start()
8888
if err != nil {
8989
return err
9090
}
91-
defer transaction.Remove()
91+
defer func() {
92+
if err := trx.Done(); err != nil {
93+
result = err
94+
}
95+
}()
9296

9397
// Build ~/.vim/pack/volt dir
9498
err = builder.Build(false, lockJSON, cfg)
9599
if err != nil {
96-
return errors.New("could not build " + pathutil.VimVoltDir() + ": " + err.Error())
100+
return errors.Wrap(err, "could not build "+pathutil.VimVoltDir())
97101
}
98-
99102
return nil
100103
}

pathutil/pathutil.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,9 +169,9 @@ func ConfigTOML() string {
169169
return filepath.Join(VoltPath(), "config.toml")
170170
}
171171

172-
// TrxLock returns fullpath of "$HOME/volt/trx.lock".
173-
func TrxLock() string {
174-
return filepath.Join(VoltPath(), "trx.lock")
172+
// TrxDir returns fullpath of "$HOME/volt/trx".
173+
func TrxDir() string {
174+
return filepath.Join(VoltPath(), "trx")
175175
}
176176

177177
// TempDir returns fullpath of "$HOME/tmp".

subcmd/build.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ Description
5353
return fs
5454
}
5555

56-
func (cmd *buildCmd) Run(runctx *RunContext) *Error {
56+
func (cmd *buildCmd) Run(runctx *RunContext) (cmdErr *Error) {
5757
// Parse args
5858
fs := cmd.FlagSet()
5959
fs.Parse(runctx.Args)
@@ -62,12 +62,16 @@ func (cmd *buildCmd) Run(runctx *RunContext) *Error {
6262
}
6363

6464
// Begin transaction
65-
err := transaction.Create()
65+
trx, err := transaction.Start()
6666
if err != nil {
6767
logger.Error()
6868
return &Error{Code: 11, Msg: "Failed to begin transaction: " + err.Error()}
6969
}
70-
defer transaction.Remove()
70+
defer func() {
71+
if err := trx.Done(); err != nil {
72+
cmdErr = &Error{Code: 13, Msg: "Failed to end transaction: " + err.Error()}
73+
}
74+
}()
7175

7276
err = builder.Build(cmd.full, runctx.LockJSON, runctx.Config)
7377
if err != nil {

subcmd/get.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ func (cmd *getCmd) getReposPathList(args []string, lockJSON *lockjson.LockJSON)
180180
return reposPathList, nil
181181
}
182182

183-
func (cmd *getCmd) doGet(reposPathList []pathutil.ReposPath, lockJSON *lockjson.LockJSON, cfg *config.Config) error {
183+
func (cmd *getCmd) doGet(reposPathList []pathutil.ReposPath, lockJSON *lockjson.LockJSON, cfg *config.Config) (result error) {
184184
// Find matching profile
185185
profile, err := lockJSON.Profiles.FindByName(lockJSON.CurrentProfileName)
186186
if err != nil {
@@ -190,11 +190,15 @@ func (cmd *getCmd) doGet(reposPathList []pathutil.ReposPath, lockJSON *lockjson.
190190
}
191191

192192
// Begin transaction
193-
err = transaction.Create()
193+
trx, err := transaction.Start()
194194
if err != nil {
195195
return err
196196
}
197-
defer transaction.Remove()
197+
defer func() {
198+
if err := trx.Done(); err != nil {
199+
result = err
200+
}
201+
}()
198202

199203
done := make(chan getParallelResult, len(reposPathList))
200204
getCount := 0

subcmd/profile.go

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ func (cmd *profileCmd) parseArgs(args []string) ([]string, error) {
153153
return fs.Args(), nil
154154
}
155155

156-
func (cmd *profileCmd) doSet(runctx *RunContext) error {
156+
func (cmd *profileCmd) doSet(runctx *RunContext) (result error) {
157157
args := runctx.Args
158158
lockJSON := runctx.LockJSON
159159

@@ -190,11 +190,15 @@ func (cmd *profileCmd) doSet(runctx *RunContext) error {
190190
}
191191

192192
// Begin transaction
193-
err := transaction.Create()
193+
trx, err := transaction.Start()
194194
if err != nil {
195195
return err
196196
}
197-
defer transaction.Remove()
197+
defer func() {
198+
if err := trx.Done(); err != nil {
199+
result = err
200+
}
201+
}()
198202

199203
// Set profile name
200204
lockJSON.CurrentProfileName = profileName
@@ -254,7 +258,7 @@ func (cmd *profileCmd) doList(runctx *RunContext) error {
254258
`, runctx.LockJSON)
255259
}
256260

257-
func (cmd *profileCmd) doNew(runctx *RunContext) error {
261+
func (cmd *profileCmd) doNew(runctx *RunContext) (result error) {
258262
args := runctx.Args
259263
lockJSON := runctx.LockJSON
260264

@@ -272,11 +276,15 @@ func (cmd *profileCmd) doNew(runctx *RunContext) error {
272276
}
273277

274278
// Begin transaction
275-
err = transaction.Create()
279+
trx, err := transaction.Start()
276280
if err != nil {
277281
return err
278282
}
279-
defer transaction.Remove()
283+
defer func() {
284+
if err := trx.Done(); err != nil {
285+
result = err
286+
}
287+
}()
280288

281289
// Add profile
282290
lockJSON.Profiles = append(lockJSON.Profiles, lockjson.Profile{
@@ -295,7 +303,7 @@ func (cmd *profileCmd) doNew(runctx *RunContext) error {
295303
return nil
296304
}
297305

298-
func (cmd *profileCmd) doDestroy(runctx *RunContext) error {
306+
func (cmd *profileCmd) doDestroy(runctx *RunContext) (result error) {
299307
args := runctx.Args
300308
lockJSON := runctx.LockJSON
301309

@@ -306,11 +314,15 @@ func (cmd *profileCmd) doDestroy(runctx *RunContext) error {
306314
}
307315

308316
// Begin transaction
309-
err := transaction.Create()
317+
trx, err := transaction.Start()
310318
if err != nil {
311319
return err
312320
}
313-
defer transaction.Remove()
321+
defer func() {
322+
if err := trx.Done(); err != nil {
323+
result = err
324+
}
325+
}()
314326

315327
var merr *multierror.Error
316328
for i := range args {
@@ -350,7 +362,7 @@ func (cmd *profileCmd) doDestroy(runctx *RunContext) error {
350362
return merr.ErrorOrNil()
351363
}
352364

353-
func (cmd *profileCmd) doRename(runctx *RunContext) error {
365+
func (cmd *profileCmd) doRename(runctx *RunContext) (result error) {
354366
args := runctx.Args
355367
lockJSON := runctx.LockJSON
356368

@@ -374,11 +386,15 @@ func (cmd *profileCmd) doRename(runctx *RunContext) error {
374386
}
375387

376388
// Begin transaction
377-
err := transaction.Create()
389+
trx, err := transaction.Start()
378390
if err != nil {
379391
return err
380392
}
381-
defer transaction.Remove()
393+
defer func() {
394+
if err := trx.Done(); err != nil {
395+
result = err
396+
}
397+
}()
382398

383399
// Rename profile names
384400
lockJSON.Profiles[index].Name = newName
@@ -515,19 +531,23 @@ func (cmd *profileCmd) parseAddArgs(lockJSON *lockjson.LockJSON, subCmd string,
515531
}
516532

517533
// Run modifyProfile and write modified structure to lock.json
518-
func (*profileCmd) transactProfile(lockJSON *lockjson.LockJSON, profileName string, modifyProfile func(*lockjson.Profile)) (*lockjson.LockJSON, error) {
534+
func (*profileCmd) transactProfile(lockJSON *lockjson.LockJSON, profileName string, modifyProfile func(*lockjson.Profile)) (resultLockJSON *lockjson.LockJSON, result error) {
519535
// Return error if profiles[]/name does not match profileName
520536
profile, err := lockJSON.Profiles.FindByName(profileName)
521537
if err != nil {
522538
return nil, err
523539
}
524540

525541
// Begin transaction
526-
err = transaction.Create()
542+
trx, err := transaction.Start()
527543
if err != nil {
528544
return nil, err
529545
}
530-
defer transaction.Remove()
546+
defer func() {
547+
if err := trx.Done(); err != nil {
548+
result = err
549+
}
550+
}()
531551

532552
modifyProfile(profile)
533553

subcmd/rm.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,13 +108,17 @@ func (cmd *rmCmd) parseArgs(args []string) ([]pathutil.ReposPath, error) {
108108
return reposPathList, nil
109109
}
110110

111-
func (cmd *rmCmd) doRemove(reposPathList []pathutil.ReposPath, lockJSON *lockjson.LockJSON) error {
111+
func (cmd *rmCmd) doRemove(reposPathList []pathutil.ReposPath, lockJSON *lockjson.LockJSON) (result error) {
112112
// Begin transaction
113-
err := transaction.Create()
113+
trx, err := transaction.Start()
114114
if err != nil {
115115
return err
116116
}
117-
defer transaction.Remove()
117+
defer func() {
118+
if err := trx.Done(); err != nil {
119+
result = err
120+
}
121+
}()
118122

119123
// Check if specified plugins are depended by some plugins
120124
for _, reposPath := range reposPathList {

0 commit comments

Comments
 (0)