Skip to content
Open
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/9490947f-209f-47e9-8c70-3698872df304.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"services": [
{
"serviceName": "DynamoDBv2",
"type": "minor",
"changeLogMessages": [
"Add support for DynamoDbUpdateBehavior for operations."
]
}
]
}
58 changes: 58 additions & 0 deletions sdk/src/Services/DynamoDBv2/Custom/DataModel/Attributes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -797,4 +797,62 @@ public DynamoDBAutoGeneratedTimestampAttribute(string attributeName, [Dynamicall
{
}
}

/// <summary>
/// Specifies the update behavior for a property when performing DynamoDB update operations.
/// This attribute can be used to control whether a property is always updated or only set when the item is created (if the attribute does not exist).
/// </summary>
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, Inherited = true, AllowMultiple = false)]
public sealed class DynamoDbUpdateBehaviorAttribute : DynamoDBPropertyAttribute
{
/// <summary>
/// Gets the update behavior for the property.
/// </summary>
public UpdateBehavior Behavior { get; }

/// <summary>
/// Default constructor. Sets behavior to Always.
/// </summary>
public DynamoDbUpdateBehaviorAttribute()
: base()
{
Behavior = UpdateBehavior.Always;
}

/// <summary>
/// Constructor that specifies the update behavior.
/// </summary>
/// <param name="behavior">The update behavior to apply.</param>
public DynamoDbUpdateBehaviorAttribute(UpdateBehavior behavior)
: base()
{
Behavior = behavior;
}

/// <summary>
/// Constructor that specifies an alternate attribute name and update behavior.
/// </summary>
/// <param name="attributeName">Name of attribute to be associated with property or field.</param>
/// <param name="behavior">The update behavior to apply.</param>
public DynamoDbUpdateBehaviorAttribute(string attributeName, UpdateBehavior behavior)
: base(attributeName)
{
Behavior = behavior;
}
}

/// <summary>
/// Specifies when a property value should be set.
/// </summary>
public enum UpdateBehavior
{
/// <summary>
/// Set the value on both create and update.
/// </summary>
Always,
/// <summary>
/// Set the value only when the item is created.
/// </summary>
IfNotExists
}
}
145 changes: 72 additions & 73 deletions sdk/src/Services/DynamoDBv2/Custom/DataModel/Context.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
/*
* 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.
*/
* 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;
Expand Down Expand Up @@ -371,42 +371,17 @@ public IMultiTableTransactWrite CreateMultiTableTransactWrite(params ITransactWr

Table table = GetTargetTable(storage.Config, flatConfig);

var counterConditionExpression = BuildCounterConditionExpression(storage);

Document updateDocument;
Expression versionExpression = null;

var returnValues=counterConditionExpression == null ? ReturnValues.None : ReturnValues.AllNewAttributes;
var updateConfig = PrepareUpdateOperation(storage, flatConfig, table);

if ((flatConfig.SkipVersionCheck.HasValue && flatConfig.SkipVersionCheck.Value) || !storage.Config.HasVersion)
{
updateDocument = table.UpdateHelper(storage.Document, table.MakeKey(storage.Document), new UpdateItemOperationConfig()
{
ReturnValues = returnValues
}, counterConditionExpression);
}
else
{
var conversionConfig = new DynamoDBEntry.AttributeConversionConfig(table.Conversion, table.IsEmptyStringValueEnabled);
versionExpression = CreateConditionExpressionForVersion(storage, conversionConfig);
SetNewVersion(storage);
var updateDocument = table.UpdateHelper(
storage.Document,
table.MakeKey(storage.Document),
updateConfig.opConfig,
updateConfig.counterConditionExpression,
updateConfig.updateIfNotExistsAttributeNames
);

var updateItemOperationConfig = new UpdateItemOperationConfig
{
ReturnValues = returnValues,
ConditionalExpression = versionExpression,
};
updateDocument = table.UpdateHelper(storage.Document, table.MakeKey(storage.Document), updateItemOperationConfig, counterConditionExpression);
}

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

if (returnValues == ReturnValues.AllNewAttributes)
{
storage.Document = updateDocument;
}

PopulateInstance(storage, value, flatConfig);
ApplyPostUpdate(storage, value, flatConfig, updateDocument, updateConfig);
}

private async Task SaveHelperAsync<[DynamicallyAccessedMembers(InternalConstants.DataModelModeledType)] T>(T value, DynamoDBFlatConfig flatConfig, CancellationToken cancellationToken)
Expand All @@ -423,46 +398,70 @@ private async Task SaveHelperAsync([DynamicallyAccessedMembers(InternalConstants

Table table = GetTargetTable(storage.Config, flatConfig);

var updateConfig = PrepareUpdateOperation(storage, flatConfig, table);

var updateDocument = await table.UpdateHelperAsync(
storage.Document,
table.MakeKey(storage.Document),
updateConfig.opConfig,
updateConfig.counterConditionExpression,
cancellationToken,
updateConfig.updateIfNotExistsAttributeNames
).ConfigureAwait(false);

ApplyPostUpdate(storage, value, flatConfig, updateDocument, updateConfig);
}

private (UpdateItemOperationConfig opConfig, Expression versionExpression, Expression counterConditionExpression,
HashSet<string> updateIfNotExistsAttributeNames, bool updateIfNotExists) PrepareUpdateOperation(ItemStorage storage, DynamoDBFlatConfig flatConfig, Table table)
{
var counterConditionExpression = BuildCounterConditionExpression(storage);

Document updateDocument;
Expression versionExpression = null;

var returnValues = counterConditionExpression == null ? ReturnValues.None : ReturnValues.AllNewAttributes;
// get the list of attributes with UpdateIfNotExists attribute
var updateIfNotExistsAttributeNames = GetUpdateIfNotExistsAttributeNames(storage);

if (
(flatConfig.SkipVersionCheck.HasValue && flatConfig.SkipVersionCheck.Value)
|| !storage.Config.HasVersion)
var updateIfNotExists = updateIfNotExistsAttributeNames.Any();

// set return values to AllNewAttributes if there is a condition expression or updateIfNotExists is true
// to get the updated document back and reflect changes to the object
var returnValues = counterConditionExpression == null && !updateIfNotExists
? ReturnValues.None
: ReturnValues.AllNewAttributes;

var updateItemOperationConfig = new UpdateItemOperationConfig
{
updateDocument = await table.UpdateHelperAsync(storage.Document, table.MakeKey(storage.Document), new UpdateItemOperationConfig
{
ReturnValues = returnValues
}, counterConditionExpression, cancellationToken).ConfigureAwait(false);
}
else
ReturnValues = returnValues
};

if (!(flatConfig.SkipVersionCheck.HasValue && flatConfig.SkipVersionCheck.Value) && storage.Config.HasVersion)
{
var conversionConfig = new DynamoDBEntry.AttributeConversionConfig(table.Conversion, table.IsEmptyStringValueEnabled);
var conversionConfig = new DynamoDBEntry.AttributeConversionConfig(table.Conversion, table.IsEmptyStringValueEnabled);
versionExpression = CreateConditionExpressionForVersion(storage, conversionConfig);
SetNewVersion(storage);

updateDocument = await table.UpdateHelperAsync(
storage.Document,
table.MakeKey(storage.Document),
new UpdateItemOperationConfig
{
ReturnValues = returnValues,
ConditionalExpression = versionExpression
}, counterConditionExpression,
cancellationToken)
.ConfigureAwait(false);
updateItemOperationConfig.ConditionalExpression = versionExpression;
}

if (counterConditionExpression == null && versionExpression == null && !storage.Config.HasAutogeneratedProperties) return;
return (updateItemOperationConfig,
versionExpression,
counterConditionExpression,
updateIfNotExistsAttributeNames,
updateIfNotExists);
}

private void ApplyPostUpdate(ItemStorage storage, object value, DynamoDBFlatConfig flatConfig, Document updateDocument,
(UpdateItemOperationConfig opConfig, Expression versionExpression, Expression counterConditionExpression,
HashSet<string> updateIfNotExistsAttributeNames, bool updateIfNotExists) updateConfig)
{
// if there is no condition expression and no versioning, and no updateIfNotExists, no need to update the instance
if (updateConfig.counterConditionExpression == null && updateConfig.versionExpression == null && !updateConfig.updateIfNotExists) return;

if (returnValues == ReturnValues.AllNewAttributes)
if (updateConfig.opConfig.ReturnValues == ReturnValues.AllNewAttributes)
{
storage.Document = updateDocument;
}

PopulateInstance(storage, value, flatConfig);
}

Expand Down Expand Up @@ -698,4 +697,4 @@ private async Task SaveHelperAsync([DynamicallyAccessedMembers(InternalConstants

#endregion
}
}
}
40 changes: 27 additions & 13 deletions sdk/src/Services/DynamoDBv2/Custom/DataModel/ContextInternal.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ internal static void SetNewVersion(ItemStorage storage)
}
storage.Document[versionAttributeName] = version;
}

private static void IncrementVersion(Type memberType, ref Primitive version)
{
if (memberType.IsAssignableFrom(typeof(Byte))) version = version.AsByte() + 1;
Expand Down Expand Up @@ -137,16 +138,15 @@ private static PropertyStorage[] GetCounterProperties(ItemStorage storage)
{
var counterProperties = storage.Config.BaseTypeStorageConfig.Properties.
Where(propertyStorage => propertyStorage.IsCounter).ToArray();
var flatten= storage.Config.BaseTypeStorageConfig.Properties.
var flatten = storage.Config.BaseTypeStorageConfig.Properties.
Where(propertyStorage => propertyStorage.FlattenProperties.Any()).ToArray();
while (flatten.Any())
{
var flattenCounters = flatten.SelectMany(p => p.FlattenProperties.Where(fp => fp.IsCounter)).ToArray();
counterProperties = counterProperties.Concat(flattenCounters).ToArray();
flatten = flatten.SelectMany(p => p.FlattenProperties.Where(fp => fp.FlattenProperties.Any())).ToArray();
}



return counterProperties;
}

Expand All @@ -172,12 +172,27 @@ private static DocumentModel.Expression CreateUpdateExpressionForCounterProperti
propertyStorage.CounterStartValue - propertyStorage.CounterDelta;
}
updateExpression.ExpressionStatement = $"SET {asserts.Substring(0, asserts.Length - 2)}";

return updateExpression;
}

#endregion

internal static HashSet<string> GetUpdateIfNotExistsAttributeNames(ItemStorage storage)
{
var ifNotExistsProperties = storage.Config.BaseTypeStorageConfig.Properties.
Where(propertyStorage => propertyStorage.UpdateBehaviorMode == UpdateBehavior.IfNotExists).ToArray();
var flatten = storage.Config.BaseTypeStorageConfig.Properties.
Where(propertyStorage => propertyStorage.FlattenProperties.Any()).ToArray();
while (flatten.Any())
{
var flattenIfNotExists = flatten.SelectMany(p => p.FlattenProperties.Where(fp => fp.UpdateBehaviorMode == UpdateBehavior.IfNotExists)).ToArray();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i open this pr in visual studio 2026 and this line is missing unit test coverage it seems. can we double check and add

Image

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

ifNotExistsProperties = ifNotExistsProperties.Concat(flattenIfNotExists).ToArray();
flatten = flatten.SelectMany(p => p.FlattenProperties.Where(fp => fp.FlattenProperties.Any())).ToArray();
}
return new HashSet<string>(ifNotExistsProperties.Select(p => p.AttributeName).ToList());
}


#region Table methods

// Retrieves the target table for the specified type
Expand Down Expand Up @@ -453,7 +468,7 @@ private void PopulateInstance(ItemStorage storage, object instance, DynamoDBFlat
{
foreach (PropertyStorage propertyStorage in storageConfig.AllPropertyStorage)
{
if(propertyStorage.IsFlattened) continue;
if (propertyStorage.IsFlattened) continue;
string attributeName = propertyStorage.AttributeName;
if (propertyStorage.ShouldFlattenChildProperties)
{
Expand Down Expand Up @@ -570,7 +585,6 @@ private void PopulateItemStorage(object toStore, ItemStorage storage, DynamoDBFl

if (ShouldSave(dbe, ignoreNullValues))
{

if (propertyStorage.ShouldFlattenChildProperties)
{
if (dbe == null) continue;
Expand Down Expand Up @@ -680,14 +694,14 @@ private object FromDynamoDBEntry(SimplePropertyStorage propertyStorage, DynamoDB

bool isAotRuntime = InternalSDKUtils.IsRunningNativeAot();
string errorMessage;

if (isAotRuntime)
{
errorMessage = $"Unable to convert DynamoDB entry [{entry}] of type {entry.GetType().FullName} to property {propertyStorage.PropertyName} of type {targetType.FullName}. " +
"Since the application is running in Native AOT mode the type could possibly be trimmed. " +
"This can happen if the type being created is a nested type of a type being used for saving and loading DynamoDB items. " +
$"This can be worked around by adding the \"[DynamicDependency(DynamicallyAccessedMemberTypes.All, typeof({targetType.FullName}))]\" attribute to the constructor of the parent type." +
" If the parent type can not be modified the attribute can also be used on the method invoking the DynamoDB sdk or some other method that you are sure is not being trimmed.";
"Since the application is running in Native AOT mode the type could possibly be trimmed. " +
"This can happen if the type being created is a nested type of a type being used for saving and loading DynamoDB items. " +
$"This can be worked around by adding the \"[DynamicDependency(DynamicallyAccessedMemberTypes.All, typeof({targetType.FullName}))]\" attribute to the constructor of the parent type." +
" If the parent type can not be modified the attribute can also be used on the method invoking the DynamoDB sdk or some other method that you are sure is not being trimmed.";
}
else
{
Expand Down Expand Up @@ -2137,7 +2151,7 @@ private ExpressionNode HandleAttributeTypeMethodCall(MethodCallExpression expr,
{
var memberObj = ContextExpressionsUtils.GetMember(expr.Arguments[0]);
var typeExpr = ContextExpressionsUtils.GetConstant(expr.Arguments[1]);
if (memberObj!=null && typeExpr!=null)
if (memberObj != null && typeExpr != null)
{
SetExpressionNodeAttributes(storageConfig, memberObj, typeExpr, node, flatConfig);
}
Expand Down Expand Up @@ -2279,7 +2293,7 @@ private ExpressionNode HandleStartsWithMethodCall(MethodCallExpression expr, Ite
};
if (expr.Object is MemberExpression memberObj && expr.Arguments[0] is ConstantExpression argConst)
{
var constantValue=ContextExpressionsUtils.GetConstant(argConst);
var constantValue = ContextExpressionsUtils.GetConstant(argConst);
SetExpressionNodeAttributes(storageConfig, memberObj, constantValue, node, flatConfig);
}
else
Expand Down
Loading