kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | 70f21c53e06b36ea0c1ba62248165e426f599453074d03656443f4c2d93d1161 |
|---|---|
| Author | Alexandre Dutra <alexandre.dutra@datastax.com> · 2015-10-27 15:26 +01:00 |
| Committer | Alexandre Dutra <alexandre.dutra@datastax.com> · 2015-11-17 14:52 +01:00 |
| Parents | 8896a4ec2588 |
11 files changed
modified changelog/README.md +1 −0
| @@ -17,6 +17,7 @@ | ||
| 17 | 17 | - [new feature] JAVA-846: Provide custom codecs library as an extra module. |
| 18 | 18 | - [new feature] JAVA-742: Codec Support for JSON. |
| 19 | 19 | - [new feature] JAVA-606: Codec support for Java 8. |
| 20 | +- [new feature] JAVA-565: Codec support for Java arrays. | |
| 20 | 21 | |
| 21 | 22 | Merged from 2.1 branch: |
| 22 | 23 |
deleted driver-core/src/test/java/com/datastax/driver/core/TypeCodecArraysIntegrationTest.java +0 −233
| @@ -1,233 +0,0 @@ | ||
| 1 | -/* | |
| 2 | - * Copyright (C) 2012-2015 DataStax Inc. | |
| 3 | - * | |
| 4 | - * Licensed under the Apache License, Version 2.0 (the "License"); | |
| 5 | - * you may not use this file except in compliance with the License. | |
| 6 | - * You may obtain a copy of the License at | |
| 7 | - * | |
| 8 | - * http://www.apache.org/licenses/LICENSE-2.0 | |
| 9 | - * | |
| 10 | - * Unless required by applicable law or agreed to in writing, software | |
| 11 | - * distributed under the License is distributed on an "AS IS" BASIS, | |
| 12 | - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 13 | - * See the License for the specific language governing permissions and | |
| 14 | - * limitations under the License. | |
| 15 | - */ | |
| 16 | -package com.datastax.driver.core; | |
| 17 | - | |
| 18 | -import java.nio.ByteBuffer; | |
| 19 | -import java.util.Collection; | |
| 20 | -import java.util.List; | |
| 21 | - | |
| 22 | -import com.google.common.collect.Lists; | |
| 23 | -import com.google.common.primitives.Ints; | |
| 24 | -import org.testng.annotations.Test; | |
| 25 | - | |
| 26 | -import static com.google.common.base.Preconditions.checkArgument; | |
| 27 | -import static org.assertj.core.api.Assertions.assertThat; | |
| 28 | - | |
| 29 | -import com.datastax.driver.core.exceptions.InvalidTypeException; | |
| 30 | - | |
| 31 | -/** | |
| 32 | - * Demonstrates how to write custom codecs for high-performance deserialization of CQL collections to primitive arrays. | |
| 33 | - */ | |
| 34 | -public class TypeCodecArraysIntegrationTest extends CCMBridge.PerClassSingleNodeCluster { | |
| 35 | - | |
| 36 | - @Override | |
| 37 | - protected Collection<String> getTableDefinitions() { | |
| 38 | - return Lists.newArrayList( | |
| 39 | - "CREATE TABLE lists(i int PRIMARY KEY, l list<int>)", | |
| 40 | - "INSERT INTO lists (i, l) VALUES (1, [1, 2, 3])" | |
| 41 | - ); | |
| 42 | - } | |
| 43 | - | |
| 44 | - @Override | |
| 45 | - protected Cluster.Builder configure(Cluster.Builder builder) { | |
| 46 | - return builder.withCodecRegistry( | |
| 47 | - new CodecRegistry().register(new IntArrayCodec()) | |
| 48 | - ); | |
| 49 | - } | |
| 50 | - | |
| 51 | - @Test(groups = "short") | |
| 52 | - public void should_read_list_column_as_array() { | |
| 53 | - Row row = session.execute("SELECT l FROM lists WHERE i = 1").one(); | |
| 54 | - | |
| 55 | - int[] ints = row.get("l", int[].class); | |
| 56 | - | |
| 57 | - assertThat(ints).containsExactly(1, 2, 3); | |
| 58 | - } | |
| 59 | - | |
| 60 | - @Test(groups = "short") | |
| 61 | - public void should_set_list_column_with_array() { | |
| 62 | - PreparedStatement pst = session.prepare("INSERT INTO lists (i, l) VALUES (?, ?)"); | |
| 63 | - BoundStatement bs = pst.bind() | |
| 64 | - .setInt(0, 2) | |
| 65 | - .set(1, new int[]{ 2, 3, 4 }, int[].class); | |
| 66 | - session.execute(bs); | |
| 67 | - | |
| 68 | - Row row = session.execute("SELECT l FROM lists WHERE i = 2").one(); | |
| 69 | - assertThat(row.getList("l", Integer.class)).containsExactly(2, 3, 4); | |
| 70 | - } | |
| 71 | - | |
| 72 | - @Test(groups = "short") | |
| 73 | - public void should_format_array_as_cql_literal() { | |
| 74 | - IntArrayCodec codec = new IntArrayCodec(); | |
| 75 | - | |
| 76 | - assertThat(codec.format(new int[]{ 1, 2, 3 })) | |
| 77 | - .isEqualTo("[1,2,3]"); | |
| 78 | - assertThat(codec.format(new int[0])) | |
| 79 | - .isEqualTo("[]"); | |
| 80 | - assertThat(codec.format(null)) | |
| 81 | - .isEqualTo("null"); | |
| 82 | - } | |
| 83 | - | |
| 84 | - @Test(groups = "short") | |
| 85 | - public void should_parse_array_from_cql_literal() { | |
| 86 | - IntArrayCodec codec = new IntArrayCodec(); | |
| 87 | - | |
| 88 | - assertThat(codec.parse("[1,2,3]")) | |
| 89 | - .containsExactly(1, 2, 3); | |
| 90 | - assertThat(codec.parse("null")) | |
| 91 | - .isNull(); | |
| 92 | - assertThat(codec.parse("")) | |
| 93 | - .isNull(); | |
| 94 | - assertThat(codec.parse(null)) | |
| 95 | - .isNull(); | |
| 96 | - } | |
| 97 | - | |
| 98 | - public static class IntArrayCodec extends TypeCodec<int[]> { | |
| 99 | - | |
| 100 | - private static final int SIZE_OF_INT = 4; | |
| 101 | - | |
| 102 | - public IntArrayCodec() { | |
| 103 | - super(DataType.list(DataType.cint()), int[].class); | |
| 104 | - } | |
| 105 | - | |
| 106 | - @Override | |
| 107 | - public ByteBuffer serialize(int[] ints, ProtocolVersion protocolVersion) throws InvalidTypeException { | |
| 108 | - if (ints == null) | |
| 109 | - return null; | |
| 110 | - | |
| 111 | - boolean isProtocolV3OrAbove = protocolVersion.compareTo(ProtocolVersion.V2) > 0; | |
| 112 | - | |
| 113 | - checkArgument(isProtocolV3OrAbove || ints.length < 65536, | |
| 114 | - "Native protocol version %d supports up to 65535 elements in any collection - but collection contains %d elements", | |
| 115 | - protocolVersion.toInt(), ints.length); | |
| 116 | - | |
| 117 | - /* | |
| 118 | - * Encoding of lists in the native protocol: | |
| 119 | - * [size of list] [size of element 1][element1] [size of element 2][element2]... | |
| 120 | - * Sizes are encoded on 2 bytes in protocol v1 and v2, 4 bytes otherwise. | |
| 121 | - * Integers are always encoded on 4 bytes. | |
| 122 | - * (See native_protocol_v*.spec in https://github.com/apache/cassandra/blob/trunk/doc/) | |
| 123 | - */ | |
| 124 | - | |
| 125 | - // Number of bytes to encode sizes | |
| 126 | - int sizeOfSize = isProtocolV3OrAbove ? 4 : 2; | |
| 127 | - | |
| 128 | - // Number of bytes to encode each element (preceded by its size) | |
| 129 | - int sizeOfElement = sizeOfSize + SIZE_OF_INT; | |
| 130 | - | |
| 131 | - // Number of bytes to encode the whole collection (size of collection + all elements) | |
| 132 | - int totalSize = sizeOfSize + ints.length * sizeOfElement; | |
| 133 | - | |
| 134 | - ByteBuffer output = ByteBuffer.allocate(totalSize); | |
| 135 | - | |
| 136 | - writeSize(output, ints.length, protocolVersion); | |
| 137 | - for (int i : ints) { | |
| 138 | - writeSize(output, SIZE_OF_INT, protocolVersion); | |
| 139 | - output.putInt(i); | |
| 140 | - } | |
| 141 | - output.flip(); | |
| 142 | - return output; | |
| 143 | - } | |
| 144 | - | |
| 145 | - @Override | |
| 146 | - public int[] deserialize(ByteBuffer bytes, ProtocolVersion protocolVersion) throws InvalidTypeException { | |
| 147 | - if (bytes == null || bytes.remaining() == 0) | |
| 148 | - return new int[0]; | |
| 149 | - | |
| 150 | - boolean isProtocolV3OrAbove = protocolVersion.compareTo(ProtocolVersion.V2) > 0; | |
| 151 | - int sizeOfSize = isProtocolV3OrAbove ? 4 : 2; | |
| 152 | - | |
| 153 | - ByteBuffer input = bytes.duplicate(); | |
| 154 | - | |
| 155 | - int size = CodecUtils.readCollectionSize(input, protocolVersion); | |
| 156 | - int[] ints = new int[size]; | |
| 157 | - for (int i = 0; i < size; i++) { | |
| 158 | - // Skip size (we know it will always be 4 since the elements are ints) | |
| 159 | - input.position(input.position() + sizeOfSize); | |
| 160 | - ints[i] = input.getInt(); | |
| 161 | - } | |
| 162 | - return ints; | |
| 163 | - } | |
| 164 | - | |
| 165 | - @Override | |
| 166 | - public String format(int[] ints) throws InvalidTypeException { | |
| 167 | - if (ints == null) | |
| 168 | - return "null"; | |
| 169 | - | |
| 170 | - StringBuilder sb = new StringBuilder(); | |
| 171 | - sb.append('['); | |
| 172 | - for (int i = 0; i < ints.length; i++) { | |
| 173 | - if (i != 0) | |
| 174 | - sb.append(","); | |
| 175 | - sb.append(ints[i]); | |
| 176 | - } | |
| 177 | - sb.append(']'); | |
| 178 | - return sb.toString(); | |
| 179 | - } | |
| 180 | - | |
| 181 | - @Override | |
| 182 | - public int[] parse(String value) throws InvalidTypeException { | |
| 183 | - if (value == null || value.isEmpty() || value.equals("null")) | |
| 184 | - return null; | |
| 185 | - | |
| 186 | - int idx = ParseUtils.skipSpaces(value, 0); | |
| 187 | - if (value.charAt(idx++) != '[') | |
| 188 | - throw new InvalidTypeException(String.format("cannot parse list value from \"%s\", at character %d expecting '[' but got '%c'", value, idx, value.charAt(idx))); | |
| 189 | - | |
| 190 | - idx = ParseUtils.skipSpaces(value, idx); | |
| 191 | - | |
| 192 | - if (value.charAt(idx) == ']') | |
| 193 | - return new int[0]; | |
| 194 | - | |
| 195 | - List<Integer> l = Lists.newArrayList(); | |
| 196 | - while (idx < value.length()) { | |
| 197 | - int n; | |
| 198 | - try { | |
| 199 | - n = ParseUtils.skipCQLValue(value, idx); | |
| 200 | - } catch (IllegalArgumentException e) { | |
| 201 | - throw new InvalidTypeException(String.format("Cannot parse list value from \"%s\", invalid CQL value at character %d", value, idx), e); | |
| 202 | - } | |
| 203 | - | |
| 204 | - l.add(Integer.parseInt(value.substring(idx, n))); | |
| 205 | - idx = n; | |
| 206 | - | |
| 207 | - idx = ParseUtils.skipSpaces(value, idx); | |
| 208 | - if (value.charAt(idx) == ']') | |
| 209 | - return Ints.toArray(l); | |
| 210 | - if (value.charAt(idx++) != ',') | |
| 211 | - throw new InvalidTypeException(String.format("Cannot parse list value from \"%s\", at character %d expecting ',' but got '%c'", value, idx, value.charAt(idx))); | |
| 212 | - | |
| 213 | - idx = ParseUtils.skipSpaces(value, idx); | |
| 214 | - } | |
| 215 | - throw new InvalidTypeException(String.format("Malformed list value \"%s\", missing closing ']'", value)); | |
| 216 | - } | |
| 217 | - | |
| 218 | - private void writeSize(ByteBuffer output, int size, ProtocolVersion protocolVersion) { | |
| 219 | - switch (protocolVersion) { | |
| 220 | - case V1: | |
| 221 | - case V2: | |
| 222 | - output.putShort((short)size); | |
| 223 | - break; | |
| 224 | - case V3: | |
| 225 | - case V4: | |
| 226 | - output.putInt(size); | |
| 227 | - break; | |
| 228 | - default: | |
| 229 | - throw protocolVersion.unsupported(); | |
| 230 | - } | |
| 231 | - } | |
| 232 | - } | |
| 233 | -} | |
| No newline at the end of the file |
added driver-extras/src/main/java/com/datastax/driver/extras/codecs/arrays/AbstractArrayCodec.java +149 −0
| @@ -0,0 +1,149 @@ | ||
| 1 | +/* | |
| 2 | + * Copyright (C) 2012-2015 DataStax Inc. | |
| 3 | + * | |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | |
| 5 | + * you may not use this file except in compliance with the License. | |
| 6 | + * You may obtain a copy of the License at | |
| 7 | + * | |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 | |
| 9 | + * | |
| 10 | + * Unless required by applicable law or agreed to in writing, software | |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, | |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 13 | + * See the License for the specific language governing permissions and | |
| 14 | + * limitations under the License. | |
| 15 | + */ | |
| 16 | +package com.datastax.driver.extras.codecs.arrays; | |
| 17 | + | |
| 18 | +import java.lang.reflect.Array; | |
| 19 | + | |
| 20 | +import static com.google.common.base.Preconditions.checkArgument; | |
| 21 | + | |
| 22 | +import com.datastax.driver.core.DataType; | |
| 23 | +import com.datastax.driver.core.ParseUtils; | |
| 24 | +import com.datastax.driver.core.TypeCodec; | |
| 25 | +import com.datastax.driver.core.exceptions.InvalidTypeException; | |
| 26 | + | |
| 27 | +import static com.datastax.driver.core.ParseUtils.skipSpaces; | |
| 28 | + | |
| 29 | +/** | |
| 30 | + * Base class for all codecs dealing with Java arrays. | |
| 31 | + * This class aims to reduce the amount of code required to create such codecs. | |
| 32 | + * | |
| 33 | + * @param <T> The Java array type this codec handles | |
| 34 | + */ | |
| 35 | +public abstract class AbstractArrayCodec<T> extends TypeCodec<T> { | |
| 36 | + | |
| 37 | + /** | |
| 38 | + * @param cqlType The CQL type. Must be a list type. | |
| 39 | + * @param javaClass The Java type. Must be an array class. | |
| 40 | + */ | |
| 41 | + public AbstractArrayCodec(DataType.CollectionType cqlType, Class<T> javaClass) { | |
| 42 | + super(cqlType, javaClass); | |
| 43 | + checkArgument(cqlType.getName() == DataType.Name.LIST, "Expecting CQL list type, got %s", cqlType); | |
| 44 | + checkArgument(javaClass.isArray(), "Expecting Java array class, got %s", javaClass); | |
| 45 | + } | |
| 46 | + | |
| 47 | + @Override | |
| 48 | + public String format(T array) throws InvalidTypeException { | |
| 49 | + if (array == null) | |
| 50 | + return "NULL"; | |
| 51 | + int length = Array.getLength(array); | |
| 52 | + StringBuilder sb = new StringBuilder(); | |
| 53 | + sb.append('['); | |
| 54 | + for (int i = 0; i < length; i++) { | |
| 55 | + if (i != 0) | |
| 56 | + sb.append(","); | |
| 57 | + formatElement(sb, array, i); | |
| 58 | + } | |
| 59 | + sb.append(']'); | |
| 60 | + return sb.toString(); | |
| 61 | + } | |
| 62 | + | |
| 63 | + @Override | |
| 64 | + public T parse(String value) throws InvalidTypeException { | |
| 65 | + if (value == null || value.isEmpty() || value.equalsIgnoreCase("NULL")) | |
| 66 | + return null; | |
| 67 | + | |
| 68 | + int idx = skipSpaces(value, 0); | |
| 69 | + if (value.charAt(idx++) != '[') | |
| 70 | + throw new InvalidTypeException(String.format("cannot parse list value from \"%s\", at character %d expecting '[' but got '%c'", value, idx, value.charAt(idx))); | |
| 71 | + | |
| 72 | + idx = skipSpaces(value, idx); | |
| 73 | + | |
| 74 | + if (value.charAt(idx) == ']') | |
| 75 | + return newInstance(0); | |
| 76 | + | |
| 77 | + // first pass: determine array length | |
| 78 | + int length = getArrayLength(value, idx); | |
| 79 | + | |
| 80 | + // second pass: parse elements | |
| 81 | + T array = newInstance(length); | |
| 82 | + int i = 0; | |
| 83 | + for( ; idx < value.length(); i++) { | |
| 84 | + int n = skipLiteral(value, idx); | |
| 85 | + parseElement(value.substring(idx, n), array, i); | |
| 86 | + idx = skipSpaces(value, n); | |
| 87 | + if (value.charAt(idx) == ']') | |
| 88 | + return array; | |
| 89 | + idx = skipComma(value, idx); | |
| 90 | + idx = skipSpaces(value, idx); | |
| 91 | + } | |
| 92 | + | |
| 93 | + throw new InvalidTypeException(String.format("Malformed list value \"%s\", missing closing ']'", value)); | |
| 94 | + } | |
| 95 | + | |
| 96 | + /** | |
| 97 | + * Create a new array instance with the given size. | |
| 98 | + * | |
| 99 | + * @param size The size of the array to instantiate. | |
| 100 | + * @return a new array instance with the given size. | |
| 101 | + */ | |
| 102 | + protected abstract T newInstance(int size); | |
| 103 | + | |
| 104 | + /** | |
| 105 | + * Format the {@code index}th element of {@code array} to {@code output}. | |
| 106 | + * | |
| 107 | + * @param output The StringBuilder to write to. | |
| 108 | + * @param array The array to read from. | |
| 109 | + * @param index The element index. | |
| 110 | + */ | |
| 111 | + protected abstract void formatElement(StringBuilder output, T array, int index); | |
| 112 | + | |
| 113 | + /** | |
| 114 | + * Parse the {@code index}th element of {@code array} from {@code input}. | |
| 115 | + * | |
| 116 | + * @param input The String to read from. | |
| 117 | + * @param array The array to write to. | |
| 118 | + * @param index The element index. | |
| 119 | + */ | |
| 120 | + protected abstract void parseElement(String input, T array, int index); | |
| 121 | + | |
| 122 | + private int getArrayLength(String value, int idx) { | |
| 123 | + int length = 1; | |
| 124 | + for( ; idx < value.length(); length++) { | |
| 125 | + idx = skipLiteral(value, idx); | |
| 126 | + idx = skipSpaces(value, idx); | |
| 127 | + if (value.charAt(idx) == ']') | |
| 128 | + break; | |
| 129 | + idx = skipComma(value, idx); | |
| 130 | + idx = skipSpaces(value, idx); | |
| 131 | + } | |
| 132 | + return length; | |
| 133 | + } | |
| 134 | + | |
| 135 | + private int skipComma(String value, int idx) { | |
| 136 | + if (value.charAt(idx) != ',') | |
| 137 | + throw new InvalidTypeException(String.format("Cannot parse list value from \"%s\", at character %d expecting ',' but got '%c'", value, idx, value.charAt(idx))); | |
| 138 | + return idx + 1; | |
| 139 | + } | |
| 140 | + | |
| 141 | + private int skipLiteral(String value, int idx) { | |
| 142 | + try { | |
| 143 | + return ParseUtils.skipCQLValue(value, idx); | |
| 144 | + } catch (IllegalArgumentException e) { | |
| 145 | + throw new InvalidTypeException(String.format("Cannot parse list value from \"%s\", invalid CQL value at character %d", value, idx), e); | |
| 146 | + } | |
| 147 | + } | |
| 148 | + | |
| 149 | +} |
added driver-extras/src/main/java/com/datastax/driver/extras/codecs/arrays/AbstractPrimitiveArrayCodec.java +120 −0
| @@ -0,0 +1,120 @@ | ||
| 1 | +/* | |
| 2 | + * Copyright (C) 2012-2015 DataStax Inc. | |
| 3 | + * | |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | |
| 5 | + * you may not use this file except in compliance with the License. | |
| 6 | + * You may obtain a copy of the License at | |
| 7 | + * | |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 | |
| 9 | + * | |
| 10 | + * Unless required by applicable law or agreed to in writing, software | |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, | |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 13 | + * See the License for the specific language governing permissions and | |
| 14 | + * limitations under the License. | |
| 15 | + */ | |
| 16 | +package com.datastax.driver.extras.codecs.arrays; | |
| 17 | + | |
| 18 | +import java.lang.reflect.Array; | |
| 19 | +import java.nio.ByteBuffer; | |
| 20 | + | |
| 21 | +import static com.google.common.base.Preconditions.checkArgument; | |
| 22 | + | |
| 23 | +import com.datastax.driver.core.CodecUtils; | |
| 24 | +import com.datastax.driver.core.DataType; | |
| 25 | +import com.datastax.driver.core.ProtocolVersion; | |
| 26 | +import com.datastax.driver.core.exceptions.InvalidTypeException; | |
| 27 | + | |
| 28 | +/** | |
| 29 | + * Base class for all codecs dealing with Java primitive arrays. | |
| 30 | + * This class provides a more efficient implementation of {@link #serialize(Object, ProtocolVersion)} | |
| 31 | + * and {@link #deserialize(ByteBuffer, ProtocolVersion)} for primitive arrays. | |
| 32 | + * | |
| 33 | + * @param <T> The Java primitive array type this codec handles | |
| 34 | + */ | |
| 35 | +public abstract class AbstractPrimitiveArrayCodec<T> extends AbstractArrayCodec<T> { | |
| 36 | + | |
| 37 | + /** | |
| 38 | + * @param cqlType The CQL type. Must be a list type. | |
| 39 | + * @param javaClass The Java type. Must be an array class. | |
| 40 | + */ | |
| 41 | + public AbstractPrimitiveArrayCodec(DataType.CollectionType cqlType, Class<T> javaClass) { | |
| 42 | + super(cqlType, javaClass); | |
| 43 | + checkArgument(javaClass.getComponentType().isPrimitive(), "Expecting primitive array component type, got %s", javaClass.getComponentType()); | |
| 44 | + } | |
| 45 | + | |
| 46 | + @Override | |
| 47 | + public ByteBuffer serialize(T array, ProtocolVersion protocolVersion) throws InvalidTypeException { | |
| 48 | + if (array == null) | |
| 49 | + return null; | |
| 50 | + boolean isProtocolV3OrAbove = protocolVersion.compareTo(ProtocolVersion.V2) > 0; | |
| 51 | + // native method | |
| 52 | + int length = Array.getLength(array); | |
| 53 | + checkArgument(isProtocolV3OrAbove || length < 65536, | |
| 54 | + "Native protocol version %d supports up to 65535 elements in any collection - but collection contains %d elements", | |
| 55 | + protocolVersion.toInt(), length); | |
| 56 | + /* | |
| 57 | + * Encoding of lists in the native protocol: | |
| 58 | + * [size of list] [size of element 1][element1] [size of element 2][element2]... | |
| 59 | + * Sizes are encoded on 2 bytes in protocol v1 and v2, 4 bytes otherwise. | |
| 60 | + * (See native_protocol_v*.spec in https://github.com/apache/cassandra/blob/trunk/doc/) | |
| 61 | + */ | |
| 62 | + // Number of bytes to encode sizes | |
| 63 | + int sizeOfSize = isProtocolV3OrAbove ? 4 : 2; | |
| 64 | + // Number of bytes to encode each element (preceded by its size) | |
| 65 | + int sizeOfElement = sizeOfSize + sizeOfComponentType(); | |
| 66 | + // Number of bytes to encode the whole collection (size of collection + all elements) | |
| 67 | + int totalSize = sizeOfSize + length * sizeOfElement; | |
| 68 | + ByteBuffer output = ByteBuffer.allocate(totalSize); | |
| 69 | + CodecUtils.writeSize(output, length, protocolVersion); | |
| 70 | + for (int i = 0; i < length; i++) { | |
| 71 | + CodecUtils.writeSize(output, sizeOfComponentType(), protocolVersion); | |
| 72 | + serializeElement(output, array, i, protocolVersion); | |
| 73 | + } | |
| 74 | + output.flip(); | |
| 75 | + return output; | |
| 76 | + } | |
| 77 | + | |
| 78 | + @Override | |
| 79 | + public T deserialize(ByteBuffer bytes, ProtocolVersion protocolVersion) throws InvalidTypeException { | |
| 80 | + if (bytes == null || bytes.remaining() == 0) | |
| 81 | + return newInstance(0); | |
| 82 | + boolean isProtocolV3OrAbove = protocolVersion.compareTo(ProtocolVersion.V2) > 0; | |
| 83 | + int sizeOfSize = isProtocolV3OrAbove ? 4 : 2; | |
| 84 | + ByteBuffer input = bytes.duplicate(); | |
| 85 | + int size = CodecUtils.readSize(input, protocolVersion); | |
| 86 | + T array = newInstance(size); | |
| 87 | + for (int i = 0; i < size; i++) { | |
| 88 | + // Skip size (we know it will always be 4 since the elements are ints) | |
| 89 | + input.position(input.position() + sizeOfSize); | |
| 90 | + deserializeElement(input, array, i, protocolVersion); | |
| 91 | + } | |
| 92 | + return array; | |
| 93 | + } | |
| 94 | + | |
| 95 | + /** | |
| 96 | + * Return the size in bytes of the array component type. | |
| 97 | + * | |
| 98 | + * @return the size in bytes of the array component type. | |
| 99 | + */ | |
| 100 | + protected abstract int sizeOfComponentType(); | |
| 101 | + | |
| 102 | + /** | |
| 103 | + * Write the {@code index}th element of {@code array} to {@code output}. | |
| 104 | + * @param output The ByteBuffer to write to. | |
| 105 | + * @param array The array to read from. | |
| 106 | + * @param index The element index. | |
| 107 | + * @param protocolVersion The protocol version to use. | |
| 108 | + */ | |
| 109 | + protected abstract void serializeElement(ByteBuffer output, T array, int index, ProtocolVersion protocolVersion); | |
| 110 | + | |
| 111 | + /** | |
| 112 | + * Read the {@code index}th element of {@code array} from {@code input}. | |
| 113 | + * @param input The ByteBuffer to read from. | |
| 114 | + * @param array The array to write to. | |
| 115 | + * @param index The element index. | |
| 116 | + * @param protocolVersion The protocol version to use. | |
| 117 | + */ | |
| 118 | + protected abstract void deserializeElement(ByteBuffer input, T array, int index, ProtocolVersion protocolVersion); | |
| 119 | + | |
| 120 | +} |
added driver-extras/src/main/java/com/datastax/driver/extras/codecs/arrays/DoubleArrayCodec.java +70 −0
| @@ -0,0 +1,70 @@ | ||
| 1 | +/* | |
| 2 | + * Copyright (C) 2012-2015 DataStax Inc. | |
| 3 | + * | |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | |
| 5 | + * you may not use this file except in compliance with the License. | |
| 6 | + * You may obtain a copy of the License at | |
| 7 | + * | |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 | |
| 9 | + * | |
| 10 | + * Unless required by applicable law or agreed to in writing, software | |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, | |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 13 | + * See the License for the specific language governing permissions and | |
| 14 | + * limitations under the License. | |
| 15 | + */ | |
| 16 | +package com.datastax.driver.extras.codecs.arrays; | |
| 17 | + | |
| 18 | +import java.nio.ByteBuffer; | |
| 19 | + | |
| 20 | +import com.datastax.driver.core.DataType; | |
| 21 | +import com.datastax.driver.core.ProtocolVersion; | |
| 22 | + | |
| 23 | +/** | |
| 24 | + * A codec that maps the CQL type {@code list<double>} to the Java type {@code double[]}. | |
| 25 | + * <p> | |
| 26 | + * Note that this codec is designed for performance and converts CQL lists | |
| 27 | + * <em>directly</em> to {@code double[]}, thus avoiding any unnecessary | |
| 28 | + * boxing and unboxing of Java primitive {@code double} values; | |
| 29 | + * it also instantiates arrays without the need for an intermediary | |
| 30 | + * Java {@code List} object. | |
| 31 | + */ | |
| 32 | +public class DoubleArrayCodec extends AbstractPrimitiveArrayCodec<double[]> { | |
| 33 | + | |
| 34 | + public static final DoubleArrayCodec instance = new DoubleArrayCodec(); | |
| 35 | + | |
| 36 | + public DoubleArrayCodec() { | |
| 37 | + super(DataType.list(DataType.cdouble()), double[].class); | |
| 38 | + } | |
| 39 | + | |
| 40 | + @Override | |
| 41 | + protected int sizeOfComponentType() { | |
| 42 | + return 8; | |
| 43 | + } | |
| 44 | + | |
| 45 | + @Override | |
| 46 | + protected void serializeElement(ByteBuffer output, double[] array, int index, ProtocolVersion protocolVersion) { | |
| 47 | + output.putDouble(array[index]); | |
| 48 | + } | |
| 49 | + | |
| 50 | + @Override | |
| 51 | + protected void deserializeElement(ByteBuffer input, double[] array, int index, ProtocolVersion protocolVersion) { | |
| 52 | + array[index] = input.getDouble(); | |
| 53 | + } | |
| 54 | + | |
| 55 | + @Override | |
| 56 | + protected void formatElement(StringBuilder output, double[] array, int index) { | |
| 57 | + output.append(array[index]); | |
| 58 | + } | |
| 59 | + | |
| 60 | + @Override | |
| 61 | + protected void parseElement(String input, double[] array, int index) { | |
| 62 | + array[index] = Double.parseDouble(input); | |
| 63 | + } | |
| 64 | + | |
| 65 | + @Override | |
| 66 | + protected double[] newInstance(int size) { | |
| 67 | + return new double[size]; | |
| 68 | + } | |
| 69 | + | |
| 70 | +} |
added driver-extras/src/main/java/com/datastax/driver/extras/codecs/arrays/FloatArrayCodec.java +70 −0
| @@ -0,0 +1,70 @@ | ||
| 1 | +/* | |
| 2 | + * Copyright (C) 2012-2015 DataStax Inc. | |
| 3 | + * | |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | |
| 5 | + * you may not use this file except in compliance with the License. | |
| 6 | + * You may obtain a copy of the License at | |
| 7 | + * | |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 | |
| 9 | + * | |
| 10 | + * Unless required by applicable law or agreed to in writing, software | |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, | |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 13 | + * See the License for the specific language governing permissions and | |
| 14 | + * limitations under the License. | |
| 15 | + */ | |
| 16 | +package com.datastax.driver.extras.codecs.arrays; | |
| 17 | + | |
| 18 | +import java.nio.ByteBuffer; | |
| 19 | + | |
| 20 | +import com.datastax.driver.core.DataType; | |
| 21 | +import com.datastax.driver.core.ProtocolVersion; | |
| 22 | + | |
| 23 | +/** | |
| 24 | + * A codec that maps the CQL type {@code list<float>} to the Java type {@code float[]}. | |
| 25 | + * <p> | |
| 26 | + * Note that this codec is designed for performance and converts CQL lists | |
| 27 | + * <em>directly</em> to {@code float[]}, thus avoiding any unnecessary | |
| 28 | + * boxing and unboxing of Java primitive {@code float} values; | |
| 29 | + * it also instantiates arrays without the need for an intermediary | |
| 30 | + * Java {@code List} object. | |
| 31 | + */ | |
| 32 | +public class FloatArrayCodec extends AbstractPrimitiveArrayCodec<float[]> { | |
| 33 | + | |
| 34 | + public static final FloatArrayCodec instance = new FloatArrayCodec(); | |
| 35 | + | |
| 36 | + public FloatArrayCodec() { | |
| 37 | + super(DataType.list(DataType.cfloat()), float[].class); | |
| 38 | + } | |
| 39 | + | |
| 40 | + @Override | |
| 41 | + protected int sizeOfComponentType() { | |
| 42 | + return 4; | |
| 43 | + } | |
| 44 | + | |
| 45 | + @Override | |
| 46 | + protected void serializeElement(ByteBuffer output, float[] array, int index, ProtocolVersion protocolVersion) { | |
| 47 | + output.putFloat(array[index]); | |
| 48 | + } | |
| 49 | + | |
| 50 | + @Override | |
| 51 | + protected void deserializeElement(ByteBuffer input, float[] array, int index, ProtocolVersion protocolVersion) { | |
| 52 | + array[index] = input.getFloat(); | |
| 53 | + } | |
| 54 | + | |
| 55 | + @Override | |
| 56 | + protected void formatElement(StringBuilder output, float[] array, int index) { | |
| 57 | + output.append(array[index]); | |
| 58 | + } | |
| 59 | + | |
| 60 | + @Override | |
| 61 | + protected void parseElement(String input, float[] array, int index) { | |
| 62 | + array[index] = Float.parseFloat(input); | |
| 63 | + } | |
| 64 | + | |
| 65 | + @Override | |
| 66 | + protected float[] newInstance(int size) { | |
| 67 | + return new float[size]; | |
| 68 | + } | |
| 69 | + | |
| 70 | +} |
added driver-extras/src/main/java/com/datastax/driver/extras/codecs/arrays/IntArrayCodec.java +70 −0
| @@ -0,0 +1,70 @@ | ||
| 1 | +/* | |
| 2 | + * Copyright (C) 2012-2015 DataStax Inc. | |
| 3 | + * | |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | |
| 5 | + * you may not use this file except in compliance with the License. | |
| 6 | + * You may obtain a copy of the License at | |
| 7 | + * | |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 | |
| 9 | + * | |
| 10 | + * Unless required by applicable law or agreed to in writing, software | |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, | |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 13 | + * See the License for the specific language governing permissions and | |
| 14 | + * limitations under the License. | |
| 15 | + */ | |
| 16 | +package com.datastax.driver.extras.codecs.arrays; | |
| 17 | + | |
| 18 | +import java.nio.ByteBuffer; | |
| 19 | + | |
| 20 | +import com.datastax.driver.core.DataType; | |
| 21 | +import com.datastax.driver.core.ProtocolVersion; | |
| 22 | + | |
| 23 | +/** | |
| 24 | + * A codec that maps the CQL type {@code list<int>} to the Java type {@code int[]}. | |
| 25 | + * <p> | |
| 26 | + * Note that this codec is designed for performance and converts CQL lists | |
| 27 | + * <em>directly</em> to {@code int[]}, thus avoiding any unnecessary | |
| 28 | + * boxing and unboxing of Java primitive {@code int} values; | |
| 29 | + * it also instantiates arrays without the need for an intermediary | |
| 30 | + * Java {@code List} object. | |
| 31 | + */ | |
| 32 | +public class IntArrayCodec extends AbstractPrimitiveArrayCodec<int[]> { | |
| 33 | + | |
| 34 | + public static final IntArrayCodec instance = new IntArrayCodec(); | |
| 35 | + | |
| 36 | + public IntArrayCodec() { | |
| 37 | + super(DataType.list(DataType.cint()), int[].class); | |
| 38 | + } | |
| 39 | + | |
| 40 | + @Override | |
| 41 | + protected int sizeOfComponentType() { | |
| 42 | + return 4; | |
| 43 | + } | |
| 44 | + | |
| 45 | + @Override | |
| 46 | + protected void serializeElement(ByteBuffer output, int[] array, int index, ProtocolVersion protocolVersion) { | |
| 47 | + output.putInt(array[index]); | |
| 48 | + } | |
| 49 | + | |
| 50 | + @Override | |
| 51 | + protected void deserializeElement(ByteBuffer input, int[] array, int index, ProtocolVersion protocolVersion) { | |
| 52 | + array[index] = input.getInt(); | |
| 53 | + } | |
| 54 | + | |
| 55 | + @Override | |
| 56 | + protected void formatElement(StringBuilder output, int[] array, int index) { | |
| 57 | + output.append(array[index]); | |
| 58 | + } | |
| 59 | + | |
| 60 | + @Override | |
| 61 | + protected void parseElement(String input, int[] array, int index) { | |
| 62 | + array[index] = Integer.parseInt(input); | |
| 63 | + } | |
| 64 | + | |
| 65 | + @Override | |
| 66 | + protected int[] newInstance(int size) { | |
| 67 | + return new int[size]; | |
| 68 | + } | |
| 69 | + | |
| 70 | +} |
added driver-extras/src/main/java/com/datastax/driver/extras/codecs/arrays/LongArrayCodec.java +70 −0
| @@ -0,0 +1,70 @@ | ||
| 1 | +/* | |
| 2 | + * Copyright (C) 2012-2015 DataStax Inc. | |
| 3 | + * | |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | |
| 5 | + * you may not use this file except in compliance with the License. | |
| 6 | + * You may obtain a copy of the License at | |
| 7 | + * | |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 | |
| 9 | + * | |
| 10 | + * Unless required by applicable law or agreed to in writing, software | |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, | |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 13 | + * See the License for the specific language governing permissions and | |
| 14 | + * limitations under the License. | |
| 15 | + */ | |
| 16 | +package com.datastax.driver.extras.codecs.arrays; | |
| 17 | + | |
| 18 | +import java.nio.ByteBuffer; | |
| 19 | + | |
| 20 | +import com.datastax.driver.core.DataType; | |
| 21 | +import com.datastax.driver.core.ProtocolVersion; | |
| 22 | + | |
| 23 | +/** | |
| 24 | + * A codec that maps the CQL type {@code list<long>} to the Java type {@code long[]}. | |
| 25 | + * <p> | |
| 26 | + * Note that this codec is designed for performance and converts CQL lists | |
| 27 | + * <em>directly</em> to {@code long[]}, thus avoiding any unnecessary | |
| 28 | + * boxing and unboxing of Java primitive {@code long} values; | |
| 29 | + * it also instantiates arrays without the need for an intermediary | |
| 30 | + * Java {@code List} object. | |
| 31 | + */ | |
| 32 | +public class LongArrayCodec extends AbstractPrimitiveArrayCodec<long[]> { | |
| 33 | + | |
| 34 | + public static final LongArrayCodec instance = new LongArrayCodec(); | |
| 35 | + | |
| 36 | + public LongArrayCodec() { | |
| 37 | + super(DataType.list(DataType.bigint()), long[].class); | |
| 38 | + } | |
| 39 | + | |
| 40 | + @Override | |
| 41 | + protected int sizeOfComponentType() { | |
| 42 | + return 8; | |
| 43 | + } | |
| 44 | + | |
| 45 | + @Override | |
| 46 | + protected void serializeElement(ByteBuffer output, long[] array, int index, ProtocolVersion protocolVersion) { | |
| 47 | + output.putLong(array[index]); | |
| 48 | + } | |
| 49 | + | |
| 50 | + @Override | |
| 51 | + protected void deserializeElement(ByteBuffer input, long[] array, int index, ProtocolVersion protocolVersion) { | |
| 52 | + array[index] = input.getLong(); | |
| 53 | + } | |
| 54 | + | |
| 55 | + @Override | |
| 56 | + protected void formatElement(StringBuilder output, long[] array, int index) { | |
| 57 | + output.append(array[index]); | |
| 58 | + } | |
| 59 | + | |
| 60 | + @Override | |
| 61 | + protected void parseElement(String input, long[] array, int index) { | |
| 62 | + array[index] = Long.parseLong(input); | |
| 63 | + } | |
| 64 | + | |
| 65 | + @Override | |
| 66 | + protected long[] newInstance(int size) { | |
| 67 | + return new long[size]; | |
| 68 | + } | |
| 69 | + | |
| 70 | +} |
added driver-extras/src/main/java/com/datastax/driver/extras/codecs/arrays/ObjectArrayCodec.java +109 −0
| @@ -0,0 +1,109 @@ | ||
| 1 | +/* | |
| 2 | + * Copyright (C) 2012-2015 DataStax Inc. | |
| 3 | + * | |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | |
| 5 | + * you may not use this file except in compliance with the License. | |
| 6 | + * You may obtain a copy of the License at | |
| 7 | + * | |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 | |
| 9 | + * | |
| 10 | + * Unless required by applicable law or agreed to in writing, software | |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, | |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 13 | + * See the License for the specific language governing permissions and | |
| 14 | + * limitations under the License. | |
| 15 | + */ | |
| 16 | +package com.datastax.driver.extras.codecs.arrays; | |
| 17 | + | |
| 18 | +import java.lang.reflect.Array; | |
| 19 | +import java.nio.BufferUnderflowException; | |
| 20 | +import java.nio.ByteBuffer; | |
| 21 | + | |
| 22 | +import com.datastax.driver.core.CodecUtils; | |
| 23 | +import com.datastax.driver.core.DataType; | |
| 24 | +import com.datastax.driver.core.ProtocolVersion; | |
| 25 | +import com.datastax.driver.core.TypeCodec; | |
| 26 | +import com.datastax.driver.core.exceptions.InvalidTypeException; | |
| 27 | + | |
| 28 | +/** | |
| 29 | + * Codec dealing with Java object arrays. | |
| 30 | + * Serialization and deserialization of elements in the array is | |
| 31 | + * delegated to the provided element codec. | |
| 32 | + * <p> | |
| 33 | + * For example, to create a codec that maps {@code list<text>} to {@code String[]}, | |
| 34 | + * declare the following: | |
| 35 | + * <pre>{@code | |
| 36 | + * ObjectArrayCodec<String> stringArrayCodec = new ObjectArrayCodec<>( | |
| 37 | + * DataType.list(DataType.varchar()), | |
| 38 | + * String[].class, | |
| 39 | + * TypeCodec.varchar()); | |
| 40 | + * }</pre> | |
| 41 | + * | |
| 42 | + * @param <E> The Java array component type this codec handles | |
| 43 | + */ | |
| 44 | +public class ObjectArrayCodec<E> extends AbstractArrayCodec<E[]> { | |
| 45 | + | |
| 46 | + protected final TypeCodec<E> eltCodec; | |
| 47 | + | |
| 48 | + public ObjectArrayCodec(DataType.CollectionType cqlType, Class<E[]> javaClass, TypeCodec<E> eltCodec) { | |
| 49 | + super(cqlType, javaClass); | |
| 50 | + this.eltCodec = eltCodec; | |
| 51 | + } | |
| 52 | + | |
| 53 | + @Override | |
| 54 | + public ByteBuffer serialize(E[] value, ProtocolVersion protocolVersion) { | |
| 55 | + if (value == null) | |
| 56 | + return null; | |
| 57 | + int i = 0; | |
| 58 | + ByteBuffer[] bbs = new ByteBuffer[value.length]; | |
| 59 | + for (E elt : value) { | |
| 60 | + if (elt == null) { | |
| 61 | + throw new NullPointerException("Collection elements cannot be null"); | |
| 62 | + } | |
| 63 | + ByteBuffer bb; | |
| 64 | + try { | |
| 65 | + bb = eltCodec.serialize(elt, protocolVersion); | |
| 66 | + } catch (ClassCastException e) { | |
| 67 | + throw new InvalidTypeException( | |
| 68 | + String.format("Invalid type for %s element, expecting %s but got %s", | |
| 69 | + cqlType, eltCodec.getJavaType(), elt.getClass()), e); | |
| 70 | + } | |
| 71 | + bbs[i++] = bb; | |
| 72 | + } | |
| 73 | + return CodecUtils.pack(bbs, value.length, protocolVersion); | |
| 74 | + } | |
| 75 | + | |
| 76 | + @Override | |
| 77 | + public E[] deserialize(ByteBuffer bytes, ProtocolVersion protocolVersion) { | |
| 78 | + if (bytes == null || bytes.remaining() == 0) | |
| 79 | + return newInstance(0); | |
| 80 | + try { | |
| 81 | + ByteBuffer input = bytes.duplicate(); | |
| 82 | + int n = CodecUtils.readSize(input, protocolVersion); | |
| 83 | + E[] array = newInstance(n); | |
| 84 | + for (int i = 0; i < n; i++) { | |
| 85 | + ByteBuffer databb = CodecUtils.readValue(input, protocolVersion); | |
| 86 | + array[i] = eltCodec.deserialize(databb, protocolVersion); | |
| 87 | + } | |
| 88 | + return array; | |
| 89 | + } catch (BufferUnderflowException e) { | |
| 90 | + throw new InvalidTypeException("Not enough bytes to deserialize list"); | |
| 91 | + } | |
| 92 | + } | |
| 93 | + | |
| 94 | + @Override | |
| 95 | + protected void formatElement(StringBuilder output, E[] array, int index) { | |
| 96 | + output.append(eltCodec.format(array[index])); | |
| 97 | + } | |
| 98 | + | |
| 99 | + @Override | |
| 100 | + protected void parseElement(String input, E[] array, int index) { | |
| 101 | + array[index] = eltCodec.parse(input); | |
| 102 | + } | |
| 103 | + | |
| 104 | + @Override | |
| 105 | + @SuppressWarnings("unchecked") | |
| 106 | + protected E[] newInstance(int size) { | |
| 107 | + return (E[])Array.newInstance(getJavaType().getRawType().getComponentType(), size); | |
| 108 | + } | |
| 109 | +} |
added driver-extras/src/main/java/com/datastax/driver/extras/codecs/arrays/package-info.java +57 −0
| @@ -0,0 +1,57 @@ | ||
| 1 | +/* | |
| 2 | + * Copyright (C) 2012-2015 DataStax Inc. | |
| 3 | + * | |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | |
| 5 | + * you may not use this file except in compliance with the License. | |
| 6 | + * You may obtain a copy of the License at | |
| 7 | + * | |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 | |
| 9 | + * | |
| 10 | + * Unless required by applicable law or agreed to in writing, software | |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, | |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 13 | + * See the License for the specific language governing permissions and | |
| 14 | + * limitations under the License. | |
| 15 | + */ | |
| 16 | + | |
| 17 | +/** | |
| 18 | + * This package contains a collection of convenience | |
| 19 | + * {@link com.datastax.driver.core.TypeCodec TypeCodec} instances useful for | |
| 20 | + * serializing between CQL lists and Java arrays. | |
| 21 | + * <p> | |
| 22 | + * The codecs in this package provide the following mappings: | |
| 23 | + * | |
| 24 | + * <table summary="Supported Mappings"> | |
| 25 | + * <tr> | |
| 26 | + * <th>Codec</th> | |
| 27 | + * <th>CQL type</th> | |
| 28 | + * <th>Java type</th> | |
| 29 | + * </tr> | |
| 30 | + * <tr> | |
| 31 | + * <td>{@link com.datastax.driver.extras.codecs.arrays.ObjectArrayCodec}</td> | |
| 32 | + * <td>{@code list<?>}</td> | |
| 33 | + * <td>{@code T[]}</td> | |
| 34 | + * </tr> | |
| 35 | + * <tr> | |
| 36 | + * <td>{@link com.datastax.driver.extras.codecs.arrays.IntArrayCodec}</td> | |
| 37 | + * <td>{@code list<int>}</td> | |
| 38 | + * <td>{@code int[]}</td> | |
| 39 | + * </tr> | |
| 40 | + * <tr> | |
| 41 | + * <td>{@link com.datastax.driver.extras.codecs.arrays.LongArrayCodec}</td> | |
| 42 | + * <td>{@code list<long>}</td> | |
| 43 | + * <td>{@code long[]}</td> | |
| 44 | + * </tr> | |
| 45 | + * <tr> | |
| 46 | + * <td>{@link com.datastax.driver.extras.codecs.arrays.FloatArrayCodec}</td> | |
| 47 | + * <td>{@code list<float>}</td> | |
| 48 | + * <td>{@code float[]}</td> | |
| 49 | + * </tr> | |
| 50 | + * <tr> | |
| 51 | + * <td>{@link com.datastax.driver.extras.codecs.arrays.DoubleArrayCodec}</td> | |
| 52 | + * <td>{@code list<double>}</td> | |
| 53 | + * <td>{@code double[]}</td> | |
| 54 | + * </tr> | |
| 55 | + * </table> | |
| 56 | + */ | |
| 57 | +package com.datastax.driver.extras.codecs.arrays; |
added driver-extras/src/test/java/com/datastax/driver/extras/codecs/arrays/ArrayCodecsTest.java +224 −0
| @@ -0,0 +1,224 @@ | ||
| 1 | +/* | |
| 2 | + * Copyright (C) 2012-2015 DataStax Inc. | |
| 3 | + * | |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | |
| 5 | + * you may not use this file except in compliance with the License. | |
| 6 | + * You may obtain a copy of the License at | |
| 7 | + * | |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 | |
| 9 | + * | |
| 10 | + * Unless required by applicable law or agreed to in writing, software | |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, | |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 13 | + * See the License for the specific language governing permissions and | |
| 14 | + * limitations under the License. | |
| 15 | + */ | |
| 16 | +package com.datastax.driver.extras.codecs.arrays; | |
| 17 | + | |
| 18 | +import java.util.Collection; | |
| 19 | +import java.util.List; | |
| 20 | + | |
| 21 | +import com.google.common.collect.Lists; | |
| 22 | +import com.google.common.primitives.Primitives; | |
| 23 | +import org.testng.annotations.DataProvider; | |
| 24 | +import org.testng.annotations.Test; | |
| 25 | + | |
| 26 | +import static org.assertj.core.api.Assertions.assertThat; | |
| 27 | + | |
| 28 | +import com.datastax.driver.core.*; | |
| 29 | +import com.datastax.driver.mapping.Mapper; | |
| 30 | +import com.datastax.driver.mapping.MappingManager; | |
| 31 | +import com.datastax.driver.mapping.annotations.Column; | |
| 32 | +import com.datastax.driver.mapping.annotations.PartitionKey; | |
| 33 | +import com.datastax.driver.mapping.annotations.Table; | |
| 34 | + | |
| 35 | +public class ArrayCodecsTest extends CCMBridge.PerClassSingleNodeCluster { | |
| 36 | + | |
| 37 | + private static final ObjectArrayCodec<String> stringArrayCodec = new ObjectArrayCodec<String>( | |
| 38 | + DataType.list(DataType.varchar()), | |
| 39 | + String[].class, | |
| 40 | + TypeCodec.varchar()); | |
| 41 | + | |
| 42 | + @Override | |
| 43 | + protected Collection<String> getTableDefinitions() { | |
| 44 | + return Lists.newArrayList( | |
| 45 | + "CREATE TABLE lists (" | |
| 46 | + + "pk int PRIMARY KEY, " | |
| 47 | + + "l_int list<int>, " | |
| 48 | + + "l_bigint list<bigint>, " | |
| 49 | + + "l_float list<float>, " | |
| 50 | + + "l_double list<double>, " | |
| 51 | + + "l_string list<text> " | |
| 52 | + + ")", | |
| 53 | + "INSERT INTO lists (pk, l_int, l_bigint, l_float, l_double, l_string) VALUES (1, " | |
| 54 | + + "[1, 2, 3], " | |
| 55 | + + "[4, 5, 6], " | |
| 56 | + + "[1.0, 2.0, 3.0], " | |
| 57 | + + "[4.0, 5.0, 6.0], " | |
| 58 | + + "['a', 'b', 'c'] " | |
| 59 | + + ")" | |
| 60 | + ); | |
| 61 | + } | |
| 62 | + | |
| 63 | + @Override | |
| 64 | + protected Cluster.Builder configure(Cluster.Builder builder) { | |
| 65 | + return builder.withCodecRegistry( | |
| 66 | + new CodecRegistry() | |
| 67 | + .register(IntArrayCodec.instance) | |
| 68 | + .register(LongArrayCodec.instance) | |
| 69 | + .register(FloatArrayCodec.instance) | |
| 70 | + .register(DoubleArrayCodec.instance) | |
| 71 | + .register(stringArrayCodec) | |
| 72 | + ); | |
| 73 | + } | |
| 74 | + | |
| 75 | + @DataProvider(name = "ArrayCodecsTest-serializing") | |
| 76 | + public static Object[][] parametersForSerializationTests() { | |
| 77 | + return new Object[][]{ | |
| 78 | + { "l_int", int[].class, new int[]{ 1, 2, 3 } }, | |
| 79 | + { "l_bigint", long[].class, new long[]{ 4, 5, 6 } }, | |
| 80 | + { "l_float", float[].class, new float[]{ 1, 2, 3 } }, | |
| 81 | + { "l_double", double[].class, new double[]{ 4, 5, 6 } }, | |
| 82 | + { "l_string", String[].class, new String[]{ "a", "b", "c" } } | |
| 83 | + }; | |
| 84 | + } | |
| 85 | + | |
| 86 | + @DataProvider(name = "ArrayCodecsTest-formatting") | |
| 87 | + public static Object[][] parametersForFormattingTests() { | |
| 88 | + return new Object[][]{ | |
| 89 | + { new IntArrayCodec(), new int[]{ 1, 2, 3 }, new int[0], "[1,2,3]" }, | |
| 90 | + { new LongArrayCodec(), new long[]{ 4, 5, 6 }, new long[0], "[4,5,6]" }, | |
| 91 | + { new FloatArrayCodec(), new float[]{ 1, 2, 3 }, new float[0], "[1.0,2.0,3.0]" }, | |
| 92 | + { new DoubleArrayCodec(), new double[]{ 4, 5, 6 }, new double[0], "[4.0,5.0,6.0]" }, | |
| 93 | + { stringArrayCodec, new String[]{ "a", "b", "c" }, new String[0], "['a','b','c']" } | |
| 94 | + }; | |
| 95 | + } | |
| 96 | + | |
| 97 | + @Test(groups = "short", dataProvider = "ArrayCodecsTest-serializing") | |
| 98 | + public <A> void should_read_list_column_as_array(String columnName, Class<A> arrayClass, A expected) { | |
| 99 | + Row row = session.execute(String.format("SELECT %s FROM lists WHERE pk = 1", columnName)).one(); | |
| 100 | + A actual = row.get(columnName, arrayClass); | |
| 101 | + assertThat(actual).isEqualTo(expected); | |
| 102 | + } | |
| 103 | + | |
| 104 | + @Test(groups = "short", dataProvider = "ArrayCodecsTest-serializing") | |
| 105 | + public <A> void should_set_list_column_with_array(String columnName, Class<A> arrayClass, A expected) { | |
| 106 | + PreparedStatement ps = session.prepare(String.format("INSERT INTO lists (pk, %s) VALUES (?, ?)", columnName)); | |
| 107 | + BoundStatement bs = ps.bind() | |
| 108 | + .setInt(0, 2) | |
| 109 | + .set(columnName, expected, arrayClass); | |
| 110 | + session.execute(bs); | |
| 111 | + Row row = session.execute(String.format("SELECT %s FROM lists WHERE pk = 2", columnName)).one(); | |
| 112 | + List<?> list = row.getList(columnName, Primitives.wrap(arrayClass.getComponentType())); | |
| 113 | + assertThat(list.toArray()).isEqualTo(expected); | |
| 114 | + } | |
| 115 | + | |
| 116 | + @Test(groups = "unit", dataProvider = "ArrayCodecsTest-formatting") | |
| 117 | + public <A> void should_format_array_as_cql_literal(TypeCodec<A> codec, A array, A emptyArray, String cql) { | |
| 118 | + assertThat(codec.format(array)).isEqualTo(cql); | |
| 119 | + assertThat(codec.format(emptyArray)).isEqualTo("[]"); | |
| 120 | + assertThat(codec.format(null)).isEqualToIgnoringCase("NULL"); | |
| 121 | + } | |
| 122 | + | |
| 123 | + @Test(groups = "unit", dataProvider = "ArrayCodecsTest-formatting") | |
| 124 | + public <A> void should_parse_array_from_cql_literal(TypeCodec<A> codec, A array, A emptyArray, String cql) { | |
| 125 | + assertThat(codec.parse(cql)).isEqualTo(array); | |
| 126 | + assertThat(codec.parse("[]")).isEqualTo(emptyArray); | |
| 127 | + assertThat(codec.parse("NULL")).isNull(); | |
| 128 | + assertThat(codec.parse("")).isNull(); | |
| 129 | + assertThat(codec.parse(null)).isNull(); | |
| 130 | + } | |
| 131 | + | |
| 132 | + @Test(groups = "short") | |
| 133 | + public void should_use_mapper_to_store_and_retrieve_values_with_custom_joda_codecs() { | |
| 134 | + // given | |
| 135 | + MappingManager manager = new MappingManager(session); | |
| 136 | + Mapper<Mapped> mapper = manager.mapper(Mapped.class); | |
| 137 | + // when | |
| 138 | + Mapped pojo = new Mapped(); | |
| 139 | + mapper.save(pojo); | |
| 140 | + Mapped actual = mapper.get(42); | |
| 141 | + // then | |
| 142 | + assertThat(actual).isEqualToComparingFieldByField(pojo); | |
| 143 | + } | |
| 144 | + | |
| 145 | + @SuppressWarnings("unused") | |
| 146 | + @Table(name = "lists") | |
| 147 | + public static class Mapped { | |
| 148 | + | |
| 149 | + @PartitionKey | |
| 150 | + private int pk; | |
| 151 | + | |
| 152 | + @Column(name = "l_string") | |
| 153 | + private String[] strings; | |
| 154 | + | |
| 155 | + @Column(name = "l_int") | |
| 156 | + private int[] ints; | |
| 157 | + | |
| 158 | + @Column(name = "l_bigint") | |
| 159 | + private long[] longs; | |
| 160 | + | |
| 161 | + @Column(name = "l_float") | |
| 162 | + private float[] floats; | |
| 163 | + | |
| 164 | + @Column(name = "l_double") | |
| 165 | + private double[] doubles; | |
| 166 | + | |
| 167 | + public Mapped() { | |
| 168 | + pk = 42; | |
| 169 | + strings = new String[]{ "a", "b", "c" }; | |
| 170 | + ints = new int[]{ 1, 2, 3 }; | |
| 171 | + longs = new long[]{ 4, 5, 6 }; | |
| 172 | + floats = new float[]{ 1, 2, 3 }; | |
| 173 | + doubles = new double[]{ 4, 5, 6 }; | |
| 174 | + } | |
| 175 | + | |
| 176 | + public int getPk() { | |
| 177 | + return pk; | |
| 178 | + } | |
| 179 | + | |
| 180 | + public void setPk(int pk) { | |
| 181 | + this.pk = pk; | |
| 182 | + } | |
| 183 | + | |
| 184 | + public String[] getStrings() { | |
| 185 | + return strings; | |
| 186 | + } | |
| 187 | + | |
| 188 | + public void setStrings(String[] strings) { | |
| 189 | + this.strings = strings; | |
| 190 | + } | |
| 191 | + | |
| 192 | + public int[] getInts() { | |
| 193 | + return ints; | |
| 194 | + } | |
| 195 | + | |
| 196 | + public void setInts(int[] ints) { | |
| 197 | + this.ints = ints; | |
| 198 | + } | |
| 199 | + | |
| 200 | + public long[] getLongs() { | |
| 201 | + return longs; | |
| 202 | + } | |
| 203 | + | |
| 204 | + public void setLongs(long[] longs) { | |
| 205 | + this.longs = longs; | |
| 206 | + } | |
| 207 | + | |
| 208 | + public float[] getFloats() { | |
| 209 | + return floats; | |
| 210 | + } | |
| 211 | + | |
| 212 | + public void setFloats(float[] floats) { | |
| 213 | + this.floats = floats; | |
| 214 | + } | |
| 215 | + | |
| 216 | + public double[] getDoubles() { | |
| 217 | + return doubles; | |
| 218 | + } | |
| 219 | + | |
| 220 | + public void setDoubles(double[] doubles) { | |
| 221 | + this.doubles = doubles; | |
| 222 | + } | |
| 223 | + } | |
| 224 | +} |