public IEnumerable <GlobalSecondaryIndexConfiguration> CreateIndexConfigByAttributes( Type entityType) { InputValidator.ThrowIfNull(entityType); var configs = new List <GlobalSecondaryIndexConfiguration>(); var hashAttributesByProperty = entityType .GetProperties() .Where(pi => pi.GetCustomAttribute < DynamoDBGlobalSecondaryIndexHashKeyAttribute>(true) != null) .Select(pi => new { PropertyInfo = pi, HashAttribute = pi.GetCustomAttribute < DynamoDBGlobalSecondaryIndexHashKeyAttribute>(true) }) .ToDictionary(kvp => kvp.PropertyInfo, kvp => kvp.HashAttribute); var rangeAttributesByProperty = entityType .GetProperties() .Where(pi => pi.GetCustomAttribute < DynamoDBGlobalSecondaryIndexRangeKeyAttribute>(true) != null) .Select(pi => new { PropertyInfo = pi, HashAttribute = pi.GetCustomAttribute < DynamoDBGlobalSecondaryIndexRangeKeyAttribute>(true) }) .ToDictionary(kvp => kvp.PropertyInfo, kvp => kvp.HashAttribute); foreach (var kvp in hashAttributesByProperty) { var indexName = kvp.Value.IndexNames.FirstOrDefault(); var currentConfig = new GlobalSecondaryIndexConfiguration { IndexName = indexName, ReadCapacityUnits = Constants.DefaultReadCapacityUnits, WriteCapacityUnits = Constants.DefaultWriteCapacityUnits, HashKeyMemberName = kvp.Key.Name, HashKeyMemberType = kvp.Key.PropertyType }; if (rangeAttributesByProperty.Any(a => a.Value.IndexNames?[0] == indexName)) { var rangeKeyProperty = rangeAttributesByProperty .First(rap => rap.Value.IndexNames[0] == indexName) .Key; currentConfig.RangeKeyMemberName = rangeKeyProperty.Name; currentConfig.RangeKeyMemberType = rangeKeyProperty.PropertyType; } configs.Add(currentConfig); } return(configs); }
/// <summary> /// Adds info for a Global Secondary Index to the configuration. /// </summary> /// <exception cref="ArgumentException"></exception> public IEntityTypeBuilder <TContext, TEntity> HasGlobalSecondaryIndex( Action <GlobalSecondaryIndexConfiguration> indexAction) { InputValidator.ThrowIfNull(indexAction, "indexAction cannot be null."); var newIndexConfig = new GlobalSecondaryIndexConfiguration(); indexAction(newIndexConfig); InputValidator.ThrowIfAnyNullOrWhitespace( newIndexConfig.HashKeyMemberName, newIndexConfig.IndexName); if (newIndexConfig.HashKeyMemberType == null) { newIndexConfig.HashKeyMemberType = typeof(TEntity) .GetProperty(newIndexConfig.HashKeyMemberName) .PropertyType; } if (!string.IsNullOrWhiteSpace(newIndexConfig.RangeKeyMemberName) && newIndexConfig.RangeKeyMemberType == null) { newIndexConfig.RangeKeyMemberType = typeof(TEntity) .GetProperty(newIndexConfig.RangeKeyMemberName) .PropertyType; } var existingIndexConfig = this.entityConfig .Indexes .FirstOrDefault(i => i.IndexName == newIndexConfig.IndexName); if (existingIndexConfig != null) { indexAction(existingIndexConfig); return(this); } this.entityConfig.Indexes.Add(newIndexConfig); return(this); }
public void CreateRequestIndexes_OneConfig_ReturnsSingleRequestIndex() { var gsiConfig = new GlobalSecondaryIndexConfiguration { HashKeyMemberName = nameof(FakeEntity.Title), HashKeyMemberType = typeof(string), IndexName = "GSI_Entities_Title_LastModified", RangeKeyMemberName = nameof(FakeEntity.LastModified), RangeKeyMemberType = typeof(DateTime), ReadCapacityUnits = 3, WriteCapacityUnits = 5 }; var gsiConfigs = new List <GlobalSecondaryIndexConfiguration> { gsiConfig }; var requestIndexes = this.factory.CreateRequestIndexes(gsiConfigs); Assert.Single(requestIndexes); }
public void CreateAttributeDefinitions_GsiConfigurationPassed_CreateDefinitionWithCorrectHashKeyType() { var gsiConfig = new GlobalSecondaryIndexConfiguration { HashKeyMemberName = nameof(FakeEntity.Title), HashKeyMemberType = typeof(string), IndexName = "GSI_Entities_Title_LAstModified", RangeKeyMemberName = nameof(FakeEntity.LastModified), RangeKeyMemberType = typeof(DateTime), ReadCapacityUnits = 3, WriteCapacityUnits = 5 }; var gsiConfigs = new List <GlobalSecondaryIndexConfiguration> { gsiConfig }; var definitions = this.factory.CreateAttributeDefinitions(null, null, gsiConfigs); Assert.Contains(definitions, d => d.AttributeName == gsiConfig.RangeKeyMemberName && d.AttributeType == ScalarAttributeType.S); }
public void CreateRequestIndexes_OneConfig_ReturnsRequestIndexWithRangeKeySchemaElement() { var gsiConfig = new GlobalSecondaryIndexConfiguration { HashKeyMemberName = nameof(FakeEntity.Title), HashKeyMemberType = typeof(string), IndexName = "GSI_Entities_Title_LastModified", RangeKeyMemberName = nameof(FakeEntity.LastModified), RangeKeyMemberType = typeof(DateTime), ReadCapacityUnits = 3, WriteCapacityUnits = 5 }; var gsiConfigs = new List <GlobalSecondaryIndexConfiguration> { gsiConfig }; var requestIndexes = this.factory.CreateRequestIndexes(gsiConfigs); var resultIndex = requestIndexes.Single(); Assert.Contains(resultIndex.KeySchema, ks => ks.AttributeName == gsiConfig.RangeKeyMemberName && ks.KeyType == KeyType.RANGE); }
public void CreateRequestIndexes_OneConfig_ReturnsRequestIndexWithCorrectWriteCapacityUnits() { var gsiConfig = new GlobalSecondaryIndexConfiguration { HashKeyMemberName = nameof(FakeEntity.Title), HashKeyMemberType = typeof(string), IndexName = "GSI_Entities_Title_LastModified", RangeKeyMemberName = nameof(FakeEntity.LastModified), RangeKeyMemberType = typeof(DateTime), ReadCapacityUnits = 3, WriteCapacityUnits = 5 }; var gsiConfigs = new List <GlobalSecondaryIndexConfiguration> { gsiConfig }; var requestIndexes = this.factory.CreateRequestIndexes(gsiConfigs); var resultIndex = requestIndexes.Single(); Assert.Equal( gsiConfig.WriteCapacityUnits, resultIndex.ProvisionedThroughput.WriteCapacityUnits); }