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