Skip to content

Commit

Permalink
Add LiteDbConnectionPool, increase version to v1.0.23
Browse files Browse the repository at this point in the history
  • Loading branch information
matsakiv committed Apr 26, 2023
1 parent e0e702a commit a963a6b
Show file tree
Hide file tree
Showing 11 changed files with 163 additions and 38 deletions.
2 changes: 1 addition & 1 deletion Beacon.Sdk/Beacon.Sdk.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<Authors>Mikhail Tatarenko</Authors>
<Product>Beacon.Sdk</Product>
<Description>Beacon .NET SDK for Tezos wallet / dApps developers.</Description>
<Version>1.0.22</Version>
<Version>1.0.23</Version>
<Copyright>Copyright © Baking Bad 2019-2022</Copyright>
<Nullable>enable</Nullable>
<TargetFramework>netstandard2.1</TargetFramework>
Expand Down
2 changes: 1 addition & 1 deletion Beacon.Sdk/BeaconClients/BeaconClientServiceExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ private static IServiceCollection AddBeaconClient(this IServiceCollection servic
});

services.AddSingleton<ISessionKeyPairRepository, InMemorySessionKeyPairRepository>();
services.AddSingleton<ILiteDbConnectionPool, LiteDbConnectionPool>();
services.AddSingleton<IPeerRepository, LiteDbPeerRepository>();
services.AddSingleton<IP2PPeerRoomRepository, LiteDbP2PPeerRoomRepository>();
services.AddSingleton<ISeedRepository, LiteDbSeedRepository>();
Expand All @@ -79,7 +80,6 @@ private static IServiceCollection AddBeaconClient(this IServiceCollection servic

#endregion


#region Domain

services.AddSingleton<KeyPairService>();
Expand Down
11 changes: 11 additions & 0 deletions Beacon.Sdk/Core/Infrastructure/ILiteDbConnectionPool.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using LiteDB;

namespace Beacon.Sdk.Core.Infrastructure
{
public interface ILiteDbConnectionPool
{
void CloseAllConnections();
void CloseConnection(string fileName);
ILiteDatabase OpenConnection(ConnectionString connectionString, BsonMapper? mapper = null);
}
}
91 changes: 91 additions & 0 deletions Beacon.Sdk/Core/Infrastructure/LiteDbConnectionPool.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
using System;
using System.Collections.Concurrent;
using System.IO;

using LiteDB;

namespace Beacon.Sdk.Core.Infrastructure
{
public class LiteDbConnectionPool : IDisposable, ILiteDbConnectionPool
{
private readonly ConcurrentDictionary<string, LiteDatabase> _dbs;
private bool _disposed;

public LiteDbConnectionPool()
{
_dbs = new ConcurrentDictionary<string, LiteDatabase>();
}

public ILiteDatabase OpenConnection(ConnectionString connectionString, BsonMapper? mapper = null)
{
var fullPath = Path.GetFullPath(connectionString.Filename);

LiteDatabase? newDb = null;

try
{
var db = _dbs.GetOrAdd(fullPath, path =>
{
newDb = new LiteDatabase(connectionString, mapper);
return newDb;
});

if (db != newDb && newDb != null)
{
newDb.Dispose();
}

return db;
}
catch (Exception ex)
{
if (!_dbs.TryGetValue(fullPath, out var db))
throw ex; // connection not found after another attempt to get => throw exception when creating connection

return db;
}
}

public void CloseConnection(string fileName)
{
var fullPath = Path.GetFullPath(fileName);

if (_dbs.TryRemove(fullPath, out var db))
{
db.Dispose();
}
}

public void CloseAllConnections()
{
var keysSnapshot = _dbs.Keys;

foreach (var fullFilePath in keysSnapshot)
{
if (_dbs.TryRemove(fullFilePath, out var db))
{
db.Dispose();
}
}
}

protected virtual void Dispose(bool disposing)
{
if (!_disposed)
{
if (disposing)
{
CloseAllConnections();
}

_disposed = true;
}
}

public void Dispose()
{
Dispose(disposing: true);
GC.SuppressFinalize(this);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ namespace Beacon.Sdk.Core.Infrastructure.Repositories
public abstract class BaseLiteDbRepository<T>
{
private readonly ILogger<BaseLiteDbRepository<T>> _logger;
private readonly LiteDatabase _db;
private readonly ILiteDatabase _db;

protected BaseLiteDbRepository(ILogger<BaseLiteDbRepository<T>> logger, RepositorySettings settings)
protected BaseLiteDbRepository(ILiteDbConnectionPool connectionPool, ILogger<BaseLiteDbRepository<T>> logger, RepositorySettings settings)
{
_logger = logger;
_db = new LiteDatabase(settings.ConnectionString);
_db = connectionPool.OpenConnection(new ConnectionString(settings.ConnectionString));
}

protected Task InConnectionAction(string collectionName, Action<ILiteCollection<T>> func)
Expand Down Expand Up @@ -46,7 +46,7 @@ protected Task<T> InConnection(string collectionName, Func<ILiteCollection<T>, T
}
}

protected Task InConnection(string collectionName, Action<LiteDatabase, ILiteCollection<T>> func)
protected Task InConnection(string collectionName, Action<ILiteDatabase, ILiteCollection<T>> func)
{
try
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,23 @@
using System;
using System.Linq;
using System.Threading.Tasks;

using LiteDB;
using Microsoft.Extensions.Logging;

namespace Beacon.Sdk.Core.Infrastructure.Repositories
{
using System.Linq;
using System.Threading.Tasks;
using Beacon;
using Microsoft.Extensions.Logging;

public class LiteDbAppMetadataRepository : BaseLiteDbRepository<AppMetadata>, IAppMetadataRepository
{
private const string CollectionName = "AppMetadata";

public LiteDbAppMetadataRepository(ILogger<LiteDbAppMetadataRepository> logger, RepositorySettings settings)
: base(logger, settings)
public LiteDbAppMetadataRepository(
ILiteDbConnectionPool connectionPool,
ILogger<LiteDbAppMetadataRepository> logger,
RepositorySettings settings)
: base(connectionPool, logger, settings)
{
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

using LiteDB;
using Microsoft.Extensions.Logging;

namespace Beacon.Sdk.Core.Infrastructure.Repositories
{
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Domain.Entities;
using Domain.Interfaces;
using Microsoft.Extensions.Logging;

public class LiteDbMatrixSyncRepository : BaseLiteDbRepository<MatrixSyncEntity>, IMatrixSyncRepository
{
private const string CollectionName = "MatrixSync";

public LiteDbMatrixSyncRepository(ILogger<LiteDbMatrixSyncRepository> logger, RepositorySettings settings)
: base(logger, settings)
public LiteDbMatrixSyncRepository(
ILiteDbConnectionPool connectionPool,
ILogger<LiteDbMatrixSyncRepository> logger,
RepositorySettings settings)
: base(connectionPool, logger, settings)
{
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

using LiteDB;
using Microsoft.Extensions.Logging;

namespace Beacon.Sdk.Core.Infrastructure.Repositories
{
using System.Threading.Tasks;
using Domain.Entities.P2P;
using Domain.Interfaces.Data;
using Microsoft.Extensions.Logging;
using Utils;

public class LiteDbP2PPeerRoomRepository : BaseLiteDbRepository<P2PPeerRoom>, IP2PPeerRoomRepository
{
private const string CollectionName = "P2PPeerRoom";

public LiteDbP2PPeerRoomRepository(ILogger<LiteDbP2PPeerRoomRepository> logger, RepositorySettings settings) :
base(logger, settings)
public LiteDbP2PPeerRoomRepository(
ILiteDbConnectionPool connectionPool,
ILogger<LiteDbP2PPeerRoomRepository> logger,
RepositorySettings settings) :
base(connectionPool, logger, settings)
{
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
using System;
using System.Collections.Generic;
using LiteDB;
using System.Threading.Tasks;

using LiteDB;
using Microsoft.Extensions.Logging;

namespace Beacon.Sdk.Core.Infrastructure.Repositories
{
using System.Threading.Tasks;
using Domain.Entities;
using Domain.Interfaces.Data;
using Microsoft.Extensions.Logging;

public class LiteDbPeerRepository : BaseLiteDbRepository<Peer>, IPeerRepository
{
private const string CollectionName = "Peer";

public LiteDbPeerRepository(ILogger<LiteDbPeerRepository> logger, RepositorySettings settings)
: base(logger, settings)
public LiteDbPeerRepository(
ILiteDbConnectionPool connectionPool,
ILogger<LiteDbPeerRepository> logger,
RepositorySettings settings)
: base(connectionPool, logger, settings)
{
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

using LiteDB;
using Microsoft.Extensions.Logging;

namespace Beacon.Sdk.Core.Infrastructure.Repositories
{
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Domain.Entities;
using Domain.Interfaces.Data;
using Microsoft.Extensions.Logging;

public class LiteDbPermissionInfoRepository : BaseLiteDbRepository<PermissionInfo>, IPermissionInfoRepository
{
private const string CollectionName = "PermissionInfo";

public LiteDbPermissionInfoRepository(ILogger<LiteDbPermissionInfoRepository> logger,
RepositorySettings settings) : base(logger, settings)
public LiteDbPermissionInfoRepository(
ILiteDbConnectionPool connectionPool,
ILogger<LiteDbPermissionInfoRepository> logger,
RepositorySettings settings)
: base(connectionPool, logger, settings)
{
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,26 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

using LiteDB;
using Microsoft.Extensions.Logging;

namespace Beacon.Sdk.Core.Infrastructure.Repositories
{
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Domain.Entities;
using Domain.Interfaces.Data;
using Microsoft.Extensions.Logging;

// Todo: Add secure storage
public class LiteDbSeedRepository : BaseLiteDbRepository<SeedEntity>, ISeedRepository
{
private const string CollectionName = "Seed";

public LiteDbSeedRepository(ILogger<LiteDbSeedRepository> logger, RepositorySettings settings)
: base(logger, settings)
public LiteDbSeedRepository(
ILiteDbConnectionPool connectionPool,
ILogger<LiteDbSeedRepository> logger,
RepositorySettings settings)
: base(connectionPool, logger, settings)
{
}

Expand Down

0 comments on commit a963a6b

Please sign in to comment.