Search in sources :

Example 11 with EmbeddedGraknTx

use of ai.grakn.kb.internal.EmbeddedGraknTx in project grakn by graknlabs.

the class CountPostProcessorTest method setupMocks.

@Before
public void setupMocks() {
    countStorage = mock(RedisCountStorage.class);
    when(countStorage.getCount(any())).thenReturn(1L);
    configMock = mock(GraknConfig.class);
    when(configMock.getProperty(GraknConfigKey.SHARDING_THRESHOLD)).thenReturn(5L);
    GraknKeyspaceStore graknKeyspaceStoreMock = mock(GraknKeyspaceStore.class);
    when(graknKeyspaceStoreMock.containsKeyspace(any())).thenReturn(true);
    EmbeddedGraknTx txMock = mock(EmbeddedGraknTx.class);
    when(txMock.admin()).thenReturn(mock(GraknAdmin.class));
    factoryMock = mock(EngineGraknTxFactory.class);
    when(factoryMock.keyspaceStore()).thenReturn(graknKeyspaceStoreMock);
    when(factoryMock.tx(any(Keyspace.class), any())).thenReturn(txMock);
    lockProviderMock = mock(LockProvider.class);
    when(lockProviderMock.getLock(any())).thenReturn(new ReentrantLock());
    metricRegistry = new MetricRegistry();
    countPostProcessor = CountPostProcessor.create(configMock, factoryMock, lockProviderMock, metricRegistry, countStorage);
}
Also used : ReentrantLock(java.util.concurrent.locks.ReentrantLock) GraknConfig(ai.grakn.engine.GraknConfig) RedisCountStorage(ai.grakn.engine.task.postprocessing.redisstorage.RedisCountStorage) Keyspace(ai.grakn.Keyspace) MetricRegistry(com.codahale.metrics.MetricRegistry) EngineGraknTxFactory(ai.grakn.engine.factory.EngineGraknTxFactory) GraknAdmin(ai.grakn.kb.admin.GraknAdmin) LockProvider(ai.grakn.engine.lock.LockProvider) EmbeddedGraknTx(ai.grakn.kb.internal.EmbeddedGraknTx) GraknKeyspaceStore(ai.grakn.engine.GraknKeyspaceStore) Before(org.junit.Before)

Example 12 with EmbeddedGraknTx

use of ai.grakn.kb.internal.EmbeddedGraknTx in project grakn by graknlabs.

the class PostProcessingTest method whenCreatingDuplicateResources_EnsureTheyAreMergedInPost.

@Test
public void whenCreatingDuplicateResources_EnsureTheyAreMergedInPost() throws InvalidKBException, InterruptedException, JsonProcessingException {
    String value = "1";
    String sample = "Sample";
    // Create GraknTx With Duplicate Resources
    EmbeddedGraknTx<?> tx = session.open(GraknTxType.WRITE);
    AttributeType<String> attributeType = tx.putAttributeType(sample, AttributeType.DataType.STRING);
    Attribute<String> attribute = attributeType.putAttribute(value);
    tx.commitSubmitNoLogs();
    tx = session.open(GraknTxType.WRITE);
    assertEquals(1, attributeType.instances().count());
    // Check duplicates have been created
    Set<Vertex> resource1 = createDuplicateResource(tx, attributeType, attribute);
    Set<Vertex> resource2 = createDuplicateResource(tx, attributeType, attribute);
    Set<Vertex> resource3 = createDuplicateResource(tx, attributeType, attribute);
    Set<Vertex> resource4 = createDuplicateResource(tx, attributeType, attribute);
    assertEquals(5, attributeType.instances().count());
    // Attribute vertex index
    String resourceIndex = resource1.iterator().next().value(INDEX.name()).toString();
    // Merge the attribute sets
    Set<Vertex> merged = Sets.newHashSet();
    merged.addAll(resource1);
    merged.addAll(resource2);
    merged.addAll(resource3);
    merged.addAll(resource4);
    tx.close();
    // Casting sets as ConceptIds
    Set<ConceptId> resourceConcepts = merged.stream().map(c -> ConceptId.of(Schema.PREFIX_VERTEX + c.id().toString())).collect(toSet());
    // Create Commit Log
    CommitLog commitLog = CommitLog.createDefault(tx.keyspace());
    commitLog.attributes().put(resourceIndex, resourceConcepts);
    // Submit it
    postProcessor.submit(commitLog);
    // Force running the PP job
    engine.server().backgroundTaskRunner().tasks().forEach(BackgroundTask::run);
    Thread.sleep(2000);
    tx = session.open(GraknTxType.READ);
    // Check it's fixed
    assertEquals(1, tx.getAttributeType(sample).instances().count());
    tx.close();
}
Also used : InvalidKBException(ai.grakn.exception.InvalidKBException) BeforeClass(org.junit.BeforeClass) GraknTestUtil(ai.grakn.util.GraknTestUtil) CountPostProcessor(ai.grakn.engine.task.postprocessing.CountPostProcessor) SampleKBLoader(ai.grakn.util.SampleKBLoader) Keyspace(ai.grakn.Keyspace) Concept(ai.grakn.concept.Concept) PostProcessingTestUtils.createDuplicateResource(ai.grakn.test.engine.postprocessing.PostProcessingTestUtils.createDuplicateResource) EntityType(ai.grakn.concept.EntityType) GraknConfig(ai.grakn.engine.GraknConfig) Attribute(ai.grakn.concept.Attribute) AttributeType(ai.grakn.concept.AttributeType) After(org.junit.After) ConceptId(ai.grakn.concept.ConceptId) ClassRule(org.junit.ClassRule) RedisCountStorage(ai.grakn.engine.task.postprocessing.redisstorage.RedisCountStorage) INDEX(ai.grakn.util.Schema.VertexProperty.INDEX) Collectors.toSet(java.util.stream.Collectors.toSet) EngineContext(ai.grakn.test.rule.EngineContext) Before(org.junit.Before) GraknTxType(ai.grakn.GraknTxType) MetricRegistry(com.codahale.metrics.MetricRegistry) Set(java.util.Set) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) Test(org.junit.Test) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) Sets(com.google.common.collect.Sets) PostProcessor(ai.grakn.engine.task.postprocessing.PostProcessor) CommitLog(ai.grakn.kb.log.CommitLog) GraknConfigKey(ai.grakn.GraknConfigKey) IndexPostProcessor(ai.grakn.engine.task.postprocessing.IndexPostProcessor) BackgroundTask(ai.grakn.engine.task.BackgroundTask) EmbeddedGraknSession(ai.grakn.factory.EmbeddedGraknSession) EmbeddedGraknTx(ai.grakn.kb.internal.EmbeddedGraknTx) Assume.assumeTrue(org.junit.Assume.assumeTrue) RedisIndexStorage(ai.grakn.engine.task.postprocessing.redisstorage.RedisIndexStorage) Schema(ai.grakn.util.Schema) Assert.assertEquals(org.junit.Assert.assertEquals) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) CommitLog(ai.grakn.kb.log.CommitLog) ConceptId(ai.grakn.concept.ConceptId) BackgroundTask(ai.grakn.engine.task.BackgroundTask) Test(org.junit.Test)

Aggregations

EmbeddedGraknTx (ai.grakn.kb.internal.EmbeddedGraknTx)12 Set (java.util.Set)7 Answer (ai.grakn.graql.admin.Answer)6 Conjunction (ai.grakn.graql.admin.Conjunction)6 Schema (ai.grakn.util.Schema)6 Sets (com.google.common.collect.Sets)6 Test (org.junit.Test)6 Concept (ai.grakn.concept.Concept)5 ConceptId (ai.grakn.concept.ConceptId)5 Var (ai.grakn.graql.Var)5 VarPatternAdmin (ai.grakn.graql.admin.VarPatternAdmin)5 ReasonerQueries (ai.grakn.graql.internal.reasoner.query.ReasonerQueries)5 List (java.util.List)5 Collectors (java.util.stream.Collectors)5 Collectors.toSet (java.util.stream.Collectors.toSet)5 Assert.assertEquals (org.junit.Assert.assertEquals)5 Unifier (ai.grakn.graql.admin.Unifier)4 Patterns (ai.grakn.graql.internal.pattern.Patterns)4 GraknTestUtil (ai.grakn.util.GraknTestUtil)4 Iterables (com.google.common.collect.Iterables)4