New: Main DB is compressed on app start

This commit is contained in:
kayone 2013-11-27 23:11:05 -08:00 committed by Mark McDowall
parent ca22ee3af3
commit 9370de0cc0
7 changed files with 94 additions and 35 deletions

View File

@ -4,6 +4,7 @@ using NzbDrone.Common;
using NzbDrone.Api.Extensions;
using NzbDrone.Common.EnvironmentInfo;
using NzbDrone.Core.Configuration;
using NzbDrone.Core.Datastore;
namespace NzbDrone.Api.System
{
@ -13,14 +14,16 @@ namespace NzbDrone.Api.System
private readonly IRuntimeInfo _runtimeInfo;
private readonly IRouteCacheProvider _routeCacheProvider;
private readonly IConfigFileProvider _configFileProvider;
private readonly IDatabase _database;
public SystemModule(IAppFolderInfo appFolderInfo, IRuntimeInfo runtimeInfo, IRouteCacheProvider routeCacheProvider, IConfigFileProvider configFileProvider)
public SystemModule(IAppFolderInfo appFolderInfo, IRuntimeInfo runtimeInfo, IRouteCacheProvider routeCacheProvider, IConfigFileProvider configFileProvider, IDatabase database)
: base("system")
{
_appFolderInfo = appFolderInfo;
_runtimeInfo = runtimeInfo;
_routeCacheProvider = routeCacheProvider;
_configFileProvider = configFileProvider;
_database = database;
Get["/status"] = x => GetStatus();
Get["/routes"] = x => GetRoutes();
}
@ -44,6 +47,7 @@ namespace NzbDrone.Api.System
Branch = _configFileProvider.Branch,
Authentication = _configFileProvider.AuthenticationEnabled,
StartOfWeek = (int)OsInfo.FirstDayOfWeek,
SqliteVersion = _database.Version,
UrlBase = _configFileProvider.UrlBase
}.AsResponse();

View File

@ -0,0 +1,36 @@
using System;
using System.Linq;
using FluentAssertions;
using NUnit.Framework;
using NzbDrone.Core.Datastore;
using NzbDrone.Core.Test.Framework;
using NzbDrone.Core.Tv;
namespace NzbDrone.Core.Test.Datastore
{
public class DatabaseFixture : DbTest
{
[Test]
public void SingleOrDefault_should_return_null_on_empty_db()
{
Mocker.Resolve<IDatabase>()
.GetDataMapper().Query<Series>()
.SingleOrDefault(c => c.CleanTitle == "SomeTitle")
.Should()
.BeNull();
}
[Test]
public void vaccume()
{
Mocker.Resolve<IDatabase>().Vacuum();
}
[Test]
public void get_version()
{
Mocker.Resolve<IDatabase>().Version.Should().BeGreaterThan(new Version("3.0.0"));
}
}
}

View File

@ -7,24 +7,9 @@ using NUnit.Framework;
using NzbDrone.Core.Datastore;
using NzbDrone.Core.Jobs;
using NzbDrone.Core.Test.Framework;
using NzbDrone.Core.Tv;
namespace NzbDrone.Core.Test.Datastore
{
public class DatabaseFixture : DbTest
{
[Test]
public void SingleOrDefault_should_return_null_on_empty_db()
{
Mocker.Resolve<IDatabase>()
.GetDataMapper().Query<Series>()
.SingleOrDefault(c => c.CleanTitle == "SomeTitle")
.Should()
.BeNull();
}
}
[TestFixture]
public class ObjectDatabaseFixture : DbTest<BasicRepository<ScheduledTask>, ScheduledTask>
{

View File

@ -106,6 +106,7 @@
<Compile Include="DataAugmentationFixture\Scene\SceneMappingServiceFixture.cs" />
<Compile Include="Datastore\BasicRepositoryFixture.cs" />
<Compile Include="Datastore\Converters\ProviderSettingConverterFixture.cs" />
<Compile Include="Datastore\DatabaseFixture.cs" />
<Compile Include="Datastore\DatabaseRelationshipFixture.cs" />
<Compile Include="Datastore\MappingExtentionFixture.cs" />
<Compile Include="Datastore\ObjectDatabaseFixture.cs" />

View File

@ -27,7 +27,7 @@ namespace NzbDrone.Core.Datastore
void InsertMany(IList<TModel> model);
void UpdateMany(IList<TModel> model);
void DeleteMany(List<TModel> model);
void Purge();
void Purge(bool vacuum = false);
bool HasItems();
void DeleteMany(IEnumerable<int> ids);
void SetFields(TModel model, params Expression<Func<TModel, object>>[] properties);
@ -216,9 +216,18 @@ namespace NzbDrone.Core.Datastore
}
}
public void Purge()
public void Purge(bool vacuum = false)
{
DataMapper.Delete<TModel>(c => c.Id > -1);
if (vacuum)
{
Vacuum();
}
}
protected void Vacuum()
{
_database.Vacuum();
}
public bool HasItems()
@ -258,11 +267,6 @@ namespace NzbDrone.Core.Datastore
.Take(pagingSpec.PageSize);
}
public void DeleteAll()
{
DataMapper.Delete<TModel>(c => c.Id > 0);
}
protected void ModelCreated(TModel model)
{
PublishModelEvent(model, ModelAction.Created);

View File

@ -1,25 +1,58 @@
using System;
using System.Data.SQLite;
using Marr.Data;
using NLog;
using NzbDrone.Common.Instrumentation;
namespace NzbDrone.Core.Datastore
{
public interface IDatabase
{
IDataMapper GetDataMapper();
Version Version { get; }
void Vacuum();
}
public class Database : IDatabase
{
private readonly Func<IDataMapper> _dataMapperFactory;
private readonly string _connectionString;
public Database(Func<IDataMapper> dataMapperFactory)
private Logger logger = NzbDroneLogger.GetLogger();
public Database(string connectionString)
{
_dataMapperFactory = dataMapperFactory;
_connectionString = connectionString;
}
public IDataMapper GetDataMapper()
{
return _dataMapperFactory();
return new DataMapper(SQLiteFactory.Instance, _connectionString)
{
SqlMode = SqlModes.Text,
};
}
public Version Version
{
get
{
var version = GetDataMapper().ExecuteScalar("SELECT sqlite_version()").ToString();
return new Version(version);
}
}
public void Vacuum()
{
try
{
logger.Info("Vacuuming database " + _connectionString);
GetDataMapper().ExecuteNonQuery("Vacuum;");
logger.Info("Database Compressed " + _connectionString);
}
catch (Exception e)
{
logger.Error("An Error occurred while vacuuming database. " + _connectionString, e);
}
}
}
}

View File

@ -73,15 +73,11 @@ namespace NzbDrone.Core.Datastore
_migrationController.MigrateToLatest(connectionString, migrationType);
return new Database(() =>
{
var dataMapper = new DataMapper(SQLiteFactory.Instance, connectionString)
{
SqlMode = SqlModes.Text,
};
var db = new Database(connectionString);
return dataMapper;
});
db.Vacuum();
return db;
}
}
}