-
Notifications
You must be signed in to change notification settings - Fork 3
/
transactor.go
110 lines (88 loc) · 2.69 KB
/
transactor.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package transactors
import (
"context"
"fmt"
"sync"
"sync/atomic"
"github.com/christianalexander/kvdb"
"github.com/christianalexander/kvdb/stores"
"github.com/sirupsen/logrus"
)
// A Transactor is able to orchestrate transactions.
type Transactor interface {
Execute(ctx context.Context, command kvdb.Command) error
Begin(ctx context.Context) (transactionID int64, err error)
Commit(ctx context.Context) error
Rollback(ctx context.Context) error
}
type transactor struct {
store stores.Store
mu sync.Mutex
transactionCommands map[int64][]kvdb.Command
latestTransactionID int64
writer stores.Writer
}
// New creates a new Transactor.
func New(store stores.Store, writer stores.Writer) Transactor {
return &transactor{
store: store,
transactionCommands: make(map[int64][]kvdb.Command),
writer: writer,
}
}
func (t *transactor) Execute(ctx context.Context, command kvdb.Command) error {
txID, ok := ctx.Value(stores.ContextKeyTransactionID).(int64)
if command.ShouldAutoTransact() && (!ok || txID == 0) {
logrus.Printf("Assigned txID %d", txID)
txID = atomic.AddInt64(&t.latestTransactionID, 1)
ctx = context.WithValue(ctx, stores.ContextKeyTransactionID, txID)
defer t.Commit(ctx)
}
err := command.Execute(ctx)
t.mu.Lock()
t.transactionCommands[txID] = append(t.transactionCommands[txID], command)
t.mu.Unlock()
return err
}
func (t *transactor) Begin(ctx context.Context) (transactionID int64, err error) {
existingID, ok := ctx.Value(stores.ContextKeyTransactionID).(int64)
if ok && existingID != 0 {
return 0, fmt.Errorf("can not start a transaction within the existing transaction '%d'", existingID)
}
return atomic.AddInt64(&t.latestTransactionID, 1), nil
}
func (t *transactor) Commit(ctx context.Context) error {
txID, ok := ctx.Value(stores.ContextKeyTransactionID).(int64)
if !ok || txID == 0 {
return fmt.Errorf("can not commit without a transaction")
}
t.store.Release(ctx)
t.mu.Lock()
delete(t.transactionCommands, txID)
t.mu.Unlock()
if t.writer != nil {
t.writer.Write(ctx, stores.Record{
Kind: stores.RecordKindCommit,
TransactionID: txID,
})
}
return nil
}
func (t *transactor) Rollback(ctx context.Context) error {
txID, ok := ctx.Value(stores.ContextKeyTransactionID).(int64)
if !ok || txID == 0 {
return fmt.Errorf("can not rollback without a transaction")
}
commands, ok := t.transactionCommands[txID]
if !ok {
return fmt.Errorf("can not roll back transaction without command history")
}
for i := len(commands) - 1; i >= 0; i-- {
commands[i].Undo(ctx)
}
t.store.Release(ctx)
t.mu.Lock()
delete(t.transactionCommands, txID)
t.mu.Unlock()
return nil
}