Search in sources :

Example 1 with IncludeIncomingOutgoingType

use of uk.gov.gchq.gaffer.operation.graph.SeededGraphFilters.IncludeIncomingOutgoingType in project Gaffer by gchq.

the class GetElementsUtil method getRelevantElements.

public static Set<Element> getRelevantElements(final MapImpl mapImpl, final ElementId elementId, final View view, final DirectedType directedType, final IncludeIncomingOutgoingType inOutType, final SeedMatchingType seedMatchingType) {
    final Set<Element> relevantElements;
    final Set<String> groups = view.getGroups();
    Predicate<Element> isFiltered = e -> !groups.contains(e.getGroup());
    if (elementId instanceof EntityId) {
        final Collection<Element> elements = mapImpl.lookup(new EntitySeed(((EntityId) elementId).getVertex()));
        if (elements.isEmpty()) {
            return Collections.emptySet();
        }
        relevantElements = new HashSet<>(elements);
        // Apply inOutType options - if option is EITHER then nothing to do
        if (inOutType == IncludeIncomingOutgoingType.INCOMING) {
            isFiltered = isFiltered.or(e -> e instanceof Edge && ((Edge) e).isDirected() && (EdgeId.MatchedVertex.SOURCE == ((Edge) e).getMatchedVertex()));
        } else if (inOutType == IncludeIncomingOutgoingType.OUTGOING) {
            isFiltered = isFiltered.or(e -> e instanceof Edge && ((Edge) e).isDirected() && (EdgeId.MatchedVertex.DESTINATION == ((Edge) e).getMatchedVertex()));
        }
        // Apply seedMatching option - if option is RELATED then nothing to do
        if (seedMatchingType == SeedMatchingType.EQUAL) {
            isFiltered = isFiltered.or(e -> e instanceof Edge);
        }
    } else {
        relevantElements = new HashSet<>();
        final EdgeId edgeId = (EdgeId) elementId;
        if (DirectedType.isEither(edgeId.getDirectedType())) {
            relevantElements.addAll(mapImpl.lookup(new EdgeSeed(edgeId.getSource(), edgeId.getDestination(), false)));
            relevantElements.addAll(mapImpl.lookup(new EdgeSeed(edgeId.getSource(), edgeId.getDestination(), true)));
        } else {
            relevantElements.addAll(mapImpl.lookup(new EdgeSeed(edgeId.getSource(), edgeId.getDestination(), edgeId.getDirectedType())));
        }
        mapImpl.lookup(new EntitySeed(edgeId.getSource())).stream().filter(e -> e instanceof Entity).forEach(relevantElements::add);
        mapImpl.lookup(new EntitySeed(edgeId.getDestination())).stream().filter(e -> e instanceof Entity).forEach(relevantElements::add);
        // If option is RELATED then nothing to do
        if (seedMatchingType == SeedMatchingType.EQUAL) {
            isFiltered = isFiltered.or(e -> e instanceof Entity);
        }
    }
    // Apply directedType flag
    if (directedType == DirectedType.DIRECTED) {
        isFiltered = isFiltered.or(e -> e instanceof Edge && !((Edge) e).isDirected());
    } else if (directedType == DirectedType.UNDIRECTED) {
        isFiltered = isFiltered.or(e -> e instanceof Edge && ((Edge) e).isDirected());
    }
    relevantElements.removeIf(isFiltered);
    return relevantElements;
}
Also used : User(uk.gov.gchq.gaffer.user.User) LoggerFactory(org.slf4j.LoggerFactory) EdgeId(uk.gov.gchq.gaffer.data.element.id.EdgeId) Element(uk.gov.gchq.gaffer.data.element.Element) Authorisations(uk.gov.gchq.gaffer.commonutil.elementvisibilityutil.Authorisations) HashSet(java.util.HashSet) EntitySeed(uk.gov.gchq.gaffer.operation.data.EntitySeed) ElementTransformer(uk.gov.gchq.gaffer.data.element.function.ElementTransformer) CloseableIterable(uk.gov.gchq.gaffer.commonutil.iterable.CloseableIterable) SeedMatchingType(uk.gov.gchq.gaffer.operation.SeedMatching.SeedMatchingType) VisibilityParseException(uk.gov.gchq.gaffer.commonutil.elementvisibilityutil.exception.VisibilityParseException) StreamSupport(java.util.stream.StreamSupport) Edge(uk.gov.gchq.gaffer.data.element.Edge) IncludeIncomingOutgoingType(uk.gov.gchq.gaffer.operation.graph.SeededGraphFilters.IncludeIncomingOutgoingType) EntityId(uk.gov.gchq.gaffer.data.element.id.EntityId) Logger(org.slf4j.Logger) ElementVisibility(uk.gov.gchq.gaffer.commonutil.elementvisibilityutil.ElementVisibility) DirectedType(uk.gov.gchq.gaffer.data.element.id.DirectedType) AggregatorUtil(uk.gov.gchq.gaffer.store.util.AggregatorUtil) VisibilityEvaluator(uk.gov.gchq.gaffer.commonutil.elementvisibilityutil.VisibilityEvaluator) Predicate(java.util.function.Predicate) Collection(java.util.Collection) EdgeSeed(uk.gov.gchq.gaffer.operation.data.EdgeSeed) Set(java.util.Set) Entity(uk.gov.gchq.gaffer.data.element.Entity) Collectors(java.util.stream.Collectors) Stream(java.util.stream.Stream) Schema(uk.gov.gchq.gaffer.store.schema.Schema) View(uk.gov.gchq.gaffer.data.elementdefinition.view.View) ViewElementDefinition(uk.gov.gchq.gaffer.data.elementdefinition.view.ViewElementDefinition) ElementId(uk.gov.gchq.gaffer.data.element.id.ElementId) Collections(java.util.Collections) Entity(uk.gov.gchq.gaffer.data.element.Entity) Element(uk.gov.gchq.gaffer.data.element.Element) EntityId(uk.gov.gchq.gaffer.data.element.id.EntityId) EdgeId(uk.gov.gchq.gaffer.data.element.id.EdgeId) EdgeSeed(uk.gov.gchq.gaffer.operation.data.EdgeSeed) EntitySeed(uk.gov.gchq.gaffer.operation.data.EntitySeed) Edge(uk.gov.gchq.gaffer.data.element.Edge)

Example 2 with IncludeIncomingOutgoingType

use of uk.gov.gchq.gaffer.operation.graph.SeededGraphFilters.IncludeIncomingOutgoingType in project Gaffer by gchq.

the class GetElementsIT method shouldGetElements.

@Test
public void shouldGetElements() {
    final List<DirectedType> directedTypes = Lists.newArrayList(DirectedType.values());
    directedTypes.add(null);
    final List<IncludeIncomingOutgoingType> inOutTypes = Lists.newArrayList(IncludeIncomingOutgoingType.values());
    inOutTypes.add(null);
    for (final boolean includeEntities : Arrays.asList(true, false)) {
        for (final boolean includeEdges : Arrays.asList(true, false)) {
            if (!includeEntities && !includeEdges) {
                // Cannot query for nothing!
                continue;
            }
            for (final DirectedType directedType : directedTypes) {
                for (final IncludeIncomingOutgoingType inOutType : inOutTypes) {
                    try {
                        shouldGetElementsBySeed(includeEntities, includeEdges, directedType, inOutType);
                    } catch (final Throwable e) {
                        throw new AssertionError("GetElementsBySeed failed with parameters: \nincludeEntities=" + includeEntities + " \nincludeEdges=" + includeEdges + " \ndirectedType=" + directedType + " \ninOutType=" + inOutType, e);
                    }
                    try {
                        shouldGetRelatedElements(includeEntities, includeEdges, directedType, inOutType);
                    } catch (final Throwable e) {
                        throw new AssertionError("GetRelatedElements failed with parameters: \nincludeEntities=" + includeEntities + " \nincludeEdges=" + includeEdges + " \ndirectedType=" + directedType + " \ninOutType=" + inOutType, e);
                    }
                }
            }
        }
    }
}
Also used : IncludeIncomingOutgoingType(uk.gov.gchq.gaffer.operation.graph.SeededGraphFilters.IncludeIncomingOutgoingType) DirectedType(uk.gov.gchq.gaffer.data.element.id.DirectedType) Test(org.junit.Test)

Example 3 with IncludeIncomingOutgoingType

use of uk.gov.gchq.gaffer.operation.graph.SeededGraphFilters.IncludeIncomingOutgoingType in project Gaffer by gchq.

the class GetAdjacentIdsIT method shouldGetEntityIds.

@Test
public void shouldGetEntityIds() throws Exception {
    final List<DirectedType> directedTypes = Lists.newArrayList(DirectedType.values());
    directedTypes.add(null);
    final List<IncludeIncomingOutgoingType> inOutTypes = Lists.newArrayList(IncludeIncomingOutgoingType.values());
    inOutTypes.add(null);
    for (final IncludeIncomingOutgoingType inOutType : inOutTypes) {
        for (final DirectedType directedType : directedTypes) {
            final List<String> expectedSeeds = new ArrayList<>();
            if (DirectedType.DIRECTED != directedType) {
                expectedSeeds.add(DEST_1);
                expectedSeeds.add(SOURCE_2);
                expectedSeeds.add(DEST_3);
                expectedSeeds.add(SOURCE_3);
                expectedSeeds.add("A1");
                expectedSeeds.add("B1");
                expectedSeeds.add("C1");
                expectedSeeds.add("D1");
            }
            if (IncludeIncomingOutgoingType.INCOMING != inOutType) {
                if (DirectedType.UNDIRECTED != directedType) {
                    expectedSeeds.add(DEST_DIR + "1");
                    expectedSeeds.add(DEST_DIR_3);
                    expectedSeeds.add("A1");
                    expectedSeeds.add("B1");
                    expectedSeeds.add("C1");
                    expectedSeeds.add("D1");
                }
            }
            if (IncludeIncomingOutgoingType.OUTGOING != inOutType) {
                if (DirectedType.UNDIRECTED != directedType) {
                    expectedSeeds.add(SOURCE_DIR_2);
                    expectedSeeds.add(SOURCE_DIR_3);
                }
            }
            shouldGetEntityIds(expectedSeeds, inOutType, directedType);
        }
    }
}
Also used : IncludeIncomingOutgoingType(uk.gov.gchq.gaffer.operation.graph.SeededGraphFilters.IncludeIncomingOutgoingType) ArrayList(java.util.ArrayList) DirectedType(uk.gov.gchq.gaffer.data.element.id.DirectedType) Test(org.junit.Test)

Example 4 with IncludeIncomingOutgoingType

use of uk.gov.gchq.gaffer.operation.graph.SeededGraphFilters.IncludeIncomingOutgoingType in project Gaffer by gchq.

the class RowRangeFactory method getRowRange.

private List<RowRange> getRowRange(final Object vertex, final GraphFilters operation, final boolean includeEdgesParam) throws SerialisationException {
    final IncludeIncomingOutgoingType inOutType = (operation instanceof SeededGraphFilters) ? ((SeededGraphFilters) operation).getIncludeIncomingOutGoing() : IncludeIncomingOutgoingType.OUTGOING;
    final DirectedType directedType = operation.getDirectedType();
    final boolean includeEdges;
    final boolean includeEntities;
    final boolean seedEqual = (operation instanceof SeedMatching) && SeedMatchingType.EQUAL.equals(((SeedMatching) operation).getSeedMatching());
    if (seedEqual) {
        includeEdges = false;
        includeEntities = true;
    } else {
        includeEdges = includeEdgesParam;
        includeEntities = operation.getView().hasEntities();
    }
    byte[] serialisedVertex = serialiser.serialiseVertex(vertex);
    if (!includeEntities && !includeEdges) {
        throw new IllegalArgumentException("Need to include either Entities or Edges or both when getting RowRange");
    }
    if (!includeEdges) {
        // return only entities
        return Collections.singletonList(new RowRange(getEntityRowId(serialisedVertex, false), true, getEntityRowId(serialisedVertex, true), true));
    } else {
        if (includeEntities) {
            if (directedType == DirectedType.DIRECTED) {
                // return onlyDirectedEdges and entities
                if (inOutType == IncludeIncomingOutgoingType.INCOMING) {
                    return Arrays.asList(new RowRange(getEntityRowId(serialisedVertex, false), true, getEntityRowId(serialisedVertex, true), true), new RowRange(getDirectedEdgeRowIdDestFirst(serialisedVertex, false), true, getDirectedEdgeRowIdDestFirst(serialisedVertex, true), true));
                } else if (inOutType == IncludeIncomingOutgoingType.OUTGOING) {
                    return Collections.singletonList(new RowRange(getEntityRowId(serialisedVertex, false), true, getDirectedEdgeRowIdSourceFirst(serialisedVertex, true), true));
                } else {
                    return Collections.singletonList(new RowRange(getEntityRowId(serialisedVertex, false), true, getDirectedEdgeRowIdDestFirst(serialisedVertex, true), false));
                }
            } else if (directedType == DirectedType.UNDIRECTED) {
                // Entity only range and undirected only range
                return Arrays.asList(new RowRange(getUndirectedEdgeRowId(serialisedVertex, false), true, getUndirectedEdgeRowId(serialisedVertex, true), true), new RowRange(getEntityRowId(serialisedVertex, false), true, getEntityRowId(serialisedVertex, true), true));
            } else {
                // Return everything
                if (inOutType == IncludeIncomingOutgoingType.INCOMING) {
                    return Arrays.asList(new RowRange(getEntityRowId(serialisedVertex, false), true, getEntityRowId(serialisedVertex, true), true), new RowRange(getDirectedEdgeRowIdDestFirst(serialisedVertex, false), true, getUndirectedEdgeRowId(serialisedVertex, true), true));
                } else if (inOutType == IncludeIncomingOutgoingType.OUTGOING) {
                    return Arrays.asList(new RowRange(getEntityRowId(serialisedVertex, false), true, getDirectedEdgeRowIdSourceFirst(serialisedVertex, true), true), new RowRange(getUndirectedEdgeRowId(serialisedVertex, false), true, getUndirectedEdgeRowId(serialisedVertex, true), true));
                } else {
                    return Collections.singletonList(new RowRange(getEntityRowId(serialisedVertex, false), true, getUndirectedEdgeRowId(serialisedVertex, true), true));
                }
            }
        } else if (directedType == DirectedType.DIRECTED) {
            if (inOutType == IncludeIncomingOutgoingType.INCOMING) {
                return Collections.singletonList(new RowRange(getDirectedEdgeRowIdDestFirst(serialisedVertex, false), true, getDirectedEdgeRowIdDestFirst(serialisedVertex, true), true));
            } else if (inOutType == IncludeIncomingOutgoingType.OUTGOING) {
                return Collections.singletonList(new RowRange(getDirectedEdgeRowIdSourceFirst(serialisedVertex, false), true, getDirectedEdgeRowIdSourceFirst(serialisedVertex, true), true));
            } else {
                return Collections.singletonList(new RowRange(getDirectedEdgeRowIdSourceFirst(serialisedVertex, false), true, getDirectedEdgeRowIdDestFirst(serialisedVertex, true), true));
            }
        } else if (directedType == DirectedType.UNDIRECTED) {
            return Collections.singletonList(new RowRange(getUndirectedEdgeRowId(serialisedVertex, false), true, getUndirectedEdgeRowId(serialisedVertex, true), true));
        } else {
            // return all edges
            if (inOutType == IncludeIncomingOutgoingType.INCOMING) {
                return Collections.singletonList(new RowRange(getDirectedEdgeRowIdDestFirst(serialisedVertex, false), true, getUndirectedEdgeRowId(serialisedVertex, true), true));
            } else if (inOutType == IncludeIncomingOutgoingType.OUTGOING) {
                return Arrays.asList(new RowRange(getDirectedEdgeRowIdSourceFirst(serialisedVertex, false), true, getDirectedEdgeRowIdSourceFirst(serialisedVertex, true), true), new RowRange(getUndirectedEdgeRowId(serialisedVertex, false), true, getUndirectedEdgeRowId(serialisedVertex, true), true));
            } else {
                final Pair<byte[], byte[]> keys = getAllEdgeOnlyRowIds(serialisedVertex);
                return Collections.singletonList(new RowRange(keys.getFirst(), false, keys.getSecond(), false));
            }
        }
    }
}
Also used : RowRange(org.apache.hadoop.hbase.filter.MultiRowRangeFilter.RowRange) IncludeIncomingOutgoingType(uk.gov.gchq.gaffer.operation.graph.SeededGraphFilters.IncludeIncomingOutgoingType) DirectedType(uk.gov.gchq.gaffer.data.element.id.DirectedType) SeededGraphFilters(uk.gov.gchq.gaffer.operation.graph.SeededGraphFilters) SeedMatching(uk.gov.gchq.gaffer.operation.SeedMatching) Pair(uk.gov.gchq.gaffer.commonutil.pair.Pair)

Example 5 with IncludeIncomingOutgoingType

use of uk.gov.gchq.gaffer.operation.graph.SeededGraphFilters.IncludeIncomingOutgoingType in project Gaffer by gchq.

the class ByteEntityRangeFactory method getRange.

@Override
protected List<Range> getRange(final Object vertex, final GraphFilters operation, final boolean includeEdgesParam) throws RangeFactoryException {
    final IncludeIncomingOutgoingType inOutType = (operation instanceof SeededGraphFilters) ? ((SeededGraphFilters) operation).getIncludeIncomingOutGoing() : IncludeIncomingOutgoingType.OUTGOING;
    final DirectedType directedType = operation.getDirectedType();
    final boolean includeEdges;
    final boolean includeEntities;
    final boolean seedEqual = (operation instanceof SeedMatching) && SeedMatchingType.EQUAL.equals(((SeedMatching) operation).getSeedMatching());
    if (seedEqual) {
        includeEdges = false;
        includeEntities = true;
    } else {
        includeEdges = includeEdgesParam;
        includeEntities = operation.getView().hasEntities();
    }
    byte[] serialisedVertex;
    try {
        serialisedVertex = ByteArrayEscapeUtils.escape(((ToBytesSerialiser) schema.getVertexSerialiser()).serialise(vertex));
    } catch (final SerialisationException e) {
        throw new RangeFactoryException("Failed to serialise identifier", e);
    }
    if (!includeEntities && !includeEdges) {
        throw new IllegalArgumentException("Need to include either Entities or Edges or both when getting Range");
    }
    if (!includeEdges) {
        // return only entities
        return Collections.singletonList(new Range(getEntityKey(serialisedVertex, false), true, getEntityKey(serialisedVertex, true), true));
    } else {
        if (includeEntities) {
            if (directedType == DirectedType.DIRECTED) {
                // return onlyDirectedEdges and entities
                if (inOutType == IncludeIncomingOutgoingType.INCOMING) {
                    return Arrays.asList(new Range(getEntityKey(serialisedVertex, false), true, getEntityKey(serialisedVertex, true), true), new Range(getDirectedEdgeKeyDestinationFirst(serialisedVertex, false), true, getDirectedEdgeKeyDestinationFirst(serialisedVertex, true), true));
                } else if (inOutType == IncludeIncomingOutgoingType.OUTGOING) {
                    return Collections.singletonList(new Range(getEntityKey(serialisedVertex, false), true, getDirectedEdgeKeySourceFirst(serialisedVertex, true), true));
                } else {
                    return Collections.singletonList(new Range(getEntityKey(serialisedVertex, false), false, getDirectedEdgeKeyDestinationFirst(serialisedVertex, true), false));
                }
            } else if (directedType == DirectedType.UNDIRECTED) {
                // Entity only range and undirected only range
                return Arrays.asList(new Range(getUnDirectedEdgeKey(serialisedVertex, false), true, getUnDirectedEdgeKey(serialisedVertex, true), true), new Range(getEntityKey(serialisedVertex, false), true, getEntityKey(serialisedVertex, true), true));
            } else {
                // Return everything
                if (inOutType == IncludeIncomingOutgoingType.INCOMING) {
                    return Arrays.asList(new Range(getEntityKey(serialisedVertex, false), true, getEntityKey(serialisedVertex, true), true), new Range(getDirectedEdgeKeyDestinationFirst(serialisedVertex, false), true, getUnDirectedEdgeKey(serialisedVertex, true), true));
                } else if (inOutType == IncludeIncomingOutgoingType.OUTGOING) {
                    return Arrays.asList(new Range(getEntityKey(serialisedVertex, false), true, getDirectedEdgeKeySourceFirst(serialisedVertex, true), true), new Range(getUnDirectedEdgeKey(serialisedVertex, false), true, getUnDirectedEdgeKey(serialisedVertex, true), true));
                } else {
                    return Collections.singletonList(new Range(getEntityKey(serialisedVertex, false), true, getUnDirectedEdgeKey(serialisedVertex, true), true));
                }
            }
        } else if (directedType == DirectedType.DIRECTED) {
            if (inOutType == IncludeIncomingOutgoingType.INCOMING) {
                return Collections.singletonList(new Range(getDirectedEdgeKeyDestinationFirst(serialisedVertex, false), true, getDirectedEdgeKeyDestinationFirst(serialisedVertex, true), true));
            } else if (inOutType == IncludeIncomingOutgoingType.OUTGOING) {
                return Collections.singletonList(new Range(getDirectedEdgeKeySourceFirst(serialisedVertex, false), true, getDirectedEdgeKeySourceFirst(serialisedVertex, true), true));
            } else {
                return Collections.singletonList(new Range(getDirectedEdgeKeySourceFirst(serialisedVertex, false), true, getDirectedEdgeKeyDestinationFirst(serialisedVertex, true), true));
            }
        } else if (directedType == DirectedType.UNDIRECTED) {
            return Collections.singletonList(new Range(getUnDirectedEdgeKey(serialisedVertex, false), true, getUnDirectedEdgeKey(serialisedVertex, true), true));
        } else {
            // return all edges
            if (inOutType == IncludeIncomingOutgoingType.INCOMING) {
                return Collections.singletonList(new Range(getDirectedEdgeKeyDestinationFirst(serialisedVertex, false), true, getUnDirectedEdgeKey(serialisedVertex, true), true));
            } else if (inOutType == IncludeIncomingOutgoingType.OUTGOING) {
                return Arrays.asList(new Range(getDirectedEdgeKeySourceFirst(serialisedVertex, false), true, getDirectedEdgeKeySourceFirst(serialisedVertex, true), true), new Range(getUnDirectedEdgeKey(serialisedVertex, false), true, getUnDirectedEdgeKey(serialisedVertex, true), true));
            } else {
                final Pair<Key, Key> keys = getAllEdgeOnlyKeys(serialisedVertex);
                return Collections.singletonList(new Range(keys.getFirst(), false, keys.getSecond(), false));
            }
        }
    }
}
Also used : SerialisationException(uk.gov.gchq.gaffer.exception.SerialisationException) IncludeIncomingOutgoingType(uk.gov.gchq.gaffer.operation.graph.SeededGraphFilters.IncludeIncomingOutgoingType) DirectedType(uk.gov.gchq.gaffer.data.element.id.DirectedType) SeededGraphFilters(uk.gov.gchq.gaffer.operation.graph.SeededGraphFilters) SeedMatching(uk.gov.gchq.gaffer.operation.SeedMatching) ToBytesSerialiser(uk.gov.gchq.gaffer.serialisation.ToBytesSerialiser) Range(org.apache.accumulo.core.data.Range) RangeFactoryException(uk.gov.gchq.gaffer.accumulostore.key.exception.RangeFactoryException) Pair(uk.gov.gchq.gaffer.commonutil.pair.Pair)

Aggregations

DirectedType (uk.gov.gchq.gaffer.data.element.id.DirectedType)6 IncludeIncomingOutgoingType (uk.gov.gchq.gaffer.operation.graph.SeededGraphFilters.IncludeIncomingOutgoingType)6 SeededGraphFilters (uk.gov.gchq.gaffer.operation.graph.SeededGraphFilters)3 Test (org.junit.Test)2 Pair (uk.gov.gchq.gaffer.commonutil.pair.Pair)2 SeedMatching (uk.gov.gchq.gaffer.operation.SeedMatching)2 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 HashSet (java.util.HashSet)1 Set (java.util.Set)1 Predicate (java.util.function.Predicate)1 Collectors (java.util.stream.Collectors)1 Stream (java.util.stream.Stream)1 StreamSupport (java.util.stream.StreamSupport)1 IteratorSetting (org.apache.accumulo.core.client.IteratorSetting)1 Range (org.apache.accumulo.core.data.Range)1 RowRange (org.apache.hadoop.hbase.filter.MultiRowRangeFilter.RowRange)1 Logger (org.slf4j.Logger)1 LoggerFactory (org.slf4j.LoggerFactory)1