Skip to content

Commit 802f238

Browse files
committed
Polishing.
Refine enum constructors to common creation patterns. Improve boolean option handling in TableOptionsSpecification. Quote unknown TableOptions as Cassandra accepts quoted options. Add since tags. Simplify tests. See #1584 Original pull request: #1586
1 parent be28897 commit 802f238

12 files changed

+391
-224
lines changed

spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/CassandraAdminTemplate.java

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -109,16 +109,19 @@ public SchemaFactory getSchemaFactory() {
109109
@Override
110110
public void createTable(boolean ifNotExists, CqlIdentifier tableName, Class<?> entityClass,
111111
Map<String, Object> optionsByName) {
112-
CassandraPersistentEntity<?> entity = getConverter().getMappingContext().getRequiredPersistentEntity(entityClass);
113112

113+
CassandraPersistentEntity<?> entity = getConverter().getMappingContext().getRequiredPersistentEntity(entityClass);
114114
CreateTableSpecification createTableSpecification = this.schemaFactory
115115
.getCreateTableSpecificationFor(entity, tableName).ifNotExists(ifNotExists);
116116

117117
if (!CollectionUtils.isEmpty(optionsByName)) {
118+
118119
optionsByName.forEach((key, value) -> {
119-
TableOption tableOption = TableOption.findByNameIgnoreCase(key);
120+
121+
TableOption tableOption = TableOption.findByName(key);
122+
120123
if (tableOption == null) {
121-
addRawTableOption(key, value, createTableSpecification);
124+
createTableSpecification.with(key, value);
122125
} else if (tableOption.requiresValue()) {
123126
createTableSpecification.with(tableOption, value);
124127
} else {
@@ -130,14 +133,6 @@ public void createTable(boolean ifNotExists, CqlIdentifier tableName, Class<?> e
130133
getCqlOperations().execute(CqlGenerator.toCql(createTableSpecification));
131134
}
132135

133-
private void addRawTableOption(String key, Object value, CreateTableSpecification createTableSpecification) {
134-
if (value instanceof String) {
135-
createTableSpecification.with(key, value, true, true);
136-
return;
137-
}
138-
createTableSpecification.with(key, value, false, false);
139-
}
140-
141136
@Override
142137
public void dropTable(Class<?> entityClass) {
143138
dropTable(getTableName(entityClass));

spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/cql/keyspace/CreateTableSpecification.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,11 @@ public CreateTableSpecification with(TableOption option, Object value) {
104104
return (CreateTableSpecification) super.with(option, value);
105105
}
106106

107+
@Override
108+
public CreateTableSpecification with(String name, @Nullable Object value) {
109+
return (CreateTableSpecification) super.with(name, value);
110+
}
111+
107112
@Override
108113
public CreateTableSpecification with(String name, @Nullable Object value, boolean escape, boolean quote) {
109114
return (CreateTableSpecification) super.with(name, value, escape, quote);

spring-data-cassandra/src/main/java/org/springframework/data/cassandra/core/cql/keyspace/TableOption.java

Lines changed: 99 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -37,82 +37,126 @@ public enum TableOption implements Option {
3737
/**
3838
* {@code comment}
3939
*/
40-
COMMENT("comment", String.class, true, true, true),
40+
COMMENT("comment", String.class, true),
41+
4142
/**
4243
* {@code COMPACT STORAGE}
4344
*
4445
* @deprecated since 2.2. Cassandra 4.x has deprecated compact storage.
4546
*/
4647
@Deprecated
47-
COMPACT_STORAGE("COMPACT STORAGE", Void.class, false, false, false),
48+
COMPACT_STORAGE("COMPACT STORAGE"),
49+
4850
/**
4951
* {@code compaction}. Value is a <code>Map&lt;CompactionOption,Object&gt;</code>.
5052
*
5153
* @see CompactionOption
5254
*/
53-
COMPACTION("compaction", Map.class, true, false, false),
55+
COMPACTION("compaction", Map.class),
56+
5457
/**
5558
* {@code compression}. Value is a <code>Map&lt;CompressionOption,Object&gt;</code>.
5659
*
5760
* @see CompressionOption
5861
*/
59-
COMPRESSION("compression", Map.class, true, false, false),
62+
COMPRESSION("compression", Map.class),
63+
6064
/**
6165
* {@code caching}
6266
*
6367
* @see CachingOption
6468
*/
65-
CACHING("caching", Map.class, true, false, false),
69+
CACHING("caching", Map.class),
70+
6671
/**
6772
* {@code bloom_filter_fp_chance}
6873
*/
69-
BLOOM_FILTER_FP_CHANCE("bloom_filter_fp_chance", Double.class, true, false, false),
74+
BLOOM_FILTER_FP_CHANCE("bloom_filter_fp_chance", Double.class),
75+
7076
/**
7177
* {@code read_repair_chance}
7278
*/
73-
READ_REPAIR_CHANCE("read_repair_chance", Double.class, true, false, false),
79+
READ_REPAIR_CHANCE("read_repair_chance", Double.class),
80+
7481
/**
7582
* {@code dclocal_read_repair_chance}
7683
*/
77-
DCLOCAL_READ_REPAIR_CHANCE("dclocal_read_repair_chance", Double.class, true, false, false),
84+
DCLOCAL_READ_REPAIR_CHANCE("dclocal_read_repair_chance", Double.class),
85+
7886
/**
7987
* {@code gc_grace_seconds}
8088
*/
81-
GC_GRACE_SECONDS("gc_grace_seconds", Long.class, true, false, false),
89+
GC_GRACE_SECONDS("gc_grace_seconds", Long.class),
90+
8291
/**
8392
* {@code default_time_to_live}
93+
*
94+
* @since 5.0
8495
*/
85-
DEFAULT_TIME_TO_LIVE("default_time_to_live", Long.class, true, false, false),
96+
DEFAULT_TIME_TO_LIVE("default_time_to_live", Long.class),
97+
8698
/**
8799
* {@code cdc}
100+
*
101+
* @since 5.0
88102
*/
89-
CDC("cdc", Boolean.class, true, false, false),
103+
CDC("cdc", Boolean.class),
104+
90105
/**
91106
* {@code speculative_retry}
107+
*
108+
* @since 5.0
92109
*/
93-
SPECULATIVE_RETRY("speculative_retry", String.class, true, true, true),
110+
SPECULATIVE_RETRY("speculative_retry", String.class, true),
111+
94112
/**
95113
* {@code memtable_flush_period_in_ms}
114+
*
115+
* @since 5.0
96116
*/
97-
MEMTABLE_FLUSH_PERIOD_IN_MS("memtable_flush_period_in_ms", Long.class, true, false, false),
117+
MEMTABLE_FLUSH_PERIOD_IN_MS("memtable_flush_period_in_ms", Long.class),
118+
98119
/**
99120
* {@code crc_check_chance}
121+
*
122+
* @since 5.0
100123
*/
101-
CRC_CHECK_CHANCE("crc_check_chance", Double.class, true, false, false),
124+
CRC_CHECK_CHANCE("crc_check_chance", Double.class),
125+
102126
/**
103127
* {@code min_index_interval}
128+
*
129+
* @since 5.0
104130
*/
105-
MIN_INDEX_INTERVAL("min_index_interval", Long.class, true, false, false),
131+
MIN_INDEX_INTERVAL("min_index_interval", Long.class),
132+
106133
/**
107134
* {@code max_index_interval}
135+
*
136+
* @since 5.0
108137
*/
109-
MAX_INDEX_INTERVAL("max_index_interval", Long.class, true, false, false),
138+
MAX_INDEX_INTERVAL("max_index_interval", Long.class),
139+
110140
/**
111141
* {@code read_repair}
142+
*
143+
* @since 5.0
112144
*/
113-
READ_REPAIR("read_repair", String.class, true, true, true);
145+
READ_REPAIR("read_repair", String.class, true);
146+
147+
private final Option delegate;
114148

115-
private Option delegate;
149+
TableOption(String name) {
150+
this(name, Void.class, false, false, false);
151+
}
152+
153+
TableOption(String name, Class<?> type) {
154+
this(name, type, true, false, false);
155+
}
156+
157+
TableOption(String name, Class<?> type, boolean escapeAndQuote) {
158+
this(name, type, true, escapeAndQuote, escapeAndQuote);
159+
}
116160

117161
TableOption(String name, Class<?> type, boolean requiresValue, boolean escapesValue, boolean quotesValue) {
118162
this.delegate = new DefaultOption(name, type, requiresValue, escapesValue, quotesValue);
@@ -127,28 +171,31 @@ public enum TableOption implements Option {
127171
* @since 4.1.1
128172
*/
129173
public static TableOption valueOfIgnoreCase(String optionName) {
130-
for (TableOption value : values()) {
131-
if (value.getName().equalsIgnoreCase(optionName)) {
132-
return value;
133-
}
174+
175+
TableOption tableOption = findByName(optionName);
176+
177+
if (tableOption != null) {
178+
return tableOption;
134179
}
180+
135181
throw new IllegalArgumentException(String.format("Unable to recognize specified Table option '%s'", optionName));
136182
}
137183

138184
/**
139185
* Look up {@link TableOption} by name using case-insensitive lookups.
140186
*
141187
* @param optionName name of the option.
142-
* @return the matching {@link TableOption}, or {@code null} if no match is found
143-
* @since 4.5.2
188+
* @return the matching {@link TableOption}, or {@literal null} if no match is found.
189+
* @since 5.0
144190
*/
145-
@Nullable
146-
public static TableOption findByNameIgnoreCase(String optionName) {
191+
public static @Nullable TableOption findByName(String optionName) {
192+
147193
for (TableOption value : values()) {
148194
if (value.getName().equalsIgnoreCase(optionName)) {
149195
return value;
150196
}
151197
}
198+
152199
return null;
153200
}
154201

@@ -212,7 +259,7 @@ public enum KeyCachingOption {
212259

213260
ALL("all"), NONE("none");
214261

215-
private String value;
262+
private final String value;
216263

217264
KeyCachingOption(String value) {
218265
this.value = value;
@@ -241,7 +288,7 @@ public enum CachingOption implements Option {
241288

242289
ROWS_PER_PARTITION("rows_per_partition", String.class, true, false, true);
243290

244-
private Option delegate;
291+
private final Option delegate;
245292

246293
CachingOption(String name, Class<?> type, boolean requiresValue, boolean escapesValue, boolean quotesValue) {
247294
this.delegate = new DefaultOption(name, type, requiresValue, escapesValue, quotesValue);
@@ -310,40 +357,52 @@ public enum CompactionOption implements Option {
310357
* {@code class}
311358
*/
312359
CLASS("class", String.class, true, false, true),
360+
313361
/**
314362
* {@code tombstone_threshold}
315363
*/
316-
TOMBSTONE_THRESHOLD("tombstone_threshold", Double.class, true, false, false),
364+
TOMBSTONE_THRESHOLD("tombstone_threshold", Double.class),
365+
317366
/**
318367
* {@code tombstone_compaction_interval}
319368
*/
320-
TOMBSTONE_COMPACTION_INTERVAL("tombstone_compaction_interval", Double.class, true, false, false),
369+
TOMBSTONE_COMPACTION_INTERVAL("tombstone_compaction_interval", Double.class),
370+
321371
/**
322372
* {@code min_sstable_size}
323373
*/
324-
MIN_SSTABLE_SIZE("min_sstable_size", Long.class, true, false, false),
374+
MIN_SSTABLE_SIZE("min_sstable_size", Long.class),
375+
325376
/**
326377
* {@code min_threshold}
327378
*/
328-
MIN_THRESHOLD("min_threshold", Long.class, true, false, false),
379+
MIN_THRESHOLD("min_threshold", Long.class),
380+
329381
/**
330382
* {@code max_threshold}
331383
*/
332-
MAX_THRESHOLD("max_threshold", Long.class, true, false, false),
384+
MAX_THRESHOLD("max_threshold", Long.class),
385+
333386
/**
334387
* {@code bucket_low}
335388
*/
336-
BUCKET_LOW("bucket_low", Double.class, true, false, false),
389+
BUCKET_LOW("bucket_low", Double.class),
390+
337391
/**
338392
* {@code bucket_high}
339393
*/
340-
BUCKET_HIGH("bucket_high", Double.class, true, false, false),
394+
BUCKET_HIGH("bucket_high", Double.class),
395+
341396
/**
342397
* {@code sstable_size_in_mb}
343398
*/
344-
SSTABLE_SIZE_IN_MB("sstable_size_in_mb", Long.class, true, false, false);
399+
SSTABLE_SIZE_IN_MB("sstable_size_in_mb", Long.class);
400+
401+
private final Option delegate;
345402

346-
private Option delegate;
403+
CompactionOption(String name, Class<?> type) {
404+
this(name, type, true, false, false);
405+
}
347406

348407
CompactionOption(String name, Class<?> type, boolean requiresValue, boolean escapesValue, boolean quotesValue) {
349408
this.delegate = new DefaultOption(name, type, requiresValue, escapesValue, quotesValue);
@@ -411,16 +470,18 @@ public enum CompressionOption implements Option {
411470
* {@code sstable_compression}
412471
*/
413472
SSTABLE_COMPRESSION("sstable_compression", String.class, true, false, true),
473+
414474
/**
415475
* {@code chunk_length_kb}
416476
*/
417477
CHUNK_LENGTH_KB("chunk_length_kb", Long.class, true, false, false),
478+
418479
/**
419480
* {@code crc_check_chance}
420481
*/
421482
CRC_CHECK_CHANCE("crc_check_chance", Double.class, true, false, false);
422483

423-
private Option delegate;
484+
private final Option delegate;
424485

425486
CompressionOption(String name, Class<?> type, boolean requiresValue, boolean escapesValue, boolean quotesValue) {
426487
this.delegate = new DefaultOption(name, type, requiresValue, escapesValue, quotesValue);

0 commit comments

Comments
 (0)