private static Map<String, Object> metricsToMap(final Metrics metrics) { final Map<String, Object> m = new HashMap<>(); m.put(GraphSONTokens.ID, metrics.getId()); m.put(GraphSONTokens.NAME, metrics.getName()); m.put(GraphSONTokens.COUNTS, metrics.getCounts()); m.put(GraphSONTokens.DURATION, metrics.getDuration(TimeUnit.NANOSECONDS) / 1000000d); if (!metrics.getAnnotations().isEmpty()) { m.put(GraphSONTokens.ANNOTATIONS, metrics.getAnnotations()); } if (!metrics.getNested().isEmpty()) { final List<Map<String, Object>> nested = new ArrayList<>(); metrics.getNested().forEach(it -> nested.add(metricsToMap(it))); m.put(GraphSONTokens.METRICS, nested); } return m; } }
/** * Create a {@code MutableMetrics} from an immutable one. */ public MutableMetrics(final Metrics other) { this.id = other.getId(); this.name = other.getName(); this.annotations.putAll(other.getAnnotations()); this.durationNs = other.getDuration(TimeUnit.NANOSECONDS); other.getCounts().forEach((key, count) -> this.counts.put(key, new AtomicLong(count))); other.getNested().forEach(nested -> this.addNested(new MutableMetrics(nested))); }
@Override public void serialize(final Metrics metrics, final JsonGenerator jsonGenerator, final SerializerProvider serializerProvider) throws IOException { final Map<String, Object> m = new HashMap<>(); m.put(GraphSONTokens.ID, metrics.getId()); m.put(GraphSONTokens.NAME, metrics.getName()); m.put(GraphSONTokens.COUNTS, metrics.getCounts()); m.put(GraphSONTokens.DURATION, metrics.getDuration(TimeUnit.NANOSECONDS) / 1000000d); if (!metrics.getAnnotations().isEmpty()) { m.put(GraphSONTokens.ANNOTATIONS, metrics.getAnnotations()); } if (!metrics.getNested().isEmpty()) { final List<Metrics> nested = new ArrayList<>(); metrics.getNested().forEach(it -> nested.add(it)); m.put(GraphSONTokens.METRICS, nested); } jsonGenerator.writeObject(m); } }
@Override public void serialize(final Metrics metrics, final JsonGenerator jsonGenerator, final SerializerProvider serializerProvider) throws IOException { final Map<String, Object> m = new HashMap<>(); m.put(GraphSONTokens.ID, metrics.getId()); m.put(GraphSONTokens.NAME, metrics.getName()); m.put(GraphSONTokens.COUNTS, metrics.getCounts()); m.put(GraphSONTokens.DURATION, metrics.getDuration(TimeUnit.NANOSECONDS) / 1000000d); if (!metrics.getAnnotations().isEmpty()) { m.put(GraphSONTokens.ANNOTATIONS, metrics.getAnnotations()); } if (!metrics.getNested().isEmpty()) { final List<Metrics> nested = new ArrayList<>(); metrics.getNested().forEach(it -> nested.add(it)); m.put(GraphSONTokens.METRICS, nested); } jsonGenerator.writeObject(m); } }
@Test @LoadGraphWith(LoadGraphWith.GraphData.MODERN) public void shouldSerializeTraversalMetrics() throws Exception { final GryoIo gryoIo = graph.io(GryoIo.build(GryoVersion.V1_0)); final GryoWriter gryoWriter = gryoIo.writer().create(); final GryoReader gryoReader = gryoIo.reader().create(); final TraversalMetrics before = g.V().both().profile().next(); final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); gryoWriter.writeObject(outputStream, before); final ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray()); final TraversalMetrics after = gryoReader.readObject(inputStream, TraversalMetrics.class); assertNotNull(after); assertEquals(before.getMetrics().size(), after.getMetrics().size()); assertEquals(before.getDuration(TimeUnit.MILLISECONDS), after.getDuration(TimeUnit.MILLISECONDS)); assertEquals(before.getMetrics(0).getCounts(), after.getMetrics().stream().findFirst().get().getCounts()); }
@Test @LoadGraphWith(LoadGraphWith.GraphData.MODERN) public void shouldSerializeTraversalMetrics() throws Exception { final GryoIo gryoIo = graph.io(GryoIo.build(GryoVersion.V3_0)); final GryoWriter gryoWriter = gryoIo.writer().create(); final GryoReader gryoReader = gryoIo.reader().create(); final TraversalMetrics before = (TraversalMetrics) g.V().both().profile().next(); final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); gryoWriter.writeObject(outputStream, before); final ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray()); final TraversalMetrics after = gryoReader.readObject(inputStream, TraversalMetrics.class); assertNotNull(after); assertEquals(before.getMetrics().size(), after.getMetrics().size()); assertEquals(before.getDuration(TimeUnit.MILLISECONDS), after.getDuration(TimeUnit.MILLISECONDS)); assertEquals(before.getMetrics(0).getCounts(), after.getMetrics().stream().findFirst().get().getCounts()); }
@Override public <O extends OutputShim> void write(final KryoShim<?, O> kryo, final O output, final Metrics object) { output.writeString(object.getId()); output.writeString(object.getName()); output.writeDouble(object.getDuration(TimeUnit.NANOSECONDS) / 1000000d); kryo.writeObject(output, object.getCounts()); // annotations is a synchronized LinkedHashMap - get rid of the "synch" for serialization as gryo // doesn't know how to deserialize that well and LinkedHashMap should work with 3.3.x and previous final Map<String, Object> annotations = new LinkedHashMap<>(); object.getAnnotations().forEach(annotations::put); kryo.writeObject(output, annotations); // kryo might have a problem with LinkedHashMap value collections. can't recreate it independently but // it gets fixed with standard collections for some reason. final List<Metrics> nested = new ArrayList<>(object.getNested()); kryo.writeObject(output, nested); }
@Test @LoadGraphWith(LoadGraphWith.GraphData.MODERN) public void shouldSerializeTraversalMetrics() throws Exception { final ObjectMapper mapper = graph.io(GraphSONIo.build(GraphSONVersion.V2_0)).mapper().version(GraphSONVersion.V2_0).create().createMapper(); final TraversalMetrics before = g.V().both().profile().next(); final String json = mapper.writeValueAsString(before); final TraversalMetrics after = mapper.readValue(json, TraversalMetrics.class); assertNotNull(after); assertEquals(before.getMetrics().size(), after.getMetrics().size()); assertEquals(before.getDuration(TimeUnit.MILLISECONDS), after.getDuration(TimeUnit.MILLISECONDS)); assertEquals(before.getMetrics().size(), after.getMetrics().size()); before.getMetrics().forEach(b -> { final Optional<? extends Metrics> mFromA = after.getMetrics().stream().filter(a -> b.getId().equals(a.getId())).findFirst(); if (mFromA.isPresent()) { final Metrics m = mFromA.get(); assertEquals(b.getAnnotations(), m.getAnnotations()); assertEquals(b.getCounts(), m.getCounts()); assertEquals(b.getName(), m.getName()); assertEquals(b.getDuration(TimeUnit.MILLISECONDS), m.getDuration(TimeUnit.MILLISECONDS)); } else { fail("Metrics were not present after deserialization"); } }); }
@Test @LoadGraphWith(LoadGraphWith.GraphData.MODERN) public void shouldSerializeTraversalMetrics() throws Exception { final ObjectMapper mapper = graph.io(GraphSONIo.build(GraphSONVersion.V3_0)).mapper().version(GraphSONVersion.V3_0).create().createMapper(); final TraversalMetrics before = g.V().both().profile().next(); final String json = mapper.writeValueAsString(before); final TraversalMetrics after = mapper.readValue(json, TraversalMetrics.class); assertNotNull(after); assertEquals(before.getMetrics().size(), after.getMetrics().size()); assertEquals(before.getDuration(TimeUnit.MILLISECONDS), after.getDuration(TimeUnit.MILLISECONDS)); assertEquals(before.getMetrics().size(), after.getMetrics().size()); before.getMetrics().forEach(b -> { final Optional<? extends Metrics> mFromA = after.getMetrics().stream().filter(a -> b.getId().equals(a.getId())).findFirst(); if (mFromA.isPresent()) { final Metrics m = mFromA.get(); assertEquals(b.getAnnotations(), m.getAnnotations()); assertEquals(b.getCounts(), m.getCounts()); assertEquals(b.getName(), m.getName()); assertEquals(b.getDuration(TimeUnit.MILLISECONDS), m.getDuration(TimeUnit.MILLISECONDS)); } else { fail("Metrics were not present after deserialization"); } }); }
@Override protected ByteBuf writeValue(final Metrics value, final ByteBufAllocator allocator, final GraphBinaryWriter context) throws SerializationException { return allocator.compositeBuffer(6).addComponents(true, context.writeValue(value.getId(), allocator, false), context.writeValue(value.getName(), allocator, false), context.writeValue(value.getDuration(TimeUnit.NANOSECONDS), allocator, false), context.writeValue(value.getCounts(), allocator, false), context.writeValue(value.getAnnotations(), allocator, false), // Avoid changing type to List collectionSerializer.writeValue(value.getNested(), allocator, context)); } }
/** * Create a {@code MutableMetrics} from an immutable one. */ public MutableMetrics(final Metrics other) { this.id = other.getId(); this.name = other.getName(); this.annotations.putAll(other.getAnnotations()); this.durationNs = other.getDuration(TimeUnit.NANOSECONDS); other.getCounts().forEach((key, count) -> this.counts.put(key, new AtomicLong(count))); other.getNested().forEach(nested -> this.addNested(new MutableMetrics(nested))); }
private static Map<String, Object> metricsToMap(final Metrics metrics) { final Map<String, Object> m = new HashMap<>(); m.put(GraphSONTokens.ID, metrics.getId()); m.put(GraphSONTokens.NAME, metrics.getName()); m.put(GraphSONTokens.COUNTS, metrics.getCounts()); m.put(GraphSONTokens.DURATION, metrics.getDuration(TimeUnit.NANOSECONDS) / 1000000d); if (!metrics.getAnnotations().isEmpty()) { m.put(GraphSONTokens.ANNOTATIONS, metrics.getAnnotations()); } if (!metrics.getNested().isEmpty()) { final List<Map<String, Object>> nested = new ArrayList<>(); metrics.getNested().forEach(it -> nested.add(metricsToMap(it))); m.put(GraphSONTokens.METRICS, nested); } return m; } }
@Override public void serialize(final Metrics metrics, final JsonGenerator jsonGenerator, final SerializerProvider serializerProvider) throws IOException { final Map<String, Object> m = new HashMap<>(); m.put(GraphSONTokens.ID, metrics.getId()); m.put(GraphSONTokens.NAME, metrics.getName()); m.put(GraphSONTokens.COUNTS, metrics.getCounts()); m.put(GraphSONTokens.DURATION, metrics.getDuration(TimeUnit.NANOSECONDS) / 1000000d); if (!metrics.getAnnotations().isEmpty()) { m.put(GraphSONTokens.ANNOTATIONS, metrics.getAnnotations()); } if (!metrics.getNested().isEmpty()) { final List<Metrics> nested = new ArrayList<>(); metrics.getNested().forEach(it -> nested.add(it)); m.put(GraphSONTokens.METRICS, nested); } jsonGenerator.writeObject(m); } }
@Override public void serialize(final Metrics metrics, final JsonGenerator jsonGenerator, final SerializerProvider serializerProvider) throws IOException { final Map<String, Object> m = new HashMap<>(); m.put(GraphSONTokens.ID, metrics.getId()); m.put(GraphSONTokens.NAME, metrics.getName()); m.put(GraphSONTokens.COUNTS, metrics.getCounts()); m.put(GraphSONTokens.DURATION, metrics.getDuration(TimeUnit.NANOSECONDS) / 1000000d); if (!metrics.getAnnotations().isEmpty()) { m.put(GraphSONTokens.ANNOTATIONS, metrics.getAnnotations()); } if (!metrics.getNested().isEmpty()) { final List<Metrics> nested = new ArrayList<>(); metrics.getNested().forEach(it -> nested.add(it)); m.put(GraphSONTokens.METRICS, nested); } jsonGenerator.writeObject(m); } }
@Test @LoadGraphWith(LoadGraphWith.GraphData.MODERN) public void shouldSerializeTraversalMetrics() throws Exception { final GryoIo gryoIo = graph.io(GryoIo.build(GryoVersion.V1_0)); final GryoWriter gryoWriter = gryoIo.writer().create(); final GryoReader gryoReader = gryoIo.reader().create(); final TraversalMetrics before = g.V().both().profile().next(); final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); gryoWriter.writeObject(outputStream, before); final ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray()); final TraversalMetrics after = gryoReader.readObject(inputStream, TraversalMetrics.class); assertNotNull(after); assertEquals(before.getMetrics().size(), after.getMetrics().size()); assertEquals(before.getDuration(TimeUnit.MILLISECONDS), after.getDuration(TimeUnit.MILLISECONDS)); assertEquals(before.getMetrics(0).getCounts(), after.getMetrics().stream().findFirst().get().getCounts()); }
@Test @LoadGraphWith(LoadGraphWith.GraphData.MODERN) public void shouldSerializeTraversalMetrics() throws Exception { final GryoIo gryoIo = graph.io(GryoIo.build(GryoVersion.V3_0)); final GryoWriter gryoWriter = gryoIo.writer().create(); final GryoReader gryoReader = gryoIo.reader().create(); final TraversalMetrics before = (TraversalMetrics) g.V().both().profile().next(); final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); gryoWriter.writeObject(outputStream, before); final ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray()); final TraversalMetrics after = gryoReader.readObject(inputStream, TraversalMetrics.class); assertNotNull(after); assertEquals(before.getMetrics().size(), after.getMetrics().size()); assertEquals(before.getDuration(TimeUnit.MILLISECONDS), after.getDuration(TimeUnit.MILLISECONDS)); assertEquals(before.getMetrics(0).getCounts(), after.getMetrics().stream().findFirst().get().getCounts()); }
@Override public <O extends OutputShim> void write(final KryoShim<?, O> kryo, final O output, final Metrics object) { output.writeString(object.getId()); output.writeString(object.getName()); output.writeDouble(object.getDuration(TimeUnit.NANOSECONDS) / 1000000d); kryo.writeObject(output, object.getCounts()); // annotations is a synchronized LinkedHashMap - get rid of the "synch" for serialization as gryo // doesn't know how to deserialize that well and LinkedHashMap should work with 3.3.x and previous final Map<String, Object> annotations = new LinkedHashMap<>(); object.getAnnotations().forEach(annotations::put); kryo.writeObject(output, annotations); // kryo might have a problem with LinkedHashMap value collections. can't recreate it independently but // it gets fixed with standard collections for some reason. final List<Metrics> nested = new ArrayList<>(object.getNested()); kryo.writeObject(output, nested); }
@Test @LoadGraphWith(LoadGraphWith.GraphData.MODERN) public void shouldSerializeTraversalMetrics() throws Exception { final ObjectMapper mapper = graph.io(GraphSONIo.build(GraphSONVersion.V2_0)).mapper().version(GraphSONVersion.V2_0).create().createMapper(); final TraversalMetrics before = g.V().both().profile().next(); final String json = mapper.writeValueAsString(before); final TraversalMetrics after = mapper.readValue(json, TraversalMetrics.class); assertNotNull(after); assertEquals(before.getMetrics().size(), after.getMetrics().size()); assertEquals(before.getDuration(TimeUnit.MILLISECONDS), after.getDuration(TimeUnit.MILLISECONDS)); assertEquals(before.getMetrics().size(), after.getMetrics().size()); before.getMetrics().forEach(b -> { final Optional<? extends Metrics> mFromA = after.getMetrics().stream().filter(a -> b.getId().equals(a.getId())).findFirst(); if (mFromA.isPresent()) { final Metrics m = mFromA.get(); assertEquals(b.getAnnotations(), m.getAnnotations()); assertEquals(b.getCounts(), m.getCounts()); assertEquals(b.getName(), m.getName()); assertEquals(b.getDuration(TimeUnit.MILLISECONDS), m.getDuration(TimeUnit.MILLISECONDS)); } else { fail("Metrics were not present after deserialization"); } }); }
@Test @LoadGraphWith(LoadGraphWith.GraphData.MODERN) public void shouldSerializeTraversalMetrics() throws Exception { final ObjectMapper mapper = graph.io(GraphSONIo.build(GraphSONVersion.V3_0)).mapper().version(GraphSONVersion.V3_0).create().createMapper(); final TraversalMetrics before = g.V().both().profile().next(); final String json = mapper.writeValueAsString(before); final TraversalMetrics after = mapper.readValue(json, TraversalMetrics.class); assertNotNull(after); assertEquals(before.getMetrics().size(), after.getMetrics().size()); assertEquals(before.getDuration(TimeUnit.MILLISECONDS), after.getDuration(TimeUnit.MILLISECONDS)); assertEquals(before.getMetrics().size(), after.getMetrics().size()); before.getMetrics().forEach(b -> { final Optional<? extends Metrics> mFromA = after.getMetrics().stream().filter(a -> b.getId().equals(a.getId())).findFirst(); if (mFromA.isPresent()) { final Metrics m = mFromA.get(); assertEquals(b.getAnnotations(), m.getAnnotations()); assertEquals(b.getCounts(), m.getCounts()); assertEquals(b.getName(), m.getName()); assertEquals(b.getDuration(TimeUnit.MILLISECONDS), m.getDuration(TimeUnit.MILLISECONDS)); } else { fail("Metrics were not present after deserialization"); } }); }
@Override public ByteBuf writeValue(final Metrics value, final ByteBufAllocator allocator, final GraphBinaryWriter context) throws SerializationException { return allocator.compositeBuffer(6).addComponents(true, context.writeValue(value.getId(), allocator, false), context.writeValue(value.getName(), allocator, false), context.writeValue(value.getDuration(TimeUnit.NANOSECONDS), allocator, false), context.writeValue(value.getCounts(), allocator, false), context.writeValue(value.getAnnotations(), allocator, false), // Avoid changing type to List collectionSerializer.writeValue(value.getNested(), allocator, context)); } }