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

com.scalar.db.sql.common.ColumnsRecord 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.scalar.db.io.Column;
import com.scalar.db.sql.ColumnDefinition;
import com.scalar.db.sql.ColumnDefinitions;
import com.scalar.db.sql.Record;
import java.nio.ByteBuffer;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Objects;
import java.util.function.Supplier;
import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;

@Immutable
public class ColumnsRecord implements Record {

  private final Supplier>> columnsMap;
  private final Supplier>> columnsList;

  /**
   * Constructs a {@code ColumnsRecord} instance with the specified columns and column definitions.
   * Note that the order of columns in the list must be the same as the order of columns in the list
   * of column definitions.
   *
   * @param columns columns of a record
   * @param columnDefinitions column definitions of a record
   */
  public ColumnsRecord(Collection> columns, ColumnDefinitions columnDefinitions) {
    Objects.requireNonNull(columns);
    columnsMap = Suppliers.memoize(() -> createColumnsMap(columns, columnDefinitions));
    columnsList = Suppliers.memoize(() -> ImmutableList.copyOf(columns));
  }

  private ImmutableMap> createColumnsMap(
      Collection> columns, ColumnDefinitions columnDefinitions) {
    Map> map = new HashMap<>();
    Iterator columnDefinitionIterator = columnDefinitions.iterator();
    for (Column column : Objects.requireNonNull(columns)) {
      ColumnDefinition columnDefinition = columnDefinitionIterator.next();

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

      // 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(), column));

      // 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(),
                    column);
              });
    }
    return ImmutableMap.copyOf(map);
  }

  @Override
  public boolean isNull(String columnName) {
    Column column = getColumnByName(columnName);
    return column.hasNullValue();
  }

  @Override
  public boolean isNull(int i) {
    Column column = getColumnByIndex(i);
    return column.hasNullValue();
  }

  @Override
  public boolean getBoolean(String columnName) {
    Column column = getColumnByName(columnName);
    return column.getBooleanValue();
  }

  @Override
  public boolean getBoolean(int i) {
    Column column = getColumnByIndex(i);
    return column.getBooleanValue();
  }

  @Override
  public int getInt(String columnName) {
    Column column = getColumnByName(columnName);
    return column.getIntValue();
  }

  @Override
  public int getInt(int i) {
    Column column = getColumnByIndex(i);
    return column.getIntValue();
  }

  @Override
  public long getBigInt(String columnName) {
    Column column = getColumnByName(columnName);
    return column.getBigIntValue();
  }

  @Override
  public long getBigInt(int i) {
    Column column = getColumnByIndex(i);
    return column.getBigIntValue();
  }

  @Override
  public float getFloat(String columnName) {
    Column column = getColumnByName(columnName);
    return column.getFloatValue();
  }

  @Override
  public float getFloat(int i) {
    Column column = getColumnByIndex(i);
    return column.getFloatValue();
  }

  @Override
  public double getDouble(String columnName) {
    Column column = getColumnByName(columnName);
    return column.getDoubleValue();
  }

  @Override
  public double getDouble(int i) {
    Column column = getColumnByIndex(i);
    return column.getDoubleValue();
  }

  @Nullable
  @Override
  public String getText(String columnName) {
    Column column = getColumnByName(columnName);
    return column.getTextValue();
  }

  @Nullable
  @Override
  public String getText(int i) {
    Column column = getColumnByIndex(i);
    return column.getTextValue();
  }

  @Nullable
  @Override
  public ByteBuffer getBlobAsByteBuffer(String columnName) {
    Column column = getColumnByName(columnName);
    return column.getBlobValueAsByteBuffer();
  }

  @Nullable
  @Override
  public ByteBuffer getBlobAsByteBuffer(int i) {
    Column column = getColumnByIndex(i);
    return column.getBlobValueAsByteBuffer();
  }

  @Nullable
  @Override
  public byte[] getBlobAsBytes(String columnName) {
    Column column = getColumnByName(columnName);
    return column.getBlobValueAsBytes();
  }

  @Nullable
  @Override
  public byte[] getBlobAsBytes(int i) {
    Column column = getColumnByIndex(i);
    return column.getBlobValueAsBytes();
  }

  @Nullable
  @Override
  public Object getAsObject(String columnName) {
    Column column = getColumnByName(columnName);
    return column.getValueAsObject();
  }

  @Nullable
  @Override
  public Object getAsObject(int i) {
    Column column = getColumnByIndex(i);
    return column.getValueAsObject();
  }

  private Column getColumnByName(String columnName) {
    if (!columnsMap.get().containsKey(columnName)) {
      throw new IllegalArgumentException(SqlError.COLUMN_NOT_FOUND.buildMessage(columnName));
    }
    return columnsMap.get().get(columnName);
  }

  private Column getColumnByIndex(int i) {
    return columnsList.get().get(i);
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy