Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.scalar.db.sql.common.metadata.TableMetadataImpl Maven / Gradle / Ivy
package com.scalar.db.sql.common.metadata;
import com.google.common.base.MoreObjects;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.scalar.db.api.Scan;
import com.scalar.db.sql.ClusteringOrder;
import com.scalar.db.sql.DataType;
import com.scalar.db.sql.metadata.ColumnMetadata;
import com.scalar.db.sql.metadata.IndexMetadata;
import com.scalar.db.sql.metadata.TableMetadata;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Function;
import javax.annotation.concurrent.Immutable;
@Immutable
public class TableMetadataImpl implements TableMetadata {
private final String namespaceName;
private final String name;
private final ImmutableList primaryKey;
private final ImmutableList partitionKey;
private final ImmutableSet partitionKeyColumnNames;
private final ImmutableMap clusteringKey;
private final ImmutableSet clusteringKeyColumnNames;
private final ImmutableMap columns;
private final ImmutableMap indexes;
private TableMetadataImpl(
String namespaceName, String name, com.scalar.db.api.TableMetadata tableMetadata) {
this.namespaceName = Objects.requireNonNull(namespaceName);
this.name = Objects.requireNonNull(name);
Objects.requireNonNull(tableMetadata);
partitionKey =
tableMetadata.getPartitionKeyNames().stream()
.map(
c ->
ColumnMetadataImpl.create(
namespaceName,
name,
c,
convertDataType(tableMetadata.getColumnDataType(c)),
false))
.collect(ImmutableList.toImmutableList());
partitionKeyColumnNames =
partitionKey.stream().map(ColumnMetadata::getName).collect(ImmutableSet.toImmutableSet());
clusteringKey =
tableMetadata.getClusteringKeyNames().stream()
.map(
c ->
ColumnMetadataImpl.create(
namespaceName,
name,
c,
convertDataType(tableMetadata.getColumnDataType(c)),
false))
.collect(
ImmutableMap.toImmutableMap(
Function.identity(),
c -> convertClusteringOrder(tableMetadata.getClusteringOrder(c.getName()))));
clusteringKeyColumnNames =
clusteringKey.keySet().stream()
.map(ColumnMetadata::getName)
.collect(ImmutableSet.toImmutableSet());
primaryKey =
ImmutableList.builder()
.addAll(partitionKey)
.addAll(clusteringKey.keySet())
.build();
columns =
tableMetadata.getColumnNames().stream()
.map(
c ->
ColumnMetadataImpl.create(
namespaceName,
name,
c,
convertDataType(tableMetadata.getColumnDataType(c)),
tableMetadata.getEncryptedColumnNames().contains(c)))
.collect(ImmutableMap.toImmutableMap(ColumnMetadata::getName, Function.identity()));
indexes =
tableMetadata.getSecondaryIndexNames().stream()
.collect(
ImmutableMap.toImmutableMap(
Function.identity(), c -> IndexMetadataImpl.create(namespaceName, name, c)));
}
private TableMetadataImpl(
String namespaceName,
String name,
List partitionKeyColumnNames,
List clusteringKeyColumnNames,
Map clusteringOrders,
List columns,
List indexes) {
this.namespaceName = Objects.requireNonNull(namespaceName);
this.name = Objects.requireNonNull(name);
Objects.requireNonNull(partitionKeyColumnNames);
Objects.requireNonNull(clusteringKeyColumnNames);
this.columns =
Objects.requireNonNull(columns).stream()
.collect(ImmutableMap.toImmutableMap(ColumnMetadata::getName, Function.identity()));
partitionKey =
partitionKeyColumnNames.stream()
.map(this.columns::get)
.collect(ImmutableList.toImmutableList());
this.partitionKeyColumnNames = ImmutableSet.copyOf(partitionKeyColumnNames);
clusteringKey =
clusteringKeyColumnNames.stream()
.collect(
ImmutableMap.toImmutableMap(
this.columns::get, c -> clusteringOrders.getOrDefault(c, ClusteringOrder.ASC)));
this.clusteringKeyColumnNames = ImmutableSet.copyOf(clusteringKeyColumnNames);
primaryKey =
ImmutableList.builder()
.addAll(partitionKey)
.addAll(clusteringKey.keySet())
.build();
this.indexes =
Objects.requireNonNull(indexes).stream()
.collect(
ImmutableMap.toImmutableMap(IndexMetadata::getColumnName, Function.identity()));
}
private DataType convertDataType(com.scalar.db.io.DataType dataType) {
switch (dataType) {
case BOOLEAN:
return DataType.BOOLEAN;
case INT:
return DataType.INT;
case BIGINT:
return DataType.BIGINT;
case FLOAT:
return DataType.FLOAT;
case DOUBLE:
return DataType.DOUBLE;
case TEXT:
return DataType.TEXT;
case BLOB:
return DataType.BLOB;
default:
throw new AssertionError();
}
}
private ClusteringOrder convertClusteringOrder(Scan.Ordering.Order order) {
switch (order) {
case ASC:
return ClusteringOrder.ASC;
case DESC:
return ClusteringOrder.DESC;
default:
throw new AssertionError();
}
}
@Override
public String getNamespaceName() {
return namespaceName;
}
@Override
public String getName() {
return name;
}
@Override
public List getPrimaryKey() {
return primaryKey;
}
@Override
public boolean isPrimaryKeyColumn(String columnName) {
return partitionKeyColumnNames.contains(columnName)
|| clusteringKeyColumnNames.contains(columnName);
}
@Override
public List getPartitionKey() {
return partitionKey;
}
@Override
public boolean isPartitionKeyColumn(String columnName) {
return partitionKeyColumnNames.contains(columnName);
}
@Override
public Map getClusteringKey() {
return clusteringKey;
}
@Override
public boolean isClusteringKeyColumn(String columnName) {
return clusteringKeyColumnNames.contains(columnName);
}
@Override
public Map getColumns() {
return columns;
}
@Override
public Optional getColumn(String columnName) {
return Optional.ofNullable(columns.get(columnName));
}
@Override
public Map getIndexes() {
return indexes;
}
@Override
public Optional getIndex(String columnName) {
return Optional.ofNullable(indexes.get(columnName));
}
@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("namespaceName", namespaceName)
.add("name", name)
.add("primaryKey", primaryKey)
.add("partitionKey", partitionKey)
.add("clusteringKey", clusteringKey)
.add("columns", columns)
.add("indexes", indexes)
.toString();
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof TableMetadataImpl)) {
return false;
}
TableMetadataImpl that = (TableMetadataImpl) o;
return Objects.equals(namespaceName, that.namespaceName)
&& Objects.equals(name, that.name)
&& Objects.equals(primaryKey, that.primaryKey)
&& Objects.equals(partitionKey, that.partitionKey)
&& Objects.equals(clusteringKey, that.clusteringKey)
&& Objects.equals(columns, that.columns)
&& Objects.equals(indexes, that.indexes);
}
@Override
public int hashCode() {
return Objects.hash(
namespaceName, name, primaryKey, partitionKey, clusteringKey, columns, indexes);
}
public static TableMetadata create(
String namespaceName, String name, com.scalar.db.api.TableMetadata tableMetadata) {
return new TableMetadataImpl(namespaceName, name, tableMetadata);
}
public static TableMetadata create(
String namespaceName,
String name,
List partitionKeyColumnNames,
List clusteringKeyColumnNames,
Map clusteringOrders,
List columns,
List indexes) {
return new TableMetadataImpl(
namespaceName,
name,
partitionKeyColumnNames,
clusteringKeyColumnNames,
clusteringOrders,
columns,
indexes);
}
}