Search in sources :

Example 1 with Index

use of com.tinkerpop.blueprints.Index in project blueprints by tinkerpop.

the class Neo4jBatchGraphTest method testAddingVerticesEdgesWithIndices.

public void testAddingVerticesEdgesWithIndices() {
    final String directory = this.getWorkingDirectory();
    final Neo4jBatchGraph batch = new Neo4jBatchGraph(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 Neo4jGraph graph = new Neo4jGraph(directory);
    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 : Vertex(com.tinkerpop.blueprints.Vertex) HashMap(java.util.HashMap) Neo4jGraph(com.tinkerpop.blueprints.impls.neo4j.Neo4jGraph) ArrayList(java.util.ArrayList) Index(com.tinkerpop.blueprints.Index) Edge(com.tinkerpop.blueprints.Edge) PropertyFilteredIterable(com.tinkerpop.blueprints.util.PropertyFilteredIterable) HashSet(java.util.HashSet)

Example 2 with Index

use of com.tinkerpop.blueprints.Index in project blueprints by tinkerpop.

the class TinkerGraph method removeVertex.

public void removeVertex(final Vertex vertex) {
    if (!this.vertices.containsKey(vertex.getId().toString()))
        throw ExceptionFactory.vertexWithIdDoesNotExist(vertex.getId());
    for (Edge edge : vertex.getEdges(Direction.BOTH)) {
        this.removeEdge(edge);
    }
    this.vertexKeyIndex.removeElement((TinkerVertex) vertex);
    for (Index index : this.getIndices()) {
        if (Vertex.class.isAssignableFrom(index.getIndexClass())) {
            TinkerIndex<TinkerVertex> idx = (TinkerIndex<TinkerVertex>) index;
            idx.removeElement((TinkerVertex) vertex);
        }
    }
    this.vertices.remove(vertex.getId().toString());
}
Also used : Index(com.tinkerpop.blueprints.Index) Edge(com.tinkerpop.blueprints.Edge)

Example 3 with Index

use of com.tinkerpop.blueprints.Index in project blueprints by tinkerpop.

the class TinkerGraph method removeEdge.

public void removeEdge(final Edge edge) {
    TinkerVertex outVertex = (TinkerVertex) edge.getVertex(Direction.OUT);
    TinkerVertex inVertex = (TinkerVertex) edge.getVertex(Direction.IN);
    if (null != outVertex && null != outVertex.outEdges) {
        final Set<Edge> edges = outVertex.outEdges.get(edge.getLabel());
        if (null != edges)
            edges.remove(edge);
    }
    if (null != inVertex && null != inVertex.inEdges) {
        final Set<Edge> edges = inVertex.inEdges.get(edge.getLabel());
        if (null != edges)
            edges.remove(edge);
    }
    this.edgeKeyIndex.removeElement((TinkerEdge) edge);
    for (Index index : this.getIndices()) {
        if (Edge.class.isAssignableFrom(index.getIndexClass())) {
            TinkerIndex<TinkerEdge> idx = (TinkerIndex<TinkerEdge>) index;
            idx.removeElement((TinkerEdge) edge);
        }
    }
    this.edges.remove(edge.getId().toString());
}
Also used : Index(com.tinkerpop.blueprints.Index) Edge(com.tinkerpop.blueprints.Edge)

Example 4 with Index

use of com.tinkerpop.blueprints.Index in project blueprints by tinkerpop.

the class RexsterGraph method getIndices.

public Iterable<Index<? extends Element>> getIndices() {
    List<Index<? extends Element>> indices = new ArrayList<Index<? extends Element>>();
    JSONArray json = RestHelper.getResultArray(this.graphURI + RexsterTokens.SLASH_INDICES);
    for (int ix = 0; ix < json.length(); ix++) {
        JSONObject index = json.optJSONObject(ix);
        Class c;
        String clazz = index.optString(RexsterTokens.CLASS);
        if (clazz.toLowerCase().contains(RexsterTokens.VERTEX))
            c = Vertex.class;
        else if (clazz.toLowerCase().contains(RexsterTokens.EDGE))
            c = Edge.class;
        else
            throw new RuntimeException("Can not determine whether " + clazz + " is a vertex or edge class");
        indices.add(new RexsterIndex(this, index.optString(RexsterTokens.NAME), c));
    }
    return indices;
}
Also used : Vertex(com.tinkerpop.blueprints.Vertex) JSONObject(org.codehaus.jettison.json.JSONObject) Element(com.tinkerpop.blueprints.Element) ArrayList(java.util.ArrayList) JSONArray(org.codehaus.jettison.json.JSONArray) Index(com.tinkerpop.blueprints.Index)

Example 5 with Index

use of com.tinkerpop.blueprints.Index in project blueprints by tinkerpop.

the class TinkerMetadataReaderTest method exampleMetadataGetsCorrectIndices.

@Test
public void exampleMetadataGetsCorrectIndices() throws IOException {
    TinkerMetadataReader.load(this.graph, TinkerMetadataReaderTest.class.getResourceAsStream("example-tinkergraph-metadata.dat"));
    Assert.assertEquals(2, this.graph.indices.size());
    Index idxAge = this.graph.getIndex("age", Vertex.class);
    CloseableIterable<Vertex> vertices = idxAge.get("age", 27);
    Assert.assertEquals(1, getIterableCount(vertices));
    vertices.close();
    Index idxWeight = this.graph.getIndex("weight", Edge.class);
    CloseableIterable<Edge> edges = idxWeight.get("weight", 0.5f);
    Assert.assertEquals(1, getIterableCount(edges));
    edges.close();
}
Also used : Vertex(com.tinkerpop.blueprints.Vertex) Index(com.tinkerpop.blueprints.Index) Edge(com.tinkerpop.blueprints.Edge) Test(org.junit.Test)

Aggregations

Index (com.tinkerpop.blueprints.Index)7 Edge (com.tinkerpop.blueprints.Edge)5 Vertex (com.tinkerpop.blueprints.Vertex)5 ArrayList (java.util.ArrayList)3 Element (com.tinkerpop.blueprints.Element)1 IndexableGraph (com.tinkerpop.blueprints.IndexableGraph)1 KeyIndexableGraph (com.tinkerpop.blueprints.KeyIndexableGraph)1 Neo4jGraph (com.tinkerpop.blueprints.impls.neo4j.Neo4jGraph)1 PropertyFilteredIterable (com.tinkerpop.blueprints.util.PropertyFilteredIterable)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Iterator (java.util.Iterator)1 JSONArray (org.codehaus.jettison.json.JSONArray)1 JSONObject (org.codehaus.jettison.json.JSONObject)1 Test (org.junit.Test)1