com.scalar.db.common.ProjectedResult Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of scalardb Show documentation
Show all versions of scalardb Show documentation
A universal transaction manager that achieves database-agnostic transactions and distributed transactions that span multiple databases
The newest version!
package com.scalar.db.common;
import com.google.common.collect.ImmutableSet;
import com.scalar.db.api.Result;
import com.scalar.db.common.error.CoreError;
import com.scalar.db.io.Column;
import com.scalar.db.io.Key;
import java.nio.ByteBuffer;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;
/** An implementation of {@code Result} that only includes projected columns. */
@Immutable
public class ProjectedResult extends AbstractResult {
private final Result original;
private final ImmutableSet containedColumnNames;
public ProjectedResult(Result original, List projections) {
this.original = Objects.requireNonNull(original);
ImmutableSet.Builder builder = ImmutableSet.builder();
original.getContainedColumnNames().stream()
.filter(c -> projections.isEmpty() || projections.contains(c))
.forEach(builder::add);
containedColumnNames = builder.build();
}
/** @deprecated As of release 3.8.0. Will be removed in release 5.0.0 */
@Deprecated
@Override
public Optional getPartitionKey() {
Optional partitionKey = original.getPartitionKey();
checkKey(partitionKey);
return partitionKey;
}
/** @deprecated As of release 3.8.0. Will be removed in release 5.0.0 */
@Deprecated
@Override
public Optional getClusteringKey() {
Optional clusteringKey = original.getClusteringKey();
checkKey(clusteringKey);
return clusteringKey;
}
private void checkKey(Optional key) {
if (!key.isPresent()) {
return;
}
for (Column> column : key.get().getColumns()) {
if (!containedColumnNames.contains(column.getName())) {
throw new IllegalStateException(CoreError.COLUMN_NOT_FOUND.buildMessage(column.getName()));
}
}
}
@Override
public boolean isNull(String columnName) {
checkIfExists(columnName);
return original.isNull(columnName);
}
@Override
public boolean getBoolean(String columnName) {
checkIfExists(columnName);
return original.getBoolean(columnName);
}
@Override
public int getInt(String columnName) {
checkIfExists(columnName);
return original.getInt(columnName);
}
@Override
public long getBigInt(String columnName) {
checkIfExists(columnName);
return original.getBigInt(columnName);
}
@Override
public float getFloat(String columnName) {
checkIfExists(columnName);
return original.getFloat(columnName);
}
@Override
public double getDouble(String columnName) {
checkIfExists(columnName);
return original.getDouble(columnName);
}
@Nullable
@Override
public String getText(String columnName) {
checkIfExists(columnName);
return original.getText(columnName);
}
@Nullable
@Override
public ByteBuffer getBlobAsByteBuffer(String columnName) {
checkIfExists(columnName);
return original.getBlobAsByteBuffer(columnName);
}
@Nullable
@Override
public byte[] getBlobAsBytes(String columnName) {
checkIfExists(columnName);
return original.getBlobAsBytes(columnName);
}
@Nullable
@Override
public Object getAsObject(String columnName) {
checkIfExists(columnName);
return original.getAsObject(columnName);
}
@Override
public boolean contains(String columnName) {
return containedColumnNames.contains(columnName);
}
@Override
public Set getContainedColumnNames() {
return containedColumnNames;
}
@Override
public Map> getColumns() {
return original.getColumns().entrySet().stream()
.filter(e -> containedColumnNames.contains(e.getKey()))
.collect(Collectors.toMap(Entry::getKey, Entry::getValue));
}
}