Merge pull request #10138 from cvium/sqlite_client_poc
commit
c7ca416206
@ -1,79 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using SQLitePCL.pretty;
|
||||
|
||||
namespace Emby.Server.Implementations.Data;
|
||||
|
||||
/// <summary>
|
||||
/// A pool of SQLite Database connections.
|
||||
/// </summary>
|
||||
public sealed class ConnectionPool : IDisposable
|
||||
{
|
||||
private readonly BlockingCollection<SQLiteDatabaseConnection> _connections = new();
|
||||
private bool _disposed;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="ConnectionPool" /> class.
|
||||
/// </summary>
|
||||
/// <param name="count">The number of database connection to create.</param>
|
||||
/// <param name="factory">Factory function to create the database connections.</param>
|
||||
public ConnectionPool(int count, Func<SQLiteDatabaseConnection> factory)
|
||||
{
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
_connections.Add(factory.Invoke());
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets a database connection from the pool if one is available, otherwise blocks.
|
||||
/// </summary>
|
||||
/// <returns>A database connection.</returns>
|
||||
public ManagedConnection GetConnection()
|
||||
{
|
||||
if (_disposed)
|
||||
{
|
||||
ThrowObjectDisposedException();
|
||||
}
|
||||
|
||||
return new ManagedConnection(_connections.Take(), this);
|
||||
|
||||
static void ThrowObjectDisposedException()
|
||||
{
|
||||
throw new ObjectDisposedException(nameof(ConnectionPool));
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Return a database connection to the pool.
|
||||
/// </summary>
|
||||
/// <param name="connection">The database connection to return.</param>
|
||||
public void Return(SQLiteDatabaseConnection connection)
|
||||
{
|
||||
if (_disposed)
|
||||
{
|
||||
connection.Dispose();
|
||||
return;
|
||||
}
|
||||
|
||||
_connections.Add(connection);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public void Dispose()
|
||||
{
|
||||
if (_disposed)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
foreach (var connection in _connections)
|
||||
{
|
||||
connection.Dispose();
|
||||
}
|
||||
|
||||
_connections.Dispose();
|
||||
|
||||
_disposed = true;
|
||||
}
|
||||
}
|
@ -1,81 +0,0 @@
|
||||
#pragma warning disable CS1591
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using SQLitePCL.pretty;
|
||||
|
||||
namespace Emby.Server.Implementations.Data
|
||||
{
|
||||
public sealed class ManagedConnection : IDisposable
|
||||
{
|
||||
private readonly ConnectionPool _pool;
|
||||
|
||||
private SQLiteDatabaseConnection _db;
|
||||
|
||||
private bool _disposed = false;
|
||||
|
||||
public ManagedConnection(SQLiteDatabaseConnection db, ConnectionPool pool)
|
||||
{
|
||||
_db = db;
|
||||
_pool = pool;
|
||||
}
|
||||
|
||||
public IStatement PrepareStatement(string sql)
|
||||
{
|
||||
return _db.PrepareStatement(sql);
|
||||
}
|
||||
|
||||
public IEnumerable<IStatement> PrepareAll(string sql)
|
||||
{
|
||||
return _db.PrepareAll(sql);
|
||||
}
|
||||
|
||||
public void ExecuteAll(string sql)
|
||||
{
|
||||
_db.ExecuteAll(sql);
|
||||
}
|
||||
|
||||
public void Execute(string sql, params object[] values)
|
||||
{
|
||||
_db.Execute(sql, values);
|
||||
}
|
||||
|
||||
public void RunQueries(string[] sql)
|
||||
{
|
||||
_db.RunQueries(sql);
|
||||
}
|
||||
|
||||
public void RunInTransaction(Action<IDatabaseConnection> action, TransactionMode mode)
|
||||
{
|
||||
_db.RunInTransaction(action, mode);
|
||||
}
|
||||
|
||||
public T RunInTransaction<T>(Func<IDatabaseConnection, T> action, TransactionMode mode)
|
||||
{
|
||||
return _db.RunInTransaction(action, mode);
|
||||
}
|
||||
|
||||
public IEnumerable<IReadOnlyList<ResultSetValue>> Query(string sql)
|
||||
{
|
||||
return _db.Query(sql);
|
||||
}
|
||||
|
||||
public IEnumerable<IReadOnlyList<ResultSetValue>> Query(string sql, params object[] values)
|
||||
{
|
||||
return _db.Query(sql, values);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
if (_disposed)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_pool.Return(_db);
|
||||
|
||||
_db = null!; // Don't dispose it
|
||||
_disposed = true;
|
||||
}
|
||||
}
|
||||
}
|
File diff suppressed because it is too large
Load Diff
Loading…
Reference in new issue