/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.facebook.presto.example;
import com.facebook.presto.spi.ColumnHandle;
import com.facebook.presto.spi.ColumnMetadata;
import com.facebook.presto.spi.ConnectorTableMetadata;
import com.facebook.presto.spi.ReadOnlyConnectorMetadata;
import com.facebook.presto.spi.SchemaTableName;
import com.facebook.presto.spi.SchemaTablePrefix;
import com.facebook.presto.spi.TableHandle;
import com.facebook.presto.spi.TableNotFoundException;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import javax.inject.Inject;
import java.util.List;
import java.util.Map;
import java.util.Set;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
public class ExampleMetadata
extends ReadOnlyConnectorMetadata
{
private final String connectorId;
private final ExampleClient exampleClient;
@Inject
public ExampleMetadata(ExampleConnectorId connectorId, ExampleClient exampleClient)
{
this.connectorId = checkNotNull(connectorId, "connectorId is null").toString();
this.exampleClient = checkNotNull(exampleClient, "client is null");
}
@Override
public boolean canHandle(TableHandle tableHandle)
{
return tableHandle instanceof ExampleTableHandle && ((ExampleTableHandle) tableHandle).getConnectorId().equals(connectorId);
}
@Override
public List<String> listSchemaNames()
{
return ImmutableList.copyOf(exampleClient.getSchemaNames());
}
@Override
public ExampleTableHandle getTableHandle(SchemaTableName tableName)
{
if (!listSchemaNames().contains(tableName.getSchemaName())) {
return null;
}
ExampleTable table = exampleClient.getTable(tableName.getSchemaName(), tableName.getTableName());
if (table == null) {
return null;
}
return new ExampleTableHandle(connectorId, tableName.getSchemaName(), tableName.getTableName());
}
@Override
public ConnectorTableMetadata getTableMetadata(TableHandle table)
{
checkArgument(table instanceof ExampleTableHandle, "tableHandle is not an instance of ExampleTableHandle");
ExampleTableHandle exampleTableHandle = (ExampleTableHandle) table;
checkArgument(exampleTableHandle.getConnectorId().equals(connectorId), "tableHandle is not for this connector");
SchemaTableName tableName = new SchemaTableName(exampleTableHandle.getSchemaName(), exampleTableHandle.getTableName());
return getTableMetadata(tableName);
}
@Override
public List<SchemaTableName> listTables(String schemaNameOrNull)
{
Set<String> schemaNames;
if (schemaNameOrNull != null) {
schemaNames = ImmutableSet.of(schemaNameOrNull);
}
else {
schemaNames = exampleClient.getSchemaNames();
}
ImmutableList.Builder<SchemaTableName> builder = ImmutableList.builder();
for (String schemaName : schemaNames) {
for (String tableName : exampleClient.getTableNames(schemaName)) {
builder.add(new SchemaTableName(schemaName, tableName));
}
}
return builder.build();
}
@Override
public ColumnHandle getColumnHandle(TableHandle tableHandle, String columnName)
{
return getColumnHandles(tableHandle).get(columnName);
}
@Override
public ColumnHandle getSampleWeightColumnHandle(TableHandle tableHandle)
{
return null;
}
@Override
public Map<String, ColumnHandle> getColumnHandles(TableHandle tableHandle)
{
checkNotNull(tableHandle, "tableHandle is null");
checkArgument(tableHandle instanceof ExampleTableHandle, "tableHandle is not an instance of ExampleTableHandle");
ExampleTableHandle exampleTableHandle = (ExampleTableHandle) tableHandle;
checkArgument(exampleTableHandle.getConnectorId().equals(connectorId), "tableHandle is not for this connector");
ExampleTable table = exampleClient.getTable(exampleTableHandle.getSchemaName(), exampleTableHandle.getTableName());
if (table == null) {
throw new TableNotFoundException(exampleTableHandle.toSchemaTableName());
}
ImmutableMap.Builder<String, ColumnHandle> columnHandles = ImmutableMap.builder();
for (ColumnMetadata columnMetadata : table.getColumnsMetadata()) {
columnHandles.put(columnMetadata.getName(), new ExampleColumnHandle(connectorId, columnMetadata));
}
return columnHandles.build();
}
@Override
public Map<SchemaTableName, List<ColumnMetadata>> listTableColumns(SchemaTablePrefix prefix)
{
checkNotNull(prefix, "prefix is null");
ImmutableMap.Builder<SchemaTableName, List<ColumnMetadata>> columns = ImmutableMap.builder();
for (SchemaTableName tableName : listTables(prefix)) {
ConnectorTableMetadata tableMetadata = getTableMetadata(tableName);
// table can disappear during listing operation
if (tableMetadata != null) {
columns.put(tableName, tableMetadata.getColumns());
}
}
return columns.build();
}
private ConnectorTableMetadata getTableMetadata(SchemaTableName tableName)
{
if (!listSchemaNames().contains(tableName.getSchemaName())) {
return null;
}
ExampleTable table = exampleClient.getTable(tableName.getSchemaName(), tableName.getTableName());
if (table == null) {
return null;
}
return new ConnectorTableMetadata(tableName, table.getColumnsMetadata());
}
private List<SchemaTableName> listTables(SchemaTablePrefix prefix)
{
if (prefix.getSchemaName() == null) {
return listTables(prefix.getSchemaName());
}
return ImmutableList.of(new SchemaTableName(prefix.getSchemaName(), prefix.getTableName()));
}
@Override
public ColumnMetadata getColumnMetadata(TableHandle tableHandle, ColumnHandle columnHandle)
{
checkNotNull(tableHandle, "tableHandle is null");
checkNotNull(columnHandle, "columnHandle is null");
checkArgument(tableHandle instanceof ExampleTableHandle, "tableHandle is not an instance of ExampleTableHandle");
checkArgument(((ExampleTableHandle) tableHandle).getConnectorId().equals(connectorId), "tableHandle is not for this connector");
checkArgument(columnHandle instanceof ExampleColumnHandle, "columnHandle is not an instance of ExampleColumnHandle");
return ((ExampleColumnHandle) columnHandle).getColumnMetadata();
}
}