public async Task <List <ManagedSite> > GetManagedSites(ManagedSiteFilter filter = null, bool reloadAll = true) { // Don't reload settings unless we need to or we are unsure if any items have changed if (!ManagedSitesCache.Any() || IsSingleInstanceMode == false || reloadAll) { await LoadAllManagedItems(); } // filter and convert dictionary to list TODO: use db instead of in memory filter? var items = ManagedSitesCache.Values.AsEnumerable(); if (filter != null) { if (!String.IsNullOrEmpty(filter.Keyword)) { items = items.Where(i => i.Name.ToLowerInvariant().Contains(filter.Keyword.ToLowerInvariant())); } //TODO: IncludeOnlyNextAutoRenew if (filter.MaxResults > 0) { items = items.Take(filter.MaxResults); } } return(new List <ManagedSite>(items)); }
public async Task LoadAllManagedItems(bool skipIfLoaded = false) { if (skipIfLoaded && ManagedSitesCache.Any()) { return; } await UpgradeSettings(); var watch = Stopwatch.StartNew(); // FIXME: this method should be async and called only when absolutely required, these // files can be hundreds of megabytes var path = GetDbPath(); if (File.Exists(path)) { var managedSites = new List <ManagedSite>(); using (var db = new SQLiteConnection($"Data Source={path}")) using (var cmd = new SQLiteCommand("SELECT id, json FROM manageditem", db)) { await db.OpenAsync(); using (var reader = await cmd.ExecuteReaderAsync()) { while (await reader.ReadAsync()) { string itemId = (string)reader["id"]; var managedSite = JsonConvert.DeserializeObject <ManagedSite>((string)reader["json"]); // in some cases users may have previously manipulated the id, causing // duplicates. Correct the ID here (database Id is unique): if (managedSite.Id != itemId) { managedSite.Id = itemId; Debug.WriteLine("LoadSettings: Corrected managed site id: " + managedSite.Name); } managedSites.Add(managedSite); } } } foreach (var site in managedSites) { site.IsChanged = false; } ManagedSitesCache = managedSites.ToDictionary(s => s.Id);; } else { ManagedSitesCache = new Dictionary <string, ManagedSite>(); } Debug.WriteLine($"LoadSettings[SQLite] took {watch.ElapsedMilliseconds}ms for {ManagedSitesCache.Count} records"); }
/// <summary> /// Perform a full backup and save of the current set of managed sites /// </summary> public async Task StoreSettings() { var watch = Stopwatch.StartNew(); var path = GetDbPath(); //create database if it doesn't exist if (!File.Exists(path)) { using (var db = new SQLiteConnection($"Data Source={path}")) { await db.OpenAsync(); using (var cmd = new SQLiteCommand("CREATE TABLE manageditem (id TEXT NOT NULL UNIQUE PRIMARY KEY, json TEXT NOT NULL)", db)) { await cmd.ExecuteNonQueryAsync(); } } } // save all new/modified items into settings database using (var db = new SQLiteConnection($"Data Source={path}")) { await db.OpenAsync(); using (var tran = db.BeginTransaction()) { foreach (var deleted in ManagedSitesCache.Values.Where(s => s.Deleted).ToList()) { using (var cmd = new SQLiteCommand("DELETE FROM manageditem WHERE id=@id", db)) { cmd.Parameters.Add(new SQLiteParameter("@id", deleted.Id)); await cmd.ExecuteNonQueryAsync(); } ManagedSitesCache.Remove(deleted.Id); } foreach (var changed in ManagedSitesCache.Values.Where(s => s.IsChanged)) { using (var cmd = new SQLiteCommand("INSERT OR REPLACE INTO manageditem (id,json) VALUES (@id,@json)", db)) { cmd.Parameters.Add(new SQLiteParameter("@id", changed.Id)); cmd.Parameters.Add(new SQLiteParameter("@json", JsonConvert.SerializeObject(changed))); await cmd.ExecuteNonQueryAsync(); } changed.IsChanged = false; } tran.Commit(); } } // reset IsChanged as all items have been persisted Debug.WriteLine($"StoreSettings[SQLite] took {watch.ElapsedMilliseconds}ms for {ManagedSitesCache.Count} records"); }
public async Task DeleteManagedSite(ManagedSite site) { // save modified items into settings database using (var db = new SQLiteConnection($"Data Source={GetDbPath()}")) { db.Open(); using (var tran = db.BeginTransaction()) { using (var cmd = new SQLiteCommand("DELETE FROM manageditem WHERE id=@id", db)) { cmd.Parameters.Add(new SQLiteParameter("@id", site.Id)); await cmd.ExecuteNonQueryAsync(); } tran.Commit(); Debug.WriteLine($"DeleteManagedSite: Completed {site.Id}"); ManagedSitesCache.Remove(site.Id); } } }
public async Task LoadAllManagedItems(bool skipIfLoaded = false) { if (skipIfLoaded && ManagedSitesCache.Any()) { return; } await UpgradeSettings(); var watch = Stopwatch.StartNew(); // FIXME: this method should be async and called only when absolutely required, these // files can be hundreds of megabytes var path = GetDbPath(); if (File.Exists(path)) { var managedSites = new List <ManagedSite>(); using (var db = new SQLiteConnection($"Data Source={path}")) using (var cmd = new SQLiteCommand("SELECT json FROM manageditem", db)) { await db.OpenAsync(); using (var reader = await cmd.ExecuteReaderAsync()) { while (await reader.ReadAsync()) { managedSites.Add(JsonConvert.DeserializeObject <ManagedSite>((string)reader["json"])); } } } foreach (var site in managedSites) { site.IsChanged = false; } ManagedSitesCache = managedSites.ToDictionary(s => s.Id); } else { ManagedSitesCache = new Dictionary <string, ManagedSite>(); } Debug.WriteLine($"LoadSettings[SQLite] took {watch.ElapsedMilliseconds}ms for {ManagedSitesCache.Count} records"); }
public async Task <ManagedSite> GetManagedSite(string siteId) { ManagedSite result = null; if (ManagedSitesCache == null || !ManagedSitesCache.Any()) { Debug.WriteLine("GetManagedSite: No managed sites loaded, will load item directly."); } else { // try to get cached version result = ManagedSitesCache.TryGetValue(siteId, out var retval) ? retval : null; } // if we don't have cached copy of info, load it from db if (result == null) { result = await LoadManagedSite(siteId); } return(result); }