private void testSerialization(Function<Geoshape,Geoshape> makeGeoshape) throws Exception { if (makeGeoshape != null) { addGeoshape(makeGeoshape); } GraphWriter writer = writerMaker.apply(graph); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); writer.writeGraph(outputStream, graph); clearGraph(config); open(config); GraphReader reader = readerMaker.apply(graph); ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray()); reader.readGraph(inputStream, graph); JanusGraphIndexTest.assertGraphOfTheGods(graph); if (makeGeoshape != null) { assertGeoshape(makeGeoshape); } }
@Test @LoadGraphWith(LoadGraphWith.GraphData.MODERN) public void shouldReadWritePropertyGraphSON() throws Exception { try (final ByteArrayOutputStream os = new ByteArrayOutputStream()) { final GraphWriter writer = writerMaker.apply(graph); final Property p = g.E(convertToEdgeId("marko", "knows", "vadas")).next().property("weight"); writer.writeProperty(os, p); final AtomicBoolean called = new AtomicBoolean(false); final GraphReader reader = readerMaker.apply(graph); try (final ByteArrayInputStream bais = new ByteArrayInputStream(os.toByteArray())) { reader.readProperty(bais, propertyAttachable -> { assertEquals(p.value(), propertyAttachable.get().value()); assertEquals(p.key(), propertyAttachable.get().key()); called.set(true); return propertyAttachable.get(); }); } assertTrue(called.get()); } } }
@Test @LoadGraphWith(LoadGraphWith.GraphData.CLASSIC) public void shouldReadWriteVerticesNoEdgesToGraphSONManual() throws Exception { assumeThat(ioType, startsWith("graphson")); try (final ByteArrayOutputStream os = new ByteArrayOutputStream()) { final GraphWriter writer = writerMaker.apply(graph); writer.writeVertices(os, g.V().has("age", P.gt(30))); final AtomicInteger called = new AtomicInteger(0); final GraphReader reader = readerMaker.apply(graph); try (final BufferedReader br = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(os.toByteArray())))) { String line = br.readLine(); reader.readVertex(new ByteArrayInputStream(line.getBytes()), attachable -> { called.incrementAndGet(); return mock(Vertex.class); }); line = br.readLine(); reader.readVertex(new ByteArrayInputStream(line.getBytes()), detachedVertex -> { called.incrementAndGet(); return mock(Vertex.class); }); } assertEquals(2, called.get()); } }
@Test @LoadGraphWith(LoadGraphWith.GraphData.MODERN) public void shouldReadWriteVertexPropertyNoMetaProperties() throws Exception { try (final ByteArrayOutputStream os = new ByteArrayOutputStream()) { final GraphWriter writer = writerMaker.apply(graph); final VertexProperty p = g.V(convertToVertexId("marko")).next().property("name"); writer.writeVertexProperty(os, p); final AtomicBoolean called = new AtomicBoolean(false); final GraphReader reader = readerMaker.apply(graph); try (final ByteArrayInputStream bais = new ByteArrayInputStream(os.toByteArray())) { reader.readVertexProperty(bais, propertyAttachable -> { assertEquals(p.value(), propertyAttachable.get().value()); assertEquals(p.key(), propertyAttachable.get().key()); assertEquals(0, IteratorUtils.count(propertyAttachable.get().properties())); called.set(true); return propertyAttachable.get(); }); } assertTrue(called.get()); } }
final GraphReader reader = readerMaker.apply(graph); try (final ByteArrayInputStream bais = new ByteArrayInputStream(os.toByteArray())) { reader.readEdge(bais, edge -> { final Edge detachedEdge = (Edge) edge; assertEquals(e.id(), assertIdDirectly ? detachedEdge.id() : graph.edges(detachedEdge.id().toString()).next().id());
public static <V> V readStringAs(String json, Class<V> destClass) throws IOException { try (final ByteArrayInputStream bais = new ByteArrayInputStream(json.getBytes("UTF-8"))) { return GRAPHSON_READER_2_0.readObject(bais, destClass); } }
@Test @LoadGraphWith(LoadGraphWith.GraphData.CREW) public void shouldReadWriteVertexPropertyWithMetaProperties() throws Exception { try (final ByteArrayOutputStream os = new ByteArrayOutputStream()) { final GraphWriter writer = writerMaker.apply(graph); // select any vertexproperty that has both start/end time final VertexProperty p = (VertexProperty) g.V(convertToVertexId("marko")).properties("location").as("p").has("endTime").select("p").next(); writer.writeVertexProperty(os, p); final AtomicBoolean called = new AtomicBoolean(false); final GraphReader reader = readerMaker.apply(graph); try (final ByteArrayInputStream bais = new ByteArrayInputStream(os.toByteArray())) { reader.readVertexProperty(bais, propertyAttachable -> { assertEquals(p.value(), propertyAttachable.get().value()); assertEquals(p.key(), propertyAttachable.get().key()); assertEquals(IteratorUtils.count(p.properties()), IteratorUtils.count(propertyAttachable.get().properties())); assertEquals(p.property("startTime").value(), ((Property) propertyAttachable.get().properties("startTime").next()).value()); assertEquals(p.property("endTime").value(), ((Property) propertyAttachable.get().properties("endTime").next()).value()); called.set(true); return propertyAttachable.get(); }); } assertTrue(called.get()); } }
final GraphReader reader = readerMaker.apply(graph); try (final ByteArrayInputStream bais = new ByteArrayInputStream(os.toByteArray())) { reader.readEdge(bais, edge -> { final Edge detachedEdge = (Edge) edge; assertEquals(e.id(), assertIdDirectly ? detachedEdge.id() : graph.edges(detachedEdge.id().toString()).next().id());
@Test public void testGeoShapeSerializationReadWriteAsGraphSONEmbedded() throws Exception { GraphOfTheGodsFactory.loadWithoutMixedIndex(graph, true); GraphSONMapper m = graph.io(IoCore.graphson()).mapper().embedTypes(true).create(); GraphWriter writer = graph.io(IoCore.graphson()).writer().mapper(m).create(); FileOutputStream fos = new FileOutputStream("/tmp/test.json"); writer.writeGraph(fos, graph); clearGraph(config); open(config); GraphReader reader = graph.io(IoCore.graphson()).reader().mapper(m).create(); FileInputStream fis = new FileInputStream("/tmp/test.json"); reader.readGraph(fis, graph); TitanIndexTest.assertGraphOfTheGods(graph); }
@Test @LoadGraphWith(LoadGraphWith.GraphData.CLASSIC) public void shouldReadWriteVerticesNoEdgesToGryoManual() throws Exception { assumeThat(ioType, is("gryo")); try (final ByteArrayOutputStream os = new ByteArrayOutputStream()) { final GraphWriter writer = writerMaker.apply(graph); writer.writeVertices(os, g.V().has("age", P.gt(30))); final AtomicInteger called = new AtomicInteger(0); final GraphReader reader = readerMaker.apply(graph); try (final VertexByteArrayInputStream vbais = new VertexByteArrayInputStream(new ByteArrayInputStream(os.toByteArray()))) { final byte[] y = vbais.readVertexBytes().toByteArray(); reader.readVertex(new ByteArrayInputStream(y), attachable -> { final Vertex detachedVertex = attachable.get(); called.incrementAndGet(); return detachedVertex; }); final byte[] z = vbais.readVertexBytes().toByteArray(); reader.readVertex(new ByteArrayInputStream(z), attachable -> { final Vertex detachedVertex = attachable.get(); called.incrementAndGet(); return detachedVertex; }); } assertEquals(2, called.get()); } }
@Test @LoadGraphWith(LoadGraphWith.GraphData.MODERN) public void shouldReadWriteVertexPropertyNoMetaProperties() throws Exception { try (final ByteArrayOutputStream os = new ByteArrayOutputStream()) { final GraphWriter writer = writerMaker.apply(graph); final VertexProperty p = g.V(convertToVertexId("marko")).next().property("name"); writer.writeVertexProperty(os, p); final AtomicBoolean called = new AtomicBoolean(false); final GraphReader reader = readerMaker.apply(graph); try (final ByteArrayInputStream bais = new ByteArrayInputStream(os.toByteArray())) { reader.readVertexProperty(bais, propertyAttachable -> { assertEquals(p.value(), propertyAttachable.get().value()); assertEquals(p.key(), propertyAttachable.get().key()); assertEquals(0, IteratorUtils.count(propertyAttachable.get().properties())); called.set(true); return propertyAttachable.get(); }); } assertTrue(called.get()); } }
final GraphReader reader = readerMaker.apply(graph); try (final ByteArrayInputStream bais = new ByteArrayInputStream(os.toByteArray())) { reader.readEdge(bais, edge -> { final Edge detachedEdge = (Edge) edge; assertEquals(e.id(), assertIdDirectly ? detachedEdge.id() : graph.edges(detachedEdge.id().toString()).next().id());
@Test @LoadGraphWith(LoadGraphWith.GraphData.MODERN) public void shouldReadWritePropertyGraphSON() throws Exception { try (final ByteArrayOutputStream os = new ByteArrayOutputStream()) { final GraphWriter writer = writerMaker.apply(graph); final Property p = g.E(convertToEdgeId("marko", "knows", "vadas")).next().property("weight"); writer.writeProperty(os, p); final AtomicBoolean called = new AtomicBoolean(false); final GraphReader reader = readerMaker.apply(graph); try (final ByteArrayInputStream bais = new ByteArrayInputStream(os.toByteArray())) { reader.readProperty(bais, propertyAttachable -> { assertEquals(p.value(), propertyAttachable.get().value()); assertEquals(p.key(), propertyAttachable.get().key()); called.set(true); return propertyAttachable.get(); }); } assertTrue(called.get()); } } }
protected Traverser.Admin<S> read(final File file) { try (final InputStream stream = new FileInputStream(file)) { final Graph graph = (Graph) this.traversal.getGraph().get(); constructReader().readGraph(stream, graph); return EmptyTraverser.instance(); } catch (IOException ioe) { throw new IllegalStateException(String.format("Could not read file %s into graph", this.file), ioe); } }
final GraphReader reader = readerMaker.apply(graph); try (final ByteArrayInputStream bais = new ByteArrayInputStream(os.toByteArray())) { reader.readVertex(bais, attachable -> { final Vertex detachedVertex = attachable.get(); if (assertViaDirectEquality) {
@Test @LoadGraphWith(LoadGraphWith.GraphData.CREW) public void shouldReadWriteVertexPropertyWithMetaProperties() throws Exception { try (final ByteArrayOutputStream os = new ByteArrayOutputStream()) { final GraphWriter writer = writerMaker.apply(graph); // select any vertexproperty that has both start/end time final VertexProperty p = (VertexProperty) g.V(convertToVertexId("marko")).properties("location").as("p").has("endTime").select("p").next(); writer.writeVertexProperty(os, p); final AtomicBoolean called = new AtomicBoolean(false); final GraphReader reader = readerMaker.apply(graph); try (final ByteArrayInputStream bais = new ByteArrayInputStream(os.toByteArray())) { reader.readVertexProperty(bais, propertyAttachable -> { assertEquals(p.value(), propertyAttachable.get().value()); assertEquals(p.key(), propertyAttachable.get().key()); assertEquals(IteratorUtils.count(p.properties()), IteratorUtils.count(propertyAttachable.get().properties())); assertEquals(p.property("startTime").value(), ((Property) propertyAttachable.get().properties("startTime").next()).value()); assertEquals(p.property("endTime").value(), ((Property) propertyAttachable.get().properties("endTime").next()).value()); called.set(true); return propertyAttachable.get(); }); } assertTrue(called.get()); } }
final GraphReader reader = readerMaker.apply(graph); try (final ByteArrayInputStream bais = new ByteArrayInputStream(os.toByteArray())) { reader.readEdge(bais, edge -> { final Edge detachedEdge = (Edge) edge; assertEquals(e.id(), assertIdDirectly ? detachedEdge.id() : graph.edges(detachedEdge.id().toString()).next().id());
private static void readGraphMLIntoGraph(final Graph g, final String file) throws IOException { final GraphReader reader = GraphMLReader.build().create(); try (final InputStream stream = IoTest.class.getResourceAsStream(TestHelper.convertPackageToResourcePath(GraphMLResourceAccess.class) + file)) { reader.readGraph(stream, g); } }
final GraphReader reader = readerMaker.apply(graph); try (final ByteArrayInputStream bais = new ByteArrayInputStream(os.toByteArray())) { reader.readVertex(bais, attachable -> { final Vertex detachedVertex = attachable.get(); assertEquals(v1.id(), assertViaDirectEquality ? detachedVertex.id() : graph.vertices(detachedVertex.id().toString()).next().id());
@Test @FeatureRequirement(featureClass = Graph.Features.EdgeFeatures.class, feature = Graph.Features.EdgeFeatures.FEATURE_ADD_EDGES) @FeatureRequirement(featureClass = Graph.Features.VertexFeatures.class, feature = Graph.Features.VertexFeatures.FEATURE_ADD_VERTICES) @FeatureRequirement(featureClass = VertexPropertyFeatures.class, feature = FEATURE_STRING_VALUES) @FeatureRequirement(featureClass = VertexPropertyFeatures.class, feature = FEATURE_INTEGER_VALUES) @FeatureRequirement(featureClass = EdgePropertyFeatures.class, feature = EdgePropertyFeatures.FEATURE_FLOAT_VALUES) public void shouldTransformGraphMLV2ToV3ViaXSLT() throws Exception { final InputStream stylesheet = Thread.currentThread().getContextClassLoader().getResourceAsStream("tp2-to-tp3-graphml.xslt"); final InputStream datafile = IoTest.class.getResourceAsStream(TestHelper.convertPackageToResourcePath(GraphMLResourceAccess.class) + "tinkerpop-classic-tp2.xml"); final ByteArrayOutputStream output = new ByteArrayOutputStream(); final TransformerFactory tFactory = TransformerFactory.newInstance(); final StreamSource stylesource = new StreamSource(stylesheet); final Transformer transformer = tFactory.newTransformer(stylesource); final StreamSource source = new StreamSource(datafile); final StreamResult result = new StreamResult(output); transformer.transform(source, result); final GraphReader reader = GraphMLReader.build().create(); reader.readGraph(new ByteArrayInputStream(output.toByteArray()), graph); assertClassicGraph(graph, false, true); }