hazelcast-client
Version:
Hazelcast - open source In-Memory Data Grid - client for NodeJS
443 lines • 23 kB
JavaScript
"use strict";
var __extends = (this && this.__extends) || function (d, b) {
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
var BaseProxy_1 = require('./BaseProxy');
var Promise = require('bluebird');
var MapPutCodec_1 = require('../codec/MapPutCodec');
var MapGetCodec_1 = require('../codec/MapGetCodec');
var MapClearCodec_1 = require('../codec/MapClearCodec');
var MapSizeCodec_1 = require('../codec/MapSizeCodec');
var MapRemoveCodec_1 = require('../codec/MapRemoveCodec');
var MapRemoveIfSameCodec_1 = require('../codec/MapRemoveIfSameCodec');
var MapContainsKeyCodec_1 = require('../codec/MapContainsKeyCodec');
var MapContainsValueCodec_1 = require('../codec/MapContainsValueCodec');
var MapIsEmptyCodec_1 = require('../codec/MapIsEmptyCodec');
var MapPutAllCodec_1 = require('../codec/MapPutAllCodec');
var MapDeleteCodec_1 = require('../codec/MapDeleteCodec');
var MapEntrySetCodec_1 = require('../codec/MapEntrySetCodec');
var MapEvictCodec_1 = require('../codec/MapEvictCodec');
var MapEvictAllCodec_1 = require('../codec/MapEvictAllCodec');
var MapFlushCodec_1 = require('../codec/MapFlushCodec');
var MapLockCodec_1 = require('../codec/MapLockCodec');
var MapIsLockedCodec_1 = require('../codec/MapIsLockedCodec');
var MapUnlockCodec_1 = require('../codec/MapUnlockCodec');
var MapForceUnlockCodec_1 = require('../codec/MapForceUnlockCodec');
var MapKeySetCodec_1 = require('../codec/MapKeySetCodec');
var MapLoadAllCodec_1 = require('../codec/MapLoadAllCodec');
var MapPutIfAbsentCodec_1 = require('../codec/MapPutIfAbsentCodec');
var MapPutTransientCodec_1 = require('../codec/MapPutTransientCodec');
var MapReplaceCodec_1 = require('../codec/MapReplaceCodec');
var MapReplaceIfSameCodec_1 = require('../codec/MapReplaceIfSameCodec');
var MapSetCodec_1 = require('../codec/MapSetCodec');
var MapValuesCodec_1 = require('../codec/MapValuesCodec');
var MapLoadGivenKeysCodec_1 = require('../codec/MapLoadGivenKeysCodec');
var MapGetAllCodec_1 = require('../codec/MapGetAllCodec');
var MapGetEntryViewCodec_1 = require('../codec/MapGetEntryViewCodec');
var MapAddIndexCodec_1 = require('../codec/MapAddIndexCodec');
var MapTryLockCodec_1 = require('../codec/MapTryLockCodec');
var MapTryPutCodec_1 = require('../codec/MapTryPutCodec');
var MapTryRemoveCodec_1 = require('../codec/MapTryRemoveCodec');
var MapAddEntryListenerCodec_1 = require('../codec/MapAddEntryListenerCodec');
var EntryEventType_1 = require('../core/EntryEventType');
var MapAddEntryListenerToKeyCodec_1 = require('../codec/MapAddEntryListenerToKeyCodec');
var MapRemoveEntryListenerCodec_1 = require('../codec/MapRemoveEntryListenerCodec');
var Util_1 = require('../Util');
var Predicate_1 = require('../core/Predicate');
var MapEntriesWithPredicateCodec_1 = require('../codec/MapEntriesWithPredicateCodec');
var MapKeySetWithPredicateCodec_1 = require('../codec/MapKeySetWithPredicateCodec');
var MapValuesWithPredicateCodec_1 = require('../codec/MapValuesWithPredicateCodec');
var MapAddEntryListenerToKeyWithPredicateCodec_1 = require('../codec/MapAddEntryListenerToKeyWithPredicateCodec');
var MapAddEntryListenerWithPredicateCodec_1 = require('../codec/MapAddEntryListenerWithPredicateCodec');
var DefaultPredicates_1 = require('../serialization/DefaultPredicates');
var MapValuesWithPagingPredicateCodec_1 = require('../codec/MapValuesWithPagingPredicateCodec');
var MapKeySetWithPagingPredicateCodec_1 = require('../codec/MapKeySetWithPagingPredicateCodec');
var MapProxy = (function (_super) {
__extends(MapProxy, _super);
function MapProxy() {
_super.apply(this, arguments);
}
MapProxy.prototype.entrySetWithPredicate = function (predicate) {
Util_1.assertNotNull(predicate);
var toObject = this.toObject.bind(this);
if (predicate instanceof DefaultPredicates_1.PagingPredicate) {
predicate.setIterationType(Predicate_1.IterationType.ENTRY);
var pData = this.toData(predicate);
return this.encodeInvokeOnRandomTarget(MapValuesWithPagingPredicateCodec_1.MapValuesWithPagingPredicateCodec, pData).then(function (rawValues) {
var deserValues = rawValues.map(function (ite) {
return [toObject(ite[0]), toObject(ite[1])];
});
return Util_1.getSortedQueryResultSet(deserValues, predicate);
});
}
else {
var pData = this.toData(predicate);
var deserializedSet = [];
return this.encodeInvokeOnRandomTarget(MapEntriesWithPredicateCodec_1.MapEntriesWithPredicateCodec, pData).then(function (entrySet) {
entrySet.forEach(function (entry) {
deserializedSet.push([toObject(entry[0]), toObject(entry[1])]);
});
return deserializedSet;
});
}
};
MapProxy.prototype.keySetWithPredicate = function (predicate) {
Util_1.assertNotNull(predicate);
var toObject = this.toObject.bind(this);
if (predicate instanceof DefaultPredicates_1.PagingPredicate) {
predicate.setIterationType(Predicate_1.IterationType.KEY);
var predData = this.toData(predicate);
return this.encodeInvokeOnRandomTarget(MapKeySetWithPagingPredicateCodec_1.MapKeySetWithPagingPredicateCodec, predData).then(function (rawValues) {
var deserValues = rawValues.map(function (ite) {
return [toObject(ite), null];
});
return Util_1.getSortedQueryResultSet(deserValues, predicate);
});
}
else {
var predicateData = this.toData(predicate);
return this.encodeInvokeOnRandomTarget(MapKeySetWithPredicateCodec_1.MapKeySetWithPredicateCodec, predicateData).then(function (entrySet) {
return entrySet.map(toObject);
});
}
};
MapProxy.prototype.valuesWithPredicate = function (predicate) {
Util_1.assertNotNull(predicate);
var toObject = this.toObject.bind(this);
if (predicate instanceof DefaultPredicates_1.PagingPredicate) {
predicate.setIterationType(Predicate_1.IterationType.VALUE);
var predData = this.toData(predicate);
return this.encodeInvokeOnRandomTarget(MapValuesWithPagingPredicateCodec_1.MapValuesWithPagingPredicateCodec, predData).then(function (rawValues) {
var deserValues = rawValues.map(function (ite) {
return [toObject(ite[0]), toObject(ite[1])];
});
return Util_1.getSortedQueryResultSet(deserValues, predicate);
});
}
else {
var predicateData = this.toData(predicate);
return this.encodeInvokeOnRandomTarget(MapValuesWithPredicateCodec_1.MapValuesWithPredicateCodec, predicateData).then(function (rawValues) {
return rawValues.map(toObject);
});
}
};
MapProxy.prototype.addEntryListenerWithPredicate = function (listener, predicate, key, includeValue) {
if (key === void 0) { key = undefined; }
if (includeValue === void 0) { includeValue = undefined; }
return this.addEntryListenerInternal(listener, predicate, key, includeValue);
};
MapProxy.prototype.containsKey = function (key) {
Util_1.assertNotNull(key);
var keyData = this.toData(key);
return this.encodeInvokeOnKey(MapContainsKeyCodec_1.MapContainsKeyCodec, keyData, keyData, 0);
};
MapProxy.prototype.containsValue = function (value) {
Util_1.assertNotNull(value);
var valueData = this.toData(value);
return this.encodeInvokeOnRandomTarget(MapContainsValueCodec_1.MapContainsValueCodec, valueData);
};
MapProxy.prototype.put = function (key, value, ttl) {
if (ttl === void 0) { ttl = -1; }
Util_1.assertNotNull(key);
Util_1.assertNotNull(value);
var keyData = this.toData(key);
var valueData = this.toData(value);
return this.encodeInvokeOnKey(MapPutCodec_1.MapPutCodec, keyData, keyData, valueData, 0, ttl);
};
MapProxy.prototype.putAll = function (pairs) {
var partitionService = this.client.getPartitionService();
var partitionsToKeys = {};
var pair;
var pairId;
for (pairId in pairs) {
pair = pairs[pairId];
var keyData = this.toData(pair[0]);
var pId = partitionService.getPartitionId(keyData);
if (!partitionsToKeys[pId]) {
partitionsToKeys[pId] = [];
}
partitionsToKeys[pId].push([keyData, this.toData(pair[1])]);
}
var partitionPromises = [];
for (var partition in partitionsToKeys) {
partitionPromises.push(this.encodeInvokeOnPartition(MapPutAllCodec_1.MapPutAllCodec, Number(partition), partitionsToKeys[partition]));
}
return Promise.all(partitionPromises).then(function () { return; });
};
MapProxy.prototype.get = function (key) {
Util_1.assertNotNull(key);
var keyData = this.toData(key);
return this.encodeInvokeOnKey(MapGetCodec_1.MapGetCodec, keyData, keyData, 0);
};
MapProxy.prototype.remove = function (key, value) {
if (value === void 0) { value = null; }
Util_1.assertNotNull(key);
var keyData = this.toData(key);
if (value == null) {
return this.encodeInvokeOnKey(MapRemoveCodec_1.MapRemoveCodec, keyData, keyData, 0);
}
else {
var valueData = this.toData(value);
return this.encodeInvokeOnKey(MapRemoveIfSameCodec_1.MapRemoveIfSameCodec, keyData, keyData, valueData, 0);
}
};
MapProxy.prototype.size = function () {
return this.encodeInvokeOnRandomTarget(MapSizeCodec_1.MapSizeCodec);
};
MapProxy.prototype.clear = function () {
return this.encodeInvokeOnRandomTarget(MapClearCodec_1.MapClearCodec);
};
MapProxy.prototype.isEmpty = function () {
return this.encodeInvokeOnRandomTarget(MapIsEmptyCodec_1.MapIsEmptyCodec);
};
MapProxy.prototype.getAll = function (keys) {
var partitionService = this.client.getPartitionService();
var partitionsToKeys = {};
var key;
for (var i in keys) {
key = keys[i];
var keyData = this.toData(key);
var pId = partitionService.getPartitionId(keyData);
if (!partitionsToKeys[pId]) {
partitionsToKeys[pId] = [];
}
partitionsToKeys[pId].push(keyData);
}
var partitionPromises = [];
for (var partition in partitionsToKeys) {
partitionPromises.push(this.encodeInvokeOnPartition(MapGetAllCodec_1.MapGetAllCodec, Number(partition), partitionsToKeys[partition]));
}
var toObject = this.toObject.bind(this);
var deserializeEntry = function (entry) {
return [toObject(entry[0]), toObject(entry[1])];
};
return Promise.all(partitionPromises).then(function (serializedEntryArrayArray) {
return Array.prototype.concat.apply([], serializedEntryArrayArray).map(deserializeEntry);
});
};
MapProxy.prototype.delete = function (key) {
Util_1.assertNotNull(key);
var keyData = this.toData(key);
return this.encodeInvokeOnKey(MapDeleteCodec_1.MapDeleteCodec, keyData, keyData, 0);
};
MapProxy.prototype.entrySet = function () {
var deserializedSet = [];
var toObject = this.toObject.bind(this);
return this.encodeInvokeOnRandomTarget(MapEntrySetCodec_1.MapEntrySetCodec).then(function (entrySet) {
entrySet.forEach(function (entry) {
deserializedSet.push([toObject(entry[0]), toObject(entry[1])]);
});
return deserializedSet;
});
};
MapProxy.prototype.evict = function (key) {
Util_1.assertNotNull(key);
var keyData = this.toData(key);
return this.encodeInvokeOnKey(MapEvictCodec_1.MapEvictCodec, keyData, keyData, 0);
};
MapProxy.prototype.evictAll = function () {
return this.encodeInvokeOnRandomTarget(MapEvictAllCodec_1.MapEvictAllCodec);
};
MapProxy.prototype.flush = function () {
return this.encodeInvokeOnRandomTarget(MapFlushCodec_1.MapFlushCodec);
};
MapProxy.prototype.lock = function (key, ttl) {
if (ttl === void 0) { ttl = -1; }
Util_1.assertNotNull(key);
var keyData = this.toData(key);
return this.encodeInvokeOnKey(MapLockCodec_1.MapLockCodec, keyData, keyData, 0, ttl, 0);
};
MapProxy.prototype.isLocked = function (key) {
Util_1.assertNotNull(key);
var keyData = this.toData(key);
return this.encodeInvokeOnKey(MapIsLockedCodec_1.MapIsLockedCodec, keyData, keyData);
};
MapProxy.prototype.unlock = function (key) {
Util_1.assertNotNull(key);
var keyData = this.toData(key);
return this.encodeInvokeOnKey(MapUnlockCodec_1.MapUnlockCodec, keyData, keyData, 0, 0);
};
MapProxy.prototype.forceUnlock = function (key) {
Util_1.assertNotNull(key);
var keyData = this.toData(key);
return this.encodeInvokeOnKey(MapForceUnlockCodec_1.MapForceUnlockCodec, keyData, keyData, 0);
};
MapProxy.prototype.keySet = function () {
var toObject = this.toObject.bind(this);
return this.encodeInvokeOnRandomTarget(MapKeySetCodec_1.MapKeySetCodec).then(function (entrySet) {
return entrySet.map(toObject);
});
};
MapProxy.prototype.loadAll = function (keys, replaceExistingValues) {
if (keys === void 0) { keys = null; }
if (replaceExistingValues === void 0) { replaceExistingValues = true; }
Util_1.assertNotNull(keys);
if (keys == null) {
return this.encodeInvokeOnRandomTarget(MapLoadAllCodec_1.MapLoadAllCodec, replaceExistingValues);
}
else {
var toData = this.toData.bind(this);
var keysData = keys.map(toData);
return this.encodeInvokeOnRandomTarget(MapLoadGivenKeysCodec_1.MapLoadGivenKeysCodec, keysData, replaceExistingValues);
}
};
MapProxy.prototype.putIfAbsent = function (key, value, ttl) {
if (ttl === void 0) { ttl = -1; }
Util_1.assertNotNull(key);
Util_1.assertNotNull(value);
var keyData = this.toData(key);
var valueData = this.toData(value);
return this.encodeInvokeOnKey(MapPutIfAbsentCodec_1.MapPutIfAbsentCodec, keyData, keyData, valueData, 0, ttl);
};
MapProxy.prototype.putTransient = function (key, value, ttl) {
if (ttl === void 0) { ttl = -1; }
Util_1.assertNotNull(key);
Util_1.assertNotNull(value);
var keyData = this.toData(key);
var valueData = this.toData(value);
return this.encodeInvokeOnKey(MapPutTransientCodec_1.MapPutTransientCodec, keyData, keyData, valueData, 0, ttl);
};
MapProxy.prototype.replace = function (key, newValue) {
Util_1.assertNotNull(key);
Util_1.assertNotNull(newValue);
var keyData = this.toData(key);
var newValueData = this.toData(newValue);
return this.encodeInvokeOnKey(MapReplaceCodec_1.MapReplaceCodec, keyData, keyData, newValueData, 0);
};
MapProxy.prototype.replaceIfSame = function (key, oldValue, newValue) {
Util_1.assertNotNull(key);
Util_1.assertNotNull(oldValue);
Util_1.assertNotNull(newValue);
var keyData = this.toData(key);
var newValueData = this.toData(newValue);
var oldValueData = this.toData(oldValue);
return this.encodeInvokeOnKey(MapReplaceIfSameCodec_1.MapReplaceIfSameCodec, keyData, keyData, oldValueData, newValueData, 0);
};
MapProxy.prototype.set = function (key, value, ttl) {
if (ttl === void 0) { ttl = -1; }
Util_1.assertNotNull(key);
Util_1.assertNotNull(value);
var keyData = this.toData(key);
var valueData = this.toData(value);
return this.encodeInvokeOnKey(MapSetCodec_1.MapSetCodec, keyData, keyData, valueData, 0, ttl);
};
MapProxy.prototype.values = function () {
var toObject = this.toObject.bind(this);
return this.encodeInvokeOnRandomTarget(MapValuesCodec_1.MapValuesCodec).then(function (valuesData) {
return valuesData.map(toObject);
});
};
MapProxy.prototype.getEntryView = function (key) {
Util_1.assertNotNull(key);
var keyData = this.toData(key);
return this.encodeInvokeOnKey(MapGetEntryViewCodec_1.MapGetEntryViewCodec, keyData, keyData, 0);
};
MapProxy.prototype.addIndex = function (attribute, ordered) {
return this.encodeInvokeOnRandomTarget(MapAddIndexCodec_1.MapAddIndexCodec, attribute, ordered);
};
MapProxy.prototype.tryLock = function (key, timeout, lease) {
if (timeout === void 0) { timeout = 0; }
if (lease === void 0) { lease = -1; }
Util_1.assertNotNull(key);
var keyData = this.toData(key);
return this.encodeInvokeOnKey(MapTryLockCodec_1.MapTryLockCodec, keyData, keyData, 0, lease, timeout, 0);
};
MapProxy.prototype.tryPut = function (key, value, timeout) {
Util_1.assertNotNull(key);
Util_1.assertNotNull(value);
var keyData = this.toData(key);
var valueData = this.toData(value);
return this.encodeInvokeOnKey(MapTryPutCodec_1.MapTryPutCodec, keyData, keyData, valueData, value, 0, timeout);
};
MapProxy.prototype.tryRemove = function (key, timeout) {
Util_1.assertNotNull(key);
var keyData = this.toData(key);
return this.encodeInvokeOnKey(MapTryRemoveCodec_1.MapTryRemoveCodec, keyData, keyData, 0, timeout);
};
MapProxy.prototype.addEntryListenerInternal = function (listener, predicate, key, includeValue) {
var flags = null;
var conversionTable = {
'added': EntryEventType_1.EntryEventType.ADDED,
'removed': EntryEventType_1.EntryEventType.REMOVED,
'updated': EntryEventType_1.EntryEventType.UPDATED,
'merged': EntryEventType_1.EntryEventType.MERGED,
'evicted': EntryEventType_1.EntryEventType.EVICTED,
'evictedAll': EntryEventType_1.EntryEventType.EVICT_ALL,
'clearedAll': EntryEventType_1.EntryEventType.CLEAR_ALL
};
for (var funcName in conversionTable) {
if (listener[funcName]) {
flags = flags | conversionTable[funcName];
}
}
var toObject = this.toObject.bind(this);
var entryEventHandler = function (key, val, oldVal, mergingVal, event, uuid, numberOfAffectedEntries) {
var eventParams = [key, oldVal, val, mergingVal, numberOfAffectedEntries, uuid];
eventParams = eventParams.map(toObject);
switch (event) {
case EntryEventType_1.EntryEventType.ADDED:
listener.added.apply(null, eventParams);
break;
case EntryEventType_1.EntryEventType.REMOVED:
listener.removed.apply(null, eventParams);
break;
case EntryEventType_1.EntryEventType.UPDATED:
listener.updated.apply(null, eventParams);
break;
case EntryEventType_1.EntryEventType.EVICTED:
listener.evicted.apply(null, eventParams);
break;
case EntryEventType_1.EntryEventType.EVICT_ALL:
listener.evictedAll.apply(null, eventParams);
break;
case EntryEventType_1.EntryEventType.CLEAR_ALL:
listener.clearedAll.apply(null, eventParams);
break;
case EntryEventType_1.EntryEventType.MERGED:
listener.merged.apply(null, eventParams);
break;
}
};
var request;
var handler;
var responser;
if (key && predicate) {
var keyData = this.toData(key);
var predicateData = this.toData(predicate);
request = MapAddEntryListenerToKeyWithPredicateCodec_1.MapAddEntryListenerToKeyWithPredicateCodec.encodeRequest(this.name, keyData, predicateData, includeValue, flags, false);
handler = MapAddEntryListenerToKeyWithPredicateCodec_1.MapAddEntryListenerToKeyWithPredicateCodec.handle;
responser = MapAddEntryListenerToKeyWithPredicateCodec_1.MapAddEntryListenerToKeyWithPredicateCodec.decodeResponse;
}
else if (key && !predicate) {
var keyData = this.toData(key);
request = MapAddEntryListenerToKeyCodec_1.MapAddEntryListenerToKeyCodec.encodeRequest(this.name, keyData, includeValue, flags, false);
handler = MapAddEntryListenerToKeyCodec_1.MapAddEntryListenerToKeyCodec.handle;
responser = MapAddEntryListenerToKeyCodec_1.MapAddEntryListenerToKeyCodec.decodeResponse;
}
else if (!key && predicate) {
var predicateData = this.toData(predicate);
request = MapAddEntryListenerWithPredicateCodec_1.MapAddEntryListenerWithPredicateCodec.encodeRequest(this.name, predicateData, includeValue, flags, false);
handler = MapAddEntryListenerWithPredicateCodec_1.MapAddEntryListenerWithPredicateCodec.handle;
responser = MapAddEntryListenerWithPredicateCodec_1.MapAddEntryListenerWithPredicateCodec.decodeResponse;
}
else {
request = MapAddEntryListenerCodec_1.MapAddEntryListenerCodec.encodeRequest(this.name, includeValue, flags, false);
handler = MapAddEntryListenerCodec_1.MapAddEntryListenerCodec.handle;
responser = MapAddEntryListenerCodec_1.MapAddEntryListenerCodec.decodeResponse;
}
return this.client.getListenerService().registerListener(request, function (m) { handler(m, entryEventHandler, toObject); }, responser);
};
MapProxy.prototype.addEntryListener = function (listener, key, includeValue) {
if (key === void 0) { key = undefined; }
if (includeValue === void 0) { includeValue = false; }
return this.addEntryListenerInternal(listener, undefined, key, includeValue);
};
MapProxy.prototype.removeEntryListener = function (listenerId) {
return this.client.getListenerService().deregisterListener(MapRemoveEntryListenerCodec_1.MapRemoveEntryListenerCodec.encodeRequest(this.name, listenerId), MapRemoveEntryListenerCodec_1.MapRemoveEntryListenerCodec.decodeResponse);
};
return MapProxy;
}(BaseProxy_1.BaseProxy));
exports.MapProxy = MapProxy;
//# sourceMappingURL=MapProxy.js.map