/src/java/org/apache/cassandra/cql3/functions/types/AbstractGettableByIndexData.java

https://github.com/beobal/cassandra · Java · 418 lines · 254 code · 45 blank · 119 comment · 20 complexity · 41e45d499ba34fdb1db05fd6a9465d40 MD5 · raw file

  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. package org.apache.cassandra.cql3.functions.types;
  19. import java.math.BigDecimal;
  20. import java.math.BigInteger;
  21. import java.net.InetAddress;
  22. import java.nio.ByteBuffer;
  23. import java.util.*;
  24. import com.google.common.reflect.TypeToken;
  25. import org.apache.cassandra.transport.ProtocolVersion;
  26. import org.apache.cassandra.cql3.functions.types.exceptions.InvalidTypeException;
  27. abstract class AbstractGettableByIndexData implements GettableByIndexData
  28. {
  29. protected final ProtocolVersion protocolVersion;
  30. AbstractGettableByIndexData(ProtocolVersion protocolVersion)
  31. {
  32. this.protocolVersion = protocolVersion;
  33. }
  34. /**
  35. * Returns the type for the value at index {@code i}.
  36. *
  37. * @param i the index of the type to fetch.
  38. * @return the type of the value at index {@code i}.
  39. * @throws IndexOutOfBoundsException if {@code i} is not a valid index.
  40. */
  41. protected abstract DataType getType(int i);
  42. /**
  43. * Returns the name corresponding to the value at index {@code i}.
  44. *
  45. * @param i the index of the name to fetch.
  46. * @return the name corresponding to the value at index {@code i}.
  47. * @throws IndexOutOfBoundsException if {@code i} is not a valid index.
  48. */
  49. protected abstract String getName(int i);
  50. /**
  51. * Returns the value at index {@code i}.
  52. *
  53. * @param i the index to fetch.
  54. * @return the value at index {@code i}.
  55. * @throws IndexOutOfBoundsException if {@code i} is not a valid index.
  56. */
  57. protected abstract ByteBuffer getValue(int i);
  58. protected abstract CodecRegistry getCodecRegistry();
  59. protected <T> TypeCodec<T> codecFor(int i)
  60. {
  61. return getCodecRegistry().codecFor(getType(i));
  62. }
  63. protected <T> TypeCodec<T> codecFor(int i, Class<T> javaClass)
  64. {
  65. return getCodecRegistry().codecFor(getType(i), javaClass);
  66. }
  67. protected <T> TypeCodec<T> codecFor(int i, TypeToken<T> javaType)
  68. {
  69. return getCodecRegistry().codecFor(getType(i), javaType);
  70. }
  71. protected <T> TypeCodec<T> codecFor(int i, T value)
  72. {
  73. return getCodecRegistry().codecFor(getType(i), value);
  74. }
  75. void checkType(int i, DataType.Name actual)
  76. {
  77. DataType.Name expected = getType(i).getName();
  78. if (!actual.isCompatibleWith(expected))
  79. throw new InvalidTypeException(
  80. String.format("Value %s is of type %s, not %s", getName(i), expected, actual));
  81. }
  82. /**
  83. * {@inheritDoc}
  84. */
  85. @Override
  86. public boolean isNull(int i)
  87. {
  88. return getValue(i) == null;
  89. }
  90. /**
  91. * {@inheritDoc}
  92. */
  93. @Override
  94. public boolean getBool(int i)
  95. {
  96. ByteBuffer value = getValue(i);
  97. TypeCodec<Boolean> codec = codecFor(i, Boolean.class);
  98. if (codec instanceof TypeCodec.PrimitiveBooleanCodec)
  99. return ((TypeCodec.PrimitiveBooleanCodec) codec).deserializeNoBoxing(value, protocolVersion);
  100. else return codec.deserialize(value, protocolVersion);
  101. }
  102. /**
  103. * {@inheritDoc}
  104. */
  105. @Override
  106. public byte getByte(int i)
  107. {
  108. ByteBuffer value = getValue(i);
  109. TypeCodec<Byte> codec = codecFor(i, Byte.class);
  110. if (codec instanceof TypeCodec.PrimitiveByteCodec)
  111. return ((TypeCodec.PrimitiveByteCodec) codec).deserializeNoBoxing(value, protocolVersion);
  112. else return codec.deserialize(value, protocolVersion);
  113. }
  114. /**
  115. * {@inheritDoc}
  116. */
  117. @Override
  118. public short getShort(int i)
  119. {
  120. ByteBuffer value = getValue(i);
  121. TypeCodec<Short> codec = codecFor(i, Short.class);
  122. if (codec instanceof TypeCodec.PrimitiveShortCodec)
  123. return ((TypeCodec.PrimitiveShortCodec) codec).deserializeNoBoxing(value, protocolVersion);
  124. else return codec.deserialize(value, protocolVersion);
  125. }
  126. /**
  127. * {@inheritDoc}
  128. */
  129. @Override
  130. public int getInt(int i)
  131. {
  132. ByteBuffer value = getValue(i);
  133. TypeCodec<Integer> codec = codecFor(i, Integer.class);
  134. if (codec instanceof TypeCodec.PrimitiveIntCodec)
  135. return ((TypeCodec.PrimitiveIntCodec) codec).deserializeNoBoxing(value, protocolVersion);
  136. else return codec.deserialize(value, protocolVersion);
  137. }
  138. /**
  139. * {@inheritDoc}
  140. */
  141. @Override
  142. public long getLong(int i)
  143. {
  144. ByteBuffer value = getValue(i);
  145. TypeCodec<Long> codec = codecFor(i, Long.class);
  146. if (codec instanceof TypeCodec.PrimitiveLongCodec)
  147. return ((TypeCodec.PrimitiveLongCodec) codec).deserializeNoBoxing(value, protocolVersion);
  148. else return codec.deserialize(value, protocolVersion);
  149. }
  150. /**
  151. * {@inheritDoc}
  152. */
  153. @Override
  154. public Date getTimestamp(int i)
  155. {
  156. ByteBuffer value = getValue(i);
  157. return codecFor(i, Date.class).deserialize(value, protocolVersion);
  158. }
  159. /**
  160. * {@inheritDoc}
  161. */
  162. @Override
  163. public LocalDate getDate(int i)
  164. {
  165. ByteBuffer value = getValue(i);
  166. return codecFor(i, LocalDate.class).deserialize(value, protocolVersion);
  167. }
  168. /**
  169. * {@inheritDoc}
  170. */
  171. @Override
  172. public long getTime(int i)
  173. {
  174. ByteBuffer value = getValue(i);
  175. TypeCodec<Long> codec = codecFor(i, Long.class);
  176. if (codec instanceof TypeCodec.PrimitiveLongCodec)
  177. return ((TypeCodec.PrimitiveLongCodec) codec).deserializeNoBoxing(value, protocolVersion);
  178. else return codec.deserialize(value, protocolVersion);
  179. }
  180. /**
  181. * {@inheritDoc}
  182. */
  183. @Override
  184. public float getFloat(int i)
  185. {
  186. ByteBuffer value = getValue(i);
  187. TypeCodec<Float> codec = codecFor(i, Float.class);
  188. if (codec instanceof TypeCodec.PrimitiveFloatCodec)
  189. return ((TypeCodec.PrimitiveFloatCodec) codec).deserializeNoBoxing(value, protocolVersion);
  190. else return codec.deserialize(value, protocolVersion);
  191. }
  192. /**
  193. * {@inheritDoc}
  194. */
  195. @Override
  196. public double getDouble(int i)
  197. {
  198. ByteBuffer value = getValue(i);
  199. TypeCodec<Double> codec = codecFor(i, Double.class);
  200. if (codec instanceof TypeCodec.PrimitiveDoubleCodec)
  201. return ((TypeCodec.PrimitiveDoubleCodec) codec).deserializeNoBoxing(value, protocolVersion);
  202. else return codec.deserialize(value, protocolVersion);
  203. }
  204. /**
  205. * {@inheritDoc}
  206. */
  207. @Override
  208. public ByteBuffer getBytesUnsafe(int i)
  209. {
  210. ByteBuffer value = getValue(i);
  211. if (value == null) return null;
  212. return value.duplicate();
  213. }
  214. /**
  215. * {@inheritDoc}
  216. */
  217. @Override
  218. public ByteBuffer getBytes(int i)
  219. {
  220. ByteBuffer value = getValue(i);
  221. return codecFor(i, ByteBuffer.class).deserialize(value, protocolVersion);
  222. }
  223. /**
  224. * {@inheritDoc}
  225. */
  226. @Override
  227. public String getString(int i)
  228. {
  229. ByteBuffer value = getValue(i);
  230. return codecFor(i, String.class).deserialize(value, protocolVersion);
  231. }
  232. /**
  233. * {@inheritDoc}
  234. */
  235. @Override
  236. public BigInteger getVarint(int i)
  237. {
  238. ByteBuffer value = getValue(i);
  239. return codecFor(i, BigInteger.class).deserialize(value, protocolVersion);
  240. }
  241. /**
  242. * {@inheritDoc}
  243. */
  244. @Override
  245. public BigDecimal getDecimal(int i)
  246. {
  247. ByteBuffer value = getValue(i);
  248. return codecFor(i, BigDecimal.class).deserialize(value, protocolVersion);
  249. }
  250. /**
  251. * {@inheritDoc}
  252. */
  253. @Override
  254. public UUID getUUID(int i)
  255. {
  256. ByteBuffer value = getValue(i);
  257. return codecFor(i, UUID.class).deserialize(value, protocolVersion);
  258. }
  259. /**
  260. * {@inheritDoc}
  261. */
  262. @Override
  263. public InetAddress getInet(int i)
  264. {
  265. ByteBuffer value = getValue(i);
  266. return codecFor(i, InetAddress.class).deserialize(value, protocolVersion);
  267. }
  268. /**
  269. * {@inheritDoc}
  270. */
  271. @Override
  272. @SuppressWarnings("unchecked")
  273. public <T> List<T> getList(int i, Class<T> elementsClass)
  274. {
  275. return getList(i, TypeToken.of(elementsClass));
  276. }
  277. /**
  278. * {@inheritDoc}
  279. */
  280. @Override
  281. @SuppressWarnings("unchecked")
  282. public <T> List<T> getList(int i, TypeToken<T> elementsType)
  283. {
  284. ByteBuffer value = getValue(i);
  285. TypeToken<List<T>> javaType = TypeTokens.listOf(elementsType);
  286. return codecFor(i, javaType).deserialize(value, protocolVersion);
  287. }
  288. /**
  289. * {@inheritDoc}
  290. */
  291. @Override
  292. @SuppressWarnings("unchecked")
  293. public <T> Set<T> getSet(int i, Class<T> elementsClass)
  294. {
  295. return getSet(i, TypeToken.of(elementsClass));
  296. }
  297. /**
  298. * {@inheritDoc}
  299. */
  300. @Override
  301. @SuppressWarnings("unchecked")
  302. public <T> Set<T> getSet(int i, TypeToken<T> elementsType)
  303. {
  304. ByteBuffer value = getValue(i);
  305. TypeToken<Set<T>> javaType = TypeTokens.setOf(elementsType);
  306. return codecFor(i, javaType).deserialize(value, protocolVersion);
  307. }
  308. /**
  309. * {@inheritDoc}
  310. */
  311. @Override
  312. @SuppressWarnings("unchecked")
  313. public <K, V> Map<K, V> getMap(int i, Class<K> keysClass, Class<V> valuesClass)
  314. {
  315. return getMap(i, TypeToken.of(keysClass), TypeToken.of(valuesClass));
  316. }
  317. /**
  318. * {@inheritDoc}
  319. */
  320. @Override
  321. @SuppressWarnings("unchecked")
  322. public <K, V> Map<K, V> getMap(int i, TypeToken<K> keysType, TypeToken<V> valuesType)
  323. {
  324. ByteBuffer value = getValue(i);
  325. TypeToken<Map<K, V>> javaType = TypeTokens.mapOf(keysType, valuesType);
  326. return codecFor(i, javaType).deserialize(value, protocolVersion);
  327. }
  328. /**
  329. * {@inheritDoc}
  330. */
  331. @Override
  332. @SuppressWarnings("unchecked")
  333. public UDTValue getUDTValue(int i)
  334. {
  335. ByteBuffer value = getValue(i);
  336. return codecFor(i, UDTValue.class).deserialize(value, protocolVersion);
  337. }
  338. /**
  339. * {@inheritDoc}
  340. */
  341. @Override
  342. @SuppressWarnings("unchecked")
  343. public TupleValue getTupleValue(int i)
  344. {
  345. ByteBuffer value = getValue(i);
  346. return codecFor(i, TupleValue.class).deserialize(value, protocolVersion);
  347. }
  348. /**
  349. * {@inheritDoc}
  350. */
  351. @Override
  352. public Object getObject(int i)
  353. {
  354. return get(i, codecFor(i));
  355. }
  356. @Override
  357. public <T> T get(int i, Class<T> targetClass)
  358. {
  359. return get(i, codecFor(i, targetClass));
  360. }
  361. @Override
  362. public <T> T get(int i, TypeToken<T> targetType)
  363. {
  364. return get(i, codecFor(i, targetType));
  365. }
  366. @Override
  367. public <T> T get(int i, TypeCodec<T> codec)
  368. {
  369. checkType(i, codec.getCqlType().getName());
  370. ByteBuffer value = getValue(i);
  371. return codec.deserialize(value, protocolVersion);
  372. }
  373. }