private T ConvertGetItemResponse <T>(GetItemRequest request, DynamoMetadataType table) { var response = Exec(() => DynamoDb.GetItem(request), rethrowExceptions: throwNotFoundExceptions); if (!response.IsItemSet) { return(default);
private static DynamoMetadataType ToMetadataType(Type type) { var alias = type.FirstAttribute <AliasAttribute>(); var props = GetTableProperties(type); var metadata = new DynamoMetadataType { Type = type, Name = alias != null ? alias.Name : type.Name, }; metadata.Fields = props.Map(p => new DynamoMetadataField { Parent = metadata, Type = p.PropertyType, PropertyInfo = p, Name = Converters.GetFieldName(p), DbType = Converters.GetFieldType(p.PropertyType), IsAutoIncrement = p.HasAttribute <AutoIncrementAttribute>(), SetValueFn = p.CreateSetter(), GetValueFn = p.CreateGetter(), }).ToArray(); return(metadata); }
private List <T> ConvertBatchGetItemResponse <T>(DynamoMetadataType table, KeysAndAttributes getItems) { var to = new List <T>(); var request = new BatchGetItemRequest(new Dictionary <string, KeysAndAttributes> { { table.Name, getItems } }); var response = Exec(() => DynamoDb.BatchGetItem(request)); List <Dictionary <string, AttributeValue> > results; if (response.Responses.TryGetValue(table.Name, out results)) { results.Each(x => to.Add(Converters.FromAttributeValues <T>(table, x))); } var i = 0; while (response.UnprocessedKeys.Count > 0) { response = Exec(() => DynamoDb.BatchGetItem(new BatchGetItemRequest(response.UnprocessedKeys))); if (response.Responses.TryGetValue(table.Name, out results)) { results.Each(x => to.Add(Converters.FromAttributeValues <T>(table, x))); } if (response.UnprocessedKeys.Count > 0) { i.SleepBackOffMultiplier(); } } return(to); }
private static DynamoLocalIndex CreateLocalIndex(Type type, DynamoMetadataType metadata, string hashField, Type indexType) { var indexProps = indexType.GetPublicProperties(); var indexProp = indexProps.FirstOrDefault(x => x.HasAttribute <IndexAttribute>() || x.HasAttribute <DynamoDBRangeKeyAttribute>()); if (indexProp == null) { throw new ArgumentException($"Missing [Index]. Could not infer Range Key in index '{indexType}'."); } var indexAlias = indexType.FirstAttribute <AliasAttribute>(); var rangeKey = metadata.GetField(indexProp.Name); if (rangeKey == null) { throw new ArgumentException($"Range Key '{indexProp.Name}' was not found on Table '{type.Name}'"); } var indexProjection = indexType.FirstAttribute <ProjectionTypeAttribute>(); var projectionType = indexProjection?.ProjectionType ?? DynamoProjectionType.Include; return(new DynamoLocalIndex { IndexType = indexType, Name = indexAlias != null ? indexAlias.Name : indexType.Name, HashKey = metadata.HashKey, RangeKey = rangeKey, ProjectionType = projectionType, ProjectedFields = projectionType == DynamoProjectionType.Include ? indexProps.Where(x => x.Name != hashField).Select(x => x.Name).ToArray() : new string[0], }); }
public DynamoDbQueryDataSource(QueryDataContext context, IPocoDynamo dynamo, bool allowScans = true) : base(context) { this.db = dynamo; this.modelDef = db.GetTableMetadata <T>(); isGlobalIndex = typeof(T).IsGlobalIndex(); this.allowScans = allowScans; }
public DynamoDbAppSettings(IPocoDynamo db, bool initSchema = false) : base(new DynamoDbSettings(db)) { Db.RegisterTable <ConfigSetting>(); this.metadata = db.GetTableMetadata <ConfigSetting>(); if (initSchema) { InitSchema(); } }
private T ConvertGetItemResponse <T>(GetItemRequest request, DynamoMetadataType table) { var response = Exec(() => DynamoDb.GetItem(request), rethrowExceptions: throwNotFoundExceptions); if (!response.IsItemSet) { return(default(T)); } var attributeValues = response.Item; return(Converters.FromAttributeValues <T>(table, attributeValues)); }
public static long[] GetNextSequences(this ISequenceSource seq, DynamoMetadataType meta, int noOfSequences) { var newCounter = seq.Increment(meta, noOfSequences); var firstId = newCounter - noOfSequences + 1; var ids = new long[noOfSequences]; for (var i = 0; i < noOfSequences; i++) { ids[i] = firstId + i; } return(ids); }
private static DynamoGlobalIndex CreateGlobalIndex(Type type, DynamoMetadataType metadata, Type indexType) { var indexProps = indexType.GetPublicProperties(); PropertyInfo indexHash, indexRange; Converters.GetHashAndRangeKeyFields(indexType, indexProps, out indexHash, out indexRange); var hashKey = metadata.GetField(indexHash.Name); if (hashKey == null) { throw new ArgumentException($"Hash Key '{indexHash.Name}' was not found on Table '{type.Name}'"); } if (indexRange == null) { indexRange = indexProps.FirstOrDefault(x => x.HasAttribute <IndexAttribute>()); } var rangeKey = indexRange != null ? metadata.GetField(indexRange.Name) : null; var indexAlias = indexType.FirstAttribute <AliasAttribute>(); var indexProvision = indexType.FirstAttribute <ProvisionedThroughputAttribute>(); var indexProjection = indexType.FirstAttribute <ProjectionTypeAttribute>(); var projectionType = indexProjection?.ProjectionType ?? DynamoProjectionType.Include; return(new DynamoGlobalIndex { IndexType = indexType, Name = indexAlias != null ? indexAlias.Name : indexType.Name, HashKey = hashKey, RangeKey = rangeKey, ProjectionType = projectionType, ProjectedFields = projectionType == DynamoProjectionType.Include ? indexProps.Where(x => x.Name != indexHash.Name).Select(x => x.Name).ToArray() : new string[0], ReadCapacityUnits = indexProvision?.ReadCapacityUnits ?? metadata.ReadCapacityUnits, WriteCapacityUnits = indexProvision?.WriteCapacityUnits ?? metadata.WriteCapacityUnits, }); }
private Task CreateTableAsync(DynamoMetadataType table) { var request = ToCreateTableRequest(table); try { return(ExecAsync(() => DynamoDb.CreateTableAsync(request))); } catch (AmazonDynamoDBException ex) { if (ex.ErrorCode == DynamoErrors.AlreadyExists) { return(PclExportClient.EmptyTask); } throw; } }
private async Task CreateTableAsync(DynamoMetadataType table, CancellationToken token = default) { var request = ToCreateTableRequest(table); CreateTableFilter?.Invoke(request); try { await ExecAsync(async() => await DynamoDb.CreateTableAsync(request, token).ConfigAwait()).ConfigAwait(); } catch (AmazonDynamoDBException ex) { if (ex.ErrorCode == DynamoErrors.AlreadyExists) { return; } throw; } }
private void ExecBatchWriteItemResponse <T>(DynamoMetadataType table, List <WriteRequest> deleteItems) { var request = new BatchWriteItemRequest(new Dictionary <string, List <WriteRequest> > { { table.Name, deleteItems } }); var response = Exec(() => DynamoDb.BatchWriteItem(request)); var i = 0; while (response.UnprocessedItems.Count > 0) { response = Exec(() => DynamoDb.BatchWriteItem(new BatchWriteItemRequest(response.UnprocessedItems))); if (response.UnprocessedItems.Count > 0) { i.SleepBackOffMultiplier(); } } }
public virtual Dictionary <string, AttributeValue> ToAttributeKeyValue(IPocoDynamo db, DynamoMetadataType table, DynamoId id) { using (AwsClientUtils.GetJsScope()) { return(new Dictionary <string, AttributeValue> { { table.HashKey.Name, ToAttributeValue(db, table.HashKey.Type, table.HashKey.DbType, id.Hash) }, { table.RangeKey.Name, ToAttributeValue(db, table.RangeKey.Type, table.RangeKey.DbType, id.Range) }, }); } }
public static async Task <bool> CreateTableIfMissingAsync(this IPocoDynamo db, DynamoMetadataType table, CancellationToken token = default) { return(await db.CreateMissingTablesAsync(new[] { table }, token).ConfigAwait()); }
public static bool CreateTableIfMissing(this IPocoDynamo db, DynamoMetadataType table) { return(db.CreateMissingTables(new[] { table })); }
public static long Increment(this ISequenceSource seq, DynamoMetadataType meta, int amount = 1) { return(seq.Increment(meta.Name, amount)); }
public static async Task <long> IncrementAsync(this ISequenceSourceAsync seq, DynamoMetadataType meta, int amount = 1, CancellationToken token = default) { return(await seq.IncrementAsync(meta.Name, amount, token).ConfigAwait()); }
private static DynamoMetadataType ToMetadataTable(Type type) { var alias = type.FirstAttribute <AliasAttribute>(); var props = GetTableProperties(type); Converters.GetHashAndRangeKeyFields(type, props, out var hash, out var range); var provision = type.FirstAttribute <ProvisionedThroughputAttribute>(); // If its a generic type, the type name will contain illegal characters (not accepted as DynamoDB table name) // so, remove the Tilde and make the type name unique to the runtime type of the generic string genericTypeNameAlias = null; if (type.IsGenericType) { var indexOfTilde = type.Name.IndexOf("`", StringComparison.Ordinal); indexOfTilde = indexOfTilde < 1 ? type.Name.Length - 1 : indexOfTilde; genericTypeNameAlias = type.Name.Substring(0, indexOfTilde) + type.GetGenericArguments().Select(t => t.Name).Join(); } var metadata = new DynamoMetadataType { Type = type, IsTable = true, Name = alias != null ? alias.Name : genericTypeNameAlias ?? type.Name, ReadCapacityUnits = provision?.ReadCapacityUnits, WriteCapacityUnits = provision?.WriteCapacityUnits, }; metadata.Fields = props.Map(p => new DynamoMetadataField { Parent = metadata, Type = p.PropertyType, PropertyInfo = p, Name = Converters.GetFieldName(p), DbType = Converters.GetFieldType(p.PropertyType), IsHashKey = p == hash, IsRangeKey = p == range, ExcludeNullValue = p.HasAttribute <IndexAttribute>() || p.HasAttribute <ExcludeNullValueAttribute>(), IsAutoIncrement = p.HasAttribute <AutoIncrementAttribute>(), SetValueFn = p.CreateSetter(), GetValueFn = p.CreateGetter(), }).ToArray(); metadata.HashKey = metadata.Fields.FirstOrDefault(x => x.IsHashKey); metadata.RangeKey = metadata.Fields.FirstOrDefault(x => x.IsRangeKey); if (metadata.HashKey == null) { throw new ArgumentException($"Could not infer Hash Key in Table '{type.Name}'"); } var hashField = metadata.HashKey.Name; metadata.LocalIndexes = props.Where(x => x.HasAttribute <IndexAttribute>()).Map(x => { var indexProjection = x.FirstAttribute <ProjectionTypeAttribute>(); var projectionType = indexProjection?.ProjectionType ?? DynamoProjectionType.Include; return(new DynamoLocalIndex { Name = $"{metadata.Name}{x.Name}Index", HashKey = metadata.HashKey, RangeKey = metadata.GetField(x.Name), ProjectionType = projectionType, ProjectedFields = projectionType == DynamoProjectionType.Include ? new[] { x.Name } : new string[0], }); }); metadata.GlobalIndexes = new List <DynamoGlobalIndex>(); var references = type.AllAttributes <ReferencesAttribute>(); foreach (var attr in references) { var localIndex = attr.Type.GetTypeWithGenericInterfaceOf(typeof(ILocalIndex <>)); if (localIndex != null) { metadata.LocalIndexes.Add(CreateLocalIndex(type, metadata, hashField, attr.Type)); } var globalIndex = attr.Type.GetTypeWithGenericInterfaceOf(typeof(IGlobalIndex <>)); if (globalIndex != null) { metadata.GlobalIndexes.Add(CreateGlobalIndex(type, metadata, attr.Type)); } } return(metadata); }