Search in sources :

Example 1 with ViewElementDefinition

use of uk.gov.gchq.gaffer.data.elementdefinition.view.ViewElementDefinition in project Gaffer by gchq.

the class ViewIT method shouldDeserialiseJsonView.

@Test
public void shouldDeserialiseJsonView() throws IOException {
    // Given
    // When
    View view = loadView();
    // Then
    final ViewElementDefinition edge = view.getEdge(TestGroups.EDGE);
    final ElementTransformer transformer = edge.getTransformer();
    assertNotNull(transformer);
    final List<ConsumerProducerFunctionContext<String, TransformFunction>> contexts = transformer.getFunctions();
    assertEquals(1, contexts.size());
    final List<String> selection = contexts.get(0).getSelection();
    assertEquals(2, selection.size());
    assertEquals(TestPropertyNames.PROP_1, selection.get(0));
    assertEquals(IdentifierType.SOURCE.name(), selection.get(1));
    final List<String> projection = contexts.get(0).getProjection();
    assertEquals(1, projection.size());
    assertEquals(TestPropertyNames.TRANSIENT_1, projection.get(0));
    assertTrue(contexts.get(0).getFunction() instanceof ExampleTransformFunction);
    final ElementFilter postFilter = edge.getPostTransformFilter();
    assertNotNull(postFilter);
    final List<ConsumerFunctionContext<String, FilterFunction>> filterContexts = postFilter.getFunctions();
    assertEquals(1, contexts.size());
    final List<String> postFilterSelection = filterContexts.get(0).getSelection();
    assertEquals(1, postFilterSelection.size());
    assertEquals(TestPropertyNames.TRANSIENT_1, postFilterSelection.get(0));
    assertTrue(filterContexts.get(0).getFunction() instanceof ExampleFilterFunction);
}
Also used : ConsumerProducerFunctionContext(uk.gov.gchq.gaffer.function.context.ConsumerProducerFunctionContext) ConsumerFunctionContext(uk.gov.gchq.gaffer.function.context.ConsumerFunctionContext) ElementTransformer(uk.gov.gchq.gaffer.data.element.function.ElementTransformer) ElementFilter(uk.gov.gchq.gaffer.data.element.function.ElementFilter) ViewElementDefinition(uk.gov.gchq.gaffer.data.elementdefinition.view.ViewElementDefinition) ExampleFilterFunction(uk.gov.gchq.gaffer.function.ExampleFilterFunction) View(uk.gov.gchq.gaffer.data.elementdefinition.view.View) ExampleTransformFunction(uk.gov.gchq.gaffer.function.ExampleTransformFunction) Test(org.junit.Test)

Example 2 with ViewElementDefinition

use of uk.gov.gchq.gaffer.data.elementdefinition.view.ViewElementDefinition in project Gaffer by gchq.

the class FiltersToOperationConverter method applyPropertyFilters.

private AbstractGetRDD<?> applyPropertyFilters(final View derivedView, final AbstractGetRDD<?> operation) {
    final List<Set<String>> groupsRelatedToFilters = new ArrayList<>();
    for (final Filter filter : filters) {
        final Set<String> groupsRelatedToFilter = getGroupsFromFilter(filter);
        if (groupsRelatedToFilter != null && !groupsRelatedToFilter.isEmpty()) {
            groupsRelatedToFilters.add(groupsRelatedToFilter);
        }
        LOGGER.info("Groups {} are related to filter {}", StringUtils.join(groupsRelatedToFilter, ','), filter);
    }
    LOGGER.info("Groups related to filters are: {}", StringUtils.join(groupsRelatedToFilters, ','));
    // Take the intersection of this list of groups - only these groups can be related to the query
    final Set<String> intersection = new HashSet<>(derivedView.getEntityGroups());
    intersection.addAll(derivedView.getEdgeGroups());
    for (final Set<String> groupsRelatedToFilter : groupsRelatedToFilters) {
        intersection.retainAll(groupsRelatedToFilter);
    }
    LOGGER.info("Groups that can be returned are: {}", StringUtils.join(intersection, ','));
    // Update view with filters and add to operation
    final Map<String, List<ConsumerFunctionContext<String, FilterFunction>>> groupToFunctions = new HashMap<>();
    for (final Filter filter : filters) {
        final Map<String, List<ConsumerFunctionContext<String, FilterFunction>>> map = getFunctionsFromFilter(filter);
        for (final Entry<String, List<ConsumerFunctionContext<String, FilterFunction>>> entry : map.entrySet()) {
            if (!groupToFunctions.containsKey(entry.getKey())) {
                groupToFunctions.put(entry.getKey(), new ArrayList<ConsumerFunctionContext<String, FilterFunction>>());
            }
            groupToFunctions.get(entry.getKey()).addAll(entry.getValue());
        }
    }
    LOGGER.info("The following functions will be applied for the given group:");
    for (final Entry<String, List<ConsumerFunctionContext<String, FilterFunction>>> entry : groupToFunctions.entrySet()) {
        LOGGER.info("Group = {}: ", entry.getKey());
        for (final ConsumerFunctionContext<String, FilterFunction> cfc : entry.getValue()) {
            LOGGER.info("\t{} {}", StringUtils.join(cfc.getSelection(), ','), cfc.getFunction());
        }
    }
    boolean updated = false;
    View.Builder builder = new View.Builder();
    for (final String group : derivedView.getEntityGroups()) {
        if (intersection.contains(group)) {
            if (groupToFunctions.get(group) != null) {
                final ViewElementDefinition ved = new ViewElementDefinition.Builder().merge(derivedView.getEntity(group)).postAggregationFilterFunctions(groupToFunctions.get(group)).build();
                LOGGER.info("Adding the following filter functions to the view for group {}:", group);
                for (final ConsumerFunctionContext<String, FilterFunction> cfc : groupToFunctions.get(group)) {
                    LOGGER.info("\t{} {}", StringUtils.join(cfc.getSelection(), ','), cfc.getFunction());
                }
                builder = builder.entity(group, ved);
                updated = true;
            } else {
                LOGGER.info("Not adding any filter functions to the view for group {}", group);
            }
        }
    }
    for (final String group : derivedView.getEdgeGroups()) {
        if (intersection.contains(group)) {
            if (groupToFunctions.get(group) != null) {
                final ViewElementDefinition ved = new ViewElementDefinition.Builder().merge(derivedView.getEdge(group)).postAggregationFilterFunctions(groupToFunctions.get(group)).build();
                LOGGER.info("Adding the following filter functions to the view for group {}:", group);
                for (final ConsumerFunctionContext<String, FilterFunction> cfc : groupToFunctions.get(group)) {
                    LOGGER.info("\t{} {}", StringUtils.join(cfc.getSelection(), ','), cfc.getFunction());
                }
                builder = builder.edge(group, ved);
                updated = true;
            } else {
                LOGGER.info("Not adding any filter functions to the view for group {}", group);
            }
        }
    }
    if (updated) {
        operation.setView(builder.build());
    } else {
        operation.setView(derivedView);
    }
    return operation;
}
Also used : FilterFunction(uk.gov.gchq.gaffer.function.FilterFunction) HashSet(java.util.HashSet) Set(java.util.Set) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ViewElementDefinition(uk.gov.gchq.gaffer.data.elementdefinition.view.ViewElementDefinition) View(uk.gov.gchq.gaffer.data.elementdefinition.view.View) ConsumerFunctionContext(uk.gov.gchq.gaffer.function.context.ConsumerFunctionContext) Filter(org.apache.spark.sql.sources.Filter) ArrayList(java.util.ArrayList) List(java.util.List) HashSet(java.util.HashSet)

Example 3 with ViewElementDefinition

use of uk.gov.gchq.gaffer.data.elementdefinition.view.ViewElementDefinition in project Gaffer by gchq.

the class AbstractCoreKeyIteratorSettingsFactory method queryTimeAggregatorRequired.

public boolean queryTimeAggregatorRequired(final View view, final AccumuloStore store) {
    Schema schema = store.getSchema();
    if (!schema.isAggregationEnabled()) {
        return false;
    }
    String visibilityProp = schema.getVisibilityProperty();
    for (final String edgeGroup : view.getEdgeGroups()) {
        SchemaEdgeDefinition edgeDefinition = schema.getEdge(edgeGroup);
        if (null != edgeDefinition) {
            if (edgeDefinition.containsProperty(visibilityProp)) {
                return true;
            }
            ViewElementDefinition viewElementDefinition = view.getEdge(edgeGroup);
            if ((null != viewElementDefinition && null != viewElementDefinition.getGroupBy()) && (edgeDefinition.getGroupBy().size() != viewElementDefinition.getGroupBy().size())) {
                return true;
            }
        }
    }
    for (final String entityGroup : view.getEntityGroups()) {
        SchemaEntityDefinition entityDefinition = schema.getEntity(entityGroup);
        if (null != entityDefinition) {
            if (entityDefinition.containsProperty(visibilityProp)) {
                return true;
            }
            ViewElementDefinition viewElementDefinition = view.getElement(entityGroup);
            if ((null != viewElementDefinition.getGroupBy()) && (entityDefinition.getGroupBy().size() != viewElementDefinition.getGroupBy().size())) {
                return true;
            }
        }
    }
    return false;
}
Also used : Schema(uk.gov.gchq.gaffer.store.schema.Schema) SchemaEdgeDefinition(uk.gov.gchq.gaffer.store.schema.SchemaEdgeDefinition) ViewElementDefinition(uk.gov.gchq.gaffer.data.elementdefinition.view.ViewElementDefinition) SchemaEntityDefinition(uk.gov.gchq.gaffer.store.schema.SchemaEntityDefinition)

Example 4 with ViewElementDefinition

use of uk.gov.gchq.gaffer.data.elementdefinition.view.ViewElementDefinition in project Gaffer by gchq.

the class CoreKeyGroupByCombiner method findTop.

/**
 * Sets the topKey and topValue based on the top key of the source.
 */
private void findTop() {
    // check if aggregation is needed
    if (super.hasTop()) {
        workKey.set(super.getTopKey());
        if (workKey.isDeleted()) {
            return;
        }
        if (null != aggregatedGroups && !aggregatedGroups.contains(workKey)) {
            return;
        }
        final byte[] columnFamily = workKey.getColumnFamilyData().getBackingArray();
        final String group;
        try {
            group = elementConverter.getGroupFromColumnFamily(columnFamily);
        } catch (final AccumuloElementConversionException e) {
            throw new RuntimeException(e);
        }
        final ViewElementDefinition elementDef = view.getElement(group);
        Set<String> groupBy = elementDef.getGroupBy();
        if (null == groupBy) {
            groupBy = schema.getElement(group).getGroupBy();
        }
        final Iterator<Properties> iter = new KeyValueIterator(getSource(), group, elementConverter, schema, groupBy);
        final Properties aggregatedProperties = reduce(group, workKey, iter, groupBy, elementDef.getAggregator());
        try {
            final Properties properties = elementConverter.getPropertiesFromColumnQualifier(group, workKey.getColumnQualifierData().getBackingArray());
            properties.putAll(elementConverter.getPropertiesFromColumnVisibility(group, workKey.getColumnVisibilityData().getBackingArray()));
            properties.putAll(aggregatedProperties);
            topValue = elementConverter.getValueFromProperties(group, properties);
            topKey = new Key(workKey.getRowData().getBackingArray(), columnFamily, elementConverter.buildColumnQualifier(group, properties), elementConverter.buildColumnVisibility(group, properties), elementConverter.buildTimestamp(group, properties));
        } catch (final AccumuloElementConversionException e) {
            throw new RuntimeException(e);
        }
        while (iter.hasNext()) {
            iter.next();
        }
    }
}
Also used : SortedKeyValueIterator(org.apache.accumulo.core.iterators.SortedKeyValueIterator) ViewElementDefinition(uk.gov.gchq.gaffer.data.elementdefinition.view.ViewElementDefinition) Properties(uk.gov.gchq.gaffer.data.element.Properties) Key(org.apache.accumulo.core.data.Key) PartialKey(org.apache.accumulo.core.data.PartialKey) AccumuloElementConversionException(uk.gov.gchq.gaffer.accumulostore.key.exception.AccumuloElementConversionException)

Example 5 with ViewElementDefinition

use of uk.gov.gchq.gaffer.data.elementdefinition.view.ViewElementDefinition in project Gaffer by gchq.

the class FederatedStoreUtilTest method shouldUpdateOperationChainAndReturnNullIfNestedOperationViewHasNoGroups.

@Test
public void shouldUpdateOperationChainAndReturnNullIfNestedOperationViewHasNoGroups() {
    // Given
    final Graph graph = createGraph();
    final OperationChain<?> operation = new OperationChain.Builder().first(new GetElements.Builder().view(new View.Builder().edge(TestGroups.EDGE_2, new ViewElementDefinition()).entity(TestGroups.ENTITY_2, new ViewElementDefinition()).build()).build()).build();
    // When
    final OperationChain<?> updatedOp = FederatedStoreUtil.updateOperationForGraph(operation, graph);
    // Then
    assertNull(updatedOp);
}
Also used : Graph(uk.gov.gchq.gaffer.graph.Graph) ViewElementDefinition(uk.gov.gchq.gaffer.data.elementdefinition.view.ViewElementDefinition) View(uk.gov.gchq.gaffer.data.elementdefinition.view.View) Test(org.junit.jupiter.api.Test)

Aggregations

ViewElementDefinition (uk.gov.gchq.gaffer.data.elementdefinition.view.ViewElementDefinition)27 View (uk.gov.gchq.gaffer.data.elementdefinition.view.View)15 Test (org.junit.jupiter.api.Test)10 ElementFilter (uk.gov.gchq.gaffer.data.element.function.ElementFilter)6 HashMap (java.util.HashMap)5 GlobalViewElementDefinition (uk.gov.gchq.gaffer.data.elementdefinition.view.GlobalViewElementDefinition)5 Graph (uk.gov.gchq.gaffer.graph.Graph)5 Set (java.util.Set)4 GetElements (uk.gov.gchq.gaffer.operation.impl.get.GetElements)4 Schema (uk.gov.gchq.gaffer.store.schema.Schema)4 SchemaEdgeDefinition (uk.gov.gchq.gaffer.store.schema.SchemaEdgeDefinition)4 ArrayList (java.util.ArrayList)3 HashSet (java.util.HashSet)3 Map (java.util.Map)3 Element (uk.gov.gchq.gaffer.data.element.Element)3 TestStore (uk.gov.gchq.gaffer.integration.store.TestStore)3 OperationChain (uk.gov.gchq.gaffer.operation.OperationChain)3 OperationView (uk.gov.gchq.gaffer.operation.graph.OperationView)3 Store (uk.gov.gchq.gaffer.store.Store)3 StoreProperties (uk.gov.gchq.gaffer.store.StoreProperties)3