File: CacheItemPriorityQueue.cs

package info (click to toggle)
mono 6.14.1%2Bds2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,282,732 kB
  • sloc: cs: 11,182,461; xml: 2,850,281; ansic: 699,123; cpp: 122,919; perl: 58,604; javascript: 30,841; asm: 21,845; makefile: 19,602; sh: 10,973; python: 4,772; pascal: 925; sql: 859; sed: 16; php: 1
file content (275 lines) | stat: -rw-r--r-- 6,874 bytes parent folder | download | duplicates (9)
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
//
// Author(s):
//  Marek Habersack <mhabersack@novell.com>
//
// (C) 2009-2010 Novell, Inc (http://novell.com)
//
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Threading;
using System.Xml;

namespace System.Web.Caching
{
	sealed partial class CacheItemPriorityQueue
	{
		const int INITIAL_HEAP_SIZE = 32;
		const int HEAP_RESIZE_THRESHOLD = 8192;
		
		CacheItem[] heap;
		int heapSize = 0;
		int heapCount = 0;

		// See comment for the cacheLock field at top of System.Web.Caching/Cache.cs
		ReaderWriterLockSlim queueLock;

		public int Count {
			get { return heapCount; }
		}

		public int Size {
			get { return heapSize; }
		}

		public CacheItem[] Heap {
			get { return heap; }
		}
		
		public CacheItemPriorityQueue ()
		{
			queueLock = new ReaderWriterLockSlim ();
			InitDebugMode ();
		}

		void ResizeHeap (int newSize)
		{
			CacheItem[] oldHeap = heap;
			Array.Resize <CacheItem> (ref heap, newSize);
			heapSize = newSize;
			
			// TODO: The code helps the GC in case the array is pinned. In such instance clearing
			// the old array will release references to the CacheItems stored in there. If the
			// array is not pinned, otoh, this is a waste of time.
			// Currently we don't know if the array is pinned or not so it's safer to always clear it.
			// However when we have more precise stack scanning the code should be
			// revisited.
			if (oldHeap != null) {
				((IList)oldHeap).Clear ();
				oldHeap = null;
			}
		}
		
		CacheItem[] GetHeapWithGrow ()
		{
			if (heap == null) {
				heap = new CacheItem [INITIAL_HEAP_SIZE];
				heapSize = INITIAL_HEAP_SIZE;
				heapCount = 0;
				return heap;
			}

			if (heapCount >= heapSize)
				ResizeHeap (heapSize <<= 1);

			return heap;
		}

		CacheItem[] GetHeapWithShrink ()
		{
			if (heap == null)
				return null;

			if (heapSize > HEAP_RESIZE_THRESHOLD) {
				int halfTheSize = heapSize >> 1;

				if (heapCount < halfTheSize)
					ResizeHeap (halfTheSize + (heapCount / 3));
			}
			
			return heap;
		}
		
		public void Enqueue (CacheItem item)
		{
			if (item == null)
				return;

			CacheItem[] heap;
			
			try {
				queueLock.EnterWriteLock ();
				heap = GetHeapWithGrow ();
				heap [heapCount] = item;
				if (heapCount == 0)
					item.PriorityQueueIndex = 0;
				BubbleUp (heap, heapCount++);
				AddSequenceEntry (item, EDSequenceEntryType.Enqueue);
			} finally {
				// See comment at the top of the file, above queueLock declaration
				queueLock.ExitWriteLock ();
			}
		}

		public CacheItem Dequeue ()
		{
			CacheItem ret = null;
			CacheItem[] heap;
			int index;
			
			try {
				queueLock.EnterWriteLock ();
				heap = GetHeapWithShrink ();
				if (heap == null || heapCount == 0)
					return null;

				ret = heap [0];
				index = --heapCount;
				heap [0] = heap [index];
				heap [index] = null;
				
				if (heapCount > 0)
					BubbleDown (heap, 0);

				AddSequenceEntry (ret, EDSequenceEntryType.Dequeue);
				return ret;
			} finally {
				// See comment at the top of the file, above queueLock declaration
				queueLock.ExitWriteLock ();
			}
		}

		public bool Update (CacheItem item)
		{
			if (item == null || item.PriorityQueueIndex <= 0 || item.PriorityQueueIndex >= heapCount - 1)
				return false;

			try {
				queueLock.EnterWriteLock ();
				CacheItem stored = heap [item.PriorityQueueIndex];
				if (stored == null ||
				    String.Compare (stored.Key, item.Key, StringComparison.Ordinal) != 0
#if DEBUG
				    || stored.Guid != item.Guid
#endif
				)
					return false;

				int oldIndex = item.PriorityQueueIndex;
				int index = BubbleUp (heap, oldIndex);
				if (index > -1 && index >= oldIndex) 
					BubbleDown (heap, index);

				AddSequenceEntry (item, EDSequenceEntryType.Update);
			} finally {
				queueLock.ExitWriteLock ();
			}
			
			return true;
		}
		
		public CacheItem Peek ()
		{
			CacheItem ret;
			
			try {
				queueLock.EnterReadLock ();
				if (heap == null || heapCount == 0)
					return null;

				ret = heap [0];
				AddSequenceEntry (ret, EDSequenceEntryType.Peek);
				
				return ret;
			} finally {
				// See comment at the top of the file, above queueLock declaration
				queueLock.ExitReadLock ();
			}
		}
		
		int BubbleDown (CacheItem[] heap, int startIndex)
		{
			int index = startIndex;
			int left = startIndex + 1;
			int right = startIndex + 2;
			CacheItem item = heap [index], tmpItem;

			int selected = (right < heapCount && heap [right].ExpiresAt < heap [left].ExpiresAt) ? 2 : 1;

			do {
				selected = index;
				left = (index << 1) + 1;
				right = left + 1;
				if (heapCount > left && heap [index].ExpiresAt > heap [left].ExpiresAt)
					index = left;
				if (heapCount > right && heap [index].ExpiresAt > heap [right].ExpiresAt)
					index = right;
				if (index == selected)
					break;
				tmpItem = heap [index];
				heap [index] = heap [selected];
				heap [index].PriorityQueueIndex = index;
				heap [selected] = tmpItem;
				tmpItem.PriorityQueueIndex = selected;
			} while (true);

			item.PriorityQueueIndex = index;
			return index;
		}
		
		int BubbleUp (CacheItem[] heap, int startIndex)
		{
			int index, parentIndex;
			CacheItem parent, item;

			if (heapCount <= 1)
				return -1;
			
			int maxIndex = heapCount - 1;
			if (startIndex < 0 || startIndex > maxIndex)
				return -1;
			
			index = startIndex;
			parentIndex = (index - 1) >> 1;

			item = heap [index];
			while (index > 0) {
				parent = heap [parentIndex];
				if (heap [index].ExpiresAt >= parent.ExpiresAt)
					break;
				
				heap [index] = parent;
				parent.PriorityQueueIndex = index;
				index = parentIndex;
				parentIndex = (index - 1) >> 1;
			}

			heap [index] = item;
			item.PriorityQueueIndex = index;

			return index;
		}
	}
}