Move storage of known Sones into database.
[Sone.git] / src / main / java / net / pterodactylus / sone / core / Core.java
index 0680d30..90b6e2e 100644 (file)
@@ -158,9 +158,6 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
        /* synchronize access on this on sones. */
        private final Map<LocalSone, SoneRescuer> soneRescuers = new HashMap<LocalSone, SoneRescuer>();
 
-       /** All known Sones. */
-       private final Set<String> knownSones = new HashSet<String>();
-
        /** The post database. */
        private final Database database;
 
@@ -669,9 +666,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                Sone sone = !newSone ? existingSone.get() : database.newSoneBuilder().from(identity).build();
                sone.setLatestEdition(latestEdition);
                if (newSone) {
-                       synchronized (knownSones) {
-                               newSone = !knownSones.contains(sone.getId());
-                       }
+                       newSone = !database.isSoneKnown(sone);
                        sone.setKnown(!newSone);
                        if (newSone) {
                                eventBus.post(new NewSoneFoundEvent(sone));
@@ -939,9 +934,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
        public void markSoneKnown(Sone sone) {
                if (!sone.isKnown()) {
                        sone.setKnown(true);
-                       synchronized (knownSones) {
-                               knownSones.add(sone.getId());
-                       }
+                       database.setSoneKnown(sone);
                        eventBus.post(new MarkSoneKnownEvent(sone));
                        touchConfiguration();
                }
@@ -1283,17 +1276,8 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
                try {
                        preferences.saveTo(configuration);
 
-                       /* save known Sones. */
-                       int soneCounter = 0;
-                       synchronized (knownSones) {
-                               for (String knownSoneId : knownSones) {
-                                       configuration.getStringValue("KnownSone/" + soneCounter++ + "/ID").setValue(knownSoneId);
-                               }
-                               configuration.getStringValue("KnownSone/" + soneCounter + "/ID").setValue(null);
-                       }
-
                        /* save Sone following times. */
-                       soneCounter = 0;
+                       int soneCounter = 0;
                        synchronized (soneFollowingTimes) {
                                for (Entry<String, Long> soneFollowingTime : soneFollowingTimes.entrySet()) {
                                        configuration.getStringValue("SoneFollowingTimes/" + soneCounter + "/Sone").setValue(soneFollowingTime.getKey());
@@ -1326,20 +1310,8 @@ public class Core extends AbstractService implements SoneProvider, PostProvider,
        private void loadConfiguration() {
                new PreferencesLoader(preferences).loadFrom(configuration);
 
-               /* load known Sones. */
-               int soneCounter = 0;
-               while (true) {
-                       String knownSoneId = configuration.getStringValue("KnownSone/" + soneCounter++ + "/ID").getValue(null);
-                       if (knownSoneId == null) {
-                               break;
-                       }
-                       synchronized (knownSones) {
-                               knownSones.add(knownSoneId);
-                       }
-               }
-
                /* load Sone following times. */
-               soneCounter = 0;
+               int soneCounter = 0;
                while (true) {
                        String soneId = configuration.getStringValue("SoneFollowingTimes/" + soneCounter + "/Sone").getValue(null);
                        if (soneId == null) {