Skip to content

Improve array iterator #19

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions src/main/java/com/arangodb/velocypack/ArrayIterator.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,21 @@ public ArrayIterator(final VPackSlice slice) throws VPackValueTypeException {
if (!slice.isArray()) {
throw new VPackValueTypeException(ValueType.ARRAY);
}
if (size > 0) {
current = slice.getNth(0).getStart();
}
}

@Override
public VPackSlice next() {
final VPackSlice next;
if (hasNext()) {
next = slice.get((int) position++);
final VPackSlice next = getCurrent();
position++;
current += next.getByteSize();
return next;
} else {
throw new NoSuchElementException();
}
return next;
}

@Override
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/com/arangodb/velocypack/VPack.java
Original file line number Diff line number Diff line change
Expand Up @@ -667,8 +667,9 @@ private <T> Object deserializeArray(final VPackSlice parent, final VPackSlice vp
final int length = vpack.getLength();
final Class<?> componentType = ((Class<?>) type).getComponentType();
final Object value = Array.newInstance(componentType, length);
for (int i = 0; i < length; i++) {
Array.set(value, i, getValue(parent, vpack.get(i), componentType, null));
int i = 0;
for (final Iterator<VPackSlice> iterator = vpack.arrayIterator(); iterator.hasNext(); ) {
Array.set(value, i++, getValue(parent, iterator.next(), componentType, null));
}
return value;
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/arangodb/velocypack/VPackParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -324,8 +324,8 @@ private void parseArray(final VPackSlice value, final StringBuilder json, final
throws VPackException {
json.append(ARRAY_OPEN);
int added = 0;
for (int i = 0; i < value.getLength(); i++) {
final VPackSlice valueAt = value.get(i);
for (final Iterator<VPackSlice> iterator = value.arrayIterator(); iterator.hasNext();) {
final VPackSlice valueAt = iterator.next();
if (!valueAt.isNull() || includeNullValues) {
if (added++ > 0) {
json.append(SEPARATOR);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/arangodb/velocypack/VPackSlice.java
Original file line number Diff line number Diff line change
Expand Up @@ -783,7 +783,7 @@ private VPackSlice getNthKey(final int index) {
return new VPackSlice(vpack, start + getNthOffset(index));
}

private VPackSlice getNth(final int index) {
public VPackSlice getNth(final int index) {
return new VPackSlice(vpack, start + getNthOffset(index));
}

Expand Down
14 changes: 13 additions & 1 deletion src/test/java/com/arangodb/velocypack/VPackSliceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -873,7 +873,7 @@ private void checkArray(final long[] expected, final byte[] vpack) throws VPackE
}

@Test
public void arrayIterator() throws VPackException {
public void arrayIteratorOnCompactArray() throws VPackException {
final Collection<String> expected = Arrays.asList("a", "b", "c", "d", "e", "f");
final VPackSlice slice = new VPackSlice(new byte[] { 0x13, 0x0f, 0x41, 0x61, 0x41, 0x62, 0x41, 0x63, 0x41, 0x64,
0x41, 0x65, 0x41, 0x66, 0x06 });
Expand All @@ -885,6 +885,18 @@ public void arrayIterator() throws VPackException {
}
}

@Test
public void arrayIteratorOnArrayWithIndexTable() throws VPackException {
final Collection<Integer> expected = Arrays.asList(1, 42, 3);
final VPackSlice slice = new VPackSlice(new byte[] { 0x06, 0x0a, 0x03, 0x31, 0x20, 0x2a, 0x33, 0x03, 0x04, 0x06 });
final Iterator<VPackSlice> iteratorSlice = slice.arrayIterator();
for (final Integer i : expected) {
final VPackSlice next = iteratorSlice.next();
assertThat(next.isInteger(), is(true));
assertThat(next.getAsInt(), is(i));
}
}

@Test
public void objectIterator() throws VPackException {
// {"a":"test","b":"test","c":"test"}
Expand Down