[longomatch] Add a new file based DB
- From: Andoni Morales Alastruey <amorales src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [longomatch] Add a new file based DB
- Date: Mon, 7 Jul 2014 11:18:08 +0000 (UTC)
commit dc1db3b7c5791bf862682a36dc7a0049666456fe
Author: Andoni Morales Alastruey <ylatuya gmail com>
Date: Sun Jan 26 01:43:58 2014 +0100
Add a new file based DB
LongoMatch.Core/Common/Constants.cs | 2 +-
LongoMatch.Core/Interfaces/IDatabase.cs | 6 +-
LongoMatch.Services/LongoMatch.Services.mdp | 2 +-
LongoMatch.Services/Makefile.am | 2 +-
LongoMatch.Services/Services/DataBaseManager.cs | 83 +------
LongoMatch.Services/Services/FileDB.cs | 267 +++++++++++++++++++----
6 files changed, 241 insertions(+), 121 deletions(-)
---
diff --git a/LongoMatch.Core/Common/Constants.cs b/LongoMatch.Core/Common/Constants.cs
index 7de7a61..73e32be 100644
--- a/LongoMatch.Core/Common/Constants.cs
+++ b/LongoMatch.Core/Common/Constants.cs
@@ -104,7 +104,7 @@ Xavier Queralt Mateu (ca)";
public const string HALF_FIELD_BACKGROUND = "half_field_background.svg";
public const string GOAL_BACKGROUND = "goal_background.svg";
- public const int DB_MAYOR_VERSION = 2;
+ public const int DB_MAYOR_VERSION = 3;
public const int DB_MINOR_VERSION = 1;
}
}
diff --git a/LongoMatch.Core/Interfaces/IDatabase.cs b/LongoMatch.Core/Interfaces/IDatabase.cs
index e02c0f0..4773545 100644
--- a/LongoMatch.Core/Interfaces/IDatabase.cs
+++ b/LongoMatch.Core/Interfaces/IDatabase.cs
@@ -27,11 +27,11 @@ namespace LongoMatch.Interfaces
Project GetProject(Guid id);
- void AddProject(Project project);
+ bool AddProject(Project project);
- void RemoveProject(Guid id);
+ bool RemoveProject(Guid id);
- void UpdateProject(Project project);
+ bool UpdateProject(Project project);
bool Exists(Project project);
diff --git a/LongoMatch.Services/LongoMatch.Services.mdp b/LongoMatch.Services/LongoMatch.Services.mdp
index aca8068..3e956a6 100644
--- a/LongoMatch.Services/LongoMatch.Services.mdp
+++ b/LongoMatch.Services/LongoMatch.Services.mdp
@@ -14,7 +14,6 @@
</Configuration>
</Configurations>
<Contents>
- <File subtype="Code" buildaction="Compile" name="Services/DataBase.cs" />
<File subtype="Code" buildaction="Compile" name="Services/TemplatesService.cs" />
<File subtype="Code" buildaction="Compile" name="Services/RenderingJobsManager.cs" />
<File subtype="Code" buildaction="Compile" name="Services/Core.cs" />
@@ -32,6 +31,7 @@
<File subtype="Code" buildaction="Compile" name="Services/DataBaseManager.cs" />
<File subtype="Code" buildaction="Compile" name="Services/MigrationsManager.cs" />
<File subtype="Code" buildaction="Compile" name="Services/ToolsManager.cs" />
+ <File subtype="Code" buildaction="Compile" name="Services/FileDB.cs" />
</Contents>
<References>
<ProjectReference type="Gac" localcopy="True" refto="System, Version=4.0.0.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089" />
diff --git a/LongoMatch.Services/Makefile.am b/LongoMatch.Services/Makefile.am
index 6712995..48d46f2 100644
--- a/LongoMatch.Services/Makefile.am
+++ b/LongoMatch.Services/Makefile.am
@@ -5,7 +5,7 @@ LINK = $(REF_DEP_LONGOMATCH_SERVICES)
SOURCES = \
Services/Core.cs \
- Services/DataBase.cs \
+ Services/FileDB.cs \
Services/DataBaseManager.cs \
Services/EventsManager.cs \
Services/GameUnitsManager.cs \
diff --git a/LongoMatch.Services/Services/DataBaseManager.cs b/LongoMatch.Services/Services/DataBaseManager.cs
index 1ad24ba..3e8f24f 100644
--- a/LongoMatch.Services/Services/DataBaseManager.cs
+++ b/LongoMatch.Services/Services/DataBaseManager.cs
@@ -49,7 +49,7 @@ namespace LongoMatch.DB
}
public void SetActiveByName (string name) {
- foreach (DataBase db in Databases) {
+ foreach (IDatabase db in Databases) {
if (db.Name == name) {
Log.Information ("Selecting active database " + db.Name);
ActiveDB = db;
@@ -57,7 +57,7 @@ namespace LongoMatch.DB
}
}
- DataBase newdb = new DataBase(NameToFile (name));
+ IDatabase newdb = new DataBase(NameToFile (name));
Log.Information ("Creating new database " + newdb.Name);
Databases.Add (newdb);
ActiveDB = newdb;
@@ -68,7 +68,7 @@ namespace LongoMatch.DB
throw new Exception("A database with the same name already exists");
}
try {
- DataBase newdb = new DataBase(NameToFile (name));
+ IDatabase newdb = new DataBase(NameToFile (name));
Log.Information ("Creating new database " + newdb.Name);
Databases.Add (newdb);
return newdb;
@@ -111,7 +111,7 @@ namespace LongoMatch.DB
string Extension {
get {
- return SUPPORTED_MAJOR_VERSION - 1 + ".db";
+ return "ldb";
}
}
@@ -127,76 +127,17 @@ namespace LongoMatch.DB
};
}
- DataBase AddDatabase (string path) {
- DataBase db = new DataBase (path);
- if (db.Version.Major == SUPPORTED_MAJOR_VERSION) {
- Log.Information ("Found new database " + db.Name);
- }
- return db;
- }
-
- DataBase TryLoad (string path) {
- DataBase db = null;
-
- try {
- db = AddDatabase (path);
- } catch (UnknownDBErrorException ex) {
- string dbName = FileToName (path);
- string backupFile = path + ".backup";
- string errorDBDir = Path.Combine (DBDir, "error");
-
- Log.Exception (ex);
- if (guiToolkit.QuestionMessage (String.Format (
- Catalog.GetString ("The database {0} is corrupted, would you like to
restore the last backup?"),
- dbName), Catalog.GetString ("Database"), null)) {
- try {
- string errorDBFile =
Path.Combine(errorDBDir,Path.GetFileName(path));
-
- if (!Directory.Exists(errorDBDir)){
- Directory.CreateDirectory (errorDBDir);
- }
- if (File.Exists (errorDBFile)) {
- File.Delete (errorDBFile);
- }
- File.Move (path, errorDBFile);
- if (File.Exists (backupFile)) {
- File.Move (backupFile, path);
- db = AddDatabase (path);
- db.Backup ();
- guiToolkit.InfoMessage (Catalog.GetString ("Backup
recovered successfully"));
- } else {
- guiToolkit.ErrorMessage (Catalog.GetString (
- "Could not recover backup, this database will
not be used"));
- }
- } catch (UnknownDBErrorException ex2) {
- Log.Exception (ex2);
- if (File.Exists (path)) {
- string errorDBFileB =
Path.Combine(errorDBDir,Path.GetFileName(backupFile));
-
- if (File.Exists (errorDBFileB)) {
- File.Delete (errorDBFileB);
- }
- File.Move (path, errorDBFileB);
- }
- guiToolkit.ErrorMessage (Catalog.GetString (
- "Could not recover backup, this database will not be
used"));
- }
- }
- }
- return db;
- }
-
void FindDBS (){
Databases = new List<IDatabase>();
+ DirectoryInfo dbdir = new DirectoryInfo (DBDir);
- var paths = Directory.GetFiles(this.DBDir).Where
- (f => f.EndsWith(Extension)).ToList();
-
- foreach (string p in paths) {
- DataBase db = TryLoad (p);
- if (db != null) {
- Databases.Add (db);
- Log.Error ("Adding db " + db);
+ foreach (DirectoryInfo subdir in dbdir.GetDirectories()) {
+ if (subdir.FullName.EndsWith (".ldb")) {
+ IDatabase db = new DataBase (subdir.FullName);
+ if (db != null) {
+ Log.Information ("Found database " + db.Name);
+ Databases.Add (db);
+ }
}
}
}
diff --git a/LongoMatch.Services/Services/FileDB.cs b/LongoMatch.Services/Services/FileDB.cs
index 11ddc67..ea8618d 100644
--- a/LongoMatch.Services/Services/FileDB.cs
+++ b/LongoMatch.Services/Services/FileDB.cs
@@ -18,77 +18,256 @@
using System;
using System.Collections.Generic;
using System.IO;
+using System.Linq;
using LongoMatch.Common;
using LongoMatch.Interfaces;
using LongoMatch.Store;
-namespace LongoMatch.Services
+namespace LongoMatch.DB
{
- public class FileDB: IDatabase
+ public class DataBase: IDatabase
{
- const string DESC = "desc";
- const string PROJECTS = "projects";
+ LiteDB projectsDB;
+ string dbDirPath;
+ string dbPath;
+ string dbName;
+ TimeSpan maxDaysWithoutBackup = new TimeSpan(5, 0, 0, 0);
- string desc_path;
- string project_path;
-
- public FileDB (string filename)
+ public DataBase (string dbDirPath)
{
- desc_path = Path.Combine(filename, DESC);
- project_path = Path.Combine(filename, PROJECTS);
+ dbName = Path.GetFileNameWithoutExtension (dbDirPath);
+ dbPath = Path.Combine (dbDirPath, Path.GetFileName (dbDirPath));
+ this.dbDirPath = dbDirPath;
- if (!Directory.Exists(desc_path))
- Directory.CreateDirectory(desc_path);
- if (!Directory.Exists(project_path))
- Directory.CreateDirectory(project_path);
+ if (!Directory.Exists(dbDirPath)) {
+ Directory.CreateDirectory(dbDirPath);
+ }
+ if (File.Exists(dbDirPath)) {
+ try {
+ projectsDB = SerializableObject.Load<LiteDB> (dbDirPath);
+ }
+ catch (Exception e){
+ Log.Exception (e);
+ }
+ }
+ if (projectsDB == null) {
+ ReloadDB ();
+ }
+ DateTime now = DateTime.UtcNow;
+ if (projectsDB.LastBackup + maxDaysWithoutBackup < now) {
+ Backup ();
+ }
+ }
+
+ /// <value>
+ /// The database version
+ /// </value>
+ public Version Version {
+ get {
+ return projectsDB.Version;
+ }
+ set {
+ projectsDB.Version = value;
+ }
+ }
+
+ public string Name {
+ get {
+ return dbName;
+ }
+ }
+
+ public DateTime LastBackup {
+ get {
+ return projectsDB.LastBackup;
+ }
+ }
+
+ public int Count {
+ get {
+ return projectsDB.Projects.Count;
+ }
+ }
+
+ public bool Exists (Project project) {
+ bool ret = false;
+ if (projectsDB.ProjectsDict.ContainsKey (project.UUID)) {
+ if (File.Exists (Path.Combine (dbDirPath, project.UUID.ToString()))) {
+ ret = true;
+ }
+ }
+ return ret;
+ }
+
+ public bool Backup () {
+ DirectoryInfo backupDir, dbDir;
+ FileInfo[] files;
+ dbDir = new DirectoryInfo (dbDirPath);
+ backupDir = new DirectoryInfo (dbDirPath + ".backup");
+ try {
+ if (backupDir.Exists) {
+ backupDir.Delete ();
+ }
+ backupDir.Create ();
+ files = dbDir.GetFiles ();
+ foreach (FileInfo file in files)
+ {
+ string temppath = Path.Combine (backupDir.FullName, file.Name);
+ file.CopyTo (temppath, false);
+ }
+ projectsDB.LastBackup = DateTime.Now;
+ projectsDB.Save ();
+ return true;
+ } catch (Exception ex) {
+ return false;
+ }
}
- public List<ProjectDescription> GetAllProjects() {
- List<ProjectDescription> list = new List<ProjectDescription>();
- foreach (string path in Directory.GetFiles(desc_path)) {
- if (File.Exists(Path.Combine(project_path, Path.GetFileName(path))))
- list.Add(SerializableObject.Load<ProjectDescription>(path));
+ public bool Delete () {
+ try {
+ Directory.Delete (dbDirPath, true);
+ return true;
+ } catch (Exception ex) {
+ Log.Exception (ex);
+ return false;
}
- return list;
+ }
+
+
+ public List<ProjectDescription> GetAllProjects() {
+ return projectsDB.Projects;
}
- public Project GetProject(Guid id) {
- string path = Path.Combine(project_path, id.ToString());
- if (File.Exists(path))
- return SerializableObject.Load<Project>(path);
+ public Project GetProject (Guid id) {
+ try {
+ string projectFile = Path.Combine (dbDirPath, id.ToString());
+ if (File.Exists (projectFile)) {
+ return SerializableObject.Load<Project> (projectFile);
+ }
+ } catch (Exception ex) {
+ Log.Exception (ex);
+ }
return null;
}
- public void AddProject(Project project){
- string path = Path.Combine(project_path, project.UUID.ToString());
+ public bool AddProject(Project project){
+ string projectFile;
- project.Description.LastModified = DateTime.Now;
- if (File.Exists(path))
- File.Delete(path);
- SerializableObject.Save(project, path);
- SerializableObject.Save(project.Description, Path.Combine(desc_path,
project.UUID.ToString()));
+ try {
+ projectFile = Path.Combine (dbDirPath, project.UUID.ToString());
+ project.Description.LastModified = DateTime.Now;
+ projectsDB.Add (project.Description);
+ try {
+ if (File.Exists(projectFile))
+ File.Delete(projectFile);
+ SerializableObject.Save(project, projectFile);
+ } catch (Exception ex) {
+ Log.Exception (ex);
+ projectsDB.Delete (project.Description.UUID);
+ }
+ return true;
+ } catch (Exception ex) {
+ Log.Exception (ex);
+ return false;
+ }
}
- public void RemoveProject(Guid id) {
- string path = Path.Combine(project_path, id.ToString());
- if (File.Exists(path))
- File.Delete(path);
-
- path = Path.Combine(desc_path, id.ToString());
- if (File.Exists(path))
- File.Delete(path);
+ public bool RemoveProject(Guid id) {
+ string projectFile;
+
+ projectFile = Path.Combine (dbDirPath, id.ToString());
+ try {
+ if (File.Exists (projectFile)) {
+ File.Delete (projectFile);
+ }
+ projectsDB.Delete (id);
+ return true;
+ } catch (Exception ex) {
+ Log.Exception (ex);
+ return false;
+ }
}
- public void UpdateProject(Project project) {
+ public bool UpdateProject (Project project) {
project.Description.LastModified = DateTime.Now;
- AddProject(project);
+ return AddProject (project);
}
- public bool Exists(Project project) {
- return File.Exists(Path.Combine(desc_path, project.UUID.ToString())) &&
- File.Exists(Path.Combine(project_path, project.UUID.ToString()));
+ void ReloadDB () {
+ projectsDB = new LiteDB (dbPath);
+ DirectoryInfo dbDir = new DirectoryInfo (dbDirPath);
+ foreach (FileInfo file in dbDir.GetFiles ()) {
+ if (file.FullName == dbPath) {
+ continue;
+ }
+ try {
+ Project project = SerializableObject.Load<Project> (file.FullName);
+ projectsDB.Add (project.Description);
+ } catch (Exception ex) {
+ Log.Exception (ex);
+ }
+ }
+ projectsDB.Save ();
+ }
+
+ }
+
+ [Serializable]
+ class LiteDB
+ {
+ string dbPath;
+
+ public LiteDB (string dbPath) {
+ this.dbPath = dbPath;
+ ProjectsDict = new Dictionary <Guid, ProjectDescription>();
+ Version = new System.Version (Constants.DB_MAYOR_VERSION,
+ Constants.DB_MINOR_VERSION);
+ LastBackup = DateTime.Now;
+ }
+
+ public LiteDB () { }
+
+ public Version Version {get; set;}
+
+ public Dictionary<Guid, ProjectDescription> ProjectsDict {get; set;}
+
+ public DateTime LastBackup {get; set;}
+
+ public List<ProjectDescription> Projects {
+ get {
+ return ProjectsDict.Select (d => d.Value).ToList();
+ }
+ }
+
+ public bool Add (ProjectDescription desc) {
+ if (ProjectsDict.ContainsKey (desc.UUID)) {
+ ProjectsDict[desc.UUID] = desc;
+ } else {
+ ProjectsDict.Add (desc.UUID, desc);
+ }
+ return Save ();
+ }
+
+ public bool Delete (Guid uuid) {
+ if (ProjectsDict.ContainsKey (uuid)) {
+ ProjectsDict.Remove (uuid);
+ return Save ();
+ }
+ return false;
+ }
+
+ public bool Save () {
+ bool ret = false;
+
+ try {
+ SerializableObject.Save (this, dbPath);
+ ret = true;
+ } catch (Exception ex) {
+ Log.Exception (ex);
+ }
+ return ret;
}
}
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]