com.yandex.ydb.table.description.TableDescription Maven / Gradle / Ivy
package com.yandex.ydb.table.description;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Nullable;
import com.google.common.collect.ImmutableList;
import com.yandex.ydb.table.settings.PartitioningSettings;
import com.yandex.ydb.table.values.OptionalType;
import com.yandex.ydb.table.values.Type;
/**
* @author Sergey Polovko
*/
public class TableDescription {
private final List primaryKeys;
private final List columns;
private final List indexes;
private final List columnFamilies;
private final List keyRanges;
@Nullable
private final TableStats tableStats;
@Nullable
private final PartitioningSettings partitioningSettings;
private final List partitionStats;
private TableDescription(Builder builder) {
this.primaryKeys = ImmutableList.copyOf(builder.primaryKeys);
this.columns = builder.buildColumns();
this.indexes = ImmutableList.copyOf(builder.indexes);
this.columnFamilies = ImmutableList.copyOf(builder.families);
this.keyRanges = ImmutableList.copyOf(builder.keyRanges);
this.tableStats = builder.tableStats;
this.partitioningSettings = builder.partitioningSettings;
this.partitionStats = ImmutableList.copyOf(builder.partitionStats);
}
public static Builder newBuilder() {
return new Builder();
}
public List getPrimaryKeys() {
return primaryKeys;
}
public List getColumns() {
return columns;
}
public List getIndexes() {
return indexes;
}
@Nullable
public PartitioningSettings getPartitioningSettings() {
return partitioningSettings;
}
@Nullable
public TableStats getTableStats() {
return tableStats;
}
public List getPartitionStats() {
return partitionStats;
}
public List getColumnFamilies() {
return columnFamilies;
}
public List getKeyRanges() {
return keyRanges;
}
/**
* BUILDER
*/
public static class Builder {
private List primaryKeys = Collections.emptyList();
private final LinkedHashMap columns = new LinkedHashMap<>();
private final List indexes = new ArrayList<>();
private final List families = new ArrayList<>();
private final List keyRanges = new ArrayList<>();
private TableStats tableStats = null;
private PartitioningSettings partitioningSettings = null;
private final List partitionStats = new ArrayList<>();
public Builder addNonnullColumn(String name, Type type) {
return addNonnullColumn(name, type, null);
}
public Builder addNonnullColumn(String name, Type type, String family) {
columns.put(name, new TypeAndFamily(type, family));
return this;
}
public Builder addKeyRange(KeyRange value) {
keyRanges.add(value);
return this;
}
public Builder addNullableColumn(String name, Type type) {
return addNullableColumn(name, type, null);
}
public Builder addNullableColumn(String name, Type type, String family) {
columns.put(name, new TypeAndFamily(OptionalType.of(type), family));
return this;
}
public Builder setPrimaryKey(String name) {
checkColumnKnown(name);
primaryKeys = ImmutableList.of(name);
return this;
}
public Builder setPrimaryKeys(String... names) {
if (names.length == 1) {
return setPrimaryKey(names[0]);
}
HashSet keys = new HashSet<>(names.length);
for (String name : names) {
checkColumnKnown(name);
if (!keys.add(name)) {
throw new IllegalArgumentException("non unique primary column name: " + name);
}
}
this.primaryKeys = ImmutableList.copyOf(names);
return this;
}
public Builder setPrimaryKeys(List names) {
if (names.size() == 1) {
return setPrimaryKey(names.get(0));
}
HashSet keys = new HashSet<>(names.size());
for (String name : names) {
checkColumnKnown(name);
if (!keys.add(name)) {
throw new IllegalArgumentException("non unique primary column name: " + name);
}
}
this.primaryKeys = ImmutableList.copyOf(names);
return this;
}
public Builder addGlobalIndex(String name, List columns) {
indexes.add(new TableIndex(name, columns, TableIndex.Type.GLOBAL));
return this;
}
public Builder setTableStats(TableStats tableStats) {
this.tableStats = tableStats;
return this;
}
public Builder setPartitioningSettings(PartitioningSettings partitioningSettings) {
this.partitioningSettings = partitioningSettings;
return this;
}
public Builder addColumnFamily(ColumnFamily family) {
this.families.add(family);
return this;
}
public Builder addPartitionStat(long rows, long size) {
this.partitionStats.add(new PartitionStats(rows, size));
return this;
}
private List buildColumns() {
if (columns.isEmpty()) {
throw new IllegalStateException("cannot build table description with no columns");
}
int i = 0;
TableColumn[] array = new TableColumn[this.columns.size()];
for (Map.Entry e : this.columns.entrySet()) {
array[i++] = new TableColumn(e.getKey(), e.getValue().type, e.getValue().family);
}
return ImmutableList.copyOf(array);
}
public TableDescription build() {
return new TableDescription(this);
}
private void checkColumnKnown(String name) {
if (!columns.containsKey(name)) {
throw new IllegalArgumentException("unknown column name: " + name);
}
}
private static class TypeAndFamily {
private final Type type;
private final String family;
public TypeAndFamily(Type type, String family) {
this.type = type;
this.family = family;
}
}
}
public static class PartitionStats {
private final long rowsEstimate;
private final long storeSize;
public PartitionStats(long rowsEstimate, long storeSize) {
this.rowsEstimate = rowsEstimate;
this.storeSize = storeSize;
}
public long rowsEstimate() {
return this.rowsEstimate;
}
public long storeSize() {
return this.storeSize;
}
}
public static class TableStats {
@Nullable
private final Instant creationTime;
@Nullable
private final Instant modificationTime;
private final long rowsEstimate;
private final long storeSize;
public TableStats(@Nullable Instant creationTime, @Nullable Instant modificationTime, long rowsEstimate, long storeSize) {
this.creationTime = creationTime;
this.modificationTime = modificationTime;
this.rowsEstimate = rowsEstimate;
this.storeSize = storeSize;
}
@Nullable
public Instant getCreationTime() {
return creationTime;
}
@Nullable
public Instant getModificationTime() {
return modificationTime;
}
public long getRowsEstimate() {
return rowsEstimate;
}
public long getStoreSize() {
return storeSize;
}
}
}