Search in sources :

Example 71 with Id

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

the class CacheManagerTest method testCacheInstance.

@Test
public void testCacheInstance() {
    // Don't mock
    teardown();
    CacheManager manager = CacheManager.instance();
    Cache<Id, Object> c1 = manager.cache("c1");
    Cache<Id, Object> c12 = manager.cache("c1");
    Cache<Id, Object> c13 = manager.cache("c1", 123);
    Assert.assertEquals(c1, c12);
    Assert.assertEquals(c1, c13);
    Assert.assertEquals(c1.capacity(), c13.capacity());
    Cache<Id, Object> c2 = manager.offheapCache(null, "c2", 1, 11);
    Cache<Id, Object> c22 = manager.offheapCache(null, "c2", 2, 22);
    Cache<Id, Object> c23 = manager.offheapCache(null, "c2", 3, 33);
    Assert.assertEquals(c2, c22);
    Assert.assertEquals(c2, c23);
    Assert.assertEquals(c2.capacity(), c23.capacity());
    Cache<Id, Object> c3 = manager.levelCache(null, "c3", 1, 1, 11);
    Cache<Id, Object> c32 = manager.levelCache(null, "c3", 2, 2, 22);
    Cache<Id, Object> c33 = manager.levelCache(null, "c3", 3, 3, 33);
    Assert.assertEquals(c3, c32);
    Assert.assertEquals(c3, c33);
    Assert.assertEquals(c3.capacity(), c33.capacity());
    Assert.assertThrows(IllegalArgumentException.class, () -> {
        manager.cache("c2");
    }, e -> {
        Assert.assertContains("Invalid cache implement:", e.getMessage());
        Assert.assertContains("OffheapCache", e.getMessage());
    });
    Assert.assertThrows(IllegalArgumentException.class, () -> {
        manager.cache("c3");
    }, e -> {
        Assert.assertContains("Invalid cache implement:", e.getMessage());
        Assert.assertContains("LevelCache", e.getMessage());
    });
    Assert.assertThrows(IllegalArgumentException.class, () -> {
        manager.offheapCache(null, "c1", 1, 11);
    }, e -> {
        Assert.assertContains("Invalid cache implement:", e.getMessage());
        Assert.assertContains("RamCache", e.getMessage());
    });
    Assert.assertThrows(IllegalArgumentException.class, () -> {
        manager.offheapCache(null, "c3", 1, 11);
    }, e -> {
        Assert.assertContains("Invalid cache implement:", e.getMessage());
        Assert.assertContains("LevelCache", e.getMessage());
    });
    Assert.assertThrows(IllegalArgumentException.class, () -> {
        manager.levelCache(null, "c1", 1, 1, 11);
    }, e -> {
        Assert.assertContains("Invalid cache implement:", e.getMessage());
        Assert.assertContains("RamCache", e.getMessage());
    });
    Assert.assertThrows(IllegalArgumentException.class, () -> {
        manager.levelCache(null, "c2", 1, 1, 11);
    }, e -> {
        Assert.assertContains("Invalid cache implement:", e.getMessage());
        Assert.assertContains("OffheapCache", e.getMessage());
    });
    this.originCaches.remove("c1");
    this.originCaches.remove("c2");
    this.originCaches.remove("c3");
}
Also used : CacheManager(com.baidu.hugegraph.backend.cache.CacheManager) Id(com.baidu.hugegraph.backend.id.Id) BaseUnitTest(com.baidu.hugegraph.unit.BaseUnitTest) Test(org.junit.Test)

Example 72 with Id

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

the class CacheManagerTest method testCacheEnableMetrics.

@Test
public void testCacheEnableMetrics() {
    // Don't mock
    teardown();
    CacheManager manager = CacheManager.instance();
    Cache<Id, Object> c1 = manager.cache("m1");
    Cache<Id, Object> c2 = manager.cache("m2");
    Cache<Id, Object> c3 = manager.offheapCache(null, "m3", 1, 11);
    Cache<Id, Object> c4 = manager.levelCache(null, "m4", 1, 1, 11);
    Assert.assertEquals(false, c1.enableMetrics(false));
    Assert.assertEquals(false, c2.enableMetrics(false));
    Assert.assertEquals(false, c3.enableMetrics(false));
    Assert.assertEquals(false, c4.enableMetrics(false));
    Assert.assertEquals(false, CacheManager.cacheEnableMetrics("m1", true));
    Assert.assertEquals(true, c1.enableMetrics(true));
    Assert.assertEquals(false, CacheManager.cacheEnableMetrics("m2", true));
    Assert.assertEquals(true, c2.enableMetrics(true));
    Assert.assertEquals(false, CacheManager.cacheEnableMetrics("m3", true));
    Assert.assertEquals(true, c3.enableMetrics(true));
    Assert.assertEquals(false, CacheManager.cacheEnableMetrics("m4", true));
    Assert.assertEquals(true, c4.enableMetrics(true));
    Assert.assertEquals(true, CacheManager.cacheEnableMetrics("m1", false));
    Assert.assertEquals(false, c1.enableMetrics(true));
    Assert.assertEquals(true, CacheManager.cacheEnableMetrics("m2", false));
    Assert.assertEquals(false, c2.enableMetrics(true));
    Assert.assertEquals(true, CacheManager.cacheEnableMetrics("m3", false));
    Assert.assertEquals(false, c3.enableMetrics(true));
    Assert.assertEquals(true, CacheManager.cacheEnableMetrics("m4", false));
    Assert.assertEquals(false, c4.enableMetrics(true));
}
Also used : CacheManager(com.baidu.hugegraph.backend.cache.CacheManager) Id(com.baidu.hugegraph.backend.id.Id) BaseUnitTest(com.baidu.hugegraph.unit.BaseUnitTest) Test(org.junit.Test)

Example 73 with Id

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

the class CacheTest method testUpdateIfAbsentWithExistKey.

@Test
public void testUpdateIfAbsentWithExistKey() {
    Cache<Id, Object> cache = newCache();
    Id id = IdGenerator.of("1");
    cache.update(id, "value-1");
    cache.updateIfAbsent(id, "value-2");
    Assert.assertEquals("value-1", cache.get(id));
}
Also used : Id(com.baidu.hugegraph.backend.id.Id) BaseUnitTest(com.baidu.hugegraph.unit.BaseUnitTest) Test(org.junit.Test)

Example 74 with Id

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

the class CacheTest method testKeyExpired.

@Test
public void testKeyExpired() {
    Cache<Id, Object> cache = newCache();
    cache.expire(2000L);
    Id key = IdGenerator.of("key");
    cache.update(key, "value", -1000L);
    waitTillNext(1);
    cache.tick();
    Assert.assertFalse(cache.containsKey(key));
    cache.update(key, "value", -2000L);
    cache.tick();
    Assert.assertFalse(cache.containsKey(key));
}
Also used : Id(com.baidu.hugegraph.backend.id.Id) BaseUnitTest(com.baidu.hugegraph.unit.BaseUnitTest) Test(org.junit.Test)

Example 75 with Id

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

the class CacheTest method testGetOrFetch.

@Test
public void testGetOrFetch() {
    Cache<Id, Object> cache = newCache();
    Id id = IdGenerator.of("1");
    Assert.assertNull(cache.get(id));
    Assert.assertEquals("value-1", cache.getOrFetch(id, key -> {
        return "value-1";
    }));
    cache.update(id, "value-2");
    Assert.assertEquals("value-2", cache.getOrFetch(id, key -> {
        return "value-1";
    }));
}
Also used : Blob(com.baidu.hugegraph.util.Blob) Date(java.util.Date) BaseUnitTest(com.baidu.hugegraph.unit.BaseUnitTest) RamCache(com.baidu.hugegraph.backend.cache.RamCache) HashMap(java.util.HashMap) Test(org.junit.Test) UUID(java.util.UUID) LevelCache(com.baidu.hugegraph.backend.cache.LevelCache) LinkedHashMap(java.util.LinkedHashMap) Mockito(org.mockito.Mockito) Bytes(com.baidu.hugegraph.util.Bytes) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) HugeGraph(com.baidu.hugegraph.HugeGraph) IdGenerator(com.baidu.hugegraph.backend.id.IdGenerator) Map(java.util.Map) After(org.junit.After) Id(com.baidu.hugegraph.backend.id.Id) Whitebox(com.baidu.hugegraph.testutil.Whitebox) Cache(com.baidu.hugegraph.backend.cache.Cache) Assert(com.baidu.hugegraph.testutil.Assert) OffheapCache(com.baidu.hugegraph.backend.cache.OffheapCache) Before(org.junit.Before) 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