diff options
author | Brendan Long <self@brendanlong.com> | 2019-01-29 02:31:02 +0300 |
---|---|---|
committer | Brendan Long <self@brendanlong.com> | 2019-01-29 02:35:13 +0300 |
commit | 9194a3b74d7e262a0869c874483af366bf26da68 (patch) | |
tree | 07177bd49ab57c18757e03ab4e6f32a774eb1443 /src | |
parent | 89ddf05ad3cd6245c9a7b51150f2455b96768df9 (diff) |
Re-use databases in the most obvious (single function) cases
These cases don't involve any threading so this should speed things
up a little without any risk.
Diffstat (limited to 'src')
-rw-r--r-- | src/ActionCache.vala | 10 | ||||
-rw-r--r-- | src/Backend/Backend.vala | 29 | ||||
-rw-r--r-- | src/Backend/FeedServer.vala | 60 | ||||
-rw-r--r-- | src/CachedActionManager.vala | 14 | ||||
-rw-r--r-- | src/Utils.vala | 13 | ||||
-rw-r--r-- | src/Widgets/FeedList.vala | 24 | ||||
-rw-r--r-- | src/Widgets/MainWindow.vala | 5 | ||||
-rw-r--r-- | src/Widgets/TagPopover.vala | 3 |
8 files changed, 91 insertions, 67 deletions
diff --git a/src/ActionCache.vala b/src/ActionCache.vala index aa892014..36413376 100644 --- a/src/ActionCache.vala +++ b/src/ActionCache.vala @@ -119,12 +119,15 @@ private void removeOpposite(CachedAction action) private void removeForFeed(string feedID) { + DataBaseReadOnly db = null; foreach(CachedAction a in m_list) { if(a.getType() == CachedActions.MARK_READ || a.getType() == CachedActions.MARK_UNREAD) { - if(feedID == DataBase.readOnly().getFeedIDofArticle(a.getID())) + if (db == null) + db = DataBase.readOnly(); + if(feedID == db.getFeedIDofArticle(a.getID())) { m_list.remove(a); } @@ -204,6 +207,7 @@ public ArticleStatus checkRead(Article a) } else if(a.getUnread() == ArticleStatus.UNREAD) { + DataBaseReadOnly db = null; foreach(CachedAction action in m_list) { switch(action.getType()) @@ -217,7 +221,9 @@ public ArticleStatus checkRead(Article a) break; case CachedActions.MARK_READ_CATEGORY: - var feedIDs = DataBase.readOnly().getFeedIDofCategorie(a.getArticleID()); + if (db == null) + db = DataBase.readOnly(); + var feedIDs = db.getFeedIDofCategorie(a.getArticleID()); foreach(string feedID in feedIDs) { if(feedID == a.getFeedID()) diff --git a/src/Backend/Backend.vala b/src/Backend/Backend.vala index 4f426963..a60197f7 100644 --- a/src/Backend/Backend.vala +++ b/src/Backend/Backend.vala @@ -437,15 +437,16 @@ public void tagArticle(Article article, Tag tag, bool add) article.removeTag(tag.getTagID()); } - DataBase.writeAccess().update_article(article); + var db = DataBase.writeAccess(); + db.update_article(article); - if(!add && !DataBase.readOnly().tag_still_used(tag)) + if(!add && !db.tag_still_used(tag)) { Logger.debug("backend: remove tag completely"); asyncPayload pl2 = () => { FeedServer.get_default().deleteTag(tag.getTagID()); }; callAsync.begin((owned)pl2, (obj, res) => { callAsync.end(res); }); - asyncPayload pl3 = () => { DataBase.writeAccess().dropTag(tag); }; + asyncPayload pl3 = () => { db.dropTag(tag); }; callAsync.begin((owned)pl3, (obj, res) => { callAsync.end(res); newFeedList(); @@ -494,8 +495,9 @@ public void updateTagColor(Tag tag) public void resetDB() { - DataBase.writeAccess().resetDB(); - DataBase.writeAccess().init(); + var db = DataBase.writeAccess(); + db.resetDB(); + db.init(); } public void resetAccount() @@ -642,7 +644,8 @@ public void markAllItemsRead() public void removeCategory(string catID) { - var feeds = DataBase.readOnly().read_feeds(); + var db = DataBase.writeAccess(); + var feeds = db.read_feeds(); foreach(Feed feed in feeds) { if(feed.hasCat(catID)) @@ -651,7 +654,7 @@ public void removeCategory(string catID) } } - var cats = DataBase.readOnly().read_categories(feeds); + var cats = db.read_categories(feeds); foreach(var cat in cats) { if(cat.getParent() == catID) @@ -663,7 +666,7 @@ public void removeCategory(string catID) asyncPayload pl = () => { FeedServer.get_default().deleteCategory(catID); }; callAsync.begin((owned)pl, (obj, res) => { callAsync.end(res); }); - asyncPayload pl2 = () => { DataBase.writeAccess().delete_category(catID); }; + asyncPayload pl2 = () => { db.delete_category(catID); }; callAsync.begin((owned)pl2, (obj, res) => { callAsync.end(res); newFeedList(); @@ -691,20 +694,21 @@ public string addCategory(string title, string? parentID = null, bool createLoca { string? parent = parentID; int level = 1; + var db = DataBase.writeAccess(); if(parentID == null || parentID == "") { parent = CategoryID.MASTER.to_string(); } else { - var parentCat = DataBase.readOnly().read_category(parentID); + var parentCat = db.read_category(parentID); level = parentCat.getLevel()+1; } var cat = new Category(catID, title, 0, 99, parent, level); var list = new Gee.LinkedList<Category>(); list.add(cat); - DataBase.writeAccess().write_categories(list); + db.write_categories(list); } return catID; @@ -712,10 +716,11 @@ public string addCategory(string title, string? parentID = null, bool createLoca public void removeCategoryWithChildren(string catID) { - var feeds = DataBase.readOnly().read_feeds(); + var db = DataBase.readOnly(); + var feeds = db.read_feeds(); deleteFeedsInCategory(catID, feeds); - var cats = DataBase.readOnly().read_categories(feeds); + var cats = db.read_categories(feeds); foreach(var cat in cats) { if(cat.getParent() == catID) diff --git a/src/Backend/FeedServer.vala b/src/Backend/FeedServer.vala index 2ee30bae..58ef831e 100644 --- a/src/Backend/FeedServer.vala +++ b/src/Backend/FeedServer.vala @@ -174,6 +174,7 @@ public void syncContent(GLib.Cancellable? cancellable = null) return; } + var db = DataBase.writeAccess(); if(syncFeedsAndCategories()) { var categories = new Gee.LinkedList<Category>(); @@ -198,21 +199,21 @@ public void syncContent(GLib.Cancellable? cancellable = null) return; // write categories - DataBase.writeAccess().reset_exists_flag(); - DataBase.writeAccess().write_categories(categories); - DataBase.writeAccess().delete_nonexisting_categories(); + db.reset_exists_flag(); + db.write_categories(categories); + db.delete_nonexisting_categories(); // write feeds - DataBase.writeAccess().reset_subscribed_flag(); - DataBase.writeAccess().write_feeds(feeds); - DataBase.writeAccess().delete_articles_without_feed(); - DataBase.writeAccess().delete_unsubscribed_feeds(); + db.reset_subscribed_flag(); + db.write_feeds(feeds); + db.delete_articles_without_feed(); + db.delete_unsubscribed_feeds(); // write tags - DataBase.writeAccess().reset_exists_tag(); - DataBase.writeAccess().write_tags(tags); - DataBase.writeAccess().update_tags(tags); - DataBase.writeAccess().delete_nonexisting_tags(); + db.reset_exists_tag(); + db.write_tags(tags); + db.update_tags(tags); + db.delete_nonexisting_tags(); FeedReaderBackend.get_default().newFeedList(); } @@ -222,7 +223,7 @@ public void syncContent(GLib.Cancellable? cancellable = null) var drop_articles = (DropArticles)Settings.general().get_enum("drop-articles-after"); DateTime? since = drop_articles.to_start_date(); - if(!DataBase.readOnly().isTableEmpty("articles")) + if(!db.isTableEmpty("articles")) { var last_sync = new DateTime.from_unix_utc(Settings.state().get_int("last-sync")); if(since == null || last_sync.to_unix() > since.to_unix()) @@ -236,7 +237,7 @@ public void syncContent(GLib.Cancellable? cancellable = null) syncProgress(_("Getting articles")); - string row_id = DataBase.readOnly().getMaxID("articles", "rowid"); + string row_id = db.getMaxID("articles", "rowid"); int before = row_id != null ? int.parse(row_id) : 0; if(unread > max && useMaxArticles()) @@ -253,10 +254,10 @@ public void syncContent(GLib.Cancellable? cancellable = null) return; //update fulltext table - DataBase.writeAccess().updateFTS(); + db.updateFTS(); - int new_and_unread = DataBase.readOnly().get_new_unread_count(row_id != null ? int.parse(row_id) : 0); - row_id = DataBase.readOnly().getMaxID("articles", "rowid"); + int new_and_unread = db.get_new_unread_count(row_id != null ? int.parse(row_id) : 0); + row_id = db.getMaxID("articles", "rowid"); int after = row_id != null ? int.parse(row_id) : 0; int newArticles = after-before; if(newArticles > 0) @@ -266,12 +267,12 @@ public void syncContent(GLib.Cancellable? cancellable = null) var drop_weeks = drop_articles.to_weeks(); if(drop_weeks != null) - DataBase.writeAccess().dropOldArticles(-(int)drop_weeks); + db.dropOldArticles(-(int)drop_weeks); var now = new DateTime.now_local(); Settings.state().set_int("last-sync", (int)now.to_unix()); - DataBase.writeAccess().checkpoint(); + db.checkpoint(); FeedReaderBackend.get_default().newFeedList(); return; } @@ -280,6 +281,7 @@ public void InitSyncContent(GLib.Cancellable? cancellable = null) { Logger.debug("FeedServer: initial sync"); + var db = DataBase.writeAccess(); if(syncFeedsAndCategories()) { var categories = new Gee.LinkedList<Category>(); @@ -297,13 +299,13 @@ public void InitSyncContent(GLib.Cancellable? cancellable = null) return; // write categories - DataBase.writeAccess().write_categories(categories); + db.write_categories(categories); // write feeds - DataBase.writeAccess().write_feeds(feeds); + db.write_feeds(feeds); // write tags - DataBase.writeAccess().write_tags(tags); + db.write_tags(tags); FeedReaderBackend.get_default().newFeedList(); } @@ -323,7 +325,7 @@ public void InitSyncContent(GLib.Cancellable? cancellable = null) // get articles for each tag syncProgress(_("Getting tagged articles")); - foreach(var tag_item in DataBase.readOnly().read_tags()) + foreach(var tag_item in db.read_tags()) { getArticles((Settings.general().get_int("max-articles")/8), ArticleStatus.ALL, since, tag_item.getTagID(), true, cancellable); if(cancellable != null && cancellable.is_cancelled()) @@ -347,7 +349,7 @@ public void InitSyncContent(GLib.Cancellable? cancellable = null) return; //update fulltext table - DataBase.writeAccess().updateFTS(); + db.updateFTS(); Settings.general().reset("content-grabber"); @@ -361,7 +363,8 @@ private void writeArticles(Gee.List<Article> articles) { if(articles.size > 0) { - DataBase.writeAccess().update_articles(articles); + var db = DataBase.writeAccess(); + db.update_articles(articles); // Reverse the list var new_articles = new Gee.ArrayList<Article>(); @@ -370,7 +373,7 @@ private void writeArticles(Gee.List<Article> articles) new_articles.insert(0, article); } - DataBase.writeAccess().write_articles(new_articles); + db.write_articles(new_articles); FeedReaderBackend.get_default().refreshFeedListCounter(); FeedReaderBackend.get_default().updateArticleList(); } @@ -383,7 +386,8 @@ public async void grabContent(GLib.Cancellable? cancellable = null) return; Logger.debug("FeedServer: grabContent"); - var articles = DataBase.readOnly().readUnfetchedArticles(); + var db = DataBase.writeAccess(); + var articles = db.readUnfetchedArticles(); int size = articles.size; int i = 0; var new_article_content = new Gee.ArrayList<Article>(); @@ -466,14 +470,14 @@ public async void grabContent(GLib.Cancellable? cancellable = null) { if (cancellable != null && cancellable.is_cancelled()) return; - DataBase.writeAccess().writeContent(content); + db.writeContent(content); } if (cancellable != null && cancellable.is_cancelled()) return; //update fulltext table - DataBase.writeAccess().updateFTS(); + db.updateFTS(); } } diff --git a/src/CachedActionManager.vala b/src/CachedActionManager.vala index 098e3dae..edfe0c7f 100644 --- a/src/CachedActionManager.vala +++ b/src/CachedActionManager.vala @@ -74,19 +74,21 @@ public void markAllRead() private void addAction(CachedAction action) { - if(DataBase.writeAccess().cachedActionNecessary(action)) + var db = DataBase.writeAccess(); + if(db.cachedActionNecessary(action)) { - DataBase.writeAccess().addCachedAction(action.getType(), action.getID()); + db.addCachedAction(action.getType(), action.getID()); } else { - DataBase.writeAccess().deleteOppositeCachedAction(action); + db.deleteOppositeCachedAction(action); } } public void executeActions() { - if(DataBase.readOnly().isTableEmpty("CachedActions")) + var db = DataBase.writeAccess(); + if(db.isTableEmpty("CachedActions")) { Logger.debug("CachedActionManager - executeActions: no actions to perform"); return; @@ -95,7 +97,7 @@ public void executeActions() Logger.debug("CachedActionManager: executeActions"); - var actions = DataBase.writeAccess().readCachedActions(); + var actions = db.readCachedActions(); foreach(CachedAction action in actions) { @@ -141,7 +143,7 @@ public void executeActions() execute(m_ids.substring(1), m_lastAction); } - DataBase.writeAccess().resetCachedActions(); + db.resetCachedActions(); } private void execute(string ids, CachedActions action) diff --git a/src/Utils.vala b/src/Utils.vala index 3716728d..b463299c 100644 --- a/src/Utils.vala +++ b/src/Utils.vala @@ -33,15 +33,16 @@ public static Soup.Session getSession() public static void generatePreviews(Gee.List<Article> articles) { string noPreview = _("No Preview Available"); + var db = DataBase.readOnly(); foreach(var Article in articles) { - if(!DataBase.readOnly().article_exists(Article.getArticleID())) + if(!db.article_exists(Article.getArticleID())) { if(Article.getPreview() != null && Article.getPreview() != "") { continue; } - if(!DataBase.readOnly().preview_empty(Article.getArticleID())) + if(!db.preview_empty(Article.getArticleID())) { continue; } @@ -85,9 +86,10 @@ public static void generatePreviews(Gee.List<Article> articles) public static void checkHTML(Gee.List<Article> articles) { + var db = DataBase.readOnly(); foreach(var Article in articles) { - if(!DataBase.readOnly().article_exists(Article.getArticleID())) + if(!db.article_exists(Article.getArticleID())) { string modified_html = _("No Text available for this article :("); if(Article.getHTML() != "") @@ -770,9 +772,10 @@ public static bool onlyShowFeeds() if(Settings.general().get_boolean("only-feeds")) return true; - if(!DataBase.readOnly().haveCategories() + var db = DataBase.readOnly(); + if(!db.haveCategories() && !FeedReaderBackend.get_default().supportTags() - && !DataBase.readOnly().haveFeedsWithoutCat()) + && !db.haveFeedsWithoutCat()) return true; return false; diff --git a/src/Widgets/FeedList.vala b/src/Widgets/FeedList.vala index 1f9efbf3..bf0cff7e 100644 --- a/src/Widgets/FeedList.vala +++ b/src/Widgets/FeedList.vala @@ -465,7 +465,8 @@ private void addTagCategory(int length) private void createCategories(ref Gee.List<Feed> feeds, bool masterCat, ArticleListState state) { - int maxCatLevel = DataBase.readOnly().getMaxCatLevel(); + var db = DataBase.readOnly(); + int maxCatLevel = db.getMaxCatLevel(); int length = (int)m_list.get_children().length(); bool supportTags = false; bool supportCategories = true; @@ -478,7 +479,7 @@ private void createCategories(ref Gee.List<Feed> feeds, bool masterCat, ArticleL supportMultiLevelCategories = FeedReaderBackend.get_default().supportMultiLevelCategories(); if((supportTags - && !DataBase.readOnly().isTableEmpty("tags")) + && !db.isTableEmpty("tags")) || masterCat) { addMasterCategory(length, _("Categories")); @@ -497,16 +498,16 @@ private void createCategories(ref Gee.List<Feed> feeds, bool masterCat, ArticleL for(int i = 1; i <= maxCatLevel; i++) { - var categories = DataBase.readOnly().read_categories_level(i, feeds); + var categories = db.read_categories_level(i, feeds); - if(DataBase.readOnly().haveFeedsWithoutCat()) + if(db.haveFeedsWithoutCat()) { categories.insert( 0, new Category( uncategorizedID, _("Uncategorized"), - (int)DataBase.readOnly().get_unread_uncategorized(), + (int)db.get_unread_uncategorized(), (int)(categories.size + 10), CategoryID.MASTER.to_string(), 1 @@ -592,9 +593,10 @@ private void createTags() public void refreshCounters(ArticleListState state) { - uint allCount = (state == ArticleListState.MARKED) ? DataBase.readOnly().get_marked_total() : DataBase.readOnly().get_unread_total(); - var feeds = DataBase.readOnly().read_feeds((state == ArticleListState.MARKED) ? true : false); - var categories = DataBase.readOnly().read_categories(feeds); + var db = DataBase.readOnly(); + uint allCount = (state == ArticleListState.MARKED) ? db.get_marked_total() : db.get_unread_total(); + var feeds = db.read_feeds((state == ArticleListState.MARKED) ? true : false); + var categories = db.read_categories(feeds); // double-check uint feedCount = 0; @@ -674,7 +676,7 @@ public void refreshCounters(ArticleListState state) } } - if(DataBase.readOnly().haveFeedsWithoutCat()) + if(db.haveFeedsWithoutCat()) { foreach(Gtk.Widget row in FeedChildList) { @@ -683,12 +685,12 @@ public void refreshCounters(ArticleListState state) { if(state == ArticleListState.MARKED) { - tmpCatRow.set_unread_count(DataBase.readOnly().get_marked_uncategorized()); + tmpCatRow.set_unread_count(db.get_marked_uncategorized()); tmpCatRow.activateUnreadEventbox(false); } else { - tmpCatRow.set_unread_count(DataBase.readOnly().get_unread_uncategorized()); + tmpCatRow.set_unread_count(db.get_unread_uncategorized()); tmpCatRow.activateUnreadEventbox(true); } if(Settings.general().get_boolean("feedlist-only-show-unread") && tmpCatRow.getUnreadCount() != 0) diff --git a/src/Widgets/MainWindow.vala b/src/Widgets/MainWindow.vala index e29105ef..76901972 100644 --- a/src/Widgets/MainWindow.vala +++ b/src/Widgets/MainWindow.vala @@ -476,14 +476,15 @@ private void markSelectedRead() { if(selectedRow[1] == FeedID.ALL.to_string()) { - var categories = DataBase.readOnly().read_categories(); + var db = DataBase.readOnly(); + var categories = db.read_categories(); foreach(Category cat in categories) { FeedReaderBackend.get_default().markFeedAsRead(cat.getCatID(), true); Logger.debug("MainWindow: mark all articles as read cat: %s".printf(cat.getTitle())); } - var feeds = DataBase.readOnly().read_feeds_without_cat(); + var feeds = db.read_feeds_without_cat(); foreach(Feed feed in feeds) { FeedReaderBackend.get_default().markFeedAsRead(feed.getFeedID(), false); diff --git a/src/Widgets/TagPopover.vala b/src/Widgets/TagPopover.vala index e33ef6db..bbba5981 100644 --- a/src/Widgets/TagPopover.vala +++ b/src/Widgets/TagPopover.vala @@ -29,12 +29,13 @@ public TagPopover(Gtk.Widget widget) m_availableTags = new Gee.ArrayList<Tag>(); m_tags = new Gee.ArrayList<Tag>(); Article? selectedArticle = ColumnView.get_default().getSelectedArticle(); + var db = DataBase.readOnly(); if(selectedArticle != null) { Gee.List<string> tagIDs = selectedArticle.getTagIDs(); foreach(string tagID in tagIDs) { - m_tags.add(DataBase.readOnly().read_tag(tagID)); + m_tags.add(db.read_tag(tagID)); } } |