Search in sources :

Example 1 with Node

use of overflowdb.Node in project overflowdb by ShiftLeftSecurity.

the class DummyNodeDb method shouldThrowExceptionForUnsupportedProperty.

@Test(expected = RuntimeException.class)
public /**
 * Same as above, but this time we persist a graph based on SchemaV2, and try to open it from SchemaV1.
 * Since SchemaV1 doesn't know about Connection2 and the additional Connection1 property, this should fail.
 */
void shouldThrowExceptionForUnsupportedProperty() throws IOException {
    final File storageFile = Files.createTempFile("overflowdb", "bin").toFile();
    Config config = Config.withDefaults().withStorageLocation(storageFile.getAbsolutePath());
    {
        Graph graph = SchemaV2.newEmptyGraph(config);
        Node thing1 = graph.addNode(SchemaV1.Thing1.LABEL);
        Node thing2 = graph.addNode(SchemaV1.Thing2.LABEL);
        thing1.addEdge(SchemaV2.Connection1.LABEL, thing2, SchemaV2.Connection1.ADDITIONAL_PROP, "additional property");
        assertEquals(2, graph.nodeCount());
        assertEquals(1, graph.edgeCount());
        graph.close();
    }
    {
        Graph graph = SchemaV1.newEmptyGraph(config);
        assertEquals(2, graph.nodeCount());
        assertEquals(1, graph.edgeCount());
        graph.close();
    }
    // cleanup after test
    storageFile.delete();
}
Also used : Graph(overflowdb.Graph) Config(overflowdb.Config) Node(overflowdb.Node) File(java.io.File) Test(org.junit.Test)

Example 2 with Node

use of overflowdb.Node in project overflowdb by ShiftLeftSecurity.

the class DummyNodeDb method shouldThrowExceptionForUnsupportedEdge.

@Test(expected = RuntimeException.class)
public /**
 * Same as above, but this time we persist a graph based on SchemaV2, and try to open it from SchemaV1.
 * Since SchemaV1 doesn't know about Connection2 and the additional Connection1 property, this should fail.
 */
void shouldThrowExceptionForUnsupportedEdge() throws IOException {
    final File storageFile = Files.createTempFile("overflowdb", "bin").toFile();
    Config config = Config.withDefaults().withStorageLocation(storageFile.getAbsolutePath());
    {
        Graph graph = SchemaV2.newEmptyGraph(config);
        Node thing1 = graph.addNode(SchemaV1.Thing1.LABEL);
        Node thing2 = graph.addNode(SchemaV1.Thing2.LABEL);
        // TODO additional test
        thing1.addEdge(SchemaV2.Connection2.LABEL, thing2);
        assertEquals(2, graph.nodeCount());
        assertEquals(1, graph.edgeCount());
        graph.close();
    }
    {
        Graph graph = SchemaV1.newEmptyGraph(config);
        assertEquals(2, graph.nodeCount());
        assertEquals(1, graph.edgeCount());
        graph.close();
    }
    // cleanup after test
    storageFile.delete();
}
Also used : Graph(overflowdb.Graph) Config(overflowdb.Config) Node(overflowdb.Node) File(java.io.File) Test(org.junit.Test)

Example 3 with Node

use of overflowdb.Node in project overflowdb by ShiftLeftSecurity.

the class DummyNodeDb method shouldLoadOldStorageFormatWhenAddingEdgeType.

/**
 * With a domain (SchemaV1) with two node types (Thing1/2) and one edge type (Connection1, which has one property),
 * create a sample graph and persist it.
 *
 * Then, reopen the file after some additions have been made to the schema (SchemaV2):
 * now there's another edge type (Connection2), and Connection1 has one additional property.
 *
 * Verify that we're still able to load the graph, since there's only additions to the schema.
 */
@Test
public void shouldLoadOldStorageFormatWhenAddingEdgeType() throws IOException {
    final File storageFile = Files.createTempFile("overflowdb", "bin").toFile();
    Config config = Config.withDefaults().withStorageLocation(storageFile.getAbsolutePath());
    final long thing1Id;
    {
        Graph graph = SchemaV1.newEmptyGraph(config);
        Node thing1 = graph.addNode(SchemaV1.Thing1.LABEL);
        Node thing2 = graph.addNode(SchemaV1.Thing2.LABEL);
        thing1.addEdge(SchemaV1.Connection1.LABEL, thing2, SchemaV1.Connection1.NAME, "thing 1");
        assertEquals(2, graph.nodeCount());
        assertEquals(1, graph.edgeCount());
        thing1Id = thing1.id();
        graph.close();
    }
    {
        Graph graph = SchemaV2.newEmptyGraph(config);
        Node thing1 = graph.node(thing1Id);
        SchemaV2.Thing1 thing1Typed = ((NodeRef<SchemaV2.Thing1>) thing1).get();
        SchemaV2.Connection1 connection = (SchemaV2.Connection1) thing1.outE().next();
        assertEquals(1, connection.propertiesMap().size());
        Node thing2 = connection.inNode();
        connection = (SchemaV2.Connection1) thing2.inE().next();
        assertEquals(1, connection.propertiesMap().size());
        SchemaV2.Thing2 thing2Typed = ((NodeRef<SchemaV2.Thing2>) thing2).get();
        assertEquals(2, graph.nodeCount());
        assertEquals(1, graph.edgeCount());
        graph.close();
    }
    // cleanup after test
    storageFile.delete();
}
Also used : Graph(overflowdb.Graph) Config(overflowdb.Config) Node(overflowdb.Node) File(java.io.File) Test(org.junit.Test)

Example 4 with Node

use of overflowdb.Node in project overflowdb by ShiftLeftSecurity.

the class GraphSaveRestoreTest method shouldOnlySerializeChangedNodes.

@Test
public void shouldOnlySerializeChangedNodes() throws IOException {
    final File storageFile = Files.createTempFile("overflowdb", "bin").toFile();
    storageFile.deleteOnExit();
    modifyAndCloseGraph(storageFile, graph -> {
        // initial import from graphml - should serialize all nodes
        loadGraphMl(graph);
        int expectedSerializationCount = 808;
        return expectedSerializationCount;
    });
    modifyAndCloseGraph(storageFile, graph -> {
        // no changes, not even traversing (and thus not deserializing nodes)
        int expectedSerializationCount = 0;
        return expectedSerializationCount;
    });
    modifyAndCloseGraph(storageFile, graph -> {
        // traversing (and thus deserializing nodes), but making no changes
        graph.nodes().forEachRemaining(x -> {
        });
        int expectedSerializationCount = 0;
        return expectedSerializationCount;
    });
    modifyAndCloseGraph(storageFile, graph -> {
        // new node, connected with existing node 'garcia'
        Node newSong = graph.addNode(Song.label);
        newSong.setProperty(Song.NAME, "new song");
        Node youngBlood = getSongs(graph, "YOUNG BLOOD").next();
        youngBlood.addEdge(FollowedBy.LABEL, newSong);
        // both youngBlood and newSong should be serialized
        int expectedSerializationCount = 2;
        return expectedSerializationCount;
    });
    modifyAndCloseGraph(storageFile, graph -> {
        // update node property
        Node newSong = getSongs(graph, "new song").next();
        newSong.setProperty(Song.PERFORMANCES, 5);
        int expectedSerializationCount = 1;
        return expectedSerializationCount;
    });
    // TODO implement property removal (both node and edge)
    // modifyAndCloseGraph(storageFile, graph -> {
    // // remove node property
    // Node newSong = (Node) graph.traversal().V().has(Song.NAME, "new song").next();
    // newSong.setProperty(Song.PERFORMANCES).remove();
    // return 1;
    // });
    modifyAndCloseGraph(storageFile, graph -> {
        // update edge property
        Node newSong = getSongs(graph, "new song").next();
        Edge followedBy = newSong.inE().next();
        followedBy.setProperty(FollowedBy.WEIGHT, 10);
        // both youngBlood and newSong should be serialized
        int expectedSerializationCount = 2;
        return expectedSerializationCount;
    });
    modifyAndCloseGraph(storageFile, graph -> {
        // remove edge
        Node newSong = getSongs(graph, "new song").next();
        Edge followedBy = newSong.inE().next();
        followedBy.remove();
        // both youngBlood and newSong should be serialized
        int expectedSerializationCount = 2;
        return expectedSerializationCount;
    });
    modifyAndCloseGraph(storageFile, graph -> {
        // remove node
        Node newSong = getSongs(graph, "new song").next();
        newSong.remove();
        int expectedSerializationCount = 0;
        return expectedSerializationCount;
    });
    // verify that deleted node is actually gone
    Graph graph = openGratefulDeadGraph(storageFile, false);
    assertFalse("node should have been deleted from storage", getSongs(graph, "new song").hasNext());
}
Also used : Graph(overflowdb.Graph) Node(overflowdb.Node) File(java.io.File) Edge(overflowdb.Edge) Test(org.junit.Test)

Example 5 with Node

use of overflowdb.Node in project overflowdb by ShiftLeftSecurity.

the class GraphSaveRestoreTest method greenField.

@Test
public void greenField() throws IOException {
    final File storageFile = Files.createTempFile("overflowdb", "bin").toFile();
    storageFile.deleteOnExit();
    final Long node0Id;
    final Long node1Id;
    // create graph and store in specified location
    try (Graph graph = openGratefulDeadGraph(storageFile, false)) {
        Node n0 = graph.addNode(Song.label, Song.NAME, "Song 1");
        Node n1 = graph.addNode(Song.label, Song.NAME, "Song 2");
        Edge edge = n0.addEdge(FollowedBy.LABEL, n1, FollowedBy.WEIGHT, 42);
        node0Id = n0.id();
        node1Id = n1.id();
    }
    // reload from disk
    try (Graph graph = openGratefulDeadGraph(storageFile, false)) {
        assertEquals(2, graph.nodeCount());
        assertEquals(1, graph.edgeCount());
        assertEquals("Song 1", graph.node(node0Id).property(Song.NAME));
        assertEquals("Song 2", graph.node(node1Id).property(Song.NAME));
        assertEquals("Song 2", graph.node(node0Id).out(FollowedBy.LABEL).next().property(Song.NAME));
        // ensure we can add more elements
        Node n1 = graph.node(node1Id);
        Node n2 = graph.addNode(Song.label, Song.NAME, "Song 3");
        n1.addEdge(FollowedBy.LABEL, n2, FollowedBy.WEIGHT, 43);
        assertEquals("Song 3", graph.node(node0Id).out().next().out(FollowedBy.LABEL).next().property(Song.NAME));
    }
}
Also used : Graph(overflowdb.Graph) Node(overflowdb.Node) File(java.io.File) Edge(overflowdb.Edge) Test(org.junit.Test)

Aggregations

Node (overflowdb.Node)16 Test (org.junit.Test)12 Graph (overflowdb.Graph)12 File (java.io.File)7 TestNode (overflowdb.testdomains.simple.TestNode)6 Config (overflowdb.Config)5 Edge (overflowdb.Edge)4 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 TestEdge (overflowdb.testdomains.simple.TestEdge)2 TestNodeDb (overflowdb.testdomains.simple.TestNodeDb)2 THashMap (gnu.trove.map.hash.THashMap)1 TLongIntHashMap (gnu.trove.map.hash.TLongIntHashMap)1 IOException (java.io.IOException)1 Arrays (java.util.Arrays)1 TreeSet (java.util.TreeSet)1 Function (java.util.function.Function)1 Assert.assertArrayEquals (org.junit.Assert.assertArrayEquals)1 Assert.assertEquals (org.junit.Assert.assertEquals)1