@kevin_men/websocket
Version:
TDengine Connector for nodejs and browser using WebSocket.
205 lines (204 loc) • 8.07 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseTmqBlock = exports.TopicPartition = exports.CommittedResp = exports.PartitionsResp = exports.SubscriptionResp = exports.AssignmentResp = exports.WSTmqFetchBlockResponse = exports.TaosTmqResult = exports.WsTmqQueryResponse = exports.WsPollResponse = void 0;
const wsResponse_1 = require("../client/wsResponse");
const constant_1 = require("../common/constant");
const taosResult_1 = require("../common/taosResult");
const wsError_1 = require("../common/wsError");
class WsPollResponse {
constructor(resp) {
this.totalTime = resp.totalTime;
this.code = resp.msg.code;
this.message = resp.msg.message;
this.action = resp.msg.action;
this.req_id = resp.msg.req_id;
this.have_message = resp.msg.have_message;
this.topic = resp.msg.topic;
this.database = resp.msg.database;
this.vgroup_id = resp.msg.vgroup_id;
this.message_id = resp.msg.message_id;
this.message_type = resp.msg.message_type;
if (resp.msg.id) {
this.id = BigInt(resp.msg.id);
}
else {
this.id = BigInt(0);
}
}
}
exports.WsPollResponse = WsPollResponse;
// resp: {"code":0,"message":"","action":"fetch","req_id":4,"message_id":1,"completed":false,"table_name":"ct2","rows":1,"fields_count":4,"fields_names":["ts","c1","c2","c3"],"fields_types":[9,4,6,8],"fields_lengths":[8,4,4,10],"precision":0}
class WsTmqQueryResponse extends wsResponse_1.WSQueryResponse {
constructor(resp) {
super(resp);
this.completed = resp.msg.completed;
this.table_name = resp.msg.table_name;
this.rows = resp.msg.rows;
this.message_id = resp.msg.message_id;
}
}
exports.WsTmqQueryResponse = WsTmqQueryResponse;
class TaosTmqResult extends taosResult_1.TaosResult {
constructor(resp, pollResp) {
super(resp);
this.table_name = resp.table_name;
// this._affectRows = resp.rows;
this.topic = pollResp.topic;
this.database = pollResp.database;
this.vgroup_id = pollResp.vgroup_id;
}
}
exports.TaosTmqResult = TaosTmqResult;
class WSTmqFetchBlockResponse {
constructor(resp) {
this.totalTime = resp.totalTime;
this.blockData = resp.msg;
}
}
exports.WSTmqFetchBlockResponse = WSTmqFetchBlockResponse;
class AssignmentResp {
constructor(resp, topic) {
this.timing = BigInt(resp.msg.timing);
this.code = resp.msg.code;
this.message = resp.msg.message;
this.req_id = resp.msg.req_id;
this.action = resp.msg.action;
this.totalTime = resp.totalTime;
this.topicPartition = resp.msg.assignment;
for (let i in this.topicPartition) {
this.topicPartition[i].topic = topic;
}
}
}
exports.AssignmentResp = AssignmentResp;
class SubscriptionResp {
constructor(resp) {
this.timing = BigInt(resp.msg.timing);
this.code = resp.msg.code;
this.message = resp.msg.message;
this.req_id = resp.msg.req_id;
this.action = resp.msg.action;
this.totalTime = resp.totalTime;
this.topics = resp.msg.topics;
}
}
exports.SubscriptionResp = SubscriptionResp;
class PartitionsResp {
constructor(resp) {
this.timing = BigInt(resp.msg.timing);
this.code = resp.msg.code;
this.message = resp.msg.message;
this.req_id = resp.msg.req_id;
this.action = resp.msg.action;
this.totalTime = resp.totalTime;
this.positions = resp.msg.position;
}
setTopicPartitions(topicPartitions) {
if (topicPartitions.length != this.positions.length) {
throw new wsError_1.WebSocketInterfaceError(wsError_1.ErrorCode.ERR_PARTITIONS_TOPIC_VGROUP_LENGTH_NOT_EQUAL, 'TopicPartitions and positions are not equal in length');
}
for (let i in this.positions) {
topicPartitions[i].offset = this.positions[i];
}
return topicPartitions;
}
}
exports.PartitionsResp = PartitionsResp;
class CommittedResp extends PartitionsResp {
constructor(resp) {
super(resp);
this.positions = resp.msg.committed;
}
}
exports.CommittedResp = CommittedResp;
class TopicPartition {
constructor(msg) {
this.vgroup_id = msg.vgroup_id;
this.offset = msg.offset;
this.begin = msg.begin;
this.end = msg.end;
this.topic = '';
}
}
exports.TopicPartition = TopicPartition;
function parseTmqBlock(rows, resp, taosResult) {
let dataList = new Array(rows);
if (!resp || !taosResult) {
return taosResult;
}
let metaList = taosResult.getTaosMeta();
let taosdata = taosResult.getData();
if (metaList && rows && taosdata) {
//get bitmap length
let bitMapOffset = getBitmapLen(rows);
//skip data head
let bufferOffset = 24 + 28 + 5 * metaList.length;
let dataBuffer = resp.blockData.slice(bufferOffset);
let metaLens = [];
for (let i = 0; i < metaList.length; i++) {
//get data len
metaLens.push(new DataView(dataBuffer, i * 4, 4).getInt32(0, true));
}
bufferOffset = metaList.length * 4;
for (let i = 0; i < metaList.length; i++) {
let data = [];
//get type code
let isVarType = (0, taosResult_1._isVarType)(metaList[i]);
//fixed length type
if (isVarType == constant_1.ColumnsBlockType.SOLID) {
let bitMapArr = dataBuffer.slice(bufferOffset, bufferOffset + bitMapOffset);
bufferOffset += bitMapOffset;
//decode column data, data is array
data = (0, taosResult_1.readSolidDataToArray)(dataBuffer, bufferOffset, rows, metaList[i], bitMapArr);
}
else {
//Variable length type
let offset = bufferOffset;
let offsets = [];
for (let i = 0; i < rows; i++, offset += constant_1.TDengineTypeLength['INT']) {
//get data length, -1 is null
offsets.push(new DataView(dataBuffer, offset, 4).getInt32(0, true));
}
let start = offset;
for (let i = 0; i < rows; i++) {
let value = '';
if (-1 == offsets[i]) {
value = null;
}
else {
let header = start + offsets[i];
let dataLength = new DataView(dataBuffer, header, 2).getInt16(0, true) & 0xFFFF;
if (isVarType == constant_1.ColumnsBlockType.VARCHAR) {
//decode var char
value = (0, taosResult_1.readVarchar)(dataBuffer, header + 2, dataLength);
}
else if (isVarType == constant_1.ColumnsBlockType.GEOMETRY || isVarType == constant_1.ColumnsBlockType.VARBINARY) {
//decode binary
value = (0, taosResult_1.readBinary)(dataBuffer, header + 2, dataLength);
}
else {
//decode nchar
value = (0, taosResult_1.readNchar)(dataBuffer, header + 2, dataLength);
}
}
data.push(value);
}
bufferOffset += rows * 4;
}
bufferOffset += metaLens[i];
//column data to row data
for (let row = 0; row < data.length; row++) {
if (dataList[row] == null) {
dataList[row] = [];
}
dataList[row].push(data[row]);
}
}
taosdata.push(...dataList);
}
return taosResult;
}
exports.parseTmqBlock = parseTmqBlock;
function getBitmapLen(n) {
return (n + 0x7) >> 3;
}