This repository was archived by the owner on Mar 31, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathTableEntitySizeCalculator.cs
More file actions
137 lines (116 loc) · 3.54 KB
/
TableEntitySizeCalculator.cs
File metadata and controls
137 lines (116 loc) · 3.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
namespace NuGet.Insights
{
/// <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>
public class TableEntitySizeCalculator
{
// Entity overhead of 4 plus a magic, discovered value.
// See: https://github.com/MicrosoftDocs/azure-docs/issues/68661
private const int InitialSize = 4 + (88 + 48 + 16 + 8);
public int Size { get; private set; }
public void Reset()
{
Size = 0;
}
public void AddEntityOverhead()
{
Size += InitialSize;
}
public void AddPartitionKey(string rowKey)
{
AddPartitionKey(rowKey.Length);
}
public void AddPartitionKey(int length)
{
Size += length * 2;
}
public void AddRowKey(string rowKey)
{
AddRowKey(rowKey.Length);
}
public void AddRowKey(int length)
{
Size += length * 2;
}
public void AddPropertyOverhead(int nameLength)
{
Size += GetPropertyOverhead(nameLength);
}
public void AddBinaryData(int binaryLength)
{
Size += GetBinaryDataSize(binaryLength);
}
public void AddInt32Data()
{
Size += GetInt32DataSize();
}
public void AddGuidData()
{
Size += GetGuidDataSize();
}
public void AddProperty(string name, object value)
{
Size += GetEntityPropertySize(name.Length, value);
}
private static int GetEntityPropertySize(int nameLength, object value)
{
if (value is null)
{
return 0;
}
int size;
switch (value)
{
case string stringValue:
size = stringValue.Length * 2 + 4;
break;
case DateTimeOffset:
size = 8;
break;
case Guid:
size = 16;
break;
case double:
size = 8;
break;
case int:
size = GetInt32DataSize();
break;
case long:
size = 8;
break;
case bool:
size = 1;
break;
case BinaryData binaryValue:
size = GetBinaryDataSize(binaryValue.ToMemory().Length);
break;
case byte[] binaryValue:
size = GetBinaryDataSize(binaryValue.Length);
break;
default:
throw new NotImplementedException();
};
return GetPropertyOverhead(nameLength) + size;
}
private static int GetPropertyOverhead(int nameLength)
{
return 8 + nameLength * 2;
}
private static int GetInt32DataSize()
{
return 4;
}
private static int GetGuidDataSize()
{
return 16;
}
private static int GetBinaryDataSize(int binaryLength)
{
return binaryLength + 4;
}
}
}