/* * Copyright 2012-2013 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; using Amazon.Runtime; 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 Factory Creates /// /// Creates a strongly-typed BatchWrite object, allowing /// a batch-write operation against DynamoDB. /// /// Type of objects to write /// Empty strongly-typed BatchWrite object /// The callback that will be invoked when the asynchronous operation completes. /// An instance of AsyncOptions that specifies how the async method should be executed. public void CreateBatchWriteAsync(AmazonDynamoDBCallback> callback, AsyncOptions asyncOptions = null) { CreateBatchWriteAsync(null, callback, asyncOptions); } /// /// Creates a strongly-typed BatchWrite object, allowing /// a batch-write operation against DynamoDB. /// /// Type of objects to write /// Config object which can be used to override that table used. /// The callback that will be invoked when the asynchronous operation completes. /// An instance of AsyncOptions that specifies how the async method should be executed. public void CreateBatchWriteAsync(DynamoDBOperationConfig operationConfig, AmazonDynamoDBCallback> callback, AsyncOptions asyncOptions = null) { asyncOptions = asyncOptions ?? new AsyncOptions(); DynamoDBAsyncExecutor.ExecuteAsync( () => { DynamoDBFlatConfig config = new DynamoDBFlatConfig(operationConfig, this.Config); return new BatchWrite(this, config); ; }, asyncOptions, callback); } #endregion #region Save async /// /// Initiates the asynchronous execution of the Save operation. /// /// Type to save as. /// Object to save. /// The callback that will be invoked when the asynchronous operation completes. /// An instance of AsyncOptions that specifies how the async method should be executed. public void SaveAsync(T value, AmazonDynamoDBCallback callback, AsyncOptions asyncOptions = null) { asyncOptions = asyncOptions ?? new AsyncOptions(); DynamoDBAsyncExecutor.ExecuteAsync( () => { SaveHelper(value, null); }, asyncOptions, callback); } /// /// Initiates the asynchronous execution of the Save operation. /// /// Type to save as. /// Object to save. /// Overriding configuration. /// The callback that will be invoked when the asynchronous operation completes. /// An instance of AsyncOptions that specifies how the async method should be executed. public void SaveAsync(T value, DynamoDBOperationConfig operationConfig, AmazonDynamoDBCallback callback, AsyncOptions asyncOptions = null) { asyncOptions = asyncOptions ?? new AsyncOptions(); DynamoDBAsyncExecutor.ExecuteAsync( () => { SaveHelper(value, operationConfig); }, asyncOptions, callback); } #endregion #region Load async /// /// Initiates the asynchronous execution of the Load operation. /// /// Type to populate. /// Hash key element of the target item. /// The callback that will be invoked when the asynchronous operation completes. /// An instance of AsyncOptions that specifies how the async method should be executed. public void LoadAsync(object hashKey, AmazonDynamoDBCallback callback, AsyncOptions asyncOptions = null) { asyncOptions = asyncOptions ?? new AsyncOptions(); DynamoDBAsyncExecutor.ExecuteAsync( () => { return LoadHelper(hashKey, null, null); }, asyncOptions, callback); } /// /// 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. /// The callback that will be invoked when the asynchronous operation completes. /// An instance of AsyncOptions that specifies how the async method should be executed. public void LoadAsync(object hashKey, object rangeKey, AmazonDynamoDBCallback callback, AsyncOptions asyncOptions = null) { asyncOptions = asyncOptions ?? new AsyncOptions(); DynamoDBAsyncExecutor.ExecuteAsync( () => { return LoadHelper(hashKey, rangeKey, null); }, asyncOptions, callback); } /// /// Initiates the asynchronous execution of the Load operation. /// /// Type to populate. /// Hash key element of the target item. /// Overriding configuration. /// The callback that will be invoked when the asynchronous operation completes. /// An instance of AsyncOptions that specifies how the async method should be executed. public void LoadAsync(object hashKey, DynamoDBOperationConfig operationConfig, AmazonDynamoDBCallback callback, AsyncOptions asyncOptions = null) { asyncOptions = asyncOptions ?? new AsyncOptions(); DynamoDBAsyncExecutor.ExecuteAsync( () => { return LoadHelper(hashKey, null, operationConfig); }, asyncOptions, callback); } /// /// 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. /// The callback that will be invoked when the asynchronous operation completes. /// An instance of AsyncOptions that specifies how the async method should be executed. public void LoadAsync(object hashKey, object rangeKey, DynamoDBOperationConfig operationConfig, AmazonDynamoDBCallback callback, AsyncOptions asyncOptions = null) { asyncOptions = asyncOptions ?? new AsyncOptions(); DynamoDBAsyncExecutor.ExecuteAsync( () => { return LoadHelper(hashKey, rangeKey, operationConfig); }, asyncOptions, callback); } /// /// Initiates the asynchronous execution of the Load operation. /// /// Type to populate. /// Key of the target item. /// The callback that will be invoked when the asynchronous operation completes. /// An instance of AsyncOptions that specifies how the async method should be executed. public void LoadAsync(T keyObject, AmazonDynamoDBCallback callback, AsyncOptions asyncOptions = null) { asyncOptions = asyncOptions ?? new AsyncOptions(); DynamoDBAsyncExecutor.ExecuteAsync( () => { return LoadHelper(keyObject, null); }, asyncOptions, callback); } /// /// Initiates the asynchronous execution of the Load operation. /// /// Type to populate. /// Key of the target item. /// Overriding configuration. /// The callback that will be invoked when the asynchronous operation completes. /// An instance of AsyncOptions that specifies how the async method should be executed. public void LoadAsync(T keyObject, DynamoDBOperationConfig operationConfig, AmazonDynamoDBCallback callback, AsyncOptions asyncOptions = null) { asyncOptions = asyncOptions ?? new AsyncOptions(); DynamoDBAsyncExecutor.ExecuteAsync( () => { return LoadHelper(keyObject, operationConfig); }, asyncOptions, callback); } #endregion #region Delete async /// /// Initiates the asynchronous execution of the Delete operation. /// /// Type of object. /// Object to delete. /// The callback that will be invoked when the asynchronous operation completes. /// An instance of AsyncOptions that specifies how the async method should be executed. public void DeleteAsync(T value, AmazonDynamoDBCallback callback, AsyncOptions asyncOptions = null) { asyncOptions = asyncOptions ?? new AsyncOptions(); DynamoDBAsyncExecutor.ExecuteAsync( () => { DeleteHelper(value, null); }, asyncOptions, callback); } /// /// Initiates the asynchronous execution of the Delete operation. /// /// Type of object. /// Object to delete. /// Overriding configuration. /// The callback that will be invoked when the asynchronous operation completes. /// An instance of AsyncOptions that specifies how the async method should be executed. public void DeleteAsync(T value, DynamoDBOperationConfig operationConfig, AmazonDynamoDBCallback callback, AsyncOptions asyncOptions = null) { asyncOptions = asyncOptions ?? new AsyncOptions(); DynamoDBAsyncExecutor.ExecuteAsync( () => { DeleteHelper(value, operationConfig); }, asyncOptions, callback); } /// /// Initiates the asynchronous execution of the Delete operation. /// /// Type of object. /// Hash key element of the object to delete. /// The callback that will be invoked when the asynchronous operation completes. /// An instance of AsyncOptions that specifies how the async method should be executed. public void DeleteAsync(object hashKey, AmazonDynamoDBCallback callback, AsyncOptions asyncOptions = null) { asyncOptions = asyncOptions ?? new AsyncOptions(); DynamoDBAsyncExecutor.ExecuteAsync( () => { DeleteHelper(hashKey, null, null); }, asyncOptions, callback); } /// /// 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. /// The callback that will be invoked when the asynchronous operation completes. /// An instance of AsyncOptions that specifies how the async method should be executed. public void DeleteAsync(object hashKey, DynamoDBOperationConfig operationConfig, AmazonDynamoDBCallback callback, AsyncOptions asyncOptions = null) { asyncOptions = asyncOptions ?? new AsyncOptions(); DynamoDBAsyncExecutor.ExecuteAsync( () => { DeleteHelper(hashKey, null, operationConfig); }, asyncOptions, callback); } /// /// 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. /// The callback that will be invoked when the asynchronous operation completes. /// An instance of AsyncOptions that specifies how the async method should be executed. public void DeleteAsync(object hashKey, object rangeKey, AmazonDynamoDBCallback callback, AsyncOptions asyncOptions = null) { asyncOptions = asyncOptions ?? new AsyncOptions(); DynamoDBAsyncExecutor.ExecuteAsync( () => { DeleteHelper(hashKey, rangeKey, null); }, asyncOptions, callback); } /// /// 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. /// The callback that will be invoked when the asynchronous operation completes. /// An instance of AsyncOptions that specifies how the async method should be executed. public void DeleteAsync(object hashKey, object rangeKey, DynamoDBOperationConfig operationConfig, AmazonDynamoDBCallback callback, AsyncOptions asyncOptions = null) { asyncOptions = asyncOptions ?? new AsyncOptions(); DynamoDBAsyncExecutor.ExecuteAsync( () => { DeleteHelper(hashKey, rangeKey, operationConfig); }, asyncOptions, callback); } #endregion #region BatchGet async /// /// Initiates the asynchronous execution of the ExecuteBatchGet operation. /// /// Configured BatchGet objects /// The callback that will be invoked when the asynchronous operation completes. /// An instance of AsyncOptions that specifies how the async method should be executed. public void ExecuteBatchGetAsync(BatchGet[] batches, AmazonDynamoDBCallback callback, AsyncOptions asyncOptions = null) { asyncOptions = asyncOptions ?? new AsyncOptions(); MultiTableBatchGet superBatch = new MultiTableBatchGet(batches); DynamoDBAsyncExecutor.ExecuteAsync( () => { superBatch.ExecuteHelper(); }, asyncOptions, callback); } #endregion #region BatchWrite async /// /// Initiates the asynchronous execution of the ExecuteBatchWrite operation. /// /// Configured BatchWrite objects /// The callback that will be invoked when the asynchronous operation completes. /// An instance of AsyncOptions that specifies how the async method should be executed. public void ExecuteBatchWriteAsync(BatchWrite[] batches, AmazonDynamoDBCallback callback, AsyncOptions asyncOptions = null) { asyncOptions = asyncOptions ?? new AsyncOptions(); MultiTableBatchWrite superBatch = new MultiTableBatchWrite(batches); DynamoDBAsyncExecutor.ExecuteAsync( () => { superBatch.ExecuteHelper(); }, asyncOptions, callback); } #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); } /// /// 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. /// The callback that will be invoked when the asynchronous operation completes. /// An instance of AsyncOptions that specifies how the async method should be executed. public void ScanAsync(IEnumerable conditions, DynamoDBOperationConfig operationConfig, AmazonDynamoDBCallback> callback, AsyncOptions asyncOptions = null) { asyncOptions = asyncOptions ?? new AsyncOptions(); DynamoDBAsyncExecutor.ExecuteAsync>( () => { if (conditions == null) throw new ArgumentNullException("conditions"); var scan = ConvertScan(conditions, operationConfig); return FromSearchAsync(scan); }, asyncOptions, callback); } /// /// 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. /// The callback that will be invoked when the asynchronous operation completes. /// An instance of AsyncOptions that specifies how the async method should be executed. public void FromScanAsync(ScanOperationConfig scanConfig, DynamoDBOperationConfig operationConfig, AmazonDynamoDBCallback> callback, AsyncOptions asyncOptions = null) { asyncOptions = asyncOptions ?? new AsyncOptions(); DynamoDBAsyncExecutor.ExecuteAsync>( () => { if (scanConfig == null) throw new ArgumentNullException("scanConfig"); var scan = ConvertFromScan(scanConfig, operationConfig); return FromSearchAsync(scan); }, asyncOptions, callback); } #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 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); } /// /// 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. 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. /// The callback that will be invoked when the asynchronous operation completes. /// An instance of AsyncOptions that specifies how the async method should be executed. public void FromQueryAsync(QueryOperationConfig queryConfig, AmazonDynamoDBCallback> callback, AsyncOptions asyncOptions = null) { FromQueryAsync(queryConfig, null, callback, asyncOptions); } /// /// 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. /// The callback that will be invoked when the asynchronous operation completes. /// An instance of AsyncOptions that specifies how the async method should be executed. public void FromQueryAsync(QueryOperationConfig queryConfig, DynamoDBOperationConfig operationConfig, AmazonDynamoDBCallback> callback, AsyncOptions asyncOptions = null) { asyncOptions = asyncOptions ?? new AsyncOptions(); DynamoDBAsyncExecutor.ExecuteAsync>( () => { if (queryConfig == null) throw new ArgumentNullException("queryConfig"); var query = ConvertFromQuery(queryConfig, operationConfig); return FromSearchAsync(query); }, asyncOptions, callback); } /// /// 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. /// The callback that will be invoked when the asynchronous operation completes. /// An instance of AsyncOptions that specifies how the async method should be executed. public void QueryAsync(object hashKeyValue, AmazonDynamoDBCallback> callback, AsyncOptions asyncOptions = null) { asyncOptions = asyncOptions ?? new AsyncOptions(); DynamoDBAsyncExecutor.ExecuteAsync>( () => { var query = ConvertQueryByValue(hashKeyValue, null, null); return FromSearchAsync(query); }, asyncOptions, callback); } /// /// 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. /// The callback that will be invoked when the asynchronous operation completes. /// An instance of AsyncOptions that specifies how the async method should be executed. public void QueryAsync(object hashKeyValue, DynamoDBOperationConfig operationConfig, AmazonDynamoDBCallback> callback, AsyncOptions asyncOptions = null) { asyncOptions = asyncOptions ?? new AsyncOptions(); DynamoDBAsyncExecutor.ExecuteAsync>( () => { ContextSearch query = ConvertQueryByValue(hashKeyValue, null, operationConfig); return FromSearchAsync(query); }, asyncOptions, callback); } /// /// 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. /// /// The callback that will be invoked when the asynchronous operation completes. /// An instance of AsyncOptions that specifies how the async method should be executed. public void QueryAsync(object hashKeyValue, QueryOperator op, AmazonDynamoDBCallback> callback, AsyncOptions asyncOptions = null, params object[] values) { if (values == null || values.Length == 0) throw new ArgumentOutOfRangeException("values"); QueryAsync(hashKeyValue, op, values, null, callback, asyncOptions); } /// /// 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. /// The callback that will be invoked when the asynchronous operation completes. /// An instance of AsyncOptions that specifies how the async method should be executed. public void QueryAsync(object hashKeyValue, QueryOperator op, IEnumerable values, DynamoDBOperationConfig operationConfig, AmazonDynamoDBCallback> callback, AsyncOptions asyncOptions = null) { asyncOptions = asyncOptions ?? new AsyncOptions(); DynamoDBAsyncExecutor.ExecuteAsync>( () => { if (values == null) throw new ArgumentNullException("values"); ContextSearch query = ConvertQueryByValue(hashKeyValue, op, values, operationConfig); return FromSearchAsync(query); }, asyncOptions, callback); } #endregion } }