|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.mnemonic.collections; |
| 19 | + |
| 20 | +import java.nio.ByteBuffer; |
| 21 | +import java.util.ArrayList; |
| 22 | +import java.util.Iterator; |
| 23 | +import java.util.List; |
| 24 | +import java.util.Random; |
| 25 | + |
| 26 | +import org.apache.mnemonic.NonVolatileMemAllocator; |
| 27 | +import org.apache.mnemonic.RestorableAllocator; |
| 28 | +import org.apache.commons.lang3.tuple.Pair; |
| 29 | +import org.apache.mnemonic.Durable; |
| 30 | +import org.apache.mnemonic.EntityFactoryProxy; |
| 31 | +import org.apache.mnemonic.Reclaim; |
| 32 | +import org.apache.mnemonic.Utils; |
| 33 | +import org.apache.mnemonic.DurableType; |
| 34 | +import org.apache.mnemonic.ParameterHolder; |
| 35 | +import org.testng.AssertJUnit; |
| 36 | +import org.testng.annotations.AfterClass; |
| 37 | +import org.testng.annotations.BeforeClass; |
| 38 | +import org.testng.annotations.Test; |
| 39 | + |
| 40 | +/** |
| 41 | + * |
| 42 | + * |
| 43 | + */ |
| 44 | + |
| 45 | +public class DurableSinglyLinkedListWithParamHolderNGTest { |
| 46 | + private long cKEYCAPACITY; |
| 47 | + private Random m_rand; |
| 48 | + private NonVolatileMemAllocator m_act; |
| 49 | + private ParameterHolder ph; |
| 50 | + |
| 51 | + @BeforeClass |
| 52 | + public void setUp() { |
| 53 | + m_rand = Utils.createRandom(); |
| 54 | + m_act = new NonVolatileMemAllocator(Utils.getNonVolatileMemoryAllocatorService("pmalloc"), 1024 * 1024 * 1024, |
| 55 | + "./pobj_NodeValue_WithParamHolder.dat", true); |
| 56 | + ph = new ParameterHolder(); |
| 57 | + |
| 58 | + cKEYCAPACITY = m_act.handlerCapacity(); |
| 59 | + m_act.setBufferReclaimer(new Reclaim<ByteBuffer>() { |
| 60 | + @Override |
| 61 | + public boolean reclaim(ByteBuffer mres, Long sz) { |
| 62 | + System.out.println(String.format("Reclaim Memory Buffer: %X Size: %s", System.identityHashCode(mres), |
| 63 | + null == sz ? "NULL" : sz.toString())); |
| 64 | + return false; |
| 65 | + } |
| 66 | + }); |
| 67 | + m_act.setChunkReclaimer(new Reclaim<Long>() { |
| 68 | + @Override |
| 69 | + public boolean reclaim(Long mres, Long sz) { |
| 70 | + System.out.println(String.format("Reclaim Memory Chunk: %X Size: %s", System.identityHashCode(mres), |
| 71 | + null == sz ? "NULL" : sz.toString())); |
| 72 | + return false; |
| 73 | + } |
| 74 | + }); |
| 75 | + |
| 76 | + for (long i = 0; i < cKEYCAPACITY; ++i) { |
| 77 | + m_act.setHandler(i, 0L); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + @AfterClass |
| 82 | + public void tearDown() { |
| 83 | + m_act.close(); |
| 84 | + } |
| 85 | + |
| 86 | + @Test(enabled = false) |
| 87 | + public void testSingleNodeValueWithInteger() { |
| 88 | + int val = m_rand.nextInt(); |
| 89 | + DurableType gtypes[] = {DurableType.INTEGER}; |
| 90 | + DurableSinglyLinkedList<Integer> plln = DurableSinglyLinkedListFactory.create(m_act, null, gtypes, false); |
| 91 | + plln.setItem(val, false); |
| 92 | + Long handler = plln.getHandler(); |
| 93 | + System.err.println("-------------Start to Restore Integer -----------"); |
| 94 | + DurableSinglyLinkedList<Integer> plln2 = DurableSinglyLinkedListFactory.restore(m_act, null, gtypes, handler, |
| 95 | + false); |
| 96 | + AssertJUnit.assertEquals(val, (int) plln2.getItem()); |
| 97 | + } |
| 98 | + |
| 99 | + @Test(enabled = false) |
| 100 | + public void testNodeValueWithString() { |
| 101 | + String val = Utils.genRandomString(); |
| 102 | + DurableType gtypes[] = {DurableType.STRING}; |
| 103 | + DurableSinglyLinkedList<String> plln = DurableSinglyLinkedListFactory.create(m_act, null, gtypes, false); |
| 104 | + plln.setItem(val, false); |
| 105 | + Long handler = plln.getHandler(); |
| 106 | + System.err.println("-------------Start to Restore String-----------"); |
| 107 | + DurableSinglyLinkedList<String> plln2 = DurableSinglyLinkedListFactory.restore(m_act, null, gtypes, handler, |
| 108 | + false); |
| 109 | + AssertJUnit.assertEquals(val, plln2.getItem()); |
| 110 | + } |
| 111 | + |
| 112 | + @Test(enabled = false) |
| 113 | + public void testNodeValueWithPerson() { |
| 114 | + |
| 115 | + DurableType gtypes[] = {DurableType.DURABLE}; |
| 116 | + EntityFactoryProxy efproxies[] = {new EntityFactoryProxy() { |
| 117 | + @Override |
| 118 | + public <A extends RestorableAllocator<A>> Person<Long> restore( |
| 119 | + A allocator, EntityFactoryProxy[] factoryproxys, |
| 120 | + DurableType[] gfields, long phandler, boolean autoreclaim) { |
| 121 | + return PersonFactory.restore(allocator, factoryproxys, gfields, phandler, autoreclaim); |
| 122 | + } |
| 123 | + @Override |
| 124 | + public <A extends RestorableAllocator<A>> Person<Long> restore(ParameterHolder<A> ph) { |
| 125 | + return PersonFactory.restore(ph.getAllocator(), |
| 126 | + ph.getEntityFactoryProxies(), ph.getGenericTypes(), ph.getHandler(), ph.getAutoReclaim()); |
| 127 | + } |
| 128 | + @Override |
| 129 | + public <A extends RestorableAllocator<A>> Person<Long> create( |
| 130 | + A allocator, EntityFactoryProxy[] factoryproxys, |
| 131 | + DurableType[] gfields, boolean autoreclaim) { |
| 132 | + return PersonFactory.create(allocator, factoryproxys, gfields, autoreclaim); |
| 133 | + } |
| 134 | + @Override |
| 135 | + public <A extends RestorableAllocator<A>> Person<Long> create(ParameterHolder<A> ph) { |
| 136 | + return PersonFactory.create(ph.getAllocator(), |
| 137 | + ph.getEntityFactoryProxies(), ph.getGenericTypes(), ph.getAutoReclaim()); |
| 138 | + } |
| 139 | + } }; |
| 140 | + |
| 141 | + ph.setAllocator(m_act); |
| 142 | + ph.setEntityFactoryProxies(efproxies); |
| 143 | + ph.setGenericTypes(gtypes); |
| 144 | + ph.setAutoReclaim(false); |
| 145 | + |
| 146 | + @SuppressWarnings("unchecked") |
| 147 | + Person<Long> person = (Person<Long>) efproxies[0].create(m_act, null, null, false); |
| 148 | + person.setAge((short) 31); |
| 149 | + |
| 150 | + DurableSinglyLinkedList<Person<Long>> plln = DurableSinglyLinkedListFactory.create(ph); |
| 151 | + plln.setItem(person, false); |
| 152 | + |
| 153 | + long handler = plln.getHandler(); |
| 154 | + ph.setHandler(handler); |
| 155 | + |
| 156 | + DurableSinglyLinkedList<Person<Long>> plln2 = DurableSinglyLinkedListFactory.restore(ph); |
| 157 | + AssertJUnit.assertEquals(31, (int) plln2.getItem().getAge()); |
| 158 | + |
| 159 | + } |
| 160 | + |
| 161 | + @SuppressWarnings("unchecked") |
| 162 | + @Test(enabled = false) |
| 163 | + public void testLinkedNodeValueWithPerson() { |
| 164 | + |
| 165 | + int elem_count = 10; |
| 166 | + List<Long> referlist = new ArrayList(); |
| 167 | + |
| 168 | + DurableType listgftypes[] = {DurableType.DURABLE}; |
| 169 | + EntityFactoryProxy listefproxies[] = {new EntityFactoryProxy() { |
| 170 | + @Override |
| 171 | + public <A extends RestorableAllocator<A>> Person<Long> restore( |
| 172 | + A allocator, EntityFactoryProxy[] factoryproxys, |
| 173 | + DurableType[] gfields, long phandler, boolean autoreclaim) { |
| 174 | + return PersonFactory.restore(allocator, factoryproxys, gfields, phandler, autoreclaim); |
| 175 | + } |
| 176 | + @Override |
| 177 | + public <A extends RestorableAllocator<A>> Person<Long> restore(ParameterHolder<A> ph) { |
| 178 | + return PersonFactory.restore(ph.getAllocator(), |
| 179 | + ph.getEntityFactoryProxies(), ph.getGenericTypes(), ph.getHandler(), ph.getAutoReclaim()); |
| 180 | + } |
| 181 | + @Override |
| 182 | + public <A extends RestorableAllocator<A>> Person<Long> create( |
| 183 | + A allocator, EntityFactoryProxy[] factoryproxys, |
| 184 | + DurableType[] gfields, boolean autoreclaim) { |
| 185 | + return PersonFactory.create(allocator, factoryproxys, gfields, autoreclaim); |
| 186 | + } |
| 187 | + @Override |
| 188 | + public <A extends RestorableAllocator<A>> Person<Long> create(ParameterHolder<A> ph) { |
| 189 | + return PersonFactory.create(ph.getAllocator(), |
| 190 | + ph.getEntityFactoryProxies(), ph.getGenericTypes(), ph.getAutoReclaim()); |
| 191 | + } |
| 192 | + } }; |
| 193 | + |
| 194 | + ph.setAllocator(m_act); |
| 195 | + ph.setEntityFactoryProxies(listefproxies); |
| 196 | + ph.setGenericTypes(listgftypes); |
| 197 | + ph.setAutoReclaim(false); |
| 198 | + |
| 199 | + DurableSinglyLinkedList<Person<Long>> firstnv = DurableSinglyLinkedListFactory.create(ph); |
| 200 | + |
| 201 | + DurableSinglyLinkedList<Person<Long>> nextnv = firstnv; |
| 202 | + |
| 203 | + Person<Long> person; |
| 204 | + long val; |
| 205 | + DurableSinglyLinkedList<Person<Long>> newnv; |
| 206 | + for (int i = 0; i < elem_count; ++i) { |
| 207 | + person = (Person<Long>) listefproxies[0].create(m_act, null, null, false); |
| 208 | + person.setAge((short) m_rand.nextInt(50)); |
| 209 | + person.setName(String.format("Name: [%s]", Utils.genRandomString()), true); |
| 210 | + nextnv.setItem(person, false); |
| 211 | + newnv = DurableSinglyLinkedListFactory.create(ph); |
| 212 | + nextnv.setNext(newnv, false); |
| 213 | + nextnv = newnv; |
| 214 | + } |
| 215 | + |
| 216 | + Person<Long> eval; |
| 217 | + DurableSinglyLinkedList<Person<Long>> iternv = firstnv; |
| 218 | + while (null != iternv) { |
| 219 | + System.out.printf(" Stage 1 --->\n"); |
| 220 | + eval = iternv.getItem(); |
| 221 | + if (null != eval) { |
| 222 | + eval.testOutput(); |
| 223 | + } |
| 224 | + iternv = iternv.getNext(); |
| 225 | + } |
| 226 | + |
| 227 | + long handler = firstnv.getHandler(); |
| 228 | + ph.setHandler(handler); |
| 229 | + |
| 230 | + DurableSinglyLinkedList<Person<Long>> firstnv2 = DurableSinglyLinkedListFactory.restore(ph); |
| 231 | + |
| 232 | + for (Person<Long> eval2 : firstnv2) { |
| 233 | + System.out.printf(" Stage 2 ---> \n"); |
| 234 | + if (null != eval2) { |
| 235 | + eval2.testOutput(); |
| 236 | + } |
| 237 | + } |
| 238 | + |
| 239 | + // Assert.assert, expected);(plist, plist2); |
| 240 | + |
| 241 | + } |
| 242 | + |
| 243 | + @Test(enabled = true) |
| 244 | + public void testLinkedNodeValueWithLinkedNodeValue() { |
| 245 | + |
| 246 | + int elem_count = 10; |
| 247 | + long slotKeyId = 10; |
| 248 | + ParameterHolder phe = new ParameterHolder(); |
| 249 | + ParameterHolder phl = new ParameterHolder(); |
| 250 | + |
| 251 | + DurableType[] elem_gftypes = {DurableType.DOUBLE}; |
| 252 | + EntityFactoryProxy[] elem_efproxies = null; |
| 253 | + |
| 254 | + DurableType linkedgftypes[] = {DurableType.DURABLE, DurableType.DOUBLE}; |
| 255 | + EntityFactoryProxy linkedefproxies[] = {new EntityFactoryProxy() { |
| 256 | + @Override |
| 257 | + public <A extends RestorableAllocator<A>> Durable restore(A allocator, EntityFactoryProxy[] factoryproxys, |
| 258 | + DurableType[] gfields, long phandler, boolean autoreclaim) { |
| 259 | + Pair<DurableType[], EntityFactoryProxy[]> dpt = Utils.shiftDurableParams(gfields, factoryproxys, 1); |
| 260 | + return DurableSinglyLinkedListFactory.restore(allocator, dpt.getRight(), dpt.getLeft(), phandler, autoreclaim); |
| 261 | + } |
| 262 | + @Override |
| 263 | + public <A extends RestorableAllocator<A>> Durable restore(ParameterHolder<A> ph) { |
| 264 | + Pair<DurableType[], EntityFactoryProxy[]> dpt = Utils.shiftDurableParams(ph.getGenericTypes(), |
| 265 | + ph.getEntityFactoryProxies(), 1); |
| 266 | + return DurableSinglyLinkedListFactory.restore(ph.getAllocator(), |
| 267 | + dpt.getRight(), dpt.getLeft(), ph.getHandler(), ph.getAutoReclaim()); |
| 268 | + } |
| 269 | + @Override |
| 270 | + public <A extends RestorableAllocator<A>> Durable create( |
| 271 | + A allocator, EntityFactoryProxy[] factoryproxys, |
| 272 | + DurableType[] gfields, boolean autoreclaim) { |
| 273 | + Pair<DurableType[], EntityFactoryProxy[]> dpt = Utils.shiftDurableParams(gfields, factoryproxys, 1); |
| 274 | + return DurableSinglyLinkedListFactory.create(allocator, dpt.getRight(), dpt.getLeft(), autoreclaim); |
| 275 | + } |
| 276 | + @Override |
| 277 | + public <A extends RestorableAllocator<A>> Durable create(ParameterHolder<A> ph) { |
| 278 | + Pair<DurableType[], EntityFactoryProxy[]> dpt = Utils.shiftDurableParams(ph.getGenericTypes(), |
| 279 | + ph.getEntityFactoryProxies(), 1); |
| 280 | + return DurableSinglyLinkedListFactory.create(ph.getAllocator(), |
| 281 | + dpt.getRight(), dpt.getLeft(), ph.getAutoReclaim()); |
| 282 | + } |
| 283 | + } }; |
| 284 | + |
| 285 | + DurableSinglyLinkedList<DurableSinglyLinkedList<Double>> nextnv = null, pre_nextnv = null; |
| 286 | + DurableSinglyLinkedList<Double> elem = null, pre_elem = null, first_elem = null; |
| 287 | + |
| 288 | + Long linkhandler = 0L; |
| 289 | + |
| 290 | + System.out.printf(" Stage 1 -testLinkedNodeValueWithLinkedNodeValue--> \n"); |
| 291 | + |
| 292 | + pre_nextnv = null; |
| 293 | + Double val; |
| 294 | + phe.setAllocator(m_act); |
| 295 | + phe.setEntityFactoryProxies(elem_efproxies); |
| 296 | + phe.setGenericTypes(elem_gftypes); |
| 297 | + phe.setAutoReclaim(false); |
| 298 | + phl.setAllocator(m_act); |
| 299 | + phl.setEntityFactoryProxies(linkedefproxies); |
| 300 | + phl.setGenericTypes(linkedgftypes); |
| 301 | + phl.setAutoReclaim(false); |
| 302 | + |
| 303 | + for (int i = 0; i < elem_count; ++i) { |
| 304 | + first_elem = null; |
| 305 | + pre_elem = null; |
| 306 | + |
| 307 | + for (int v = 0; v < 3; ++v) { |
| 308 | + elem = DurableSinglyLinkedListFactory.create(phe); |
| 309 | + val = m_rand.nextDouble(); |
| 310 | + elem.setItem(val, false); |
| 311 | + if (null == pre_elem) { |
| 312 | + first_elem = elem; |
| 313 | + } else { |
| 314 | + pre_elem.setNext(elem, false); |
| 315 | + } |
| 316 | + pre_elem = elem; |
| 317 | + System.out.printf("%f ", val); |
| 318 | + } |
| 319 | + |
| 320 | + nextnv = DurableSinglyLinkedListFactory.create(phl); |
| 321 | + nextnv.setItem(first_elem, false); |
| 322 | + if (null == pre_nextnv) { |
| 323 | + linkhandler = nextnv.getHandler(); |
| 324 | + } else { |
| 325 | + pre_nextnv.setNext(nextnv, false); |
| 326 | + } |
| 327 | + pre_nextnv = nextnv; |
| 328 | + System.out.printf(" generated an item... \n"); |
| 329 | + } |
| 330 | + m_act.setHandler(slotKeyId, linkhandler); |
| 331 | + |
| 332 | + long handler = m_act.getHandler(slotKeyId); |
| 333 | + phl.setHandler(handler); |
| 334 | + |
| 335 | + DurableSinglyLinkedList<DurableSinglyLinkedList<Double>> linkedvals = DurableSinglyLinkedListFactory.restore(phl); |
| 336 | + Iterator<DurableSinglyLinkedList<Double>> iter = linkedvals.iterator(); |
| 337 | + Iterator<Double> elemiter = null; |
| 338 | + |
| 339 | + System.out.printf(" Stage 2 -testLinkedNodeValueWithLinkedNodeValue--> \n"); |
| 340 | + while (iter.hasNext()) { |
| 341 | + elemiter = iter.next().iterator(); |
| 342 | + while (elemiter.hasNext()) { |
| 343 | + System.out.printf("%f ", elemiter.next()); |
| 344 | + } |
| 345 | + System.out.printf(" Fetched an item... \n"); |
| 346 | + } |
| 347 | + |
| 348 | + // Assert.assert, expected);(plist, plist2); |
| 349 | + |
| 350 | + } |
| 351 | + |
| 352 | +} |
0 commit comments