Search in sources :

Example 61 with Id

use of com.baidu.hugegraph.backend.id.Id in project incubator-hugegraph by apache.

the class Vertices method vertices.

public Iterator<Vertex> vertices(HugeGraph g) {
    Map<String, Object> props = this.properties;
    E.checkArgument(!((this.ids == null || this.ids.isEmpty()) && (props == null || props.isEmpty()) && this.label == null), "No source vertices provided");
    Iterator<Vertex> iterator;
    if (this.ids != null && !this.ids.isEmpty()) {
        List<Id> sourceIds = new ArrayList<>(this.ids.size());
        for (Object id : this.ids) {
            sourceIds.add(HugeVertex.getIdValue(id));
        }
        iterator = g.vertices(sourceIds.toArray());
        E.checkArgument(iterator.hasNext(), "Not exist source vertices with ids %s", this.ids);
    } else {
        ConditionQuery query = new ConditionQuery(HugeType.VERTEX);
        if (this.label != null) {
            Id label = g.vertexLabel(this.label).id();
            query.eq(HugeKeys.LABEL, label);
        }
        if (props != null && !props.isEmpty()) {
            Map<Id, Object> pks = TraversalUtil.transProperties(g, props);
            TraversalUtil.fillConditionQuery(query, pks, g);
        }
        assert !query.empty();
        iterator = g.vertices(query);
        E.checkArgument(iterator.hasNext(), "Not exist source vertex " + "with label '%s' and properties '%s'", this.label, props);
    }
    return iterator;
}
Also used : Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) HugeVertex(com.baidu.hugegraph.structure.HugeVertex) ConditionQuery(com.baidu.hugegraph.backend.query.ConditionQuery) ArrayList(java.util.ArrayList) Id(com.baidu.hugegraph.backend.id.Id)

Example 62 with Id

use of com.baidu.hugegraph.backend.id.Id in project incubator-hugegraph by apache.

the class FakeObjects method newEdge.

public HugeEdge newEdge(long sourceVertexId, long targetVertexId) {
    PropertyKey name = this.newPropertyKey(IdGenerator.of(1), "name");
    PropertyKey age = this.newPropertyKey(IdGenerator.of(2), "age", DataType.INT, Cardinality.SINGLE);
    PropertyKey city = this.newPropertyKey(IdGenerator.of(3), "city");
    PropertyKey date = this.newPropertyKey(IdGenerator.of(4), "date", DataType.DATE);
    PropertyKey weight = this.newPropertyKey(IdGenerator.of(5), "weight", DataType.DOUBLE);
    VertexLabel vl = this.newVertexLabel(IdGenerator.of(1), "person", IdStrategy.CUSTOMIZE_NUMBER, name.id(), age.id(), city.id());
    EdgeLabel el = this.newEdgeLabel(IdGenerator.of(1), "knows", Frequency.SINGLE, vl.id(), vl.id(), date.id(), weight.id());
    HugeVertex source = new HugeVertex(this.graph(), IdGenerator.of(sourceVertexId), vl);
    source.addProperty(name, "tom");
    source.addProperty(age, 18);
    source.addProperty(city, "Beijing");
    HugeVertex target = new HugeVertex(this.graph(), IdGenerator.of(targetVertexId), vl);
    target.addProperty(name, "cat");
    target.addProperty(age, 20);
    target.addProperty(city, "Shanghai");
    Id id = EdgeId.parse("L123456>1>>L987654");
    HugeEdge edge = new HugeEdge(this.graph(), id, el);
    Whitebox.setInternalState(edge, "sourceVertex", source);
    Whitebox.setInternalState(edge, "targetVertex", target);
    edge.assignId();
    edge.addProperty(date, new Date());
    edge.addProperty(weight, 0.75);
    return edge;
}
Also used : VertexLabel(com.baidu.hugegraph.schema.VertexLabel) EdgeLabel(com.baidu.hugegraph.schema.EdgeLabel) HugeEdge(com.baidu.hugegraph.structure.HugeEdge) EdgeId(com.baidu.hugegraph.backend.id.EdgeId) Id(com.baidu.hugegraph.backend.id.Id) HugeVertex(com.baidu.hugegraph.structure.HugeVertex) PropertyKey(com.baidu.hugegraph.schema.PropertyKey) Date(java.util.Date)

Example 63 with Id

use of com.baidu.hugegraph.backend.id.Id in project incubator-hugegraph by apache.

the class FakeObjects method newEdge.

public HugeEdge newEdge(String sourceVertexId, String targetVertexId) {
    PropertyKey name = this.newPropertyKey(IdGenerator.of(1), "name");
    PropertyKey age = this.newPropertyKey(IdGenerator.of(2), "age", DataType.INT, Cardinality.SINGLE);
    PropertyKey city = this.newPropertyKey(IdGenerator.of(3), "city");
    PropertyKey date = this.newPropertyKey(IdGenerator.of(4), "date", DataType.DATE);
    PropertyKey weight = this.newPropertyKey(IdGenerator.of(5), "weight", DataType.DOUBLE);
    VertexLabel vl = this.newVertexLabel(IdGenerator.of(1), "person", IdStrategy.CUSTOMIZE_NUMBER, name.id(), age.id(), city.id());
    EdgeLabel el = this.newEdgeLabel(IdGenerator.of(1), "knows", Frequency.SINGLE, vl.id(), vl.id(), date.id(), weight.id());
    HugeVertex source = new HugeVertex(this.graph(), IdGenerator.of(sourceVertexId), vl);
    source.addProperty(name, "tom");
    source.addProperty(age, 18);
    source.addProperty(city, "Beijing");
    HugeVertex target = new HugeVertex(this.graph(), IdGenerator.of(targetVertexId), vl);
    target.addProperty(name, "cat");
    target.addProperty(age, 20);
    target.addProperty(city, "Shanghai");
    Id id = EdgeId.parse("L123456>1>>L987654");
    HugeEdge edge = new HugeEdge(this.graph(), id, el);
    Whitebox.setInternalState(edge, "sourceVertex", source);
    Whitebox.setInternalState(edge, "targetVertex", target);
    edge.assignId();
    edge.addProperty(date, new Date());
    edge.addProperty(weight, 0.75);
    return edge;
}
Also used : VertexLabel(com.baidu.hugegraph.schema.VertexLabel) EdgeLabel(com.baidu.hugegraph.schema.EdgeLabel) HugeEdge(com.baidu.hugegraph.structure.HugeEdge) EdgeId(com.baidu.hugegraph.backend.id.EdgeId) Id(com.baidu.hugegraph.backend.id.Id) HugeVertex(com.baidu.hugegraph.structure.HugeVertex) PropertyKey(com.baidu.hugegraph.schema.PropertyKey) Date(java.util.Date)

Example 64 with Id

use of com.baidu.hugegraph.backend.id.Id in project incubator-hugegraph by apache.

the class CacheTest method testMultiThreadsGetWith3Items.

@Test
public void testMultiThreadsGetWith3Items() {
    Cache<Id, Object> cache = newCache(10);
    Id id1 = IdGenerator.of("1");
    cache.update(id1, "value-1");
    Id id2 = IdGenerator.of("2");
    cache.update(id2, "value-2");
    Id id3 = IdGenerator.of("3");
    cache.update(id3, "value-3");
    Assert.assertEquals(3, cache.size());
    runWithThreads(THREADS_NUM, () -> {
        for (int i = 0; i < 10000 * 100; i++) {
            Assert.assertEquals("value-1", cache.get(id1));
            Assert.assertEquals("value-2", cache.get(id2));
            Assert.assertEquals("value-2", cache.get(id2));
            Assert.assertEquals("value-3", cache.get(id3));
            Assert.assertEquals("value-1", cache.get(id1));
        }
    });
    Assert.assertEquals(3, cache.size());
}
Also used : Id(com.baidu.hugegraph.backend.id.Id) BaseUnitTest(com.baidu.hugegraph.unit.BaseUnitTest) Test(org.junit.Test)

Example 65 with Id

use of com.baidu.hugegraph.backend.id.Id in project incubator-hugegraph by apache.

the class CacheTest method testHitsAndMiss.

@Test
public void testHitsAndMiss() {
    Cache<Id, Object> cache = newCache();
    Assert.assertEquals(false, cache.enableMetrics(true));
    Assert.assertEquals(0L, cache.hits());
    Assert.assertEquals(0L, cache.miss());
    Id id = IdGenerator.of("1");
    cache.update(id, "value-1");
    Assert.assertEquals(0L, cache.hits());
    Assert.assertEquals(0L, cache.miss());
    cache.get(IdGenerator.of("not-exist"));
    Assert.assertEquals(0L, cache.hits());
    Assert.assertEquals(1L, cache.miss());
    cache.get(IdGenerator.of("1"));
    Assert.assertEquals(1L, cache.hits());
    Assert.assertEquals(1L, cache.miss());
    cache.get(IdGenerator.of("not-exist"));
    Assert.assertEquals(1L, cache.hits());
    Assert.assertEquals(2L, cache.miss());
    cache.get(IdGenerator.of("1"));
    Assert.assertEquals(2L, cache.hits());
    Assert.assertEquals(2L, cache.miss());
}
Also used : Id(com.baidu.hugegraph.backend.id.Id) BaseUnitTest(com.baidu.hugegraph.unit.BaseUnitTest) Test(org.junit.Test)

Aggregations

Id (com.baidu.hugegraph.backend.id.Id)381 Test (org.junit.Test)124 HugeGraph (com.baidu.hugegraph.HugeGraph)104 EdgeId (com.baidu.hugegraph.backend.id.EdgeId)71 BaseUnitTest (com.baidu.hugegraph.unit.BaseUnitTest)54 Timed (com.codahale.metrics.annotation.Timed)41 Produces (jakarta.ws.rs.Produces)40 AuthManager (com.baidu.hugegraph.auth.AuthManager)39 HugeVertex (com.baidu.hugegraph.structure.HugeVertex)37 ArrayList (java.util.ArrayList)35 HugeEdge (com.baidu.hugegraph.structure.HugeEdge)33 Vertex (org.apache.tinkerpop.gremlin.structure.Vertex)33 Watched (com.baidu.hugegraph.perf.PerfUtil.Watched)32 PropertyKey (com.baidu.hugegraph.schema.PropertyKey)32 Directions (com.baidu.hugegraph.type.define.Directions)29 ConditionQuery (com.baidu.hugegraph.backend.query.ConditionQuery)25 IndexLabel (com.baidu.hugegraph.schema.IndexLabel)25 VertexLabel (com.baidu.hugegraph.schema.VertexLabel)24 Edge (org.apache.tinkerpop.gremlin.structure.Edge)22 HugeType (com.baidu.hugegraph.type.HugeType)21