@Test @LoadGraphWith(LoadGraphWith.GraphData.MODERN) public void shouldSerializeTree() 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 Tree before = g.V(convertToVertexId("marko")).out().properties("name").tree().next(); final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); gryoWriter.writeObject(outputStream, before); final ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray()); final Tree after = gryoReader.readObject(inputStream, Tree.class); assertNotNull(after); //The following assertions should be sufficent. assertThat("Type does not match", after, instanceOf(Tree.class)); assertEquals("The objects differ", after, before); } }
@Test public void testGeoShapeSerializationReadWriteAsGryo() throws Exception { GraphOfTheGodsFactory.loadWithoutMixedIndex(graph, true); graph.io(IoCore.gryo()).writeGraph("/tmp/test.kryo"); clearGraph(config); open(config); graph.io(IoCore.gryo()).readGraph("/tmp/test.kryo"); TitanIndexTest.assertGraphOfTheGods(graph); } }
@Parameterized.Parameters(name = "{0}") public static Iterable<Object[]> data() { final GraphSONMapper v1mapper = GraphSONMapper.build().version(GraphSONVersion.V1_0).typeInfo(TypeInfo.PARTIAL_TYPES).addRegistry(JanusGraphIoRegistryV1d0.getInstance()).create(); final GraphSONMapper v2mapper = GraphSONMapper.build().version(GraphSONVersion.V2_0).typeInfo(TypeInfo.PARTIAL_TYPES).addRegistry(JanusGraphIoRegistry.getInstance()).create(); final GraphSONMapper v3mapper = GraphSONMapper.build().version(GraphSONVersion.V3_0).typeInfo(TypeInfo.PARTIAL_TYPES).addRegistry(JanusGraphIoRegistry.getInstance()).create(); return Arrays.asList(new Object[][]{ {"graphson-v1-embedded", (Function<Graph, GraphReader>) g -> GraphSONReader.build().mapper(v1mapper).create(), (Function<Graph, GraphWriter>) g -> GraphSONWriter.build().mapper(v1mapper).create()}, {"graphson-v2-embedded", (Function<Graph, GraphReader>) g -> GraphSONReader.build().mapper(v2mapper).create(), (Function<Graph, GraphWriter>) g -> GraphSONWriter.build().mapper(v2mapper).create()}, {"graphson-v3", (Function<Graph, GraphReader>) g -> GraphSONReader.build().mapper(v3mapper).create(), (Function<Graph, GraphWriter>) g -> GraphSONWriter.build().mapper(v3mapper).create()}, {"gryo", (Function<Graph, GraphReader>) g -> g.io(IoCore.gryo()).reader().mapper(g.io(IoCore.gryo()).mapper().create()).create(), (Function<Graph, GraphWriter>) g -> g.io(IoCore.gryo()).writer().mapper(g.io(IoCore.gryo()).mapper().create()).create()} }); }
private Pair<StarGraph, Integer> serializeDeserialize(final StarGraph starGraph) { final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); try { graph.io(IoCore.gryo()).writer().create().writeObject(outputStream, starGraph); return Pair.with(graph.io(IoCore.gryo()).reader().create().readObject(new ByteArrayInputStream(outputStream.toByteArray()), StarGraph.class), outputStream.size()); } catch (IOException ioe) { throw new RuntimeException(ioe); } } }
@Parameterized.Parameters(name = "{0}") public static Iterable<Object[]> data() { final SimpleModule moduleV1d0 = new SimpleModule(); moduleV1d0.addSerializer(CustomId.class, new CustomId.CustomIdJacksonSerializerV1d0()); final SimpleModule moduleV2d0 = new CustomId.CustomIdTinkerPopJacksonModuleV2d0(); final SimpleModule modulev3d0 = new CustomId.CustomIdTinkerPopJacksonModuleV3d0(); return Arrays.asList(new Object[][]{ {"graphson-v1-embedded", true, (Function<Graph, GraphReader>) g -> g.io(GraphSONIo.build(GraphSONVersion.V1_0)).reader().mapper(g.io(GraphSONIo.build(GraphSONVersion.V1_0)).mapper().addCustomModule(moduleV1d0).typeInfo(TypeInfo.PARTIAL_TYPES).create()).create(), (Function<Graph, GraphWriter>) g -> g.io(GraphSONIo.build(GraphSONVersion.V1_0)).writer().mapper(g.io(GraphSONIo.build(GraphSONVersion.V1_0)).mapper().addCustomModule(moduleV1d0).typeInfo(TypeInfo.PARTIAL_TYPES).create()).create()}, {"graphson-v2-embedded", true, (Function<Graph, GraphReader>) g -> g.io(GraphSONIo.build(GraphSONVersion.V2_0)).reader().mapper(g.io(GraphSONIo.build(GraphSONVersion.V2_0)).mapper().addCustomModule(moduleV2d0).typeInfo(TypeInfo.PARTIAL_TYPES).create()).create(), (Function<Graph, GraphWriter>) g -> g.io(GraphSONIo.build(GraphSONVersion.V2_0)).writer().mapper(g.io(GraphSONIo.build(GraphSONVersion.V2_0)).mapper().addCustomModule(moduleV2d0).typeInfo(TypeInfo.PARTIAL_TYPES).create()).create()}, {"graphson-v3", true, (Function<Graph, GraphReader>) g -> g.io(GraphSONIo.build(GraphSONVersion.V3_0)).reader().mapper(g.io(GraphSONIo.build(GraphSONVersion.V3_0)).mapper().addCustomModule(modulev3d0).create()).create(), (Function<Graph, GraphWriter>) g -> g.io(GraphSONIo.build(GraphSONVersion.V3_0)).writer().mapper(g.io(GraphSONIo.build(GraphSONVersion.V3_0)).mapper().addCustomModule(modulev3d0).create()).create()}, {"gryo-v1", true, (Function<Graph, GraphReader>) g -> g.io(GryoIo.build(GryoVersion.V1_0)).reader().mapper(g.io(GryoIo.build(GryoVersion.V1_0)).mapper().version(GryoVersion.V1_0).addCustom(CustomId.class).create()).create(), (Function<Graph, GraphWriter>) g -> g.io(GryoIo.build(GryoVersion.V1_0)).writer().mapper(g.io(GryoIo.build(GryoVersion.V1_0)).mapper().version(GryoVersion.V1_0).addCustom(CustomId.class).create()).create()}, {"gryo-v3", true, (Function<Graph, GraphReader>) g -> g.io(GryoIo.build(GryoVersion.V3_0)).reader().mapper(g.io(GryoIo.build(GryoVersion.V3_0)).mapper().version(GryoVersion.V3_0).addCustom(CustomId.class).create()).create(), (Function<Graph, GraphWriter>) g -> g.io(GryoIo.build(GryoVersion.V3_0)).writer().mapper(g.io(GryoIo.build(GryoVersion.V3_0)).mapper().version(GryoVersion.V3_0).addCustom(CustomId.class).create()).create()} }); }
/** * {@inheritDoc} */ @Override public void writeGraph(final String file) throws IOException { try (final OutputStream out = new FileOutputStream(file)) { writer().create().writeGraph(out, graph); } }
/** * {@inheritDoc} */ @Override public void readGraph(final String file) throws IOException { try (final InputStream in = new FileInputStream(file)) { reader().create().readGraph(in, graph); } }
private void loadGraph() { final File f = new File(graphLocation); if (f.exists() && f.isFile()) { try { if (graphFormat.equals("graphml")) { io(IoCore.graphml()).readGraph(graphLocation); } else if (graphFormat.equals("graphson")) { io(IoCore.graphson()).readGraph(graphLocation); } else if (graphFormat.equals("gryo")) { io(IoCore.gryo()).readGraph(graphLocation); } else { io(IoCore.createIoBuilder(graphFormat)).readGraph(graphLocation); } } catch (Exception ex) { throw new RuntimeException(String.format("Could not load graph at %s with %s", graphLocation, graphFormat), ex); } } }
/** * {@inheritDoc} */ @Override public GryoReader.Builder reader() { return GryoReader.build().mapper(mapper().create()); }
private void saveGraph() { final File f = new File(graphLocation); if (f.exists()) { f.delete(); } else { final File parent = f.getParentFile(); // the parent would be null in the case of an relative path if the graphLocation was simply: "f.gryo" if (parent != null && !parent.exists()) { parent.mkdirs(); } } try { if (graphFormat.equals("graphml")) { io(IoCore.graphml()).writeGraph(graphLocation); } else if (graphFormat.equals("graphson")) { io(IoCore.graphson()).writeGraph(graphLocation); } else if (graphFormat.equals("gryo")) { io(IoCore.gryo()).writeGraph(graphLocation); } else { io(IoCore.createIoBuilder(graphFormat)).writeGraph(graphLocation); } } catch (Exception ex) { throw new RuntimeException(String.format("Could not save graph at %s with %s", graphLocation, graphFormat), ex); } }
/** * Creates a basic Gryo-based {@link org.apache.tinkerpop.gremlin.structure.io.Io.Builder}. */ public static Io.Builder<GryoIo> gryo() { return GryoIo.build(); }
private Pair<StarGraph, Integer> serializeDeserialize(final StarGraph starGraph) { final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); try { graph.io(IoCore.gryo()).writer().create().writeObject(outputStream, starGraph); return Pair.with(graph.io(IoCore.gryo()).reader().create().readObject(new ByteArrayInputStream(outputStream.toByteArray()), StarGraph.class), outputStream.size()); } catch (IOException ioe) { throw new RuntimeException(ioe); } }
@Parameterized.Parameters(name = "{0}") public static Iterable<Object[]> data() { final SimpleModule moduleV1d0 = new SimpleModule(); moduleV1d0.addSerializer(CustomId.class, new CustomId.CustomIdJacksonSerializerV1d0()); final SimpleModule moduleV2d0 = new CustomId.CustomIdTinkerPopJacksonModuleV2d0(); final SimpleModule modulev3d0 = new CustomId.CustomIdTinkerPopJacksonModuleV3d0(); return Arrays.asList(new Object[][]{ {"graphson-v1-embedded", true, (Function<Graph, GraphReader>) g -> g.io(GraphSONIo.build(GraphSONVersion.V1_0)).reader().mapper(g.io(GraphSONIo.build(GraphSONVersion.V1_0)).mapper().addCustomModule(moduleV1d0).typeInfo(TypeInfo.PARTIAL_TYPES).create()).create(), (Function<Graph, GraphWriter>) g -> g.io(GraphSONIo.build(GraphSONVersion.V1_0)).writer().mapper(g.io(GraphSONIo.build(GraphSONVersion.V1_0)).mapper().addCustomModule(moduleV1d0).typeInfo(TypeInfo.PARTIAL_TYPES).create()).create()}, {"graphson-v2-embedded", true, (Function<Graph, GraphReader>) g -> g.io(GraphSONIo.build(GraphSONVersion.V2_0)).reader().mapper(g.io(GraphSONIo.build(GraphSONVersion.V2_0)).mapper().addCustomModule(moduleV2d0).typeInfo(TypeInfo.PARTIAL_TYPES).create()).create(), (Function<Graph, GraphWriter>) g -> g.io(GraphSONIo.build(GraphSONVersion.V2_0)).writer().mapper(g.io(GraphSONIo.build(GraphSONVersion.V2_0)).mapper().addCustomModule(moduleV2d0).typeInfo(TypeInfo.PARTIAL_TYPES).create()).create()}, {"graphson-v3", true, (Function<Graph, GraphReader>) g -> g.io(GraphSONIo.build(GraphSONVersion.V3_0)).reader().mapper(g.io(GraphSONIo.build(GraphSONVersion.V3_0)).mapper().addCustomModule(modulev3d0).create()).create(), (Function<Graph, GraphWriter>) g -> g.io(GraphSONIo.build(GraphSONVersion.V3_0)).writer().mapper(g.io(GraphSONIo.build(GraphSONVersion.V3_0)).mapper().addCustomModule(modulev3d0).create()).create()}, {"gryo-v1", true, (Function<Graph, GraphReader>) g -> g.io(GryoIo.build(GryoVersion.V1_0)).reader().mapper(g.io(GryoIo.build(GryoVersion.V1_0)).mapper().version(GryoVersion.V1_0).addCustom(CustomId.class).create()).create(), (Function<Graph, GraphWriter>) g -> g.io(GryoIo.build(GryoVersion.V1_0)).writer().mapper(g.io(GryoIo.build(GryoVersion.V1_0)).mapper().version(GryoVersion.V1_0).addCustom(CustomId.class).create()).create()}, {"gryo-v3", true, (Function<Graph, GraphReader>) g -> g.io(GryoIo.build(GryoVersion.V3_0)).reader().mapper(g.io(GryoIo.build(GryoVersion.V3_0)).mapper().version(GryoVersion.V3_0).addCustom(CustomId.class).create()).create(), (Function<Graph, GraphWriter>) g -> g.io(GryoIo.build(GryoVersion.V3_0)).writer().mapper(g.io(GryoIo.build(GryoVersion.V3_0)).mapper().version(GryoVersion.V3_0).addCustom(CustomId.class).create()).create()} }); }
final GryoWriter writer = graph.io(IoCore.gryo()).writer().create(); writer.writeObject(outputStream, DetachedFactory.detach(vertex, true)); vertex.edges(Direction.BOTH).forEachRemaining(e -> writer.writeObject(outputStream, DetachedFactory.detach(e, true)));
/** * Generate the graph in {@link #createGratefulDead()} into an existing graph. */ public static void generateGratefulDead(final TinkerGraph graph) { final InputStream stream = TinkerFactory.class.getResourceAsStream("grateful-dead.kryo"); try { graph.io(gryo()).reader().create().readGraph(stream, graph); } catch (Exception ex) { throw new IllegalStateException(ex); } }
private void loadGraph() { final File f = new File(graphLocation); if (f.exists() && f.isFile()) { try { if (graphFormat.equals("graphml")) { io(IoCore.graphml()).readGraph(graphLocation); } else if (graphFormat.equals("graphson")) { io(IoCore.graphson()).readGraph(graphLocation); } else if (graphFormat.equals("gryo")) { io(IoCore.gryo()).readGraph(graphLocation); } else { io(IoCore.createIoBuilder(graphFormat)).readGraph(graphLocation); } } catch (Exception ex) { throw new RuntimeException(String.format("Could not load graph at %s with %s", graphLocation, graphFormat), ex); } } }
/** * {@inheritDoc} */ @Override public GryoWriter.Builder writer() { return GryoWriter.build().mapper(mapper().create()); }
private void saveGraph() { final File f = new File(graphLocation); if (f.exists()) { f.delete(); } else { final File parent = f.getParentFile(); // the parent would be null in the case of an relative path if the graphLocation was simply: "f.gryo" if (parent != null && !parent.exists()) { parent.mkdirs(); } } try { if (graphFormat.equals("graphml")) { io(IoCore.graphml()).writeGraph(graphLocation); } else if (graphFormat.equals("graphson")) { io(IoCore.graphson()).writeGraph(graphLocation); } else if (graphFormat.equals("gryo")) { io(IoCore.gryo()).writeGraph(graphLocation); } else { io(IoCore.createIoBuilder(graphFormat)).writeGraph(graphLocation); } } catch (Exception ex) { throw new RuntimeException(String.format("Could not save graph at %s with %s", graphLocation, graphFormat), ex); } }
/** * Create a new builder using the default version of Gryo - v3. */ public static Io.Builder<GryoIo> build() { return build(GryoVersion.V3_0); }
@Test @LoadGraphWith(LoadGraphWith.GraphData.MODERN) public void shouldSerializeTree() 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 Tree before = g.V(convertToVertexId("marko")).out().properties("name").tree().next(); final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); gryoWriter.writeObject(outputStream, before); final ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray()); final Tree after = gryoReader.readObject(inputStream, Tree.class); assertNotNull(after); //The following assertions should be sufficent. assertThat("Type does not match", after, instanceOf(Tree.class)); assertEquals("The objects differ", after, before); } }