|
19 | 19 |
|
20 | 20 | import java.util.Iterator;
|
21 | 21 | import java.nio.ByteBuffer;
|
| 22 | +import java.util.Random; |
| 23 | +import java.util.zip.Checksum; |
| 24 | +import java.util.zip.CRC32; |
22 | 25 |
|
23 | 26 | import org.apache.mnemonic.Utils;
|
24 | 27 | import org.apache.mnemonic.RestorableAllocator;
|
25 | 28 | import org.apache.mnemonic.NonVolatileMemAllocator;
|
| 29 | +import org.apache.mnemonic.OutOfHybridMemory; |
26 | 30 | import org.apache.mnemonic.EntityFactoryProxy;
|
27 | 31 | import org.apache.mnemonic.DurableType;
|
| 32 | +import org.apache.mnemonic.DurableBuffer; |
| 33 | +import org.apache.mnemonic.DurableChunk; |
28 | 34 | import org.apache.mnemonic.Reclaim;
|
29 | 35 | import org.apache.mnemonic.Durable;
|
30 | 36 | import org.apache.commons.lang3.tuple.Pair;
|
| 37 | +import org.apache.commons.lang3.RandomUtils; |
31 | 38 | import org.testng.annotations.AfterClass;
|
32 | 39 | import org.testng.annotations.BeforeClass;
|
33 | 40 | import org.testng.annotations.Test;
|
34 | 41 | import org.testng.AssertJUnit;
|
| 42 | +import org.testng.Assert; |
| 43 | + |
| 44 | +import sun.misc.Unsafe; |
35 | 45 |
|
36 | 46 | /**
|
37 | 47 | *
|
|
41 | 51 | public class DurableHashMapNGTest {
|
42 | 52 | private long cKEYCAPACITY;
|
43 | 53 | private NonVolatileMemAllocator m_act;
|
| 54 | + private Random rand; |
| 55 | + private Unsafe unsafe; |
44 | 56 | private long mInitialCapacity = 1;
|
45 | 57 |
|
| 58 | + protected DurableBuffer<NonVolatileMemAllocator> |
| 59 | + genuptBuffer(NonVolatileMemAllocator act, Checksum cs, int size) { |
| 60 | + DurableBuffer<NonVolatileMemAllocator> ret = null; |
| 61 | + ret = act.createBuffer(size, false); |
| 62 | + if (null == ret) { |
| 63 | + throw new OutOfHybridMemory("Create Durable Buffer Failed."); |
| 64 | + } |
| 65 | + ret.get().clear(); |
| 66 | + byte[] rdbytes = RandomUtils.nextBytes(size); |
| 67 | + Assert.assertNotNull(rdbytes); |
| 68 | + ret.get().put(rdbytes); |
| 69 | + cs.update(rdbytes, 0, rdbytes.length); |
| 70 | + return ret; |
| 71 | + } |
| 72 | + |
| 73 | + protected DurableChunk<NonVolatileMemAllocator> |
| 74 | + genuptChunk(NonVolatileMemAllocator act, Checksum cs, long size) { |
| 75 | + DurableChunk<NonVolatileMemAllocator> ret = null; |
| 76 | + ret = act.createChunk(size, false); |
| 77 | + if (null == ret) { |
| 78 | + throw new OutOfHybridMemory("Create Durable Chunk Failed."); |
| 79 | + } |
| 80 | + byte b; |
| 81 | + for (int i = 0; i < ret.getSize(); ++i) { |
| 82 | + b = (byte) rand.nextInt(255); |
| 83 | + unsafe.putByte(ret.get() + i, b); |
| 84 | + cs.update(b); |
| 85 | + } |
| 86 | + return ret; |
| 87 | + } |
| 88 | + |
| 89 | + protected int genRandSize() { |
| 90 | + return rand.nextInt(1024 * 1024) + 1024 * 1024; |
| 91 | + } |
| 92 | + |
46 | 93 | @BeforeClass
|
47 |
| - public void setUp() { |
| 94 | + public void setUp() throws Exception { |
| 95 | + rand = Utils.createRandom(); |
| 96 | + unsafe = Utils.getUnsafe(); |
48 | 97 | m_act = new NonVolatileMemAllocator(Utils.getNonVolatileMemoryAllocatorService("pmalloc"), 1024 * 1024 * 1024,
|
49 | 98 | "./pobj_hashmaps.dat", true);
|
50 | 99 | cKEYCAPACITY = m_act.handlerCapacity();
|
@@ -510,4 +559,88 @@ public void testMapIterator() {
|
510 | 559 | map.destroy();
|
511 | 560 | }
|
512 | 561 |
|
| 562 | + @Test(enabled = true) |
| 563 | + public void testMapValueBuffer() { |
| 564 | + DurableType gtypes[] = {DurableType.STRING, DurableType.BUFFER}; |
| 565 | + DurableHashMap<String, DurableBuffer> map = DurableHashMapFactory.create(m_act, null, gtypes, 1, false); |
| 566 | + long bufVal; |
| 567 | + |
| 568 | + Checksum bufferCheckSum = new CRC32(); |
| 569 | + bufferCheckSum.reset(); |
| 570 | + |
| 571 | + Long handler = map.getHandler(); |
| 572 | + for (int i = 0; i < 10; i++) { |
| 573 | + map.put("buffer" + i, genuptBuffer(m_act, bufferCheckSum, genRandSize())); |
| 574 | + } |
| 575 | + |
| 576 | + bufVal = bufferCheckSum.getValue(); |
| 577 | + |
| 578 | + bufferCheckSum.reset(); |
| 579 | + for (int i = 0; i < 10; i++) { |
| 580 | + DurableBuffer<NonVolatileMemAllocator> db = map.get("buffer" + i); |
| 581 | + Assert.assertNotNull(db); |
| 582 | + byte buf[] = new byte[db.get().capacity()]; |
| 583 | + db.get().get(buf); |
| 584 | + bufferCheckSum.update(buf, 0, buf.length); |
| 585 | + } |
| 586 | + Assert.assertEquals(bufferCheckSum.getValue(), bufVal); |
| 587 | + |
| 588 | + bufferCheckSum.reset(); |
| 589 | + DurableHashMap<String, DurableBuffer> restoredMap = DurableHashMapFactory.restore(m_act, |
| 590 | + null, gtypes, handler, false); |
| 591 | + for (int i = 0; i < 10; i++) { |
| 592 | + DurableBuffer<NonVolatileMemAllocator> db = restoredMap.get("buffer" + i); |
| 593 | + Assert.assertNotNull(db); |
| 594 | + byte buf[] = new byte[db.get().capacity()]; |
| 595 | + db.get().get(buf); |
| 596 | + bufferCheckSum.update(buf, 0, buf.length); |
| 597 | + } |
| 598 | + Assert.assertEquals(bufferCheckSum.getValue(), bufVal); |
| 599 | + |
| 600 | + restoredMap.destroy(); |
| 601 | + } |
| 602 | + |
| 603 | + @Test(enabled = true) |
| 604 | + public void testMapValueChunk() { |
| 605 | + DurableType gtypes[] = {DurableType.STRING, DurableType.CHUNK}; |
| 606 | + DurableHashMap<String, DurableChunk> map = DurableHashMapFactory.create(m_act, null, gtypes, 1, false); |
| 607 | + long chunkVal; |
| 608 | + |
| 609 | + Checksum chunkCheckSum = new CRC32(); |
| 610 | + chunkCheckSum.reset(); |
| 611 | + |
| 612 | + Long handler = map.getHandler(); |
| 613 | + for (int i = 0; i < 10; i++) { |
| 614 | + map.put("chunk" + i, genuptChunk(m_act, chunkCheckSum, genRandSize())); |
| 615 | + } |
| 616 | + |
| 617 | + chunkVal = chunkCheckSum.getValue(); |
| 618 | + chunkCheckSum.reset(); |
| 619 | + |
| 620 | + for (int i = 0; i < 10; i++) { |
| 621 | + DurableChunk<NonVolatileMemAllocator> dc = map.get("chunk" + i); |
| 622 | + for (int j = 0; j < dc.getSize(); ++j) { |
| 623 | + byte b = unsafe.getByte(dc.get() + j); |
| 624 | + chunkCheckSum.update(b); |
| 625 | + } |
| 626 | + } |
| 627 | + chunkVal = chunkCheckSum.getValue(); |
| 628 | + Assert.assertEquals(chunkCheckSum.getValue(), chunkVal); |
| 629 | + |
| 630 | + chunkCheckSum.reset(); |
| 631 | + DurableHashMap<String, DurableChunk> restoredMap = DurableHashMapFactory.restore(m_act, |
| 632 | + null, gtypes, handler, false); |
| 633 | + |
| 634 | + for (int i = 0; i < 10; i++) { |
| 635 | + DurableChunk<NonVolatileMemAllocator> dc = restoredMap.get("chunk" + i); |
| 636 | + for (int j = 0; j < dc.getSize(); ++j) { |
| 637 | + byte b = unsafe.getByte(dc.get() + j); |
| 638 | + chunkCheckSum.update(b); |
| 639 | + } |
| 640 | + } |
| 641 | + chunkVal = chunkCheckSum.getValue(); |
| 642 | + Assert.assertEquals(chunkCheckSum.getValue(), chunkVal); |
| 643 | + |
| 644 | + restoredMap.destroy(); |
| 645 | + } |
513 | 646 | }
|
0 commit comments