Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
smiklosovic committed Apr 3, 2024
1 parent 4559ba3 commit 718a625
Show file tree
Hide file tree
Showing 10 changed files with 37 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,7 @@ public KeyspaceMetadata apply(Epoch epoch, KeyspaceMetadata keyspace, TableMetad
{
attrs.validate(keyspaceName);

TableParams params = attrs.asAlteredTableParams(table.params, keyspaceName);
TableParams params = attrs.asAlteredTableParams(table.params);

if (table.isCounter() && params.defaultTimeToLive > 0)
throw ire("Cannot set default_time_to_live on a table with counters");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public Keyspaces apply(ClusterMetadata metadata)
// Guardrails on table properties
Guardrails.tableProperties.guard(attrs.updatedProperties(), attrs::removeProperty, state);

TableParams params = attrs.asAlteredTableParams(view.metadata.params, keyspaceName);
TableParams params = attrs.asAlteredTableParams(view.metadata.params);

if (params.gcGraceSeconds == 0)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,19 +57,19 @@ public final class TableAttributes extends PropertyDefinitions
public void validate(String keyspace)
{
validate(validKeywords, obsoleteKeywords);
build(TableParams.builder(), keyspace).validate();
partiallyBuild(TableParams.builder()).setDefaultCompressionIfNotSet(keyspace).build().validate();
}

TableParams asNewTableParams(String keyspace)
{
return build(TableParams.builder(), keyspace);
return partiallyBuild(TableParams.builder()).setDefaultCompressionIfNotSet(keyspace).build();
}

TableParams asAlteredTableParams(TableParams previous, String keyspaceName)
TableParams asAlteredTableParams(TableParams previous)
{
if (getId() != null)
throw new ConfigurationException("Cannot alter table id.");
return build(previous.unbuild(), keyspaceName);
return build(previous.unbuild());
}

public TableId getId() throws ConfigurationException
Expand All @@ -95,7 +95,12 @@ public static Set<String> allKeywords()
return Sets.union(validKeywords, obsoleteKeywords);
}

private TableParams build(TableParams.Builder builder, String keyspace)
private TableParams build(TableParams.Builder builder)
{
return partiallyBuild(builder).build();
}

private TableParams.Builder partiallyBuild(TableParams.Builder builder)
{
if (hasOption(ALLOW_AUTO_SNAPSHOT))
builder.allowAutoSnapshot(getBoolean(ALLOW_AUTO_SNAPSHOT.toString(), true));
Expand Down Expand Up @@ -151,7 +156,7 @@ private TableParams build(TableParams.Builder builder, String keyspace)
if (hasOption(READ_REPAIR))
builder.readRepair(ReadRepairStrategy.fromString(getString(READ_REPAIR)));

return builder.build(keyspace);
return builder;
}

public boolean hasOption(Option option)
Expand Down
2 changes: 1 addition & 1 deletion src/java/org/apache/cassandra/db/ColumnFamilyStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -3006,7 +3006,7 @@ public void setCrcCheckChance(double crcCheckChance)
{
try
{
TableParams.builder().crcCheckChance(crcCheckChance).build(keyspace.getName()).validate();
TableParams.builder().crcCheckChance(crcCheckChance).build().validate();
for (ColumnFamilyStore cfs : concatWithIndexes())
{
cfs.crcCheckChance.set(crcCheckChance);
Expand Down
2 changes: 1 addition & 1 deletion src/java/org/apache/cassandra/schema/SchemaKeyspace.java
Original file line number Diff line number Diff line change
Expand Up @@ -1050,7 +1050,7 @@ static TableParams createTableParamsFromRow(UntypedResultSet.Row row)
if (row.has("incremental_backups"))
builder.incrementalBackups(row.getBoolean("incremental_backups"));

return builder.build(row.getString("keyspace_name"));
return builder.build();
}

private static List<ColumnMetadata> fetchColumns(String keyspace, String table, Types types, UserFunctions functions)
Expand Down
13 changes: 7 additions & 6 deletions src/java/org/apache/cassandra/schema/TableMetadata.java
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ protected TableMetadata(Builder builder)
epoch = builder.epoch;
partitioner = builder.partitioner;
kind = builder.kind;
params = builder.params.build(keyspace);
params = builder.params.setDefaultCompressionIfNotSet(keyspace).build();

indexName = kind == Kind.INDEX ? name.substring(name.indexOf('.') + 1) : null;

Expand Down Expand Up @@ -665,9 +665,9 @@ public TableMetadata updateIndexTableMetadata(TableParams baseTableParams)
builder.caching(baseTableParams.caching.cacheKeys() ? CachingParams.CACHE_KEYS : CachingParams.CACHE_NOTHING);

Builder unbuilt = unbuild();
String keyspace = unbuilt.keyspace;
builder.setDefaultCompressionIfNotSet(unbuilt.keyspace);

return unbuilt.params(builder.build(keyspace)).build();
return unbuilt.params(builder.build()).build();
}

boolean referencesUserType(ByteBuffer name)
Expand Down Expand Up @@ -1773,7 +1773,7 @@ public void serialize(TableMetadata t, DataOutputPlus out, Version version) thro
t.id.serialize(out);
out.writeUTF(t.partitioner.getClass().getCanonicalName());
out.writeUTF(t.kind.name());
new TableParams.Serializer(t.keyspace).serialize(t.params, out, version);
TableParams.serializer.serialize(t.params, out, version);

out.writeInt(t.flags.size());
for (Flag f : t.flags)
Expand Down Expand Up @@ -1809,7 +1809,8 @@ public TableMetadata deserialize(DataInputPlus in, Types types, UserFunctions fu
builder.epoch(epoch);
builder.partitioner(FBUtilities.newPartitioner(in.readUTF()));
builder.kind(Kind.valueOf(in.readUTF()));
builder.params(new TableParams.Serializer(ks).deserialize(in, version));
builder.params(TableParams.serializer.deserialize(in, version));
builder.params.setDefaultCompressionIfNotSet(ks);
int flagCount = in.readInt();
Set<Flag> flags = new HashSet<>();
for (int i = 0; i < flagCount; i++)
Expand All @@ -1835,7 +1836,7 @@ public long serializedSize(TableMetadata t, Version version)
t.id.serializedSize() +
sizeof(t.partitioner.getClass().getCanonicalName()) +
sizeof(t.kind.name()) +
new TableParams.Serializer(t.keyspace).serializedSize(t.params, version);
TableParams.serializer.serializedSize(t.params, version);

size += sizeof(t.epoch != null);
if (t.epoch != null)
Expand Down
21 changes: 11 additions & 10 deletions src/java/org/apache/cassandra/schema/TableParams.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@

public final class TableParams
{
public static final Serializer serializer = new Serializer();
public enum Option
{
ALLOW_AUTO_SNAPSHOT,
Expand Down Expand Up @@ -366,11 +367,8 @@ public Builder()
{
}

public TableParams build(String keyspace)
public TableParams build()
{
if (compression == null)
compression = CompressionParams.defaultParams(keyspace);

return new TableParams(this);
}

Expand Down Expand Up @@ -470,6 +468,14 @@ public Builder compression(CompressionParams val)
return this;
}

public Builder setDefaultCompressionIfNotSet(String keyspace)
{
if (compression == null)
compression = CompressionParams.defaultParams(keyspace);

return this;
}

public Builder cdc(boolean val)
{
cdc = val;
Expand All @@ -491,11 +497,6 @@ public Builder extensions(Map<String, ByteBuffer> val)

public static class Serializer implements MetadataSerializer<TableParams>
{
private final String keyspace;
Serializer(String keyspace) {
this.keyspace = keyspace;
}

public void serialize(TableParams t, DataOutputPlus out, Version version) throws IOException
{
out.writeUTF(t.comment);
Expand Down Expand Up @@ -538,7 +539,7 @@ public TableParams deserialize(DataInputPlus in, Version version) throws IOExcep
.extensions(deserializeMapBB(in))
.cdc(in.readBoolean())
.readRepair(ReadRepairStrategy.fromString(in.readUTF()));
return builder.build(keyspace);
return builder.build();
}

public long serializedSize(TableParams t, Version version)
Expand Down
4 changes: 2 additions & 2 deletions test/unit/org/apache/cassandra/db/ReadCommandTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -878,7 +878,7 @@ public void testRepairedDataOverreadMetrics()
cfs.disableAutoCompaction();
cfs.metadata().withSwapped(cfs.metadata().params.unbuild()
.caching(CachingParams.CACHE_NOTHING)
.build(KEYSPACE));
.build());
// Insert and repair
insert(cfs, IntStream.range(0, 10), () -> IntStream.range(0, 10));
Util.flush(cfs);
Expand Down Expand Up @@ -929,7 +929,7 @@ public void testRepairedDataOverreadMetrics()

private void setGCGrace(ColumnFamilyStore cfs, int gcGrace)
{
TableParams newParams = cfs.metadata().params.unbuild().gcGraceSeconds(gcGrace).build(cfs.keyspace.getName());
TableParams newParams = cfs.metadata().params.unbuild().gcGraceSeconds(gcGrace).build();
KeyspaceMetadata keyspaceMetadata = Schema.instance.getKeyspaceMetadata(cfs.metadata().keyspace);
SchemaTestUtil.addOrUpdateKeyspace(
keyspaceMetadata.withSwapped(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public static void setUpClass() throws Throwable
SchemaLoader.loadSchema();
SchemaLoader.schemaDefinition("PaxosStateTest");
metadata = Keyspace.open("PaxosStateTestKeyspace1").getColumnFamilyStore("Standard1").metadata.get();
metadata.withSwapped(metadata.params.unbuild().gcGraceSeconds(3600).build("PaxosStateTestKeyspace1"));
metadata.withSwapped(metadata.params.unbuild().gcGraceSeconds(3600).build());
}

@AfterClass
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ public TableMetadata build(RandomnessSource rnd)
.partitioner(PARTITIONER_GEN.generate(rnd))
.kind(tableKindGen.generate(rnd))
.isCounter(BOOLEAN_GEN.generate(rnd))
.params(params.build(ks));
.params(params.setDefaultCompressionIfNotSet(ks).build());

int numPartitionColumns = numPartitionColumnsGen.generate(rnd);
int numClusteringColumns = numClusteringColumnsGen.generate(rnd);
Expand Down

0 comments on commit 718a625

Please sign in to comment.