1
0
mirror of https://github.com/Radarr/Radarr.git synced 2024-11-04 10:02:40 +01:00

Fixed: A lot of memory issues, such as IDisposable leakage and leaking all the media covers. (#3227)

Thanks again to @Taloth
This commit is contained in:
Leonardo Galli 2019-01-10 08:36:34 +01:00 committed by GitHub
commit 02eb30b8ee
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
50 changed files with 515 additions and 386 deletions

2
.gitignore vendored
View File

@ -104,7 +104,7 @@ _TeamCity*
# Radarr
Backups/
logs/
MediaCover/
#MediaCover/
UpdateLogs/
xdg/
config.xml

0
debian/rules vendored Executable file → Normal file
View File

View File

@ -941,21 +941,21 @@ protected virtual void Dispose(bool disposing)
{
if (disposing)
{
if (Command.Transaction != null)
if (_command != null)
{
Command.Transaction.Dispose();
Command.Transaction = null;
}
if (_command.Transaction != null)
{
_command.Transaction.Dispose();
_command.Transaction = null;
}
if (Command.Connection != null)
{
Command.Connection.Dispose();
Command.Connection = null;
}
if (_command.Connection != null)
{
_command.Connection.Dispose();
_command.Connection = null;
}
if (Command != null)
{
Command.Dispose();
_command.Dispose();
_command = null;
}
}

View File

@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Data.Common;
@ -91,9 +91,11 @@ private void PrepareLazyLoadedProperties(object ent)
Type entType = ent.GetType();
if (_repos.Relationships.ContainsKey(entType))
{
var provider = _db.ProviderFactory;
var connectionString = _db.ConnectionString;
Func<IDataMapper> dbCreate = () =>
{
var db = new DataMapper(_db.ProviderFactory, _db.ConnectionString);
var db = new DataMapper(provider, connectionString);
db.SqlMode = SqlModes.Text;
return db;
};

View File

@ -123,27 +123,6 @@ public bool CanUseGDIPlus()
}
}
public bool IsValidGDIPlusImage(string filename)
{
if (!CanUseGDIPlus())
{
return true;
}
try
{
using (var bmp = new Bitmap(filename))
{
}
return true;
}
catch (Exception ex)
{
Logger.Debug(ex, "Corrupted image found at: {0}.", filename);
return false;
}
}
public bool FolderWritable(string path)
{
Ensure.That(path, () => path).IsValidPath();

View File

@ -20,7 +20,6 @@ public interface IDiskProvider
bool FileExists(string path);
bool FileExists(string path, StringComparison stringComparison);
bool CanUseGDIPlus();
bool IsValidGDIPlusImage(string path);
bool FolderWritable(string path);
string[] GetDirectories(string path);
string[] GetFiles(string path, SearchOption searchOption);

View File

@ -66,95 +66,96 @@ public HttpResponse GetResponse(HttpRequest request, CookieContainer cookies)
lock (CurlGlobalHandle.Instance)
{
Stream responseStream = new MemoryStream();
Stream headerStream = new MemoryStream();
using (var curlEasy = new CurlEasy())
using (Stream responseStream = new MemoryStream())
using (Stream headerStream = new MemoryStream())
{
curlEasy.AutoReferer = false;
curlEasy.WriteFunction = (b, s, n, o) =>
using (var curlEasy = new CurlEasy())
{
responseStream.Write(b, 0, s * n);
return s * n;
};
curlEasy.HeaderFunction = (b, s, n, o) =>
{
headerStream.Write(b, 0, s * n);
return s * n;
};
AddProxy(curlEasy, request);
curlEasy.Url = request.Url.FullUri;
switch (request.Method)
{
case HttpMethod.GET:
curlEasy.HttpGet = true;
break;
case HttpMethod.POST:
curlEasy.Post = true;
break;
case HttpMethod.PUT:
curlEasy.Put = true;
break;
default:
throw new NotSupportedException(string.Format("HttpCurl method {0} not supported", request.Method));
}
curlEasy.FollowLocation = false;
curlEasy.UserAgent = request.UseSimplifiedUserAgent ? UserAgentBuilder.UserAgentSimplified : UserAgentBuilder.UserAgent; ;
if (request.RequestTimeout != TimeSpan.Zero)
{
curlEasy.Timeout = (int)Math.Ceiling(request.RequestTimeout.TotalSeconds);
}
if (OsInfo.IsWindows)
{
curlEasy.CaInfo = _caBundleFilePath;
}
if (cookies != null)
{
curlEasy.Cookie = cookies.GetCookieHeader((Uri)request.Url);
}
if (request.ContentData != null)
{
curlEasy.PostFieldSize = request.ContentData.Length;
curlEasy.SetOpt(CurlOption.CopyPostFields, new string(Array.ConvertAll(request.ContentData, v => (char)v)));
}
// Yes, we have to keep a ref to the object to prevent corrupting the unmanaged state
using (var httpRequestHeaders = SerializeHeaders(request))
{
curlEasy.HttpHeader = httpRequestHeaders;
var result = curlEasy.Perform();
if (result != CurlCode.Ok)
curlEasy.AutoReferer = false;
curlEasy.WriteFunction = (b, s, n, o) =>
{
switch (result)
responseStream.Write(b, 0, s * n);
return s * n;
};
curlEasy.HeaderFunction = (b, s, n, o) =>
{
headerStream.Write(b, 0, s * n);
return s * n;
};
AddProxy(curlEasy, request);
curlEasy.Url = request.Url.FullUri;
switch (request.Method)
{
case HttpMethod.GET:
curlEasy.HttpGet = true;
break;
case HttpMethod.POST:
curlEasy.Post = true;
break;
case HttpMethod.PUT:
curlEasy.Put = true;
break;
default:
throw new NotSupportedException(string.Format("HttpCurl method {0} not supported", request.Method));
}
curlEasy.FollowLocation = false;
curlEasy.UserAgent = request.UseSimplifiedUserAgent ? UserAgentBuilder.UserAgentSimplified : UserAgentBuilder.UserAgent; ;
if (request.RequestTimeout != TimeSpan.Zero)
{
curlEasy.Timeout = (int)Math.Ceiling(request.RequestTimeout.TotalSeconds);
}
if (OsInfo.IsWindows)
{
curlEasy.CaInfo = _caBundleFilePath;
}
if (cookies != null)
{
curlEasy.Cookie = cookies.GetCookieHeader((Uri)request.Url);
}
if (request.ContentData != null)
{
curlEasy.PostFieldSize = request.ContentData.Length;
curlEasy.SetOpt(CurlOption.CopyPostFields, new string(Array.ConvertAll(request.ContentData, v => (char)v)));
}
// Yes, we have to keep a ref to the object to prevent corrupting the unmanaged state
using (var httpRequestHeaders = SerializeHeaders(request))
{
curlEasy.HttpHeader = httpRequestHeaders;
var result = curlEasy.Perform();
if (result != CurlCode.Ok)
{
case CurlCode.SslCaCert:
case (CurlCode)77:
throw new WebException(string.Format("Curl Error {0} for Url {1}, issues with your operating system SSL Root Certificate Bundle (ca-bundle).", result, curlEasy.Url));
default:
throw new WebException(string.Format("Curl Error {0} for Url {1}", result, curlEasy.Url));
switch (result)
{
case CurlCode.SslCaCert:
case (CurlCode)77:
throw new WebException(string.Format("Curl Error {0} for Url {1}, issues with your operating system SSL Root Certificate Bundle (ca-bundle).", result, curlEasy.Url));
default:
throw new WebException(string.Format("Curl Error {0} for Url {1}", result, curlEasy.Url));
}
}
}
var webHeaderCollection = ProcessHeaderStream(request, cookies, headerStream);
var responseData = ProcessResponseStream(request, responseStream, webHeaderCollection);
var httpHeader = new HttpHeader(webHeaderCollection);
return new HttpResponse(request, httpHeader, responseData, (HttpStatusCode)curlEasy.ResponseCode);
}
var webHeaderCollection = ProcessHeaderStream(request, cookies, headerStream);
var responseData = ProcessResponseStream(request, responseStream, webHeaderCollection);
var httpHeader = new HttpHeader(webHeaderCollection);
return new HttpResponse(request, httpHeader, responseData, (HttpStatusCode)curlEasy.ResponseCode);
}
}
}
@ -259,6 +260,7 @@ private string FixSetCookieHeader(string setCookie)
private byte[] ProcessResponseStream(HttpRequest request, Stream responseStream, WebHeaderCollection webHeaderCollection)
{
byte[] bytes = null;
responseStream.Position = 0;
if (responseStream.Length != 0)
@ -268,21 +270,27 @@ private byte[] ProcessResponseStream(HttpRequest request, Stream responseStream,
{
if (encoding.IndexOf("gzip") != -1)
{
responseStream = new GZipStream(responseStream, CompressionMode.Decompress);
using (var zipStream = new GZipStream(responseStream, CompressionMode.Decompress))
{
bytes = zipStream.ToBytes();
}
webHeaderCollection.Remove("Content-Encoding");
}
else if (encoding.IndexOf("deflate") != -1)
{
responseStream = new DeflateStream(responseStream, CompressionMode.Decompress);
using (var deflateStream = new DeflateStream(responseStream, CompressionMode.Decompress))
{
bytes = deflateStream.ToBytes();
}
webHeaderCollection.Remove("Content-Encoding");
}
}
}
return responseStream.ToBytes();
if (bytes == null) bytes = responseStream.ToBytes();
return bytes;
}
}

View File

@ -20,72 +20,84 @@ public ManagedHttpDispatcher(IHttpProxySettingsProvider proxySettingsProvider, I
public HttpResponse GetResponse(HttpRequest request, CookieContainer cookies)
{
var webRequest = (HttpWebRequest)WebRequest.Create((Uri)request.Url);
// Deflate is not a standard and could break depending on implementation.
// we should just stick with the more compatible Gzip
//http://stackoverflow.com/questions/8490718/how-to-decompress-stream-deflated-with-java-util-zip-deflater-in-net
webRequest.AutomaticDecompression = DecompressionMethods.GZip;
webRequest.Method = request.Method.ToString();
webRequest.UserAgent = request.UseSimplifiedUserAgent ? UserAgentBuilder.UserAgentSimplified : UserAgentBuilder.UserAgent;
webRequest.KeepAlive = request.ConnectionKeepAlive;
webRequest.AllowAutoRedirect = false;
webRequest.CookieContainer = cookies;
if (request.RequestTimeout != TimeSpan.Zero)
{
webRequest.Timeout = (int)Math.Ceiling(request.RequestTimeout.TotalMilliseconds);
}
AddProxy(webRequest, request);
if (request.Headers != null)
{
AddRequestHeaders(webRequest, request.Headers);
}
if (request.ContentData != null)
{
webRequest.ContentLength = request.ContentData.Length;
using (var writeStream = webRequest.GetRequestStream())
{
writeStream.Write(request.ContentData, 0, request.ContentData.Length);
}
}
HttpWebResponse httpWebResponse;
HttpWebResponse httpWebResponse = null;
HttpWebRequest webRequest = null;
try
{
httpWebResponse = (HttpWebResponse)webRequest.GetResponse();
webRequest = (HttpWebRequest) WebRequest.Create((Uri) request.Url);
// Deflate is not a standard and could break depending on implementation.
// we should just stick with the more compatible Gzip
//http://stackoverflow.com/questions/8490718/how-to-decompress-stream-deflated-with-java-util-zip-deflater-in-net
webRequest.AutomaticDecompression = DecompressionMethods.GZip;
webRequest.Method = request.Method.ToString();
webRequest.UserAgent = request.UseSimplifiedUserAgent
? UserAgentBuilder.UserAgentSimplified
: UserAgentBuilder.UserAgent;
webRequest.KeepAlive = request.ConnectionKeepAlive;
webRequest.AllowAutoRedirect = false;
webRequest.CookieContainer = cookies;
if (request.RequestTimeout != TimeSpan.Zero)
{
webRequest.Timeout = (int) Math.Ceiling(request.RequestTimeout.TotalMilliseconds);
}
AddProxy(webRequest, request);
if (request.Headers != null)
{
AddRequestHeaders(webRequest, request.Headers);
}
if (request.ContentData != null)
{
webRequest.ContentLength = request.ContentData.Length;
using (var writeStream = webRequest.GetRequestStream())
{
writeStream.Write(request.ContentData, 0, request.ContentData.Length);
}
}
try
{
httpWebResponse = (HttpWebResponse) webRequest.GetResponse();
}
catch (WebException e)
{
if (e.Status == WebExceptionStatus.SecureChannelFailure && OsInfo.IsWindows)
{
SecurityProtocolPolicy.DisableTls12();
}
httpWebResponse = (HttpWebResponse) e.Response;
if (httpWebResponse == null)
{
throw;
}
}
byte[] data = null;
using (var responseStream = httpWebResponse.GetResponseStream())
{
if (responseStream != null)
{
data = responseStream.ToBytes();
}
}
return new HttpResponse(request, new HttpHeader(httpWebResponse.Headers), data,
httpWebResponse.StatusCode);
}
catch (WebException e)
finally
{
if (e.Status == WebExceptionStatus.SecureChannelFailure && OsInfo.IsWindows)
{
SecurityProtocolPolicy.DisableTls12();
}
httpWebResponse = (HttpWebResponse)e.Response;
if (httpWebResponse == null)
{
throw;
}
webRequest = null;
(httpWebResponse as IDisposable)?.Dispose();
httpWebResponse = null;
}
byte[] data = null;
using (var responseStream = httpWebResponse.GetResponseStream())
{
if (responseStream != null)
{
data = responseStream.ToBytes();
}
}
return new HttpResponse(request, new HttpHeader(httpWebResponse.Headers), data, httpWebResponse.StatusCode);
}
protected virtual void AddProxy(HttpWebRequest webRequest, HttpRequest request)

View File

@ -226,11 +226,13 @@ public void DownloadFile(string url, string fileName)
_logger.Debug("Downloading [{0}] to [{1}]", url, fileName);
var stopWatch = Stopwatch.StartNew();
var webClient = new GZipWebClient();
webClient.Headers.Add(HttpRequestHeader.UserAgent, UserAgentBuilder.UserAgent);
webClient.DownloadFile(url, fileName);
stopWatch.Stop();
_logger.Debug("Downloading Completed. took {0:0}s", stopWatch.Elapsed.Seconds);
using (var webClient = new GZipWebClient())
{
webClient.Headers.Add(HttpRequestHeader.UserAgent, UserAgentBuilder.UserAgent);
webClient.DownloadFile(url, fileName);
stopWatch.Stop();
_logger.Debug("Downloading Completed. took {0:0}s", stopWatch.Elapsed.Seconds);
}
}
catch (WebException e)
{

View File

@ -44,6 +44,8 @@ private static void HandleAppDomainException(object sender, UnhandledExceptionEv
return;
}
}
Console.WriteLine(exception.StackTrace);
Console.WriteLine("EPIC FAIL: {0}", exception);
Logger.Fatal(exception, "EPIC FAIL: " + exception.Message);

View File

@ -37,15 +37,6 @@ private void GivenExistingFileSize(long bytes)
}
private void GivenImageFileCorrupt(bool corrupt)
{
GivenFileExistsOnDisk();
Mocker.GetMock<IDiskProvider>()
.Setup(c => c.IsValidGDIPlusImage(It.IsAny<string>()))
.Returns(!corrupt);
}
[Test]
public void should_return_false_if_file_not_exists()
{
@ -61,21 +52,11 @@ public void should_return_false_if_file_exists_but_diffrent_size()
Subject.AlreadyExists("http://url", "c:\\file.exe").Should().BeFalse();
}
[Test]
public void should_return_false_if_file_exists_and_same_size_and_corrupt()
{
GivenExistingFileSize(100);
GivenImageFileCorrupt(true);
_httpResponse.Headers.ContentLength = 100;
Subject.AlreadyExists("http://url", "c:\\file.exe").Should().BeFalse();
}
[Test]
public void should_return_true_if_file_exists_and_same_size_and_not_corrupt()
{
GivenExistingFileSize(100);
GivenImageFileCorrupt(false);
_httpResponse.Headers.ContentLength = 100;
Subject.AlreadyExists("http://url", "c:\\file.exe").Should().BeTrue();
}

View File

@ -8,6 +8,7 @@
using NzbDrone.Common.Disk;
using NzbDrone.Common.EnvironmentInfo;
using NzbDrone.Core.MediaCover;
using NzbDrone.Core.Messaging.Commands;
using NzbDrone.Core.Test.Framework;
using NzbDrone.Core.Movies;
using NzbDrone.Core.Movies.Events;
@ -28,6 +29,18 @@ public void Setup()
.With(v => v.Id = 2)
.With(v => v.Images = new List<MediaCover.MediaCover> { new MediaCover.MediaCover(MediaCoverTypes.Poster, "") })
.Build();
Mocker.GetMock<IMovieService>().Setup(m => m.GetMovie(It.Is<int>(id => id == _movie.Id))).Returns(_movie);
}
private void ExecuteAndVerifyCommand(Movie movie)
{
Subject.HandleAsync(new MovieUpdatedEvent(movie));
Mocker.GetMock<IManageCommandQueue>()
.Verify(v => v.Push(It.Is<EnsureMediaCoversCommand>(c => c.MovieId == movie.Id), It.IsAny<CommandPriority>(), It.IsAny<CommandTrigger>()), Times.Once());
Subject.Execute(new EnsureMediaCoversCommand(movie.Id));
}
[Test]
@ -76,7 +89,7 @@ public void should_resize_covers_if_main_downloaded()
.Setup(v => v.FileExists(It.IsAny<string>()))
.Returns(true);
Subject.HandleAsync(new MovieUpdatedEvent(_movie));
ExecuteAndVerifyCommand(_movie);
Mocker.GetMock<IImageResizer>()
.Verify(v => v.Resize(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<int>()), Times.Exactly(2));
@ -93,7 +106,7 @@ public void should_resize_covers_if_missing()
.Setup(v => v.FileExists(It.IsAny<string>()))
.Returns(false);
Subject.HandleAsync(new MovieUpdatedEvent(_movie));
ExecuteAndVerifyCommand(_movie);
Mocker.GetMock<IImageResizer>()
.Verify(v => v.Resize(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<int>()), Times.Exactly(2));
@ -114,7 +127,7 @@ public void should_not_resize_covers_if_exists()
.Setup(v => v.GetFileSize(It.IsAny<string>()))
.Returns(1000);
Subject.HandleAsync(new MovieUpdatedEvent(_movie));
ExecuteAndVerifyCommand(_movie);
Mocker.GetMock<IImageResizer>()
.Verify(v => v.Resize(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<int>()), Times.Never());
@ -135,7 +148,7 @@ public void should_resize_covers_if_existing_is_empty()
.Setup(v => v.GetFileSize(It.IsAny<string>()))
.Returns(0);
Subject.HandleAsync(new MovieUpdatedEvent(_movie));
ExecuteAndVerifyCommand(_movie);
Mocker.GetMock<IImageResizer>()
.Verify(v => v.Resize(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<int>()), Times.Exactly(2));
@ -156,7 +169,7 @@ public void should_log_error_if_resize_failed()
.Setup(v => v.Resize(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<int>()))
.Throws<ApplicationException>();
Subject.HandleAsync(new MovieUpdatedEvent(_movie));
ExecuteAndVerifyCommand(_movie);
Mocker.GetMock<IImageResizer>()
.Verify(v => v.Resize(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<int>()), Times.Exactly(2));

View File

@ -20,12 +20,12 @@ public UserRepository(IMainDatabase database, IEventAggregator eventAggregator)
public User FindUser(string username)
{
return Query.Where(u => u.Username == username).SingleOrDefault();
return Query(q => q.Where(u => u.Username == username).SingleOrDefault());
}
public User FindUser(Guid identifier)
{
return Query.Where(u => u.Identifier == identifier).SingleOrDefault();
return Query(q => q.Where(u => u.Identifier == identifier).SingleOrDefault());
}
}
}

View File

@ -26,7 +26,11 @@ public MakeDatabaseBackup(Logger logger)
public void BackupDatabase(IDatabase database, string targetDirectory)
{
var sourceConnectionString = database.GetDataMapper().ConnectionString;
var sourceConnectionString = "";
using (var db = database.GetDataMapper())
{
sourceConnectionString = db.ConnectionString;
}
var backupConnectionStringBuilder = new SQLiteConnectionStringBuilder(sourceConnectionString);
backupConnectionStringBuilder.DataSource = Path.Combine(targetDirectory, Path.GetFileName(backupConnectionStringBuilder.DataSource));

View File

@ -22,19 +22,19 @@ public BlacklistRepository(IMainDatabase database, IEventAggregator eventAggrega
public List<Blacklist> BlacklistedByTitle(int movieId, string sourceTitle)
{
return Query.Where(e => e.MovieId == movieId)
.AndWhere(e => e.SourceTitle.Contains(sourceTitle));
return Query(q => q.Where(e => e.MovieId == movieId)
.AndWhere(e => e.SourceTitle.Contains(sourceTitle)).ToList());
}
public List<Blacklist> BlacklistedByTorrentInfoHash(int movieId, string torrentInfoHash)
{
return Query.Where(e => e.MovieId == movieId)
.AndWhere(e => e.TorrentInfoHash.Contains(torrentInfoHash));
return Query(q => q.Where(e => e.MovieId == movieId)
.AndWhere(e => e.TorrentInfoHash.Contains(torrentInfoHash)).ToList());
}
public List<Blacklist> BlacklistedByMovie(int movieId)
{
return Query.Where(b => b.MovieId == movieId);
return Query(q => q.Where(b => b.MovieId == movieId).ToList());
}
protected override SortBuilder<Blacklist> GetPagedQuery(QueryBuilder<Blacklist> query, PagingSpec<Blacklist> pagingSpec)

View File

@ -21,7 +21,7 @@ public ConfigRepository(IMainDatabase database, IEventAggregator eventAggregator
public Config Get(string key)
{
return Query.Where(c => c.Key == key).SingleOrDefault();
return Query(q => q.Where(c => c.Key == key).SingleOrDefault());
}
public Config Upsert(string key, string value)

View File

@ -42,7 +42,10 @@ namespace NzbDrone.Core.Datastore
private readonly IDatabase _database;
private readonly IEventAggregator _eventAggregator;
protected IDataMapper DataMapper => _database.GetDataMapper();
protected IDataMapper DataMapper()
{
return _database.GetDataMapper();
}
public BasicRepository(IDatabase database, IEventAggregator eventAggregator)
{
@ -50,26 +53,40 @@ public BasicRepository(IDatabase database, IEventAggregator eventAggregator)
_eventAggregator = eventAggregator;
}
protected QueryBuilder<TModel> Query => AddJoinQueries(DataMapper.Query<TModel>());
protected T Query<T>(Func<QueryBuilder<TModel>, T> finalizeQuery)
{
using (var mapper = DataMapper())
{
var query = AddJoinQueries(mapper.Query<TModel>());
return finalizeQuery(query);
}
}
protected void Delete(Expression<Func<TModel, bool>> filter)
{
DataMapper.Delete(filter);
using (var db = DataMapper())
{
db.Delete(filter);
}
}
public IEnumerable<TModel> All()
{
return Query.ToList();
return Query((q => q.ToList()));
}
public int Count()
{
return DataMapper.Query<TModel>().GetRowCount();
using (var db = DataMapper())
{
return db.Query<TModel>().GetRowCount();
}
}
public TModel Get(int id)
{
var model = Query.Where(c => c.Id == id).SingleOrDefault();
TModel model = Query(q => q.Where(c => c.Id == id).SingleOrDefault());
if (model == null)
{
@ -83,7 +100,7 @@ public IEnumerable<TModel> Get(IEnumerable<int> ids)
{
var idList = ids.ToList();
var query = string.Format("Id IN ({0})", string.Join(",", idList));
var result = Query.Where(m => m.Id.In(idList)).ToList();
var result = Query(q => q.Where(m => m.Id.In(idList)).ToList());
//var result = Query.Where(query).ToList();
if (result.Count != idList.Count())
@ -111,7 +128,10 @@ public TModel Insert(TModel model)
throw new InvalidOperationException("Can't insert model with existing ID " + model.Id);
}
DataMapper.Insert(model);
using (var db = DataMapper())
{
db.Insert(model);
}
ModelCreated(model);
@ -125,7 +145,10 @@ public TModel Update(TModel model)
throw new InvalidOperationException("Can't update model with ID 0");
}
DataMapper.Update(model, c => c.Id == model.Id);
using (var db = DataMapper())
{
db.Update(model, c => c.Id == model.Id);
}
ModelUpdated(model);
@ -139,7 +162,7 @@ public void Delete(TModel model)
public void InsertMany(IList<TModel> models)
{
using (var unitOfWork = new UnitOfWork(() => DataMapper))
using (var unitOfWork = new UnitOfWork(() => DataMapper()))
{
unitOfWork.BeginTransaction(IsolationLevel.ReadCommitted);
@ -154,7 +177,7 @@ public void InsertMany(IList<TModel> models)
public void UpdateMany(IList<TModel> models)
{
using (var unitOfWork = new UnitOfWork(() => DataMapper))
using (var unitOfWork = new UnitOfWork(() => DataMapper()))
{
unitOfWork.BeginTransaction(IsolationLevel.ReadCommitted);
@ -192,12 +215,15 @@ public TModel Upsert(TModel model)
public void Delete(int id)
{
DataMapper.Delete<TModel>(c => c.Id == id);
using (var db = DataMapper())
{
db.Delete<TModel>(c => c.Id == id);
}
}
public void DeleteMany(IEnumerable<int> ids)
{
using (var unitOfWork = new UnitOfWork(() => DataMapper))
using (var unitOfWork = new UnitOfWork(() => DataMapper()))
{
unitOfWork.BeginTransaction(IsolationLevel.ReadCommitted);
@ -214,7 +240,10 @@ public void DeleteMany(IEnumerable<int> ids)
public void Purge(bool vacuum = false)
{
DataMapper.Delete<TModel>(c => c.Id > -1);
using (var db = DataMapper())
{
db.Delete<TModel>(c => c.Id > -1);
}
if (vacuum)
{
Vacuum();
@ -238,20 +267,23 @@ public void SetFields(TModel model, params Expression<Func<TModel, object>>[] pr
throw new InvalidOperationException("Attempted to updated model without ID");
}
DataMapper.Update<TModel>()
.Where(c => c.Id == model.Id)
.ColumnsIncluding(properties)
.Entity(model)
.Execute();
using (var db = DataMapper())
{
db.Update<TModel>()
.Where(c => c.Id == model.Id)
.ColumnsIncluding(properties)
.Entity(model)
.Execute();
}
ModelUpdated(model);
}
public virtual PagingSpec<TModel> GetPaged(PagingSpec<TModel> pagingSpec)
{
pagingSpec.Records = GetPagedQuery(Query, pagingSpec).Skip(pagingSpec.PagingOffset())
.Take(pagingSpec.PageSize).ToList();
pagingSpec.TotalRecords = GetPagedQuery(Query, pagingSpec).GetRowCount();
pagingSpec.Records = Query(q => GetPagedQuery(q, pagingSpec).Skip(pagingSpec.PagingOffset())
.Take(pagingSpec.PageSize).ToList());
pagingSpec.TotalRecords = Query(q => GetPagedQuery(q, pagingSpec).GetRowCount());
return pagingSpec;
}
@ -285,7 +317,7 @@ private void PublishModelEvent(TModel model, ModelAction action)
}
}
protected virtual QueryBuilder<TModel> AddJoinQueries(QueryBuilder<TModel> baseQuery)
protected virtual QueryBuilder<TActual> AddJoinQueries<TActual>(QueryBuilder<TActual> baseQuery)
{
return baseQuery;
}

View File

@ -35,7 +35,7 @@ private static string GetConnectionString(string dbPath)
var connectionBuilder = new SQLiteConnectionStringBuilder();
connectionBuilder.DataSource = dbPath;
connectionBuilder.CacheSize = (int)-10.Megabytes();
connectionBuilder.CacheSize = (int)-20000;
connectionBuilder.DateTimeKind = DateTimeKind.Utc;
connectionBuilder.JournalMode = OsInfo.IsOsx ? SQLiteJournalModeEnum.Truncate : SQLiteJournalModeEnum.Wal;
connectionBuilder.Pooling = true;

View File

@ -34,8 +34,11 @@ public Version Version
{
get
{
var version = _datamapperFactory().ExecuteScalar("SELECT sqlite_version()").ToString();
return new Version(version);
using (var db = _datamapperFactory())
{
var version = db.ExecuteScalar("SELECT sqlite_version()").ToString();
return new Version(version);
}
}
}
@ -44,7 +47,10 @@ public void Vacuum()
try
{
_logger.Info("Vacuuming {0} database", _databaseName);
_datamapperFactory().ExecuteNonQuery("Vacuum;");
using (var db = _datamapperFactory())
{
db.ExecuteNonQuery("Vacuum;");
}
_logger.Info("{0} database compressed", _databaseName);
}
catch (Exception e)

View File

@ -24,7 +24,7 @@ public void DeleteByMovieId(int movieId)
public List<PendingRelease> AllByMovieId(int movieId)
{
return Query.Where(p => p.MovieId == movieId);
return Query(q => q.Where(p => p.MovieId == movieId).ToList());
}
}
}

View File

@ -34,17 +34,17 @@ public void DeleteForMovieFile(int movieFileId)
public List<TExtraFile> GetFilesByMovie(int movieId)
{
return Query.Where(c => c.MovieId == movieId);
return Query(q => q.Where(c => c.MovieId == movieId).ToList());
}
public List<TExtraFile> GetFilesByMovieFile(int movieFileId)
{
return Query.Where(c => c.MovieFileId == movieFileId);
return Query(q => q.Where(c => c.MovieFileId == movieFileId).ToList());
}
public TExtraFile FindByPath(string path)
{
return Query.Where(c => c.RelativePath == path).SingleOrDefault();
return Query(q => q.Where(c => c.RelativePath == path).SingleOrDefault());
}
}
}

View File

@ -30,37 +30,37 @@ public HistoryRepository(IMainDatabase database, IEventAggregator eventAggregato
public List<QualityModel> GetBestQualityInHistory(int movieId)
{
var history = Query.Where(c => c.MovieId == movieId);
var history = Query(q => q.Where(c => c.MovieId == movieId).ToList());
return history.Select(h => h.Quality).ToList();
}
public History MostRecentForDownloadId(string downloadId)
{
return Query.Where(h => h.DownloadId == downloadId)
return Query(q => q.Where(h => h.DownloadId == downloadId)
.OrderByDescending(h => h.Date)
.FirstOrDefault();
.FirstOrDefault());
}
public List<History> FindByDownloadId(string downloadId)
{
return Query.Where(h => h.DownloadId == downloadId);
return Query(q => q.Where(h => h.DownloadId == downloadId).ToList());
}
public List<History> FindDownloadHistory(int idMovieId, QualityModel quality)
{
return Query.Where(h =>
return Query(q => q.Where(h =>
h.MovieId == idMovieId &&
h.Quality == quality &&
(h.EventType == HistoryEventType.Grabbed ||
h.EventType == HistoryEventType.DownloadFailed ||
h.EventType == HistoryEventType.DownloadFolderImported)
).ToList();
).ToList());
}
public List<History> FindByMovieId(int movieId)
{
return Query.Where(h => h.MovieId == movieId);
return Query(q => q.Where(h => h.MovieId == movieId).ToList());
}
public void DeleteForMovie(int movieId)
@ -77,9 +77,9 @@ protected override SortBuilder<History> GetPagedQuery(QueryBuilder<History> quer
public History MostRecentForMovie(int movieId)
{
return Query.Where(h => h.MovieId == movieId)
return Query(q => q.Where(h => h.MovieId == movieId)
.OrderByDescending(h => h.Date)
.FirstOrDefault();
.FirstOrDefault());
}
}
}

View File

@ -13,9 +13,9 @@ public CleanupAbsolutePathMetadataFiles(IMainDatabase database)
public void Clean()
{
var mapper = _database.GetDataMapper();
mapper.ExecuteNonQuery(@"DELETE FROM MetadataFiles
using (var mapper = _database.GetDataMapper())
{
mapper.ExecuteNonQuery(@"DELETE FROM MetadataFiles
WHERE Id IN (
SELECT Id FROM MetadataFiles
WHERE RelativePath
@ -25,6 +25,7 @@ OR RelativePath
OR RelativePath
LIKE '/%'
)");
}
}
}
}

View File

@ -13,12 +13,14 @@ public CleanupAdditionalNamingSpecs(IMainDatabase database)
public void Clean()
{
var mapper = _database.GetDataMapper();
using (var mapper = _database.GetDataMapper())
{
mapper.ExecuteNonQuery(@"DELETE FROM NamingConfig
mapper.ExecuteNonQuery(@"DELETE FROM NamingConfig
WHERE ID NOT IN (
SELECT ID FROM NamingConfig
LIMIT 1)");
}
}
}
}

View File

@ -13,13 +13,15 @@ public CleanupAdditionalUsers(IMainDatabase database)
public void Clean()
{
var mapper = _database.GetDataMapper();
using (var mapper = _database.GetDataMapper())
{
mapper.ExecuteNonQuery(@"DELETE FROM Users
mapper.ExecuteNonQuery(@"DELETE FROM Users
WHERE ID NOT IN (
SELECT ID FROM Users
LIMIT 1)");
}
}
}
}
}

View File

@ -19,28 +19,32 @@ public void Clean()
private void DeleteDuplicateMovieMetadata()
{
var mapper = _database.GetDataMapper();
using (var mapper = _database.GetDataMapper())
{
mapper.ExecuteNonQuery(@"DELETE FROM MetadataFiles
mapper.ExecuteNonQuery(@"DELETE FROM MetadataFiles
WHERE Id IN (
SELECT Id FROM MetadataFiles
WHERE Type = 1
GROUP BY MovieId, Consumer
HAVING COUNT(MovieId) > 1
)");
}
}
private void DeleteDuplicateMovieFileMetadata()
{
var mapper = _database.GetDataMapper();
using (var mapper = _database.GetDataMapper())
{
mapper.ExecuteNonQuery(@"DELETE FROM MetadataFiles
mapper.ExecuteNonQuery(@"DELETE FROM MetadataFiles
WHERE Id IN (
SELECT Id FROM MetadataFiles
WHERE Type = 1
GROUP BY MovieFileId, Consumer
HAVING COUNT(MovieFileId) > 1
)");
}
}
}
}

View File

@ -13,14 +13,16 @@ public CleanupOrphanedAlternativeTitles(IMainDatabase database)
public void Clean()
{
var mapper = _database.GetDataMapper();
using (var mapper = _database.GetDataMapper())
{
mapper.ExecuteNonQuery(@"DELETE FROM AlternativeTitles
mapper.ExecuteNonQuery(@"DELETE FROM AlternativeTitles
WHERE Id IN (
SELECT AlternativeTitles.Id FROM AlternativeTitles
LEFT OUTER JOIN Movies
ON AlternativeTitles.MovieId = Movies.Id
WHERE Movies.Id IS NULL)");
}
}
}
}

View File

@ -13,14 +13,16 @@ public CleanupOrphanedBlacklist(IMainDatabase database)
public void Clean()
{
var mapper = _database.GetDataMapper();
using (var mapper = _database.GetDataMapper())
{
mapper.ExecuteNonQuery(@"DELETE FROM Blacklist
mapper.ExecuteNonQuery(@"DELETE FROM Blacklist
WHERE Id IN (
SELECT Blacklist.Id FROM Blacklist
LEFT OUTER JOIN Movies
ON Blacklist.MovieId = Movies.Id
WHERE Movies.Id IS NULL)");
}
}
}
}

View File

@ -18,14 +18,16 @@ public void Clean()
private void CleanupOrphanedByMovie()
{
var mapper = _database.GetDataMapper();
using (var mapper = _database.GetDataMapper())
{
mapper.ExecuteNonQuery(@"DELETE FROM History
mapper.ExecuteNonQuery(@"DELETE FROM History
WHERE Id IN (
SELECT History.Id FROM History
LEFT OUTER JOIN Movies
ON History.MovieId = Movies.Id
WHERE Movies.Id IS NULL)");
}
}
}
}

View File

@ -13,14 +13,16 @@ public CleanupOrphanedIndexerStatus(IMainDatabase database)
public void Clean()
{
var mapper = _database.GetDataMapper();
using (var mapper = _database.GetDataMapper())
{
mapper.ExecuteNonQuery(@"DELETE FROM IndexerStatus
mapper.ExecuteNonQuery(@"DELETE FROM IndexerStatus
WHERE Id IN (
SELECT IndexerStatus.Id FROM IndexerStatus
LEFT OUTER JOIN Indexers
ON IndexerStatus.IndexerId = Indexers.Id
WHERE Indexers.Id IS NULL)");
}
}
}
}

View File

@ -20,38 +20,44 @@ public void Clean()
private void DeleteOrphanedByMovie()
{
var mapper = _database.GetDataMapper();
using (var mapper = _database.GetDataMapper())
{
mapper.ExecuteNonQuery(@"DELETE FROM MetadataFiles
mapper.ExecuteNonQuery(@"DELETE FROM MetadataFiles
WHERE Id IN (
SELECT MetadataFiles.Id FROM MetadataFiles
LEFT OUTER JOIN Movies
ON MetadataFiles.MovieId = Movies.Id
WHERE Movies.Id IS NULL)");
}
}
private void DeleteOrphanedByMovieFile()
{
var mapper = _database.GetDataMapper();
using (var mapper = _database.GetDataMapper())
{
mapper.ExecuteNonQuery(@"DELETE FROM MetadataFiles
mapper.ExecuteNonQuery(@"DELETE FROM MetadataFiles
WHERE Id IN (
SELECT MetadataFiles.Id FROM MetadataFiles
LEFT OUTER JOIN MovieFiles
ON MetadataFiles.MovieFileId = MovieFiles.Id
WHERE MetadataFiles.MovieFileId > 0
AND MovieFiles.Id IS NULL)");
}
}
private void DeleteWhereMovieFileIsZero()
{
var mapper = _database.GetDataMapper();
using (var mapper = _database.GetDataMapper())
{
mapper.ExecuteNonQuery(@"DELETE FROM MetadataFiles
mapper.ExecuteNonQuery(@"DELETE FROM MetadataFiles
WHERE Id IN (
SELECT Id FROM MetadataFiles
WHERE Type IN (1, 2)
AND MovieFileId = 0)");
}
}
}
}

View File

@ -13,14 +13,15 @@ public CleanupOrphanedMovieFiles(IMainDatabase database)
public void Clean()
{
var mapper = _database.GetDataMapper();
mapper.ExecuteNonQuery(@"DELETE FROM MovieFiles
using (var mapper = _database.GetDataMapper())
{
mapper.ExecuteNonQuery(@"DELETE FROM MovieFiles
WHERE Id IN (
SELECT MovieFiles.Id FROM MovieFiles
LEFT OUTER JOIN Movies
ON MovieFiles.Id = Movies.MovieFileId
WHERE Movies.Id IS NULL)");
}
}
}
}

View File

@ -13,14 +13,16 @@ public CleanupOrphanedPendingReleases(IMainDatabase database)
public void Clean()
{
var mapper = _database.GetDataMapper();
using (var mapper = _database.GetDataMapper())
{
mapper.ExecuteNonQuery(@"DELETE FROM PendingReleases
mapper.ExecuteNonQuery(@"DELETE FROM PendingReleases
WHERE Id IN (
SELECT PendingReleases.Id FROM PendingReleases
LEFT OUTER JOIN Movies
ON PendingReleases.MovieId = Movies.Id
WHERE Movies.Id IS NULL)");
}
}
}
}

View File

@ -17,16 +17,18 @@ public CleanupUnusedTags(IMainDatabase database)
public void Clean()
{
var mapper = _database.GetDataMapper();
using (var mapper = _database.GetDataMapper())
{
var usedTags = new[] { "Movies", "Notifications", "DelayProfiles", "Restrictions" }
.SelectMany(v => GetUsedTags(v, mapper))
.Distinct()
.ToArray();
var usedTags = new[] {"Movies", "Notifications", "DelayProfiles", "Restrictions"}
.SelectMany(v => GetUsedTags(v, mapper))
.Distinct()
.ToArray();
var usedTagsList = string.Join(",", usedTags.Select(d => d.ToString()).ToArray());
var usedTagsList = string.Join(",", usedTags.Select(d => d.ToString()).ToArray());
mapper.ExecuteNonQuery($"DELETE FROM Tags WHERE NOT Id IN ({usedTagsList})");
mapper.ExecuteNonQuery($"DELETE FROM Tags WHERE NOT Id IN ({usedTagsList})");
}
}
private int[] GetUsedTags(string table, IDataMapper mapper)

View File

@ -23,12 +23,14 @@ public void Clean()
_logger.Debug("Not running scheduled task last execution cleanup during debug");
}
var mapper = _database.GetDataMapper();
mapper.AddParameter("time", DateTime.UtcNow);
using (var mapper = _database.GetDataMapper())
{
mapper.AddParameter("time", DateTime.UtcNow);
mapper.ExecuteNonQuery(@"UPDATE ScheduledTasks
mapper.ExecuteNonQuery(@"UPDATE ScheduledTasks
SET LastExecution = @time
WHERE LastExecution > @time");
}
}
}
}

View File

@ -20,7 +20,7 @@ public IndexerStatusRepository(IMainDatabase database, IEventAggregator eventAgg
public IndexerStatus FindByIndexerId(int indexerId)
{
return Query.Where(c => c.IndexerId == indexerId).SingleOrDefault();
return Query(q => q.Where(c => c.IndexerId == indexerId).SingleOrDefault());
}
}
}

View File

@ -15,13 +15,14 @@ public class DatabaseTarget : TargetWithLayout, IHandle<ApplicationShutdownReque
{
private readonly SQLiteConnection _connection;
private readonly IConnectionStringFactory _connectionStringFactory;
const string INSERT_COMMAND = "INSERT INTO [Logs]([Message],[Time],[Logger],[Exception],[ExceptionType],[Level]) " +
"VALUES(@Message,@Time,@Logger,@Exception,@ExceptionType,@Level)";
public DatabaseTarget(IConnectionStringFactory connectionStringFactory)
{
_connection = new SQLiteConnection(connectionStringFactory.LogDbConnectionString);
_connection.Open();
_connectionStringFactory = connectionStringFactory;
}
public void Register()
@ -84,16 +85,25 @@ protected override void Write(LogEventInfo logEvent)
log.Level = logEvent.Level.Name;
var sqlCommand = new SQLiteCommand(INSERT_COMMAND, _connection);
using (var connection =
SQLiteFactory.Instance.CreateConnection())
{
connection.ConnectionString = _connectionStringFactory.LogDbConnectionString;
connection.Open();
using (var sqlCommand = connection.CreateCommand())
{
sqlCommand.CommandText = INSERT_COMMAND;
sqlCommand.Parameters.Add(new SQLiteParameter("Message", DbType.String) { Value = log.Message });
sqlCommand.Parameters.Add(new SQLiteParameter("Time", DbType.DateTime) { Value = log.Time.ToUniversalTime() });
sqlCommand.Parameters.Add(new SQLiteParameter("Logger", DbType.String) { Value = log.Logger });
sqlCommand.Parameters.Add(new SQLiteParameter("Exception", DbType.String) { Value = log.Exception });
sqlCommand.Parameters.Add(new SQLiteParameter("ExceptionType", DbType.String) { Value = log.ExceptionType });
sqlCommand.Parameters.Add(new SQLiteParameter("Level", DbType.String) { Value = log.Level });
sqlCommand.Parameters.Add(new SQLiteParameter("Message", DbType.String) { Value = log.Message });
sqlCommand.Parameters.Add(new SQLiteParameter("Time", DbType.DateTime) { Value = log.Time.ToUniversalTime() });
sqlCommand.Parameters.Add(new SQLiteParameter("Logger", DbType.String) { Value = log.Logger });
sqlCommand.Parameters.Add(new SQLiteParameter("Exception", DbType.String) { Value = log.Exception });
sqlCommand.Parameters.Add(new SQLiteParameter("ExceptionType", DbType.String) { Value = log.ExceptionType });
sqlCommand.Parameters.Add(new SQLiteParameter("Level", DbType.String) { Value = log.Level });
sqlCommand.ExecuteNonQuery();
}
}
sqlCommand.ExecuteNonQuery();
}
catch (SQLiteException ex)
{
@ -104,7 +114,7 @@ protected override void Write(LogEventInfo logEvent)
public void Handle(ApplicationShutdownRequested message)
{
if (LogManager.Configuration.LoggingRules.Contains(Rule))
if (LogManager.Configuration?.LoggingRules?.Contains(Rule) == true)
{
UnRegister();
}

View File

@ -22,7 +22,7 @@ public ScheduledTaskRepository(IMainDatabase database, IEventAggregator eventAgg
public ScheduledTask GetDefinition(Type type)
{
return Query.Where(c => c.TypeName == type.FullName).Single();
return Query(q => q.Where(c => c.TypeName == type.FullName).Single());
}
public void SetLastExecutionTime(int id, DateTime executionTime)

View File

@ -31,12 +31,6 @@ public bool AlreadyExists(string url, string path)
return false;
}
if (!_diskProvider.IsValidGDIPlusImage(path))
{
_diskProvider.DeleteFile(path);
return false;
}
var headers = _httpClient.Head(new HttpRequest(url)).Headers;
var fileSize = _diskProvider.GetFileSize(path);
return fileSize == headers.ContentLength;

View File

@ -0,0 +1,19 @@
using NzbDrone.Core.Messaging.Commands;
namespace NzbDrone.Core.MediaCover
{
public class EnsureMediaCoversCommand : Command
{
public int MovieId { get; set; }
public EnsureMediaCoversCommand()
{
}
public EnsureMediaCoversCommand(int movieId)
{
MovieId = movieId;
}
}
}

View File

@ -8,6 +8,7 @@
using NzbDrone.Common.Extensions;
using NzbDrone.Common.Http;
using NzbDrone.Core.Configuration;
using NzbDrone.Core.Messaging.Commands;
using NzbDrone.Core.Messaging.Events;
using NzbDrone.Core.Movies;
using NzbDrone.Core.Movies.Events;
@ -24,6 +25,7 @@ public class MediaCoverService :
IHandleAsync<MovieUpdatedEvent>,
IHandleAsync<MovieAddedEvent>,
IHandleAsync<MovieDeletedEvent>,
IExecute<EnsureMediaCoversCommand>,
IMapCoversToLocal
{
private readonly IImageResizer _resizer;
@ -32,6 +34,8 @@ public class MediaCoverService :
private readonly ICoverExistsSpecification _coverExistsSpecification;
private readonly IConfigFileProvider _configFileProvider;
private readonly IEventAggregator _eventAggregator;
private readonly IManageCommandQueue _commandQueue;
private readonly IMovieService _movieService;
private readonly Logger _logger;
private readonly string _coverRootFolder;
@ -43,6 +47,8 @@ public MediaCoverService(IImageResizer resizer,
ICoverExistsSpecification coverExistsSpecification,
IConfigFileProvider configFileProvider,
IEventAggregator eventAggregator,
IManageCommandQueue commandQueue,
IMovieService movieService,
Logger logger)
{
_resizer = resizer;
@ -51,6 +57,8 @@ public MediaCoverService(IImageResizer resizer,
_coverExistsSpecification = coverExistsSpecification;
_configFileProvider = configFileProvider;
_eventAggregator = eventAggregator;
_commandQueue = commandQueue;
_movieService = movieService;
_logger = logger;
_coverRootFolder = appFolderInfo.GetMediaCoverPath();
@ -109,7 +117,7 @@ private void EnsureCovers(Movie movie, int retried = 0)
_logger.Warn(e, string.Format("Couldn't download media cover for {0}. {1}", movie, e.Message));
if (retried < 3)
{
retried = +1;
retried += 1;
_logger.Warn("Retrying for the {0}. time in ten seconds.", retried);
System.Threading.Thread.Sleep(10 * 1000);
EnsureCovers(movie, retried);
@ -182,16 +190,26 @@ private void EnsureResizedCovers(Movie movie, MediaCover cover, bool forceResize
}
}
public void Execute(EnsureMediaCoversCommand command)
{
var movie = _movieService.GetMovie(command.MovieId);
EnsureCovers(movie);
_eventAggregator.PublishEvent(new MediaCoversUpdatedEvent(movie));
}
public void HandleAsync(MovieUpdatedEvent message)
{
EnsureCovers(message.Movie);
_eventAggregator.PublishEvent(new MediaCoversUpdatedEvent(message.Movie));
//EnsureCovers(message.Movie);
_logger.Info("Testing: {0}, {1}", _commandQueue, message.Movie.Id);
_commandQueue.Push(new EnsureMediaCoversCommand(message.Movie.Id));
//_eventAggregator.PublishEvent(new MediaCoversUpdatedEvent(message.Movie));
}
public void HandleAsync(MovieAddedEvent message)
{
EnsureCovers(message.Movie);
_eventAggregator.PublishEvent(new MediaCoversUpdatedEvent(message.Movie));
//EnsureCovers(message.Movie);
_commandQueue.Push(new EnsureMediaCoversCommand(message.Movie.Id));
//_eventAggregator.PublishEvent(new MediaCoversUpdatedEvent(message.Movie));
}
public void HandleAsync(MovieDeletedEvent message)

View File

@ -21,12 +21,12 @@ public MediaFileRepository(IMainDatabase database, IEventAggregator eventAggrega
public List<MovieFile> GetFilesByMovie(int movieId)
{
return Query.Where(c => c.MovieId == movieId).ToList();
return Query(q => q.Where(c => c.MovieId == movieId).ToList());
}
public List<MovieFile> GetFilesWithoutMediaInfo()
{
return Query.Where(c => c.MediaInfo == null).ToList();
return Query(q => q.Where(c => c.MediaInfo == null).ToList());
}
}
}

View File

@ -37,37 +37,39 @@ public void Trim()
public void OrphanStarted()
{
var mapper = _database.GetDataMapper();
using (var mapper = _database.GetDataMapper())
{
mapper.Parameters.Add(new SQLiteParameter("@orphaned", (int)CommandStatus.Orphaned));
mapper.Parameters.Add(new SQLiteParameter("@started", (int)CommandStatus.Started));
mapper.Parameters.Add(new SQLiteParameter("@ended", DateTime.UtcNow));
mapper.Parameters.Add(new SQLiteParameter("@orphaned", (int) CommandStatus.Orphaned));
mapper.Parameters.Add(new SQLiteParameter("@started", (int) CommandStatus.Started));
mapper.Parameters.Add(new SQLiteParameter("@ended", DateTime.UtcNow));
mapper.ExecuteNonQuery(@"UPDATE Commands
mapper.ExecuteNonQuery(@"UPDATE Commands
SET Status = @orphaned, EndedAt = @ended
WHERE Status = @started");
}
}
public List<CommandModel> FindCommands(string name)
{
return Query.Where(c => c.Name == name).ToList();
return Query(q => q.Where(c => c.Name == name).ToList());
}
public List<CommandModel> FindQueuedOrStarted(string name)
{
return Query.Where(c => c.Name == name)
return Query(q => q.Where(c => c.Name == name)
.AndWhere("[Status] IN (0,1)")
.ToList();
.ToList());
}
public List<CommandModel> Queued()
{
return Query.Where(c => c.Status == CommandStatus.Queued);
return Query(q => q.Where(c => c.Status == CommandStatus.Queued).ToList());
}
public List<CommandModel> Started()
{
return Query.Where(c => c.Status == CommandStatus.Started);
return Query(q => q.Where(c => c.Status == CommandStatus.Started).ToList());
}
public void Start(CommandModel command)

View File

@ -25,17 +25,17 @@ public AlternativeTitleRepository(IMainDatabase database, IEventAggregator event
public AlternativeTitle FindBySourceId(int sourceId)
{
return Query.Where(t => t.SourceId == sourceId).FirstOrDefault();
return Query(q => q.Where(t => t.SourceId == sourceId).FirstOrDefault());
}
public List<AlternativeTitle> FindBySourceIds(List<int> sourceIds)
{
return Query.Where(t => t.SourceId.In(sourceIds)).ToList();
return Query(q => q.Where(t => t.SourceId.In(sourceIds)).ToList());
}
public List<AlternativeTitle> FindByMovieId(int movieId)
{
return Query.Where(t => t.MovieId == movieId).ToList();
return Query(q => q.Where(t => t.MovieId == movieId).ToList());
}
}
}

View File

@ -41,7 +41,7 @@ public MovieRepository(IMainDatabase database, IEventAggregator eventAggregator)
public bool MoviePathExists(string path)
{
return Query.Where(c => c.Path == path).Any();
return Query(q => q.Where(c => c.Path == path).Any());
}
public Movie FindByTitle(string cleanTitle)
@ -57,12 +57,12 @@ public Movie FindByTitle(string cleanTitle, int year)
public Movie FindByImdbId(string imdbid)
{
var imdbIdWithPrefix = Parser.Parser.NormalizeImdbId(imdbid);
return Query.Where(s => s.ImdbId == imdbIdWithPrefix).SingleOrDefault();
return Query(q => q.Where(s => s.ImdbId == imdbIdWithPrefix).SingleOrDefault());
}
public List<Movie> GetMoviesByFileId(int fileId)
{
return Query.Where(m => m.MovieFileId == fileId).ToList();
return Query(q => q.Where(m => m.MovieFileId == fileId).ToList());
}
public void SetFileId(int fileId, int movieId)
@ -72,32 +72,37 @@ public void SetFileId(int fileId, int movieId)
public Movie FindByTitleSlug(string slug)
{
return Query.FirstOrDefault(m => m.TitleSlug == slug);
return Query(q => q.Where(m => m.TitleSlug == slug).FirstOrDefault());
}
public List<Movie> MoviesBetweenDates(DateTime start, DateTime end, bool includeUnmonitored)
{
var query = Query.Where(m => (m.InCinemas >= start && m.InCinemas <= end) || (m.PhysicalRelease >= start && m.PhysicalRelease <= end));
if (!includeUnmonitored)
return Query(q =>
{
query.AndWhere(e => e.Monitored == true);
}
var query = q.Where(m =>
(m.InCinemas >= start && m.InCinemas <= end) ||
(m.PhysicalRelease >= start && m.PhysicalRelease <= end));
if (!includeUnmonitored)
{
query.AndWhere(e => e.Monitored == true);
}
return query.ToList();
return query.ToList();
});
}
public List<Movie> MoviesWithFiles(int movieId)
{
return Query.Join<Movie, MovieFile>(JoinType.Inner, m => m.MovieFile, (m, mf) => m.MovieFileId == mf.Id)
.Where(m => m.Id == movieId);
return Query(q => q.Join<Movie, MovieFile>(JoinType.Inner, m => m.MovieFile, (m, mf) => m.MovieFileId == mf.Id)
.Where(m => m.Id == movieId).ToList());
}
public PagingSpec<Movie> MoviesWithoutFiles(PagingSpec<Movie> pagingSpec)
{
pagingSpec.TotalRecords = GetMoviesWithoutFilesQuery(pagingSpec).GetRowCount();
pagingSpec.Records = GetMoviesWithoutFilesQuery(pagingSpec).ToList();
pagingSpec.TotalRecords = Query(q => GetMoviesWithoutFilesQuery(q, pagingSpec).GetRowCount());
pagingSpec.Records = Query(q => GetMoviesWithoutFilesQuery(q, pagingSpec).ToList());
return pagingSpec;
}
@ -157,7 +162,7 @@ public PagingSpec<Movie> MoviesWithoutFiles(PagingSpec<Movie> pagingSpec)
return newQuery;
}*/
public SortBuilder<Movie> GetMoviesWithoutFilesQuery(PagingSpec<Movie> pagingSpec)
public SortBuilder<Movie> GetMoviesWithoutFilesQuery(QueryBuilder<Movie> Query, PagingSpec<Movie> pagingSpec)
{
return Query.Where(pagingSpec.FilterExpression)
.AndWhere(m => m.MovieFileId == 0)
@ -168,13 +173,13 @@ public SortBuilder<Movie> GetMoviesWithoutFilesQuery(PagingSpec<Movie> pagingSpe
public PagingSpec<Movie> MoviesWhereCutoffUnmet(PagingSpec<Movie> pagingSpec, List<QualitiesBelowCutoff> qualitiesBelowCutoff)
{
pagingSpec.TotalRecords = MoviesWhereCutoffUnmetQuery(pagingSpec, qualitiesBelowCutoff).GetRowCount();
pagingSpec.Records = MoviesWhereCutoffUnmetQuery(pagingSpec, qualitiesBelowCutoff).ToList();
pagingSpec.TotalRecords = Query(q => MoviesWhereCutoffUnmetQuery(q, pagingSpec, qualitiesBelowCutoff).GetRowCount());
pagingSpec.Records = Query(q => MoviesWhereCutoffUnmetQuery(q, pagingSpec, qualitiesBelowCutoff).ToList());
return pagingSpec;
}
private SortBuilder<Movie> MoviesWhereCutoffUnmetQuery(PagingSpec<Movie> pagingSpec, List<QualitiesBelowCutoff> qualitiesBelowCutoff)
private SortBuilder<Movie> MoviesWhereCutoffUnmetQuery(QueryBuilder<Movie> Query, PagingSpec<Movie> pagingSpec, List<QualitiesBelowCutoff> qualitiesBelowCutoff)
{
return Query.Where(pagingSpec.FilterExpression)
.AndWhere(m => m.MovieFileId != 0)
@ -229,34 +234,41 @@ private Movie FindByTitle(string cleanTitle, int? year)
cleanTitleWithArabicNumbers = cleanTitleWithArabicNumbers.Replace(romanNumber, arabicNumber);
}
Movie result = Query.Where(s => s.CleanTitle == cleanTitle).FirstWithYear(year);
if (result == null)
return Query(q =>
{
result = Query.Where(movie => movie.CleanTitle == cleanTitleWithArabicNumbers).FirstWithYear(year) ??
Query.Where(movie => movie.CleanTitle == cleanTitleWithRomanNumbers).FirstWithYear(year);
Movie result = q.Where(s => s.CleanTitle == cleanTitle).FirstWithYear(year);
if (result == null)
{
/*IEnumerable<Movie> movies = All();
Func<string, string> titleCleaner = title => CoreParser.CleanSeriesTitle(title.ToLower());
Func<IEnumerable<AlternativeTitle>, string, bool> altTitleComparer =
(alternativeTitles, atitle) =>
alternativeTitles.Any(altTitle => altTitle.CleanTitle == atitle);*/
result =
q.Where(movie => movie.CleanTitle == cleanTitleWithArabicNumbers).FirstWithYear(year) ??
q.Where(movie => movie.CleanTitle == cleanTitleWithRomanNumbers).FirstWithYear(year);
/*result = movies.Where(m => altTitleComparer(m.AlternativeTitles, cleanTitle) ||
altTitleComparer(m.AlternativeTitles, cleanTitleWithRomanNumbers) ||
altTitleComparer(m.AlternativeTitles, cleanTitleWithArabicNumbers)).FirstWithYear(year);*/
if (result == null)
{
/*IEnumerable<Movie> movies = All();
Func<string, string> titleCleaner = title => CoreParser.CleanSeriesTitle(title.ToLower());
Func<IEnumerable<AlternativeTitle>, string, bool> altTitleComparer =
(alternativeTitles, atitle) =>
alternativeTitles.Any(altTitle => altTitle.CleanTitle == atitle);*/
//result = Query.Join<Movie, AlternativeTitle>(JoinType.Inner, m => m._newAltTitles,
/*result = movies.Where(m => altTitleComparer(m.AlternativeTitles, cleanTitle) ||
altTitleComparer(m.AlternativeTitles, cleanTitleWithRomanNumbers) ||
altTitleComparer(m.AlternativeTitles, cleanTitleWithArabicNumbers)).FirstWithYear(year);*/
//result = Query.Join<Movie, AlternativeTitle>(JoinType.Inner, m => m._newAltTitles,
//(m, t) => m.Id == t.MovieId && (t.CleanTitle == cleanTitle)).FirstWithYear(year);
result = Query.Where<AlternativeTitle>(t =>
t.CleanTitle == cleanTitle || t.CleanTitle == cleanTitleWithArabicNumbers
|| t.CleanTitle == cleanTitleWithRomanNumbers).FirstWithYear(year);
result = q.Where<AlternativeTitle>(t =>
t.CleanTitle == cleanTitle || t.CleanTitle == cleanTitleWithArabicNumbers
|| t.CleanTitle == cleanTitleWithRomanNumbers)
.FirstWithYear(year);
}
}
}
return result;
return result;
});
/*return year.HasValue
? results?.FirstOrDefault(movie => movie.Year == year.Value)
@ -264,7 +276,7 @@ private Movie FindByTitle(string cleanTitle, int? year)
: results?.FirstOrDefault();*/
}
protected override QueryBuilder<Movie> AddJoinQueries(QueryBuilder<Movie> baseQuery)
protected override QueryBuilder<TActual> AddJoinQueries<TActual>(QueryBuilder<TActual> baseQuery)
{
baseQuery = base.AddJoinQueries(baseQuery);
baseQuery = baseQuery.Join<Movie, AlternativeTitle>(JoinType.Left, m => m.AlternativeTitles,
@ -276,7 +288,7 @@ protected override QueryBuilder<Movie> AddJoinQueries(QueryBuilder<Movie> baseQu
public Movie FindByTmdbId(int tmdbid)
{
return Query.Where(m => m.TmdbId == tmdbid).FirstOrDefault();
return Query(q => q.Where(m => m.TmdbId == tmdbid).FirstOrDefault());
}
}
}

View File

@ -8,7 +8,7 @@ public static class QueryExtensions
{
public static Movie FirstWithYear(this SortBuilder<Movie> query, int? year)
{
return year.HasValue ? query.FirstOrDefault(movie => movie.Year == year || movie.SecondaryYear == year) : query.FirstOrDefault();
return year.HasValue ? query.AndWhere(movie => movie.Year == year || movie.SecondaryYear == year).FirstOrDefault() : query.FirstOrDefault();
}
}

View File

@ -30,12 +30,12 @@ public ImportExclusionsRepository(IMainDatabase database, IEventAggregator event
public bool IsMovieExcluded(int tmdbid)
{
return Query.Where(ex => ex.TmdbId == tmdbid).Any();
return Query(q => q.Where(ex => ex.TmdbId == tmdbid).Any());
}
public ImportExclusion GetByTmdbid(int tmdbid)
{
return Query.Where(ex => ex.TmdbId == tmdbid).First();
return Query(q => q.Where(ex => ex.TmdbId == tmdbid).First());
}
}
}

View File

@ -153,6 +153,7 @@
<Compile Include="Extras\Others\OtherExtraFileRenamer.cs" />
<Compile Include="HealthCheck\Checks\PTPOldSettingsCheck.cs" />
<Compile Include="Housekeeping\Housekeepers\CleanupOrphanedAlternativeTitles.cs" />
<Compile Include="MediaCover\EnsureMediaCoversCommand.cs" />
<Compile Include="MediaFiles\Commands\UpdateMovieFileQualityCommand.cs" />
<Compile Include="MediaFiles\MovieImport\Specifications\GrabbedReleaseQualitySpecification.cs" />
<Compile Include="MediaFiles\MovieImport\Specifications\SameFileSpecification.cs" />

View File

@ -17,7 +17,10 @@ public ProfileRepository(IMainDatabase database, IEventAggregator eventAggregato
public bool Exists(int id)
{
return DataMapper.Query<Profile>().Where(p => p.Id == id).GetRowCount() == 1;
using (var mapper = DataMapper())
{
return mapper.Query<Profile>().Where(p => p.Id == id).GetRowCount() == 1;
}
}
}
}

View File

@ -19,7 +19,7 @@ public TagRepository(IMainDatabase database, IEventAggregator eventAggregator)
public Tag GetByLabel(string label)
{
var model = Query.Where(c => c.Label == label).SingleOrDefault();
var model = Query(q => q.Where(c => c.Label == label).SingleOrDefault());
if (model == null)
{