Skip to content

Commit

Permalink
Remove setters of Cosmos marshalling objects
Browse files Browse the repository at this point in the history
  • Loading branch information
Torch3333 committed Aug 15, 2023
1 parent 23f2dbc commit f34a7aa
Show file tree
Hide file tree
Showing 7 changed files with 209 additions and 182 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import com.google.common.collect.ImmutableList;
import com.scalar.db.config.DatabaseConfig;
import com.scalar.db.util.AdminTestUtils;
import java.util.Collections;
import java.util.Properties;

public class CosmosAdminTestUtils extends AdminTestUtils {
Expand Down Expand Up @@ -65,9 +66,14 @@ record ->

@Override
public void corruptMetadata(String namespace, String table) {
CosmosTableMetadata corruptedMetadata = new CosmosTableMetadata();
corruptedMetadata.setId(getFullTableName(namespace, table));
corruptedMetadata.setPartitionKeyNames(ImmutableList.of("corrupted"));
CosmosTableMetadata corruptedMetadata =
new CosmosTableMetadata(
getFullTableName(namespace, table),
ImmutableList.of("corrupted"),
Collections.emptyList(),
Collections.emptyMap(),
Collections.emptySet(),
Collections.emptyMap());

CosmosContainer container =
client.getDatabase(metadataDatabase).getContainer(CosmosAdmin.METADATA_CONTAINER);
Expand Down
19 changes: 9 additions & 10 deletions core/src/main/java/com/scalar/db/storage/cosmos/CosmosAdmin.java
Original file line number Diff line number Diff line change
Expand Up @@ -243,24 +243,23 @@ private CosmosContainer getMetadataContainer() {

private CosmosTableMetadata convertToCosmosTableMetadata(
String fullTableName, TableMetadata tableMetadata) {
CosmosTableMetadata cosmosTableMetadata = new CosmosTableMetadata();
cosmosTableMetadata.setId(fullTableName);
cosmosTableMetadata.setPartitionKeyNames(new ArrayList<>(tableMetadata.getPartitionKeyNames()));
cosmosTableMetadata.setClusteringKeyNames(
new ArrayList<>(tableMetadata.getClusteringKeyNames()));
cosmosTableMetadata.setClusteringOrders(
Map<String, String> clusteringOrders =
tableMetadata.getClusteringKeyNames().stream()
.collect(Collectors.toMap(c -> c, c -> tableMetadata.getClusteringOrder(c).name())));
cosmosTableMetadata.setSecondaryIndexNames(tableMetadata.getSecondaryIndexNames());
.collect(Collectors.toMap(c -> c, c -> tableMetadata.getClusteringOrder(c).name()));
Map<String, String> columnTypeByName = new HashMap<>();
tableMetadata
.getColumnNames()
.forEach(
columnName ->
columnTypeByName.put(
columnName, tableMetadata.getColumnDataType(columnName).name().toLowerCase()));
cosmosTableMetadata.setColumns(columnTypeByName);
return cosmosTableMetadata;
return new CosmosTableMetadata(
fullTableName,
new ArrayList<>(tableMetadata.getPartitionKeyNames()),
new ArrayList<>(tableMetadata.getClusteringKeyNames()),
clusteringOrders,
tableMetadata.getSecondaryIndexNames(),
columnTypeByName);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,20 +53,18 @@ public MutationType getMutationType() {
@Nonnull
public Record makeRecord() {
Mutation mutation = (Mutation) getOperation();
Record record = new Record();

if (mutation instanceof Delete) {
return record;
return new Record();
}
Put put = (Put) mutation;

record.setId(getId());
record.setConcatenatedPartitionKey(getConcatenatedPartitionKey());
record.setPartitionKey(toMap(put.getPartitionKey().getColumns()));
put.getClusteringKey().ifPresent(k -> record.setClusteringKey(toMap(k.getColumns())));
record.setValues(toMapForPut(put));

return record;
return new Record(
getId(),
getConcatenatedPartitionKey(),
toMap(put.getPartitionKey().getColumns()),
put.getClusteringKey().map(k -> toMap(k.getColumns())).orElse(null),
toMapForPut(put));
}

@Nonnull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,46 +7,44 @@
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import javax.annotation.concurrent.NotThreadSafe;
import javax.annotation.concurrent.ThreadSafe;

/**
* A metadata class for a table of ScalarDB to know the type of each column
*
* @author Yuji Ito
*/
@SuppressFBWarnings({"EI_EXPOSE_REP", "EI_EXPOSE_REP2"})
@NotThreadSafe
@ThreadSafe
public class CosmosTableMetadata {
private String id;
private LinkedHashSet<String> partitionKeyNames;
private LinkedHashSet<String> clusteringKeyNames;
private Map<String, String> clusteringOrders;
private Set<String> secondaryIndexNames;
private Map<String, String> columns;

public CosmosTableMetadata() {}

public void setId(String id) {
this.id = id;
private final String id;
private final LinkedHashSet<String> partitionKeyNames;
private final LinkedHashSet<String> clusteringKeyNames;
private final Map<String, String> clusteringOrders;
private final Set<String> secondaryIndexNames;
private final Map<String, String> columns;

public CosmosTableMetadata() {
this.id = null;
this.partitionKeyNames = null;
this.clusteringKeyNames = null;
this.clusteringOrders = null;
this.secondaryIndexNames = null;
this.columns = null;
}

public void setPartitionKeyNames(List<String> partitionKeyNames) {
public CosmosTableMetadata(
String id,
List<String> partitionKeyNames,
List<String> clusteringKeyNames,
Map<String, String> clusteringOrders,
Set<String> secondaryIndexNames,
Map<String, String> columns) {
this.id = id;
this.partitionKeyNames = new LinkedHashSet<>(partitionKeyNames);
}

public void setClusteringKeyNames(List<String> clusteringKeyNames) {
this.clusteringKeyNames = new LinkedHashSet<>(clusteringKeyNames);
}

public void setClusteringOrders(Map<String, String> clusteringOrders) {
this.clusteringOrders = clusteringOrders;
}

public void setSecondaryIndexNames(Set<String> secondaryIndexNames) {
this.secondaryIndexNames = secondaryIndexNames;
}

public void setColumns(Map<String, String> columns) {
this.columns = columns;
}

Expand Down
52 changes: 26 additions & 26 deletions core/src/main/java/com/scalar/db/storage/cosmos/Record.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,42 +4,42 @@
import java.util.Collections;
import java.util.Map;
import java.util.Objects;
import javax.annotation.concurrent.NotThreadSafe;
import javax.annotation.concurrent.ThreadSafe;

/**
* A record class that Cosmos DB uses for storing a document based on ScalarDB data model.
*
* @author Yuji Ito
*/
@SuppressFBWarnings({"EI_EXPOSE_REP", "EI_EXPOSE_REP2"})
@NotThreadSafe
@ThreadSafe
public class Record {
private String id = "";
private String concatenatedPartitionKey = "";
private Map<String, Object> partitionKey = Collections.emptyMap();
private Map<String, Object> clusteringKey = Collections.emptyMap();
private Map<String, Object> values = Collections.emptyMap();

public Record() {}

public void setId(String id) {
this.id = id;
}

public void setConcatenatedPartitionKey(String concatenatedPartitionKey) {
this.concatenatedPartitionKey = concatenatedPartitionKey;
}

public void setPartitionKey(Map<String, Object> partitionKey) {
this.partitionKey = partitionKey;
}

public void setClusteringKey(Map<String, Object> clusteringKey) {
this.clusteringKey = clusteringKey;
private final String id;
private final String concatenatedPartitionKey;
private final Map<String, Object> partitionKey;
private final Map<String, Object> clusteringKey;
private final Map<String, Object> values;

public Record() {
this.id = "";
this.concatenatedPartitionKey = "";
this.partitionKey = Collections.emptyMap();
this.clusteringKey = Collections.emptyMap();
this.values = Collections.emptyMap();
}

public void setValues(Map<String, Object> values) {
this.values = values;
public Record(
String id,
String concatenatedPartitionKey,
Map<String, Object> partitionKey,
Map<String, Object> clusteringKey,
Map<String, Object> values) {
this.id = id != null ? id : "";
this.concatenatedPartitionKey =
concatenatedPartitionKey != null ? concatenatedPartitionKey : "";
this.partitionKey = partitionKey != null ? partitionKey : Collections.emptyMap();
this.clusteringKey = clusteringKey != null ? clusteringKey : Collections.emptyMap();
this.values = values != null ? values : Collections.emptyMap();
}

public String getId() {
Expand Down
Loading

0 comments on commit f34a7aa

Please sign in to comment.