Search in sources :

Example 1 with Neo4j2Graph

use of com.tinkerpop.blueprints.impls.neo4j2.Neo4j2Graph in project blueprints by tinkerpop.

the class Neo4j2GraphConfiguration method configureGraphInstance.

public Graph configureGraphInstance(final GraphConfigurationContext context) throws GraphConfigurationException {
    final String graphFile = context.getProperties().getString(Tokens.REXSTER_GRAPH_LOCATION);
    if (graphFile == null || graphFile.length() == 0) {
        throw new GraphConfigurationException("Check graph configuration. Missing or empty configuration element: " + Tokens.REXSTER_GRAPH_LOCATION);
    }
    final boolean highAvailabilityMode = context.getProperties().getBoolean(Tokens.REXSTER_GRAPH_HA, false);
    // get the <properties> section of the xml configuration
    final HierarchicalConfiguration graphSectionConfig = (HierarchicalConfiguration) context.getProperties();
    SubnodeConfiguration neo4jSpecificConfiguration;
    try {
        neo4jSpecificConfiguration = graphSectionConfig.configurationAt(Tokens.REXSTER_GRAPH_PROPERTIES);
    } catch (IllegalArgumentException iae) {
        throw new GraphConfigurationException("Check graph configuration. Missing or empty configuration element: " + Tokens.REXSTER_GRAPH_PROPERTIES);
    }
    try {
        // properties to initialize the neo4j instance.
        final HashMap<String, String> neo4jProperties = new HashMap<String, String>();
        // read the properties from the xml file and convert them to properties
        // to be injected into neo4j.
        final Iterator<String> neo4jSpecificConfigurationKeys = neo4jSpecificConfiguration.getKeys();
        while (neo4jSpecificConfigurationKeys.hasNext()) {
            String key = neo4jSpecificConfigurationKeys.next();
            // replace the ".." put in play by apache commons configuration.  that's expected behavior
            // due to parsing key names to xml.
            neo4jProperties.put(key.replace("..", "."), neo4jSpecificConfiguration.getString(key));
        }
        if (highAvailabilityMode) {
            if (!neo4jProperties.containsKey("ha.machine_id")) {
                throw new GraphConfigurationException("Check graph configuration. Neo4j HA requires [ha.machine_id] in the <properties> of the configuration");
            }
            if (!neo4jProperties.containsKey("ha.server")) {
                throw new GraphConfigurationException("Check graph configuration. Neo4j HA requires [ha.server] <properties> of the configuration");
            }
            if (!neo4jProperties.containsKey("ha.initial_hosts")) {
                throw new GraphConfigurationException("Check graph configuration. Neo4j HA requires [ha.initial_hosts] <properties> of the configuration");
            }
            return new Neo4j2HaGraph(graphFile, neo4jProperties);
        } else {
            return new Neo4j2Graph(graphFile, neo4jProperties);
        }
    } catch (GraphConfigurationException gce) {
        throw gce;
    } catch (Exception ex) {
        throw new GraphConfigurationException(ex);
    }
}
Also used : SubnodeConfiguration(org.apache.commons.configuration.SubnodeConfiguration) HashMap(java.util.HashMap) GraphConfigurationException(com.tinkerpop.rexster.config.GraphConfigurationException) Neo4j2HaGraph(com.tinkerpop.blueprints.impls.neo4j2.Neo4j2HaGraph) HierarchicalConfiguration(org.apache.commons.configuration.HierarchicalConfiguration) GraphConfigurationException(com.tinkerpop.rexster.config.GraphConfigurationException) Neo4j2Graph(com.tinkerpop.blueprints.impls.neo4j2.Neo4j2Graph)

Example 2 with Neo4j2Graph

use of com.tinkerpop.blueprints.impls.neo4j2.Neo4j2Graph in project blueprints by tinkerpop.

the class Neo4j2GraphSpecificTestSuite method testLuceneQueryIndex.

public void testLuceneQueryIndex() throws Exception {
    Neo4j2Graph graph = (Neo4j2Graph) graphTest.generateGraph();
    Index<Vertex> vertexIndex = graph.createIndex("vertices", Vertex.class);
    Vertex a = graph.addVertex(null);
    vertexIndex.put("name", "marko", a);
    Iterator<Vertex> ittyLuceneQuery = ((Neo4j2VertexIndex) graph.getIndex("vertices", Vertex.class)).query("name:*rko").iterator();
    int counter = 0;
    while (ittyLuceneQuery.hasNext()) {
        counter++;
        assertEquals(a, ittyLuceneQuery.next());
    }
    assertEquals(counter, 1);
    vertexIndex.put("name", "marko some_other name", graph.addVertex(null));
    ittyLuceneQuery = ((Neo4j2VertexIndex) graph.getIndex("vertices", Vertex.class)).query("name:*rko*").iterator();
    counter = 0;
    while (ittyLuceneQuery.hasNext()) {
        ittyLuceneQuery.next();
        counter++;
    }
    assertEquals(2, counter);
    graph.shutdown();
}
Also used : Vertex(com.tinkerpop.blueprints.Vertex) Neo4j2VertexIndex(com.tinkerpop.blueprints.impls.neo4j2.index.Neo4j2VertexIndex)

Example 3 with Neo4j2Graph

use of com.tinkerpop.blueprints.impls.neo4j2.Neo4j2Graph in project blueprints by tinkerpop.

the class Neo4j2BatchGraphTest method testAddingVerticesWithUserIdsAsStringsThenSettingProperties.

public void testAddingVerticesWithUserIdsAsStringsThenSettingProperties() {
    List<Object> ids = new ArrayList<Object>(Arrays.asList(100L, 5.0d, "10", 4.0f, "10000.00"));
    final String directory = this.getWorkingDirectory();
    final Neo4j2BatchGraph batch = new Neo4j2BatchGraph(directory);
    for (final Object id : ids) {
        Vertex v = batch.addVertex(id);
        v.setProperty("theKey", id);
    }
    for (final Object id : ids) {
        assertNotNull(batch.getVertex(id));
        assertEquals(batch.getVertex(id).getProperty("theKey"), id);
    }
    assertNull(batch.getVertex(1L));
    assertNull(batch.getVertex(2L));
    assertNull(batch.getVertex(200000L));
    batch.shutdown();
    // native neo4j graph load
    final Neo4j2Graph graph = new Neo4j2Graph(directory);
    graph.autoStartTransaction(true);
    assertEquals(count(graph.getVertices()), ids.size());
    assertNotNull(graph.getVertex(100l));
    assertNotNull(graph.getVertex(5l));
    assertNotNull(graph.getVertex(10l));
    assertNotNull(graph.getVertex(4l));
    assertNotNull(graph.getVertex(10000));
    graph.shutdown();
}
Also used : ArrayList(java.util.ArrayList) Neo4j2Graph(com.tinkerpop.blueprints.impls.neo4j2.Neo4j2Graph)

Example 4 with Neo4j2Graph

use of com.tinkerpop.blueprints.impls.neo4j2.Neo4j2Graph in project blueprints by tinkerpop.

the class Neo4j2BatchGraphTest method testIndexParameters.

public void testIndexParameters() throws Exception {
    final String directory = this.getWorkingDirectory();
    final Neo4j2BatchGraph batch = new Neo4j2BatchGraph(directory);
    Index<Vertex> index = batch.createIndex("testIdx", Vertex.class, new Parameter("analyzer", LowerCaseKeywordAnalyzer.class.getName()));
    Vertex a = batch.addVertex(null);
    a.setProperty("name", "marko");
    index.put("name", "marko", a);
    batch.flushIndices();
    batch.shutdown();
    // native neo4j graph load
    Neo4j2Graph graph = new Neo4j2Graph(directory);
    graph.autoStartTransaction(true);
    Iterator<Vertex> itty = graph.getIndex("testIdx", Vertex.class).query("name", "*rko").iterator();
    int counter = 0;
    while (itty.hasNext()) {
        counter++;
        assertEquals(itty.next().getProperty("name"), "marko");
    }
    assertEquals(counter, 1);
    itty = graph.getIndex("testIdx", Vertex.class).query("name", "MaRkO").iterator();
    counter = 0;
    while (itty.hasNext()) {
        counter++;
        assertEquals(itty.next().getProperty("name"), "marko");
    }
    assertEquals(counter, 1);
    graph.shutdown();
}
Also used : Neo4j2Graph(com.tinkerpop.blueprints.impls.neo4j2.Neo4j2Graph)

Example 5 with Neo4j2Graph

use of com.tinkerpop.blueprints.impls.neo4j2.Neo4j2Graph in project blueprints by tinkerpop.

the class Neo4j2BatchGraphTest method testAddingVerticesEdgesWithIndices.

public void testAddingVerticesEdgesWithIndices() {
    final String directory = this.getWorkingDirectory();
    final Neo4j2BatchGraph batch = new Neo4j2BatchGraph(directory);
    assertEquals(0, count(batch.getIndices()));
    batch.createKeyIndex("name", Vertex.class);
    batch.createKeyIndex("age", Vertex.class);
    Index<Edge> edgeIndex = batch.createIndex("edgeIdx", Edge.class);
    assertEquals(1, count(batch.getIndices()));
    for (final Index index : batch.getIndices()) {
        if (index.getIndexName().equals("edgeIdx")) {
            assertEquals(index.getIndexClass(), Edge.class);
        } else {
            throw new RuntimeException("There should not be another index.");
        }
    }
    final List<Long> ids = new ArrayList<Long>();
    for (int i = 0; i < 10; i++) {
        final Map<String, Object> map = new HashMap<String, Object>();
        map.put("name", i + "");
        map.put("age", i * 10);
        map.put("nothing", 0);
        ids.add((Long) batch.addVertex(map).getId());
    }
    for (int i = 1; i < ids.size(); i++) {
        final Map<String, Object> map = new HashMap<String, Object>();
        map.put("weight", 0.5f);
        long idA = ids.get(i - 1);
        long idB = ids.get(i);
        final Edge edge = batch.addEdge(map, batch.getVertex(idA), batch.getVertex(idB), idA + "-" + idB);
        edgeIndex.put("unique", idA + "-" + idB, edge);
        edgeIndex.put("full", "blah", edge);
    }
    batch.flushIndices();
    batch.shutdown();
    // native neo4j graph load
    final Neo4j2Graph graph = new Neo4j2Graph(directory);
    graph.autoStartTransaction(true);
    assertEquals(count(graph.getIndices()), 1);
    assertEquals(graph.getIndexedKeys(Vertex.class).size(), 2);
    assertTrue(graph.getIndexedKeys(Vertex.class).contains("name"));
    assertTrue(graph.getIndexedKeys(Vertex.class).contains("age"));
    edgeIndex = graph.getIndex("edgeIdx", Edge.class);
    assertEquals(edgeIndex.getIndexClass(), Edge.class);
    assertEquals(count(graph.getVertices()), 10);
    assertTrue(graph.getVertices("nothing", 0) instanceof PropertyFilteredIterable);
    assertTrue(graph.getVertices("blah", "blop") instanceof PropertyFilteredIterable);
    // key index used
    assertFalse(graph.getVertices("name", "marko") instanceof PropertyFilteredIterable);
    // key indexed used
    assertFalse(graph.getVertices("age", 32) instanceof PropertyFilteredIterable);
    for (final Vertex vertex : graph.getVertices()) {
        int age = (Integer) vertex.getProperty("age");
        assertEquals(vertex.getProperty("name"), (age / 10) + "");
        assertTrue(graph.getVertices("nothing", 0).iterator().hasNext());
        assertEquals(count(graph.getVertices("age", age)), 1);
        assertEquals(graph.getVertices("age", age).iterator().next(), vertex);
        assertEquals(count(graph.getVertices("name", (age / 10) + "")), 1);
        assertEquals(graph.getVertices("name", (age / 10) + "").iterator().next(), vertex);
        assertEquals(vertex.getPropertyKeys().size(), 3);
        vertex.setProperty("NEW", age);
        assertEquals(vertex.getPropertyKeys().size(), 4);
    }
    for (final Vertex vertex : graph.getVertices()) {
        int age = (Integer) vertex.getProperty("age");
        assertEquals(vertex.getProperty("NEW"), age);
        assertEquals(vertex.getPropertyKeys().size(), 4);
        vertex.removeProperty("NEW");
    }
    for (final Vertex vertex : graph.getVertices()) {
        assertNull(vertex.getProperty("NEW"));
        assertEquals(vertex.getPropertyKeys().size(), 3);
    }
    assertEquals(count(graph.getEdges()), 9);
    assertEquals(count(edgeIndex.get("full", "blah")), 9);
    Set<Edge> edges = new HashSet<Edge>();
    for (Edge edge : edgeIndex.get("full", "blah")) {
        edges.add(edge);
    }
    assertEquals(edges.size(), 9);
    for (final Edge edge : graph.getEdges()) {
        long idA = (Long) edge.getVertex(Direction.OUT).getId();
        long idB = (Long) edge.getVertex(Direction.IN).getId();
        assertEquals(idA + 1, idB);
        assertEquals(edge.getLabel(), idA + "-" + idB);
        assertEquals(edge.getPropertyKeys().size(), 1);
        assertEquals(edge.getProperty("weight"), 0.5f);
        assertEquals(edgeIndex.count("weight", 0.5f), 0);
        assertEquals(edgeIndex.count("unique", idA + "-" + idB), 1);
        assertEquals(edgeIndex.get("unique", idA + "-" + idB).iterator().next(), edge);
        assertTrue(edges.contains(edge));
    }
    graph.shutdown();
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Neo4j2Graph(com.tinkerpop.blueprints.impls.neo4j2.Neo4j2Graph) PropertyFilteredIterable(com.tinkerpop.blueprints.util.PropertyFilteredIterable) HashSet(java.util.HashSet)

Aggregations

Neo4j2Graph (com.tinkerpop.blueprints.impls.neo4j2.Neo4j2Graph)8 ArrayList (java.util.ArrayList)5 HashMap (java.util.HashMap)3 Vertex (com.tinkerpop.blueprints.Vertex)1 Neo4j2HaGraph (com.tinkerpop.blueprints.impls.neo4j2.Neo4j2HaGraph)1 Neo4j2VertexIndex (com.tinkerpop.blueprints.impls.neo4j2.index.Neo4j2VertexIndex)1 PropertyFilteredIterable (com.tinkerpop.blueprints.util.PropertyFilteredIterable)1 GraphMLReader (com.tinkerpop.blueprints.util.io.graphml.GraphMLReader)1 GraphConfigurationException (com.tinkerpop.rexster.config.GraphConfigurationException)1 HashSet (java.util.HashSet)1 Random (java.util.Random)1 HierarchicalConfiguration (org.apache.commons.configuration.HierarchicalConfiguration)1 SubnodeConfiguration (org.apache.commons.configuration.SubnodeConfiguration)1