Add method to expose a Sone loader.
[Sone.git] / src / main / java / net / pterodactylus / sone / database / memory / MemoryDatabase.java
index 8c5c64f..02ca290 100644 (file)
@@ -173,6 +173,16 @@ public class MemoryDatabase extends AbstractService implements Database {
        }
 
        @Override
+       public Function<String, Optional<Sone>> getSone() {
+               return new Function<String, Optional<Sone>>() {
+                       @Override
+                       public Optional<Sone> apply(String soneId) {
+                               return (soneId == null) ? Optional.<Sone>absent() : getSone(soneId);
+                       }
+               };
+       }
+
+       @Override
        public Optional<Sone> getSone(String soneId) {
                lock.readLock().lock();
                try {
@@ -218,7 +228,12 @@ public class MemoryDatabase extends AbstractService implements Database {
                        @Override
                        public Sone build(Optional<SoneCreated> soneCreated) throws IllegalStateException {
                                Sone sone = super.build(soneCreated);
-                               sones.put(sone.getId(), sone);
+                               lock.writeLock().lock();
+                               try {
+                                       sones.put(sone.getId(), sone);
+                               } finally {
+                                       lock.writeLock().unlock();
+                               }
                                return sone;
                        }
                };
@@ -531,7 +546,9 @@ public class MemoryDatabase extends AbstractService implements Database {
                lock.writeLock().lock();
                try {
                        allAlbums.put(album.getId(), album);
-                       albumChildren.put(album.getParent().getId(), album.getId());
+                       if (!album.isRoot()) {
+                               albumChildren.put(album.getParent().getId(), album.getId());
+                       }
                } finally {
                        lock.writeLock().unlock();
                }