-
Notifications
You must be signed in to change notification settings - Fork 9
/
Identity.cs
75 lines (64 loc) · 1.83 KB
/
Identity.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
using System;
using System.Data;
using Lucene.Net.Index;
using Lucene.Net.Search;
using LukeMapper;
/// <summary>
/// Identity of a cached query in Dapper, used for extensability
/// </summary>
public class Identity : IEquatable<Identity>
{
internal Identity(IndexSearcher searcher, Query query, Type type/*, Type[] otherTypes (for MultiMap)*/)
{
var reader = searcher.GetIndexReader();
var fieldNames = reader.GetFieldNames(IndexReader.FieldOption.ALL);
this.type = type;
unchecked
{
hashCode = 17; // we *know* we are using this in a dictionary, so pre-compute this
hashCode = hashCode * 23 + (type == null ? 0 : type.GetHashCode());
}
foreach (var fieldName in fieldNames)
{
unchecked
{
hashCode = hashCode*23 + fieldName.GetHashCode();
}
}
}
internal Identity(Type type)
{
var fieldNames = new[] {"one", "two", "three"};
unchecked
{
hashCode = 17; // we *know* we are using this in a dictionary, so pre-compute this
hashCode = hashCode * 23 + (type == null ? 0 : type.GetHashCode());
}
foreach (var fieldName in fieldNames)
{
unchecked
{
hashCode = hashCode * 23 + fieldName.GetHashCode();
}
}
}
public override bool Equals(object obj)
{
return Equals(obj as Identity);
}
public readonly int hashCode;
private readonly Type type;
public override int GetHashCode()
{
return hashCode;
}
/// <summary>
/// Compare 2 Identity objects
/// </summary>
public bool Equals(Identity other)
{
return
other != null &&
GetHashCode() == other.GetHashCode();
}
}