cache service: fix duplicating index to archive associations
It is unclear whether archives in an index can change without the index crc or revision changing, so always create new indexes for each cache. This is much simplier with not much more overhead.
This commit is contained in:
@@ -57,22 +57,16 @@ public class CacheDAO
|
||||
|
||||
public List<IndexEntry> findIndexesForCache(Connection con, CacheEntry cache)
|
||||
{
|
||||
return con.createQuery("select index.id, index.indexId, index.crc, index.revision from cache "
|
||||
+ "join cache_index on cache_index.cache = cache.id "
|
||||
+ "join `index` on cache_index.index = index.id "
|
||||
+ "where cache.id = :id "
|
||||
+ "order by index.indexId asc")
|
||||
.addParameter("id", cache.getId())
|
||||
return con.createQuery("select id, indexId, crc, revision from `index` where cache = :cache")
|
||||
.addParameter("cache", cache.getId())
|
||||
.executeAndFetch(IndexEntry.class);
|
||||
}
|
||||
|
||||
public IndexEntry findIndexForCache(Connection con, CacheEntry cache, int indexId)
|
||||
{
|
||||
return con.createQuery("select index.id, index.indexId, index.crc, index.revision from cache "
|
||||
+ "join cache_index on cache_index.cache = cache.id "
|
||||
+ "join `index` on cache_index.index = index.id "
|
||||
+ "where cache.id = :id "
|
||||
+ "and index.indexId = :indexId")
|
||||
return con.createQuery("select id, indexId, crc, revision from `index` "
|
||||
+ "where cache = :id "
|
||||
+ "and indexId = :indexId")
|
||||
.addParameter("id", cache.getId())
|
||||
.addParameter("indexId", indexId)
|
||||
.executeAndFetchFirst(IndexEntry.class);
|
||||
@@ -80,18 +74,18 @@ public class CacheDAO
|
||||
|
||||
public ResultSetIterable<ArchiveEntry> findArchivesForIndex(Connection con, IndexEntry indexEntry)
|
||||
{
|
||||
return con.createQuery("select archive.id, archive.archiveId, archive.nameHash," +
|
||||
" archive.crc, archive.revision, archive.hash from index_archive "
|
||||
return con.createQuery("select archive.id, archive.archiveId, archive.nameHash,"
|
||||
+ " archive.crc, archive.revision, archive.hash from index_archive "
|
||||
+ "join archive on index_archive.archive = archive.id "
|
||||
+ "where index_archive.index = :id")
|
||||
.addParameter("id", indexEntry.getId())
|
||||
.executeAndFetchLazy(ArchiveEntry.class);
|
||||
}
|
||||
|
||||
|
||||
public ArchiveEntry findArchiveForIndex(Connection con, IndexEntry indexEntry, int archiveId)
|
||||
{
|
||||
return con.createQuery("select archive.id, archive.archiveId, archive.nameHash," +
|
||||
" archive.crc, archive.revision, archive.hash from index_archive "
|
||||
return con.createQuery("select archive.id, archive.archiveId, archive.nameHash,"
|
||||
+ " archive.crc, archive.revision, archive.hash from index_archive "
|
||||
+ "join archive on index_archive.archive = archive.id "
|
||||
+ "where index_archive.index = :id "
|
||||
+ "and archive.archiveId = :archiveId")
|
||||
@@ -102,13 +96,11 @@ public class CacheDAO
|
||||
|
||||
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 "
|
||||
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 "
|
||||
+ "where index.cache = :cacheId "
|
||||
+ "and index.indexId = :indexId "
|
||||
+ "and archive.archiveId = :archiveId "
|
||||
+ "limit 1")
|
||||
@@ -120,13 +112,11 @@ public class CacheDAO
|
||||
|
||||
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 "
|
||||
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 "
|
||||
+ "where index.cache = :cacheId "
|
||||
+ "and index.indexId = :indexId "
|
||||
+ "and archive.nameHash = :nameHash "
|
||||
+ "limit 1")
|
||||
@@ -163,7 +153,7 @@ public class CacheDAO
|
||||
entry.setDate(date);
|
||||
return entry;
|
||||
}
|
||||
|
||||
|
||||
public CacheEntry findCache(Connection con, int cacheId)
|
||||
{
|
||||
return con.createQuery("select id, revision, date from cache "
|
||||
@@ -172,50 +162,17 @@ public class CacheDAO
|
||||
.executeAndFetchFirst(CacheEntry.class);
|
||||
}
|
||||
|
||||
public IndexEntry findIndex(Connection con, int indexId, int crc, int revision)
|
||||
public IndexEntry createIndex(Connection con, CacheEntry cache, int indexId, int crc, int revision)
|
||||
{
|
||||
return con.createQuery("select id, indexId, crc, revision from `index` "
|
||||
+ "where indexId = :indexId "
|
||||
+ "and crc = :crc "
|
||||
+ "and revision = :revision")
|
||||
.addParameter("indexId", indexId)
|
||||
.addParameter("crc", crc)
|
||||
.addParameter("revision", revision)
|
||||
.executeAndFetchFirst(IndexEntry.class);
|
||||
}
|
||||
|
||||
public void associateIndexToCache(Connection con, CacheEntry cache, IndexEntry index)
|
||||
{
|
||||
con.createQuery("insert into cache_index (cache, `index`) values (:cache, :index)")
|
||||
int id = con.createQuery("insert into `index` (cache, indexId, crc, revision) values (:cache, :indexId, :crc, :revision)")
|
||||
.addParameter("cache", cache.getId())
|
||||
.addParameter("index", index.getId())
|
||||
.executeUpdate();
|
||||
}
|
||||
|
||||
public IndexEntry findOrCreateIndex(Connection con, CacheEntry cache, int indexId, int crc, int revision)
|
||||
{
|
||||
IndexEntry entry = con.createQuery("select id, indexId, crc, revision from `index`"
|
||||
+ "where indexId = :indexId "
|
||||
+ "and crc = :crc "
|
||||
+ "and revision = :revision")
|
||||
.addParameter("indexId", indexId)
|
||||
.addParameter("crc", crc)
|
||||
.addParameter("revision", revision)
|
||||
.executeAndFetchFirst(IndexEntry.class);
|
||||
|
||||
if (entry != null)
|
||||
{
|
||||
return entry;
|
||||
}
|
||||
|
||||
int id = con.createQuery("insert into `index` (indexId, crc, revision) values (:indexId, :crc, :revision)")
|
||||
.addParameter("indexId", indexId)
|
||||
.addParameter("crc", crc)
|
||||
.addParameter("revision", revision)
|
||||
.executeUpdate()
|
||||
.getKey(int.class);
|
||||
|
||||
entry = new IndexEntry();
|
||||
IndexEntry entry = new IndexEntry();
|
||||
entry.setId(id);
|
||||
entry.setIndexId(indexId);
|
||||
entry.setCrc(crc);
|
||||
|
||||
@@ -26,6 +26,7 @@ package net.runelite.http.service.cache;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import net.runelite.cache.fs.Archive;
|
||||
import net.runelite.cache.fs.Index;
|
||||
import net.runelite.cache.fs.Storage;
|
||||
@@ -37,6 +38,7 @@ import net.runelite.http.service.cache.beans.IndexEntry;
|
||||
import org.sql2o.Connection;
|
||||
import org.sql2o.ResultSetIterable;
|
||||
|
||||
@Slf4j
|
||||
public class CacheStorage implements Storage
|
||||
{
|
||||
private CacheEntry cacheEntry;
|
||||
@@ -84,6 +86,11 @@ public class CacheStorage implements Storage
|
||||
{
|
||||
for (ArchiveEntry archiveEntry : archives)
|
||||
{
|
||||
if (index.getArchive(archiveEntry.getArchiveId()) != null)
|
||||
{
|
||||
throw new IOException("Duplicate archive " + archiveEntry + " on " + indexEntry);
|
||||
}
|
||||
|
||||
Archive archive = index.addArchive(archiveEntry.getArchiveId());
|
||||
archive.setNameHash(archiveEntry.getNameHash());
|
||||
archive.setCrc(archiveEntry.getCrc());
|
||||
@@ -101,9 +108,7 @@ public class CacheStorage implements Storage
|
||||
{
|
||||
for (Index index : store.getIndexes())
|
||||
{
|
||||
IndexEntry entry = cacheDao.findOrCreateIndex(con, cacheEntry, index.getId(), index.getCrc(), index.getRevision());
|
||||
// this assumes nothing is associated to the cache yet
|
||||
cacheDao.associateIndexToCache(con, cacheEntry, entry);
|
||||
IndexEntry entry = cacheDao.createIndex(con, cacheEntry, index.getId(), index.getCrc(), index.getRevision());
|
||||
|
||||
for (Archive archive : index.getArchives())
|
||||
{
|
||||
@@ -120,6 +125,7 @@ public class CacheStorage implements Storage
|
||||
cacheDao.associateFileToArchive(con, archiveEntry, file.getId(), file.getNameHash());
|
||||
}
|
||||
}
|
||||
|
||||
cacheDao.associateArchiveToIndex(con, archiveEntry, entry);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user