Skip to content

Add support for DynamoDBAutoGeneratedTimestampAttribute that sets current timestamp during persistence operations. #3892

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: development
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions generator/.DevConfigs/c952ab1e-3056-4598-9d0e-f7f02187e982.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"services": [
{
"serviceName": "DynamoDBv2",
"type": "patch",
"changeLogMessages": [
"Add support for DynamoDBAutoGeneratedTimestampAttribute that sets current timestamp during persistence operations."
]
}
]
}
119 changes: 119 additions & 0 deletions sdk/src/Services/DynamoDBv2/Custom/DataModel/Attributes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -688,4 +688,123 @@ public DynamoDBLocalSecondaryIndexRangeKeyAttribute(params string[] indexNames)
IndexNames = indexNames.Distinct(StringComparer.Ordinal).ToArray();
}
}

/// <summary>
/// Specifies that the decorated property or field should have its value automatically
/// set to the current timestamp during persistence operations.
/// </summary>
/// <remarks>
/// The <see cref="Mode"/> property controls when the timestamp is set:
/// <list type="bullet">
/// <item><description><see cref="TimestampMode.Create"/>: Set only when the item is created.</description></item>
/// <item><description><see cref="TimestampMode.Always"/>: Set on both create and update.</description></item>
/// </list>
/// </remarks>
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, Inherited = true, AllowMultiple = false)]
public sealed class DynamoDBAutoGeneratedTimestampAttribute : DynamoDBPropertyAttribute
{
/// <summary>
/// Gets or sets when the timestamp should be generated.
/// </summary>
public TimestampMode Mode { get; }

/// <summary>
/// Default constructor. Timestamp is set on both create and update.
/// </summary>
public DynamoDBAutoGeneratedTimestampAttribute()
: base()
{
Mode = TimestampMode.Always;
}

/// <summary>
/// Constructor that specifies when the timestamp should be generated.
/// </summary>
/// <param name="mode">Specifies when the timestamp should be generated.</param>
public DynamoDBAutoGeneratedTimestampAttribute(TimestampMode mode)
: base()
{
Mode = mode;
}

/// <summary>
/// Constructor that specifies an alternate attribute name.
/// </summary>
/// <param name="attributeName">Name of attribute to be associated with property or field.</param>
public DynamoDBAutoGeneratedTimestampAttribute(string attributeName)
: base(attributeName)
{
Mode = TimestampMode.Always;
}

/// <summary>
/// Constructor that specifies an alternate attribute name and when the timestamp should be generated.
/// </summary>
/// <param name="attributeName">Name of attribute to be associated with property or field.</param>
/// <param name="mode">Specifies when the timestamp should be generated.</param>
public DynamoDBAutoGeneratedTimestampAttribute(string attributeName, TimestampMode mode)
: base(attributeName)
{
Mode = mode;
}

/// <summary>
/// Constructor that specifies a custom converter.
/// </summary>
/// <param name="converter">Custom converter type.</param>
public DynamoDBAutoGeneratedTimestampAttribute([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.Interfaces)] Type converter)
: base(converter)
{
Mode = TimestampMode.Always;
}

/// <summary>
/// Constructor that specifies a custom converter and when the timestamp should be generated.
/// </summary>
/// <param name="converter">Custom converter type.</param>
/// <param name="mode">Specifies when the timestamp should be generated.</param>
public DynamoDBAutoGeneratedTimestampAttribute([DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.Interfaces)] Type converter, TimestampMode mode)
: base(converter)
{
Mode = mode;
}

/// <summary>
/// Constructor that specifies an alternate attribute name and a custom converter.
/// </summary>
/// <param name="attributeName">Name of attribute to be associated with property or field.</param>
/// <param name="converter">Custom converter type.</param>
public DynamoDBAutoGeneratedTimestampAttribute(string attributeName, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.Interfaces)] Type converter)
: base(attributeName, converter)
{
Mode = TimestampMode.Always;
}

/// <summary>
/// Constructor that specifies an alternate attribute name, a custom converter, and when the timestamp should be generated.
/// </summary>
/// <param name="attributeName">Name of attribute to be associated with property or field.</param>
/// <param name="converter">Custom converter type.</param>
/// <param name="mode">Specifies when the timestamp should be generated.</param>
public DynamoDBAutoGeneratedTimestampAttribute(string attributeName, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.Interfaces)] Type converter, TimestampMode mode)
: base(attributeName, converter)
{
Mode = mode;
}
}

/// <summary>
/// Specifies when an auto-generated timestamp should be set.
/// </summary>
public enum TimestampMode
{
/// <summary>
/// Set the timestamp only when the item is created.
/// </summary>
Create,
/// <summary>
/// Set the timestamp on both create and update.
/// </summary>
Always
}
}
30 changes: 21 additions & 9 deletions sdk/src/Services/DynamoDBv2/Custom/DataModel/Context.cs
Original file line number Diff line number Diff line change
Expand Up @@ -375,17 +375,23 @@ public IMultiTableTransactWrite CreateMultiTableTransactWrite(params ITransactWr

var counterConditionExpression = BuildCounterConditionExpression(storage);

var autoGeneratedTimestampExpression = BuildTimestampConditionExpression(storage);

Document updateDocument;
Expression versionExpression = null;

var returnValues=counterConditionExpression == null ? ReturnValues.None : ReturnValues.AllNewAttributes;

var updateExpression = Expression.MergeUpdateExpressions(counterConditionExpression, autoGeneratedTimestampExpression);

var returnValues = updateExpression == null
? ReturnValues.None
: ReturnValues.AllNewAttributes;

if ((flatConfig.SkipVersionCheck.HasValue && flatConfig.SkipVersionCheck.Value) || !storage.Config.HasVersion)
{
updateDocument = table.UpdateHelper(storage.Document, table.MakeKey(storage.Document), new UpdateItemOperationConfig()
{
ReturnValues = returnValues
}, counterConditionExpression);
}, updateExpression);
}
else
{
Expand All @@ -398,10 +404,10 @@ public IMultiTableTransactWrite CreateMultiTableTransactWrite(params ITransactWr
ReturnValues = returnValues,
ConditionalExpression = versionExpression,
};
updateDocument = table.UpdateHelper(storage.Document, table.MakeKey(storage.Document), updateItemOperationConfig, counterConditionExpression);
updateDocument = table.UpdateHelper(storage.Document, table.MakeKey(storage.Document), updateItemOperationConfig, updateExpression);
}

if (counterConditionExpression == null && versionExpression == null) return;
if (updateExpression == null && versionExpression == null) return;

if (returnValues == ReturnValues.AllNewAttributes)
{
Expand All @@ -428,10 +434,16 @@ private async Task SaveHelperAsync([DynamicallyAccessedMembers(InternalConstants

var counterConditionExpression = BuildCounterConditionExpression(storage);

var autoGeneratedTimestampExpression = BuildTimestampConditionExpression(storage);

Document updateDocument;
Expression versionExpression = null;

var returnValues = counterConditionExpression == null ? ReturnValues.None : ReturnValues.AllNewAttributes;
var updateExpression = Expression.MergeUpdateExpressions(counterConditionExpression, autoGeneratedTimestampExpression);

var returnValues = updateExpression == null
? ReturnValues.None
: ReturnValues.AllNewAttributes;

if (
(flatConfig.SkipVersionCheck.HasValue && flatConfig.SkipVersionCheck.Value)
Expand All @@ -440,7 +452,7 @@ private async Task SaveHelperAsync([DynamicallyAccessedMembers(InternalConstants
updateDocument = await table.UpdateHelperAsync(storage.Document, table.MakeKey(storage.Document), new UpdateItemOperationConfig
{
ReturnValues = returnValues
}, counterConditionExpression, cancellationToken).ConfigureAwait(false);
}, updateExpression, cancellationToken).ConfigureAwait(false);
}
else
{
Expand All @@ -455,12 +467,12 @@ private async Task SaveHelperAsync([DynamicallyAccessedMembers(InternalConstants
{
ReturnValues = returnValues,
ConditionalExpression = versionExpression
}, counterConditionExpression,
}, updateExpression,
cancellationToken)
.ConfigureAwait(false);
}

if (counterConditionExpression == null && versionExpression == null) return;
if (updateExpression == null && versionExpression == null) return;

if (returnValues == ReturnValues.AllNewAttributes)
{
Expand Down
91 changes: 90 additions & 1 deletion sdk/src/Services/DynamoDBv2/Custom/DataModel/ContextInternal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
using Amazon.Util.Internal;
using System.Globalization;
using System.Diagnostics.CodeAnalysis;
using Amazon.Util;
using ThirdParty.RuntimeBackports;

namespace Amazon.DynamoDBv2.DataModel
Expand Down Expand Up @@ -118,6 +119,86 @@ internal static Expression CreateConditionExpressionForVersion(ItemStorage stora

#endregion

#region Autogenerated Timestamp

internal static Expression BuildTimestampConditionExpression(ItemStorage storage)
{
var timestampProperties = GetTimestampProperties(storage);
Expression timestampConditionExpression = null;

if (timestampProperties.Length != 0)
{
timestampConditionExpression = CreateUpdateExpressionForTimestampProperties(timestampProperties);
}

return timestampConditionExpression;
}

private static Expression CreateUpdateExpressionForTimestampProperties(PropertyStorage[] timestampProperties)
{
if (timestampProperties == null || timestampProperties.Length == 0)
return null;

var updateExpression = new Expression();
var setClauses = new List<string>();

var dateTime = AWSSDKUtils.CorrectedUtcNow; // Use corrected UTC time
foreach (var propertyStorage in timestampProperties)
{
string attributeName = propertyStorage.AttributeName;
string attributeRef = Common.GetAttributeReference(attributeName);
string valueRef = $":{attributeName}Timestamp";
updateExpression.ExpressionAttributeNames[attributeRef] = attributeName;

if (propertyStorage.StoreAsEpochLong)
{
string epochSecondsAsString = AWSSDKUtils.ConvertToUnixEpochSecondsString(dateTime);
updateExpression.ExpressionAttributeValues[valueRef] = new Primitive(epochSecondsAsString, saveAsNumeric: true);
}
else if (propertyStorage.StoreAsEpoch)
{
updateExpression.ExpressionAttributeValues[valueRef] = AWSSDKUtils.ConvertToUnixEpochSeconds(dateTime);
}
else
{
updateExpression.ExpressionAttributeValues[valueRef] = dateTime.ToString("o");
}

// Determine SET clause based on TimestampMode
string clause;
var mode = propertyStorage.AutoGeneratedTimestampMode;
switch (mode)
{
case TimestampMode.Create:
clause = $"{attributeRef} = if_not_exists({attributeRef}, {valueRef})";
break;
case TimestampMode.Always:
default:
clause = $"{attributeRef} = {valueRef}";
break;
}
setClauses.Add(clause);
}

if (setClauses.Count > 0)
{
updateExpression.ExpressionStatement = "SET " + string.Join(", ", setClauses);
}

return updateExpression;
}

private static PropertyStorage[] GetTimestampProperties(ItemStorage storage)
{
//todo : adapt this to work with polymorphic types
var counterProperties = storage.Config.BaseTypeStorageConfig.AllPropertyStorage.
Where(propertyStorage => propertyStorage.IsAutoGeneratedTimestamp).ToArray();

return counterProperties;
}

#endregion

#region Atomic counters

internal static Expression BuildCounterConditionExpression(ItemStorage storage)
Expand All @@ -135,7 +216,7 @@ internal static Expression BuildCounterConditionExpression(ItemStorage storage)

private static PropertyStorage[] GetCounterProperties(ItemStorage storage)
{
var counterProperties = storage.Config.BaseTypeStorageConfig.Properties.
var counterProperties = storage.Config.BaseTypeStorageConfig.AllPropertyStorage.
Where(propertyStorage => propertyStorage.IsCounter).ToArray();

return counterProperties;
Expand Down Expand Up @@ -570,6 +651,14 @@ private void PopulateItemStorage(object toStore, ItemStorage storage, DynamoDBFl
{
document[pair.Key] = pair.Value;
}

if (propertyStorage.FlattenProperties.Any(p => p.IsVersion))
{
var innerVersionProperty =
propertyStorage.FlattenProperties.First(p => p.IsVersion);
storage.CurrentVersion =
innerDocument[innerVersionProperty.AttributeName] as Primitive;
}
}
else
{
Expand Down
Loading