public async Task ProcessEventsAsync(PartitionContext context, IEnumerable<EventData> messages)
        {
            var batch = new TableBatchOperation();

            foreach(var msg in messages)
            {
                var snap = JsonConvert.DeserializeObject<BusSnapshotInfo>(Encoding.UTF8.GetString(msg.GetBytes()));

                var entity = new DynamicTableEntity(snap.RouteShortName, snap.VehicleId.ToString());

                entity.Properties.Add("RouteShortName", EntityProperty.GeneratePropertyForString(snap.RouteShortName));
                entity.Properties.Add("VehicleId", EntityProperty.GeneratePropertyForInt(snap.VehicleId));
                entity.Properties.Add("TripId", EntityProperty.GeneratePropertyForInt(snap.TripId));
                entity.Properties.Add("Latitude", EntityProperty.GeneratePropertyForDouble(snap.Latitude));
                entity.Properties.Add("Longitude", EntityProperty.GeneratePropertyForDouble(snap.Longitude));
                entity.Properties.Add("DirectionOfTravel", EntityProperty.GeneratePropertyForString(snap.DirectionOfTravel.ToString()));
                entity.Properties.Add("NextStopId", EntityProperty.GeneratePropertyForInt(snap.NextStopId));
                entity.Properties.Add("Timeliness", EntityProperty.GeneratePropertyForString(snap.Timeliness.ToString()));
                entity.Properties.Add("TimelinessOffset", EntityProperty.GeneratePropertyForInt(snap.TimelinessOffset));
                entity.Properties.Add("Timestamp", EntityProperty.GeneratePropertyForDateTimeOffset(snap.Timestamp));

                batch.Add(TableOperation.InsertOrReplace(entity));
            }

            var tableClient = _account.CreateCloudTableClient();

            var table = tableClient.GetTableReference("snapshots");

            await table.CreateIfNotExistsAsync();

            await table.ExecuteBatchAsync(batch);

            await context.CheckpointAsync();
        }
        public void ObjectToDynamicMeasurement()
        {
            // Arrange
            Country entity = ObjectsFactory.GetCountry();
            Stopwatch stopWatch = Stopwatch.StartNew();
            DynamicTableEntity dynamicTableEntity = null;

            // Act
            for (int i = 0; i < IteractionsCount; i++)
            {
                dynamicTableEntity = new DynamicTableEntity(entity.Continent, entity.Name)
                    {
                        Properties = new Dictionary<string, EntityProperty>
                            {
                                {"Area", new EntityProperty(entity.Area)},
                                {"TopSecretKey", new EntityProperty(entity.TopSecretKey)},
                                {"Formed", new EntityProperty(entity.Formed)},
                                {"Id", new EntityProperty(entity.Id)},
                                {"IsExists", new EntityProperty(entity.IsExists)},
                                {"Population", new EntityProperty(entity.Population)},
                                {"PresidentsCount", new EntityProperty(entity.PresidentsCount)}
                            }
                    };
            }

            stopWatch.Stop();

            Assert.NotNull(dynamicTableEntity);

            Console.WriteLine(ResultFormat, IteractionsCount, stopWatch.ElapsedMilliseconds);
        }
Ejemplo n.º 3
0
        public static DynamicTableEntity ToDte(Post post)
        {
            var dte = new DynamicTableEntity(post.UniqueKey, string.Empty);

            dte.Properties.Add("Slug", EntityProperty.GeneratePropertyForString(post.Slug));
            dte.Properties.Add("Title", EntityProperty.GeneratePropertyForString(WebUtility.HtmlEncode(post.Title)));
            dte.Properties.Add("Date", EntityProperty.GeneratePropertyForDateTimeOffset(post.Date));
            dte.Properties.Add("DateModified", EntityProperty.GeneratePropertyForDateTimeOffset(post.DateModified));
            dte.Properties.Add("Categories", EntityProperty.GeneratePropertyForString(Category.ToString(post.Categories)));
            dte.Properties.Add("Tags", EntityProperty.GeneratePropertyForString(Category.ToString(post.Tags)));
            dte.Properties.Add("Author", EntityProperty.GeneratePropertyForString(PostAuthor.ToString(post.Author)));
            dte.Properties.Add("CommentCount", EntityProperty.GeneratePropertyForInt(post.CommentCount));

            if (post.DasBlogEntryId.HasValue)
            {
                dte.Properties.Add("DasBlogEntryId", EntityProperty.GeneratePropertyForGuid(post.DasBlogEntryId.Value));
            }

            if (!string.IsNullOrEmpty(post.DasBlogTitle))
            {
                dte.Properties.Add("DasBlogTitle", EntityProperty.GeneratePropertyForString(post.DasBlogTitle));
            }

            if (!string.IsNullOrEmpty(post.DasBlogUniqueTitle))
            {
                dte.Properties.Add("DasBlogUniqueTitle", EntityProperty.GeneratePropertyForString(post.DasBlogUniqueTitle));
            }

            return dte;
        }
Ejemplo n.º 4
0
		public async Task<long> NextId()
		{
			lock (nextLock)
			{
				if (last < max)
				{
					return ++last;
				}
			}
			await semaphore.WaitAsync();
			try
			{
				lock (nextLock)
				{
					if (last < max)
					{
						return ++last;
					}
				}
				for (var tries = 0; tries < 10; ++tries)
				{
					HttpStatusCode saveResultCode;
					long oldMax, newMax;
					var hiEntity = await HiloTable.RetreiveAsync(tableName, "hi");
					if (hiEntity == null)
					{
						oldMax = 0;
						newMax = chunkSize;
						hiEntity = new DynamicTableEntity(tableName, "hi");
						hiEntity["max"] = new EntityProperty(newMax);
						saveResultCode = await HiloTable.InsertAsync(hiEntity);
					}
					else
					{
						oldMax = hiEntity["max"].Int64Value.GetValueOrDefault(0);
						newMax = oldMax + chunkSize;
						hiEntity["max"] = new EntityProperty(newMax);
						saveResultCode = await HiloTable.ReplaceAsync(hiEntity);
					}
					if (saveResultCode == HttpStatusCode.Created || saveResultCode == HttpStatusCode.NoContent)
					{
						lock (nextLock)
						{
							last = oldMax;
							max = newMax;
							return ++last;
						}
					}
				}
				throw new Exception(
					string.Format(
						"Could not allocate id range for table '{0}' with chunkSize={1} due to high contention.\r\nConsider increasing the chunk size",
						tableName, chunkSize));
			}
			finally
			{
				semaphore.Release();
			}

		}
        T StripDTO(Microsoft.WindowsAzure.Storage.Table.DynamicTableEntity a)
        {
            T result = new T();


            Type t1         = result.GetType();
            var  dictionary = (IDictionary <string, EntityProperty>)a.Properties;

            foreach (PropertyInfo p1 in t1.GetProperties())      //for each property in the entity,
            {
                foreach (var value in dictionary)                //see if we have a correspinding property in the DTO
                {
                    if (p1.Name == value.Key)
                    {
                        Type t = p1.PropertyType;
                        if (t.IsPrimitive || t == typeof(string))
                        {
                            p1.SetValue(result, GetValue(value.Value));
                        }
                        else if (t.IsGenericType && typeof(ICollection <>).IsAssignableFrom(t.GetGenericTypeDefinition()) ||
                                 t.GetInterfaces().Any(x => x.IsGenericType && x.GetGenericTypeDefinition() == typeof(ICollection <>)) || t.IsClass)
                        {
                            var customClass = JsonConvert.DeserializeObject(value.Value.StringValue, t);
                            p1.SetValue(result, customClass);
                        }
                    }
                }
            }

            return(result);
        }
Ejemplo n.º 6
0
        public void Insert()
        {
            TableBatchOperation batchOperation = new TableBatchOperation();
            Hashtable bank = new Hashtable();

            for (int i = id; i < id + 100 && i < Program._DATA_TABLE.Rows.Count; i++)
            {
                if (!bank.ContainsKey(Program._DATA_TABLE.Rows[i]["ID"].ToString().Trim()))
                {
                    try
                    {
                        DynamicTableEntity data = new DynamicTableEntity();
                        data.PartitionKey = "88888888";
                        data.RowKey = Program._DATA_TABLE.Rows[i]["ID"].ToString().Trim().PadLeft(6, '0');
                        Dictionary<string, EntityProperty> properties = new Dictionary<string, EntityProperty>();

                        properties.Add("Bank", new EntityProperty(Program._DATA_TABLE.Rows[i]["Bank"].ToString().Trim()));
                        properties.Add("BranchName", new EntityProperty(Program._DATA_TABLE.Rows[i]["BranchName"].ToString().ToLower().Trim()));
                        properties.Add("AccountNumber", new EntityProperty(Program._DATA_TABLE.Rows[i]["AccountNumber"].ToString().Trim()));
                        properties.Add("AccountName", new EntityProperty(Program._DATA_TABLE.Rows[i]["AccountName"].ToString().Trim()));
                        properties.Add("AccountType", new EntityProperty(int.Parse(Program._DATA_TABLE.Rows[i]["AccountType"].ToString().Trim())));

                        //BankEntity data = new BankEntity("88888888", Program._DATA_TABLE.Rows[i]["ID"].ToString().Trim().PadLeft(6, '0'));
                        //data.Bank = Program._DATA_TABLE.Rows[i]["Bank"].ToString().Trim();
                        //data.BranchName = Program._DATA_TABLE.Rows[i]["BranchName"].ToString().ToLower().Trim();
                        //data.AccountNumber = Program._DATA_TABLE.Rows[i]["AccountNumber"].ToString().Trim();
                        //data.AccountName = Program._DATA_TABLE.Rows[i]["AccountName"].ToString().Trim();
                        //data.AccountType = int.Parse(Program._DATA_TABLE.Rows[i]["AccountType"].ToString().Trim());
                        batchOperation.InsertOrMerge(data);
                        recBank++;
                        bank[Program._DATA_TABLE.Rows[i]["ID"].ToString().Trim()] = true;
                    }
                    catch { }
                }
            }

            try
            {
                if (Program._DATA_TABLE.Rows.Count > 0)
                {
                    if (Program._UPDATE)
                    {
                        Program._RECORD++;
                        Console.WriteLine("Update Record {0}-{1}\t\tTotal {2} Records", id + 1, id + 100, Program._RECORD * 100);
                        Program._CLOUD_TABLE.ExecuteBatch(batchOperation);
                    }
                    else
                    {
                        Program._RECORD++;
                        Console.WriteLine("Insert Record {0}-{1}\t\tTotal {2} Records", id + 1, id + 100, Program._RECORD * 100);
                        Program._CLOUD_TABLE.ExecuteBatch(batchOperation);
                    }
                }

            }
            catch (Exception e)
            {
                Program.WriteErrorLog("Record " + (id + 1) + "-" + (id + 100) + " Error \n" + e.Message + "\n" + e.StackTrace);
            }
        }
            public Entity(DynamicTableEntity entity)
            {
                var splitted = entity.RowKey.Split(new string[] { "-" }, StringSplitOptions.None);
                _PartitionKey = entity.PartitionKey;
                Timestamp = entity.Timestamp;
                TxId = uint256.Parse(splitted[0]);
                Type = GetType(splitted[1]);
                if(splitted.Length >= 3 && splitted[2] != string.Empty)
                    BlockId = uint256.Parse(splitted[2]);
                var bytes = Helper.GetEntityProperty(entity, "a");
                if(bytes != null && bytes.Length != 0)
                {
                    Transaction = new Transaction();
                    Transaction.ReadWrite(bytes);
                }
                bytes = Helper.GetEntityProperty(entity, "b");
                if(bytes != null && bytes.Length != 0)
                {
                    ColoredTransaction = new ColoredTransaction();
                    ColoredTransaction.ReadWrite(bytes);
                }
                _PreviousTxOuts = Helper.DeserializeList<TxOut>(Helper.GetEntityProperty(entity, "c"));

                var timestamp = Helper.GetEntityProperty(entity, "d");
                if(timestamp != null && timestamp.Length == 8)
                {
                    Timestamp = new DateTimeOffset((long)ToUInt64(timestamp, 0), TimeSpan.Zero);
                }
            }
        public static Action Create(this TransactionLog transactionLogMessage, string connectionString)
        {
            IAzureTableUtility azureTableUtility = ContextFactory.Create(connectionString, transactionLogMessage.TableName);

            JObject jsonMessage = JsonConvert.DeserializeObject<JObject>(transactionLogMessage.Object);

            DynamicTableEntity temp = new DynamicTableEntity();
            foreach (KeyValuePair<string, JToken> keyValuePair in jsonMessage)
            {
                if (keyValuePair.Key.Equals("Timestamp") || keyValuePair.Key.Equals("ETag"))
                    continue;
                
                if (keyValuePair.Key.Equals("PartitionKey"))
                    temp.PartitionKey = keyValuePair.Value.ToString();
                else if (keyValuePair.Key.Equals("RowKey"))
                    temp.RowKey = keyValuePair.Value.ToString();
                else
                    temp.Properties.Add(keyValuePair.Key, EntityProperty.CreateEntityPropertyFromObject(keyValuePair.Value));

            }

            string actionType = transactionLogMessage.Action;
            if (actionType.Equals("UPSERT", StringComparison.OrdinalIgnoreCase) || actionType.Equals("INSERT", StringComparison.OrdinalIgnoreCase))
                return () => azureTableUtility.Upset<DynamicTableEntity>(temp);
            if (actionType.Equals("DELETE", StringComparison.OrdinalIgnoreCase))
                return () => azureTableUtility.DeleteEntity(temp.PartitionKey, temp.RowKey);

            return default(Action);
        }
 public bool DeleteApiDetails()
 {
     var entity = new DynamicTableEntity(ApiRegistrationTableEntity.GetPartitionKey(ApiRegistrationProviderType.Jasper),
                         ApiRegistrationTableEntity.GetRowKey(ApiRegistrationProviderType.Jasper));
     entity.ETag = "*";
     _table.Execute(TableOperation.Delete(entity));
     return true;
 }
        public int PutVideo(string video)
        {
            var entity = new DynamicTableEntity("VidParams", "LastVideo") { ETag = "*" };
            entity.Properties["VideoCode"] = new EntityProperty(video);
            _updateOperation = TableOperation.Replace(entity);

            dynamic result = _table.Execute(_updateOperation).Result;
            return result.Properties.Count;
        }
 private void CopyEntity(DynamicTableEntity entity, CloudTable destTable)
 {
     ConsoleWrite.Verbose("    Copying {0} / {1}", entity.PartitionKey, entity.RowKey);
     if (_args.IsReal)
     {
         var operation = TableOperation.Insert(entity);
         destTable.Execute(operation);
     }
 }
Ejemplo n.º 12
0
        public void CanSpreadBytes()
        {
            var bytes =
                Helper.SerializeList(Enumerable.Range(0, 300000).Select(e => new OrderedBalanceChange.IntCompactVarInt((uint)e)).ToArray());

            DynamicTableEntity entity = new DynamicTableEntity();
            Helper.SetEntityProperty(entity, "a", bytes);
            var actualBytes = Helper.GetEntityProperty(entity, "a");
            Assert.True(actualBytes.SequenceEqual(bytes));
        }
		public void GetSizeTest()
		{
			var tableEntity = new DynamicTableEntity("TestPartitionKey", "TestRowKey");

			tableEntity.AddProperty("suppa", "duppa");

			tableEntity.AddProperty("suppa2", 10);

			var size = tableEntity.GetSize();
		}
Ejemplo n.º 14
0
        public void Insert()
        {
            TableBatchOperation batchOperation = new TableBatchOperation();
            Hashtable TransferD = new Hashtable();

            for (int i = id; i < id + 100 && i < Program._DATA_TABLE.Rows.Count; i++)
            {
                string Rowkey = (Program._DATA_TABLE.Rows[i]["ID"].ToString().Trim() + "-" + Program._DATA_TABLE.Rows[i]["SellNumber"].ToString().Trim());
                if (!TransferD.ContainsKey(Rowkey))
                {
                    try
                    {
                        DynamicTableEntity data = new DynamicTableEntity();
                        data.PartitionKey = "88888888";
                        data.RowKey = Rowkey;
                        Dictionary<string, EntityProperty> properties = new Dictionary<string, EntityProperty>();

                        properties.Add("SellNumber", new EntityProperty(Program._DATA_TABLE.Rows[i]["SellNumber"].ToString().Trim().PadLeft(8, '0')));
                        properties.Add("ReceiveMoney", new EntityProperty(double.Parse(Program._DATA_TABLE.Rows[i]["ReceiveMoney"].ToString().ToLower().Trim())));

                        //BankTransferDetailEntity data = new BankTransferDetailEntity("88888888", Rowkey);
                        //data.SellNumber = Program._DATA_TABLE.Rows[i]["SellNumber"].ToString().Trim().PadLeft(8, '0');
                        //data.ReceiveMoney = double.Parse(Program._DATA_TABLE.Rows[i]["ReceiveMoney"].ToString().ToLower().Trim());
                        batchOperation.InsertOrMerge(data);
                        recTransferD++;
                        TransferD[Rowkey] = true;
                    }
                    catch { }
                }
            }

            try
            {
                if (Program._DATA_TABLE.Rows.Count > 0)
                {
                    if (Program._UPDATE)
                    {
                        Program._RECORD++;
                        Console.WriteLine("Update Record {0}-{1}\t\tTotal {2} Records", id + 1, id + 100, Program._RECORD * 100);
                        Program._CLOUD_TABLE.ExecuteBatch(batchOperation);
                    }
                    else
                    {
                        Program._RECORD++;
                        Console.WriteLine("Insert Record {0}-{1}\t\tTotal {2} Records", id + 1, id + 100, Program._RECORD * 100);
                        Program._CLOUD_TABLE.ExecuteBatch(batchOperation);
                    }
                }

            }
            catch (Exception e)
            {
                Program.WriteErrorLog("Record " + (id + 1) + "-" + (id + 100) + " Error \n" + e.Message + "\n" + e.StackTrace);
            }
        }
        public void CanConvertFromSourceToSummary_WithAlternateTypeName()
        {
            var entity = new DynamicTableEntity("pk", "rk");
            entity.Properties["dpi"] = EntityProperty.GeneratePropertyForInt(2);
            entity.Properties["AlternateTypeName"] = EntityProperty.GeneratePropertyForString("vahshi");
            var source = new DiagnosticsSource(entity);

            var summary = source.ToSummary();

            Assert.Equal(summary.TypeName, source.ToTypeKey());
        }
Ejemplo n.º 16
0
        public void ActionResult(string id, IDictionary<string, string> result)
        {
            var resultEntity = new DynamicTableEntity(id, DateTime.UtcNow.ToString("s"));

            foreach (var resultProperty in result)
            {
                resultEntity.Properties[resultProperty.Key] = new EntityProperty(resultProperty.Value);
            }

            m_ResultTable.Execute(TableOperation.Insert(resultEntity));
        }
Ejemplo n.º 17
0
        /// <summary>
        /// Fire-and-forget logging of new sessions to nosql storage
        /// </summary>
        /// <param name="sessionID"></param>
        /// <param name="uri"></param>
        public static void LogAPIUse(string sessionID, string uri)
        {
            CloudStorageAccount csa = GetCUAHSILogsStorageAccount();
            CloudTableClient tableClient = csa.CreateCloudTableClient();
            CloudTable logTbl = tableClient.GetTableReference(DiscoveryStorageTableNames.SessionData);

            DynamicTableEntity log = new DynamicTableEntity(sessionID, LogHelper.DescendingRowKey(DateTime.UtcNow));
            log.Properties.Add("uri", new EntityProperty(uri));

            TableOperation logerror = TableOperation.Insert(log);
            logTbl.BeginExecute(logerror, null, null);
        }
Ejemplo n.º 18
0
        public static DynamicTableEntity ToDte(Comment comment, string postKey)
        {
            var dte = new DynamicTableEntity(postKey, comment.UniqueKey);

            dte.Properties.Add("Date", EntityProperty.GeneratePropertyForDateTimeOffset(comment.Date));
            dte.Properties.Add("Content", EntityProperty.GeneratePropertyForString(comment.Content));
            dte.Properties.Add("AuthorName", EntityProperty.GeneratePropertyForString(comment.Author.Name));
            dte.Properties.Add("AuthorEmail", EntityProperty.GeneratePropertyForString(comment.Author.Email));
            dte.Properties.Add("AuthorUrl", EntityProperty.GeneratePropertyForString(comment.Author.Url));

            return dte;
        }
        public WalletRuleEntry(DynamicTableEntity entity, IndexerClient client)
        {
            WalletId = Encoding.UTF8.GetString(Encoders.Hex.DecodeData(entity.PartitionKey));

            if (!entity.Properties.ContainsKey("a0")) //Legacy
            {
                Rule = Helper.DeserializeObject<WalletRule>(Encoding.UTF8.GetString(Encoders.Hex.DecodeData(entity.RowKey)));
            }
            else
            {
                Rule = Helper.DeserializeObject<WalletRule>(Encoding.UTF8.GetString(Helper.GetEntityProperty(entity, "a")));
            }
        }
        public static dynamic GenerateDynamicItem(DynamicTableEntity actualItem)
        {
            // Parse EdmTypes
            IEnumerable<PropertySpec> types = actualItem.Properties
                            .Where(p => TargetType.GetProperty(p.Key) == null)
                            .Select(p => new PropertySpec(Type.GetType("System." + p.Value.PropertyType.ToString()), p.Key));

            Type targetType = DynamicTypeFactory.GenerateDynamicType(TargetType, false, types.ToArray<PropertySpec>());

            dynamic dynamicItem = DynamicTypeFactory.GenerateCustomItem(targetType);

            return PopulateDynamicItem(targetType, actualItem, dynamicItem);
        }
        public DynamicTableEntity CreateTableEntity()
        {
            DynamicTableEntity entity = new DynamicTableEntity();
            entity.ETag = "*";
            entity.PartitionKey = Encoders.Hex.EncodeData(Encoding.UTF8.GetBytes(WalletId));

            if (Rule != null)
            {
                entity.RowKey = Rule.Id;
                Helper.SetEntityProperty(entity, "a", Encoding.UTF8.GetBytes(Helper.Serialize(Rule)));
            }
            return entity;
        }
Ejemplo n.º 22
0
        public void SetRowKeyValue()
        {
            // Arrange
            FieldInfo fieldInfo = typeof(EntityWithFields).GetField("String");
            var property = new RowKeyProperty<EntityWithFields>(fieldInfo);
            var tableEntity = new DynamicTableEntity {RowKey = "Key"};
            var entity = new EntityWithFields();

            // Act
            property.SetMemberValue(tableEntity, entity);

            // Assert
            Assert.Equal(tableEntity.RowKey, entity.String);
        }
Ejemplo n.º 23
0
        public void GetETagValue()
        {
            // Arrange
            FieldInfo fieldInfo = typeof(EntityWithFields).GetField("String");
            var property = new ETagProperty<EntityWithFields>(fieldInfo);
            var tableEntity = new DynamicTableEntity();
            var entity = new EntityWithFields{ String = "*"};

            // Act
            property.GetMemberValue(entity, tableEntity);

            // Assert
            Assert.Equal(entity.String, tableEntity.ETag);
        }
Ejemplo n.º 24
0
        public async Task IsPopulated()
        {
            var buildId = new BuildId(42, JobId.ParseName(Guid.NewGuid().ToString()));
            Assert.False(await _populator.IsPopulated(buildId));

            var key = BuildResultEntity.GetExactEntityKey(buildId);
            var entity = new DynamicTableEntity()
            {
                PartitionKey = key.PartitionKey,
                RowKey = key.RowKey
            };
            await _buildResultExactTable.ExecuteAsync(TableOperation.Insert(entity));
            Assert.True(await _populator.IsPopulated(buildId));
        }
        public void GetTimestampValue()
        {
            // Arrange
            FieldInfo fieldInfo = typeof(EntityWithFields).GetField("DateTime");
            var property = new TimestampProperty<EntityWithFields>(fieldInfo);
            var tableEntity = new DynamicTableEntity();
            var entity = new EntityWithFields{ DateTime = new DateTime(1980, 1, 1)};

            // Act
            property.GetMemberValue(entity, tableEntity);

            // Assert
            Assert.Equal(DateTime.MinValue, tableEntity.Timestamp.UtcDateTime);
        }
		public void GetSizeNullTest()
		{
			var tableEntity = new DynamicTableEntity("TestPartitionKey", "TestRowKey");

			tableEntity.AddProperty("suppa", "duppa");

			tableEntity.AddProperty("suppa2", 10);

			tableEntity.Properties.Add("suppa3", new EntityProperty((Int32?) null));

			tableEntity.Properties.Add("suppa4", new EntityProperty((Byte[]) null));

			var size = tableEntity.GetSize();
		}
Ejemplo n.º 27
0
 public static Comment FromDte(DynamicTableEntity dte)
 {
     return new Comment
     {
         Content = dte.Properties["Content"].StringValue,
         Date = dte.Properties["Date"].DateTimeOffsetValue.Value,
         Author = new CommentAuthor
         {
             Name = dte.Properties["AuthorName"].StringValue,
             Email = dte.Properties["AuthorEmail"].StringValue,
             Url = dte.Properties["AuthorUrl"].StringValue,
         },
     };
 }
Ejemplo n.º 28
0
        public Dictionary<string, object> CreatePropertiesFromDynamicTableEntity(DynamicTableEntity entity)
        {
            var result = new Dictionary<string, object>();

            foreach (var entityProperty in entity.Properties)
            {
                object value;
                switch (entityProperty.Value.PropertyType)
                {
                    case EdmType.String:
                        value = entityProperty.Value.StringValue;
                        break;
                    case EdmType.Binary:
                        value = ReadBinaryEntityProperty(entityProperty.Value);
                        break;
                    case EdmType.Boolean:
                        value = entityProperty.Value.BooleanValue;
                        break;
                    case EdmType.DateTime:
                        value = entityProperty.Value.DateTimeOffsetValue;
                        break;
                    case EdmType.Double:
                        value = entityProperty.Value.DoubleValue;
                        break;
                    case EdmType.Guid:
                        value = entityProperty.Value.GuidValue;
                        break;
                    case EdmType.Int32:
                        value = entityProperty.Value.Int32Value;
                        break;
                    case EdmType.Int64:
                        value = entityProperty.Value.Int64Value;
                        break;
                    default:
                        throw new InvalidOperationException("Reading property type '{0}' not supported.".FormatString(entityProperty.Value.PropertyType));
                }

                if (value != null)
                {
                    result.Add(entityProperty.Key, value);
                }
            }

            result.Add(KnownProperties.PartitionKey, entity.PartitionKey);
            result.Add(KnownProperties.RowKey, entity.RowKey);
            result.Add(KnownProperties.ETag, entity.ETag);
            result.Add(KnownProperties.Timestamp, entity.Timestamp);

            return result;
        }
        public void ItShouldInflateArrays()
        {
            // g
            var expected = new object[] {"1", "2", "3"};
            var expectedString = JsonConvert.SerializeObject(expected);

            var ce = new DynamicTableEntity();
            ce.Properties.Add("dragon_ids", new EntityProperty(expectedString));

            // w
            var json = ce.ToJsonObject();

            // t
            json["dragon_ids"].ShouldBeEquivalentTo(expected);
        }
        private DynamicTableEntity AppendInternalProperties(DynamicTableEntity entity)
        {
            if (configuration.InternalFields == AzureTableInternalFields.None)
                return entity;

            if (configuration.InternalFields == AzureTableInternalFields.All)
            {
                entity.Properties[PartitionKeyFieldName] = new EntityProperty(entity.PartitionKey);
                entity.Properties[TimestampFieldName] = new EntityProperty(entity.Timestamp);
            }

            entity.Properties[RowKeyFieldName] = new EntityProperty(entity.RowKey);

            return entity;
        }
        public void SetRegularPropertyValue()
        {
            // Arrange
            FieldInfo fieldInfo = typeof (EntityWithFields).GetField("Guid");
            var property = new RegularProperty<EntityWithFields>(fieldInfo);
            var tableEntity = new DynamicTableEntity();
            tableEntity.Properties.Add("Guid", new EntityProperty(Guid.NewGuid()));
            var entity = new EntityWithFields();

            // Act
            property.SetMemberValue(tableEntity, entity);

            // Assert
            Assert.Equal(tableEntity.Properties["Guid"].GuidValue, entity.Guid);
        }
Ejemplo n.º 32
0
        /// <summary>
        /// Checks existence of the queue.
        /// </summary>
        /// <param name="primaryOnly">If <c>true</c>, the command will be executed against the primary location.</param>
        /// <param name="requestOptions">A <see cref="TableRequestOptions"/> object that specifies execution options, such as retry policy and timeout settings, for the operation.</param>
        /// <param name="operationContext">An <see cref="OperationContext"/> object for tracking the current operation.</param>
        /// <returns><c>true</c> if the queue exists.</returns>
        private IAsyncOperation <bool> ExistsAsync(bool primaryOnly, TableRequestOptions requestOptions, OperationContext operationContext)
        {
            requestOptions   = TableRequestOptions.ApplyDefaults(requestOptions, this.ServiceClient);
            operationContext = operationContext ?? new OperationContext();

            DynamicTableEntity tblEntity = new DynamicTableEntity();

            tblEntity.Properties.Add(TableConstants.TableName, new EntityProperty(this.Name));
            TableOperation operation = new TableOperation(tblEntity, TableOperationType.Retrieve);

            operation.IsTableEntity         = true;
            operation.IsPrimaryOnlyRetrieve = primaryOnly;

            return(AsyncInfo.Run(async(cancellationToken) =>
            {
                TableResult res = await this.ServiceClient.ExecuteAsync(TableConstants.TableServiceTablesName, operation, requestOptions, operationContext).AsTask(cancellationToken);

                // Only other option is not found, other status codes will throw prior to this.
                return res.HttpStatusCode == (int)HttpStatusCode.OK;
            }));
        }
        public void TableTestTableQueryCancellation()
        {
            CloudTableClient    tableClient = GenerateCloudTableClient();
            TableBatchOperation batch       = new TableBatchOperation();

            for (int m = 0; m < 100; m++)
            {
                // Insert Entity
                DynamicTableEntity insertEntity = new DynamicTableEntity("insert test", m.ToString());
                insertEntity.Properties.Add("prop" + m.ToString(), new EntityProperty(new byte[30 * 1024]));
                batch.Insert(insertEntity);
            }

            currentTable.ExecuteBatch(batch);
            TableQuery query = new TableQuery().Where(TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, "insert test"));

            TestHelper.ExecuteAPMMethodWithCancellation(4000,
                                                        new[] { DelayBehaviors.DelayAllRequestsIf(4000 * 3, XStoreSelectors.TableTraffic().IfHostNameContains(tableClient.Credentials.AccountName)) },
                                                        (options, opContext, callback, state) => currentTable.BeginExecuteQuerySegmented(query, null, (TableRequestOptions)options, opContext, callback, state),
                                                        (res) => currentTable.EndExecuteQuerySegmented(res));
        }
Ejemplo n.º 34
0
        public static void MyClassInitialize(TestContext testContext)
        {
            CloudTableClient tableClient = GenerateCloudTableClient();

            currentTable = tableClient.GetTableReference(GenerateRandomTableName());
            currentTable.CreateIfNotExistsAsync().AsTask().Wait();

            for (int i = 0; i < 15; i++)
            {
                TableBatchOperation batch = new TableBatchOperation();

                for (int j = 0; j < 100; j++)
                {
                    DynamicTableEntity ent = GenerateRandomEntity("tables_batch_" + i.ToString());
                    ent.RowKey = string.Format("{0:0000}", j);
                    batch.Insert(ent);
                }

                currentTable.ExecuteBatchAsync(batch).AsTask().Wait();
            }
        }
Ejemplo n.º 35
0
        public void TableIngressEgressQuery()
        {
            CloudTableClient    tableClient = GenerateCloudTableClient();
            TableBatchOperation batch       = new TableBatchOperation();

            for (int m = 0; m < 100; m++)
            {
                // Insert Entity
                DynamicTableEntity insertEntity = new DynamicTableEntity("insert test", m.ToString());
                insertEntity.Properties.Add("prop" + m.ToString(), new EntityProperty(new byte[30 * 1024]));
                batch.Insert(insertEntity, true);
            }

            currentTable.ExecuteBatch(batch);
            TableQuery query = new TableQuery().Where(TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, "insert test"));

            // APM
            TestHelper.ValidateIngressEgress(Selectors.IfUrlContains(currentTable.Uri.ToString()), () =>
            {
                OperationContext opContext = new OperationContext();
                currentTable.EndExecuteQuerySegmented(currentTable.BeginExecuteQuerySegmented(query, null, new TableRequestOptions()
                {
                    RetryPolicy = new RetryPolicies.NoRetry()
                }, opContext, null, null));
                return(opContext.LastResult);
            });

            // SYNC
            TestHelper.ValidateIngressEgress(Selectors.IfUrlContains(currentTable.Uri.ToString()), () =>
            {
                OperationContext opContext = new OperationContext();
                currentTable.ExecuteQuerySegmented(query, null, new TableRequestOptions()
                {
                    RetryPolicy = new RetryPolicies.NoRetry()
                }, opContext);
                return(opContext.LastResult);
            });
        }
        public async Task TableRetrieveWithIgnoreAttributeWriteAsync()
        {
            string pk = Guid.NewGuid().ToString();
            string rk = Guid.NewGuid().ToString();

            IgnoreEntity sendEnt = new IgnoreEntity(pk, rk);

            sendEnt.Bool               = true;
            sendEnt.BoolN              = true;
            sendEnt.BoolNull           = null;
            sendEnt.BoolPrimitive      = true;
            sendEnt.BoolPrimitiveN     = true;
            sendEnt.BoolPrimitiveNull  = true;
            sendEnt.DateTime           = DateTime.UtcNow.AddMinutes(1);
            sendEnt.DateTimeN          = DateTime.UtcNow.AddMinutes(1);
            sendEnt.DateTimeNull       = null;
            sendEnt.DateTimeOffset     = DateTimeOffset.Now.AddMinutes(1);
            sendEnt.DateTimeOffsetN    = DateTimeOffset.Now.AddMinutes(1);
            sendEnt.DateTimeOffsetNull = DateTimeOffset.Now.AddMinutes(1);

            await currentTable.ExecuteAsync(TableOperation.Insert(sendEnt));

            TableResult result = await currentTable.ExecuteAsync(TableOperation.Retrieve(sendEnt.PartitionKey, sendEnt.RowKey));

            DynamicTableEntity retrievedEntity = result.Result as DynamicTableEntity;

            Assert.IsFalse(retrievedEntity.Properties.ContainsKey("BoolPrimitiveNull"));
            Assert.IsFalse(retrievedEntity.Properties.ContainsKey("Bool"));
            Assert.AreEqual(sendEnt.BoolPrimitive, retrievedEntity.Properties["BoolPrimitive"].BooleanValue);
            Assert.AreEqual(sendEnt.BoolPrimitiveN, retrievedEntity.Properties["BoolPrimitiveN"].BooleanValue);
            Assert.AreEqual(sendEnt.BoolN, retrievedEntity.Properties["BoolN"].BooleanValue);

            Assert.IsFalse(retrievedEntity.Properties.ContainsKey("DateTimeOffset"));
            Assert.IsFalse(retrievedEntity.Properties.ContainsKey("DateTimeOffsetNull"));
            Assert.AreEqual(sendEnt.DateTimeOffsetN, retrievedEntity.Properties["DateTimeOffsetN"].DateTimeOffsetValue);
            Assert.AreEqual(sendEnt.DateTime, retrievedEntity.Properties["DateTime"].DateTime);
            Assert.AreEqual(sendEnt.DateTimeN, retrievedEntity.Properties["DateTimeN"].DateTime);
        }
Ejemplo n.º 37
0
        public void TableOperationConflictDoesNotRetry()
        {
            CloudTableClient   tableClient  = GenerateCloudTableClient();
            DynamicTableEntity insertEntity = new DynamicTableEntity("insert test", "foo");

            currentTable.Execute(TableOperation.Insert(insertEntity));

            OperationContext opContext = new OperationContext();

            try
            {
                currentTable.Execute(TableOperation.Insert(insertEntity), null, opContext);
                Assert.Fail();
            }
            catch (StorageException)
            {
                TestHelper.AssertNAttempts(opContext, 1);
            }
            catch (Exception)
            {
                Assert.Fail();
            }
        }
Ejemplo n.º 38
0
        private async Task DoTableQueryEmptyValueAsync(TablePayloadFormat format)
        {
            tableClient.DefaultRequestOptions.PayloadFormat = format;
            CloudTable table = tableClient.GetTableReference(GenerateRandomTableName());
            await table.CreateAsync();

            // Setup
            string pk = Guid.NewGuid().ToString();

            DynamicTableEntity dynEnt = new DynamicTableEntity(pk, "rowkey");

            dynEnt.Properties.Add("A", new EntityProperty(string.Empty));
            await table.ExecuteAsync(TableOperation.Insert(dynEnt));

            // 1. Filter on String
            List <DynamicTableEntity> results = (await table.ExecuteQuerySegmentedAsync(new TableQuery().Where(TableQuery.GenerateFilterCondition("A", QueryComparisons.Equal, string.Empty)), null)).ToList();

            Assert.AreEqual(1, results.Count);

            List <BaseEntity> pocoresults = (await table.ExecuteQuerySegmentedAsync(new TableQuery <BaseEntity>().Where(TableQuery.GenerateFilterCondition("A", QueryComparisons.Equal, string.Empty)), null)).ToList();

            Assert.AreEqual(1, pocoresults.Count);
        }
Ejemplo n.º 39
0
        public void TableOperationNoRetry()
        {
            CloudTableClient tableClient = GenerateCloudTableClient();

            tableClient.DefaultRequestOptions.RetryPolicy = new NoRetry();
            CloudTable currentTable = tableClient.GetTableReference("noretrytable");

            currentTable.CreateIfNotExists();
            DynamicTableEntity insertEntity = new DynamicTableEntity("insert test", "foo");

            currentTable.Execute(TableOperation.Insert(insertEntity));
            TableQuery query = new TableQuery().Where(TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, "insert test"));

            try
            {
                TestHelper.ExecuteMethodWithRetry(
                    1,
                    new[] {
                    //Insert upstream network delay to prevent upload to server @ 1000ms / kb
                    PerformanceBehaviors.InsertDownstreamNetworkDelay(10000,
                                                                      AzureStorageSelectors.TableTraffic().IfHostNameContains(tableClient.Credentials.AccountName).Alternating(true)),
                    // After 100 ms return throttle message
                    DelayedActionBehaviors.ExecuteAfter(Actions.ThrottleTableRequest,
                                                        100,
                                                        AzureStorageSelectors.TableTraffic().IfHostNameContains(tableClient.Credentials.AccountName).Alternating(true))
                },
                    (options, opContext) => currentTable.ExecuteQuery(query, (TableRequestOptions)options, opContext).ToList());
            }
            catch (StorageException ex)
            {
                Assert.IsTrue(ex.RequestInformation.HttpStatusCode == 503);
            }
            finally
            {
                currentTable.DeleteIfExists();
            }
        }
Ejemplo n.º 40
0
        public void TableQueryWithRetryAPM()
        {
            CloudTableClient    tableClient = GenerateCloudTableClient();
            TableBatchOperation batch       = new TableBatchOperation();

            for (int m = 0; m < 1500; m++)
            {
                // Insert Entity
                DynamicTableEntity insertEntity = new DynamicTableEntity("insert test", m.ToString());
                insertEntity.Properties.Add("prop" + m.ToString(), new EntityProperty(new byte[1 * 1024]));
                batch.Insert(insertEntity);

                if ((m + 1) % 100 == 0)
                {
                    currentTable.ExecuteBatch(batch);
                    batch = new TableBatchOperation();
                }
            }

            TableQuery query = new TableQuery().Where(TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, "insert test"));


            TestHelper.ExecuteAPMMethodWithRetry(
                2, // 1 failure, one success
                new[] {
                //Insert upstream network delay to prevent upload to server @ 1000ms / kb
                PerformanceBehaviors.InsertDownstreamNetworkDelay(10000,
                                                                  XStoreSelectors.TableTraffic().IfHostNameContains(tableClient.Credentials.AccountName).Alternating(true)),
                // After 100 ms return throttle message
                DelayedActionBehaviors.ExecuteAfter(Actions.ThrottleTableRequest,
                                                    100,
                                                    XStoreSelectors.TableTraffic().IfHostNameContains(tableClient.Credentials.AccountName).Alternating(true))
            },
                (options, opContext, callback, state) => currentTable.BeginExecuteQuerySegmented(query, null, (TableRequestOptions)options, opContext, callback, state),
                (res) => currentTable.EndExecuteQuerySegmented(res));
        }
Ejemplo n.º 41
0
        internal KeyRotationEntity(DynamicTableEntity dte)
        {
            CommonUtility.AssertNotNull("properties", dte.Properties);

            // Store the information about this entity.  Make a copy of the properties list, in case the caller decides to reuse the list.
            this.PartitionKey = dte.PartitionKey;
            this.RowKey       = dte.RowKey;
            this.Timestamp    = dte.Timestamp;
            this.ETag         = dte.ETag;
            if (!dte.Properties.ContainsKey(Constants.EncryptionConstants.TableEncryptionKeyDetails))
            {
                // This should only be possible if RequireEncryption is true, otherwise the entity would have been filtered out in the query.
                throw new InvalidOperationException(SR.KeyRotationNoEncryptionMetadata);
            }
            else
            {
                this.encryptionMetadataJson = dte.Properties[Constants.EncryptionConstants.TableEncryptionKeyDetails].StringValue;
            }
            Dictionary <string, EntityProperty> properties = new Dictionary <string, EntityProperty>(dte.Properties);

            properties.Remove(Constants.EncryptionConstants.TableEncryptionKeyDetails);
            properties.Remove(Constants.EncryptionConstants.TableEncryptionPropertyDetails);
            this.Properties = new System.Collections.ObjectModel.ReadOnlyDictionary <string, EntityProperty>(properties);
        }
        public async Task TableOperationReplaceFailAsync()
        {
            CloudTableClient tableClient = GenerateCloudTableClient();

            // Insert Entity
            DynamicTableEntity baseEntity = new DynamicTableEntity("merge test", "foo");

            baseEntity.Properties.Add("prop1", new EntityProperty("value1"));
            await currentTable.ExecuteAsync(TableOperation.Insert(baseEntity));

            string staleEtag = baseEntity.ETag;

            // update entity to rev etag
            baseEntity.Properties["prop1"].StringValue = "updated value";
            await currentTable.ExecuteAsync(TableOperation.Replace(baseEntity));

            OperationContext opContext = new OperationContext();

            try
            {
                // Attempt a merge with stale etag
                DynamicTableEntity replaceEntity = new DynamicTableEntity(baseEntity.PartitionKey, baseEntity.RowKey)
                {
                    ETag = staleEtag
                };
                replaceEntity.Properties.Add("prop2", new EntityProperty("value2"));
                await currentTable.ExecuteAsync(TableOperation.Replace(replaceEntity), null, opContext);

                Assert.Fail();
            }
            catch (Exception)
            {
                TestHelper.ValidateResponse(opContext,
                                            1,
                                            (int)HttpStatusCode.PreconditionFailed,
                                            new string[] { "UpdateConditionNotSatisfied", "ConditionNotMet" },
                                            new string[] { "The update condition specified in the request was not satisfied.", "The condition specified using HTTP conditional header(s) is not met." });
            }

            // Delete Entity
            await currentTable.ExecuteAsync(TableOperation.Delete(baseEntity));

            opContext = new OperationContext();

            // try replacing with deleted entity
            try
            {
                DynamicTableEntity replaceEntity = new DynamicTableEntity(baseEntity.PartitionKey, baseEntity.RowKey)
                {
                    ETag = baseEntity.ETag
                };
                replaceEntity.Properties.Add("prop2", new EntityProperty("value2"));
                await currentTable.ExecuteAsync(TableOperation.Replace(replaceEntity), null, opContext);

                Assert.Fail();
            }
            catch (Exception)
            {
                TestHelper.ValidateResponse(opContext, 1, (int)HttpStatusCode.NotFound, new string[] { "ResourceNotFound" }, "The specified resource does not exist.");
            }
        }
        public async Task TableOperationsWithEmptyKeysAsync()
        {
            CloudTableClient tableClient = GenerateCloudTableClient();

            // Insert Entity
            DynamicTableEntity ent = new DynamicTableEntity()
            {
                PartitionKey = "", RowKey = ""
            };

            ent.Properties.Add("foo2", new EntityProperty("bar2"));
            ent.Properties.Add("foo", new EntityProperty("bar"));
            await currentTable.ExecuteAsync(TableOperation.Insert(ent));

            // Retrieve Entity
            TableResult result = await currentTable.ExecuteAsync(TableOperation.Retrieve(ent.PartitionKey, ent.RowKey));

            DynamicTableEntity retrievedEntity = result.Result as DynamicTableEntity;

            Assert.IsNotNull(retrievedEntity);
            Assert.AreEqual(ent.PartitionKey, retrievedEntity.PartitionKey);
            Assert.AreEqual(ent.RowKey, retrievedEntity.RowKey);
            Assert.AreEqual(ent.Properties.Count, retrievedEntity.Properties.Count);
            Assert.AreEqual(ent.Properties["foo"].StringValue, retrievedEntity.Properties["foo"].StringValue);
            Assert.AreEqual(ent.Properties["foo"], retrievedEntity.Properties["foo"]);
            Assert.AreEqual(ent.Properties["foo2"].StringValue, retrievedEntity.Properties["foo2"].StringValue);
            Assert.AreEqual(ent.Properties["foo2"], retrievedEntity.Properties["foo2"]);

            // InsertOrMerge
            DynamicTableEntity insertOrMergeEntity = new DynamicTableEntity(ent.PartitionKey, ent.RowKey);

            insertOrMergeEntity.Properties.Add("foo3", new EntityProperty("value"));
            await currentTable.ExecuteAsync(TableOperation.InsertOrMerge(insertOrMergeEntity));

            result = await currentTable.ExecuteAsync(TableOperation.Retrieve(ent.PartitionKey, ent.RowKey));

            retrievedEntity = result.Result as DynamicTableEntity;
            Assert.IsNotNull(retrievedEntity);
            Assert.AreEqual(insertOrMergeEntity.Properties["foo3"], retrievedEntity.Properties["foo3"]);

            // InsertOrReplace
            DynamicTableEntity insertOrReplaceEntity = new DynamicTableEntity(ent.PartitionKey, ent.RowKey);

            insertOrReplaceEntity.Properties.Add("prop2", new EntityProperty("otherValue"));
            await currentTable.ExecuteAsync(TableOperation.InsertOrReplace(insertOrReplaceEntity));

            result = await currentTable.ExecuteAsync(TableOperation.Retrieve(ent.PartitionKey, ent.RowKey));

            retrievedEntity = result.Result as DynamicTableEntity;
            Assert.IsNotNull(retrievedEntity);
            Assert.AreEqual(1, retrievedEntity.Properties.Count);
            Assert.AreEqual(insertOrReplaceEntity.Properties["prop2"], retrievedEntity.Properties["prop2"]);

            // Merge
            DynamicTableEntity mergeEntity = new DynamicTableEntity(retrievedEntity.PartitionKey, retrievedEntity.RowKey)
            {
                ETag = retrievedEntity.ETag
            };

            mergeEntity.Properties.Add("mergeProp", new EntityProperty("merged"));
            await currentTable.ExecuteAsync(TableOperation.Merge(mergeEntity));

            // Retrieve Entity & Verify Contents
            result = await currentTable.ExecuteAsync(TableOperation.Retrieve(ent.PartitionKey, ent.RowKey));

            retrievedEntity = result.Result as DynamicTableEntity;

            Assert.IsNotNull(retrievedEntity);
            Assert.AreEqual(mergeEntity.Properties["mergeProp"], retrievedEntity.Properties["mergeProp"]);

            // Replace
            DynamicTableEntity replaceEntity = new DynamicTableEntity(ent.PartitionKey, ent.RowKey)
            {
                ETag = retrievedEntity.ETag
            };

            replaceEntity.Properties.Add("replaceProp", new EntityProperty("replace"));
            await currentTable.ExecuteAsync(TableOperation.Replace(replaceEntity));

            // Retrieve Entity & Verify Contents
            result = await currentTable.ExecuteAsync(TableOperation.Retrieve(ent.PartitionKey, ent.RowKey));

            retrievedEntity = result.Result as DynamicTableEntity;
            Assert.IsNotNull(retrievedEntity);
            Assert.AreEqual(replaceEntity.Properties.Count, retrievedEntity.Properties.Count);
            Assert.AreEqual(replaceEntity.Properties["replaceProp"], retrievedEntity.Properties["replaceProp"]);

            // Delete Entity
            await currentTable.ExecuteAsync(TableOperation.Delete(retrievedEntity));

            // Retrieve Entity
            TableResult result2 = await currentTable.ExecuteAsync(TableOperation.Retrieve(ent.PartitionKey, ent.RowKey));

            Assert.IsNull(result2.Result);
        }
 Task <T> StripDTOAsync(Microsoft.WindowsAzure.Storage.Table.DynamicTableEntity a)
 {
     return(Task.Run(() => StripDTO(a)));
 }
Ejemplo n.º 45
0
        public async Task TableRegionalQueryOnSupportedTypesAsync()
        {
            string currentPrimaryLanguage = ApplicationLanguages.PrimaryLanguageOverride;

            ApplicationLanguages.PrimaryLanguageOverride = "tr";

            CloudTableClient client = GenerateCloudTableClient();

            CloudTable table = client.GetTableReference(GenerateRandomTableName());
            await table.CreateAsync();

            try
            {
                // Setup
                TableBatchOperation batch    = new TableBatchOperation();
                string             pk        = Guid.NewGuid().ToString();
                DynamicTableEntity middleRef = null;
                for (int m = 0; m < 100; m++)
                {
                    ComplexEntity complexEntity = new ComplexEntity();
                    complexEntity.String           = string.Format("{0:0000}", m);
                    complexEntity.Binary           = new byte[] { 0x01, 0x02, (byte)m };
                    complexEntity.BinaryPrimitive  = new byte[] { 0x01, 0x02, (byte)m };
                    complexEntity.Bool             = m % 2 == 0 ? true : false;
                    complexEntity.BoolPrimitive    = m % 2 == 0 ? true : false;
                    complexEntity.Double           = m + ((double)m / 100);
                    complexEntity.DoublePrimitive  = m + ((double)m / 100);
                    complexEntity.Int32            = m;
                    complexEntity.IntegerPrimitive = m;
                    complexEntity.Int64            = (long)int.MaxValue + m;
                    complexEntity.LongPrimitive    = (long)int.MaxValue + m;
                    complexEntity.Guid             = Guid.NewGuid();

                    DynamicTableEntity dynEnt = new DynamicTableEntity(pk, string.Format("{0:0000}", m));
                    dynEnt.Properties = complexEntity.WriteEntity(null);
                    batch.Insert(dynEnt);

                    if (m == 50)
                    {
                        middleRef = dynEnt;
                    }

                    // Add delay to make times unique
                    await Task.Delay(100);
                }

                await table.ExecuteBatchAsync(batch);

                // 1. Filter on String
                ExecuteQueryAndAssertResults(table,
                                             TableQuery.GenerateFilterCondition("String", QueryComparisons.GreaterThanOrEqual, "0050"), 50);

                // 2. Filter on Guid
                ExecuteQueryAndAssertResults(table,
                                             TableQuery.GenerateFilterConditionForGuid("Guid", QueryComparisons.Equal, middleRef.Properties["Guid"].GuidValue.Value), 1);

                // 3. Filter on Long
                ExecuteQueryAndAssertResults(table,
                                             TableQuery.GenerateFilterConditionForLong("Int64", QueryComparisons.GreaterThanOrEqual,
                                                                                       middleRef.Properties["LongPrimitive"].Int64Value.Value), 50);

                ExecuteQueryAndAssertResults(table, TableQuery.GenerateFilterConditionForLong("LongPrimitive",
                                                                                              QueryComparisons.GreaterThanOrEqual, middleRef.Properties["LongPrimitive"].Int64Value.Value), 50);

                // 4. Filter on Double
                ExecuteQueryAndAssertResults(table,
                                             TableQuery.GenerateFilterConditionForDouble("Double", QueryComparisons.GreaterThanOrEqual,
                                                                                         middleRef.Properties["Double"].DoubleValue.Value), 50);

                ExecuteQueryAndAssertResults(table, TableQuery.GenerateFilterConditionForDouble("DoublePrimitive",
                                                                                                QueryComparisons.GreaterThanOrEqual, middleRef.Properties["DoublePrimitive"].DoubleValue.Value), 50);

                // 5. Filter on Integer
                ExecuteQueryAndAssertResults(table,
                                             TableQuery.GenerateFilterConditionForInt("Int32", QueryComparisons.GreaterThanOrEqual,
                                                                                      middleRef.Properties["Int32"].Int32Value.Value), 50);

                ExecuteQueryAndAssertResults(table, TableQuery.GenerateFilterConditionForInt("IntegerPrimitive",
                                                                                             QueryComparisons.GreaterThanOrEqual, middleRef.Properties["IntegerPrimitive"].Int32Value.Value), 50);

                // 6. Filter on Date
                ExecuteQueryAndAssertResults(table,
                                             TableQuery.GenerateFilterConditionForDate("DateTimeOffset", QueryComparisons.GreaterThanOrEqual,
                                                                                       middleRef.Properties["DateTimeOffset"].DateTimeOffsetValue.Value), 50);

                // 7. Filter on Boolean
                ExecuteQueryAndAssertResults(table,
                                             TableQuery.GenerateFilterConditionForBool("Bool", QueryComparisons.Equal, middleRef.Properties["Bool"].BooleanValue.Value), 50);

                ExecuteQueryAndAssertResults(table,
                                             TableQuery.GenerateFilterConditionForBool("BoolPrimitive", QueryComparisons.Equal, middleRef.Properties["BoolPrimitive"].BooleanValue.Value),
                                             50);

                // 8. Filter on Binary
                ExecuteQueryAndAssertResults(table,
                                             TableQuery.GenerateFilterConditionForBinary("Binary", QueryComparisons.Equal, middleRef.Properties["Binary"].BinaryValue), 1);

                ExecuteQueryAndAssertResults(table,
                                             TableQuery.GenerateFilterConditionForBinary("BinaryPrimitive", QueryComparisons.Equal,
                                                                                         middleRef.Properties["BinaryPrimitive"].BinaryValue), 1);

                // 9. Filter on Binary GTE
                ExecuteQueryAndAssertResults(table,
                                             TableQuery.GenerateFilterConditionForBinary("Binary", QueryComparisons.GreaterThanOrEqual,
                                                                                         middleRef.Properties["Binary"].BinaryValue), 50);

                ExecuteQueryAndAssertResults(table, TableQuery.GenerateFilterConditionForBinary("BinaryPrimitive",
                                                                                                QueryComparisons.GreaterThanOrEqual, middleRef.Properties["BinaryPrimitive"].BinaryValue), 50);

                // 10. Complex Filter on Binary GTE
                ExecuteQueryAndAssertResults(table, TableQuery.CombineFilters(
                                                 TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal,
                                                                                    middleRef.PartitionKey),
                                                 TableOperators.And,
                                                 TableQuery.GenerateFilterConditionForBinary("Binary", QueryComparisons.GreaterThanOrEqual,
                                                                                             middleRef.Properties["Binary"].BinaryValue)), 50);

                ExecuteQueryAndAssertResults(table, TableQuery.GenerateFilterConditionForBinary("BinaryPrimitive",
                                                                                                QueryComparisons.GreaterThanOrEqual, middleRef.Properties["BinaryPrimitive"].BinaryValue), 50);
            }
            finally
            {
                ApplicationLanguages.PrimaryLanguageOverride = currentPrimaryLanguage;
                table.DeleteIfExistsAsync().AsTask().Wait();
            }
        }
        private void DoEscapeTest(string data, bool useBatch, bool includeKey)
        {
            DynamicTableEntity ent = new DynamicTableEntity(includeKey ? "temp" + data : "temp", Guid.NewGuid().ToString());

            ent.Properties.Add("foo", new EntityProperty(data));

            // Insert
            if (useBatch)
            {
                TableBatchOperation batch = new TableBatchOperation();
                batch.Insert(ent);
                currentTable.ExecuteBatch(batch);
            }
            else
            {
                currentTable.Execute(TableOperation.Insert(ent));
            }

            // Retrieve
            TableResult res = null;

            if (useBatch)
            {
                TableBatchOperation batch = new TableBatchOperation();
                batch.Retrieve(ent.PartitionKey, ent.RowKey);
                res = (currentTable.ExecuteBatch(batch))[0];
            }
            else
            {
                res = currentTable.Execute(TableOperation.Retrieve(ent.PartitionKey, ent.RowKey));
            }

            // Check equality
            DynamicTableEntity retrievedEntity = res.Result as DynamicTableEntity;

            Assert.IsNotNull(retrievedEntity);
            Assert.AreEqual(ent.PartitionKey, retrievedEntity.PartitionKey);
            Assert.AreEqual(ent.RowKey, retrievedEntity.RowKey);
            Assert.AreEqual(ent.ETag, retrievedEntity.ETag);
            Assert.AreEqual(ent.Properties.Count, retrievedEntity.Properties.Count);
            Assert.AreEqual(ent.Properties["foo"], retrievedEntity.Properties["foo"]);

            // Query using data filter
            TableQuery query = new TableQuery();

            query.Where(string.Format(
                            "(PartitionKey eq \'{0}\') and (RowKey eq \'{1}\') and (foo eq \'{2}\')",
                            ent.PartitionKey,
                            ent.RowKey,
                            data.Replace("\'", "\'\'")));

            retrievedEntity = currentTable.ExecuteQuery(query).Single();

            Assert.IsNotNull(retrievedEntity);
            Assert.AreEqual(ent.PartitionKey, retrievedEntity.PartitionKey);
            Assert.AreEqual(ent.RowKey, retrievedEntity.RowKey);
            Assert.AreEqual(ent.ETag, retrievedEntity.ETag);
            Assert.AreEqual(ent.Properties.Count, retrievedEntity.Properties.Count);
            Assert.AreEqual(ent.Properties["foo"], retrievedEntity.Properties["foo"]);

            // Merge
            ent.Properties.Add("foo2", new EntityProperty("bar2"));

            if (useBatch)
            {
                TableBatchOperation batch = new TableBatchOperation();
                batch.Merge(ent);
                currentTable.ExecuteBatch(batch);
            }
            else
            {
                currentTable.Execute(TableOperation.Merge(ent));
            }

            // Retrieve
            if (useBatch)
            {
                TableBatchOperation batch = new TableBatchOperation();
                batch.Retrieve(ent.PartitionKey, ent.RowKey);
                res = (currentTable.ExecuteBatch(batch))[0];
            }
            else
            {
                res = currentTable.Execute(TableOperation.Retrieve(ent.PartitionKey, ent.RowKey));
            }

            retrievedEntity = res.Result as DynamicTableEntity;
            Assert.AreEqual(ent.PartitionKey, retrievedEntity.PartitionKey);
            Assert.AreEqual(ent.RowKey, retrievedEntity.RowKey);
            Assert.AreEqual(ent.ETag, retrievedEntity.ETag);
            Assert.AreEqual(ent.Properties.Count, retrievedEntity.Properties.Count);
            Assert.AreEqual(ent.Properties["foo"], retrievedEntity.Properties["foo"]);

            // Replace
            ent.Properties.Remove("foo2");
            ent.Properties.Add("foo3", new EntityProperty("bar3"));

            if (useBatch)
            {
                TableBatchOperation batch = new TableBatchOperation();
                batch.Replace(ent);
                currentTable.ExecuteBatch(batch);
            }
            else
            {
                currentTable.Execute(TableOperation.Replace(ent));
            }

            // Retrieve
            if (useBatch)
            {
                TableBatchOperation batch = new TableBatchOperation();
                batch.Retrieve(ent.PartitionKey, ent.RowKey);
                res = (currentTable.ExecuteBatch(batch))[0];
            }
            else
            {
                res = currentTable.Execute(TableOperation.Retrieve(ent.PartitionKey, ent.RowKey));
            }

            retrievedEntity = res.Result as DynamicTableEntity;
            Assert.AreEqual(ent.PartitionKey, retrievedEntity.PartitionKey);
            Assert.AreEqual(ent.RowKey, retrievedEntity.RowKey);
            Assert.AreEqual(ent.ETag, retrievedEntity.ETag);
            Assert.AreEqual(ent.Properties.Count, retrievedEntity.Properties.Count);
            Assert.AreEqual(ent.Properties["foo"], retrievedEntity.Properties["foo"]);
        }
Ejemplo n.º 47
0
        private async Task DoEscapeTestAsync(string data, bool useBatch, bool includeKey)
        {
            DynamicTableEntity ent = new DynamicTableEntity(includeKey ? "temp" + data : "temp", Guid.NewGuid().ToString());

            ent.Properties.Add("foo", new EntityProperty(data));

            // Insert
            if (useBatch)
            {
                TableBatchOperation batch = new TableBatchOperation();
                batch.Insert(ent);
                await currentTable.ExecuteBatchAsync(batch);
            }
            else
            {
                await currentTable.ExecuteAsync(TableOperation.Insert(ent));
            }

            // Retrieve
            TableResult res = null;

            if (useBatch)
            {
                TableBatchOperation batch = new TableBatchOperation();
                batch.Retrieve(ent.PartitionKey, ent.RowKey);
                res = (await currentTable.ExecuteBatchAsync(batch))[0];
            }
            else
            {
                res = await currentTable.ExecuteAsync(TableOperation.Retrieve(ent.PartitionKey, ent.RowKey));
            }

            // Check equality
            DynamicTableEntity retrievedEntity = res.Result as DynamicTableEntity;

            Assert.AreEqual(ent.PartitionKey, retrievedEntity.PartitionKey);
            Assert.AreEqual(ent.RowKey, retrievedEntity.RowKey);
            Assert.AreEqual(ent.ETag, retrievedEntity.ETag);
            Assert.AreEqual(ent.Properties.Count, retrievedEntity.Properties.Count);
            Assert.AreEqual(ent.Properties["foo"], retrievedEntity.Properties["foo"]);

            // Merge
            ent.Properties.Add("foo2", new EntityProperty("bar2"));

            if (useBatch)
            {
                TableBatchOperation batch = new TableBatchOperation();
                batch.Merge(ent);
                await currentTable.ExecuteBatchAsync(batch);
            }
            else
            {
                await currentTable.ExecuteAsync(TableOperation.Merge(ent));
            }

            // Retrieve
            if (useBatch)
            {
                TableBatchOperation batch = new TableBatchOperation();
                batch.Retrieve(ent.PartitionKey, ent.RowKey);
                res = (await currentTable.ExecuteBatchAsync(batch))[0];
            }
            else
            {
                res = await currentTable.ExecuteAsync(TableOperation.Retrieve(ent.PartitionKey, ent.RowKey));
            }

            retrievedEntity = res.Result as DynamicTableEntity;
            Assert.AreEqual(ent.PartitionKey, retrievedEntity.PartitionKey);
            Assert.AreEqual(ent.RowKey, retrievedEntity.RowKey);
            Assert.AreEqual(ent.ETag, retrievedEntity.ETag);
            Assert.AreEqual(ent.Properties.Count, retrievedEntity.Properties.Count);
            Assert.AreEqual(ent.Properties["foo"], retrievedEntity.Properties["foo"]);

            // Replace
            ent.Properties.Remove("foo2");
            ent.Properties.Add("foo3", new EntityProperty("bar3"));

            if (useBatch)
            {
                TableBatchOperation batch = new TableBatchOperation();
                batch.Replace(ent);
                await currentTable.ExecuteBatchAsync(batch);
            }
            else
            {
                await currentTable.ExecuteAsync(TableOperation.Replace(ent));
            }

            // Retrieve
            if (useBatch)
            {
                TableBatchOperation batch = new TableBatchOperation();
                batch.Retrieve(ent.PartitionKey, ent.RowKey);
                res = (await currentTable.ExecuteBatchAsync(batch))[0];
            }
            else
            {
                res = await currentTable.ExecuteAsync(TableOperation.Retrieve(ent.PartitionKey, ent.RowKey));
            }

            retrievedEntity = res.Result as DynamicTableEntity;
            Assert.AreEqual(ent.PartitionKey, retrievedEntity.PartitionKey);
            Assert.AreEqual(ent.RowKey, retrievedEntity.RowKey);
            Assert.AreEqual(ent.ETag, retrievedEntity.ETag);
            Assert.AreEqual(ent.Properties.Count, retrievedEntity.Properties.Count);
            Assert.AreEqual(ent.Properties["foo"], retrievedEntity.Properties["foo"]);
        }