Configuration for the Table.UpdateItem operation
Inheritance: IConditionalOperationConfig
Exemple #1
0
        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);
        }
Exemple #2
0
 /// <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);
 }
Exemple #3
0
 /// <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);
     }
 }
Exemple #4
0
 /// <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);
     }
 }
Exemple #5
0
        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);
        }
Exemple #6
0
 /// <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));
 }
Exemple #7
0
 /// <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);
 }
Exemple #8
0
 /// <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));
 }
Exemple #9
0
 /// <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));
 }
Exemple #11
0
 /// <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);
 }
Exemple #12
0
 /// <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);
 }
Exemple #13
0
        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;
        }
Exemple #14
0
 /// <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));
 }
Exemple #16
0
 /// <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);
 }
Exemple #17
0
        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));
 }
Exemple #19
0
 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);
 }
Exemple #20
0
 /// <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);
 }
Exemple #21
0
 /// <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;
     }
 }
Exemple #22
0
 /// <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);
 }
Exemple #23
0
 /// <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));
 }
Exemple #24
0
 /// <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));
 }
Exemple #25
0
 /// <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);
 }
Exemple #26
0
 /// <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));
 }
Exemple #27
0
 /// <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));
 }
Exemple #28
0
 /// <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));
 }
Exemple #29
0
 /// <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));
 }
Exemple #30
0
 /// <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));
 }
Exemple #31
0
 /// <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);
     }
 }
Exemple #32
0
 /// <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);
 }
Exemple #33
0
 /// <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));
 }
Exemple #34
0
 /// <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);
 }
Exemple #35
0
        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));
        }
Exemple #36
0
 /// <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;
     }
 }
Exemple #37
0
        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);
        }
Exemple #38
0
 /// <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);
 }
Exemple #39
0
        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;
        }
Exemple #40
0
 /// <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);
 }