コード例 #1
0
        void HasRangeKey_OfType_(string name, ScalarAttributeType type)
        {
            var key = _table.KeySchema.Single(x => x.AttributeName == name);

            PAssert.IsTrue(() => key.KeyType == KeyType.RANGE);
            var attributeDefinition = _table.AttributeDefinitions.Single(x => x.AttributeName == name);

            PAssert.IsTrue(() => attributeDefinition.AttributeType == type);
        }
コード例 #2
0
        public IEnumerable <AttributeDefinition> CreateAttributeDefinitions(
            string primaryKeyMemberName,
            ScalarAttributeType primaryKeyMemberAttributeType,
            IEnumerable <GlobalSecondaryIndexConfiguration> gsisConfiguration)
        {
            gsisConfiguration = gsisConfiguration
                                ?? Enumerable.Empty <GlobalSecondaryIndexConfiguration>();

            var definitions = new List <AttributeDefinition>();

            if (!string.IsNullOrWhiteSpace(primaryKeyMemberName) &&
                primaryKeyMemberAttributeType != null)
            {
                definitions.Add(new AttributeDefinition
                {
                    AttributeName = primaryKeyMemberName,
                    AttributeType = primaryKeyMemberAttributeType
                });
            }

            foreach (var gsiConfig in gsisConfiguration)
            {
                if (definitions.All(def => def.AttributeName != gsiConfig.HashKeyMemberName))
                {
                    definitions.Add(new AttributeDefinition
                    {
                        AttributeName = gsiConfig.HashKeyMemberName,
                        AttributeType = Constants.AttributeTypesMap[gsiConfig.HashKeyMemberType]
                    });
                }

                if (!string.IsNullOrWhiteSpace(gsiConfig.RangeKeyMemberName) &&
                    definitions.All(def => def.AttributeName != gsiConfig.RangeKeyMemberName))
                {
                    definitions.Add(new AttributeDefinition
                    {
                        AttributeName = gsiConfig.RangeKeyMemberName,
                        AttributeType = Constants.AttributeTypesMap[gsiConfig.RangeKeyMemberType]
                    });
                }
            }

            return(definitions);
        }
コード例 #3
0
        public void CreateTable(string tableName, string partionKeyName, ScalarAttributeType partitionKeyType)
        {
            var tableResponse = GetTables();

            if (!tableResponse.Contains(tableName))
            {
                var response = DynomoClient.CreateTableAsync(new CreateTableRequest
                {
                    TableName = tableName,
                    KeySchema = new List <KeySchemaElement>
                    {
                        new KeySchemaElement
                        {
                            AttributeName = partionKeyName,
                            KeyType       = KeyType.HASH
                        }
                    },
                    AttributeDefinitions = new List <AttributeDefinition>
                    {
                        new AttributeDefinition {
                            AttributeName = partionKeyName,
                            AttributeType = partitionKeyType
                        }
                    },
                    ProvisionedThroughput = new ProvisionedThroughput
                    {
                        ReadCapacityUnits  = 3,
                        WriteCapacityUnits = 3
                    },
                });
                Console.WriteLine($"HTTP Response : {response.Result.HttpStatusCode}");
                System.Threading.Thread.Sleep(5000);
                Console.WriteLine($"Table Status : {response.Result.TableDescription.TableStatus}");
            }
            else
            {
                Console.WriteLine($"{tableName} isimli tablo zaten var.");
            }
        }
コード例 #4
0
 /// <summary>
 /// Instantiates AttributeDefinition with the parameterized properties
 /// </summary>
 /// <param name="attributeName">A name for the attribute.</param>
 /// <param name="attributeType">The data type for the attribute.</param>
 public AttributeDefinition(string attributeName, ScalarAttributeType attributeType)
 {
     _attributeName = attributeName;
     _attributeType = attributeType;
 }
コード例 #5
0
 /// <summary>
 /// Instantiates AttributeDefinition with the parameterized properties
 /// </summary>
 /// <param name="attributeName">A name for the attribute.</param>
 /// <param name="attributeType">The data type for the attribute.</param>
 public AttributeDefinition(string attributeName, ScalarAttributeType attributeType)
 {
     _attributeName = attributeName;
     _attributeType = attributeType;
 }
コード例 #6
0
        void LocalSecondaryIndex_HasNonKeyAttribute_OfType_(string indexName, string attributeName, ScalarAttributeType type)
        {
            var index = _table.LocalSecondaryIndexes.Single(x => x.IndexName == indexName);

            PAssert.IsTrue(() => index.Projection.NonKeyAttributes.Contains(attributeName));
        }
コード例 #7
0
        void LocalSecondaryIndex_HasRangeKey_OfType_(string indexName, string keyName, ScalarAttributeType type)
        {
            var index = _table.LocalSecondaryIndexes.Single(x => x.IndexName == indexName);
            var key   = index.KeySchema.SingleOrDefault(x => x.AttributeName == keyName);

            PAssert.IsTrue(() => key.KeyType == KeyType.RANGE);
            var attributeDefinition = _table.AttributeDefinitions.Single(x => x.AttributeName == keyName);

            PAssert.IsTrue(() => attributeDefinition.AttributeType == type);
        }
コード例 #8
0
        void GlobalSecondaryIndex_HasHashKey_OfType_(string indexName, string keyName, ScalarAttributeType type)
        {
            var index = _table.GlobalSecondaryIndexes.Single(x => x.IndexName == indexName);
            var key   = index.KeySchema.Single(x => x.AttributeName == keyName);

            PAssert.IsTrue(() => key.KeyType == KeyType.HASH);
            var attributeDefinition = _table.AttributeDefinitions.Single(x => x.AttributeName == keyName);

            PAssert.IsTrue(() => attributeDefinition.AttributeType == type);
        }
コード例 #9
0
ファイル: TableCreator.cs プロジェクト: msotiroff/EasyDynamo
        public async Task <string> CreateTableAsync(
            Type contextType, Type entityType, string tableName)
        {
            InputValidator.ThrowIfAnyNullOrWhitespace(entityType, tableName);

            var dynamoDbTableAttribute = entityType.GetCustomAttribute <DynamoDBTableAttribute>(true);

            tableName = dynamoDbTableAttribute?.TableName ?? tableName;
            var hashKeyMember = entityType
                                .GetProperties()
                                .SingleOrDefault(pi => pi.GetCustomAttributes()
                                                 .Any(attr => attr.GetType() == typeof(DynamoDBHashKeyAttribute)));
            var hashKeyMemberAttribute = entityType
                                         .GetProperty(hashKeyMember?.Name ?? string.Empty)
                                         ?.GetCustomAttribute <DynamoDBHashKeyAttribute>(true);
            var hashKeyMemberType = entityType
                                    .GetProperty(hashKeyMember?.Name ?? string.Empty)
                                    ?.PropertyType;
            var entityConfigRequired = hashKeyMember == null;
            var entityConfig         = this.entityConfigurationProvider
                                       .TryGetEntityConfiguration(contextType, entityType);

            if (entityConfigRequired && entityConfig == null)
            {
                throw new DynamoContextConfigurationException(string.Format(
                                                                  ExceptionMessage.EntityConfigurationNotFound, entityType.FullName));
            }

            var hashKeyMemberName = entityConfig?.HashKeyMemberName
                                    ?? hashKeyMemberAttribute.AttributeName
                                    ?? hashKeyMember.Name;
            var hashKeyMemberAttributeType = new ScalarAttributeType(
                hashKeyMemberType != null
                ? Constants.AttributeTypesMap[hashKeyMemberType]
                : Constants.AttributeTypesMap[entityConfig.HashKeyMemberType]);
            var readCapacityUnits = entityConfig?.ReadCapacityUnits
                                    ?? Constants.DefaultReadCapacityUnits;
            var writeCapacityUnits = entityConfig?.WriteCapacityUnits
                                     ?? Constants.DefaultWriteCapacityUnits;
            var gsisConfiguration = (entityConfig?.Indexes?.Count ?? 0) > 0
                ? entityConfig.Indexes
                : this.indexConfigurationFactory.CreateIndexConfigByAttributes(entityType);

            var request = new CreateTableRequest
            {
                TableName            = tableName,
                AttributeDefinitions = this.attributeDefinitionFactory.CreateAttributeDefinitions(
                    hashKeyMemberName, hashKeyMemberAttributeType, gsisConfiguration).ToList(),
                KeySchema = new List <KeySchemaElement>
                {
                    new KeySchemaElement(hashKeyMemberName, KeyType.HASH)
                },
                ProvisionedThroughput = new ProvisionedThroughput
                {
                    ReadCapacityUnits  = readCapacityUnits,
                    WriteCapacityUnits = writeCapacityUnits
                },
                GlobalSecondaryIndexes = gsisConfiguration.Count() == 0
                ? null
                : this.indexFactory.CreateRequestIndexes(gsisConfiguration).ToList()
            };

            try
            {
                var response = await this.client.CreateTableAsync(request);

                if (!response.HttpStatusCode.IsSuccessful())
                {
                    throw new CreateTableFailedException(
                              response.ResponseMetadata.Metadata.JoinByNewLine());
                }

                return(request.TableName);
            }
            catch (Exception ex)
            {
                throw new CreateTableFailedException("Failed to create a table.", ex);
            }
        }
コード例 #10
0
        public async Task CreateTable(string tableName, string hashKey, ScalarAttributeType hashKeyType, string rangeKey = null, ScalarAttributeType rangeKeyType = null)
        {
            var schemaElements       = new List <KeySchemaElement>();
            var attributeDefinitions = new List <AttributeDefinition>();

            // Build a 'CreateTableRequest' for the new table
            CreateTableRequest createRequest = new CreateTableRequest
            {
                TableName   = tableName,
                BillingMode = BillingMode.PAY_PER_REQUEST
                              //ProvisionedThroughput = new ProvisionedThroughput
                              //{
                              //    ReadCapacityUnits = 20000,
                              //    WriteCapacityUnits = 20000
                              //}
            };

            schemaElements.Add(new KeySchemaElement
            {
                AttributeName = hashKey,
                KeyType       = KeyType.HASH
            });

            attributeDefinitions.Add(new AttributeDefinition
            {
                AttributeName = hashKey,
                AttributeType = hashKeyType
            }
                                     );

            if (!string.IsNullOrEmpty(rangeKey) && !string.IsNullOrEmpty(rangeKeyType))
            {
                schemaElements.Add(new KeySchemaElement
                {
                    AttributeName = rangeKey,
                    KeyType       = KeyType.RANGE
                });
                attributeDefinitions.Add(new AttributeDefinition
                {
                    AttributeName = rangeKey,
                    AttributeType = rangeKeyType
                }
                                         );
            }

            createRequest.KeySchema.AddRange(schemaElements);
            createRequest.AttributeDefinitions.AddRange(attributeDefinitions);

            try
            {
                var client = new DbContextFactory().GetDynamoDbClient();
                await client.CreateTableAsync(createRequest);

                bool isTableAvailable = false;
                while (!isTableAvailable)
                {
                    Thread.Sleep(2000);
                    var tableStatus = await client.DescribeTableAsync(tableName);

                    isTableAvailable = tableStatus.Table.TableStatus == "ACTIVE";
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error: failed to create the new table: {ex.Message}");
            }
        }