Search in sources :

Example 11 with DirectedType

use of uk.gov.gchq.gaffer.data.element.id.DirectedType 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 12 with DirectedType

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

the class AbstractCoreKeyRangeFactory method getRange.

private List<Range> getRange(final ElementId elementId, final GraphFilters operation, final SeededGraphFilters.IncludeIncomingOutgoingType inOutType) throws RangeFactoryException {
    if (elementId instanceof EntityId) {
        return getRange(((EntityId) elementId).getVertex(), operation, operation.getView().hasEdges());
    } else {
        final EdgeId edgeId = (EdgeId) elementId;
        final List<Range> ranges = new ArrayList<>();
        if (operation.getView().hasEdges() && DirectedType.areCompatible(operation.getDirectedType(), edgeId.getDirectedType())) {
            // EQUALS and RELATED seed matching.
            final DirectedType directed = DirectedType.and(operation.getDirectedType(), edgeId.getDirectedType());
            ranges.addAll(getRange(edgeId.getSource(), edgeId.getDestination(), directed, operation, inOutType));
        }
        // Do related - if operation doesn't have seed matching or it has seed matching equal to RELATED
        final boolean doRelated = !(operation instanceof SeedMatching) || !SeedMatching.SeedMatchingType.EQUAL.equals(((SeedMatching) operation).getSeedMatching());
        if (doRelated && operation.getView().hasEntities()) {
            // Get Entities related to EdgeIds
            ranges.addAll(getRange(edgeId.getSource(), operation, false));
            ranges.addAll(getRange(edgeId.getDestination(), operation, false));
        }
        return ranges;
    }
}
Also used : EntityId(uk.gov.gchq.gaffer.data.element.id.EntityId) EdgeId(uk.gov.gchq.gaffer.data.element.id.EdgeId) ArrayList(java.util.ArrayList) DirectedType(uk.gov.gchq.gaffer.data.element.id.DirectedType) SeedMatching(uk.gov.gchq.gaffer.operation.SeedMatching) Range(org.apache.accumulo.core.data.Range)

Example 13 with DirectedType

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

the class RowRangeFactory method getRowRange.

@SuppressFBWarnings(value = "BC_UNCONFIRMED_CAST", justification = "If an element is not an Entity it must be an Edge")
public List<RowRange> getRowRange(final ElementId elementId, final GraphFilters operation) throws SerialisationException {
    if (elementId instanceof EntityId) {
        return getRowRange(((EntityId) elementId).getVertex(), operation, operation.getView().hasEdges());
    } else {
        final EdgeId edgeId = (EdgeId) elementId;
        final List<RowRange> ranges = new ArrayList<>();
        if (operation.getView().hasEdges() && DirectedType.areCompatible(operation.getDirectedType(), edgeId.getDirectedType())) {
            // Get Edges with the given EdgeSeed - This is applicable for
            // EQUALS and RELATED seed matching.
            final DirectedType directed = DirectedType.and(operation.getDirectedType(), edgeId.getDirectedType());
            // To do that we need to create 2 ranges
            if (DirectedType.isEither(directed)) {
                ranges.add(new RowRange(getEdgeRowId(edgeId.getSource(), edgeId.getDestination(), false, false), true, getEdgeRowId(edgeId.getSource(), edgeId.getDestination(), false, true), true));
                ranges.add(new RowRange(getEdgeRowId(edgeId.getSource(), edgeId.getDestination(), true, false), true, getEdgeRowId(edgeId.getSource(), edgeId.getDestination(), true, true), true));
            } else {
                ranges.add(new RowRange(getEdgeRowId(edgeId.getSource(), edgeId.getDestination(), directed.isDirected(), false), true, getEdgeRowId(edgeId.getSource(), edgeId.getDestination(), directed.isDirected(), true), true));
            }
        }
        // Do related - if operation doesn't have seed matching or it has seed matching equal to RELATED
        final boolean doRelated = !(operation instanceof SeedMatching) || !SeedMatchingType.EQUAL.equals(((SeedMatching) operation).getSeedMatching());
        if (doRelated && operation.getView().hasEntities()) {
            // Get Entities related to EdgeIds
            ranges.addAll(getRowRange(edgeId.getSource(), operation, false));
            ranges.addAll(getRowRange(edgeId.getDestination(), operation, false));
        }
        return ranges;
    }
}
Also used : EntityId(uk.gov.gchq.gaffer.data.element.id.EntityId) RowRange(org.apache.hadoop.hbase.filter.MultiRowRangeFilter.RowRange) EdgeId(uk.gov.gchq.gaffer.data.element.id.EdgeId) ArrayList(java.util.ArrayList) DirectedType(uk.gov.gchq.gaffer.data.element.id.DirectedType) SeedMatching(uk.gov.gchq.gaffer.operation.SeedMatching) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings)

Example 14 with DirectedType

use of uk.gov.gchq.gaffer.data.element.id.DirectedType 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 15 with DirectedType

use of uk.gov.gchq.gaffer.data.element.id.DirectedType 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)16 View (uk.gov.gchq.gaffer.data.elementdefinition.view.View)7 IncludeIncomingOutgoingType (uk.gov.gchq.gaffer.operation.graph.SeededGraphFilters.IncludeIncomingOutgoingType)7 ArrayList (java.util.ArrayList)6 Edge (uk.gov.gchq.gaffer.data.element.Edge)5 Entity (uk.gov.gchq.gaffer.data.element.Entity)5 EntityId (uk.gov.gchq.gaffer.data.element.id.EntityId)5 ViewElementDefinition (uk.gov.gchq.gaffer.data.elementdefinition.view.ViewElementDefinition)5 SeededGraphFilters (uk.gov.gchq.gaffer.operation.graph.SeededGraphFilters)5 HashSet (java.util.HashSet)4 List (java.util.List)4 Set (java.util.Set)4 Test (org.junit.Test)4 TestGroups (uk.gov.gchq.gaffer.commonutil.TestGroups)4 CloseableIterable (uk.gov.gchq.gaffer.commonutil.iterable.CloseableIterable)4 Element (uk.gov.gchq.gaffer.data.element.Element)4 ElementFilter (uk.gov.gchq.gaffer.data.element.function.ElementFilter)4 SeedMatching (uk.gov.gchq.gaffer.operation.SeedMatching)4 GetAllElements (uk.gov.gchq.gaffer.operation.impl.get.GetAllElements)4 Collectors (java.util.stream.Collectors)3