Return-Path: X-Original-To: apmail-cassandra-commits-archive@www.apache.org Delivered-To: apmail-cassandra-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 395151005F for ; Mon, 8 Jul 2013 17:39:37 +0000 (UTC) Received: (qmail 25166 invoked by uid 500); 8 Jul 2013 17:39:37 -0000 Delivered-To: apmail-cassandra-commits-archive@cassandra.apache.org Received: (qmail 24946 invoked by uid 500); 8 Jul 2013 17:39:36 -0000 Mailing-List: contact commits-help@cassandra.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@cassandra.apache.org Delivered-To: mailing list commits@cassandra.apache.org Received: (qmail 24923 invoked by uid 99); 8 Jul 2013 17:39:36 -0000 Received: from tyr.zones.apache.org (HELO tyr.zones.apache.org) (140.211.11.114) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 08 Jul 2013 17:39:36 +0000 Received: by tyr.zones.apache.org (Postfix, from userid 65534) id 3F01E885BAC; Mon, 8 Jul 2013 17:39:36 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: slebresne@apache.org To: commits@cassandra.apache.org Date: Mon, 08 Jul 2013 17:39:37 -0000 Message-Id: In-Reply-To: <49bcb7e7f1944961b44825b0d1a9eb1f@git.apache.org> References: <49bcb7e7f1944961b44825b0d1a9eb1f@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: [2/3] Renames post 4495 http://git-wip-us.apache.org/repos/asf/cassandra/blob/fc8b76f7/src/java/org/apache/cassandra/serializers/Int32Serializer.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/cassandra/serializers/Int32Serializer.java b/src/java/org/apache/cassandra/serializers/Int32Serializer.java new file mode 100644 index 0000000..a6399cd --- /dev/null +++ b/src/java/org/apache/cassandra/serializers/Int32Serializer.java @@ -0,0 +1,68 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.cassandra.serializers; + +import org.apache.cassandra.utils.ByteBufferUtil; + +import java.nio.ByteBuffer; + +public class Int32Serializer implements TypeSerializer +{ + public static final Int32Serializer instance = new Int32Serializer(); + + public Integer serialize(ByteBuffer bytes) + { + return ByteBufferUtil.toInt(bytes); + } + + public ByteBuffer deserialize(Integer value) + { + return value == null ? ByteBufferUtil.EMPTY_BYTE_BUFFER : ByteBufferUtil.bytes(value); + } + + public void validate(ByteBuffer bytes) throws MarshalException + { + if (bytes.remaining() != 4 && bytes.remaining() != 0) + 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); + } + + public Class getType() + { + return Integer.class; + } +} http://git-wip-us.apache.org/repos/asf/cassandra/blob/fc8b76f7/src/java/org/apache/cassandra/serializers/IntegerSerializer.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/cassandra/serializers/IntegerSerializer.java b/src/java/org/apache/cassandra/serializers/IntegerSerializer.java new file mode 100644 index 0000000..01ecb36 --- /dev/null +++ b/src/java/org/apache/cassandra/serializers/IntegerSerializer.java @@ -0,0 +1,64 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.cassandra.serializers; + +import org.apache.cassandra.utils.ByteBufferUtil; + +import java.math.BigInteger; +import java.nio.ByteBuffer; + +public class IntegerSerializer implements TypeSerializer +{ + public static final IntegerSerializer instance = new IntegerSerializer(); + + public BigInteger serialize(ByteBuffer bytes) + { + return new BigInteger(ByteBufferUtil.getArray(bytes)); + } + + public ByteBuffer deserialize(BigInteger value) + { + return ByteBuffer.wrap(value.toByteArray()); + } + + public void validate(ByteBuffer bytes) throws MarshalException + { + // 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); + } + + public Class getType() + { + return BigInteger.class; + } +} http://git-wip-us.apache.org/repos/asf/cassandra/blob/fc8b76f7/src/java/org/apache/cassandra/serializers/ListSerializer.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/cassandra/serializers/ListSerializer.java b/src/java/org/apache/cassandra/serializers/ListSerializer.java new file mode 100644 index 0000000..94ffbd0 --- /dev/null +++ b/src/java/org/apache/cassandra/serializers/ListSerializer.java @@ -0,0 +1,115 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.cassandra.serializers; + +import java.nio.BufferUnderflowException; +import java.nio.ByteBuffer; +import java.util.*; + +public class ListSerializer extends CollectionSerializer> +{ + // interning instances + private static final Map, ListSerializer> instances = new HashMap, ListSerializer>(); + + public final TypeSerializer elements; + + public static synchronized ListSerializer getInstance(TypeSerializer elements) + { + ListSerializer t = instances.get(elements); + if (t == null) + { + t = new ListSerializer(elements); + instances.put(elements, t); + } + return t; + } + + private ListSerializer(TypeSerializer elements) + { + this.elements = elements; + } + + public List serialize(ByteBuffer bytes) + { + try + { + ByteBuffer input = bytes.duplicate(); + int n = getUnsignedShort(input); + List l = new ArrayList(n); + for (int i = 0; i < n; i++) + { + int s = getUnsignedShort(input); + byte[] data = new byte[s]; + input.get(data); + ByteBuffer databb = ByteBuffer.wrap(data); + elements.validate(databb); + l.add(elements.serialize(databb)); + } + return l; + } + catch (BufferUnderflowException e) + { + throw new MarshalException("Not enough bytes to read a list"); + } + } + + /** + * 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 deserialize(List value) + { + List bbs = new ArrayList(value.size()); + int size = 0; + for (T elt : value) + { + ByteBuffer bb = elements.deserialize(elt); + bbs.add(bb); + size += 2 + bb.remaining(); + } + return pack(bbs, value.size(), size); + } + + public String toString(List value) + { + StringBuffer sb = new StringBuffer(); + boolean isFirst = true; + for (T element : value) + { + if (isFirst) + { + isFirst = false; + } + else + { + sb.append("; "); + } + sb.append(elements.toString(element)); + } + return sb.toString(); + } + + public Class> getType() + { + return (Class) List.class; + } +} http://git-wip-us.apache.org/repos/asf/cassandra/blob/fc8b76f7/src/java/org/apache/cassandra/serializers/LongSerializer.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/cassandra/serializers/LongSerializer.java b/src/java/org/apache/cassandra/serializers/LongSerializer.java new file mode 100644 index 0000000..b103b03 --- /dev/null +++ b/src/java/org/apache/cassandra/serializers/LongSerializer.java @@ -0,0 +1,68 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.cassandra.serializers; + +import org.apache.cassandra.utils.ByteBufferUtil; + +import java.nio.ByteBuffer; + +public class LongSerializer implements TypeSerializer +{ + public static final LongSerializer instance = new LongSerializer(); + + public Long serialize(ByteBuffer bytes) + { + return ByteBufferUtil.toLong(bytes); + } + + public ByteBuffer deserialize(Long value) + { + return ByteBufferUtil.bytes(value); + } + + 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 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); + } + + public Class getType() + { + return Long.class; + } +} http://git-wip-us.apache.org/repos/asf/cassandra/blob/fc8b76f7/src/java/org/apache/cassandra/serializers/MapSerializer.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/cassandra/serializers/MapSerializer.java b/src/java/org/apache/cassandra/serializers/MapSerializer.java new file mode 100644 index 0000000..d7526e4 --- /dev/null +++ b/src/java/org/apache/cassandra/serializers/MapSerializer.java @@ -0,0 +1,126 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.cassandra.serializers; + +import org.apache.cassandra.utils.Pair; + +import java.nio.BufferUnderflowException; +import java.nio.ByteBuffer; +import java.util.*; + +public class MapSerializer extends CollectionSerializer> +{ + // interning instances + private static final Map, TypeSerializer>, MapSerializer> instances = new HashMap, TypeSerializer>, MapSerializer>(); + + public final TypeSerializer keys; + public final TypeSerializer values; + + public static synchronized MapSerializer getInstance(TypeSerializer keys, TypeSerializer values) + { + Pair, TypeSerializer> p = Pair., TypeSerializer>create(keys, values); + MapSerializer t = instances.get(p); + if (t == null) + { + t = new MapSerializer(keys, values); + instances.put(p, t); + } + return t; + } + + private MapSerializer(TypeSerializer keys, TypeSerializer values) + { + this.keys = keys; + this.values = values; + } + + public Map serialize(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.serialize(kbb), values.serialize(vbb)); + } + return m; + } + catch (BufferUnderflowException e) + { + throw new MarshalException("Not enough bytes to read a map"); + } + } + + public ByteBuffer deserialize(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()); + bbs.add(bbk); + bbs.add(bbv); + size += 4 + bbk.remaining() + bbv.remaining(); + } + return pack(bbs, value.size(), size); + } + + public String toString(Map value) + { + StringBuffer sb = new StringBuffer(); + boolean isFirst = true; + for (Map.Entry element : value.entrySet()) + { + if (isFirst) + { + isFirst = false; + } + else + { + sb.append("; "); + } + sb.append('('); + sb.append(keys.toString(element.getKey())); + sb.append(", "); + sb.append(values.toString(element.getValue())); + sb.append(')'); + } + return sb.toString(); + } + + public Class> getType() + { + return (Class)Map.class; + } +} http://git-wip-us.apache.org/repos/asf/cassandra/blob/fc8b76f7/src/java/org/apache/cassandra/serializers/MarshalException.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/cassandra/serializers/MarshalException.java b/src/java/org/apache/cassandra/serializers/MarshalException.java new file mode 100644 index 0000000..d5a8385 --- /dev/null +++ b/src/java/org/apache/cassandra/serializers/MarshalException.java @@ -0,0 +1,32 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.cassandra.serializers; + +public class MarshalException extends RuntimeException +{ + public MarshalException(String message) + { + super(message); + } + + public MarshalException(String message, Throwable cause) + { + super(message, cause); + } +} http://git-wip-us.apache.org/repos/asf/cassandra/blob/fc8b76f7/src/java/org/apache/cassandra/serializers/SetSerializer.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/cassandra/serializers/SetSerializer.java b/src/java/org/apache/cassandra/serializers/SetSerializer.java new file mode 100644 index 0000000..be33318 --- /dev/null +++ b/src/java/org/apache/cassandra/serializers/SetSerializer.java @@ -0,0 +1,115 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.cassandra.serializers; + +import java.nio.BufferUnderflowException; +import java.nio.ByteBuffer; +import java.util.*; + +public class SetSerializer extends CollectionSerializer> +{ + // interning instances + private static final Map, SetSerializer> instances = new HashMap, SetSerializer>(); + + public final TypeSerializer elements; + + public static synchronized SetSerializer getInstance(TypeSerializer elements) + { + SetSerializer t = instances.get(elements); + if (t == null) + { + t = new SetSerializer(elements); + instances.put(elements, t); + } + return t; + } + + private SetSerializer(TypeSerializer elements) + { + this.elements = elements; + } + + public Set serialize(ByteBuffer bytes) + { + try + { + ByteBuffer input = bytes.duplicate(); + int n = getUnsignedShort(input); + Set l = new LinkedHashSet(n); + for (int i = 0; i < n; i++) + { + int s = getUnsignedShort(input); + byte[] data = new byte[s]; + input.get(data); + ByteBuffer databb = ByteBuffer.wrap(data); + elements.validate(databb); + l.add(elements.serialize(databb)); + } + return l; + } + catch (BufferUnderflowException e) + { + throw new MarshalException("Not enough bytes to read a list"); + } + } + + /** + * 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 deserialize(Set value) + { + List bbs = new ArrayList(value.size()); + int size = 0; + for (T elt : value) + { + ByteBuffer bb = elements.deserialize(elt); + bbs.add(bb); + size += 2 + bb.remaining(); + } + return pack(bbs, value.size(), size); + } + + public String toString(Set value) + { + StringBuffer sb = new StringBuffer(); + boolean isFirst = true; + for (T element : value) + { + if (isFirst) + { + isFirst = false; + } + else + { + sb.append("; "); + } + sb.append(elements.toString(element)); + } + return sb.toString(); + } + + public Class> getType() + { + return (Class) Set.class; + } +} http://git-wip-us.apache.org/repos/asf/cassandra/blob/fc8b76f7/src/java/org/apache/cassandra/serializers/TimeUUIDSerializer.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/cassandra/serializers/TimeUUIDSerializer.java b/src/java/org/apache/cassandra/serializers/TimeUUIDSerializer.java new file mode 100644 index 0000000..22750be --- /dev/null +++ b/src/java/org/apache/cassandra/serializers/TimeUUIDSerializer.java @@ -0,0 +1,40 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.cassandra.serializers; + +import java.nio.ByteBuffer; + +public class TimeUUIDSerializer extends UUIDSerializer +{ + @Override + public void validate(ByteBuffer bytes) throws MarshalException + { + super.validate(bytes); + + // Super class only validates the Time UUID + ByteBuffer slice = bytes.slice(); + // version is bits 4-7 of byte 6. + if (bytes.remaining() > 0) + { + slice.position(6); + if ((slice.get() & 0xf0) != 0x10) + throw new MarshalException("Invalid version for TimeUUID type."); + } + } +} http://git-wip-us.apache.org/repos/asf/cassandra/blob/fc8b76f7/src/java/org/apache/cassandra/serializers/TimestampSerializer.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/cassandra/serializers/TimestampSerializer.java b/src/java/org/apache/cassandra/serializers/TimestampSerializer.java new file mode 100644 index 0000000..49766c7 --- /dev/null +++ b/src/java/org/apache/cassandra/serializers/TimestampSerializer.java @@ -0,0 +1,97 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.cassandra.serializers; + +import org.apache.cassandra.utils.ByteBufferUtil; + +import java.nio.ByteBuffer; +import java.text.SimpleDateFormat; +import java.util.Date; + +public class TimestampSerializer implements TypeSerializer +{ + public static final String[] iso8601Patterns = new String[] { + "yyyy-MM-dd HH:mm", + "yyyy-MM-dd HH:mm:ss", + "yyyy-MM-dd HH:mmZ", + "yyyy-MM-dd HH:mm:ssZ", + "yyyy-MM-dd'T'HH:mm", + "yyyy-MM-dd'T'HH:mmZ", + "yyyy-MM-dd'T'HH:mm:ss", + "yyyy-MM-dd'T'HH:mm:ssZ", + "yyyy-MM-dd", + "yyyy-MM-ddZ" + }; + + static final String DEFAULT_FORMAT = iso8601Patterns[3]; + + static final ThreadLocal FORMATTER = new ThreadLocal() + { + protected SimpleDateFormat initialValue() + { + return new SimpleDateFormat(DEFAULT_FORMAT); + } + }; + + public static final TimestampSerializer instance = new TimestampSerializer(); + + public Date serialize(ByteBuffer bytes) + { + return bytes.remaining() > 0 + ? new Date(ByteBufferUtil.toLong(bytes)) + : null; + } + + public ByteBuffer deserialize(Date value) + { + return (value == null) + ? ByteBufferUtil.EMPTY_BYTE_BUFFER + : ByteBufferUtil.bytes(value.getTime()); + } + + 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 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); + } + + public Class getType() + { + return Date.class; + } +} http://git-wip-us.apache.org/repos/asf/cassandra/blob/fc8b76f7/src/java/org/apache/cassandra/serializers/TypeSerializer.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/cassandra/serializers/TypeSerializer.java b/src/java/org/apache/cassandra/serializers/TypeSerializer.java new file mode 100644 index 0000000..9d30508 --- /dev/null +++ b/src/java/org/apache/cassandra/serializers/TypeSerializer.java @@ -0,0 +1,36 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.cassandra.serializers; + +import java.nio.ByteBuffer; + +public interface TypeSerializer +{ + public T serialize(ByteBuffer bytes); + public ByteBuffer deserialize(T value); + + + /* validate that the byte array is a valid sequence for the type we are supposed to be comparing */ + public void validate(ByteBuffer bytes) throws MarshalException; + + public String getString(ByteBuffer bytes); + public String toString(T value); + + public Class getType(); +} http://git-wip-us.apache.org/repos/asf/cassandra/blob/fc8b76f7/src/java/org/apache/cassandra/serializers/UTF8Serializer.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/cassandra/serializers/UTF8Serializer.java b/src/java/org/apache/cassandra/serializers/UTF8Serializer.java new file mode 100644 index 0000000..c0790a6 --- /dev/null +++ b/src/java/org/apache/cassandra/serializers/UTF8Serializer.java @@ -0,0 +1,158 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.cassandra.serializers; + +import org.apache.cassandra.utils.ByteBufferUtil; + +import java.nio.ByteBuffer; +import java.nio.charset.CharacterCodingException; +import java.nio.charset.Charset; + +public class UTF8Serializer extends AbstractTextSerializer +{ + public static final UTF8Serializer instance = new UTF8Serializer(); + + private UTF8Serializer() + { + super(Charset.forName("UTF-8")); + } + + public void validate(ByteBuffer bytes) throws MarshalException + { + if (!UTF8Validator.validate(bytes)) + throw new MarshalException("String didn't validate."); + } + + static class UTF8Validator + { + enum State { + START, + TWO, + TWO_80, + THREE_a0bf, + THREE_80bf_1, + THREE_80bf_2, + FOUR_90bf, + FOUR_80bf_3, + }; + + // since we're not converting to java strings, we don't need to worry about converting to surrogates. + // buf has already been sliced/duplicated. + static boolean validate(ByteBuffer buf) + { + buf = buf.slice(); + int b = 0; + State state = State.START; + while (buf.remaining() > 0) + { + b = buf.get(); + switch (state) + { + case START: + if (b >= 0) + { + // ascii, state stays start. + if (b > 127) + return false; + } + else if ((b >> 5) == -2) + { + // validate first byte of 2-byte char, 0xc2-0xdf + if (b == (byte) 0xc0) + // speical case: modified utf8 null is 0xc080. + state = State.TWO_80; + else if ((b & 0x1e) == 0) + return false; + state = State.TWO; + } + else if ((b >> 4) == -2) + { + // 3 bytes. first byte will be 0xe0 or 0xe1-0xef. handling of second byte will differ. + // so 0xe0,0xa0-0xbf,0x80-0xbf or 0xe1-0xef,0x80-0xbf,0x80-0xbf. + if (b == (byte)0xe0) + state = State.THREE_a0bf; + else + state = State.THREE_80bf_2; + break; + } + else if ((b >> 3) == -2) + { + // 4 bytes. this is where the fun starts. + if (b == (byte)0xf0) + // 0xf0, 0x90-0xbf, 0x80-0xbf, 0x80-0xbf + state = State.FOUR_90bf; + else if (b == (byte)0xf4) + // 0xf4, 0x80-0xbf, 0x80-0xbf, 0x80-0xbf + state = State.FOUR_80bf_3; + else + // 0xf1-0xf3, 0x80-0xbf, 0x80-0xbf, 0x80-0xbf + state = State.FOUR_80bf_3; + break; + } + else + return false; // malformed. + break; + case TWO: + // validate second byte of 2-byte char, 0x80-0xbf + if ((b & 0xc0) != 0x80) + return false; + state = State.START; + break; + case TWO_80: + if (b != (byte)0x80) + return false; + state = State.START; + break; + case THREE_a0bf: + if ((b & 0xe0) == 0x80) + return false; + state = State.THREE_80bf_1; + break; + case THREE_80bf_1: + // expecting 0x80-0xbf + if ((b & 0xc0) != 0x80) + return false; + state = State.START; + break; + case THREE_80bf_2: + // expecting 0x80-bf and then another of the same. + if ((b & 0xc0) != 0x80) + return false; + state = State.THREE_80bf_1; + break; + case FOUR_90bf: + // expecting 0x90-bf. 2nd byte of 4byte sequence. after that it should degrade to 80-bf,80-bf (like 3byte seq). + if ((b & 0x30) == 0) + return false; + state = State.THREE_80bf_2; + break; + case FOUR_80bf_3: + // expecting 0x80-bf 3 times. degenerates to THREE_80bf_2. + if ((b & 0xc0) != 0x80) + return false; + state = State.THREE_80bf_2; + break; + default: + return false; // invalid state. + } + } + // if state != start, we've got underflow. that's an error. + return state == State.START; + } + } +} http://git-wip-us.apache.org/repos/asf/cassandra/blob/fc8b76f7/src/java/org/apache/cassandra/serializers/UUIDSerializer.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/cassandra/serializers/UUIDSerializer.java b/src/java/org/apache/cassandra/serializers/UUIDSerializer.java new file mode 100644 index 0000000..4bfd6d5 --- /dev/null +++ b/src/java/org/apache/cassandra/serializers/UUIDSerializer.java @@ -0,0 +1,69 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.cassandra.serializers; + +import org.apache.cassandra.utils.UUIDGen; + +import java.nio.ByteBuffer; +import java.util.UUID; + +public class UUIDSerializer implements TypeSerializer +{ + public static final UUIDSerializer instance = new UUIDSerializer(); + + public UUID serialize(ByteBuffer bytes) + { + return UUIDGen.getUUID(bytes); + } + + public ByteBuffer deserialize(UUID value) + { + return ByteBuffer.wrap(UUIDGen.decompose(value)); + } + + public void validate(ByteBuffer bytes) throws MarshalException + { + if (bytes.remaining() != 16 && bytes.remaining() != 0) + throw new MarshalException(String.format("UUID should be 16 or 0 bytes (%d)", bytes.remaining())); + // 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(); + } + + public Class getType() + { + return UUID.class; + } +} http://git-wip-us.apache.org/repos/asf/cassandra/blob/fc8b76f7/src/java/org/apache/cassandra/thrift/CassandraServer.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/cassandra/thrift/CassandraServer.java b/src/java/org/apache/cassandra/thrift/CassandraServer.java index c9cb9eb..6211dbb 100644 --- a/src/java/org/apache/cassandra/thrift/CassandraServer.java +++ b/src/java/org/apache/cassandra/thrift/CassandraServer.java @@ -45,12 +45,11 @@ import org.apache.cassandra.cql.CQLStatement; import org.apache.cassandra.cql.QueryProcessor; import org.apache.cassandra.db.*; import org.apache.cassandra.db.context.CounterContext; -import org.apache.cassandra.db.columniterator.IdentityQueryFilter; import org.apache.cassandra.db.filter.IDiskAtomFilter; import org.apache.cassandra.db.filter.NamesQueryFilter; import org.apache.cassandra.db.filter.SliceQueryFilter; import org.apache.cassandra.db.marshal.CompositeType; -import org.apache.cassandra.type.MarshalException; +import org.apache.cassandra.serializers.MarshalException; import org.apache.cassandra.db.marshal.TimeUUIDType; import org.apache.cassandra.dht.*; import org.apache.cassandra.exceptions.*; http://git-wip-us.apache.org/repos/asf/cassandra/blob/fc8b76f7/src/java/org/apache/cassandra/thrift/ThriftValidation.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/cassandra/thrift/ThriftValidation.java b/src/java/org/apache/cassandra/thrift/ThriftValidation.java index 6f14a37..af2496a 100644 --- a/src/java/org/apache/cassandra/thrift/ThriftValidation.java +++ b/src/java/org/apache/cassandra/thrift/ThriftValidation.java @@ -20,7 +20,7 @@ package org.apache.cassandra.thrift; import java.nio.ByteBuffer; import java.util.*; -import org.apache.cassandra.type.MarshalException; +import org.apache.cassandra.serializers.MarshalException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; http://git-wip-us.apache.org/repos/asf/cassandra/blob/fc8b76f7/src/java/org/apache/cassandra/tools/SSTableImport.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/cassandra/tools/SSTableImport.java b/src/java/org/apache/cassandra/tools/SSTableImport.java index 6e032d0..36f19cc 100644 --- a/src/java/org/apache/cassandra/tools/SSTableImport.java +++ b/src/java/org/apache/cassandra/tools/SSTableImport.java @@ -28,7 +28,7 @@ import java.util.SortedMap; import java.util.TreeMap; import java.util.concurrent.TimeUnit; -import org.apache.cassandra.type.MarshalException; +import org.apache.cassandra.serializers.MarshalException; import org.apache.commons.cli.CommandLine; import org.apache.commons.cli.CommandLineParser; import org.apache.commons.cli.Option; http://git-wip-us.apache.org/repos/asf/cassandra/blob/fc8b76f7/src/java/org/apache/cassandra/tools/Shuffle.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/cassandra/tools/Shuffle.java b/src/java/org/apache/cassandra/tools/Shuffle.java index 0c5c92a..d89337b 100644 --- a/src/java/org/apache/cassandra/tools/Shuffle.java +++ b/src/java/org/apache/cassandra/tools/Shuffle.java @@ -43,7 +43,7 @@ import javax.management.MBeanServerConnection; import javax.management.MalformedObjectNameException; import javax.management.ObjectName; -import org.apache.cassandra.type.TimestampSerializer; +import org.apache.cassandra.serializers.TimestampSerializer; import org.apache.cassandra.dht.IPartitioner; import org.apache.cassandra.dht.Token; import org.apache.cassandra.locator.EndpointSnitchInfoMBean; http://git-wip-us.apache.org/repos/asf/cassandra/blob/fc8b76f7/src/java/org/apache/cassandra/type/AbstractSerializer.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/cassandra/type/AbstractSerializer.java b/src/java/org/apache/cassandra/type/AbstractSerializer.java deleted file mode 100644 index 85a0e39..0000000 --- a/src/java/org/apache/cassandra/type/AbstractSerializer.java +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.cassandra.type; - -import java.nio.ByteBuffer; - -public abstract class AbstractSerializer -{ - public abstract T serialize(ByteBuffer bytes); - public abstract ByteBuffer deserialize(T value); - - - /* 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 abstract String getString(ByteBuffer bytes); - public abstract String toString(T value); - - public abstract Class getType(); -} http://git-wip-us.apache.org/repos/asf/cassandra/blob/fc8b76f7/src/java/org/apache/cassandra/type/AsciiSerializer.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/cassandra/type/AsciiSerializer.java b/src/java/org/apache/cassandra/type/AsciiSerializer.java deleted file mode 100644 index 72a8312..0000000 --- a/src/java/org/apache/cassandra/type/AsciiSerializer.java +++ /dev/null @@ -1,80 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.cassandra.type; - -import org.apache.cassandra.utils.ByteBufferUtil; - -import java.nio.ByteBuffer; -import java.nio.charset.CharacterCodingException; -import java.nio.charset.Charset; - -public class AsciiSerializer extends AbstractSerializer -{ - public static final AsciiSerializer instance = new AsciiSerializer(); - private static final Charset US_ASCII = Charset.forName("US-ASCII"); - - @Override - public String serialize(ByteBuffer bytes) - { - return getString(bytes); - } - - @Override - public ByteBuffer deserialize(String value) - { - return ByteBufferUtil.bytes(value, US_ASCII); - } - - @Override - public void validate(ByteBuffer bytes) throws MarshalException - { - // 0-127 - for (int i = bytes.position(); i < bytes.limit(); i++) - { - byte b = bytes.get(i); - if (b < 0 || b > 127) - throw new MarshalException("Invalid byte for ascii: " + Byte.toString(b)); - } - } - - @Override - public String getString(ByteBuffer bytes) - { - try - { - return ByteBufferUtil.string(bytes, US_ASCII); - } - catch (CharacterCodingException e) - { - throw new MarshalException("Invalid ascii bytes " + ByteBufferUtil.bytesToHex(bytes)); - } - } - - @Override - public String toString(String value) - { - return value; - } - - @Override - public Class getType() - { - return String.class; - } -} http://git-wip-us.apache.org/repos/asf/cassandra/blob/fc8b76f7/src/java/org/apache/cassandra/type/BooleanSerializer.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/cassandra/type/BooleanSerializer.java b/src/java/org/apache/cassandra/type/BooleanSerializer.java deleted file mode 100644 index e2d200c..0000000 --- a/src/java/org/apache/cassandra/type/BooleanSerializer.java +++ /dev/null @@ -1,79 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.cassandra.type; - -import org.apache.cassandra.utils.ByteBufferUtil; - -import java.nio.ByteBuffer; - -public class BooleanSerializer extends AbstractSerializer -{ - public static final BooleanSerializer instance = new BooleanSerializer(); - - @Override - public Boolean serialize(ByteBuffer bytes) - { - byte value = bytes.get(bytes.position()); - return value != 0; - } - - @Override - public ByteBuffer deserialize(Boolean value) - { - return (value == null) ? ByteBufferUtil.EMPTY_BYTE_BUFFER - : value ? ByteBuffer.wrap(new byte[] {1}) // true - : ByteBuffer.wrap(new byte[] {0}); // false - } - - @Override - public void validate(ByteBuffer bytes) throws MarshalException - { - if (bytes.remaining() != 1 && bytes.remaining() != 0) - throw new MarshalException(String.format("Expected 1 or 0 byte value (%d)", bytes.remaining())); - } - - @Override - 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(); - } - - @Override - public String toString(Boolean value) - { - return value == null ? "" : value.toString(); - } - - @Override - public Class getType() - { - return Boolean.class; - } - -} http://git-wip-us.apache.org/repos/asf/cassandra/blob/fc8b76f7/src/java/org/apache/cassandra/type/BytesSerializer.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/cassandra/type/BytesSerializer.java b/src/java/org/apache/cassandra/type/BytesSerializer.java deleted file mode 100644 index 2889739..0000000 --- a/src/java/org/apache/cassandra/type/BytesSerializer.java +++ /dev/null @@ -1,66 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.cassandra.type; - -import org.apache.cassandra.utils.ByteBufferUtil; - -import java.nio.ByteBuffer; - -public class BytesSerializer extends AbstractSerializer -{ - public static final BytesSerializer instance = new BytesSerializer(); - - @Override - public ByteBuffer serialize(ByteBuffer bytes) - { - // We make a copy in case the user modifies the input - return bytes.duplicate(); - } - - @Override - public ByteBuffer deserialize(ByteBuffer value) - { - // This is from the DB, so it is not shared with someone else - return value; - } - - @Override - public void validate(ByteBuffer bytes) throws MarshalException - { - // all bytes are legal. - } - - @Override - public String getString(ByteBuffer bytes) - { - return ByteBufferUtil.bytesToHex(bytes); - } - - @Override - public String toString(ByteBuffer value) - { - return getString(value); - } - - @Override - public Class getType() - { - return ByteBuffer.class; - } -} http://git-wip-us.apache.org/repos/asf/cassandra/blob/fc8b76f7/src/java/org/apache/cassandra/type/CollectionSerializer.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/cassandra/type/CollectionSerializer.java b/src/java/org/apache/cassandra/type/CollectionSerializer.java deleted file mode 100644 index 2eb3944..0000000 --- a/src/java/org/apache/cassandra/type/CollectionSerializer.java +++ /dev/null @@ -1,64 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.cassandra.type; - -import java.nio.ByteBuffer; -import java.util.List; - -public abstract class CollectionSerializer extends AbstractSerializer -{ - @Override - public void validate(ByteBuffer bytes) throws MarshalException - { - // 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) - { - ByteBuffer result = ByteBuffer.allocate(2 + size); - result.putShort((short)elements); - for (ByteBuffer bb : buffers) - { - result.putShort((short)bb.remaining()); - result.put(bb.duplicate()); - } - return (ByteBuffer)result.flip(); - } - - public static ByteBuffer pack(List buffers, int elements) - { - int size = 0; - for (ByteBuffer bb : buffers) - size += 2 + bb.remaining(); - return pack(buffers, elements, size); - } - - - protected static int getUnsignedShort(ByteBuffer bb) - { - int length = (bb.get() & 0xFF) << 8; - return length | (bb.get() & 0xFF); - } -} http://git-wip-us.apache.org/repos/asf/cassandra/blob/fc8b76f7/src/java/org/apache/cassandra/type/CounterSerializer.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/cassandra/type/CounterSerializer.java b/src/java/org/apache/cassandra/type/CounterSerializer.java index c0625c3..b08ec1e 100644 --- a/src/java/org/apache/cassandra/type/CounterSerializer.java +++ b/src/java/org/apache/cassandra/type/CounterSerializer.java @@ -15,8 +15,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - -package org.apache.cassandra.type; +package org.apache.cassandra.serializers; public class CounterSerializer extends LongSerializer { http://git-wip-us.apache.org/repos/asf/cassandra/blob/fc8b76f7/src/java/org/apache/cassandra/type/DecimalSerializer.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/cassandra/type/DecimalSerializer.java b/src/java/org/apache/cassandra/type/DecimalSerializer.java deleted file mode 100644 index b8ad00b..0000000 --- a/src/java/org/apache/cassandra/type/DecimalSerializer.java +++ /dev/null @@ -1,94 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.cassandra.type; - -import org.apache.cassandra.utils.ByteBufferUtil; - -import java.math.BigDecimal; -import java.math.BigInteger; -import java.nio.ByteBuffer; - -public class DecimalSerializer extends AbstractSerializer -{ - public static final DecimalSerializer instance = new DecimalSerializer(); - - @Override - public BigDecimal serialize(ByteBuffer bytes) - { - if (bytes == null) - return null; - - // do not consume the contents of the ByteBuffer - bytes = bytes.duplicate(); - int scale = bytes.getInt(); - byte[] bibytes = new byte[bytes.remaining()]; - bytes.get(bibytes); - - BigInteger bi = new BigInteger(bibytes); - return new BigDecimal(bi, scale); - } - - @Override - public ByteBuffer deserialize(BigDecimal value) - { - if (value == null) - return ByteBufferUtil.EMPTY_BYTE_BUFFER; - - BigInteger bi = value.unscaledValue(); - Integer scale = value.scale(); - byte[] bibytes = bi.toByteArray(); - byte[] sbytes = ByteBufferUtil.bytes(scale).array(); - byte[] bytes = new byte[bi.toByteArray().length + 4]; - - for (int i = 0; i < 4; i++) - bytes[i] = sbytes[i]; - for (int i = 4; i < bibytes.length + 4; i++) - bytes[i] = bibytes[i - 4]; - - return ByteBuffer.wrap(bytes); - } - - @Override - public void validate(ByteBuffer bytes) throws MarshalException - { - // no useful check for invalid decimals. - } - - @Override - public String getString(ByteBuffer bytes) - { - if (bytes.remaining() == 0) - { - return ""; - } - return serialize(bytes).toPlainString(); - } - - @Override - public String toString(BigDecimal value) - { - return value == null ? "" : value.toPlainString(); - } - - @Override - public Class getType() - { - return BigDecimal.class; - } -} http://git-wip-us.apache.org/repos/asf/cassandra/blob/fc8b76f7/src/java/org/apache/cassandra/type/DoubleSerializer.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/cassandra/type/DoubleSerializer.java b/src/java/org/apache/cassandra/type/DoubleSerializer.java deleted file mode 100644 index d9b1776..0000000 --- a/src/java/org/apache/cassandra/type/DoubleSerializer.java +++ /dev/null @@ -1,74 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.cassandra.type; - -import org.apache.cassandra.utils.ByteBufferUtil; - -import java.nio.ByteBuffer; - -public class DoubleSerializer extends AbstractSerializer -{ - public static final DoubleSerializer instance = new DoubleSerializer(); - - @Override - public Double serialize(ByteBuffer bytes) - { - return ByteBufferUtil.toDouble(bytes); - } - - @Override - public ByteBuffer deserialize(Double value) - { - return (value == null) ? ByteBufferUtil.EMPTY_BYTE_BUFFER : ByteBufferUtil.bytes(value); - } - - @Override - public void validate(ByteBuffer bytes) throws MarshalException - { - if (bytes.remaining() != 8 && bytes.remaining() != 0) - throw new MarshalException(String.format("Expected 8 or 0 byte value for a double (%d)", bytes.remaining())); - } - - @Override - 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)); - } - - @Override - public String toString(Double value) - { - return value == null ? "" : value.toString(); - } - - @Override - public Class getType() - { - return Double.class; - } -} http://git-wip-us.apache.org/repos/asf/cassandra/blob/fc8b76f7/src/java/org/apache/cassandra/type/EmptySerializer.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/cassandra/type/EmptySerializer.java b/src/java/org/apache/cassandra/type/EmptySerializer.java deleted file mode 100644 index 760ee29..0000000 --- a/src/java/org/apache/cassandra/type/EmptySerializer.java +++ /dev/null @@ -1,65 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.cassandra.type; - -import org.apache.cassandra.utils.ByteBufferUtil; - -import java.nio.ByteBuffer; - -public class EmptySerializer extends AbstractSerializer -{ - public static final EmptySerializer instance = new EmptySerializer(); - - @Override - public Void serialize(ByteBuffer bytes) - { - return null; - } - - @Override - public ByteBuffer deserialize(Void value) - { - return ByteBufferUtil.EMPTY_BYTE_BUFFER; - } - - @Override - public void validate(ByteBuffer bytes) throws MarshalException - { - if (bytes.remaining() > 0) - throw new MarshalException("EmptyType only accept empty values"); - } - - @Override - public String getString(ByteBuffer bytes) - { - return ""; - } - - @Override - public String toString(Void value) - { - return ""; - } - - @Override - public Class getType() - { - return Void.class; - } -} http://git-wip-us.apache.org/repos/asf/cassandra/blob/fc8b76f7/src/java/org/apache/cassandra/type/FloatSerializer.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/cassandra/type/FloatSerializer.java b/src/java/org/apache/cassandra/type/FloatSerializer.java deleted file mode 100644 index d43003f..0000000 --- a/src/java/org/apache/cassandra/type/FloatSerializer.java +++ /dev/null @@ -1,74 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.cassandra.type; - -import org.apache.cassandra.utils.ByteBufferUtil; - -import java.nio.ByteBuffer; - -public class FloatSerializer extends AbstractSerializer -{ - public static final FloatSerializer instance = new FloatSerializer(); - - @Override - public Float serialize(ByteBuffer bytes) - { - return ByteBufferUtil.toFloat(bytes); - } - - @Override - public ByteBuffer deserialize(Float value) - { - return (value == null) ? ByteBufferUtil.EMPTY_BYTE_BUFFER : ByteBufferUtil.bytes(value); - } - - @Override - public void validate(ByteBuffer bytes) throws MarshalException - { - if (bytes.remaining() != 4 && bytes.remaining() != 0) - throw new MarshalException(String.format("Expected 4 or 0 byte value for a float (%d)", bytes.remaining())); - } - - @Override - 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)); - } - - @Override - public String toString(Float value) - { - return value == null ? "" : String.valueOf(value); - } - - @Override - public Class getType() - { - return Float.class; - } -} http://git-wip-us.apache.org/repos/asf/cassandra/blob/fc8b76f7/src/java/org/apache/cassandra/type/InetAddressSerializer.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/cassandra/type/InetAddressSerializer.java b/src/java/org/apache/cassandra/type/InetAddressSerializer.java deleted file mode 100644 index f1a30d9..0000000 --- a/src/java/org/apache/cassandra/type/InetAddressSerializer.java +++ /dev/null @@ -1,80 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.cassandra.type; - -import org.apache.cassandra.utils.ByteBufferUtil; - -import java.net.InetAddress; -import java.net.UnknownHostException; -import java.nio.ByteBuffer; - -public class InetAddressSerializer extends AbstractSerializer -{ - public static final InetAddressSerializer instance = new InetAddressSerializer(); - - @Override - public InetAddress serialize(ByteBuffer bytes) - { - try - { - return InetAddress.getByAddress(ByteBufferUtil.getArray(bytes)); - } - catch (UnknownHostException e) - { - throw new AssertionError(e); - } - } - - @Override - public ByteBuffer deserialize(InetAddress value) - { - return ByteBuffer.wrap(value.getAddress()); - } - - @Override - public void validate(ByteBuffer bytes) throws MarshalException - { - try - { - InetAddress.getByAddress(ByteBufferUtil.getArray(bytes)); - } - catch (UnknownHostException e) - { - throw new MarshalException(String.format("Expected 4 or 16 byte inetaddress; got %s", ByteBufferUtil.bytesToHex(bytes))); - } - } - - @Override - public String getString(ByteBuffer bytes) - { - return serialize(bytes).getHostAddress(); - } - - @Override - public String toString(InetAddress value) - { - return value.getHostAddress(); - } - - @Override - public Class getType() - { - return InetAddress.class; - } -} http://git-wip-us.apache.org/repos/asf/cassandra/blob/fc8b76f7/src/java/org/apache/cassandra/type/Int32Serializer.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/cassandra/type/Int32Serializer.java b/src/java/org/apache/cassandra/type/Int32Serializer.java deleted file mode 100644 index 2688218..0000000 --- a/src/java/org/apache/cassandra/type/Int32Serializer.java +++ /dev/null @@ -1,74 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.cassandra.type; - -import org.apache.cassandra.utils.ByteBufferUtil; - -import java.nio.ByteBuffer; - -public class Int32Serializer extends AbstractSerializer -{ - public static final Int32Serializer instance = new Int32Serializer(); - - @Override - public Integer serialize(ByteBuffer bytes) - { - return ByteBufferUtil.toInt(bytes); - } - - @Override - public ByteBuffer deserialize(Integer value) - { - return value == null ? ByteBufferUtil.EMPTY_BYTE_BUFFER : ByteBufferUtil.bytes(value); - } - - @Override - public void validate(ByteBuffer bytes) throws MarshalException - { - if (bytes.remaining() != 4 && bytes.remaining() != 0) - throw new MarshalException(String.format("Expected 4 or 0 byte int (%d)", bytes.remaining())); - } - - @Override - 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)); - } - - @Override - public String toString(Integer value) - { - return value == null ? "" : String.valueOf(value); - } - - @Override - public Class getType() - { - return Integer.class; - } -} http://git-wip-us.apache.org/repos/asf/cassandra/blob/fc8b76f7/src/java/org/apache/cassandra/type/IntegerSerializer.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/cassandra/type/IntegerSerializer.java b/src/java/org/apache/cassandra/type/IntegerSerializer.java deleted file mode 100644 index d54a47c..0000000 --- a/src/java/org/apache/cassandra/type/IntegerSerializer.java +++ /dev/null @@ -1,70 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.cassandra.type; - -import org.apache.cassandra.utils.ByteBufferUtil; - -import java.math.BigInteger; -import java.nio.ByteBuffer; - -public class IntegerSerializer extends AbstractSerializer -{ - public static final IntegerSerializer instance = new IntegerSerializer(); - - @Override - public BigInteger serialize(ByteBuffer bytes) - { - return new BigInteger(ByteBufferUtil.getArray(bytes)); - } - - @Override - public ByteBuffer deserialize(BigInteger value) - { - return ByteBuffer.wrap(value.toByteArray()); - } - - @Override - public void validate(ByteBuffer bytes) throws MarshalException - { - // no invalid integers. - } - - @Override - public String getString(ByteBuffer bytes) - { - if (bytes.remaining() == 0) - { - return ""; - } - - return new BigInteger(ByteBufferUtil.getArray(bytes)).toString(10); - } - - @Override - public String toString(BigInteger value) - { - return value.toString(10); - } - - @Override - public Class getType() - { - return BigInteger.class; - } -} http://git-wip-us.apache.org/repos/asf/cassandra/blob/fc8b76f7/src/java/org/apache/cassandra/type/ListSerializer.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/cassandra/type/ListSerializer.java b/src/java/org/apache/cassandra/type/ListSerializer.java deleted file mode 100644 index 1cbaf08..0000000 --- a/src/java/org/apache/cassandra/type/ListSerializer.java +++ /dev/null @@ -1,115 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.cassandra.type; - -import java.nio.BufferUnderflowException; -import java.nio.ByteBuffer; -import java.util.*; - -public class ListSerializer extends CollectionSerializer> -{ - // interning instances - private static final Map, ListSerializer> instances = new HashMap, ListSerializer>(); - - public final AbstractSerializer elements; - - public static synchronized ListSerializer getInstance(AbstractSerializer elements) - { - ListSerializer t = instances.get(elements); - if (t == null) - { - t = new ListSerializer(elements); - instances.put(elements, t); - } - return t; - } - - private ListSerializer(AbstractSerializer elements) - { - this.elements = elements; - } - - public List serialize(ByteBuffer bytes) - { - try - { - ByteBuffer input = bytes.duplicate(); - int n = getUnsignedShort(input); - List l = new ArrayList(n); - for (int i = 0; i < n; i++) - { - int s = getUnsignedShort(input); - byte[] data = new byte[s]; - input.get(data); - ByteBuffer databb = ByteBuffer.wrap(data); - elements.validate(databb); - l.add(elements.serialize(databb)); - } - return l; - } - catch (BufferUnderflowException e) - { - throw new MarshalException("Not enough bytes to read a list"); - } - } - - /** - * 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 deserialize(List value) - { - List bbs = new ArrayList(value.size()); - int size = 0; - for (T elt : value) - { - ByteBuffer bb = elements.deserialize(elt); - bbs.add(bb); - size += 2 + bb.remaining(); - } - return pack(bbs, value.size(), size); - } - - public String toString(List value) - { - StringBuffer sb = new StringBuffer(); - boolean isFirst = true; - for (T element : value) - { - if (isFirst) - { - isFirst = false; - } - else - { - sb.append("; "); - } - sb.append(elements.toString(element)); - } - return sb.toString(); - } - - public Class> getType() - { - return (Class) List.class; - } -} http://git-wip-us.apache.org/repos/asf/cassandra/blob/fc8b76f7/src/java/org/apache/cassandra/type/LongSerializer.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/cassandra/type/LongSerializer.java b/src/java/org/apache/cassandra/type/LongSerializer.java deleted file mode 100644 index f983539..0000000 --- a/src/java/org/apache/cassandra/type/LongSerializer.java +++ /dev/null @@ -1,74 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.cassandra.type; - -import org.apache.cassandra.utils.ByteBufferUtil; - -import java.nio.ByteBuffer; - -public class LongSerializer extends AbstractSerializer -{ - public static final LongSerializer instance = new LongSerializer(); - - @Override - public Long serialize(ByteBuffer bytes) - { - return ByteBufferUtil.toLong(bytes); - } - - @Override - public ByteBuffer deserialize(Long value) - { - return ByteBufferUtil.bytes(value); - } - - @Override - 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())); - } - - @Override - 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)); - } - - @Override - public String toString(Long value) - { - return String.valueOf(value); - } - - @Override - public Class getType() - { - return Long.class; - } -} http://git-wip-us.apache.org/repos/asf/cassandra/blob/fc8b76f7/src/java/org/apache/cassandra/type/MapSerializer.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/cassandra/type/MapSerializer.java b/src/java/org/apache/cassandra/type/MapSerializer.java deleted file mode 100644 index 44253fb..0000000 --- a/src/java/org/apache/cassandra/type/MapSerializer.java +++ /dev/null @@ -1,130 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.cassandra.type; - -import org.apache.cassandra.utils.Pair; - -import java.nio.BufferUnderflowException; -import java.nio.ByteBuffer; -import java.util.*; - -public class MapSerializer extends CollectionSerializer> -{ - // interning instances - private static final Map, AbstractSerializer>, MapSerializer> instances = new HashMap, AbstractSerializer>, MapSerializer>(); - - public final AbstractSerializer keys; - public final AbstractSerializer values; - - public static synchronized MapSerializer getInstance(AbstractSerializer keys, AbstractSerializer values) - { - Pair, AbstractSerializer> p = Pair., AbstractSerializer>create(keys, values); - MapSerializer t = instances.get(p); - if (t == null) - { - t = new MapSerializer(keys, values); - instances.put(p, t); - } - return t; - } - - private MapSerializer(AbstractSerializer keys, AbstractSerializer values) - { - this.keys = keys; - this.values = values; - } - - @Override - public Map serialize(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.serialize(kbb), values.serialize(vbb)); - } - return m; - } - catch (BufferUnderflowException e) - { - throw new MarshalException("Not enough bytes to read a map"); - } - } - - @Override - public ByteBuffer deserialize(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()); - bbs.add(bbk); - bbs.add(bbv); - size += 4 + bbk.remaining() + bbv.remaining(); - } - return pack(bbs, value.size(), size); - } - - @Override - public String toString(Map value) - { - StringBuffer sb = new StringBuffer(); - boolean isFirst = true; - for (Map.Entry element : value.entrySet()) - { - if (isFirst) - { - isFirst = false; - } - else - { - sb.append("; "); - } - sb.append('('); - sb.append(keys.toString(element.getKey())); - sb.append(", "); - sb.append(values.toString(element.getValue())); - sb.append(')'); - } - return sb.toString(); - } - - @Override - public Class> getType() - { - return (Class)Map.class; - } -} http://git-wip-us.apache.org/repos/asf/cassandra/blob/fc8b76f7/src/java/org/apache/cassandra/type/MarshalException.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/cassandra/type/MarshalException.java b/src/java/org/apache/cassandra/type/MarshalException.java deleted file mode 100644 index b556a88..0000000 --- a/src/java/org/apache/cassandra/type/MarshalException.java +++ /dev/null @@ -1,32 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.cassandra.type; - -public class MarshalException extends RuntimeException -{ - public MarshalException(String message) - { - super(message); - } - - public MarshalException(String message, Throwable cause) - { - super(message, cause); - } -}