protected UserType getUserType(String userType) { return getSession().getCluster().getMetadata().getKeyspace(KAA).getUserType(userType); }
private void checkUserTypes(Metadata metadata) { KeyspaceMetadata keyspaceMetadata = metadata.getKeyspace(KEYSPACE); UserType a = keyspaceMetadata.getUserType("\"A\""); UserType b = keyspaceMetadata.getUserType("\"B\""); UserType c = keyspaceMetadata.getUserType("\"C\""); UserType d = keyspaceMetadata.getUserType("\"D\""); UserType e = keyspaceMetadata.getUserType("\"E\""); UserType f = keyspaceMetadata.getUserType("\"F\""); UserType g = keyspaceMetadata.getUserType("g"); UserType h = keyspaceMetadata.getUserType("h");
@SuppressWarnings("unchecked") private UDTValue convertValue(Object value, CassandraEntityMapper<?> mapper, CassandraClient cassandraClient) { String keyspace = cassandraClient.getSession().getLoggedKeyspace(); UserType userType = cassandraClient.getSession() .getCluster() .getMetadata() .getKeyspace(keyspace) .getUserType(mapper.getName()); UDTValue udtValue = userType.newValue(); for (String name : mapper.getNonKeyColumnNames()) { Object fieldValue = mapper.getColumnValueForName(name, value, cassandraClient); if (fieldValue != null) { udtValue.set(name, fieldValue, (Class<Object>) fieldValue.getClass()); } else { udtValue.setToNull(name); } } return udtValue; } }
@SuppressWarnings("RedundantCast") @Test(groups = "short", dataProvider = "existingKeyspaceName") @CassandraVersion("2.1.0") public void should_notify_of_udt_drop(String keyspace) { session1.execute(String.format("CREATE TYPE %s.type1(i int)", keyspace)); session1.execute(String.format("DROP TYPE %s.type1", keyspace)); for (SchemaChangeListener listener : listeners) { ArgumentCaptor<UserType> removed = ArgumentCaptor.forClass(UserType.class); verify(listener, timeout(NOTIF_TIMEOUT_MS).times(1)).onUserTypeRemoved(removed.capture()); assertThat((DataType) removed.getValue()).isUserType(handleId(keyspace), "type1"); } for (Metadata m : metadatas()) assertThat((DataType) m.getKeyspace(keyspace).getUserType("type1")).isNull(); }
@SuppressWarnings("RedundantCast") @Test(groups = "short", dataProvider = "existingKeyspaceName") @CassandraVersion("2.1.0") public void should_notify_of_udt_creation(String keyspace) { session1.execute(String.format("CREATE TYPE %s.type1(i int)", keyspace)); for (SchemaChangeListener listener : listeners) { ArgumentCaptor<UserType> added = ArgumentCaptor.forClass(UserType.class); verify(listener, timeout(NOTIF_TIMEOUT_MS).times(1)).onUserTypeAdded(added.capture()); assertThat((DataType) added.getValue()).isUserType(handleId(keyspace), "type1"); } for (Metadata m : metadatas()) assertThat((DataType) m.getKeyspace(keyspace).getUserType("type1")).isNotNull(); }
@Test(groups = "short", dataProvider = "existingKeyspaceName") @CassandraVersion("2.1.0") public void should_notify_of_udt_update(String keyspace) { session1.execute(String.format("CREATE TYPE %s.type1(i int)", keyspace)); session1.execute(String.format("ALTER TYPE %s.type1 ADD j int", keyspace)); for (SchemaChangeListener listener : listeners) { ArgumentCaptor<UserType> current = ArgumentCaptor.forClass(UserType.class); ArgumentCaptor<UserType> previous = ArgumentCaptor.forClass(UserType.class); verify(listener, timeout(NOTIF_TIMEOUT_MS).times(1)) .onUserTypeChanged(current.capture(), previous.capture()); assertThat(previous.getValue().getFieldNames()).doesNotContain("j"); assertThat(current.getValue().getFieldNames()).contains("j"); } for (Metadata m : metadatas()) assertThat(m.getKeyspace(keyspace).getUserType("type1").getFieldType("j")).isNotNull(); }
@Test(groups = "short") public void insertUdtTest() throws Exception { UserType udtType = cluster().getMetadata().getKeyspace(keyspace).getUserType("udt"); UDTValue udtValue = udtType.newValue().setInt("i", 2).setInet("a", InetAddress.getByName("localhost")); Statement insert = insertInto("udtTest").value("k", 1).value("t", udtValue); assertEquals(insert.toString(), "INSERT INTO udtTest (k,t) 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(); assertEquals(rows.size(), 1); Row r1 = rows.get(0); assertEquals("127.0.0.1", r1.getUDTValue("t").getInet("a").getHostAddress()); }
@Test(groups = "short") public void should_handle_UDT_with_many_fields() throws Exception { int MAX_TEST_LENGTH = 1024; // create the seed udt StringBuilder sb = new StringBuilder(); for (int i = 0; i < MAX_TEST_LENGTH; ++i) { sb.append(String.format("v_%s int", i)); if (i + 1 < MAX_TEST_LENGTH) sb.append(","); } session().execute(String.format("CREATE TYPE lengthy_udt (%s)", sb.toString())); // create a table with multiple sizes of udts session().execute("CREATE TABLE lengthy_udt_table (k int PRIMARY KEY, v frozen<lengthy_udt>)"); // hold onto the UserType for future use UserType udtDef = cluster().getMetadata().getKeyspace(keyspace).getUserType("lengthy_udt"); // verify inserts and reads for (int i : Arrays.asList(0, 1, 2, 3, MAX_TEST_LENGTH)) { // create udt UDTValue createdUDT = udtDef.newValue(); for (int j = 0; j < i; ++j) { createdUDT.setInt(j, j); } // write udt session().execute("INSERT INTO lengthy_udt_table (k, v) VALUES (0, ?)", createdUDT); // verify udt was written and read correctly UDTValue r = session().execute("SELECT v FROM lengthy_udt_table WHERE k=0").one().getUDTValue("v"); assertThat(r.toString()).isEqualTo(createdUDT.toString()); } }
@Test(groups = "short") public void should_store_type_definitions_in_their_keyspace() throws Exception { KeyspaceMetadata thisKeyspace = cluster().getMetadata().getKeyspace(this.keyspace); // Types that don't exist don't have definitions assertThat(thisKeyspace.getUserType("address1")).isNull(); assertThat(thisKeyspace.getUserType("phone1")).isNull(); // Types created by this test have definitions assertThat(thisKeyspace.getUserType(quote("\"User Address\""))).isNotNull(); assertThat(thisKeyspace.getUserType("phone")).isNotNull(); // If we create another keyspace, it doesn't have the definitions of this keyspace String otherKeyspaceName = this.keyspace + "_nonEx"; session() .execute( "CREATE KEYSPACE " + otherKeyspaceName + " " + "WITH replication = { 'class' : 'SimpleStrategy', 'replication_factor': '1'}"); KeyspaceMetadata otherKeyspace = cluster().getMetadata().getKeyspace(otherKeyspaceName); assertThat(otherKeyspace.getUserType(quote("\"User Address\""))).isNull(); assertThat(otherKeyspace.getUserType("phone")).isNull(); }
@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); }
private void setUpUserTypes(Cluster cluster) { addressType = cluster.getMetadata().getKeyspace(keyspace).getUserType("address"); phoneType = cluster.getMetadata().getKeyspace(keyspace).getUserType("phone"); UDTValue phone1Value = phoneType.newValue().setString("number", phone1.number).setSet("tags", phone1.tags); UDTValue phone2Value = phoneType.newValue().setString("number", phone2.number).setSet("tags", phone2.tags); addressValue = addressType .newValue() .setString("street", address.street) .setInt(1, address.zipcode) .setList("phones", Lists.newArrayList(phone1Value, phone2Value)); }
@Test(groups = "short") public void should_store_and_retrieve_with_simple_statements() throws Exception { int userId = 1; UserType addrDef = cluster().getMetadata().getKeyspace(keyspace).getUserType(quote("\"User Address\"")); UserType phoneDef = cluster().getMetadata().getKeyspace(keyspace).getUserType("phone"); UDTValue phone1 = phoneDef.newValue().setString("alias", "home").setString("number", "0123548790"); UDTValue phone2 = phoneDef.newValue().setString("alias", "work").setString("number", "0698265251"); UDTValue addr = addrDef .newValue() .setString("street", "1600 Pennsylvania Ave NW") .setInt(quote("ZIP\""), 20500) .setSet("phones", ImmutableSet.of(phone1, phone2)); session().execute("INSERT INTO user(id, addr) VALUES (?, ?)", userId, addr); Row r = session().execute("SELECT * FROM user WHERE id=?", userId).one(); assertThat(r.getInt("id")).isEqualTo(userId); assertThat(r.getUDTValue("addr")).isEqualTo(addr); }
cluster().getMetadata().getKeyspace(keyspace).getUserType("user_null_values"); UDTValue userType = userTypeDef
@Test(groups = "short") @CassandraVersion(value = "3.6", description = "Non-frozen UDTs were introduced in C* 3.6") public void should_indicate_user_type_is_not_frozen() { session().execute("CREATE TABLE not_frozen_table(k int primary key, v type_for_frozen_test)"); KeyspaceMetadata keyspaceMetadata = cluster().getMetadata().getKeyspace(this.keyspace); assertThat(keyspaceMetadata.getUserType("type_for_frozen_test")).isNotFrozen(); DataType userType = keyspaceMetadata.getTable("not_frozen_table").getColumn("v").getType(); assertThat(userType).isNotFrozen(); assertThat(userType.toString()).isEqualTo(keyspace + ".type_for_frozen_test"); ResultSet rs = session().execute("SELECT v FROM not_frozen_table WHERE k = 1"); assertThat(rs.getColumnDefinitions().getType(0)).isNotFrozen(); PreparedStatement pst = session().prepare("SELECT v FROM not_frozen_table WHERE k = ?"); assertThat(pst.getVariables().getType(0)).isNotFrozen(); } }
@Test(groups = "short") public void should_indicate_user_type_is_frozen() { session() .execute("CREATE TABLE frozen_table(k int primary key, v frozen<type_for_frozen_test>)"); KeyspaceMetadata keyspaceMetadata = cluster().getMetadata().getKeyspace(this.keyspace); assertThat(keyspaceMetadata.getUserType("type_for_frozen_test")).isNotFrozen(); DataType userType = keyspaceMetadata.getTable("frozen_table").getColumn("v").getType(); assertThat(userType).isFrozen(); assertThat(userType.toString()).isEqualTo("frozen<" + keyspace + ".type_for_frozen_test>"); // The frozen flag is not set for result set definitions (the protocol does not provide // that information and it's not really useful in that situation). We always return false. ResultSet rs = session().execute("SELECT v FROM frozen_table WHERE k = 1"); assertThat(rs.getColumnDefinitions().getType(0)).isNotFrozen(); // Same thing for prepared statements PreparedStatement pst = session().prepare("SELECT v FROM frozen_table WHERE k = ?"); assertThat(pst.getVariables().getType(0)).isNotFrozen(); }
@Test(groups = "short") public void should_store_and_retrieve_with_prepared_statements() throws Exception { int userId = 0; PreparedStatement ins = session().prepare("INSERT INTO user(id, addr) VALUES (?, ?)"); PreparedStatement sel = session().prepare("SELECT * FROM user WHERE id=?"); UserType addrDef = cluster().getMetadata().getKeyspace(keyspace).getUserType(quote("\"User Address\"")); UserType phoneDef = cluster().getMetadata().getKeyspace(keyspace).getUserType("phone"); UDTValue phone1 = phoneDef.newValue().setString("alias", "home").setString("number", "0123548790"); UDTValue phone2 = phoneDef.newValue().setString("alias", "work").setString("number", "0698265251"); UDTValue addr = addrDef .newValue() .setString("street", "1600 Pennsylvania Ave NW") .setInt(quote("ZIP\""), 20500) .setSet("phones", ImmutableSet.of(phone1, phone2)); session().execute(ins.bind(userId, addr)); Row r = session().execute(sel.bind(userId)).one(); assertThat(r.getInt("id")).isEqualTo(0); }
UserType addressType = keyspace.getUserType("\"Address\""); FunctionMetadata function = keyspace.getFunction("\"NUM_PHONES_ACCU\"", cint(), addressType); assertThat(function).isNotNull();
.withPort(ccm().getBinaryPort()) .build()); UserType fooType = cluster2.getMetadata().getKeyspace("ks").getUserType("foo");
UserType addressType = keyspace.getUserType("\"Address\""); FunctionMetadata stateFunc = keyspace.getFunction("\"MY_FUNC\"", addressType, addressType); AggregateMetadata aggregate = keyspace.getAggregate("\"MY_AGGREGATE\"", addressType);
@Test(groups = "short") public void should_parse_nested_collection_types() { Metadata metadata = cluster().getMetadata(); KeyspaceMetadata keyspaceMetadata = metadata.getKeyspace(this.keyspace); assertThat(parse("list<list<int>>", cluster(), null, null, null, false, false)) .isEqualTo(list(list(cint()))); assertThat( parse( "set<list<frozen<map<bigint,varchar>>>>", cluster(), null, null, null, false, false)) .isEqualTo(set(list(map(bigint(), varchar(), true)))); UserType keyType = keyspaceMetadata.getUserType(quote("Incr,edibly\" EvilTy<>><<><p\"e")); UserType valueType = keyspaceMetadata.getUserType(quote("A")); assertThat( parse( "map<frozen<\"Incr,edibly\"\" EvilTy<>><<><p\"\"e\">,frozen<\"A\">>", cluster(), keyspace, keyspaceMetadata.userTypes, null, false, false)) .isEqualTo(map(keyType, valueType, false)); }