flexmonster-mongo-connector
Version:
Custom data source API implementation for MongoDB
67 lines • 2.28 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Register = void 0;
class Register {
constructor() {
this._storage = null;
this._capacity = 0;
this._delay = 5 * 60 * 60 * 1000;
this._startingCapacity = 500;
this._storage = new Map();
this._capacity = this._startingCapacity;
}
addItem(key, item) {
if (item == null || key == null)
throw new Error("Illegal argument exception");
this._storage.set(key, { element: item, lastProcessed: new Date().getTime() });
if (this._storage.size == this._capacity) {
this.clearOutdated();
this.changeCapacity(this._capacity * 2);
}
}
hasItem(key) {
if (key == null)
throw new Error("Illegal argument exception");
return this._storage.has(key);
}
getItem(key) {
if (key == null)
throw new Error("Illegal argument exception");
return this._storage.get(key).element;
}
deleteItem(key) {
if (key == null)
throw new Error("Illegal argument exception");
let apiRequest = null;
if (this._storage.has(key)) {
apiRequest = this._storage.get(key).element;
this._storage.delete(key);
}
if (this._storage.size < this._capacity / 4) {
this.clearOutdated();
const newCapacity = (this._capacity / 4 < this._startingCapacity) ? this._startingCapacity : this._capacity / 4;
this.changeCapacity(newCapacity);
}
return apiRequest;
}
isEmpty() {
return this._storage.size == 0;
}
clearOutdated() {
const keysForDelete = [];
const currentTime = new Date().getTime();
this._storage.forEach((value, key, map) => {
if (currentTime - value.lastProcessed > this._delay) {
keysForDelete.push(key);
}
});
keysForDelete.forEach((value) => {
this._storage.delete(value);
});
}
changeCapacity(value) {
this._capacity = value;
}
}
exports.Register = Register;
//# sourceMappingURL=Register.js.map