Skip to content

Commit 724b733

Browse files
committed
wip
1 parent 643e85d commit 724b733

File tree

4 files changed

+141
-15
lines changed

4 files changed

+141
-15
lines changed

sdk/src/Services/DynamoDBv2/Custom/DataModel/DocumentOperationRequest.cs renamed to sdk/src/Services/DynamoDBv2/Custom/DocumentModel/DocumentOperationRequest.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using System.Collections.Generic;
2-
using Amazon.DynamoDBv2.DocumentModel;
32

4-
namespace Amazon.DynamoDBv2.DataModel
3+
namespace Amazon.DynamoDBv2.DocumentModel
54
{
65
/// <summary>
76
/// Base class for requests that perform operations on a document in DynamoDB.

sdk/src/Services/DynamoDBv2/Custom/DocumentModel/Table.cs

Lines changed: 86 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,18 @@
1313
* permissions and limitations under the License.
1414
*/
1515

16-
using System;
17-
16+
using Amazon.DynamoDBv2.DataModel;
1817
using Amazon.DynamoDBv2.Model;
19-
using Amazon.Runtime;
20-
using Amazon.Util;
18+
using Amazon.Runtime.Internal.UserAgent;
19+
using Amazon.Runtime.Internal.Util;
20+
using Amazon.Runtime.Telemetry.Tracing;
21+
using System;
22+
using System.Collections.Generic;
23+
using System.Globalization;
2124
using System.Linq;
25+
using System.Linq.Expressions;
2226
using System.Threading;
2327
using System.Threading.Tasks;
24-
using System.Collections.Generic;
25-
using System.Globalization;
26-
using Amazon.DynamoDBv2.DataModel;
27-
using Amazon.Runtime.Internal.Util;
28-
using Amazon.Runtime.Telemetry.Tracing;
29-
using Amazon.Runtime.Internal.UserAgent;
3028

3129
namespace Amazon.DynamoDBv2.DocumentModel
3230
{
@@ -1362,6 +1360,82 @@ internal Task<Document> UpdateHelperAsync(Document doc, Primitive hashKey, Primi
13621360
return UpdateHelperAsync(doc, key, config, expression, cancellationToken);
13631361
}
13641362

1363+
//internal async Task<Document> UpdateHelperAsync(UpdateItemDocumentOperationRequest request,
1364+
// CancellationToken cancellationToken)
1365+
//{
1366+
// if (request == null)
1367+
// throw new ArgumentNullException(nameof(request));
1368+
1369+
// var doc = request.Document;
1370+
1371+
// Expression updateExpression = request.UpdateExpression;
1372+
1373+
// // Validate that either doc or updateExpression is set, but not both null or both set to non-meaningful values
1374+
// if ((doc == null && (updateExpression == null || !updateExpression.IsSet)) ||
1375+
// (doc != null && updateExpression != null && updateExpression.IsSet))
1376+
// {
1377+
// throw new ArgumentException("Either Document or UpdateExpression must be set in the request.",
1378+
// nameof(request));
1379+
// }
1380+
1381+
// Key key = request.Key != null ? MakeKey(request.Key) : null;
1382+
1383+
// if(doc!=null)
1384+
// {
1385+
// key ??= MakeKey(doc);
1386+
// bool haveKeysChanged = HaveKeysChanged(doc);
1387+
// bool updateChangedAttributesOnly = !haveKeysChanged;
1388+
// var attributeUpdates = this.ToAttributeUpdateMap(doc, updateChangedAttributesOnly);
1389+
// foreach (var keyName in this.KeyNames)
1390+
// {
1391+
// attributeUpdates.Remove(keyName);
1392+
// }
1393+
// //todo build update expression from attributeUpdates
1394+
// string statement;
1395+
// Dictionary<string, AttributeValue> expressionAttributeValues;
1396+
// Dictionary<string, string> expressionAttributeNames;
1397+
1398+
// Common.ConvertAttributeUpdatesToUpdateExpression(attributeUpdates, updateExpression, this,
1399+
// out statement, out expressionAttributeValues, out expressionAttributeNames);
1400+
1401+
// //updateExpression = new Expression
1402+
// //{
1403+
// // ExpressionStatement = statement,
1404+
// // ExpressionAttributeValues = expressionAttributeValues,
1405+
// // ExpressionAttributeNames = expressionAttributeNames
1406+
// //};
1407+
// }
1408+
1409+
// if (key == null || key.Count == 0)
1410+
// {
1411+
// throw new InvalidOperationException(
1412+
// "UpdateItem requires a key to be specified either in the request or in the Document.");
1413+
// }
1414+
1415+
// UpdateItemRequest req = new UpdateItemRequest
1416+
// {
1417+
// TableName = TableName,
1418+
// Key = key,
1419+
// ReturnValues = EnumMapper.Convert(request.ReturnValues)
1420+
// };
1421+
// request.UpdateExpression.ApplyExpression(req, this);
1422+
// request.ConditionalExpression?.ApplyExpression(req, this);
1423+
1424+
// this.UpdateRequestUserAgentDetails(req, isAsync: true);
1425+
1426+
// var resp = await DDBClient.UpdateItemAsync(req, cancellationToken).ConfigureAwait(false);
1427+
// var returnedAttributes = resp.Attributes;
1428+
// doc.CommitChanges();
1429+
1430+
// Document ret = null;
1431+
// if (request.ReturnValues != ReturnValues.None)
1432+
// {
1433+
// ret = this.FromAttributeMap(returnedAttributes);
1434+
// }
1435+
1436+
// return ret;
1437+
//}
1438+
13651439
internal Document UpdateHelper(Document doc, Key key, UpdateItemOperationConfig config, Expression updateExpression)
13661440
{
13671441
var currentConfig = config ?? new UpdateItemOperationConfig();
@@ -1809,7 +1883,8 @@ public IDocumentTransactWrite CreateTransactWrite()
18091883
{
18101884
return new DocumentTransactWrite(this);
18111885
}
1812-
1886+
// Add this private method to the Table class
18131887
#endregion
1888+
18141889
}
18151890
}

sdk/src/Services/DynamoDBv2/Custom/DocumentModel/_async/Table.Async.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -452,12 +452,13 @@ public partial class Table : ITable
452452
}
453453

454454
/// <inheritdoc/>
455-
public async Task<Document> UpdateItemAsync(UpdateItemDocumentOperationRequest request, CancellationToken cancellationToken = default(CancellationToken))
455+
public Task<Document> UpdateItemAsync(UpdateItemDocumentOperationRequest request, CancellationToken cancellationToken = default(CancellationToken))
456456
{
457457
var operationName = DynamoDBTelemetry.ExtractOperationName(nameof(Table), nameof(UpdateItemAsync));
458458
using (DynamoDBTelemetry.CreateSpan(TracerProvider, operationName, spanKind: SpanKind.CLIENT))
459459
{
460-
throw new NotImplementedException();
460+
//return await UpdateHelperAsync(request, cancellationToken);
461+
throw new NotImplementedException("UpdateItemAsync with UpdateItemDocumentOperationRequest is not implemented yet.");
461462
}
462463
}
463464

sdk/test/Services/DynamoDBv2/IntegrationTests/DataModelTests.cs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -912,6 +912,57 @@ public void TestContext_Scan_WithExpressionFilter()
912912
Assert.AreEqual("Sam", upcomingEnumResult[0].Name);
913913
}
914914

915+
[TestMethod]
916+
[TestCategory("DynamoDBv2")]
917+
public void TestContext_SaveItem_WithTTL()
918+
{
919+
TableCache.Clear();
920+
CleanupTables();
921+
TableCache.Clear();
922+
923+
// Create context and table if needed
924+
CreateContext(DynamoDBEntryConversion.V2, true);
925+
926+
// Define TTL to be 2 minutes in the future
927+
var ttlEpoch = DateTimeOffset.UtcNow.AddMinutes(2).ToUnixTimeSeconds();
928+
929+
var item = new TtlTestItem
930+
{
931+
Id = 1,
932+
Data = "Test with TTL",
933+
Ttl = ttlEpoch
934+
};
935+
936+
Context.Save(item);
937+
938+
// Load immediately, should exist
939+
var loaded = Context.Load<TtlTestItem>(item.Id);
940+
Assert.IsNotNull(loaded);
941+
Assert.AreEqual(item.Id, loaded.Id);
942+
Assert.AreEqual(item.Data, loaded.Data);
943+
Assert.AreEqual(item.Ttl, loaded.Ttl);
944+
945+
item.Ttl = DateTimeOffset.UtcNow.AddMinutes(3).ToUnixTimeSeconds();
946+
Context.Save(item);
947+
var loaded2 = Context.Load<TtlTestItem>(item.Id);
948+
Assert.IsNotNull(loaded2);
949+
Assert.AreEqual(item.Id, loaded2.Id);
950+
Assert.AreEqual(item.Data, loaded2.Data);
951+
Assert.AreEqual(item.Ttl, loaded2.Ttl);
952+
}
953+
954+
// Example model for TTL
955+
[DynamoDBTable("HashTable")]
956+
public class TtlTestItem
957+
{
958+
[DynamoDBHashKey]
959+
public int Id { get; set; }
960+
961+
public string Data { get; set; }
962+
963+
[DynamoDBProperty("TTL")]
964+
public long Ttl { get; set; }
965+
}
915966

916967
[TestMethod]
917968
[TestCategory("DynamoDBv2")]

0 commit comments

Comments
 (0)