Search in sources :

Example 51 with EntitySeed

use of uk.gov.gchq.gaffer.operation.data.EntitySeed in project Gaffer by gchq.

the class ToArrayHandlerTest method shouldConvertIterableOfElementIdsToArray.

@Test
public void shouldConvertIterableOfElementIdsToArray() throws OperationException {
    // Given
    final ElementId[] originalArray = new ElementId[] { new EntitySeed("vertex"), new EdgeSeed("src", "dest", true) };
    final Iterable<ElementId> originalResults = new WrappedCloseableIterable<>(Arrays.asList(originalArray));
    final ToArrayHandler<ElementId> handler = new ToArrayHandler<>();
    final ToArray operation = mock(ToArray.class);
    given(operation.getInput()).willReturn(originalResults);
    // When
    final ElementId[] results = handler.doOperation(operation, new Context(), null);
    // Then
    assertArrayEquals(originalArray, results);
}
Also used : ToArray(uk.gov.gchq.gaffer.operation.impl.output.ToArray) Context(uk.gov.gchq.gaffer.store.Context) WrappedCloseableIterable(uk.gov.gchq.gaffer.commonutil.iterable.WrappedCloseableIterable) EdgeSeed(uk.gov.gchq.gaffer.operation.data.EdgeSeed) EntitySeed(uk.gov.gchq.gaffer.operation.data.EntitySeed) ElementId(uk.gov.gchq.gaffer.data.element.id.ElementId) Test(org.junit.jupiter.api.Test)

Example 52 with EntitySeed

use of uk.gov.gchq.gaffer.operation.data.EntitySeed in project Gaffer by gchq.

the class ToArrayHandlerTest method shouldConvertIterableOfElementsAndElementIdsToArray.

@Test
public void shouldConvertIterableOfElementsAndElementIdsToArray() throws OperationException {
    // Given
    final ElementId[] originalArray = new ElementId[] { new Entity.Builder().group("entity").build(), new Edge.Builder().group("edge").build(), new EntitySeed("vertex"), new EdgeSeed("src", "dest", true) };
    final Iterable<ElementId> originalResults = new WrappedCloseableIterable<>(Arrays.asList(originalArray));
    final ToArrayHandler<ElementId> handler = new ToArrayHandler<>();
    final ToArray operation = mock(ToArray.class);
    given(operation.getInput()).willReturn(originalResults);
    // When
    final ElementId[] results = handler.doOperation(operation, new Context(), null);
    // Then
    assertArrayEquals(originalArray, results);
}
Also used : ToArray(uk.gov.gchq.gaffer.operation.impl.output.ToArray) Context(uk.gov.gchq.gaffer.store.Context) WrappedCloseableIterable(uk.gov.gchq.gaffer.commonutil.iterable.WrappedCloseableIterable) EdgeSeed(uk.gov.gchq.gaffer.operation.data.EdgeSeed) EntitySeed(uk.gov.gchq.gaffer.operation.data.EntitySeed) ElementId(uk.gov.gchq.gaffer.data.element.id.ElementId) Test(org.junit.jupiter.api.Test)

Example 53 with EntitySeed

use of uk.gov.gchq.gaffer.operation.data.EntitySeed in project Gaffer by gchq.

the class ToArrayHandlerTest method shouldConvertIterableOfObjectsToArray.

@Test
public void shouldConvertIterableOfObjectsToArray() throws OperationException {
    // Given
    final Object[] originalArray = new Object[] { new Entity("entity"), new Edge.Builder().group("edge"), new EntitySeed("vertex"), new EdgeSeed("src", "dest", true), 1, 2, 1.5 };
    final Iterable<Object> originalResults = new WrappedCloseableIterable<>(Arrays.asList(originalArray));
    final ToArrayHandler<Object> handler = new ToArrayHandler<>();
    final ToArray operation = mock(ToArray.class);
    given(operation.getInput()).willReturn(originalResults);
    // When
    final Object[] results = handler.doOperation(operation, new Context(), null);
    // Then
    assertArrayEquals(originalArray, results);
}
Also used : ToArray(uk.gov.gchq.gaffer.operation.impl.output.ToArray) Context(uk.gov.gchq.gaffer.store.Context) Entity(uk.gov.gchq.gaffer.data.element.Entity) WrappedCloseableIterable(uk.gov.gchq.gaffer.commonutil.iterable.WrappedCloseableIterable) EdgeSeed(uk.gov.gchq.gaffer.operation.data.EdgeSeed) EntitySeed(uk.gov.gchq.gaffer.operation.data.EntitySeed) Edge(uk.gov.gchq.gaffer.data.element.Edge) Test(org.junit.jupiter.api.Test)

Example 54 with EntitySeed

use of uk.gov.gchq.gaffer.operation.data.EntitySeed in project Gaffer by gchq.

the class AccumuloIDBetweenSetsRetrieverTest method shouldDealWithFalsePositives.

private void shouldDealWithFalsePositives(final boolean loadIntoMemory, final AccumuloStore store) throws StoreException {
    final Set<EntityId> seeds = new HashSet<>();
    seeds.add(AccumuloTestData.SEED_A0);
    seeds.add(AccumuloTestData.SEED_A23);
    // positive sensible.
    for (int i = 0; i < 10; i++) {
        seeds.add(new EntitySeed("abc" + i));
    }
    // Need to make sure that the Bloom filter we create has the same size and the same number of hashes as the
    // one that GraphElementsWithStatisticsWithinSetRetriever creates.
    final int numItemsToBeAdded = loadIntoMemory ? seeds.size() : 20;
    if (!loadIntoMemory) {
        store.getProperties().setMaxEntriesForBatchScanner("20");
    }
    // Find something that will give a false positive
    // Need to repeat the logic used in the getGraphElementsWithStatisticsWithinSet() method.
    // Calculate sensible size of filter, aiming for false positive rate of 1 in 10000, with a maximum size of
    // maxBloomFilterToPassToAnIterator bytes.
    int size = (int) (-numItemsToBeAdded * Math.log(0.0001) / Math.pow(Math.log(2.0), 2.0));
    size = Math.min(size, store.getProperties().getMaxBloomFilterToPassToAnIterator());
    // Work out optimal number of hashes to use in Bloom filter based on size of set - optimal number of hashes is
    // (m/n)ln 2 where m is the size of the filter in bits and n is the number of items that will be added to the set.
    final int numHashes = Math.max(1, (int) ((size / numItemsToBeAdded) * Math.log(2)));
    // Create Bloom filter and add seeds to it
    final BloomFilter filter = new BloomFilter(size, numHashes, Hash.MURMUR_HASH);
    for (final EntityId seed : seeds) {
        filter.add(new Key(store.getKeyPackage().getKeyConverter().serialiseVertex(seed.getVertex())));
    }
    // Test random items against it - should only have to shouldRetrieveElementsInRangeBetweenSeeds MAX_SIZE_BLOOM_FILTER / 2 on average before find a
    // false positive (but impose an arbitrary limit to avoid an infinite loop if there's a problem).
    int count = 0;
    int maxNumberOfTries = 50 * store.getProperties().getMaxBloomFilterToPassToAnIterator();
    while (count < maxNumberOfTries) {
        count++;
        if (filter.membershipTest(new Key(("" + count).getBytes()))) {
            break;
        }
    }
    if (count == maxNumberOfTries) {
        fail("Didn't find a false positive");
    }
    // False positive is "" + count so create an edge from seeds to that
    final Edge edge = new Edge.Builder().group(TestGroups.EDGE).source("A0").dest("" + count).directed(true).build();
    edge.putProperty(AccumuloPropertyNames.COUNT, 1000000);
    Set<Element> data = new HashSet<>();
    data.add(edge);
    final User user = new User();
    addElements(data, store, user);
    // Now query for all edges in set - shouldn't get the false positive
    GetElementsBetweenSets op = new GetElementsBetweenSets.Builder().input(AccumuloTestData.SEED_A0_SET).inputB(seeds).view(defaultView).build();
    final Set<Element> results = returnElementsFromOperation(store, op, new User(), loadIntoMemory);
    // Check results are as expected
    assertThat(results).hasSize(2).contains(AccumuloTestData.EDGE_A0_A23, AccumuloTestData.A0_ENTITY);
}
Also used : User(uk.gov.gchq.gaffer.user.User) GetElementsBetweenSets(uk.gov.gchq.gaffer.accumulostore.operation.impl.GetElementsBetweenSets) Element(uk.gov.gchq.gaffer.data.element.Element) BloomFilter(org.apache.hadoop.util.bloom.BloomFilter) EntityId(uk.gov.gchq.gaffer.data.element.id.EntityId) EntitySeed(uk.gov.gchq.gaffer.operation.data.EntitySeed) Edge(uk.gov.gchq.gaffer.data.element.Edge) Key(org.apache.hadoop.util.bloom.Key) HashSet(java.util.HashSet)

Example 55 with EntitySeed

use of uk.gov.gchq.gaffer.operation.data.EntitySeed in project Gaffer by gchq.

the class AccumuloRangeIDRetrieverTest method shouldRetrieveElementsInRangeBetweenSeeds.

private void shouldRetrieveElementsInRangeBetweenSeeds(final AccumuloStore store) throws Exception {
    // Create set to query for
    final Set<Pair<ElementId, ElementId>> simpleEntityRanges = new HashSet<>();
    simpleEntityRanges.add(new Pair<>(new EntitySeed("0000"), new EntitySeed("0999")));
    // Retrieve elements when less simple entities are provided than the max number of entries for the batch scanner
    final GetElementsInRanges operation = new GetElementsInRanges.Builder().view(defaultView).input(simpleEntityRanges).build();
    final AccumuloRangeIDRetriever<?> retriever = new AccumuloRangeIDRetriever<>(store, operation, new User());
    final List<Element> elements = Lists.newArrayList(retriever);
    for (final Element element : elements) {
        if (element instanceof Edge) {
            assertEquals(EdgeId.MatchedVertex.SOURCE, ((Edge) element).getMatchedVertex());
        }
    }
    assertEquals(NUM_ENTRIES, elements.size());
}
Also used : User(uk.gov.gchq.gaffer.user.User) GetElementsInRanges(uk.gov.gchq.gaffer.accumulostore.operation.impl.GetElementsInRanges) Element(uk.gov.gchq.gaffer.data.element.Element) EntitySeed(uk.gov.gchq.gaffer.operation.data.EntitySeed) Edge(uk.gov.gchq.gaffer.data.element.Edge) Pair(uk.gov.gchq.gaffer.commonutil.pair.Pair) HashSet(java.util.HashSet)

Aggregations

EntitySeed (uk.gov.gchq.gaffer.operation.data.EntitySeed)284 Test (org.junit.jupiter.api.Test)122 GetElements (uk.gov.gchq.gaffer.operation.impl.get.GetElements)122 Element (uk.gov.gchq.gaffer.data.element.Element)102 User (uk.gov.gchq.gaffer.user.User)92 AddElements (uk.gov.gchq.gaffer.operation.impl.add.AddElements)90 View (uk.gov.gchq.gaffer.data.elementdefinition.view.View)87 HashSet (java.util.HashSet)71 Graph (uk.gov.gchq.gaffer.graph.Graph)69 Entity (uk.gov.gchq.gaffer.data.element.Entity)65 Edge (uk.gov.gchq.gaffer.data.element.Edge)61 Test (org.junit.Test)58 ArrayList (java.util.ArrayList)55 ViewElementDefinition (uk.gov.gchq.gaffer.data.elementdefinition.view.ViewElementDefinition)46 EntityId (uk.gov.gchq.gaffer.data.element.id.EntityId)41 CloseableIterable (uk.gov.gchq.gaffer.commonutil.iterable.CloseableIterable)40 OperationChain (uk.gov.gchq.gaffer.operation.OperationChain)38 ElementFilter (uk.gov.gchq.gaffer.data.element.function.ElementFilter)36 EdgeSeed (uk.gov.gchq.gaffer.operation.data.EdgeSeed)36 OperationTest (uk.gov.gchq.gaffer.operation.OperationTest)35