2 * Sone - MemoryDatabase.java - Copyright © 2013 David Roden
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 package net.pterodactylus.sone.database.memory;
20 import static com.google.common.base.Optional.fromNullable;
21 import static com.google.common.base.Preconditions.checkNotNull;
22 import static com.google.common.base.Predicates.not;
23 import static com.google.common.collect.FluentIterable.from;
24 import static java.lang.String.format;
25 import static java.util.logging.Level.WARNING;
26 import static net.pterodactylus.sone.data.Reply.TIME_COMPARATOR;
27 import static net.pterodactylus.sone.data.Sone.LOCAL_SONE_FILTER;
28 import static net.pterodactylus.sone.data.Sone.toAllAlbums;
29 import static net.pterodactylus.sone.data.Sone.toAllImages;
31 import java.util.Collection;
32 import java.util.Collections;
33 import java.util.Comparator;
34 import java.util.HashMap;
35 import java.util.HashSet;
36 import java.util.List;
39 import java.util.concurrent.locks.ReadWriteLock;
40 import java.util.concurrent.locks.ReentrantReadWriteLock;
41 import java.util.logging.Level;
42 import java.util.logging.Logger;
44 import net.pterodactylus.sone.core.ConfigurationSoneParser;
45 import net.pterodactylus.sone.core.ConfigurationSoneParser.InvalidAlbumFound;
46 import net.pterodactylus.sone.core.ConfigurationSoneParser.InvalidImageFound;
47 import net.pterodactylus.sone.core.ConfigurationSoneParser.InvalidParentAlbumFound;
48 import net.pterodactylus.sone.core.ConfigurationSoneParser.InvalidPostFound;
49 import net.pterodactylus.sone.core.ConfigurationSoneParser.InvalidPostReplyFound;
50 import net.pterodactylus.sone.data.Album;
51 import net.pterodactylus.sone.data.Client;
52 import net.pterodactylus.sone.data.Image;
53 import net.pterodactylus.sone.data.Post;
54 import net.pterodactylus.sone.data.PostReply;
55 import net.pterodactylus.sone.data.Profile;
56 import net.pterodactylus.sone.data.Profile.Field;
57 import net.pterodactylus.sone.data.Sone;
58 import net.pterodactylus.sone.data.Sone.ShowCustomAvatars;
59 import net.pterodactylus.sone.data.impl.AlbumBuilderImpl;
60 import net.pterodactylus.sone.data.impl.ImageBuilderImpl;
61 import net.pterodactylus.sone.database.AlbumBuilder;
62 import net.pterodactylus.sone.database.Database;
63 import net.pterodactylus.sone.database.DatabaseException;
64 import net.pterodactylus.sone.database.ImageBuilder;
65 import net.pterodactylus.sone.database.PostBuilder;
66 import net.pterodactylus.sone.database.PostDatabase;
67 import net.pterodactylus.sone.database.PostReplyBuilder;
68 import net.pterodactylus.sone.database.SoneBuilder;
69 import net.pterodactylus.sone.database.SoneProvider;
70 import net.pterodactylus.sone.freenet.wot.OwnIdentity;
71 import net.pterodactylus.sone.main.SonePlugin;
72 import net.pterodactylus.sone.utils.Optionals;
73 import net.pterodactylus.util.config.Configuration;
74 import net.pterodactylus.util.config.ConfigurationException;
76 import com.google.common.base.Function;
77 import com.google.common.base.Optional;
78 import com.google.common.base.Predicate;
79 import com.google.common.collect.FluentIterable;
80 import com.google.common.collect.HashMultimap;
81 import com.google.common.collect.Multimap;
82 import com.google.common.collect.SortedSetMultimap;
83 import com.google.common.collect.TreeMultimap;
84 import com.google.common.primitives.Longs;
85 import com.google.common.util.concurrent.AbstractService;
86 import com.google.inject.Inject;
87 import com.google.inject.Singleton;
90 * Memory-based {@link PostDatabase} implementation.
92 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
95 public class MemoryDatabase extends AbstractService implements Database {
97 private static final Logger logger = Logger.getLogger("Sone.Database.Memory");
98 private static final String LATEST_EDITION_PROPERTY = "Sone.LatestEdition";
100 private final ReadWriteLock lock = new ReentrantReadWriteLock();
102 /** The Sone provider. */
103 private final SoneProvider soneProvider;
105 /** The configuration. */
106 private final Configuration configuration;
107 private final ConfigurationLoader configurationLoader;
109 private final Set<String> localSones = new HashSet<String>();
110 private final Map<String, Sone> allSones = new HashMap<String, Sone>();
111 private final Map<String, String> lastInsertFingerprints = new HashMap<String, String>();
113 /** All posts by their ID. */
114 private final Map<String, Post> allPosts = new HashMap<String, Post>();
116 /** All posts by their Sones. */
117 private final Multimap<String, Post> sonePosts = HashMultimap.create();
119 /** Whether posts are known. */
120 private final Set<String> knownPosts = new HashSet<String>();
122 /** All post replies by their ID. */
123 private final Map<String, PostReply> allPostReplies = new HashMap<String, PostReply>();
125 /** Replies sorted by Sone. */
126 private final SortedSetMultimap<String, PostReply> sonePostReplies = TreeMultimap.create(new Comparator<String>() {
129 public int compare(String leftString, String rightString) {
130 return leftString.compareTo(rightString);
134 /** Whether post replies are known. */
135 private final Set<String> knownPostReplies = new HashSet<String>();
137 private final Map<String, Album> allAlbums = new HashMap<String, Album>();
138 private final Multimap<String, Album> soneAlbums = HashMultimap.create();
140 private final Map<String, Image> allImages = new HashMap<String, Image>();
141 private final Multimap<String, Image> soneImages = HashMultimap.create();
143 private final MemoryBookmarkDatabase memoryBookmarkDatabase;
144 private final MemoryFriendDatabase memoryFriendDatabase;
147 * Creates a new memory database.
149 * @param soneProvider
151 * @param configuration
152 * The configuration for loading and saving elements
155 public MemoryDatabase(SoneProvider soneProvider, Configuration configuration) {
156 this.soneProvider = soneProvider;
157 this.configuration = configuration;
158 this.configurationLoader = new ConfigurationLoader(configuration);
159 memoryBookmarkDatabase =
160 new MemoryBookmarkDatabase(this, configurationLoader);
161 memoryFriendDatabase = new MemoryFriendDatabase(configurationLoader);
170 public Sone registerLocalSone(OwnIdentity ownIdentity) {
171 final Sone localSone = loadLocalSone(ownIdentity);
172 localSones.add(ownIdentity.getId());
176 private Sone loadLocalSone(OwnIdentity ownIdentity) {
177 Sone localSone = newSoneBuilder().local().from(ownIdentity).build();
178 localSone.setLatestEdition(
179 Optional.fromNullable(
180 Longs.tryParse(ownIdentity.getProperty(LATEST_EDITION_PROPERTY)))
182 localSone.setClient(new Client("Sone", SonePlugin.VERSION.toString()));
183 localSone.setKnown(true);
189 public void loadSone(Sone sone) {
190 long soneTime = configurationLoader.getLocalSoneTime(sone.getId());
191 if (soneTime == -1) {
196 ConfigurationSoneParser configurationSoneParser = new ConfigurationSoneParser(configuration, sone);
197 Profile profile = configurationSoneParser.parseProfile();
200 Collection<Post> posts;
202 posts = configurationSoneParser.parsePosts(this);
203 } catch (InvalidPostFound ipf) {
204 logger.log(Level.WARNING, "Invalid post found, aborting load!");
209 Collection<PostReply> postReplies;
211 postReplies = configurationSoneParser.parsePostReplies(this);
212 } catch (InvalidPostReplyFound iprf) {
213 logger.log(Level.WARNING, "Invalid reply found, aborting load!");
217 /* load post likes. */
218 Set<String> likedPostIds = configurationSoneParser.parseLikedPostIds();
220 /* load reply likes. */
221 Set<String> likedReplyIds = configurationSoneParser.parseLikedPostReplyIds();
224 List<Album> topLevelAlbums;
226 topLevelAlbums = configurationSoneParser.parseTopLevelAlbums(this);
227 } catch (InvalidAlbumFound iaf) {
228 logger.log(Level.WARNING, "Invalid album found, aborting load!");
230 } catch (InvalidParentAlbumFound ipaf) {
231 logger.log(Level.WARNING,
232 format("Invalid parent album ID: %s", ipaf.getAlbumParentId()));
238 configurationSoneParser.parseImages(this);
239 } catch (InvalidImageFound iif) {
240 logger.log(WARNING, "Invalid image found, aborting load!");
242 } catch (InvalidParentAlbumFound ipaf) {
243 logger.log(Level.WARNING,
244 format("Invalid album image (%s) encountered, aborting load!",
245 ipaf.getAlbumParentId()));
250 String sonePrefix = "Sone/" + sone.getId();
251 String avatarId = configuration.getStringValue(sonePrefix + "/Profile/Avatar").getValue(null);
252 if (avatarId != null) {
253 final Map<String, Image> images = configurationSoneParser.getImages();
254 profile.setAvatar(images.get(avatarId));
258 sone.getOptions().setAutoFollow(configuration.getBooleanValue(sonePrefix + "/Options/AutoFollow").getValue(null));
259 sone.getOptions().setSoneInsertNotificationEnabled(configuration.getBooleanValue(sonePrefix + "/Options/EnableSoneInsertNotifications").getValue(null));
260 sone.getOptions().setShowNewSoneNotifications(configuration.getBooleanValue(sonePrefix + "/Options/ShowNotification/NewSones").getValue(null));
261 sone.getOptions().setShowNewPostNotifications(configuration.getBooleanValue(sonePrefix + "/Options/ShowNotification/NewPosts").getValue(null));
262 sone.getOptions().setShowNewReplyNotifications(configuration.getBooleanValue(sonePrefix + "/Options/ShowNotification/NewReplies").getValue(null));
263 sone.getOptions().setShowCustomAvatars(ShowCustomAvatars.valueOf(
264 configuration.getStringValue(sonePrefix + "/Options/ShowCustomAvatars")
265 .getValue(ShowCustomAvatars.NEVER.name())));
267 /* if we’re still here, Sone was loaded successfully. */
268 lock.writeLock().lock();
270 sone.setTime(soneTime);
271 sone.setProfile(profile);
272 sone.setLikePostIds(likedPostIds);
273 sone.setLikeReplyIds(likedReplyIds);
275 String lastInsertFingerprint = configurationLoader.getLastInsertFingerprint(sone.getId());
276 lastInsertFingerprints.put(sone.getId(), lastInsertFingerprint);
278 allSones.put(sone.getId(), sone);
279 storePosts(sone.getId(), posts);
280 storePostReplies(sone.getId(), postReplies);
281 storeAlbums(sone.getId(), topLevelAlbums);
282 storeImages(sone.getId(), from(topLevelAlbums).transformAndConcat(Album.FLATTENER).transformAndConcat(Album.IMAGES).toList());
284 lock.writeLock().unlock();
286 for (Post post : posts) {
289 for (PostReply reply : postReplies) {
290 reply.setKnown(true);
293 logger.info(String.format("Sone loaded successfully: %s", sone));
297 public String getLastInsertFingerprint(Sone sone) {
298 lock.readLock().lock();
300 if (!lastInsertFingerprints.containsKey(sone.getId())) {
303 return lastInsertFingerprints.get(sone.getId());
305 lock.readLock().unlock();
310 public void setLastInsertFingerprint(Sone sone, String lastInsertFingerprint) {
311 lock.writeLock().lock();
313 lastInsertFingerprints.put(sone.getId(), lastInsertFingerprint);
315 lock.writeLock().unlock();
320 * Saves the database.
322 * @throws DatabaseException
323 * if an error occurs while saving
326 public void save() throws DatabaseException {
327 lock.writeLock().lock();
330 saveKnownPostReplies();
331 for (Sone localSone : from(localSones).transform(soneLoader()).transform(Optionals.<Sone>get())) {
335 lock.writeLock().unlock();
339 private synchronized void saveSone(Sone sone) {
340 logger.log(Level.INFO, String.format("Saving Sone: %s", sone));
342 /* save Sone into configuration. */
343 String sonePrefix = "Sone/" + sone.getId();
344 configuration.getLongValue(sonePrefix + "/Time").setValue(sone.getTime());
345 configuration.getStringValue(sonePrefix + "/LastInsertFingerprint").setValue(lastInsertFingerprints.get(sone.getId()));
348 Profile profile = sone.getProfile();
349 configuration.getStringValue(sonePrefix + "/Profile/FirstName").setValue(profile.getFirstName());
350 configuration.getStringValue(sonePrefix + "/Profile/MiddleName").setValue(profile.getMiddleName());
351 configuration.getStringValue(sonePrefix + "/Profile/LastName").setValue(profile.getLastName());
352 configuration.getIntValue(sonePrefix + "/Profile/BirthDay").setValue(profile.getBirthDay());
353 configuration.getIntValue(sonePrefix + "/Profile/BirthMonth").setValue(profile.getBirthMonth());
354 configuration.getIntValue(sonePrefix + "/Profile/BirthYear").setValue(profile.getBirthYear());
355 configuration.getStringValue(sonePrefix + "/Profile/Avatar").setValue(profile.getAvatar());
357 /* save profile fields. */
358 int fieldCounter = 0;
359 for (Field profileField : profile.getFields()) {
360 String fieldPrefix = sonePrefix + "/Profile/Fields/" + fieldCounter++;
361 configuration.getStringValue(fieldPrefix + "/Name").setValue(profileField.getName());
362 configuration.getStringValue(fieldPrefix + "/Value").setValue(profileField.getValue());
364 configuration.getStringValue(sonePrefix + "/Profile/Fields/" + fieldCounter + "/Name").setValue(null);
368 for (Post post : sone.getPosts()) {
369 String postPrefix = sonePrefix + "/Posts/" + postCounter++;
370 configuration.getStringValue(postPrefix + "/ID").setValue(post.getId());
371 configuration.getStringValue(postPrefix + "/Recipient").setValue(post.getRecipientId().orNull());
372 configuration.getLongValue(postPrefix + "/Time").setValue(post.getTime());
373 configuration.getStringValue(postPrefix + "/Text").setValue(post.getText());
375 configuration.getStringValue(sonePrefix + "/Posts/" + postCounter + "/ID").setValue(null);
378 int replyCounter = 0;
379 for (PostReply reply : sone.getReplies()) {
380 String replyPrefix = sonePrefix + "/Replies/" + replyCounter++;
381 configuration.getStringValue(replyPrefix + "/ID").setValue(reply.getId());
382 configuration.getStringValue(replyPrefix + "/Post/ID").setValue(reply.getPostId());
383 configuration.getLongValue(replyPrefix + "/Time").setValue(reply.getTime());
384 configuration.getStringValue(replyPrefix + "/Text").setValue(reply.getText());
386 configuration.getStringValue(sonePrefix + "/Replies/" + replyCounter + "/ID").setValue(null);
388 /* save post likes. */
389 int postLikeCounter = 0;
390 for (String postId : sone.getLikedPostIds()) {
391 configuration.getStringValue(sonePrefix + "/Likes/Post/" + postLikeCounter++ + "/ID").setValue(postId);
393 configuration.getStringValue(sonePrefix + "/Likes/Post/" + postLikeCounter + "/ID").setValue(null);
395 /* save reply likes. */
396 int replyLikeCounter = 0;
397 for (String replyId : sone.getLikedReplyIds()) {
398 configuration.getStringValue(sonePrefix + "/Likes/Reply/" + replyLikeCounter++ + "/ID").setValue(replyId);
400 configuration.getStringValue(sonePrefix + "/Likes/Reply/" + replyLikeCounter + "/ID").setValue(null);
402 /* save albums. first, collect in a flat structure, top-level first. */
403 List<Album> albums = FluentIterable.from(sone.getRootAlbum().getAlbums()).transformAndConcat(Album.FLATTENER).toList();
405 int albumCounter = 0;
406 for (Album album : albums) {
407 String albumPrefix = sonePrefix + "/Albums/" + albumCounter++;
408 configuration.getStringValue(albumPrefix + "/ID").setValue(album.getId());
409 configuration.getStringValue(albumPrefix + "/Title").setValue(album.getTitle());
410 configuration.getStringValue(albumPrefix + "/Description").setValue(album.getDescription());
411 configuration.getStringValue(albumPrefix + "/Parent").setValue(album.getParent().equals(sone.getRootAlbum()) ? null : album.getParent().getId());
412 configuration.getStringValue(albumPrefix + "/AlbumImage").setValue(album.getAlbumImage() == null ? null : album.getAlbumImage().getId());
414 configuration.getStringValue(sonePrefix + "/Albums/" + albumCounter + "/ID").setValue(null);
417 int imageCounter = 0;
418 for (Album album : albums) {
419 for (Image image : album.getImages()) {
420 if (!image.isInserted()) {
423 String imagePrefix = sonePrefix + "/Images/" + imageCounter++;
424 configuration.getStringValue(imagePrefix + "/ID").setValue(image.getId());
425 configuration.getStringValue(imagePrefix + "/Album").setValue(album.getId());
426 configuration.getStringValue(imagePrefix + "/Key").setValue(image.getKey());
427 configuration.getStringValue(imagePrefix + "/Title").setValue(image.getTitle());
428 configuration.getStringValue(imagePrefix + "/Description").setValue(image.getDescription());
429 configuration.getLongValue(imagePrefix + "/CreationTime").setValue(image.getCreationTime());
430 configuration.getIntValue(imagePrefix + "/Width").setValue(image.getWidth());
431 configuration.getIntValue(imagePrefix + "/Height").setValue(image.getHeight());
434 configuration.getStringValue(sonePrefix + "/Images/" + imageCounter + "/ID").setValue(null);
437 configuration.getBooleanValue(sonePrefix + "/Options/AutoFollow").setValue(sone.getOptions().isAutoFollow());
438 configuration.getBooleanValue(sonePrefix + "/Options/EnableSoneInsertNotifications").setValue(sone.getOptions().isSoneInsertNotificationEnabled());
439 configuration.getBooleanValue(sonePrefix + "/Options/ShowNotification/NewSones").setValue(sone.getOptions().isShowNewSoneNotifications());
440 configuration.getBooleanValue(sonePrefix + "/Options/ShowNotification/NewPosts").setValue(sone.getOptions().isShowNewPostNotifications());
441 configuration.getBooleanValue(sonePrefix + "/Options/ShowNotification/NewReplies").setValue(sone.getOptions().isShowNewReplyNotifications());
442 configuration.getStringValue(sonePrefix + "/Options/ShowCustomAvatars").setValue(sone.getOptions().getShowCustomAvatars().name());
444 configuration.save();
446 logger.log(Level.INFO, String.format("Sone %s saved.", sone));
447 } catch (ConfigurationException ce1) {
448 logger.log(Level.WARNING, String.format("Could not save Sone: %s", sone), ce1);
456 /** {@inheritDocs} */
458 protected void doStart() {
459 memoryBookmarkDatabase.start();
461 loadKnownPostReplies();
465 /** {@inheritDocs} */
467 protected void doStop() {
469 memoryBookmarkDatabase.stop();
472 } catch (DatabaseException de1) {
478 public SoneBuilder newSoneBuilder() {
479 return new MemorySoneBuilder(this);
483 public void storeSone(Sone sone) {
484 lock.writeLock().lock();
488 allSones.put(sone.getId(), sone);
489 storePosts(sone.getId(), sone.getPosts());
490 storePostReplies(sone.getId(), sone.getReplies());
491 storeAlbums(sone.getId(), toAllAlbums.apply(sone));
492 storeImages(sone.getId(), toAllImages.apply(sone));
494 lock.writeLock().unlock();
498 private void storePosts(String soneId, Collection<Post> posts) {
499 sonePosts.putAll(soneId, posts);
500 for (Post post : posts) {
501 allPosts.put(post.getId(), post);
505 private void storePostReplies(String soneId, Collection<PostReply> postReplies) {
506 sonePostReplies.putAll(soneId, postReplies);
507 for (PostReply postReply : postReplies) {
508 allPostReplies.put(postReply.getId(), postReply);
512 private void storeAlbums(String soneId, Collection<Album> albums) {
513 soneAlbums.putAll(soneId, albums);
514 for (Album album : albums) {
515 allAlbums.put(album.getId(), album);
519 private void storeImages(String soneId, Collection<Image> images) {
520 soneImages.putAll(soneId, images);
521 for (Image image : images) {
522 allImages.put(image.getId(), image);
527 public void removeSone(Sone sone) {
528 lock.writeLock().lock();
530 allSones.remove(sone.getId());
531 Collection<Post> removedPosts = sonePosts.removeAll(sone.getId());
532 for (Post removedPost : removedPosts) {
533 allPosts.remove(removedPost.getId());
535 Collection<PostReply> removedPostReplies =
536 sonePostReplies.removeAll(sone.getId());
537 for (PostReply removedPostReply : removedPostReplies) {
538 allPostReplies.remove(removedPostReply.getId());
540 Collection<Album> removedAlbums =
541 soneAlbums.removeAll(sone.getId());
542 for (Album removedAlbum : removedAlbums) {
543 allAlbums.remove(removedAlbum.getId());
545 Collection<Image> removedImages =
546 soneImages.removeAll(sone.getId());
547 for (Image removedImage : removedImages) {
548 allImages.remove(removedImage.getId());
551 lock.writeLock().unlock();
556 public Function<String, Optional<Sone>> soneLoader() {
557 return new Function<String, Optional<Sone>>() {
559 public Optional<Sone> apply(String soneId) {
560 return getSone(soneId);
566 public Optional<Sone> getSone(String soneId) {
567 lock.readLock().lock();
569 return fromNullable(allSones.get(soneId));
571 lock.readLock().unlock();
576 public Collection<Sone> getSones() {
577 lock.readLock().lock();
579 return new HashSet<Sone>(allSones.values());
581 lock.readLock().unlock();
586 public Collection<Sone> getLocalSones() {
587 lock.readLock().lock();
589 return from(allSones.values()).filter(LOCAL_SONE_FILTER).toSet();
591 lock.readLock().unlock();
596 public Collection<Sone> getRemoteSones() {
597 lock.readLock().lock();
599 return from(allSones.values())
600 .filter(not(LOCAL_SONE_FILTER)) .toSet();
602 lock.readLock().unlock();
607 public Collection<String> getFriends(Sone localSone) {
608 if (!localSone.isLocal()) {
609 return Collections.emptySet();
611 return memoryFriendDatabase.getFriends(localSone.getId());
615 public boolean isFriend(Sone localSone, String friendSoneId) {
616 if (!localSone.isLocal()) {
619 return memoryFriendDatabase.isFriend(localSone.getId(), friendSoneId);
623 public void addFriend(Sone localSone, String friendSoneId) {
624 if (!localSone.isLocal()) {
627 memoryFriendDatabase.addFriend(localSone.getId(), friendSoneId);
631 public void removeFriend(Sone localSone, String friendSoneId) {
632 if (!localSone.isLocal()) {
635 memoryFriendDatabase.removeFriend(localSone.getId(), friendSoneId);
639 // POSTPROVIDER METHODS
642 /** {@inheritDocs} */
644 public Optional<Post> getPost(String postId) {
645 lock.readLock().lock();
647 return fromNullable(allPosts.get(postId));
649 lock.readLock().unlock();
653 /** {@inheritDocs} */
655 public Collection<Post> getPosts(String soneId) {
656 return new HashSet<Post>(getPostsFrom(soneId));
659 /** {@inheritDocs} */
661 public Collection<Post> getDirectedPosts(final String recipientId) {
662 lock.readLock().lock();
664 return from(sonePosts.values()).filter(new Predicate<Post>() {
666 public boolean apply(Post post) {
667 return post.getRecipientId().asSet().contains(recipientId);
671 lock.readLock().unlock();
676 // POSTBUILDERFACTORY METHODS
679 /** {@inheritDocs} */
681 public PostBuilder newPostBuilder() {
682 return new MemoryPostBuilder(this, soneProvider);
689 /** {@inheritDocs} */
691 public void storePost(Post post) {
692 checkNotNull(post, "post must not be null");
693 lock.writeLock().lock();
695 allPosts.put(post.getId(), post);
696 getPostsFrom(post.getSone().getId()).add(post);
698 lock.writeLock().unlock();
702 /** {@inheritDocs} */
704 public void removePost(Post post) {
705 checkNotNull(post, "post must not be null");
706 lock.writeLock().lock();
708 allPosts.remove(post.getId());
709 getPostsFrom(post.getSone().getId()).remove(post);
710 post.getSone().removePost(post);
712 lock.writeLock().unlock();
717 // POSTREPLYPROVIDER METHODS
720 /** {@inheritDocs} */
722 public Optional<PostReply> getPostReply(String id) {
723 lock.readLock().lock();
725 return fromNullable(allPostReplies.get(id));
727 lock.readLock().unlock();
731 /** {@inheritDocs} */
733 public List<PostReply> getReplies(final String postId) {
734 lock.readLock().lock();
736 return from(allPostReplies.values())
737 .filter(new Predicate<PostReply>() {
739 public boolean apply(PostReply postReply) {
740 return postReply.getPostId().equals(postId);
742 }).toSortedList(TIME_COMPARATOR);
744 lock.readLock().unlock();
749 // POSTREPLYBUILDERFACTORY METHODS
752 /** {@inheritDocs} */
754 public PostReplyBuilder newPostReplyBuilder() {
755 return new MemoryPostReplyBuilder(this, soneProvider);
759 // POSTREPLYSTORE METHODS
762 /** {@inheritDocs} */
764 public void storePostReply(PostReply postReply) {
765 lock.writeLock().lock();
767 allPostReplies.put(postReply.getId(), postReply);
769 lock.writeLock().unlock();
773 /** {@inheritDocs} */
775 public void removePostReply(PostReply postReply) {
776 lock.writeLock().lock();
778 allPostReplies.remove(postReply.getId());
780 lock.writeLock().unlock();
785 // ALBUMPROVDER METHODS
789 public Optional<Album> getAlbum(String albumId) {
790 lock.readLock().lock();
792 return fromNullable(allAlbums.get(albumId));
794 lock.readLock().unlock();
799 // ALBUMBUILDERFACTORY METHODS
803 public AlbumBuilder newAlbumBuilder() {
804 return new AlbumBuilderImpl();
808 // ALBUMSTORE METHODS
812 public void storeAlbum(Album album) {
813 lock.writeLock().lock();
815 allAlbums.put(album.getId(), album);
816 soneAlbums.put(album.getSone().getId(), album);
818 lock.writeLock().unlock();
823 public void removeAlbum(Album album) {
824 lock.writeLock().lock();
826 allAlbums.remove(album.getId());
827 soneAlbums.remove(album.getSone().getId(), album);
829 lock.writeLock().unlock();
834 // IMAGEPROVIDER METHODS
838 public Optional<Image> getImage(String imageId) {
839 lock.readLock().lock();
841 return fromNullable(allImages.get(imageId));
843 lock.readLock().unlock();
848 // IMAGEBUILDERFACTORY METHODS
852 public ImageBuilder newImageBuilder() {
853 return new ImageBuilderImpl();
857 // IMAGESTORE METHODS
861 public void storeImage(Image image) {
862 lock.writeLock().lock();
864 allImages.put(image.getId(), image);
865 soneImages.put(image.getSone().getId(), image);
867 lock.writeLock().unlock();
872 public void removeImage(Image image) {
873 lock.writeLock().lock();
875 allImages.remove(image.getId());
876 soneImages.remove(image.getSone().getId(), image);
878 lock.writeLock().unlock();
883 public void bookmarkPost(Post post) {
884 memoryBookmarkDatabase.bookmarkPost(post);
888 public void unbookmarkPost(Post post) {
889 memoryBookmarkDatabase.unbookmarkPost(post);
893 public boolean isPostBookmarked(Post post) {
894 return memoryBookmarkDatabase.isPostBookmarked(post);
898 public Set<Post> getBookmarkedPosts() {
899 return memoryBookmarkDatabase.getBookmarkedPosts();
903 // PACKAGE-PRIVATE METHODS
907 * Returns whether the given post is known.
911 * @return {@code true} if the post is known, {@code false} otherwise
913 boolean isPostKnown(Post post) {
914 lock.readLock().lock();
916 return knownPosts.contains(post.getId());
918 lock.readLock().unlock();
923 * Sets whether the given post is known.
928 * {@code true} if the post is known, {@code false} otherwise
930 void setPostKnown(Post post, boolean known) {
931 lock.writeLock().lock();
934 knownPosts.add(post.getId());
936 knownPosts.remove(post.getId());
939 lock.writeLock().unlock();
944 * Returns whether the given post reply is known.
948 * @return {@code true} if the given post reply is known, {@code false}
951 boolean isPostReplyKnown(PostReply postReply) {
952 lock.readLock().lock();
954 return knownPostReplies.contains(postReply.getId());
956 lock.readLock().unlock();
961 * Sets whether the given post reply is known.
966 * {@code true} if the post reply is known, {@code false} otherwise
968 void setPostReplyKnown(PostReply postReply, boolean known) {
969 lock.writeLock().lock();
972 knownPostReplies.add(postReply.getId());
974 knownPostReplies.remove(postReply.getId());
977 lock.writeLock().unlock();
986 * Gets all posts for the given Sone, creating a new collection if there is
990 * The ID of the Sone to get the posts for
993 private Collection<Post> getPostsFrom(String soneId) {
994 lock.readLock().lock();
996 return sonePosts.get(soneId);
998 lock.readLock().unlock();
1002 /** Loads the known posts. */
1003 private void loadKnownPosts() {
1004 Set<String> knownPosts = configurationLoader.loadKnownPosts();
1005 lock.writeLock().lock();
1007 this.knownPosts.clear();
1008 this.knownPosts.addAll(knownPosts);
1010 lock.writeLock().unlock();
1015 * Saves the known posts to the configuration.
1017 * @throws DatabaseException
1018 * if a configuration error occurs
1020 private void saveKnownPosts() throws DatabaseException {
1021 lock.readLock().lock();
1023 int postCounter = 0;
1024 for (String knownPostId : knownPosts) {
1025 configuration.getStringValue("KnownPosts/" + postCounter++ + "/ID").setValue(
1028 configuration.getStringValue("KnownPosts/" + postCounter + "/ID").setValue(null);
1029 } catch (ConfigurationException ce1) {
1030 throw new DatabaseException("Could not save database.", ce1);
1032 lock.readLock().unlock();
1036 /** Loads the known post replies. */
1037 private void loadKnownPostReplies() {
1038 Set<String> knownPostReplies = configurationLoader.loadKnownPostReplies();
1039 lock.writeLock().lock();
1041 this.knownPostReplies.clear();
1042 this.knownPostReplies.addAll(knownPostReplies);
1044 lock.writeLock().unlock();
1049 * Saves the known post replies to the configuration.
1051 * @throws DatabaseException
1052 * if a configuration error occurs
1054 private void saveKnownPostReplies() throws DatabaseException {
1055 lock.readLock().lock();
1057 int replyCounter = 0;
1058 for (String knownReplyId : knownPostReplies) {
1059 configuration.getStringValue("KnownReplies/" + replyCounter++ + "/ID").setValue(
1062 configuration.getStringValue("KnownReplies/" + replyCounter + "/ID").setValue(null);
1063 } catch (ConfigurationException ce1) {
1064 throw new DatabaseException("Could not save database.", ce1);
1066 lock.readLock().unlock();