Extract memory-based identity database into its own class.
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sat, 8 Mar 2014 21:30:04 +0000 (22:30 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sat, 8 Mar 2014 21:30:49 +0000 (22:30 +0100)
src/main/java/net/pterodactylus/sone/database/memory/MemoryDatabase.java
src/main/java/net/pterodactylus/sone/database/memory/MemoryIdentityDatabase.java [new file with mode: 0644]

index 8d60e0f..bace9d9 100644 (file)
@@ -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<String, Identity> identities = Maps.newHashMap();
        private final Map<String, Sone> sones = new HashMap<String, Sone>();
+       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<Identity> 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 (file)
index 0000000..9ae1343
--- /dev/null
@@ -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 <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
+ */
+public class MemoryIdentityDatabase implements IdentityDatabase {
+
+       private final ReadWriteLock lock;
+       private final Map<String, Identity> identities = newHashMap();
+
+       public MemoryIdentityDatabase(ReadWriteLock readWriteLock) {
+               this.lock = readWriteLock;
+       }
+
+       @Override
+       public Optional<Identity> 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();
+               }
+       }
+
+}