Much faster for grid refresh

This commit is contained in:
Robert McRackan 2021-07-29 14:55:48 -04:00
parent a3542c53e2
commit 1fcacb9cfb
10 changed files with 30 additions and 21 deletions

View File

@ -119,6 +119,9 @@ namespace ApplicationServices
var udi = book.UserDefinedItem;
if (udi.Tags == newTags)
return 0;
// Attach() NoTracking entities before SaveChanges()
udi.Tags = newTags;
context.Attach(udi).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
@ -144,6 +147,9 @@ namespace ApplicationServices
var udi = libraryBook.Book.UserDefinedItem;
if (udi.BookStatus == liberatedStatus && udi.BookLocation == finalAudioPath)
return 0;
// Attach() NoTracking entities before SaveChanges()
udi.BookStatus = liberatedStatus;
udi.BookLocation = finalAudioPath;
@ -169,6 +175,9 @@ namespace ApplicationServices
var udi = libraryBook.Book.UserDefinedItem;
if (udi.PdfStatus == liberatedStatus)
return 0;
// Attach() NoTracking entities before SaveChanges()
udi.PdfStatus = liberatedStatus;
context.Attach(udi).State = Microsoft.EntityFrameworkCore.EntityState.Modified;

View File

@ -7,10 +7,10 @@ namespace ApplicationServices
{
public static class SearchEngineCommands
{
public static void FullReIndex()
public static void FullReIndex(SearchEngine engine = null)
{
var engine = new SearchEngine(DbContexts.GetContext());
engine.CreateNewIndex();
engine ??= new SearchEngine();
engine.CreateNewIndex(DbContexts.GetContext());
}
public static SearchResultSet Search(string searchString) => performSearchEngineFunc_safe(e =>
@ -27,28 +27,28 @@ namespace ApplicationServices
private static void performSearchEngineAction_safe(Action<SearchEngine> action)
{
var engine = new SearchEngine(DbContexts.GetContext());
var engine = new SearchEngine();
try
{
action(engine);
}
catch (FileNotFoundException)
{
FullReIndex();
FullReIndex(engine);
action(engine);
}
}
private static T performSearchEngineFunc_safe<T>(Func<SearchEngine, T> action)
{
var engine = new SearchEngine(DbContexts.GetContext());
var engine = new SearchEngine();
try
{
return action(engine);
}
catch (FileNotFoundException)
{
FullReIndex();
FullReIndex(engine);
return action(engine);
}
}

View File

@ -6,6 +6,8 @@ using Microsoft.EntityFrameworkCore;
namespace DataLayer
{
// only library importing should use tracking. All else should be NoTracking.
// only library importing should directly query Book. All else should use LibraryBook
public static class BookQueries
{
public static Book GetBook_Flat_NoTracking(this LibationContext context, string productId)

View File

@ -4,6 +4,8 @@ using Microsoft.EntityFrameworkCore;
namespace DataLayer
{
// only library importing should use tracking. All else should be NoTracking.
// only library importing should directly query Book. All else should use LibraryBook
public static class LibraryQueries
{
//// tracking is a bad idea for main grid. it prevents anything else from updating entities unless getting them from the grid

View File

@ -361,7 +361,7 @@ namespace LibationLauncher
config.ConfigureLogging();
// Fwd Console to serilog.
// Serilog also write to Console (should probably change this) so it might be asking for trouble.
// Serilog also writes to Console (should probably change this) so it might be asking for trouble.
// SerilogTextWriter needs to be more robust and tested. Esp the Write() methods.
// Empirical testing so far has shown no issues.
Console.SetOut(new MultiTextWriter(origOut, new SerilogTextWriter()));

View File

@ -18,8 +18,6 @@ namespace LibationSearchEngine
{
public const Lucene.Net.Util.Version Version = Lucene.Net.Util.Version.LUCENE_30;
private LibationContext context { get; }
// not customizable. don't move to config
private static string SearchEngineDirectory { get; }
= new System.IO.DirectoryInfo(Configuration.Instance.LibationFiles).CreateSubdirectory("SearchEngine").FullName;
@ -32,8 +30,6 @@ namespace LibationSearchEngine
// the workaround which allows displaying all books when query is empty
public const string ALL_QUERY = "*:*";
public SearchEngine(LibationContext context) => this.context = context;
#region index rules
private static ReadOnlyDictionary<string, Func<LibraryBook, string>> idIndexRules { get; }
= new ReadOnlyDictionary<string, Func<LibraryBook, string>>(
@ -202,7 +198,7 @@ namespace LibationSearchEngine
/// create new. ie: full re-index
/// </summary>
/// <param name="overwrite"></param>
public void CreateNewIndex(bool overwrite = true)
public void CreateNewIndex(LibationContext context, bool overwrite = true)
{
// 300 titles: 200- 400 ms
// 1021 titles: 1777-2250 ms
@ -235,7 +231,7 @@ namespace LibationSearchEngine
}
/// <summary>Long running. Use await Task.Run(() => UpdateBook(productId))</summary>
public void UpdateBook(string productId)
public void UpdateBook(LibationContext context, string productId)
{
var libraryBook = context.GetLibraryBook_Flat_NoTracking(productId);
var term = new Term(_ID_, productId);

View File

@ -1,6 +1,6 @@
namespace LibationWinForms.Dialogs
{
partial class EditTagsDialog
partial class BookDetailsDialog
{
/// <summary>
/// Required designer variable.

View File

@ -3,15 +3,15 @@ using System.Windows.Forms;
namespace LibationWinForms.Dialogs
{
public partial class EditTagsDialog : Form
public partial class BookDetailsDialog : Form
{
public string NewTags { get; private set; }
public EditTagsDialog()
public BookDetailsDialog()
{
InitializeComponent();
}
public EditTagsDialog(string title, string rawTags) : this()
public BookDetailsDialog(string title, string rawTags) : this()
{
this.Text = $"Edit Tags - {title}";

View File

@ -255,11 +255,11 @@ namespace LibationWinForms
// EditTagsDialog should display better-formatted title
liveGridEntry.TryDisplayValue(nameof(liveGridEntry.Title), out string value);
var editTagsForm = new EditTagsDialog(value, liveGridEntry.Tags);
if (editTagsForm.ShowDialog() != DialogResult.OK)
var bookDetailsForm = new BookDetailsDialog(value, liveGridEntry.Tags);
if (bookDetailsForm.ShowDialog() != DialogResult.OK)
return;
var qtyChanges = LibraryCommands.UpdateTags(liveGridEntry.GetBook(), editTagsForm.NewTags);
var qtyChanges = LibraryCommands.UpdateTags(liveGridEntry.GetBook(), bookDetailsForm.NewTags);
if (qtyChanges == 0)
return;