Skip to content

Commit e39667f

Browse files
committed
refactor: define Transaction interface
1 parent a4772ae commit e39667f

File tree

1 file changed

+31
-20
lines changed

1 file changed

+31
-20
lines changed

transaction/transaction.go

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
)
1313

1414
// Start creates $VOLTPATH/trx/lock directory
15-
func Start() (*Transaction, error) {
15+
func Start() (Transaction, error) {
1616
os.MkdirAll(pathutil.TrxDir(), 0755)
1717
lockDir := filepath.Join(pathutil.TrxDir(), "lock")
1818
if err := os.Mkdir(lockDir, 0755); err != nil {
@@ -22,7 +22,30 @@ func Start() (*Transaction, error) {
2222
if err != nil {
2323
return nil, errors.Wrap(err, "could not allocate a new transaction ID")
2424
}
25-
return &Transaction{trxID: trxID}, nil
25+
return &transaction{id: trxID}, nil
26+
}
27+
28+
// Transaction provides transaction methods
29+
type Transaction interface {
30+
// Done renames "lock" directory to "{trxid}" directory
31+
Done() error
32+
33+
// ID returns transaction ID
34+
ID() TrxID
35+
}
36+
37+
type transaction struct {
38+
id TrxID
39+
}
40+
41+
func (trx *transaction) ID() TrxID {
42+
return trx.id
43+
}
44+
45+
func (trx *transaction) Done() error {
46+
lockDir := filepath.Join(pathutil.TrxDir(), "lock")
47+
trxIDDir := filepath.Join(pathutil.TrxDir(), string(trx.id))
48+
return os.Rename(lockDir, trxIDDir)
2649
}
2750

2851
// genNewTrxID gets unallocated transaction ID looking $VOLTPATH/trx/ directory
@@ -52,7 +75,7 @@ func genNewTrxID() (_ TrxID, result error) {
5275
if maxID == nil {
5376
return TrxID("1"), nil // no transaction ID directory
5477
}
55-
return maxID.Add(1)
78+
return maxID.Inc()
5679
}
5780

5881
func greaterThan(a, b string) bool {
@@ -78,27 +101,15 @@ func isTrxDirName(name string) bool {
78101
// transaction log file.
79102
type TrxID []byte
80103

81-
// Add adds n to transaction ID
82-
func (tid *TrxID) Add(n int) (TrxID, error) {
104+
// Inc increments transaction ID
105+
func (tid *TrxID) Inc() (TrxID, error) {
83106
newID, err := strconv.ParseUint(string(*tid), 10, 32)
84107
if err != nil {
85108
return nil, err
86109
}
87-
if newID+uint64(n) < newID {
110+
if newID+uint64(1) < newID {
88111
// TODO: compute in string?
89-
return nil, errors.Errorf("%d + %d causes overflow", newID, n)
112+
return nil, errors.Errorf("%d + %d causes overflow", newID, 1)
90113
}
91-
return TrxID(strconv.FormatUint(newID+uint64(n), 10)), nil
92-
}
93-
94-
// Transaction provides transaction methods
95-
type Transaction struct {
96-
trxID TrxID
97-
}
98-
99-
// Done rename "lock" directory to "{trxid}" directory
100-
func (trx *Transaction) Done() error {
101-
lockDir := filepath.Join(pathutil.TrxDir(), "lock")
102-
trxIDDir := filepath.Join(pathutil.TrxDir(), string(trx.trxID))
103-
return os.Rename(lockDir, trxIDDir)
114+
return TrxID(strconv.FormatUint(newID+uint64(1), 10)), nil
104115
}

0 commit comments

Comments
 (0)