Skip to content

Commit 72c729b

Browse files
lukasz-antoniakabsurdfarce
authored andcommitted
CASSANDRA-19932: Allow to define extensions while creating table
patch by Lukasz Antoniak; reviewed by Bret McGuire and Chris Lohfink
1 parent eb57fd7 commit 72c729b

File tree

3 files changed

+64
-1
lines changed

3 files changed

+64
-1
lines changed

query-builder/src/main/java/com/datastax/oss/driver/api/querybuilder/schema/CreateTableWithOptions.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,23 @@
1818
package com.datastax.oss.driver.api.querybuilder.schema;
1919

2020
import com.datastax.oss.driver.api.querybuilder.BuildableQuery;
21+
import com.datastax.oss.driver.internal.querybuilder.schema.RawOptionsWrapper;
22+
import com.datastax.oss.driver.shaded.guava.common.collect.Maps;
23+
import edu.umd.cs.findbugs.annotations.CheckReturnValue;
2124
import edu.umd.cs.findbugs.annotations.NonNull;
25+
import java.util.Map;
2226

2327
public interface CreateTableWithOptions
2428
extends BuildableQuery, RelationStructure<CreateTableWithOptions> {
2529

2630
/** Enables COMPACT STORAGE in the CREATE TABLE statement. */
2731
@NonNull
2832
CreateTableWithOptions withCompactStorage();
33+
34+
/** Attaches custom metadata to CQL table definition. */
35+
@NonNull
36+
@CheckReturnValue
37+
default CreateTableWithOptions withExtensions(@NonNull Map<String, byte[]> extensions) {
38+
return withOption("extensions", Maps.transformValues(extensions, RawOptionsWrapper::of));
39+
}
2940
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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 com.datastax.oss.driver.internal.querybuilder.schema;
19+
20+
import com.datastax.oss.driver.api.core.data.ByteUtils;
21+
22+
/**
23+
* Wrapper class to indicate that the contained String value should be understood to represent a CQL
24+
* literal that can be included directly in a CQL statement (i.e. without escaping).
25+
*/
26+
public class RawOptionsWrapper {
27+
private final String val;
28+
29+
private RawOptionsWrapper(String val) {
30+
this.val = val;
31+
}
32+
33+
public static RawOptionsWrapper of(String val) {
34+
return new RawOptionsWrapper(val);
35+
}
36+
37+
public static RawOptionsWrapper of(byte[] val) {
38+
return new RawOptionsWrapper(ByteUtils.toHexString(val));
39+
}
40+
41+
@Override
42+
public String toString() {
43+
return this.val;
44+
}
45+
}

query-builder/src/test/java/com/datastax/oss/driver/api/querybuilder/schema/CreateTableTest.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import com.datastax.oss.driver.api.querybuilder.schema.compaction.TimeWindowCompactionStrategy.CompactionWindowUnit;
2929
import com.datastax.oss.driver.api.querybuilder.schema.compaction.TimeWindowCompactionStrategy.TimestampResolution;
3030
import com.datastax.oss.driver.shaded.guava.common.collect.ImmutableMap;
31+
import java.nio.charset.StandardCharsets;
3132
import org.junit.Test;
3233

3334
public class CreateTableTest {
@@ -169,14 +170,20 @@ public void should_generate_create_table_with_options() {
169170
.withComment("Hello world")
170171
.withDcLocalReadRepairChance(0.54)
171172
.withDefaultTimeToLiveSeconds(86400)
173+
.withExtensions(
174+
ImmutableMap.of(
175+
"key1",
176+
"apache".getBytes(StandardCharsets.UTF_8),
177+
"key2",
178+
"cassandra".getBytes(StandardCharsets.UTF_8)))
172179
.withGcGraceSeconds(864000)
173180
.withMemtableFlushPeriodInMs(10000)
174181
.withMinIndexInterval(1024)
175182
.withMaxIndexInterval(4096)
176183
.withReadRepairChance(0.55)
177184
.withSpeculativeRetry("99percentile"))
178185
.hasCql(
179-
"CREATE TABLE bar (k int PRIMARY KEY,v text) WITH bloom_filter_fp_chance=0.42 AND cdc=false AND comment='Hello world' AND dclocal_read_repair_chance=0.54 AND default_time_to_live=86400 AND gc_grace_seconds=864000 AND memtable_flush_period_in_ms=10000 AND min_index_interval=1024 AND max_index_interval=4096 AND read_repair_chance=0.55 AND speculative_retry='99percentile'");
186+
"CREATE TABLE bar (k int PRIMARY KEY,v text) WITH bloom_filter_fp_chance=0.42 AND cdc=false AND comment='Hello world' AND dclocal_read_repair_chance=0.54 AND default_time_to_live=86400 AND extensions={'key1':0x617061636865,'key2':0x63617373616e647261} AND gc_grace_seconds=864000 AND memtable_flush_period_in_ms=10000 AND min_index_interval=1024 AND max_index_interval=4096 AND read_repair_chance=0.55 AND speculative_retry='99percentile'");
180187
}
181188

182189
@Test

0 commit comments

Comments
 (0)