New: Main DB is compressed on app start
This commit is contained in:
parent
ca22ee3af3
commit
9370de0cc0
|
@ -4,6 +4,7 @@ using NzbDrone.Common;
|
||||||
using NzbDrone.Api.Extensions;
|
using NzbDrone.Api.Extensions;
|
||||||
using NzbDrone.Common.EnvironmentInfo;
|
using NzbDrone.Common.EnvironmentInfo;
|
||||||
using NzbDrone.Core.Configuration;
|
using NzbDrone.Core.Configuration;
|
||||||
|
using NzbDrone.Core.Datastore;
|
||||||
|
|
||||||
namespace NzbDrone.Api.System
|
namespace NzbDrone.Api.System
|
||||||
{
|
{
|
||||||
|
@ -13,14 +14,16 @@ namespace NzbDrone.Api.System
|
||||||
private readonly IRuntimeInfo _runtimeInfo;
|
private readonly IRuntimeInfo _runtimeInfo;
|
||||||
private readonly IRouteCacheProvider _routeCacheProvider;
|
private readonly IRouteCacheProvider _routeCacheProvider;
|
||||||
private readonly IConfigFileProvider _configFileProvider;
|
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")
|
: base("system")
|
||||||
{
|
{
|
||||||
_appFolderInfo = appFolderInfo;
|
_appFolderInfo = appFolderInfo;
|
||||||
_runtimeInfo = runtimeInfo;
|
_runtimeInfo = runtimeInfo;
|
||||||
_routeCacheProvider = routeCacheProvider;
|
_routeCacheProvider = routeCacheProvider;
|
||||||
_configFileProvider = configFileProvider;
|
_configFileProvider = configFileProvider;
|
||||||
|
_database = database;
|
||||||
Get["/status"] = x => GetStatus();
|
Get["/status"] = x => GetStatus();
|
||||||
Get["/routes"] = x => GetRoutes();
|
Get["/routes"] = x => GetRoutes();
|
||||||
}
|
}
|
||||||
|
@ -44,6 +47,7 @@ namespace NzbDrone.Api.System
|
||||||
Branch = _configFileProvider.Branch,
|
Branch = _configFileProvider.Branch,
|
||||||
Authentication = _configFileProvider.AuthenticationEnabled,
|
Authentication = _configFileProvider.AuthenticationEnabled,
|
||||||
StartOfWeek = (int)OsInfo.FirstDayOfWeek,
|
StartOfWeek = (int)OsInfo.FirstDayOfWeek,
|
||||||
|
SqliteVersion = _database.Version,
|
||||||
UrlBase = _configFileProvider.UrlBase
|
UrlBase = _configFileProvider.UrlBase
|
||||||
}.AsResponse();
|
}.AsResponse();
|
||||||
|
|
||||||
|
|
|
@ -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"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -7,24 +7,9 @@ using NUnit.Framework;
|
||||||
using NzbDrone.Core.Datastore;
|
using NzbDrone.Core.Datastore;
|
||||||
using NzbDrone.Core.Jobs;
|
using NzbDrone.Core.Jobs;
|
||||||
using NzbDrone.Core.Test.Framework;
|
using NzbDrone.Core.Test.Framework;
|
||||||
using NzbDrone.Core.Tv;
|
|
||||||
|
|
||||||
namespace NzbDrone.Core.Test.Datastore
|
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]
|
[TestFixture]
|
||||||
public class ObjectDatabaseFixture : DbTest<BasicRepository<ScheduledTask>, ScheduledTask>
|
public class ObjectDatabaseFixture : DbTest<BasicRepository<ScheduledTask>, ScheduledTask>
|
||||||
{
|
{
|
||||||
|
|
|
@ -106,6 +106,7 @@
|
||||||
<Compile Include="DataAugmentationFixture\Scene\SceneMappingServiceFixture.cs" />
|
<Compile Include="DataAugmentationFixture\Scene\SceneMappingServiceFixture.cs" />
|
||||||
<Compile Include="Datastore\BasicRepositoryFixture.cs" />
|
<Compile Include="Datastore\BasicRepositoryFixture.cs" />
|
||||||
<Compile Include="Datastore\Converters\ProviderSettingConverterFixture.cs" />
|
<Compile Include="Datastore\Converters\ProviderSettingConverterFixture.cs" />
|
||||||
|
<Compile Include="Datastore\DatabaseFixture.cs" />
|
||||||
<Compile Include="Datastore\DatabaseRelationshipFixture.cs" />
|
<Compile Include="Datastore\DatabaseRelationshipFixture.cs" />
|
||||||
<Compile Include="Datastore\MappingExtentionFixture.cs" />
|
<Compile Include="Datastore\MappingExtentionFixture.cs" />
|
||||||
<Compile Include="Datastore\ObjectDatabaseFixture.cs" />
|
<Compile Include="Datastore\ObjectDatabaseFixture.cs" />
|
||||||
|
|
|
@ -27,7 +27,7 @@ namespace NzbDrone.Core.Datastore
|
||||||
void InsertMany(IList<TModel> model);
|
void InsertMany(IList<TModel> model);
|
||||||
void UpdateMany(IList<TModel> model);
|
void UpdateMany(IList<TModel> model);
|
||||||
void DeleteMany(List<TModel> model);
|
void DeleteMany(List<TModel> model);
|
||||||
void Purge();
|
void Purge(bool vacuum = false);
|
||||||
bool HasItems();
|
bool HasItems();
|
||||||
void DeleteMany(IEnumerable<int> ids);
|
void DeleteMany(IEnumerable<int> ids);
|
||||||
void SetFields(TModel model, params Expression<Func<TModel, object>>[] properties);
|
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);
|
DataMapper.Delete<TModel>(c => c.Id > -1);
|
||||||
|
if (vacuum)
|
||||||
|
{
|
||||||
|
Vacuum();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void Vacuum()
|
||||||
|
{
|
||||||
|
_database.Vacuum();
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool HasItems()
|
public bool HasItems()
|
||||||
|
@ -258,11 +267,6 @@ namespace NzbDrone.Core.Datastore
|
||||||
.Take(pagingSpec.PageSize);
|
.Take(pagingSpec.PageSize);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void DeleteAll()
|
|
||||||
{
|
|
||||||
DataMapper.Delete<TModel>(c => c.Id > 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected void ModelCreated(TModel model)
|
protected void ModelCreated(TModel model)
|
||||||
{
|
{
|
||||||
PublishModelEvent(model, ModelAction.Created);
|
PublishModelEvent(model, ModelAction.Created);
|
||||||
|
|
|
@ -1,25 +1,58 @@
|
||||||
using System;
|
using System;
|
||||||
|
using System.Data.SQLite;
|
||||||
using Marr.Data;
|
using Marr.Data;
|
||||||
|
using NLog;
|
||||||
|
using NzbDrone.Common.Instrumentation;
|
||||||
|
|
||||||
namespace NzbDrone.Core.Datastore
|
namespace NzbDrone.Core.Datastore
|
||||||
{
|
{
|
||||||
public interface IDatabase
|
public interface IDatabase
|
||||||
{
|
{
|
||||||
IDataMapper GetDataMapper();
|
IDataMapper GetDataMapper();
|
||||||
|
Version Version { get; }
|
||||||
|
void Vacuum();
|
||||||
}
|
}
|
||||||
|
|
||||||
public class Database : IDatabase
|
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()
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -73,15 +73,11 @@ namespace NzbDrone.Core.Datastore
|
||||||
|
|
||||||
_migrationController.MigrateToLatest(connectionString, migrationType);
|
_migrationController.MigrateToLatest(connectionString, migrationType);
|
||||||
|
|
||||||
return new Database(() =>
|
var db = new Database(connectionString);
|
||||||
{
|
|
||||||
var dataMapper = new DataMapper(SQLiteFactory.Instance, connectionString)
|
|
||||||
{
|
|
||||||
SqlMode = SqlModes.Text,
|
|
||||||
};
|
|
||||||
|
|
||||||
return dataMapper;
|
db.Vacuum();
|
||||||
});
|
|
||||||
|
return db;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue