Skip to content

Commit e912470

Browse files
author
Johnu George
committed
MNEMONIC-228: Implement iterators for durable arrays
1 parent 640becd commit e912470

File tree

2 files changed

+83
-1
lines changed

2 files changed

+83
-1
lines changed

mnemonic-collections/src/main/java/org/apache/mnemonic/collections/DurableArrayImpl.java

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.apache.mnemonic.RetrieveDurableEntityError;
2929
import org.apache.mnemonic.Utils;
3030

31+
import java.util.NoSuchElementException;
3132
import sun.misc.Unsafe;
3233
import java.util.Iterator;
3334

@@ -215,6 +216,36 @@ public void createDurableEntity(A allocator, EntityFactoryProxy[] factoryProxy,
215216

216217
@Override
217218
public Iterator<E> iterator() {
218-
return null;
219+
return new ArrayItr(this);
220+
}
221+
222+
private class ArrayItr implements Iterator<E> {
223+
224+
protected DurableArray<E> array = null;
225+
int currentIndex = 0;
226+
227+
ArrayItr(DurableArray<E> itr) {
228+
array = itr;
229+
}
230+
231+
@Override
232+
public boolean hasNext() {
233+
return currentIndex < array.arraySize;
234+
}
235+
236+
@Override
237+
public E next() {
238+
if (currentIndex >= array.arraySize) {
239+
throw new NoSuchElementException();
240+
}
241+
E item = get(currentIndex);
242+
currentIndex++;
243+
return item;
244+
}
245+
246+
@Override
247+
public void remove() {
248+
throw new UnsupportedOperationException();
249+
}
219250
}
220251
}

mnemonic-collections/src/test/java/org/apache/mnemonic/collections/DurableArrayNGTest.java

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.util.Random;
2222
import java.util.zip.Checksum;
2323
import java.util.zip.CRC32;
24+
import java.util.Iterator;
2425
import org.apache.commons.lang3.tuple.Pair;
2526
import org.apache.commons.lang3.ArrayUtils;
2627

@@ -138,6 +139,14 @@ public void testGetSetArrayPrimitives() {
138139
for (int i = 0; i < capacity; i++) {
139140
Assert.assertEquals(array.get(i).intValue(), 100 + i);
140141
}
142+
143+
Iterator<Integer> itr = array.iterator();
144+
int val = 0;
145+
while (itr.hasNext()) {
146+
Assert.assertEquals(itr.next().intValue(), 100 + val);
147+
val++;
148+
}
149+
Assert.assertEquals(val, capacity);
141150
array.destroy();
142151
}
143152

@@ -156,6 +165,15 @@ public void testGetSetArrayString() {
156165
for (int i = 0; i < capacity; i++) {
157166
Assert.assertEquals(array.get(i), "string" + i);
158167
}
168+
169+
Iterator<String> itr = array.iterator();
170+
int val = 0;
171+
while (itr.hasNext()) {
172+
Assert.assertEquals(itr.next(), "string" + val);
173+
val++;
174+
}
175+
Assert.assertEquals(val, capacity);
176+
159177
array.destroy();
160178
}
161179

@@ -185,6 +203,7 @@ public void testGetSetArrayBuffer() {
185203
byte buf[] = new byte[db.get().capacity()];
186204
db.get().get(buf);
187205
bufferCheckSum.update(buf, 0, buf.length);
206+
db.get().clear();
188207
}
189208
Assert.assertEquals(bufferCheckSum.getValue(), bufVal);
190209
bufferCheckSum.reset();
@@ -196,9 +215,25 @@ public void testGetSetArrayBuffer() {
196215
byte buf[] = new byte[db.get().capacity()];
197216
db.get().get(buf);
198217
bufferCheckSum.update(buf, 0, buf.length);
218+
db.get().clear();
199219
}
200220
Assert.assertEquals(bufferCheckSum.getValue(), bufVal);
201221

222+
bufferCheckSum.reset();
223+
Iterator<DurableBuffer> itr = restoredArray.iterator();
224+
int val = 0;
225+
while (itr.hasNext()) {
226+
DurableBuffer<NonVolatileMemAllocator> db = itr.next();
227+
Assert.assertNotNull(db);
228+
byte buf[] = new byte[db.get().capacity()];
229+
db.get().get(buf);
230+
bufferCheckSum.update(buf, 0, buf.length);
231+
db.get().clear();
232+
val++;
233+
}
234+
Assert.assertEquals(val, capacity);
235+
Assert.assertEquals(bufferCheckSum.getValue(), bufVal);
236+
202237
restoredArray.destroy();
203238
}
204239

@@ -243,6 +278,22 @@ public void testGetSetArrayChunk() {
243278
}
244279
}
245280
Assert.assertEquals(chunkCheckSum.getValue(), chunkVal);
281+
282+
chunkCheckSum.reset();
283+
Iterator<DurableChunk> itr = restoredArray.iterator();
284+
int val = 0;
285+
while (itr.hasNext()) {
286+
DurableChunk<NonVolatileMemAllocator> dc = itr.next();
287+
Assert.assertNotNull(dc);
288+
for (int j = 0; j < dc.getSize(); ++j) {
289+
byte b = unsafe.getByte(dc.get() + j);
290+
chunkCheckSum.update(b);
291+
}
292+
val++;
293+
}
294+
Assert.assertEquals(val, capacity);
295+
Assert.assertEquals(chunkCheckSum.getValue(), chunkVal);
296+
246297
restoredArray.destroy();
247298
}
248299

0 commit comments

Comments
 (0)