Skip to content

Commit

Permalink
Massive Speedup in Queue
Browse files Browse the repository at this point in the history
  • Loading branch information
da3dsoul committed Jan 20, 2024
1 parent 0e107b9 commit 1903433
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 25 deletions.
6 changes: 4 additions & 2 deletions Shoko.Server/API/v3/Controllers/QueueController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@
using Shoko.Server.API.v3.Models.Shoko;
using Shoko.Server.Commands.Generic;
using Shoko.Server.Databases;
using Shoko.Server.Databases.NHIbernate;
using Shoko.Server.Repositories;
using Shoko.Server.Server;
using Shoko.Server.Settings;
using Shoko.Server.Utilities;

namespace Shoko.Server.API.v3.Controllers;

Expand Down Expand Up @@ -204,7 +206,7 @@ public ActionResult ClearQueueByName([FromRoute] string queueName)

return BaseRepository.Lock(processor, _connectivityService, (commandProcessor, service) =>
{
var session = DatabaseFactory.SessionFactory.OpenSession();
using var session = DatabaseFactory.SessionFactory.OpenSession();
return queueName.ToLowerInvariant() switch
{
"general" => RepoFactory.CommandRequest.GetGeneralCommandsUnsafe(session, showAll)
Expand Down Expand Up @@ -235,7 +237,7 @@ public ActionResult<Dictionary<CommandRequestType, int>> GetTypesForItemsInQueue
{
return BaseRepository.Lock(() =>
{
var session = DatabaseFactory.SessionFactory.OpenSession();
using var session = DatabaseFactory.SessionFactory.OpenSession();
return queueName.ToLowerInvariant() switch
{
"general" => RepoFactory.CommandRequest.GetGeneralCommandsUnsafe(session, true)
Expand Down
30 changes: 13 additions & 17 deletions Shoko.Server/API/v3/Helpers/ModelHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
using File = Shoko.Server.API.v3.Models.Shoko.File;
using FileSource = Shoko.Server.API.v3.Models.Shoko.FileSource;
using GroupSizes = Shoko.Server.API.v3.Models.Shoko.GroupSizes;
using Series = Shoko.Server.API.v3.Models.Shoko.Series;
using SeriesSizes = Shoko.Server.API.v3.Models.Shoko.SeriesSizes;
using SeriesType = Shoko.Server.API.v3.Models.Shoko.SeriesType;

Expand All @@ -20,30 +19,28 @@ public static class ModelHelper
{
public static ListResult<T> ToListResult<T>(this IEnumerable<T> enumerable)
{
var list = enumerable as IReadOnlyList<T> ?? enumerable.ToList();
return new ListResult<T>
{
Total = list.Count,
List = list
Total = enumerable.Count(),
List = enumerable.ToList()
};
}

public static ListResult<T> ToListResult<T>(this IEnumerable<T> enumerable, int page, int pageSize)
{
var list = enumerable as IReadOnlyList<T> ?? enumerable.ToList();
if (pageSize <= 0)
{
return new ListResult<T>
{
Total = list.Count,
List = list
Total = enumerable.Count(),
List = enumerable.ToList()
};
}

return new ListResult<T>
{
Total = list.Count,
List = list
Total = enumerable.Count(),
List = enumerable.AsQueryable()
.Skip(pageSize * (page - 1))
.Take(pageSize)
.ToList()
Expand All @@ -53,24 +50,24 @@ public static ListResult<T> ToListResult<T>(this IEnumerable<T> enumerable, int
public static ListResult<U> ToListResult<T, U>(this IEnumerable<T> enumerable, Func<T, U> mapper, int page,
int pageSize)
{
var list = enumerable as IReadOnlyList<T> ?? enumerable.ToList();
if (pageSize <= 0)
{
return new ListResult<U>
{
Total = list.Count,
List = list
Total = enumerable.Count(),
List = enumerable.ToList()
.Select(mapper)
.ToList()
};
}

return new ListResult<U>
{
Total = list.Count,
List = list
Total = enumerable.Count(),
List = enumerable.AsQueryable()
.Skip(pageSize * (page - 1))
.Take(pageSize)
.ToList()
.Select(mapper)
.ToList()
};
Expand All @@ -93,7 +90,7 @@ public static ListResult<U> ToListResult<T, U>(this IEnumerable<T> enumerable, F
return new ListResult<U>
{
Total = total,
List = enumerable
List = enumerable.AsQueryable()
.Skip(pageSize * (page - 1))
.Take(pageSize)
.ToList()
Expand Down Expand Up @@ -137,8 +134,7 @@ public static int GetTotalEpisodesForType(List<SVR_AnimeEpisode> episodeList, Ep
{
return episodeList
.Select(episode => episode.AniDB_Episode)
.Where(anidbEpisode => anidbEpisode != null && (EpisodeType)anidbEpisode.EpisodeType == episodeType)
.Count();
.Count(anidbEpisode => anidbEpisode != null && (EpisodeType)anidbEpisode.EpisodeType == episodeType);
}

public static string ToDataURL(byte[] byteArray, string contentType, string fieldName = "ByteArrayToDataUrl", ModelStateDictionary modelState = null)
Expand Down
2 changes: 1 addition & 1 deletion Shoko.Server/Commands/CommandProcessorGeneral.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ protected override CommandRequest GetNextCommandRequest()
return BaseRepository.Lock(() =>
{
using var session = DatabaseFactory.SessionFactory.OpenSession();
return RepoFactory.CommandRequest.GetGeneralCommandsUnsafe(session).Take(1).SingleOrDefault();
return RepoFactory.CommandRequest.GetGeneralCommandsUnsafe(session).FirstOrDefault();
});
}
catch (Exception e)
Expand Down
2 changes: 1 addition & 1 deletion Shoko.Server/Commands/CommandProcessorHasher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ protected override CommandRequest GetNextCommandRequest()
return BaseRepository.Lock(() =>
{
using var session = DatabaseFactory.SessionFactory.OpenSession();
return RepoFactory.CommandRequest.GetHasherCommandsUnsafe(session).Take(1).SingleOrDefault();
return RepoFactory.CommandRequest.GetHasherCommandsUnsafe(session).FirstOrDefault();
});
}
catch (Exception e)
Expand Down
3 changes: 2 additions & 1 deletion Shoko.Server/Commands/CommandProcessorImages.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Shoko.Plugin.Abstractions.Services;
using Shoko.Server.Commands.Generic;
using Shoko.Server.Databases;
using Shoko.Server.Databases.NHIbernate;
using Shoko.Server.Models;
using Shoko.Server.Repositories;

Expand Down Expand Up @@ -43,7 +44,7 @@ protected override CommandRequest GetNextCommandRequest()
return BaseRepository.Lock(() =>
{
using var session = DatabaseFactory.SessionFactory.OpenSession();
return RepoFactory.CommandRequest.GetImageCommandsUnsafe(session).Take(1).SingleOrDefault();
return RepoFactory.CommandRequest.GetImageCommandsUnsafe(session).FirstOrDefault();
});
}
catch (Exception e)
Expand Down
16 changes: 16 additions & 0 deletions Shoko.Server/Databases/NHIbernate/NLogInterceptor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using NHibernate;
using NHibernate.SqlCommand;
using NLog;

namespace Shoko.Server.Databases.NHIbernate;

public class NLogInterceptor : EmptyInterceptor
{
private Logger _logger = LogManager.GetCurrentClassLogger();
public override SqlString OnPrepareStatement(SqlString sql)
{
_logger.Trace($"Executing Query: {sql}");

return base.OnPrepareStatement(sql);
}
}
6 changes: 3 additions & 3 deletions Shoko.Server/Repositories/Direct/CommandRequestRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ public bool CheckIfCommandRequestIsDisabled(CommandRequestType type, IConnectivi
return false;
}

public IEnumerable<CommandRequest> GetGeneralCommandsUnsafe(ISession session, bool includeDisabled = false)
public IOrderedQueryable<CommandRequest> GetGeneralCommandsUnsafe(ISession session, bool includeDisabled = false)
{
try
{
Expand All @@ -205,7 +205,7 @@ public IEnumerable<CommandRequest> GetGeneralCommandsUnsafe(ISession session, bo
return null;
}
}
public IEnumerable<CommandRequest> GetHasherCommandsUnsafe(ISession session, bool includeDisabled = false)
public IOrderedQueryable<CommandRequest> GetHasherCommandsUnsafe(ISession session, bool includeDisabled = false)
{
try
{
Expand All @@ -223,7 +223,7 @@ public IEnumerable<CommandRequest> GetHasherCommandsUnsafe(ISession session, boo
}
}

public IEnumerable<CommandRequest> GetImageCommandsUnsafe(ISession session, bool includeDisabled = false)
public IOrderedQueryable<CommandRequest> GetImageCommandsUnsafe(ISession session, bool includeDisabled = false)
{
try
{
Expand Down

0 comments on commit 1903433

Please sign in to comment.