From adbd60904ae1852c29d3e49f2b25af12b8c0814f 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:46:32 +0200 Subject: [PATCH] Integrate basic album storage and retrieval in memory database. --- .../pterodactylus/sone/database/AlbumDatabase.java | 30 +++++++++++++++ .../pterodactylus/sone/database/AlbumProvider.java | 41 ++++++++++++++++++++ .../pterodactylus/sone/database/AlbumStore.java | 45 ++++++++++++++++++++++ .../net/pterodactylus/sone/database/Database.java | 2 +- .../sone/database/memory/MemoryDatabase.java | 41 ++++++++++++++++++++ 5 files changed, 158 insertions(+), 1 deletion(-) create mode 100644 src/main/java/net/pterodactylus/sone/database/AlbumDatabase.java create mode 100644 src/main/java/net/pterodactylus/sone/database/AlbumProvider.java create mode 100644 src/main/java/net/pterodactylus/sone/database/AlbumStore.java diff --git a/src/main/java/net/pterodactylus/sone/database/AlbumDatabase.java b/src/main/java/net/pterodactylus/sone/database/AlbumDatabase.java new file mode 100644 index 0000000..446debc --- /dev/null +++ b/src/main/java/net/pterodactylus/sone/database/AlbumDatabase.java @@ -0,0 +1,30 @@ +/* + * Sone - AlbumDatabase.java - Copyright © 2013 David Roden + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package net.pterodactylus.sone.database; + +/** + * Combines an {@link AlbumProvider} and an {@link AlbumStore} into an album + * database. + * + * @author David ‘Bombe’ Roden + */ +public interface AlbumDatabase extends AlbumProvider, AlbumStore { + + /* nothing here. */ + +} diff --git a/src/main/java/net/pterodactylus/sone/database/AlbumProvider.java b/src/main/java/net/pterodactylus/sone/database/AlbumProvider.java new file mode 100644 index 0000000..b83b179 --- /dev/null +++ b/src/main/java/net/pterodactylus/sone/database/AlbumProvider.java @@ -0,0 +1,41 @@ +/* + * Sone - AlbumProvider.java - Copyright © 2013 David Roden + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package net.pterodactylus.sone.database; + +import net.pterodactylus.sone.data.Album; + +import com.google.common.base.Optional; + +/** + * Interface for objects that can provide {@link Album}s by their ID. + * + * @author David ‘Bombe’ Roden + */ +public interface AlbumProvider { + + /** + * Returns the album with the given ID, or {@link Optional#absent()} if no such + * album exists. + * + * @param albumId + * The ID of the album + * @return The album, or {@link Optional#absent()} if the album does not exist + */ + Optional getAlbum(String albumId); + +} diff --git a/src/main/java/net/pterodactylus/sone/database/AlbumStore.java b/src/main/java/net/pterodactylus/sone/database/AlbumStore.java new file mode 100644 index 0000000..c625535 --- /dev/null +++ b/src/main/java/net/pterodactylus/sone/database/AlbumStore.java @@ -0,0 +1,45 @@ +/* + * Sone - AlbumStore.java - Copyright © 2013 David Roden + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package net.pterodactylus.sone.database; + +import net.pterodactylus.sone.data.Album; + +/** + * Interface for a store of albums. + * + * @author David ‘Bombe’ Roden + */ +public interface AlbumStore { + + /** + * Stores the given album. + * + * @param album + * The album to store + */ + void storeAlbum(Album album); + + /** + * Removes the given album from the store. + * + * @param album + * The album to remove + */ + void removeAlbum(Album album); + +} diff --git a/src/main/java/net/pterodactylus/sone/database/Database.java b/src/main/java/net/pterodactylus/sone/database/Database.java index c93c061..4bf863f 100644 --- a/src/main/java/net/pterodactylus/sone/database/Database.java +++ b/src/main/java/net/pterodactylus/sone/database/Database.java @@ -26,7 +26,7 @@ import com.google.common.util.concurrent.Service; * * @author David ‘Bombe’ Roden */ -public interface Database extends Service, PostDatabase, PostReplyDatabase { +public interface Database extends Service, PostDatabase, PostReplyDatabase, AlbumDatabase { /** * Saves the database. diff --git a/src/main/java/net/pterodactylus/sone/database/memory/MemoryDatabase.java b/src/main/java/net/pterodactylus/sone/database/memory/MemoryDatabase.java index 504d155..bc96715 100644 --- a/src/main/java/net/pterodactylus/sone/database/memory/MemoryDatabase.java +++ b/src/main/java/net/pterodactylus/sone/database/memory/MemoryDatabase.java @@ -34,6 +34,7 @@ import java.util.TreeSet; import java.util.concurrent.locks.ReadWriteLock; import java.util.concurrent.locks.ReentrantReadWriteLock; +import net.pterodactylus.sone.data.Album; import net.pterodactylus.sone.data.Post; import net.pterodactylus.sone.data.PostReply; import net.pterodactylus.sone.data.Reply; @@ -99,6 +100,8 @@ public class MemoryDatabase extends AbstractService implements Database { /** Whether post replies are known. */ private final Set knownPostReplies = new HashSet(); + private final Map allAlbums = new HashMap(); + /** * Creates a new memory database. * @@ -413,6 +416,44 @@ public class MemoryDatabase extends AbstractService implements Database { } // + // ALBUMPROVDER METHODS + // + + @Override + public Optional getAlbum(String albumId) { + lock.readLock().lock(); + try { + return fromNullable(allAlbums.get(albumId)); + } finally { + lock.readLock().unlock(); + } + } + + // + // ALBUMSTORE METHODS + // + + @Override + public void storeAlbum(Album album) { + lock.writeLock().lock(); + try { + allAlbums.put(album.getId(), album); + } finally { + lock.writeLock().unlock(); + } + } + + @Override + public void removeAlbum(Album album) { + lock.writeLock().lock(); + try { + allAlbums.remove(album.getId()); + } finally { + lock.writeLock().unlock(); + } + } + + // // PACKAGE-PRIVATE METHODS // -- 2.7.4