Skip to content

Instantly share code, notes, and snippets.

View joao-r-reis's full-sized avatar

João Reis joao-r-reis

View GitHub Profile
@joao-r-reis
joao-r-reis / BetterTests.cs
Last active November 1, 2020 16:04
BetterTests.cs
[Test]
public void Should_ReturnEqualsTrueAndSameHashCode_When_BothStrategiesHaveSameReplicationSettings()
{
var target1 = new NetworkTopologyStrategy(
new Dictionary<string, int>
{
{ "dc1", 2 },
{ "dc2", 3 },
{ "dc3", 1 }
});
@joao-r-reis
joao-r-reis / CoarseTest.cs
Created October 31, 2020 19:33
CoarseTest.cs
[Test]
public void Build_Should_OnlyCallOncePerReplicationConfiguration_When_MultipleKeyspacesWithSameReplicationOptions()
{
var hosts = new List<Host>
{
{ TestHelper.CreateHost("192.168.0.0", "dc1", "rack", new HashSet<string>{"0"})},
{ TestHelper.CreateHost("192.168.0.1", "dc1", "rack", new HashSet<string>{"10"})},
{ TestHelper.CreateHost("192.168.0.2", "dc1", "rack", new HashSet<string>{"20"})},
{ TestHelper.CreateHost("192.168.0.3", "dc2", "rack", new HashSet<string>{"30"})},
{ TestHelper.CreateHost("192.168.0.4", "dc2", "rack", new HashSet<string>{"40"})}
@joao-r-reis
joao-r-reis / DatacenterReplicationFactorComparer.cs
Last active October 31, 2020 20:38
DatacenterReplicationFactorComparer.cs
internal class DatacenterReplicationFactorComparer : IComparer<DatacenterReplicationFactor>
{
private DatacenterReplicationFactorComparer() { }
public static DatacenterReplicationFactorComparer Instance { get; } = new DatacenterReplicationFactorComparer();
public int Compare(DatacenterReplicationFactor x, DatacenterReplicationFactor y)
{
return string.Compare(x.Datacenter, y.Datacenter, StringComparison.Ordinal);
}
@joao-r-reis
joao-r-reis / SetEquals.cs
Last active October 31, 2020 20:59
SetEquals.cs
public bool SetEquals(IEnumerable<T> other)
{
SortedSet<T> asSorted = other as SortedSet<T>;
if (asSorted != null && AreComparersEqual(this, asSorted))
{
IEnumerator<T> mine = this.GetEnumerator();
IEnumerator<T> theirs = asSorted.GetEnumerator();
bool mineEnded = !mine.MoveNext();
bool theirsEnded = !theirs.MoveNext();
while (!mineEnded && !theirsEnded)
@joao-r-reis
joao-r-reis / NetworkTopologyStrategy.cs
Last active October 31, 2020 20:38
NetworkTopologyStrategy.cs
internal class NetworkTopologyStrategy : IReplicationStrategy, IEquatable<NetworkTopologyStrategy>
{
private readonly SortedSet<DatacenterReplicationFactor> _replicationFactorsSet;
private readonly IReadOnlyDictionary<string, int> _replicationFactorsMap;
private readonly int _hashCode;
public NetworkTopologyStrategy(IReadOnlyDictionary<string, int> replicationFactors)
{
_replicationFactorsSet = new SortedSet<DatacenterReplicationFactor>(
replicationFactors.Select(rf => new DatacenterReplicationFactor(rf.Key, rf.Value)),
@joao-r-reis
joao-r-reis / Insert.cs
Last active October 31, 2020 21:02
Insert.cs
private void Insert(TKey key, TValue value, bool add)
{
// these 3 variables are fields of the Dictionary
// int[] buckets;
// Entry[] entries;
// IEqualityComparer<TKey> comparer;
int hashCode = comparer.GetHashCode(key) & 0x7FFFFFFF;
int targetBucket = hashCode % buckets.Length;
@joao-r-reis
joao-r-reis / TryAddInternal.cs
Last active October 31, 2020 20:59
TryAddInternal.cs
private bool TryAddInternal(TKey key, TValue value, bool updateIfExists, bool acquireLock, out TValue resultingValue)
{
IEqualityComparer<TKey> comparer = m_tables.m_comparer;
var hashcode = comparer.GetHashCode(key);
var bucketCount = m_tables.m_buckets.Length;
var bucketNo = (hashcode & 0x7fffffff) % bucketCount;
// removed locking logic for simplicity
for (Node node = tables.m_buckets[bucketNo]; node != null; node = node.m_next)
@joao-r-reis
joao-r-reis / InitializeFromCollection.cs
Last active October 31, 2020 16:46
InitializeFromCollection.cs
private void InitializeFromCollection(IEnumerable<KeyValuePair<TKey, TValue>> collection)
{
TValue dummy;
foreach (KeyValuePair<TKey, TValue> pair in collection)
{
if (pair.Key == null) throw new ArgumentNullException("key");
if (!TryAddInternal(pair.Key, pair.Value, false, false, out dummy))
{
throw new ArgumentException(GetResource("ConcurrentDictionary_SourceContainsDuplicateKeys"));
@joao-r-reis
joao-r-reis / TokenMap.cs
Last active October 31, 2020 20:32
TokenMap.cs
internal async Task RebuildTokenMapAsync()
{
keyspacesList = await _schemaParser.GetKeyspacesAsync().ConfigureAwait(false);
_tokenMap = TokenMap.Build(keyspacesList);
}
public static TokenMap Build(ICollection<KeyspaceMetadata> keyspaces)
{
var primaryReplicas = new Dictionary<IToken, Host>();
var sortedTokens = new SortedSet<IToken>();
@joao-r-reis
joao-r-reis / TryCatch.cs
Created October 31, 2020 13:32
TryCatch.cs
ISession session;
try
{
session = cluster.Connect();
}
catch (NoHostAvailableException ex)
{
var innerExceptions = ex.Errors.Select(
kvp => $"Host: {kvp.Key}; Message: {kvp.Value.Message}; StackTrace: {kvp.Value.StackTrace}");
log.Error($"{ex.Message};{innerExceptions}");