/* * 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. */ using System; using System.Collections.Generic; using System.Linq; using Amazon.DynamoDBv2.DocumentModel; using Amazon.DynamoDBv2.Model; 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. /// An AsyncCallback delegate that is invoked when the operation completes. /// A user-defined state object that is passed to the callback procedure. Retrieve this object from within the callback /// procedure using the AsyncState property. /// An IAsyncResult that can be used to poll or wait for results, or both; this value is also needed when invoking EndSave /// operation. public IAsyncResult BeginSave(T value, AsyncCallback callback, object state) { return DynamoDBAsyncExecutor.BeginOperation(() => { SaveHelper(value, null); return null; }, callback, state); } /// /// Initiates the asynchronous execution of the Save operation. /// /// /// Type to save as. /// Object to save. /// Overriding configuration. /// An AsyncCallback delegate that is invoked when the operation completes. /// A user-defined state object that is passed to the callback procedure. Retrieve this object from within the callback /// procedure using the AsyncState property. /// An IAsyncResult that can be used to poll or wait for results, or both; this value is also needed when invoking EndSave /// operation. public IAsyncResult BeginSave(T value, DynamoDBOperationConfig operationConfig, AsyncCallback callback, object state) { return DynamoDBAsyncExecutor.BeginOperation(() => { SaveHelper(value, operationConfig); return null; }, callback, state); } /// /// Finishes the asynchronous execution of the Save operation. /// /// /// The IAsyncResult returned by the call to BeginSave. public void EndSave(IAsyncResult asyncResult) { DynamoDBAsyncExecutor.EndOperation(asyncResult); } #endregion #region Load async /// /// Initiates the asynchronous execution of the Load operation. /// /// /// Type to populate. /// Hash key element of the target item. /// An AsyncCallback delegate that is invoked when the operation completes. /// A user-defined state object that is passed to the callback procedure. Retrieve this object from within the callback /// procedure using the AsyncState property. /// An IAsyncResult that can be used to poll or wait for results, or both; this value is also needed when invoking EndLoad /// operation. public IAsyncResult BeginLoad(object hashKey, AsyncCallback callback, object state) { return DynamoDBAsyncExecutor.BeginOperation(() => LoadHelper(hashKey, null, null), callback, state); } /// /// 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. /// An AsyncCallback delegate that is invoked when the operation completes. /// A user-defined state object that is passed to the callback procedure. Retrieve this object from within the callback /// procedure using the AsyncState property. /// An IAsyncResult that can be used to poll or wait for results, or both; this value is also needed when invoking EndLoad /// operation. public IAsyncResult BeginLoad(object hashKey, object rangeKey, AsyncCallback callback, object state) { return DynamoDBAsyncExecutor.BeginOperation(() => LoadHelper(hashKey, rangeKey, null), callback, state); } /// /// Initiates the asynchronous execution of the Load operation. /// /// /// Type to populate. /// Hash key element of the target item. /// Overriding configuration. /// An AsyncCallback delegate that is invoked when the operation completes. /// A user-defined state object that is passed to the callback procedure. Retrieve this object from within the callback /// procedure using the AsyncState property. /// An IAsyncResult that can be used to poll or wait for results, or both; this value is also needed when invoking EndLoad /// operation. public IAsyncResult BeginLoad(object hashKey, DynamoDBOperationConfig operationConfig, AsyncCallback callback, object state) { return DynamoDBAsyncExecutor.BeginOperation(() => LoadHelper(hashKey, null, operationConfig), callback, state); } /// /// 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. /// An AsyncCallback delegate that is invoked when the operation completes. /// A user-defined state object that is passed to the callback procedure. Retrieve this object from within the callback /// procedure using the AsyncState property. /// An IAsyncResult that can be used to poll or wait for results, or both; this value is also needed when invoking EndLoad /// operation. public IAsyncResult BeginLoad(object hashKey, object rangeKey, DynamoDBOperationConfig operationConfig, AsyncCallback callback, object state) { return DynamoDBAsyncExecutor.BeginOperation(() => LoadHelper(hashKey, rangeKey, operationConfig), callback, state); } /// /// Initiates the asynchronous execution of the Load operation. /// /// /// Type to populate. /// Key of the target item. /// An AsyncCallback delegate that is invoked when the operation completes. /// A user-defined state object that is passed to the callback procedure. Retrieve this object from within the callback /// procedure using the AsyncState property. /// An IAsyncResult that can be used to poll or wait for results, or both; this value is also needed when invoking EndLoad /// operation. public IAsyncResult BeginLoad(T keyObject, AsyncCallback callback, object state) { return DynamoDBAsyncExecutor.BeginOperation(() => LoadHelper(keyObject, null), callback, state); } /// /// Initiates the asynchronous execution of the Load operation. /// /// /// Type to populate. /// Key of the target item. /// Overriding configuration. /// An AsyncCallback delegate that is invoked when the operation completes. /// A user-defined state object that is passed to the callback procedure. Retrieve this object from within the callback /// procedure using the AsyncState property. /// An IAsyncResult that can be used to poll or wait for results, or both; this value is also needed when invoking EndLoad /// operation. public IAsyncResult BeginLoad(T keyObject, DynamoDBOperationConfig operationConfig, AsyncCallback callback, object state) { return DynamoDBAsyncExecutor.BeginOperation(() => LoadHelper(keyObject, operationConfig), callback, state); } /// /// Finishes the asynchronous execution of the Load operation. /// /// /// The IAsyncResult returned by the call to BeginLoad. /// /// Object of type T, populated with properties of item loaded from DynamoDB. /// public T EndLoad(IAsyncResult asyncResult) { return DynamoDBAsyncExecutor.EndOperation(asyncResult); } #endregion #region Delete async /// /// Initiates the asynchronous execution of the Delete operation. /// /// /// Type of object. /// Object to delete. /// An AsyncCallback delegate that is invoked when the operation completes. /// A user-defined state object that is passed to the callback procedure. Retrieve this object from within the callback /// procedure using the AsyncState property. /// An IAsyncResult that can be used to poll or wait for results, or both; this value is also needed when invoking EndDelete /// operation. public IAsyncResult BeginDelete(T value, AsyncCallback callback, object state) { return DynamoDBAsyncExecutor.BeginOperation(() => { DeleteHelper(value, null); return null; }, callback, state); } /// /// Initiates the asynchronous execution of the Delete operation. /// /// /// Type of object. /// Object to delete. /// Overriding configuration. /// An AsyncCallback delegate that is invoked when the operation completes. /// A user-defined state object that is passed to the callback procedure. Retrieve this object from within the callback /// procedure using the AsyncState property. /// An IAsyncResult that can be used to poll or wait for results, or both; this value is also needed when invoking EndDelete /// operation. public IAsyncResult BeginDelete(T value, DynamoDBOperationConfig operationConfig, AsyncCallback callback, object state) { return DynamoDBAsyncExecutor.BeginOperation(() => { DeleteHelper(value, operationConfig); return null; }, callback, state); } /// /// Initiates the asynchronous execution of the Delete operation. /// /// /// Type of object. /// Hash key element of the object to delete. /// An AsyncCallback delegate that is invoked when the operation completes. /// A user-defined state object that is passed to the callback procedure. Retrieve this object from within the callback /// procedure using the AsyncState property. /// An IAsyncResult that can be used to poll or wait for results, or both; this value is also needed when invoking EndDelete /// operation. public IAsyncResult BeginDelete(object hashKey, AsyncCallback callback, object state) { return DynamoDBAsyncExecutor.BeginOperation(() => { DeleteHelper(hashKey, null, null); return null; }, callback, state); } /// /// 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. /// An AsyncCallback delegate that is invoked when the operation completes. /// A user-defined state object that is passed to the callback procedure. Retrieve this object from within the callback /// procedure using the AsyncState property. /// An IAsyncResult that can be used to poll or wait for results, or both; this value is also needed when invoking EndDelete /// operation. public IAsyncResult BeginDelete(object hashKey, DynamoDBOperationConfig operationConfig, AsyncCallback callback, object state) { return DynamoDBAsyncExecutor.BeginOperation(() => { DeleteHelper(hashKey, null, operationConfig); return null; }, callback, state); } /// /// 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. /// An AsyncCallback delegate that is invoked when the operation completes. /// A user-defined state object that is passed to the callback procedure. Retrieve this object from within the callback /// procedure using the AsyncState property. /// An IAsyncResult that can be used to poll or wait for results, or both; this value is also needed when invoking EndDelete /// operation. public IAsyncResult BeginDelete(object hashKey, object rangeKey, AsyncCallback callback, object state) { return DynamoDBAsyncExecutor.BeginOperation(() => { DeleteHelper(hashKey, rangeKey, null); return null; }, callback, state); } /// /// 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. /// An AsyncCallback delegate that is invoked when the operation completes. /// A user-defined state object that is passed to the callback procedure. Retrieve this object from within the callback /// procedure using the AsyncState property. /// An IAsyncResult that can be used to poll or wait for results, or both; this value is also needed when invoking EndDelete /// operation. public IAsyncResult BeginDelete(object hashKey, object rangeKey, DynamoDBOperationConfig operationConfig, AsyncCallback callback, object state) { return DynamoDBAsyncExecutor.BeginOperation(() => { DeleteHelper(hashKey, rangeKey, operationConfig); return null; }, callback, state); } /// /// Finishes the asynchronous execution of the Delete operation. /// /// /// The IAsyncResult returned by the call to BeginDelete. public void EndDelete(IAsyncResult asyncResult) { DynamoDBAsyncExecutor.EndOperation(asyncResult); } #endregion #region BatchGet async /// /// Issues a batch-get request with multiple batches. /// /// Results are stored in the individual batches. /// /// Configured BatchGet objects /// An AsyncCallback delegate that is invoked when the operation completes. /// A user-defined state object that is passed to the callback procedure. Retrieve this object from within the callback /// procedure using the AsyncState property. /// An IAsyncResult that can be used to poll or wait for results, or both; this value is also needed when invoking EndExecuteBatchGet /// operation. public IAsyncResult BeginExecuteBatchGet(BatchGet[] batches, AsyncCallback callback, object state) { MultiTableBatchGet superBatch = new MultiTableBatchGet(batches); return DynamoDBAsyncExecutor.BeginOperation(() => { superBatch.ExecuteHelper(); return null; }, callback, state); } /// /// Finishes the asynchronous execution of the ExecuteBatchGet operation. /// /// /// The IAsyncResult returned by the call to BeginExecuteBatchGet. public void EndExecuteBatchGet(IAsyncResult asyncResult) { DynamoDBAsyncExecutor.EndOperation(asyncResult); } #endregion #region BatchWrite async /// /// Issues a batch-write request with multiple batches. /// /// Configured BatchWrite objects /// An AsyncCallback delegate that is invoked when the operation completes. /// A user-defined state object that is passed to the callback procedure. Retrieve this object from within the callback /// procedure using the AsyncState property. /// An IAsyncResult that can be used to poll or wait for results, or both; this value is also needed when invoking EndDelete /// operation. public IAsyncResult BeginExecuteBatchWrite(BatchWrite[] batches, AsyncCallback callback, object state) { MultiTableBatchWrite superBatch = new MultiTableBatchWrite(batches); return DynamoDBAsyncExecutor.BeginOperation(() => { superBatch.ExecuteHelper(); return null; }, callback, state); } /// /// Finishes the asynchronous execution of the ExecuteBatchWrite operation. /// /// /// The IAsyncResult returned by the call to BeginExecuteBatchWrite. public void EndExecuteBatchWrite(IAsyncResult asyncResult) { DynamoDBAsyncExecutor.EndOperation(asyncResult); } #endregion #region TransactGet async /// /// Issues a transactional get request with multiple TransactGet objects. /// Results are stored in the individual TransactGet objects. /// /// Configured TransactGet objects. /// An AsyncCallback delegate that is invoked when the operation completes. /// /// A user-defined state object that is passed to the callback procedure. Retrieve this object from within the callback /// procedure using the AsyncState property. /// /// /// An IAsyncResult that can be used to poll or wait for results, or both; this value is also needed when invoking EndExecuteBatchGet /// operation. /// public IAsyncResult BeginExecuteTransactGet(TransactGet[] transactionParts, AsyncCallback callback, object state) { MultiTableTransactGet transaction = new MultiTableTransactGet(transactionParts); return DynamoDBAsyncExecutor.BeginOperation(() => { transaction.ExecuteHelper(); return null; }, callback, state); } /// /// Finishes the asynchronous execution of the ExecuteTransactGet operation. /// /// /// The IAsyncResult returned by the call to BeginExecuteTransactGet. public void EndExecuteTransactGet(IAsyncResult asyncResult) { DynamoDBAsyncExecutor.EndOperation(asyncResult); } #endregion #region TransactWrite async /// /// Issues a transactional write request with multiple TransactWrite objects. /// /// Configured TransactWrite objects. /// An AsyncCallback delegate that is invoked when the operation completes. /// /// A user-defined state object that is passed to the callback procedure. Retrieve this object from within the callback /// procedure using the AsyncState property. /// /// /// An IAsyncResult that can be used to poll or wait for results, or both; this value is also needed when invoking EndDelete /// operation. /// public IAsyncResult BeginExecuteTransactWrite(TransactWrite[] transactionParts, AsyncCallback callback, object state) { MultiTableTransactWrite transaction = new MultiTableTransactWrite(transactionParts); return DynamoDBAsyncExecutor.BeginOperation(() => { transaction.ExecuteHelper(); return null; }, callback, state); } /// /// Finishes the asynchronous execution of the ExecuteTransactWrite operation. /// /// /// The IAsyncResult returned by the call to BeginExecuteTransactWrite. public void EndExecuteTransactWrite(IAsyncResult asyncResult) { DynamoDBAsyncExecutor.EndOperation(asyncResult); } #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. /// /// AsyncSearch which can be used to retrieve DynamoDB data. public AsyncSearch ScanAsync(params ScanCondition[] conditions) { if (conditions == null) throw new ArgumentNullException("conditions"); return ScanAsync(conditions, null); } /// /// 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) { if (conditions == null) throw new ArgumentNullException("conditions"); 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. /// AsyncSearch which can be used to retrieve DynamoDB data. public AsyncSearch FromScanAsync(ScanOperationConfig scanConfig) { return FromScanAsync(scanConfig, null); } /// /// 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) { if (scanConfig == null) throw new ArgumentNullException("scanConfig"); var scan = ConvertFromScan(scanConfig, operationConfig); return FromSearchAsync(scan); } #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. /// AsyncSearch which can be used to retrieve DynamoDB data. public AsyncSearch QueryAsync(object hashKeyValue) { var query = ConvertQueryByValue(hashKeyValue, null, null); return FromSearchAsync(query); } /// /// 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) { ContextSearch 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. /// /// AsyncSearch which can be used to retrieve DynamoDB data. public AsyncSearch QueryAsync(object hashKeyValue, QueryOperator op, params object[] values) { if (values == null || values.Length == 0) throw new ArgumentOutOfRangeException("values"); return QueryAsync(hashKeyValue, op, values, null); } /// /// 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) { if (values == null) throw new ArgumentNullException("values"); ContextSearch 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. /// AsyncSearch which can be used to retrieve DynamoDB data. public AsyncSearch FromQueryAsync(QueryOperationConfig queryConfig) { return FromQueryAsync(queryConfig, null); } /// /// 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) { if (queryConfig == null) throw new ArgumentNullException("queryConfig"); var query = ConvertFromQuery(queryConfig, operationConfig); return FromSearchAsync(query); } #endregion } }