Many (update/insert/delete) DB operations now use transactions

Fixed: Improved series/episode info refresh speed
This commit is contained in:
Mark McDowall 2014-02-20 23:03:36 -08:00
parent 36387dd13f
commit 0a837be9ff
2 changed files with 40 additions and 7 deletions

View File

@ -1,6 +1,5 @@
using System; using System;
using System.Linq; using System.Linq;
using System.Web.UI.WebControls;
using NLog; using NLog;
using NzbDrone.Common.Cache; using NzbDrone.Common.Cache;
using NzbDrone.Core.Messaging.Events; using NzbDrone.Core.Messaging.Events;

View File

@ -2,6 +2,7 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Linq.Expressions; using System.Linq.Expressions;
using System.Security.Cryptography.X509Certificates;
using Marr.Data; using Marr.Data;
using Marr.Data.QGen; using Marr.Data.QGen;
using NzbDrone.Core.Datastore.Events; using NzbDrone.Core.Datastore.Events;
@ -142,23 +143,44 @@ namespace NzbDrone.Core.Datastore
public void InsertMany(IList<TModel> models) public void InsertMany(IList<TModel> models)
{ {
foreach (var model in models) using (var unitOfWork = new UnitOfWork(() => DataMapper))
{ {
Insert(model); unitOfWork.BeginTransaction();
foreach (var model in models)
{
unitOfWork.DB.Insert(model);
}
unitOfWork.Commit();
} }
} }
public void UpdateMany(IList<TModel> models) public void UpdateMany(IList<TModel> models)
{ {
foreach (var model in models) using (var unitOfWork = new UnitOfWork(() => DataMapper))
{ {
Update(model); unitOfWork.BeginTransaction();
foreach (var model in models)
{
var localModel = model;
if (model.Id == 0)
{
throw new InvalidOperationException("Can't update model with ID 0");
}
unitOfWork.DB.Update(model, c => c.Id == localModel.Id);
}
unitOfWork.Commit();
} }
} }
public void DeleteMany(List<TModel> models) public void DeleteMany(List<TModel> models)
{ {
models.ForEach(Delete); DeleteMany(models.Select(m => m.Id));
} }
public TModel Upsert(TModel model) public TModel Upsert(TModel model)
@ -179,7 +201,19 @@ namespace NzbDrone.Core.Datastore
public void DeleteMany(IEnumerable<int> ids) public void DeleteMany(IEnumerable<int> ids)
{ {
ids.ToList().ForEach(Delete); using (var unitOfWork = new UnitOfWork(() => DataMapper))
{
unitOfWork.BeginTransaction();
foreach (var id in ids)
{
var localId = id;
unitOfWork.DB.Delete<TModel>(c => c.Id == localId);
}
unitOfWork.Commit();
}
} }
public void Purge() public void Purge()