Search in sources :

Example 1 with AccumuloElementConversionException

use of uk.gov.gchq.gaffer.accumulostore.key.exception.AccumuloElementConversionException in project Gaffer by gchq.

the class ByteEntityAccumuloElementConverter method getSourceAndDestinationFromRowKey.

@Override
protected boolean getSourceAndDestinationFromRowKey(final byte[] rowKey, final byte[][] sourceDestValues, final Map<String, String> options) throws AccumuloElementConversionException {
    // 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);
    }
    if (directionFlag == ByteEntityPositions.UNDIRECTED_EDGE) {
        // Edge is undirected
        sourceDestValues[0] = getSourceBytes(rowKey, positionsOfDelimiters);
        sourceDestValues[1] = getDestBytes(rowKey, positionsOfDelimiters);
        return false;
    } else if (directionFlag == ByteEntityPositions.CORRECT_WAY_DIRECTED_EDGE) {
        // Edge is directed and the first identifier is the source of the edge
        sourceDestValues[0] = getSourceBytes(rowKey, positionsOfDelimiters);
        sourceDestValues[1] = getDestBytes(rowKey, positionsOfDelimiters);
        return true;
    } else if (directionFlag == ByteEntityPositions.INCORRECT_WAY_DIRECTED_EDGE) {
        // Edge is directed and the second identifier is the source of the edge
        int src = 1;
        int dst = 0;
        if (matchEdgeSource(options)) {
            src = 0;
            dst = 1;
        }
        sourceDestValues[src] = getSourceBytes(rowKey, positionsOfDelimiters);
        sourceDestValues[dst] = getDestBytes(rowKey, positionsOfDelimiters);
        return true;
    } else {
        throw new AccumuloElementConversionException("Invalid direction flag in row key - flag was " + directionFlag);
    }
}
Also used : AccumuloElementConversionException(uk.gov.gchq.gaffer.accumulostore.key.exception.AccumuloElementConversionException)

Example 2 with AccumuloElementConversionException

use of uk.gov.gchq.gaffer.accumulostore.key.exception.AccumuloElementConversionException in project Gaffer by gchq.

the class AccumuloStore method insertGraphElements.

protected void insertGraphElements(final Iterable<Element> elements) throws StoreException {
    // Create BatchWriter
    final BatchWriter writer = TableUtils.createBatchWriter(this);
    // too high a latency, etc.
    if (elements != null) {
        for (final Element element : elements) {
            final Pair<Key> keys;
            try {
                keys = keyPackage.getKeyConverter().getKeysFromElement(element);
            } catch (final AccumuloElementConversionException e) {
                LOGGER.error("Failed to create an accumulo key from element of type " + element.getGroup() + " when trying to insert elements");
                continue;
            }
            final Value value;
            try {
                value = keyPackage.getKeyConverter().getValueFromElement(element);
            } catch (final AccumuloElementConversionException e) {
                LOGGER.error("Failed to create an accumulo value from element of type " + element.getGroup() + " when trying to insert elements");
                continue;
            }
            final Mutation m = new Mutation(keys.getFirst().getRow());
            m.put(keys.getFirst().getColumnFamily(), keys.getFirst().getColumnQualifier(), new ColumnVisibility(keys.getFirst().getColumnVisibility()), keys.getFirst().getTimestamp(), value);
            try {
                writer.addMutation(m);
            } catch (final MutationsRejectedException e) {
                LOGGER.error("Failed to create an accumulo key mutation");
                continue;
            }
            // If the GraphElement is an Edge then there will be 2 keys.
            if (keys.getSecond() != null) {
                final Mutation m2 = new Mutation(keys.getSecond().getRow());
                m2.put(keys.getSecond().getColumnFamily(), keys.getSecond().getColumnQualifier(), new ColumnVisibility(keys.getSecond().getColumnVisibility()), keys.getSecond().getTimestamp(), value);
                try {
                    writer.addMutation(m2);
                } catch (final MutationsRejectedException e) {
                    LOGGER.error("Failed to create an accumulo key mutation");
                }
            }
        }
    } else {
        throw new GafferRuntimeException("Could not find any elements to add to graph.", Status.BAD_REQUEST);
    }
    try {
        writer.close();
    } catch (final MutationsRejectedException e) {
        LOGGER.warn("Accumulo batch writer failed to close", e);
    }
}
Also used : Element(uk.gov.gchq.gaffer.data.element.Element) Value(org.apache.accumulo.core.data.Value) BatchWriter(org.apache.accumulo.core.client.BatchWriter) Mutation(org.apache.accumulo.core.data.Mutation) ColumnVisibility(org.apache.accumulo.core.security.ColumnVisibility) Key(org.apache.accumulo.core.data.Key) AccumuloElementConversionException(uk.gov.gchq.gaffer.accumulostore.key.exception.AccumuloElementConversionException) MutationsRejectedException(org.apache.accumulo.core.client.MutationsRejectedException) GafferRuntimeException(uk.gov.gchq.gaffer.core.exception.GafferRuntimeException)

Example 3 with AccumuloElementConversionException

use of uk.gov.gchq.gaffer.accumulostore.key.exception.AccumuloElementConversionException in project Gaffer by gchq.

the class ByteEntityAccumuloElementConverter method getRowKeyFromEntity.

@Override
protected byte[] getRowKeyFromEntity(final Entity entity) throws AccumuloElementConversionException {
    byte[] value;
    try {
        value = ByteArrayEscapeUtils.escape(getVertexSerialiser().serialise(entity.getVertex()));
        final byte[] returnVal = Arrays.copyOf(value, value.length + 2);
        returnVal[returnVal.length - 2] = ByteArrayEscapeUtils.DELIMITER;
        returnVal[returnVal.length - 1] = ByteEntityPositions.ENTITY;
        return returnVal;
    } catch (final SerialisationException e) {
        throw new AccumuloElementConversionException("Failed to serialise Entity Identifier", e);
    }
}
Also used : SerialisationException(uk.gov.gchq.gaffer.exception.SerialisationException) AccumuloElementConversionException(uk.gov.gchq.gaffer.accumulostore.key.exception.AccumuloElementConversionException)

Example 4 with AccumuloElementConversionException

use of uk.gov.gchq.gaffer.accumulostore.key.exception.AccumuloElementConversionException in project Gaffer by gchq.

the class ByteEntityAccumuloElementConverter method getEntityFromKey.

@Override
protected Entity getEntityFromKey(final Key key) throws AccumuloElementConversionException {
    try {
        final Entity entity = new Entity(getGroupFromKey(key), getVertexSerialiser().deserialise(ByteArrayEscapeUtils.unEscape(Arrays.copyOfRange(key.getRowData().getBackingArray(), 0, (key.getRowData().getBackingArray().length) - 2))));
        addPropertiesToElement(entity, key);
        return entity;
    } catch (final SerialisationException e) {
        throw new AccumuloElementConversionException("Failed to re-create Entity from key", e);
    }
}
Also used : Entity(uk.gov.gchq.gaffer.data.element.Entity) SerialisationException(uk.gov.gchq.gaffer.exception.SerialisationException) AccumuloElementConversionException(uk.gov.gchq.gaffer.accumulostore.key.exception.AccumuloElementConversionException)

Example 5 with AccumuloElementConversionException

use of uk.gov.gchq.gaffer.accumulostore.key.exception.AccumuloElementConversionException in project Gaffer by gchq.

the class ClassicAccumuloElementConverter method getEntityFromKey.

@Override
protected Entity getEntityFromKey(final Key key) throws AccumuloElementConversionException {
    try {
        final Entity entity = new Entity(getGroupFromKey((key)), getVertexSerialiser().deserialise(ByteArrayEscapeUtils.unEscape(key.getRowData().getBackingArray())));
        addPropertiesToElement(entity, key);
        return entity;
    } catch (final SerialisationException e) {
        throw new AccumuloElementConversionException("Failed to re-create Entity from key", e);
    }
}
Also used : Entity(uk.gov.gchq.gaffer.data.element.Entity) SerialisationException(uk.gov.gchq.gaffer.exception.SerialisationException) AccumuloElementConversionException(uk.gov.gchq.gaffer.accumulostore.key.exception.AccumuloElementConversionException)

Aggregations

AccumuloElementConversionException (uk.gov.gchq.gaffer.accumulostore.key.exception.AccumuloElementConversionException)29 SerialisationException (uk.gov.gchq.gaffer.exception.SerialisationException)12 Key (org.apache.accumulo.core.data.Key)11 Properties (uk.gov.gchq.gaffer.data.element.Properties)7 Value (org.apache.accumulo.core.data.Value)6 Entity (uk.gov.gchq.gaffer.data.element.Entity)6 Range (org.apache.accumulo.core.data.Range)4 Test (org.junit.jupiter.api.Test)4 Edge (uk.gov.gchq.gaffer.data.element.Edge)4 EdgeDirection (uk.gov.gchq.gaffer.data.element.EdgeDirection)4 SchemaElementDefinition (uk.gov.gchq.gaffer.store.schema.SchemaElementDefinition)4 ToBytesSerialiser (uk.gov.gchq.gaffer.serialisation.ToBytesSerialiser)3 TypeDefinition (uk.gov.gchq.gaffer.store.schema.TypeDefinition)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 BatchWriter (org.apache.accumulo.core.client.BatchWriter)2 MutationsRejectedException (org.apache.accumulo.core.client.MutationsRejectedException)2 Mutation (org.apache.accumulo.core.data.Mutation)2 PartialKey (org.apache.accumulo.core.data.PartialKey)2 ColumnVisibility (org.apache.accumulo.core.security.ColumnVisibility)2 GafferRuntimeException (uk.gov.gchq.gaffer.core.exception.GafferRuntimeException)2