kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | 74f9324b5c473803c51fa68c3e2c36cf15387c22eb7e317fcd20ea270d91d882 |
|---|---|
| Author | Sylvain Lebresne <sylvain@datastax.com> · 2013-12-10 02:37 -08:00 |
| Committer | Sylvain Lebresne <sylvain@datastax.com> · 2013-12-10 02:37 -08:00 |
| Parents | 6e5e17356592, 8efaa66f722b |
2 files changed
modified driver-core/src/main/java/com/datastax/driver/core/Codec.java +74 −8
| @@ -15,14 +15,12 @@ | ||
| 15 | 15 | */ |
| 16 | 16 | package com.datastax.driver.core; |
| 17 | 17 | |
| 18 | -import java.util.*; | |
| 19 | - | |
| 20 | -import com.datastax.driver.core.DataType; | |
| 21 | - | |
| 22 | 18 | import com.google.common.collect.ImmutableMap; |
| 23 | - | |
| 19 | +import com.google.common.collect.Maps; | |
| 24 | 20 | import org.apache.cassandra.db.marshal.*; |
| 25 | 21 | |
| 22 | +import java.util.Map; | |
| 23 | + | |
| 26 | 24 | /** |
| 27 | 25 | * Static method to code/decode serialized data given their types. |
| 28 | 26 | */ |
| @@ -47,6 +45,46 @@ | ||
| 47 | 45 | .put(TimeUUIDType.instance, DataType.timeuuid()) |
| 48 | 46 | .build(); |
| 49 | 47 | |
| 48 | + private static Map<DataType.Name, SetType<?>> SETS = buildSets(); | |
| 49 | + private static Map<DataType.Name, ListType<?>> LISTS = buildLists(); | |
| 50 | + private static Map<DataType.Name, Map<DataType.Name, MapType<?, ?>>> MAPS = buildMaps(); | |
| 51 | + | |
| 52 | + | |
| 53 | + protected static Map<DataType.Name, Map<DataType.Name, MapType<?, ?>>> buildMaps() { | |
| 54 | + | |
| 55 | + ImmutableMap.Builder<DataType.Name, Map<DataType.Name, MapType<?, ?>>> mapsBuilder = ImmutableMap.builder(); | |
| 56 | + | |
| 57 | + for (DataType typeArg1 : DataType.allPrimitiveTypes()) { | |
| 58 | + ImmutableMap.Builder<DataType.Name, MapType<?, ?>> innerMapsBuilder = ImmutableMap.builder(); | |
| 59 | + for (DataType typeArg2 : DataType.allPrimitiveTypes()) { | |
| 60 | + innerMapsBuilder.put( | |
| 61 | + typeArg2.getName(), | |
| 62 | + MapType.getInstance(getCodec(typeArg1), getCodec(typeArg2)) | |
| 63 | + ); | |
| 64 | + } | |
| 65 | + mapsBuilder.put(typeArg1.getName(), Maps.newEnumMap(innerMapsBuilder.build())); | |
| 66 | + } | |
| 67 | + return Maps.newEnumMap(mapsBuilder.build()); | |
| 68 | + } | |
| 69 | + | |
| 70 | + protected static ImmutableMap<DataType.Name, ListType<?>> buildLists() { | |
| 71 | + ImmutableMap.Builder<DataType.Name, ListType<?>> listsBuilder = ImmutableMap.builder(); | |
| 72 | + for (DataType typeArg : DataType.allPrimitiveTypes()) { | |
| 73 | + listsBuilder.put(typeArg.getName(), ListType.getInstance(getCodec(typeArg))); | |
| 74 | + } | |
| 75 | + return Maps.immutableEnumMap(listsBuilder.build()); | |
| 76 | + } | |
| 77 | + | |
| 78 | + protected static ImmutableMap<DataType.Name, SetType<?>> buildSets() { | |
| 79 | + ImmutableMap.Builder<DataType.Name, SetType<?>> setsBuilder = ImmutableMap.builder(); | |
| 80 | + for (DataType typeArg : DataType.allPrimitiveTypes()) { | |
| 81 | + final AbstractType<Object> codec = getCodec(typeArg); | |
| 82 | + setsBuilder.put(typeArg.getName(), SetType.getInstance(codec)); | |
| 83 | + } | |
| 84 | + return Maps.immutableEnumMap(setsBuilder.build()); | |
| 85 | + } | |
| 86 | + | |
| 87 | + | |
| 50 | 88 | private Codec() {} |
| 51 | 89 | |
| 52 | 90 | @SuppressWarnings("unchecked") |
| @@ -72,15 +110,43 @@ | ||
| 72 | 110 | case VARCHAR: return UTF8Type.instance; |
| 73 | 111 | case VARINT: return IntegerType.instance; |
| 74 | 112 | case TIMEUUID: return TimeUUIDType.instance; |
| 75 | - case LIST: return ListType.getInstance(getCodec(type.getTypeArguments().get(0))); | |
| 76 | - case SET: return SetType.getInstance(getCodec(type.getTypeArguments().get(0))); | |
| 77 | - case MAP: return MapType.getInstance(getCodec(type.getTypeArguments().get(0)), getCodec(type.getTypeArguments().get(1))); | |
| 113 | + case LIST: return getListType(type); | |
| 114 | + case SET: return getSetType(type); | |
| 115 | + case MAP: return getMapType(type); | |
| 78 | 116 | // We don't interpret custom values in any way |
| 79 | 117 | case CUSTOM: return BytesType.instance; |
| 80 | 118 | default: throw new RuntimeException("Unknown type"); |
| 81 | 119 | } |
| 82 | 120 | } |
| 83 | 121 | |
| 122 | + private static MapType<?, ?> getMapType(DataType type) { | |
| 123 | + final DataType dataTypeArg0 = type.getTypeArguments().get(0); | |
| 124 | + final DataType dateTypeArg1 = type.getTypeArguments().get(1); | |
| 125 | + | |
| 126 | + if(dataTypeArg0.getName() == DataType.Name.CUSTOM || dateTypeArg1.getName() == DataType.Name.CUSTOM) { | |
| 127 | + return MapType.getInstance(getCodecInternal(dataTypeArg0), getCodecInternal(dateTypeArg1)); | |
| 128 | + } | |
| 129 | + return MAPS.get(dataTypeArg0.getName()).get(dateTypeArg1.getName()); | |
| 130 | + } | |
| 131 | + | |
| 132 | + private static SetType<?> getSetType(DataType type) { | |
| 133 | + final DataType dataTypeArg0 = type.getTypeArguments().get(0); | |
| 134 | + | |
| 135 | + if(dataTypeArg0.getName() == DataType.Name.CUSTOM) { | |
| 136 | + return SetType.getInstance(getCodecInternal(dataTypeArg0)); | |
| 137 | + } | |
| 138 | + return SETS.get(type.getTypeArguments().get(0).getName()); | |
| 139 | + } | |
| 140 | + | |
| 141 | + private static ListType<?> getListType(final DataType type) { | |
| 142 | + final DataType dataTypeArg0 = type.getTypeArguments().get(0); | |
| 143 | + | |
| 144 | + if(dataTypeArg0.getName() == DataType.Name.CUSTOM) { | |
| 145 | + return ListType.getInstance(getCodecInternal(dataTypeArg0)); | |
| 146 | + } | |
| 147 | + return LISTS.get(type.getTypeArguments().get(0).getName()); | |
| 148 | + } | |
| 149 | + | |
| 84 | 150 | public static DataType rawTypeToDataType(AbstractType<?> rawType) { |
| 85 | 151 | if (rawType instanceof ReversedType<?>) |
| 86 | 152 | rawType = ((ReversedType<?>) rawType).baseType; |
added driver-core/src/test/java/com/datastax/driver/core/CodecTest.java +143 −0
| @@ -0,0 +1,143 @@ | ||
| 1 | +/* | |
| 2 | + * Copyright (C) 2012 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 org.apache.cassandra.db.marshal.*; | |
| 19 | +import org.apache.cassandra.utils.Pair; | |
| 20 | +import org.testng.Assert; | |
| 21 | +import org.testng.annotations.Test; | |
| 22 | +import org.testng.collections.Maps; | |
| 23 | + | |
| 24 | +import java.util.*; | |
| 25 | + | |
| 26 | +import static com.datastax.driver.core.DataType.*; | |
| 27 | + | |
| 28 | +public class CodecTest { | |
| 29 | + | |
| 30 | + public static final DataType CUSTOM_FOO = DataType.custom("com.example.FooBar"); | |
| 31 | + | |
| 32 | + @Test(groups = "unit") | |
| 33 | + public void testCustomList() throws Exception { | |
| 34 | + ListType listType = (ListType) Codec.getCodec(DataType.list(CUSTOM_FOO)); | |
| 35 | + Assert.assertNotNull(listType); | |
| 36 | + Assert.assertTrue(listType.valueComparator() instanceof BytesType); | |
| 37 | + | |
| 38 | + } | |
| 39 | + | |
| 40 | + | |
| 41 | + @Test(groups = "unit") | |
| 42 | + public void testCustomSet() throws Exception { | |
| 43 | + SetType setType = (SetType) Codec.getCodec(DataType.set(CUSTOM_FOO)); | |
| 44 | + Assert.assertNotNull(setType); | |
| 45 | + Assert.assertEquals(setType.nameComparator().getClass(),BytesType.class); | |
| 46 | + } | |
| 47 | + | |
| 48 | + | |
| 49 | + @Test(groups = "unit") | |
| 50 | + public void testCustomKeyMap() throws Exception { | |
| 51 | + MapType mapType = (MapType) Codec.getCodec(DataType.map(CUSTOM_FOO, text())); | |
| 52 | + Assert.assertNotNull(mapType); | |
| 53 | + Assert.assertEquals(mapType.nameComparator().getClass(), BytesType.class); | |
| 54 | + Assert.assertEquals(mapType.valueComparator().getClass(), UTF8Type.class); | |
| 55 | + } | |
| 56 | + | |
| 57 | + | |
| 58 | + @Test(groups = "unit") | |
| 59 | + public void testCustomValueMap() throws Exception { | |
| 60 | + MapType mapType = (MapType) Codec.getCodec(DataType.map(text(), CUSTOM_FOO)); | |
| 61 | + Assert.assertNotNull(mapType); | |
| 62 | + Assert.assertEquals(mapType.nameComparator().getClass(), UTF8Type.class); | |
| 63 | + Assert.assertEquals(mapType.valueComparator().getClass(), BytesType.class); | |
| 64 | + } | |
| 65 | + | |
| 66 | + | |
| 67 | + @Test(groups = "unit") | |
| 68 | + public void testSetTypeCodec() throws Exception { | |
| 69 | + Map<Name, SetType<?>> dateTypeSetTypeMap = new HashMap<Name, SetType<?>>(Codec.buildSets()); | |
| 70 | + | |
| 71 | + final Set<DataType> dataTypes = new HashSet<DataType>(allPrimitiveTypes()); | |
| 72 | + | |
| 73 | + //Special case: DataType varchar yields Codec.rawTypeToDataType(...) => text causing assertion fail | |
| 74 | + //Solution: Remove DataType.varchar and check that it yields DataType.text | |
| 75 | + Assert.assertTrue(dataTypes.remove(varchar())); | |
| 76 | + Assert.assertEquals(Codec.rawTypeToDataType(dateTypeSetTypeMap.get(varchar().getName()).nameComparator()), text()) ; | |
| 77 | + dateTypeSetTypeMap.remove(varchar().getName()); | |
| 78 | + | |
| 79 | + for (DataType dataType : dataTypes) { | |
| 80 | + final SetType<?> setType = dateTypeSetTypeMap.remove(dataType.getName()); | |
| 81 | + Assert.assertEquals(Codec.rawTypeToDataType(setType.nameComparator()),dataType) ; | |
| 82 | + } | |
| 83 | + Assert.assertTrue(dateTypeSetTypeMap.isEmpty(), "All map types should have been tested: "+dateTypeSetTypeMap); | |
| 84 | + } | |
| 85 | + | |
| 86 | + @Test(groups = "unit") | |
| 87 | + public void testListTypeCodec() throws Exception { | |
| 88 | + Map<Name,ListType<?>> dateTypeListetTypeMap = new HashMap<Name, ListType<?>>(Codec.buildLists()); | |
| 89 | + | |
| 90 | + final Set<DataType> dataTypes = new HashSet<DataType>(allPrimitiveTypes()); | |
| 91 | + | |
| 92 | + //Special case: DataType varchar yields Codec.rawTypeToDataType(...) => text causing assertion fail | |
| 93 | + //Solution: Remove DataType.varchar and check that it yields DataType.text | |
| 94 | + Assert.assertTrue(dataTypes.remove(varchar())); | |
| 95 | + final AbstractType<?> rawType = dateTypeListetTypeMap.get(varchar().getName()).valueComparator(); | |
| 96 | + Assert.assertEquals(Codec.rawTypeToDataType(rawType), text()) ; | |
| 97 | + dateTypeListetTypeMap.remove(varchar().getName()); | |
| 98 | + | |
| 99 | + | |
| 100 | + for (DataType dataType : dataTypes) { | |
| 101 | + final ListType<?> setType = dateTypeListetTypeMap.remove(dataType.getName()); | |
| 102 | + Assert.assertEquals(Codec.rawTypeToDataType(setType.valueComparator()),dataType) ; | |
| 103 | + } | |
| 104 | + Assert.assertTrue(dateTypeListetTypeMap.isEmpty(), "All set types should have been tested: "+dateTypeListetTypeMap); | |
| 105 | + | |
| 106 | + } | |
| 107 | + | |
| 108 | + @Test(groups = "unit") | |
| 109 | + public void testMapTypeCodec() throws Exception { | |
| 110 | + final Map<Name, Map<Name, MapType<?, ?>>> dateTypeMapTypeMap = Maps.newHashMap(Codec.buildMaps()); | |
| 111 | + | |
| 112 | + for (DataType dataTypeArg0 : allPrimitiveTypes()) { | |
| 113 | + final Map<Name, MapType<?, ?>> innerNameToMapType = Maps.newHashMap(dateTypeMapTypeMap.get(dataTypeArg0.getName())); | |
| 114 | + | |
| 115 | + for (DataType dataTypeArg1 : allPrimitiveTypes()) { | |
| 116 | + final MapType<?, ?> mapType = innerNameToMapType.remove(dataTypeArg1.getName()); | |
| 117 | + testPairAgainstMapType(dataTypeArg0, dataTypeArg1, mapType); | |
| 118 | + } | |
| 119 | + Assert.assertTrue(innerNameToMapType.isEmpty(), | |
| 120 | + "All inner map types should have been tested for outer type '"+dataTypeArg0+"': " + dateTypeMapTypeMap); | |
| 121 | + dateTypeMapTypeMap.remove(dataTypeArg0.getName()); | |
| 122 | + } | |
| 123 | + Assert.assertTrue(dateTypeMapTypeMap.isEmpty(), "All map types should have been tested: "+dateTypeMapTypeMap); | |
| 124 | + } | |
| 125 | + | |
| 126 | + private Pair<Name, Name> convertToNamePair(Pair<DataType, DataType> pair) { | |
| 127 | + return Pair.create(pair.left.getName(), pair.right.getName()); | |
| 128 | + } | |
| 129 | + | |
| 130 | + | |
| 131 | + private void testPairAgainstMapType(DataType dataTypeArg0, DataType dataTypeArg1, MapType<?, ?> mapType) { | |
| 132 | + //Special case: DataType varchar yields Codec.rawTypeToDataType(...) => text causing assertion fail | |
| 133 | + //Solution: Remove DataType.varchar and check that it yields DataType.text | |
| 134 | + | |
| 135 | + //In the map case this would be quite many combinations of text/varchar so I translate varchar to text | |
| 136 | + //in order to expect text instead of varchar back from those cases: | |
| 137 | + if(dataTypeArg0.equals(varchar())) dataTypeArg0 = text(); | |
| 138 | + if(dataTypeArg1.equals(varchar())) dataTypeArg1 = text(); | |
| 139 | + | |
| 140 | + Assert.assertEquals(Codec.rawTypeToDataType(mapType.nameComparator()), dataTypeArg0) ; | |
| 141 | + Assert.assertEquals(Codec.rawTypeToDataType(mapType.valueComparator()),dataTypeArg1) ; | |
| 142 | + } | |
| 143 | +} |