public List <T> GetItems <T>(IEnumerable <DynamoId> ids)
        {
            var to = new List <T>();

            var table        = DynamoMetadata.GetTable <T>();
            var remainingIds = ids.ToList();

            while (remainingIds.Count > 0)
            {
                var batchSize = Math.Min(remainingIds.Count, MaxReadBatchSize);
                var nextBatch = remainingIds.GetRange(0, batchSize);
                remainingIds.RemoveRange(0, batchSize);

                var getItems = new KeysAndAttributes
                {
                    ConsistentRead = ConsistentRead,
                };
                nextBatch.Each(id =>
                               getItems.Keys.Add(Converters.ToAttributeKeyValue(this, table, id)));

                to.AddRange(ConvertBatchGetItemResponse <T>(table, getItems));
            }

            return(to);
        }
        public void PutItems <T>(IEnumerable <T> items)
        {
            var table     = DynamoMetadata.GetTable <T>();
            var remaining = items.ToList();

            PopulateMissingHashes(table, remaining);

            while (remaining.Count > 0)
            {
                var batchSize = Math.Min(remaining.Count, MaxWriteBatchSize);
                var nextBatch = remaining.GetRange(0, batchSize);
                remaining.RemoveRange(0, batchSize);

                var putItems = nextBatch.Map(x => new WriteRequest(
                                                 new PutRequest(Converters.ToAttributeValues(this, x, table))));

                var request = new BatchWriteItemRequest(new Dictionary <string, List <WriteRequest> > {
                    { table.Name, putItems }
                });

                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 long DescribeItemCount <T>()
        {
            var table    = DynamoMetadata.GetTable <T>();
            var response = Exec(() => DynamoDb.DescribeTable(new DescribeTableRequest(table.Name)));

            return(response.Table.ItemCount);
        }
        public long Increment <T>(object hash, string fieldName, long amount = 1)
        {
            var type    = DynamoMetadata.GetType <T>();
            var request = new UpdateItemRequest
            {
                TableName        = type.Name,
                Key              = Converters.ToAttributeKeyValue(this, type.HashKey, hash),
                AttributeUpdates = new Dictionary <string, AttributeValueUpdate> {
                    {
                        fieldName,
                        new AttributeValueUpdate {
                            Action = AttributeAction.ADD,
                            Value  = new AttributeValue {
                                N = amount.ToString()
                            }
                        }
                    }
                },
                ReturnValues = ReturnValue.ALL_NEW,
            };

            var response = DynamoDb.UpdateItem(request);

            return(response.Attributes.Count > 0
                ? Convert.ToInt64(response.Attributes[fieldName].N)
                : 0);
        }
        public long ScanItemCount <T>()
        {
            var table    = DynamoMetadata.GetTable <T>();
            var request  = new ScanRequest(table.Name);
            var response = Scan(request, r => new[] { r.Count });

            return(response.Sum());
        }
        public void UpdateItem <T>(DynamoUpdateItem update)
        {
            var table   = DynamoMetadata.GetTable <T>();
            var request = new UpdateItemRequest
            {
                TableName        = table.Name,
                Key              = Converters.ToAttributeKeyValue(this, table, update.Hash, update.Range),
                AttributeUpdates = new Dictionary <string, AttributeValueUpdate>(),
                ReturnValues     = ReturnValue.NONE,
            };

            if (update.Put != null)
            {
                foreach (var entry in update.Put)
                {
                    var field = table.GetField(entry.Key);
                    if (field == null)
                    {
                        continue;
                    }

                    request.AttributeUpdates[field.Name] = new AttributeValueUpdate(
                        Converters.ToAttributeValue(this, field.Type, field.DbType, entry.Value), DynamoAttributeAction.Put);
                }
            }

            if (update.Add != null)
            {
                foreach (var entry in update.Add)
                {
                    var field = table.GetField(entry.Key);
                    if (field == null)
                    {
                        continue;
                    }

                    request.AttributeUpdates[field.Name] = new AttributeValueUpdate(
                        Converters.ToAttributeValue(this, field.Type, field.DbType, entry.Value), DynamoAttributeAction.Add);
                }
            }

            if (update.Delete != null)
            {
                foreach (var key in update.Delete)
                {
                    var field = table.GetField(key);
                    if (field == null)
                    {
                        continue;
                    }

                    request.AttributeUpdates[field.Name] = new AttributeValueUpdate(null, DynamoAttributeAction.Delete);
                }
            }

            Exec(() => DynamoDb.UpdateItem(request));
        }
 public PocoDynamoExpression(Type type)
 {
     Type             = type;
     Table            = DynamoMetadata.GetType(type);
     ParamPrefix      = "p";
     Params           = new Dictionary <string, object>();
     ReferencedFields = new List <string>();
     Aliases          = new Dictionary <string, string>();
 }
        public IEnumerable <T> ScanAll <T>()
        {
            var type    = DynamoMetadata.GetType <T>();
            var request = new ScanRequest
            {
                Limit     = PagingLimit,
                TableName = type.Name,
            };

            return(Scan(request, r => r.ConvertAll <T>()));
        }
        public void DeleteRelatedItems <T>(object hash, IEnumerable <object> ranges)
        {
            var table = DynamoMetadata.GetTable <T>();

            if (table.HashKey == null || table.RangeKey == null)
            {
                throw new ArgumentException($"Related table '{typeof(T).Name}' needs both a HashKey and RangeKey");
            }

            DeleteItems <T>(ranges.Map(range => new DynamoId(hash, range)));
        }
        public T GetItem <T>(object hash, object range)
        {
            var table   = DynamoMetadata.GetTable <T>();
            var request = new GetItemRequest
            {
                TableName      = table.Name,
                Key            = Converters.ToAttributeKeyValue(this, table, hash, range),
                ConsistentRead = ConsistentRead,
            };

            return(ConvertGetItemResponse <T>(request, table));
        }
        public void PutRelatedItem <T>(object hash, T item)
        {
            var table = DynamoMetadata.GetTable <T>();

            if (table.HashKey == null || table.RangeKey == null)
            {
                throw new ArgumentException($"Related table '{typeof(T).Name}' needs both a HashKey and RangeKey");
            }

            table.HashKey.SetValue(item, hash);
            PutItem(item);
        }
        public void PutRelatedItems <T>(object hash, IEnumerable <T> items)
        {
            var table = DynamoMetadata.GetTable <T>();

            if (table.HashKey == null || table.RangeKey == null)
            {
                throw new ArgumentException($"Related table '{typeof(T).Name}' needs both a HashKey and RangeKey");
            }

            var related = items.ToList();

            related.Each(x => table.HashKey.SetValue(x, hash));
            PutItems(related);
        }
        public QueryExpression <T> QueryComposite <T>(Expression <Func <T, bool> > keyExpression)
        {
            var table = DynamoMetadata.GetTable <T>();
            var q     = new QueryExpression <T>(this)
            {
                TableName        = table.Name,
                Limit            = PagingLimit,
                ConsistentRead   = false,
                ScanIndexForward = this.ScanIndexForward,
            };

            q.KeyCondition(keyExpression);

            return(q);
        }
Exemple #14
0
        public static DynamoMetadataType GetIndexTable(this Type indexType)
        {
            var genericIndex = indexType.GetTypeWithGenericInterfaceOf(typeof(IDynamoIndex <>));

            if (genericIndex == null)
            {
                return(null);
            }

            var tableType = genericIndex.GetGenericArguments().FirstOrDefault();

            return(tableType != null
                ? DynamoMetadata.GetTable(tableType)
                : null);
        }
        public T DeleteByItem <T>(T item)
        {
            var    table     = DynamoMetadata.GetTable <T>();
            string hashKey   = table.HashKey.Name;
            object hashValue = typeof(T).GetProperty(hashKey).GetValue(item, null);

            if (table.RangeKey != null)
            {
                string rangeKey   = table.RangeKey.Name;
                object rangeValue = typeof(T).GetProperty(rangeKey).GetValue(item, null);
                return(DeleteItem <T>(hashValue, rangeValue));
            }
            else
            {
                return(DeleteItem <T>(hashValue));
            }
        }
        public void DeleteItems <T>(IEnumerable <DynamoId> ids)
        {
            var table        = DynamoMetadata.GetTable <T>();
            var remainingIds = ids.ToList();

            while (remainingIds.Count > 0)
            {
                var batchSize = Math.Min(remainingIds.Count, MaxWriteBatchSize);
                var nextBatch = remainingIds.GetRange(0, batchSize);
                remainingIds.RemoveRange(0, batchSize);

                var deleteItems = nextBatch.Map(id => new WriteRequest(
                                                    new DeleteRequest(Converters.ToAttributeKeyValue(this, table, id))));

                ExecBatchWriteItemResponse <T>(table, deleteItems);
            }
        }
        public Table GetTableSchema(Type type)
        {
            var table = DynamoMetadata.GetTable(type);

            return(Exec(() =>
            {
                try
                {
                    Table.TryLoadTable(DynamoDb, table.Name, out var awsTable);
                    return awsTable;
                }
                catch (ResourceNotFoundException)
                {
                    return null;
                }
            }, throwNotFoundExceptions));
        }
        public IEnumerable <T> GetRelatedItems <T>(object hash)
        {
            var table = DynamoMetadata.GetTable <T>();

            var argType = hash.GetType();
            var dbType  = Converters.GetFieldType(argType);
            var request = new QueryRequest(table.Name)
            {
                Limit = PagingLimit,
                KeyConditionExpression    = $"{table.HashKey.Name} = :k1",
                ExpressionAttributeValues = new Dictionary <string, AttributeValue> {
                    { ":k1", Converters.ToAttributeValue(this, argType, dbType, hash) }
                }
            };

            return(Query(request, r => r.ConvertAll <T>()));
        }
        public T GetItem <T>(object hash)
        {
            var table    = DynamoMetadata.GetTable <T>();
            var rangeKey = table.RangeKey;

            if (rangeKey != null)
            {
                return(GetItemFromQuery <T>(table.Name, table.HashKey.Name, hash));
            }
            var request = new GetItemRequest
            {
                TableName      = table.Name,
                Key            = Converters.ToAttributeKeyValue(this, table.HashKey, hash),
                ConsistentRead = ConsistentRead
            };

            return(ConvertGetItemResponse <T>(request, table));
        }
        public T PutItem <T>(T value, bool returnOld = false)
        {
            var table   = DynamoMetadata.GetTable <T>();
            var request = new PutItemRequest
            {
                TableName    = table.Name,
                Item         = Converters.ToAttributeValues(this, value, table),
                ReturnValues = returnOld ? ReturnValue.ALL_OLD : ReturnValue.NONE,
            };

            var response = Exec(() => DynamoDb.PutItem(request));

            if (response.Attributes.IsEmpty())
            {
                return(default(T));
            }

            return(Converters.FromAttributeValues <T>(table, response.Attributes));
        }
        public T DeleteItem <T>(object hash, object range, ReturnItem returnItem = ReturnItem.None)
        {
            var table   = DynamoMetadata.GetTable <T>();
            var request = new DeleteItemRequest
            {
                TableName    = table.Name,
                Key          = Converters.ToAttributeKeyValue(this, table, hash, range),
                ReturnValues = returnItem.ToReturnValue(),
            };

            var response = Exec(() => DynamoDb.DeleteItem(request));

            if (response.Attributes.IsEmpty())
            {
                return(default(T));
            }

            return(Converters.FromAttributeValues <T>(table, response.Attributes));
        }
 public Task InitSchemaAsync()
 {
     return(CreateMissingTablesAsync(DynamoMetadata.GetTables()));
 }
Exemple #23
0
 public static IPocoDynamo RegisterTable <T>(this IPocoDynamo db)
 {
     DynamoMetadata.RegisterTable(typeof(T));
     return(db);
 }
 public void InitSchema()
 {
     CreateMissingTables(DynamoMetadata.GetTables());
 }
Exemple #25
0
 public static IPocoDynamo RegisterTable(this IPocoDynamo db, Type tableType)
 {
     DynamoMetadata.RegisterTable(tableType);
     return(db);
 }
Exemple #26
0
 public static IPocoDynamo RegisterTables(this IPocoDynamo db, IEnumerable <Type> tableTypes)
 {
     DynamoMetadata.RegisterTables(tableTypes);
     return(db);
 }
 public DynamoMetadataType GetTableMetadata(Type table)
 {
     return(DynamoMetadata.TryGetTable(table) ?? DynamoMetadata.RegisterTable(table));
 }
 public UpdateExpression <T> UpdateExpression <T>(object hash, object range = null)
 {
     return(new UpdateExpression <T>(this, DynamoMetadata.GetTable <T>(), hash, range));
 }
Exemple #29
0
 public static List <T> ConvertAll <T>(this QueryResponse response)
 {
     return(response.Items
            .Select(values => DynamoMetadata.GetType <T>().ConvertTo <T>(values))
            .ToList());
 }