forked from ravendb/ravendb
/
Etag.cs
338 lines (301 loc) · 6.96 KB
/
Etag.cs
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
using System.Linq;
#if NETFX_CORE
using Raven.Abstractions.Util;
#else
using System.Security.Cryptography;
#endif
using Raven.Imports.Newtonsoft.Json;
namespace Raven.Abstractions.Data
{
public class Etag : IEquatable<Etag>, IComparable<Etag>
{
public override int GetHashCode()
{
unchecked
{
return (restarts.GetHashCode()*397) ^ changes.GetHashCode();
}
}
long restarts;
long changes;
public long Restarts
{
get { return restarts; }
}
public long Changes
{
get { return changes; }
}
public Etag()
{
}
public Etag(string str)
{
var etag = Parse(str);
restarts = etag.restarts;
changes = etag.changes;
}
public Etag(UuidType type, long restarts, long changes)
{
this.restarts = ((long)type << 56) | restarts;
this.changes = changes;
}
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != GetType()) return false;
return Equals((Etag) obj);
}
public static bool operator ==(Etag a, Etag b)
{
if (ReferenceEquals(a, null) && ReferenceEquals(b, null))
return true;
if (ReferenceEquals(a, null))
return false;
return a.Equals(b);
}
public static bool operator !=(Etag a, Etag b)
{
return !(a == b);
}
public bool Equals(Etag other)
{
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
return restarts == other.restarts && changes == other.changes;
}
public int CompareTo(Etag other)
{
if (ReferenceEquals(other, null))
return -1;
var sub = restarts - other.restarts;
if (Math.Abs(sub) > 0)
return sub > 0 ? 1 : -1;
sub = changes - other.changes;
if (sub != 0)
return sub > 0 ? 1 : -1;
return 0;
}
private IEnumerable<byte> ToBytes()
{
foreach (var source in BitConverter.GetBytes(restarts).Reverse())
{
yield return source;
}
foreach (var source in BitConverter.GetBytes(changes).Reverse())
{
yield return source;
}
}
public byte[] ToByteArray()
{
return ToBytes().ToArray();
}
public override string ToString()
{
var sb = new StringBuilder(36);
foreach (var by in ToBytes())
{
sb.Append(by.ToString("X2"));
}
sb.Insert(8, "-")
.Insert(13, "-")
.Insert(18, "-")
.Insert(23, "-");
return sb.ToString();
}
public static Etag Parse(byte[] bytes)
{
return new Etag
{
restarts = BitConverter.ToInt64(bytes.Take(8).Reverse().ToArray(), 0),
changes = BitConverter.ToInt64(bytes.Skip(8).Take(8).Reverse().ToArray(), 0)
};
}
public static bool TryParse(string str, out Etag etag)
{
try
{
etag = Parse(str);
return true;
}
catch (Exception)
{
etag = null;
return false;
}
}
public static Etag Parse(string str)
{
if (string.IsNullOrEmpty(str))
throw new ArgumentException("str cannot be empty or null");
if (str.Length != 36)
throw new ArgumentException("str must be 36 characters");
var buffer = new byte[16]
{
byte.Parse(str.Substring(16, 2), NumberStyles.HexNumber),
byte.Parse(str.Substring(14, 2), NumberStyles.HexNumber),
byte.Parse(str.Substring(11, 2), NumberStyles.HexNumber),
byte.Parse(str.Substring(9, 2), NumberStyles.HexNumber),
byte.Parse(str.Substring(6, 2), NumberStyles.HexNumber),
byte.Parse(str.Substring(4, 2), NumberStyles.HexNumber),
byte.Parse(str.Substring(2, 2), NumberStyles.HexNumber),
byte.Parse(str.Substring(0, 2), NumberStyles.HexNumber),
byte.Parse(str.Substring(34, 2), NumberStyles.HexNumber),
byte.Parse(str.Substring(32, 2), NumberStyles.HexNumber),
byte.Parse(str.Substring(30, 2), NumberStyles.HexNumber),
byte.Parse(str.Substring(28, 2), NumberStyles.HexNumber),
byte.Parse(str.Substring(26, 2), NumberStyles.HexNumber),
byte.Parse(str.Substring(24, 2), NumberStyles.HexNumber),
byte.Parse(str.Substring(21, 2), NumberStyles.HexNumber),
byte.Parse(str.Substring(19, 2), NumberStyles.HexNumber)
};
return new Etag
{
restarts = BitConverter.ToInt64(buffer, 0),
changes = BitConverter.ToInt64(buffer, 8)
};
}
public static Etag InvalidEtag
{
get
{
return new Etag
{
restarts = -1,
changes = -1
};
}
}
public static Etag Empty
{
get
{
return new Etag
{
restarts = 0,
changes = 0
};
}
}
public Etag Setup(UuidType type, long restartsNum)
{
return new Etag
{
restarts = ((long)type << 56) | restartsNum,
changes = changes
};
}
public Etag IncrementBy(int amount)
{
return new Etag
{
restarts = restarts,
changes = changes + amount
};
}
public static implicit operator string(Etag etag)
{
if (etag == null)
return null;
return etag.ToString();
}
public static implicit operator Etag(string s)
{
return Parse(s);
}
public static implicit operator Etag(Guid g)
{
return GuidToEtag(g);
}
public static implicit operator Guid?(Etag e)
{
if (e == null)
return null;
return EtagToGuid(e);
}
public static implicit operator Etag(Guid? g)
{
if (g == null)
return null;
return GuidToEtag(g.Value);
}
public static implicit operator Guid(Etag e)
{
return EtagToGuid(e);
}
public Etag HashWith(Etag other)
{
return HashWith(other.ToBytes());
}
public Etag HashWith(IEnumerable<byte> bytes)
{
var etagBytes = ToBytes().Concat(bytes).ToArray();
#if NETFX_CORE
return Parse(MD5Core.GetHash(etagBytes));
#else
using (var md5 = MD5.Create())
{
return Parse(md5.ComputeHash(etagBytes));
}
#endif
}
public static Etag Max(Etag first, Etag second)
{
if (first == null)
return second;
return first.CompareTo(second) > 0
? first
: second;
}
private static Guid EtagToGuid(Etag etag)
{
var bytes = etag.ToByteArray();
if (BitConverter.IsLittleEndian)
{
Array.Reverse(bytes, 0, 4);
Array.Reverse(bytes, 4, 2);
Array.Reverse(bytes, 6, 2);
}
return new Guid(bytes);
}
private static Etag GuidToEtag(Guid guid)
{
var bytes = guid.ToByteArray();
if (BitConverter.IsLittleEndian)
{
Array.Reverse(bytes, 0, 4);
Array.Reverse(bytes, 4, 2);
Array.Reverse(bytes, 6, 2);
}
return Etag.Parse(bytes);
}
}
public class EtagJsonConverter : JsonConverter
{
public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
{
var etag = value as Etag;
if(etag == null)
writer.WriteNull();
else
writer.WriteValue(etag.ToString());
}
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
var s = reader.Value as string;
if (s == null)
return null;
return Etag.Parse(s);
}
public override bool CanConvert(Type objectType)
{
return objectType == typeof (Etag);
}
}
}