Repository navigation
Expand file tree
/
Copy pathStoreDirectTest.java
More file actions
464 lines (364 loc) · 14.5 KB
/
Copy pathStoreDirectTest.java
File metadata and controls
464 lines (364 loc) · 14.5 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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
package org.mapdb;
import org.junit.Test;
import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import static org.junit.Assert.*;
import static org.mapdb.StoreDirect.*;
public class StoreDirectTest <E extends StoreDirect> extends EngineTest<E>{
@Override boolean canRollback(){return false;}
File f = UtilsTest.tempDbFile();
Volume.Factory fac = Volume.fileFactory(false,0,f, 0L,false);
static final long IO_RECID = StoreDirect.IO_FREE_RECID+32;
@Override protected E openEngine() {
return (E) new StoreDirect(fac);
}
int countIndexRecords(){
int ret = 0;
for(int pos = StoreDirect.IO_USER_START; pos<e.indexSize; pos+=8){
long val = e.index.getLong(pos);
if(val!=0 && val != StoreDirect.MASK_ARCHIVE) ret++;
}
return ret;
}
List<Long> getLongStack(long ioRecid){
ArrayList<Long> ret =new ArrayList<Long>();
long pagePhysid = e.index.getLong(ioRecid) & StoreDirect.MASK_OFFSET;
long pageOffset = e.index.getLong(ioRecid) >>>48;
while(pagePhysid!=0){
while(pageOffset>=8){
//System.out.println(pagePhysid + " - "+pageOffset);
final Long l = e.phys.getSixLong(pagePhysid + pageOffset);
pageOffset-=6;
ret.add(l);
}
//System.out.println(ret);
//read location of previous page
pagePhysid = e.phys.getLong(pagePhysid) & StoreDirect.MASK_OFFSET;
pageOffset = (e.phys.getLong(pagePhysid) >>>48) - 6;
}
return ret;
}
@Test
public void phys_append_alloc(){
e.structuralLock.lock();
long[] ret = e.physAllocate(100,true,false);
long expected = 100L<<48 | 16L;
assertArrayEquals(new long[]{expected}, ret);
}
@Test
public void phys_append_alloc_link2(){
e.structuralLock.lock();
long[] ret = e.physAllocate(100 + MAX_REC_SIZE,true,false);
long exp1 = MASK_LINKED |((long)MAX_REC_SIZE)<<48 | 16L;
long exp2 = 108L<<48 | (16L+MAX_REC_SIZE+1);
assertArrayEquals(new long[]{exp1, exp2}, ret);
}
@Test
public void phys_append_alloc_link3(){
e.structuralLock.lock();
long[] ret = e.physAllocate(100 + MAX_REC_SIZE*2,true,false);
long exp1 = MASK_LINKED | ((long)MAX_REC_SIZE)<<48 | 16L;
long exp2 = MASK_LINKED | ((long)MAX_REC_SIZE)<<48 | (16L+MAX_REC_SIZE+1);
long exp3 = ((long)116)<<48 | (16L+MAX_REC_SIZE*2+2);
assertArrayEquals(new long[]{exp1, exp2, exp3}, ret);
}
@Test public void second_rec_pos_round_to_16(){
e.structuralLock.lock();
long[] ret= e.physAllocate(1,true,false);
assertArrayEquals(new long[]{1L<<48|16L},ret);
ret= e.physAllocate(1,true,false);
assertArrayEquals(new long[]{1L<<48|32L},ret);
}
@Test public void test_index_record_delete(){
long recid = e.put(1000L, Serializer.LONG);
e.commit();
assertEquals(1, countIndexRecords());
e.delete(recid,Serializer.LONG);
e.commit();
assertEquals(0, countIndexRecords());
e.structuralLock.lock();
assertEquals(recid*8 + StoreDirect.IO_USER_START, e.freeIoRecidTake(true));
}
@Test public void test_size2IoList(){
long old= StoreDirect.IO_FREE_RECID;
for(int size=1;size<= StoreDirect.MAX_REC_SIZE;size++){
long ioListRecid = size2ListIoRecid(size);
assertTrue(ioListRecid> StoreDirect.IO_FREE_RECID);
assertTrue(ioListRecid< StoreDirect.IO_USER_START);
assertEquals(ioListRecid,old+(size%16==1?8:0));
old=ioListRecid;
}
}
@Test public void test_index_record_delete_and_reusef(){
long recid = e.put(1000L, Serializer.LONG);
e.commit();
assertEquals(1, countIndexRecords());
assertEquals(e.LAST_RESERVED_RECID+1, recid);
e.delete(recid,Serializer.LONG);
e.commit();
assertEquals(0, countIndexRecords());
long recid2 = e.put(1000L, Serializer.LONG);
e.commit();
//test that previously deleted index slot was reused
assertEquals(recid, recid2);
assertEquals(1, countIndexRecords());
assertTrue(0!=e.index.getLong(recid*8+ StoreDirect.IO_USER_START));
}
@Test public void test_index_record_delete_and_reuse_large(){
final long MAX = 10;
List<Long> recids= new ArrayList<Long>();
for(int i = 0;i<MAX;i++){
recids.add(e.put(0L, Serializer.LONG));
}
for(long recid:recids){
e.delete(recid,Serializer.LONG);
}
//now allocate again second recid list
List<Long> recids2= new ArrayList<Long>();
for(int i = 0;i<MAX;i++){
recids2.add(e.put(0L, Serializer.LONG));
}
//second list should be reverse of first, as Linked Offset List is LIFO
Collections.reverse(recids);
assertEquals(recids, recids2);
}
@Test public void test_phys_record_reused(){
final long recid = e.put(1L, Serializer.LONG);
assertEquals((Long)1L, e.get(recid, Serializer.LONG));
final long physRecid = e.index.getLong(recid*8+ StoreDirect.IO_USER_START);
e.delete(recid, Serializer.LONG);
final long recid2 = e.put(1L, Serializer.LONG);
assertEquals((Long)1L, e.get(recid2, Serializer.LONG));
assertEquals(recid, recid2);
assertEquals(physRecid, e.index.getLong(recid*8+ StoreDirect.IO_USER_START));
}
@Test public void test_index_stores_record_size() throws IOException {
final long recid = e.put(1, Serializer.INTEGER);
e.commit();
assertEquals(4, e.index.getUnsignedShort(recid * 8+ StoreDirect.IO_USER_START));
assertEquals(Integer.valueOf(1), e.get(recid, Serializer.INTEGER));
e.update(recid, 1L, Serializer.LONG);
e.commit();
assertEquals(8, e.index.getUnsignedShort(recid * 8+ StoreDirect.IO_USER_START));
assertEquals(Long.valueOf(1), e.get(recid, Serializer.LONG));
}
@Test public void test_long_stack_puts_record_offset_into_index() throws IOException {
e.structuralLock.lock();
e.longStackPut(IO_RECID, 1,false);
e.commit();
assertEquals(8,
e.index.getLong(IO_RECID)>>>48);
}
@Test public void test_long_stack_put_take() throws IOException {
e.structuralLock.lock();
final long max = 150;
for(long i=1;i<max;i++){
e.longStackPut(IO_RECID, i,false);
}
for(long i = max-1;i>0;i--){
assertEquals(i, e.longStackTake(IO_RECID,false));
}
assertEquals(0, getLongStack(IO_RECID).size());
}
@Test public void test_long_stack_put_take_simple() throws IOException {
e.structuralLock.lock();
e.longStackPut(IO_RECID, 111,false);
assertEquals(111L, e.longStackTake(IO_RECID,false));
}
@Test public void test_basic_long_stack() throws IOException {
//dirty hack to make sure we have lock
e.structuralLock.lock();
final long max = 150;
ArrayList<Long> list = new ArrayList<Long>();
for(long i=1;i<max;i++){
e.longStackPut(IO_RECID, i,false);
list.add(i);
}
Collections.reverse(list);
e.commit();
assertEquals(list, getLongStack(IO_RECID));
for(long i =max-1;i>=1;i--){
assertEquals(i, e.longStackTake(IO_RECID,false));
}
}
@Test public void test_large_long_stack() throws IOException {
//dirty hack to make sure we have lock
e.structuralLock.lock();
final long max = 15000;
ArrayList<Long> list = new ArrayList<Long>();
for(long i=1;i<max;i++){
e.longStackPut(IO_RECID, i,false);
list.add(i);
}
Collections.reverse(list);
e.commit();
assertEquals(list, getLongStack(IO_RECID));
for(long i =max-1;i>=1;i--){
assertEquals(i, e.longStackTake(IO_RECID,false));
}
}
@Test public void test_basic_long_stack_no_commit() throws IOException {
//dirty hack to make sure we have lock
e.structuralLock.lock();
final long max = 150;
for(long i=1;i<max;i++){
e.longStackPut(IO_RECID, i,false);
}
for(long i =max-1;i>=1;i--){
assertEquals(i, e.longStackTake(IO_RECID,false));
}
}
@Test public void test_large_long_stack_no_commit() throws IOException {
//dirty hack to make sure we have lock
e.structuralLock.lock();
final long max = 15000;
for(long i=1;i<max;i++){
e.longStackPut(IO_RECID, i,false);
}
for(long i =max-1;i>=1;i--){
assertEquals(i, e.longStackTake(IO_RECID,false));
}
}
@Test public void long_stack_page_created_after_put() throws IOException {
e.structuralLock.lock();
e.longStackPut(IO_RECID, 111,false);
e.commit();
long pageId = e.index.getLong(IO_RECID);
assertEquals(8, pageId>>>48);
pageId = pageId & StoreDirect.MASK_OFFSET;
assertEquals(16L, pageId);
assertEquals(LONG_STACK_PREF_SIZE, e.phys.getLong(pageId)>>>48);
assertEquals(0, e.phys.getLong(pageId)& StoreDirect.MASK_OFFSET);
assertEquals(111, e.phys.getSixLong(pageId + 8));
}
@Test public void long_stack_put_five() throws IOException {
e.structuralLock.lock();
e.longStackPut(IO_RECID, 111,false);
e.longStackPut(IO_RECID, 112,false);
e.longStackPut(IO_RECID, 113,false);
e.longStackPut(IO_RECID, 114,false);
e.longStackPut(IO_RECID, 115,false);
e.commit();
long pageId = e.index.getLong(IO_RECID);
assertEquals(8+6*4, pageId>>>48);
pageId = pageId & StoreDirect.MASK_OFFSET;
assertEquals(16L, pageId);
assertEquals(LONG_STACK_PREF_SIZE, e.phys.getLong(pageId)>>>48);
assertEquals(0, e.phys.getLong(pageId)&MASK_OFFSET);
assertEquals(111, e.phys.getSixLong(pageId + 8));
assertEquals(112, e.phys.getSixLong(pageId + 14));
assertEquals(113, e.phys.getSixLong(pageId + 20));
assertEquals(114, e.phys.getSixLong(pageId + 26));
assertEquals(115, e.phys.getSixLong(pageId + 32));
}
@Test public void long_stack_page_deleted_after_take() throws IOException {
e.structuralLock.lock();
e.longStackPut(IO_RECID, 111,false);
e.commit();
assertEquals(111L, e.longStackTake(IO_RECID,false));
e.commit();
assertEquals(0L, e.index.getLong(IO_RECID));
}
@Test public void long_stack_page_overflow() throws IOException {
e.structuralLock.lock();
//fill page until near overflow
for(int i=0;i< StoreDirect.LONG_STACK_PREF_COUNT;i++){
e.longStackPut(IO_RECID, 1000L+i,false);
}
e.commit();
//check content
long pageId = e.index.getLong(IO_RECID);
assertEquals(StoreDirect.LONG_STACK_PREF_SIZE-6, pageId>>>48);
pageId = pageId & StoreDirect.MASK_OFFSET;
assertEquals(16L, pageId);
assertEquals(StoreDirect.LONG_STACK_PREF_SIZE, e.phys.getLong(pageId)>>>48);
for(int i=0;i< StoreDirect.LONG_STACK_PREF_COUNT;i++){
assertEquals(1000L+i, e.phys.getSixLong(pageId + 8 + i * 6));
}
//add one more item, this will trigger page overflow
e.longStackPut(IO_RECID, 11L,false);
e.commit();
//check page overflowed
pageId = e.index.getLong(IO_RECID);
assertEquals(8, pageId>>>48);
pageId = pageId & StoreDirect.MASK_OFFSET;
assertEquals(16L+ StoreDirect.LONG_STACK_PREF_SIZE, pageId);
assertEquals(LONG_STACK_PREF_SIZE, e.phys.getLong(pageId)>>>48);
assertEquals(16L, e.phys.getLong(pageId)& StoreDirect.MASK_OFFSET);
assertEquals(11L, e.phys.getSixLong(pageId + 8));
}
@Test public void test_constants(){
assertTrue(StoreDirect.LONG_STACK_PREF_SIZE%16==0);
}
@Test public void delete_files_after_close(){
File f = UtilsTest.tempDbFile();
File phys = new File(f.getPath()+StoreDirect.DATA_FILE_EXT);
DB db = DBMaker.newFileDB(f).transactionDisable().deleteFilesAfterClose().make();
db.getHashMap("test").put("aa","bb");
db.commit();
assertTrue(f.exists());
assertTrue(phys.exists());
db.close();
assertFalse(f.exists());
assertFalse(phys.exists());
}
@Test public void freeSpaceWorks(){
long oldFree = e.getFreeSize();
long recid = e.put(new byte[10000],Serializer.BYTE_ARRAY_NOSIZE);
e.commit();
assertEquals(oldFree, e.getFreeSize());
e.delete(recid,Serializer.BYTE_ARRAY_NOSIZE);
assertEquals(oldFree+10000,e.getFreeSize());
e.commit();
assertEquals(oldFree+10000,e.getFreeSize());
}
@Test public void prealloc(){
long recid = e.preallocate();
assertNull(e.get(recid,UtilsTest.FAIL));
e.commit();
assertNull(e.get(recid,UtilsTest.FAIL));
}
@Test public void header_index_inc() throws IOException {
e.put(new byte[10000],Serializer.BYTE_ARRAY_NOSIZE);
e.commit();
e.close();
//increment store version
Volume v = Volume.volumeForFile(f,true,false,0,false);
v.putUnsignedShort(4,StoreDirect.STORE_VERSION+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 header_phys_inc() throws IOException {
e.put(new byte[10000],Serializer.BYTE_ARRAY_NOSIZE);
e.commit();
e.close();
//increment store version
File phys = new File(f.getPath()+StoreDirect.DATA_FILE_EXT);
Volume v = Volume.volumeForFile(phys,true,false,0,false);
v.putUnsignedShort(4,StoreDirect.STORE_VERSION+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"));
}
}
}