internal Document UpdateHelper(Document doc, Key key, UpdateItemOperationConfig config, bool isAsync) { var currentConfig = config ?? new UpdateItemOperationConfig(); // If the keys have been changed, treat entire document as having changed bool haveKeysChanged = HaveKeysChanged(doc); bool updateChangedAttributesOnly = !haveKeysChanged; var attributeUpdates = doc.ToAttributeUpdateMap(updateChangedAttributesOnly); foreach (var keyName in this.keyNames) { attributeUpdates.Remove(keyName); } UpdateItemRequest req = new UpdateItemRequest { TableName = TableName, Key = key, AttributeUpdates = attributeUpdates.Count == 0 ? null : attributeUpdates, // pass null if keys-only update ReturnValues = EnumMapper.Convert(currentConfig.ReturnValues) }; req.BeforeRequestEvent += isAsync ? new RequestEventHandler(UserAgentRequestEventHandlerAsync) : new RequestEventHandler(UserAgentRequestEventHandlerSync); if (currentConfig.Expected != null && currentConfig.ExpectedState != null) { throw new InvalidOperationException("Expected and ExpectedState cannot be set at the same time"); } if (currentConfig.Expected != null) { req.Expected = currentConfig.Expected.ToExpectedAttributeMap(); } if (currentConfig.ExpectedState != null && currentConfig.ExpectedState.ExpectedValues != null && currentConfig.ExpectedState.ExpectedValues.Count > 0) { req.Expected = currentConfig.ExpectedState.ToExpectedAttributeMap(); if (req.Expected.Count > 1) { req.ConditionalOperator = EnumMapper.Convert(currentConfig.ExpectedState.ConditionalOperator); } } var resp = DDBClient.UpdateItem(req); var returnedAttributes = resp.Attributes; doc.CommitChanges(); Document ret = null; if (currentConfig.ReturnValues != ReturnValues.None) { ret = Document.FromAttributeMap(returnedAttributes); } return(ret); }
/// <summary> /// Initiates the asynchronous execution of the UpdateItem operation. /// <seealso cref="Amazon.DynamoDBv2.DocumentModel.Table.UpdateItem"/> /// </summary> /// <param name="doc">Attributes to update.</param> /// <param name="hashKey">Hash key element of the document.</param> /// <param name="config">Configuration to use.</param> /// <param name="callback">The callback that will be invoked when the asynchronous operation completes.</param> /// <param name="asyncOptions">An instance of AsyncOptions that specifies how the async method should be executed.</param> public void UpdateItemAsync(Document doc, Primitive hashKey, UpdateItemOperationConfig config, AmazonDynamoDBCallback <Document> callback, AsyncOptions asyncOptions = null) { asyncOptions = asyncOptions ?? new AsyncOptions(); DynamoDBAsyncExecutor.ExecuteAsync <Document>( () => { return(UpdateHelper(doc, MakeKey(hashKey, null), config, true)); }, asyncOptions, callback); }
/// <summary> /// Update a document in DynamoDB, using specified config. /// </summary> /// <param name="doc">Document to update.</param> /// <param name="config">Configuration to use.</param> /// <returns>True if updated or false if the condition in the config was not met.</returns> /// <seealso cref="Amazon.DynamoDBv2.DocumentModel.UpdateItemOperationConfig"/> public bool TryUpdateItem(Document doc, UpdateItemOperationConfig config = null) { try { UpdateHelper(doc, MakeKey(doc), config, false); return(true); } catch (ConditionalCheckFailedException) { return(false); } }
/// <summary> /// Update a document in DynamoDB, with a hash primary key to identify the /// document, and using the specified config. /// </summary> /// <param name="doc">Attributes to update.</param> /// <param name="hashKey">Hash key element of the document.</param> /// <param name="config">Configuration to use.</param> /// <returns>True if updated or false if the condition in the config was not met.</returns> /// <seealso cref="Amazon.DynamoDBv2.DocumentModel.UpdateItemOperationConfig"/> public bool TryUpdateItem(Document doc, Primitive hashKey, UpdateItemOperationConfig config) { try { UpdateHelper(doc, MakeKey(hashKey, null), config); return(true); } catch (ConditionalCheckFailedException) { return(false); } }
internal Document UpdateHelper(Document doc, Key key, UpdateItemOperationConfig config, bool isAsync) { var currentConfig = config ?? new UpdateItemOperationConfig(); // If the keys have been changed, treat entire document as having changed bool haveKeysChanged = HaveKeysChanged(doc); bool updateChangedAttributesOnly = !haveKeysChanged; var attributeUpdates = doc.ToAttributeUpdateMap(Conversion, updateChangedAttributesOnly); foreach (var keyName in this.KeyNames) { attributeUpdates.Remove(keyName); } UpdateItemRequest req = new UpdateItemRequest { TableName = TableName, Key = key, AttributeUpdates = attributeUpdates.Count == 0 ? null : attributeUpdates, // pass null if keys-only update ReturnValues = EnumMapper.Convert(currentConfig.ReturnValues) }; ((Amazon.Runtime.Internal.IAmazonWebServiceRequest)req).AddBeforeRequestHandler(isAsync ? new RequestEventHandler(UserAgentRequestEventHandlerAsync) : new RequestEventHandler(UserAgentRequestEventHandlerSync) ); ValidateConditional(currentConfig); if (currentConfig.Expected != null) { req.Expected = currentConfig.Expected.ToExpectedAttributeMap(Conversion); } else if (currentConfig.ExpectedState != null && currentConfig.ExpectedState.ExpectedValues != null && currentConfig.ExpectedState.ExpectedValues.Count > 0) { req.Expected = currentConfig.ExpectedState.ToExpectedAttributeMap(Conversion); if (req.Expected.Count > 1) { req.ConditionalOperator = EnumMapper.Convert(currentConfig.ExpectedState.ConditionalOperator); } } else if (currentConfig.ConditionalExpression != null && currentConfig.ConditionalExpression.IsSet) { currentConfig.ConditionalExpression.ApplyExpression(req, this.Conversion); string statement; Dictionary <string, AttributeValue> expressionAttributeValues; Dictionary <string, string> expressionAttributeNames; Common.ConvertAttributeUpdatesToUpdateExpression(attributeUpdates, out statement, out expressionAttributeValues, out expressionAttributeNames); req.AttributeUpdates = null; req.UpdateExpression = statement; if (req.ExpressionAttributeValues == null) { req.ExpressionAttributeValues = expressionAttributeValues; } else { foreach (var kvp in expressionAttributeValues) { req.ExpressionAttributeValues.Add(kvp.Key, kvp.Value); } } if (req.ExpressionAttributeNames == null) { req.ExpressionAttributeNames = expressionAttributeNames; } else { foreach (var kvp in expressionAttributeNames) { req.ExpressionAttributeNames.Add(kvp.Key, kvp.Value); } } } var resp = DDBClient.UpdateItem(req); var returnedAttributes = resp.Attributes; doc.CommitChanges(); Document ret = null; if (currentConfig.ReturnValues != ReturnValues.None) { ret = Document.FromAttributeMap(returnedAttributes); } return(ret); }
/// <summary> /// Initiates the asynchronous execution of the UpdateItem operation. /// <seealso cref="Amazon.DynamoDBv2.DocumentModel.Table.UpdateItem"/> /// </summary> /// <param name="doc">Attributes to update.</param> /// <param name="config">Configuration to use.</param> /// <param name="cancellationToken">Token which can be used to cancel the task.</param> /// <returns>A Task that can be used to poll or wait for results, or both.</returns> public Task <Document> UpdateItemAsync(Document doc, UpdateItemOperationConfig config, CancellationToken cancellationToken = default(CancellationToken)) { return(UpdateHelperAsync(doc, null, null, config, cancellationToken)); }
/// <summary> /// Initiates the asynchronous execution of the UpdateItem operation. /// <seealso cref="Amazon.DynamoDBv2.DocumentModel.Table.UpdateItem"/> /// </summary> /// <param name="doc">Attributes to update.</param> /// <param name="hashKey">Hash key element of the document.</param> /// <param name="rangeKey">Range key element of the document.</param> /// <param name="config">Configuration to use.</param> /// <param name="callback">An AmazonDynamoCallback delegate that is invoked when the operation completes.</param> /// <param name="state">A user-defined state object that is passed to the callback procedure. Retrieve this object from within the callback /// procedure using the AsyncState property.</param> /// <returns>void</returns> public void UpdateItemAsync(Document doc, Primitive hashKey, Primitive rangeKey, UpdateItemOperationConfig config, AmazonDynamoCallback <Document> callback, object state) { DynamoDBAsyncExecutor.AsyncOperation <Document>(() => UpdateHelper(doc, MakeKey(hashKey, rangeKey), config, true), "UpdateItemAsync", callback, state); }
/// <summary> /// Initiates the asynchronous execution of the UpdateItem operation. /// <seealso cref="Amazon.DynamoDBv2.DocumentModel.Table.UpdateItem"/> /// </summary> /// <param name="doc">Attributes to update.</param> /// <param name="hashKey">Hash key element of the document.</param> /// <param name="rangeKey">Range key element of the document.</param> /// <param name="config">Configuration to use.</param> /// <param name="callback">An AsyncCallback delegate that is invoked when the operation completes.</param> /// <param name="state">A user-defined state object that is passed to the callback procedure. Retrieve this object from within the callback /// procedure using the AsyncState property.</param> /// <returns>An IAsyncResult that can be used to poll or wait for results, or both; this value is also needed when invoking EndUpdateItem /// operation.</returns> public IAsyncResult BeginUpdateItem(Document doc, Primitive hashKey, Primitive rangeKey, UpdateItemOperationConfig config, AsyncCallback callback, object state) { return(DynamoDBAsyncExecutor.BeginOperation(() => UpdateHelper(doc, MakeKey(hashKey, rangeKey), config, true), callback, state)); }
/// <summary> /// Initiates the asynchronous execution of the UpdateItem operation. /// </summary> /// <param name="doc">Attributes to update.</param> /// <param name="hashKey">Hash key element of the document.</param> /// <param name="rangeKey">Range key element of the document.</param> /// <param name="config">Configuration to use.</param> /// <param name="callback">The callback that will be invoked when the asynchronous operation completes.</param> /// <param name="asyncOptions">An instance of AsyncOptions that specifies how the async method should be executed.</param> public void UpdateItemAsync(Document doc, Primitive hashKey, Primitive rangeKey, UpdateItemOperationConfig config, AmazonDynamoDBCallback<Document> callback, AsyncOptions asyncOptions = null) { asyncOptions = asyncOptions ?? new AsyncOptions(); DynamoDBAsyncExecutor.ExecuteAsync<Document>( () => { return UpdateHelper(doc, MakeKey(hashKey, rangeKey), config, true); }, asyncOptions, callback); }
/// <summary> /// Update a document in DynamoDB, with a key to identify the /// document, and using the specified config. /// </summary> /// <param name="doc">Attributes to update.</param> /// <param name="key">Key of the document.</param> /// <param name="config">Configuration to use.</param> /// <returns>Null or updated attributes, depending on config.</returns> /// <seealso cref="Amazon.DynamoDBv2.DocumentModel.UpdateItemOperationConfig"/> internal Document UpdateItem(Document doc, IDictionary <string, DynamoDBEntry> key, UpdateItemOperationConfig config) { DynamoDBAsyncExecutor.IsMainThread("UpdateItemAsync"); return(UpdateHelper(doc, MakeKey(key), config, false)); }
/// <summary> /// Initiates the asynchronous execution of the UpdateItem operation. /// <seealso cref="Amazon.DynamoDBv2.DocumentModel.Table.UpdateItem"/> /// </summary> /// <param name="doc">Attributes to update.</param> /// <param name="key">Key of the document.</param> /// <param name="config">Configuration to use.</param> /// <param name="cancellationToken">Token which can be used to cancel the task.</param> /// <returns>A Task that can be used to poll or wait for results, or both.</returns> public Task<Document> UpdateItemAsync(Document doc, IDictionary<string, DynamoDBEntry> key, UpdateItemOperationConfig config, CancellationToken cancellationToken = default(CancellationToken)) { return AsyncRunner.Run(() => UpdateHelper(doc, MakeKey(key), config, true), cancellationToken); }
/// <summary> /// Initiates the asynchronous execution of the UpdateItem operation. /// <seealso cref="Amazon.DynamoDBv2.DocumentModel.Table.UpdateItem"/> /// </summary> /// <param name="doc">Attributes to update.</param> /// <param name="key">Key of the document.</param> /// <param name="config">Configuration to use.</param> /// <param name="callback">An AsyncCallback delegate that is invoked when the operation completes.</param> /// <param name="state">A user-defined state object that is passed to the callback procedure. Retrieve this object from within the callback /// procedure using the AsyncState property.</param> /// <returns>An IAsyncResult that can be used to poll or wait for results, or both; this value is also needed when invoking EndUpdateItem /// operation.</returns> public IAsyncResult BeginUpdateItem(Document doc, IDictionary<string, DynamoDBEntry> key, UpdateItemOperationConfig config, AsyncCallback callback, object state) { return DynamoDBAsyncExecutor.BeginOperation(() => UpdateHelper(doc, MakeKey(key), config, true), callback, state); }
internal Document UpdateHelper(Document doc, Key key, UpdateItemOperationConfig config, bool isAsync) { var currentConfig = config ?? new UpdateItemOperationConfig(); // If the keys have been changed, treat entire document as having changed bool haveKeysChanged = HaveKeysChanged(doc); bool updateChangedAttributesOnly = !haveKeysChanged; var attributeUpdates = doc.ToAttributeUpdateMap(updateChangedAttributesOnly); foreach (var keyName in this.keyNames) { attributeUpdates.Remove(keyName); } UpdateItemRequest req = new UpdateItemRequest { TableName = TableName, Key = key, AttributeUpdates = attributeUpdates.Count == 0 ? null : attributeUpdates, // pass null if keys-only update ReturnValues = EnumToStringMapper.Convert(currentConfig.ReturnValues) }; req.BeforeRequestEvent += isAsync ? new RequestEventHandler(UserAgentRequestEventHandlerAsync) : new RequestEventHandler(UserAgentRequestEventHandlerSync); if (currentConfig.Expected != null) req.Expected = currentConfig.Expected.ToExpectedAttributeMap(); var resp = DDBClient.UpdateItem(req); var returnedAttributes = resp.Attributes; doc.CommitChanges(); Document ret = null; if (currentConfig.ReturnValues != ReturnValues.None) { ret = Document.FromAttributeMap(returnedAttributes); } return ret; }
/// <summary> /// Initiates the asynchronous execution of the UpdateItem operation. /// <seealso cref="Amazon.DynamoDBv2.DocumentModel.Table.UpdateItem"/> /// </summary> /// <param name="doc">Attributes to update.</param> /// <param name="hashKey">Hash key element of the document.</param> /// <param name="rangeKey">Range key element of the document.</param> /// <param name="config">Configuration to use.</param> /// <param name="callback">An AsyncCallback delegate that is invoked when the operation completes.</param> /// <param name="state">A user-defined state object that is passed to the callback procedure. Retrieve this object from within the callback /// procedure using the AsyncState property.</param> /// <returns>An IAsyncResult that can be used to poll or wait for results, or both; this value is also needed when invoking EndUpdateItem /// operation.</returns> public IAsyncResult BeginUpdateItem(Document doc, Primitive hashKey, Primitive rangeKey, UpdateItemOperationConfig config, AsyncCallback callback, object state) { return DynamoDBAsyncExecutor.BeginOperation(() => UpdateHelper(doc, MakeKey(hashKey, rangeKey), config, true), callback, state); }
/// <summary> /// Initiates the asynchronous execution of the UpdateItem operation. /// <seealso cref="Amazon.DynamoDBv2.DocumentModel.Table.UpdateItem"/> /// </summary> /// <param name="doc">Attributes to update.</param> /// <param name="hashKey">Hash key element of the document.</param> /// <param name="config">Configuration to use.</param> /// <param name="cancellationToken">Token which can be used to cancel the task.</param> /// <returns>A Task that can be used to poll or wait for results, or both.</returns> public Task <Document> UpdateItemAsync(Document doc, Primitive hashKey, UpdateItemOperationConfig config, CancellationToken cancellationToken = default(CancellationToken)) { return(AsyncRunner.Run(() => UpdateHelper(doc, hashKey, null, config, true), cancellationToken)); }
/// <summary> /// Initiates the asynchronous execution of the UpdateItem operation. /// <seealso cref="Amazon.DynamoDBv2.DocumentModel.Table.UpdateItem"/> /// </summary> /// <param name="doc">Attributes to update.</param> /// <param name="key">Key of the document.</param> /// <param name="config">Configuration to use.</param> /// <param name="callback">An AmazonDynamoCallback delegate that is invoked when the operation completes.</param> /// <param name="state">A user-defined state object that is passed to the callback procedure. Retrieve this object from within the callback /// procedure using the AsyncState property.</param> /// <returns>void</returns> public void UpdateItemAsync(Document doc, IDictionary <string, DynamoDBEntry> key, UpdateItemOperationConfig config, AmazonDynamoCallback <Document> callback, object state) { DynamoDBAsyncExecutor.AsyncOperation <Document>(() => UpdateHelper(doc, MakeKey(key), config, true), "UpdateItemAsync", callback, state); }
internal Task <Document> UpdateHelperAsync(Document doc, Primitive hashKey, Primitive rangeKey, UpdateItemOperationConfig config, CancellationToken cancellationToken) { Key key = (hashKey != null || rangeKey != null) ? MakeKey(hashKey, rangeKey) : MakeKey(doc); return(UpdateHelperAsync(doc, key, config, cancellationToken)); }
/// <summary> /// Update a document in DynamoDB, with a hash-and-range primary key to identify /// the document, and using the specified config. /// </summary> /// <param name="doc">Attributes to update.</param> /// <param name="hashKey">Hash key element of the document.</param> /// <param name="rangeKey">Range key element of the document.</param> /// <param name="config">Configuration to use.</param> /// <returns>Null or updated attributes, depending on config.</returns> /// <seealso cref="Amazon.DynamoDBv2.DocumentModel.UpdateItemOperationConfig"/> internal Document UpdateItem(Document doc, Primitive hashKey, Primitive rangeKey, UpdateItemOperationConfig config) { DynamoDBAsyncExecutor.IsMainThread("UpdateItemAsync"); return(UpdateHelper(doc, MakeKey(hashKey, rangeKey), config, false)); }
internal Document UpdateHelper(Document doc, Primitive hashKey, Primitive rangeKey, UpdateItemOperationConfig config, bool isAsync) { Key key = (hashKey != null || rangeKey != null) ? MakeKey(hashKey, rangeKey) : MakeKey(doc); return UpdateHelper(doc, key, config, isAsync); }
/// <summary> /// Initiates the asynchronous execution of the UpdateItem operation. /// <seealso cref="Amazon.DynamoDBv2.DocumentModel.Table.UpdateItem"/> /// </summary> /// <param name="doc">Attributes to update.</param> /// <param name="hashKey">Hash key element of the document.</param> /// <param name="rangeKey">Range key element of the document.</param> /// <param name="config">Configuration to use.</param> /// <param name="cancellationToken">Token which can be used to cancel the task.</param> /// <returns>A Task that can be used to poll or wait for results, or both.</returns> public Task<Document> UpdateItemAsync(Document doc, Primitive hashKey, Primitive rangeKey, UpdateItemOperationConfig config, CancellationToken cancellationToken = default(CancellationToken)) { return AsyncRunner.Run(() => UpdateHelper(doc, hashKey, rangeKey, config, true), cancellationToken); }
/// <summary> /// Update a document in DynamoDB, with a hash-and-range primary key to identify /// the document, and using the specified config. /// </summary> /// <param name="doc">Attributes to update.</param> /// <param name="hashKey">Hash key element of the document.</param> /// <param name="rangeKey">Range key element of the document.</param> /// <param name="config">Configuration to use.</param> /// <returns>True if updated or false if the condition in the config was not met.</returns> /// <seealso cref="Amazon.DynamoDBv2.DocumentModel.UpdateItemOperationConfig"/> public bool TryUpdateItem(Document doc, Primitive hashKey, Primitive rangeKey, UpdateItemOperationConfig config = null) { try { UpdateHelper(doc, MakeKey(hashKey, rangeKey), config, false); return true; } catch (ConditionalCheckFailedException) { return false; } }
/// <summary> /// Initiates the asynchronous execution of the UpdateItem operation. /// <seealso cref="Amazon.DynamoDBv2.DocumentModel.Table.UpdateItem"/> /// </summary> /// <param name="doc">Attributes to update.</param> /// <param name="key">Key of the document.</param> /// <param name="config">Configuration to use.</param> /// <param name="callback">The callback that will be invoked when the asynchronous operation completes.</param> /// <param name="asyncOptions">An instance of AsyncOptions that specifies how the async method should be executed.</param> public void UpdateItemAsync(Document doc, IDictionary <string, DynamoDBEntry> key, UpdateItemOperationConfig config, AmazonDynamoDBCallback <Document> callback, AsyncOptions asyncOptions = null) { asyncOptions = asyncOptions ?? new AsyncOptions(); DynamoDBAsyncExecutor.ExecuteAsync <Document>( () => { return(UpdateHelper(doc, MakeKey(key), config, true)); }, asyncOptions, callback); }
/// <summary> /// Initiates the asynchronous execution of the UpdateItem operation. /// <seealso cref="Amazon.DynamoDBv2.DocumentModel.Table.UpdateItem"/> /// </summary> /// <param name="doc">Attributes to update.</param> /// <param name="key">Key of the document.</param> /// <param name="config">Configuration to use.</param> /// <param name="callback">An AsyncCallback delegate that is invoked when the operation completes.</param> /// <param name="state">A user-defined state object that is passed to the callback procedure. Retrieve this object from within the callback /// procedure using the AsyncState property.</param> /// <returns>An IAsyncResult that can be used to poll or wait for results, or both; this value is also needed when invoking EndUpdateItem /// operation.</returns> public IAsyncResult BeginUpdateItem(Document doc, IDictionary <string, DynamoDBEntry> key, UpdateItemOperationConfig config, AsyncCallback callback, object state) { return(DynamoDBAsyncExecutor.BeginOperation(() => UpdateHelper(doc, MakeKey(key), config, true), callback, state)); }
/// <summary> /// Initiates the asynchronous execution of the UpdateItem operation. /// <seealso cref="Amazon.DynamoDBv2.DocumentModel.Table.UpdateItem"/> /// </summary> /// <param name="doc">Attributes to update.</param> /// <param name="hashKey">Hash key element of the document.</param> /// <param name="rangeKey">Range key element of the document.</param> /// <param name="config">Configuration to use.</param> /// <param name="cancellationToken">Token which can be used to cancel the task.</param> /// <returns>A Task that can be used to poll or wait for results, or both.</returns> public Task <Document> UpdateItemAsync(Document doc, Primitive hashKey, Primitive rangeKey, UpdateItemOperationConfig config, CancellationToken cancellationToken = default(CancellationToken)) { return(UpdateHelperAsync(doc, hashKey, rangeKey, config, cancellationToken)); }
/// <summary> /// Initiates the asynchronous execution of the UpdateItem operation. /// </summary> /// <param name="doc">Attributes to update.</param> /// <param name="key">Key of the document.</param> /// <param name="config">Configuration to use.</param> /// <param name="callback">The callback that will be invoked when the asynchronous operation completes.</param> /// <param name="asyncOptions">An instance of AsyncOptions that specifies how the async method should be executed.</param> public void UpdateItemAsync(Document doc, IDictionary<string, DynamoDBEntry> key, UpdateItemOperationConfig config, AmazonDynamoDBCallback<Document> callback, AsyncOptions asyncOptions = null) { asyncOptions = asyncOptions ?? new AsyncOptions(); DynamoDBAsyncExecutor.ExecuteAsync<Document>( () => { return UpdateHelper(doc, MakeKey(key), config, true); }, asyncOptions, callback); }
/// <summary> /// Update a document in DynamoDB, with a key to identify the /// document, and using the specified config. /// </summary> /// <param name="doc">Attributes to update.</param> /// <param name="key">Key of the document.</param> /// <param name="config">Configuration to use.</param> /// <returns>Null or updated attributes, depending on config.</returns> /// <seealso cref="Amazon.DynamoDBv2.DocumentModel.UpdateItemOperationConfig"/> public Document UpdateItem(Document doc, IDictionary <string, DynamoDBEntry> key, UpdateItemOperationConfig config) { return(UpdateHelper(doc, MakeKey(key), config)); }
/// <summary> /// Initiates the asynchronous execution of the UpdateItem operation. /// <seealso cref="Amazon.DynamoDBv2.DocumentModel.Table.UpdateItem"/> /// </summary> /// <param name="doc">Attributes to update.</param> /// <param name="key">Key of the document.</param> /// <param name="config">Configuration to use.</param> /// <param name="cancellationToken">Token which can be used to cancel the task.</param> /// <returns>A Task that can be used to poll or wait for results, or both.</returns> public Task <Document> UpdateItemAsync(Document doc, IDictionary <string, DynamoDBEntry> key, UpdateItemOperationConfig config, CancellationToken cancellationToken = default(CancellationToken)) { return(UpdateHelperAsync(doc, MakeKey(key), config, cancellationToken)); }
/// <summary> /// Initiates the asynchronous execution of the UpdateItem operation. /// <seealso cref="Amazon.DynamoDBv2.DocumentModel.Table.UpdateItem"/> /// </summary> /// <param name="doc">Document to update.</param> /// <param name="config">Configuration to use.</param> /// <param name="callback">An AsyncCallback delegate that is invoked when the operation completes.</param> /// <param name="state">A user-defined state object that is passed to the callback procedure. Retrieve this object from within the callback /// procedure using the AsyncState property.</param> /// <returns>An IAsyncResult that can be used to poll or wait for results, or both; this value is also needed when invoking EndUpdateItem /// operation.</returns> public IAsyncResult BeginUpdateItem(Document doc, UpdateItemOperationConfig config, AsyncCallback callback, object state) { return(DynamoDBAsyncExecutor.BeginOperation(() => UpdateHelper(doc, MakeKey(doc), config), callback, state)); }
/// <summary> /// Update a document in DynamoDB, using specified config. /// </summary> /// <param name="doc">Document to update.</param> /// <param name="config">Configuration to use.</param> /// <returns>Null or updated attributes, depending on config.</returns> /// <seealso cref="Amazon.DynamoDBv2.DocumentModel.UpdateItemOperationConfig"/> public Document UpdateItem(Document doc, UpdateItemOperationConfig config) { return(UpdateHelper(doc, MakeKey(doc), config)); }
/// <summary> /// Update a document in DynamoDB, with a hash primary key to identify /// the document, and using the specified config. /// </summary> /// <param name="doc">Attributes to update.</param> /// <param name="hashKey">Hash key element of the document.</param> /// <param name="config">Configuration to use.</param> /// <returns>Null or updated attributes, depending on config.</returns> /// <seealso cref="Amazon.DynamoDBv2.DocumentModel.UpdateItemOperationConfig"/> public Document UpdateItem(Document doc, Primitive hashKey, UpdateItemOperationConfig config = null) { return(UpdateHelper(doc, MakeKey(hashKey, null), config, false)); }
/// <summary> /// Update a document in DynamoDB, with a key to identify the /// document, and using the specified config. /// </summary> /// <param name="doc">Attributes to update.</param> /// <param name="key">Key of the document.</param> /// <param name="config">Configuration to use.</param> /// <returns>True if updated or false if the condition in the config was not met.</returns> /// <seealso cref="Amazon.DynamoDBv2.DocumentModel.UpdateItemOperationConfig"/> public bool TryUpdateItem(Document doc, IDictionary <string, DynamoDBEntry> key, UpdateItemOperationConfig config) { try { UpdateHelper(doc, MakeKey(key), config); return(true); } catch (ConditionalCheckFailedException) { return(false); } }
/// <summary> /// Update a document in DynamoDB, using specified config. /// </summary> /// <param name="doc">Document to update.</param> /// <param name="config">Configuration to use.</param> /// <returns>Null or updated attributes, depending on config.</returns> /// <seealso cref="Amazon.DynamoDBv2.DocumentModel.UpdateItemOperationConfig"/> public Document UpdateItem(Document doc, UpdateItemOperationConfig config = null) { return UpdateHelper(doc, MakeKey(doc), config, false); }
/// <summary> /// Update a document in DynamoDB, with a hash-and-range primary key to identify /// the document, and using the specified config. /// </summary> /// <param name="doc">Attributes to update.</param> /// <param name="hashKey">Hash key element of the document.</param> /// <param name="rangeKey">Range key element of the document.</param> /// <param name="config">Configuration to use.</param> /// <returns>Null or updated attributes, depending on config.</returns> /// <seealso cref="Amazon.DynamoDBv2.DocumentModel.UpdateItemOperationConfig"/> public Document UpdateItem(Document doc, Primitive hashKey, Primitive rangeKey, UpdateItemOperationConfig config) { return(UpdateHelper(doc, MakeKey(hashKey, rangeKey), config)); }
/// <summary> /// Update a document in DynamoDB, with a key to identify the /// document, and using the specified config. /// </summary> /// <param name="doc">Attributes to update.</param> /// <param name="key">Key of the document.</param> /// <param name="config">Configuration to use.</param> /// <returns>Null or updated attributes, depending on config.</returns> /// <seealso cref="Amazon.DynamoDBv2.DocumentModel.UpdateItemOperationConfig"/> public Document UpdateItem(Document doc, IDictionary<string, DynamoDBEntry> key, UpdateItemOperationConfig config = null) { return UpdateHelper(doc, MakeKey(key), config, false); }
internal Document UpdateHelper(Document doc, Primitive hashKey, Primitive rangeKey, UpdateItemOperationConfig config) { Key key = (hashKey != null || rangeKey != null) ? MakeKey(hashKey, rangeKey) : MakeKey(doc); return(UpdateHelper(doc, key, config)); }
/// <summary> /// Update a document in DynamoDB, with a key to identify the /// document, and using the specified config. /// </summary> /// <param name="doc">Attributes to update.</param> /// <param name="key">Key of the document.</param> /// <param name="config">Configuration to use.</param> /// <returns>True if updated or false if the condition in the config was not met.</returns> /// <seealso cref="Amazon.DynamoDBv2.DocumentModel.UpdateItemOperationConfig"/> public bool TryUpdateItem(Document doc, IDictionary<string, DynamoDBEntry> key, UpdateItemOperationConfig config = null) { try { UpdateHelper(doc, MakeKey(key), config, false); return true; } catch (ConditionalCheckFailedException) { return false; } }
internal async Task <Document> UpdateHelperAsync(Document doc, Key key, UpdateItemOperationConfig config, CancellationToken cancellationToken) { var currentConfig = config ?? new UpdateItemOperationConfig(); // If the keys have been changed, treat entire document as having changed bool haveKeysChanged = HaveKeysChanged(doc); bool updateChangedAttributesOnly = !haveKeysChanged; var attributeUpdates = this.ToAttributeUpdateMap(doc, updateChangedAttributesOnly); foreach (var keyName in this.KeyNames) { attributeUpdates.Remove(keyName); } UpdateItemRequest req = new UpdateItemRequest { TableName = TableName, Key = key, AttributeUpdates = attributeUpdates.Count == 0 ? null : attributeUpdates, // pass null if keys-only update ReturnValues = EnumMapper.Convert(currentConfig.ReturnValues) }; this.AddRequestHandler(req, isAsync: true); ValidateConditional(currentConfig); if (currentConfig.Expected != null) { req.Expected = this.ToExpectedAttributeMap(currentConfig.Expected); } else if (currentConfig.ExpectedState != null && currentConfig.ExpectedState.ExpectedValues != null && currentConfig.ExpectedState.ExpectedValues.Count > 0) { req.Expected = currentConfig.ExpectedState.ToExpectedAttributeMap(this); if (req.Expected.Count > 1) { req.ConditionalOperator = EnumMapper.Convert(currentConfig.ExpectedState.ConditionalOperator); } } else if (currentConfig.ConditionalExpression != null && currentConfig.ConditionalExpression.IsSet) { currentConfig.ConditionalExpression.ApplyExpression(req, this); string statement; Dictionary <string, AttributeValue> expressionAttributeValues; Dictionary <string, string> expressionAttributeNames; Common.ConvertAttributeUpdatesToUpdateExpression(attributeUpdates, out statement, out expressionAttributeValues, out expressionAttributeNames); req.AttributeUpdates = null; req.UpdateExpression = statement; if (req.ExpressionAttributeValues == null) { req.ExpressionAttributeValues = expressionAttributeValues; } else { foreach (var kvp in expressionAttributeValues) { req.ExpressionAttributeValues.Add(kvp.Key, kvp.Value); } } if (req.ExpressionAttributeNames == null) { req.ExpressionAttributeNames = expressionAttributeNames; } else { foreach (var kvp in expressionAttributeNames) { req.ExpressionAttributeNames.Add(kvp.Key, kvp.Value); } } } var resp = await DDBClient.UpdateItemAsync(req, cancellationToken).ConfigureAwait(false); var returnedAttributes = resp.Attributes; doc.CommitChanges(); Document ret = null; if (currentConfig.ReturnValues != ReturnValues.None) { ret = this.FromAttributeMap(returnedAttributes); } return(ret); }
/// <summary> /// Update a document in DynamoDB, with a hash-and-range primary key to identify /// the document, and using the specified config. /// </summary> /// <param name="doc">Attributes to update.</param> /// <param name="hashKey">Hash key element of the document.</param> /// <param name="rangeKey">Range key element of the document.</param> /// <param name="config">Configuration to use.</param> /// <returns>Null or updated attributes, depending on config.</returns> /// <seealso cref="Amazon.DynamoDBv2.DocumentModel.UpdateItemOperationConfig"/> public Document UpdateItem(Document doc, Primitive hashKey, Primitive rangeKey, UpdateItemOperationConfig config = null) { return UpdateHelper(doc, MakeKey(hashKey, rangeKey), config, false); }
internal Document UpdateHelper(Document doc, Key key, UpdateItemOperationConfig config, bool isAsync) { var currentConfig = config ?? new UpdateItemOperationConfig(); // If the keys have been changed, treat entire document as having changed bool haveKeysChanged = HaveKeysChanged(doc); bool updateChangedAttributesOnly = !haveKeysChanged; var attributeUpdates = doc.ToAttributeUpdateMap(Conversion, updateChangedAttributesOnly); foreach (var keyName in this.KeyNames) { attributeUpdates.Remove(keyName); } UpdateItemRequest req = new UpdateItemRequest { TableName = TableName, Key = key, AttributeUpdates = attributeUpdates.Count == 0 ? null : attributeUpdates, // pass null if keys-only update ReturnValues = EnumMapper.Convert(currentConfig.ReturnValues) }; ((Amazon.Runtime.Internal.IAmazonWebServiceRequest)req).AddBeforeRequestHandler(isAsync ? new RequestEventHandler(UserAgentRequestEventHandlerAsync) : new RequestEventHandler(UserAgentRequestEventHandlerSync) ); ValidateConditional(currentConfig); if (currentConfig.Expected != null) { req.Expected = currentConfig.Expected.ToExpectedAttributeMap(Conversion); } else if (currentConfig.ExpectedState != null && currentConfig.ExpectedState.ExpectedValues != null && currentConfig.ExpectedState.ExpectedValues.Count > 0) { req.Expected = currentConfig.ExpectedState.ToExpectedAttributeMap(Conversion); if (req.Expected.Count > 1) req.ConditionalOperator = EnumMapper.Convert(currentConfig.ExpectedState.ConditionalOperator); } else if (currentConfig.ConditionalExpression != null && currentConfig.ConditionalExpression.IsSet) { currentConfig.ConditionalExpression.ApplyExpression(req, this.Conversion); string statement; Dictionary<string, AttributeValue> expressionAttributeValues; Dictionary<string, string> expressionAttributeNames; Common.ConvertAttributeUpdatesToUpdateExpression(attributeUpdates, out statement, out expressionAttributeValues, out expressionAttributeNames); req.AttributeUpdates = null; req.UpdateExpression = statement; if (req.ExpressionAttributeValues == null) req.ExpressionAttributeValues = expressionAttributeValues; else { foreach (var kvp in expressionAttributeValues) req.ExpressionAttributeValues.Add(kvp.Key, kvp.Value); } if (req.ExpressionAttributeNames == null) req.ExpressionAttributeNames = expressionAttributeNames; else { foreach (var kvp in expressionAttributeNames) req.ExpressionAttributeNames.Add(kvp.Key, kvp.Value); } } var resp = DDBClient.UpdateItem(req); var returnedAttributes = resp.Attributes; doc.CommitChanges(); Document ret = null; if (currentConfig.ReturnValues != ReturnValues.None) { ret = Document.FromAttributeMap(returnedAttributes); } return ret; }
/// <summary> /// Initiates the asynchronous execution of the UpdateItem operation. /// <seealso cref="Amazon.DynamoDBv2.DocumentModel.Table.UpdateItem"/> /// </summary> /// <param name="doc">Attributes to update.</param> /// <param name="config">Configuration to use.</param> /// <param name="cancellationToken">Token which can be used to cancel the task.</param> /// <returns>A Task that can be used to poll or wait for results, or both.</returns> public Task<Document> UpdateItemAsync(Document doc, UpdateItemOperationConfig config, CancellationToken cancellationToken = default(CancellationToken)) { return AsyncRunner.Run(() => UpdateHelper(doc, null, null, config, true), cancellationToken); }