/* * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"). * You may not use this file except in compliance with the License. * A copy of the License is located at * * http://aws.amazon.com/apache2.0 * * or in the "license" file accompanying this file. This file 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. */ #pragma warning disable 1574 using System; using System.Collections.Generic; using System.Linq; using System.Threading; using System.Threading.Tasks; using Amazon.DynamoDBv2.DocumentModel; using Amazon.DynamoDBv2.Model; using Amazon.Runtime.Internal; namespace Amazon.DynamoDBv2.DataModel { /// /// Context object for using the DataModel mode of DynamoDB. /// Used to interact with the service, save/load objects, etc. /// public partial class DynamoDBContext : IDynamoDBContext { #region Save async /// /// Initiates the asynchronous execution of the Save operation. /// /// /// Type to save as. /// Object to save. /// Token which can be used to cancel the task. /// A Task that can be used to poll or wait for results, or both. public Task SaveAsync(T value, CancellationToken cancellationToken = default(CancellationToken)) { return SaveHelperAsync(value, null, cancellationToken); } /// /// Initiates the asynchronous execution of the Save operation. /// /// /// Type to save as. /// Object to save. /// Overriding configuration. /// Token which can be used to cancel the task. /// A Task that can be used to poll or wait for results, or both. public Task SaveAsync(T value, DynamoDBOperationConfig operationConfig, CancellationToken cancellationToken = default(CancellationToken)) { return SaveHelperAsync(value, operationConfig, cancellationToken); } /// /// Initiates the asynchronous execution of the Save operation. /// /// /// Type of the Object to save. /// Object to save. /// Token which can be used to cancel the task. /// A Task that can be used to poll or wait for results, or both. public Task SaveAsync(Type valueType, object value, CancellationToken cancellationToken = default(CancellationToken)) { return SaveHelperAsync(valueType, value, null, cancellationToken); } /// /// Initiates the asynchronous execution of the Save operation. /// /// /// Type of the Object to save. /// Object to save. /// Overriding configuration. /// Token which can be used to cancel the task. /// A Task that can be used to poll or wait for results, or both. public Task SaveAsync(Type valueType, object value, DynamoDBOperationConfig operationConfig, CancellationToken cancellationToken = default(CancellationToken)) { return SaveHelperAsync(valueType, value, operationConfig, cancellationToken); } #endregion #region Load async /// /// Initiates the asynchronous execution of the Load operation. /// /// /// Type to populate. /// Hash key element of the target item. /// Token which can be used to cancel the task. /// A Task that can be used to poll or wait for results, or both. public Task LoadAsync(object hashKey, CancellationToken cancellationToken = default(CancellationToken)) { return LoadHelperAsync(hashKey, null, null, cancellationToken); } /// /// Initiates the asynchronous execution of the Load operation. /// /// /// Type to populate. /// Hash key element of the target item. /// Overriding configuration. /// Token which can be used to cancel the task. /// A Task that can be used to poll or wait for results, or both. public Task LoadAsync(object hashKey, DynamoDBOperationConfig operationConfig, CancellationToken cancellationToken = default(CancellationToken)) { return LoadHelperAsync(hashKey, null, operationConfig, cancellationToken); } /// /// Initiates the asynchronous execution of the Load operation. /// /// /// Type to populate. /// Hash key element of the target item. /// Range key element of the target item. /// Token which can be used to cancel the task. /// A Task that can be used to poll or wait for results, or both. public Task LoadAsync(object hashKey, object rangeKey, CancellationToken cancellationToken = default(CancellationToken)) { return LoadHelperAsync(hashKey, rangeKey, null, cancellationToken); } /// /// Initiates the asynchronous execution of the Load operation. /// /// /// Type to populate. /// Hash key element of the target item. /// Range key element of the target item. /// Overriding configuration. /// Token which can be used to cancel the task. /// A Task that can be used to poll or wait for results, or both. public Task LoadAsync(object hashKey, object rangeKey, DynamoDBOperationConfig operationConfig, CancellationToken cancellationToken = default(CancellationToken)) { return LoadHelperAsync(hashKey, rangeKey, operationConfig, cancellationToken); } /// /// Initiates the asynchronous execution of the Load operation. /// /// /// Type to populate. /// Key of the target item. /// Token which can be used to cancel the task. /// A Task that can be used to poll or wait for results, or both. public Task LoadAsync(T keyObject, CancellationToken cancellationToken = default(CancellationToken)) { return LoadHelperAsync(keyObject, null, cancellationToken); } /// /// Initiates the asynchronous execution of the Load operation. /// /// /// Type to populate. /// Key of the target item. /// Overriding configuration. /// Token which can be used to cancel the task. /// A Task that can be used to poll or wait for results, or both. public Task LoadAsync(T keyObject, DynamoDBOperationConfig operationConfig, CancellationToken cancellationToken = default(CancellationToken)) { return LoadHelperAsync(keyObject, operationConfig, cancellationToken); } #endregion #region Delete async /// /// Initiates the asynchronous execution of the Delete operation. /// /// /// Type of object. /// Object to delete. /// Token which can be used to cancel the task. /// A Task that can be used to poll or wait for results, or both. public Task DeleteAsync(T value, CancellationToken cancellationToken = default(CancellationToken)) { return DeleteHelperAsync(value, null, cancellationToken); } /// /// Initiates the asynchronous execution of the Delete operation. /// /// /// Type of object. /// Object to delete. /// Overriding configuration. /// Token which can be used to cancel the task. /// A Task that can be used to poll or wait for results, or both. public Task DeleteAsync(T value, DynamoDBOperationConfig operationConfig, CancellationToken cancellationToken = default(CancellationToken)) { return DeleteHelperAsync(value, operationConfig, cancellationToken); } /// /// Initiates the asynchronous execution of the Delete operation. /// /// /// Type of object. /// Hash key element of the object to delete. /// Token which can be used to cancel the task. /// A Task that can be used to poll or wait for results, or both. public Task DeleteAsync(object hashKey, CancellationToken cancellationToken = default(CancellationToken)) { return DeleteHelperAsync(hashKey, null, null, cancellationToken); } /// /// Initiates the asynchronous execution of the Delete operation. /// /// /// Type of object. /// Hash key element of the object to delete. /// Config object which can be used to override that table used. /// Token which can be used to cancel the task. /// A Task that can be used to poll or wait for results, or both. public Task DeleteAsync(object hashKey, DynamoDBOperationConfig operationConfig, CancellationToken cancellationToken = default(CancellationToken)) { return DeleteHelperAsync(hashKey, null, operationConfig, cancellationToken); } /// /// Initiates the asynchronous execution of the Delete operation. /// /// /// Type of object. /// Hash key element of the object to delete. /// Range key element of the object to delete. /// Token which can be used to cancel the task. /// A Task that can be used to poll or wait for results, or both. public Task DeleteAsync(object hashKey, object rangeKey, CancellationToken cancellationToken = default(CancellationToken)) { return DeleteHelperAsync(hashKey, rangeKey, null, cancellationToken); } /// /// Initiates the asynchronous execution of the Delete operation. /// /// /// Type of object. /// Hash key element of the object to delete. /// Range key element of the object to delete. /// Config object which can be used to override that table used. /// Token which can be used to cancel the task. /// A Task that can be used to poll or wait for results, or both. public Task DeleteAsync(object hashKey, object rangeKey, DynamoDBOperationConfig operationConfig, CancellationToken cancellationToken = default(CancellationToken)) { return DeleteHelperAsync(hashKey, rangeKey, operationConfig, cancellationToken); } #endregion #region BatchGet async /// /// Issues a batch-get request with multiple batches. /// /// Results are stored in the individual batches. /// /// /// Configured BatchGet objects /// public Task ExecuteBatchGetAsync(params BatchGet[] batches) { return ExecuteBatchGetAsync(batches, default(CancellationToken)); } /// /// Issues a batch-get request with multiple batches. /// /// Results are stored in the individual batches. /// /// /// Configured BatchGet objects /// /// /// A cancellation token that can be used by other objects or threads to receive notice of cancellation. /// public Task ExecuteBatchGetAsync(BatchGet[] batches, CancellationToken cancellationToken = default(CancellationToken)) { MultiTableBatchGet superBatch = new MultiTableBatchGet(batches); return superBatch.ExecuteAsync(cancellationToken); } #endregion #region BatchWrite async /// /// Issues a batch-write request with multiple batches. /// /// /// Configured BatchWrite objects /// /// /// A cancellation token that can be used by other objects or threads to receive notice of cancellation. /// public Task ExecuteBatchWriteAsync(BatchWrite[] batches, CancellationToken cancellationToken = default(CancellationToken)) { MultiTableBatchWrite superBatch = new MultiTableBatchWrite(batches); return superBatch.ExecuteAsync(cancellationToken); } #endregion #region TransactGet async /// /// Issues a transactional get request with multiple TransactGet objects. /// Results are stored in the individual TransactGet objects. /// /// Configured TransactGet objects. /// Token which can be used to cancel the task. /// A Task that can be used to poll or wait for results, or both. public Task ExecuteTransactGetAsync(TransactGet[] transactionParts, CancellationToken cancellationToken = default(CancellationToken)) { MultiTableTransactGet transaction = new MultiTableTransactGet(transactionParts); return transaction.ExecuteAsync(cancellationToken); } #endregion #region TransactWrite async /// /// Issues a transactional write request with multiple TransactWrite objects. /// /// Configured TransactWrite objects. /// Token which can be used to cancel the task. /// A Task that can be used to poll or wait for results, or both. public Task ExecuteTransactWriteAsync(TransactWrite[] transactionParts, CancellationToken cancellationToken = default(CancellationToken)) { MultiTableTransactWrite transaction = new MultiTableTransactWrite(transactionParts); return transaction.ExecuteAsync(cancellationToken); } #endregion #region Scan async /// /// Configures an async Scan operation against DynamoDB, finding items /// that match the specified conditions. /// /// Type of object. /// /// Conditions that the results should meet. /// /// Config object which can be used to override that table used. /// AsyncSearch which can be used to retrieve DynamoDB data. public AsyncSearch ScanAsync(IEnumerable conditions, DynamoDBOperationConfig operationConfig = null) { var scan = ConvertScan(conditions, operationConfig); return FromSearchAsync(scan); } /// /// Configures an async Scan operation against DynamoDB, finding items /// that match the specified conditions. /// /// Type of object. /// Scan request object. /// Config object which can be used to override the table used. /// AsyncSearch which can be used to retrieve DynamoDB data. public AsyncSearch FromScanAsync(ScanOperationConfig scanConfig, DynamoDBOperationConfig operationConfig = null) { if (scanConfig == null) throw new ArgumentNullException("scanConfig"); var search = ConvertFromScan(scanConfig, operationConfig); return FromSearchAsync(search); } #endregion #region Query async /// /// Configures an async Query operation against DynamoDB, finding items /// that match the specified hash primary key. /// /// Type of object. /// Hash key of the items to query. /// Config object which can be used to override the table used. /// AsyncSearch which can be used to retrieve DynamoDB data. public AsyncSearch QueryAsync(object hashKeyValue, DynamoDBOperationConfig operationConfig = null) { var query = ConvertQueryByValue(hashKeyValue, null, operationConfig); return FromSearchAsync(query); } /// /// Configures an async Query operation against DynamoDB, finding items /// that match the specified range element condition for a hash-and-range primary key. /// /// Type of object. /// Hash key of the items to query. /// Operation of the condition. /// /// Value(s) of the condition. /// For all operations except QueryOperator.Between, values should be one value. /// For QueryOperator.Between, values should be two values. /// /// Config object which can be used to override the table used. /// AsyncSearch which can be used to retrieve DynamoDB data. public AsyncSearch QueryAsync(object hashKeyValue, QueryOperator op, IEnumerable values, DynamoDBOperationConfig operationConfig = null) { if (values == null) throw new ArgumentNullException("values"); var query = ConvertQueryByValue(hashKeyValue, op, values, operationConfig); return FromSearchAsync(query); } /// /// Configures an async Query operation against DynamoDB, finding items /// that match the specified conditions. /// /// Type of object. /// Query request object. /// Config object which can be used to override the table used. /// AsyncSearch which can be used to retrieve DynamoDB data. public AsyncSearch FromQueryAsync(QueryOperationConfig queryConfig, DynamoDBOperationConfig operationConfig = null) { if (queryConfig == null) throw new ArgumentNullException("queryConfig"); var search = ConvertFromQuery(queryConfig, operationConfig); return FromSearchAsync(search); } #endregion } }