Search in sources :

Example 1 with IndexEntry

use of net.runelite.cache.updater.beans.IndexEntry in project runelite by runelite.

the class CacheStorage method load.

@Override
public void load(Store store) throws IOException {
    List<IndexEntry> indexes = cacheDao.findIndexesForCache(con, cacheEntry);
    for (IndexEntry indexEntry : indexes) {
        Index index = store.addIndex(indexEntry.getIndexId());
        index.setCrc(indexEntry.getCrc());
        index.setRevision(indexEntry.getRevision());
        try (ResultSetIterable<ArchiveEntry> archives = cacheDao.findArchivesForIndex(con, indexEntry)) {
            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());
                archive.setRevision(archiveEntry.getRevision());
                archive.setHash(archiveEntry.getHash());
            // File data is not necessary for cache updating
            }
        }
    }
}
Also used : Archive(net.runelite.cache.fs.Archive) IndexEntry(net.runelite.cache.updater.beans.IndexEntry) Index(net.runelite.cache.fs.Index) ArchiveEntry(net.runelite.cache.updater.beans.ArchiveEntry) IOException(java.io.IOException)

Example 2 with IndexEntry

use of net.runelite.cache.updater.beans.IndexEntry in project runelite by runelite.

the class CacheUpdater method update.

public void update() throws IOException, InvalidEndpointException, InvalidPortException, InterruptedException {
    int rsVersion = RuneLiteAPI.getRsVersion();
    try (Connection con = sql2o.beginTransaction()) {
        CacheDAO cacheDao = new CacheDAO();
        CacheEntry cache = cacheDao.findMostRecent(con);
        boolean created = false;
        if (cache == null) {
            created = true;
            cache = cacheDao.createCache(con, rsVersion, Instant.now());
        }
        CacheStorage storage = new CacheStorage(cache, cacheDao, con);
        Store store = new Store(storage);
        store.load();
        ExecutorService executor = Executors.newSingleThreadExecutor();
        CacheClient client = new CacheClient(store, rsVersion, (Archive archive, byte[] data) -> executor.submit(new CacheUploader(minioClient, minioBucket, archive, data)));
        client.connect();
        HandshakeResponseType result = client.handshake().join();
        if (result != HandshakeResponseType.RESPONSE_OK) {
            logger.warn("Out of date!");
            return;
        }
        List<IndexInfo> indexes = client.requestIndexes();
        List<IndexEntry> entries = cacheDao.findIndexesForCache(con, cache);
        if (!checkOutOfDate(indexes, entries)) {
            logger.info("All up to date.");
            return;
        }
        client.download();
        CacheEntry newCache = created ? cache : cacheDao.createCache(con, rsVersion, Instant.now());
        storage.setCacheEntry(newCache);
        store.save();
        // ensure objects are added to the store before they become
        // visible in the database
        executor.shutdown();
        while (!executor.awaitTermination(1, TimeUnit.SECONDS)) {
            logger.debug("Waiting for termination of executor...");
        }
        // commit database
        con.commit();
    }
}
Also used : CacheClient(net.runelite.cache.client.CacheClient) Archive(net.runelite.cache.fs.Archive) HandshakeResponseType(net.runelite.protocol.api.login.HandshakeResponseType) Connection(org.sql2o.Connection) Store(net.runelite.cache.fs.Store) IndexEntry(net.runelite.cache.updater.beans.IndexEntry) IndexInfo(net.runelite.cache.client.IndexInfo) CacheEntry(net.runelite.cache.updater.beans.CacheEntry) ExecutorService(java.util.concurrent.ExecutorService)

Example 3 with IndexEntry

use of net.runelite.cache.updater.beans.IndexEntry in project runelite by runelite.

the class CacheDAO method createIndex.

public IndexEntry createIndex(Connection con, CacheEntry cache, int indexId, int crc, int revision) {
    int id = con.createQuery("insert into `index` (cache, indexId, crc, revision) values (:cache, :indexId, :crc, :revision)").addParameter("cache", cache.getId()).addParameter("indexId", indexId).addParameter("crc", crc).addParameter("revision", revision).executeUpdate().getKey(int.class);
    IndexEntry entry = new IndexEntry();
    entry.setId(id);
    entry.setIndexId(indexId);
    entry.setCrc(crc);
    entry.setRevision(revision);
    return entry;
}
Also used : IndexEntry(net.runelite.cache.updater.beans.IndexEntry)

Example 4 with IndexEntry

use of net.runelite.cache.updater.beans.IndexEntry in project runelite by runelite.

the class CacheStorage method save.

@Override
public void save(Store store) throws IOException {
    for (Index index : store.getIndexes()) {
        IndexEntry entry = cacheDao.createIndex(con, cacheEntry, index.getId(), index.getCrc(), index.getRevision());
        for (Archive archive : index.getArchives()) {
            ArchiveEntry archiveEntry = cacheDao.findArchive(con, entry, archive.getArchiveId(), archive.getNameHash(), archive.getCrc(), archive.getRevision());
            if (archiveEntry == null) {
                byte[] hash = archive.getHash();
                archiveEntry = cacheDao.createArchive(con, entry, archive.getArchiveId(), archive.getNameHash(), archive.getCrc(), archive.getRevision(), hash);
                for (FileData file : archive.getFileData()) {
                    cacheDao.associateFileToArchive(con, archiveEntry, file.getId(), file.getNameHash());
                }
            }
            cacheDao.associateArchiveToIndex(con, archiveEntry, entry);
        }
    }
}
Also used : Archive(net.runelite.cache.fs.Archive) IndexEntry(net.runelite.cache.updater.beans.IndexEntry) Index(net.runelite.cache.fs.Index) ArchiveEntry(net.runelite.cache.updater.beans.ArchiveEntry) FileData(net.runelite.cache.index.FileData)

Example 5 with IndexEntry

use of net.runelite.cache.updater.beans.IndexEntry in project runelite by runelite.

the class CacheUpdater method checkOutOfDate.

private boolean checkOutOfDate(List<IndexInfo> indexes, List<IndexEntry> dbIndexes) {
    if (indexes.size() != dbIndexes.size()) {
        return true;
    }
    for (int i = 0; i < indexes.size(); ++i) {
        IndexInfo ii = indexes.get(i);
        IndexEntry ie = dbIndexes.get(i);
        if (ii.getId() != ie.getIndexId() || ii.getRevision() != ie.getRevision() || ii.getCrc() != ie.getCrc()) {
            return true;
        }
    }
    return false;
}
Also used : IndexEntry(net.runelite.cache.updater.beans.IndexEntry) IndexInfo(net.runelite.cache.client.IndexInfo)

Aggregations

IndexEntry (net.runelite.cache.updater.beans.IndexEntry)5 Archive (net.runelite.cache.fs.Archive)3 IndexInfo (net.runelite.cache.client.IndexInfo)2 Index (net.runelite.cache.fs.Index)2 ArchiveEntry (net.runelite.cache.updater.beans.ArchiveEntry)2 IOException (java.io.IOException)1 ExecutorService (java.util.concurrent.ExecutorService)1 CacheClient (net.runelite.cache.client.CacheClient)1 Store (net.runelite.cache.fs.Store)1 FileData (net.runelite.cache.index.FileData)1 CacheEntry (net.runelite.cache.updater.beans.CacheEntry)1 HandshakeResponseType (net.runelite.protocol.api.login.HandshakeResponseType)1 Connection (org.sql2o.Connection)1