diff --git a/Shoko.Server/API/v3/Controllers/QueueController.cs b/Shoko.Server/API/v3/Controllers/QueueController.cs index fc56ee6e6..ee9ace7f4 100644 --- a/Shoko.Server/API/v3/Controllers/QueueController.cs +++ b/Shoko.Server/API/v3/Controllers/QueueController.cs @@ -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; @@ -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) @@ -235,7 +237,7 @@ public ActionResult> 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) diff --git a/Shoko.Server/API/v3/Helpers/ModelHelper.cs b/Shoko.Server/API/v3/Helpers/ModelHelper.cs index e0536b0ad..92844c021 100644 --- a/Shoko.Server/API/v3/Helpers/ModelHelper.cs +++ b/Shoko.Server/API/v3/Helpers/ModelHelper.cs @@ -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; @@ -20,30 +19,28 @@ public static class ModelHelper { public static ListResult ToListResult(this IEnumerable enumerable) { - var list = enumerable as IReadOnlyList ?? enumerable.ToList(); return new ListResult { - Total = list.Count, - List = list + Total = enumerable.Count(), + List = enumerable.ToList() }; } public static ListResult ToListResult(this IEnumerable enumerable, int page, int pageSize) { - var list = enumerable as IReadOnlyList ?? enumerable.ToList(); if (pageSize <= 0) { return new ListResult { - Total = list.Count, - List = list + Total = enumerable.Count(), + List = enumerable.ToList() }; } return new ListResult { - Total = list.Count, - List = list + Total = enumerable.Count(), + List = enumerable.AsQueryable() .Skip(pageSize * (page - 1)) .Take(pageSize) .ToList() @@ -53,13 +50,12 @@ public static ListResult ToListResult(this IEnumerable enumerable, int public static ListResult ToListResult(this IEnumerable enumerable, Func mapper, int page, int pageSize) { - var list = enumerable as IReadOnlyList ?? enumerable.ToList(); if (pageSize <= 0) { return new ListResult { - Total = list.Count, - List = list + Total = enumerable.Count(), + List = enumerable.ToList() .Select(mapper) .ToList() }; @@ -67,10 +63,11 @@ public static ListResult ToListResult(this IEnumerable enumerable, F return new ListResult { - Total = list.Count, - List = list + Total = enumerable.Count(), + List = enumerable.AsQueryable() .Skip(pageSize * (page - 1)) .Take(pageSize) + .ToList() .Select(mapper) .ToList() }; @@ -93,7 +90,7 @@ public static ListResult ToListResult(this IEnumerable enumerable, F return new ListResult { Total = total, - List = enumerable + List = enumerable.AsQueryable() .Skip(pageSize * (page - 1)) .Take(pageSize) .ToList() @@ -137,8 +134,7 @@ public static int GetTotalEpisodesForType(List 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) diff --git a/Shoko.Server/Commands/CommandProcessorGeneral.cs b/Shoko.Server/Commands/CommandProcessorGeneral.cs index 1e87342cf..df0987ddc 100755 --- a/Shoko.Server/Commands/CommandProcessorGeneral.cs +++ b/Shoko.Server/Commands/CommandProcessorGeneral.cs @@ -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) diff --git a/Shoko.Server/Commands/CommandProcessorHasher.cs b/Shoko.Server/Commands/CommandProcessorHasher.cs index 3753a61cb..7ae757ef3 100755 --- a/Shoko.Server/Commands/CommandProcessorHasher.cs +++ b/Shoko.Server/Commands/CommandProcessorHasher.cs @@ -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) diff --git a/Shoko.Server/Commands/CommandProcessorImages.cs b/Shoko.Server/Commands/CommandProcessorImages.cs index 6c7fc5be8..e8c0d9e9a 100755 --- a/Shoko.Server/Commands/CommandProcessorImages.cs +++ b/Shoko.Server/Commands/CommandProcessorImages.cs @@ -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; @@ -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) diff --git a/Shoko.Server/Databases/NHIbernate/NLogInterceptor.cs b/Shoko.Server/Databases/NHIbernate/NLogInterceptor.cs new file mode 100644 index 000000000..92b8138f7 --- /dev/null +++ b/Shoko.Server/Databases/NHIbernate/NLogInterceptor.cs @@ -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); + } +} diff --git a/Shoko.Server/Repositories/Direct/CommandRequestRepository.cs b/Shoko.Server/Repositories/Direct/CommandRequestRepository.cs index 21008dd85..637b5f9c5 100644 --- a/Shoko.Server/Repositories/Direct/CommandRequestRepository.cs +++ b/Shoko.Server/Repositories/Direct/CommandRequestRepository.cs @@ -188,7 +188,7 @@ public bool CheckIfCommandRequestIsDisabled(CommandRequestType type, IConnectivi return false; } - public IEnumerable GetGeneralCommandsUnsafe(ISession session, bool includeDisabled = false) + public IOrderedQueryable GetGeneralCommandsUnsafe(ISession session, bool includeDisabled = false) { try { @@ -205,7 +205,7 @@ public IEnumerable GetGeneralCommandsUnsafe(ISession session, bo return null; } } - public IEnumerable GetHasherCommandsUnsafe(ISession session, bool includeDisabled = false) + public IOrderedQueryable GetHasherCommandsUnsafe(ISession session, bool includeDisabled = false) { try { @@ -223,7 +223,7 @@ public IEnumerable GetHasherCommandsUnsafe(ISession session, boo } } - public IEnumerable GetImageCommandsUnsafe(ISession session, bool includeDisabled = false) + public IOrderedQueryable GetImageCommandsUnsafe(ISession session, bool includeDisabled = false) { try {