Cleanup AbstractType/TypeSerializer classes

patch by slebresne; reviewed by carlyeks for CASSANDRA-5744
This commit is contained in:
Sylvain Lebresne 2013-07-12 15:25:48 +02:00
parent 829688f9eb
commit daff1fce0f
56 changed files with 175 additions and 764 deletions

View File

@ -1,4 +1,9 @@
2.0
2.0.0-beta2
* Allow nodetool with no args, and with help to run without a server (CASSANDRA-5734)
* Cleanup AbstractType/TypeSerializer classes (CASSANDRA-5744)
2.0.0-beta1
* Removed on-heap row cache (CASSANDRA-5348)
* use nanotime consistently for node-local timeouts (CASSANDRA-5581)
* Avoid unnecessary second pass on name-based queries (CASSANDRA-5577)
@ -73,7 +78,7 @@
* Auto paging in binary protocol (CASSANDRA-4415, 5714)
* Don't tie client side use of AbstractType to JDBC (CASSANDRA-4495)
* Adds new TimestampType to replace DateType (CASSANDRA-5723, CASSANDRA-5729)
* Allow nodetool with no args, and with help to run without a server (CASSANDRA-5734)
1.2.7
* Add timeout events to query traces (CASSANDRA-5520)

View File

@ -138,7 +138,7 @@ public abstract class Lists
{
// Collections have this small hack that validate cannot be called on a serialized object,
// but compose does the validation (so we're fine).
List<?> l = type.compose(value);
List<?> l = (List<?>)type.compose(value);
List<ByteBuffer> elements = new ArrayList<ByteBuffer>(l.size());
for (Object element : l)
elements.add(type.elements.decompose(element));

View File

@ -155,7 +155,7 @@ public abstract class Maps
{
// Collections have this small hack that validate cannot be called on a serialized object,
// but compose does the validation (so we're fine).
Map<?, ?> m = type.compose(value);
Map<?, ?> m = (Map<?, ?>)type.compose(value);
Map<ByteBuffer, ByteBuffer> map = new LinkedHashMap<ByteBuffer, ByteBuffer>(m.size());
for (Map.Entry<?, ?> entry : m.entrySet())
map.put(type.keys.decompose(entry.getKey()), type.values.decompose(entry.getValue()));

View File

@ -148,7 +148,7 @@ public abstract class Sets
{
// Collections have this small hack that validate cannot be called on a serialized object,
// but compose does the validation (so we're fine).
Set<?> s = type.compose(value);
Set<?> s = (Set<?>)type.compose(value);
Set<ByteBuffer> elements = new LinkedHashSet<ByteBuffer>(s.size());
for (Object element : s)
elements.add(type.elements.decompose(element));

View File

@ -29,11 +29,13 @@ public abstract class AbstractCommutativeType extends AbstractType<Long>
return true;
}
@Override
public Long compose(ByteBuffer bytes)
{
return CounterContext.instance().total(bytes);
}
@Override
public ByteBuffer decompose(Long value)
{
return ByteBufferUtil.bytes(value);

View File

@ -286,6 +286,7 @@ public abstract class AbstractCompositeType extends AbstractType<ByteBuffer>
return bb;
}
@Override
public void validate(ByteBuffer bytes) throws MarshalException
{
ByteBuffer bb = bytes.duplicate();
@ -319,17 +320,6 @@ public abstract class AbstractCompositeType extends AbstractType<ByteBuffer>
public abstract ByteBuffer decompose(Object... objects);
public ByteBuffer compose(ByteBuffer bytes)
{
return bytes;
}
public ByteBuffer decompose(ByteBuffer value)
{
return value;
}
@Override
public TypeSerializer<ByteBuffer> getSerializer()
{
return BytesSerializer.instance;

View File

@ -137,12 +137,25 @@ public abstract class AbstractType<T> implements Comparator<ByteBuffer>
};
}
public abstract T compose(ByteBuffer bytes);
public T compose(ByteBuffer bytes)
{
return getSerializer().deserialize(bytes);
}
public abstract ByteBuffer decompose(T value);
public ByteBuffer decompose(T value)
{
return getSerializer().serialize(value);
}
/** get a string representation of the bytes suitable for log messages */
public abstract String getString(ByteBuffer bytes);
public String getString(ByteBuffer bytes)
{
TypeSerializer<T> serializer = getSerializer();
serializer.validate(bytes);
T value = serializer.deserialize(bytes);
return value == null ? "null" : serializer.toString(value);
}
/** get a byte representation of the given string. */
public abstract ByteBuffer fromString(String source) throws MarshalException;
@ -154,7 +167,10 @@ public abstract class AbstractType<T> implements Comparator<ByteBuffer>
}
/* validate that the byte array is a valid sequence for the type we are supposed to be comparing */
public abstract void validate(ByteBuffer bytes) throws MarshalException;
public void validate(ByteBuffer bytes) throws MarshalException
{
getSerializer().validate(bytes);
}
/* Most of our internal type should override that. */
public CQL3Type asCQL3Type()

View File

@ -30,36 +30,16 @@ public class AsciiType extends AbstractType<String>
AsciiType() {} // singleton
public String getString(ByteBuffer bytes)
{
return AsciiSerializer.instance.getString(bytes);
}
public int compare(ByteBuffer o1, ByteBuffer o2)
{
return BytesType.bytesCompare(o1, o2);
}
public String compose(ByteBuffer bytes)
{
return AsciiSerializer.instance.getString(bytes);
}
public ByteBuffer decompose(String value)
{
return AsciiSerializer.instance.deserialize(value);
}
public ByteBuffer fromString(String source)
{
return decompose(source);
}
public void validate(ByteBuffer bytes) throws MarshalException
{
AsciiSerializer.instance.validate(bytes);
}
public CQL3Type asCQL3Type()
{
return CQL3Type.Native.ASCII;

View File

@ -26,59 +26,37 @@ import org.apache.cassandra.serializers.MarshalException;
public class BooleanType extends AbstractType<Boolean>
{
public static final BooleanType instance = new BooleanType();
public static final BooleanType instance = new BooleanType();
BooleanType() {} // singleton
BooleanType() {} // singleton
public Boolean compose(ByteBuffer bytes)
{
return BooleanSerializer.instance.serialize(bytes);
}
public int compare(ByteBuffer o1, ByteBuffer o2)
{
if ((o1 == null) || (o1.remaining() != 1))
return ((o2 == null) || (o2.remaining() != 1)) ? 0 : -1;
if ((o2 == null) || (o2.remaining() != 1))
return 1;
public ByteBuffer decompose(Boolean value)
{
return BooleanSerializer.instance.deserialize(value);
}
return o1.compareTo(o2);
}
public int compare(ByteBuffer o1, ByteBuffer o2)
{
if ((o1 == null) || (o1.remaining() != 1))
return ((o2 == null) || (o2.remaining() != 1)) ? 0 : -1;
if ((o2 == null) || (o2.remaining() != 1))
return 1;
public ByteBuffer fromString(String source) throws MarshalException
{
return o1.compareTo(o2);
}
if (source.isEmpty()|| source.equalsIgnoreCase(Boolean.FALSE.toString()))
return decompose(false);
public String getString(ByteBuffer bytes)
{
return BooleanSerializer.instance.getString(bytes);
}
if (source.equalsIgnoreCase(Boolean.TRUE.toString()))
return decompose(true);
public ByteBuffer fromString(String source) throws MarshalException
{
throw new MarshalException(String.format("unable to make boolean from '%s'", source));
}
if (source.isEmpty()|| source.equalsIgnoreCase(Boolean.FALSE.toString()))
return decompose(false);
public CQL3Type asCQL3Type()
{
return CQL3Type.Native.BOOLEAN;
}
if (source.equalsIgnoreCase(Boolean.TRUE.toString()))
return decompose(true);
throw new MarshalException(String.format("unable to make boolean from '%s'", source));
}
public void validate(ByteBuffer bytes) throws MarshalException
{
BooleanSerializer.instance.validate(bytes);
}
public CQL3Type asCQL3Type()
{
return CQL3Type.Native.BOOLEAN;
}
@Override
public TypeSerializer<Boolean> getSerializer()
{
return BooleanSerializer.instance;

View File

@ -32,16 +32,6 @@ public class BytesType extends AbstractType<ByteBuffer>
BytesType() {} // singleton
public ByteBuffer compose(ByteBuffer bytes)
{
return BytesSerializer.instance.serialize(bytes);
}
public ByteBuffer decompose(ByteBuffer value)
{
return BytesSerializer.instance.deserialize(value);
}
public int compare(ByteBuffer o1, ByteBuffer o2)
{
return BytesType.bytesCompare(o1, o2);
@ -55,11 +45,6 @@ public class BytesType extends AbstractType<ByteBuffer>
return ByteBufferUtil.compareUnsigned(o1, o2);
}
public String getString(ByteBuffer bytes)
{
return BytesSerializer.instance.getString(bytes);
}
public ByteBuffer fromString(String source)
{
try
@ -72,11 +57,6 @@ public class BytesType extends AbstractType<ByteBuffer>
}
}
public void validate(ByteBuffer bytes) throws MarshalException
{
BytesSerializer.instance.validate(bytes);
}
@Override
public boolean isCompatibleWith(AbstractType<?> previous)
{
@ -90,7 +70,6 @@ public class BytesType extends AbstractType<ByteBuffer>
return CQL3Type.Native.BLOB;
}
@Override
public TypeSerializer<ByteBuffer> getSerializer()
{
return BytesSerializer.instance;

View File

@ -74,16 +74,6 @@ public class ColumnToCollectionType extends AbstractType<ByteBuffer>
return t.nameComparator().compare(o1, o2);
}
public ByteBuffer compose(ByteBuffer bytes)
{
return BytesType.instance.compose(bytes);
}
public ByteBuffer decompose(ByteBuffer value)
{
return BytesType.instance.decompose(value);
}
public String getString(ByteBuffer bytes)
{
return BytesType.instance.getString(bytes);
@ -101,6 +91,7 @@ public class ColumnToCollectionType extends AbstractType<ByteBuffer>
}
}
@Override
public void validate(ByteBuffer bytes)
{
throw new UnsupportedOperationException("ColumnToCollectionType should only be used in composite types, never alone");

View File

@ -58,12 +58,6 @@ public class CounterColumnType extends AbstractCommutativeType
return ByteBufferUtil.hexToBytes(source);
}
public void validate(ByteBuffer bytes) throws MarshalException
{
if (bytes.remaining() != 8 && bytes.remaining() != 0)
throw new MarshalException(String.format("Expected 8 or 0 byte long (%d)", bytes.remaining()));
}
public CQL3Type asCQL3Type()
{
return CQL3Type.Native.COUNTER;

View File

@ -42,16 +42,6 @@ public class DateType extends AbstractType<Date>
DateType() {} // singleton
public Date compose(ByteBuffer bytes)
{
return TimestampSerializer.instance.serialize(bytes);
}
public ByteBuffer decompose(Date value)
{
return TimestampSerializer.instance.deserialize(value);
}
public int compare(ByteBuffer o1, ByteBuffer o2)
{
if (o1.remaining() == 0)
@ -66,11 +56,6 @@ public class DateType extends AbstractType<Date>
return ByteBufferUtil.compareUnsigned(o1, o2);
}
public String getString(ByteBuffer bytes)
{
return TimestampSerializer.instance.getString(bytes);
}
public ByteBuffer fromString(String source) throws MarshalException
{
// Return an empty ByteBuffer for an empty string.
@ -134,11 +119,6 @@ public class DateType extends AbstractType<Date>
return false;
}
public void validate(ByteBuffer bytes) throws MarshalException
{
TimestampSerializer.instance.validate(bytes);
}
public TypeSerializer<Date> getSerializer()
{
return TimestampSerializer.instance;

View File

@ -46,25 +46,6 @@ public class DecimalType extends AbstractType<BigDecimal>
return compose(bb0).compareTo(compose(bb1));
}
public BigDecimal compose(ByteBuffer bytes)
{
return DecimalSerializer.instance.serialize(bytes);
}
/**
* The bytes of the ByteBuffer are made up of 4 bytes of int containing the scale
* followed by the n bytes it takes to store a BigInteger.
*/
public ByteBuffer decompose(BigDecimal value)
{
return DecimalSerializer.instance.deserialize(value);
}
public String getString(ByteBuffer bytes)
{
return DecimalSerializer.instance.getString(bytes);
}
public ByteBuffer fromString(String source) throws MarshalException
{
// Return an empty ByteBuffer for an empty string.
@ -84,17 +65,11 @@ public class DecimalType extends AbstractType<BigDecimal>
return decompose(decimal);
}
public void validate(ByteBuffer bytes) throws MarshalException
{
DecimalSerializer.instance.validate(bytes);
}
public CQL3Type asCQL3Type()
{
return CQL3Type.Native.DECIMAL;
}
@Override
public TypeSerializer<BigDecimal> getSerializer()
{
return DecimalSerializer.instance;

View File

@ -31,16 +31,6 @@ public class DoubleType extends AbstractType<Double>
DoubleType() {} // singleton
public Double compose(ByteBuffer bytes)
{
return DoubleSerializer.instance.serialize(bytes);
}
public ByteBuffer decompose(Double value)
{
return DoubleSerializer.instance.deserialize(value);
}
public int compare(ByteBuffer o1, ByteBuffer o2)
{
if (o1.remaining() == 0)
@ -55,11 +45,6 @@ public class DoubleType extends AbstractType<Double>
return compose(o1).compareTo(compose(o2));
}
public String getString(ByteBuffer bytes)
{
return DoubleSerializer.instance.getString(bytes);
}
public ByteBuffer fromString(String source) throws MarshalException
{
// Return an empty ByteBuffer for an empty string.
@ -79,17 +64,11 @@ public class DoubleType extends AbstractType<Double>
return decompose(d);
}
public void validate(ByteBuffer bytes) throws MarshalException
{
DoubleSerializer.instance.validate(bytes);
}
public CQL3Type asCQL3Type()
{
return CQL3Type.Native.DOUBLE;
}
@Override
public TypeSerializer<Double> getSerializer()
{
return DoubleSerializer.instance;

View File

@ -336,11 +336,13 @@ public class DynamicCompositeType extends AbstractCompositeType
return cmp;
}
@Override
public Void compose(ByteBuffer bytes)
{
throw new UnsupportedOperationException();
}
@Override
public ByteBuffer decompose(Void value)
{
throw new UnsupportedOperationException();
@ -356,12 +358,12 @@ public class DynamicCompositeType extends AbstractCompositeType
throw new UnsupportedOperationException();
}
@Override
public void validate(ByteBuffer bytes)
{
throw new UnsupportedOperationException();
}
@Override
public TypeSerializer<Void> getSerializer()
{
throw new UnsupportedOperationException();

View File

@ -34,16 +34,6 @@ public class EmptyType extends AbstractType<Void>
private EmptyType() {} // singleton
public Void compose(ByteBuffer bytes)
{
return null;
}
public ByteBuffer decompose(Void value)
{
return ByteBufferUtil.EMPTY_BYTE_BUFFER;
}
public int compare(ByteBuffer o1, ByteBuffer o2)
{
return 0;
@ -62,11 +52,6 @@ public class EmptyType extends AbstractType<Void>
return ByteBufferUtil.EMPTY_BYTE_BUFFER;
}
public void validate(ByteBuffer bytes) throws MarshalException
{
EmptySerializer.instance.validate(bytes);
}
public TypeSerializer<Void> getSerializer()
{
return EmptySerializer.instance;

View File

@ -32,16 +32,6 @@ public class FloatType extends AbstractType<Float>
FloatType() {} // singleton
public Float compose(ByteBuffer bytes)
{
return FloatSerializer.instance.serialize(bytes);
}
public ByteBuffer decompose(Float value)
{
return FloatSerializer.instance.deserialize(value);
}
public int compare(ByteBuffer o1, ByteBuffer o2)
{
if (o1.remaining() == 0)
@ -56,11 +46,6 @@ public class FloatType extends AbstractType<Float>
return compose(o1).compareTo(compose(o2));
}
public String getString(ByteBuffer bytes)
{
return FloatSerializer.instance.getString(bytes);
}
public ByteBuffer fromString(String source) throws MarshalException
{
// Return an empty ByteBuffer for an empty string.
@ -78,17 +63,11 @@ public class FloatType extends AbstractType<Float>
}
}
public void validate(ByteBuffer bytes) throws MarshalException
{
FloatSerializer.instance.validate(bytes);
}
public CQL3Type asCQL3Type()
{
return CQL3Type.Native.FLOAT;
}
@Override
public TypeSerializer<Float> getSerializer()
{
return FloatSerializer.instance;

View File

@ -32,26 +32,11 @@ public class InetAddressType extends AbstractType<InetAddress>
InetAddressType() {} // singleton
public InetAddress compose(ByteBuffer bytes)
{
return InetAddressSerializer.instance.serialize(bytes);
}
public ByteBuffer decompose(InetAddress value)
{
return InetAddressSerializer.instance.deserialize(value);
}
public int compare(ByteBuffer o1, ByteBuffer o2)
{
return ByteBufferUtil.compareUnsigned(o1, o2);
}
public String getString(ByteBuffer bytes)
{
return InetAddressSerializer.instance.getString(bytes);
}
public ByteBuffer fromString(String source) throws MarshalException
{
// Return an empty ByteBuffer for an empty string.
@ -72,17 +57,11 @@ public class InetAddressType extends AbstractType<InetAddress>
return decompose(address);
}
public void validate(ByteBuffer bytes) throws MarshalException
{
InetAddressSerializer.instance.validate(bytes);
}
public CQL3Type asCQL3Type()
{
return CQL3Type.Native.INET;
}
@Override
public TypeSerializer<InetAddress> getSerializer()
{
return InetAddressSerializer.instance;

View File

@ -33,16 +33,6 @@ public class Int32Type extends AbstractType<Integer>
{
} // singleton
public Integer compose(ByteBuffer bytes)
{
return Int32Serializer.instance.serialize(bytes);
}
public ByteBuffer decompose(Integer value)
{
return Int32Serializer.instance.deserialize(value);
}
public int compare(ByteBuffer o1, ByteBuffer o2)
{
if (o1.remaining() == 0)
@ -62,11 +52,6 @@ public class Int32Type extends AbstractType<Integer>
return ByteBufferUtil.compareUnsigned(o1, o2);
}
public String getString(ByteBuffer bytes)
{
return Int32Serializer.instance.getString(bytes);
}
public ByteBuffer fromString(String source) throws MarshalException
{
// Return an empty ByteBuffer for an empty string.
@ -87,17 +72,11 @@ public class Int32Type extends AbstractType<Integer>
return decompose(int32Type);
}
public void validate(ByteBuffer bytes) throws MarshalException
{
Int32Serializer.instance.validate(bytes);
}
public CQL3Type asCQL3Type()
{
return CQL3Type.Native.INT;
}
@Override
public TypeSerializer<Integer> getSerializer()
{
return Int32Serializer.instance;

View File

@ -58,16 +58,6 @@ public final class IntegerType extends AbstractType<BigInteger>
IntegerType() {/* singleton */}
public BigInteger compose(ByteBuffer bytes)
{
return IntegerSerializer.instance.serialize(bytes);
}
public ByteBuffer decompose(BigInteger value)
{
return IntegerSerializer.instance.deserialize(value);
}
public int compare(ByteBuffer lhs, ByteBuffer rhs)
{
int lhsLen = lhs.remaining();
@ -126,11 +116,6 @@ public final class IntegerType extends AbstractType<BigInteger>
return 0;
}
public String getString(ByteBuffer bytes)
{
return IntegerSerializer.instance.getString(bytes);
}
public ByteBuffer fromString(String source) throws MarshalException
{
// Return an empty ByteBuffer for an empty string.
@ -151,17 +136,11 @@ public final class IntegerType extends AbstractType<BigInteger>
return decompose(integerType);
}
public void validate(ByteBuffer bytes) throws MarshalException
{
IntegerSerializer.instance.validate(bytes);
}
public CQL3Type asCQL3Type()
{
return CQL3Type.Native.VARINT;
}
@Override
public TypeSerializer<BigInteger> getSerializer()
{
return IntegerSerializer.instance;

View File

@ -34,16 +34,6 @@ public class LexicalUUIDType extends AbstractType<UUID>
{
} // singleton
public UUID compose(ByteBuffer bytes)
{
return UUIDSerializer.instance.serialize(bytes);
}
public ByteBuffer decompose(UUID value)
{
return UUIDSerializer.instance.deserialize(value);
}
public int compare(ByteBuffer o1, ByteBuffer o2)
{
if (o1.remaining() == 0)
@ -58,11 +48,6 @@ public class LexicalUUIDType extends AbstractType<UUID>
return UUIDGen.getUUID(o1).compareTo(UUIDGen.getUUID(o2));
}
public String getString(ByteBuffer bytes)
{
return UUIDSerializer.instance.getString(bytes);
}
public ByteBuffer fromString(String source) throws MarshalException
{
// Return an empty ByteBuffer for an empty string.
@ -79,12 +64,6 @@ public class LexicalUUIDType extends AbstractType<UUID>
}
}
public void validate(ByteBuffer bytes) throws MarshalException
{
UUIDSerializer.instance.validate(bytes);
}
@Override
public TypeSerializer<UUID> getSerializer()
{
return UUIDSerializer.instance;

View File

@ -33,7 +33,7 @@ public class ListType<T> extends CollectionType<List<T>>
private static final Map<AbstractType<?>, ListType> instances = new HashMap<AbstractType<?>, ListType>();
public final AbstractType<T> elements;
public final ListSerializer<T> composer;
public final ListSerializer<T> serializer;
public static ListType<?> getInstance(TypeParser parser) throws ConfigurationException, SyntaxException
{
@ -59,7 +59,7 @@ public class ListType<T> extends CollectionType<List<T>>
{
super(Kind.LIST);
this.elements = elements;
this.composer = ListSerializer.getInstance(elements.getSerializer());
this.serializer = ListSerializer.getInstance(elements.getSerializer());
}
public AbstractType<UUID> nameComparator()
@ -72,26 +72,9 @@ public class ListType<T> extends CollectionType<List<T>>
return elements;
}
public List<T> compose(ByteBuffer bytes)
{
return composer.serialize(bytes);
}
/**
* Layout is: {@code <n><s_1><b_1>...<s_n><b_n> }
* where:
* n is the number of elements
* s_i is the number of bytes composing the ith element
* b_i is the s_i bytes composing the ith element
*/
public ByteBuffer decompose(List<T> value)
{
return composer.deserialize(value);
}
public TypeSerializer<List<T>> getSerializer()
{
return composer;
return serializer;
}
protected void appendToStringBuilder(StringBuilder sb)

View File

@ -39,11 +39,13 @@ public class LocalByPartionerType<T extends Token> extends AbstractType<ByteBuff
this.partitioner = partitioner;
}
@Override
public ByteBuffer compose(ByteBuffer bytes)
{
throw new UnsupportedOperationException("You can't do this with a local partitioner.");
}
@Override
public ByteBuffer decompose(ByteBuffer bytes)
{
throw new UnsupportedOperationException("You can't do this with a local partitioner.");
@ -65,6 +67,7 @@ public class LocalByPartionerType<T extends Token> extends AbstractType<ByteBuff
return RowPosition.forKey(o1, partitioner).compareTo(RowPosition.forKey(o2, partitioner));
}
@Override
public void validate(ByteBuffer bytes) throws MarshalException
{
throw new IllegalStateException("You shouldn't be validating this.");

View File

@ -31,16 +31,6 @@ public class LongType extends AbstractType<Long>
LongType() {} // singleton
public Long compose(ByteBuffer bytes)
{
return LongSerializer.instance.serialize(bytes);
}
public ByteBuffer decompose(Long value)
{
return LongSerializer.instance.deserialize(value);
}
public int compare(ByteBuffer o1, ByteBuffer o2)
{
return compareLongs(o1, o2);
@ -64,11 +54,6 @@ public class LongType extends AbstractType<Long>
return ByteBufferUtil.compareUnsigned(o1, o2);
}
public String getString(ByteBuffer bytes)
{
return LongSerializer.instance.getString(bytes);
}
public ByteBuffer fromString(String source) throws MarshalException
{
// Return an empty ByteBuffer for an empty string.
@ -89,17 +74,11 @@ public class LongType extends AbstractType<Long>
return decompose(longType);
}
public void validate(ByteBuffer bytes) throws MarshalException
{
}
public CQL3Type asCQL3Type()
{
return CQL3Type.Native.BIGINT;
}
@Override
public TypeSerializer<Long> getSerializer()
{
return LongSerializer.instance;

View File

@ -36,7 +36,7 @@ public class MapType<K, V> extends CollectionType<Map<K, V>>
public final AbstractType<K> keys;
public final AbstractType<V> values;
private final MapSerializer<K, V> composer;
private final MapSerializer<K, V> serializer;
public static MapType<?, ?> getInstance(TypeParser parser) throws ConfigurationException, SyntaxException
{
@ -64,7 +64,7 @@ public class MapType<K, V> extends CollectionType<Map<K, V>>
super(Kind.MAP);
this.keys = keys;
this.values = values;
this.composer = MapSerializer.getInstance(keys.getSerializer(), values.getSerializer());
this.serializer = MapSerializer.getInstance(keys.getSerializer(), values.getSerializer());
}
public AbstractType<K> nameComparator()
@ -77,65 +77,10 @@ public class MapType<K, V> extends CollectionType<Map<K, V>>
return values;
}
public Map<K, V> compose(ByteBuffer bytes)
{
try
{
ByteBuffer input = bytes.duplicate();
int n = getUnsignedShort(input);
Map<K, V> m = new LinkedHashMap<K, V>(n);
for (int i = 0; i < n; i++)
{
int sk = getUnsignedShort(input);
byte[] datak = new byte[sk];
input.get(datak);
ByteBuffer kbb = ByteBuffer.wrap(datak);
keys.validate(kbb);
int sv = getUnsignedShort(input);
byte[] datav = new byte[sv];
input.get(datav);
ByteBuffer vbb = ByteBuffer.wrap(datav);
values.validate(vbb);
m.put(keys.compose(kbb), values.compose(vbb));
}
return m;
}
catch (BufferUnderflowException e)
{
throw new MarshalException("Not enough bytes to read a map");
}
}
/**
* Layout is: {@code <n><sk_1><k_1><sv_1><v_1>...<sk_n><k_n><sv_n><v_n> }
* where:
* n is the number of elements
* sk_i is the number of bytes composing the ith key k_i
* k_i is the sk_i bytes composing the ith key
* sv_i is the number of bytes composing the ith value v_i
* v_i is the sv_i bytes composing the ith value
*/
public ByteBuffer decompose(Map<K, V> value)
{
List<ByteBuffer> bbs = new ArrayList<ByteBuffer>(2 * value.size());
int size = 0;
for (Map.Entry<K, V> entry : value.entrySet())
{
ByteBuffer bbk = keys.decompose(entry.getKey());
ByteBuffer bbv = values.decompose(entry.getValue());
bbs.add(bbk);
bbs.add(bbv);
size += 4 + bbk.remaining() + bbv.remaining();
}
return pack(bbs, value.size(), size);
}
@Override
public TypeSerializer<Map<K, V>> getSerializer()
{
return composer;
return serializer;
}
protected void appendToStringBuilder(StringBuilder sb)
@ -144,7 +89,7 @@ public class MapType<K, V> extends CollectionType<Map<K, V>>
}
/**
* Creates the same output than deserialize, but from the internal representation.
* Creates the same output than serialize, but from the internal representation.
*/
public ByteBuffer serialize(List<Pair<ByteBuffer, Column>> columns)
{

View File

@ -84,28 +84,12 @@ public class ReversedType<T> extends AbstractType<T>
return baseType.fromString(source);
}
public void validate(ByteBuffer bytes) throws MarshalException
{
baseType.validate(bytes);
}
public T compose(ByteBuffer bytes)
{
return baseType.compose(bytes);
}
public ByteBuffer decompose(T value)
{
return baseType.decompose(value);
}
@Override
public CQL3Type asCQL3Type()
{
return baseType.asCQL3Type();
}
@Override
public TypeSerializer<T> getSerializer()
{
return baseType.getSerializer();

View File

@ -33,7 +33,7 @@ public class SetType<T> extends CollectionType<Set<T>>
private static final Map<AbstractType<?>, SetType> instances = new HashMap<AbstractType<?>, SetType>();
public final AbstractType<T> elements;
private final SetSerializer<T> composer;
private final SetSerializer<T> serializer;
public static SetType<?> getInstance(TypeParser parser) throws ConfigurationException, SyntaxException
{
@ -59,7 +59,7 @@ public class SetType<T> extends CollectionType<Set<T>>
{
super(Kind.SET);
this.elements = elements;
this.composer = SetSerializer.getInstance(elements.getSerializer());
this.serializer = SetSerializer.getInstance(elements.getSerializer());
}
public AbstractType<T> nameComparator()
@ -72,19 +72,9 @@ public class SetType<T> extends CollectionType<Set<T>>
return EmptyType.instance;
}
public Set<T> compose(ByteBuffer bytes)
{
return composer.serialize(bytes);
}
public ByteBuffer decompose(Set<T> value)
{
return composer.deserialize(value);
}
public TypeSerializer<Set<T>> getSerializer()
{
return composer;
return serializer;
}
protected void appendToStringBuilder(StringBuilder sb)

View File

@ -38,16 +38,6 @@ public class TimeUUIDType extends AbstractType<UUID>
{
} // singleton
public UUID compose(ByteBuffer bytes)
{
return TimeUUIDSerializer.instance.serialize(bytes);
}
public ByteBuffer decompose(UUID value)
{
return TimeUUIDSerializer.instance.deserialize(value);
}
public int compare(ByteBuffer o1, ByteBuffer o2)
{
if (o1.remaining() == 0)
@ -93,11 +83,6 @@ public class TimeUUIDType extends AbstractType<UUID>
return (o1.get(o1Pos + 3) & 0xFF) - (o2.get(o2Pos + 3) & 0xFF);
}
public String getString(ByteBuffer bytes)
{
return TimeUUIDSerializer.instance.getString(bytes);
}
// This accepts dates are valid TimeUUID represensation, which is bogus
// (see #4936) but kept for CQL2 for compatibility sake.
@Override
@ -164,18 +149,11 @@ public class TimeUUIDType extends AbstractType<UUID>
return idBytes;
}
public void validate(ByteBuffer bytes) throws MarshalException
{
TimeUUIDSerializer.instance.validate(bytes);
}
public CQL3Type asCQL3Type()
{
return CQL3Type.Native.TIMEUUID;
}
@Override
public TypeSerializer<UUID> getSerializer()
{
return TimeUUIDSerializer.instance;

View File

@ -46,26 +46,11 @@ public class TimestampType extends AbstractType<Date>
private TimestampType() {} // singleton
public Date compose(ByteBuffer bytes)
{
return TimestampSerializer.instance.serialize(bytes);
}
public ByteBuffer decompose(Date value)
{
return TimestampSerializer.instance.deserialize(value);
}
public int compare(ByteBuffer o1, ByteBuffer o2)
{
return LongType.compareLongs(o1, o2);
}
public String getString(ByteBuffer bytes)
{
return TimestampSerializer.instance.getString(bytes);
}
public ByteBuffer fromString(String source) throws MarshalException
{
// Return an empty ByteBuffer for an empty string.
@ -111,11 +96,6 @@ public class TimestampType extends AbstractType<Date>
return millis;
}
public void validate(ByteBuffer bytes) throws MarshalException
{
TimestampSerializer.instance.validate(bytes);
}
@Override
public boolean isCompatibleWith(AbstractType<?> previous)
{

View File

@ -30,36 +30,16 @@ public class UTF8Type extends AbstractType<String>
UTF8Type() {} // singleton
public String compose(ByteBuffer bytes)
{
return UTF8Serializer.instance.serialize(bytes);
}
public ByteBuffer decompose(String value)
{
return UTF8Serializer.instance.deserialize(value);
}
public int compare(ByteBuffer o1, ByteBuffer o2)
{
return BytesType.bytesCompare(o1, o2);
}
public String getString(ByteBuffer bytes)
{
return UTF8Serializer.instance.getString(bytes);
}
public ByteBuffer fromString(String source)
{
return decompose(source);
}
public void validate(ByteBuffer bytes) throws MarshalException
{
UTF8Serializer.instance.validate(bytes);
}
@Override
public boolean isCompatibleWith(AbstractType<?> previous)
{
@ -73,7 +53,6 @@ public class UTF8Type extends AbstractType<String>
return CQL3Type.Native.TEXT;
}
@Override
public TypeSerializer<String> getSerializer()
{
return UTF8Serializer.instance;

View File

@ -158,27 +158,6 @@ public class UUIDType extends AbstractType<UUID>
return (o1.get(o1Pos + 3) & 0xFF) - (o2.get(o2Pos + 3) & 0xFF);
}
public UUID compose(ByteBuffer bytes)
{
return UUIDSerializer.instance.serialize(bytes);
}
public void validate(ByteBuffer bytes)
{
UUIDSerializer.instance.validate(bytes);
}
public String getString(ByteBuffer bytes)
{
return UUIDSerializer.instance.getString(bytes);
}
public ByteBuffer decompose(UUID value)
{
return UUIDSerializer.instance.deserialize(value);
}
@Override
public ByteBuffer fromString(String source) throws MarshalException
{
@ -239,7 +218,6 @@ public class UUIDType extends AbstractType<UUID>
return CQL3Type.Native.UUID;
}
@Override
public TypeSerializer<UUID> getSerializer()
{
return UUIDSerializer.instance;

View File

@ -32,17 +32,7 @@ public abstract class AbstractTextSerializer implements TypeSerializer<String>
this.charset = charset;
}
public String serialize(ByteBuffer bytes)
{
return getString(bytes);
}
public ByteBuffer deserialize(String value)
{
return ByteBufferUtil.bytes(value, charset);
}
public String getString(ByteBuffer bytes)
public String deserialize(ByteBuffer bytes)
{
try
{
@ -54,6 +44,11 @@ public abstract class AbstractTextSerializer implements TypeSerializer<String>
}
}
public ByteBuffer serialize(String value)
{
return ByteBufferUtil.bytes(value, charset);
}
public String toString(String value)
{
return value;

View File

@ -28,13 +28,16 @@ public class BooleanSerializer implements TypeSerializer<Boolean>
public static final BooleanSerializer instance = new BooleanSerializer();
public Boolean serialize(ByteBuffer bytes)
public Boolean deserialize(ByteBuffer bytes)
{
if (bytes.remaining() == 0)
return null;
byte value = bytes.get(bytes.position());
return value != 0;
}
public ByteBuffer deserialize(Boolean value)
public ByteBuffer serialize(Boolean value)
{
return (value == null) ? ByteBufferUtil.EMPTY_BYTE_BUFFER
: value ? TRUE : FALSE; // false
@ -46,21 +49,6 @@ public class BooleanSerializer implements TypeSerializer<Boolean>
throw new MarshalException(String.format("Expected 1 or 0 byte value (%d)", bytes.remaining()));
}
public String getString(ByteBuffer bytes)
{
if (bytes.remaining() == 0)
{
return Boolean.FALSE.toString();
}
if (bytes.remaining() != 1)
{
throw new MarshalException("A boolean is stored in exactly 1 byte: " + bytes.remaining());
}
byte value = bytes.get(bytes.position());
return value == 0 ? Boolean.FALSE.toString() : Boolean.TRUE.toString();
}
public String toString(Boolean value)
{
return value == null ? "" : value.toString();

View File

@ -43,14 +43,9 @@ public class BytesSerializer implements TypeSerializer<ByteBuffer>
// all bytes are legal.
}
public String getString(ByteBuffer bytes)
{
return ByteBufferUtil.bytesToHex(bytes);
}
public String toString(ByteBuffer value)
{
return getString(value);
return ByteBufferUtil.bytesToHex(value);
}
public Class<ByteBuffer> getType()

View File

@ -28,11 +28,6 @@ public abstract class CollectionSerializer<T> implements TypeSerializer<T>
// The collection is not currently being properly validated.
}
public String getString(ByteBuffer bytes)
{
return BytesSerializer.instance.getString(bytes);
}
// Utilitary method
protected static ByteBuffer pack(List<ByteBuffer> buffers, int elements, int size)
{
@ -54,7 +49,6 @@ public abstract class CollectionSerializer<T> implements TypeSerializer<T>
return pack(buffers, elements, size);
}
protected static int getUnsignedShort(ByteBuffer bb)
{
int length = (bb.get() & 0xFF) << 8;

View File

@ -27,9 +27,9 @@ public class DecimalSerializer implements TypeSerializer<BigDecimal>
{
public static final DecimalSerializer instance = new DecimalSerializer();
public BigDecimal serialize(ByteBuffer bytes)
public BigDecimal deserialize(ByteBuffer bytes)
{
if (bytes == null)
if (bytes == null || bytes.remaining() == 0)
return null;
// do not consume the contents of the ByteBuffer
@ -42,7 +42,7 @@ public class DecimalSerializer implements TypeSerializer<BigDecimal>
return new BigDecimal(bi, scale);
}
public ByteBuffer deserialize(BigDecimal value)
public ByteBuffer serialize(BigDecimal value)
{
if (value == null)
return ByteBufferUtil.EMPTY_BYTE_BUFFER;
@ -63,16 +63,9 @@ public class DecimalSerializer implements TypeSerializer<BigDecimal>
public void validate(ByteBuffer bytes) throws MarshalException
{
// no useful check for invalid decimals.
}
public String getString(ByteBuffer bytes)
{
if (bytes.remaining() == 0)
{
return "";
}
return serialize(bytes).toPlainString();
// We at least store the scale.
if (bytes.remaining() != 0 && bytes.remaining() < 4)
throw new MarshalException(String.format("Expected 0 or at least 4 bytes (%d)", bytes.remaining()));
}
public String toString(BigDecimal value)

View File

@ -26,12 +26,14 @@ public class DoubleSerializer implements TypeSerializer<Double>
{
public static final DoubleSerializer instance = new DoubleSerializer();
public Double serialize(ByteBuffer bytes)
public Double deserialize(ByteBuffer bytes)
{
if (bytes.remaining() == 0)
return null;
return ByteBufferUtil.toDouble(bytes);
}
public ByteBuffer deserialize(Double value)
public ByteBuffer serialize(Double value)
{
return (value == null) ? ByteBufferUtil.EMPTY_BYTE_BUFFER : ByteBufferUtil.bytes(value);
}
@ -42,20 +44,6 @@ public class DoubleSerializer implements TypeSerializer<Double>
throw new MarshalException(String.format("Expected 8 or 0 byte value for a double (%d)", bytes.remaining()));
}
public String getString(ByteBuffer bytes)
{
if (bytes.remaining() == 0)
{
return "";
}
if (bytes.remaining() != 8)
{
throw new MarshalException("A double is exactly 8 bytes : " + bytes.remaining());
}
return String.valueOf(ByteBufferUtil.toDouble(bytes));
}
public String toString(Double value)
{
return value == null ? "" : value.toString();

View File

@ -26,12 +26,12 @@ public class EmptySerializer implements TypeSerializer<Void>
{
public static final EmptySerializer instance = new EmptySerializer();
public Void serialize(ByteBuffer bytes)
public Void deserialize(ByteBuffer bytes)
{
return null;
}
public ByteBuffer deserialize(Void value)
public ByteBuffer serialize(Void value)
{
return ByteBufferUtil.EMPTY_BYTE_BUFFER;
}
@ -42,11 +42,6 @@ public class EmptySerializer implements TypeSerializer<Void>
throw new MarshalException("EmptyType only accept empty values");
}
public String getString(ByteBuffer bytes)
{
return "";
}
public String toString(Void value)
{
return "";

View File

@ -26,12 +26,15 @@ public class FloatSerializer implements TypeSerializer<Float>
{
public static final FloatSerializer instance = new FloatSerializer();
public Float serialize(ByteBuffer bytes)
public Float deserialize(ByteBuffer bytes)
{
if (bytes.remaining() == 0)
return null;
return ByteBufferUtil.toFloat(bytes);
}
public ByteBuffer deserialize(Float value)
public ByteBuffer serialize(Float value)
{
return (value == null) ? ByteBufferUtil.EMPTY_BYTE_BUFFER : ByteBufferUtil.bytes(value);
}
@ -42,20 +45,6 @@ public class FloatSerializer implements TypeSerializer<Float>
throw new MarshalException(String.format("Expected 4 or 0 byte value for a float (%d)", bytes.remaining()));
}
public String getString(ByteBuffer bytes)
{
if (bytes.remaining() == 0)
{
return "";
}
if (bytes.remaining() != 4)
{
throw new MarshalException("A float is exactly 4 bytes : " + bytes.remaining());
}
return String.valueOf(ByteBufferUtil.toFloat(bytes));
}
public String toString(Float value)
{
return value == null ? "" : String.valueOf(value);

View File

@ -28,8 +28,11 @@ public class InetAddressSerializer implements TypeSerializer<InetAddress>
{
public static final InetAddressSerializer instance = new InetAddressSerializer();
public InetAddress serialize(ByteBuffer bytes)
public InetAddress deserialize(ByteBuffer bytes)
{
if (bytes.remaining() == 0)
return null;
try
{
return InetAddress.getByAddress(ByteBufferUtil.getArray(bytes));
@ -40,13 +43,16 @@ public class InetAddressSerializer implements TypeSerializer<InetAddress>
}
}
public ByteBuffer deserialize(InetAddress value)
public ByteBuffer serialize(InetAddress value)
{
return ByteBuffer.wrap(value.getAddress());
return value == null ? ByteBufferUtil.EMPTY_BYTE_BUFFER : ByteBuffer.wrap(value.getAddress());
}
public void validate(ByteBuffer bytes) throws MarshalException
{
if (bytes.remaining() == 0)
return;
try
{
InetAddress.getByAddress(ByteBufferUtil.getArray(bytes));
@ -57,11 +63,6 @@ public class InetAddressSerializer implements TypeSerializer<InetAddress>
}
}
public String getString(ByteBuffer bytes)
{
return serialize(bytes).getHostAddress();
}
public String toString(InetAddress value)
{
return value.getHostAddress();

View File

@ -26,12 +26,12 @@ public class Int32Serializer implements TypeSerializer<Integer>
{
public static final Int32Serializer instance = new Int32Serializer();
public Integer serialize(ByteBuffer bytes)
public Integer deserialize(ByteBuffer bytes)
{
return ByteBufferUtil.toInt(bytes);
return bytes.remaining() == 0 ? null : ByteBufferUtil.toInt(bytes);
}
public ByteBuffer deserialize(Integer value)
public ByteBuffer serialize(Integer value)
{
return value == null ? ByteBufferUtil.EMPTY_BYTE_BUFFER : ByteBufferUtil.bytes(value);
}
@ -42,20 +42,6 @@ public class Int32Serializer implements TypeSerializer<Integer>
throw new MarshalException(String.format("Expected 4 or 0 byte int (%d)", bytes.remaining()));
}
public String getString(ByteBuffer bytes)
{
if (bytes.remaining() == 0)
{
return "";
}
if (bytes.remaining() != 4)
{
throw new MarshalException("A int is exactly 4 bytes: " + bytes.remaining());
}
return String.valueOf(ByteBufferUtil.toInt(bytes));
}
public String toString(Integer value)
{
return value == null ? "" : String.valueOf(value);

View File

@ -27,12 +27,12 @@ public class IntegerSerializer implements TypeSerializer<BigInteger>
{
public static final IntegerSerializer instance = new IntegerSerializer();
public BigInteger serialize(ByteBuffer bytes)
public BigInteger deserialize(ByteBuffer bytes)
{
return new BigInteger(ByteBufferUtil.getArray(bytes));
}
public ByteBuffer deserialize(BigInteger value)
public ByteBuffer serialize(BigInteger value)
{
return ByteBuffer.wrap(value.toByteArray());
}
@ -42,16 +42,6 @@ public class IntegerSerializer implements TypeSerializer<BigInteger>
// no invalid integers.
}
public String getString(ByteBuffer bytes)
{
if (bytes.remaining() == 0)
{
return "";
}
return new BigInteger(ByteBufferUtil.getArray(bytes)).toString(10);
}
public String toString(BigInteger value)
{
return value.toString(10);

View File

@ -45,7 +45,7 @@ public class ListSerializer<T> extends CollectionSerializer<List<T>>
this.elements = elements;
}
public List<T> serialize(ByteBuffer bytes)
public List<T> deserialize(ByteBuffer bytes)
{
try
{
@ -59,7 +59,7 @@ public class ListSerializer<T> extends CollectionSerializer<List<T>>
input.get(data);
ByteBuffer databb = ByteBuffer.wrap(data);
elements.validate(databb);
l.add(elements.serialize(databb));
l.add(elements.deserialize(databb));
}
return l;
}
@ -76,13 +76,13 @@ public class ListSerializer<T> extends CollectionSerializer<List<T>>
* s_i is the number of bytes composing the ith element
* b_i is the s_i bytes composing the ith element
*/
public ByteBuffer deserialize(List<T> value)
public ByteBuffer serialize(List<T> value)
{
List<ByteBuffer> bbs = new ArrayList<ByteBuffer>(value.size());
int size = 0;
for (T elt : value)
{
ByteBuffer bb = elements.deserialize(elt);
ByteBuffer bb = elements.serialize(elt);
bbs.add(bb);
size += 2 + bb.remaining();
}

View File

@ -26,14 +26,14 @@ public class LongSerializer implements TypeSerializer<Long>
{
public static final LongSerializer instance = new LongSerializer();
public Long serialize(ByteBuffer bytes)
public Long deserialize(ByteBuffer bytes)
{
return ByteBufferUtil.toLong(bytes);
return bytes.remaining() == 0 ? null : ByteBufferUtil.toLong(bytes);
}
public ByteBuffer deserialize(Long value)
public ByteBuffer serialize(Long value)
{
return ByteBufferUtil.bytes(value);
return value == null ? ByteBufferUtil.EMPTY_BYTE_BUFFER : ByteBufferUtil.bytes(value);
}
public void validate(ByteBuffer bytes) throws MarshalException
@ -42,20 +42,6 @@ public class LongSerializer implements TypeSerializer<Long>
throw new MarshalException(String.format("Expected 8 or 0 byte long (%d)", bytes.remaining()));
}
public String getString(ByteBuffer bytes)
{
if (bytes.remaining() == 0)
{
return "";
}
if (bytes.remaining() != 8)
{
throw new MarshalException("A long is exactly 8 bytes: " + bytes.remaining());
}
return String.valueOf(ByteBufferUtil.toLong(bytes));
}
public String toString(Long value)
{
return String.valueOf(value);

View File

@ -50,7 +50,7 @@ public class MapSerializer<K, V> extends CollectionSerializer<Map<K, V>>
this.values = values;
}
public Map<K, V> serialize(ByteBuffer bytes)
public Map<K, V> deserialize(ByteBuffer bytes)
{
try
{
@ -71,7 +71,7 @@ public class MapSerializer<K, V> extends CollectionSerializer<Map<K, V>>
ByteBuffer vbb = ByteBuffer.wrap(datav);
values.validate(vbb);
m.put(keys.serialize(kbb), values.serialize(vbb));
m.put(keys.deserialize(kbb), values.deserialize(vbb));
}
return m;
}
@ -81,14 +81,23 @@ public class MapSerializer<K, V> extends CollectionSerializer<Map<K, V>>
}
}
public ByteBuffer deserialize(Map<K, V> value)
/**
* Layout is: {@code <n><sk_1><k_1><sv_1><v_1>...<sk_n><k_n><sv_n><v_n> }
* where:
* n is the number of elements
* sk_i is the number of bytes composing the ith key k_i
* k_i is the sk_i bytes composing the ith key
* sv_i is the number of bytes composing the ith value v_i
* v_i is the sv_i bytes composing the ith value
*/
public ByteBuffer serialize(Map<K, V> value)
{
List<ByteBuffer> bbs = new ArrayList<ByteBuffer>(2 * value.size());
int size = 0;
for (Map.Entry<K, V> entry : value.entrySet())
{
ByteBuffer bbk = keys.deserialize(entry.getKey());
ByteBuffer bbv = values.deserialize(entry.getValue());
ByteBuffer bbk = keys.serialize(entry.getKey());
ByteBuffer bbv = values.serialize(entry.getValue());
bbs.add(bbk);
bbs.add(bbv);
size += 4 + bbk.remaining() + bbv.remaining();

View File

@ -45,7 +45,7 @@ public class SetSerializer<T> extends CollectionSerializer<Set<T>>
this.elements = elements;
}
public Set<T> serialize(ByteBuffer bytes)
public Set<T> deserialize(ByteBuffer bytes)
{
try
{
@ -59,7 +59,7 @@ public class SetSerializer<T> extends CollectionSerializer<Set<T>>
input.get(data);
ByteBuffer databb = ByteBuffer.wrap(data);
elements.validate(databb);
l.add(elements.serialize(databb));
l.add(elements.deserialize(databb));
}
return l;
}
@ -76,13 +76,13 @@ public class SetSerializer<T> extends CollectionSerializer<Set<T>>
* s_i is the number of bytes composing the ith element
* b_i is the s_i bytes composing the ith element
*/
public ByteBuffer deserialize(Set<T> value)
public ByteBuffer serialize(Set<T> value)
{
List<ByteBuffer> bbs = new ArrayList<ByteBuffer>(value.size());
int size = 0;
for (T elt : value)
{
ByteBuffer bb = elements.deserialize(elt);
ByteBuffer bb = elements.serialize(elt);
bbs.add(bb);
size += 2 + bb.remaining();
}

View File

@ -50,18 +50,14 @@ public class TimestampSerializer implements TypeSerializer<Date>
public static final TimestampSerializer instance = new TimestampSerializer();
public Date serialize(ByteBuffer bytes)
public Date deserialize(ByteBuffer bytes)
{
return bytes.remaining() > 0
? new Date(ByteBufferUtil.toLong(bytes))
: null;
return bytes.remaining() == 0 ? null : new Date(ByteBufferUtil.toLong(bytes));
}
public ByteBuffer deserialize(Date value)
public ByteBuffer serialize(Date value)
{
return (value == null)
? ByteBufferUtil.EMPTY_BYTE_BUFFER
: ByteBufferUtil.bytes(value.getTime());
return value == null ? ByteBufferUtil.EMPTY_BYTE_BUFFER : ByteBufferUtil.bytes(value.getTime());
}
public void validate(ByteBuffer bytes) throws MarshalException
@ -70,21 +66,6 @@ public class TimestampSerializer implements TypeSerializer<Date>
throw new MarshalException(String.format("Expected 8 or 0 byte long for date (%d)", bytes.remaining()));
}
public String getString(ByteBuffer bytes)
{
if (bytes.remaining() == 0)
{
return "";
}
if (bytes.remaining() != 8)
{
throw new MarshalException("A date is exactly 8 bytes (stored as a long): " + bytes.remaining());
}
// uses ISO-8601 formatted string
return FORMATTER.get().format(new Date(ByteBufferUtil.toLong(bytes)));
}
public String toString(Date value)
{
return FORMATTER.get().format(value);

View File

@ -22,14 +22,15 @@ import java.nio.ByteBuffer;
public interface TypeSerializer<T>
{
public T serialize(ByteBuffer bytes);
public ByteBuffer deserialize(T value);
public ByteBuffer serialize(T value);
public T deserialize(ByteBuffer bytes);
/* validate that the byte array is a valid sequence for the type we are supposed to be comparing */
/*
* Validate that the byte array is a valid sequence for the type this represents.
* This guarantees deserialize() can be called without errors.
*/
public void validate(ByteBuffer bytes) throws MarshalException;
public String getString(ByteBuffer bytes);
public String toString(T value);
public Class<T> getType();

View File

@ -17,23 +17,24 @@
*/
package org.apache.cassandra.serializers;
import org.apache.cassandra.utils.UUIDGen;
import java.nio.ByteBuffer;
import java.util.UUID;
import org.apache.cassandra.utils.ByteBufferUtil;
import org.apache.cassandra.utils.UUIDGen;
public class UUIDSerializer implements TypeSerializer<UUID>
{
public static final UUIDSerializer instance = new UUIDSerializer();
public UUID serialize(ByteBuffer bytes)
public UUID deserialize(ByteBuffer bytes)
{
return UUIDGen.getUUID(bytes);
return bytes.remaining() == 0 ? null : UUIDGen.getUUID(bytes);
}
public ByteBuffer deserialize(UUID value)
public ByteBuffer serialize(UUID value)
{
return ByteBuffer.wrap(UUIDGen.decompose(value));
return value == null ? ByteBufferUtil.EMPTY_BYTE_BUFFER : ByteBuffer.wrap(UUIDGen.decompose(value));
}
public void validate(ByteBuffer bytes) throws MarshalException
@ -43,20 +44,6 @@ public class UUIDSerializer implements TypeSerializer<UUID>
// not sure what the version should be for this.
}
public String getString(ByteBuffer bytes)
{
if (bytes.remaining() == 0)
{
return "";
}
if (bytes.remaining() != 16)
{
throw new MarshalException("UUIDs must be exactly 16 bytes");
}
UUID uuid = UUIDGen.getUUID(bytes);
return uuid.toString();
}
public String toString(UUID value)
{
return value.toString();

View File

@ -389,7 +389,7 @@ public class Shuffle extends AbstractJmxClient
ByteBuffer tokenBytes = ByteBuffer.wrap(row.getColumns().get(0).getValue());
ByteBuffer requestedAt = ByteBuffer.wrap(row.getColumns().get(1).getValue());
Date time = TimestampSerializer.instance.serialize(requestedAt);
Date time = TimestampSerializer.instance.deserialize(requestedAt);
Token<?> token = partitioner.getTokenFactory().fromByteArray(tokenBytes);
writeln("%-42s %-15s %s", token.toString(), host, time.toString());

View File

@ -41,4 +41,4 @@ public class BooleanSerializer implements IVersionedSerializer<Boolean>
{
return 1;
}
}
}

View File

@ -33,8 +33,8 @@ public class JdbcDecimalTest
BigDecimal expected = new BigDecimal("123456789123456789.987654321");
DecimalSerializer decimal = new DecimalSerializer();
ByteBuffer buffer = decimal.deserialize(expected);
BigDecimal actual = decimal.serialize(buffer);
ByteBuffer buffer = decimal.serialize(expected);
BigDecimal actual = decimal.deserialize(buffer);
Assert.assertEquals(expected, actual);
}
}

View File

@ -83,10 +83,6 @@ public class RoundTripTest
public void testBytes()
{
byte[] v = new byte[] {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
assert BytesSerializer.instance.getString(BytesType.instance.fromString(Hex.bytesToHex(v)))
.equals(Hex.bytesToHex(v));
assert BytesType.instance.fromString(BytesSerializer.instance.getString(ByteBuffer.wrap(v)))
.equals(ByteBuffer.wrap(v));
assert BytesType.instance.compose(ByteBuffer.wrap(v)).equals(ByteBuffer.wrap(v));
assert BytesSerializer.instance.toString(ByteBuffer.wrap(v)).equals(Hex.bytesToHex(v));
}
@ -95,8 +91,6 @@ public class RoundTripTest
public void testLexicalUUID()
{
UUID uuid = UUIDGen.getTimeUUID();
assert UUIDSerializer.instance.getString(LexicalUUIDType.instance.fromString(uuid.toString()))
.equals(uuid.toString());
assert LexicalUUIDType.instance.fromString(LexicalUUIDType.instance.getString(ByteBuffer.wrap(UUIDGen.decompose(uuid))))
.equals(ByteBuffer.wrap(UUIDGen.decompose(uuid)));
assert LexicalUUIDType.instance.compose(ByteBuffer.wrap(UUIDGen.decompose(uuid))).equals(uuid);

View File

@ -37,35 +37,24 @@ public class ClientUtilsTest
@Test
public void test() throws UnknownHostException
{
AsciiSerializer.instance.serialize(AsciiSerializer.instance.deserialize("string"));
BooleanSerializer.instance.serialize(BooleanSerializer.instance.deserialize(true));
BytesSerializer.instance.serialize(BytesSerializer.instance.deserialize(ByteBuffer.wrap("string".getBytes())));
AsciiSerializer.instance.deserialize(AsciiSerializer.instance.serialize("string"));
BooleanSerializer.instance.deserialize(BooleanSerializer.instance.serialize(true));
BytesSerializer.instance.deserialize(BytesSerializer.instance.serialize(ByteBuffer.wrap("string".getBytes())));
Date date = new Date(System.currentTimeMillis());
ByteBuffer dateBB = TimestampSerializer.instance.deserialize(date);
TimestampSerializer.instance.serialize(dateBB);
assert (TimestampSerializer.instance.toString(date).equals(TimestampSerializer.instance.getString(dateBB)));
ByteBuffer dateBB = TimestampSerializer.instance.serialize(date);
TimestampSerializer.instance.deserialize(dateBB);
DecimalSerializer.instance.serialize(DecimalSerializer.instance.deserialize(new BigDecimal(1)));
DoubleSerializer.instance.serialize(DoubleSerializer.instance.deserialize(new Double(1.0d)));
FloatSerializer.instance.serialize(FloatSerializer.instance.deserialize(new Float(1.0f)));
Int32Serializer.instance.serialize(Int32Serializer.instance.deserialize(1));
IntegerSerializer.instance.serialize(IntegerSerializer.instance.deserialize(new BigInteger("1")));
LongSerializer.instance.serialize(LongSerializer.instance.deserialize(1L));
UTF8Serializer.instance.serialize(UTF8Serializer.instance.deserialize("string"));
DecimalSerializer.instance.deserialize(DecimalSerializer.instance.serialize(new BigDecimal(1)));
DoubleSerializer.instance.deserialize(DoubleSerializer.instance.serialize(new Double(1.0d)));
FloatSerializer.instance.deserialize(FloatSerializer.instance.serialize(new Float(1.0f)));
Int32Serializer.instance.deserialize(Int32Serializer.instance.serialize(1));
IntegerSerializer.instance.deserialize(IntegerSerializer.instance.serialize(new BigInteger("1")));
LongSerializer.instance.deserialize(LongSerializer.instance.serialize(1L));
UTF8Serializer.instance.deserialize(UTF8Serializer.instance.serialize("string"));
// UUIDGen
UUID uuid = UUIDGen.getTimeUUID();
UUIDSerializer.instance.serialize(UUIDSerializer.instance.deserialize(uuid));
// Raise a MarshalException
try
{
UUIDSerializer.instance.getString(ByteBuffer.wrap("notauuid".getBytes()));
}
catch (MarshalException me)
{
// Success
}
UUIDSerializer.instance.deserialize(UUIDSerializer.instance.serialize(uuid));
}
}