/* * 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/serialize /// /// Saves an object to DynamoDB. /// /// Uses DynamoDBContextConfig configured on the context. /// Type must be marked up with DynamoDBTableAttribute and at least /// one public field/property with DynamoDBHashKeyAttribute. /// /// Type to save as. /// Object to save. public void Save(T value) { Save(value, null); } /// /// Saves an object to DynamoDB using passed-in configs. /// /// Passed-in config overrides DynamoDBContextConfig on the context. /// Type must be marked up with DynamoDBTableAttribute and at least /// one public field/property with DynamoDBHashKeyAttribute. /// /// Type to save as. /// Object to save. /// Overriding configuration. public void Save(T value, DynamoDBOperationConfig operationConfig) { SaveHelper(value, operationConfig); } #endregion #region Load/deserialize /// /// Loads an object from DynamoDB for the given hash primary key. /// /// Uses DynamoDBContextConfig configured on the context. /// Type must be marked up with DynamoDBTableAttribute and at least /// one public field/property with DynamoDBHashKeyAttribute. /// /// Type to populate. /// Hash key element of the target item. /// /// Object of type T, populated with properties of item loaded from DynamoDB. /// public T Load(object hashKey) { return Load(hashKey, null, null); } /// /// Loads an object from DynamoDB for the given hash primary key and using the given config. /// /// Passed-in config overrides DynamoDBContextConfig on the context. /// Type must be marked up with DynamoDBTableAttribute and at least /// one public field/property with DynamoDBHashKeyAttribute. /// /// Type to populate. /// Hash key element of the target item. /// Overriding configuration. /// /// Object of type T, populated with properties of item loaded from DynamoDB. /// public T Load(object hashKey, DynamoDBOperationConfig operationConfig) { return Load(hashKey, null, operationConfig); } /// /// Loads an object from DynamoDB for the given hash-and-range primary key. /// /// Uses DynamoDBContextConfig configured on the context. /// Type must be marked up with DynamoDBTableAttribute and at least /// one public field/property with DynamoDBHashKeyAttribute. /// /// Type to populate. /// Hash key element of the target item. /// Range key element of the target item. /// /// Object of type T, populated with properties of item loaded from DynamoDB. /// public T Load(object hashKey, object rangeKey) { return Load(hashKey, rangeKey, null); } /// /// Loads an object from DynamoDB for the given hash-and-range primary key and using the given config. /// /// Passed-in config overrides DynamoDBContextConfig on the context. /// Type must be marked up with DynamoDBTableAttribute and at least /// one public field/property with DynamoDBHashKeyAttribute. /// /// Type to populate. /// Hash key element of the target item. /// Range key element of the target item. /// Overriding configuration. /// /// Object of type T, populated with properties of item loaded from DynamoDB. /// public T Load(object hashKey, object rangeKey, DynamoDBOperationConfig operationConfig) { return LoadHelper(hashKey, rangeKey, operationConfig); } /// /// Loads an object from DynamoDB for the given key. /// The keyObject is a partially-specified instance, where the /// hash/range properties are equal to the key of the item you /// want to load. /// /// Uses DynamoDBContextConfig configured on the context. /// Type must be marked up with DynamoDBTableAttribute and at least /// one public field/property with DynamoDBHashKeyAttribute. /// /// Type to populate. /// Key object defining the the target item. /// /// Object of type T, populated with properties of item loaded from DynamoDB. /// public T Load(T keyObject) { return Load(keyObject, null); } /// /// Loads an object from DynamoDB for the given key and using the given config. /// The keyObject is a partially-specified instance, where the /// hash/range properties are equal to the key of the item you /// want to load. /// /// Passed-in config overrides DynamoDBContextConfig on the context. /// Type must be marked up with DynamoDBTableAttribute and at least /// one public field/property with DynamoDBHashKeyAttribute. /// /// Type to populate. /// Key object defining the the target item. /// Overriding configuration. /// /// Object of type T, populated with properties of item loaded from DynamoDB. /// public T Load(T keyObject, DynamoDBOperationConfig operationConfig) { return LoadHelper(keyObject, operationConfig); } #endregion #region Delete /// /// Deletes an item in DynamoDB corresponding to given object. /// /// Uses DynamoDBContextConfig configured on the context. /// If SkipVersionCheck=false, will check version of object before deleting. /// Type must be marked up with DynamoDBTableAttribute and at least /// one public field/property with DynamoDBHashKeyAttribute. /// /// Type of object. /// Object to delete. public void Delete(T value) { Delete(value, null); } /// /// Deletes an item in DynamoDB corresponding to given object. /// /// Passed-in config overrides DynamoDBContextConfig on the context. /// If SkipVersionCheck=false, will check version of object before deleting. /// Type must be marked up with DynamoDBTableAttribute and at least /// one public field/property with DynamoDBHashKeyAttribute. /// /// Type of object. /// Object to delete. /// Overriding configuration. public void Delete(T value, DynamoDBOperationConfig operationConfig) { DeleteHelper(value, operationConfig); } /// /// Deletes an item in DynamoDB corresponding to a given hash primary key. /// /// No version check is done prior to delete. /// Type must be marked up with DynamoDBTableAttribute and at least /// one public field/property with DynamoDBHashKeyAttribute. /// /// Type of object. /// Hash key element of the object to delete. public void Delete(object hashKey) { Delete(hashKey, null); } /// /// Deletes an item in DynamoDB corresponding to a given hash primary key. /// /// No version check is done prior to delete. /// Type must be marked up with DynamoDBTableAttribute and at least /// one public field/property with DynamoDBHashKeyAttribute. /// /// Type of object. /// Hash key element of the object to delete. /// Config object which can be used to override that table used. public void Delete(object hashKey, DynamoDBOperationConfig operationConfig) { Delete(hashKey, null, operationConfig); } /// /// Deletes an item in DynamoDB corresponding to a given hash-and-range primary key. /// /// No version check is done prior to delete. /// Type must be marked up with DynamoDBTableAttribute and at least /// one public field/property with DynamoDBHashKeyAttribute. /// /// Type of object. /// Hash key element of the object to delete. /// Range key element of the object to delete. public void Delete(object hashKey, object rangeKey) { Delete(hashKey, rangeKey, null); } /// /// Deletes an item in DynamoDB corresponding to a given hash-and-range primary key. /// /// No version check is done prior to delete. /// Type must be marked up with DynamoDBTableAttribute and at least /// one public field/property with DynamoDBHashKeyAttribute. /// /// 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. public void Delete(object hashKey, object rangeKey, DynamoDBOperationConfig operationConfig) { DeleteHelper(hashKey, rangeKey, operationConfig); } #endregion #region BatchGet /// /// Issues a batch-get request with multiple batches. /// /// Results are stored in the individual batches. /// /// /// Configured BatchGet objects /// public void ExecuteBatchGet(params BatchGet[] batches) { MultiTableBatchGet superBatch = new MultiTableBatchGet(batches); superBatch.Execute(); } #endregion #region Batch Write /// /// Issues a batch-write request with multiple batches. /// /// /// Configured BatchWrite objects /// public void ExecuteBatchWrite(params BatchWrite[] batches) { MultiTableBatchWrite superBatch = new MultiTableBatchWrite(batches); superBatch.Execute(); } #endregion #region Transact Get /// /// Issues a transactional get request with multiple TransactGet objects. /// Results are stored in the individual TransactGet objects. /// /// Configured TransactGet objects. public void ExecuteTransactGet(params TransactGet[] transactionParts) { MultiTableTransactGet transaction = new MultiTableTransactGet(transactionParts); transaction.Execute(); } #endregion #region Transact Write /// /// Issues a transactional write request with multiple TransactWrite objects. /// /// Configured TransactWrite objects. public void ExecuteTransactWrite(params TransactWrite[] transactionParts) { MultiTableTransactWrite transaction = new MultiTableTransactWrite(transactionParts); transaction.Execute(); } #endregion #region Scan /// /// Executes a Scan operation against DynamoDB, finding items /// that match the specified conditions. /// /// Type of object. /// /// Conditions that the results should meet. /// /// Lazy-loaded collection of results. public IEnumerable Scan(params ScanCondition[] conditions) { if (conditions == null) throw new ArgumentNullException("conditions"); return Scan(conditions, null); } /// /// Executes a 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 the table used. /// Lazy-loaded collection of results. public IEnumerable Scan(IEnumerable conditions, DynamoDBOperationConfig operationConfig) { if (conditions == null) throw new ArgumentNullException("conditions"); var scan = ConvertScan(conditions, operationConfig); return FromSearch(scan); } /// /// Executes a Scan operation against DynamoDB, finding items /// that match the specified conditions. /// /// Type of object. /// Scan request object. /// Lazy-loaded collection of results. public IEnumerable FromScan(ScanOperationConfig scanConfig) { return FromScan(scanConfig, null); } /// /// Executes a 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. /// Lazy-loaded collection of results. public IEnumerable FromScan(ScanOperationConfig scanConfig, DynamoDBOperationConfig operationConfig) { if (scanConfig == null) throw new ArgumentNullException("scanConfig"); var scan = ConvertFromScan(scanConfig, operationConfig); return FromSearch(scan); } #endregion #region Query /// /// Executes a Query operation against DynamoDB, finding items /// that match the specified hash primary key. /// /// Type of object. /// Hash key of the items to query. /// Lazy-loaded collection of results. public IEnumerable Query(object hashKeyValue) { var query = ConvertQueryByValue(hashKeyValue, null, null); return FromSearch(query); } /// /// Executes a 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. /// Lazy-loaded collection of results. public IEnumerable Query(object hashKeyValue, DynamoDBOperationConfig operationConfig) { var query = ConvertQueryByValue(hashKeyValue, null, operationConfig); return FromSearch(query); } /// /// Executes a 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. /// /// Lazy-loaded collection of results. public IEnumerable Query(object hashKeyValue, QueryOperator op, params object[] values) { if (values == null || values.Length == 0) throw new ArgumentOutOfRangeException("values"); return Query(hashKeyValue, op, values, null); } /// /// Executes a 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. /// Lazy-loaded collection of results. public IEnumerable Query(object hashKeyValue, QueryOperator op, IEnumerable values, DynamoDBOperationConfig operationConfig) { if (values == null) throw new ArgumentNullException("values"); var query = ConvertQueryByValue(hashKeyValue, op, values, operationConfig); return FromSearch(query); } /// /// Executes a Query operation against DynamoDB, finding items /// that match the specified conditions. /// /// Type of object. /// Query request object. /// Lazy-loaded collection of results. public IEnumerable FromQuery(QueryOperationConfig queryConfig) { return FromQuery(queryConfig, null); } /// /// Executes a 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. /// Lazy-loaded collection of results. public IEnumerable FromQuery(QueryOperationConfig queryConfig, DynamoDBOperationConfig operationConfig) { if (queryConfig == null) throw new ArgumentNullException("queryConfig"); var search = ConvertFromQuery(queryConfig, operationConfig); return FromSearch(search); } #endregion #region Table methods /// /// Retrieves the target table for the specified type /// /// Type to retrieve table for /// Table object public Table GetTargetTable() { return GetTargetTable(null); } /// /// Retrieves the target table for the specified type /// /// Type to retrieve table for /// Table object public Table GetTargetTable(DynamoDBOperationConfig operationConfig) { return GetTargetTableInternal(operationConfig); } #endregion } }