Search in sources :

Example 31 with SerialisationException

use of uk.gov.gchq.gaffer.exception.SerialisationException in project Gaffer by gchq.

the class AbstractCoreKeyAccumuloElementConverter method getEdgeFromKey.

protected Edge getEdgeFromKey(final Key key, final Map<String, String> options) throws AccumuloElementConversionException {
    final byte[][] result = new byte[3][];
    final boolean directed = getSourceAndDestinationFromRowKey(key.getRowData().getBackingArray(), result, options);
    String group;
    try {
        group = new String(key.getColumnFamilyData().getBackingArray(), CommonConstants.UTF_8);
    } catch (final UnsupportedEncodingException e) {
        throw new AccumuloElementConversionException(e.getMessage(), e);
    }
    try {
        final Edge edge = new Edge(group, getVertexSerialiser().deserialise(result[0]), getVertexSerialiser().deserialise(result[1]), directed);
        addPropertiesToElement(edge, key);
        return edge;
    } catch (final SerialisationException e) {
        throw new AccumuloElementConversionException("Failed to re-create Edge from key", e);
    }
}
Also used : SerialisationException(uk.gov.gchq.gaffer.exception.SerialisationException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Edge(uk.gov.gchq.gaffer.data.element.Edge) AccumuloElementConversionException(uk.gov.gchq.gaffer.accumulostore.key.exception.AccumuloElementConversionException)

Example 32 with SerialisationException

use of uk.gov.gchq.gaffer.exception.SerialisationException in project Gaffer by gchq.

the class AbstractCoreKeyAccumuloElementConverter method getPropertiesAsBytesFromColumnQualifier.

@Override
public byte[] getPropertiesAsBytesFromColumnQualifier(final String group, final byte[] bytes, final int numProps) throws AccumuloElementConversionException {
    if (numProps == 0 || bytes == null || bytes.length == 0) {
        return AccumuloStoreConstants.EMPTY_BYTES;
    }
    final SchemaElementDefinition elementDefinition = schema.getElement(group);
    if (null == elementDefinition) {
        throw new AccumuloElementConversionException("No SchemaElementDefinition found for group " + group + ", is this group in your schema or do your table iterators need updating?");
    }
    if (numProps == elementDefinition.getProperties().size()) {
        return bytes;
    }
    int lastDelimiter = 0;
    final int arrayLength = bytes.length;
    long currentPropLength;
    int propIndex = 0;
    while (propIndex < numProps && lastDelimiter < arrayLength) {
        final int numBytesForLength = CompactRawSerialisationUtils.decodeVIntSize(bytes[lastDelimiter]);
        final byte[] length = new byte[numBytesForLength];
        System.arraycopy(bytes, lastDelimiter, length, 0, numBytesForLength);
        try {
            currentPropLength = CompactRawSerialisationUtils.readLong(length);
        } catch (final SerialisationException e) {
            throw new AccumuloElementConversionException("Exception reading length of property", e);
        }
        lastDelimiter += numBytesForLength;
        if (currentPropLength > 0) {
            lastDelimiter += currentPropLength;
        }
        propIndex++;
    }
    final byte[] propertyBytes = new byte[lastDelimiter];
    System.arraycopy(bytes, 0, propertyBytes, 0, lastDelimiter);
    return propertyBytes;
}
Also used : SerialisationException(uk.gov.gchq.gaffer.exception.SerialisationException) SchemaElementDefinition(uk.gov.gchq.gaffer.store.schema.SchemaElementDefinition) AccumuloElementConversionException(uk.gov.gchq.gaffer.accumulostore.key.exception.AccumuloElementConversionException)

Example 33 with SerialisationException

use of uk.gov.gchq.gaffer.exception.SerialisationException in project Gaffer by gchq.

the class AvroSerialiser method serialise.

public byte[] serialise(final Object object) throws SerialisationException {
    Schema schema = ReflectData.get().getSchema(object.getClass());
    DatumWriter<Object> datumWriter = new ReflectDatumWriter<>(schema);
    DataFileWriter<Object> dataFileWriter = new DataFileWriter<>(datumWriter);
    ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
    try {
        dataFileWriter.create(schema, byteOut);
        dataFileWriter.append(object);
        dataFileWriter.flush();
    } catch (final IOException e) {
        throw new SerialisationException("Unable to serialise given object of class: " + object.getClass().getName(), e);
    } finally {
        close(dataFileWriter);
    }
    return byteOut.toByteArray();
}
Also used : SerialisationException(uk.gov.gchq.gaffer.exception.SerialisationException) Schema(org.apache.avro.Schema) DataFileWriter(org.apache.avro.file.DataFileWriter) ReflectDatumWriter(org.apache.avro.reflect.ReflectDatumWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Example 34 with SerialisationException

use of uk.gov.gchq.gaffer.exception.SerialisationException in project Gaffer by gchq.

the class AvroSerialiser method deserialise.

public <T> T deserialise(final byte[] bytes, final Class<T> clazz) throws SerialisationException {
    DatumReader<T> datumReader = new ReflectDatumReader<>();
    DataFileStream<T> in = null;
    T ret = null;
    try {
        in = new DataFileStream<>(new ByteArrayInputStream(bytes), datumReader);
        ret = in.next();
    } catch (final IOException e) {
        throw new SerialisationException("Unable to deserialise object, failed to read input bytes", e);
    } finally {
        close(in);
    }
    return ret;
}
Also used : SerialisationException(uk.gov.gchq.gaffer.exception.SerialisationException) ByteArrayInputStream(java.io.ByteArrayInputStream) IOException(java.io.IOException) ReflectDatumReader(org.apache.avro.reflect.ReflectDatumReader)

Example 35 with SerialisationException

use of uk.gov.gchq.gaffer.exception.SerialisationException in project Gaffer by gchq.

the class IntegerFreqMapSerialiser method serialise.

@Override
public byte[] serialise(final IntegerFreqMap map) throws SerialisationException {
    Set<Entry<String, Integer>> entrySet = map.entrySet();
    StringBuilder builder = new StringBuilder();
    int last = entrySet.size() - 1;
    int start = 0;
    for (final Entry<String, Integer> entry : entrySet) {
        Integer value = entry.getValue();
        if (value == null) {
            continue;
        }
        builder.append(entry.getKey() + SEPERATOR + value);
        ++start;
        if (start > last) {
            break;
        }
        builder.append(SEPERATOR);
    }
    try {
        return builder.toString().getBytes(CommonConstants.ISO_8859_1_ENCODING);
    } catch (UnsupportedEncodingException e) {
        throw new SerialisationException(e.getMessage(), e);
    }
}
Also used : Entry(java.util.Map.Entry) SerialisationException(uk.gov.gchq.gaffer.exception.SerialisationException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Aggregations

SerialisationException (uk.gov.gchq.gaffer.exception.SerialisationException)39 IOException (java.io.IOException)12 UnsupportedEncodingException (java.io.UnsupportedEncodingException)9 AccumuloElementConversionException (uk.gov.gchq.gaffer.accumulostore.key.exception.AccumuloElementConversionException)8 ByteArrayOutputStream (java.io.ByteArrayOutputStream)7 RangeFactoryException (uk.gov.gchq.gaffer.accumulostore.key.exception.RangeFactoryException)4 SchemaElementDefinition (uk.gov.gchq.gaffer.store.schema.SchemaElementDefinition)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 Properties (uk.gov.gchq.gaffer.data.element.Properties)3 Serialisation (uk.gov.gchq.gaffer.serialisation.Serialisation)3 StoreException (uk.gov.gchq.gaffer.store.StoreException)3 TypeDefinition (uk.gov.gchq.gaffer.store.schema.TypeDefinition)3 HyperLogLogPlus (com.clearspring.analytics.stream.cardinality.HyperLogLogPlus)2 DataInputStream (java.io.DataInputStream)2 DataOutputStream (java.io.DataOutputStream)2 Entry (java.util.Map.Entry)2 Key (org.apache.accumulo.core.data.Key)2 Test (org.junit.Test)2 RoaringBitmap (org.roaringbitmap.RoaringBitmap)2 Entity (uk.gov.gchq.gaffer.data.element.Entity)2