Search in sources :

Example 16 with SerialisationException

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

the class ReservoirLongsUnionSerialiserTest method testSerialiser.

private void testSerialiser(final ReservoirLongsUnion union) {
    final ReservoirLongsSketch result = union.getResult();
    final boolean resultIsNull = result == null;
    long[] sample = new long[] {};
    if (!resultIsNull) {
        sample = union.getResult().getSamples();
    }
    final byte[] unionSerialised;
    try {
        unionSerialised = SERIALISER.serialise(union);
    } catch (final SerialisationException exception) {
        fail("A SerialisationException occurred");
        return;
    }
    final ReservoirLongsUnion unionDeserialised;
    try {
        unionDeserialised = SERIALISER.deserialise(unionSerialised);
    } catch (final SerialisationException exception) {
        fail("A SerialisationException occurred");
        return;
    }
    final ReservoirLongsSketch deserialisedResult = unionDeserialised.getResult();
    if (deserialisedResult == null) {
        assertTrue(resultIsNull);
    } else {
        assertArrayEquals(sample, unionDeserialised.getResult().getSamples());
    }
}
Also used : SerialisationException(uk.gov.gchq.gaffer.exception.SerialisationException) ReservoirLongsUnion(com.yahoo.sketches.sampling.ReservoirLongsUnion) ReservoirLongsSketch(com.yahoo.sketches.sampling.ReservoirLongsSketch)

Example 17 with SerialisationException

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

the class UnionSerialiserTest method testSerialiser.

private void testSerialiser(final Union union) {
    final double estimate = union.getResult().getEstimate();
    final byte[] unionSerialised;
    try {
        unionSerialised = SERIALISER.serialise(union);
    } catch (final SerialisationException exception) {
        fail("A SerialisationException occurred");
        return;
    }
    final Union unionDeserialised;
    try {
        unionDeserialised = SERIALISER.deserialise(unionSerialised);
    } catch (final SerialisationException exception) {
        fail("A SerialisationException occurred");
        return;
    }
    assertEquals(estimate, unionDeserialised.getResult().getEstimate(), DELTA);
}
Also used : SerialisationException(uk.gov.gchq.gaffer.exception.SerialisationException) Union(com.yahoo.sketches.theta.Union)

Example 18 with SerialisationException

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

the class RoaringBitmapUtils method upConvertSerialisedForm.

public static byte[] upConvertSerialisedForm(final byte[] serialisedBitmap) throws SerialisationException {
    DataInputStream input = new DataInputStream(new ByteArrayInputStream(serialisedBitmap));
    int cookie;
    try {
        cookie = Integer.reverseBytes(input.readInt());
    } catch (final IOException e) {
        throw new SerialisationException("I failed to read the bitmap version cookie", e);
    }
    if (cookie == VERSION_ZERO_ONE_FIVE_TO_ZERO_THREE_SEVEN_SERIAL_COOKIE) {
        try {
            ByteArrayOutputStream baos = new ByteArrayOutputStream(128);
            DataOutputStream out = new DataOutputStream(baos);
            out.write(VERSION_ZERO_FOUR_ZERO_TO_SIX_THIRTY_FIVE_NO_RUNCONTAINER_COOKIE_BYTES);
            int sizeInt = input.readInt();
            int size = Integer.reverseBytes(sizeInt);
            //Doesn't need to be reversed (already read as reversed)
            out.writeInt(sizeInt);
            int startOffSet = 4 + 4 + 4 * size + 4 * size;
            //Need to extract the cardinalities to calculate the offsets
            //Bitmap containers are a fixed size (BITMAP_CONTAINER_SIZE) and are used when the cardinality is greater than 4096
            //Array containers are variable size (cardinality * 2)
            int[] cardinalities = new int[size];
            for (int i = 0; i < size; ++i) {
                //Read and write the key (Don't need to use this just copy it across)
                out.writeShort(input.readShort());
                //Get the cardinality and store it for calculating offsets
                short cardShort = input.readShort();
                cardinalities[i] = 1 + (0xFFFF & Short.reverseBytes(cardShort));
                //Doesn't need reversing (already read as reversed)
                out.writeShort(cardShort);
            }
            int currentOffSet = startOffSet;
            for (int i = 0; i < size; ++i) {
                out.writeInt(Integer.reverseBytes(currentOffSet));
                if (cardinalities[i] > MAX_ARRAY_CONTAINER_SIZE) {
                    //It's a bitmap container
                    currentOffSet += BITMAP_CONTAINER_SIZE;
                } else {
                    //It's an array container
                    currentOffSet += (cardinalities[i] * 2);
                }
            }
            int expectedNumContainerBytes = currentOffSet - startOffSet;
            //Write out all the container data
            int numContainerBytes = 0;
            int b;
            while ((b = input.read()) != -1) {
                out.write(b);
                ++numContainerBytes;
            }
            out.flush();
            if (numContainerBytes != expectedNumContainerBytes) {
                throw new SerialisationException("I failed to convert roaring bitmap from pre 0.4.0 version");
            }
            return baos.toByteArray();
        } catch (SerialisationException e) {
            throw (e);
        } catch (final IOException e) {
            throw new SerialisationException("IOException: I failed to convert roaring bitmap from pre 0.4.0 version", e);
        }
    } else if (cookie == VERSION_ZERO_FOUR_ZERO_TO_SIX_THRIRTY_FIVE_NO_RUNCONTAINER_COOKIE || (cookie & 0xFFFF) == VERSION_ZERO_FIVE_ZERO_TO_SIX_THIRTY_FIVE_COOKIE) {
        return serialisedBitmap;
    } else {
        throw new SerialisationException("I failed to find a known roaring bitmap cookie (cookie = " + cookie + ")");
    }
}
Also used : SerialisationException(uk.gov.gchq.gaffer.exception.SerialisationException) ByteArrayInputStream(java.io.ByteArrayInputStream) DataOutputStream(java.io.DataOutputStream) IOException(java.io.IOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) DataInputStream(java.io.DataInputStream)

Example 19 with SerialisationException

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

the class ProxyStore method executeOpChainViaUrl.

protected <OUTPUT> OUTPUT executeOpChainViaUrl(final OperationChain<OUTPUT> operationChain, final Context context) throws OperationException {
    final String opChainJson;
    try {
        opChainJson = new String(jsonSerialiser.serialise(operationChain), CommonConstants.UTF_8);
    } catch (final UnsupportedEncodingException | SerialisationException e) {
        throw new OperationException("Unable to serialise operation chain into JSON.", e);
    }
    final URL url = getProperties().getGafferUrl("graph/doOperation");
    try {
        return doPost(url, opChainJson, operationChain.getOutputTypeReference(), context);
    } catch (final StoreException e) {
        throw new OperationException(e.getMessage(), e);
    }
}
Also used : SerialisationException(uk.gov.gchq.gaffer.exception.SerialisationException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) OperationException(uk.gov.gchq.gaffer.operation.OperationException) URL(java.net.URL) StoreException(uk.gov.gchq.gaffer.store.StoreException)

Example 20 with SerialisationException

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

the class JavaSerialiser method serialise.

public byte[] serialise(final Object object) throws SerialisationException {
    ObjectOutputStream out = null;
    ByteArrayOutputStream byteOut = null;
    try {
        byteOut = new ByteArrayOutputStream();
        out = new ObjectOutputStream(byteOut);
        out.writeObject(object);
        return byteOut.toByteArray();
    } catch (final IOException e) {
        throw new SerialisationException("Unable to serialise given object of class: " + object.getClass().getName() + ", does it implement the serializable interface?", e);
    } finally {
        close(out);
        close(byteOut);
    }
}
Also used : SerialisationException(uk.gov.gchq.gaffer.exception.SerialisationException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ObjectOutputStream(java.io.ObjectOutputStream)

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