How to make Service Fabric Reliable collections case-insensitive? - c#

I have a Stateful Service Fabric service and create, update or read data using IReliableDictionary created with the following code:
var dictionary = await StateManager.GetOrAddAsync<IReliableDictionary<string, Entry>>(ReliableDictionaryName);
// Read
using (ITransaction tx = StateManager.CreateTransaction())
{
ConditionalValue<Entry> result = await dictionary.TryGetValueAsync(tx, name);
return result.HasValue ? result.Value : null;
}
// Create or update
using (ITransaction tx = StateManager.CreateTransaction())
{
await dictionary.AddOrUpdateAsync(tx, entry.Name, entry, (key, prev) => entry);
await tx.CommitAsync();
}
It works, but it is case-sensitive.
Is there any way to make Reliable collection store and get data in a case-insensitive way, except for applying .ToLower() to the keys, which is kind of hacky?

This behavior you see is mostly a property of how strings are compared by default in C#. Reliable dictionaries use a key's implementation of IEquatable and IComparable to perform lookups. If the default behavior of string doesn't work for you, you can implement a type that performs string comparisons the way you want. Then, use the new type as the key for your reliable dictionary. You could implement implicit operators to convert between raw strings and the custom type to make usage painless. Here's an example:
using System.Runtime.Serialization;
[DataContract]
public class CaseInsensitiveString : IEquatable<CaseInsensitiveString>,
IComparable<CaseInsensitiveString>
{
#region Constructors
public CaseInsensitiveString(string value)
{
this.Value = value;
}
#endregion
#region Instance Properties
[DataMember]
public string Value
{
get;
set;
}
#endregion
#region Instance Methods
public override bool Equals(object obj)
{
if (ReferenceEquals(null,
obj))
{
return false;
}
if (ReferenceEquals(this,
obj))
{
return true;
}
if (obj.GetType() != this.GetType())
{
return false;
}
return this.Equals((CaseInsensitiveString)obj);
}
public override int GetHashCode()
{
return this.Value != null
? this.Value.GetHashCode()
: 0;
}
public int CompareTo(CaseInsensitiveString other)
{
return string.Compare(this.Value,
other?.Value,
StringComparison.OrdinalIgnoreCase);
}
public bool Equals(CaseInsensitiveString other)
{
if (ReferenceEquals(null,
other))
{
return false;
}
if (ReferenceEquals(this,
other))
{
return true;
}
return string.Equals(this.Value,
other.Value,
StringComparison.OrdinalIgnoreCase);
}
#endregion
#region Class Methods
public static bool operator ==(CaseInsensitiveString left,
CaseInsensitiveString right)
{
return Equals(left,
right);
}
public static implicit operator CaseInsensitiveString(string value)
{
return new CaseInsensitiveString(value);
}
public static implicit operator string(CaseInsensitiveString caseInsensitiveString)
{
return caseInsensitiveString.Value;
}
public static bool operator !=(CaseInsensitiveString left,
CaseInsensitiveString right)
{
return !Equals(left,
right);
}
#endregion
}

Related

C# equality with list-based properties

I've read numerous articles related to proper equality in C#:
http://www.loganfranken.com/blog/687/overriding-equals-in-c-part-1/
What is the best algorithm for an overridden System.Object.GetHashCode?
Assume the following sample class:
public class CustomData
{
public string Name { get; set;}
public IList<double> Values = new List<double>();
}
Would it still be the case to compare the Values property using .Equals()? Here is a full equality sample of what I mean:
#region Equality
public override bool Equals(object value)
{
if(Object.ReferenceEquals(null, value)) return false; // Is null?
if (Object.ReferenceEquals(this, value)) return true; // Is the same object?
if (value.GetType() != this.GetType()) return false; // Is the same type?
return IsEqual((CustomData)value);
}
public bool Equals(CustomData obj)
{
if (Object.ReferenceEquals(null, obj)) return false; // Is null?
if (Object.ReferenceEquals(this, obj)) return true; // Is the same object?
return IsEqual(obj);
}
private bool IsEqual(CustomData obj)
{
return obj is CustomData other
&& other.Name.Equals(Name)
&& other.Values.Equals(Values);
}
public override int GetHashCode()
{
unchecked
{
// Choose large primes to avoid hashing collisions
const int HashingBase = (int) 2166136261;
const int HashingMultiplier = 16777619;
int hash = HashingBase;
hash = (hash * HashingMultiplier) ^ (!Object.ReferenceEquals(null, Name) ? Name.GetHashCode() : 0);
hash = (hash * HashingMultiplier) ^ (!Object.ReferenceEquals(null, Values) ? Values.GetHashCode() : 0);
return hash;
}
}
public static bool operator ==(CustomData obj, CustomData other)
{
if (Object.ReferenceEquals(obj, other)) return true;
if (Object.ReferenceEquals(null, obj)) return false; // Ensure that "obj" isn't null
return (obj.Equals(other));
}
public static bool operator !=(CustomData obj, CustomData other) => !(obj == other);
#endregion
List<T>.Equals(List<T> other) will compare references. If you want equality for property Values to be defined as identical sequences of doubles, use the IEnumerable<TSource>.SequenceEqual.(IEnemerable<TSource> other) method (MSDN). See a refactored version of your IsEqual(CustomData obj) below:
private bool IsEqual(CustomData obj)
{
return obj is CustomData other
&& other.Name.Equals(Name)
&& other.Values.SequenceEqual(Values);
}

GetHashCode() override coliding way to often

I'm using unity, and unity does not have a tuple in it, so I created my own tuple class to work since I needed it for my Dictionary.
Dictionary <Tuple<int,int>, Tile>
Tile class that I created and isn't really relevant to solve this problem(at least I think it wont help).
But the problem is that I'm using both negative and positive integer in my tuples, and when I use my current GetHashCode() with the Tuples, sometimes I get the same HashCode, for example Tuple<-10, 8> and Tuple<-9,-10> both gives -172 when I return the hashcode.
Is there any good GetHashCode that wouldn't get me conflicts?
To be honest I'm only using the operator ==, because I need to check if both tuples have the same integers inside of them, if I could get a operator == that only collides when both integer are the same and in the same order, it would solve my problem.
Some other minor problems, I can't get to understand the Equals override, as it is, it is working, but I don't know how well it works, since I kind of changed every single thing until it worked.
public class Tuple<T1, T2>
{
public T1 First { get; private set; }
public T2 Second { get; private set; }
public Tuple(T1 _First, T2 _Second)
{
First = _First;
Second = _Second;
}
public override int GetHashCode()
{
int hash = 0;
hash = First.GetHashCode() * 17 + Second.GetHashCode() + First.GetHashCode();
return hash;
}
public static bool operator==(Tuple<T1, T2> obj1, Tuple<T1, T2> obj2)
{
if (ReferenceEquals(null, obj2))
return false;
return (obj1.GetHashCode() == obj2.GetHashCode());
}
public static bool operator!=(Tuple<T1, T2> obj1, Tuple<T1, T2> obj2)
{
if (ReferenceEquals(null, obj2))
return true;
return !(obj1.GetHashCode() == obj2.GetHashCode());
}
public bool Equals(Tuple<T1, T2> other)
{
if (other == null)
return false;
if (GetHashCode() == other.GetHashCode())
return true;
else
return false;
}
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj))
{
return false;
}
if (ReferenceEquals(this, obj))
{
return true;
}
Tuple<T1, T2> other = obj as Tuple<T1, T2>;
return obj.GetType() == GetType() && Equals(other);
}
}
public static class Tuple
{
public static Tuple<T1, T2> New<T1, T2>(T1 first, T2 second)
{
var tuple = new Tuple<T1, T2>(first, second);
return tuple;
}
}
GetHashCode() isn't supposed to be collision free. You should use it to determine if two things might be the same objects, and then you have to actually do a thorough check to see if they are.
For example, your == method should be written more like this:
public static bool operator==(Tuple<T1, T2> obj1, Tuple<T1, T2> obj2)
{
if (ReferenceEquals(null, obj2))
return false;
if (obj1.GetHashCode() != obj2.GetHashCode())
{
return false;
}
return DefaultComparer<T1>.Equals(obj1.First, obj2.First) && DefaultComparer<T2>.Equals(obj1.Second, obj2.Second);
}
Also, don't forget to consider the case where obj1 and obj2 are both null.
If you're implementing your own Tuple, you might consider just stealing Microsoft's from the Reference Source repository, or at least use it as a base for your own.
I'm using unity, and unity does not have a tuple in it
It supports Tuple if you have Unity 2017 and above.
Go to Edit --> Project Settings --> Player --> Other Settings --> Configuration --> Scripting Runtime Version --> .NET 4.x Equivalent.
Reload or restart Visual Studio and you should be able to use Tuple. If you are not using Unity 2017 and above and also don't want to update then see John's answer.
This is what resharper generates for you automatically. Just note how they do the GetHashCode() and Equals.
private class Tuple<T1,T2> : IEquatable<Tuple<T1, T2>>
{
public T1 First {get;}
public T2 Second {get;}
public Tuple(T1 first, T2 second)
{
First = first;
Second = second;
}
public bool Equals(Tuple<T1, T2> other)
{
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
return EqualityComparer<T1>.Default.Equals(First, other.First) && EqualityComparer<T2>.Default.Equals(Second, other.Second);
}
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != this.GetType()) return false;
return Equals((Tuple<T1, T2>) obj);
}
public override int GetHashCode()
{
unchecked
{
return (EqualityComparer<T1>.Default.GetHashCode(First) * 397) ^ EqualityComparer<T2>.Default.GetHashCode(Second);
}
}
public static bool operator ==(Tuple<T1, T2> left, Tuple<T1, T2> right)
{
return Equals(left, right);
}
public static bool operator !=(Tuple<T1, T2> left, Tuple<T1, T2> right)
{
return !Equals(left, right);
}
}
I have found PropertyCompare (*) to be useful in auto-generating Equals implementations. It will automatically compare all public properties (so if you add a new public property you don't need to change anything except GetHashCode (and even that is technically optional).
It uses Cache to be reasonably performant - it takes a one-off hit (per type) to generate the appropriate expressions for the comparisons.
using System;
using System.Linq.Expressions;
namespace YourApp
{
public class Tuple<T1, T2>
{
public T1 First { get; private set; }
public T2 Second { get; private set; }
public Tuple(T1 _First, T2 _Second)
{
First = _First;
Second = _Second;
}
public override int GetHashCode()
{
var hash = 0;
// Implement this however you like
hash = First.GetHashCode() * 17 + Second.GetHashCode() + First.GetHashCode();
return hash;
}
public static bool operator ==(Tuple<T1, T2> x, Tuple<T1, T2> y)
{
return PropertyCompare.Equal(x, y);
}
public static bool operator !=(Tuple<T1, T2> x, Tuple<T1, T2> y)
{
return !PropertyCompare.Equal(x, y);
}
public bool Equals(Tuple<T1, T2> other)
{
return PropertyCompare.Equal(this, other);
}
public override bool Equals(object obj)
{
return PropertyCompare.Equal(this, obj);
}
}
public static class Tuple
{
public static Tuple<T1, T2> New<T1, T2>(T1 first, T2 second)
{
var tuple = new Tuple<T1, T2>(first, second);
return tuple;
}
}
public class Program
{
public static void Main()
{
var bob1 = Tuple.New("a", 1);
var bob2 = Tuple.New("a", 1);
Console.WriteLine(bob1 == bob2);
Console.ReadLine();
}
}
public static class PropertyCompare
{
public static bool Equal<T>(T x, object y) where T : class
{
return Cache<T>.Compare(x, y as T);
}
public static bool Equal<T>(T x, T y)
{
if (x == null)
{
return y == null;
}
if (y == null)
{
return false;
}
return Cache<T>.Compare(x, y);
}
private static class Cache<T>
{
internal static readonly Func<T, T, bool> Compare;
static Cache()
{
var props = typeof(T).GetProperties();
if (props.Length == 0)
{
Compare = delegate { return true; };
return;
}
var x = Expression.Parameter(typeof(T), "x");
var y = Expression.Parameter(typeof(T), "y");
Expression body = null;
for (var i = 0; i < props.Length; i++)
{
var propEqual = Expression.Equal(
Expression.Property(x, props[i]),
Expression.Property(y, props[i]));
if (body == null)
{
body = propEqual;
}
else
{
body = Expression.AndAlso(body, propEqual);
}
}
Compare = Expression.Lambda<Func<T, T, bool>>(body, x, y)
.Compile();
}
}
}
}
(*) I found it online somewhere, alas I can't remember where and Google is failing me here.

C# operator overloading: Object.Equals(object o) & Object.GetHashCode()

So I am creating a BST and want it to be a generic tree, right now I am developing the Node<T> class. I need some help with some of the operator overloading specifics. Class is below:
public class Node<T> where T : IComparable
{
//Private member data
private T data;
private Node<T> left;
private Node<T> right;
//private readonly IComparer<T> _comparer;
//Node constructor
public Node()
{
data = default(T); //
left = null;
right = null;
}
//Setters/getters for node private data members
public T Data
{
get { return data; }
set { data = value; }
}
public Node<T> Left
{
get { return left; }
set { left = value; }
}
public Node<T> Right
{
get { return right; }
set { right = value; }
}
public static bool operator ==(Node<T> lhs, Node<T> rhs)
{
if((lhs.Data).CompareTo(rhs.Data) == 0)
{
return true;
}
else
{
return false;
}
}
public static bool operator !=(Node<T> lhs, Node<T> rhs)
{
if (lhs.Data.CompareTo(rhs.Data) != 0)
{
return true;
}
else
{
return false;
}
}
}
But Visual Studio (and sources I have seen online) say I need to overload the Object.Equals(object o) method and also the Object.GetHashCode.
From what I know about .Equals just through using C#, it's like value type semantics, or will compare the values of 2 objects, instead of their references, aka checking if they are actually the same object, which I think is what == usually does when comparing objects. So this is what I tried to do:
public override bool Equals(Object obj)
{
if ((lhs.Data).CompareTo(rhs.Data) == 0)
{
return true;
}
else
{
return false;
}
}
It's basically the same as my == operator, but it doesn't work because the lhs/rhs parameters don't exist. I have no idea how do accomplish this for my case since how I believe it will be called is n1.Equals(n2) and will check to see if the data values are the same for the nodes. Examples online are unclear to me. I also have no idea why I have to involve this hash method at all, but I am still trying to do research on that. Mostly curious about the Equals override for now.
Make sure you implement the Equals method according to the MS guide. Here is my snippet. I use all the time:
// override object.Equals
public override bool Equals(object obj)
{
// shortcut
if (object.ReferenceEquals(this, obj))
{
return true;
}
// check for null and make sure we do not break oop / inheritance
if (obj == null || GetType() != obj.GetType())
{
return false;
}
// TODO: write your implementation of Equals() here
// do not call base.equals !
throw new NotImplementedException();
return false;
}
public static bool operator ==(Class lobj, Class robj)
{
// users expect == working the same way as Equals
return object.Equals(lobj, robj);
}
public static bool operator !=(Class lobj, Class robj)
{
return !object.Equals(lobj, robj);
}
// override object.GetHashCode
public override int GetHashCode()
{
// TODO: write your implementation of GetHashCode() here
// return field.GetHashCode() ^ field2.GetHashCode() ^ base.GetHashCode();
// or simply return the unique id if any
throw new NotImplementedException();
}

Inline Comparer

I have a class Person with a Name property.
I have a collection of persons.
I have a method to add a new person but I need to check of the collection already contains the person.
I would like to use coll.Contains(newPerson,[here is the comparer]) where the comparer will make the comparison on the name property.
Is it possible to make the comparison inline (anonymously) without creating a new class implementing IEqualityComparer?
In the case you don't want duplicate Person objects, and want to operate on that collection as a set, you can use a HashSet<Person> instead which when calling its Add method will do the check if such a person already exists. For that to work, you can implement IEquatable<Person> in your class. It would look roughly like this:
public class Person : IEquatable<Person>
{
public Person(string name)
{
Name = name;
}
public string Name { get; private set; }
public bool Equals(Person other)
{
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
return string.Equals(Name, other.Name, StringComparison.OrdinalIgnoreCase);
}
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != this.GetType()) return false;
return Equals((Person) obj);
}
public override int GetHashCode()
{
return (Name != null ? Name.GetHashCode() : 0);
}
public static bool operator ==(Person left, Person right)
{
return Equals(left, right);
}
public static bool operator !=(Person left, Person right)
{
return !Equals(left, right);
}
}
And now you can use it in your HashSet<Person> like this:
void Main()
{
var firstPerson = new Person { Name = "Yuval" };
var secondPerson = new Person { Name = "yuval" };
var personSet = new HashSet<Person> { firstPerson };
Console.WriteLine(personSet.Add(secondPerson)); // Will print false.
}
Note this won't give you the flexibility of multiple comparers, but this way you won't have to create a new class implementing IEqualityComparer<T>.
You can use linq instead.
bool contains = coll.Any(p => p.Name == newPerson.Name);
You can add any condition here as you want. for example as WaiHaLee noted you can make compare ignore case.
bool contains = coll.Any(p => p.Name.Equals(newPerson.Name, StringComparison.OrdinalIgnoreCase));

Problem with IDictionary<Complex Key, Complex Value>.Remove() implementation

Hi I don't understand why this code doesn't work - it don't remove key; I still get "2" on output.
Bencode.BencodeDict d = new myTorrent.Bencode.BencodeDict();
d.Dict.Add(new Bencode.BencodeString("info"), new Bencode.BencodeString("1"));
d.Dict.Add(new Bencode.BencodeString("info2"), new Bencode.BencodeString("2"));
d.Dict.Add(new Bencode.BencodeString("info3"), new Bencode.BencodeString("3"));
d.Remove(new Bencode.BencodeString("info2"));
Bencode.BencodeVariable s1;
s1 = d[new Bencode.BencodeString("info2")];
if (s1 != null)
Console.WriteLine(System.Text.UTF8Encoding.UTF8.GetString(s1.Encode()));
My BencodeDict and BencodeString
namespace myTorrent.Bencode
{
class BencodeDict : BencodeVariable, IDictionary<BencodeString, BencodeVariable>
{
private Dictionary<BencodeString, BencodeVariable> dict;
public BencodeDict() {
this.dict = new Dictionary<BencodeString,BencodeVariable>();
}
protected override void InternalDecode(BinaryReader data) { /*...*/ }
public override long ByteLength() { /*...*/ }
public override byte[] Encode() { /*...*/ }
//#region Overridden Methods
public override bool Equals(object ob)
{
if (ob == null)
return false;
BencodeDict y = ob as BencodeDict;
if (this.dict.Count != y.dict.Count)
return false;
BencodeVariable val;
foreach (KeyValuePair<BencodeString, BencodeVariable> keypair in this.dict)
{
if (!y.TryGetValue(keypair.Key, out val))
return false;
if (!keypair.Value.Equals(val))
return false;
}
return true;
}
public override int GetHashCode()
{
int result = 0;
foreach (KeyValuePair<BencodeString, BencodeVariable> keypair in this.dict)
{
result ^= keypair.Key.GetHashCode();
result ^= keypair.Value.GetHashCode();
}
return result;
}
#region IDictionary and IList methods
public void Add(BencodeString key, BencodeVariable value)
{
this.dict.Add(key, value);
}
public void Add(KeyValuePair<BencodeString, BencodeVariable> item)
{
this.dict.Add(item.Key, item.Value);
}
public void Clear()
{
this.dict.Clear();
}
public bool Contains(KeyValuePair<BencodeString, BencodeVariable> item)
{
if (!this.dict.ContainsKey(item.Key))
return false;
return this.dict[item.Key].Equals(item.Value);
}
public bool ContainsKey(BencodeString key)
{
foreach(KeyValuePair<BencodeString, BencodeVariable> pair in this.dict) {
if (pair.Key.Equals(key))
return true;
}
return false;
}
public void CopyTo(KeyValuePair<BencodeString, BencodeVariable>[] array, int arrayIndex) { /*...*/ }
public int Count
{
get { return this.dict.Count; }
}
public bool IsReadOnly
{
get { return false; }
}
public bool Remove(BencodeString key)
{
return this.dict.Remove(key);
}
public bool Remove(KeyValuePair<BencodeString, BencodeVariable> item)
{
return this.dict.Remove(item.Key);
}
public bool TryGetValue(BencodeString key, out BencodeVariable value)
{
foreach(KeyValuePair<BencodeString, BencodeVariable> pair in this.dict)
if ( pair.Key.Equals(key) ) {
value = pair.Value;
return true;
}
value = null;
return false;
}
public BencodeVariable this[BencodeString key]
{
get {
foreach(KeyValuePair<BencodeString, BencodeVariable> pair in this.dict)
if ( pair.Key.Equals(key) )
return pair.Value;
return null;
}
set { this.dict[key] = value; }
}
public ICollection<BencodeString> Keys
{
get { return this.dict.Keys; }
}
public ICollection<BencodeVariable> Values
{
get { return this.dict.Values; }
}
public IEnumerator<KeyValuePair<BencodeString, BencodeVariable>> GetEnumerator()
{
return this.dict.GetEnumerator();
}
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
return this.dict.GetEnumerator();
}
#endregion
}
}
class BencodeString : BencodeVariable
{
private byte[] str;
public BencodeString() {
this.str = null;
}
public BencodeString(string str) {
this.str = encoding.GetBytes(str);
}
public override bool Equals(object ob)
{
if (ob == null)
return false;
BencodeString y = ob as BencodeString;
return (encoding.GetString(this.str) == encoding.GetString(y.str));
}
public override int GetHashCode()
{
return this.str.GetHashCode();
}
}
You're relying on byte[].GetHashCode() doing something desirable. It won't. Arrays don't implement equality or hash operations - you'll get the default (identity) behaviour.
Rewrite your GetHashCode method as something like this:
public override int GetHashCode()
{
int result = 17;
foreach (byte b in str)
{
result = result * 31 + b;
}
return result;
}
(Also it's not clear what encoding is, but that's a different matter.)
Note that your Equals override will also throw a NullReferenceException if ob is a non-null reference, but not to a BencodeString.
EDIT: Assuming you're actually wanting to check for the byte arrays being the same, I wouldn't call Encoding.GetString in your equality check. There's no point. Just check the byte array contents directly. Something like this is a reasonable byte array equality check - although I'd generally prefer to write a generic equivalent:
private static bool ArraysEqual(byte[] x, byte[] y)
{
if (x == y)
{
return true;
}
if (x == null || y == null)
{
return false;
}
if (x.Length != y.Length)
{
return false;
}
for (int i = 0; i < x.Length; i++)
{
if (x[i] != y[i])
{
return false;
}
}
return true;
}
If you do want to check whether two byte arrays are decoded to equal strings, then you should use Encoding.GetString in both places... but that would rarely be an appropriate thing to do, IMO.
Mind you, it's not clear why you've got your own string-like class to start with. There are all kinds of potential problems here... unequal encodings, null references etc.
It is very important that values that are Equal also produce the same hash code. An obvious (but not necessarily efficient) workaround is this:
public override int GetHashCode()
{
return encoding.GetString(this.str).GetHashCode();
}
Making strings not behave as Unicode strings internally is a a code smell but possibly intentional here. It is normally applied at the outer interface. Your implementation would allow for the encoding to change after the string is read. But a really serious problem with that is that the dictionary is no longer valid when that happens. You won't be able to find keys back.

Categories

Resources