IdentityManager.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Reflection;
  6. using System.Security;
  7. using System.Security.Permissions;
  8. using System.Runtime.CompilerServices;
  9. namespace System.Data.Linq {
  10. using System.Data.Linq.Mapping;
  11. using System.Data.Linq.Provider;
  12. internal abstract class IdentityManager {
  13. internal abstract object InsertLookup(MetaType type, object instance);
  14. internal abstract bool RemoveLike(MetaType type, object instance);
  15. internal abstract object Find(MetaType type, object[] keyValues);
  16. internal abstract object FindLike(MetaType type, object instance);
  17. internal static IdentityManager CreateIdentityManager(bool asReadOnly) {
  18. if (asReadOnly) {
  19. return new ReadOnlyIdentityManager();
  20. }
  21. else {
  22. return new StandardIdentityManager();
  23. }
  24. }
  25. class StandardIdentityManager : IdentityManager {
  26. Dictionary<MetaType, IdentityCache> caches;
  27. IdentityCache currentCache;
  28. MetaType currentType;
  29. internal StandardIdentityManager() {
  30. this.caches = new Dictionary<MetaType, IdentityCache>();
  31. }
  32. internal override object InsertLookup(MetaType type, object instance) {
  33. this.SetCurrent(type);
  34. return this.currentCache.InsertLookup(instance);
  35. }
  36. internal override bool RemoveLike(MetaType type, object instance) {
  37. this.SetCurrent(type);
  38. return this.currentCache.RemoveLike(instance);
  39. }
  40. internal override object Find(MetaType type, object[] keyValues) {
  41. this.SetCurrent(type);
  42. return this.currentCache.Find(keyValues);
  43. }
  44. internal override object FindLike(MetaType type, object instance) {
  45. this.SetCurrent(type);
  46. return this.currentCache.FindLike(instance);
  47. }
  48. [MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.NoOptimization)]
  49. private void SetCurrent(MetaType type) {
  50. type = type.InheritanceRoot;
  51. if (this.currentType != type) {
  52. if (!this.caches.TryGetValue(type, out this.currentCache)) {
  53. KeyManager km = GetKeyManager(type);
  54. this.currentCache = (IdentityCache)Activator.CreateInstance(
  55. typeof(IdentityCache<,>).MakeGenericType(type.Type, km.KeyType),
  56. BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null,
  57. new object[] { km }, null
  58. );
  59. this.caches.Add(type, this.currentCache);
  60. }
  61. this.currentType = type;
  62. }
  63. }
  64. [MethodImpl(MethodImplOptions.NoInlining | MethodImplOptions.NoOptimization)]
  65. static KeyManager GetKeyManager(MetaType type) {
  66. int n = type.IdentityMembers.Count;
  67. MetaDataMember mm = type.IdentityMembers[0];
  68. KeyManager km = (KeyManager)Activator.CreateInstance(
  69. typeof(SingleKeyManager<,>).MakeGenericType(type.Type, mm.Type),
  70. BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null,
  71. new object[] { mm.StorageAccessor, 0 }, null
  72. );
  73. for (int i = 1; i < n; i++) {
  74. mm = type.IdentityMembers[i];
  75. km = (KeyManager)
  76. Activator.CreateInstance(
  77. typeof(MultiKeyManager<,,>).MakeGenericType(type.Type, mm.Type, km.KeyType),
  78. BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null,
  79. new object[] { mm.StorageAccessor, i, km }, null
  80. );
  81. }
  82. return km;
  83. }
  84. #region Nested type definitions
  85. // These types are internal rather than private to work around
  86. // CLR bug #117419 related to type visibility under partial trust
  87. // in nested class scenarios.
  88. internal abstract class KeyManager {
  89. internal abstract Type KeyType { get; }
  90. }
  91. internal abstract class KeyManager<T, K> : KeyManager {
  92. internal abstract K CreateKeyFromInstance(T instance);
  93. internal abstract bool TryCreateKeyFromValues(object[] values, out K k);
  94. internal abstract IEqualityComparer<K> Comparer { get; }
  95. }
  96. internal class SingleKeyManager<T, V> : KeyManager<T, V> {
  97. bool isKeyNullAssignable;
  98. MetaAccessor<T, V> accessor;
  99. int offset;
  100. IEqualityComparer<V> comparer;
  101. internal SingleKeyManager(MetaAccessor<T, V> accessor, int offset) {
  102. this.accessor = accessor;
  103. this.offset = offset;
  104. this.isKeyNullAssignable = System.Data.Linq.SqlClient.TypeSystem.IsNullAssignable(typeof(V));
  105. }
  106. internal override V CreateKeyFromInstance(T instance) {
  107. return this.accessor.GetValue(instance);
  108. }
  109. internal override bool TryCreateKeyFromValues(object[] values, out V v) {
  110. object o = values[this.offset];
  111. if (o == null && !this.isKeyNullAssignable) {
  112. v = default(V);
  113. return false;
  114. }
  115. v = (V)o;
  116. return true;
  117. }
  118. internal override Type KeyType {
  119. get { return typeof(V); }
  120. }
  121. internal override IEqualityComparer<V> Comparer {
  122. get {
  123. if (this.comparer == null) {
  124. this.comparer = EqualityComparer<V>.Default;
  125. }
  126. return this.comparer;
  127. }
  128. }
  129. }
  130. internal class MultiKeyManager<T, V1, V2> : KeyManager<T, MultiKey<V1, V2>> {
  131. MetaAccessor<T, V1> accessor;
  132. int offset;
  133. KeyManager<T, V2> next;
  134. IEqualityComparer<MultiKey<V1, V2>> comparer;
  135. internal MultiKeyManager(MetaAccessor<T, V1> accessor, int offset, KeyManager<T, V2> next) {
  136. this.accessor = accessor;
  137. this.next = next;
  138. this.offset = offset;
  139. }
  140. internal override MultiKey<V1, V2> CreateKeyFromInstance(T instance) {
  141. return new MultiKey<V1, V2>(
  142. this.accessor.GetValue(instance),
  143. this.next.CreateKeyFromInstance(instance)
  144. );
  145. }
  146. internal override bool TryCreateKeyFromValues(object[] values, out MultiKey<V1, V2> k) {
  147. System.Diagnostics.Debug.Assert(this.offset < values.Length, "offset is outside the bounds of the values array");
  148. object o = values[this.offset];
  149. if (o == null && typeof(V1).IsValueType) {
  150. k = default(MultiKey<V1, V2>);
  151. return false;
  152. }
  153. V2 v2;
  154. if (!this.next.TryCreateKeyFromValues(values, out v2)) {
  155. k = default(MultiKey<V1, V2>);
  156. return false;
  157. }
  158. k = new MultiKey<V1, V2>((V1)o, v2);
  159. return true;
  160. }
  161. internal override Type KeyType {
  162. get { return typeof(MultiKey<V1, V2>); }
  163. }
  164. internal override IEqualityComparer<MultiKey<V1, V2>> Comparer {
  165. get {
  166. if (this.comparer == null) {
  167. this.comparer = new MultiKey<V1, V2>.Comparer(EqualityComparer<V1>.Default, next.Comparer);
  168. }
  169. return this.comparer;
  170. }
  171. }
  172. }
  173. internal struct MultiKey<T1, T2> {
  174. T1 value1;
  175. T2 value2;
  176. internal MultiKey(T1 value1, T2 value2) {
  177. this.value1 = value1;
  178. this.value2 = value2;
  179. }
  180. internal class Comparer : IEqualityComparer<MultiKey<T1, T2>>, IEqualityComparer {
  181. IEqualityComparer<T1> comparer1;
  182. IEqualityComparer<T2> comparer2;
  183. internal Comparer(IEqualityComparer<T1> comparer1, IEqualityComparer<T2> comparer2) {
  184. this.comparer1 = comparer1;
  185. this.comparer2 = comparer2;
  186. }
  187. public bool Equals(MultiKey<T1, T2> x, MultiKey<T1, T2> y) {
  188. return this.comparer1.Equals(x.value1, y.value1) &&
  189. this.comparer2.Equals(x.value2, y.value2);
  190. }
  191. public int GetHashCode(MultiKey<T1, T2> x) {
  192. return this.comparer1.GetHashCode(x.value1) ^ this.comparer2.GetHashCode(x.value2);
  193. }
  194. bool IEqualityComparer.Equals(object x, object y) {
  195. return this.Equals((MultiKey<T1, T2>)x, (MultiKey<T1, T2>)y);
  196. }
  197. int IEqualityComparer.GetHashCode(object x) {
  198. return this.GetHashCode((MultiKey<T1, T2>)x);
  199. }
  200. }
  201. }
  202. internal abstract class IdentityCache {
  203. internal abstract object Find(object[] keyValues);
  204. internal abstract object FindLike(object instance);
  205. internal abstract object InsertLookup(object instance);
  206. internal abstract bool RemoveLike(object instance);
  207. }
  208. internal class IdentityCache<T, K> : IdentityCache {
  209. int[] buckets;
  210. Slot[] slots;
  211. int count;
  212. int freeList;
  213. KeyManager<T, K> keyManager;
  214. IEqualityComparer<K> comparer;
  215. public IdentityCache(KeyManager<T, K> keyManager) {
  216. this.keyManager = keyManager;
  217. this.comparer = keyManager.Comparer;
  218. buckets = new int[7];
  219. slots = new Slot[7];
  220. freeList = -1;
  221. }
  222. internal override object InsertLookup(object instance) {
  223. T value = (T)instance;
  224. K key = this.keyManager.CreateKeyFromInstance(value);
  225. Find(key, ref value, true);
  226. return value;
  227. }
  228. internal override bool RemoveLike(object instance) {
  229. T value = (T)instance;
  230. K key = this.keyManager.CreateKeyFromInstance(value);
  231. int hashCode = comparer.GetHashCode(key) & 0x7FFFFFFF;
  232. int bucket = hashCode % buckets.Length;
  233. int last = -1;
  234. for (int i = buckets[bucket] - 1; i >= 0; last = i, i = slots[i].next) {
  235. if (slots[i].hashCode == hashCode && comparer.Equals(slots[i].key, key)) {
  236. if (last < 0) {
  237. buckets[bucket] = slots[i].next + 1;
  238. }
  239. else {
  240. slots[last].next = slots[i].next;
  241. }
  242. slots[i].hashCode = -1;
  243. slots[i].value = default(T);
  244. slots[i].next = freeList;
  245. freeList = i;
  246. return true;
  247. }
  248. }
  249. return false;
  250. }
  251. internal override object Find(object[] keyValues) {
  252. K key;
  253. if (this.keyManager.TryCreateKeyFromValues(keyValues, out key)) {
  254. T value = default(T);
  255. if (Find(key, ref value, false))
  256. return value;
  257. }
  258. return null;
  259. }
  260. internal override object FindLike(object instance) {
  261. T value = (T)instance;
  262. K key = this.keyManager.CreateKeyFromInstance(value);
  263. if (Find(key, ref value, false))
  264. return value;
  265. return null;
  266. }
  267. bool Find(K key, ref T value, bool add) {
  268. int hashCode = comparer.GetHashCode(key) & 0x7FFFFFFF;
  269. for (int i = buckets[hashCode % buckets.Length] - 1; i >= 0; i = slots[i].next) {
  270. if (slots[i].hashCode == hashCode && comparer.Equals(slots[i].key, key)) {
  271. value = slots[i].value;
  272. return true;
  273. }
  274. }
  275. if (add) {
  276. int index;
  277. if (freeList >= 0) {
  278. index = freeList;
  279. freeList = slots[index].next;
  280. }
  281. else {
  282. if (count == slots.Length) Resize();
  283. index = count;
  284. count++;
  285. }
  286. int bucket = hashCode % buckets.Length;
  287. slots[index].hashCode = hashCode;
  288. slots[index].key = key;
  289. slots[index].value = value;
  290. slots[index].next = buckets[bucket] - 1;
  291. buckets[bucket] = index + 1;
  292. }
  293. return false;
  294. }
  295. void Resize() {
  296. int newSize = checked(count * 2 + 1);
  297. int[] newBuckets = new int[newSize];
  298. Slot[] newSlots = new Slot[newSize];
  299. Array.Copy(slots, 0, newSlots, 0, count);
  300. for (int i = 0; i < count; i++) {
  301. int bucket = newSlots[i].hashCode % newSize;
  302. newSlots[i].next = newBuckets[bucket] - 1;
  303. newBuckets[bucket] = i + 1;
  304. }
  305. buckets = newBuckets;
  306. slots = newSlots;
  307. }
  308. internal struct Slot {
  309. internal int hashCode;
  310. internal K key;
  311. internal T value;
  312. internal int next;
  313. }
  314. }
  315. #endregion
  316. }
  317. /// <summary>
  318. /// This is the noop implementation used when object tracking is disabled.
  319. /// </summary>
  320. class ReadOnlyIdentityManager : IdentityManager {
  321. internal ReadOnlyIdentityManager() { }
  322. internal override object InsertLookup(MetaType type, object instance) { return instance; }
  323. internal override bool RemoveLike(MetaType type, object instance) { return false; }
  324. internal override object Find(MetaType type, object[] keyValues) { return null; }
  325. internal override object FindLike(MetaType type, object instance) { return null; }
  326. }
  327. }
  328. }