Streamline GetLibrary_Flat_NoTracking with better context dispose

This commit is contained in:
Robert McRackan 2021-09-02 14:51:06 -04:00
parent 62c98c66a3
commit b4803c42a5
8 changed files with 39 additions and 44 deletions

View File

@ -1,4 +1,5 @@
using System; using System;
using System.Collections.Generic;
using DataLayer; using DataLayer;
using FileManager; using FileManager;
@ -12,5 +13,11 @@ namespace ApplicationServices
public static LibationContext GetContext() public static LibationContext GetContext()
=> LibationContext.Create(SqliteStorage.ConnectionString); => LibationContext.Create(SqliteStorage.ConnectionString);
public static List<LibraryBook> GetLibrary_Flat_NoTracking()
{
using var context = GetContext();
return context.GetLibrary_Flat_NoTracking();
}
} }
} }

View File

@ -64,6 +64,7 @@ namespace ApplicationServices
LibraryResponseGroups = LibraryOptions.ResponseGroupOptions.ALL_OPTIONS; LibraryResponseGroups = LibraryOptions.ResponseGroupOptions.ALL_OPTIONS;
} }
} }
#region FULL LIBRARY scan and import #region FULL LIBRARY scan and import
public static async Task<(int totalCount, int newCount)> ImportAccountAsync(Func<Account, ILoginCallback> loginCallbackFactoryFunc, params Account[] accounts) public static async Task<(int totalCount, int newCount)> ImportAccountAsync(Func<Account, ILoginCallback> loginCallbackFactoryFunc, params Account[] accounts)
{ {
@ -186,6 +187,21 @@ namespace ApplicationServices
} }
#endregion #endregion
#region remove books
public static List<LibraryBook> RemoveBooks(List<string> idsToRemove)
{
using var context = DbContexts.GetContext();
var libBooks = context.GetLibrary_Flat_NoTracking();
var removeLibraryBooks = libBooks.Where(lb => idsToRemove.Contains(lb.Book.AudibleProductId)).ToList();
context.Library.RemoveRange(removeLibraryBooks);
context.SaveChanges();
return removeLibraryBooks;
}
#endregion
public static LiberatedStatus Liberated_Status(Book book) public static LiberatedStatus Liberated_Status(Book book)
=> book.Audio_Exists ? LiberatedStatus.Liberated => book.Audio_Exists ? LiberatedStatus.Liberated
: FileManager.AudibleFileStorage.AaxcExists(book.AudibleProductId) ? LiberatedStatus.PartialDownload : FileManager.AudibleFileStorage.AaxcExists(book.AudibleProductId) ? LiberatedStatus.PartialDownload
@ -201,7 +217,7 @@ namespace ApplicationServices
public record LibraryStats(int booksFullyBackedUp, int booksDownloadedOnly, int booksNoProgress, int booksError, int pdfsDownloaded, int pdfsNotDownloaded) { } public record LibraryStats(int booksFullyBackedUp, int booksDownloadedOnly, int booksNoProgress, int booksError, int pdfsDownloaded, int pdfsNotDownloaded) { }
public static LibraryStats GetCounts() public static LibraryStats GetCounts()
{ {
var libraryBooks = DbContexts.GetContext().GetLibrary_Flat_NoTracking(); var libraryBooks = DbContexts.GetLibrary_Flat_NoTracking();
var results = libraryBooks var results = libraryBooks
.AsParallel() .AsParallel()

View File

@ -135,9 +135,7 @@ namespace ApplicationServices
{ {
public static void ToCsv(string saveFilePath) public static void ToCsv(string saveFilePath)
{ {
using var context = DbContexts.GetContext(); var dtos = DbContexts.GetLibrary_Flat_NoTracking().ToDtos();
var dtos = context.GetLibrary_Flat_NoTracking().ToDtos();
if (!dtos.Any()) if (!dtos.Any())
return; return;
@ -151,17 +149,14 @@ namespace ApplicationServices
public static void ToJson(string saveFilePath) public static void ToJson(string saveFilePath)
{ {
using var context = DbContexts.GetContext(); var dtos = DbContexts.GetLibrary_Flat_NoTracking().ToDtos();
var dtos = context.GetLibrary_Flat_NoTracking().ToDtos();
var json = Newtonsoft.Json.JsonConvert.SerializeObject(dtos, Newtonsoft.Json.Formatting.Indented); var json = Newtonsoft.Json.JsonConvert.SerializeObject(dtos, Newtonsoft.Json.Formatting.Indented);
System.IO.File.WriteAllText(saveFilePath, json); System.IO.File.WriteAllText(saveFilePath, json);
} }
public static void ToXlsx(string saveFilePath) public static void ToXlsx(string saveFilePath)
{ {
using var context = DbContexts.GetContext(); var dtos = DbContexts.GetLibrary_Flat_NoTracking().ToDtos();
var dtos = context.GetLibrary_Flat_NoTracking().ToDtos();
var workbook = new XSSFWorkbook(); var workbook = new XSSFWorkbook();
var sheet = workbook.CreateSheet("Library"); var sheet = workbook.CreateSheet("Library");

View File

@ -10,7 +10,8 @@ namespace ApplicationServices
public static void FullReIndex(SearchEngine engine = null) public static void FullReIndex(SearchEngine engine = null)
{ {
engine ??= new SearchEngine(); engine ??= new SearchEngine();
engine.CreateNewIndex(DbContexts.GetContext()); var library = DbContexts.GetLibrary_Flat_NoTracking();
engine.CreateNewIndex(library);
} }
public static SearchResultSet Search(string searchString) => performSearchEngineFunc_safe(e => public static SearchResultSet Search(string searchString) => performSearchEngineFunc_safe(e =>

View File

@ -197,25 +197,9 @@ namespace LibationSearchEngine
#endregion #endregion
#region create and update index #region create and update index
/// <summary> /// <summary>create new. ie: full re-index</summary>
/// create new. ie: full re-index public void CreateNewIndex(IEnumerable<LibraryBook> library, bool overwrite = true)
/// </summary>
/// <param name="overwrite"></param>
public void CreateNewIndex(LibationContext context, bool overwrite = true)
{ {
// 300 titles: 200- 400 ms
// 1021 titles: 1777-2250 ms
var sw = System.Diagnostics.Stopwatch.StartNew();
var stamps = new List<long>();
void log() => stamps.Add(sw.ElapsedMilliseconds);
log();
var library = context.GetLibrary_Flat_NoTracking();
log();
// location of index/create the index // location of index/create the index
using var index = getIndex(); using var index = getIndex();
var exists = IndexReader.IndexExists(index); var exists = IndexReader.IndexExists(index);
@ -229,8 +213,6 @@ namespace LibationSearchEngine
var doc = createBookIndexDocument(libraryBook); var doc = createBookIndexDocument(libraryBook);
ixWriter.AddDocument(doc); ixWriter.AddDocument(doc);
} }
log();
} }
/// <summary>Long running. Use await Task.Run(() => UpdateBook(productId))</summary> /// <summary>Long running. Use await Task.Run(() => UpdateBook(productId))</summary>

View File

@ -304,7 +304,7 @@ An error occurred while trying to process this book.
protected override async Task RunAsync() protected override async Task RunAsync()
{ {
// support for 'skip this time only' requires state. iterators provide this state for free. therefore: use foreach/iterator here // support for 'skip this time only' requires state. iterators provide this state for free. therefore: use foreach/iterator here
foreach (var libraryBook in Processable.GetValidLibraryBooks(ApplicationServices.DbContexts.GetContext().GetLibrary_Flat_NoTracking())) foreach (var libraryBook in Processable.GetValidLibraryBooks(ApplicationServices.DbContexts.GetLibrary_Flat_NoTracking()))
{ {
var keepGoing = await ProcessOneAsync(libraryBook, validate: false); var keepGoing = await ProcessOneAsync(libraryBook, validate: false);
if (!keepGoing) if (!keepGoing)

View File

@ -26,7 +26,7 @@ namespace LibationWinForms.Dialogs
public RemoveBooksDialog(params Account[] accounts) public RemoveBooksDialog(params Account[] accounts)
{ {
_libraryBooks = DbContexts.GetContext().GetLibrary_Flat_NoTracking(); _libraryBooks = DbContexts.GetLibrary_Flat_NoTracking();
_accounts = accounts; _accounts = accounts;
InitializeComponent(); InitializeComponent();
@ -58,9 +58,9 @@ namespace LibationWinForms.Dialogs
return; return;
try try
{ {
var rmovedBooks = await LibraryCommands.FindInactiveBooks((account) => new WinformResponder(account), _libraryBooks, _accounts); var removedBooks = await LibraryCommands.FindInactiveBooks((account) => new WinformResponder(account), _libraryBooks, _accounts);
var removable = _removableGridEntries.Where(rge => rmovedBooks.Any(rb => rb.Book.AudibleProductId == rge.AudibleProductId)); var removable = _removableGridEntries.Where(rge => removedBooks.Any(rb => rb.Book.AudibleProductId == rge.AudibleProductId));
if (!removable.Any()) if (!removable.Any())
return; return;
@ -104,14 +104,8 @@ namespace LibationWinForms.Dialogs
if (result == DialogResult.Yes) if (result == DialogResult.Yes)
{ {
using var context = DbContexts.GetContext(); var idsToRemove = selectedBooks.Select(rge => rge.AudibleProductId).ToList();
var removeLibraryBooks = LibraryCommands.RemoveBooks(idsToRemove);
var libBooks = context.GetLibrary_Flat_NoTracking();
var removeLibraryBooks = libBooks.Where(lb => selectedBooks.Any(rge => rge.AudibleProductId == lb.Book.AudibleProductId)).ToList();
context.Library.RemoveRange(removeLibraryBooks);
context.SaveChanges();
foreach (var rEntry in selectedBooks) foreach (var rEntry in selectedBooks)
_removableGridEntries.Remove(rEntry); _removableGridEntries.Remove(rEntry);
@ -121,6 +115,7 @@ namespace LibationWinForms.Dialogs
UpdateSelection(); UpdateSelection();
} }
} }
private void UpdateSelection() private void UpdateSelection()
{ {
var selectedCount = SelectedCount; var selectedCount = SelectedCount;

View File

@ -116,8 +116,7 @@ namespace LibationWinForms
// //
// transform into sorted GridEntry.s BEFORE binding // transform into sorted GridEntry.s BEFORE binding
// //
using var context = DbContexts.GetContext(); var lib = DbContexts.GetLibrary_Flat_NoTracking();
var lib = context.GetLibrary_Flat_NoTracking();
// if no data. hide all columns. return // if no data. hide all columns. return
if (!lib.Any()) if (!lib.Any())