2020-11-06 22:44:16 +05:00
|
|
|
import Storage from "./storage";
|
|
|
|
|
import Indexer from "./indexer";
|
2020-11-09 10:30:33 +05:00
|
|
|
import SparkMD5 from "spark-md5";
|
2020-11-06 22:44:16 +05:00
|
|
|
|
|
|
|
|
export default class PersistentCachedMap {
|
|
|
|
|
/**
|
|
|
|
|
*
|
|
|
|
|
* @param {string} key
|
|
|
|
|
* @param {Storage} storage
|
|
|
|
|
*/
|
|
|
|
|
constructor(key, storage) {
|
|
|
|
|
this.key = key;
|
|
|
|
|
this.indexer = new Indexer(storage, key);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async init() {
|
|
|
|
|
await this.indexer.init();
|
|
|
|
|
const data = await this.indexer.readMulti(this.indexer.indices);
|
|
|
|
|
this.map = new Map(data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async set(key, value) {
|
2020-11-09 10:30:33 +05:00
|
|
|
key = SparkMD5.hash(key);
|
2020-11-06 22:44:16 +05:00
|
|
|
this.map.set(key, value);
|
|
|
|
|
await this.indexer.write(key, value);
|
|
|
|
|
await this.indexer.index(key);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async delete(key) {
|
|
|
|
|
await this.indexer.remove(key);
|
|
|
|
|
await this.indexer.deindex(key);
|
|
|
|
|
return this.map.delete(key);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
get(key) {
|
2020-11-09 10:30:33 +05:00
|
|
|
key = SparkMD5.hash(key);
|
2020-11-06 22:44:16 +05:00
|
|
|
return this.map.get(key);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
has(key) {
|
2020-11-09 10:30:33 +05:00
|
|
|
key = SparkMD5.hash(key);
|
2020-11-06 22:44:16 +05:00
|
|
|
return this.map.has(key);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async clear() {
|
|
|
|
|
await this.indexer.clear();
|
|
|
|
|
this.map.clear();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
values() {
|
|
|
|
|
return this.map.values();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
forEach(callbackFn) {
|
|
|
|
|
return this.map.forEach(callbackFn);
|
|
|
|
|
}
|
|
|
|
|
}
|