UNPKG

hazelcast-client

Version:

Hazelcast - open source In-Memory Data Grid - client for NodeJS

44 lines 1.7 kB
"use strict"; var Q = require('q'); var GetPartitionsCodec = require('./codec/GetPartitionsCodec'); var PartitionService = (function () { function PartitionService(client) { this.client = client; } PartitionService.prototype.initialize = function () { var _this = this; var deferred = Q.defer(); this.refresh().then(function () { deferred.resolve(_this); }); return deferred.promise; }; PartitionService.prototype.refresh = function () { var _this = this; var ownerConnection = this.client.getClusterService().getOwnerConnection(); var clientMessage = GetPartitionsCodec.encodeRequest(); return this.client.getInvocationService() .invokeOnConnection(ownerConnection, clientMessage) .then(function (clientMessage) { _this.partitionMap = GetPartitionsCodec.decodeResponse(clientMessage); _this.partitionCount = Object.keys(_this.partitionMap).length; }); }; ; PartitionService.prototype.getAddressForPartition = function (partitionId) { return this.partitionMap[partitionId]; }; PartitionService.prototype.getPartitionId = function (key) { var partitionHash; if (typeof key === 'object' && 'getPartitionHash' in key) { partitionHash = key.getPartitionHash(); } else { partitionHash = this.client.getSerializationService().toData(key).getPartitionHash(); } return Math.abs(partitionHash) % this.partitionCount; }; return PartitionService; }()); module.exports = PartitionService; //# sourceMappingURL=PartitionService.js.map