-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSqlMapper.TypeDeserializerCache.cs
161 lines (148 loc) · 6.46 KB
/
SqlMapper.TypeDeserializerCache.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
using System;
using System.Data;
using System.Collections;
using System.Collections.Generic;
using System.Text;
namespace Dapper
{
public static partial class SqlMapper
{
private class TypeDeserializerCache
{
private TypeDeserializerCache(Type type)
{
this.type = type;
}
private static readonly Hashtable byType = new Hashtable();
private readonly Type type;
internal static void Purge(Type type)
{
lock (byType)
{
byType.Remove(type);
}
}
internal static void Purge()
{
lock (byType)
{
byType.Clear();
}
}
internal static Func<IDataReader, object> GetReader(Type type, IDataReader reader, int startBound, int length, bool returnNullIfFirstMissing)
{
var found = (TypeDeserializerCache)byType[type];
if (found == null)
{
lock (byType)
{
found = (TypeDeserializerCache)byType[type];
if (found == null)
{
byType[type] = found = new TypeDeserializerCache(type);
}
}
}
return found.GetReader(reader, startBound, length, returnNullIfFirstMissing);
}
private readonly Dictionary<DeserializerKey, Func<IDataReader, object>> readers = new Dictionary<DeserializerKey, Func<IDataReader, object>>();
private struct DeserializerKey : IEquatable<DeserializerKey>
{
private readonly int startBound, length;
private readonly bool returnNullIfFirstMissing;
private readonly IDataReader reader;
private readonly string[] names;
private readonly Type[] types;
private readonly int hashCode;
public DeserializerKey(int hashCode, int startBound, int length, bool returnNullIfFirstMissing, IDataReader reader, bool copyDown)
{
this.hashCode = hashCode;
this.startBound = startBound;
this.length = length;
this.returnNullIfFirstMissing = returnNullIfFirstMissing;
if (copyDown)
{
this.reader = null;
names = new string[length];
types = new Type[length];
int index = startBound;
for (int i = 0; i < length; i++)
{
names[i] = reader.GetName(index);
types[i] = reader.GetFieldType(index++);
}
}
else
{
this.reader = reader;
names = null;
types = null;
}
}
public override int GetHashCode() => hashCode;
public override string ToString()
{ // only used in the debugger
if (names != null)
{
return string.Join(", ", names);
}
if (reader != null)
{
var sb = new StringBuilder();
int index = startBound;
for (int i = 0; i < length; i++)
{
if (i != 0) sb.Append(", ");
sb.Append(reader.GetName(index++));
}
return sb.ToString();
}
return base.ToString();
}
public override bool Equals(object obj)
=> obj is DeserializerKey key && Equals(key);
public bool Equals(DeserializerKey other)
{
if (hashCode != other.hashCode
|| startBound != other.startBound
|| length != other.length
|| returnNullIfFirstMissing != other.returnNullIfFirstMissing)
{
return false; // clearly different
}
for (int i = 0; i < length; i++)
{
if ((names?[i] ?? reader?.GetName(startBound + i)) != (other.names?[i] ?? other.reader?.GetName(startBound + i))
||
(types?[i] ?? reader?.GetFieldType(startBound + i)) != (other.types?[i] ?? other.reader?.GetFieldType(startBound + i))
)
{
return false; // different column name or type
}
}
return true;
}
}
private Func<IDataReader, object> GetReader(IDataReader reader, int startBound, int length, bool returnNullIfFirstMissing)
{
if (length < 0) length = reader.FieldCount - startBound;
int hash = GetColumnHash(reader, startBound, length);
if (returnNullIfFirstMissing) hash *= -27;
// get a cheap key first: false means don't copy the values down
var key = new DeserializerKey(hash, startBound, length, returnNullIfFirstMissing, reader, false);
Func<IDataReader, object> deser;
lock (readers)
{
if (readers.TryGetValue(key, out deser)) return deser;
}
deser = GetTypeDeserializerImpl(type, reader, startBound, length, returnNullIfFirstMissing);
// get a more expensive key: true means copy the values down so it can be used as a key later
key = new DeserializerKey(hash, startBound, length, returnNullIfFirstMissing, reader, true);
lock (readers)
{
return readers[key] = deser;
}
}
}
}
}