Repository navigation
Expand file tree
/
Copy pathEngineTest.java
More file actions
250 lines (191 loc) · 6.7 KB
/
Copy pathEngineTest.java
File metadata and controls
250 lines (191 loc) · 6.7 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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
package org.mapdb;
import org.junit.Before;
import org.junit.Test;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.mapdb.Serializer.BYTE_ARRAY_NOSIZE;
/**
* Tests contract of various implementations of Engine interface
*/
public abstract class EngineTest<ENGINE extends Engine>{
protected abstract ENGINE openEngine();
void reopen(){
if(!canReopen()) return;
e.close();
e=openEngine();
}
boolean canReopen(){return true;}
boolean canRollback(){return true;}
ENGINE e;
@Before public void init(){
e = openEngine();
}
@Test public void put_get(){
Long l = 11231203099090L;
long recid = e.put(l, Serializer.LONG);
assertEquals(l, e.get(recid, Serializer.LONG));
}
@Test public void put_reopen_get(){
if(!canReopen()) return;
Long l = 11231203099090L;
long recid = e.put(l, Serializer.LONG);
e.commit();
reopen();
assertEquals(l, e.get(recid, Serializer.LONG));
}
@Test public void put_get_large(){
byte[] b = new byte[(int) 1e6];
new Random().nextBytes(b);
long recid = e.put(b, Serializer.BYTE_ARRAY_NOSIZE);
assertArrayEquals(b, e.get(recid, Serializer.BYTE_ARRAY_NOSIZE));
}
@Test public void put_reopen_get_large(){
if(!canReopen()) return;
byte[] b = new byte[(int) 1e6];
new Random().nextBytes(b);
long recid = e.put(b, Serializer.BYTE_ARRAY_NOSIZE);
e.commit();
reopen();
assertArrayEquals(b, e.get(recid, Serializer.BYTE_ARRAY_NOSIZE));
}
@Test public void first_recid(){
assertEquals(Store.LAST_RESERVED_RECID+1, e.put(1,Serializer.INTEGER));
}
@Test public void compact0(){
Long v1 = 129031920390121423L;
Long v2 = 909090901290129990L;
Long v3 = 998898989L;
long recid1 = e.put(v1, Serializer.LONG);
long recid2 = e.put(v2, Serializer.LONG);
e.commit();
e.compact();
assertEquals(v1, e.get(recid1,Serializer.LONG));
assertEquals(v2, e.get(recid2,Serializer.LONG));
long recid3 = e.put(v3, Serializer.LONG);
assertEquals(v1, e.get(recid1,Serializer.LONG));
assertEquals(v2, e.get(recid2,Serializer.LONG));
assertEquals(v3, e.get(recid3,Serializer.LONG));
e.commit();
assertEquals(v1, e.get(recid1,Serializer.LONG));
assertEquals(v2, e.get(recid2,Serializer.LONG));
assertEquals(v3, e.get(recid3,Serializer.LONG));
}
@Test public void compact(){
Map<Long,Long> recids = new HashMap<Long, Long>();
for(Long l=0L;l<1000;l++){
recids.put(l,
e.put(l, Serializer.LONG));
}
e.commit();
e.compact();
for(Map.Entry<Long,Long> m:recids.entrySet()){
Long recid= m.getValue();
Long value = m.getKey();
assertEquals(value, e.get(recid, Serializer.LONG));
}
}
@Test public void compact2(){
Map<Long,Long> recids = new HashMap<Long, Long>();
for(Long l=0L;l<1000;l++){
recids.put(l,
e.put(l, Serializer.LONG));
}
e.commit();
e.compact();
for(Long l=1000L;l<2000;l++){
recids.put(l, e.put(l, Serializer.LONG));
}
for(Map.Entry<Long,Long> m:recids.entrySet()){
Long recid= m.getValue();
Long value = m.getKey();
assertEquals(value, e.get(recid, Serializer.LONG));
}
}
@Test public void compact_large_record(){
byte[] b = new byte[100000];
long recid = e.put(b, Serializer.BYTE_ARRAY_NOSIZE);
e.commit();
e.compact();
assertArrayEquals(b, e.get(recid, Serializer.BYTE_ARRAY_NOSIZE));
}
@Test public void testSetGet(){
long recid = e.put((long) 10000, Serializer.LONG);
Long s2 = e.get(recid, Serializer.LONG);
assertEquals(s2, Long.valueOf(10000));
}
@Test
public void large_record(){
byte[] b = new byte[100000];
Arrays.fill(b, (byte) 111);
long recid = e.put(b, BYTE_ARRAY_NOSIZE);
byte[] b2 = e.get(recid, BYTE_ARRAY_NOSIZE);
assertArrayEquals(b,b2);
}
@Test public void large_record_update(){
byte[] b = new byte[100000];
Arrays.fill(b, (byte) 111);
long recid = e.put(b, BYTE_ARRAY_NOSIZE);
Arrays.fill(b, (byte)222);
e.update(recid, b, BYTE_ARRAY_NOSIZE);
byte[] b2 = e.get(recid, BYTE_ARRAY_NOSIZE);
assertArrayEquals(b,b2);
e.commit();
reopen();
b2 = e.get(recid, BYTE_ARRAY_NOSIZE);
assertArrayEquals(b,b2);
}
@Test public void large_record_delete(){
byte[] b = new byte[100000];
Arrays.fill(b, (byte) 111);
long recid = e.put(b, BYTE_ARRAY_NOSIZE);
e.delete(recid, BYTE_ARRAY_NOSIZE);
}
@Test public void large_record_larger(){
byte[] b = new byte[10000000];
Arrays.fill(b, (byte) 111);
long recid = e.put(b, BYTE_ARRAY_NOSIZE);
byte[] b2 = e.get(recid, BYTE_ARRAY_NOSIZE);
assertArrayEquals(b,b2);
e.commit();
reopen();
b2 = e.get(recid, BYTE_ARRAY_NOSIZE);
assertArrayEquals(b,b2);
}
@Test public void test_store_reopen(){
long recid = e.put("aaa", Serializer.STRING_NOSIZE);
e.commit();
reopen();
String aaa = e.get(recid, Serializer.STRING_NOSIZE);
assertEquals("aaa",aaa);
}
@Test public void test_store_reopen_nocommit(){
long recid = e.put("aaa", Serializer.STRING_NOSIZE);
e.commit();
e.update(recid,"bbb",Serializer.STRING_NOSIZE);
reopen();
String expected = canRollback()&&canReopen()?"aaa":"bbb";
assertEquals(expected, e.get(recid, Serializer.STRING_NOSIZE));
}
@Test public void rollback(){
long recid = e.put("aaa", Serializer.STRING_NOSIZE);
e.commit();
e.update(recid, "bbb", Serializer.STRING_NOSIZE);
if(!canRollback())return;
e.rollback();
assertEquals("aaa",e.get(recid, Serializer.STRING_NOSIZE));
}
@Test public void rollback_reopen(){
long recid = e.put("aaa", Serializer.STRING_NOSIZE);
e.commit();
e.update(recid, "bbb", Serializer.STRING_NOSIZE);
if(!canRollback())return;
e.rollback();
assertEquals("aaa",e.get(recid, Serializer.STRING_NOSIZE));
reopen();
assertEquals("aaa",e.get(recid, Serializer.STRING_NOSIZE));
}
}