use of uk.gov.gchq.gaffer.data.element.EdgeDirection in project Gaffer by gchq.
the class AbstractCoreKeyAccumuloElementConverterTest method shouldDeserialiseSourceDestinationValuesIncorrectWayRound.
@Test
public void shouldDeserialiseSourceDestinationValuesIncorrectWayRound() {
// Given
final Edge edge = new Edge.Builder().source("1").dest("2").directed(true).group(TestGroups.ENTITY).build();
final byte[] rowKey = converter.getRowKeysFromEdge(edge).getSecond();
final byte[][] sourceDestValues = new byte[2][];
// When
final EdgeDirection direction = converter.getSourceAndDestinationFromRowKey(rowKey, sourceDestValues);
// Then
assertEquals(EdgeDirection.DIRECTED_REVERSED, direction);
}
use of uk.gov.gchq.gaffer.data.element.EdgeDirection in project Gaffer by gchq.
the class AbstractCoreKeyAccumuloElementConverter method getEdgeFromKey.
@SuppressWarnings("WeakerAccess")
protected Edge getEdgeFromKey(final Key key, final byte[] row, final boolean includeMatchedVertex) {
final byte[][] result = new byte[2][];
final EdgeDirection direction = getSourceAndDestinationFromRowKey(row, result);
final EdgeId.MatchedVertex matchedVertex;
if (!includeMatchedVertex) {
matchedVertex = null;
} else if (EdgeDirection.DIRECTED_REVERSED == direction) {
matchedVertex = EdgeId.MatchedVertex.DESTINATION;
} else {
matchedVertex = EdgeId.MatchedVertex.SOURCE;
}
final String group = getGroupFromColumnFamily(key.getColumnFamilyData().getBackingArray());
try {
final Edge edge = new Edge(group, ((ToBytesSerialiser) schema.getVertexSerialiser()).deserialise(result[0]), ((ToBytesSerialiser) schema.getVertexSerialiser()).deserialise(result[1]), direction.isDirected(), matchedVertex, null);
addPropertiesToElement(edge, key);
return edge;
} catch (final SerialisationException e) {
throw new AccumuloElementConversionException("Failed to re-create Edge from key", e);
}
}
use of uk.gov.gchq.gaffer.data.element.EdgeDirection in project Gaffer by gchq.
the class AbstractCoreKeyAccumuloElementConverter method getEdgeId.
protected EdgeId getEdgeId(final byte[] row, final boolean includeMatchedVertex) {
final byte[][] result = new byte[2][];
final EdgeDirection direction = getSourceAndDestinationFromRowKey(row, result);
final EdgeId.MatchedVertex matchedVertex;
if (!includeMatchedVertex) {
matchedVertex = null;
} else if (EdgeDirection.DIRECTED_REVERSED == direction) {
matchedVertex = EdgeId.MatchedVertex.DESTINATION;
} else {
matchedVertex = EdgeId.MatchedVertex.SOURCE;
}
try {
return new EdgeSeed(((ToBytesSerialiser) schema.getVertexSerialiser()).deserialise(result[0]), ((ToBytesSerialiser) schema.getVertexSerialiser()).deserialise(result[1]), direction.isDirected(), matchedVertex);
} catch (final SerialisationException e) {
throw new AccumuloElementConversionException("Failed to create EdgeId from Accumulo row key", e);
}
}
use of uk.gov.gchq.gaffer.data.element.EdgeDirection in project Gaffer by gchq.
the class ElementSerialisationTest method shouldDeserialiseSourceDestinationValuesIncorrectWayRound.
@Test
public void shouldDeserialiseSourceDestinationValuesIncorrectWayRound() throws SerialisationException {
// Given
final Edge edge = new Edge.Builder().source("1").dest("2").directed(true).group(TestGroups.ENTITY).build();
final byte[] rowKey = serialisation.getRowKeys(edge).getSecond();
final byte[][] sourceDestValues = new byte[2][];
// When
final EdgeDirection direction = serialisation.getSourceAndDestination(rowKey, sourceDestValues);
// Then
assertEquals(EdgeDirection.DIRECTED_REVERSED, direction);
}
use of uk.gov.gchq.gaffer.data.element.EdgeDirection in project Gaffer by gchq.
the class ByteEntityAccumuloElementConverter method getSourceAndDestinationFromRowKey.
@Override
protected EdgeDirection getSourceAndDestinationFromRowKey(final byte[] rowKey, final byte[][] sourceDestValues) {
// Get element class, sourceValue, destinationValue and directed flag from row key
// Expect to find 3 delimiters (4 fields)
final int[] positionsOfDelimiters = new int[3];
short numDelims = 0;
// Last byte will be directional flag so don't count it
for (int i = 0; i < rowKey.length - 1; ++i) {
if (rowKey[i] == ByteArrayEscapeUtils.DELIMITER) {
if (numDelims >= 3) {
throw new AccumuloElementConversionException("Too many delimiters found in row key - found more than the expected 3.");
}
positionsOfDelimiters[numDelims++] = i;
}
}
if (numDelims != 3) {
throw new AccumuloElementConversionException("Wrong number of delimiters found in row key - found " + numDelims + ", expected 3.");
}
// If edge is undirected then create edge
// (no need to worry about which direction the vertices should go in).
// If the edge is directed then need to decide which way round the vertices should go.
byte directionFlag;
try {
directionFlag = rowKey[rowKey.length - 1];
} catch (final NumberFormatException e) {
throw new AccumuloElementConversionException("Error parsing direction flag from row key - " + e);
}
byte[] sourceBytes = ByteArrayEscapeUtils.unEscape(rowKey, 0, positionsOfDelimiters[0]);
byte[] destBytes = ByteArrayEscapeUtils.unEscape(rowKey, positionsOfDelimiters[1] + 1, positionsOfDelimiters[2]);
EdgeDirection rtn;
sourceDestValues[0] = sourceBytes;
sourceDestValues[1] = destBytes;
switch(directionFlag) {
case ByteEntityPositions.UNDIRECTED_EDGE:
// Edge is undirected
rtn = EdgeDirection.UNDIRECTED;
break;
case ByteEntityPositions.CORRECT_WAY_DIRECTED_EDGE:
// Edge is directed and the first identifier is the source of the edge
rtn = EdgeDirection.DIRECTED;
break;
case ByteEntityPositions.INCORRECT_WAY_DIRECTED_EDGE:
// Edge is directed and the second identifier is the source of the edge
sourceDestValues[0] = destBytes;
sourceDestValues[1] = sourceBytes;
rtn = EdgeDirection.DIRECTED_REVERSED;
break;
default:
throw new AccumuloElementConversionException("Invalid direction flag in row key - flag was " + directionFlag);
}
return rtn;
}
Aggregations