SqliteFileOrganizationRepository.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. using MediaBrowser.Controller;
  2. using MediaBrowser.Controller.Persistence;
  3. using MediaBrowser.Model.FileOrganization;
  4. using MediaBrowser.Model.Logging;
  5. using MediaBrowser.Model.Querying;
  6. using System;
  7. using System.Data;
  8. using System.IO;
  9. using System.Threading;
  10. using System.Threading.Tasks;
  11. namespace MediaBrowser.Server.Implementations.Persistence
  12. {
  13. public class SqliteFileOrganizationRepository : IFileOrganizationRepository
  14. {
  15. private IDbConnection _connection;
  16. private readonly ILogger _logger;
  17. private readonly SemaphoreSlim _writeLock = new SemaphoreSlim(1, 1);
  18. private SqliteShrinkMemoryTimer _shrinkMemoryTimer;
  19. private readonly IServerApplicationPaths _appPaths;
  20. public SqliteFileOrganizationRepository(ILogManager logManager, IServerApplicationPaths appPaths)
  21. {
  22. _appPaths = appPaths;
  23. _logger = logManager.GetLogger(GetType().Name);
  24. }
  25. /// <summary>
  26. /// Opens the connection to the database
  27. /// </summary>
  28. /// <returns>Task.</returns>
  29. public async Task Initialize()
  30. {
  31. var dbFile = Path.Combine(_appPaths.DataPath, "fileorganization.db");
  32. _connection = await SqliteExtensions.ConnectToDb(dbFile, _logger).ConfigureAwait(false);
  33. string[] queries = {
  34. //pragmas
  35. "pragma temp_store = memory",
  36. "pragma shrink_memory"
  37. };
  38. _connection.RunQueries(queries, _logger);
  39. PrepareStatements();
  40. _shrinkMemoryTimer = new SqliteShrinkMemoryTimer(_connection, _writeLock, _logger);
  41. }
  42. private void PrepareStatements()
  43. {
  44. }
  45. public Task SaveResult(FileOrganizationResult result, CancellationToken cancellationToken)
  46. {
  47. return Task.FromResult(true);
  48. }
  49. public QueryResult<FileOrganizationResult> GetResults(FileOrganizationResultQuery query)
  50. {
  51. return new QueryResult<FileOrganizationResult>();
  52. }
  53. /// <summary>
  54. /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
  55. /// </summary>
  56. public void Dispose()
  57. {
  58. Dispose(true);
  59. GC.SuppressFinalize(this);
  60. }
  61. private readonly object _disposeLock = new object();
  62. /// <summary>
  63. /// Releases unmanaged and - optionally - managed resources.
  64. /// </summary>
  65. /// <param name="dispose"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
  66. protected virtual void Dispose(bool dispose)
  67. {
  68. if (dispose)
  69. {
  70. try
  71. {
  72. lock (_disposeLock)
  73. {
  74. if (_shrinkMemoryTimer != null)
  75. {
  76. _shrinkMemoryTimer.Dispose();
  77. _shrinkMemoryTimer = null;
  78. }
  79. if (_connection != null)
  80. {
  81. if (_connection.IsOpen())
  82. {
  83. _connection.Close();
  84. }
  85. _connection.Dispose();
  86. _connection = null;
  87. }
  88. }
  89. }
  90. catch (Exception ex)
  91. {
  92. _logger.ErrorException("Error disposing database", ex);
  93. }
  94. }
  95. }
  96. }
  97. }