From: David ‘Bombe’ Roden Date: Tue, 4 Nov 2014 18:39:50 +0000 (+0100) Subject: Load bookmarked posts in configuration loader, too. X-Git-Tag: 0.9-rc1^2~3^2~52 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=36b8e20651c14970ca30a1b8ed43af219b367bc9;p=Sone.git Load bookmarked posts in configuration loader, too. --- diff --git a/src/main/java/net/pterodactylus/sone/database/memory/ConfigurationLoader.java b/src/main/java/net/pterodactylus/sone/database/memory/ConfigurationLoader.java index 078b5f4..24505ac 100644 --- a/src/main/java/net/pterodactylus/sone/database/memory/ConfigurationLoader.java +++ b/src/main/java/net/pterodactylus/sone/database/memory/ConfigurationLoader.java @@ -48,4 +48,19 @@ public class ConfigurationLoader { return knownPostReplies; } + public synchronized Set loadBookmarkedPosts() { + Set bookmarkedPosts = new HashSet(); + int postCounter = 0; + while (true) { + String bookmarkedPostId = configuration + .getStringValue("Bookmarks/Post/" + postCounter++ + "/ID") + .getValue(null); + if (bookmarkedPostId == null) { + break; + } + bookmarkedPosts.add(bookmarkedPostId); + } + return bookmarkedPosts; + } + } diff --git a/src/main/java/net/pterodactylus/sone/database/memory/MemoryBookmarkDatabase.java b/src/main/java/net/pterodactylus/sone/database/memory/MemoryBookmarkDatabase.java index 1aec573..78c8a4f 100644 --- a/src/main/java/net/pterodactylus/sone/database/memory/MemoryBookmarkDatabase.java +++ b/src/main/java/net/pterodactylus/sone/database/memory/MemoryBookmarkDatabase.java @@ -21,10 +21,28 @@ public class MemoryBookmarkDatabase implements BookmarkDatabase { private final ReadWriteLock lock = new ReentrantReadWriteLock(); private final MemoryDatabase memoryDatabase; + private final ConfigurationLoader configurationLoader; private final Set bookmarkedPosts = new HashSet(); - public MemoryBookmarkDatabase(MemoryDatabase memoryDatabase) { + public MemoryBookmarkDatabase(MemoryDatabase memoryDatabase, + ConfigurationLoader configurationLoader) { this.memoryDatabase = memoryDatabase; + this.configurationLoader = configurationLoader; + } + + public void start() { + loadBookmarkedPosts(); + } + + private void loadBookmarkedPosts() { + Set bookmarkedPosts = configurationLoader.loadBookmarkedPosts(); + lock.writeLock().lock(); + try { + this.bookmarkedPosts.clear(); + this.bookmarkedPosts.addAll(bookmarkedPosts); + } finally { + lock.writeLock().unlock(); + } } @Override diff --git a/src/main/java/net/pterodactylus/sone/database/memory/MemoryDatabase.java b/src/main/java/net/pterodactylus/sone/database/memory/MemoryDatabase.java index 3c5ef55..6481a1b 100644 --- a/src/main/java/net/pterodactylus/sone/database/memory/MemoryDatabase.java +++ b/src/main/java/net/pterodactylus/sone/database/memory/MemoryDatabase.java @@ -116,8 +116,7 @@ public class MemoryDatabase extends AbstractService implements Database { private final Map allImages = new HashMap(); private final Multimap soneImages = HashMultimap.create(); - private final MemoryBookmarkDatabase memoryBookmarkDatabase = - new MemoryBookmarkDatabase(this); + private final MemoryBookmarkDatabase memoryBookmarkDatabase; /** * Creates a new memory database. @@ -132,6 +131,8 @@ public class MemoryDatabase extends AbstractService implements Database { this.soneProvider = soneProvider; this.configuration = configuration; this.configurationLoader = new ConfigurationLoader(configuration); + memoryBookmarkDatabase = + new MemoryBookmarkDatabase(this, configurationLoader); } // @@ -157,6 +158,7 @@ public class MemoryDatabase extends AbstractService implements Database { /** {@inheritDocs} */ @Override protected void doStart() { + memoryBookmarkDatabase.start(); loadKnownPosts(); loadKnownPostReplies(); notifyStarted(); diff --git a/src/test/java/net/pterodactylus/sone/database/memory/ConfigurationLoaderTest.java b/src/test/java/net/pterodactylus/sone/database/memory/ConfigurationLoaderTest.java index 82d465b..de357e9 100644 --- a/src/test/java/net/pterodactylus/sone/database/memory/ConfigurationLoaderTest.java +++ b/src/test/java/net/pterodactylus/sone/database/memory/ConfigurationLoaderTest.java @@ -48,4 +48,16 @@ public class ConfigurationLoaderTest { containsInAnyOrder("PostReply1", "PostReply2")); } + @Test + public void loaderCanLoadBookmarkedPosts() { + when(configuration.getStringValue("Bookmarks/Post/0/ID")) + .thenReturn(new TestValue("Post2")); + when(configuration.getStringValue("Bookmarks/Post/1/ID")) + .thenReturn(new TestValue("Post1")); + when(configuration.getStringValue("Bookmarks/Post/2/ID")) + .thenReturn(new TestValue(null)); + Set knownPosts = configurationLoader.loadBookmarkedPosts(); + assertThat(knownPosts, containsInAnyOrder("Post1", "Post2")); + } + } diff --git a/src/test/java/net/pterodactylus/sone/database/memory/MemoryBookmarkDatabaseTest.java b/src/test/java/net/pterodactylus/sone/database/memory/MemoryBookmarkDatabaseTest.java index 93efe11..ce61ade 100644 --- a/src/test/java/net/pterodactylus/sone/database/memory/MemoryBookmarkDatabaseTest.java +++ b/src/test/java/net/pterodactylus/sone/database/memory/MemoryBookmarkDatabaseTest.java @@ -29,8 +29,9 @@ import org.mockito.stubbing.Answer; public class MemoryBookmarkDatabaseTest { private final MemoryDatabase memoryDatabase = mock(MemoryDatabase.class); + private final ConfigurationLoader configurationLoader = mock(ConfigurationLoader.class); private final MemoryBookmarkDatabase bookmarkDatabase = - new MemoryBookmarkDatabase(memoryDatabase); + new MemoryBookmarkDatabase(memoryDatabase, configurationLoader); private final Map posts = new HashMap(); @Before