X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fnet%2Fpterodactylus%2Fsone%2Fdatabase%2Fmemory%2FMemoryDatabase.java;h=485c45246d9be3d956d68ef41c1fc24d759be965;hb=50cca938f17fed2df8262c9bebc9ebe1712d8da5;hp=8815abc5c21cd35632f7eb8ce9667a97a22c09d5;hpb=f8672b1385173a103d7f085d8e9cd43bc5762d71;p=Sone.git 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 8815abc..485c452 100644 --- a/src/main/java/net/pterodactylus/sone/database/memory/MemoryDatabase.java +++ b/src/main/java/net/pterodactylus/sone/database/memory/MemoryDatabase.java @@ -182,45 +182,57 @@ public class MemoryDatabase extends AbstractService implements Database { } private LocalSone loadLocalSone(OwnIdentity ownIdentity) { - LocalSone localSone = newSoneBuilder().from(ownIdentity).using( - new Client("Sone", SonePlugin.VERSION.toString())).buildLocal(); + final SoneBuilder soneBuilder = newSoneBuilder().from(ownIdentity).using( + new Client("Sone", SonePlugin.VERSION.toString())); + + loadElements(soneBuilder, ownIdentity.getId()); + + LocalSone localSone = soneBuilder.buildLocal(); + loadSone(localSone); + localSone.setKnown(true); localSone.setLatestEdition( Optional.fromNullable( Longs.tryParse(ownIdentity.getProperty(LATEST_EDITION_PROPERTY))) .or(0L)); - localSone.setKnown(true); - - loadSone(localSone); return localSone; } - private void loadSone(LocalSone sone) { - long soneTime = configurationLoader.getLocalSoneTime(sone.getId()); + private void loadElements(SoneBuilder soneBuilder, String soneId) { + long soneTime = configurationLoader.getLocalSoneTime(soneId); if (soneTime == -1) { return; } + soneBuilder.lastUpdated(soneTime); - /* load profile. */ - ConfigurationSoneParser configurationSoneParser = new ConfigurationSoneParser(configuration, sone); - Profile profile = configurationSoneParser.parseProfile(); + ConfigurationSoneParser configurationSoneParser = new ConfigurationSoneParser(configuration, soneId); - /* load posts. */ - Collection posts; try { - posts = configurationSoneParser.parsePosts(this); + Set posts = configurationSoneParser.parsePosts(this); + soneBuilder.withPosts(posts); + for (Post post : posts) { + post.setKnown(true); + } } catch (InvalidPostFound ipf) { logger.log(Level.WARNING, "Invalid post found, aborting load!"); return; } - /* load replies. */ - Collection postReplies; try { - postReplies = configurationSoneParser.parsePostReplies(this); + Set postReplies = configurationSoneParser.parsePostReplies(this); + soneBuilder.withPostReplies(postReplies); + for (PostReply reply : postReplies) { + reply.setKnown(true); + } } catch (InvalidPostReplyFound iprf) { logger.log(Level.WARNING, "Invalid reply found, aborting load!"); return; } + } + + private void loadSone(LocalSone sone) { + /* load profile. */ + ConfigurationSoneParser configurationSoneParser = new ConfigurationSoneParser(configuration, sone.getId()); + Profile profile = configurationSoneParser.parseProfile(sone); /* load post likes. */ Set likedPostIds = configurationSoneParser.parseLikedPostIds(); @@ -231,7 +243,7 @@ public class MemoryDatabase extends AbstractService implements Database { /* load albums. */ List topLevelAlbums; try { - topLevelAlbums = configurationSoneParser.parseTopLevelAlbums(this); + topLevelAlbums = configurationSoneParser.parseTopLevelAlbums(this, sone); } catch (InvalidAlbumFound iaf) { logger.log(Level.WARNING, "Invalid album found, aborting load!"); return; @@ -243,7 +255,7 @@ public class MemoryDatabase extends AbstractService implements Database { /* load images. */ try { - configurationSoneParser.parseImages(this); + configurationSoneParser.parseImages(this, sone); } catch (InvalidImageFound iif) { logger.log(WARNING, "Invalid image found, aborting load!"); return; @@ -275,7 +287,7 @@ public class MemoryDatabase extends AbstractService implements Database { /* if we’re still here, Sone was loaded successfully. */ lock.writeLock().lock(); try { - updateSoneTime(sone, soneTime); + updateSoneTime(sone, sone.getTime()); sone.setProfile(profile); sone.setLikePostIds(likedPostIds); sone.setLikeReplyIds(likedReplyIds); @@ -284,19 +296,13 @@ public class MemoryDatabase extends AbstractService implements Database { lastInsertFingerprints.put(sone.getId(), lastInsertFingerprint); allSones.put(sone.getId(), sone); - storePosts(sone.getId(), posts); - storePostReplies(sone.getId(), postReplies); + storePosts(sone.getId(), sone.getPosts()); + storePostReplies(sone.getId(), sone.getReplies()); storeAlbums(sone.getId(), topLevelAlbums); storeImages(sone.getId(), from(topLevelAlbums).transformAndConcat(Album.FLATTENER).transformAndConcat(Album.IMAGES).toList()); } finally { lock.writeLock().unlock(); } - for (Post post : posts) { - post.setKnown(true); - } - for (PostReply reply : postReplies) { - reply.setKnown(true); - } logger.info(String.format("Sone loaded successfully: %s", sone)); }