2013-03-25 06:51:32 +03:00
|
|
|
using System;
|
|
|
|
using Marr.Data;
|
2013-11-28 10:11:05 +03:00
|
|
|
using NLog;
|
|
|
|
using NzbDrone.Common.Instrumentation;
|
2013-03-25 06:51:32 +03:00
|
|
|
|
|
|
|
namespace NzbDrone.Core.Datastore
|
|
|
|
{
|
|
|
|
public interface IDatabase
|
|
|
|
{
|
2013-07-16 03:45:49 +03:00
|
|
|
IDataMapper GetDataMapper();
|
2013-11-28 10:11:05 +03:00
|
|
|
Version Version { get; }
|
|
|
|
void Vacuum();
|
2013-03-25 06:51:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public class Database : IDatabase
|
|
|
|
{
|
2015-05-03 20:52:07 +02:00
|
|
|
private readonly string _databaseName;
|
2013-11-28 10:52:22 +03:00
|
|
|
private readonly Func<IDataMapper> _datamapperFactory;
|
2013-03-25 06:51:32 +03:00
|
|
|
|
2014-12-17 09:12:26 +02:00
|
|
|
private readonly Logger _logger = NzbDroneLogger.GetLogger(typeof(Database));
|
2013-11-28 10:11:05 +03:00
|
|
|
|
2015-05-03 20:52:07 +02:00
|
|
|
public Database(string databaseName, Func<IDataMapper> datamapperFactory)
|
2013-03-25 06:51:32 +03:00
|
|
|
{
|
2015-05-03 20:52:07 +02:00
|
|
|
_databaseName = databaseName;
|
2013-11-28 10:52:22 +03:00
|
|
|
_datamapperFactory = datamapperFactory;
|
2013-03-25 06:51:32 +03:00
|
|
|
}
|
|
|
|
|
2013-11-28 10:52:22 +03:00
|
|
|
|
2013-07-16 03:45:49 +03:00
|
|
|
public IDataMapper GetDataMapper()
|
2013-05-11 23:06:57 +03:00
|
|
|
{
|
2013-11-28 10:52:22 +03:00
|
|
|
return _datamapperFactory();
|
2013-11-28 10:11:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public Version Version
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
2013-11-28 10:52:22 +03:00
|
|
|
var version = _datamapperFactory().ExecuteScalar("SELECT sqlite_version()").ToString();
|
2013-11-28 10:11:05 +03:00
|
|
|
return new Version(version);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Vacuum()
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2015-05-03 20:52:07 +02:00
|
|
|
_logger.Info("Vacuuming {0} database", _databaseName);
|
2013-11-28 10:52:22 +03:00
|
|
|
_datamapperFactory().ExecuteNonQuery("Vacuum;");
|
2015-05-03 20:52:07 +02:00
|
|
|
_logger.Info("{0} database compressed", _databaseName);
|
2013-11-28 10:11:05 +03:00
|
|
|
}
|
|
|
|
catch (Exception e)
|
|
|
|
{
|
2014-12-17 09:12:26 +02:00
|
|
|
_logger.Error("An Error occurred while vacuuming database.", e);
|
2013-11-28 10:11:05 +03:00
|
|
|
}
|
2013-05-11 23:06:57 +03:00
|
|
|
}
|
2013-03-25 06:51:32 +03:00
|
|
|
}
|
|
|
|
}
|