Repository navigation
Expand file tree
/
Copy pathStoreWALTest.java
More file actions
201 lines (154 loc) · 4.77 KB
/
Copy pathStoreWALTest.java
File metadata and controls
201 lines (154 loc) · 4.77 KB
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package org.mapdb;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import java.io.File;
import java.io.IOError;
import java.io.IOException;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;
import static org.junit.Assert.*;
public class StoreWALTest extends StoreDirectTest<StoreWAL>{
Volume.Factory fac;
@Before public void init(){
fac = Volume.fileFactory(f,0,false, 0L,CC.VOLUME_SLICE_SHIFT,0);
super.init();
}
@Override
protected StoreWAL openEngine() {
return new StoreWAL(fac);
}
@Override
boolean canRollback() {
return true;
}
@Test
public void delete_files_after_close2(){
File f = UtilsTest.tempDbFile();
File phys = new File(f.getPath()+StoreDirect.DATA_FILE_EXT);
File wal = new File(f.getPath()+StoreWAL.TRANS_LOG_FILE_EXT);
DB db = DBMaker.newFileDB(f).deleteFilesAfterClose().make();
db.getHashMap("test").put("aa","bb");
db.commit();
assertTrue(f.exists());
assertTrue(phys.exists());
assertTrue(wal.exists());
db.getHashMap("test").put("a12a","bb");
assertTrue(wal.exists());
db.close();
assertFalse(f.exists());
assertFalse(phys.exists());
assertFalse(wal.exists());
}
@Test public void header_index_ver() throws IOException {
e.put(new byte[10000],Serializer.BYTE_ARRAY_NOSIZE);
e.commit();
e.close();
//increment store version
File index = new File(f.getPath()+StoreWAL.TRANS_LOG_FILE_EXT);
Volume v = Volume.volumeForFile(index,true,false,0,CC.VOLUME_SLICE_SHIFT,0);
v.ensureAvailable(100);
v.putInt(0,StoreWAL.HEADER);
v.putUnsignedShort(4,StoreDirect.STORE_VERSION+1);
v.putLong(8,StoreWAL.LOG_SEAL);
v.putInt(80,1);
v.sync();
v.close();
try{
e = openEngine();
fail();
}catch(IOError e){
Throwable e2 = e;
while (e2 instanceof IOError){
e2 = e2.getCause();
}
assertTrue(e2.getMessage().contains("version"));
}
}
@Test public void replay_good_log() throws IOException {
final AtomicBoolean replay = new AtomicBoolean(true);
StoreWAL wal = new StoreWAL(fac){
@Override
protected void replayLogFile() {
if(replay.get())
super.replayLogFile();
else
throw new IllegalAccessError();
}
};
DB db = new DB(wal);
Map m = db.getHashMap("map");
//fill map and commit
int max = (int) 1e5;
for(int i=0;i<max;i++){
m.put(i,i);
}
wal.commit();
//fill log, commit but do not replay
replay.set(false);
try {
for (int i = max; i < max*2; i++) {
m.put(i, i);
}
wal.commit();
fail("Should throw an error");
}catch(IllegalAccessError e){
}
wal.log.close();
wal.phys.close();
wal.index.close();
//now reopen and check content
wal = new StoreWAL(fac);
db = new DB(wal);
m = db.getHashMap("map");
assertEquals(max*2, m.size());
for(int i=0;i<max;i++){
assertEquals(i,m.get(i));
}
}
@Ignore
@Test public void discard_corrupted_log() throws IOException {
final AtomicBoolean replay = new AtomicBoolean(true);
StoreWAL wal = new StoreWAL(fac){
@Override
protected void replayLogFile() {
if(replay.get())
super.replayLogFile();
else throw new IllegalAccessError();
}
};
DB db = new DB(wal);
Map m = db.getHashMap("map");
//fill map and commit
int max = (int) 1e5;
for(int i=0;i<max;i++){
m.put(i,i);
}
wal.commit();
//fill log, commit but do not replay
replay.set(false);
try {
for (int i = max; i < max*2; i++) {
m.put(i, i);
}
wal.commit();
fail("Should throw an error");
}catch(IllegalAccessError e){
}
//corrupt log randomly
wal.log.putLong(1000,111111111L);
wal.log.putLong(2000,111111111L);
wal.log.sync();
wal.log.close();
wal.phys.close();
wal.index.close();
//now reopen and check content
wal = new StoreWAL(fac);
db = new DB(wal);
m = db.getHashMap("map");
assertEquals(max, m.size());
for(int i=0;i<max;i++){
assertEquals(i,m.get(i));
}
}
}