CacheItemPriorityQueue.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. //
  2. // System.Web.Caching.CacheItem
  3. //
  4. // Author(s):
  5. // Marek Habersack <[email protected]>
  6. //
  7. // (C) 2009 Novell, Inc (http://novell.com)
  8. //
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using System;
  30. using System.Text;
  31. namespace System.Web.Caching
  32. {
  33. sealed class CacheItemPriorityQueue
  34. {
  35. sealed class Node
  36. {
  37. public CacheItem Data;
  38. public Node Left;
  39. public Node Right;
  40. public Node Parent;
  41. public Node Next;
  42. public Node Prev;
  43. public CacheItem SwapData (CacheItem newData)
  44. {
  45. CacheItem ret = Data;
  46. Data = newData;
  47. return ret;
  48. }
  49. public Node (CacheItem data)
  50. {
  51. Data = data;
  52. }
  53. }
  54. Node root;
  55. Node lastAdded;
  56. Node firstParent;
  57. Node lastParent;
  58. public void Enqueue (CacheItem item)
  59. {
  60. if (item == null)
  61. return;
  62. Node node = new Node (item);
  63. if (root == null) {
  64. root = lastAdded = lastParent = firstParent = node;
  65. return;
  66. }
  67. if (lastParent.Left != null && lastParent.Right != null) {
  68. lastParent = lastParent.Next;
  69. if (lastParent == null) {
  70. lastParent = firstParent = firstParent.Left;
  71. lastAdded = null;
  72. }
  73. }
  74. node.Parent = lastParent;
  75. if (lastParent.Left == null)
  76. lastParent.Left = node;
  77. else
  78. lastParent.Right = node;
  79. if (lastAdded != null) {
  80. lastAdded.Next = node;
  81. node.Prev = lastAdded;
  82. }
  83. lastAdded = node;
  84. BubbleUp (node);
  85. }
  86. public CacheItem Dequeue ()
  87. {
  88. if (root == null)
  89. return null;
  90. CacheItem ret;
  91. if (root.Left == null && root.Right == null) {
  92. ret = root.Data;
  93. root = null;
  94. if (ret.Disabled)
  95. return null;
  96. return ret;
  97. }
  98. ret = root.Data;
  99. do {
  100. Node last = lastAdded;
  101. if (last == null)
  102. return null;
  103. if (last.Prev == null) {
  104. Node parent = last.Parent;
  105. while (true) {
  106. if (parent.Next == null)
  107. break;
  108. parent = parent.Next;
  109. }
  110. lastAdded = parent;
  111. } else {
  112. lastAdded = last.Prev;
  113. lastAdded.Next = null;
  114. }
  115. if (last.Parent.Left == last)
  116. last.Parent.Left = null;
  117. else
  118. last.Parent.Right = null;
  119. root.Data = last.Data;
  120. BubbleDown (root);
  121. } while (ret.Disabled);
  122. return ret;
  123. }
  124. public CacheItem Peek ()
  125. {
  126. if (root == null)
  127. return null;
  128. return root.Data;
  129. }
  130. void BubbleDown (Node item)
  131. {
  132. if (item == null || (item.Left == null && item.Right == null))
  133. return;
  134. if (item.Left == null)
  135. SwapBubbleDown (item, item.Right);
  136. else if (item.Right == null)
  137. SwapBubbleDown (item, item.Left);
  138. else {
  139. if (item.Left.Data.ExpiresAt < item.Right.Data.ExpiresAt)
  140. SwapBubbleDown (item, item.Left);
  141. else
  142. SwapBubbleDown (item, item.Right);
  143. }
  144. }
  145. void SwapBubbleDown (Node item, Node otherItem)
  146. {
  147. if (otherItem.Data.ExpiresAt < item.Data.ExpiresAt) {
  148. item.Data = otherItem.SwapData (item.Data);
  149. BubbleDown (otherItem);
  150. }
  151. }
  152. void BubbleUp (Node item)
  153. {
  154. if (item == null || item.Data == null)
  155. return;
  156. Node parent = item.Parent;
  157. if (parent == null)
  158. return;
  159. if (item.Data.ExpiresAt > parent.Data.ExpiresAt)
  160. return;
  161. item.Data = parent.SwapData (item.Data);
  162. BubbleUp (parent);
  163. }
  164. public string GetDotScript ()
  165. {
  166. StringBuilder sb = new StringBuilder ();
  167. sb.Append ("graph CacheItemPriorityQueue {\n");
  168. sb.Append ("\tnode [color=lightblue, style=filled];\n");
  169. if (root != null) {
  170. if (root.Left == null && root.Right == null)
  171. sb.AppendFormat ("\t{0};", root.Data.ExpiresAt);
  172. else
  173. TraverseTree (sb, root);
  174. }
  175. sb.Append ("}\n");
  176. return sb.ToString ();
  177. }
  178. void TraverseTree (StringBuilder sb, Node root)
  179. {
  180. if (root.Left != null) {
  181. sb.AppendFormat ("\t{0} -- {1};\n", root.Data.ExpiresAt, root.Left.Data.ExpiresAt);
  182. TraverseTree (sb, root.Left);
  183. }
  184. if (root.Right != null) {
  185. sb.AppendFormat ("\t{0} -- {1};\n", root.Data.ExpiresAt, root.Right.Data.ExpiresAt);
  186. TraverseTree (sb, root.Right);
  187. }
  188. }
  189. }
  190. }