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);
}
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;
}
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;
}
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();
}
}
}
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);
}
Aggregations