@trap_stevo/star-vault
Version:
Deterministic data engine that eliminates query-time joins and enables normalized data execution. Architect secure, scalable, real-time systems with integrated sharding, encryption, and event-driven data flows. Manage hierarchical structures, execute adva
162 lines (161 loc) • 7.7 kB
JavaScript
"use strict";
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); }
function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } }
function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; }
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : i + ""; }
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
function _classPrivateMethodInitSpec(e, a) { _checkPrivateRedeclaration(e, a), a.add(e); }
function _classPrivateFieldInitSpec(e, t, a) { _checkPrivateRedeclaration(e, t), t.set(e, a); }
function _checkPrivateRedeclaration(e, t) { if (t.has(e)) throw new TypeError("Cannot initialize the same private elements twice on an object"); }
function _classPrivateFieldGet(s, a) { return s.get(_assertClassBrand(s, a)); }
function _classPrivateFieldSet(s, a, r) { return s.set(_assertClassBrand(s, a), r), r; }
function _assertClassBrand(e, t, n) { if ("function" == typeof e ? e === t : e.has(t)) return arguments.length < 3 ? t : n; throw new TypeError("Private element is not present on this object"); }
var _evictions = /*#__PURE__*/new WeakMap();
var _misses = /*#__PURE__*/new WeakMap();
var _limit = /*#__PURE__*/new WeakMap();
var _hits = /*#__PURE__*/new WeakMap();
var _map = /*#__PURE__*/new WeakMap();
var _StarCache_brand = /*#__PURE__*/new WeakSet();
var StarCache = /*#__PURE__*/function () {
function StarCache() {
var limit = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 100;
_classCallCheck(this, StarCache);
_classPrivateMethodInitSpec(this, _StarCache_brand);
_classPrivateFieldInitSpec(this, _evictions, void 0);
_classPrivateFieldInitSpec(this, _misses, void 0);
_classPrivateFieldInitSpec(this, _limit, void 0);
_classPrivateFieldInitSpec(this, _hits, void 0);
_classPrivateFieldInitSpec(this, _map, void 0);
_classPrivateFieldSet(_limit, this, Number.isInteger(limit) && limit > 0 ? limit : 100);
_classPrivateFieldSet(_map, this, new Map());
_classPrivateFieldSet(_evictions, this, 0);
_classPrivateFieldSet(_misses, this, 0);
_classPrivateFieldSet(_hits, this, 0);
}
return _createClass(StarCache, [{
key: "get",
value: function get(key) {
var _this$hits, _this$hits2;
if (!_classPrivateFieldGet(_map, this).has(key)) {
var _this$misses, _this$misses2;
_classPrivateFieldSet(_misses, this, (_this$misses = _classPrivateFieldGet(_misses, this), _this$misses2 = _this$misses++, _this$misses)), _this$misses2;
return null;
}
var value = _classPrivateFieldGet(_map, this).get(key);
_classPrivateFieldGet(_map, this)["delete"](key);
_classPrivateFieldGet(_map, this).set(key, value);
_classPrivateFieldSet(_hits, this, (_this$hits = _classPrivateFieldGet(_hits, this), _this$hits2 = _this$hits++, _this$hits)), _this$hits2;
return value;
}
}, {
key: "peek",
value: function peek(key) {
var _this$hits3, _this$hits4;
if (!_classPrivateFieldGet(_map, this).has(key)) {
var _this$misses3, _this$misses4;
_classPrivateFieldSet(_misses, this, (_this$misses3 = _classPrivateFieldGet(_misses, this), _this$misses4 = _this$misses3++, _this$misses3)), _this$misses4;
return null;
}
_classPrivateFieldSet(_hits, this, (_this$hits3 = _classPrivateFieldGet(_hits, this), _this$hits4 = _this$hits3++, _this$hits3)), _this$hits4;
return _classPrivateFieldGet(_map, this).get(key);
}
}, {
key: "set",
value: function set(key, value) {
if (_classPrivateFieldGet(_map, this).has(key)) {
_classPrivateFieldGet(_map, this)["delete"](key);
} else if (_classPrivateFieldGet(_map, this).size >= _classPrivateFieldGet(_limit, this)) {
var _this$evictions, _this$evictions2;
var oldestKey = _classPrivateFieldGet(_map, this).keys().next().value;
_classPrivateFieldGet(_map, this)["delete"](oldestKey);
_classPrivateFieldSet(_evictions, this, (_this$evictions = _classPrivateFieldGet(_evictions, this), _this$evictions2 = _this$evictions++, _this$evictions)), _this$evictions2;
}
_classPrivateFieldGet(_map, this).set(key, value);
}
}, {
key: "delete",
value: function _delete(key) {
return _assertClassBrand(_StarCache_brand, this, _deleteInternal).call(this, key);
}
}, {
key: "clear",
value: function clear() {
_classPrivateFieldGet(_map, this).clear();
}
}, {
key: "has",
value: function has(key) {
return _classPrivateFieldGet(_map, this).has(key);
}
}, {
key: "size",
value: function size() {
return _classPrivateFieldGet(_map, this).size;
}
}, {
key: "setCapacity",
value: function setCapacity(newLimit) {
var cap = Number(newLimit);
if (!Number.isInteger(cap) || cap <= 0) {
return this.getCapacity();
}
_classPrivateFieldSet(_limit, this, cap);
while (_classPrivateFieldGet(_map, this).size > _classPrivateFieldGet(_limit, this)) {
var _this$evictions3, _this$evictions4;
var oldestKey = _classPrivateFieldGet(_map, this).keys().next().value;
_classPrivateFieldGet(_map, this)["delete"](oldestKey);
_classPrivateFieldSet(_evictions, this, (_this$evictions3 = _classPrivateFieldGet(_evictions, this), _this$evictions4 = _this$evictions3++, _this$evictions3)), _this$evictions4;
}
return this.getCapacity();
}
}, {
key: "getCapacity",
value: function getCapacity() {
return _classPrivateFieldGet(_limit, this);
}
}, {
key: "stats",
value: function stats() {
return {
evictions: _classPrivateFieldGet(_evictions, this),
capacity: _classPrivateFieldGet(_limit, this),
misses: _classPrivateFieldGet(_misses, this),
size: _classPrivateFieldGet(_map, this).size,
hits: _classPrivateFieldGet(_hits, this)
};
}
}, {
key: "resetStats",
value: function resetStats() {
_classPrivateFieldSet(_evictions, this, 0);
_classPrivateFieldSet(_misses, this, 0);
_classPrivateFieldSet(_hits, this, 0);
return this.stats();
}
}, {
key: "keys",
value: function keys() {
return Array.from(_classPrivateFieldGet(_map, this).keys());
}
}, {
key: "values",
value: function values() {
return Array.from(_classPrivateFieldGet(_map, this).values());
}
}, {
key: "entries",
value: function entries() {
return Array.from(_classPrivateFieldGet(_map, this).entries());
}
}]);
}();
function _deleteInternal(key) {
if (!_classPrivateFieldGet(_map, this).has(key)) {
return false;
}
_classPrivateFieldGet(_map, this)["delete"](key);
return true;
}
;
module.exports = StarCache;