-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add LiteDbConnectionPool, increase version to v1.0.23
- Loading branch information
Showing
11 changed files
with
163 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 9 additions & 5 deletions
14
Beacon.Sdk/Core/Infrastructure/Repositories/LiteDbAppMetadataRepository.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 10 additions & 6 deletions
16
Beacon.Sdk/Core/Infrastructure/Repositories/LiteDbMatrixSyncRepository.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 8 additions & 4 deletions
12
Beacon.Sdk/Core/Infrastructure/Repositories/LiteDbP2PPeerRoomRepository.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 8 additions & 5 deletions
13
Beacon.Sdk/Core/Infrastructure/Repositories/LiteDbPeerRepository.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 10 additions & 6 deletions
16
Beacon.Sdk/Core/Infrastructure/Repositories/LiteDbPermissionInfoRepository.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 10 additions & 6 deletions
16
Beacon.Sdk/Core/Infrastructure/Repositories/LiteDbSeedRepository.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters