-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEntity.cs
More file actions
147 lines (126 loc) · 4.46 KB
/
Entity.cs
File metadata and controls
147 lines (126 loc) · 4.46 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
138
139
140
141
142
143
144
145
146
147
using System;
using System.Xml;
using System.IO;
using System.Runtime.Serialization;
using System.Collections.Generic;
using Microsoft.WindowsAzure.StorageClient;
using JoshCodes.Web.Models.Domain;
using System.Diagnostics.Contracts;
namespace JoshCodes.Persistence.Azure.Storage
{
public class Entity : TableServiceEntity
{
private const string GuidFormat = "N";
public Entity()
{
}
public Entity(Guid key, DateTime lastModified)
{
if (key == default(Guid) || key == Guid.Empty) // Likely the same thing
{
throw new ArgumentException("key value must be set", "key");
}
if (lastModified == default(DateTime))
{
throw new ArgumentException("Last Modified value must be set", "lastModified");
}
this.RowKey = Entity.BuildRowKey(key);
this.PartitionKey = Entity.BuildPartitionKey(this.RowKey);
this.LastModified = lastModified;
}
public Entity(string key, DateTime lastModified)
{
if (String.IsNullOrWhiteSpace(key))
{
throw new ArgumentException("key value must be set", "key");
}
if (lastModified == default(DateTime))
{
throw new ArgumentException("Last Modified value must be set", "lastModified");
}
this.RowKey = key;
this.PartitionKey = Entity.BuildPartitionKey(this.RowKey);
this.LastModified = lastModified;
}
public Guid GetKey()
{
//TODO: return Guid.ParseExact(this.RowKey, GuidFormat);
return Guid.Parse(this.RowKey);
}
public DateTime LastModified { get; set; }
internal static string BuildRowKey(Guid rowKey)
{
return rowKey.ToString(GuidFormat);
}
private static int GetHashCode(string str)
{
unsafe
{
fixed (char* src = str)
{
Contract.Assert(src[str.Length] == '\0', "src[this.Length] == '\\0'");
Contract.Assert(((int)src) % 4 == 0, "Managed string should start at 4 bytes boundary");
int hash1 = (5381 << 16) + 5381;
int hash2 = hash1;
// 32 bit machines.
int* pint = (int*)src;
int len = str.Length;
while (len > 2)
{
hash1 = ((hash1 << 5) + hash1 + (hash1 >> 27)) ^ pint[0];
hash2 = ((hash2 << 5) + hash2 + (hash2 >> 27)) ^ pint[1];
pint += 2;
len -= 4;
}
if (len > 0)
{
hash1 = ((hash1 << 5) + hash1 + (hash1 >> 27)) ^ pint[0];
}
return hash1 + (hash2 * 1566083941);
}
}
}
internal static string BuildPartitionKey(string rowKey)
{
int hashCode = GetHashCode(rowKey);
return (hashCode % 13).ToString();
}
#region Encoding / Decoding
public static T Decode<T>(string value)
{
if (String.IsNullOrWhiteSpace(value))
{
return default(T);
}
var reader = XmlReader.Create(new StringReader(value));
var serializer = new DataContractSerializer(typeof(T));
try
{
T result = (T)serializer.ReadObject(reader);
return result;
}
catch (SerializationException)
{
return default(T);
}
}
public static string Encode<T>(T value)
{
if (EqualityComparer<T>.Default.Equals(value))
{
return String.Empty;
}
string serializedString;
using (MemoryStream memoryStream = new MemoryStream())
using (StreamReader reader = new StreamReader(memoryStream))
{
DataContractSerializer serializer = new DataContractSerializer(typeof(T));
serializer.WriteObject(memoryStream, value);
memoryStream.Position = 0;
serializedString = reader.ReadToEnd();
}
return serializedString;
}
#endregion
}
}