import net.pterodactylus.util.config.Configuration
import net.pterodactylus.util.config.ConfigurationException
import java.util.concurrent.locks.ReentrantReadWriteLock
import net.pterodactylus.util.config.Configuration
import net.pterodactylus.util.config.ConfigurationException
import java.util.concurrent.locks.ReentrantReadWriteLock
class MemoryDatabase @Inject constructor(private val configuration: Configuration) : AbstractService(), Database {
private val lock = ReentrantReadWriteLock()
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 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 allPosts = mutableMapOf<String, MemoryPost.Shell>()
+ private val sonePosts: Multimap<String, MemoryPost.Shell> = HashMultimap.create<String, MemoryPost.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>()
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>()
- sonePosts.putAll(sone.id, sone.posts)
- for (post in sone.posts) {
+ sonePosts.putAll(sone.id, sone.posts.map(Post::toShell))
+ for (post in sone.posts.map(Post::toShell)) {
allPosts[post.id] = post
}
sonePostReplies.putAll(sone.id, sone.replies)
for (postReply in sone.replies) {
allPosts[post.id] = post
}
sonePostReplies.putAll(sone.id, sone.replies)
for (postReply in sone.replies) {
}
sone.allAlbums.let { albums ->
soneAlbums.putAll(sone.id, albums)
}
sone.allAlbums.let { albums ->
soneAlbums.putAll(sone.id, albums)
override fun getFollowingTime(friendSoneId: String) =
memoryFriendDatabase.getFollowingTime(friendSoneId)
override fun getFollowingTime(friendSoneId: String) =
memoryFriendDatabase.getFollowingTime(friendSoneId)
override fun storePost(post: Post) {
checkNotNull(post, "post must not be null")
writeLock.withLock {
override fun storePost(post: Post) {
checkNotNull(post, "post must not be null")
writeLock.withLock {
- allPosts[post.id] = post
- sonePosts[post.sone.id].add(post)
+ post.toShell().also { shell ->
+ allPosts[post.id] = shell
+ sonePosts[post.sone.id].add(shell)
+ }