Skip to content

Commit 45f6b3d

Browse files
committed
perf: cache full merged DB by commit SHA, skip all shard loading on no-change refresh
1 parent bf09ff5 commit 45f6b3d

2 files changed

Lines changed: 23 additions & 0 deletions

File tree

frontend/js/app.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,15 @@ async function _fetchAndDecryptIndex(owner, repo, indexSha, password, token) {
160160
}
161161

162162
async function _loadFromShardManifest(manifest, owner, repo, password, token, progress) {
163+
// 0) Check if the full merged DB is already cached for this commit SHA.
164+
// If the data branch hasn't moved, we can skip ALL shard loading.
165+
var mergedKey = "merged:" + manifest.commitSha;
166+
var cachedMerged = await _idbGet(mergedKey);
167+
if (cachedMerged) {
168+
await ICS.db.initDB(cachedMerged);
169+
return;
170+
}
171+
163172
// 1) Load index: check if index SHA matches cached → reuse decrypted JSON;
164173
// otherwise fetch + decrypt + cache for next time.
165174
var cachedIndexSha = null;
@@ -193,6 +202,13 @@ async function _loadFromShardManifest(manifest, owner, repo, password, token, pr
193202
var shardBytes = await _loadShard(owner, repo, entry, password, token);
194203
await ICS.db.attachShard(shardBytes);
195204
}
205+
206+
// 3) Cache the merged DB in IndexedDB so next load with the same commit
207+
// SHA skips EVERYTHING — no index fetch, no shard iteration, no merge.
208+
try {
209+
var merged = ICS.db.exportDB();
210+
await _idbPut(mergedKey, merged);
211+
} catch (e) { /* non-critical — silently skip */ }
196212
}
197213

198214
async function _loadFromLegacyBlob(manifest, owner, repo, secrets, token) {

frontend/js/db.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@ async function _initEmpty() {
4747
_db.exec(_schemaSql());
4848
}
4949

50+
function _exportDB() {
51+
// Returns the full merged DB as Uint8Array (for caching in IndexedDB).
52+
if (!_db) throw new Error("Database not initialized");
53+
return _db.export();
54+
}
55+
5056
function _copyRows(src, dst, table) {
5157
const result = src.exec(`SELECT * FROM ${table}`);
5258
if (!result.length || !result[0].values.length) return;
@@ -199,6 +205,7 @@ window.ICS.db = {
199205
initDB: _initFromBytes,
200206
initEmpty: _initEmpty,
201207
attachShard: _attachShard,
208+
exportDB: _exportDB,
202209
getCourses: _getCourses,
203210
getLectures: _getLectures,
204211
getLecture: _getLecture,

0 commit comments

Comments
 (0)