You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mnemonic.apache.org by ga...@apache.org on 2017/03/22 20:59:45 UTC

incubator-mnemonic git commit: MNEMONIC-228: Implement iterators for durable arrays

Repository: incubator-mnemonic
Updated Branches:
  refs/heads/master 640becdde -> e912470d0


MNEMONIC-228: Implement iterators for durable arrays


Project: http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/commit/e912470d
Tree: http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/tree/e912470d
Diff: http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/diff/e912470d

Branch: refs/heads/master
Commit: e912470d078f34d3b0b290d09fa4de152767c86b
Parents: 640becd
Author: Johnu George <jo...@cisco.com>
Authored: Wed Mar 22 12:05:54 2017 -0700
Committer: Johnu George <jo...@cisco.com>
Committed: Wed Mar 22 12:05:54 2017 -0700

----------------------------------------------------------------------
 .../mnemonic/collections/DurableArrayImpl.java  | 33 ++++++++++++-
 .../collections/DurableArrayNGTest.java         | 51 ++++++++++++++++++++
 2 files changed, 83 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/e912470d/mnemonic-collections/src/main/java/org/apache/mnemonic/collections/DurableArrayImpl.java
----------------------------------------------------------------------
diff --git a/mnemonic-collections/src/main/java/org/apache/mnemonic/collections/DurableArrayImpl.java b/mnemonic-collections/src/main/java/org/apache/mnemonic/collections/DurableArrayImpl.java
index 5407c5d..3654f9c 100644
--- a/mnemonic-collections/src/main/java/org/apache/mnemonic/collections/DurableArrayImpl.java
+++ b/mnemonic-collections/src/main/java/org/apache/mnemonic/collections/DurableArrayImpl.java
@@ -28,6 +28,7 @@ import org.apache.mnemonic.RestoreDurableEntityError;
 import org.apache.mnemonic.RetrieveDurableEntityError;
 import org.apache.mnemonic.Utils;
 
+import java.util.NoSuchElementException;
 import sun.misc.Unsafe;
 import java.util.Iterator;
 
@@ -215,6 +216,36 @@ public class DurableArrayImpl<A extends RestorableAllocator<A>, E>
 
   @Override
   public Iterator<E> iterator() {
-    return null;
+    return new ArrayItr(this);
+  }
+
+  private class ArrayItr implements Iterator<E> {
+
+    protected DurableArray<E> array = null;
+    int currentIndex = 0;
+
+    ArrayItr(DurableArray<E> itr) {
+      array = itr;
+    }
+
+    @Override
+    public boolean hasNext() {
+      return currentIndex < array.arraySize;
+    }
+
+    @Override
+    public E next() {
+      if (currentIndex >= array.arraySize) {
+        throw new NoSuchElementException();
+      }
+      E item = get(currentIndex);
+      currentIndex++;
+      return item;
+    }
+
+    @Override
+    public void remove() {
+      throw new UnsupportedOperationException();
+    }
   }
 }

http://git-wip-us.apache.org/repos/asf/incubator-mnemonic/blob/e912470d/mnemonic-collections/src/test/java/org/apache/mnemonic/collections/DurableArrayNGTest.java
----------------------------------------------------------------------
diff --git a/mnemonic-collections/src/test/java/org/apache/mnemonic/collections/DurableArrayNGTest.java b/mnemonic-collections/src/test/java/org/apache/mnemonic/collections/DurableArrayNGTest.java
index c5bcd33..f92ad7e 100644
--- a/mnemonic-collections/src/test/java/org/apache/mnemonic/collections/DurableArrayNGTest.java
+++ b/mnemonic-collections/src/test/java/org/apache/mnemonic/collections/DurableArrayNGTest.java
@@ -21,6 +21,7 @@ import java.nio.ByteBuffer;
 import java.util.Random;
 import java.util.zip.Checksum;
 import java.util.zip.CRC32;
+import java.util.Iterator;
 import org.apache.commons.lang3.tuple.Pair;
 import org.apache.commons.lang3.ArrayUtils;
 
@@ -138,6 +139,14 @@ public class DurableArrayNGTest {
     for (int i = 0; i < capacity; i++) {
       Assert.assertEquals(array.get(i).intValue(), 100 + i);
     }
+
+    Iterator<Integer> itr = array.iterator();
+    int val = 0;
+    while (itr.hasNext()) {
+     Assert.assertEquals(itr.next().intValue(), 100 + val);
+     val++;
+    }
+    Assert.assertEquals(val, capacity);
     array.destroy();
   }
 
@@ -156,6 +165,15 @@ public class DurableArrayNGTest {
     for (int i = 0; i < capacity; i++) {
       Assert.assertEquals(array.get(i), "string" + i);
     }
+
+    Iterator<String> itr = array.iterator();
+    int val = 0;
+    while (itr.hasNext()) {
+     Assert.assertEquals(itr.next(), "string" + val);
+     val++;
+    }
+    Assert.assertEquals(val, capacity);
+
     array.destroy();
   }
 
@@ -185,6 +203,7 @@ public class DurableArrayNGTest {
       byte buf[] = new byte[db.get().capacity()];
       db.get().get(buf);
       bufferCheckSum.update(buf, 0, buf.length);
+      db.get().clear();
     }
     Assert.assertEquals(bufferCheckSum.getValue(), bufVal);
     bufferCheckSum.reset();
@@ -196,9 +215,25 @@ public class DurableArrayNGTest {
       byte buf[] = new byte[db.get().capacity()];
       db.get().get(buf);
       bufferCheckSum.update(buf, 0, buf.length);
+      db.get().clear();
     }
     Assert.assertEquals(bufferCheckSum.getValue(), bufVal);
 
+    bufferCheckSum.reset();
+    Iterator<DurableBuffer> itr = restoredArray.iterator();
+    int val = 0;
+    while (itr.hasNext()) {
+      DurableBuffer<NonVolatileMemAllocator> db = itr.next();
+      Assert.assertNotNull(db);
+      byte buf[] = new byte[db.get().capacity()];
+      db.get().get(buf);
+      bufferCheckSum.update(buf, 0, buf.length);
+      db.get().clear();
+      val++;
+    }
+    Assert.assertEquals(val, capacity);
+    Assert.assertEquals(bufferCheckSum.getValue(), bufVal);
+
     restoredArray.destroy();
   }
 
@@ -243,6 +278,22 @@ public class DurableArrayNGTest {
       }
     }
     Assert.assertEquals(chunkCheckSum.getValue(), chunkVal);
+
+    chunkCheckSum.reset();
+    Iterator<DurableChunk> itr = restoredArray.iterator();
+    int val = 0;
+    while (itr.hasNext()) {
+      DurableChunk<NonVolatileMemAllocator> dc = itr.next();
+      Assert.assertNotNull(dc);
+      for (int j = 0; j < dc.getSize(); ++j) {
+        byte b = unsafe.getByte(dc.get() + j);
+        chunkCheckSum.update(b);
+      }
+      val++;
+    }
+    Assert.assertEquals(val, capacity);
+    Assert.assertEquals(chunkCheckSum.getValue(), chunkVal);
+
     restoredArray.destroy();
   }