Skip to content

Commit

Permalink
Support paging for DOM instances
Browse files Browse the repository at this point in the history
  • Loading branch information
TomW-Skyline committed Nov 13, 2024
1 parent 55eeeae commit 1ba133c
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 12 deletions.
66 changes: 66 additions & 0 deletions Utils.DOM/UnitTesting/DomPagingHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
namespace Skyline.DataMiner.Utils.DOM.UnitTesting
{
using System;
using System.Collections.Generic;

using Skyline.DataMiner.Net.Messages.SLDataGateway;

internal class DomPagingHandler<T> : IDisposable
{
private readonly IEnumerator<T> _enumerator;

private bool _hasNext;
private T _nextRow;

private bool _isDisposed;

public DomPagingHandler(IEnumerable<T> items)
{
if (items == null)
{
throw new ArgumentNullException(nameof(items));
}

_enumerator = items.GetEnumerator();
TryMoveNext();
}

public PagingCookie Cookie { get; } = PagingCookie.CreateNew();

public List<T> GetNextPage(long pageSize, out bool isLast)
{
var page = new List<T>();

for (int i = 0; i < pageSize && _hasNext; i++)
{
page.Add(_nextRow);
TryMoveNext();
}

if (!_hasNext)
{
Dispose();
}

isLast = !_hasNext;
return page;
}

public void Dispose()
{
if (_isDisposed)
{
return;
}

_enumerator?.Dispose();
_isDisposed = true;
}

private void TryMoveNext()
{
_hasNext = _enumerator.MoveNext();
_nextRow = _hasNext ? _enumerator.Current : default;
}
}
}
72 changes: 60 additions & 12 deletions Utils.DOM/UnitTesting/DomSLNetMessageHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using Skyline.DataMiner.Net.Apps.DataMinerObjectModel;
using Skyline.DataMiner.Net.Apps.DataMinerObjectModel.CustomMessages;
using Skyline.DataMiner.Net.Messages;
using Skyline.DataMiner.Net.Messages.SLDataGateway;
using Skyline.DataMiner.Net.Sections;

/// <summary>
Expand All @@ -21,6 +22,8 @@ public class DomSLNetMessageHandler
private readonly ConcurrentDictionary<Guid, DomInstance> _instances = new ConcurrentDictionary<Guid, DomInstance>();
private readonly ConcurrentDictionary<Guid, DomBehaviorDefinition> _behaviorDefinitions = new ConcurrentDictionary<Guid, DomBehaviorDefinition>();

private readonly ConcurrentDictionary<PagingCookie, DomPagingHandler<DomInstance>> _pagingHandlers = new ConcurrentDictionary<PagingCookie, DomPagingHandler<DomInstance>>();

/// <summary>
/// Initializes a new instance of the <see cref="DomSLNetMessageHandler"/> class.
/// </summary>
Expand Down Expand Up @@ -201,24 +204,69 @@ public bool TryHandleMessage(DMSMessage message, out DMSMessage response)
#region Instances

case ManagerStoreReadRequest<DomInstance> request:
var instances = request.Query.ExecuteInMemory(_instances.Values).ToList();
response = new ManagerStoreCrudResponse<DomInstance>(instances);
return true;
{
var instances = request.Query.ExecuteInMemory(_instances.Values).ToList();
response = new ManagerStoreCrudResponse<DomInstance>(instances);
return true;
}

case ManagerStoreCreateRequest<DomInstance> request:
_instances[request.Object.ID.SafeId()] = request.Object;
response = new ManagerStoreCrudResponse<DomInstance>(request.Object);
return true;
{
_instances[request.Object.ID.SafeId()] = request.Object;
response = new ManagerStoreCrudResponse<DomInstance>(request.Object);
return true;
}

case ManagerStoreUpdateRequest<DomInstance> request:
_instances[request.Object.ID.SafeId()] = request.Object;
response = new ManagerStoreCrudResponse<DomInstance>(request.Object);
return true;
{
_instances[request.Object.ID.SafeId()] = request.Object;
response = new ManagerStoreCrudResponse<DomInstance>(request.Object);
return true;
}

case ManagerStoreDeleteRequest<DomInstance> request:
_instances.TryRemove(request.Object.ID.SafeId(), out _);
response = new ManagerStoreCrudResponse<DomInstance>(request.Object);
return true;
{
_instances.TryRemove(request.Object.ID.SafeId(), out _);
response = new ManagerStoreCrudResponse<DomInstance>(request.Object);
return true;
}

case ManagerStoreStartPagingRequest<DomInstance> request:
{
var instances = request.Filter.ExecuteInMemory(_instances.Values).ToList();
var pagingHandler = new DomPagingHandler<DomInstance>(instances);
_pagingHandlers.TryAdd(pagingHandler.Cookie, pagingHandler);

var nextPage = pagingHandler.GetNextPage(request.PreferredPageSize, out var isLast);

if (isLast)
{
_pagingHandlers.TryRemove(pagingHandler.Cookie, out pagingHandler);
pagingHandler.Dispose();
}

response = new ManagerStorePagingResponse<DomInstance>(nextPage, isLast, pagingHandler.Cookie);
return true;
}

case ManagerStoreNextPagingRequest<DomInstance> request:
{
if (!_pagingHandlers.TryGetValue(request.PagingCookie, out var pagingHandler))
{
throw new InvalidOperationException($"Invalid paging cookie: {request.PagingCookie}");
}

var nextPage = pagingHandler.GetNextPage(request.PreferredPageSize, out var isLast);

if (isLast)
{
_pagingHandlers.TryRemove(pagingHandler.Cookie, out pagingHandler);
pagingHandler.Dispose();
}

response = new ManagerStorePagingResponse<DomInstance>(nextPage, isLast, pagingHandler.Cookie);
return true;
}

#endregion

Expand Down

0 comments on commit 1ba133c

Please sign in to comment.