2020-08-08 08:53:35 +02:00
|
|
|
|
namespace Giants.Services
|
|
|
|
|
{
|
2020-08-09 01:31:16 +02:00
|
|
|
|
using System;
|
2020-08-08 08:53:35 +02:00
|
|
|
|
using System.Collections.Concurrent;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
2020-08-09 01:31:16 +02:00
|
|
|
|
using System.Linq.Expressions;
|
2020-08-08 08:53:35 +02:00
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
public class InMemoryServerRegistryStore : IServerRegistryStore
|
|
|
|
|
{
|
2020-08-09 01:31:16 +02:00
|
|
|
|
private ConcurrentDictionary<string, ServerInfo> servers = new ConcurrentDictionary<string, ServerInfo>();
|
2020-08-08 08:53:35 +02:00
|
|
|
|
|
2020-08-09 01:31:16 +02:00
|
|
|
|
public Task<ServerInfo> GetServerInfo(string ipAddress)
|
2020-08-08 08:53:35 +02:00
|
|
|
|
{
|
2020-08-10 09:22:33 +02:00
|
|
|
|
if (this.servers.ContainsKey(ipAddress))
|
2020-08-08 08:53:35 +02:00
|
|
|
|
{
|
2020-08-10 09:22:33 +02:00
|
|
|
|
return Task.FromResult(this.servers[ipAddress]);
|
2020-08-08 08:53:35 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Task.FromResult((ServerInfo)null);
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-09 01:31:16 +02:00
|
|
|
|
public Task Initialize()
|
2020-08-08 08:53:35 +02:00
|
|
|
|
{
|
2020-08-09 01:31:16 +02:00
|
|
|
|
this.servers.Clear();
|
|
|
|
|
|
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Task UpsertServerInfo(ServerInfo serverInfo)
|
|
|
|
|
{
|
|
|
|
|
this.servers.TryAdd(serverInfo.HostIpAddress, serverInfo);
|
2020-08-08 08:53:35 +02:00
|
|
|
|
|
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
}
|
2020-08-09 02:26:41 +02:00
|
|
|
|
|
2020-08-16 10:47:25 +02:00
|
|
|
|
public Task<IEnumerable<ServerInfo>> GetServerInfos(
|
|
|
|
|
Expression<Func<ServerInfo, bool>> whereExpression = null,
|
|
|
|
|
bool includeExpired = false,
|
|
|
|
|
string partitionKey = null)
|
2020-08-09 02:26:41 +02:00
|
|
|
|
{
|
2020-08-10 07:11:10 +02:00
|
|
|
|
IQueryable<ServerInfo> serverInfoQuery = this.servers.Values.AsQueryable();
|
|
|
|
|
|
|
|
|
|
if (whereExpression != null)
|
|
|
|
|
{
|
|
|
|
|
serverInfoQuery = serverInfoQuery.Where(whereExpression);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Task.FromResult(serverInfoQuery.AsEnumerable());
|
2020-08-09 02:26:41 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-08-16 10:47:25 +02:00
|
|
|
|
public Task<IEnumerable<TSelect>> GetServerInfos<TSelect>(
|
|
|
|
|
Expression<Func<ServerInfo, TSelect>> selectExpression,
|
|
|
|
|
bool includeExpired = false,
|
|
|
|
|
Expression <Func<ServerInfo, bool>> whereExpression = null,
|
|
|
|
|
string partitionKey = null)
|
2020-08-09 02:26:41 +02:00
|
|
|
|
{
|
2020-08-10 07:11:10 +02:00
|
|
|
|
IQueryable<ServerInfo> serverInfoQuery = this.servers.Values.AsQueryable();
|
|
|
|
|
|
|
|
|
|
if (whereExpression != null)
|
|
|
|
|
{
|
|
|
|
|
serverInfoQuery = serverInfoQuery.Where(whereExpression);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Task.FromResult(serverInfoQuery.Select(selectExpression).AsEnumerable());
|
2020-08-09 02:26:41 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Task DeleteServers(IEnumerable<string> ids, string partitionKey = null)
|
|
|
|
|
{
|
|
|
|
|
foreach (string id in ids)
|
|
|
|
|
{
|
|
|
|
|
this.servers.TryRemove(id, out _);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
}
|
2020-08-10 07:11:10 +02:00
|
|
|
|
|
|
|
|
|
public Task DeleteServer(string id, string partitionKey = null)
|
|
|
|
|
{
|
|
|
|
|
this.servers.TryRemove(id, out ServerInfo _);
|
|
|
|
|
|
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
}
|
2020-08-08 08:53:35 +02:00
|
|
|
|
}
|
|
|
|
|
}
|