All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.scalar.db.sql.common.ColumnDefinitionsImpl Maven / Gradle / Ivy

There is a newer version: 3.14.0
Show newest version
package com.scalar.db.sql.common;

import com.google.common.base.Suppliers;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.scalar.db.sql.ColumnDefinition;
import com.scalar.db.sql.ColumnDefinitions;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.function.Supplier;
import javax.annotation.concurrent.Immutable;

@Immutable
public class ColumnDefinitionsImpl implements ColumnDefinitions {

  private final Supplier> columnDefinitionsMap;
  private final Supplier> columnDefinitionsList;
  private final Supplier> columnNames;

  ColumnDefinitionsImpl(Collection columnDefinitions) {
    Objects.requireNonNull(columnDefinitions);
    columnDefinitionsMap = Suppliers.memoize(() -> createColumnDefinitionsMap(columnDefinitions));
    columnDefinitionsList = Suppliers.memoize(() -> ImmutableList.copyOf(columnDefinitions));
    columnNames =
        Suppliers.memoize(
            () -> {
              ImmutableSet.Builder builder = ImmutableSet.builder();
              for (ColumnDefinition columnDefinition : columnDefinitions) {
                String columnName = columnDefinition.getColumnName();
                if (columnDefinition.getTableName().isPresent()) {
                  columnName = columnDefinition.getTableName().get() + "." + columnName;
                }
                if (columnDefinition.getNamespaceName().isPresent()) {
                  columnName = columnDefinition.getNamespaceName().get() + "." + columnName;
                }
                builder.add(columnName);
              }
              return builder.build();
            });
  }

  private ImmutableMap createColumnDefinitionsMap(
      Collection columnDefinitions) {
    Map map = new HashMap<>();
    for (ColumnDefinition columnDefinition : columnDefinitions) {

      // Put an entry for a column name to a column object
      map.putIfAbsent(columnDefinition.getColumnName(), columnDefinition);

      // Put an entry for a table name and a column name to a column object if a table name exists
      columnDefinition
          .getTableName()
          .ifPresent(
              t -> map.putIfAbsent(t + "." + columnDefinition.getColumnName(), columnDefinition));

      // Put an entry for a namespace name, a table name, and a column name to a column object if a
      // namespace name exists
      columnDefinition
          .getNamespaceName()
          .ifPresent(
              n -> {
                assert columnDefinition.getTableName().isPresent();
                map.putIfAbsent(
                    n
                        + "."
                        + columnDefinition.getTableName().get()
                        + "."
                        + columnDefinition.getColumnName(),
                    columnDefinition);
              });
    }
    return ImmutableMap.copyOf(map);
  }

  @Override
  public ColumnDefinition getColumnDefinition(String columnName) {
    if (!contains(columnName)) {
      throw new IllegalArgumentException(SqlError.COLUMN_NOT_FOUND.buildMessage(columnName));
    }
    return columnDefinitionsMap.get().get(columnName);
  }

  @Override
  public ColumnDefinition getColumnDefinition(int i) {
    return columnDefinitionsList.get().get(i);
  }

  @Override
  public boolean contains(String columnName) {
    return columnDefinitionsMap.get().containsKey(columnName);
  }

  @Override
  public Set getColumnNames() {
    return columnNames.get();
  }

  @Override
  public int size() {
    return columnDefinitionsList.get().size();
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy