Skip to content

Commit

Permalink
Inline AbstractPropertyManager
Browse files Browse the repository at this point in the history
This abstraction is overly complex for the two uses, and is much simpler
when inlined
  • Loading branch information
dain committed Jan 31, 2022
1 parent 9ee8f74 commit 2051e78
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 120 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,13 @@
*/
package io.trino.metadata;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.Maps;
import io.trino.Session;
import io.trino.connector.CatalogName;
import io.trino.security.AccessControl;
import io.trino.spi.ErrorCodeSupplier;
import io.trino.spi.TrinoException;
import io.trino.spi.session.PropertyMetadata;
import io.trino.sql.PlannerContext;
import io.trino.sql.tree.Expression;
Expand All @@ -29,29 +32,41 @@
import java.util.Map;
import java.util.Map.Entry;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

import static com.google.common.base.Preconditions.checkState;
import static com.google.common.collect.ImmutableMap.toImmutableMap;
import static io.trino.metadata.PropertyUtil.evaluateProperties;
import static io.trino.spi.StandardErrorCode.NOT_FOUND;
import static java.lang.String.format;
import static java.util.Objects.requireNonNull;

abstract class AbstractCatalogPropertyManager
extends AbstractPropertyManager<CatalogName>
{
private final ConcurrentMap<CatalogName, Map<String, PropertyMetadata<?>>> connectorProperties = new ConcurrentHashMap<>();
private final String propertyType;
private final ErrorCodeSupplier propertyError;

protected AbstractCatalogPropertyManager(String propertyType, ErrorCodeSupplier propertyError)
{
super(propertyError);
this.propertyType = propertyType;
this.propertyError = requireNonNull(propertyError, "propertyError is null");
}

public void addProperties(CatalogName catalogName, List<PropertyMetadata<?>> properties)
{
doAddProperties(catalogName, properties);
requireNonNull(catalogName, "catalogName is null");
requireNonNull(properties, "properties is null");

Map<String, PropertyMetadata<?>> propertiesByName = Maps.uniqueIndex(properties, PropertyMetadata::getName);

checkState(connectorProperties.putIfAbsent(catalogName, propertiesByName) == null, "Properties for key %s are already registered", catalogName);
}

public void removeProperties(CatalogName catalogName)
{
doRemoveProperties(catalogName);
connectorProperties.remove(catalogName);
}

public Map<String, Object> getProperties(
Expand Down Expand Up @@ -85,19 +100,27 @@ public Map<String, Optional<Object>> getNullableProperties(
Map<NodeRef<Parameter>, Expression> parameters,
boolean includeAllProperties)
{
return doGetNullableProperties(
catalog,
Map<String, PropertyMetadata<?>> propertyMetadata = connectorProperties.get(catalog);
if (propertyMetadata == null) {
throw new TrinoException(NOT_FOUND, format("Catalog '%s' %s property not found", catalog, propertyType));
}

return evaluateProperties(
properties,
session,
plannerContext,
accessControl,
parameters,
includeAllProperties,
propertyMetadata,
propertyError,
format("catalog '%s' %s property", catalog, propertyType));
}

public Collection<PropertyMetadata<?>> getAllProperties(CatalogName catalogName)
{
return doGetAllProperties(catalogName);
return Optional.ofNullable(connectorProperties.get(catalogName))
.map(Map::values)
.orElse(ImmutableList.of());
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@
*/
package io.trino.metadata;

import com.google.common.collect.Maps;
import io.trino.Session;
import io.trino.connector.CatalogName;
import io.trino.security.AccessControl;
import io.trino.spi.TrinoException;
import io.trino.spi.session.PropertyMetadata;
import io.trino.sql.PlannerContext;
import io.trino.sql.tree.Expression;
Expand All @@ -30,25 +32,33 @@
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

import static com.google.common.base.Preconditions.checkState;
import static com.google.common.collect.ImmutableList.toImmutableList;
import static com.google.common.collect.ImmutableMap.toImmutableMap;
import static com.google.common.collect.ImmutableSet.toImmutableSet;
import static io.trino.metadata.PropertyUtil.evaluateProperties;
import static io.trino.spi.StandardErrorCode.INVALID_PROCEDURE_ARGUMENT;
import static io.trino.spi.StandardErrorCode.NOT_FOUND;
import static java.lang.String.format;
import static java.util.Objects.requireNonNull;

public class TableProceduresPropertyManager
extends AbstractPropertyManager<TableProceduresPropertyManager.Key>
{
public TableProceduresPropertyManager()
{
super(INVALID_PROCEDURE_ARGUMENT);
}
private final ConcurrentMap<Key, Map<String, PropertyMetadata<?>>> connectorProperties = new ConcurrentHashMap<>();

public void addProperties(CatalogName catalogName, String procedureName, List<PropertyMetadata<?>> properties)
{
doAddProperties(new Key(catalogName, procedureName), properties);
requireNonNull(catalogName, "catalogName is null");
requireNonNull(procedureName, "procedureName is null");
requireNonNull(catalogName, "catalogName is null");

Map<String, PropertyMetadata<?>> propertiesByName = Maps.uniqueIndex(properties, PropertyMetadata::getName);

Key propertiesKey = new Key(catalogName, procedureName);
checkState(connectorProperties.putIfAbsent(propertiesKey, propertiesByName) == null, "Properties for key %s are already registered", propertiesKey);
}

public void removeProperties(CatalogName catalogName)
Expand All @@ -57,7 +67,7 @@ public void removeProperties(CatalogName catalogName)
.filter(key -> catalogName.equals(key.getCatalogName()))
.collect(toImmutableSet());
for (Key key : keysToRemove) {
doRemoveProperties(key);
connectorProperties.remove(key);
}
}

Expand All @@ -70,8 +80,12 @@ public Map<String, Object> getProperties(
AccessControl accessControl,
Map<NodeRef<Parameter>, Expression> parameters)
{
Map<String, Optional<Object>> propertyValues = doGetNullableProperties(
new Key(catalog, procedureName),
Map<String, PropertyMetadata<?>> supportedProperties = connectorProperties.get(new Key(catalog, procedureName));
if (supportedProperties == null) {
throw new TrinoException(NOT_FOUND, format("Catalog '%s' table procedure '%s' property not found", catalog, procedureName));
}

Map<String, Optional<Object>> propertyValues = evaluateProperties(
sqlPropertyValues.entrySet().stream()
.map(entry -> new Property(new Identifier(entry.getKey()), entry.getValue()))
.collect(toImmutableList()),
Expand All @@ -80,6 +94,8 @@ public Map<String, Object> getProperties(
accessControl,
parameters,
true,
supportedProperties,
INVALID_PROCEDURE_ARGUMENT,
format("catalog '%s' table procedure '%s' property", catalog, procedureName));
return propertyValues.entrySet().stream()
.filter(entry -> entry.getValue().isPresent())
Expand Down

0 comments on commit 2051e78

Please sign in to comment.