@@ -12,7 +12,7 @@ import (
12
12
)
13
13
14
14
// Start creates $VOLTPATH/trx/lock directory
15
- func Start () (* Transaction , error ) {
15
+ func Start () (Transaction , error ) {
16
16
os .MkdirAll (pathutil .TrxDir (), 0755 )
17
17
lockDir := filepath .Join (pathutil .TrxDir (), "lock" )
18
18
if err := os .Mkdir (lockDir , 0755 ); err != nil {
@@ -22,7 +22,30 @@ func Start() (*Transaction, error) {
22
22
if err != nil {
23
23
return nil , errors .Wrap (err , "could not allocate a new transaction ID" )
24
24
}
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 )
26
49
}
27
50
28
51
// genNewTrxID gets unallocated transaction ID looking $VOLTPATH/trx/ directory
@@ -52,7 +75,7 @@ func genNewTrxID() (_ TrxID, result error) {
52
75
if maxID == nil {
53
76
return TrxID ("1" ), nil // no transaction ID directory
54
77
}
55
- return maxID .Add ( 1 )
78
+ return maxID .Inc ( )
56
79
}
57
80
58
81
func greaterThan (a , b string ) bool {
@@ -78,27 +101,15 @@ func isTrxDirName(name string) bool {
78
101
// transaction log file.
79
102
type TrxID []byte
80
103
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 ) {
83
106
newID , err := strconv .ParseUint (string (* tid ), 10 , 32 )
84
107
if err != nil {
85
108
return nil , err
86
109
}
87
- if newID + uint64 (n ) < newID {
110
+ if newID + uint64 (1 ) < newID {
88
111
// 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 )
90
113
}
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
104
115
}
0 commit comments