kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | f43cc0a579a9e5b3ebebcf2eac2dc7dd3513375cb0c7dfe5fbfcae1260dc7787 |
|---|---|
| Author | Sylvain Lebresne <sylvain@datastax.com> · 2013-12-10 14:38 +01:00 |
| Committer | Sylvain Lebresne <sylvain@datastax.com> · 2013-12-10 14:38 +01:00 |
| Parents | 74f9324b5c47 |
3 files changed
modified driver-core/CHANGELOG.rst +2 −0
| @@ -9,6 +9,8 @@ | ||
| 9 | 9 | - [improvement] Limit internal thread pool size (JAVA-203) |
| 10 | 10 | - [improvement] New getter to obtain a snapshot of the scores maintained by |
| 11 | 11 | LatencyAwarePolicy. |
| 12 | +- [improvement] Avoid synchronization when getting codec for collection | |
| 13 | + types (JAVA-222) | |
| 12 | 14 | - [bug] Don't retain unused PreparedStatement in memory (JAVA-201, JAVA-213) |
| 13 | 15 | - [bug] Add missing clustering order info in TableMetadata |
| 14 | 16 | - [bug] Allow bind markers for collections in the query builder (JAVA-196) |
modified driver-core/src/main/java/com/datastax/driver/core/Codec.java +10 −16
| @@ -15,12 +15,12 @@ | ||
| 15 | 15 | */ |
| 16 | 16 | package com.datastax.driver.core; |
| 17 | 17 | |
| 18 | +import java.util.Map; | |
| 19 | + | |
| 18 | 20 | import com.google.common.collect.ImmutableMap; |
| 19 | 21 | import com.google.common.collect.Maps; |
| 20 | 22 | import org.apache.cassandra.db.marshal.*; |
| 21 | 23 | |
| 22 | -import java.util.Map; | |
| 23 | - | |
| 24 | 24 | /** |
| 25 | 25 | * Static method to code/decode serialized data given their types. |
| 26 | 26 | */ |
| @@ -49,7 +49,6 @@ | ||
| 49 | 49 | private static Map<DataType.Name, ListType<?>> LISTS = buildLists(); |
| 50 | 50 | private static Map<DataType.Name, Map<DataType.Name, MapType<?, ?>>> MAPS = buildMaps(); |
| 51 | 51 | |
| 52 | - | |
| 53 | 52 | protected static Map<DataType.Name, Map<DataType.Name, MapType<?, ?>>> buildMaps() { |
| 54 | 53 | |
| 55 | 54 | ImmutableMap.Builder<DataType.Name, Map<DataType.Name, MapType<?, ?>>> mapsBuilder = ImmutableMap.builder(); |
| @@ -84,7 +83,6 @@ | ||
| 84 | 83 | return Maps.immutableEnumMap(setsBuilder.build()); |
| 85 | 84 | } |
| 86 | 85 | |
| 87 | - | |
| 88 | 86 | private Codec() {} |
| 89 | 87 | |
| 90 | 88 | @SuppressWarnings("unchecked") |
| @@ -123,28 +121,24 @@ | ||
| 123 | 121 | final DataType dataTypeArg0 = type.getTypeArguments().get(0); |
| 124 | 122 | final DataType dateTypeArg1 = type.getTypeArguments().get(1); |
| 125 | 123 | |
| 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()); | |
| 124 | + Map<DataType.Name, MapType<?, ?>> tmp = MAPS.get(dataTypeArg0.getName()); | |
| 125 | + MapType<?, ?> map = tmp == null ? null : tmp.get(dateTypeArg1.getName()); | |
| 126 | + | |
| 127 | + return map == null ? MapType.getInstance(getCodecInternal(dataTypeArg0), getCodecInternal(dateTypeArg1)) : map; | |
| 130 | 128 | } |
| 131 | 129 | |
| 132 | 130 | private static SetType<?> getSetType(DataType type) { |
| 133 | 131 | final DataType dataTypeArg0 = type.getTypeArguments().get(0); |
| 134 | 132 | |
| 135 | - if(dataTypeArg0.getName() == DataType.Name.CUSTOM) { | |
| 136 | - return SetType.getInstance(getCodecInternal(dataTypeArg0)); | |
| 137 | - } | |
| 138 | - return SETS.get(type.getTypeArguments().get(0).getName()); | |
| 133 | + SetType<?> set = SETS.get(dataTypeArg0.getName()); | |
| 134 | + return set == null ? SetType.getInstance(getCodecInternal(dataTypeArg0)) : set; | |
| 139 | 135 | } |
| 140 | 136 | |
| 141 | 137 | private static ListType<?> getListType(final DataType type) { |
| 142 | 138 | final DataType dataTypeArg0 = type.getTypeArguments().get(0); |
| 143 | 139 | |
| 144 | - if(dataTypeArg0.getName() == DataType.Name.CUSTOM) { | |
| 145 | - return ListType.getInstance(getCodecInternal(dataTypeArg0)); | |
| 146 | - } | |
| 147 | - return LISTS.get(type.getTypeArguments().get(0).getName()); | |
| 140 | + ListType<?> list = LISTS.get(dataTypeArg0.getName()); | |
| 141 | + return list == null ? ListType.getInstance(getCodecInternal(dataTypeArg0)) : list; | |
| 148 | 142 | } |
| 149 | 143 | |
| 150 | 144 | public static DataType rawTypeToDataType(AbstractType<?> rawType) { |
modified driver-core/src/test/java/com/datastax/driver/core/CodecTest.java +15 −28
| @@ -16,7 +16,6 @@ | ||
| 16 | 16 | package com.datastax.driver.core; |
| 17 | 17 | |
| 18 | 18 | import org.apache.cassandra.db.marshal.*; |
| 19 | -import org.apache.cassandra.utils.Pair; | |
| 20 | 19 | import org.testng.Assert; |
| 21 | 20 | import org.testng.annotations.Test; |
| 22 | 21 | import org.testng.collections.Maps; |
| @@ -31,47 +30,42 @@ | ||
| 31 | 30 | |
| 32 | 31 | @Test(groups = "unit") |
| 33 | 32 | public void testCustomList() throws Exception { |
| 34 | - ListType listType = (ListType) Codec.getCodec(DataType.list(CUSTOM_FOO)); | |
| 33 | + ListType<?> listType = (ListType) Codec.getCodec(DataType.list(CUSTOM_FOO)); | |
| 35 | 34 | Assert.assertNotNull(listType); |
| 36 | 35 | Assert.assertTrue(listType.valueComparator() instanceof BytesType); |
| 37 | - | |
| 38 | 36 | } |
| 39 | 37 | |
| 40 | - | |
| 41 | 38 | @Test(groups = "unit") |
| 42 | 39 | public void testCustomSet() throws Exception { |
| 43 | - SetType setType = (SetType) Codec.getCodec(DataType.set(CUSTOM_FOO)); | |
| 40 | + SetType<?> setType = (SetType) Codec.getCodec(DataType.set(CUSTOM_FOO)); | |
| 44 | 41 | Assert.assertNotNull(setType); |
| 45 | 42 | Assert.assertEquals(setType.nameComparator().getClass(),BytesType.class); |
| 46 | 43 | } |
| 47 | 44 | |
| 48 | - | |
| 49 | 45 | @Test(groups = "unit") |
| 50 | 46 | public void testCustomKeyMap() throws Exception { |
| 51 | - MapType mapType = (MapType) Codec.getCodec(DataType.map(CUSTOM_FOO, text())); | |
| 47 | + MapType<?, ?> mapType = (MapType) Codec.getCodec(DataType.map(CUSTOM_FOO, text())); | |
| 52 | 48 | Assert.assertNotNull(mapType); |
| 53 | 49 | Assert.assertEquals(mapType.nameComparator().getClass(), BytesType.class); |
| 54 | 50 | Assert.assertEquals(mapType.valueComparator().getClass(), UTF8Type.class); |
| 55 | 51 | } |
| 56 | 52 | |
| 57 | - | |
| 58 | 53 | @Test(groups = "unit") |
| 59 | 54 | public void testCustomValueMap() throws Exception { |
| 60 | - MapType mapType = (MapType) Codec.getCodec(DataType.map(text(), CUSTOM_FOO)); | |
| 55 | + MapType<?, ?> mapType = (MapType) Codec.getCodec(DataType.map(text(), CUSTOM_FOO)); | |
| 61 | 56 | Assert.assertNotNull(mapType); |
| 62 | 57 | Assert.assertEquals(mapType.nameComparator().getClass(), UTF8Type.class); |
| 63 | 58 | Assert.assertEquals(mapType.valueComparator().getClass(), BytesType.class); |
| 64 | 59 | } |
| 65 | 60 | |
| 66 | - | |
| 67 | 61 | @Test(groups = "unit") |
| 68 | 62 | public void testSetTypeCodec() throws Exception { |
| 69 | 63 | Map<Name, SetType<?>> dateTypeSetTypeMap = new HashMap<Name, SetType<?>>(Codec.buildSets()); |
| 70 | 64 | |
| 71 | 65 | final Set<DataType> dataTypes = new HashSet<DataType>(allPrimitiveTypes()); |
| 72 | 66 | |
| 73 | - //Special case: DataType varchar yields Codec.rawTypeToDataType(...) => text causing assertion fail | |
| 74 | - //Solution: Remove DataType.varchar and check that it yields DataType.text | |
| 67 | + // Special case: DataType varchar yields Codec.rawTypeToDataType(...) => text causing assertion fail | |
| 68 | + // Solution: Remove DataType.varchar and check that it yields DataType.text | |
| 75 | 69 | Assert.assertTrue(dataTypes.remove(varchar())); |
| 76 | 70 | Assert.assertEquals(Codec.rawTypeToDataType(dateTypeSetTypeMap.get(varchar().getName()).nameComparator()), text()) ; |
| 77 | 71 | dateTypeSetTypeMap.remove(varchar().getName()); |
| @@ -89,20 +83,18 @@ | ||
| 89 | 83 | |
| 90 | 84 | final Set<DataType> dataTypes = new HashSet<DataType>(allPrimitiveTypes()); |
| 91 | 85 | |
| 92 | - //Special case: DataType varchar yields Codec.rawTypeToDataType(...) => text causing assertion fail | |
| 93 | - //Solution: Remove DataType.varchar and check that it yields DataType.text | |
| 86 | + // Special case: DataType varchar yields Codec.rawTypeToDataType(...) => text causing assertion fail | |
| 87 | + // Solution: Remove DataType.varchar and check that it yields DataType.text | |
| 94 | 88 | Assert.assertTrue(dataTypes.remove(varchar())); |
| 95 | 89 | final AbstractType<?> rawType = dateTypeListetTypeMap.get(varchar().getName()).valueComparator(); |
| 96 | 90 | Assert.assertEquals(Codec.rawTypeToDataType(rawType), text()) ; |
| 97 | 91 | dateTypeListetTypeMap.remove(varchar().getName()); |
| 98 | 92 | |
| 99 | - | |
| 100 | 93 | for (DataType dataType : dataTypes) { |
| 101 | 94 | final ListType<?> setType = dateTypeListetTypeMap.remove(dataType.getName()); |
| 102 | 95 | Assert.assertEquals(Codec.rawTypeToDataType(setType.valueComparator()),dataType) ; |
| 103 | 96 | } |
| 104 | 97 | Assert.assertTrue(dateTypeListetTypeMap.isEmpty(), "All set types should have been tested: "+dateTypeListetTypeMap); |
| 105 | - | |
| 106 | 98 | } |
| 107 | 99 | |
| 108 | 100 | @Test(groups = "unit") |
| @@ -123,19 +115,14 @@ | ||
| 123 | 115 | Assert.assertTrue(dateTypeMapTypeMap.isEmpty(), "All map types should have been tested: "+dateTypeMapTypeMap); |
| 124 | 116 | } |
| 125 | 117 | |
| 126 | - private Pair<Name, Name> convertToNamePair(Pair<DataType, DataType> pair) { | |
| 127 | - return Pair.create(pair.left.getName(), pair.right.getName()); | |
| 128 | - } | |
| 129 | - | |
| 130 | - | |
| 131 | 118 | 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(); | |
| 119 | + // Special case: DataType varchar yields Codec.rawTypeToDataType(...) => text causing assertion fail | |
| 120 | + // Solution: Remove DataType.varchar and check that it yields DataType.text | |
| 121 | + | |
| 122 | + // In the map case this would be quite many combinations of text/varchar so I translate varchar to text | |
| 123 | + // in order to expect text instead of varchar back from those cases: | |
| 124 | + if (dataTypeArg0.equals(varchar())) dataTypeArg0 = text(); | |
| 125 | + if (dataTypeArg1.equals(varchar())) dataTypeArg1 = text(); | |
| 139 | 126 | |
| 140 | 127 | Assert.assertEquals(Codec.rawTypeToDataType(mapType.nameComparator()), dataTypeArg0) ; |
| 141 | 128 | Assert.assertEquals(Codec.rawTypeToDataType(mapType.valueComparator()),dataTypeArg1) ; |