use transactions when adding covers to db (thx to rayo)

This commit is contained in:
Alexander Eckart committed 2012-03-14 19:55:59 +01:00
1 parent 89aa513ed7
commit 0ea6a9043b
6 files changed
+106 -172

No files matched your search

+36 -15
View File
@@ -29,6 +29,9 @@ namespace Vocaluxe.Base
private static string _CoverFilePath; private static string _CoverFilePath;
private static string _CreditsRessourcesFilePath; private static string _CreditsRessourcesFilePath;
private static SQLiteConnection _ConnectionCover = null;
private static SQLiteTransaction _TransactionCover = null;
public static void Init() public static void Init()
{ {
_HighscoreFilePath = Path.Combine(System.Environment.CurrentDirectory, CSettings.sFileHighscoreDB); _HighscoreFilePath = Path.Combine(System.Environment.CurrentDirectory, CSettings.sFileHighscoreDB);
@@ -959,18 +962,15 @@ namespace Vocaluxe.Base
return false; return false;
} }
SQLiteConnection connection = new SQLiteConnection(); if (_ConnectionCover == null)
connection.ConnectionString = "Data Source=" + _CoverFilePath; {
_ConnectionCover = new SQLiteConnection();
_ConnectionCover.ConnectionString = "Data Source=" + _CoverFilePath;
_ConnectionCover.Open();
}
SQLiteCommand command; SQLiteCommand command;
try command = new SQLiteCommand(_ConnectionCover);
{
connection.Open();
}
catch (Exception)
{
return false;
}
command = new SQLiteCommand(connection);
command.CommandText = "SELECT id, width, height FROM Cover WHERE [Path] = @path"; command.CommandText = "SELECT id, width, height FROM Cover WHERE [Path] = @path";
command.Parameters.Add("@path", System.Data.DbType.String, 0).Value = CoverPath; command.Parameters.Add("@path", System.Data.DbType.String, 0).Value = CoverPath;
@@ -1016,6 +1016,11 @@ namespace Vocaluxe.Base
if (reader != null) if (reader != null)
reader.Close(); reader.Close();
if (_TransactionCover == null)
{
_TransactionCover = _ConnectionCover.BeginTransaction();
}
Bitmap origin; Bitmap origin;
try try
{ {
@@ -1032,8 +1037,6 @@ namespace Vocaluxe.Base
reader.Dispose(); reader.Dispose();
} }
command.Dispose(); command.Dispose();
connection.Close();
connection.Dispose();
return false; return false;
} }
@@ -1094,12 +1097,30 @@ namespace Vocaluxe.Base
reader.Dispose(); reader.Dispose();
} }
command.Dispose(); command.Dispose();
connection.Close();
connection.Dispose();
return result; 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() private static bool InitCoverDB()
{ {
SQLiteConnection connection = new SQLiteConnection(); SQLiteConnection connection = new SQLiteConnection();
+48 -137
View File
@@ -12,8 +12,6 @@ namespace Vocaluxe.Base
{ {
static class CDraw static class CDraw
{ {
private static Object mutex = new object();
private static IDraw _Draw = null; private static IDraw _Draw = null;
public static void InitDraw() public static void InitDraw()
@@ -50,259 +48,172 @@ namespace Vocaluxe.Base
public static int GetScreenWidth() public static int GetScreenWidth()
{ {
lock (mutex) return _Draw.GetScreenWidth();
{
return _Draw.GetScreenWidth();
}
} }
public static int GetScreenHeight() public static int GetScreenHeight()
{ {
lock (mutex) return _Draw.GetScreenHeight();
{
return _Draw.GetScreenHeight();
}
} }
public static RectangleF GetTextBounds(CText text) public static RectangleF GetTextBounds(CText text)
{ {
lock (mutex) return _Draw.GetTextBounds(text);
{
return _Draw.GetTextBounds(text);
}
} }
public static RectangleF GetTextBounds(CText text, float Height) 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) 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) 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 - 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 + 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 - 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 + Rect.W - Thickness / 2, Rect.Y - Thickness / 2, Thickness, Rect.H + Thickness, Rect.Z));
}
} }
public static void DrawColor(SColorF color, SRectF rect) public static void DrawColor(SColorF color, SRectF rect)
{ {
lock (mutex) _Draw.DrawColor(color, rect);
{
_Draw.DrawColor(color, rect);
}
} }
public static void ClearScreen() public static void ClearScreen()
{ {
lock (mutex) _Draw.ClearScreen();
{
_Draw.ClearScreen();
}
} }
public static STexture CopyScreen() public static STexture CopyScreen()
{ {
lock (mutex) return _Draw.CopyScreen();
{
return _Draw.CopyScreen();
}
} }
public static void CopyScreen(ref STexture Texture) public static void CopyScreen(ref STexture Texture)
{ {
lock (mutex) _Draw.CopyScreen(ref Texture);
{
_Draw.CopyScreen(ref Texture);
}
} }
public static void MakeScreenShot() public static void MakeScreenShot()
{ {
lock (mutex) _Draw.MakeScreenShot();
{
_Draw.MakeScreenShot();
}
} }
// Draw Basic Text (must be deleted later) // Draw Basic Text (must be deleted later)
public static void DrawText(string Text, int x, int y, int h) 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) public static STexture AddTexture(Bitmap Bitmap)
{ {
lock (mutex) return _Draw.AddTexture(Bitmap);
{
return _Draw.AddTexture(Bitmap);
}
} }
public static STexture AddTexture(string TexturePath) public static STexture AddTexture(string TexturePath)
{ {
lock (mutex) return _Draw.AddTexture(TexturePath);
{
return _Draw.AddTexture(TexturePath);
}
} }
public static STexture AddTexture(string TexturePath, int MaxSize) 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)) if (!System.IO.File.Exists(TexturePath))
return new STexture(-1); return new STexture(-1);
Bitmap origin = new Bitmap(TexturePath); Bitmap origin = new Bitmap(TexturePath);
int w = MaxSize; int w = MaxSize;
int h = MaxSize; int h = MaxSize;
if (origin.Width >= origin.Height && origin.Width > w) if (origin.Width >= origin.Height && origin.Width > w)
h = (int)Math.Round((float)w / origin.Width * origin.Height); h = (int)Math.Round((float)w / origin.Width * origin.Height);
else if (origin.Height > origin.Width && origin.Height > h) else if (origin.Height > origin.Width && origin.Height > h)
w = (int)Math.Round((float)h / origin.Height * origin.Width); w = (int)Math.Round((float)h / origin.Height * origin.Width);
Bitmap bmp = new Bitmap(origin, w, h); Bitmap bmp = new Bitmap(origin, w, h);
STexture tex = _Draw.AddTexture(bmp); STexture tex = _Draw.AddTexture(bmp);
bmp.Dispose(); bmp.Dispose();
return tex; return tex;
}
} }
public static STexture AddTexture(int W, int H, IntPtr Data) 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) 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) 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) 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) public static void RemoveTexture(ref STexture Texture)
{ {
lock (mutex) _Draw.RemoveTexture(ref Texture);
{
_Draw.RemoveTexture(ref Texture);
}
} }
public static void DrawTexture(STexture Texture) public static void DrawTexture(STexture Texture)
{ {
lock (mutex) _Draw.DrawTexture(Texture);
{
_Draw.DrawTexture(Texture);
}
} }
public static void DrawTexture(STexture Texture, SRectF rect) 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) 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) 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) 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) 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) 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) 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() public static int TextureCount()
{ {
lock (mutex) return _Draw.TextureCount();
{
return _Draw.TextureCount();
}
} }
} }
+19 -18
View File
@@ -829,7 +829,7 @@ namespace Vocaluxe.Base
CLog.StopBenchmark(1, "Load Songs "); CLog.StopBenchmark(1, "Load Songs ");
} }
public static void LoadCover(long WaitTime) public static void LoadCover(long WaitTime, int NumLoads)
{ {
if (!SongsLoaded) if (!SongsLoaded)
return; return;
@@ -846,25 +846,26 @@ namespace Vocaluxe.Base
STexture texture = new STexture(-1); STexture texture = new STexture(-1);
if (_CoverLoadTimer.ElapsedMilliseconds >= WaitTime) if (_CoverLoadTimer.ElapsedMilliseconds >= WaitTime)
{ {
CSong song = new CSong(); for (int i = 0; i < NumLoads; i++)
int i = GetNextSongWithoutCover(ref song);
if (i < 0)
return;
song.ReadNotes();
if (song.CoverFileName != String.Empty)
{ {
if (!CDataBase.GetCover(Path.Combine(song.Folder, song.CoverFileName), ref texture, CConfig.CoverSize)) CSong song = new CSong();
texture = CCover.NoCover; int n = GetNextSongWithoutCover(ref song);
}
else
texture = CCover.NoCover;
SetCoverSmall(i, texture);
SetCoverBig(i, texture);
_CoverLoadTimer.Reset(); if (n < 0)
_CoverLoadTimer.Start(); return;
song.ReadNotes();
texture = song.CoverTextureSmall;
SetCoverSmall(n, texture);
SetCoverBig(n, texture);
if (CoverLoaded)
CDataBase.CommitCovers();
_CoverLoadTimer.Reset();
_CoverLoadTimer.Start();
}
} }
} }
} }
+1 -1
View File
@@ -317,7 +317,7 @@ namespace Vocaluxe.Menu
CBackgroundMusic.Update(); CBackgroundMusic.Update();
if (CConfig.CoverLoading == ECoverLoading.TR_CONFIG_COVERLOADING_DYNAMIC && ActualScreen != EScreens.ScreenSing) if (CConfig.CoverLoading == ECoverLoading.TR_CONFIG_COVERLOADING_DYNAMIC && ActualScreen != EScreens.ScreenSing)
CSongs.LoadCover(30L); CSongs.LoadCover(30L, 1);
if (CSettings.GameState != EGameState.EditTheme) if (CSettings.GameState != EGameState.EditTheme)
{ {
+1
View File
@@ -152,6 +152,7 @@ namespace Vocaluxe
CVideo.VdCloseAll(); CVideo.VdCloseAll();
CDraw.Unload(); CDraw.Unload();
CLog.CloseAll(); CLog.CloseAll();
CDataBase.CloseConnections();
} }
static Assembly AssemblyResolver(Object sender, ResolveEventArgs args) static Assembly AssemblyResolver(Object sender, ResolveEventArgs args)
+1 -1
View File
@@ -153,7 +153,7 @@ namespace Vocaluxe.Screens
if (CSongs.SongsLoaded && CConfig.CoverLoading == ECoverLoading.TR_CONFIG_COVERLOADING_ATSTART && _timer.ElapsedMilliseconds > 1000L) 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) || bool next = ((CConfig.CoverLoading == ECoverLoading.TR_CONFIG_COVERLOADING_ATSTART && CSongs.CoverLoaded) ||