From daff1fce0f17c03d51e06d1d9a27a70d9785a5a6 Mon Sep 17 00:00:00 2001 From: Sylvain Lebresne Date: Fri, 12 Jul 2013 15:25:48 +0200 Subject: [PATCH] Cleanup AbstractType/TypeSerializer classes patch by slebresne; reviewed by carlyeks for CASSANDRA-5744 --- CHANGES.txt | 9 ++- src/java/org/apache/cassandra/cql3/Lists.java | 2 +- src/java/org/apache/cassandra/cql3/Maps.java | 2 +- src/java/org/apache/cassandra/cql3/Sets.java | 2 +- .../db/marshal/AbstractCommutativeType.java | 2 + .../db/marshal/AbstractCompositeType.java | 12 +--- .../cassandra/db/marshal/AbstractType.java | 24 +++++-- .../cassandra/db/marshal/AsciiType.java | 20 ------ .../cassandra/db/marshal/BooleanType.java | 66 ++++++------------ .../cassandra/db/marshal/BytesType.java | 21 ------ .../db/marshal/ColumnToCollectionType.java | 11 +-- .../db/marshal/CounterColumnType.java | 6 -- .../apache/cassandra/db/marshal/DateType.java | 20 ------ .../cassandra/db/marshal/DecimalType.java | 25 ------- .../cassandra/db/marshal/DoubleType.java | 21 ------ .../db/marshal/DynamicCompositeType.java | 4 +- .../cassandra/db/marshal/EmptyType.java | 15 ---- .../cassandra/db/marshal/FloatType.java | 21 ------ .../cassandra/db/marshal/InetAddressType.java | 21 ------ .../cassandra/db/marshal/Int32Type.java | 21 ------ .../cassandra/db/marshal/IntegerType.java | 21 ------ .../cassandra/db/marshal/LexicalUUIDType.java | 21 ------ .../apache/cassandra/db/marshal/ListType.java | 23 +----- .../db/marshal/LocalByPartionerType.java | 3 + .../apache/cassandra/db/marshal/LongType.java | 21 ------ .../apache/cassandra/db/marshal/MapType.java | 63 ++--------------- .../cassandra/db/marshal/ReversedType.java | 16 ----- .../apache/cassandra/db/marshal/SetType.java | 16 +---- .../cassandra/db/marshal/TimeUUIDType.java | 22 ------ .../cassandra/db/marshal/TimestampType.java | 20 ------ .../apache/cassandra/db/marshal/UTF8Type.java | 21 ------ .../apache/cassandra/db/marshal/UUIDType.java | 22 ------ .../serializers/AbstractTextSerializer.java | 17 ++--- .../serializers/BooleanSerializer.java | 22 ++---- .../serializers/BytesSerializer.java | 7 +- .../serializers/CollectionSerializer.java | 6 -- .../serializers/DecimalSerializer.java | 19 ++--- .../serializers/DoubleSerializer.java | 20 ++---- .../serializers/EmptySerializer.java | 9 +-- .../serializers/FloatSerializer.java | 21 ++---- .../serializers/InetAddressSerializer.java | 17 ++--- .../serializers/Int32Serializer.java | 20 +----- .../serializers/IntegerSerializer.java | 14 +--- .../cassandra/serializers/ListSerializer.java | 8 +-- .../cassandra/serializers/LongSerializer.java | 22 ++---- .../cassandra/serializers/MapSerializer.java | 19 +++-- .../cassandra/serializers/SetSerializer.java | 8 +-- .../serializers/TimestampSerializer.java | 27 ++----- .../cassandra/serializers/TypeSerializer.java | 11 +-- .../cassandra/serializers/UUIDSerializer.java | 27 ++----- .../org/apache/cassandra/tools/Shuffle.java | 2 +- .../cassandra/utils/BooleanSerializer.java | 2 +- .../data/serialization/2.0/db.RowMutation.bin | Bin 3599 -> 3599 bytes .../cassandra/cql/jdbc/JdbcDecimalTest.java | 4 +- .../cassandra/db/marshal/RoundTripTest.java | 6 -- .../serializers/ClientUtilsTest.java | 37 ++++------ 56 files changed, 175 insertions(+), 764 deletions(-) diff --git a/CHANGES.txt b/CHANGES.txt index db74becaf0..ee62cacf22 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -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) diff --git a/src/java/org/apache/cassandra/cql3/Lists.java b/src/java/org/apache/cassandra/cql3/Lists.java index e2a4945def..8906afb93c 100644 --- a/src/java/org/apache/cassandra/cql3/Lists.java +++ b/src/java/org/apache/cassandra/cql3/Lists.java @@ -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 elements = new ArrayList(l.size()); for (Object element : l) elements.add(type.elements.decompose(element)); diff --git a/src/java/org/apache/cassandra/cql3/Maps.java b/src/java/org/apache/cassandra/cql3/Maps.java index 4486dca46d..108cb644b1 100644 --- a/src/java/org/apache/cassandra/cql3/Maps.java +++ b/src/java/org/apache/cassandra/cql3/Maps.java @@ -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 map = new LinkedHashMap(m.size()); for (Map.Entry entry : m.entrySet()) map.put(type.keys.decompose(entry.getKey()), type.values.decompose(entry.getValue())); diff --git a/src/java/org/apache/cassandra/cql3/Sets.java b/src/java/org/apache/cassandra/cql3/Sets.java index 3c50db24bf..9007cecb68 100644 --- a/src/java/org/apache/cassandra/cql3/Sets.java +++ b/src/java/org/apache/cassandra/cql3/Sets.java @@ -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 elements = new LinkedHashSet(s.size()); for (Object element : s) elements.add(type.elements.decompose(element)); diff --git a/src/java/org/apache/cassandra/db/marshal/AbstractCommutativeType.java b/src/java/org/apache/cassandra/db/marshal/AbstractCommutativeType.java index a9c151db5f..01db148c27 100644 --- a/src/java/org/apache/cassandra/db/marshal/AbstractCommutativeType.java +++ b/src/java/org/apache/cassandra/db/marshal/AbstractCommutativeType.java @@ -29,11 +29,13 @@ public abstract class AbstractCommutativeType extends AbstractType return true; } + @Override public Long compose(ByteBuffer bytes) { return CounterContext.instance().total(bytes); } + @Override public ByteBuffer decompose(Long value) { return ByteBufferUtil.bytes(value); diff --git a/src/java/org/apache/cassandra/db/marshal/AbstractCompositeType.java b/src/java/org/apache/cassandra/db/marshal/AbstractCompositeType.java index 0626ef569d..d002aa79fb 100644 --- a/src/java/org/apache/cassandra/db/marshal/AbstractCompositeType.java +++ b/src/java/org/apache/cassandra/db/marshal/AbstractCompositeType.java @@ -286,6 +286,7 @@ public abstract class AbstractCompositeType extends AbstractType return bb; } + @Override public void validate(ByteBuffer bytes) throws MarshalException { ByteBuffer bb = bytes.duplicate(); @@ -319,17 +320,6 @@ public abstract class AbstractCompositeType extends AbstractType public abstract ByteBuffer decompose(Object... objects); - public ByteBuffer compose(ByteBuffer bytes) - { - return bytes; - } - - public ByteBuffer decompose(ByteBuffer value) - { - return value; - } - - @Override public TypeSerializer getSerializer() { return BytesSerializer.instance; diff --git a/src/java/org/apache/cassandra/db/marshal/AbstractType.java b/src/java/org/apache/cassandra/db/marshal/AbstractType.java index 82068f35e2..74fe446319 100644 --- a/src/java/org/apache/cassandra/db/marshal/AbstractType.java +++ b/src/java/org/apache/cassandra/db/marshal/AbstractType.java @@ -137,12 +137,25 @@ public abstract class AbstractType implements Comparator }; } - 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 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 implements Comparator } /* 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() diff --git a/src/java/org/apache/cassandra/db/marshal/AsciiType.java b/src/java/org/apache/cassandra/db/marshal/AsciiType.java index fb96aff55b..5142f376b8 100644 --- a/src/java/org/apache/cassandra/db/marshal/AsciiType.java +++ b/src/java/org/apache/cassandra/db/marshal/AsciiType.java @@ -30,36 +30,16 @@ public class AsciiType extends AbstractType 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; diff --git a/src/java/org/apache/cassandra/db/marshal/BooleanType.java b/src/java/org/apache/cassandra/db/marshal/BooleanType.java index 95c6de02b5..3a9fdfae88 100644 --- a/src/java/org/apache/cassandra/db/marshal/BooleanType.java +++ b/src/java/org/apache/cassandra/db/marshal/BooleanType.java @@ -26,59 +26,37 @@ import org.apache.cassandra.serializers.MarshalException; public class BooleanType extends AbstractType { - 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 getSerializer() { return BooleanSerializer.instance; diff --git a/src/java/org/apache/cassandra/db/marshal/BytesType.java b/src/java/org/apache/cassandra/db/marshal/BytesType.java index 6601af3b5f..4aa1ff4333 100644 --- a/src/java/org/apache/cassandra/db/marshal/BytesType.java +++ b/src/java/org/apache/cassandra/db/marshal/BytesType.java @@ -32,16 +32,6 @@ public class BytesType extends AbstractType 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 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 } } - 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 return CQL3Type.Native.BLOB; } - @Override public TypeSerializer getSerializer() { return BytesSerializer.instance; diff --git a/src/java/org/apache/cassandra/db/marshal/ColumnToCollectionType.java b/src/java/org/apache/cassandra/db/marshal/ColumnToCollectionType.java index 7dda157d46..a4f7857b4c 100644 --- a/src/java/org/apache/cassandra/db/marshal/ColumnToCollectionType.java +++ b/src/java/org/apache/cassandra/db/marshal/ColumnToCollectionType.java @@ -74,16 +74,6 @@ public class ColumnToCollectionType extends AbstractType 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 } } + @Override public void validate(ByteBuffer bytes) { throw new UnsupportedOperationException("ColumnToCollectionType should only be used in composite types, never alone"); diff --git a/src/java/org/apache/cassandra/db/marshal/CounterColumnType.java b/src/java/org/apache/cassandra/db/marshal/CounterColumnType.java index ef1ffc6528..ba7c59c4cb 100644 --- a/src/java/org/apache/cassandra/db/marshal/CounterColumnType.java +++ b/src/java/org/apache/cassandra/db/marshal/CounterColumnType.java @@ -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; diff --git a/src/java/org/apache/cassandra/db/marshal/DateType.java b/src/java/org/apache/cassandra/db/marshal/DateType.java index f97067afa3..dda17e841b 100644 --- a/src/java/org/apache/cassandra/db/marshal/DateType.java +++ b/src/java/org/apache/cassandra/db/marshal/DateType.java @@ -42,16 +42,6 @@ public class DateType extends AbstractType 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 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 return false; } - public void validate(ByteBuffer bytes) throws MarshalException - { - TimestampSerializer.instance.validate(bytes); - } - public TypeSerializer getSerializer() { return TimestampSerializer.instance; diff --git a/src/java/org/apache/cassandra/db/marshal/DecimalType.java b/src/java/org/apache/cassandra/db/marshal/DecimalType.java index 230df51d8e..2ac84801b3 100644 --- a/src/java/org/apache/cassandra/db/marshal/DecimalType.java +++ b/src/java/org/apache/cassandra/db/marshal/DecimalType.java @@ -46,25 +46,6 @@ public class DecimalType extends AbstractType 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 return decompose(decimal); } - public void validate(ByteBuffer bytes) throws MarshalException - { - DecimalSerializer.instance.validate(bytes); - } - public CQL3Type asCQL3Type() { return CQL3Type.Native.DECIMAL; } - @Override public TypeSerializer getSerializer() { return DecimalSerializer.instance; diff --git a/src/java/org/apache/cassandra/db/marshal/DoubleType.java b/src/java/org/apache/cassandra/db/marshal/DoubleType.java index 4ff4db607d..35b33d6466 100644 --- a/src/java/org/apache/cassandra/db/marshal/DoubleType.java +++ b/src/java/org/apache/cassandra/db/marshal/DoubleType.java @@ -31,16 +31,6 @@ public class DoubleType extends AbstractType 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 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 return decompose(d); } - public void validate(ByteBuffer bytes) throws MarshalException - { - DoubleSerializer.instance.validate(bytes); - } - public CQL3Type asCQL3Type() { return CQL3Type.Native.DOUBLE; } - @Override public TypeSerializer getSerializer() { return DoubleSerializer.instance; diff --git a/src/java/org/apache/cassandra/db/marshal/DynamicCompositeType.java b/src/java/org/apache/cassandra/db/marshal/DynamicCompositeType.java index b68505e069..35e6e33aad 100644 --- a/src/java/org/apache/cassandra/db/marshal/DynamicCompositeType.java +++ b/src/java/org/apache/cassandra/db/marshal/DynamicCompositeType.java @@ -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 getSerializer() { throw new UnsupportedOperationException(); diff --git a/src/java/org/apache/cassandra/db/marshal/EmptyType.java b/src/java/org/apache/cassandra/db/marshal/EmptyType.java index 74cf3fee88..0ddb9eafde 100644 --- a/src/java/org/apache/cassandra/db/marshal/EmptyType.java +++ b/src/java/org/apache/cassandra/db/marshal/EmptyType.java @@ -34,16 +34,6 @@ public class EmptyType extends AbstractType 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 return ByteBufferUtil.EMPTY_BYTE_BUFFER; } - public void validate(ByteBuffer bytes) throws MarshalException - { - EmptySerializer.instance.validate(bytes); - } - public TypeSerializer getSerializer() { return EmptySerializer.instance; diff --git a/src/java/org/apache/cassandra/db/marshal/FloatType.java b/src/java/org/apache/cassandra/db/marshal/FloatType.java index 6b9782e966..bee226e47e 100644 --- a/src/java/org/apache/cassandra/db/marshal/FloatType.java +++ b/src/java/org/apache/cassandra/db/marshal/FloatType.java @@ -32,16 +32,6 @@ public class FloatType extends AbstractType 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 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 } } - public void validate(ByteBuffer bytes) throws MarshalException - { - FloatSerializer.instance.validate(bytes); - } - public CQL3Type asCQL3Type() { return CQL3Type.Native.FLOAT; } - @Override public TypeSerializer getSerializer() { return FloatSerializer.instance; diff --git a/src/java/org/apache/cassandra/db/marshal/InetAddressType.java b/src/java/org/apache/cassandra/db/marshal/InetAddressType.java index fa5ce959d6..c29e8acf70 100644 --- a/src/java/org/apache/cassandra/db/marshal/InetAddressType.java +++ b/src/java/org/apache/cassandra/db/marshal/InetAddressType.java @@ -32,26 +32,11 @@ public class InetAddressType extends AbstractType 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 return decompose(address); } - public void validate(ByteBuffer bytes) throws MarshalException - { - InetAddressSerializer.instance.validate(bytes); - } - public CQL3Type asCQL3Type() { return CQL3Type.Native.INET; } - @Override public TypeSerializer getSerializer() { return InetAddressSerializer.instance; diff --git a/src/java/org/apache/cassandra/db/marshal/Int32Type.java b/src/java/org/apache/cassandra/db/marshal/Int32Type.java index ec05cb12df..2f377211ed 100644 --- a/src/java/org/apache/cassandra/db/marshal/Int32Type.java +++ b/src/java/org/apache/cassandra/db/marshal/Int32Type.java @@ -33,16 +33,6 @@ public class Int32Type extends AbstractType { } // 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 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 return decompose(int32Type); } - public void validate(ByteBuffer bytes) throws MarshalException - { - Int32Serializer.instance.validate(bytes); - } - public CQL3Type asCQL3Type() { return CQL3Type.Native.INT; } - @Override public TypeSerializer getSerializer() { return Int32Serializer.instance; diff --git a/src/java/org/apache/cassandra/db/marshal/IntegerType.java b/src/java/org/apache/cassandra/db/marshal/IntegerType.java index d602442d1d..726769b531 100644 --- a/src/java/org/apache/cassandra/db/marshal/IntegerType.java +++ b/src/java/org/apache/cassandra/db/marshal/IntegerType.java @@ -58,16 +58,6 @@ public final class IntegerType extends AbstractType 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 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 return decompose(integerType); } - public void validate(ByteBuffer bytes) throws MarshalException - { - IntegerSerializer.instance.validate(bytes); - } - public CQL3Type asCQL3Type() { return CQL3Type.Native.VARINT; } - @Override public TypeSerializer getSerializer() { return IntegerSerializer.instance; diff --git a/src/java/org/apache/cassandra/db/marshal/LexicalUUIDType.java b/src/java/org/apache/cassandra/db/marshal/LexicalUUIDType.java index d1a6590bd8..303169f09f 100644 --- a/src/java/org/apache/cassandra/db/marshal/LexicalUUIDType.java +++ b/src/java/org/apache/cassandra/db/marshal/LexicalUUIDType.java @@ -34,16 +34,6 @@ public class LexicalUUIDType extends AbstractType { } // 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 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 } } - public void validate(ByteBuffer bytes) throws MarshalException - { - UUIDSerializer.instance.validate(bytes); - } - - @Override public TypeSerializer getSerializer() { return UUIDSerializer.instance; diff --git a/src/java/org/apache/cassandra/db/marshal/ListType.java b/src/java/org/apache/cassandra/db/marshal/ListType.java index 93c4dae2de..8331897450 100644 --- a/src/java/org/apache/cassandra/db/marshal/ListType.java +++ b/src/java/org/apache/cassandra/db/marshal/ListType.java @@ -33,7 +33,7 @@ public class ListType extends CollectionType> private static final Map, ListType> instances = new HashMap, ListType>(); public final AbstractType elements; - public final ListSerializer composer; + public final ListSerializer serializer; public static ListType getInstance(TypeParser parser) throws ConfigurationException, SyntaxException { @@ -59,7 +59,7 @@ public class ListType extends CollectionType> { super(Kind.LIST); this.elements = elements; - this.composer = ListSerializer.getInstance(elements.getSerializer()); + this.serializer = ListSerializer.getInstance(elements.getSerializer()); } public AbstractType nameComparator() @@ -72,26 +72,9 @@ public class ListType extends CollectionType> return elements; } - public List compose(ByteBuffer bytes) - { - return composer.serialize(bytes); - } - - /** - * Layout is: {@code ... } - * 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 value) - { - return composer.deserialize(value); - } - public TypeSerializer> getSerializer() { - return composer; + return serializer; } protected void appendToStringBuilder(StringBuilder sb) diff --git a/src/java/org/apache/cassandra/db/marshal/LocalByPartionerType.java b/src/java/org/apache/cassandra/db/marshal/LocalByPartionerType.java index 22b52144e7..2703489d84 100644 --- a/src/java/org/apache/cassandra/db/marshal/LocalByPartionerType.java +++ b/src/java/org/apache/cassandra/db/marshal/LocalByPartionerType.java @@ -39,11 +39,13 @@ public class LocalByPartionerType extends AbstractType extends AbstractType 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 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 return decompose(longType); } - public void validate(ByteBuffer bytes) throws MarshalException - { - - } - public CQL3Type asCQL3Type() { return CQL3Type.Native.BIGINT; } - @Override public TypeSerializer getSerializer() { return LongSerializer.instance; diff --git a/src/java/org/apache/cassandra/db/marshal/MapType.java b/src/java/org/apache/cassandra/db/marshal/MapType.java index 6d20166a50..39cf307923 100644 --- a/src/java/org/apache/cassandra/db/marshal/MapType.java +++ b/src/java/org/apache/cassandra/db/marshal/MapType.java @@ -36,7 +36,7 @@ public class MapType extends CollectionType> public final AbstractType keys; public final AbstractType values; - private final MapSerializer composer; + private final MapSerializer serializer; public static MapType getInstance(TypeParser parser) throws ConfigurationException, SyntaxException { @@ -64,7 +64,7 @@ public class MapType extends CollectionType> 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 nameComparator() @@ -77,65 +77,10 @@ public class MapType extends CollectionType> return values; } - public Map compose(ByteBuffer bytes) - { - try - { - ByteBuffer input = bytes.duplicate(); - int n = getUnsignedShort(input); - Map m = new LinkedHashMap(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 ... } - * 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 value) - { - List bbs = new ArrayList(2 * value.size()); - int size = 0; - for (Map.Entry 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> getSerializer() { - return composer; + return serializer; } protected void appendToStringBuilder(StringBuilder sb) @@ -144,7 +89,7 @@ public class MapType extends CollectionType> } /** - * 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> columns) { diff --git a/src/java/org/apache/cassandra/db/marshal/ReversedType.java b/src/java/org/apache/cassandra/db/marshal/ReversedType.java index b111fa6f36..a292f3b767 100644 --- a/src/java/org/apache/cassandra/db/marshal/ReversedType.java +++ b/src/java/org/apache/cassandra/db/marshal/ReversedType.java @@ -84,28 +84,12 @@ public class ReversedType extends AbstractType 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 getSerializer() { return baseType.getSerializer(); diff --git a/src/java/org/apache/cassandra/db/marshal/SetType.java b/src/java/org/apache/cassandra/db/marshal/SetType.java index 4979e17077..0f36ec12c9 100644 --- a/src/java/org/apache/cassandra/db/marshal/SetType.java +++ b/src/java/org/apache/cassandra/db/marshal/SetType.java @@ -33,7 +33,7 @@ public class SetType extends CollectionType> private static final Map, SetType> instances = new HashMap, SetType>(); public final AbstractType elements; - private final SetSerializer composer; + private final SetSerializer serializer; public static SetType getInstance(TypeParser parser) throws ConfigurationException, SyntaxException { @@ -59,7 +59,7 @@ public class SetType extends CollectionType> { super(Kind.SET); this.elements = elements; - this.composer = SetSerializer.getInstance(elements.getSerializer()); + this.serializer = SetSerializer.getInstance(elements.getSerializer()); } public AbstractType nameComparator() @@ -72,19 +72,9 @@ public class SetType extends CollectionType> return EmptyType.instance; } - public Set compose(ByteBuffer bytes) - { - return composer.serialize(bytes); - } - - public ByteBuffer decompose(Set value) - { - return composer.deserialize(value); - } - public TypeSerializer> getSerializer() { - return composer; + return serializer; } protected void appendToStringBuilder(StringBuilder sb) diff --git a/src/java/org/apache/cassandra/db/marshal/TimeUUIDType.java b/src/java/org/apache/cassandra/db/marshal/TimeUUIDType.java index d4f145e283..51cf47a459 100644 --- a/src/java/org/apache/cassandra/db/marshal/TimeUUIDType.java +++ b/src/java/org/apache/cassandra/db/marshal/TimeUUIDType.java @@ -38,16 +38,6 @@ public class TimeUUIDType extends AbstractType { } // 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 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 return idBytes; } - public void validate(ByteBuffer bytes) throws MarshalException - { - TimeUUIDSerializer.instance.validate(bytes); - - } - public CQL3Type asCQL3Type() { return CQL3Type.Native.TIMEUUID; } - @Override public TypeSerializer getSerializer() { return TimeUUIDSerializer.instance; diff --git a/src/java/org/apache/cassandra/db/marshal/TimestampType.java b/src/java/org/apache/cassandra/db/marshal/TimestampType.java index c525bdf8e4..ae008409dc 100644 --- a/src/java/org/apache/cassandra/db/marshal/TimestampType.java +++ b/src/java/org/apache/cassandra/db/marshal/TimestampType.java @@ -46,26 +46,11 @@ public class TimestampType extends AbstractType 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 return millis; } - public void validate(ByteBuffer bytes) throws MarshalException - { - TimestampSerializer.instance.validate(bytes); - } - @Override public boolean isCompatibleWith(AbstractType previous) { diff --git a/src/java/org/apache/cassandra/db/marshal/UTF8Type.java b/src/java/org/apache/cassandra/db/marshal/UTF8Type.java index 33284d76c7..0e18a45e03 100644 --- a/src/java/org/apache/cassandra/db/marshal/UTF8Type.java +++ b/src/java/org/apache/cassandra/db/marshal/UTF8Type.java @@ -30,36 +30,16 @@ public class UTF8Type extends AbstractType 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 return CQL3Type.Native.TEXT; } - @Override public TypeSerializer getSerializer() { return UTF8Serializer.instance; diff --git a/src/java/org/apache/cassandra/db/marshal/UUIDType.java b/src/java/org/apache/cassandra/db/marshal/UUIDType.java index 21e20b7bc4..ebb7021271 100644 --- a/src/java/org/apache/cassandra/db/marshal/UUIDType.java +++ b/src/java/org/apache/cassandra/db/marshal/UUIDType.java @@ -158,27 +158,6 @@ public class UUIDType extends AbstractType 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 return CQL3Type.Native.UUID; } - @Override public TypeSerializer getSerializer() { return UUIDSerializer.instance; diff --git a/src/java/org/apache/cassandra/serializers/AbstractTextSerializer.java b/src/java/org/apache/cassandra/serializers/AbstractTextSerializer.java index a132ce4c18..f1de6a460d 100644 --- a/src/java/org/apache/cassandra/serializers/AbstractTextSerializer.java +++ b/src/java/org/apache/cassandra/serializers/AbstractTextSerializer.java @@ -32,17 +32,7 @@ public abstract class AbstractTextSerializer implements TypeSerializer 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 } } + public ByteBuffer serialize(String value) + { + return ByteBufferUtil.bytes(value, charset); + } + public String toString(String value) { return value; diff --git a/src/java/org/apache/cassandra/serializers/BooleanSerializer.java b/src/java/org/apache/cassandra/serializers/BooleanSerializer.java index f2add98d57..dffecd6502 100644 --- a/src/java/org/apache/cassandra/serializers/BooleanSerializer.java +++ b/src/java/org/apache/cassandra/serializers/BooleanSerializer.java @@ -28,13 +28,16 @@ public class BooleanSerializer implements TypeSerializer 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 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(); diff --git a/src/java/org/apache/cassandra/serializers/BytesSerializer.java b/src/java/org/apache/cassandra/serializers/BytesSerializer.java index 6504f845e2..4dcaa829b0 100644 --- a/src/java/org/apache/cassandra/serializers/BytesSerializer.java +++ b/src/java/org/apache/cassandra/serializers/BytesSerializer.java @@ -43,14 +43,9 @@ public class BytesSerializer implements TypeSerializer // 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 getType() diff --git a/src/java/org/apache/cassandra/serializers/CollectionSerializer.java b/src/java/org/apache/cassandra/serializers/CollectionSerializer.java index 563e88b38b..9d4e4a4355 100644 --- a/src/java/org/apache/cassandra/serializers/CollectionSerializer.java +++ b/src/java/org/apache/cassandra/serializers/CollectionSerializer.java @@ -28,11 +28,6 @@ public abstract class CollectionSerializer implements TypeSerializer // 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 buffers, int elements, int size) { @@ -54,7 +49,6 @@ public abstract class CollectionSerializer implements TypeSerializer return pack(buffers, elements, size); } - protected static int getUnsignedShort(ByteBuffer bb) { int length = (bb.get() & 0xFF) << 8; diff --git a/src/java/org/apache/cassandra/serializers/DecimalSerializer.java b/src/java/org/apache/cassandra/serializers/DecimalSerializer.java index 21031b0d2b..0ffea9ed98 100644 --- a/src/java/org/apache/cassandra/serializers/DecimalSerializer.java +++ b/src/java/org/apache/cassandra/serializers/DecimalSerializer.java @@ -27,9 +27,9 @@ public class DecimalSerializer implements TypeSerializer { 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 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 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) diff --git a/src/java/org/apache/cassandra/serializers/DoubleSerializer.java b/src/java/org/apache/cassandra/serializers/DoubleSerializer.java index 9fbd627b79..13ca33bcde 100644 --- a/src/java/org/apache/cassandra/serializers/DoubleSerializer.java +++ b/src/java/org/apache/cassandra/serializers/DoubleSerializer.java @@ -26,12 +26,14 @@ public class DoubleSerializer implements TypeSerializer { 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 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(); diff --git a/src/java/org/apache/cassandra/serializers/EmptySerializer.java b/src/java/org/apache/cassandra/serializers/EmptySerializer.java index 2e082b6bf5..2ccecc5c51 100644 --- a/src/java/org/apache/cassandra/serializers/EmptySerializer.java +++ b/src/java/org/apache/cassandra/serializers/EmptySerializer.java @@ -26,12 +26,12 @@ public class EmptySerializer implements TypeSerializer { 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 throw new MarshalException("EmptyType only accept empty values"); } - public String getString(ByteBuffer bytes) - { - return ""; - } - public String toString(Void value) { return ""; diff --git a/src/java/org/apache/cassandra/serializers/FloatSerializer.java b/src/java/org/apache/cassandra/serializers/FloatSerializer.java index 35fc528001..94a24fec62 100644 --- a/src/java/org/apache/cassandra/serializers/FloatSerializer.java +++ b/src/java/org/apache/cassandra/serializers/FloatSerializer.java @@ -26,12 +26,15 @@ public class FloatSerializer implements TypeSerializer { 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 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); diff --git a/src/java/org/apache/cassandra/serializers/InetAddressSerializer.java b/src/java/org/apache/cassandra/serializers/InetAddressSerializer.java index 39d9a9ea2e..f5406f2d9f 100644 --- a/src/java/org/apache/cassandra/serializers/InetAddressSerializer.java +++ b/src/java/org/apache/cassandra/serializers/InetAddressSerializer.java @@ -28,8 +28,11 @@ public class InetAddressSerializer implements TypeSerializer { 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 } } - 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 } } - public String getString(ByteBuffer bytes) - { - return serialize(bytes).getHostAddress(); - } - public String toString(InetAddress value) { return value.getHostAddress(); diff --git a/src/java/org/apache/cassandra/serializers/Int32Serializer.java b/src/java/org/apache/cassandra/serializers/Int32Serializer.java index a6399cd8f6..73e350ff10 100644 --- a/src/java/org/apache/cassandra/serializers/Int32Serializer.java +++ b/src/java/org/apache/cassandra/serializers/Int32Serializer.java @@ -26,12 +26,12 @@ public class Int32Serializer implements TypeSerializer { 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 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); diff --git a/src/java/org/apache/cassandra/serializers/IntegerSerializer.java b/src/java/org/apache/cassandra/serializers/IntegerSerializer.java index 01ecb361ef..b028cd4458 100644 --- a/src/java/org/apache/cassandra/serializers/IntegerSerializer.java +++ b/src/java/org/apache/cassandra/serializers/IntegerSerializer.java @@ -27,12 +27,12 @@ public class IntegerSerializer implements TypeSerializer { 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 // 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); diff --git a/src/java/org/apache/cassandra/serializers/ListSerializer.java b/src/java/org/apache/cassandra/serializers/ListSerializer.java index 94ffbd0c6c..043f254d2d 100644 --- a/src/java/org/apache/cassandra/serializers/ListSerializer.java +++ b/src/java/org/apache/cassandra/serializers/ListSerializer.java @@ -45,7 +45,7 @@ public class ListSerializer extends CollectionSerializer> this.elements = elements; } - public List serialize(ByteBuffer bytes) + public List deserialize(ByteBuffer bytes) { try { @@ -59,7 +59,7 @@ public class ListSerializer extends CollectionSerializer> 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 extends CollectionSerializer> * 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 value) + public ByteBuffer serialize(List value) { List bbs = new ArrayList(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(); } diff --git a/src/java/org/apache/cassandra/serializers/LongSerializer.java b/src/java/org/apache/cassandra/serializers/LongSerializer.java index b103b033ad..277c395227 100644 --- a/src/java/org/apache/cassandra/serializers/LongSerializer.java +++ b/src/java/org/apache/cassandra/serializers/LongSerializer.java @@ -26,14 +26,14 @@ public class LongSerializer implements TypeSerializer { 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 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); diff --git a/src/java/org/apache/cassandra/serializers/MapSerializer.java b/src/java/org/apache/cassandra/serializers/MapSerializer.java index d7526e44f8..39b3cba756 100644 --- a/src/java/org/apache/cassandra/serializers/MapSerializer.java +++ b/src/java/org/apache/cassandra/serializers/MapSerializer.java @@ -50,7 +50,7 @@ public class MapSerializer extends CollectionSerializer> this.values = values; } - public Map serialize(ByteBuffer bytes) + public Map deserialize(ByteBuffer bytes) { try { @@ -71,7 +71,7 @@ public class MapSerializer extends CollectionSerializer> 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 extends CollectionSerializer> } } - public ByteBuffer deserialize(Map value) + /** + * Layout is: {@code ... } + * 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 value) { List bbs = new ArrayList(2 * value.size()); int size = 0; for (Map.Entry 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(); diff --git a/src/java/org/apache/cassandra/serializers/SetSerializer.java b/src/java/org/apache/cassandra/serializers/SetSerializer.java index be333187cd..5595077a01 100644 --- a/src/java/org/apache/cassandra/serializers/SetSerializer.java +++ b/src/java/org/apache/cassandra/serializers/SetSerializer.java @@ -45,7 +45,7 @@ public class SetSerializer extends CollectionSerializer> this.elements = elements; } - public Set serialize(ByteBuffer bytes) + public Set deserialize(ByteBuffer bytes) { try { @@ -59,7 +59,7 @@ public class SetSerializer extends CollectionSerializer> 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 extends CollectionSerializer> * 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 value) + public ByteBuffer serialize(Set value) { List bbs = new ArrayList(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(); } diff --git a/src/java/org/apache/cassandra/serializers/TimestampSerializer.java b/src/java/org/apache/cassandra/serializers/TimestampSerializer.java index 49766c713f..77bd59ae22 100644 --- a/src/java/org/apache/cassandra/serializers/TimestampSerializer.java +++ b/src/java/org/apache/cassandra/serializers/TimestampSerializer.java @@ -50,18 +50,14 @@ public class TimestampSerializer implements TypeSerializer 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 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); diff --git a/src/java/org/apache/cassandra/serializers/TypeSerializer.java b/src/java/org/apache/cassandra/serializers/TypeSerializer.java index 9d30508853..7b037c0c21 100644 --- a/src/java/org/apache/cassandra/serializers/TypeSerializer.java +++ b/src/java/org/apache/cassandra/serializers/TypeSerializer.java @@ -22,14 +22,15 @@ import java.nio.ByteBuffer; public interface TypeSerializer { - 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 getType(); diff --git a/src/java/org/apache/cassandra/serializers/UUIDSerializer.java b/src/java/org/apache/cassandra/serializers/UUIDSerializer.java index 4bfd6d5471..0e64fcad0c 100644 --- a/src/java/org/apache/cassandra/serializers/UUIDSerializer.java +++ b/src/java/org/apache/cassandra/serializers/UUIDSerializer.java @@ -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 { 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 // 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(); diff --git a/src/java/org/apache/cassandra/tools/Shuffle.java b/src/java/org/apache/cassandra/tools/Shuffle.java index d89337b209..502c2382f9 100644 --- a/src/java/org/apache/cassandra/tools/Shuffle.java +++ b/src/java/org/apache/cassandra/tools/Shuffle.java @@ -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()); diff --git a/src/java/org/apache/cassandra/utils/BooleanSerializer.java b/src/java/org/apache/cassandra/utils/BooleanSerializer.java index 88db2fca84..0c37f67d06 100644 --- a/src/java/org/apache/cassandra/utils/BooleanSerializer.java +++ b/src/java/org/apache/cassandra/utils/BooleanSerializer.java @@ -41,4 +41,4 @@ public class BooleanSerializer implements IVersionedSerializer { return 1; } -} \ No newline at end of file +} diff --git a/test/data/serialization/2.0/db.RowMutation.bin b/test/data/serialization/2.0/db.RowMutation.bin index 4b525d3658a5e515faaeeb15df19f4dc1a5c45d5..698b0b68b9ddcf684c2445abc3d3041ddaa4f938 100644 GIT binary patch delta 174 zcmeB|>6e+XkNE*h#>Rt=j39dALGj6Nc?>4|FzttMZ6^CNaZhGv{EaHhHTfF%Uxd&T s2-AA>Mb@-enV*9L;$ESxE*LCz=xe&8N7L#bAa500XN=A^-pY delta 174 zcmeB|>6e+XkNM7l#~TkiGJ@!d2gN79