Esempio n. 1
0
        /// <summary>
        /// Remove one row from the reminder table
        /// </summary>
        /// <param name="grainRef"> specific grain ref to locate the row </param>
        /// <param name="reminderName"> remider name to locate the row </param>
        /// <param name="eTag"> e tag </param>
        /// <returns> Return true if the row was removed </returns>
        public async Task <bool> RemoveRow(GrainReference grainRef, string reminderName, string eTag)
        {
            var reminderId = ConstructReminderId(serviceId, grainRef, reminderName);

            var keys = new Dictionary <string, AttributeValue>
            {
                { $"{REMINDER_ID_PROPERTY_NAME}", new AttributeValue(reminderId) },
                { $"{GRAIN_HASH_PROPERTY_NAME}", new AttributeValue {
                      N = grainRef.GetUniformHashCode().ToString()
                  } }
            };

            try
            {
                var conditionalValues = new Dictionary <string, AttributeValue> {
                    { CURRENT_ETAG_ALIAS, new AttributeValue {
                          N = eTag
                      } }
                };
                var expression = $"{ETAG_PROPERTY_NAME} = {CURRENT_ETAG_ALIAS}";

                await storage.DeleteEntryAsync(TABLE_NAME_DEFAULT_VALUE, keys, expression, conditionalValues).ConfigureAwait(false);

                return(true);
            }
            catch (ConditionalCheckFailedException)
            {
                return(false);
            }
        }
Esempio n. 2
0
        /// <summary> Clear / Delete state data function for this storage provider. </summary>
        /// <remarks>
        /// If the <c>DeleteStateOnClear</c> is set to <c>true</c> then the table row
        /// for this grain will be deleted / removed, otherwise the table row will be
        /// cleared by overwriting with default / null values.
        /// </remarks>
        /// <see cref="IStorageProvider.ClearStateAsync"/>
        public async Task ClearStateAsync(string grainType, GrainReference grainReference, IGrainState grainState)
        {
            if (storage == null)
            {
                throw new ArgumentException("GrainState-Table property not initialized");
            }

            string partitionKey = GetKeyString(grainReference);

            if (Log.IsVerbose3)
            {
                Log.Verbose3(ErrorCode.StorageProviderBase, "Clearing: GrainType={0} Pk={1} Grainid={2} ETag={3} DeleteStateOnClear={4} from Table={5}", grainType, partitionKey, grainReference, grainState.ETag, isDeleteStateOnClear, tableName);
            }
            string rowKey = AWSUtils.ValidateDynamoDBRowKey(grainType);
            var    record = new GrainStateRecord {
                GrainReference = partitionKey, ETag = string.IsNullOrWhiteSpace(grainState.ETag) ? 0 : int.Parse(grainState.ETag), GrainType = rowKey
            };

            var operation = "Clearing";

            try
            {
                if (isDeleteStateOnClear)
                {
                    operation = "Deleting";
                    var keys = new Dictionary <string, AttributeValue>();
                    keys.Add(GRAIN_REFERENCE_PROPERTY_NAME, new AttributeValue(record.GrainReference));
                    keys.Add(GRAIN_TYPE_PROPERTY_NAME, new AttributeValue(record.GrainType));

                    await storage.DeleteEntryAsync(tableName, keys).ConfigureAwait(false);

                    grainState.ETag = string.Empty;
                }
                else
                {
                    await WriteStateInternal(grainState, record, true);
                }
            }
            catch (Exception exc)
            {
                Log.Error(ErrorCode.StorageProviderBase, string.Format("Error {0}: GrainType={1} Grainid={2} ETag={3} from Table={4} Exception={5}",
                                                                       operation, grainType, grainReference, grainState.ETag, tableName, exc.Message), exc);
                throw;
            }
        }