private static String buildMapValue(Row row, int i, CassandraType keyType, CassandraType valueType) { StringBuilder sb = new StringBuilder(); sb.append("{"); for (Map.Entry<?, ?> entry : row.getMap(i, keyType.javaType, valueType.javaType).entrySet()) { if (sb.length() > 1) { sb.append(","); } sb.append(objectToString(entry.getKey(), keyType)); sb.append(":"); sb.append(objectToString(entry.getValue(), valueType)); } sb.append("}"); return sb.toString(); }
record.getString(1), new TreeMap<String, String>(record.getMap(2, String.class, String.class)));
@Override public Session get(final Builder builder) { ResultSet rs = session .execute(new BoundStatement(selectSQL.apply(tableName)).bind(builder.sessionId())); return Optional.ofNullable(rs.one()) .map(row -> { long createdAt = row.getTimestamp(CREATED_AT).getTime(); long accessedAt = row.getTimestamp(ACCESSED_AT).getTime(); long savedAt = row.getTimestamp(SAVED_AT).getTime(); Map<String, String> attributes = row.getMap(ATTRIBUTES, String.class, String.class); Session session = builder .accessedAt(accessedAt) .createdAt(createdAt) .savedAt(savedAt) .set(attributes) .build(); // touch ttl if (timeout > 0) { save(session); } return session; }) .orElse(null); }
TypeCodec secondCodec = codecRegistry.codecFor(secondArg); if (dataType.equals(DataType.map(firstArg, secondArg))) { return row.getMap(i, firstCodec.getJavaType(), secondCodec.getJavaType());
/** Build an IndexMetadata from a system_schema.indexes row. */ static IndexMetadata fromRow(TableMetadata table, Row indexRow) { String name = indexRow.getString(NAME); Kind kind = Kind.valueOf(indexRow.getString(KIND)); Map<String, String> options = indexRow.getMap(OPTIONS, String.class, String.class); String target = options.get(TARGET_OPTION_NAME); return new IndexMetadata(table, name, kind, target, options); }
static KeyspaceMetadata build(Row row, VersionNumber cassandraVersion) { if (cassandraVersion.getMajor() <= 2) { String name = row.getString(KS_NAME); boolean durableWrites = row.getBool(DURABLE_WRITES); Map<String, String> replicationOptions; replicationOptions = new HashMap<String, String>(); replicationOptions.put("class", row.getString(STRATEGY_CLASS)); replicationOptions.putAll(SimpleJSONParser.parseStringMap(row.getString(STRATEGY_OPTIONS))); return new KeyspaceMetadata(name, durableWrites, replicationOptions, false); } else { String name = row.getString(KS_NAME); boolean durableWrites = row.getBool(DURABLE_WRITES); return new KeyspaceMetadata( name, durableWrites, row.getMap(REPLICATION, String.class, String.class), false); } }
retVal = row.getMap(columnName, mapGenericClasses.get(0).isAssignableFrom(byte[].class) ? ByteBuffer.class : mapGenericClasses.get(0), mapValueClazz);
@Override public <K, V> Map<K, V> getMap(String name, Class<K> keysClass, Class<V> valuesClass) { return row.getMap(name, keysClass, valuesClass); }
parameters = Collections.unmodifiableMap( sessRow.getMap("parameters", String.class, String.class)); startedAt = sessRow.getTimestamp("started_at").getTime();
@Override public <K, V> Map<K, V> getMap(int i, Class<K> keysClass, Class<V> valuesClass) { return row.getMap(i, keysClass, valuesClass); }
@Override public <K, V> Map<K, V> getMap(int i, TypeToken<K> typeToken, TypeToken<V> typeToken1) { return row.getMap(i, typeToken, typeToken1); }
private void assertRow(Row row) { assertThat(row.getInt(0)).isEqualTo(n_int); assertThat(row.getList(1, Integer.class)).isEqualTo(l_int); assertThat(row.getList(2, Long.class)).isEqualTo(l_bigint); assertThat(row.getSet(3, Float.class)).isEqualTo(s_float); assertThat(row.getSet(4, Double.class)).isEqualTo(s_double); assertThat(row.getMap(5, Integer.class, BigInteger.class)).isEqualTo(m_varint); assertThat(row.getMap(6, Integer.class, BigDecimal.class)).isEqualTo(m_decimal); // with get + type assertThat(row.get(1, TypeTokens.listOf(Integer.class))).isEqualTo(l_int); assertThat(row.get(2, TypeTokens.listOf(Long.class))).isEqualTo(l_bigint); assertThat(row.get(3, TypeTokens.setOf(Float.class))).isEqualTo(s_float); assertThat(row.get(4, TypeTokens.setOf(Double.class))).isEqualTo(s_double); assertThat(row.get(5, TypeTokens.mapOf(Integer.class, BigInteger.class))).isEqualTo(m_varint); assertThat(row.get(6, TypeTokens.mapOf(Integer.class, BigDecimal.class))).isEqualTo(m_decimal); // with getObject assertThat(row.getObject(1)).isEqualTo(l_int); assertThat(row.getObject(2)).isEqualTo(l_bigint); assertThat(row.getObject(3)).isEqualTo(s_float); assertThat(row.getObject(4)).isEqualTo(s_double); assertThat(row.getObject(5)).isEqualTo(m_varint); assertThat(row.getObject(6)).isEqualTo(m_decimal); } }
this.caching = ImmutableMap.copyOf(row.getMap(CACHING, String.class, String.class)); } else if (is210) { this.caching = ImmutableMap.copyOf(SimpleJSONParser.parseStringMap(row.getString(CACHING))); this.compaction = ImmutableMap.copyOf(row.getMap(COMPACTION, String.class, String.class)); else { this.compaction = this.compression = ImmutableMap.copyOf(row.getMap(COMPRESSION, String.class, String.class)); else this.compression = this.extensions = ImmutableMap.copyOf(row.getMap(EXTENSIONS, String.class, ByteBuffer.class)); else this.extensions = ImmutableMap.of();
@Test(groups = "short") public void should_handle_collections_of_UDT() throws Exception { UserType udtType = cluster().getMetadata().getKeyspace(keyspace).getUserType("udt"); UDTValue udtValue = udtType.newValue().setInt("i", 2).setInet("a", InetAddress.getByName("localhost")); UDTValue udtValue2 = udtType.newValue().setInt("i", 3).setInet("a", InetAddress.getByName("localhost")); Statement insert = insertInto("udtTest").value("k", 1).value("l", ImmutableList.of(udtValue)); assertThat(insert.toString()) .isEqualTo("INSERT INTO udtTest (k,l) VALUES (1,[{i:2,a:'127.0.0.1'}]);"); session().execute(insert); List<Row> rows = session().execute(select().from("udtTest").where(eq("k", 1))).all(); assertThat(rows.size()).isEqualTo(1); Row r1 = rows.get(0); assertThat(r1.getList("l", UDTValue.class).get(0).getInet("a").getHostAddress()) .isEqualTo("127.0.0.1"); Map<Integer, UDTValue> map = Maps.newHashMap(); map.put(0, udtValue); map.put(2, udtValue2); Statement updateMap = update("udtTest").with(putAll("m", map)).where(eq("k", 1)); assertThat(updateMap.toString()) .isEqualTo( "UPDATE udtTest SET m=m+{0:{i:2,a:'127.0.0.1'},2:{i:3,a:'127.0.0.1'}} WHERE k=1;"); session().execute(updateMap); rows = session().execute(select().from("udtTest").where(eq("k", 1))).all(); r1 = rows.get(0); assertThat(r1.getMap("m", Integer.class, UDTValue.class)).isEqualTo(map); }
@Test(groups = "short") public void should_delete_map_entry() throws Exception { // given session().execute("INSERT INTO test_coll (k, b) VALUES (1, {1:'foo', 2:'bar'})"); // when BuiltStatement statement = delete().mapElt("b", 1).from("test_coll").where(eq("k", 1)); session().execute(statement); // then Map<Integer, String> actual = session() .execute("SELECT b FROM test_coll WHERE k = 1") .one() .getMap("b", Integer.class, String.class); assertThat(actual).containsExactly(entry(2, "bar")); }
@Test(groups = "short") public void should_handle_contains_on_map_with_index() { PreparedStatement byFeatures = session() .prepare( select("id", "description", "features_values") .from("products") .where(contains("features_values", bindMarker("feature")))); ResultSet results = session().execute(byFeatures.bind().setString("feature", "LED")); Row row = results.one(); assertThat(row).isNotNull(); assertThat(row.getInt("id")).isEqualTo(29412); assertThat(row.getMap("features_values", String.class, String.class)) .containsEntry("techno", "LED"); }
@Test(groups = "short") public void should_handle_contains_key_on_map_with_index() { PreparedStatement byFeatures = session() .prepare( select("id", "description", "features_keys") .from("products") .where(containsKey("features_keys", bindMarker("feature")))); ResultSet results = session().execute(byFeatures.bind().setString("feature", "refresh-rate")); Row row = results.one(); assertThat(row).isNotNull(); assertThat(row.getInt("id")).isEqualTo(34134); assertThat(row.getMap("features_keys", String.class, String.class)) .containsEntry("refresh-rate", "400hz"); } }
@Test(groups = "short") public void should_delete_map_entry_with_bind_marker() throws Exception { // given session().execute("INSERT INTO test_coll (k, a, b) VALUES (1, null, {1:'foo', 2:'bar'})"); // when BuiltStatement statement = delete().mapElt("b", bindMarker()).from("test_coll").where(eq("k", 1)); PreparedStatement ps = session().prepare(statement); session().execute(ps.bind().setInt(0, 1)); // then Map<Integer, String> actual = session() .execute("SELECT b FROM test_coll WHERE k = 1") .one() .getMap("b", Integer.class, String.class); assertThat(actual).containsExactly(entry(2, "bar")); }
/** * Validates that columns using collections of custom types are properly handled by the driver. * * @jira_ticket JAVA-1034 * @test_category metadata */ @Test(groups = "short") public void should_serialize_and_deserialize_collections_of_custom_types() { TestUtils.compactStorageSupportCheck(ccm()); TableMetadata table = cluster().getMetadata().getKeyspace(keyspace).getTable("test_collection"); assertThat(table.getColumn("c1")).hasType(DataType.list(CUSTOM_DYNAMIC_COMPOSITE)); assertThat(table.getColumn("c2")) .hasType(DataType.map(CUSTOM_DYNAMIC_COMPOSITE, CUSTOM_DYNAMIC_COMPOSITE)); session() .execute( "INSERT INTO test_collection(k, c1, c2) VALUES (0, [ 's@foo:i@32' ], { 's@foo:i@32': 's@bar:i@42' })"); Row r = session().execute("SELECT * FROM test_collection").one(); assertThat(r.getColumnDefinitions().getType("c1")) .isEqualTo(DataType.list(CUSTOM_DYNAMIC_COMPOSITE)); List<ByteBuffer> c1 = r.getList("c1", ByteBuffer.class); assertThat(c1.get(0)).isEqualTo(serializeForDynamicCompositeType("foo", 32)); assertThat(r.getColumnDefinitions().getType("c2")) .isEqualTo(DataType.map(CUSTOM_DYNAMIC_COMPOSITE, CUSTOM_DYNAMIC_COMPOSITE)); Map<ByteBuffer, ByteBuffer> c2 = r.getMap("c2", ByteBuffer.class, ByteBuffer.class); Map.Entry<ByteBuffer, ByteBuffer> entry = c2.entrySet().iterator().next(); assertThat(entry.getKey()).isEqualTo(serializeForDynamicCompositeType("foo", 32)); assertThat(entry.getValue()).isEqualTo(serializeForDynamicCompositeType("bar", 42)); }
assertThat(getValue(row, "v", mapping.outerType, registry)).isEqualTo(mapping.value); assertThat(row.getList("l", mapping.codec.getJavaType())).isEqualTo(list); assertThat(row.getMap("m", mapping.codec.getJavaType(), mapping.codec.getJavaType())) .isEqualTo(map); assertThat(row.getMap(2, mapping.codec.getJavaType(), mapping.codec.getJavaType())) .isEqualTo(map);