projects
/
Sone.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
🔀 Merge “refactoring/no-negative-identity-filter”
[Sone.git]
/
src
/
main
/
java
/
net
/
pterodactylus
/
sone
/
database
/
memory
/
MemoryBookmarkDatabase.java
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
..
1a4e119
100644
(file)
--- a/
src/main/java/net/pterodactylus/sone/database/memory/MemoryBookmarkDatabase.java
+++ b/
src/main/java/net/pterodactylus/sone/database/memory/MemoryBookmarkDatabase.java
@@
-1,5
+1,6
@@
package net.pterodactylus.sone.database.memory;
package net.pterodactylus.sone.database.memory;
+import static com.google.common.base.Optional.fromNullable;
import static com.google.common.collect.FluentIterable.from;
import java.util.HashSet;
import static com.google.common.collect.FluentIterable.from;
import java.util.HashSet;
@@
-8,40
+9,61
@@
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import net.pterodactylus.sone.data.Post;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import net.pterodactylus.sone.data.Post;
+import net.pterodactylus.sone.data.Post.EmptyPost;
import net.pterodactylus.sone.database.BookmarkDatabase;
import com.google.common.base.Function;
/**
* Memory-based {@link BookmarkDatabase} implementation.
import net.pterodactylus.sone.database.BookmarkDatabase;
import com.google.common.base.Function;
/**
* Memory-based {@link BookmarkDatabase} implementation.
- *
- * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
*/
public class MemoryBookmarkDatabase implements BookmarkDatabase {
private final ReadWriteLock lock = new ReentrantReadWriteLock();
private final MemoryDatabase memoryDatabase;
*/
public class MemoryBookmarkDatabase implements BookmarkDatabase {
private final ReadWriteLock lock = new ReentrantReadWriteLock();
private final MemoryDatabase memoryDatabase;
- private final Set<String> bookmarkedPosts = new HashSet<String>();
+ private final ConfigurationLoader configurationLoader;
+ private final Set<String> bookmarkedPosts = new HashSet<>();
- public MemoryBookmarkDatabase(MemoryDatabase memoryDatabase) {
+ public MemoryBookmarkDatabase(MemoryDatabase memoryDatabase,
+ ConfigurationLoader configurationLoader) {
this.memoryDatabase = memoryDatabase;
this.memoryDatabase = memoryDatabase;
+ this.configurationLoader = configurationLoader;
}
}
- @Override
- public void bookmarkPost(String postId) {
+ public void start() {
+ loadBookmarkedPosts();
+ }
+
+ private void loadBookmarkedPosts() {
+ Set<String> bookmarkedPosts = configurationLoader.loadBookmarkedPosts();
lock.writeLock().lock();
try {
lock.writeLock().lock();
try {
- bookmarkedPosts.add(postId);
+ this.bookmarkedPosts.clear();
+ this.bookmarkedPosts.addAll(bookmarkedPosts);
} finally {
lock.writeLock().unlock();
}
}
} finally {
lock.writeLock().unlock();
}
}
+ public void stop() {
+ saveBookmarkedPosts();
+ }
+
+ private void saveBookmarkedPosts() {
+ lock.readLock().lock();
+ try {
+ configurationLoader.saveBookmarkedPosts(this.bookmarkedPosts);
+ } finally {
+ lock.readLock().unlock();
+ }
+ }
+
@Override
public void bookmarkPost(Post post) {
lock.writeLock().lock();
try {
bookmarkedPosts.add(post.getId());
@Override
public void bookmarkPost(Post post) {
lock.writeLock().lock();
try {
bookmarkedPosts.add(post.getId());
+ saveBookmarkedPosts();
} finally {
lock.writeLock().unlock();
}
} finally {
lock.writeLock().unlock();
}
@@
-52,6
+74,7
@@
public class MemoryBookmarkDatabase implements BookmarkDatabase {
lock.writeLock().lock();
try {
bookmarkedPosts.remove(post.getId());
lock.writeLock().lock();
try {
bookmarkedPosts.remove(post.getId());
+ saveBookmarkedPosts();
} finally {
lock.writeLock().unlock();
}
} finally {
lock.writeLock().unlock();
}
@@
-71,11
+94,12
@@
public class MemoryBookmarkDatabase implements BookmarkDatabase {
public Set<Post> getBookmarkedPosts() {
lock.readLock().lock();
try {
public Set<Post> getBookmarkedPosts() {
lock.readLock().lock();
try {
- return from(bookmarkedPosts).transform
AndConcat
(
- new Function<String,
Iterable<Post>
>() {
+ return from(bookmarkedPosts).transform(
+ new Function<String,
Post
>() {
@Override
@Override
- public Iterable<Post> apply(String postId) {
- return memoryDatabase.getPost(postId).asSet();
+ public Post apply(String postId) {
+ return fromNullable(memoryDatabase.getPost(postId))
+ .or(new EmptyPost(postId));
}
}).toSet();
} finally {
}
}).toSet();
} finally {