Search in sources :

Example 1 with RamTable

use of com.baidu.hugegraph.backend.store.ram.RamTable in project incubator-hugegraph by apache.

the class CachedGraphTransaction method queryEdgesFromBackend.

@Override
@Watched(prefix = "graphcache")
protected final Iterator<HugeEdge> queryEdgesFromBackend(Query query) {
    RamTable ramtable = this.params().ramtable();
    if (ramtable != null && ramtable.matched(query)) {
        return ramtable.query(query);
    }
    if (!this.enableCacheEdge() || query.empty() || query.paging() || query.bigCapacity()) {
        // Query all edges or query edges in paging, don't cache it
        return super.queryEdgesFromBackend(query);
    }
    Id cacheKey = new QueryId(query);
    Object value = this.edgesCache.get(cacheKey);
    @SuppressWarnings("unchecked") Collection<HugeEdge> edges = (Collection<HugeEdge>) value;
    if (value != null) {
        for (HugeEdge edge : edges) {
            if (edge.expired()) {
                this.edgesCache.invalidate(cacheKey);
                value = null;
            }
        }
    }
    if (value != null) {
        // Not cached or the cache expired
        return edges.iterator();
    }
    Iterator<HugeEdge> rs = super.queryEdgesFromBackend(query);
    /*
         * Iterator can't be cached, caching list instead
         * there may be super node and too many edges in a query,
         * try fetch a few of the head results and determine whether to cache.
         */
    final int tryMax = 1 + MAX_CACHE_EDGES_PER_QUERY;
    assert tryMax > MAX_CACHE_EDGES_PER_QUERY;
    edges = new ArrayList<>(tryMax);
    for (int i = 0; rs.hasNext() && i < tryMax; i++) {
        edges.add(rs.next());
    }
    if (edges.size() == 0) {
        this.edgesCache.update(cacheKey, Collections.emptyList());
    } else if (edges.size() <= MAX_CACHE_EDGES_PER_QUERY) {
        this.edgesCache.update(cacheKey, edges);
    }
    return new ExtendableIterator<>(edges.iterator(), rs);
}
Also used : ExtendableIterator(com.baidu.hugegraph.iterator.ExtendableIterator) QueryId(com.baidu.hugegraph.backend.cache.CachedBackendStore.QueryId) HugeEdge(com.baidu.hugegraph.structure.HugeEdge) Collection(java.util.Collection) RamTable(com.baidu.hugegraph.backend.store.ram.RamTable) QueryId(com.baidu.hugegraph.backend.cache.CachedBackendStore.QueryId) Id(com.baidu.hugegraph.backend.id.Id) Watched(com.baidu.hugegraph.perf.PerfUtil.Watched)

Example 2 with RamTable

use of com.baidu.hugegraph.backend.store.ram.RamTable in project incubator-hugegraph by apache.

the class RamTableTest method testAddAndQuery.

@Test
public void testAddAndQuery() throws Exception {
    HugeGraph graph = this.graph();
    int el1 = (int) graph.edgeLabel("el1").id().asLong();
    int el2 = (int) graph.edgeLabel("el2").id().asLong();
    RamTable table = new RamTable(graph, VERTEX_SIZE, EDGE_SIZE);
    long oldSize = table.edgesSize();
    // insert edges
    for (int i = 0; i < VERTEX_SIZE; i++) {
        table.addEdge(true, i, i, Directions.OUT, el1);
        Assert.assertEquals(oldSize + 2 * i + 1, table.edgesSize());
        table.addEdge(false, i, i + 1, Directions.IN, el2);
        Assert.assertEquals(oldSize + 2 * i + 2, table.edgesSize());
    }
    // query by BOTH
    for (int i = 0; i < VERTEX_SIZE; i++) {
        Iterator<HugeEdge> edges = table.query(i, Directions.BOTH, 0);
        Assert.assertTrue(edges.hasNext());
        HugeEdge edge1 = edges.next();
        Assert.assertEquals(i, edge1.id().ownerVertexId().asLong());
        Assert.assertEquals(i, edge1.id().otherVertexId().asLong());
        Assert.assertEquals(Directions.OUT, edge1.direction());
        Assert.assertEquals("el1", edge1.label());
        Assert.assertTrue(edges.hasNext());
        HugeEdge edge2 = edges.next();
        Assert.assertEquals(i, edge2.id().ownerVertexId().asLong());
        Assert.assertEquals(i + 1L, edge2.id().otherVertexId().asLong());
        Assert.assertEquals(Directions.IN, edge2.direction());
        Assert.assertEquals("el2", edge2.label());
        Assert.assertFalse(edges.hasNext());
    }
    // query by OUT
    for (int i = 0; i < VERTEX_SIZE; i++) {
        Iterator<HugeEdge> edges = table.query(i, Directions.OUT, el1);
        Assert.assertTrue(edges.hasNext());
        HugeEdge edge1 = edges.next();
        Assert.assertEquals(i, edge1.id().ownerVertexId().asLong());
        Assert.assertEquals(i, edge1.id().otherVertexId().asLong());
        Assert.assertEquals(Directions.OUT, edge1.direction());
        Assert.assertEquals("el1", edge1.label());
        Assert.assertFalse(edges.hasNext());
    }
    // query by IN
    for (int i = 0; i < VERTEX_SIZE; i++) {
        Iterator<HugeEdge> edges = table.query(i, Directions.IN, el2);
        Assert.assertTrue(edges.hasNext());
        HugeEdge edge1 = edges.next();
        Assert.assertEquals(i, edge1.id().ownerVertexId().asLong());
        Assert.assertEquals(i + 1L, edge1.id().otherVertexId().asLong());
        Assert.assertEquals(Directions.IN, edge1.direction());
        Assert.assertEquals("el2", edge1.label());
        Assert.assertFalse(edges.hasNext());
    }
    // query by BOTH & label 1
    for (int i = 0; i < VERTEX_SIZE; i++) {
        Iterator<HugeEdge> edges = table.query(i, Directions.BOTH, el1);
        Assert.assertTrue(edges.hasNext());
        HugeEdge edge1 = edges.next();
        Assert.assertEquals(i, edge1.id().ownerVertexId().asLong());
        Assert.assertEquals(i, edge1.id().otherVertexId().asLong());
        Assert.assertEquals(Directions.OUT, edge1.direction());
        Assert.assertEquals("el1", edge1.label());
        Assert.assertFalse(edges.hasNext());
    }
    // query by BOTH & label 2
    for (int i = 0; i < VERTEX_SIZE; i++) {
        Iterator<HugeEdge> edges = table.query(i, Directions.BOTH, el2);
        Assert.assertTrue(edges.hasNext());
        HugeEdge edge1 = edges.next();
        Assert.assertEquals(i, edge1.id().ownerVertexId().asLong());
        Assert.assertEquals(i + 1L, edge1.id().otherVertexId().asLong());
        Assert.assertEquals(Directions.IN, edge1.direction());
        Assert.assertEquals("el2", edge1.label());
        Assert.assertFalse(edges.hasNext());
    }
    // query non-exist vertex
    Iterator<HugeEdge> edges = table.query(VERTEX_SIZE, Directions.BOTH, 0);
    Assert.assertFalse(edges.hasNext());
}
Also used : HugeGraph(com.baidu.hugegraph.HugeGraph) HugeEdge(com.baidu.hugegraph.structure.HugeEdge) RamTable(com.baidu.hugegraph.backend.store.ram.RamTable) Test(org.junit.Test)

Example 3 with RamTable

use of com.baidu.hugegraph.backend.store.ram.RamTable in project incubator-hugegraph by apache.

the class RamTableTest method testAddAndQueryWithoutAdjEdges.

@Test
public void testAddAndQueryWithoutAdjEdges() throws Exception {
    HugeGraph graph = this.graph();
    int el1 = (int) graph.edgeLabel("el1").id().asLong();
    int el2 = (int) graph.edgeLabel("el2").id().asLong();
    RamTable table = new RamTable(graph, VERTEX_SIZE, EDGE_SIZE);
    long oldSize = table.edgesSize();
    // insert edges
    for (int i = 0; i < VERTEX_SIZE; i++) {
        if (i % 3 != 0) {
            // don't insert edges for 2/3 vertices
            continue;
        }
        table.addEdge(true, i, i, Directions.OUT, el1);
        Assert.assertEquals(oldSize + i + 1, table.edgesSize());
        table.addEdge(false, i, i, Directions.OUT, el2);
        Assert.assertEquals(oldSize + i + 2, table.edgesSize());
        table.addEdge(false, i, i + 1, Directions.IN, el2);
        Assert.assertEquals(oldSize + i + 3, table.edgesSize());
    }
    // query by BOTH
    for (int i = 0; i < VERTEX_SIZE; i++) {
        Iterator<HugeEdge> edges = table.query(i, Directions.BOTH, 0);
        if (i % 3 != 0) {
            Assert.assertFalse(edges.hasNext());
            continue;
        }
        Assert.assertTrue(edges.hasNext());
        HugeEdge edge1 = edges.next();
        Assert.assertEquals(i, edge1.id().ownerVertexId().asLong());
        Assert.assertEquals(i, edge1.id().otherVertexId().asLong());
        Assert.assertEquals(Directions.OUT, edge1.direction());
        Assert.assertEquals("el1", edge1.label());
        Assert.assertTrue(edges.hasNext());
        HugeEdge edge2 = edges.next();
        Assert.assertEquals(i, edge2.id().ownerVertexId().asLong());
        Assert.assertEquals(i, edge2.id().otherVertexId().asLong());
        Assert.assertEquals(Directions.OUT, edge2.direction());
        Assert.assertEquals("el2", edge2.label());
        Assert.assertTrue(edges.hasNext());
        HugeEdge edge3 = edges.next();
        Assert.assertEquals(i, edge3.id().ownerVertexId().asLong());
        Assert.assertEquals(i + 1L, edge3.id().otherVertexId().asLong());
        Assert.assertEquals(Directions.IN, edge3.direction());
        Assert.assertEquals("el2", edge3.label());
        Assert.assertFalse(edges.hasNext());
    }
}
Also used : HugeGraph(com.baidu.hugegraph.HugeGraph) HugeEdge(com.baidu.hugegraph.structure.HugeEdge) RamTable(com.baidu.hugegraph.backend.store.ram.RamTable) Test(org.junit.Test)

Example 4 with RamTable

use of com.baidu.hugegraph.backend.store.ram.RamTable in project incubator-hugegraph by apache.

the class RamTableTest method setup.

@Override
@Before
public void setup() {
    super.setup();
    HugeGraph graph = this.graph();
    Assume.assumeTrue("Ramtable is not supported by backend", graph.backendStoreFeatures().supportsScanKeyPrefix());
    this.ramtable = Whitebox.getInternalState(graph, "ramtable");
    if (this.ramtable == null) {
        Whitebox.setInternalState(graph, "ramtable", new RamTable(graph, 2000, 1200));
    }
    graph.schema().vertexLabel("vl1").useCustomizeNumberId().create();
    graph.schema().vertexLabel("vl2").useCustomizeNumberId().create();
    graph.schema().edgeLabel("el1").sourceLabel("vl1").targetLabel("vl1").create();
    graph.schema().edgeLabel("el2").sourceLabel("vl2").targetLabel("vl2").create();
}
Also used : HugeGraph(com.baidu.hugegraph.HugeGraph) RamTable(com.baidu.hugegraph.backend.store.ram.RamTable) Before(org.junit.Before)

Example 5 with RamTable

use of com.baidu.hugegraph.backend.store.ram.RamTable in project incubator-hugegraph by apache.

the class RamTableTest method testAddInvalidVertexOrEdge.

@Test
public void testAddInvalidVertexOrEdge() {
    HugeGraph graph = this.graph();
    VertexLabel vl3 = graph.vertexLabel("vl3");
    EdgeLabel el3 = graph.edgeLabel("el3");
    VertexLabel vl2 = graph.vertexLabel("vl2");
    EdgeLabel el2 = graph.edgeLabel("el2");
    RamTable table = new RamTable(graph, VERTEX_SIZE, EDGE_SIZE);
    HugeVertex ownerVertex = new HugeVertex(graph, IdGenerator.of(1), vl3);
    HugeEdge edge1 = HugeEdge.constructEdge(ownerVertex, true, el3, "marko", IdGenerator.of(2));
    Assert.assertThrows(HugeException.class, () -> {
        table.addEdge(true, edge1);
    }, e -> {
        Assert.assertContains("Only edge label without sortkey is " + "supported by ramtable, but got 'el3(id=3)'", e.getMessage());
    });
    HugeVertex v1 = new HugeVertex(graph, IdGenerator.of("s1"), vl2);
    HugeEdge edge2 = HugeEdge.constructEdge(v1, true, el2, "marko", IdGenerator.of("s2"));
    Assert.assertThrows(HugeException.class, () -> {
        table.addEdge(true, edge2);
    }, e -> {
        Assert.assertContains("Only number id is supported by ramtable, " + "but got string id 's1'", e.getMessage());
    });
    HugeVertex v2 = new HugeVertex(graph, IdGenerator.of(2), vl2);
    HugeEdge edge3 = HugeEdge.constructEdge(v2, true, el2, "marko", IdGenerator.of("s2"));
    Assert.assertThrows(HugeException.class, () -> {
        table.addEdge(true, edge3);
    }, e -> {
        Assert.assertContains("Only number id is supported by ramtable, " + "but got string id 's2'", e.getMessage());
    });
}
Also used : HugeGraph(com.baidu.hugegraph.HugeGraph) VertexLabel(com.baidu.hugegraph.schema.VertexLabel) EdgeLabel(com.baidu.hugegraph.schema.EdgeLabel) HugeEdge(com.baidu.hugegraph.structure.HugeEdge) RamTable(com.baidu.hugegraph.backend.store.ram.RamTable) HugeVertex(com.baidu.hugegraph.structure.HugeVertex) Test(org.junit.Test)

Aggregations

RamTable (com.baidu.hugegraph.backend.store.ram.RamTable)5 HugeGraph (com.baidu.hugegraph.HugeGraph)4 HugeEdge (com.baidu.hugegraph.structure.HugeEdge)4 Test (org.junit.Test)3 QueryId (com.baidu.hugegraph.backend.cache.CachedBackendStore.QueryId)1 Id (com.baidu.hugegraph.backend.id.Id)1 ExtendableIterator (com.baidu.hugegraph.iterator.ExtendableIterator)1 Watched (com.baidu.hugegraph.perf.PerfUtil.Watched)1 EdgeLabel (com.baidu.hugegraph.schema.EdgeLabel)1 VertexLabel (com.baidu.hugegraph.schema.VertexLabel)1 HugeVertex (com.baidu.hugegraph.structure.HugeVertex)1 Collection (java.util.Collection)1 Before (org.junit.Before)1