@Override public void insert(CassandraSessionPool.Session session, CassandraBackendEntry.Row entry) { Map<HugeKeys, Object> columns = entry.columns(); assert columns.containsKey(HugeKeys.ID); Object id = columns.get(HugeKeys.ID); Object label = columns.get(HugeKeys.LABEL); E.checkState(label != null, "The label of inserting vertex can't be null"); Map<?, ?> properties = (Map<?, ?>) columns.get(HugeKeys.PROPERTIES); E.checkState(properties != null, "The properties of inserting vertex can't be null"); Update update = QueryBuilder.update(table()); update.with(QueryBuilder.set(formatKey(HugeKeys.LABEL), label)); update.with(QueryBuilder.putAll(formatKey(HugeKeys.PROPERTIES), properties)); update.where(formatEQ(HugeKeys.ID, id)); session.add(update); } }
/** * Append several elements to the collection column of a row */ @Override public void append(CassandraSessionPool.Session session, CassandraBackendEntry.Row entry) { List<HugeKeys> idNames = this.idColumnName(); List<HugeKeys> colNames = this.modifiableColumnName(); Map<HugeKeys, Object> columns = entry.columns(); Update update = QueryBuilder.update(table()); for (HugeKeys key : colNames) { if (!columns.containsKey(key)) { continue; } String name = formatKey(key); Object value = columns.get(key); if (value instanceof Map) { update.with(QueryBuilder.putAll(name, (Map<?, ?>) value)); } else if (value instanceof List) { update.with(QueryBuilder.appendAll(name, (List<?>) value)); } else { update.with(QueryBuilder.append(name, value)); } } for (HugeKeys idName : idNames) { assert columns.containsKey(idName); update.where(formatEQ(idName, columns.get(idName))); } session.add(update); }
@Test(groups = "unit") public void should_handle_nested_collections() { String query; Statement statement; query = "UPDATE foo SET l=[[1],[2]] WHERE k=1;"; ImmutableList<ImmutableList<Integer>> list = ImmutableList.of(ImmutableList.of(1), ImmutableList.of(2)); statement = update("foo").with(set("l", list)).where(eq("k", 1)); assertThat(statement.toString()).isEqualTo(query); query = "UPDATE foo SET m={1:[[1],[2]],2:[[1],[2]]} WHERE k=1;"; statement = update("foo").with(set("m", ImmutableMap.of(1, list, 2, list))).where(eq("k", 1)); assertThat(statement.toString()).isEqualTo(query); query = "UPDATE foo SET m=m+{1:[[1],[2]],2:[[1],[2]]} WHERE k=1;"; statement = update("foo").with(putAll("m", ImmutableMap.of(1, list, 2, list))).where(eq("k", 1)); assertThat(statement.toString()).isEqualTo(query); query = "UPDATE foo SET l=[[1]]+l WHERE k=1;"; statement = update("foo").with(prepend("l", ImmutableList.of(1))).where(eq("k", 1)); assertThat(statement.toString()).isEqualTo(query); query = "UPDATE foo SET l=[[1],[2]]+l WHERE k=1;"; statement = update("foo").with(prependAll("l", list)).where(eq("k", 1)); assertThat(statement.toString()).isEqualTo(query); }
@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); }
.and(put("c", "k", "v")) .and( putAll( "d", new TreeMap<String, Integer>() {
@Override public UpdateBuilder<T> put(String name, Map<?, ?> map) { update.with(QueryBuilder.putAll(classBean.toColumn(name), map)); return this; } @Override
private static Assignment getAssignment(AddToMapOp updateOp) { return QueryBuilder.putAll(updateOp.getColumnName().toCql(), updateOp.getValue()); }
@Override public void insert(CassandraSessionPool.Session session, CassandraBackendEntry.Row entry) { Map<HugeKeys, Object> columns = entry.columns(); assert columns.containsKey(HugeKeys.ID); Object id = columns.get(HugeKeys.ID); Object label = columns.get(HugeKeys.LABEL); E.checkState(label != null, "The label of inserting vertex can't be null"); Map<?, ?> properties = (Map<?, ?>) columns.get(HugeKeys.PROPERTIES); E.checkState(properties != null, "The properties of inserting vertex can't be null"); Update update = QueryBuilder.update(table()); update.with(QueryBuilder.set(formatKey(HugeKeys.LABEL), label)); update.with(QueryBuilder.putAll(formatKey(HugeKeys.PROPERTIES), properties)); update.where(formatEQ(HugeKeys.ID, id)); session.add(update); } }
/** * Append several elements to the collection column of a row */ @Override public void append(CassandraSessionPool.Session session, CassandraBackendEntry.Row entry) { List<HugeKeys> idNames = this.idColumnName(); List<HugeKeys> colNames = this.modifiableColumnName(); Map<HugeKeys, Object> columns = entry.columns(); Update update = QueryBuilder.update(table()); for (HugeKeys key : colNames) { if (!columns.containsKey(key)) { continue; } String name = formatKey(key); Object value = columns.get(key); if (value instanceof Map) { update.with(QueryBuilder.putAll(name, (Map<?, ?>) value)); } else if (value instanceof List) { update.with(QueryBuilder.appendAll(name, (List<?>) value)); } else { update.with(QueryBuilder.append(name, value)); } } for (HugeKeys idName : idNames) { assert columns.containsKey(idName); update.where(formatEQ(idName, columns.get(idName))); } session.add(update); }
@Test(groups = "unit") public void should_handle_nested_collections() { String query; Statement statement; query = "UPDATE foo SET l=[[1],[2]] WHERE k=1;"; ImmutableList<ImmutableList<Integer>> list = ImmutableList.of(ImmutableList.of(1), ImmutableList.of(2)); statement = update("foo").with(set("l", list)).where(eq("k", 1)); assertThat(statement.toString()).isEqualTo(query); query = "UPDATE foo SET m={1:[[1],[2]],2:[[1],[2]]} WHERE k=1;"; statement = update("foo").with(set("m", ImmutableMap.of(1, list, 2, list))).where(eq("k", 1)); assertThat(statement.toString()).isEqualTo(query); query = "UPDATE foo SET m=m+{1:[[1],[2]],2:[[1],[2]]} WHERE k=1;"; statement = update("foo").with(putAll("m", ImmutableMap.of(1, list, 2, list))).where(eq("k", 1)); assertThat(statement.toString()).isEqualTo(query); query = "UPDATE foo SET l=[[1]]+l WHERE k=1;"; statement = update("foo").with(prepend("l", ImmutableList.of(1))).where(eq("k", 1)); assertThat(statement.toString()).isEqualTo(query); query = "UPDATE foo SET l=[[1],[2]]+l WHERE k=1;"; statement = update("foo").with(prependAll("l", list)).where(eq("k", 1)); assertThat(statement.toString()).isEqualTo(query); }
@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); }
public static BoundStatement prepareAppendItemToCollection(Object id, Class<?> clazz, String propertyName, Object item, WriteOptions options, String keyspace, Session session) { EntityTypeMetadata emeta = EntityTypeParser.getEntityMetadata(clazz); EntityFieldMetaData fmeta = emeta.getFieldMetadata(propertyName); Update update = QueryBuilder.update(keyspace, emeta.getTableName()); if (item instanceof Set<?> && fmeta.getType() == Set.class) { Set<?> set = (Set<?>) item; if (set.size() == 0) return null; update.with(QueryBuilder.addAll(fmeta.getColumnName(), set)); } else if (item instanceof List<?> && fmeta.getType() == List.class) { List<?> list = (List<?>) item; if (list.size() == 0) return null; update.with(QueryBuilder.appendAll(fmeta.getColumnName(), list)); } else if (item instanceof Map<?, ?>) { Map<?, ?> map = (Map<?, ?>) item; if (map.size() == 0) return null; update.with(QueryBuilder.putAll(fmeta.getColumnName(), map)); } else if (fmeta.getType() == Set.class) { update.with(QueryBuilder.add(fmeta.getColumnName(), item)); } else if (fmeta.getType() == List.class) { update.with(QueryBuilder.append(fmeta.getColumnName(), item)); } applyOptions(options, update, null); return prepareUpdate(id, emeta, update, session); }
.and(put("c", "k", "v")) .and( putAll( "d", new TreeMap<String, Integer>() {