UNPKG

globalstorage

Version:

Global Storage is a Global Distributed Data Warehouse

181 lines (137 loc) 6.17 kB
'use strict'; function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); } function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); } function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); } var _require = require('@metarhia/common'), Uint64 = _require.Uint64, sequence = _require.sequence; var _require2 = require('./provider'), StorageProvider = _require2.StorageProvider; var RemoteServer = /*#__PURE__*/ function (_StorageProvider) { _inherits(RemoteServer, _StorageProvider); function RemoteServer(_ref, providerOptions) { var _this; var host = _ref.host, ports = _ref.ports; _classCallCheck(this, RemoteServer); _this = _possibleConstructorReturn(this, _getPrototypeOf(RemoteServer).call(this, providerOptions)); _this.host = host; _this.ports = ports.length > 1 ? sequence(ports, 0) : ports; _this.pool = []; return _this; } return RemoteServer; }(StorageProvider); var SYSTEM_BITMASK_SIZE = 24; var SYSTEM_BITMASK = new Uint64(1).shiftLeft(SYSTEM_BITMASK_SIZE).dec(); var getTreeDepth = function getTreeDepth(node) { var depth = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0; if (node[0] && node[1]) { return Math.max(getTreeDepth(node[0], depth + 1), getTreeDepth(node[1], depth + 1)); } return depth; }; var compressTree = function compressTree(tree) { var result = {}; var parseTree = function parseTree(index, depth, node) { var isLeaf = !node[0] || !node[1]; if (isLeaf) { result[index] = node; return; } parseTree(index, depth + 1, node[0]); parseTree(index + (1 << depth), depth + 1, node[1]); }; parseTree(0, 0, tree); return result; }; var buildIndex = function buildIndex(tree, depth, serverIds) { var result = new Array(Math.pow(2, depth)); var parseTree = function parseTree(index, depth, node) { var isLeaf = !node[0] || !node[1]; if (isLeaf) { var serverSuffix = new Uint64(index).shiftLeft(SYSTEM_BITMASK_SIZE).or(serverIds.systemSuffix); var serverBitmask = new Uint64(1).shiftLeft(depth).dec().shiftLeft(SYSTEM_BITMASK_SIZE).or(SYSTEM_BITMASK); result[index] = new RemoteServer(node, Object.assign({}, serverIds, { serverSuffix: serverSuffix, serverBitmask: serverBitmask })); for (var i = 1;; i++) { var refIndex = i << depth | index; if (refIndex >= result.length) return; result[refIndex] = result[index]; } } parseTree(index, depth + 1, node[0]); parseTree(index + (1 << depth), depth + 1, node[1]); }; parseTree(0, 0, tree); return result; }; var ServerTree = /*#__PURE__*/ function () { function ServerTree(tree, systemId) { _classCallCheck(this, ServerTree); this.tree = tree; this.compressedTree = compressTree(tree); this.depth = getTreeDepth(this.tree); this.serverBitmask = new Uint64(1).shiftLeft(this.depth).dec(); this.treeIndex = buildIndex(this.tree, this.depth, { systemSuffix: new Uint64(systemId), systemBitmask: new Uint64(SYSTEM_BITMASK) }); } _createClass(ServerTree, [{ key: "findServer", value: function findServer(id) { return this.treeIndex[Uint64.shiftRight(id, SYSTEM_BITMASK_SIZE).and(this.serverBitmask).toUint32()]; } }, { key: "toJSON", value: function toJSON() { return this.compressedTree; } }]); return ServerTree; }(); var SystemList = /*#__PURE__*/ function () { function SystemList(systems) { _classCallCheck(this, SystemList); this.systems = {}; for (var id in systems) { this.systems[id] = new ServerTree(systems[id], new Uint64(id)); } } _createClass(SystemList, [{ key: "findServer", value: function findServer(id) { var serverTree = this.systems[Uint64.and(SYSTEM_BITMASK).toUint32()]; if (!serverTree) { return null; } return serverTree.findServer(id); } }, { key: "toJSON", value: function toJSON() { return this.systems; } }]); return SystemList; }(); module.exports = { RemoteServer: RemoteServer, ServerTree: ServerTree, SystemList: SystemList };