Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolve N + 1 problem and add cascading deletion #546

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Open Judge System/Data/OJS.Data/IOjsData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public interface IOjsData : IDisposable
{
IContestsRepository Contests { get; }

IDeletableEntityRepository<Problem> Problems { get; }
IProblemsRepository Problems { get; }

ITestRepository Tests { get; }

Expand Down
2 changes: 2 additions & 0 deletions Open Judge System/Data/OJS.Data/OJS.Data.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@
<Compile Include="Repositories\Contracts\IContestsRepository.cs" />
<Compile Include="IOjsData.cs" />
<Compile Include="IOjsDbContext.cs" />
<Compile Include="Repositories\Contracts\IProblemsRepository.cs" />
<Compile Include="Repositories\Contracts\ISubmissionsRepository.cs" />
<Compile Include="Repositories\Contracts\ITestRepository.cs" />
<Compile Include="Repositories\Contracts\ITestRunsRepository.cs" />
Expand All @@ -100,6 +101,7 @@
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Repositories\Contracts\IParticipantsRepository.cs" />
<Compile Include="Repositories\ParticipantsRepository.cs" />
<Compile Include="Repositories\ProblemsRepository.cs" />
<Compile Include="Repositories\SubmissionsRepository.cs" />
<Compile Include="Repositories\TestRepository.cs" />
<Compile Include="Repositories\TestRunsRepository.cs" />
Expand Down
7 changes: 6 additions & 1 deletion Open Judge System/Data/OJS.Data/OjsData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public OjsData(IOjsDbContext context)

public IContestsRepository Contests => (ContestsRepository)this.GetRepository<Contest>();

public IDeletableEntityRepository<Problem> Problems => this.GetDeletableEntityRepository<Problem>();
public IProblemsRepository Problems => (ProblemsRepository)this.GetRepository<Problem>();

public ITestRepository Tests => (TestRepository)this.GetRepository<Test>();

Expand Down Expand Up @@ -111,6 +111,11 @@ private IRepository<T> GetRepository<T>()
type = typeof(ContestsRepository);
}

if (typeof(T).IsAssignableFrom(typeof(Problem)))
{
type = typeof(ProblemsRepository);
}

if (typeof(T).IsAssignableFrom(typeof(Submission)))
{
type = typeof(SubmissionsRepository);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace OJS.Data.Repositories.Contracts
{
using System;
using System.Linq;
using System.Linq.Expressions;

using OJS.Data.Contracts;
using OJS.Data.Models;

public interface IProblemsRepository : IRepository<Problem>, IDeletableEntityRepository<Problem>
{
IQueryable<Problem> AllIncludesSubmissionsTestsAndResources(Expression<Func<Problem, bool>> orExpressionProblemIds);
}
}
24 changes: 24 additions & 0 deletions Open Judge System/Data/OJS.Data/Repositories/ProblemsRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
namespace OJS.Data.Repositories
{
using System;
using System.Data.Entity;
using System.Linq;
using System.Linq.Expressions;

using OJS.Data.Models;
using OJS.Data.Repositories.Base;
using OJS.Data.Repositories.Contracts;

public class ProblemsRepository : DeletableEntityRepository<Problem>, IProblemsRepository
{
public ProblemsRepository(IOjsDbContext context)
: base(context)
{
}

public IQueryable<Problem> AllIncludesSubmissionsTestsAndResources(Expression<Func<Problem, bool>> orExpressionProblemIds)
{
return this.All().Where(orExpressionProblemIds).Include(p => p.Submissions).Include(p => p.Tests).Include(p => p.Resources);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -374,11 +374,32 @@ public ActionResult ConfirmDeleteAll(int? id)
return this.RedirectToAction(GlobalConstants.Index);
}

var orExpressionProblemIds = ExpressionBuilder.BuildOrExpression<Problem, int>(contest.Problems.Select(p => p.Id), p => p.Id);

var problems = this.Data.Problems.AllIncludesSubmissionsTestsAndResources(orExpressionProblemIds);

// TODO: check for N + 1
foreach (var problem in contest.Problems.ToList())
foreach (var problem in problems.ToList())
{
// TODO: Add cascading deletion of submissions, tests, resources
this.Data.Problems.Delete(problem.Id);

var tests = problem.Tests;
foreach (var test in tests)
{
this.Data.Tests.Delete(test.Id);
}

var submissions = problem.Submissions;
foreach (var submission in submissions)
{
this.Data.Submissions.Delete(submission.Id);
}

var resources = problem.Resources;
foreach (var resource in resources)
{
this.Data.Resources.Delete(resource.Id);
}
}

this.Data.SaveChanges();
Expand Down