From 9d9b901b7173a9b8221a8b830ece31bc5ee36578 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Sat, 8 Mar 2014 22:30:04 +0100 Subject: [PATCH] Extract memory-based identity database into its own class. --- .../sone/database/memory/MemoryDatabase.java | 18 ++------ .../database/memory/MemoryIdentityDatabase.java | 49 ++++++++++++++++++++++ 2 files changed, 53 insertions(+), 14 deletions(-) create mode 100644 src/main/java/net/pterodactylus/sone/database/memory/MemoryIdentityDatabase.java 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 8d60e0f..bace9d9 100644 --- a/src/main/java/net/pterodactylus/sone/database/memory/MemoryDatabase.java +++ b/src/main/java/net/pterodactylus/sone/database/memory/MemoryDatabase.java @@ -57,7 +57,6 @@ import com.google.common.base.Optional; import com.google.common.collect.ArrayListMultimap; import com.google.common.collect.HashMultimap; import com.google.common.collect.ListMultimap; -import com.google.common.collect.Maps; import com.google.common.collect.SetMultimap; import com.google.common.collect.SortedSetMultimap; import com.google.common.collect.TreeMultimap; @@ -79,8 +78,8 @@ public class MemoryDatabase extends AbstractService implements Database { /** The configuration. */ private final Configuration configuration; - private final Map identities = Maps.newHashMap(); private final Map sones = new HashMap(); + private final MemoryIdentityDatabase memoryIdentityDatabase; private final MemoryPostDatabase memoryPostDatabase; /** All post replies by their ID. */ @@ -125,6 +124,7 @@ public class MemoryDatabase extends AbstractService implements Database { public MemoryDatabase(Configuration configuration) { this.configuration = configuration; memoryPostDatabase = new MemoryPostDatabase(this, lock, configuration); + memoryIdentityDatabase = new MemoryIdentityDatabase(lock); } // @@ -150,22 +150,12 @@ public class MemoryDatabase extends AbstractService implements Database { @Override public Optional getIdentity(String identityId) { - lock.readLock().lock(); - try { - return fromNullable(identities.get(identityId)); - } finally { - lock.readLock().unlock(); - } + return memoryIdentityDatabase.getIdentity(identityId); } @Override public void storeIdentity(Identity identitiy) { - lock.writeLock().lock(); - try { - identities.put(identitiy.getId(), identitiy); - } finally { - lock.writeLock().unlock(); - } + memoryIdentityDatabase.storeIdentity(identitiy); } @Override diff --git a/src/main/java/net/pterodactylus/sone/database/memory/MemoryIdentityDatabase.java b/src/main/java/net/pterodactylus/sone/database/memory/MemoryIdentityDatabase.java new file mode 100644 index 0000000..9ae1343 --- /dev/null +++ b/src/main/java/net/pterodactylus/sone/database/memory/MemoryIdentityDatabase.java @@ -0,0 +1,49 @@ +package net.pterodactylus.sone.database.memory; + +import static com.google.common.base.Optional.fromNullable; +import static com.google.common.collect.Maps.newHashMap; + +import java.util.Map; +import java.util.concurrent.locks.ReadWriteLock; + +import net.pterodactylus.sone.database.IdentityDatabase; +import net.pterodactylus.sone.freenet.wot.Identity; + +import com.google.common.base.Optional; + +/** + * {@link IdentityDatabase} implementation that keeps all {@link Identity}s in + * memory. + * + * @author David ‘Bombe’ Roden + */ +public class MemoryIdentityDatabase implements IdentityDatabase { + + private final ReadWriteLock lock; + private final Map identities = newHashMap(); + + public MemoryIdentityDatabase(ReadWriteLock readWriteLock) { + this.lock = readWriteLock; + } + + @Override + public Optional getIdentity(String identityId) { + lock.readLock().lock(); + try { + return fromNullable(identities.get(identityId)); + } finally { + lock.readLock().unlock(); + } + } + + @Override + public void storeIdentity(Identity identitiy) { + lock.writeLock().lock(); + try { + identities.put(identitiy.getId(), identitiy); + } finally { + lock.writeLock().unlock(); + } + } + +} -- 2.7.4