File size: 11,667 Bytes
8c763fb | 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 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 | // Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System;
using System.Collections.Generic;
using System.Diagnostics;
namespace Microsoft.PowerShell.Commands.Internal.Format
{
/// <summary>
/// Queue to provide sliding window capabilities for auto size functionality
/// It provides caching capabilities (either the first N objects in a group
/// or all the objects in a group)
/// </summary>
internal sealed class OutputGroupQueue
{
/// <summary>
/// Create a grouping cache.
/// </summary>
/// <param name="callBack">Notification callback to be called when the desired number of objects is reached.</param>
/// <param name="objectCount">Max number of objects to be cached.</param>
internal OutputGroupQueue(FormattedObjectsCache.ProcessCachedGroupNotification callBack, int objectCount)
{
_notificationCallBack = callBack;
_objectCount = objectCount;
}
/// <summary>
/// Create a time-bounded grouping cache.
/// </summary>
/// <param name="callBack">Notification callback to be called when the desired number of objects is reached.</param>
/// <param name="groupingDuration">Max amount of time to cache of objects.</param>
internal OutputGroupQueue(FormattedObjectsCache.ProcessCachedGroupNotification callBack, TimeSpan groupingDuration)
{
_notificationCallBack = callBack;
_groupingDuration = groupingDuration;
}
/// <summary>
/// Add an object to the cache.
/// </summary>
/// <param name="o">Object to add.</param>
/// <returns>Objects the cache needs to return. It can be null.</returns>
internal List<PacketInfoData> Add(PacketInfoData o)
{
if (o is FormatStartData fsd)
{
// just cache the reference (used during the notification call)
_formatStartData = fsd;
}
UpdateObjectCount(o);
// STATE TRANSITION: we are not processing and we start
if (!_processingGroup && (o is GroupStartData))
{
// just set the flag and start caching
_processingGroup = true;
_currentObjectCount = 0;
if (_groupingDuration > TimeSpan.MinValue)
{
_groupingTimer = Stopwatch.StartNew();
}
_queue.Enqueue(o);
return null;
}
// STATE TRANSITION: we are processing and we stop
if (_processingGroup &&
((o is GroupEndData) ||
(_objectCount > 0) && (_currentObjectCount >= _objectCount)) ||
((_groupingTimer != null) && (_groupingTimer.Elapsed > _groupingDuration))
)
{
// reset the object count
_currentObjectCount = 0;
if (_groupingTimer != null)
{
_groupingTimer.Stop();
_groupingTimer = null;
}
// add object to queue, to be picked up
_queue.Enqueue(o);
// we are at the end of a group, drain the queue
Notify();
_processingGroup = false;
List<PacketInfoData> retVal = new List<PacketInfoData>();
while (_queue.Count > 0)
{
retVal.Add(_queue.Dequeue());
}
return retVal;
}
// NO STATE TRANSITION: check the state we are in
if (_processingGroup)
{
// we are in the caching state
_queue.Enqueue(o);
return null;
}
// we are not processing, so just return it
List<PacketInfoData> ret = new List<PacketInfoData>();
ret.Add(o);
return ret;
}
private void UpdateObjectCount(PacketInfoData o)
{
// add only of it's not a control message
// and it's not out of band
if (o is FormatEntryData fed && !fed.outOfBand)
{
_currentObjectCount++;
}
}
private void Notify()
{
if (_notificationCallBack == null)
return;
// filter out the out of band data, since they do not participate in the
// auto resize algorithm
List<PacketInfoData> validObjects = new List<PacketInfoData>();
foreach (PacketInfoData x in _queue)
{
if (x is FormatEntryData fed && fed.outOfBand)
continue;
validObjects.Add(x);
}
_notificationCallBack(_formatStartData, validObjects);
}
/// <summary>
/// Remove a single object from the queue.
/// </summary>
/// <returns>Object retrieved, null if queue is empty.</returns>
internal PacketInfoData Dequeue()
{
if (_queue.Count == 0)
return null;
return _queue.Dequeue();
}
/// <summary>
/// Queue to store the currently cached objects.
/// </summary>
private readonly Queue<PacketInfoData> _queue = new Queue<PacketInfoData>();
/// <summary>
/// Number of objects to compute the best fit.
/// Zero: all the objects
/// a positive number N: use the first N.
/// </summary>
private readonly int _objectCount = 0;
/// <summary>
/// Maximum amount of time for record processing to compute the best fit.
/// MaxValue: all the objects.
/// A positive timespan: use all objects that have been processed within the timeframe.
/// </summary>
private readonly TimeSpan _groupingDuration = TimeSpan.MinValue;
private Stopwatch _groupingTimer = null;
/// <summary>
/// Notification callback to be called when we have accumulated enough
/// data to compute a hint.
/// </summary>
private readonly FormattedObjectsCache.ProcessCachedGroupNotification _notificationCallBack = null;
/// <summary>
/// Reference kept to be used during notification.
/// </summary>
private FormatStartData _formatStartData = null;
/// <summary>
/// State flag to signal we are queuing.
/// </summary>
private bool _processingGroup = false;
/// <summary>
/// Current object count.
/// </summary>
private int _currentObjectCount = 0;
}
/// <summary>
/// Facade class managing the front end and the autosize cache.
/// </summary>
internal sealed class FormattedObjectsCache
{
/// <summary>
/// Delegate to allow notifications when the autosize queue is about to be drained.
/// </summary>
/// <param name="formatStartData">Current Fs control message.</param>
/// <param name="objects">Enumeration of PacketInfoData objects.</param>
internal delegate void ProcessCachedGroupNotification(FormatStartData formatStartData, List<PacketInfoData> objects);
/// <summary>
/// Decide right away if we need a front end cache (e.g. printing)
/// </summary>
/// <param name="cacheFrontEnd">If true, create a front end cache object.</param>
internal FormattedObjectsCache(bool cacheFrontEnd)
{
if (cacheFrontEnd)
_frontEndQueue = new Queue<PacketInfoData>();
}
/// <summary>
/// If needed, add a back end autosize (grouping) cache.
/// </summary>
/// <param name="callBack">Notification callback to be called when the desired number of objects is reached.</param>
/// <param name="objectCount">Max number of objects to be cached.</param>
internal void EnableGroupCaching(ProcessCachedGroupNotification callBack, int objectCount)
{
if (callBack != null)
_groupQueue = new OutputGroupQueue(callBack, objectCount);
}
/// <summary>
/// If needed, add a back end autosize (grouping) cache.
/// </summary>
/// <param name="callBack">Notification callback to be called when the desired number of objects is reached.</param>
/// <param name="groupingDuration">Max amount of time to cache of objects.</param>
internal void EnableGroupCaching(ProcessCachedGroupNotification callBack, TimeSpan groupingDuration)
{
if (callBack != null)
_groupQueue = new OutputGroupQueue(callBack, groupingDuration);
}
/// <summary>
/// Add an object to the cache. the behavior depends on the object added, the
/// objects already in the cache and the cache settings.
/// </summary>
/// <param name="o">Object to add.</param>
/// <returns>List of objects the cache is flushing.</returns>
internal List<PacketInfoData> Add(PacketInfoData o)
{
// if neither there, pass thru
if (_frontEndQueue == null && _groupQueue == null)
{
List<PacketInfoData> retVal = new List<PacketInfoData>();
retVal.Add(o);
return retVal;
}
// if front present, add to front
if (_frontEndQueue != null)
{
_frontEndQueue.Enqueue(o);
return null;
}
// if back only, add to back
return _groupQueue.Add(o);
}
/// <summary>
/// Remove all the objects from the cache.
/// </summary>
/// <returns>All the objects that were in the cache.</returns>
internal List<PacketInfoData> Drain()
{
// if neither there,we did not cache at all
if (_frontEndQueue == null && _groupQueue == null)
{
return null;
}
List<PacketInfoData> retVal = new List<PacketInfoData>();
if (_frontEndQueue != null)
{
if (_groupQueue == null)
{
// drain the front queue and return the data
while (_frontEndQueue.Count > 0)
retVal.Add(_frontEndQueue.Dequeue());
return retVal;
}
// move from the front to the back queue
while (_frontEndQueue.Count > 0)
{
List<PacketInfoData> groupQueueOut = _groupQueue.Add(_frontEndQueue.Dequeue());
if (groupQueueOut != null)
foreach (PacketInfoData x in groupQueueOut)
retVal.Add(x);
}
}
// drain the back queue
while (true)
{
PacketInfoData obj = _groupQueue.Dequeue();
if (obj == null)
break;
retVal.Add(obj);
}
return retVal;
}
/// <summary>
/// Front end queue (if present, cache ALL, if not, bypass)
/// </summary>
private readonly Queue<PacketInfoData> _frontEndQueue;
/// <summary>
/// Back end grouping queue.
/// </summary>
private OutputGroupQueue _groupQueue = null;
}
}
|