@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
320 lines (319 loc) • 15.9 kB
JavaScript
"use strict";
function _slicedToArray(r, e) { return _arrayWithHoles(r) || _iterableToArrayLimit(r, e) || _unsupportedIterableToArray(r, e) || _nonIterableRest(); }
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t["return"] && (u = t["return"](), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
function _arrayWithHoles(r) { if (Array.isArray(r)) return r; }
function _createForOfIteratorHelper(r, e) { var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (!t) { if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) { t && (r = t); var _n = 0, F = function F() {}; return { s: F, n: function n() { return _n >= r.length ? { done: !0 } : { done: !1, value: r[_n++] }; }, e: function e(r) { throw r; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var o, a = !0, u = !1; return { s: function s() { t = t.call(r); }, n: function n() { var r = t.next(); return a = r.done, r; }, e: function e(r) { u = !0, o = r; }, f: function f() { try { a || null == t["return"] || t["return"](); } finally { if (u) throw o; } } }; }
function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } }
function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; }
function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
function _defineProperty(e, r, t) { return (r = _toPropertyKey(r)) in e ? Object.defineProperty(e, r, { value: t, enumerable: !0, configurable: !0, writable: !0 }) : e[r] = t, e; }
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 _wal = /*#__PURE__*/new WeakMap();
var _enabled = /*#__PURE__*/new WeakMap();
var _serviceName = /*#__PURE__*/new WeakMap();
var _pid = /*#__PURE__*/new WeakMap();
var _maxKeys = /*#__PURE__*/new WeakMap();
var _sampleHitRate = /*#__PURE__*/new WeakMap();
var _byKey = /*#__PURE__*/new WeakMap();
var _lastKey = /*#__PURE__*/new WeakMap();
var _last = /*#__PURE__*/new WeakMap();
var _StarQueryMetrics_brand = /*#__PURE__*/new WeakSet();
var StarQueryMetrics = /*#__PURE__*/function () {
function StarQueryMetrics(wal) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
_classCallCheck(this, StarQueryMetrics);
_classPrivateMethodInitSpec(this, _StarQueryMetrics_brand);
_classPrivateFieldInitSpec(this, _wal, null);
_classPrivateFieldInitSpec(this, _enabled, true);
_classPrivateFieldInitSpec(this, _serviceName, "");
_classPrivateFieldInitSpec(this, _pid, 0);
_classPrivateFieldInitSpec(this, _maxKeys, 256);
_classPrivateFieldInitSpec(this, _sampleHitRate, 0);
_classPrivateFieldInitSpec(this, _byKey, null);
_classPrivateFieldInitSpec(this, _lastKey, "");
_classPrivateFieldInitSpec(this, _last, null);
_classPrivateFieldSet(_wal, this, wal || null);
_classPrivateFieldSet(_serviceName, this, String(options.serviceName || ""));
_classPrivateFieldSet(_pid, this, Number(options.pid || process.pid || 0) | 0);
if (options.enabled === false) {
_classPrivateFieldSet(_enabled, this, false);
}
if (Number.isFinite(options.maxKeys) && options.maxKeys > 0) {
_classPrivateFieldSet(_maxKeys, this, options.maxKeys | 0);
}
if (Number.isFinite(options.sampleHitRate) && options.sampleHitRate >= 0) {
var r = Number(options.sampleHitRate);
_classPrivateFieldSet(_sampleHitRate, this, r > 1 ? 1 : r);
}
_classPrivateFieldSet(_byKey, this, new Map());
}
return _createClass(StarQueryMetrics, [{
key: "setEnabled",
value: function setEnabled(enabled) {
_classPrivateFieldSet(_enabled, this, enabled === true);
return _classPrivateFieldGet(_enabled, this);
}
}, {
key: "setMaxKeys",
value: function setMaxKeys(n) {
if (Number.isFinite(n) && n > 0) {
_classPrivateFieldSet(_maxKeys, this, n | 0);
_assertClassBrand(_StarQueryMetrics_brand, this, _enforceMaxKeys).call(this);
}
return _classPrivateFieldGet(_maxKeys, this);
}
}, {
key: "setSampleHitRate",
value: function setSampleHitRate(rate) {
if (Number.isFinite(rate) && rate >= 0) {
var r = Number(rate);
_classPrivateFieldSet(_sampleHitRate, this, r > 1 ? 1 : r);
}
return _classPrivateFieldGet(_sampleHitRate, this);
}
}, {
key: "noteHit",
value: function noteHit(sourceID, collection, id, cache) {
var actionInfo = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : {};
if (_classPrivateFieldGet(_enabled, this) !== true) {
return;
}
var p = _assertClassBrand(_StarQueryMetrics_brand, this, _getOrCreate).call(this, sourceID, collection);
p.total = (p.total | 0) + 1 | 0;
if (cache === "dec-hit") {
p.decHit = (p.decHit | 0) + 1 | 0;
if (!p.example.decHit && _assertClassBrand(_StarQueryMetrics_brand, this, _shouldSampleHit).call(this)) {
p.example.decHit = _assertClassBrand(_StarQueryMetrics_brand, this, _makeExample).call(this, sourceID, collection, {
id: id,
exists: true,
cache: "dec-hit"
}, actionInfo);
}
} else {
p.diskHit = (p.diskHit | 0) + 1 | 0;
if (!p.example.diskHit && _assertClassBrand(_StarQueryMetrics_brand, this, _shouldSampleHit).call(this)) {
p.example.diskHit = _assertClassBrand(_StarQueryMetrics_brand, this, _makeExample).call(this, sourceID, collection, {
id: id,
exists: true,
cache: "disk"
}, actionInfo);
}
}
}
}, {
key: "noteMissImmediate",
value: function noteMissImmediate(sourceID, collection, id) {
var actionInfo = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
if (_classPrivateFieldGet(_enabled, this) !== true) {
return;
}
if (!_classPrivateFieldGet(_wal, this) || typeof _classPrivateFieldGet(_wal, this).logSystemAction !== "function") {
return;
}
var details = _assertClassBrand(_StarQueryMetrics_brand, this, _makeExample).call(this, sourceID, collection, {
id: id,
exists: false,
cache: "miss"
}, actionInfo);
_classPrivateFieldGet(_wal, this).logSystemAction(String(sourceID || "UNKNOWN"), String(collection || ""), details, actionInfo);
var p = _assertClassBrand(_StarQueryMetrics_brand, this, _getOrCreate).call(this, sourceID, collection);
p.total = (p.total | 0) + 1 | 0;
p.miss = (p.miss | 0) + 1 | 0;
if (!p.example.miss) {
p.example.miss = details;
}
}
}, {
key: "noteRun",
value: function noteRun(sourceID, collection, details) {
var actionInfo = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
if (_classPrivateFieldGet(_enabled, this) !== true) {
return;
}
var p = _assertClassBrand(_StarQueryMetrics_brand, this, _getOrCreate).call(this, sourceID, collection);
p.total = (p.total | 0) + 1 | 0;
p.run = (p.run | 0) + 1 | 0;
if (!p.example.run) {
p.example.run = _assertClassBrand(_StarQueryMetrics_brand, this, _makeExample).call(this, sourceID, collection, details || {
ran: true
}, actionInfo);
}
}
}, {
key: "flushNow",
value: function flushNow() {
_assertClassBrand(_StarQueryMetrics_brand, this, _flush).call(this, true);
}
}, {
key: "shutdown",
value: function shutdown() {
_assertClassBrand(_StarQueryMetrics_brand, this, _flush).call(this, true);
}
}]);
}();
function _shouldSampleHit() {
var r = _classPrivateFieldGet(_sampleHitRate, this);
if (r <= 0) {
return false;
}
if (r >= 1) {
return true;
}
return Math.random() < r;
}
function _key(sourceID, collection) {
var src = String(sourceID || "UNKNOWN").trim().toUpperCase();
var col = String(collection || "");
return "".concat(src, "::").concat(col);
}
function _getOrCreate(sourceID, collection) {
var key = _assertClassBrand(_StarQueryMetrics_brand, this, _key).call(this, sourceID, collection);
if (key === _classPrivateFieldGet(_lastKey, this) && _classPrivateFieldGet(_last, this)) {
return _classPrivateFieldGet(_last, this);
}
var map = _classPrivateFieldGet(_byKey, this);
var p = map.get(key);
if (p) {
_classPrivateFieldSet(_lastKey, this, key);
_classPrivateFieldSet(_last, this, p);
return p;
}
p = {
key: key,
sourceID: String(sourceID || "UNKNOWN").trim().toUpperCase(),
collection: String(collection || ""),
total: 0,
run: 0,
decHit: 0,
diskHit: 0,
miss: 0,
example: {
run: null,
decHit: null,
diskHit: null,
miss: null
}
};
map.set(key, p);
_classPrivateFieldSet(_lastKey, this, key);
_classPrivateFieldSet(_last, this, p);
_assertClassBrand(_StarQueryMetrics_brand, this, _enforceMaxKeys).call(this);
return p;
}
function _enforceMaxKeys() {
var max = _classPrivateFieldGet(_maxKeys, this) | 0;
if (max <= 0) {
return;
}
var map = _classPrivateFieldGet(_byKey, this);
if (map.size <= max) {
return;
}
var overflow = map.size - max;
if (overflow <= 0) {
return;
}
for (var i = 0; i < overflow; i++) {
var first = map.keys().next().value;
if (first == null) {
break;
}
map["delete"](first);
if (first === _classPrivateFieldGet(_lastKey, this)) {
_classPrivateFieldSet(_lastKey, this, "");
_classPrivateFieldSet(_last, this, null);
}
}
}
function _makeExample(sourceID, collection, details, actionInfo) {
var _ref, _info$transactionID, _ref2, _info$idempotencyKey;
var info = actionInfo && _typeof(actionInfo) === "object" ? actionInfo : {};
var tx = (_ref = (_info$transactionID = info.transactionID) !== null && _info$transactionID !== void 0 ? _info$transactionID : info.transactionId) !== null && _ref !== void 0 ? _ref : null;
var idem = (_ref2 = (_info$idempotencyKey = info.idempotencyKey) !== null && _info$idempotencyKey !== void 0 ? _info$idempotencyKey : info.idempotency) !== null && _ref2 !== void 0 ? _ref2 : null;
var out = _objectSpread({}, details && _typeof(details) === "object" ? details : {});
out.collection = String(collection || "");
if (tx != null || idem != null) {
out.meta = {
transactionID: tx !== null && tx !== void 0 ? tx : null,
idempotencyKey: idem !== null && idem !== void 0 ? idem : null
};
}
out.sourceID = String(sourceID || "UNKNOWN").trim().toUpperCase();
return out;
}
function _flush(force) {
if (_classPrivateFieldGet(_enabled, this) !== true) {
return;
}
if (!_classPrivateFieldGet(_wal, this) || typeof _classPrivateFieldGet(_wal, this).logSystemAction !== "function") {
return;
}
var map = _classPrivateFieldGet(_byKey, this);
if (!map || map.size === 0) {
if (force !== true) {
return;
}
return;
}
var _iterator = _createForOfIteratorHelper(map),
_step;
try {
for (_iterator.s(); !(_step = _iterator.n()).done;) {
var _step$value = _slicedToArray(_step.value, 2),
p = _step$value[1];
var total = p.total | 0;
if (total <= 0 && force !== true) {
continue;
}
var payload = {
source: "query-metrics",
sourceID: p.sourceID,
collection: p.collection,
service: _classPrivateFieldGet(_serviceName, this) || null,
pid: _classPrivateFieldGet(_pid, this) || null,
total: total,
run: p.run | 0,
decHit: p.decHit | 0,
diskHit: p.diskHit | 0,
miss: p.miss | 0,
example: {
run: p.example.run || null,
decHit: p.example.decHit || null,
diskHit: p.example.diskHit || null,
miss: p.example.miss || null
}
};
p.diskHit = 0;
p.decHit = 0;
p.total = 0;
p.miss = 0;
p.run = 0;
p.example = {
diskHit: null,
decHit: null,
miss: null,
run: null
};
_classPrivateFieldGet(_wal, this).logSystemAction(p.sourceID, "system", payload, {});
}
} catch (err) {
_iterator.e(err);
} finally {
_iterator.f();
}
}
;
module.exports = StarQueryMetrics;