import net.pterodactylus.util.config.Configuration
import net.pterodactylus.util.config.ConfigurationException
import java.util.concurrent.locks.ReentrantReadWriteLock
+import java.util.concurrent.locks.ReentrantReadWriteLock.ReadLock
+import java.util.concurrent.locks.ReentrantReadWriteLock.WriteLock
import kotlin.concurrent.withLock
/**
class MemoryDatabase @Inject constructor(private val configuration: Configuration) : AbstractService(), Database {
private val lock = ReentrantReadWriteLock()
- private val readLock by lazy { lock.readLock()!! }
- private val writeLock by lazy { lock.writeLock()!! }
+ private val readLock: ReadLock by lazy { lock.readLock() }
+ private val writeLock: WriteLock by lazy { lock.writeLock() }
private val configurationLoader = ConfigurationLoader(configuration)
private val allSones = mutableMapOf<String, Sone>()
private val allPosts = mutableMapOf<String, Post>()
private val sonePosts: Multimap<String, Post> = HashMultimap.create<String, Post>()
private val knownPosts = mutableSetOf<String>()
- private val allPostReplies = mutableMapOf<String, PostReply>()
+ private val allPostReplies = mutableMapOf<String, MemoryPostReply.Shell>()
private val sonePostReplies: Multimap<String, PostReply> = TreeMultimap.create<String, PostReply>(Comparator { leftString, rightString -> leftString.compareTo(rightString) }, newestReplyFirst)
private val knownPostReplies = mutableSetOf<String>()
private val allAlbums = mutableMapOf<String, Album>()
}
sonePostReplies.putAll(sone.id, sone.replies)
for (postReply in sone.replies) {
- allPostReplies[postReply.id] = postReply
+ allPostReplies[postReply.id] = postReply.toShell()
}
sone.allAlbums.let { albums ->
soneAlbums.putAll(sone.id, albums)
}
}
- override fun getPostReply(id: String) = readLock.withLock { allPostReplies[id] }
+ override fun getPostReply(id: String) = readLock.withLock {
+ allPostReplies[id]?.build(newPostReplyBuilder())
+ }
override fun getReplies(postId: String) =
readLock.withLock {
allPostReplies.values
.filter { it.postId == postId }
+ .map { it.build(newPostReplyBuilder()) }
.sortedWith(newestReplyFirst.reversed())
}
override fun storePostReply(postReply: PostReply) =
writeLock.withLock {
- allPostReplies[postReply.id] = postReply
+ allPostReplies[postReply.id] = postReply.toShell()
}
override fun removePostReply(postReply: PostReply) =
override fun isPostBookmarked(post: Post) =
memoryBookmarkDatabase.isPostBookmarked(post)
- protected fun isPostKnown(post: Post) = readLock.withLock { post.id in knownPosts }
+ internal fun isPostKnown(post: Post) = readLock.withLock { post.id in knownPosts }
fun setPostKnown(post: Post, known: Boolean): Unit =
writeLock.withLock {
saveKnownPosts()
}
- protected fun isPostReplyKnown(postReply: PostReply) = readLock.withLock { postReply.id in knownPostReplies }
+ internal fun isPostReplyKnown(postReply: PostReply) = readLock.withLock { postReply.id in knownPostReplies }
override fun setPostReplyKnown(postReply: PostReply): Unit =
writeLock.withLock {