mirror of
https://github.com/vladmandic/automatic
synced 2026-09-20 01:31:13 +02:00
Switch to folder-based database cleanup
This commit is contained in:
+58
-22
@@ -1,9 +1,12 @@
|
||||
let db;
|
||||
/**
|
||||
* @type {?IDBDatabase}
|
||||
*/
|
||||
let db = null;
|
||||
|
||||
async function initIndexDB() {
|
||||
async function createDB() {
|
||||
return new Promise((resolve, reject) => {
|
||||
const request = indexedDB.open('SDNext');
|
||||
const request = indexedDB.open('SDNext', 2);
|
||||
request.onerror = (evt) => reject(evt);
|
||||
request.onsuccess = (evt) => {
|
||||
db = evt.target.result;
|
||||
@@ -16,9 +19,15 @@ async function initIndexDB() {
|
||||
};
|
||||
request.onupgradeneeded = (evt) => {
|
||||
db = evt.target.result;
|
||||
const store = db.createObjectStore('thumbs', { keyPath: 'hash' });
|
||||
store.createIndex('hash', 'hash', { unique: true });
|
||||
const index = store.index('hash');
|
||||
const oldver = evt.oldVersion;
|
||||
if (oldver < 1) {
|
||||
const store = db.createObjectStore('thumbs', { keyPath: 'hash' });
|
||||
store.createIndex('hash', 'hash', { unique: true });
|
||||
}
|
||||
if (oldver < 2) {
|
||||
const existingStore = request.transaction.objectStore('thumbs');
|
||||
existingStore.createIndex('folder', 'folder', { unique: false });
|
||||
}
|
||||
resolve();
|
||||
};
|
||||
});
|
||||
@@ -75,54 +84,81 @@ async function put(record) {
|
||||
});
|
||||
}
|
||||
|
||||
async function idbGetAllKeys() {
|
||||
async function idbGetAllKeys(index = null, query = null) {
|
||||
if (!db) return null;
|
||||
return new Promise((resolve, reject) => {
|
||||
const request = db
|
||||
let request;
|
||||
const store = db
|
||||
.transaction('thumbs', 'readonly')
|
||||
.objectStore('thumbs')
|
||||
.getAllKeys();
|
||||
.objectStore('thumbs');
|
||||
if (index) {
|
||||
request = store.index(index).getAllKeys(query);
|
||||
} else {
|
||||
request = store.getAllKeys(query);
|
||||
}
|
||||
request.onsuccess = () => resolve(request.result);
|
||||
request.onerror = (evt) => reject(evt);
|
||||
});
|
||||
}
|
||||
|
||||
async function idbCount() {
|
||||
async function idbCount(folder = null) {
|
||||
if (!db) return null;
|
||||
return new Promise((resolve, reject) => {
|
||||
const request = db
|
||||
let request;
|
||||
const store = db
|
||||
.transaction('thumbs', 'readonly')
|
||||
.objectStore('thumbs')
|
||||
.count();
|
||||
.objectStore('thumbs');
|
||||
if (folder) {
|
||||
request = store.index('folder').count(folder);
|
||||
} else {
|
||||
request = store.count();
|
||||
}
|
||||
request.onsuccess = () => resolve(request.result);
|
||||
request.onerror = (evt) => reject(evt);
|
||||
});
|
||||
}
|
||||
|
||||
async function idbClean(keepSet, folder = null) {
|
||||
/**
|
||||
* @param {Set<string>} keepSet - Set containing the hashes of the current files in the folder.
|
||||
* @param {string} folder - Folder name/path
|
||||
* @param {updateMsgCallback} msgCallback - Callback for updating progress display
|
||||
*/
|
||||
async function idbClean(keepSet, folder, msgCallback) {
|
||||
if (!db) return null;
|
||||
if (!(keepSet instanceof Set)) {
|
||||
throw new TypeError('IndexedDB cleaning function must be given a Set() of hashes to keep');
|
||||
}
|
||||
if (folder === null) {
|
||||
if (!folder) {
|
||||
throw new Error('IndexedDB cleaning function must be told the current active folder');
|
||||
}
|
||||
const folderCached = new Set(await idbGetAllKeys('folder', folder));
|
||||
const removals = folderCached.difference(keepSet);
|
||||
const totalRemovals = removals.size;
|
||||
let counter = 0;
|
||||
return new Promise((resolve, reject) => {
|
||||
let counter = 0;
|
||||
const request = db
|
||||
const folderIndex = db
|
||||
.transaction('thumbs', 'readwrite')
|
||||
.objectStore('thumbs')
|
||||
.openCursor();
|
||||
.index('folder');
|
||||
const request = folderIndex.openCursor(folder);
|
||||
|
||||
request.onsuccess = (evt) => {
|
||||
const cursor = evt.target.result;
|
||||
if (cursor) {
|
||||
if (folder === cursor.value.folder && !keepSet.has(cursor.key)) {
|
||||
cursor.delete();
|
||||
if (removals.has(cursor.primaryKey)) {
|
||||
counter++;
|
||||
cursor.delete();
|
||||
}
|
||||
if (counter === totalRemovals) {
|
||||
resolve(counter); // Got lucky with element order and can stop early
|
||||
} else {
|
||||
if (counter % 100 === 0 && counter !== 0) {
|
||||
msgCallback(Math.floor((counter / totalRemovals) * 100));
|
||||
}
|
||||
cursor.continue();
|
||||
}
|
||||
cursor.continue();
|
||||
} else {
|
||||
resolve(counter, folder);
|
||||
resolve(counter);
|
||||
}
|
||||
};
|
||||
request.onerror = (evt) => reject(evt);
|
||||
|
||||
Reference in New Issue
Block a user