Search in sources :

Example 11 with EdgeDirection

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

the class ElementSerialisation method getSourceAndDestination.

public EdgeDirection getSourceAndDestination(final byte[] rowKey, final byte[][] sourceDestValues) throws SerialisationException {
    // Get element class, sourceValue, destinationValue and directed flag from row cell
    // 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 SerialisationException("Too many delimiters found in row cell - found more than the expected 3.");
            }
            positionsOfDelimiters[numDelims++] = i;
        }
    }
    if (numDelims != 3) {
        throw new SerialisationException("Wrong number of delimiters found in row cell - 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 SerialisationException("Error parsing direction flag from row cell - " + e);
    }
    byte[] sourceBytes = ByteArrayEscapeUtils.unEscape(rowKey, 0, positionsOfDelimiters[0]);
    byte[] destBytes = ByteArrayEscapeUtils.unEscape(rowKey, positionsOfDelimiters[1] + 1, positionsOfDelimiters[2]);
    sourceDestValues[0] = sourceBytes;
    sourceDestValues[1] = destBytes;
    EdgeDirection rtn;
    switch(directionFlag) {
        case HBaseStoreConstants.UNDIRECTED_EDGE:
            // Edge is undirected
            rtn = EdgeDirection.UNDIRECTED;
            break;
        case HBaseStoreConstants.CORRECT_WAY_DIRECTED_EDGE:
            // Edge is directed and the first identifier is the source of the edge
            rtn = EdgeDirection.DIRECTED;
            break;
        case HBaseStoreConstants.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 SerialisationException("Invalid direction flag in row cell - flag was " + directionFlag);
    }
    return rtn;
}
Also used : SerialisationException(uk.gov.gchq.gaffer.exception.SerialisationException) EdgeDirection(uk.gov.gchq.gaffer.data.element.EdgeDirection)

Example 12 with EdgeDirection

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

the class ElementSerialisation method getEdge.

private Edge getEdge(final Cell cell, final boolean includeMatchedVertex) throws SerialisationException {
    final byte[][] result = new byte[3][];
    final EdgeDirection direction = getSourceAndDestination(CellUtil.cloneRow(cell), 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 = getGroup(cell);
    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, cell);
        return edge;
    } catch (final SerialisationException e) {
        throw new SerialisationException("Failed to re-create Edge from cell", e);
    }
}
Also used : SerialisationException(uk.gov.gchq.gaffer.exception.SerialisationException) EdgeDirection(uk.gov.gchq.gaffer.data.element.EdgeDirection) EdgeId(uk.gov.gchq.gaffer.data.element.id.EdgeId) Edge(uk.gov.gchq.gaffer.data.element.Edge)

Aggregations

EdgeDirection (uk.gov.gchq.gaffer.data.element.EdgeDirection)12 Edge (uk.gov.gchq.gaffer.data.element.Edge)8 Test (org.junit.jupiter.api.Test)6 AccumuloElementConversionException (uk.gov.gchq.gaffer.accumulostore.key.exception.AccumuloElementConversionException)4 SerialisationException (uk.gov.gchq.gaffer.exception.SerialisationException)4 AbstractAccumuloElementConverterTest (uk.gov.gchq.gaffer.accumulostore.key.AbstractAccumuloElementConverterTest)3 EdgeId (uk.gov.gchq.gaffer.data.element.id.EdgeId)3 EdgeSeed (uk.gov.gchq.gaffer.operation.data.EdgeSeed)1