Created
January 10, 2021 07:06
-
-
Save joelverhagen/2bf45db3f5160bbfaf1056319b2172d0 to your computer and use it in GitHub Desktop.
Entity limits
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Size: 1048488 (1048576 - 1048488 = 88)... success! | |
Size: 1048489 (1048576 - 1048489 = 87)... Bad Request |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Threading.Tasks; | |
using Microsoft.WindowsAzure.Storage; | |
using Microsoft.WindowsAzure.Storage.Table; | |
namespace EntityLimits | |
{ | |
class Program | |
{ | |
static async Task Main() | |
{ | |
var connectionString = "DefaultEndpointsProtocol=https;AccountName=account;AccountKey=key;EndpointSuffix=core.windows.net"; | |
var account = CloudStorageAccount.Parse(connectionString); | |
var client = account.CreateCloudTableClient(); | |
var table = client.GetTableReference("test"); | |
await TryAsync(table, offset: 0); | |
await TryAsync(table, offset: -1); | |
} | |
private static async Task TryAsync(CloudTable table, int offset) | |
{ | |
const int MaxEntitySize = 1024 * 1024; // 1 MB | |
const int MaxBinarySize = 64 * 1024; // 64 KiB | |
var dynamicEntity = new DynamicTableEntity | |
{ | |
PartitionKey = "D1", | |
RowKey = "R1", | |
Properties = | |
{ | |
{ "A", new EntityProperty(new byte[MaxBinarySize]) }, | |
{ "B", new EntityProperty(new byte[MaxBinarySize]) }, | |
{ "C", new EntityProperty(new byte[MaxBinarySize]) }, | |
{ "D", new EntityProperty(new byte[MaxBinarySize]) }, | |
{ "E", new EntityProperty(new byte[MaxBinarySize]) }, | |
{ "F", new EntityProperty(new byte[MaxBinarySize]) }, | |
{ "G", new EntityProperty(new byte[MaxBinarySize]) }, | |
{ "H", new EntityProperty(new byte[MaxBinarySize]) }, | |
{ "I", new EntityProperty(new byte[MaxBinarySize]) }, | |
{ "J", new EntityProperty(new byte[MaxBinarySize]) }, | |
{ "K", new EntityProperty(new byte[MaxBinarySize]) }, | |
{ "L", new EntityProperty(new byte[MaxBinarySize]) }, | |
{ "M", new EntityProperty(new byte[MaxBinarySize]) }, | |
{ "N", new EntityProperty(new byte[MaxBinarySize]) }, | |
{ "O", new EntityProperty(new byte[MaxBinarySize]) }, | |
{ "P", new EntityProperty(new byte[65212 - offset]) }, | |
} | |
}; | |
try | |
{ | |
var entitySize = GetEntitySize(dynamicEntity); | |
Console.Write($"Size: {entitySize} ({MaxEntitySize} - {entitySize} = {MaxEntitySize - entitySize})... "); | |
await table.ExecuteAsync(TableOperation.InsertOrReplace(dynamicEntity)); | |
Console.WriteLine("success!"); | |
} | |
catch (StorageException ex) | |
{ | |
Console.WriteLine(ex.Message); | |
} | |
} | |
/// <summary> | |
/// Source: https://docs.microsoft.com/en-us/archive/blogs/avkashchauhan/how-the-size-of-an-entity-is-caclulated-in-windows-azure-table-storage | |
/// </summary> | |
private static int GetEntitySize(DynamicTableEntity entity) | |
{ | |
var size = 4 + (entity.PartitionKey.Length + entity.RowKey.Length) * 2; | |
foreach (var property in entity.Properties) | |
{ | |
size += GetEntityPropertySize(property.Key, property.Value); | |
} | |
// size += GetEntityPropertySize("ETag", new EntityProperty("W/\"datetime'2021-01-10T06%3A11%3A51.4183681Z'\"")); | |
// size += GetEntityPropertySize("ETag", new EntityProperty("'2021-01-10T06%3A11%3A51.4183681Z'")); | |
return size; | |
} | |
private static int GetEntityPropertySize(string name, EntityProperty value) | |
{ | |
var size = 8 + name.Length * 2; | |
switch (value.PropertyType) | |
{ | |
case EdmType.String: | |
size += value.StringValue.Length * 2 + 4; | |
break; | |
case EdmType.Binary: | |
size += value.BinaryValue.Length + 4; | |
break; | |
default: | |
throw new NotImplementedException(); | |
} | |
return size; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment