From e01202030dcd16cd9c7c3327b4e411be7de02614 Mon Sep 17 00:00:00 2001 From: Luke Pulverenti Date: Tue, 21 May 2013 11:52:59 -0400 Subject: removed sql delayed writer in favor of prepared statements --- .../Sqlite/SQLiteRepository.cs | 165 ++------------------- 1 file changed, 14 insertions(+), 151 deletions(-) (limited to 'MediaBrowser.Server.Implementations/Sqlite/SQLiteRepository.cs') diff --git a/MediaBrowser.Server.Implementations/Sqlite/SQLiteRepository.cs b/MediaBrowser.Server.Implementations/Sqlite/SQLiteRepository.cs index 6aeb63d9b..bd60c834f 100644 --- a/MediaBrowser.Server.Implementations/Sqlite/SQLiteRepository.cs +++ b/MediaBrowser.Server.Implementations/Sqlite/SQLiteRepository.cs @@ -18,24 +18,11 @@ namespace MediaBrowser.Server.Implementations.Sqlite /// /// The db file name /// - protected string dbFileName; + protected string DbFileName; /// /// The connection /// - protected SQLiteConnection connection; - /// - /// The delayed commands - /// - protected ConcurrentQueue delayedCommands = new ConcurrentQueue(); - /// - /// The flush interval - /// - private const int FlushInterval = 2000; - - /// - /// The flush timer - /// - private Timer FlushTimer; + protected SQLiteConnection Connection; /// /// Gets the logger. @@ -43,18 +30,6 @@ namespace MediaBrowser.Server.Implementations.Sqlite /// The logger. protected ILogger Logger { get; private set; } - /// - /// Gets a value indicating whether [enable delayed commands]. - /// - /// true if [enable delayed commands]; otherwise, false. - protected virtual bool EnableDelayedCommands - { - get - { - return true; - } - } - /// /// Initializes a new instance of the class. /// @@ -83,7 +58,7 @@ namespace MediaBrowser.Server.Implementations.Sqlite throw new ArgumentNullException("dbPath"); } - dbFileName = dbPath; + DbFileName = dbPath; var connectionstr = new SQLiteConnectionStringBuilder { PageSize = 4096, @@ -93,15 +68,9 @@ namespace MediaBrowser.Server.Implementations.Sqlite JournalMode = SQLiteJournalModeEnum.Memory }; - connection = new SQLiteConnection(connectionstr.ConnectionString); + Connection = new SQLiteConnection(connectionstr.ConnectionString); - await connection.OpenAsync().ConfigureAwait(false); - - if (EnableDelayedCommands) - { - // Run once - FlushTimer = new Timer(Flush, null, TimeSpan.FromMilliseconds(FlushInterval), TimeSpan.FromMilliseconds(-1)); - } + await Connection.OpenAsync().ConfigureAwait(false); } /// @@ -117,11 +86,11 @@ namespace MediaBrowser.Server.Implementations.Sqlite throw new ArgumentNullException("queries"); } - using (var tran = connection.BeginTransaction()) + using (var tran = Connection.BeginTransaction()) { try { - using (var cmd = connection.CreateCommand()) + using (var cmd = Connection.CreateCommand()) { foreach (var query in queries) { @@ -165,26 +134,15 @@ namespace MediaBrowser.Server.Implementations.Sqlite { lock (_disposeLock) { - if (connection != null) + if (Connection != null) { - if (EnableDelayedCommands) + if (Connection.IsOpen()) { - FlushOnDispose(); + Connection.Close(); } - if (connection.IsOpen()) - { - connection.Close(); - } - - connection.Dispose(); - connection = null; - } - - if (FlushTimer != null) - { - FlushTimer.Dispose(); - FlushTimer = null; + Connection.Dispose(); + Connection = null; } } } @@ -195,101 +153,6 @@ namespace MediaBrowser.Server.Implementations.Sqlite } } - /// - /// Flushes the on dispose. - /// - private void FlushOnDispose() - { - // If we're not already flushing, do it now - if (!_isFlushing) - { - Flush(null); - } - - // Don't dispose in the middle of a flush - while (_isFlushing) - { - Thread.Sleep(25); - } - } - - /// - /// Queues the command. - /// - /// The CMD. - /// cmd - protected void QueueCommand(SQLiteCommand cmd) - { - if (cmd == null) - { - throw new ArgumentNullException("cmd"); - } - - delayedCommands.Enqueue(cmd); - } - - /// - /// The is flushing - /// - private bool _isFlushing; - - /// - /// Flushes the specified sender. - /// - /// The sender. - private void Flush(object sender) - { - // Cannot call Count on a ConcurrentQueue since it's an O(n) operation - // Use IsEmpty instead - if (delayedCommands.IsEmpty) - { - FlushTimer.Change(TimeSpan.FromMilliseconds(FlushInterval), TimeSpan.FromMilliseconds(-1)); - return; - } - - if (_isFlushing) - { - return; - } - - _isFlushing = true; - var numCommands = 0; - - using (var tran = connection.BeginTransaction()) - { - try - { - while (!delayedCommands.IsEmpty) - { - SQLiteCommand command; - - delayedCommands.TryDequeue(out command); - - command.Connection = connection; - command.Transaction = tran; - - command.ExecuteNonQuery(); - - command.Dispose(); - - numCommands++; - } - - tran.Commit(); - } - catch (Exception e) - { - Logger.ErrorException("Failed to commit transaction.", e); - tran.Rollback(); - } - } - - Logger.Debug("SQL Delayed writer executed " + numCommands + " commands"); - - FlushTimer.Change(TimeSpan.FromMilliseconds(FlushInterval), TimeSpan.FromMilliseconds(-1)); - _isFlushing = false; - } - /// /// Executes the command. /// @@ -303,11 +166,11 @@ namespace MediaBrowser.Server.Implementations.Sqlite throw new ArgumentNullException("cmd"); } - using (var tran = connection.BeginTransaction()) + using (var tran = Connection.BeginTransaction()) { try { - cmd.Connection = connection; + cmd.Connection = Connection; cmd.Transaction = tran; await cmd.ExecuteNonQueryAsync().ConfigureAwait(false); -- cgit v1.2.3