Java Code Examples for org.apache.tinkerpop.gremlin.structure.Edge#id()

The following examples show how to use org.apache.tinkerpop.gremlin.structure.Edge#id() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: StarGraph.java    From tinkerpop with Apache License 2.0 6 votes vote down vote up
@Override
public Edge addEdge(final String label, final Vertex inVertex, final Object... keyValues) {
    final Edge edge = this.addOutEdge(label, inVertex, keyValues);
    if (inVertex.equals(this)) {
        if (ElementHelper.getIdValue(keyValues).isPresent()) {
            // reuse edge ID from method params
            this.addInEdge(label, this, keyValues);
        } else {
            // copy edge ID that we just allocated with addOutEdge
            final Object[] keyValuesWithId = Arrays.copyOf(keyValues, keyValues.length + 2);
            keyValuesWithId[keyValuesWithId.length - 2] = T.id;
            keyValuesWithId[keyValuesWithId.length - 1] = edge.id();
            this.addInEdge(label, this, keyValuesWithId);
        }
    }
    return edge;
}
 
Example 2
Source File: TestBatchStreamVertex.java    From sqlg with MIT License 6 votes vote down vote up
@Test(expected = IllegalStateException.class)
    public void testAccessPropertyFromEdgeWhileStreaming() {
        Vertex v1 = this.sqlgGraph.addVertex(T.label, "Person", "name", "a1");
        Vertex v2 = this.sqlgGraph.addVertex(T.label, "Person", "name", "a2");
        Edge e1 = v1.addEdge("friend", v2);
        this.sqlgGraph.tx().commit();

        this.sqlgGraph.tx().streamingBatchModeOn();
        LinkedHashMap<String, Object> properties = new LinkedHashMap<>();
        for (int i = 0; i < 100; i++) {
            properties.put("name", "aa" + i);
            this.sqlgGraph.streamVertex("Person", properties);
            properties.clear();
        }
        RecordId recordId = (RecordId) e1.id();
//        Assert.assertEquals("a1", SqlgEdge.of(this.sqlgGraph, recordId.getId(), recordId.getSchemaTable().getSchema(), recordId.getSchemaTable().getTable()).value("name"));
        Assert.assertEquals("a1", this.sqlgGraph.traversal().E(recordId).next().value("name"));
        this.sqlgGraph.tx().commit();
    }
 
Example 3
Source File: EdgeReader.java    From hgraphdb with Apache License 2.0 5 votes vote down vote up
@Override
public void load(Edge edge, Result result) {
    if (result.isEmpty()) {
        throw new HBaseGraphNotFoundException(edge, "Edge does not exist: " + edge.id());
    }
    Object inVertexId = null;
    Object outVertexId = null;
    String label = null;
    Long createdAt = null;
    Long updatedAt = null;
    Map<String, byte[]> rawProps = new HashMap<>();
    for (Cell cell : result.listCells()) {
        String key = Bytes.toString(CellUtil.cloneQualifier(cell));
        if (!Graph.Hidden.isHidden(key)) {
            rawProps.put(key, CellUtil.cloneValue(cell));
        } else if (key.equals(Constants.TO)) {
            inVertexId = ValueUtils.deserialize(CellUtil.cloneValue(cell));
        } else if (key.equals(Constants.FROM)) {
            outVertexId = ValueUtils.deserialize(CellUtil.cloneValue(cell));
        } else if (key.equals(Constants.LABEL)) {
            label = ValueUtils.deserialize(CellUtil.cloneValue(cell));
        } else if (key.equals(Constants.CREATED_AT)) {
            createdAt = ValueUtils.deserialize(CellUtil.cloneValue(cell));
        } else if (key.equals(Constants.UPDATED_AT)) {
            updatedAt = ValueUtils.deserialize(CellUtil.cloneValue(cell));
        }
    }
    final String labelStr = label;
    Map<String, Object> props = rawProps.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey,
            e -> ValueUtils.deserializePropertyValue(graph, ElementType.EDGE, labelStr, e.getKey(), e.getValue())));
    if (inVertexId != null && outVertexId != null && label != null) {
        HBaseEdge newEdge = new HBaseEdge(graph, edge.id(), label, createdAt, updatedAt, props,
                graph.findOrCreateVertex(inVertexId),
                graph.findOrCreateVertex(outVertexId));
        ((HBaseEdge) edge).copyFrom(newEdge);
    } else {
        throw new IllegalStateException("Unable to parse edge from cells");
    }
}
 
Example 4
Source File: EdgeSerializer.java    From tinkergraph-gremlin with Apache License 2.0 4 votes vote down vote up
@Override
protected long getId(Edge edge) {
  return (long) edge.id();
}
 
Example 5
Source File: BitsyMemGraphIT.java    From bitsy with Apache License 2.0 4 votes vote down vote up
public void testObsolescence() {
    IGraphStore store = ((BitsyGraph)graph).getStore();
    
    // Create a vertex
    Vertex v = graph.addVertex();
    Object vid = v.id();
    v.property("foo", "bar");
    
    // Self edge
    Edge e = v.addEdge("self", v);
    Object eid = e.id();
    
    graph.tx().commit();

    Record v1MRec = new Record(RecordType.V, "{\"id\":\"" + vid + "\",\"v\":1,\"s\":\"M\"}");
    assertFalse(v1MRec.checkObsolete(store, false, 1, null));
    assertFalse(v1MRec.checkObsolete(store, true, 1, null));

    Record e1MRec = new Record(RecordType.E, "{\"id\":\"" + eid + "\",\"v\":1,\"s\":\"M\",\"o\":\"" + vid + "\",\"l\":\"" + vid + "\",\"i\":\"" + vid + "\"}");
    assertFalse(e1MRec.checkObsolete(store, false, 1, null));
    assertFalse(e1MRec.checkObsolete(store, true, 1, null));

    // Create a vertex
    v = graph.vertices(vid).next();
    v.property("foo", "baz");

    e = v.edges(Direction.IN, "self").next();
    e.property("foo", "baz");

    graph.tx().commit();

    Record v2MRec = new Record(RecordType.V, "{\"id\":\"" + vid + "\",\"v\":2,\"s\":\"M\"}");
    Record v1DRec = new Record(RecordType.V, "{\"id\":\"" + vid + "\",\"v\":1,\"s\":\"D\"}");
    
    assertTrue(v1MRec.checkObsolete(store, false, 1, null));
    assertTrue(v1MRec.checkObsolete(store, true, 1, null));

    assertFalse(v1DRec.checkObsolete(store, false, 1, null));
    assertTrue(v1DRec.checkObsolete(store, true, 1, null));

    assertFalse(v2MRec.checkObsolete(store, false, 1, null));
    assertFalse(v2MRec.checkObsolete(store, true, 1, null));

    Record e2MRec = new Record(RecordType.E, "{\"id\":\"" + eid + "\",\"v\":2,\"s\":\"M\",\"o\":\"" + vid + "\",\"l\":\"" + vid + "\",\"i\":\"" + vid + "\"}");
    Record e1DRec = new Record(RecordType.E, "{\"id\":\"" + eid + "\",\"v\":1,\"s\":\"D\",\"o\":\"" + vid + "\",\"l\":\"" + vid + "\",\"i\":\"" + vid + "\"}");
    
    assertTrue(e1MRec.checkObsolete(store, false, 1, null));
    assertTrue(e1MRec.checkObsolete(store, true, 1, null));

    assertFalse(e1DRec.checkObsolete(store, false, 1, null));
    assertTrue(e1DRec.checkObsolete(store, true, 1, null));

    assertFalse(e2MRec.checkObsolete(store, false, 1, null));
    assertFalse(e2MRec.checkObsolete(store, true, 1, null));

    // Delete vertex
    v = graph.vertices(vid).next();
    v.remove();

    // Edge will get deleted automatically!
    
    graph.tx().commit();
    
    Record v2DRec = new Record(RecordType.V, "{\"id\":\"" + vid + "\",\"v\":1,\"s\":\"D\"}");
    assertFalse(v2DRec.checkObsolete(store, false, 1, null));
    assertTrue(v2DRec.checkObsolete(store, true, 1, null));
}
 
Example 6
Source File: TitanIndexTest.java    From titan1withtp3.1 with Apache License 2.0 4 votes vote down vote up
@Test
public void testEdgeTTLWithMixedIndices() throws Exception {
    if (!features.hasCellTTL() || !indexFeatures.supportsDocumentTTL()) {
        return;
    }

    PropertyKey name = mgmt.makePropertyKey("name").dataType(String.class).make();
    PropertyKey text = mgmt.makePropertyKey("text").dataType(String.class).make();
    PropertyKey time = makeKey("time", Long.class);

    EdgeLabel label = mgmt.makeEdgeLabel("likes").make();
    final int likesTTLSeconds = (int) TestGraphConfigs.getTTL(TimeUnit.SECONDS);
    mgmt.setTTL(label, Duration.ofSeconds(likesTTLSeconds));

    mgmt.buildIndex("index1", Edge.class).
            addKey(name, getStringMapping()).addKey(time).buildMixedIndex(INDEX);
    mgmt.buildIndex("index2", Edge.class).indexOnly(label).
            addKey(text, getTextMapping()).buildMixedIndex(INDEX);

    assertEquals(Duration.ZERO, mgmt.getTTL(name));
    assertEquals(Duration.ofSeconds(likesTTLSeconds), mgmt.getTTL(label));
    finishSchema();

    TitanVertex v1 = tx.addVertex(), v2 = tx.addVertex(), v3 = tx.addVertex();

    Edge e1 = v1.addEdge("likes", v2, "name", "v1 likes v2", "text", "this will help to identify the edge");
    long time1 = System.currentTimeMillis();
    e1.property("time", time1);
    Edge e2 = v2.addEdge("likes", v3, "name", "v2 likes v3", "text", "this won't match anything");
    long time2 = time1 + 1;
    e2.property("time", time2);

    tx.commit();

    clopen();
    Object e1Id = e1.id();
    Object e2Id = e2.id();

    evaluateQuery(tx.query().has("text", Text.CONTAINS, "help").has(LABEL_NAME, "likes"),
            ElementCategory.EDGE, 1, new boolean[]{true, true}, "index2");
    evaluateQuery(tx.query().has("name", "v2 likes v3").orderBy("time", decr),
            ElementCategory.EDGE, 1, new boolean[]{true, true}, tx.getPropertyKey("time"), Order.DESC, "index1");
    v1 = getV(tx, v1.id());
    v2 = getV(tx, v2.id());
    v3 = getV(tx, v3.id());
    e1 = getE(tx, e1Id);
    e2 = getE(tx, e1Id);
    assertNotNull(v1);
    assertNotNull(v2);
    assertNotNull(v3);
    assertNotNull(e1);
    assertNotNull(e2);
    assertNotEmpty(v1.query().direction(Direction.OUT).edges());
    assertNotEmpty(v2.query().direction(Direction.OUT).edges());


    Thread.sleep(TimeUnit.MILLISECONDS.convert((long) Math.ceil(likesTTLSeconds * 1.25), TimeUnit.SECONDS));
    clopen();

    // ...indexes have expired
    evaluateQuery(tx.query().has("text", Text.CONTAINS, "help").has(LABEL_NAME, "likes"),
            ElementCategory.EDGE, 0, new boolean[]{true, true}, "index2");
    evaluateQuery(tx.query().has("name", "v2 likes v3").orderBy("time", decr),
            ElementCategory.EDGE, 0, new boolean[]{true, true}, tx.getPropertyKey("time"), Order.DESC, "index1");

    v1 = getV(tx, v1.id());
    v2 = getV(tx, v2.id());
    v3 = getV(tx, v3.id());
    e1 = getE(tx, e1Id);
    e2 = getE(tx, e1Id);
    assertNotNull(v1);
    assertNotNull(v2);
    assertNotNull(v3);
    // edges have expired from the graph...
    assertNull(e1);
    assertNull(e2);
    assertEmpty(v1.query().direction(Direction.OUT).edges());
    assertEmpty(v2.query().direction(Direction.OUT).edges());
}
 
Example 7
Source File: TitanEventualGraphTest.java    From titan1withtp3.1 with Apache License 2.0 4 votes vote down vote up
/**
 * Tests the correct interpretation of the commit time and that timestamps can be read
 */
@Test
public void testTimestampSetting() {
    clopen(option(GraphDatabaseConfiguration.STORE_META_TIMESTAMPS,"edgestore"),true,
            option(GraphDatabaseConfiguration.STORE_META_TTL,"edgestore"),true);


    // Transaction 1: Init graph with two vertices, having set "name" and "age" properties
    TitanTransaction tx1 = graph.buildTransaction().commitTime(Instant.ofEpochSecond(100)).start();
    String name = "name";
    String age = "age";
    String address = "address";

    TitanVertex v1 = tx1.addVertex(name, "a");
    TitanVertex v2 = tx1.addVertex(age, "14", name, "b", age, "42");
    tx1.commit();

    // Fetch vertex ids
    long id1 = getId(v1);
    long id2 = getId(v2);

    // Transaction 2: Remove "name" property from v1, set "address" property; create
    // an edge v2 -> v1
    TitanTransaction tx2 = graph.buildTransaction().commitTime(Instant.ofEpochSecond(1000)).start();
    v1 = getV(tx2,id1);
    v2 = getV(tx2,id2);
    for (Iterator<VertexProperty<Object>> propiter = v1.properties(name); propiter.hasNext(); ) {
        VertexProperty prop = propiter.next();
        if (features.hasTimestamps()) {
            Instant t = prop.value("~timestamp");
            assertEquals(100,t.getEpochSecond());
            assertEquals(Instant.ofEpochSecond(0, 1000).getNano(),t.getNano());
        }
        if (features.hasCellTTL()) {
            Duration d = prop.value("~ttl");
            assertEquals(0l, d.getSeconds());
            assertTrue(d.isZero());
        }
    }
    assertEquals(1, v1.query().propertyCount());
    assertEquals(1, v1.query().has("~timestamp", Cmp.GREATER_THAN, Instant.ofEpochSecond(10)).propertyCount());
    assertEquals(1, v1.query().has("~timestamp", Instant.ofEpochSecond(100, 1000)).propertyCount());
    v1.property(name).remove();
    v1.property(VertexProperty.Cardinality.single, address,  "xyz");
    Edge edge = v2.addEdge("parent",v1);
    tx2.commit();
    Object edgeId = edge.id();

    TitanVertex afterTx2 = getV(graph,id1);

    // Verify that "name" property is gone
    assertFalse(afterTx2.keys().contains(name));
    // Verify that "address" property is set
    assertEquals("xyz", afterTx2.value(address));
    // Verify that the edge is properly registered with the endpoint vertex
    assertCount(1, afterTx2.query().direction(IN).labels("parent").edges());
    // Verify that edge is registered under the id
    assertNotNull(getE(graph,edgeId));
    graph.tx().commit();

    // Transaction 3: Remove "address" property from v1 with earlier timestamp than
    // when the value was set
    TitanTransaction tx3 = graph.buildTransaction().commitTime(Instant.ofEpochSecond(200)).start();
    v1 = getV(tx3,id1);
    v1.property(address).remove();
    tx3.commit();

    TitanVertex afterTx3 = getV(graph,id1);
    graph.tx().commit();
    // Verify that "address" is still set
    assertEquals("xyz", afterTx3.value(address));

    // Transaction 4: Modify "age" property on v2, remove edge between v2 and v1
    TitanTransaction tx4 = graph.buildTransaction().commitTime(Instant.ofEpochSecond(2000)).start();
    v2 = getV(tx4,id2);
    v2.property(VertexProperty.Cardinality.single, age,  "15");
    getE(tx4,edgeId).remove();
    tx4.commit();

    TitanVertex afterTx4 = getV(graph,id2);
    // Verify that "age" property is modified
    assertEquals("15", afterTx4.value(age));
    // Verify that edge is no longer registered with the endpoint vertex
    assertCount(0, afterTx4.query().direction(OUT).labels("parent").edges());
    // Verify that edge entry disappeared from id registry
    assertNull(getE(graph,edgeId));

    // Transaction 5: Modify "age" property on v2 with earlier timestamp
    TitanTransaction tx5 = graph.buildTransaction().commitTime(Instant.ofEpochSecond(1500)).start();
    v2 = getV(tx5,id2);
    v2.property(VertexProperty.Cardinality.single, age,  "16");
    tx5.commit();
    TitanVertex afterTx5 = getV(graph,id2);

    // Verify that the property value is unchanged
    assertEquals("15", afterTx5.value(age));
}
 
Example 8
Source File: StringFactory.java    From tinkerpop with Apache License 2.0 4 votes vote down vote up
/**
 * Construct the representation for a {@link Edge}.
 */
public static String edgeString(final Edge edge) {
    return E + L_BRACKET + edge.id() + R_BRACKET + L_BRACKET + edge.outVertex().id() + DASH + edge.label() + ARROW + edge.inVertex().id() + R_BRACKET;
}