public void storeSone(Sone sone) {
lock.writeLock().lock();
try {
+ removeSone(sone);
+
+ allSones.put(sone.getId(), sone);
+ sonePosts.putAll(sone.getId(), sone.getPosts());
+ for (Post post : sone.getPosts()) {
+ allPosts.put(post.getId(), post);
+ }
+ sonePostReplies.putAll(sone.getId(), sone.getReplies());
+ for (PostReply postReply : sone.getReplies()) {
+ allPostReplies.put(postReply.getId(), postReply);
+ }
+ soneAlbums.putAll(sone.getId(), toAllAlbums.apply(sone));
+ for (Album album : toAllAlbums.apply(sone)) {
+ allAlbums.put(album.getId(), album);
+ }
+ soneImages.putAll(sone.getId(), toAllImages.apply(sone));
+ for (Image image : toAllImages.apply(sone)) {
+ allImages.put(image.getId(), image);
+ }
+ } finally {
+ lock.writeLock().unlock();
+ }
+ }
+
+ @Override
+ public void removeSone(Sone sone) {
+ lock.writeLock().lock();
+ try {
+ allSones.remove(sone.getId());
Collection<Post> removedPosts = sonePosts.removeAll(sone.getId());
for (Post removedPost : removedPosts) {
allPosts.remove(removedPost.getId());
for (Image removedImage : removedImages) {
allImages.remove(removedImage.getId());
}
-
- allSones.put(sone.getId(), sone);
- sonePosts.putAll(sone.getId(), sone.getPosts());
- for (Post post : sone.getPosts()) {
- allPosts.put(post.getId(), post);
- }
- sonePostReplies.putAll(sone.getId(), sone.getReplies());
- for (PostReply postReply : sone.getReplies()) {
- allPostReplies.put(postReply.getId(), postReply);
- }
- soneAlbums.putAll(sone.getId(), toAllAlbums.apply(sone));
- for (Album album : toAllAlbums.apply(sone)) {
- allAlbums.put(album.getId(), album);
- }
- soneImages.putAll(sone.getId(), toAllImages.apply(sone));
- for (Image image : toAllImages.apply(sone)) {
- allImages.put(image.getId(), image);
- }
} finally {
lock.writeLock().unlock();
}
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.contains;
+import static org.hamcrest.Matchers.empty;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
}
@Test
+ public void storedAndRemovedSoneIsNotAvailable() {
+ storedSoneIsMadeAvailable();
+ memoryDatabase.removeSone(sone);
+ assertThat(memoryDatabase.getSones(), empty());
+ }
+
+ @Test
public void postRecipientsAreDetectedCorrectly() {
Post postWithRecipient = createPost(of(RECIPIENT_ID));
memoryDatabase.storePost(postWithRecipient);