Skip to content

Commit

Permalink
Use Optional instead of null in Raptor handles
Browse files Browse the repository at this point in the history
  • Loading branch information
electrum committed Aug 27, 2015
1 parent 0901dd7 commit 13d9ac6
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 40 deletions.
Expand Up @@ -20,9 +20,8 @@
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.collect.ImmutableList;

import javax.annotation.Nullable;

import java.util.List;
import java.util.Optional;

import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
Expand All @@ -34,8 +33,7 @@ public class RaptorInsertTableHandle
private final long tableId;
private final List<RaptorColumnHandle> columnHandles;
private final List<Type> columnTypes;
@Nullable
private final String externalBatchId;
private final Optional<String> externalBatchId;
private final List<RaptorColumnHandle> sortColumnHandles;
private final List<SortOrder> sortOrders;

Expand All @@ -45,7 +43,7 @@ public RaptorInsertTableHandle(
@JsonProperty("tableId") long tableId,
@JsonProperty("columnHandles") List<RaptorColumnHandle> columnHandles,
@JsonProperty("columnTypes") List<Type> columnTypes,
@JsonProperty("externalBatchId") @Nullable String externalBatchId,
@JsonProperty("externalBatchId") Optional<String> externalBatchId,
@JsonProperty("sortColumnHandles") List<RaptorColumnHandle> sortColumnHandles,
@JsonProperty("sortOrders") List<SortOrder> sortOrders)
{
Expand All @@ -55,7 +53,7 @@ public RaptorInsertTableHandle(
this.tableId = tableId;
this.columnHandles = ImmutableList.copyOf(checkNotNull(columnHandles, "columnHandles is null"));
this.columnTypes = ImmutableList.copyOf(checkNotNull(columnTypes, "columnTypes is null"));
this.externalBatchId = externalBatchId;
this.externalBatchId = checkNotNull(externalBatchId, "externalBatchId is null");

this.sortOrders = ImmutableList.copyOf(checkNotNull(sortOrders, "sortOrders is null"));
this.sortColumnHandles = ImmutableList.copyOf(checkNotNull(sortColumnHandles, "sortColumnHandles is null"));
Expand Down Expand Up @@ -85,9 +83,8 @@ public List<Type> getColumnTypes()
return columnTypes;
}

@Nullable
@JsonProperty
public String getExternalBatchId()
public Optional<String> getExternalBatchId()
{
return externalBatchId;
}
Expand Down
Expand Up @@ -147,15 +147,12 @@ private ConnectorTableHandle getTableHandle(SchemaTableName tableName)
}
}

if (sampleWeightColumnHandle != null) {
sampleWeightColumnHandle = new RaptorColumnHandle(connectorId, SAMPLE_WEIGHT_COLUMN_NAME, sampleWeightColumnHandle.getColumnId(), BIGINT);
}
return new RaptorTableHandle(
connectorId,
tableName.getSchemaName(),
tableName.getTableName(),
table.getTableId(),
sampleWeightColumnHandle);
Optional.ofNullable(sampleWeightColumnHandle));
}

@Override
Expand Down Expand Up @@ -196,7 +193,7 @@ public Map<String, ColumnHandle> getColumnHandles(ConnectorSession session, Conn
@Override
public ColumnHandle getSampleWeightColumnHandle(ConnectorSession session, ConnectorTableHandle tableHandle)
{
return checkType(tableHandle, RaptorTableHandle.class, "tableHandle").getSampleWeightColumnHandle();
return checkType(tableHandle, RaptorTableHandle.class, "tableHandle").getSampleWeightColumnHandle().orElse(null);
}

@Override
Expand Down Expand Up @@ -319,7 +316,7 @@ public ConnectorOutputTableHandle beginCreateTable(ConnectorSession session, Con
tableMetadata.getTable().getTableName(),
columnHandles.build(),
columnTypes.build(),
sampleWeightColumnHandle,
Optional.ofNullable(sampleWeightColumnHandle),
sortColumnHandles,
nCopies(sortColumnHandles.size(), ASC_NULLS_FIRST),
temporalColumnHandle);
Expand Down Expand Up @@ -405,7 +402,7 @@ public ConnectorInsertTableHandle beginInsert(ConnectorSession session, Connecto
columnTypes.add(column.getDataType());
}

String externalBatchId = getExternalBatchId(session);
Optional<String> externalBatchId = getExternalBatchId(session);
List<RaptorColumnHandle> sortColumnHandles = getSortColumnHandles(tableId);
return new RaptorInsertTableHandle(connectorId,
tableId,
Expand All @@ -431,7 +428,7 @@ public void commitInsert(ConnectorSession session, ConnectorInsertTableHandle in
{
RaptorInsertTableHandle handle = checkType(insertHandle, RaptorInsertTableHandle.class, "insertHandle");
long tableId = handle.getTableId();
Optional<String> externalBatchId = Optional.ofNullable(handle.getExternalBatchId());
Optional<String> externalBatchId = handle.getExternalBatchId();
List<ColumnInfo> columns = handle.getColumnHandles().stream().map(ColumnInfo::fromHandle).collect(toList());

shardManager.commitShards(tableId, columns, parseFragments(fragments), externalBatchId);
Expand Down
Expand Up @@ -20,8 +20,6 @@
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.collect.ImmutableList;

import javax.annotation.Nullable;

import java.util.List;
import java.util.Optional;

Expand All @@ -36,8 +34,7 @@ public class RaptorOutputTableHandle
private final String tableName;
private final List<RaptorColumnHandle> columnHandles;
private final List<Type> columnTypes;
@Nullable
private final RaptorColumnHandle sampleWeightColumnHandle;
private final Optional<RaptorColumnHandle> sampleWeightColumnHandle;
private final List<RaptorColumnHandle> sortColumnHandles;
private final List<SortOrder> sortOrders;
private final Optional<RaptorColumnHandle> temporalColumnHandle;
Expand All @@ -48,7 +45,7 @@ public RaptorOutputTableHandle(
@JsonProperty("tableName") String tableName,
@JsonProperty("columnHandles") List<RaptorColumnHandle> columnHandles,
@JsonProperty("columnTypes") List<Type> columnTypes,
@JsonProperty("sampleWeightColumnHandle") @Nullable RaptorColumnHandle sampleWeightColumnHandle,
@JsonProperty("sampleWeightColumnHandle") Optional<RaptorColumnHandle> sampleWeightColumnHandle,
@JsonProperty("sortColumnHandles") List<RaptorColumnHandle> sortColumnHandles,
@JsonProperty("sortOrders") List<SortOrder> sortOrders,
@JsonProperty("temporalColumnHandle") Optional<RaptorColumnHandle> temporalColumnHandle)
Expand All @@ -57,7 +54,7 @@ public RaptorOutputTableHandle(
this.tableName = checkTableName(tableName);
this.columnHandles = ImmutableList.copyOf(checkNotNull(columnHandles, "columnHandles is null"));
this.columnTypes = ImmutableList.copyOf(checkNotNull(columnTypes, "columnTypes is null"));
this.sampleWeightColumnHandle = sampleWeightColumnHandle;
this.sampleWeightColumnHandle = checkNotNull(sampleWeightColumnHandle, "sampleWeightColumnHandle is null");
this.sortOrders = checkNotNull(sortOrders, "sortOrders is null");
this.sortColumnHandles = checkNotNull(sortColumnHandles, "sortColumnHandles is null");
this.temporalColumnHandle = checkNotNull(temporalColumnHandle, "temporalColumnHandle is null");
Expand Down Expand Up @@ -87,9 +84,8 @@ public List<Type> getColumnTypes()
return columnTypes;
}

@Nullable
@JsonProperty
public RaptorColumnHandle getSampleWeightColumnHandle()
public Optional<RaptorColumnHandle> getSampleWeightColumnHandle()
{
return sampleWeightColumnHandle;
}
Expand Down
Expand Up @@ -61,7 +61,7 @@ public ConnectorPageSink createPageSink(ConnectorSession session, ConnectorOutpu
shardInfoCodec,
toColumnIds(handle.getColumnHandles()),
handle.getColumnTypes(),
optionalColumnId(handle.getSampleWeightColumnHandle()),
handle.getSampleWeightColumnHandle().map(RaptorColumnHandle::getColumnId),
toColumnIds(handle.getSortColumnHandles()),
handle.getSortOrders(),
maxBufferSize);
Expand All @@ -87,9 +87,4 @@ private static List<Long> toColumnIds(List<RaptorColumnHandle> columnHandles)
{
return columnHandles.stream().map(RaptorColumnHandle::getColumnId).collect(toList());
}

private static Optional<Long> optionalColumnId(RaptorColumnHandle handle)
{
return Optional.ofNullable(handle).map(RaptorColumnHandle::getColumnId);
}
}
Expand Up @@ -20,6 +20,7 @@
import javax.inject.Inject;

import java.util.List;
import java.util.Optional;

import static com.facebook.presto.spi.session.PropertyMetadata.stringSessionProperty;

Expand All @@ -45,8 +46,8 @@ public List<PropertyMetadata<?>> getSessionProperties()
return sessionProperties;
}

public static String getExternalBatchId(ConnectorSession session)
public static Optional<String> getExternalBatchId(ConnectorSession session)
{
return session.getProperty(EXTERNAL_BATCH_ID, String.class);
return Optional.ofNullable(session.getProperty(EXTERNAL_BATCH_ID, String.class));
}
}
Expand Up @@ -17,9 +17,8 @@
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;

import javax.annotation.Nullable;

import java.util.Objects;
import java.util.Optional;

import static com.facebook.presto.raptor.util.MetadataUtil.checkSchemaName;
import static com.facebook.presto.raptor.util.MetadataUtil.checkTableName;
Expand All @@ -33,16 +32,15 @@ public final class RaptorTableHandle
private final String schemaName;
private final String tableName;
private final long tableId;
@Nullable
private final RaptorColumnHandle sampleWeightColumnHandle;
private final Optional<RaptorColumnHandle> sampleWeightColumnHandle;

@JsonCreator
public RaptorTableHandle(
@JsonProperty("connectorId") String connectorId,
@JsonProperty("schemaName") String schemaName,
@JsonProperty("tableName") String tableName,
@JsonProperty("tableId") long tableId,
@JsonProperty("sampleWeightColumnHandle") @Nullable RaptorColumnHandle sampleWeightColumnHandle)
@JsonProperty("sampleWeightColumnHandle") Optional<RaptorColumnHandle> sampleWeightColumnHandle)
{
this.connectorId = checkNotNull(connectorId, "connectorId is null");
this.schemaName = checkSchemaName(schemaName);
Expand All @@ -51,7 +49,7 @@ public RaptorTableHandle(
checkArgument(tableId > 0, "tableId must be greater than zero");
this.tableId = tableId;

this.sampleWeightColumnHandle = sampleWeightColumnHandle;
this.sampleWeightColumnHandle = checkNotNull(sampleWeightColumnHandle, "sampleWeightColumnHandle is null");
}

@JsonProperty
Expand All @@ -78,9 +76,8 @@ public long getTableId()
return tableId;
}

@Nullable
@JsonProperty
public RaptorColumnHandle getSampleWeightColumnHandle()
public Optional<RaptorColumnHandle> getSampleWeightColumnHandle()
{
return sampleWeightColumnHandle;
}
Expand Down

0 comments on commit 13d9ac6

Please sign in to comment.