From 0ea6a9043bc7fc7c184c6e703d19293b49a718e3 Mon Sep 17 00:00:00 2001 From: Alexander Eckart Date: Wed, 14 Mar 2012 19:55:59 +0100 Subject: [PATCH] use transactions when adding covers to db (thx to rayo) --- Vocaluxe/Base/CDataBase.cs | 51 ++++++--- Vocaluxe/Base/CDraw.cs | 185 +++++++++----------------------- Vocaluxe/Base/CSongs.cs | 37 +++---- Vocaluxe/Menu/CGraphics.cs | 2 +- Vocaluxe/Program.cs | 1 + Vocaluxe/Screens/CScreenLoad.cs | 2 +- 6 files changed, 106 insertions(+), 172 deletions(-) diff --git a/Vocaluxe/Base/CDataBase.cs b/Vocaluxe/Base/CDataBase.cs index 15c6f6f2..daa25460 100644 --- a/Vocaluxe/Base/CDataBase.cs +++ b/Vocaluxe/Base/CDataBase.cs @@ -29,6 +29,9 @@ namespace Vocaluxe.Base private static string _CoverFilePath; private static string _CreditsRessourcesFilePath; + private static SQLiteConnection _ConnectionCover = null; + private static SQLiteTransaction _TransactionCover = null; + public static void Init() { _HighscoreFilePath = Path.Combine(System.Environment.CurrentDirectory, CSettings.sFileHighscoreDB); @@ -959,18 +962,15 @@ namespace Vocaluxe.Base return false; } - SQLiteConnection connection = new SQLiteConnection(); - connection.ConnectionString = "Data Source=" + _CoverFilePath; + if (_ConnectionCover == null) + { + _ConnectionCover = new SQLiteConnection(); + _ConnectionCover.ConnectionString = "Data Source=" + _CoverFilePath; + _ConnectionCover.Open(); + } + SQLiteCommand command; - try - { - connection.Open(); - } - catch (Exception) - { - return false; - } - command = new SQLiteCommand(connection); + command = new SQLiteCommand(_ConnectionCover); command.CommandText = "SELECT id, width, height FROM Cover WHERE [Path] = @path"; command.Parameters.Add("@path", System.Data.DbType.String, 0).Value = CoverPath; @@ -1016,6 +1016,11 @@ namespace Vocaluxe.Base if (reader != null) reader.Close(); + if (_TransactionCover == null) + { + _TransactionCover = _ConnectionCover.BeginTransaction(); + } + Bitmap origin; try { @@ -1032,8 +1037,6 @@ namespace Vocaluxe.Base reader.Dispose(); } command.Dispose(); - connection.Close(); - connection.Dispose(); return false; } @@ -1094,12 +1097,30 @@ namespace Vocaluxe.Base reader.Dispose(); } command.Dispose(); - connection.Close(); - connection.Dispose(); return result; } + public static void CommitCovers() + { + if (_TransactionCover != null) + { + _TransactionCover.Commit(); + _TransactionCover = null; + } + } + + public static void CloseConnections() + { + CommitCovers(); + + if (_ConnectionCover != null) + { + _ConnectionCover.Close(); + _ConnectionCover = null; + } + } + private static bool InitCoverDB() { SQLiteConnection connection = new SQLiteConnection(); diff --git a/Vocaluxe/Base/CDraw.cs b/Vocaluxe/Base/CDraw.cs index 3a62415b..6c0e0684 100644 --- a/Vocaluxe/Base/CDraw.cs +++ b/Vocaluxe/Base/CDraw.cs @@ -12,8 +12,6 @@ namespace Vocaluxe.Base { static class CDraw { - private static Object mutex = new object(); - private static IDraw _Draw = null; public static void InitDraw() @@ -50,259 +48,172 @@ namespace Vocaluxe.Base public static int GetScreenWidth() { - lock (mutex) - { - return _Draw.GetScreenWidth(); - } + return _Draw.GetScreenWidth(); } public static int GetScreenHeight() { - lock (mutex) - { - return _Draw.GetScreenHeight(); - } + return _Draw.GetScreenHeight(); } public static RectangleF GetTextBounds(CText text) { - lock (mutex) - { - return _Draw.GetTextBounds(text); - } + return _Draw.GetTextBounds(text); } public static RectangleF GetTextBounds(CText text, float Height) { - lock (mutex) - { - return _Draw.GetTextBounds(text, Height); - } + return _Draw.GetTextBounds(text, Height); } public static void DrawLine(int a, int r, int g, int b, int w, int x1, int y1, int x2, int y2) { - lock (mutex) - { - _Draw.DrawLine(a, r, g, b, w, x1, y1, x2, y2); - } + _Draw.DrawLine(a, r, g, b, w, x1, y1, x2, y2); } public static void DrawRect(SColorF Color, SRectF Rect, float Thickness) { - lock (mutex) - { - if (Thickness <= 0f) - return; + if (Thickness <= 0f) + return; - _Draw.DrawColor(Color, new SRectF(Rect.X - Thickness / 2, Rect.Y - Thickness / 2, Rect.W + Thickness, Thickness, Rect.Z)); - _Draw.DrawColor(Color, new SRectF(Rect.X - Thickness / 2, Rect.Y + Rect.H - Thickness / 2, Rect.W + Thickness, Thickness, Rect.Z)); - _Draw.DrawColor(Color, new SRectF(Rect.X - Thickness / 2, Rect.Y - Thickness / 2, Thickness, Rect.H + Thickness, Rect.Z)); - _Draw.DrawColor(Color, new SRectF(Rect.X + Rect.W - Thickness / 2, Rect.Y - Thickness / 2, Thickness, Rect.H + Thickness, Rect.Z)); - } + _Draw.DrawColor(Color, new SRectF(Rect.X - Thickness / 2, Rect.Y - Thickness / 2, Rect.W + Thickness, Thickness, Rect.Z)); + _Draw.DrawColor(Color, new SRectF(Rect.X - Thickness / 2, Rect.Y + Rect.H - Thickness / 2, Rect.W + Thickness, Thickness, Rect.Z)); + _Draw.DrawColor(Color, new SRectF(Rect.X - Thickness / 2, Rect.Y - Thickness / 2, Thickness, Rect.H + Thickness, Rect.Z)); + _Draw.DrawColor(Color, new SRectF(Rect.X + Rect.W - Thickness / 2, Rect.Y - Thickness / 2, Thickness, Rect.H + Thickness, Rect.Z)); } public static void DrawColor(SColorF color, SRectF rect) { - lock (mutex) - { - _Draw.DrawColor(color, rect); - } + _Draw.DrawColor(color, rect); } public static void ClearScreen() { - lock (mutex) - { - _Draw.ClearScreen(); - } + _Draw.ClearScreen(); } public static STexture CopyScreen() { - lock (mutex) - { - return _Draw.CopyScreen(); - } + return _Draw.CopyScreen(); } public static void CopyScreen(ref STexture Texture) { - lock (mutex) - { - _Draw.CopyScreen(ref Texture); - } + _Draw.CopyScreen(ref Texture); } public static void MakeScreenShot() { - lock (mutex) - { - _Draw.MakeScreenShot(); - } + _Draw.MakeScreenShot(); } // Draw Basic Text (must be deleted later) public static void DrawText(string Text, int x, int y, int h) { - lock (mutex) - { - _Draw.DrawText(Text, x, y, h); - } + _Draw.DrawText(Text, x, y, h); } public static STexture AddTexture(Bitmap Bitmap) { - lock (mutex) - { - return _Draw.AddTexture(Bitmap); - } + return _Draw.AddTexture(Bitmap); } public static STexture AddTexture(string TexturePath) { - lock (mutex) - { - return _Draw.AddTexture(TexturePath); - } + return _Draw.AddTexture(TexturePath); } public static STexture AddTexture(string TexturePath, int MaxSize) { - lock (mutex) - { - if (MaxSize == 0) - return _Draw.AddTexture(TexturePath); + if (MaxSize == 0) + return _Draw.AddTexture(TexturePath); - if (!System.IO.File.Exists(TexturePath)) - return new STexture(-1); + if (!System.IO.File.Exists(TexturePath)) + return new STexture(-1); - Bitmap origin = new Bitmap(TexturePath); - int w = MaxSize; - int h = MaxSize; + Bitmap origin = new Bitmap(TexturePath); + int w = MaxSize; + int h = MaxSize; - if (origin.Width >= origin.Height && origin.Width > w) - h = (int)Math.Round((float)w / origin.Width * origin.Height); - else if (origin.Height > origin.Width && origin.Height > h) - w = (int)Math.Round((float)h / origin.Height * origin.Width); + if (origin.Width >= origin.Height && origin.Width > w) + h = (int)Math.Round((float)w / origin.Width * origin.Height); + else if (origin.Height > origin.Width && origin.Height > h) + w = (int)Math.Round((float)h / origin.Height * origin.Width); - Bitmap bmp = new Bitmap(origin, w, h); - STexture tex = _Draw.AddTexture(bmp); - bmp.Dispose(); - return tex; - } + Bitmap bmp = new Bitmap(origin, w, h); + STexture tex = _Draw.AddTexture(bmp); + bmp.Dispose(); + return tex; } public static STexture AddTexture(int W, int H, IntPtr Data) { - lock (mutex) - { - return _Draw.AddTexture(W, H, Data); - } + return _Draw.AddTexture(W, H, Data); } public static STexture AddTexture(int W, int H, ref byte[] Data) { - lock (mutex) - { - return _Draw.AddTexture(W, H, ref Data); - } + return _Draw.AddTexture(W, H, ref Data); } public static bool UpdateTexture(ref STexture Texture, ref byte[] Data) { - lock (mutex) - { - return _Draw.UpdateTexture(ref Texture, ref Data); - } + return _Draw.UpdateTexture(ref Texture, ref Data); } public static bool UpdateTexture(ref STexture Texture, IntPtr Data) { - lock (mutex) - { - return _Draw.UpdateTexture(ref Texture, Data); - } + return _Draw.UpdateTexture(ref Texture, Data); } public static void RemoveTexture(ref STexture Texture) { - lock (mutex) - { - _Draw.RemoveTexture(ref Texture); - } + _Draw.RemoveTexture(ref Texture); } public static void DrawTexture(STexture Texture) { - lock (mutex) - { - _Draw.DrawTexture(Texture); - } + _Draw.DrawTexture(Texture); } public static void DrawTexture(STexture Texture, SRectF rect) { - lock (mutex) - { - _Draw.DrawTexture(Texture, rect); - } + _Draw.DrawTexture(Texture, rect); } public static void DrawTexture(STexture Texture, SRectF rect, SColorF color) { - lock (mutex) - { - _Draw.DrawTexture(Texture, rect, color); - } + _Draw.DrawTexture(Texture, rect, color); } public static void DrawTexture(STexture Texture, SRectF rect, SColorF color, SRectF bounds) { - lock (mutex) - { - _Draw.DrawTexture(Texture, rect, color, bounds); - } + _Draw.DrawTexture(Texture, rect, color, bounds); } public static void DrawTexture(STexture Texture, SRectF rect, SColorF color,bool mirrored) { - lock (mutex) - { - _Draw.DrawTexture(Texture, rect, color, mirrored); - } + _Draw.DrawTexture(Texture, rect, color, mirrored); } public static void DrawTexture(STexture Texture, SRectF rect, SColorF color, SRectF bounds, bool mirrored) { - lock (mutex) - { - _Draw.DrawTexture(Texture, rect, color, bounds, mirrored); - } + _Draw.DrawTexture(Texture, rect, color, bounds, mirrored); } public static void DrawTexture(STexture Texture, SRectF rect, SColorF color, float begin, float end) { - lock (mutex) - { - _Draw.DrawTexture(Texture, rect, color, begin, end); - } + _Draw.DrawTexture(Texture, rect, color, begin, end); } public static void DrawTextureReflection(STexture Texture, SRectF rect, SColorF color, SRectF bounds, float space, float height) { - lock (mutex) - { - _Draw.DrawTextureReflection(Texture, rect, color, bounds, space, height); - } + _Draw.DrawTextureReflection(Texture, rect, color, bounds, space, height); } public static int TextureCount() { - lock (mutex) - { - return _Draw.TextureCount(); - } + return _Draw.TextureCount(); } } diff --git a/Vocaluxe/Base/CSongs.cs b/Vocaluxe/Base/CSongs.cs index 778197f0..c9d4d71d 100644 --- a/Vocaluxe/Base/CSongs.cs +++ b/Vocaluxe/Base/CSongs.cs @@ -829,7 +829,7 @@ namespace Vocaluxe.Base CLog.StopBenchmark(1, "Load Songs "); } - public static void LoadCover(long WaitTime) + public static void LoadCover(long WaitTime, int NumLoads) { if (!SongsLoaded) return; @@ -846,25 +846,26 @@ namespace Vocaluxe.Base STexture texture = new STexture(-1); if (_CoverLoadTimer.ElapsedMilliseconds >= WaitTime) { - CSong song = new CSong(); - int i = GetNextSongWithoutCover(ref song); - - if (i < 0) - return; - - song.ReadNotes(); - if (song.CoverFileName != String.Empty) + for (int i = 0; i < NumLoads; i++) { - if (!CDataBase.GetCover(Path.Combine(song.Folder, song.CoverFileName), ref texture, CConfig.CoverSize)) - texture = CCover.NoCover; - } - else - texture = CCover.NoCover; - SetCoverSmall(i, texture); - SetCoverBig(i, texture); + CSong song = new CSong(); + int n = GetNextSongWithoutCover(ref song); - _CoverLoadTimer.Reset(); - _CoverLoadTimer.Start(); + if (n < 0) + return; + + song.ReadNotes(); + texture = song.CoverTextureSmall; + + SetCoverSmall(n, texture); + SetCoverBig(n, texture); + + if (CoverLoaded) + CDataBase.CommitCovers(); + + _CoverLoadTimer.Reset(); + _CoverLoadTimer.Start(); + } } } } diff --git a/Vocaluxe/Menu/CGraphics.cs b/Vocaluxe/Menu/CGraphics.cs index a47dd769..d924c584 100644 --- a/Vocaluxe/Menu/CGraphics.cs +++ b/Vocaluxe/Menu/CGraphics.cs @@ -317,7 +317,7 @@ namespace Vocaluxe.Menu CBackgroundMusic.Update(); if (CConfig.CoverLoading == ECoverLoading.TR_CONFIG_COVERLOADING_DYNAMIC && ActualScreen != EScreens.ScreenSing) - CSongs.LoadCover(30L); + CSongs.LoadCover(30L, 1); if (CSettings.GameState != EGameState.EditTheme) { diff --git a/Vocaluxe/Program.cs b/Vocaluxe/Program.cs index b5f3228e..854b4d8d 100644 --- a/Vocaluxe/Program.cs +++ b/Vocaluxe/Program.cs @@ -152,6 +152,7 @@ namespace Vocaluxe CVideo.VdCloseAll(); CDraw.Unload(); CLog.CloseAll(); + CDataBase.CloseConnections(); } static Assembly AssemblyResolver(Object sender, ResolveEventArgs args) diff --git a/Vocaluxe/Screens/CScreenLoad.cs b/Vocaluxe/Screens/CScreenLoad.cs index 1ffa133c..9d3f92c2 100644 --- a/Vocaluxe/Screens/CScreenLoad.cs +++ b/Vocaluxe/Screens/CScreenLoad.cs @@ -153,7 +153,7 @@ namespace Vocaluxe.Screens if (CSongs.SongsLoaded && CConfig.CoverLoading == ECoverLoading.TR_CONFIG_COVERLOADING_ATSTART && _timer.ElapsedMilliseconds > 1000L) { - CSongs.LoadCover(20L); + CSongs.LoadCover(0L, 1); } bool next = ((CConfig.CoverLoading == ECoverLoading.TR_CONFIG_COVERLOADING_ATSTART && CSongs.CoverLoaded) ||