UNPKG

hazelcast-client

Version:

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

132 lines 5.78 kB
"use strict"; /* * Copyright (c) 2008-2018, Hazelcast, Inc. All Rights Reserved. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ Object.defineProperty(exports, "__esModule", { value: true }); var ClassDefinitionContext_1 = require("./ClassDefinitionContext"); var ClassDefinition_1 = require("./ClassDefinition"); var ClassDefinitionWriter_1 = require("./ClassDefinitionWriter"); var BitsUtil_1 = require("../../BitsUtil"); var PortableContext = /** @class */ (function () { function PortableContext(service, portableVersion) { this.portableVersion = 0; this.service = service; this.portableVersion = portableVersion; this.classDefContext = {}; } PortableContext.prototype.getVersion = function () { return this.portableVersion; }; PortableContext.prototype.readClassDefinitionFromInput = function (input, factoryId, classId, version) { var register = true; var cdWriter = new ClassDefinitionWriter_1.ClassDefinitionWriter(this, factoryId, classId, version); input.readInt(); var fieldCount = input.readInt(); var offset = input.position(); for (var i = 0; i < fieldCount; i++) { var pos = input.readInt(offset + i * BitsUtil_1.BitsUtil.INT_SIZE_IN_BYTES); input.position(pos); var len = input.readShort(); var chars = ''; for (var j = 0; j < len; j++) { chars += String.fromCharCode(input.readUnsignedByte()); } var type = input.readByte(); var name = chars; var fieldFactoryId = 0; var fieldClassId = 0; if (type === ClassDefinition_1.FieldType.PORTABLE) { //is null if (input.readBoolean()) { register = false; } fieldFactoryId = input.readInt(); fieldClassId = input.readInt(); // TODO: what there's a null inner Portable field if (register) { var fieldVersion = input.readInt(); this.readClassDefinitionFromInput(input, fieldFactoryId, fieldClassId, fieldVersion); } } else if (type === ClassDefinition_1.FieldType.PORTABLE_ARRAY) { var k = input.readInt(); fieldFactoryId = input.readInt(); fieldClassId = input.readInt(); // TODO: what there's a null inner Portable field if (k > 0) { var p = input.readInt(); input.position(p); var fieldVersion = input.readInt(); this.readClassDefinitionFromInput(input, fieldFactoryId, fieldClassId, fieldVersion); } else { register = false; } } cdWriter.addFieldByType(name, type, fieldFactoryId, fieldClassId); } cdWriter.end(); var classDefinition = cdWriter.getDefinition(); if (register) { classDefinition = cdWriter.registerAndGet(); } return classDefinition; }; PortableContext.prototype.lookupOrRegisterClassDefinition = function (portable) { var version = this.getClassVersion(portable); var definition = this.lookupClassDefinition(portable.getFactoryId(), portable.getClassId(), version); if (definition == null) { definition = this.generateClassDefinitionForPortable(portable); this.registerClassDefinition(definition); } return definition; }; PortableContext.prototype.lookupClassDefinition = function (factoryId, classId, version) { var factory = this.classDefContext[factoryId]; if (factory == null) { return null; } else { return factory.lookup(classId, version); } }; PortableContext.prototype.generateClassDefinitionForPortable = function (portable) { var version = this.getClassVersion(portable); var classDefinitionWriter = new ClassDefinitionWriter_1.ClassDefinitionWriter(this, portable.getFactoryId(), portable.getClassId(), version); portable.writePortable(classDefinitionWriter); classDefinitionWriter.end(); return classDefinitionWriter.registerAndGet(); }; PortableContext.prototype.registerClassDefinition = function (classDefinition) { var factoryId = classDefinition.getFactoryId(); var classId = classDefinition.getClassId(); var version = classDefinition.getVersion(); if (!this.classDefContext[factoryId]) { this.classDefContext[factoryId] = new ClassDefinitionContext_1.ClassDefinitionContext(factoryId, this.portableVersion); } return this.classDefContext[factoryId].register(classDefinition); }; PortableContext.prototype.getClassVersion = function (portable) { if (portable.getVersion) { return portable.getVersion(); } else { return this.portableVersion; } }; return PortableContext; }()); exports.PortableContext = PortableContext; //# sourceMappingURL=PortableContext.js.map