-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Save and Load Analysis from IndexedDB (#57)
* feat: handler for managing indexed db operations * feat: ui changes to save and load from indexeddb * Promisify the Kana code. * Added reference counting for files. * Use separate store for the file reference count. * Shared files across multiple analyses in KanaDb. * Added MD5 sum to the file IDs to further guarantee uniqueness. * Asyncify a few lambdas for nicer viewing. * feat: db handler return records after save/deletion * feat: can now delete indexed db saved analysis * feat: set name after upload, fix issue when new dataset is chosen Co-authored-by: LTLA <infinite.monkeys.with.keyboards@gmail.com>
- Loading branch information
Showing
7 changed files
with
679 additions
and
118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,240 @@ | ||
var kana_db = {}; | ||
|
||
(function (x) { | ||
/** Private members **/ | ||
var kanaDB; | ||
var init = null; | ||
|
||
x.initialize = function () { | ||
init = new Promise(resolve => { | ||
// initialize database on worker creation | ||
kanaDB = indexedDB.open("KanaDB"); | ||
|
||
kanaDB.onupgradeneeded = (e) => { | ||
var kanaDBClient = e.target.result; | ||
kanaDBClient.createObjectStore("analysis", { keyPath: 'id' }); | ||
kanaDBClient.createObjectStore("analysis_files", { keyPath: 'id' }); | ||
kanaDBClient.createObjectStore("file", { keyPath: 'id' }); | ||
kanaDBClient.createObjectStore("file_ref_count", { keyPath: 'id' }); | ||
}; | ||
|
||
// Send existing stored analyses, if available. | ||
kanaDB.onsuccess = () => { | ||
getRecords(resolve); | ||
}; | ||
|
||
kanaDB.onerror = () => { | ||
resolve(null); | ||
}; | ||
}); | ||
|
||
return init; | ||
}; | ||
|
||
function getRecords(resolve) { | ||
var allAnalysis = kanaDB.result | ||
.transaction(["analysis"], "readonly") | ||
.objectStore("analysis").getAllKeys(); | ||
|
||
allAnalysis.onsuccess = function () { | ||
resolve(allAnalysis.result); | ||
}; | ||
allAnalysis.onerror = function () { | ||
resolve(null); | ||
}; | ||
} | ||
|
||
/** Helper functions **/ | ||
async function loadContent(id, store) { | ||
return new Promise(resolve => { | ||
let request = store.get(id); | ||
request.onsuccess = function () { | ||
if (request.result !== undefined) { | ||
resolve(request.result.payload); | ||
} else { | ||
resolve(null); | ||
} | ||
}; | ||
request.onerror = function () { | ||
resolve(null); | ||
}; | ||
}); | ||
} | ||
|
||
function allOK(promises) { | ||
return Promise.allSettled(promises) | ||
.then(vals => { | ||
for (const x of vals) { | ||
if (!x) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
}); | ||
} | ||
|
||
/** Functions to save content **/ | ||
x.saveFile = async function (id, buffer) { | ||
await init; | ||
let trans = kanaDB.result.transaction(["file", "file_ref_count"], "readwrite"); | ||
let file_store = trans.objectStore("file"); | ||
let ref_store = trans.objectStore("file_ref_count"); | ||
|
||
var refcount = await loadContent(id, ref_store); | ||
if (refcount === null) { | ||
refcount = 0; | ||
} | ||
refcount++; | ||
|
||
var data_saving = new Promise(resolve => { | ||
var putrequest = file_store.put({ "id": id, "payload": buffer }); | ||
putrequest.onsuccess = function (event) { | ||
resolve(true); | ||
}; | ||
putrequest.onerror = function (event) { | ||
resolve(false); | ||
}; | ||
}); | ||
|
||
var ref_saving = new Promise(resolve => { | ||
var putrequest = ref_store.put({ "id": id, "payload": refcount }); | ||
putrequest.onsuccess = function (event) { | ||
resolve(true); | ||
}; | ||
putrequest.onerror = function (event) { | ||
resolve(false); | ||
}; | ||
}); | ||
|
||
return allOK([data_saving, ref_saving]) | ||
}; | ||
|
||
x.saveAnalysis = async function (id, state, files) { | ||
await init; | ||
let trans = kanaDB.result.transaction(["analysis", "analysis_files"], "readwrite") | ||
let analysis_store = trans.objectStore("analysis"); | ||
let file_id_store = trans.objectStore("analysis_files"); | ||
|
||
var data_saving = new Promise(resolve => { | ||
var putrequest = analysis_store.put({ "id": id, "payload": state }); | ||
putrequest.onsuccess = function (event) { | ||
getRecords(resolve); | ||
}; | ||
putrequest.onerror = function (event) { | ||
resolve(false); | ||
}; | ||
}); | ||
|
||
var id_saving = new Promise(resolve => { | ||
var putrequest = file_id_store.put({ "id": id, "payload": files }); | ||
putrequest.onsuccess = function (event) { | ||
resolve(true); | ||
}; | ||
putrequest.onerror = function (event) { | ||
resolve(false); | ||
}; | ||
}); | ||
|
||
return allOK([data_saving, id_saving]) | ||
}; | ||
|
||
/** Functions to load content **/ | ||
x.loadFile = async function (id) { | ||
await init; | ||
let file_store = kanaDB.result | ||
.transaction(["file"], "readonly") | ||
.objectStore("file"); | ||
return loadContent(id, file_store); | ||
}; | ||
|
||
x.loadAnalysis = async function (id) { | ||
await init; | ||
let analysis_store = kanaDB.result | ||
.transaction(["analysis"], "readonly") | ||
.objectStore("analysis"); | ||
return loadContent(id, analysis_store); | ||
}; | ||
|
||
/** Functions to load content **/ | ||
x.removeFile = async function removeFile(id) { | ||
await init; | ||
let trans = kanaDB.result.transaction(["file", "file_ref_count"], "readwrite"); | ||
let file_store = trans.objectStore("file"); | ||
let ref_store = trans.objectStore("file_ref_count"); | ||
|
||
var promises = []; | ||
var refcount = await loadContent(id, file_ref_count); | ||
refcount--; | ||
|
||
if (refcount == 0) { | ||
promises.push(new Promise(resolve => { | ||
let request = file_store.remove(id); | ||
request.onerror = function (event) { | ||
resolve(false); | ||
}; | ||
request.onsuccess = function (event) { | ||
resolve(true); | ||
}; | ||
})); | ||
promises.push(new Promise(resolve => { | ||
let request = ref_store.delete(id); | ||
request.onerror = function (event) { | ||
resolve(false); | ||
}; | ||
request.onsuccess = function (event) { | ||
resolve(true); | ||
}; | ||
})) | ||
} else { | ||
promises.push(new Promise(resolve => { | ||
let request = ref_store.put({ "id": id, "payload": refcount }) | ||
request.onsuccess = function (event) { | ||
resolve(true); | ||
}; | ||
request.onerror = function (event) { | ||
resolve(false); | ||
}; | ||
})); | ||
} | ||
|
||
return allOK(promises); | ||
}; | ||
|
||
x.removeAnalysis = async function removeFile(id) { | ||
await init; | ||
let trans = kanaDB.result.transaction(["analysis", "analysis_files"], "readwrite") | ||
let analysis_store = trans.objectStore("analysis"); | ||
let file_id_store = trans.objectStore("analysis_files"); | ||
|
||
var promises = []; | ||
|
||
promises.push(new Promise(resolve => { | ||
let request = analysis_store.delete(id); | ||
request.onsuccess = function (event) { | ||
getRecords(resolve); | ||
}; | ||
request.onerror = function (event) { | ||
resolve(false); | ||
}; | ||
})); | ||
|
||
// Removing all files as well. | ||
var files = await loadContent(id, file_id_store); | ||
for (const f of files) { | ||
promises.push(x.removeFile(f)); | ||
} | ||
|
||
promises.push(new Promise(resolve => { | ||
let request = file_id_store.delete(id); | ||
request.onsuccess = function (event) { | ||
resolve(true); | ||
}; | ||
request.onerror = function (event) { | ||
resolve(false); | ||
}; | ||
})); | ||
|
||
return allOK(promises); | ||
}; | ||
|
||
})(kana_db); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.