-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Expand file tree
/
Copy pathGrainManifest.cs
More file actions
101 lines (88 loc) · 3.12 KB
/
Copy pathGrainManifest.cs
File metadata and controls
101 lines (88 loc) · 3.12 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
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using Orleans.Runtime;
namespace Orleans.Metadata
{
/// <summary>
/// Information about available grains.
/// </summary>
[Serializable, GenerateSerializer, Immutable]
public sealed class GrainManifest : IEquatable<GrainManifest>
{
[NonSerialized]
private int? _hashCode;
/// <summary>
/// Initializes a new instance of the <see cref="GrainManifest"/> class.
/// </summary>
/// <param name="grains">
/// The grain properties.
/// </param>
/// <param name="interfaces">
/// The interface properties.
/// </param>
public GrainManifest(
ImmutableDictionary<GrainType, GrainProperties> grains,
ImmutableDictionary<GrainInterfaceType, GrainInterfaceProperties> interfaces)
{
ArgumentNullException.ThrowIfNull(grains);
ArgumentNullException.ThrowIfNull(interfaces);
this.Interfaces = interfaces;
this.Grains = grains;
}
/// <summary>
/// Gets the interfaces available on this silo.
/// </summary>
[Id(0)]
public ImmutableDictionary<GrainInterfaceType, GrainInterfaceProperties> Interfaces { get; }
/// <summary>
/// Gets the grain types available on this silo.
/// </summary>
[Id(1)]
public ImmutableDictionary<GrainType, GrainProperties> Grains { get; }
public override int GetHashCode() => _hashCode ??= HashCode.Combine(
ComputeHashCode(Interfaces),
ComputeHashCode(Grains));
public override bool Equals(object? obj) => obj is GrainManifest other && Equals(other);
public bool Equals(GrainManifest? other)
{
if (ReferenceEquals(this, other)) return true;
if (other is null) return false;
return DictionariesEqual(Interfaces, other.Interfaces) && DictionariesEqual(Grains, other.Grains);
}
private static bool DictionariesEqual<TKey, TValue>(
ImmutableDictionary<TKey, TValue> left,
ImmutableDictionary<TKey, TValue> right)
where TKey : notnull
{
if (ReferenceEquals(left, right))
{
return true;
}
if (left.Count != right.Count)
{
return false;
}
var comparer = EqualityComparer<TValue>.Default;
foreach (var entry in left)
{
if (!right.TryGetValue(entry.Key, out var value)
|| !comparer.Equals(entry.Value, value))
{
return false;
}
}
return true;
}
private static int ComputeHashCode<TKey, TValue>(ImmutableDictionary<TKey, TValue> dictionary)
where TKey : notnull
{
var hash = 0;
foreach (var entry in dictionary)
{
hash ^= HashCode.Combine(entry.Key, entry.Value);
}
return hash;
}
}
}