-
Notifications
You must be signed in to change notification settings - Fork 2k
/
QueueId.cs
95 lines (79 loc) · 3.01 KB
/
QueueId.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
using System;
using Orleans.Concurrency;
using Orleans.Runtime;
namespace Orleans.Streams
{
/// <summary>
/// Identifier of a durable queue.
/// Used by Orleans streaming extensions.
/// </summary>
[Serializable]
[Immutable]
public class QueueId : IRingIdentifier<QueueId>, IEquatable<QueueId>, IComparable<QueueId>
{
private static readonly Lazy<Interner<QueueId, QueueId>> queueIdInternCache = new Lazy<Interner<QueueId, QueueId>>(
() => new Interner<QueueId, QueueId>(InternerConstants.SIZE_LARGE, InternerConstants.DefaultCacheCleanupFreq));
private readonly string queueNamePrefix;
private readonly uint queueId;
private readonly uint uniformHashCache;
// TODO: Need to integrate with Orleans serializer to really use Interner.
private QueueId(string queuePrefix, uint id, uint hash)
{
queueNamePrefix = queuePrefix;
queueId = id;
uniformHashCache = hash;
}
public static QueueId GetQueueId(string queueName, uint queueId, uint hash)
{
return FindOrCreateQueueId(queueName, queueId, hash);
}
private static QueueId FindOrCreateQueueId(string queuePrefix, uint id, uint hash)
{
var key = new QueueId(queuePrefix, id, hash);
return queueIdInternCache.Value.FindOrCreate(key, k => k);
}
public string GetStringNamePrefix()
{
return queueNamePrefix;
}
public uint GetNumericId()
{
return queueId;
}
public uint GetUniformHashCode()
{
return uniformHashCache;
}
public int CompareTo(QueueId other)
{
int cmp = queueId.CompareTo(other.queueId);
if (cmp != 0) return cmp;
cmp = String.Compare(queueNamePrefix, other.queueNamePrefix, StringComparison.Ordinal);
if (cmp != 0) return cmp;
return uniformHashCache.CompareTo(other.uniformHashCache);
}
public virtual bool Equals(QueueId other)
{
return other != null
&& queueId == other.queueId
&& String.Equals(queueNamePrefix, other.queueNamePrefix, StringComparison.Ordinal)
&& uniformHashCache == other.uniformHashCache;
}
public override bool Equals(object obj)
{
return this.Equals(obj as QueueId);
}
public override int GetHashCode()
{
return (int)queueId ^ (queueNamePrefix !=null ? queueNamePrefix.GetHashCode() : 0) ^ (int)uniformHashCache;
}
public override string ToString()
{
return String.Format("{0}-{1}", (queueNamePrefix !=null ? queueNamePrefix.ToLower() : String.Empty), queueId.ToString());
}
public string ToStringWithHashCode()
{
return String.Format("{0}-0x{1, 8:X8}", this.ToString(), this.GetUniformHashCode());
}
}
}