Search in sources :

Example 1 with ToBytesSerialiser

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

the class CompactRawLongSerialiserTest method test.

private void test(final long value) throws SerialisationException {
    // Given When
    final byte[] b = serialiser.serialise(value);
    final Object o = ((ToBytesSerialiser) serialiser).deserialise(b, 0, b.length);
    // Then
    assertEquals(Long.class, o.getClass());
    assertEquals(value, o);
    final ByteArrayOutputStream stream = new ByteArrayOutputStream();
    CompactRawSerialisationUtils.write(value, new DataOutputStream(stream));
    final long result = CompactRawSerialisationUtils.read(new DataInputStream(new ByteArrayInputStream(stream.toByteArray())));
    assertEquals(result, value);
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) DataOutputStream(java.io.DataOutputStream) ToBytesSerialiser(uk.gov.gchq.gaffer.serialisation.ToBytesSerialiser) ByteArrayOutputStream(org.apache.commons.io.output.ByteArrayOutputStream) DataInputStream(java.io.DataInputStream)

Example 2 with ToBytesSerialiser

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

the class MapSerialiser method serialise.

@Override
public byte[] serialise(final Map object) throws SerialisationException {
    LengthValueBytesSerialiserUtil.LengthValueBuilder builder = new LengthValueBytesSerialiserUtil.LengthValueBuilder();
    try {
        for (final Object o : object.entrySet()) {
            if (o instanceof Map.Entry) {
                Map.Entry entry = (Map.Entry) o;
                final ToBytesSerialiser keySerialiser = getKeySerialiser();
                final ToBytesSerialiser valueSerialiser = getValueSerialiser();
                checkSerialiers(keySerialiser, valueSerialiser);
                builder.appendLengthValueFromObjectToByteStream(keySerialiser, entry.getKey());
                builder.appendLengthValueFromObjectToByteStream(valueSerialiser, entry.getValue());
            } else {
                throw new SerialisationException("Was not able to process EntrySet of Map");
            }
        }
    } catch (final IOException e) {
        throw new SerialisationException(e.getMessage(), e);
    }
    return builder.toArray();
}
Also used : SerialisationException(uk.gov.gchq.gaffer.exception.SerialisationException) LengthValueBytesSerialiserUtil(uk.gov.gchq.gaffer.serialisation.util.LengthValueBytesSerialiserUtil) ToBytesSerialiser(uk.gov.gchq.gaffer.serialisation.ToBytesSerialiser) IOException(java.io.IOException) HashMap(java.util.HashMap) Map(java.util.Map)

Example 3 with ToBytesSerialiser

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

the class MultiSerialiserStorage method canHandle.

/**
 * @param handleClass class to check
 * @return {@link ToBytesSerialiser#canHandle(Class)}
 * @see ToBytesSerialiser
 */
public boolean canHandle(final Class handleClass) {
    boolean rtn = false;
    final Byte key = classToKey.get(handleClass);
    if (null != key) {
        Serialiser serialiser = keyToSerialiser.get(key);
        rtn = null != serialiser && serialiser.canHandle(handleClass);
    }
    return rtn;
}
Also used : ToBytesSerialiser(uk.gov.gchq.gaffer.serialisation.ToBytesSerialiser) Serialiser(uk.gov.gchq.gaffer.serialisation.Serialiser)

Example 4 with ToBytesSerialiser

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

the class MultiSerialiserStorageTest method shouldUpdateToNewerValueToSerialiser.

@Test
public void shouldUpdateToNewerValueToSerialiser() throws Exception {
    // give
    byte serialiserEncoding = BYTE + 1;
    // when
    mss.put(serialiserEncoding, SERIALISER_CLASS2, SUPPORTED_CLASS);
    mss.put(BYTE, SERIALISER_CLASS, SUPPORTED_CLASS);
    // then
    checkBasicPut();
    assertEquals(BYTE, (byte) mss.getKeyFromValue(VALUE));
    ToBytesSerialiser actualClassFromByte2 = mss.getSerialiserFromKey(serialiserEncoding);
    assertNotNull(actualClassFromByte2, "Byte key not found");
    assertEquals(SERIALISER_CLASS2, actualClassFromByte2, "Wrong SerialiserClass returned for key");
    ToBytesSerialiser actualClassFromValue2 = mss.getSerialiserFromValue(Integer.MAX_VALUE);
    assertNotNull(actualClassFromValue2, "Value class not found");
    assertEquals(SERIALISER_CLASS, actualClassFromValue2, "Wrong SerialiserClass, should have updated to newer SerialiserClass");
}
Also used : ToBytesSerialiser(uk.gov.gchq.gaffer.serialisation.ToBytesSerialiser) Test(org.junit.jupiter.api.Test)

Example 5 with ToBytesSerialiser

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

the class MultiSerialiserStorageTest method shouldNotRetainOldSerialiserWhenKeyIsOverWritten.

@Test
public void shouldNotRetainOldSerialiserWhenKeyIsOverWritten() throws Exception {
    // when
    mss.put(BYTE, SERIALISER_CLASS, SUPPORTED_CLASS);
    mss.put(BYTE, SERIALISER_CLASS2, SUPPORTED_CLASS);
    // then
    assertNotNull(mss.getKeyFromValue(VALUE));
    assertEquals((Object) BYTE, mss.getKeyFromValue(VALUE), "Wrong key for value");
    ToBytesSerialiser actualClassFromByte = mss.getSerialiserFromKey(BYTE);
    assertNotNull(actualClassFromByte, "Byte key not found");
    assertEquals(SERIALISER_CLASS2, actualClassFromByte, "Wrong new SerialiserClass returned for key");
    ToBytesSerialiser actualClassFromValue = mss.getSerialiserFromValue(Integer.MAX_VALUE);
    assertNotNull(actualClassFromValue, "Value class not found");
    assertEquals(SERIALISER_CLASS2, actualClassFromValue, "Wrong new SerialiserClass returned for value class");
}
Also used : ToBytesSerialiser(uk.gov.gchq.gaffer.serialisation.ToBytesSerialiser) Test(org.junit.jupiter.api.Test)

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