Skip to content

Commit 178b340

Browse files
committed
Merge remote-tracking branch 'upstream/master' into tags
2 parents 07ee9ed + 6ba8cc9 commit 178b340

File tree

7 files changed

+93
-29
lines changed

7 files changed

+93
-29
lines changed

pom.xml

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
<properties>
2424
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
2525
<slf4j-api.version>1.7.13</slf4j-api.version>
26-
<jackson.core.version>2.9.10</jackson.core.version>
2726
<logback-classic.version>1.1.3</logback-classic.version>
2827
<hamcrest-all.version>1.3</hamcrest-all.version>
2928
<junit.version>4.12</junit.version>
@@ -109,6 +108,9 @@
109108
<configuration>
110109
<source>1.7</source>
111110
<target>1.7</target>
111+
<compilerArgs>
112+
<arg>-Werror</arg>
113+
</compilerArgs>
112114
<compilerArgument></compilerArgument>
113115
</configuration>
114116
</plugin>
@@ -241,10 +243,12 @@
241243
<version>${slf4j-api.version}</version>
242244
</dependency>
243245
<dependency>
244-
<groupId>com.fasterxml.jackson.core</groupId>
245-
<artifactId>jackson-core</artifactId>
246-
<version>${jackson.core.version}</version>
247-
</dependency>
246+
<groupId>com.fasterxml.jackson</groupId>
247+
<artifactId>jackson-bom</artifactId>
248+
<version>2.9.10.20191020</version>
249+
<scope>import</scope>
250+
<type>pom</type>
251+
</dependency>
248252
<dependency>
249253
<groupId>ch.qos.logback</groupId>
250254
<artifactId>logback-classic</artifactId>

src/main/java/com/arangodb/velocypack/VPack.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -876,7 +876,8 @@ private void addValue(
876876
} else if (type instanceof Class && Map.class.isAssignableFrom((Class<?>) type)) {
877877
serializeMap(name, value, builder, String.class, additionalFields);
878878
} else if (type instanceof Class && ((Class) type).isArray()) {
879-
serializeArray(name, value, builder);
879+
final Type elemType = ((Class<?>) type).getComponentType();
880+
serializeArray(name, value, builder, elemType);
880881
} else if (type instanceof Class && ((Class) type).isEnum()) {
881882
builder.add(name, Enum.class.cast(value).name());
882883
} else if (type != value.getClass()) {
@@ -888,13 +889,14 @@ private void addValue(
888889
}
889890
}
890891

891-
private void serializeArray(final String name, final Object value, final VPackBuilder builder)
892+
private void serializeArray(final String name, final Object value, final VPackBuilder builder, final Type type)
892893
throws NoSuchMethodException, IllegalAccessException, InvocationTargetException, VPackException {
893894
builder.add(name, ValueType.ARRAY);
894895
for (int i = 0; i < Array.getLength(value); i++) {
895896
final Object element = Array.get(value, i);
896897
if (element != null) {
897-
addValue(null, element.getClass(), element, builder, null, Collections.<String, Object>emptyMap());
898+
final Type t = type != null ? type : element.getClass();
899+
addValue(null, t, element, builder, null, Collections.<String, Object> emptyMap());
898900
} else {
899901
builder.add(ValueType.NULL);
900902
}

src/main/java/com/arangodb/velocypack/VPackBuilder.java

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import com.arangodb.velocypack.internal.DefaultVPackBuilderOptions;
4444
import com.arangodb.velocypack.internal.Value;
4545
import com.arangodb.velocypack.internal.util.NumberUtil;
46+
import com.fasterxml.jackson.core.io.CharTypes;
4647

4748
/**
4849
* @author Mark Vollmary
@@ -213,7 +214,7 @@ public void append(final VPackBuilder builder, final VPackSlice value) throws VP
213214
private int size;
214215
private final List<Integer> stack; // Start positions of open
215216
// objects/arrays
216-
private List<Integer>[] index; // Indices for starts
217+
private List<List<Integer>> index; // Indices for starts
217218
// of
218219
// subindex
219220
private boolean keyWritten; // indicates that in the current object the key
@@ -230,7 +231,7 @@ public VPackBuilder(final BuilderOptions options) {
230231
size = 0;
231232
buffer = new byte[10];
232233
stack = new ArrayList<Integer>();
233-
index = new List[4];
234+
index = new ArrayList<List<Integer>>(4);
234235
}
235236

236237
public BuilderOptions getOptions() {
@@ -809,7 +810,7 @@ private void appendSQLTimestamp(final Timestamp value) {
809810
append(value.getTime(), LONG_BYTES);
810811
}
811812

812-
private void appendString(final String value) throws VPackBuilderException {
813+
private void appendString(final String value) {
813814
final byte[] bytes = value.getBytes(StandardCharsets.UTF_8);
814815
final int length = bytes.length;
815816
if (length <= 126) {
@@ -856,11 +857,7 @@ private void addObject(final boolean unindexed) {
856857
private void addCompoundValue(final byte head) {
857858
// an Array or Object is started:
858859
stack.add(size);
859-
if (index.length < stack.size()) {
860-
// ensureCapacity
861-
index = Arrays.copyOf(index, index.length * 2);
862-
}
863-
index[stack.size() - 1] = new ArrayList<Integer>();
860+
index.add(stack.size() - 1, new ArrayList<Integer>());
864861
add(head);
865862
// Will be filled later with bytelength and nr subs
866863
size += 8;
@@ -872,12 +869,12 @@ private void appendLength(final long length) {
872869
}
873870

874871
private void reportAdd() {
875-
final Collection<Integer> depth = index[stack.size() - 1];
872+
final Collection<Integer> depth = index.get(stack.size() - 1);
876873
depth.add(size - stack.get(stack.size() - 1));
877874
}
878875

879876
private void cleanupAdd() {
880-
final List<Integer> depth = index[stack.size() - 1];
877+
final List<Integer> depth = index.get(stack.size() - 1);
881878
depth.remove(depth.size() - 1);
882879
}
883880

@@ -898,7 +895,7 @@ protected VPackBuilder close(final boolean sort)
898895
}
899896
final byte head = head();
900897
final boolean isArray = head == 0x06 || head == 0x13;
901-
final List<Integer> in = index[stack.size() - 1];
898+
final List<Integer> in = index.get(stack.size() - 1);
902899
final int tos = stack.get(stack.size() - 1);
903900
if (in.isEmpty()) {
904901
return closeEmptyArrayOrObject(tos, isArray);

src/main/java/com/arangodb/velocypack/VPackSlice.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import java.util.Iterator;
3232
import java.util.List;
3333
import java.util.Map.Entry;
34+
import java.lang.Comparable;
3435

3536
import com.arangodb.velocypack.exception.VPackException;
3637
import com.arangodb.velocypack.exception.VPackKeyTypeException;

src/main/java/com/arangodb/velocypack/VPackStringSlice.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
import java.nio.charset.StandardCharsets;
44

55
/**
6-
* Wrapper around a {@link ValueType.STRING} supporting fast bytewise comparison.
6+
* Wrapper around a {@link ValueType#STRING} supporting fast bytewise comparison.
77
*
8-
* @see https://github.com/arangodb/velocypack/blob/master/VelocyPack.md#objects
8+
* @see <a href="https://github.com/arangodb/velocypack/blob/master/VelocyPack.md#objects">VelocyPack Objects</a>
99
*/
1010
public class VPackStringSlice implements Comparable<VPackStringSlice> {
1111
private byte[] vpack;

src/test/java/com/arangodb/velocypack/Bench.java

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,19 @@
33
import org.openjdk.jmh.annotations.*;
44
import org.openjdk.jmh.infra.Blackhole;
55
import org.openjdk.jmh.profile.GCProfiler;
6-
import org.openjdk.jmh.profile.StackProfiler;
76
import org.openjdk.jmh.results.format.ResultFormatType;
87
import org.openjdk.jmh.runner.Runner;
98
import org.openjdk.jmh.runner.RunnerException;
109
import org.openjdk.jmh.runner.options.Options;
1110
import org.openjdk.jmh.runner.options.OptionsBuilder;
12-
import org.spf4j.stackmonitor.JmhFlightRecorderProfiler;
1311

1412
import java.io.IOException;
15-
import java.net.URISyntaxException;
1613
import java.nio.file.Files;
14+
import java.nio.file.Path;
1715
import java.nio.file.Paths;
16+
import java.text.SimpleDateFormat;
17+
import java.util.ArrayList;
18+
import java.util.Date;
1819
import java.util.concurrent.TimeUnit;
1920

2021
@Warmup(iterations = 8, time = 1, timeUnit = TimeUnit.SECONDS)
@@ -59,14 +60,23 @@ public VPackSlice buildKoko() {
5960

6061
}
6162

62-
public static void main(String[] args) throws RunnerException {
63+
public static void main(String[] args) throws RunnerException, IOException {
64+
String datetime = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss").format(new Date());
65+
Path target = Files.createDirectories(Paths.get("target", "jmh-result"));
66+
67+
ArrayList<String> jvmArgs = new ArrayList<>();
68+
jvmArgs.add("-Xms256m");
69+
jvmArgs.add("-Xmx256m");
70+
if (Integer.parseInt(System.getProperty("java.version").split("\\.")[0]) >= 11) {
71+
jvmArgs.add("-XX:StartFlightRecording=filename=" + target.resolve(datetime + ".jfr") + ",settings=profile");
72+
}
73+
6374
Options opt = new OptionsBuilder()
6475
.include(Bench.class.getSimpleName())
65-
// .addProfiler(GCProfiler.class)
66-
.addProfiler(JmhFlightRecorderProfiler.class)
67-
.jvmArgs("-Xmx256m", "-Xms256m", "-XX:+UnlockCommercialFeatures") // https://stackoverflow.com/a/37857708
76+
.addProfiler(GCProfiler.class)
77+
.jvmArgs(jvmArgs.toArray(new String[0]))
6878
.resultFormat(ResultFormatType.JSON)
69-
.result("target/jmh-result-" + System.currentTimeMillis() + ".json")
79+
.result(target.resolve(datetime + ".json").toString())
7080
.build();
7181

7282
new Runner(opt).run();

src/test/java/com/arangodb/velocypack/VPackSerializeDeserializeTest.java

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4074,6 +4074,56 @@ public void objectList() {
40744074
assertThat(entityOut.value.get(0) instanceof TestEntityTypeInfo, is(true));
40754075
}
40764076

4077+
static class TestEntityObjectArray {
4078+
Object[] value;
4079+
}
4080+
4081+
@Test
4082+
public void heterogeneousArray() {
4083+
final TestEntityObjectArray entity = new TestEntityObjectArray();
4084+
entity.value = new Object[]{new TestEntityTypeInfo()};
4085+
4086+
final VPack vpack = new VPack.Builder().build();
4087+
final VPackSlice slice = vpack.serialize(entity);
4088+
4089+
assertThat(slice.isObject(), is(true));
4090+
assertThat(slice.get("value").isArray(), is(true));
4091+
assertThat(slice.get("value").get(0).isObject(), is(true));
4092+
assertThat(slice.get("value").get(0).get("_class").isString(), is(true));
4093+
assertThat(slice.get("value").get(0).get("_class").getAsString(),
4094+
is("com.arangodb.velocypack.VPackSerializeDeserializeTest$TestEntityTypeInfo"));
4095+
4096+
final TestEntityObjectArray entityOut = vpack.deserialize(slice, TestEntityObjectArray.class);
4097+
assertThat(entityOut, is(notNullValue()));
4098+
assertThat(entityOut.value, is(notNullValue()));
4099+
assertThat(entityOut.value.length, is(1));
4100+
assertThat(entityOut.value[0] instanceof TestEntityTypeInfo, is(true));
4101+
}
4102+
4103+
static class TestEntityTypeInfoArray {
4104+
TestEntityTypeInfo[] value;
4105+
}
4106+
4107+
@Test
4108+
public void homogeneousArray() {
4109+
final TestEntityTypeInfoArray entity = new TestEntityTypeInfoArray();
4110+
entity.value = new TestEntityTypeInfo[]{new TestEntityTypeInfo()};
4111+
4112+
final VPack vpack = new VPack.Builder().build();
4113+
final VPackSlice slice = vpack.serialize(entity);
4114+
4115+
assertThat(slice.isObject(), is(true));
4116+
assertThat(slice.get("value").isArray(), is(true));
4117+
assertThat(slice.get("value").get(0).isObject(), is(true));
4118+
assertThat(slice.get("value").get(0).get("_class").isNone(), is(true));
4119+
4120+
final TestEntityTypeInfoArray entityOut = vpack.deserialize(slice, TestEntityTypeInfoArray.class);
4121+
assertThat(entityOut, is(notNullValue()));
4122+
assertThat(entityOut.value, is(notNullValue()));
4123+
assertThat(entityOut.value.length, is(1));
4124+
assertThat(entityOut.value[0], notNullValue());
4125+
}
4126+
40774127
public static class Limb {
40784128
}
40794129

0 commit comments

Comments
 (0)