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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
-- MySQL dump 10.16 Distrib 10.1.20-MariaDB, for Linux (x86_64)
|
||||
-- MySQL dump 10.16 Distrib 10.2.9-MariaDB, for Linux (x86_64)
|
||||
--
|
||||
-- Host: localhost Database: localhost
|
||||
-- Host: localhost Database: cache
|
||||
-- ------------------------------------------------------
|
||||
-- Server version 10.1.20-MariaDB
|
||||
-- Server version 10.2.9-MariaDB
|
||||
|
||||
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
|
||||
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
|
||||
@@ -44,31 +44,12 @@ DROP TABLE IF EXISTS `cache`;
|
||||
CREATE TABLE `cache` (
|
||||
`id` int(11) NOT NULL AUTO_INCREMENT,
|
||||
`revision` int(11) NOT NULL,
|
||||
`date` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
|
||||
`date` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
|
||||
PRIMARY KEY (`id`),
|
||||
UNIQUE KEY `revision_date` (`revision`,`date`)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
|
||||
/*!40101 SET character_set_client = @saved_cs_client */;
|
||||
|
||||
--
|
||||
-- Table structure for table `cache_index`
|
||||
--
|
||||
|
||||
DROP TABLE IF EXISTS `cache_index`;
|
||||
/*!40101 SET @saved_cs_client = @@character_set_client */;
|
||||
/*!40101 SET character_set_client = utf8 */;
|
||||
CREATE TABLE `cache_index` (
|
||||
`id` int(11) NOT NULL AUTO_INCREMENT,
|
||||
`cache` int(11) NOT NULL,
|
||||
`index` int(11) NOT NULL,
|
||||
PRIMARY KEY (`id`),
|
||||
KEY `cacheId` (`cache`),
|
||||
KEY `indexId` (`index`),
|
||||
CONSTRAINT `cache_index_ibfk_1` FOREIGN KEY (`cache`) REFERENCES `cache` (`id`),
|
||||
CONSTRAINT `cache_index_ibfk_2` FOREIGN KEY (`index`) REFERENCES `index` (`id`)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
|
||||
/*!40101 SET character_set_client = @saved_cs_client */;
|
||||
|
||||
--
|
||||
-- Table structure for table `file`
|
||||
--
|
||||
@@ -96,11 +77,13 @@ DROP TABLE IF EXISTS `index`;
|
||||
/*!40101 SET character_set_client = utf8 */;
|
||||
CREATE TABLE `index` (
|
||||
`id` int(11) NOT NULL AUTO_INCREMENT,
|
||||
`cache` int(11) NOT NULL,
|
||||
`indexId` int(11) NOT NULL,
|
||||
`crc` int(11) NOT NULL,
|
||||
`revision` int(11) NOT NULL,
|
||||
PRIMARY KEY (`id`),
|
||||
UNIQUE KEY `indexId` (`indexId`,`revision`,`crc`) USING BTREE
|
||||
UNIQUE KEY `indexId` (`cache`,`indexId`,`revision`,`crc`) USING BTREE,
|
||||
CONSTRAINT `index_ibfk_1` FOREIGN KEY (`cache`) REFERENCES `cache` (`id`)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
|
||||
/*!40101 SET character_set_client = @saved_cs_client */;
|
||||
|
||||
@@ -116,7 +99,7 @@ CREATE TABLE `index_archive` (
|
||||
`index` int(11) NOT NULL,
|
||||
`archive` int(11) NOT NULL,
|
||||
PRIMARY KEY (`id`),
|
||||
KEY `index` (`index`) USING BTREE,
|
||||
UNIQUE KEY `idx_index_archive` (`index`,`archive`) USING BTREE,
|
||||
KEY `archive` (`archive`) USING BTREE,
|
||||
CONSTRAINT `index_archive_ibfk_1` FOREIGN KEY (`index`) REFERENCES `index` (`id`),
|
||||
CONSTRAINT `index_archive_ibfk_2` FOREIGN KEY (`archive`) REFERENCES `archive` (`id`)
|
||||
@@ -132,4 +115,4 @@ CREATE TABLE `index_archive` (
|
||||
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
|
||||
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
|
||||
|
||||
-- Dump completed on 2017-09-24 15:38:31
|
||||
-- Dump completed on 2018-02-02 21:55:48
|
||||
|
||||
Reference in New Issue
Block a user