Class MultiSerialiser
- java.lang.Object
-
- uk.gov.gchq.gaffer.serialisation.implementation.MultiSerialiser
-
- All Implemented Interfaces:
Serializable
,Serialiser<Object,byte[]>
,ToBytesSerialiser<Object>
public class MultiSerialiser extends Object implements ToBytesSerialiser<Object>
This class is used to serialise and deserialise multiple object types. This overcomes the limitation of a graph schema requiring all vertex types to use the same serialiser.The serialiser used is stored at the first byte of the serialised byte[] The maximum number of different serialiser keys that can be used is limited to 256 (the size of a byte).
byte array [42,*,*,*,*,*] ^ ^ serialiser byte key
When multiple serialisers are used that operate on the same value class then the last serialiser in the list will be used for serialisation; this happens regardless of the key's natural-ordering. In the example Integers will be serialised with no.8.
For deserialising the correct serialiser is always chosen based on the byte key at the start of the byte[].In the below example, the MultiSerialiser has 3 Integer serialisers. This has backwards compatibility to read and deserialise each byte array regardless of the keyed serialiser used at the time of writing, as long as its on the classpath. However when re-serialising the Integer object, only the last serialiser will be used (key no.8, CompactRawIntegerSerialiser)
This allows the MultiSerialiser to be updated with improvements and maintain backwards compatibility. This also allows serialisation to be updated before a Serialiser is deprecated and removed.Json { "serialisers" : [ { "key" : 0, "serialiser" : { "class" : "uk.gov.gchq.gaffer.serialisation.implementation.StringSerialiser", "charset" : "UTF-8" }, "valueClass" : "java.lang.String" }, { "key" : 7, "serialiser" : { "class" : "old.deprecated.IntegerSerialiser", "charset" : "ISO-8859-1" }, "valueClass" : "java.lang.Integer" }, { "key" : 24, "serialiser" : { "class" : "uk.gov.gchq.gaffer.serialisation.implementation.ordered.OrderedIntegerSerialiser" }, "valueClass" : "java.lang.Integer" }, { "key" : 8, "serialiser" : { "class" : "uk.gov.gchq.gaffer.serialisation.implementation.raw.CompactRawIntegerSerialiser" }, "valueClass" : "java.lang.Integer" } ] }
- See Also:
- Serialized Form
-
-
Field Summary
-
Fields inherited from interface uk.gov.gchq.gaffer.serialisation.ToBytesSerialiser
EMPTY_BYTES
-
-
Constructor Summary
Constructors Constructor Description MultiSerialiser()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description MultiSerialiser
addSerialiser(byte key, ToBytesSerialiser serialiser, Class aClass)
boolean
canHandle(Class clazz)
Check whether the serialiser can serialise a particular class.Object
deserialise(byte[] bytes)
Deserialise an array of bytes into the original object.Object
deserialiseEmpty()
Handle an empty byte array and reconstruct an appropriate representation in T form.boolean
equals(Object obj)
List<MultiSerialiserStorage.SerialiserDetail>
getSerialisers()
int
hashCode()
boolean
isConsistent()
Indicates whether the serialisation process produces a predictable, consistent OUTPUT, from a given INPUT, ie the same object should always serialise in the same way for this to be true.boolean
preservesObjectOrdering()
Indicates whether the serialisation process preserves the ordering of the T, i.e.byte[]
serialise(Object object)
Serialise some object and returns the raw bytes of the serialised form.void
setSerialisers(List<MultiSerialiserStorage.SerialiserDetail> serialisers)
-
Methods inherited from class java.lang.Object
getClass, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface uk.gov.gchq.gaffer.serialisation.ToBytesSerialiser
deserialise, serialiseNull
-
-
-
-
Method Detail
-
setSerialisers
public void setSerialisers(List<MultiSerialiserStorage.SerialiserDetail> serialisers) throws GafferCheckedException
- Throws:
GafferCheckedException
-
addSerialiser
public MultiSerialiser addSerialiser(byte key, ToBytesSerialiser serialiser, Class aClass) throws GafferCheckedException
- Throws:
GafferCheckedException
-
getSerialisers
public List<MultiSerialiserStorage.SerialiserDetail> getSerialisers()
-
serialise
public byte[] serialise(Object object) throws SerialisationException
Description copied from interface:ToBytesSerialiser
Serialise some object and returns the raw bytes of the serialised form.- Specified by:
serialise
in interfaceSerialiser<Object,byte[]>
- Specified by:
serialise
in interfaceToBytesSerialiser<Object>
- Parameters:
object
- the object to be serialised- Returns:
- byte[] the serialised bytes
- Throws:
SerialisationException
- if the object fails to serialise
-
deserialise
public Object deserialise(byte[] bytes) throws SerialisationException
Description copied from interface:ToBytesSerialiser
Deserialise an array of bytes into the original object.- Specified by:
deserialise
in interfaceSerialiser<Object,byte[]>
- Specified by:
deserialise
in interfaceToBytesSerialiser<Object>
- Parameters:
bytes
- the bytes to deserialise- Returns:
- T the deserialised object
- Throws:
SerialisationException
- if the object fails to deserialiseNote that this implementation is less efficient than using deserialise with an offset and a length, but may still be used if necessary. It has been marked deprecated but will not be removed as it is needed in the Serialiser interface.
- See Also:
ToBytesSerialiser.deserialise(byte[], int, int)
-
deserialiseEmpty
public Object deserialiseEmpty() throws SerialisationException
Description copied from interface:ToBytesSerialiser
Handle an empty byte array and reconstruct an appropriate representation in T form.- Specified by:
deserialiseEmpty
in interfaceSerialiser<Object,byte[]>
- Specified by:
deserialiseEmpty
in interfaceToBytesSerialiser<Object>
- Returns:
- T the deserialised object
- Throws:
SerialisationException
- if the object fails to deserialise
-
preservesObjectOrdering
public boolean preservesObjectOrdering()
Description copied from interface:ToBytesSerialiser
Indicates whether the serialisation process preserves the ordering of the T, i.e. if x and y are objects of class T, and x is less than y, then this method should return true if the serialised form of x is guaranteed to be less than the serialised form of y (using the standard ordering of byte arrays). If T is not Comparable then this test makes no sense and false should be returned.- Specified by:
preservesObjectOrdering
in interfaceSerialiser<Object,byte[]>
- Specified by:
preservesObjectOrdering
in interfaceToBytesSerialiser<Object>
- Returns:
- true if the serialisation will preserve the order of the T, otherwise false.
-
isConsistent
public boolean isConsistent()
Description copied from interface:Serialiser
Indicates whether the serialisation process produces a predictable, consistent OUTPUT, from a given INPUT, ie the same object should always serialise in the same way for this to be true.- Specified by:
isConsistent
in interfaceSerialiser<Object,byte[]>
- Returns:
- true if serialisation is consistent for a given object, otherwise false.
-
canHandle
public boolean canHandle(Class clazz)
Description copied from interface:Serialiser
Check whether the serialiser can serialise a particular class.//Warning: Due to erasure you may get false positives. e.g.
Set<Foo> set = Sets.newHashSet(); boolean b = new Serialiser<Set<Bar>, byte[]>().canHandle(set.getClass()); b will incorrectly be true because of type erasure between Set<Foo> Set<Bar>
- Specified by:
canHandle
in interfaceSerialiser<Object,byte[]>
- Parameters:
clazz
- the object class to serialise- Returns:
- boolean true if it can be handled
-
-