Skip to content

Commit 4ab80fa

Browse files
author
Michael Karsten
committed
refactor: leave existing methods in place for backward compatibility. Add Zstd as a built-in option.
1 parent 7e3cca7 commit 4ab80fa

File tree

3 files changed

+210
-28
lines changed

3 files changed

+210
-28
lines changed

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

Lines changed: 94 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,15 @@
1919

2020
import static com.datastax.oss.driver.api.querybuilder.SchemaBuilder.RowsPerPartition;
2121

22+
import com.datastax.oss.driver.api.core.config.DriverConfigLoader;
2223
import com.datastax.oss.driver.api.querybuilder.SchemaBuilder;
2324
import com.datastax.oss.driver.api.querybuilder.schema.compaction.CompactionStrategy;
2425
import com.datastax.oss.driver.shaded.guava.common.collect.ImmutableMap;
2526
import edu.umd.cs.findbugs.annotations.CheckReturnValue;
2627
import edu.umd.cs.findbugs.annotations.NonNull;
2728

29+
import java.net.URL;
30+
2831
public interface RelationOptions<SelfT extends RelationOptions<SelfT>>
2932
extends OptionProvider<SelfT> {
3033

@@ -97,22 +100,32 @@ default SelfT withCompaction(@NonNull CompactionStrategy<?> compactionStrategy)
97100
}
98101

99102
/**
100-
* Configures compression using the LZ4 algorithm with the given chunk length and crc check
101-
* chance.
102-
*
103-
* @see #withCompression(String, int, double)
103+
* @deprecated This method only exists for backward compatibility. Use {@link
104+
* RelationOptions#withLZ4Compression(chunkLengthKB)} instead.
104105
*/
106+
@Deprecated
105107
@NonNull
106108
@CheckReturnValue
107109
default SelfT withLZ4Compression(int chunkLengthKB, double crcCheckChance) {
108110
return withCompression("LZ4Compressor", chunkLengthKB, crcCheckChance);
109111
}
110112

113+
/**
114+
* Configures compression using the LZ4 algorithm with the given chunk length.
115+
*
116+
* @see #withCompression(String, int)
117+
*/
118+
@NonNull
119+
@CheckReturnValue
120+
default SelfT withLZ4Compression(int chunkLengthKB) {
121+
return withCompression("LZ4Compressor", chunkLengthKB);
122+
}
123+
111124
/**
112125
* Configures compression using the LZ4 algorithm using the default configuration (64kb
113-
* chunk_length, and 1.0 crc_check_chance).
126+
* chunk_length).
114127
*
115-
* @see #withCompression(String, int, double)
128+
* @see #withCompression(String, int)
116129
*/
117130
@NonNull
118131
@CheckReturnValue
@@ -121,22 +134,55 @@ default SelfT withLZ4Compression() {
121134
}
122135

123136
/**
124-
* Configures compression using the Snappy algorithm with the given chunk length and crc check
125-
* chance.
137+
* Configures compression using the Zstd algorithm with the given chunk length.
138+
*
139+
* @see #withCompression(String, int)
140+
*/
141+
@NonNull
142+
@CheckReturnValue
143+
default SelfT withZstdCompression(int chunkLengthKB) {
144+
return withCompression("org.apache.cassandra.io.compress.ZstdCompressor", chunkLengthKB);
145+
}
146+
147+
/**
148+
* Configures compression using the Zstd algorithm using the default configuration (64kb
149+
* chunk_length).
126150
*
127-
* @see #withCompression(String, int, double)
151+
* @see #withCompression(String, int)
152+
*/
153+
@NonNull
154+
@CheckReturnValue
155+
default SelfT withZstdCompression() {
156+
return withCompression("org.apache.cassandra.io.compress.ZstdCompressor");
157+
}
158+
159+
/**
160+
* @deprecated This method only exists for backward compatibility. Use {@link
161+
* RelationOptions#withSnappyCompression(chunkLengthKB)} instead.
128162
*/
163+
@Deprecated
129164
@NonNull
130165
@CheckReturnValue
131166
default SelfT withSnappyCompression(int chunkLengthKB, double crcCheckChance) {
132167
return withCompression("SnappyCompressor", chunkLengthKB, crcCheckChance);
133168
}
134169

170+
/**
171+
* Configures compression using the Snappy algorithm with the given chunk length.
172+
*
173+
* @see #withCompression(String, int)
174+
*/
175+
@NonNull
176+
@CheckReturnValue
177+
default SelfT withSnappyCompression(int chunkLengthKB) {
178+
return withCompression("SnappyCompressor", chunkLengthKB);
179+
}
180+
135181
/**
136182
* Configures compression using the Snappy algorithm using the default configuration (64kb
137-
* chunk_length, and 1.0 crc_check_chance).
183+
* chunk_length).
138184
*
139-
* @see #withCompression(String, int, double)
185+
* @see #withCompression(String, int)
140186
*/
141187
@NonNull
142188
@CheckReturnValue
@@ -145,22 +191,32 @@ default SelfT withSnappyCompression() {
145191
}
146192

147193
/**
148-
* Configures compression using the Deflate algorithm with the given chunk length and crc check
149-
* chance.
150-
*
151-
* @see #withCompression(String, int, double)
194+
* @deprecated This method only exists for backward compatibility. Use {@link
195+
* RelationOptions#withDeflateCompression(chunkLengthKB)} instead.
152196
*/
197+
@Deprecated
153198
@NonNull
154199
@CheckReturnValue
155200
default SelfT withDeflateCompression(int chunkLengthKB, double crcCheckChance) {
156201
return withCompression("DeflateCompressor", chunkLengthKB, crcCheckChance);
157202
}
158203

204+
/**
205+
* Configures compression using the Deflate algorithm with the given chunk length.
206+
*
207+
* @see #withCompression(String, int)
208+
*/
209+
@NonNull
210+
@CheckReturnValue
211+
default SelfT withDeflateCompression(int chunkLengthKB) {
212+
return withCompression("DeflateCompressor", chunkLengthKB);
213+
}
214+
159215
/**
160216
* Configures compression using the Deflate algorithm using the default configuration (64kb
161217
* chunk_length, and 1.0 crc_check_chance).
162218
*
163-
* @see #withCompression(String, int, double)
219+
* @see #withCompression(String, int)
164220
*/
165221
@NonNull
166222
@CheckReturnValue
@@ -170,13 +226,13 @@ default SelfT withDeflateCompression() {
170226

171227
/**
172228
* Configures compression using the given algorithm using the default configuration (64kb
173-
* chunk_length, and 1.0 crc_check_chance).
229+
* chunk_length).
174230
*
175231
* <p>Unless specifying a custom compression algorithm implementation, it is recommended to use
176232
* {@link #withLZ4Compression()}, {@link #withSnappyCompression()}, or {@link
177233
* #withDeflateCompression()}.
178234
*
179-
* @see #withCompression(String, int, double)
235+
* @see #withCompression(String, int)
180236
*/
181237
@NonNull
182238
@CheckReturnValue
@@ -185,27 +241,43 @@ default SelfT withCompression(@NonNull String compressionAlgorithmName) {
185241
}
186242

187243
/**
188-
* Configures compression using the given algorithm, chunk length and crc check chance.
244+
* Configures compression using the given algorithm, chunk length.
189245
*
190246
* <p>Unless specifying a custom compression algorithm implementation, it is recommended to use
191247
* {@link #withLZ4Compression()}, {@link #withSnappyCompression()}, or {@link
192248
* #withDeflateCompression()}.
193249
*
194250
* @param compressionAlgorithmName The class name of the compression algorithm.
195251
* @param chunkLengthKB The chunk length in KB of compression blocks. Defaults to 64.
196-
* @param crcCheckChance The probability (0.0 to 1.0) that checksum will be checked on each read.
197-
* Defaults to 1.0.
198252
*/
199253
@NonNull
200254
@CheckReturnValue
255+
default SelfT withCompression(
256+
@NonNull String compressionAlgorithmName, int chunkLengthKB) {
257+
return withOption(
258+
"compression",
259+
ImmutableMap.of(
260+
"class",
261+
compressionAlgorithmName,
262+
"chunk_length_in_kb",
263+
chunkLengthKB));
264+
}
265+
266+
/**
267+
* @deprecated This method only exists for backward compatibility. Use {@link
268+
* RelationOptions#withCompression(compressionAlgorithmName, chunkLengthKB)} instead.
269+
*/
270+
@NonNull
271+
@CheckReturnValue
272+
@Deprecated
201273
default SelfT withCompression(
202274
@NonNull String compressionAlgorithmName, int chunkLengthKB, double crcCheckChance) {
203275
return withOption(
204276
"compression",
205277
ImmutableMap.of(
206278
"class",
207279
compressionAlgorithmName,
208-
"chunk_length_in_kb",
280+
"chunk_length_kb",
209281
chunkLengthKB,
210282
"crc_check_chance",
211283
crcCheckChance));

query-builder/src/test/java/com/datastax/dse/driver/api/querybuilder/schema/CreateDseTableTest.java

Lines changed: 58 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -195,13 +195,46 @@ public void should_generate_create_table_lz4_compression() {
195195

196196
@Test
197197
public void should_generate_create_table_lz4_compression_options() {
198+
assertThat(
199+
createDseTable("bar")
200+
.withPartitionKey("k", DataTypes.INT)
201+
.withColumn("v", DataTypes.TEXT)
202+
.withLZ4Compression(1024))
203+
.hasCql(
204+
"CREATE TABLE bar (k int PRIMARY KEY,v text) WITH compression={'class':'LZ4Compressor','chunk_length_kb':1024}");
205+
}
206+
207+
@Test
208+
public void should_generate_create_table_lz4_compression_options_crc() {
198209
assertThat(
199210
createDseTable("bar")
200211
.withPartitionKey("k", DataTypes.INT)
201212
.withColumn("v", DataTypes.TEXT)
202213
.withLZ4Compression(1024, .5))
203214
.hasCql(
204-
"CREATE TABLE bar (k int PRIMARY KEY,v text) WITH compression={'class':'LZ4Compressor','chunk_length_in_kb':1024,'crc_check_chance':0.5}");
215+
"CREATE TABLE bar (k int PRIMARY KEY,v text) WITH compression={'class':'LZ4Compressor','chunk_length_kb':1024,'crc_check_chance':0.5}");
216+
}
217+
218+
@Test
219+
public void should_generate_create_table_zstd_compression() {
220+
assertThat(
221+
createDseTable("bar")
222+
.withPartitionKey("k", DataTypes.INT)
223+
.withColumn("v", DataTypes.TEXT)
224+
.withZstdCompression())
225+
.hasCql(
226+
"CREATE TABLE bar (k int PRIMARY KEY,v text) WITH compression={'class':'org.apache.cassandra.io.compress.ZstdCompressor'}");
227+
}
228+
229+
@Test
230+
public void should_generate_create_table_zstd_compression_options() {
231+
assertThat(
232+
createDseTable("bar")
233+
.withPartitionKey("k", DataTypes.INT)
234+
.withColumn("v", DataTypes.TEXT)
235+
.withZstdCompression(1024))
236+
.hasCql(
237+
"CREATE TABLE bar (k int PRIMARY KEY,v text) WITH compression={'class':'org.apache.cassandra.io.compress.ZstdCompressor','chunk_length_in_kb':1024}");
205238
}
206239

207240
@Test
@@ -217,13 +250,24 @@ public void should_generate_create_table_snappy_compression() {
217250

218251
@Test
219252
public void should_generate_create_table_snappy_compression_options() {
253+
assertThat(
254+
createDseTable("bar")
255+
.withPartitionKey("k", DataTypes.INT)
256+
.withColumn("v", DataTypes.TEXT)
257+
.withSnappyCompression(2048))
258+
.hasCql(
259+
"CREATE TABLE bar (k int PRIMARY KEY,v text) WITH compression={'class':'SnappyCompressor','chunk_length_in_kb':2048}");
260+
}
261+
262+
@Test
263+
public void should_generate_create_table_snappy_compression_options_crc() {
220264
assertThat(
221265
createDseTable("bar")
222266
.withPartitionKey("k", DataTypes.INT)
223267
.withColumn("v", DataTypes.TEXT)
224268
.withSnappyCompression(2048, .25))
225269
.hasCql(
226-
"CREATE TABLE bar (k int PRIMARY KEY,v text) WITH compression={'class':'SnappyCompressor','chunk_length_in_kb':2048,'crc_check_chance':0.25}");
270+
"CREATE TABLE bar (k int PRIMARY KEY,v text) WITH compression={'class':'SnappyCompressor','chunk_length_kb':2048,'crc_check_chance':0.25}");
227271
}
228272

229273
@Test
@@ -239,13 +283,24 @@ public void should_generate_create_table_deflate_compression() {
239283

240284
@Test
241285
public void should_generate_create_table_deflate_compression_options() {
286+
assertThat(
287+
createDseTable("bar")
288+
.withPartitionKey("k", DataTypes.INT)
289+
.withColumn("v", DataTypes.TEXT)
290+
.withDeflateCompression(4096))
291+
.hasCql(
292+
"CREATE TABLE bar (k int PRIMARY KEY,v text) WITH compression={'class':'DeflateCompressor','chunk_length_in_kb':4096}");
293+
}
294+
295+
@Test
296+
public void should_generate_create_table_deflate_compression_options_crc() {
242297
assertThat(
243298
createDseTable("bar")
244299
.withPartitionKey("k", DataTypes.INT)
245300
.withColumn("v", DataTypes.TEXT)
246301
.withDeflateCompression(4096, .1))
247302
.hasCql(
248-
"CREATE TABLE bar (k int PRIMARY KEY,v text) WITH compression={'class':'DeflateCompressor','chunk_length_in_kb':4096,'crc_check_chance':0.1}");
303+
"CREATE TABLE bar (k int PRIMARY KEY,v text) WITH compression={'class':'DeflateCompressor','chunk_length_kb':4096,'crc_check_chance':0.1}");
249304
}
250305

251306
@Test

0 commit comments

Comments
 (0)