Search in sources :

Example 21 with ToBytesSerialiser

use of uk.gov.gchq.gaffer.serialisation.ToBytesSerialiser in project Gaffer by gchq.

the class ClassicRangeFactory method getKeyFromEdgeId.

protected Key getKeyFromEdgeId(final Object sourceVal, final Object destVal, final DirectedType directed, final IncludeIncomingOutgoingType inOutType, final boolean endKey) throws RangeFactoryException {
    final byte directionFlag1;
    if (DirectedType.isEither(directed)) {
        // Get directed and undirected edges
        directionFlag1 = endKey ? ClassicBytePositions.INCORRECT_WAY_DIRECTED_EDGE : ClassicBytePositions.UNDIRECTED_EDGE;
    } else if (directed.isDirected()) {
        if (inOutType == IncludeIncomingOutgoingType.INCOMING) {
            directionFlag1 = ClassicBytePositions.INCORRECT_WAY_DIRECTED_EDGE;
        } else {
            directionFlag1 = ClassicBytePositions.CORRECT_WAY_DIRECTED_EDGE;
        }
    } else {
        directionFlag1 = ClassicBytePositions.UNDIRECTED_EDGE;
    }
    final ToBytesSerialiser vertexSerialiser = (ToBytesSerialiser) schema.getVertexSerialiser();
    // Serialise source and destination to byte arrays, escaping if
    // necessary
    byte[] source;
    try {
        source = ByteArrayEscapeUtils.escape(vertexSerialiser.serialise(sourceVal));
    } catch (final SerialisationException e) {
        throw new RangeFactoryException("Failed to serialise Edge Source", e);
    }
    byte[] destination;
    try {
        destination = ByteArrayEscapeUtils.escape(vertexSerialiser.serialise(destVal));
    } catch (final SerialisationException e) {
        throw new RangeFactoryException("Failed to serialise Edge Destination", e);
    }
    // Length of row key is the length of the source
    // plus the length of the destination
    // plus one for the delimiter in between the source and destination
    // plus one for the delimiter in between the destination and the direction flag
    // plus one for the direction flag at the end.
    byte[] key;
    if (endKey) {
        key = new byte[source.length + destination.length + 4];
        key[key.length - 3] = ByteArrayEscapeUtils.DELIMITER;
        key[key.length - 2] = directionFlag1;
        key[key.length - 1] = ByteArrayEscapeUtils.DELIMITER_PLUS_ONE;
    } else {
        key = new byte[source.length + destination.length + 3];
        key[key.length - 2] = ByteArrayEscapeUtils.DELIMITER;
        key[key.length - 1] = directionFlag1;
    }
    // Create first key: source DELIMITER destination DELIMITER (CORRECT_WAY_DIRECTED_EDGE or UNDIRECTED_EDGE)
    // Here if we desire an EdgeID we and the user has asked for incoming
    // edges only for related items, we put the destination first so we find the flipped edge's key,
    // this key will pass the filter iterators check for incoming edges, but
    // the result will be flipped back to the correct edge on the client end conversion
    // Simply put when looking up an EDGE ID that ID counts as both incoming
    // and outgoing so we use the reversed key when looking up incoming.
    byte[] firstValue;
    byte[] secondValue;
    if (inOutType == IncludeIncomingOutgoingType.INCOMING) {
        firstValue = destination;
        secondValue = source;
    } else {
        firstValue = source;
        secondValue = destination;
    }
    System.arraycopy(firstValue, 0, key, 0, firstValue.length);
    key[firstValue.length] = ByteArrayEscapeUtils.DELIMITER;
    System.arraycopy(secondValue, 0, key, firstValue.length + 1, secondValue.length);
    return new Key(key, AccumuloStoreConstants.EMPTY_BYTES, AccumuloStoreConstants.EMPTY_BYTES, AccumuloStoreConstants.EMPTY_BYTES, Long.MAX_VALUE);
}
Also used : SerialisationException(uk.gov.gchq.gaffer.exception.SerialisationException) ToBytesSerialiser(uk.gov.gchq.gaffer.serialisation.ToBytesSerialiser) Key(org.apache.accumulo.core.data.Key) RangeFactoryException(uk.gov.gchq.gaffer.accumulostore.key.exception.RangeFactoryException)

Example 22 with ToBytesSerialiser

use of uk.gov.gchq.gaffer.serialisation.ToBytesSerialiser 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)

Example 23 with ToBytesSerialiser

use of uk.gov.gchq.gaffer.serialisation.ToBytesSerialiser in project Gaffer by gchq.

the class ElementSerialisation method getPropertiesFromColumnQualifier.

public Properties getPropertiesFromColumnQualifier(final String group, final byte[] bytes) throws SerialisationException {
    final SchemaElementDefinition elementDefinition = schema.getElement(group);
    if (null == elementDefinition) {
        throw new SerialisationException("No SchemaElementDefinition found for group " + group + ", is this group in your schema or do your table iterators need updating?");
    }
    final Properties properties = new Properties();
    if (null == bytes || bytes.length == 0) {
        return properties;
    }
    int carriage = CompactRawSerialisationUtils.decodeVIntSize(bytes[0]) + Bytes.toBytes(group).length;
    final int arrayLength = bytes.length;
    final Iterator<String> propertyNames = elementDefinition.getGroupBy().iterator();
    while (propertyNames.hasNext() && carriage < arrayLength) {
        final String propertyName = propertyNames.next();
        final TypeDefinition typeDefinition = elementDefinition.getPropertyTypeDef(propertyName);
        final ToBytesSerialiser serialiser = (null != typeDefinition) ? (ToBytesSerialiser) typeDefinition.getSerialiser() : null;
        if (null != serialiser) {
            final int numBytesForLength = CompactRawSerialisationUtils.decodeVIntSize(bytes[carriage]);
            int currentPropLength;
            try {
                // this value is never greater than int.
                currentPropLength = (int) CompactRawSerialisationUtils.readLong(bytes, carriage);
            } catch (final SerialisationException e) {
                throw new SerialisationException("Exception reading length of property");
            }
            carriage += numBytesForLength;
            if (currentPropLength > 0) {
                try {
                    properties.put(propertyName, serialiser.deserialise(bytes, carriage, currentPropLength));
                    carriage += currentPropLength;
                } catch (final SerialisationException e) {
                    throw new SerialisationException("Failed to deserialise property " + propertyName, e);
                }
            } else {
                try {
                    properties.put(propertyName, serialiser.deserialiseEmpty());
                } catch (final SerialisationException e) {
                    throw new SerialisationException("Failed to deserialise property " + propertyName, e);
                }
            }
        }
    }
    return properties;
}
Also used : SerialisationException(uk.gov.gchq.gaffer.exception.SerialisationException) ToBytesSerialiser(uk.gov.gchq.gaffer.serialisation.ToBytesSerialiser) Properties(uk.gov.gchq.gaffer.data.element.Properties) SchemaElementDefinition(uk.gov.gchq.gaffer.store.schema.SchemaElementDefinition) TypeDefinition(uk.gov.gchq.gaffer.store.schema.TypeDefinition)

Aggregations

ToBytesSerialiser (uk.gov.gchq.gaffer.serialisation.ToBytesSerialiser)23 SerialisationException (uk.gov.gchq.gaffer.exception.SerialisationException)14 TypeDefinition (uk.gov.gchq.gaffer.store.schema.TypeDefinition)10 SchemaElementDefinition (uk.gov.gchq.gaffer.store.schema.SchemaElementDefinition)7 IOException (java.io.IOException)4 RangeFactoryException (uk.gov.gchq.gaffer.accumulostore.key.exception.RangeFactoryException)4 Test (org.junit.jupiter.api.Test)3 AccumuloElementConversionException (uk.gov.gchq.gaffer.accumulostore.key.exception.AccumuloElementConversionException)3 Properties (uk.gov.gchq.gaffer.data.element.Properties)3 Serialiser (uk.gov.gchq.gaffer.serialisation.Serialiser)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Key (org.apache.accumulo.core.data.Key)2 SeedMatching (uk.gov.gchq.gaffer.operation.SeedMatching)2 LengthValueBytesSerialiserUtil (uk.gov.gchq.gaffer.serialisation.util.LengthValueBytesSerialiserUtil)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 DataInputStream (java.io.DataInputStream)1 DataOutputStream (java.io.DataOutputStream)1 Range (org.apache.accumulo.core.data.Range)1