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