From 3d6e9664c7f0eb1ff37111b7cbd1bfdca2798567 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Wed, 9 Oct 2013 19:47:07 +0200 Subject: [PATCH] Use the memory database instead of storing the albums in the core. --- .../java/net/pterodactylus/sone/core/Core.java | 49 ++++++++++------------ 1 file changed, 21 insertions(+), 28 deletions(-) diff --git a/src/main/java/net/pterodactylus/sone/core/Core.java b/src/main/java/net/pterodactylus/sone/core/Core.java index a6a3316..8ce908b 100644 --- a/src/main/java/net/pterodactylus/sone/core/Core.java +++ b/src/main/java/net/pterodactylus/sone/core/Core.java @@ -188,9 +188,6 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, /** Trusted identities, sorted by own identities. */ private final Multimap trustedIdentities = Multimaps.synchronizedSetMultimap(HashMultimap.create()); - /** All known albums. */ - private final Map albums = new HashMap(); - /** All known images. */ private final Map images = new HashMap(); @@ -637,14 +634,16 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, * given ID exists and {@code create} is {@code false} */ public Album getAlbum(String albumId, boolean create) { - synchronized (albums) { - Album album = albums.get(albumId); - if (create && (album == null)) { - album = new AlbumImpl(albumId); - albums.put(albumId, album); - } - return album; + Optional album = database.getAlbum(albumId); + if (album.isPresent()) { + return album.get(); } + if (!create) { + return null; + } + Album newAlbum = new AlbumImpl(albumId); + database.storeAlbum(newAlbum); + return newAlbum; } /** @@ -1034,19 +1033,17 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, } } database.storePostReplies(sone, sone.getReplies()); - synchronized (albums) { - synchronized (images) { - for (Album album : storedSone.get().getRootAlbum().getAlbums()) { - albums.remove(album.getId()); - for (Image image : album.getImages()) { - images.remove(image.getId()); - } + synchronized (images) { + for (Album album : storedSone.get().getRootAlbum().getAlbums()) { + database.removeAlbum(album); + for (Image image : album.getImages()) { + images.remove(image.getId()); } - for (Album album : sone.getRootAlbum().getAlbums()) { - albums.put(album.getId(), album); - for (Image image : album.getImages()) { - images.put(image.getId(), image); - } + } + for (Album album : sone.getRootAlbum().getAlbums()) { + database.storeAlbum(album); + for (Image image : album.getImages()) { + images.put(image.getId(), image); } } } @@ -1605,9 +1602,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, */ public Album createAlbum(Sone sone, Album parent) { AlbumImpl album = new AlbumImpl(); - synchronized (albums) { - albums.put(album.getId(), album); - } + database.storeAlbum(album); album.setSone(sone); parent.addAlbum(album); return album; @@ -1627,9 +1622,7 @@ public class Core extends AbstractService implements SoneProvider, PostProvider, return; } album.getParent().removeAlbum(album); - synchronized (albums) { - albums.remove(album.getId()); - } + database.removeAlbum(album); touchConfiguration(); } -- 2.7.4