kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | 1a09a0064d51aa3aa1ea32a7c01bbbd29f2f374d0066e1853688a1b651e4d4fa |
|---|---|
| Author | Olve Sæther Hansen <olve@vimond.com> · 2013-12-09 13:24 +01:00 |
| Committer | Olve Sæther Hansen <olve@vimond.com> · 2013-12-09 13:24 +01:00 |
| Parents | 6e5e17356592 |
2 files changed
modified driver-core/src/main/java/com/datastax/driver/core/Codec.java +48 −8
| @@ -15,13 +15,11 @@ | ||
| 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 | - | |
| 24 | 19 | import org.apache.cassandra.db.marshal.*; |
| 20 | +import org.apache.cassandra.utils.Pair; | |
| 21 | + | |
| 22 | +import java.util.Map; | |
| 25 | 23 | |
| 26 | 24 | /** |
| 27 | 25 | * Static method to code/decode serialized data given their types. |
| @@ -47,6 +45,47 @@ | ||
| 47 | 45 | .put(TimeUUIDType.instance, DataType.timeuuid()) |
| 48 | 46 | .build(); |
| 49 | 47 | |
| 48 | + private static Map<DataType, SetType<?>> SETS; | |
| 49 | + private static Map<DataType, ListType<?>> LISTS; | |
| 50 | + private static ImmutableMap<Pair<DataType, DataType>, MapType<?, ?>> MAPS; | |
| 51 | + | |
| 52 | + | |
| 53 | + static { | |
| 54 | + SETS = buildSets(); | |
| 55 | + LISTS = buildLists(); | |
| 56 | + MAPS = buildMaps(); | |
| 57 | + } | |
| 58 | + | |
| 59 | + protected static ImmutableMap<Pair<DataType, DataType>, MapType<?, ?>> buildMaps() { | |
| 60 | + ImmutableMap.Builder<Pair<DataType,DataType>, MapType<?,?>> mapsBuilder = ImmutableMap.builder(); | |
| 61 | + for (DataType typeArg1 : DataType.allPrimitiveTypes()) { | |
| 62 | + for (DataType typeArg2 : DataType.allPrimitiveTypes()) { | |
| 63 | + mapsBuilder.put(Pair.create(typeArg1,typeArg2), MapType.getInstance( | |
| 64 | + getCodec(typeArg1), getCodec(typeArg2) | |
| 65 | + )); | |
| 66 | + } | |
| 67 | + } | |
| 68 | + return mapsBuilder.build(); | |
| 69 | + } | |
| 70 | + | |
| 71 | + protected static ImmutableMap<DataType, ListType<?>> buildLists() { | |
| 72 | + ImmutableMap.Builder<DataType, ListType<?>> listsBuilder = ImmutableMap.builder(); | |
| 73 | + for (DataType typeArg : DataType.allPrimitiveTypes()) { | |
| 74 | + listsBuilder.put(typeArg, ListType.getInstance(getCodec(typeArg))); | |
| 75 | + } | |
| 76 | + return listsBuilder.build(); | |
| 77 | + } | |
| 78 | + | |
| 79 | + protected static ImmutableMap<DataType, SetType<?>> buildSets() { | |
| 80 | + ImmutableMap.Builder<DataType, SetType<?>> setsBuilder = ImmutableMap.builder(); | |
| 81 | + for (DataType typeArg : DataType.allPrimitiveTypes()) { | |
| 82 | + final AbstractType<Object> codec = getCodec(typeArg); | |
| 83 | + setsBuilder.put(typeArg, SetType.getInstance(codec)); | |
| 84 | + } | |
| 85 | + return setsBuilder.build(); | |
| 86 | + } | |
| 87 | + | |
| 88 | + | |
| 50 | 89 | private Codec() {} |
| 51 | 90 | |
| 52 | 91 | @SuppressWarnings("unchecked") |
| @@ -72,9 +111,10 @@ | ||
| 72 | 111 | case VARCHAR: return UTF8Type.instance; |
| 73 | 112 | case VARINT: return IntegerType.instance; |
| 74 | 113 | 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))); | |
| 114 | + case LIST: return LISTS.get(type.getTypeArguments().get(0)); | |
| 115 | + case SET: return SETS.get(type.getTypeArguments().get(0)); | |
| 116 | + case MAP: return MAPS.get(Pair.create(type.getTypeArguments().get(0), | |
| 117 | + type.getTypeArguments().get(1))); | |
| 78 | 118 | // We don't interpret custom values in any way |
| 79 | 119 | case CUSTOM: return BytesType.instance; |
| 80 | 120 | default: throw new RuntimeException("Unknown type"); |
added driver-core/src/test/java/com/datastax/driver/core/CodecTest.java +102 −0
| @@ -0,0 +1,102 @@ | ||
| 1 | +package com.datastax.driver.core; | |
| 2 | + | |
| 3 | +import org.apache.cassandra.db.marshal.AbstractType; | |
| 4 | +import org.apache.cassandra.db.marshal.ListType; | |
| 5 | +import org.apache.cassandra.db.marshal.MapType; | |
| 6 | +import org.apache.cassandra.db.marshal.SetType; | |
| 7 | +import org.apache.cassandra.utils.Pair; | |
| 8 | +import org.testng.Assert; | |
| 9 | +import org.testng.annotations.Test; | |
| 10 | + | |
| 11 | +import java.util.*; | |
| 12 | + | |
| 13 | +import static com.datastax.driver.core.DataType.*; | |
| 14 | + | |
| 15 | +/** | |
| 16 | + * User: Olve S. Hansen mailto:olve@vimond.com | |
| 17 | + * Date: 06/12/13 | |
| 18 | + * Time: 23:37 | |
| 19 | + */ | |
| 20 | +public class CodecTest { | |
| 21 | + | |
| 22 | + @Test(groups = "unit") | |
| 23 | + public void testSetTypeCodec() throws Exception { | |
| 24 | + Map<DataType, SetType<?>> dateTypeSetTypeMap = new HashMap<DataType, SetType<?>>(Codec.buildSets()); | |
| 25 | + | |
| 26 | + final Set<DataType> dataTypes = new HashSet<DataType>(allPrimitiveTypes()); | |
| 27 | + | |
| 28 | + //Special case: DataType varchar yields Codec.rawTypeToDataType(...) => text causing assertion fail | |
| 29 | + //Solution: Remove DataType.varchar and check that it yields DataType.text | |
| 30 | + Assert.assertTrue(dataTypes.remove(varchar())); | |
| 31 | + Assert.assertEquals(Codec.rawTypeToDataType(dateTypeSetTypeMap.get(varchar()).nameComparator()), text()) ; | |
| 32 | + dateTypeSetTypeMap.remove(varchar()); | |
| 33 | + | |
| 34 | + for (DataType dataType : dataTypes) { | |
| 35 | + final SetType<?> setType = dateTypeSetTypeMap.remove(dataType); | |
| 36 | + Assert.assertEquals(Codec.rawTypeToDataType(setType.nameComparator()),dataType) ; | |
| 37 | + } | |
| 38 | + Assert.assertTrue(dateTypeSetTypeMap.isEmpty(), "All map types should have been tested: "+dateTypeSetTypeMap); | |
| 39 | + | |
| 40 | + } | |
| 41 | + | |
| 42 | + @Test(groups = "unit") | |
| 43 | + public void testListTypeCodec() throws Exception { | |
| 44 | + Map<DataType,ListType<?>> dateTypeListetTypeMap = new HashMap<DataType, ListType<?>>(Codec.buildLists()); | |
| 45 | + | |
| 46 | + final Set<DataType> dataTypes = new HashSet<DataType>(allPrimitiveTypes()); | |
| 47 | + | |
| 48 | + //Special case: DataType varchar yields Codec.rawTypeToDataType(...) => text causing assertion fail | |
| 49 | + //Solution: Remove DataType.varchar and check that it yields DataType.text | |
| 50 | + Assert.assertTrue(dataTypes.remove(varchar())); | |
| 51 | + final AbstractType<?> rawType = dateTypeListetTypeMap.get(varchar()).valueComparator(); | |
| 52 | + Assert.assertEquals(Codec.rawTypeToDataType(rawType), text()) ; | |
| 53 | + dateTypeListetTypeMap.remove(varchar()); | |
| 54 | + | |
| 55 | + | |
| 56 | + for (DataType dataType : dataTypes) { | |
| 57 | + final ListType<?> setType = dateTypeListetTypeMap.remove(dataType); | |
| 58 | + Assert.assertEquals(Codec.rawTypeToDataType(setType.valueComparator()),dataType) ; | |
| 59 | + } | |
| 60 | + Assert.assertTrue(dateTypeListetTypeMap.isEmpty(), "All set types should have been tested: "+dateTypeListetTypeMap); | |
| 61 | + | |
| 62 | + } | |
| 63 | + | |
| 64 | + @Test(groups = "unit") | |
| 65 | + public void testMapTypeCodec() throws Exception { | |
| 66 | + Map<Pair<DataType,DataType>,MapType<?,?>> dateTypeSetTypeMap = | |
| 67 | + new HashMap<Pair<DataType, DataType>, MapType<?, ?>>(Codec.buildMaps()); | |
| 68 | + | |
| 69 | + for (DataType dataTypeArg1 : allPrimitiveTypes()) { | |
| 70 | + for (DataType dataTypeArg2 : allPrimitiveTypes()) { | |
| 71 | + final Pair<DataType, DataType> pair = Pair.create(dataTypeArg1, dataTypeArg2); | |
| 72 | + final MapType<?, ?> mapType = dateTypeSetTypeMap.remove(pair); | |
| 73 | + | |
| 74 | + testPairAgainstMapType(pair, mapType); | |
| 75 | + } | |
| 76 | + } | |
| 77 | + Assert.assertTrue(dateTypeSetTypeMap.isEmpty(), "All map types should have been tested: "+dateTypeSetTypeMap); | |
| 78 | + } | |
| 79 | + | |
| 80 | + | |
| 81 | + private void testPairAgainstMapType(Pair<DataType, DataType> pair, MapType<?, ?> mapType) { | |
| 82 | + System.out.println("testing pair: "+pair); | |
| 83 | + DataType dataType = pair.left; | |
| 84 | + DataType dataType1 = pair.right; | |
| 85 | + | |
| 86 | + //Special case: DataType varchar yields Codec.rawTypeToDataType(...) => text causing assertion fail | |
| 87 | + //Solution: Remove DataType.varchar and check that it yields DataType.text | |
| 88 | + | |
| 89 | + //In the map case this would be quite many combinations of text/varchar so I translate varchar to text | |
| 90 | + //in order to expect text instead of varchar back from those cases: | |
| 91 | + if(dataType.equals(varchar())) dataType = text(); | |
| 92 | + if(dataType1.equals(varchar())) dataType1 = text(); | |
| 93 | + | |
| 94 | + Assert.assertEquals(Codec.rawTypeToDataType(mapType.nameComparator()), dataType) ; | |
| 95 | + Assert.assertEquals(Codec.rawTypeToDataType(mapType.valueComparator()),dataType1) ; | |
| 96 | + } | |
| 97 | + | |
| 98 | + | |
| 99 | + public void testRawTypeToDataType() throws Exception { | |
| 100 | + | |
| 101 | + } | |
| 102 | +} |