/*
* 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
{
public partial class DynamoDBContext : IDynamoDBContext
{
#region Save/serialize
///
/// 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 = null)
{
SaveHelper(value, operationConfig);
}
#endregion
#region Load/deserialize
///
/// 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.
///
/// Object of type T, populated with properties of item loaded from DynamoDB.
///
public T Load(object hashKey)
{
return LoadHelper(hashKey, null, 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.
/// Overriding configuration.
///
/// Object of type T, populated with properties of item loaded from DynamoDB.
///
public T Load(object hashKey, DynamoDBOperationConfig operationConfig)
{
return LoadHelper(hashKey, null, operationConfig);
}
///
/// 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.
///
/// Object of type T, populated with properties of item loaded from DynamoDB.
///
public T Load(object hashKey, object rangeKey)
{
return LoadHelper(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 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 = null)
{
return LoadHelper(keyObject, operationConfig);
}
#endregion
#region Delete
///
/// 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.
public void Delete(T value)
{
DeleteHelper(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-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.
public void Delete(object hashKey)
{
DeleteHelper(hashKey, null, 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.
/// Config object which can be used to override that table used.
public void Delete(object hashKey, DynamoDBOperationConfig operationConfig)
{
DeleteHelper(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)
{
DeleteHelper(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 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 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 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)
{
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 that table used.
/// Lazy-loaded collection of results.
public IEnumerable Scan(IEnumerable conditions, DynamoDBOperationConfig operationConfig)
{
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.
/// Config object which can be used to override the table used.
/// Lazy-loaded collection of results.
public IEnumerable FromScan(ScanOperationConfig scanConfig, DynamoDBOperationConfig operationConfig = null)
{
if (scanConfig == null) throw new ArgumentNullException("scanConfig");
var search = ConvertFromScan(scanConfig, operationConfig);
return FromSearch(search);
}
#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.Betwee, 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.Betwee, 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