hazelcast-client
Version:
Hazelcast - open source In-Memory Data Grid - client for NodeJS
113 lines • 4.92 kB
JavaScript
"use strict";
var ClassDefinitionContext_1 = require('./ClassDefinitionContext');
var ClassDefinition_1 = require('./ClassDefinition');
var ClassDefinitionWriter_1 = require('./ClassDefinitionWriter');
var BitsUtil_1 = require('../../BitsUtil');
var PortableContext = (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) {
if (input.readBoolean()) {
register = false;
}
fieldFactoryId = input.readInt();
fieldClassId = input.readInt();
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();
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