From f1bc016cf9f9d7e7a34a05eb05d90e4ad8a8729e Mon Sep 17 00:00:00 2001 From: Adam Date: Thu, 28 Sep 2017 19:22:56 -0400 Subject: [PATCH] cache: use the most recent cache instead of the archive with the highest revision, which isn't always reliable --- .../runelite/http/service/cache/CacheDAO.java | 39 +++++++++++++------ .../http/service/cache/CacheService.java | 15 +++++-- 2 files changed, 38 insertions(+), 16 deletions(-) diff --git a/http-service/src/main/java/net/runelite/http/service/cache/CacheDAO.java b/http-service/src/main/java/net/runelite/http/service/cache/CacheDAO.java index 3476272dfc..670427abb6 100644 --- a/http-service/src/main/java/net/runelite/http/service/cache/CacheDAO.java +++ b/http-service/src/main/java/net/runelite/http/service/cache/CacheDAO.java @@ -26,6 +26,7 @@ package net.runelite.http.service.cache; import java.time.Instant; import java.util.List; +import net.runelite.cache.IndexType; import net.runelite.http.service.cache.beans.ArchiveEntry; import net.runelite.http.service.cache.beans.CacheEntry; import net.runelite.http.service.cache.beans.FileEntry; @@ -98,28 +99,42 @@ public class CacheDAO .executeAndFetchFirst(ArchiveEntry.class); } - /** - * Finds the most recent archive for the given indexId/archiveId - * @param con - * @param indexId - * @param archiveId - * @return - */ - public ArchiveEntry findMostRecentArchive(Connection con, int indexId, int archiveId) + public ArchiveEntry findArchiveById(Connection con, CacheEntry cache, IndexType index, int archiveId) { return con.createQuery("select archive.id, archive.archiveId, archive.nameHash," + " archive.crc, archive.revision, archive.hash from archive " + "join index_archive on index_archive.archive = archive.id " + "join `index` on index.id = index_archive.index " - + "where index.indexId = :indexId and archive.archiveId = :archiveId " - + "group by archive.id " - + "order by archive.revision desc " + + "join cache_index on cache_index.index = index.id " + + "join cache on cache.id = cache_index.cache " + + "where cache.id = :cacheId " + + "and index.indexId = :indexId " + + "and archive.archiveId = :archiveId " + "limit 1") - .addParameter("indexId", indexId) + .addParameter("cacheId", cache.getId()) + .addParameter("indexId", index.getNumber()) .addParameter("archiveId", archiveId) .executeAndFetchFirst(ArchiveEntry.class); } + public ArchiveEntry findArchiveByName(Connection con, CacheEntry cache, IndexType index, int nameHash) + { + return con.createQuery("select archive.id, archive.archiveId, archive.nameHash," + + " archive.crc, archive.revision, archive.hash from archive " + + "join index_archive on index_archive.archive = archive.id " + + "join `index` on index.id = index_archive.index " + + "join cache_index on cache_index.index = index.id " + + "join cache on cache.id = cache_index.cache " + + "where cache.id = :cacheId " + + "and index.indexId = :indexId " + + "and archive.nameHash = :nameHash " + + "limit 1") + .addParameter("cacheId", cache.getId()) + .addParameter("indexId", index.getNumber()) + .addParameter("nameHash", nameHash) + .executeAndFetchFirst(ArchiveEntry.class); + } + public List findFilesForArchive(Connection con, ArchiveEntry archiveEntry) { if (findFilesForArchive == null) diff --git a/http-service/src/main/java/net/runelite/http/service/cache/CacheService.java b/http-service/src/main/java/net/runelite/http/service/cache/CacheService.java index 5d191a1547..b2b31604f5 100644 --- a/http-service/src/main/java/net/runelite/http/service/cache/CacheService.java +++ b/http-service/src/main/java/net/runelite/http/service/cache/CacheService.java @@ -107,9 +107,10 @@ public class CacheService /** * retrieve archive from storage + * @param archiveEntry * @return */ - private byte[] getArchive(ArchiveEntry archiveEntry) + public byte[] getArchive(ArchiveEntry archiveEntry) { String hashStr = BaseEncoding.base16().encode(archiveEntry.getHash()); String path = new StringBuilder() @@ -297,7 +298,9 @@ public class CacheService try (Connection con = sql2o.open()) { CacheDAO cacheDao = new CacheDAO(); - archiveEntry = cacheDao.findMostRecentArchive(con, IndexType.CONFIGS.getNumber(), ConfigType.ITEM.getId()); + + CacheEntry cache = cacheDao.findMostRecent(con); + archiveEntry = cacheDao.findArchiveById(con, cache, IndexType.CONFIGS, ConfigType.ITEM.getId()); if (archiveEntry == null) { throw new NotFoundException(); @@ -329,7 +332,9 @@ public class CacheService try (Connection con = sql2o.open()) { CacheDAO cacheDao = new CacheDAO(); - archiveEntry = cacheDao.findMostRecentArchive(con, IndexType.CONFIGS.getNumber(), ConfigType.OBJECT.getId()); + + CacheEntry cache = cacheDao.findMostRecent(con); + archiveEntry = cacheDao.findArchiveById(con, cache, IndexType.CONFIGS, ConfigType.OBJECT.getId()); if (archiveEntry == null) { throw new NotFoundException(); @@ -361,7 +366,9 @@ public class CacheService try (Connection con = sql2o.open()) { CacheDAO cacheDao = new CacheDAO(); - archiveEntry = cacheDao.findMostRecentArchive(con, IndexType.CONFIGS.getNumber(), ConfigType.NPC.getId()); + + CacheEntry cache = cacheDao.findMostRecent(con); + archiveEntry = cacheDao.findArchiveById(con, cache, IndexType.CONFIGS, ConfigType.NPC.getId()); if (archiveEntry == null) { throw new NotFoundException();