Skip to content

Commit 8c8d077

Browse files
committed
test closed range index as well
1 parent cac43d3 commit 8c8d077

File tree

1 file changed

+99
-48
lines changed

1 file changed

+99
-48
lines changed

dump/test/util/dump/MmapLongIdIndexTest.java

Lines changed: 99 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import java.util.Collection;
1111
import java.util.List;
1212
import java.util.Random;
13+
import java.util.function.BiFunction;
1314

1415
import org.assertj.core.util.Arrays;
1516
import org.junit.After;
@@ -59,6 +60,11 @@ public static void setUpTmpdir() throws IOException {
5960
private final int _dumpSize;
6061
private final long _negativeOffset;
6162

63+
private final BiFunction<Dump<Bean>, Field, MmapLongIdIndex<Bean>> _createOpenRangeIndex = //
64+
( dump, field ) -> MmapLongIdIndex.forOpenRange(dump, new FieldFieldAccessor(field), minId());
65+
private final BiFunction<Dump<Bean>, Field, MmapLongIdIndex<Bean>> _createClosedRangeIndex = //
66+
( dump, field ) -> MmapLongIdIndex.forClosedRange(dump, new FieldFieldAccessor(field), minId(), maxId());
67+
6268
public MmapLongIdIndexTest( Integer dumpSize ) {
6369
_dumpSize = dumpSize;
6470
_negativeOffset = -_dumpSize; // need to cater to generated negative indexed
@@ -131,84 +137,80 @@ public void testGetNumKeys() throws Exception {
131137
}
132138
}
133139

140+
134141
@Test
135-
public void testIntKeyIndex() throws Exception {
142+
public void testIntKey_OpenRangeIndex() throws Exception {
143+
testIntKeyIndex(_createOpenRangeIndex);
144+
}
145+
146+
@Test
147+
public void testIntKey_ClosedRangeIndex() throws Exception {
148+
testIntKeyIndex(_createClosedRangeIndex);
149+
}
150+
151+
private void testIntKeyIndex(BiFunction<Dump<Bean>, Field, MmapLongIdIndex<Bean>> createIndex) throws Exception {
136152
testIndex("_idInt", new TestConfiguration() {
137153

138154
@Override
139155
public Object createKey( int id ) {
140156
return id;
141157
}
142-
});
158+
}, createIndex);
159+
}
160+
161+
@Test
162+
public void testLongKey_OpenRangeIndex() throws Exception {
163+
testLongKeyIndex(_createOpenRangeIndex);
143164
}
144165

145166
@Test
146-
public void testLongKeyIndex() throws Exception {
167+
public void testLongKey_ClosedRangeIndex() throws Exception {
168+
testLongKeyIndex(_createClosedRangeIndex);
169+
}
170+
171+
private void testLongKeyIndex(BiFunction<Dump<Bean>, Field, MmapLongIdIndex<Bean>> createIndex) throws Exception {
147172
testIndex("_idLong", new TestConfiguration() {
148173

149174
@Override
150175
public Object createKey( int id ) {
151176
return (long)id;
152177
}
153-
});
178+
}, createIndex);
179+
}
180+
181+
@Test
182+
public void testLongObjectKey_OpenRangeIndex() throws Exception {
183+
testLongObjectKeyIndex(_createOpenRangeIndex);
154184
}
155185

156186
@Test
157-
public void testLongObjectKeyIndex() throws Exception {
187+
public void testLongObjectKey_ClosedRangeIndex() throws Exception {
188+
testLongObjectKeyIndex(_createClosedRangeIndex);
189+
}
190+
191+
private void testLongObjectKeyIndex(BiFunction<Dump<Bean>, Field, MmapLongIdIndex<Bean>> createIndex) throws Exception {
158192
testIndex("_idLongObject", new TestConfiguration() {
159193

160194
@Override
161195
public Object createKey( int id ) {
162196
return (long)id;
163197
}
164-
});
198+
}, createIndex);
165199
}
166200

167201
@Test
168-
public void testRecreateIndex() throws NoSuchFieldException, IOException {
169-
File dumpFile = new File(_tmpdir, DUMP_FILENAME);
170-
171-
Dump<Bean> dump = new Dump<>(Bean.class, dumpFile);
172-
try {
173-
Field field = Reflection.getField(Bean.class, "_idInt");
174-
UniqueConstraint<Bean> index = MmapLongIdIndex.forOpenRange(dump, new FieldFieldAccessor(field), _negativeOffset);
175-
176-
validateNumKeys(dump, index);
177-
178-
fillDump(dump);
179-
180-
validateNumKeys(dump, index);
181-
182-
dump.close();
183-
184-
System.out.println("Closing and re-opening dump, deleting index");
185-
Assert.assertTrue("Failed to delete index",
186-
new File(_tmpdir, DUMP_FILENAME + "._idInt.mmap.lookup").delete() && !new File(_tmpdir, DUMP_FILENAME + "._idInt.mmap.lookup").exists());
187-
188-
dump = new Dump<>(Bean.class, dumpFile);
189-
index = MmapLongIdIndex.forOpenRange(dump, new FieldFieldAccessor(field), _negativeOffset);
190-
191-
long t = System.currentTimeMillis();
192-
for ( int j = 0; j < READ_NUMBER; j++ ) {
193-
int i = _random.nextInt(_dumpSize);
194-
Bean bean = index.lookup(i);
195-
Assert.assertNotNull("no Bean for index " + i, bean);
196-
Assert.assertEquals(i, bean._idInt);
197-
Assert.assertTrue(bean._data.startsWith(i + "-"));
198-
}
199-
System.out.println("Read " + READ_NUMBER + " instances from dump. Needed " + (System.currentTimeMillis() - t) / (float)READ_NUMBER + " ms/instance.");
202+
public void testRecreateIndex_ClosedRange() throws IOException, NoSuchFieldException {
203+
testRecreateIndex(_createClosedRangeIndex);
204+
}
200205

201-
Bean nonExistingBean = index.lookup(_dumpSize + 1);
202-
Assert.assertNull(nonExistingBean);
203-
}
204-
finally {
205-
dump.close();
206-
}
206+
@Test
207+
public void testRecreateIndex_OpenRange() throws IOException, NoSuchFieldException {
208+
testRecreateIndex(_createOpenRangeIndex);
207209
}
208210

209-
protected void testIndex( String fieldName, TestConfiguration config ) throws Exception {
211+
protected void testIndex( String fieldName, TestConfiguration config, BiFunction<Dump<Bean>, Field, MmapLongIdIndex<Bean>> createIndex) throws Exception {
210212

211-
testLateOpenIndex(fieldName, config);
213+
testLateOpenIndex(fieldName, config, createIndex);
212214

213215
File dumpFile = new File(_tmpdir, DUMP_FILENAME);
214216

@@ -368,7 +370,15 @@ private void fillDump( Dump<Bean> dump ) throws IOException {
368370
System.out.println("Written " + _dumpSize + " instances to dump. Needed " + (System.currentTimeMillis() - t) / (float)_dumpSize + " ms/instance.");
369371
}
370372

371-
private void testLateOpenIndex( String fieldName, TestConfiguration config ) throws Exception {
373+
private long maxId() {
374+
return _dumpSize;
375+
}
376+
377+
private long minId() {
378+
return _negativeOffset;
379+
}
380+
381+
private void testLateOpenIndex( String fieldName, TestConfiguration config, BiFunction<Dump<Bean>, Field, MmapLongIdIndex<Bean>> createIndex ) throws Exception {
372382
File dumpFile = new File(_tmpdir, DUMP_FILENAME);
373383

374384
/* create dump and index */
@@ -377,7 +387,7 @@ private void testLateOpenIndex( String fieldName, TestConfiguration config ) thr
377387
Field field = Reflection.getField(Bean.class, fieldName);
378388

379389
fillDump(dump);
380-
UniqueConstraint<Bean> index = MmapLongIdIndex.forOpenRange(dump, new FieldFieldAccessor(field), _negativeOffset);
390+
UniqueConstraint<Bean> index = createIndex.apply(dump, field);
381391

382392
testLookup(config, field, index);
383393
}
@@ -431,6 +441,47 @@ private void testLookupAfterUpdates( TestConfiguration config, Field field, Uniq
431441
System.out.println("Read " + READ_NUMBER + " instances from dump. Needed " + (System.currentTimeMillis() - t) / (float)READ_NUMBER + " ms/instance.");
432442
}
433443

444+
private void testRecreateIndex( BiFunction<Dump<Bean>, Field, MmapLongIdIndex<Bean>> createIndex ) throws NoSuchFieldException, IOException {
445+
File dumpFile = new File(_tmpdir, DUMP_FILENAME);
446+
447+
Dump<Bean> dump = new Dump<>(Bean.class, dumpFile);
448+
try {
449+
Field field = Reflection.getField(Bean.class, "_idInt");
450+
UniqueConstraint<Bean> index = createIndex.apply(dump, field);
451+
452+
validateNumKeys(dump, index);
453+
454+
fillDump(dump);
455+
456+
validateNumKeys(dump, index);
457+
458+
dump.close();
459+
460+
System.out.println("Closing and re-opening dump, deleting index");
461+
Assert.assertTrue("Failed to delete index",
462+
new File(_tmpdir, DUMP_FILENAME + "._idInt.mmap.lookup").delete() && !new File(_tmpdir, DUMP_FILENAME + "._idInt.mmap.lookup").exists());
463+
464+
dump = new Dump<>(Bean.class, dumpFile);
465+
index = MmapLongIdIndex.forOpenRange(dump, new FieldFieldAccessor(field), _negativeOffset);
466+
467+
long t = System.currentTimeMillis();
468+
for ( int j = 0; j < READ_NUMBER; j++ ) {
469+
int i = _random.nextInt(_dumpSize);
470+
Bean bean = index.lookup(i);
471+
Assert.assertNotNull("no Bean for index " + i, bean);
472+
Assert.assertEquals(i, bean._idInt);
473+
Assert.assertTrue(bean._data.startsWith(i + "-"));
474+
}
475+
System.out.println("Read " + READ_NUMBER + " instances from dump. Needed " + (System.currentTimeMillis() - t) / (float)READ_NUMBER + " ms/instance.");
476+
477+
Bean nonExistingBean = index.lookup(_dumpSize + 1);
478+
Assert.assertNull(nonExistingBean);
479+
}
480+
finally {
481+
dump.close();
482+
}
483+
}
484+
434485
private void validateNumKeys( Dump<Bean> dump, UniqueConstraint<?> index ) {
435486
// count keys
436487
TIntSet keys = new TIntHashSet();

0 commit comments

Comments
 (0)