Example #1
0
        public static void Replace(this IStorageTable table, ITableEntity entity)
        {
            if (table == null)
            {
                throw new ArgumentNullException("table");
            }

            IStorageTableOperation operation = table.CreateReplaceOperation(entity);

            table.ExecuteAsync(operation, CancellationToken.None).GetAwaiter().GetResult();
        }
        public Task SetValueAsync(object value, CancellationToken cancellationToken)
        {
            // Not ByRef, so can ignore value argument.

            if (_value.PartitionKey != _entityContext.PartitionKey || _value.RowKey != _entityContext.RowKey)
            {
                throw new InvalidOperationException(
                          "When binding to a table entity, the partition key and row key must not be changed.");
            }

            if (HasChanged)
            {
                IStorageTable          table     = _entityContext.Table;
                IStorageTableOperation operation = table.CreateReplaceOperation(_value);
                return(table.ExecuteAsync(operation, cancellationToken));
            }

            return(Task.FromResult(0));
        }
Example #3
0
        public Task SetValueAsync(object value, CancellationToken cancellationToken)
        {
            // Not ByRef, so can ignore value argument.
            ITableEntity entity = _converter.Convert(_value);

            if (!_converter.ConvertsPartitionKey)
            {
                entity.PartitionKey = _entityContext.PartitionKey;
            }

            if (!_converter.ConvertsRowKey)
            {
                entity.RowKey = _entityContext.RowKey;
            }

            if (!_converter.ConvertsETag)
            {
                entity.ETag = _eTag;
            }

            if (entity.PartitionKey != _entityContext.PartitionKey)
            {
                throw new InvalidOperationException(
                          "When binding to a table entity, the partition key must not be changed.");
            }

            if (entity.RowKey != _entityContext.RowKey)
            {
                throw new InvalidOperationException(
                          "When binding to a table entity, the row key must not be changed.");
            }

            if (HasChanges(entity))
            {
                IStorageTable          table     = _entityContext.Table;
                IStorageTableOperation operation = table.CreateReplaceOperation(entity);
                return(table.ExecuteAsync(operation, cancellationToken));
            }

            return(Task.FromResult(0));
        }
Example #4
0
        public async Task AddAsync(T item, CancellationToken cancellationToken = default(CancellationToken))
        {
            // Careful:
            // 1. even with upsert, all rowkeys within a batch must be unique. If they aren't, the previous items
            // will be flushed.
            // 2. Capture at time of Add, in case item is mutated after add.
            // 3. Validate rowkey on the client so we get a nice error instead of the cryptic 400 from auzre.

            string partitionKey = item.PartitionKey;
            string rowKey       = item.RowKey;

            TableClient.ValidateAzureTableKeyValue(partitionKey);
            TableClient.ValidateAzureTableKeyValue(rowKey);

            Dictionary <string, IStorageTableOperation> partition;

            if (!_map.TryGetValue(partitionKey, out partition))
            {
                if (_map.Count >= MaxPartitionWidth)
                {
                    // Offline cache is too large. Clear some room
                    await FlushAsync(cancellationToken);
                }

                partition          = new Dictionary <string, IStorageTableOperation>();
                _map[partitionKey] = partition;
            }

            var itemCopy = Copy(item);

            if (partition.ContainsKey(rowKey))
            {
                // Replacing item forces a flush to ensure correct eTag behaviour.
                await FlushPartitionAsync(partition, cancellationToken);

                // Reinitialize partition
                partition          = new Dictionary <string, IStorageTableOperation>();
                _map[partitionKey] = partition;
            }

            _log.EntitiesWritten++;

            if (String.IsNullOrEmpty(itemCopy.ETag))
            {
                partition.Add(rowKey, _table.CreateInsertOperation(itemCopy));
            }
            else if (itemCopy.ETag.Equals("*"))
            {
                partition.Add(rowKey, _table.CreateInsertOrReplaceOperation(itemCopy));
            }
            else
            {
                partition.Add(rowKey, _table.CreateReplaceOperation(itemCopy));
            }

            if (partition.Count >= MaxBatchSize)
            {
                await FlushPartitionAsync(partition, cancellationToken);

                _map.Remove(partitionKey);
            }
        }