package net.pterodactylus.sone.database.memory;
+import static java.util.logging.Level.WARNING;
+
+import java.util.Collection;
+import java.util.HashMap;
import java.util.HashSet;
+import java.util.Map;
+import java.util.Map.Entry;
import java.util.Set;
+import java.util.logging.Logger;
import net.pterodactylus.util.config.Configuration;
+import net.pterodactylus.util.config.ConfigurationException;
+
+import com.google.common.base.Optional;
/**
* Helper class for interacting with a {@link Configuration}.
*/
public class ConfigurationLoader {
+ private static final Logger logger =
+ Logger.getLogger("Sone.Database.Memory.Configuration");
private final Configuration configuration;
public ConfigurationLoader(Configuration configuration) {
this.configuration = configuration;
}
- public synchronized Set<String> loadKnownPosts() {
- Set<String> knownPosts = new HashSet<String>();
- int postCounter = 0;
+ public synchronized Set<String> loadFriends(String localSoneId) {
+ return loadIds("Sone/" + localSoneId + "/Friends");
+ }
+
+ public void saveFriends(String soneId, Collection<String> friends) {
+ saveIds("Sone/" + soneId + "/Friends", friends);
+ }
+
+ public synchronized Set<String> loadKnownSones() {
+ return loadIds("KnownSones");
+ }
+
+ public synchronized void saveKnownSones(Set<String> knownSones) {
+ saveIds("KnownSones", knownSones);
+ }
+
+ public synchronized Map<String, Long> loadSoneFollowingTimes() {
+ Map<String, Long> soneFollowingTimes = new HashMap<String, Long>();
+ int counter = 0;
while (true) {
- String knownPostId = configuration
- .getStringValue("KnownPosts/" + postCounter++ + "/ID")
- .getValue(null);
- if (knownPostId == null) {
+ String soneId = configuration.getStringValue("SoneFollowingTimes/" + counter + "/Sone").getValue(null);
+ if (soneId == null) {
break;
}
- knownPosts.add(knownPostId);
+ long followingTime = configuration.getLongValue("SoneFollowingTimes/" + counter + "/Time").getValue(Long.MAX_VALUE);
+ soneFollowingTimes.put(soneId, followingTime);
+ counter++;
}
- return knownPosts;
+ return soneFollowingTimes;
+ }
+
+ public synchronized void saveSoneFollowingTimes(Map<String, Long> soneFollowingTimes) {
+ try {
+ int counter = 0;
+ for (Entry<String, Long> soneFollowingTime : soneFollowingTimes.entrySet()) {
+ configuration.getStringValue("SoneFollowingTimes/" + counter + "/Sone")
+ .setValue(soneFollowingTime.getKey());
+ configuration.getLongValue("SoneFollowingTimes/" + counter + "/Time")
+ .setValue(soneFollowingTime.getValue());
+ counter++;
+ }
+ configuration.getStringValue("SoneFollowingTimes/" + counter + "/Sone").setValue(null);
+ } catch (ConfigurationException ce1) {
+ logger.log(WARNING, "Could not save Sone following times!", ce1);
+ }
+ }
+
+ public synchronized Set<String> loadKnownPosts() {
+ return loadIds("KnownPosts");
+ }
+
+ public synchronized void saveKnownPosts(Set<String> knownPosts) {
+ saveIds("KnownPosts", knownPosts);
}
public synchronized Set<String> loadKnownPostReplies() {
- Set<String> knownPostReplies = new HashSet<String>();
- int replyCounter = 0;
+ return loadIds("KnownReplies");
+ }
+
+ public synchronized Set<String> loadBookmarkedPosts() {
+ return loadIds("Bookmarks/Post");
+ }
+
+ private Set<String> loadIds(String prefix) {
+ Set<String> ids = new HashSet<String>();
+ int idCounter = 0;
while (true) {
- String knownReplyId = configuration
- .getStringValue("KnownReplies/" + replyCounter++ + "/ID")
+ String id = configuration
+ .getStringValue(prefix + "/" + idCounter++ + "/ID")
.getValue(null);
- if (knownReplyId == null) {
+ if (id == null) {
break;
}
- knownPostReplies.add(knownReplyId);
+ ids.add(id);
}
- return knownPostReplies;
+ return ids;
+ }
+
+ public synchronized void saveBookmarkedPosts(
+ Set<String> bookmarkedPosts) {
+ saveIds("Bookmarks/Post", bookmarkedPosts);
+ }
+
+ private void saveIds(String prefix, Collection<String> ids) {
+ try {
+ int idCounter = 0;
+ for (String id : ids) {
+ configuration
+ .getStringValue(prefix + "/" + idCounter++ + "/ID")
+ .setValue(id);
+ }
+ configuration
+ .getStringValue(prefix + "/" + idCounter + "/ID")
+ .setValue(null);
+ } catch (ConfigurationException ce1) {
+ logger.log(WARNING, "Could not save bookmarked posts!", ce1);
+ }
+ }
+
+ public long getLocalSoneTime(String localSoneId) {
+ Long time = configuration.getLongValue("Sone/" + localSoneId + "/Time").getValue(null);
+ return Optional.fromNullable(time).or(-1L);
+ }
+
+ public String getLastInsertFingerprint(String localSoneId) {
+ return configuration.getStringValue("Sone/" + localSoneId + "/LastInsertFingerprint").getValue("");
}
}