X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fcore%2FCore.java;h=a2e54a67b5b20c6a6ad0cc0f56292a0b25f13a03;hb=11a7eb67daf4f2ca120c476123bb676b9b304e73;hp=3ca52713668d0fb69b499ca4f4ab29cc46aa923c;hpb=b72418365155d4c0dd89215c5aeff27feeae68dd;p=Sone.git diff --git a/src/main/java/net/pterodactylus/sone/core/Core.java b/src/main/java/net/pterodactylus/sone/core/Core.java index 3ca5271..a2e54a6 100644 --- a/src/main/java/net/pterodactylus/sone/core/Core.java +++ b/src/main/java/net/pterodactylus/sone/core/Core.java @@ -172,10 +172,6 @@ public class Core extends AbstractService implements SoneProvider { /* synchronize access on this on sones. */ private final Map soneRescuers = new HashMap(); - /** All Sones. */ - /* synchronize access on this on itself. */ - private final Map sones = new HashMap(); - /** All known Sones. */ private final Set knownSones = new HashSet(); @@ -295,7 +291,7 @@ public class Core extends AbstractService implements SoneProvider { public SoneRescuer getSoneRescuer(Sone sone) { checkNotNull(sone, "sone must not be null"); checkArgument(sone.isLocal(), "sone must be local"); - synchronized (sones) { + synchronized (soneRescuers) { SoneRescuer soneRescuer = soneRescuers.get(sone); if (soneRescuer == null) { soneRescuer = new SoneRescuer(this, soneDownloader, sone); @@ -321,9 +317,7 @@ public class Core extends AbstractService implements SoneProvider { @Override public Collection getSones() { - synchronized (sones) { - return ImmutableSet.copyOf(sones.values()); - } + return database.getSones(); } @Override @@ -333,22 +327,12 @@ public class Core extends AbstractService implements SoneProvider { @Override public Optional getSone(String id) { - synchronized (sones) { - return Optional.fromNullable(sones.get(id)); - } + return database.getSone(id); } @Override public Collection getLocalSones() { - synchronized (sones) { - return from(sones.values()).filter(new Predicate() { - - @Override - public boolean apply(Sone sone) { - return sone.isLocal(); - } - }).toSet(); - } + return database.getLocalSones(); } /** @@ -364,15 +348,7 @@ public class Core extends AbstractService implements SoneProvider { @Override public Collection getRemoteSones() { - synchronized (sones) { - return from(sones.values()).filter(new Predicate() { - - @Override - public boolean apply(Sone sone) { - return !sone.isLocal(); - } - }).toSet(); - } + return database.getRemoteSones(); } /** @@ -413,40 +389,6 @@ public class Core extends AbstractService implements SoneProvider { } /** - * Returns all Sones that have liked the given post. - * - * @param post - * The post to get the liking Sones for - * @return The Sones that like the given post - */ - public Set getLikes(Post post) { - Set sones = new HashSet(); - for (Sone sone : getSones()) { - if (sone.getLikedPostIds().contains(post.getId())) { - sones.add(sone); - } - } - return sones; - } - - /** - * Returns all Sones that have liked the given reply. - * - * @param reply - * The reply to get the liking Sones for - * @return The Sones that like the given reply - */ - public Set getLikes(PostReply reply) { - Set sones = new HashSet(); - for (Sone sone : getSones()) { - if (sone.getLikedReplyIds().contains(reply.getId())) { - sones.add(sone); - } - } - return sones; - } - - /** * Returns whether the given post is bookmarked. * * @param post @@ -559,13 +501,13 @@ public class Core extends AbstractService implements SoneProvider { return null; } logger.info(String.format("Adding Sone from OwnIdentity: %s", ownIdentity)); - synchronized (sones) { + synchronized (soneRescuers) { final Sone sone; sone = database.newSoneBuilder().by(ownIdentity.getId()).local().using(new Client("Sone", SonePlugin.VERSION.toString())).build(Optional.absent()); sone.modify().setLatestEdition(Numbers.safeParseLong(ownIdentity.getProperty("Sone.LatestEdition"), (long) 0)).update(); sone.setKnown(true); /* TODO - load posts ’n stuff */ - sones.put(ownIdentity.getId(), sone); + database.storeSone(sone); final SoneInserter soneInserter = new SoneInserter(this, eventBus, freenetInterface, sone); soneInserters.put(sone, soneInserter); sone.setStatus(SoneStatus.idle); @@ -612,13 +554,13 @@ public class Core extends AbstractService implements SoneProvider { logger.log(Level.WARNING, "Given Identity is null!"); return null; } - synchronized (sones) { + synchronized (soneRescuers) { Optional existingSone = database.getSone(identity.getId()); if (existingSone.isPresent() && existingSone.get().isLocal()) { return existingSone.get(); } boolean newSone = !existingSone.isPresent(); - final Sone sone = newSone ? database.newSoneBuilder().by(identity.getId()).build(Optional.absent()) : existingSone.get(); + final Sone sone = newSone ? database.newSoneBuilder().by(identity.getId()).using(new Client("Sone", SonePlugin.VERSION.toString())).build(Optional.absent()) : existingSone.get(); sone.modify().setLatestEdition(Numbers.safeParseLong(identity.getProperty("Sone.LatestEdition"), (long) 0)).update(); if (newSone) { synchronized (knownSones) { @@ -855,7 +797,7 @@ public class Core extends AbstractService implements SoneProvider { database.storeImage(image); } } - synchronized (sones) { + synchronized (soneRescuers) { sone.setOptions(storedSone.get().getOptions()); sone.setKnown(storedSone.get().isKnown()); sone.setStatus((sone.getTime() == 0) ? SoneStatus.unknown : SoneStatus.idle); @@ -863,7 +805,7 @@ public class Core extends AbstractService implements SoneProvider { soneInserters.get(storedSone.get()).setSone(sone); touchConfiguration(); } - sones.put(sone.getId(), sone); + database.storeSone(sone); } } } @@ -881,12 +823,11 @@ public class Core extends AbstractService implements SoneProvider { logger.log(Level.WARNING, String.format("Tried to delete Sone of non-own identity: %s", sone)); return; } - synchronized (sones) { + synchronized (soneRescuers) { if (!getLocalSones().contains(sone)) { logger.log(Level.WARNING, String.format("Tried to delete non-local Sone: %s", sone)); return; } - sones.remove(sone.getId()); SoneInserter soneInserter = soneInserters.remove(sone); soneInserter.stop(); } @@ -1337,7 +1278,7 @@ public class Core extends AbstractService implements SoneProvider { @Override public void serviceStop() { localElementTicker.shutdownNow(); - synchronized (sones) { + synchronized (soneRescuers) { for (Entry soneInserter : soneInserters.entrySet()) { soneInserter.getValue().stop(); saveSone(soneInserter.getKey()); @@ -1680,6 +1621,7 @@ public class Core extends AbstractService implements SoneProvider { OwnIdentity ownIdentity = ownIdentityAddedEvent.ownIdentity(); logger.log(Level.FINEST, String.format("Adding OwnIdentity: %s", ownIdentity)); if (ownIdentity.hasContext("Sone")) { + database.storeIdentity(ownIdentity); addLocalSone(ownIdentity); } } @@ -1708,6 +1650,7 @@ public class Core extends AbstractService implements SoneProvider { Identity identity = identityAddedEvent.identity(); logger.log(Level.FINEST, String.format("Adding Identity: %s", identity)); trustedIdentities.put(identityAddedEvent.ownIdentity(), identity); + database.storeIdentity(identity); addRemoteSone(identity); } @@ -1720,6 +1663,7 @@ public class Core extends AbstractService implements SoneProvider { @Subscribe public void identityUpdated(IdentityUpdatedEvent identityUpdatedEvent) { final Identity identity = identityUpdatedEvent.identity(); + database.storeIdentity(identity); soneDownloaders.execute(new Runnable() { @Override @@ -1770,9 +1714,6 @@ public class Core extends AbstractService implements SoneProvider { for (PostReply reply : sone.get().getReplies()) { eventBus.post(new PostReplyRemovedEvent(reply)); } - synchronized (sones) { - sones.remove(identity.getId()); - } eventBus.post(new SoneRemovedEvent(sone.get())); }