Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * 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.metadata;
 
 
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
 import static com.facebook.presto.spi.StandardErrorCode.ALREADY_EXISTS;
 import static com.facebook.presto.util.Types.checkType;
 import static com.google.common.base.Preconditions.checkArgument;
 import static com.google.common.base.Preconditions.checkNotNull;
 
 public class TestingMetadata
         implements ConnectorMetadata
 {
     private final ConcurrentMap<SchemaTableNameStringviews = new ConcurrentHashMap<>();
 
     @Override
     public List<StringlistSchemaNames(ConnectorSession session)
     {
         Set<StringschemaNames = new HashSet<>();
 
         for (SchemaTableName schemaTableName : .keySet()) {
             schemaNames.add(schemaTableName.getSchemaName());
         }
 
         return ImmutableList.copyOf(schemaNames);
     }
 
     @Override
     public ConnectorTableHandle getTableHandle(ConnectorSession sessionSchemaTableName tableName)
     {
         checkNotNull(tableName"tableName is null");
         if (!.containsKey(tableName)) {
             return null;
         }
         return new InMemoryTableHandle(tableName);
     }
 
     @Override
     {
         checkNotNull(tableHandle"tableHandle is null");
         SchemaTableName tableName = getTableName(tableHandle);
         ConnectorTableMetadata tableMetadata = .get(tableName);
         checkArgument(tableMetadata != null"Table %s does not exist"tableName);
         return tableMetadata;
     }
 
     @Override
     {
         ImmutableMap.Builder<StringConnectorColumnHandlebuilder = ImmutableMap.builder();
         for (ColumnMetadata columnMetadata : getTableMetadata(tableHandle).getColumns()) {
             builder.put(columnMetadata.getName(), new InMemoryColumnHandle(columnMetadata.getName(), columnMetadata.getOrdinalPosition(), columnMetadata.getType()));
         }
         return builder.build();
     }
 
     @Override
     {
         return null;
     }
 
    @Override
    public boolean canCreateSampledTables(ConnectorSession session)
    {
        return false;
    }
    @Override
    {
        checkNotNull(prefix"prefix is null");
        ImmutableMap.Builder<SchemaTableNameList<ColumnMetadata>> tableColumns = ImmutableMap.builder();
        for (SchemaTableName tableName : listTables(sessionprefix.getSchemaName())) {
            int position = 1;
            ImmutableList.Builder<ColumnMetadatacolumns = ImmutableList.builder();
            for (ColumnMetadata column : .get(tableName).getColumns()) {
                columns.add(new ColumnMetadata(column.getName(), column.getType(), positionfalse));
                position++;
            }
            tableColumns.put(tableNamecolumns.build());
        }
        return tableColumns.build();
    }
    @Override
    public ColumnMetadata getColumnMetadata(ConnectorTableHandle tableHandleConnectorColumnHandle columnHandle)
    {
        SchemaTableName tableName = getTableName(tableHandle);
        int columnIndex = checkType(columnHandleInMemoryColumnHandle.class"columnHandle").getOrdinalPosition();
        return .get(tableName).getColumns().get(columnIndex);
    }
    @Override
    public List<SchemaTableNamelistTables(ConnectorSession sessionString schemaNameOrNull)
    {
        ImmutableList.Builder<SchemaTableNamebuilder = ImmutableList.builder();
        for (SchemaTableName tableName : .keySet()) {
            if (schemaNameOrNull == null || schemaNameOrNull.equals(tableName.getSchemaName())) {
                builder.add(tableName);
            }
        }
        return builder.build();
    }
    @Override
    public void renameTable(ConnectorTableHandle tableHandleSchemaTableName newTableName)
    {
        // TODO: use locking to do this properly
        ConnectorTableMetadata table = getTableMetadata(tableHandle);
        if (.putIfAbsent(newTableNametable) != null) {
            throw new IllegalArgumentException("Target table already exists: " + newTableName);
        }
        .remove(table.getTable(), table);
    }
    @Override
    public void createTable(ConnectorSession sessionConnectorTableMetadata tableMetadata)
    {
        ConnectorTableMetadata existingTable = .putIfAbsent(tableMetadata.getTable(), tableMetadata);
        checkArgument(existingTable == null"Table %s already exists"tableMetadata.getTable());
    }
    @Override
    public void dropTable(ConnectorTableHandle tableHandle)
    {
        .remove(getTableName(tableHandle));
    }
    @Override
    {
        throw new UnsupportedOperationException();
    }
    @Override
    public void commitCreateTable(ConnectorOutputTableHandle tableHandleCollection<Slicefragments)
    {
        throw new UnsupportedOperationException();
    }
    @Override
    {
        throw new UnsupportedOperationException();
    }
    @Override
    public void commitInsert(ConnectorInsertTableHandle insertHandleCollection<Slicefragments)
    {
        throw new UnsupportedOperationException();
    }
    @Override
    public void createView(ConnectorSession sessionSchemaTableName viewNameString viewDataboolean replace)
    {
        if (replace) {
            .put(viewNameviewData);
        }
        else if (.putIfAbsent(viewNameviewData) != null) {
            throw new PrestoException("View already exists: " + viewName);
        }
    }
    @Override
    public void dropView(ConnectorSession sessionSchemaTableName viewName)
    {
        if (.remove(viewName) == null) {
            throw new ViewNotFoundException(viewName);
        }
    }
    @Override
    public List<SchemaTableNamelistViews(ConnectorSession sessionString schemaNameOrNull)
    {
        ImmutableList.Builder<SchemaTableNamebuilder = ImmutableList.builder();
        for (SchemaTableName viewName : .keySet()) {
            if ((schemaNameOrNull == null) || schemaNameOrNull.equals(viewName.getSchemaName())) {
                builder.add(viewName);
            }
        }
        return builder.build();
    }
    @Override
    {
        ImmutableMap.Builder<SchemaTableNameStringmap = ImmutableMap.builder();
        for (Map.Entry<SchemaTableNameStringentry : .entrySet()) {
            if (prefix.matches(entry.getKey())) {
                map.put(entry);
            }
        }
        return map.build();
    }
    private static SchemaTableName getTableName(ConnectorTableHandle tableHandle)
    {
        checkNotNull(tableHandle"tableHandle is null");
        checkArgument(tableHandle instanceof InMemoryTableHandle"tableHandle is not an instance of InMemoryTableHandle");
        InMemoryTableHandle inMemoryTableHandle = (InMemoryTableHandletableHandle;
        return inMemoryTableHandle.getTableName();
    }
    public static class InMemoryTableHandle
            implements ConnectorTableHandle
    {
        private final SchemaTableName tableName;
        public InMemoryTableHandle(SchemaTableName schemaTableName)
        {
            this. = schemaTableName;
        }
        public SchemaTableName getTableName()
        {
            return ;
        }
    }
    public static class InMemoryColumnHandle
            implements ConnectorColumnHandle
    {
        private final String name;
        private final int ordinalPosition;
        private final Type type;
        public InMemoryColumnHandle(String nameint ordinalPositionType type)
        {
            this. = name;
            this. = ordinalPosition;
            this. = type;
        }
        public String getName()
        {
            return ;
        }
        public int getOrdinalPosition()
        {
            return ;
        }
        public Type getType()
        {
            return ;
        }
    }
New to GrepCode? Check out our FAQ X