PrometheusMetadata.java
/*
* 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.plugin.prometheus;
import com.facebook.presto.spi.ColumnHandle;
import com.facebook.presto.spi.ColumnMetadata;
import com.facebook.presto.spi.ConnectorSession;
import com.facebook.presto.spi.ConnectorTableHandle;
import com.facebook.presto.spi.ConnectorTableLayout;
import com.facebook.presto.spi.ConnectorTableLayoutHandle;
import com.facebook.presto.spi.ConnectorTableLayoutResult;
import com.facebook.presto.spi.ConnectorTableMetadata;
import com.facebook.presto.spi.Constraint;
import com.facebook.presto.spi.SchemaTableName;
import com.facebook.presto.spi.SchemaTablePrefix;
import com.facebook.presto.spi.TableNotFoundException;
import com.facebook.presto.spi.connector.ConnectorMetadata;
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.Optional;
import java.util.Set;
import static com.google.common.collect.ImmutableList.toImmutableList;
import static java.util.Objects.requireNonNull;
public class PrometheusMetadata
implements ConnectorMetadata
{
private final PrometheusClient prometheusClient;
@Inject
public PrometheusMetadata(PrometheusClient prometheusClient)
{
requireNonNull(prometheusClient, "client is null");
this.prometheusClient = prometheusClient;
}
private static List<String> listSchemaNames()
{
return ImmutableList.copyOf(ImmutableSet.of("default"));
}
@Override
public List<String> listSchemaNames(ConnectorSession session)
{
return listSchemaNames();
}
@Override
public ConnectorTableHandle getTableHandle(ConnectorSession session, SchemaTableName tableName)
{
if (!listSchemaNames(session).contains(tableName.getSchemaName())) {
return null;
}
if (prometheusClient.getTable(tableName.getSchemaName(), tableName.getTableName()) == null) {
return null;
}
return new PrometheusTableHandle(tableName.getSchemaName(), tableName.getTableName());
}
@Override
public List<ConnectorTableLayoutResult> getTableLayouts(ConnectorSession session, ConnectorTableHandle table, Constraint<ColumnHandle> constraint, Optional<Set<ColumnHandle>> desiredColumns)
{
PrometheusTableHandle tableHandle = (PrometheusTableHandle) table;
ConnectorTableLayout layout = new ConnectorTableLayout(new PrometheusTableLayoutHandle(tableHandle, constraint.getSummary()));
return ImmutableList.of(new ConnectorTableLayoutResult(layout, constraint.getSummary()));
}
@Override
public ConnectorTableLayout getTableLayout(ConnectorSession session, ConnectorTableLayoutHandle handle)
{
return new ConnectorTableLayout(handle);
}
@Override
public ConnectorTableMetadata getTableMetadata(ConnectorSession session, ConnectorTableHandle table)
{
return getTableMetadata(((PrometheusTableHandle) table).toSchemaTableName());
}
@Override
public List<SchemaTableName> listTables(ConnectorSession session, Optional<String> schemaName)
{
Set<String> schemaNames = schemaName.map(ImmutableSet::of)
.orElseGet(() -> ImmutableSet.copyOf(ImmutableSet.of("default")));
return schemaNames.stream()
.flatMap(thisSchemaName ->
prometheusClient.getTableNames(thisSchemaName).stream().map(tableName -> new SchemaTableName(thisSchemaName, tableName)))
.collect(toImmutableList());
}
@Override
public Map<String, ColumnHandle> getColumnHandles(ConnectorSession session, ConnectorTableHandle tableHandle)
{
PrometheusTableHandle prometheusTableHandle = (PrometheusTableHandle) tableHandle;
PrometheusTable table = prometheusClient.getTable(prometheusTableHandle.getSchemaName(), prometheusTableHandle.getTableName());
if (table == null) {
throw new TableNotFoundException(prometheusTableHandle.toSchemaTableName());
}
ImmutableMap.Builder<String, ColumnHandle> columnHandles = ImmutableMap.builder();
int index = 0;
for (ColumnMetadata column : table.getColumnsMetadata()) {
columnHandles.put(column.getName(), new PrometheusColumnHandle(column.getName(), column.getType(), index));
index++;
}
return columnHandles.build();
}
@Override
public ColumnMetadata getColumnMetadata(ConnectorSession session, ConnectorTableHandle tableHandle, ColumnHandle columnHandle)
{
return ((PrometheusColumnHandle) columnHandle).getColumnMetadata();
}
@Override
public Map<SchemaTableName, List<ColumnMetadata>> listTableColumns(ConnectorSession session, SchemaTablePrefix prefix)
{
requireNonNull(prefix, "prefix is null");
ImmutableMap.Builder<SchemaTableName, List<ColumnMetadata>> columns = ImmutableMap.builder();
for (SchemaTableName tableName : listTables(session, 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;
}
PrometheusTable table = prometheusClient.getTable(tableName.getSchemaName(), tableName.getTableName());
if (table == null) {
return null;
}
return new ConnectorTableMetadata(tableName, table.getColumnsMetadata());
}
private List<SchemaTableName> listTables(ConnectorSession session, SchemaTablePrefix prefix)
{
if (prefix.toSchemaTableName() == null || prefix.getTableName() == null) {
return listTables(session, Optional.ofNullable(prefix.getSchemaName()));
}
return ImmutableList.of(prefix.toSchemaTableName());
}
}