Search in sources :

Example 1 with CachedSchemaTransaction

use of com.baidu.hugegraph.backend.cache.CachedSchemaTransaction in project incubator-hugegraph by apache.

the class CachedSchemaTransactionTest method testEventClear.

@Test
public void testEventClear() throws Exception {
    CachedSchemaTransaction cache = this.cache();
    FakeObjects objects = new FakeObjects("unit-test");
    cache.addPropertyKey(objects.newPropertyKey(IdGenerator.of(1), "fake-pk-1"));
    cache.addPropertyKey(objects.newPropertyKey(IdGenerator.of(2), "fake-pk-2"));
    Assert.assertEquals(2L, Whitebox.invoke(cache, "idCache", "size"));
    Assert.assertEquals(2L, Whitebox.invoke(cache, "nameCache", "size"));
    Assert.assertEquals("fake-pk-1", cache.getPropertyKey(IdGenerator.of(1)).name());
    Assert.assertEquals(IdGenerator.of(1), cache.getPropertyKey("fake-pk-1").id());
    Assert.assertEquals("fake-pk-2", cache.getPropertyKey(IdGenerator.of(2)).name());
    Assert.assertEquals(IdGenerator.of(2), cache.getPropertyKey("fake-pk-2").id());
    this.params.schemaEventHub().notify(Events.CACHE, "clear", null).get();
    Assert.assertEquals(0L, Whitebox.invoke(cache, "idCache", "size"));
    Assert.assertEquals(0L, Whitebox.invoke(cache, "nameCache", "size"));
    Assert.assertEquals("fake-pk-1", cache.getPropertyKey(IdGenerator.of(1)).name());
    Assert.assertEquals(IdGenerator.of(1), cache.getPropertyKey("fake-pk-1").id());
    Assert.assertEquals("fake-pk-2", cache.getPropertyKey(IdGenerator.of(2)).name());
    Assert.assertEquals(IdGenerator.of(2), cache.getPropertyKey("fake-pk-2").id());
    Assert.assertEquals(2L, Whitebox.invoke(cache, "idCache", "size"));
    Assert.assertEquals(2L, Whitebox.invoke(cache, "nameCache", "size"));
}
Also used : FakeObjects(com.baidu.hugegraph.unit.FakeObjects) CachedSchemaTransaction(com.baidu.hugegraph.backend.cache.CachedSchemaTransaction) BaseUnitTest(com.baidu.hugegraph.unit.BaseUnitTest) Test(org.junit.Test)

Example 2 with CachedSchemaTransaction

use of com.baidu.hugegraph.backend.cache.CachedSchemaTransaction in project incubator-hugegraph by apache.

the class CachedSchemaTransactionTest method setup.

@Before
public void setup() {
    HugeGraph graph = HugeFactory.open(FakeObjects.newConfig());
    this.params = Whitebox.getInternalState(graph, "params");
    this.cache = new CachedSchemaTransaction(this.params, this.params.loadSchemaStore());
}
Also used : HugeGraph(com.baidu.hugegraph.HugeGraph) CachedSchemaTransaction(com.baidu.hugegraph.backend.cache.CachedSchemaTransaction) Before(org.junit.Before)

Example 3 with CachedSchemaTransaction

use of com.baidu.hugegraph.backend.cache.CachedSchemaTransaction in project incubator-hugegraph by apache.

the class CachedSchemaTransactionTest method testEventInvalid.

@Test
public void testEventInvalid() throws Exception {
    CachedSchemaTransaction cache = this.cache();
    FakeObjects objects = new FakeObjects("unit-test");
    cache.addPropertyKey(objects.newPropertyKey(IdGenerator.of(1), "fake-pk-1"));
    cache.addPropertyKey(objects.newPropertyKey(IdGenerator.of(2), "fake-pk-2"));
    Assert.assertEquals(2L, Whitebox.invoke(cache, "idCache", "size"));
    Assert.assertEquals(2L, Whitebox.invoke(cache, "nameCache", "size"));
    Assert.assertEquals("fake-pk-1", cache.getPropertyKey(IdGenerator.of(1)).name());
    Assert.assertEquals(IdGenerator.of(1), cache.getPropertyKey("fake-pk-1").id());
    Assert.assertEquals("fake-pk-2", cache.getPropertyKey(IdGenerator.of(2)).name());
    Assert.assertEquals(IdGenerator.of(2), cache.getPropertyKey("fake-pk-2").id());
    this.params.schemaEventHub().notify(Events.CACHE, "invalid", HugeType.PROPERTY_KEY, IdGenerator.of(1)).get();
    Assert.assertEquals(1L, Whitebox.invoke(cache, "idCache", "size"));
    Assert.assertEquals(1L, Whitebox.invoke(cache, "nameCache", "size"));
    Assert.assertEquals("fake-pk-1", cache.getPropertyKey(IdGenerator.of(1)).name());
    Assert.assertEquals(IdGenerator.of(1), cache.getPropertyKey("fake-pk-1").id());
    Assert.assertEquals("fake-pk-2", cache.getPropertyKey(IdGenerator.of(2)).name());
    Assert.assertEquals(IdGenerator.of(2), cache.getPropertyKey("fake-pk-2").id());
    Assert.assertEquals(2L, Whitebox.invoke(cache, "idCache", "size"));
    Assert.assertEquals(2L, Whitebox.invoke(cache, "nameCache", "size"));
}
Also used : FakeObjects(com.baidu.hugegraph.unit.FakeObjects) CachedSchemaTransaction(com.baidu.hugegraph.backend.cache.CachedSchemaTransaction) BaseUnitTest(com.baidu.hugegraph.unit.BaseUnitTest) Test(org.junit.Test)

Example 4 with CachedSchemaTransaction

use of com.baidu.hugegraph.backend.cache.CachedSchemaTransaction in project incubator-hugegraph by apache.

the class CachedSchemaTransactionTest method testResetCachedAllIfReachedCapacity.

@Test
public void testResetCachedAllIfReachedCapacity() throws Exception {
    CachedSchemaTransaction cache = this.cache();
    Object old = Whitebox.getInternalState(cache, "idCache.capacity");
    Whitebox.setInternalState(cache, "idCache.capacity", 2);
    try {
        Assert.assertEquals(0L, Whitebox.invoke(cache, "idCache", "size"));
        FakeObjects objects = new FakeObjects("unit-test");
        cache.addPropertyKey(objects.newPropertyKey(IdGenerator.of(1), "fake-pk-1"));
        Assert.assertEquals(1L, Whitebox.invoke(cache, "idCache", "size"));
        Assert.assertEquals(1, cache.getPropertyKeys().size());
        Whitebox.invoke(CachedSchemaTransaction.class, "cachedTypes", cache);
        Assert.assertEquals(ImmutableMap.of(HugeType.PROPERTY_KEY, true), Whitebox.invoke(CachedSchemaTransaction.class, "cachedTypes", cache));
        cache.addPropertyKey(objects.newPropertyKey(IdGenerator.of(3), "fake-pk-2"));
        cache.addPropertyKey(objects.newPropertyKey(IdGenerator.of(2), "fake-pk-3"));
        Assert.assertEquals(2L, Whitebox.invoke(cache, "idCache", "size"));
        Assert.assertEquals(3, cache.getPropertyKeys().size());
        Assert.assertEquals(ImmutableMap.of(), Whitebox.invoke(CachedSchemaTransaction.class, "cachedTypes", cache));
    } finally {
        Whitebox.setInternalState(cache, "idCache.capacity", old);
    }
}
Also used : FakeObjects(com.baidu.hugegraph.unit.FakeObjects) CachedSchemaTransaction(com.baidu.hugegraph.backend.cache.CachedSchemaTransaction) BaseUnitTest(com.baidu.hugegraph.unit.BaseUnitTest) Test(org.junit.Test)

Example 5 with CachedSchemaTransaction

use of com.baidu.hugegraph.backend.cache.CachedSchemaTransaction in project incubator-hugegraph by apache.

the class CachedSchemaTransactionTest method testGetSchema.

@Test
public void testGetSchema() throws Exception {
    CachedSchemaTransaction cache = this.cache();
    FakeObjects objects = new FakeObjects("unit-test");
    cache.addPropertyKey(objects.newPropertyKey(IdGenerator.of(1), "fake-pk-1"));
    this.params.schemaEventHub().notify(Events.CACHE, "clear", null).get();
    Assert.assertEquals("fake-pk-1", cache.getPropertyKey(IdGenerator.of(1)).name());
    Assert.assertEquals(IdGenerator.of(1), cache.getPropertyKey("fake-pk-1").id());
    this.params.schemaEventHub().notify(Events.CACHE, "clear", null).get();
    Assert.assertEquals(IdGenerator.of(1), cache.getPropertyKey("fake-pk-1").id());
    Assert.assertEquals("fake-pk-1", cache.getPropertyKey(IdGenerator.of(1)).name());
}
Also used : FakeObjects(com.baidu.hugegraph.unit.FakeObjects) CachedSchemaTransaction(com.baidu.hugegraph.backend.cache.CachedSchemaTransaction) BaseUnitTest(com.baidu.hugegraph.unit.BaseUnitTest) Test(org.junit.Test)

Aggregations

CachedSchemaTransaction (com.baidu.hugegraph.backend.cache.CachedSchemaTransaction)5 BaseUnitTest (com.baidu.hugegraph.unit.BaseUnitTest)4 FakeObjects (com.baidu.hugegraph.unit.FakeObjects)4 Test (org.junit.Test)4 HugeGraph (com.baidu.hugegraph.HugeGraph)1 Before (org.junit.Before)1