Search in sources :

Example 11 with ToBytesSerialiser

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

the class MultiSerialiser method deserialise.

@Override
public Object deserialise(final byte[] bytes) throws SerialisationException {
    try {
        byte keyByte = bytes[0];
        ToBytesSerialiser serialiser = nullCheck(supportedSerialisers.getSerialiserFromKey(keyByte));
        return serialiser.deserialise(bytes, 1, bytes.length - 1);
    } catch (final SerialisationException e) {
        // re-throw SerialisationException
        throw e;
    } catch (final Exception e) {
        // wraps other exceptions.
        throw new SerialisationException(e.getMessage(), e);
    }
}
Also used : SerialisationException(uk.gov.gchq.gaffer.exception.SerialisationException) ToBytesSerialiser(uk.gov.gchq.gaffer.serialisation.ToBytesSerialiser) GafferCheckedException(uk.gov.gchq.gaffer.core.exception.GafferCheckedException) SerialisationException(uk.gov.gchq.gaffer.exception.SerialisationException)

Example 12 with ToBytesSerialiser

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

the class StoreTest method shouldFindInvalidSerialiser.

@Test
public void shouldFindInvalidSerialiser() throws Exception {
    final Class<StringToStringSerialiser> invalidSerialiserClass = StringToStringSerialiser.class;
    Schema invalidSchema = new Schema.Builder().edge(TestGroups.EDGE, new SchemaEdgeDefinition.Builder().source("string").destination("invalidString").directed("true").property(TestPropertyNames.PROP_1, "string").property(TestPropertyNames.PROP_2, "string").build()).type("string", new TypeDefinition.Builder().clazz(String.class).serialiser(new StringSerialiser()).build()).type("invalidString", new TypeDefinition.Builder().clazz(String.class).serialiser(invalidSerialiserClass.newInstance()).build()).type("true", Boolean.class).build();
    final StoreProperties properties = mock(StoreProperties.class);
    given(properties.getJobExecutorThreadCount()).willReturn(1);
    final Class<ToBytesSerialiser> validSerialiserInterface = ToBytesSerialiser.class;
    try {
        new StoreImpl() {

            @Override
            protected Class<? extends Serialiser> getRequiredParentSerialiserClass() {
                return validSerialiserInterface;
            }
        }.initialise("graphId", invalidSchema, properties);
        fail("Should have thrown exception");
    } catch (final SchemaException e) {
        assertTrue(e.getMessage().contains(invalidSerialiserClass.getSimpleName()));
    }
}
Also used : SchemaException(uk.gov.gchq.gaffer.data.elementdefinition.exception.SchemaException) StringSerialiser(uk.gov.gchq.gaffer.serialisation.implementation.StringSerialiser) StringToStringSerialiser(uk.gov.gchq.gaffer.serialisation.implementation.tostring.StringToStringSerialiser) JSONSerialiser(uk.gov.gchq.gaffer.jsonserialisation.JSONSerialiser) Serialiser(uk.gov.gchq.gaffer.serialisation.Serialiser) StringSerialiser(uk.gov.gchq.gaffer.serialisation.implementation.StringSerialiser) ToBytesSerialiser(uk.gov.gchq.gaffer.serialisation.ToBytesSerialiser) StringToStringSerialiser(uk.gov.gchq.gaffer.serialisation.implementation.tostring.StringToStringSerialiser) Schema(uk.gov.gchq.gaffer.store.schema.Schema) GetSchema(uk.gov.gchq.gaffer.store.operation.GetSchema) StringToStringSerialiser(uk.gov.gchq.gaffer.serialisation.implementation.tostring.StringToStringSerialiser) TypeDefinition(uk.gov.gchq.gaffer.store.schema.TypeDefinition) SchemaEdgeDefinition(uk.gov.gchq.gaffer.store.schema.SchemaEdgeDefinition) ToBytesSerialiser(uk.gov.gchq.gaffer.serialisation.ToBytesSerialiser) Test(org.junit.jupiter.api.Test)

Example 13 with ToBytesSerialiser

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

the class ByteEntityRangeFactory method getKeyFromEdgeId.

protected Key getKeyFromEdgeId(final Object source, final Object destination, final boolean directed, final boolean endKey) throws RangeFactoryException {
    final ToBytesSerialiser vertexSerialiser = (ToBytesSerialiser) schema.getVertexSerialiser();
    final byte directionFlag = directed ? ByteEntityPositions.CORRECT_WAY_DIRECTED_EDGE : ByteEntityPositions.UNDIRECTED_EDGE;
    byte[] sourceValue;
    try {
        sourceValue = ByteArrayEscapeUtils.escape(vertexSerialiser.serialise(source));
    } catch (final SerialisationException e) {
        throw new RangeFactoryException("Failed to serialise Edge Source", e);
    }
    byte[] destinationValue;
    try {
        destinationValue = ByteArrayEscapeUtils.escape(vertexSerialiser.serialise(destination));
    } catch (final SerialisationException e) {
        throw new RangeFactoryException("Failed to serialise Edge Destination", e);
    }
    byte[] key = getKey(endKey, directionFlag, sourceValue, destinationValue);
    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 14 with ToBytesSerialiser

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

the class PropertiesWalkthrough method getSerialisers.

private static List<Serialiser> getSerialisers() {
    final List<Serialiser> serialisers = getSubClassInstances(Serialiser.class);
    serialisers.removeIf(c -> {
        boolean contains = false;
        for (final ToBytesSerialiser serialiser : TO_BYTES_SERIALISERS) {
            if (serialiser.getClass().equals(c.getClass())) {
                contains = true;
                break;
            }
        }
        return contains;
    });
    return serialisers;
}
Also used : Serialiser(uk.gov.gchq.gaffer.serialisation.Serialiser) JavaSerialiser(uk.gov.gchq.gaffer.serialisation.implementation.JavaSerialiser) ToBytesSerialiser(uk.gov.gchq.gaffer.serialisation.ToBytesSerialiser) AvroSerialiser(uk.gov.gchq.gaffer.serialisation.AvroSerialiser) ToBytesSerialiser(uk.gov.gchq.gaffer.serialisation.ToBytesSerialiser)

Example 15 with ToBytesSerialiser

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

the class MapSerialiser method deserialise.

@Override
public Map<? extends Object, ? extends Object> deserialise(final byte[] bytes) throws SerialisationException {
    Map map;
    if (null == getMapClass()) {
        map = new HashMap<>();
    } else {
        try {
            map = getMapClass().newInstance();
        } catch (final IllegalAccessException | IllegalArgumentException | SecurityException | InstantiationException e) {
            throw new SerialisationException("Failed to create map instance" + e.getMessage(), e);
        }
    }
    final int arrayLength = bytes.length;
    int carriage = 0;
    while (carriage < arrayLength) {
        final ToBytesSerialiser keySerialiser = getKeySerialiser();
        final ToBytesSerialiser valueSerialiser = getValueSerialiser();
        checkSerialiers(keySerialiser, valueSerialiser);
        LengthValueBytesSerialiserUtil.ObjectCarriage c = LengthValueBytesSerialiserUtil.deserialiseNextObject(keySerialiser, carriage, bytes);
        LengthValueBytesSerialiserUtil.ObjectCarriage c2 = LengthValueBytesSerialiserUtil.deserialiseNextObject(valueSerialiser, c.getCarriage(), bytes);
        map.put(c.getObject(), c2.getObject());
        carriage = c2.getCarriage();
    }
    return map;
}
Also used : SerialisationException(uk.gov.gchq.gaffer.exception.SerialisationException) LengthValueBytesSerialiserUtil(uk.gov.gchq.gaffer.serialisation.util.LengthValueBytesSerialiserUtil) ToBytesSerialiser(uk.gov.gchq.gaffer.serialisation.ToBytesSerialiser) HashMap(java.util.HashMap) Map(java.util.Map)

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