Search in sources :

Example 31 with EntityId

use of uk.gov.gchq.gaffer.data.element.id.EntityId in project Gaffer by gchq.

the class FilterToOperationConverterTest method testSpecifySourceOrDestinationAndPropertyFilter.

@Test
public void testSpecifySourceOrDestinationAndPropertyFilter() {
    final Schema schema = getSchema();
    final SparkSession sparkSession = SparkSessionProvider.getSparkSession();
    // Specify src and a filter on property1
    Filter[] filters = new Filter[2];
    filters[0] = new GreaterThan("property1", 5);
    filters[1] = new EqualTo(SchemaToStructTypeConverter.SRC_COL_NAME, "0");
    FiltersToOperationConverter converter = new FiltersToOperationConverter(getViewFromSchema(schema), schema, filters);
    Operation operation = converter.getOperation();
    assertTrue(operation instanceof GetRDDOfElements);
    assertEquals(0, ((GraphFilters) operation).getView().getEntityGroups().size());
    assertEquals(2, ((GraphFilters) operation).getView().getEdgeGroups().size());
    final Set<EntityId> seeds = new HashSet<>();
    for (final Object seed : ((GetRDDOfElements) operation).getInput()) {
        seeds.add((EntitySeed) seed);
    }
    assertEquals(Collections.singleton(new EntitySeed("0")), seeds);
    View opView = ((GraphFilters) operation).getView();
    for (final String edgeGroup : EDGE_GROUPS) {
        final List<TupleAdaptedPredicate<String, ?>> edgePostAggFilters = opView.getEdge(edgeGroup).getPostAggregationFilterFunctions();
        assertThat(edgePostAggFilters).hasSize(1);
        assertArrayEquals(new String[] { "property1" }, edgePostAggFilters.get(0).getSelection());
        assertEquals(new IsMoreThan(5, false), edgePostAggFilters.get(0).getPredicate());
    }
    // Specify src and filters on property1 and property4
    filters = new Filter[3];
    filters[0] = new GreaterThan("property1", 5);
    filters[1] = new EqualTo(SchemaToStructTypeConverter.SRC_COL_NAME, "0");
    filters[2] = new LessThan("property4", 8);
    converter = new FiltersToOperationConverter(getViewFromSchema(schema), schema, filters);
    operation = converter.getOperation();
    assertTrue(operation instanceof GetRDDOfElements);
    assertEquals(0, ((GraphFilters) operation).getView().getEntityGroups().size());
    assertEquals(1, ((GraphFilters) operation).getView().getEdgeGroups().size());
    seeds.clear();
    for (final Object seed : ((GetRDDOfElements) operation).getInput()) {
        seeds.add((EntitySeed) seed);
    }
    assertEquals(Collections.singleton(new EntitySeed("0")), seeds);
    opView = ((GraphFilters) operation).getView();
    final List<TupleAdaptedPredicate<String, ?>> entityPostAggFilters = opView.getEdge(EDGE_GROUP).getPostAggregationFilterFunctions();
    assertThat(entityPostAggFilters).hasSize(2);
    final List<String> expectedProperties = new ArrayList<>();
    expectedProperties.add("property1");
    expectedProperties.add("property4");
    assertThat(entityPostAggFilters.get(0).getSelection()).hasSize(1);
    assertEquals(expectedProperties.get(0), entityPostAggFilters.get(0).getSelection()[0]);
    assertEquals(new IsMoreThan(5, false), entityPostAggFilters.get(0).getPredicate());
    assertThat(entityPostAggFilters.get(1).getSelection()).hasSize(1);
    assertEquals(expectedProperties.get(1), entityPostAggFilters.get(1).getSelection()[0]);
    assertEquals(new IsLessThan(8, false), entityPostAggFilters.get(1).getPredicate());
}
Also used : TupleAdaptedPredicate(uk.gov.gchq.koryphe.tuple.predicate.TupleAdaptedPredicate) SparkSession(org.apache.spark.sql.SparkSession) Schema(uk.gov.gchq.gaffer.store.schema.Schema) ArrayList(java.util.ArrayList) GraphFilters(uk.gov.gchq.gaffer.operation.graph.GraphFilters) Operation(uk.gov.gchq.gaffer.operation.Operation) View(uk.gov.gchq.gaffer.data.elementdefinition.view.View) EqualTo(org.apache.spark.sql.sources.EqualTo) EntityId(uk.gov.gchq.gaffer.data.element.id.EntityId) IsLessThan(uk.gov.gchq.koryphe.impl.predicate.IsLessThan) LessThan(org.apache.spark.sql.sources.LessThan) IsLessThan(uk.gov.gchq.koryphe.impl.predicate.IsLessThan) Filter(org.apache.spark.sql.sources.Filter) GreaterThan(org.apache.spark.sql.sources.GreaterThan) EntitySeed(uk.gov.gchq.gaffer.operation.data.EntitySeed) IsMoreThan(uk.gov.gchq.koryphe.impl.predicate.IsMoreThan) GetRDDOfElements(uk.gov.gchq.gaffer.spark.operation.scalardd.GetRDDOfElements) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 32 with EntityId

use of uk.gov.gchq.gaffer.data.element.id.EntityId in project Gaffer by gchq.

the class AccumuloStoreTest method testAbleToInsertAndRetrieveEntityQueryingEqualAndRelated.

private void testAbleToInsertAndRetrieveEntityQueryingEqualAndRelated(final AccumuloStore store) throws OperationException {
    final Entity e = new Entity(TestGroups.ENTITY, "1");
    e.putProperty(TestPropertyNames.PROP_1, 1);
    e.putProperty(TestPropertyNames.PROP_2, 2);
    e.putProperty(TestPropertyNames.PROP_3, 3);
    e.putProperty(TestPropertyNames.PROP_4, 4);
    e.putProperty(TestPropertyNames.COUNT, 1);
    final User user = new User();
    final AddElements add = new AddElements.Builder().input(e).build();
    store.execute(add, new Context(user));
    final EntityId entityId1 = new EntitySeed("1");
    final GetElements getBySeed = new GetElements.Builder().view(new View.Builder().entity(TestGroups.ENTITY).build()).input(entityId1).build();
    CloseableIterable<? extends Element> results = null;
    try {
        results = store.execute(getBySeed, new Context(user));
        assertEquals(1, Iterables.size(results));
        assertTrue(Iterables.contains(results, e));
    } finally {
        if (results != null) {
            results.close();
        }
    }
    final GetElements getRelated = new GetElements.Builder().view(new View.Builder().entity(TestGroups.ENTITY).build()).input(entityId1).build();
    CloseableIterable<? extends Element> relatedResults = null;
    try {
        relatedResults = store.execute(getRelated, store.createContext(user));
        assertEquals(1, Iterables.size(relatedResults));
        assertTrue(Iterables.contains(relatedResults, e));
        final GetElements getRelatedWithPostAggregationFilter = new GetElements.Builder().view(new View.Builder().entity(TestGroups.ENTITY, new ViewElementDefinition.Builder().preAggregationFilter(new ElementFilter.Builder().select(TestPropertyNames.PROP_1).execute(new IsMoreThan(0)).build()).postAggregationFilter(new ElementFilter.Builder().select(TestPropertyNames.COUNT).execute(new IsMoreThan(6)).build()).build()).build()).input(entityId1).build();
        relatedResults = store.execute(getRelatedWithPostAggregationFilter, store.createContext(user));
        assertEquals(0, Iterables.size(relatedResults));
    } finally {
        if (relatedResults != null) {
            relatedResults.close();
        }
    }
}
Also used : AddElements(uk.gov.gchq.gaffer.operation.impl.add.AddElements) Context(uk.gov.gchq.gaffer.store.Context) Entity(uk.gov.gchq.gaffer.data.element.Entity) User(uk.gov.gchq.gaffer.user.User) GetElements(uk.gov.gchq.gaffer.operation.impl.get.GetElements) View(uk.gov.gchq.gaffer.data.elementdefinition.view.View) EntityId(uk.gov.gchq.gaffer.data.element.id.EntityId) ElementFilter(uk.gov.gchq.gaffer.data.element.function.ElementFilter) EntitySeed(uk.gov.gchq.gaffer.operation.data.EntitySeed) IsMoreThan(uk.gov.gchq.koryphe.impl.predicate.IsMoreThan)

Example 33 with EntityId

use of uk.gov.gchq.gaffer.data.element.id.EntityId in project Gaffer by gchq.

the class BloomFilterIT method calculateRandomLookUpRate.

private double calculateRandomLookUpRate(final FileSKVIterator reader, final HashSet<Entity> dataSet, final Random random, final RangeFactory rangeFactory) throws IOException, RangeFactoryException {
    final EntityId[] randomData = new EntitySeed[5000];
    for (int i = 0; i < 5000; i++) {
        randomData[i] = new EntitySeed("type" + random.nextInt(Integer.MAX_VALUE));
    }
    final long start = System.currentTimeMillis();
    for (int i = 0; i < 5000; i++) {
        seek(reader, randomData[i], rangeFactory);
        if (dataSet.contains(randomData[i])) {
            assertThat(reader.hasTop()).isTrue();
        }
    }
    final long end = System.currentTimeMillis();
    final double randomRate = 5000 / ((end - start) / 1000.0);
    LOGGER.info("Random look up rate = {}", randomRate);
    return randomRate;
}
Also used : EntityId(uk.gov.gchq.gaffer.data.element.id.EntityId) EntitySeed(uk.gov.gchq.gaffer.operation.data.EntitySeed)

Example 34 with EntityId

use of uk.gov.gchq.gaffer.data.element.id.EntityId in project Gaffer by gchq.

the class QueryGenerator method seedToPredicate.

private FilterPredicate seedToPredicate(final ParquetElementSeed seed, final SeededGraphFilters.IncludeIncomingOutgoingType includeIncomingOutgoingType, final SeedMatching.SeedMatchingType seedMatchingType, final String group, final boolean reversed) {
    final boolean isEntityGroup = schemaUtils.getEntityGroups().contains(group);
    FilterPredicate filter = null;
    final ElementId elementId = seed.getElementId();
    // Is it an entity group?
    if (isEntityGroup) {
        // EntityId case
        if (elementId instanceof EntityId) {
            filter = getIsEqualFilter(ParquetStore.VERTEX, ((ParquetEntitySeed) seed).getSeed(), group);
        } else {
            // EdgeId case
            // Does the seed type need to match the group type?
            final ParquetEdgeSeed edgeSeed = (ParquetEdgeSeed) seed;
            if (seedMatchingType != SeedMatching.SeedMatchingType.EQUAL) {
                // Vertex = source of edge seed or Vertex = destination of edge seed
                // look in partition 0 with filter src = A and partition 1 with filter src = B
                filter = getIsEqualFilter(ParquetStore.VERTEX, edgeSeed.getSource(), group);
                if (null != ((ParquetEdgeSeed) seed).getDestination()) {
                    filter = FilterPredicateUtils.or(filter, getIsEqualFilter(ParquetStore.VERTEX, edgeSeed.getDestination(), group));
                }
            }
        }
    } else {
        // EntityId case
        if (elementId instanceof EntityId) {
            // If seedMatchingType is EQUAL then we can't find anything in an edge group
            if (seedMatchingType != SeedMatching.SeedMatchingType.EQUAL) {
                if (includeIncomingOutgoingType == SeededGraphFilters.IncludeIncomingOutgoingType.INCOMING) {
                    if (reversed) {
                        // Dst is seed
                        filter = getIsEqualFilter(ParquetStore.DESTINATION, ((ParquetEntitySeed) seed).getSeed(), group);
                    } else {
                        // Src is seed and edge is undirected
                        filter = getIsEqualFilter(ParquetStore.SOURCE, ((ParquetEntitySeed) seed).getSeed(), group);
                        filter = FilterPredicateUtils.and(filter, getIsEqualFilter(ParquetStore.DIRECTED, new Object[] { false }, group));
                    }
                } else if (includeIncomingOutgoingType == SeededGraphFilters.IncludeIncomingOutgoingType.OUTGOING) {
                    if (reversed) {
                        // Dst is seed and edge is undirected
                        filter = getIsEqualFilter(ParquetStore.DESTINATION, ((ParquetEntitySeed) seed).getSeed(), group);
                        filter = FilterPredicateUtils.and(filter, getIsEqualFilter(ParquetStore.DIRECTED, new Object[] { false }, group));
                    } else {
                        // Src is seed
                        filter = getIsEqualFilter(ParquetStore.SOURCE, ((ParquetEntitySeed) seed).getSeed(), group);
                    }
                } else {
                    if (reversed) {
                        // Dst is seed
                        filter = getIsEqualFilter(ParquetStore.DESTINATION, ((ParquetEntitySeed) seed).getSeed(), group);
                    } else {
                        // Src is seed
                        filter = getIsEqualFilter(ParquetStore.SOURCE, ((ParquetEntitySeed) seed).getSeed(), group);
                    }
                }
            }
        } else {
            // EdgeId case
            final ParquetEdgeSeed edgeSeed = (ParquetEdgeSeed) seed;
            if (!reversed) {
                // Src is source of edge seed and destination is destination of edge seed
                filter = getIsEqualFilter(ParquetStore.SOURCE, edgeSeed.getSource(), group);
                // WRONG seed is already serialised source and dest - now fixed?
                filter = FilterPredicateUtils.and(filter, getIsEqualFilter(ParquetStore.DESTINATION, edgeSeed.getDestination(), group));
                final DirectedType directedType = edgeSeed.getDirectedType();
                if (directedType == DirectedType.DIRECTED) {
                    filter = FilterPredicateUtils.and(filter, getIsEqualFilter(ParquetStore.DIRECTED, new Object[] { true }, group));
                } else if (directedType == DirectedType.UNDIRECTED) {
                    filter = FilterPredicateUtils.and(filter, getIsEqualFilter(ParquetStore.DIRECTED, new Object[] { false }, group));
                }
            } else {
                // TODO Optimise this - there are times this is unnecessary
                filter = getIsEqualFilter(ParquetStore.DESTINATION, edgeSeed.getSource(), group);
                filter = FilterPredicateUtils.and(filter, getIsEqualFilter(ParquetStore.SOURCE, edgeSeed.getDestination(), group));
                final DirectedType directedType = edgeSeed.getDirectedType();
                if (directedType == DirectedType.DIRECTED) {
                    filter = FilterPredicateUtils.and(filter, getIsEqualFilter(ParquetStore.DIRECTED, new Object[] { true }, group));
                } else if (directedType == DirectedType.UNDIRECTED) {
                    filter = FilterPredicateUtils.and(filter, getIsEqualFilter(ParquetStore.DIRECTED, new Object[] { false }, group));
                }
            }
        }
    }
    LOGGER.debug("Returning {} from seedToPredicate", filter);
    return filter;
}
Also used : EntityId(uk.gov.gchq.gaffer.data.element.id.EntityId) DirectedType(uk.gov.gchq.gaffer.data.element.id.DirectedType) FilterPredicate(org.apache.parquet.filter2.predicate.FilterPredicate) ElementId(uk.gov.gchq.gaffer.data.element.id.ElementId)

Example 35 with EntityId

use of uk.gov.gchq.gaffer.data.element.id.EntityId in project Gaffer by gchq.

the class QueryGenerator method seedToParquetObject.

private ParquetElementSeed seedToParquetObject(final ElementId seed, final String group, final boolean isEntityGroup) throws SerialisationException {
    final GafferGroupObjectConverter converter = schemaUtils.getConverter(group);
    final String column;
    if (isEntityGroup) {
        column = ParquetStore.VERTEX;
    } else {
        column = ParquetStore.SOURCE;
    }
    if (seed instanceof EntityId) {
        return new ParquetEntitySeed(seed, converter.gafferObjectToParquetObjects(column, ((EntityId) seed).getVertex()));
    } else {
        return converter.edgeIdToParquetObjects((EdgeId) seed);
    }
}
Also used : EntityId(uk.gov.gchq.gaffer.data.element.id.EntityId) GafferGroupObjectConverter(uk.gov.gchq.gaffer.parquetstore.utils.GafferGroupObjectConverter)

Aggregations

EntityId (uk.gov.gchq.gaffer.data.element.id.EntityId)93 Test (org.junit.jupiter.api.Test)60 EntitySeed (uk.gov.gchq.gaffer.operation.data.EntitySeed)41 HashSet (java.util.HashSet)22 User (uk.gov.gchq.gaffer.user.User)21 JSONSerialisationTest (uk.gov.gchq.gaffer.JSONSerialisationTest)15 Entity (uk.gov.gchq.gaffer.data.element.Entity)15 EdgeId (uk.gov.gchq.gaffer.data.element.id.EdgeId)15 View (uk.gov.gchq.gaffer.data.elementdefinition.view.View)15 Edge (uk.gov.gchq.gaffer.data.element.Edge)14 Element (uk.gov.gchq.gaffer.data.element.Element)14 ElementId (uk.gov.gchq.gaffer.data.element.id.ElementId)14 CloseableIterable (uk.gov.gchq.gaffer.commonutil.iterable.CloseableIterable)13 GetAdjacentIds (uk.gov.gchq.gaffer.operation.impl.get.GetAdjacentIds)13 GetElements (uk.gov.gchq.gaffer.operation.impl.get.GetElements)12 AddElements (uk.gov.gchq.gaffer.operation.impl.add.AddElements)10 ArrayList (java.util.ArrayList)8 ViewElementDefinition (uk.gov.gchq.gaffer.data.elementdefinition.view.ViewElementDefinition)7 Graph (uk.gov.gchq.gaffer.graph.Graph)7 Context (uk.gov.gchq.gaffer.store.Context)7