-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmanifest_txn_test.go
164 lines (133 loc) · 3.5 KB
/
manifest_txn_test.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package bitcask
import (
"errors"
"os"
"testing"
"github.com./stretchr/testify/assert"
)
func setupManifestTxn(t *testing.T) (*ManifestTxn, func()) {
dir := "./test_bitcask_db"
_ = os.RemoveAll(dir)
assert.Nil(t, os.MkdirAll(dir, os.ModePerm))
manifest, err := NewManifest(dir)
assert.Nil(t, err)
txn, err := manifest.NewTxn()
assert.Nil(t, err)
return txn, func() {
os.RemoveAll(dir)
}
}
func TestManifestTxn_Commit(t *testing.T) {
txn, closer := setupManifestTxn(t)
defer closer()
manifest := txn.manifest
dir := manifest.dir
wal3, _ := NewWal(WalPath(dir, 3), 3, -1)
wal4, _ := NewWal(WalPath(dir, 4), 4, -1)
wal5, _ := NewWal(WalPath(dir, 5), 5, -1)
wal6, _ := NewWal(WalPath(dir, 6), 6, -1)
wal7, _ := NewWal(WalPath(dir, 7), 7, -1)
edit1 := &ManifestEdit{
addFiles: []LogFile{
{fid: 3, wal: wal3},
{fid: 4, wal: wal4},
{fid: 5, wal: wal5},
},
deleteFiles: nil,
hasNextFid: true,
nextFid: 6,
freeBytes: make(map[uint64]uint64),
}
edit1.freeBytes[2] = 123
// apply
txn.Apply(edit1)
// check
assert.False(t, txn.aborted)
assert.False(t, txn.committed)
assert.False(t, txn.IsDone())
assert.Equal(t, wal3, txn.ToWal(3))
assert.Equal(t, wal4, txn.ToWal(4))
assert.Equal(t, wal5, txn.ToWal(5))
assert.Equal(t, txn.NextFid(), uint64(6))
assert.Equal(t, wal3, manifest.ToWal(3))
assert.Equal(t, wal4, manifest.ToWal(4))
assert.Equal(t, wal5, manifest.ToWal(5))
assert.Equal(t, manifest.NextFid(), uint64(6))
// commit
edit2 := &ManifestEdit{
addFiles: []LogFile{
{fid: 6, wal: wal6},
{fid: 7, wal: wal7},
},
deleteFiles: []LogFile{
{fid: 3, wal: wal3},
{fid: 4, wal: wal4},
{fid: 5, wal: wal5},
},
hasNextFid: true,
nextFid: 8,
freeBytes: make(map[uint64]uint64),
}
edit2.freeBytes[2] = 123
assert.Nil(t, txn.Commit(edit2))
// check
assert.True(t, txn.committed)
assert.False(t, txn.aborted)
assert.True(t, txn.IsDone())
assert.Nil(t, manifest.ToWal(3))
assert.Nil(t, manifest.ToWal(4))
assert.Nil(t, manifest.ToWal(5))
assert.Equal(t, wal6, manifest.ToWal(6))
assert.Equal(t, wal7, manifest.ToWal(7))
assert.Equal(t, manifest.NextFid(), uint64(8))
// commit again
err := txn.Commit(nil)
assert.True(t, errors.Is(err, ErrCommittedManfiestTxn))
}
func TestManifestTxn_Abort(t *testing.T) {
txn, closer := setupManifestTxn(t)
defer closer()
manifest := txn.manifest
dir := manifest.dir
wal3, _ := NewWal(WalPath(dir, 3), 3, -1)
wal4, _ := NewWal(WalPath(dir, 4), 4, -1)
wal5, _ := NewWal(WalPath(dir, 5), 5, -1)
edit1 := &ManifestEdit{
addFiles: []LogFile{
{fid: 3, wal: wal3},
{fid: 4, wal: wal4},
{fid: 5, wal: wal5},
},
deleteFiles: nil,
hasNextFid: true,
nextFid: 6,
freeBytes: make(map[uint64]uint64),
}
edit1.freeBytes[2] = 123
// apply
txn.Apply(edit1)
// check
assert.False(t, txn.aborted)
assert.False(t, txn.committed)
assert.False(t, txn.IsDone())
assert.Equal(t, wal3, txn.ToWal(3))
assert.Equal(t, wal4, txn.ToWal(4))
assert.Equal(t, wal5, txn.ToWal(5))
assert.Equal(t, txn.NextFid(), uint64(6))
assert.Equal(t, wal3, manifest.ToWal(3))
assert.Equal(t, wal4, manifest.ToWal(4))
assert.Equal(t, wal5, manifest.ToWal(5))
assert.Equal(t, manifest.NextFid(), uint64(6))
// abort
txn.Abort()
// check
assert.True(t, txn.aborted)
assert.False(t, txn.committed)
assert.True(t, txn.IsDone())
assert.Nil(t, manifest.ToWal(3))
assert.Nil(t, manifest.ToWal(4))
assert.Nil(t, manifest.ToWal(5))
assert.Equal(t, manifest.NextFid(), uint64(3))
// abort again
txn.Abort()
}