Github user kaspersorensen commented on a diff in the pull request:

    https://github.com/apache/metamodel/pull/140#discussion_r97700078
  
    --- Diff: 
dynamodb/src/main/java/org/apache/metamodel/dynamodb/DynamoDbDataContext.java 
---
    @@ -0,0 +1,320 @@
    +/**
    + * Licensed to the Apache Software Foundation (ASF) under one
    + * or more contributor license agreements.  See the NOTICE file
    + * distributed with this work for additional information
    + * regarding copyright ownership.  The ASF licenses this file
    + * to you 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 org.apache.metamodel.dynamodb;
    +
    +import java.io.Closeable;
    +import java.util.ArrayList;
    +import java.util.Collections;
    +import java.util.HashMap;
    +import java.util.List;
    +import java.util.Map;
    +
    +import org.apache.metamodel.MetaModelException;
    +import org.apache.metamodel.QueryPostprocessDataContext;
    +import org.apache.metamodel.UpdateScript;
    +import org.apache.metamodel.UpdateableDataContext;
    +import org.apache.metamodel.data.DataSet;
    +import org.apache.metamodel.data.DefaultRow;
    +import org.apache.metamodel.data.Row;
    +import org.apache.metamodel.data.SimpleDataSetHeader;
    +import org.apache.metamodel.query.FilterItem;
    +import org.apache.metamodel.query.SelectItem;
    +import org.apache.metamodel.schema.Column;
    +import org.apache.metamodel.schema.ColumnType;
    +import org.apache.metamodel.schema.MutableColumn;
    +import org.apache.metamodel.schema.MutableSchema;
    +import org.apache.metamodel.schema.MutableTable;
    +import org.apache.metamodel.schema.Schema;
    +import org.apache.metamodel.schema.Table;
    +import org.apache.metamodel.util.SimpleTableDef;
    +import org.slf4j.Logger;
    +import org.slf4j.LoggerFactory;
    +
    +import com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
    +import com.amazonaws.services.dynamodbv2.AmazonDynamoDBClientBuilder;
    +import com.amazonaws.services.dynamodbv2.model.AttributeDefinition;
    +import com.amazonaws.services.dynamodbv2.model.AttributeValue;
    +import com.amazonaws.services.dynamodbv2.model.DescribeTableResult;
    +import com.amazonaws.services.dynamodbv2.model.GetItemRequest;
    +import com.amazonaws.services.dynamodbv2.model.GetItemResult;
    +import 
com.amazonaws.services.dynamodbv2.model.GlobalSecondaryIndexDescription;
    +import com.amazonaws.services.dynamodbv2.model.KeySchemaElement;
    +import com.amazonaws.services.dynamodbv2.model.ListTablesResult;
    +import 
com.amazonaws.services.dynamodbv2.model.LocalSecondaryIndexDescription;
    +import com.amazonaws.services.dynamodbv2.model.ScanRequest;
    +import com.amazonaws.services.dynamodbv2.model.ScanResult;
    +import com.amazonaws.services.dynamodbv2.model.TableDescription;
    +
    +/**
    + * DataContext implementation for Amazon DynamoDB.
    + */
    +public class DynamoDbDataContext extends QueryPostprocessDataContext 
implements UpdateableDataContext, Closeable {
    +
    +    /**
    +     * System property key used for getting the read throughput capacity 
when
    +     * creating new tables. Defaults to 5.
    +     */
    +    public static final String SYSTEM_PROPERTY_THROUGHPUT_READ_CAPACITY = 
"metamodel.dynamodb.throughput.capacity.read";
    +
    +    /**
    +     * System property key used for getting the write throughput capacity 
when
    +     * creating new tables. Defaults to 5.
    +     */
    +    public static final String SYSTEM_PROPERTY_THROUGHPUT_WRITE_CAPACITY = 
"metamodel.dynamodb.throughput.capacity.write";
    +
    +    private static final Logger logger = 
LoggerFactory.getLogger(DynamoDbDataContext.class);
    +
    +    /**
    +     * The artificial schema name used by this DataContext.
    +     */
    +    public static final String SCHEMA_NAME = "public";
    +
    +    private final AmazonDynamoDB _dynamoDb;
    +    private final boolean _shutdownOnClose;
    +    private final SimpleTableDef[] _tableDefs;
    +
    +    public DynamoDbDataContext() {
    +        this(AmazonDynamoDBClientBuilder.defaultClient(), null, true);
    +    }
    +
    +    public DynamoDbDataContext(SimpleTableDef[] tableDefs) {
    +        this(AmazonDynamoDBClientBuilder.defaultClient(), tableDefs, true);
    +    }
    +
    +    public DynamoDbDataContext(AmazonDynamoDB client) {
    +        this(client, null, false);
    +    }
    +
    +    public DynamoDbDataContext(AmazonDynamoDB client, SimpleTableDef[] 
tableDefs) {
    +        this(client, tableDefs, false);
    +    }
    +
    +    private DynamoDbDataContext(AmazonDynamoDB client, SimpleTableDef[] 
tableDefs, boolean shutdownOnClose) {
    +        _dynamoDb = client;
    +        _tableDefs = (tableDefs == null ? new SimpleTableDef[0] : 
tableDefs);
    +        _shutdownOnClose = shutdownOnClose;
    +    }
    +
    +    public AmazonDynamoDB getDynamoDb() {
    +        return _dynamoDb;
    +    }
    +
    +    @Override
    +    public void close() {
    +        if (_shutdownOnClose) {
    +            _dynamoDb.shutdown();
    +        }
    +    }
    +
    +    @Override
    +    protected Schema getMainSchema() throws MetaModelException {
    +        final Map<String, SimpleTableDef> tableDefs = new HashMap<>();
    +        for (SimpleTableDef tableDef : _tableDefs) {
    +            tableDefs.put(tableDef.getName(), tableDef);
    +        }
    +
    +        final MutableSchema schema = new 
MutableSchema(getMainSchemaName());
    +        final ListTablesResult tables = _dynamoDb.listTables();
    +        final List<String> tableNames = tables.getTableNames();
    +        for (String tableName : tableNames) {
    +            final MutableTable table = new MutableTable(tableName, schema);
    +            schema.addTable(table);
    +
    +            final DescribeTableResult descripeTableResult = 
_dynamoDb.describeTable(tableName);
    +            final TableDescription tableDescription = 
descripeTableResult.getTable();
    +
    +            // add primary keys
    +            addColumnFromKeySchema("Primary index", 
tableDescription.getKeySchema(), table, true);
    +
    +            // add attributes from global and local indices
    +            final List<GlobalSecondaryIndexDescription> 
globalSecondaryIndexes = tableDescription
    +                    .getGlobalSecondaryIndexes();
    +            if (globalSecondaryIndexes != null) {
    +                for (GlobalSecondaryIndexDescription globalSecondaryIndex 
: globalSecondaryIndexes) {
    +                    
addColumnFromKeySchema(globalSecondaryIndex.getIndexName(), 
globalSecondaryIndex.getKeySchema(),
    --- End diff --
    
    Unfortunately the GlobalSecondaryIndexDescription and 
LocalSecondaryIndexDescription classes don't share a common super-class, so I'm 
not sure if it can be made more general.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at [email protected] or file a JIRA ticket
with INFRA.
---

Reply via email to