CacheItemPriorityQueue.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. //
  2. // Author(s):
  3. // Marek Habersack <[email protected]>
  4. //
  5. // (C) 2009-2010 Novell, Inc (http://novell.com)
  6. //
  7. //
  8. // Permission is hereby granted, free of charge, to any person obtaining
  9. // a copy of this software and associated documentation files (the
  10. // "Software"), to deal in the Software without restriction, including
  11. // without limitation the rights to use, copy, modify, merge, publish,
  12. // distribute, sublicense, and/or sell copies of the Software, and to
  13. // permit persons to whom the Software is furnished to do so, subject to
  14. // the following conditions:
  15. //
  16. // The above copyright notice and this permission notice shall be
  17. // included in all copies or substantial portions of the Software.
  18. //
  19. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  20. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  21. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  22. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  23. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  24. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  25. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  26. //
  27. using System;
  28. using System.Collections;
  29. using System.Collections.Generic;
  30. using System.IO;
  31. using System.Text;
  32. using System.Threading;
  33. using System.Xml;
  34. namespace System.Web.Caching
  35. {
  36. sealed partial class CacheItemPriorityQueue
  37. {
  38. const int INITIAL_HEAP_SIZE = 32;
  39. const int HEAP_RESIZE_THRESHOLD = 8192;
  40. CacheItem[] heap;
  41. int heapSize = 0;
  42. int heapCount = 0;
  43. // See comment for the cacheLock field at top of System.Web.Caching/Cache.cs
  44. ReaderWriterLockSlim queueLock;
  45. public int Count {
  46. get { return heapCount; }
  47. }
  48. public int Size {
  49. get { return heapSize; }
  50. }
  51. public CacheItem[] Heap {
  52. get { return heap; }
  53. }
  54. public CacheItemPriorityQueue ()
  55. {
  56. queueLock = new ReaderWriterLockSlim ();
  57. InitDebugMode ();
  58. }
  59. void ResizeHeap (int newSize)
  60. {
  61. CacheItem[] oldHeap = heap;
  62. Array.Resize <CacheItem> (ref heap, newSize);
  63. heapSize = newSize;
  64. // TODO: The code helps the GC in case the array is pinned. In such instance clearing
  65. // the old array will release references to the CacheItems stored in there. If the
  66. // array is not pinned, otoh, this is a waste of time.
  67. // Currently we don't know if the array is pinned or not so it's safer to always clear it.
  68. // However when we have more precise stack scanning the code should be
  69. // revisited.
  70. if (oldHeap != null) {
  71. ((IList)oldHeap).Clear ();
  72. oldHeap = null;
  73. }
  74. }
  75. CacheItem[] GetHeapWithGrow ()
  76. {
  77. if (heap == null) {
  78. heap = new CacheItem [INITIAL_HEAP_SIZE];
  79. heapSize = INITIAL_HEAP_SIZE;
  80. heapCount = 0;
  81. return heap;
  82. }
  83. if (heapCount >= heapSize)
  84. ResizeHeap (heapSize <<= 1);
  85. return heap;
  86. }
  87. CacheItem[] GetHeapWithShrink ()
  88. {
  89. if (heap == null)
  90. return null;
  91. if (heapSize > HEAP_RESIZE_THRESHOLD) {
  92. int halfTheSize = heapSize >> 1;
  93. if (heapCount < halfTheSize)
  94. ResizeHeap (halfTheSize + (heapCount / 3));
  95. }
  96. return heap;
  97. }
  98. public void Enqueue (CacheItem item)
  99. {
  100. if (item == null)
  101. return;
  102. CacheItem[] heap;
  103. try {
  104. queueLock.EnterWriteLock ();
  105. heap = GetHeapWithGrow ();
  106. heap [heapCount] = item;
  107. if (heapCount == 0)
  108. item.PriorityQueueIndex = 0;
  109. BubbleUp (heap, heapCount++);
  110. AddSequenceEntry (item, EDSequenceEntryType.Enqueue);
  111. } finally {
  112. // See comment at the top of the file, above queueLock declaration
  113. queueLock.ExitWriteLock ();
  114. }
  115. }
  116. public CacheItem Dequeue ()
  117. {
  118. CacheItem ret = null;
  119. CacheItem[] heap;
  120. int index;
  121. try {
  122. queueLock.EnterWriteLock ();
  123. heap = GetHeapWithShrink ();
  124. if (heap == null || heapCount == 0)
  125. return null;
  126. ret = heap [0];
  127. index = --heapCount;
  128. heap [0] = heap [index];
  129. heap [index] = null;
  130. if (heapCount > 0)
  131. BubbleDown (heap, 0);
  132. AddSequenceEntry (ret, EDSequenceEntryType.Dequeue);
  133. return ret;
  134. } finally {
  135. // See comment at the top of the file, above queueLock declaration
  136. queueLock.ExitWriteLock ();
  137. }
  138. }
  139. public bool Update (CacheItem item)
  140. {
  141. if (item == null || item.PriorityQueueIndex <= 0 || item.PriorityQueueIndex >= heapCount - 1)
  142. return false;
  143. try {
  144. queueLock.EnterWriteLock ();
  145. CacheItem stored = heap [item.PriorityQueueIndex];
  146. if (stored == null ||
  147. String.Compare (stored.Key, item.Key, StringComparison.Ordinal) != 0
  148. #if DEBUG
  149. || stored.Guid != item.Guid
  150. #endif
  151. )
  152. return false;
  153. int oldIndex = item.PriorityQueueIndex;
  154. int index = BubbleUp (heap, oldIndex);
  155. if (index > -1 && index >= oldIndex)
  156. BubbleDown (heap, index);
  157. AddSequenceEntry (item, EDSequenceEntryType.Update);
  158. } finally {
  159. queueLock.ExitWriteLock ();
  160. }
  161. return true;
  162. }
  163. public CacheItem Peek ()
  164. {
  165. CacheItem ret;
  166. try {
  167. queueLock.EnterReadLock ();
  168. if (heap == null || heapCount == 0)
  169. return null;
  170. ret = heap [0];
  171. AddSequenceEntry (ret, EDSequenceEntryType.Peek);
  172. return ret;
  173. } finally {
  174. // See comment at the top of the file, above queueLock declaration
  175. queueLock.ExitReadLock ();
  176. }
  177. }
  178. int BubbleDown (CacheItem[] heap, int startIndex)
  179. {
  180. int index = startIndex;
  181. int left = startIndex + 1;
  182. int right = startIndex + 2;
  183. CacheItem item = heap [index], tmpItem;
  184. int selected = (right < heapCount && heap [right].ExpiresAt < heap [left].ExpiresAt) ? 2 : 1;
  185. do {
  186. selected = index;
  187. left = (index << 1) + 1;
  188. right = left + 1;
  189. if (heapCount > left && heap [index].ExpiresAt > heap [left].ExpiresAt)
  190. index = left;
  191. if (heapCount > right && heap [index].ExpiresAt > heap [right].ExpiresAt)
  192. index = right;
  193. if (index == selected)
  194. break;
  195. tmpItem = heap [index];
  196. heap [index] = heap [selected];
  197. heap [index].PriorityQueueIndex = index;
  198. heap [selected] = tmpItem;
  199. tmpItem.PriorityQueueIndex = selected;
  200. } while (true);
  201. item.PriorityQueueIndex = index;
  202. return index;
  203. }
  204. int BubbleUp (CacheItem[] heap, int startIndex)
  205. {
  206. int index, parentIndex;
  207. CacheItem parent, item;
  208. if (heapCount <= 1)
  209. return -1;
  210. int maxIndex = heapCount - 1;
  211. if (startIndex < 0 || startIndex > maxIndex)
  212. return -1;
  213. index = startIndex;
  214. parentIndex = (index - 1) >> 1;
  215. item = heap [index];
  216. while (index > 0) {
  217. parent = heap [parentIndex];
  218. if (heap [index].ExpiresAt >= parent.ExpiresAt)
  219. break;
  220. heap [index] = parent;
  221. parent.PriorityQueueIndex = index;
  222. index = parentIndex;
  223. parentIndex = (index - 1) >> 1;
  224. }
  225. heap [index] = item;
  226. item.PriorityQueueIndex = index;
  227. return index;
  228. }
  229. }
  230. }