dolphindb 3.0.22 → 3.0.24

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.d.ts CHANGED
@@ -300,8 +300,8 @@ export declare class DdbObj<TValue extends DdbValue = DdbValue> {
300
300
  - 矩阵对应 DdbMatrixData
301
301
  - 字典对应普通的 js 对象 Record<string, any>
302
302
  - 图对应 DdbChartValue */
303
- data<TResult extends any[]>(this: DdbVectorObj): TResult;
304
- data<TResult = any>(this: DdbObj): TResult;
303
+ data<TResult extends any[]>(this: DdbVectorObj, options?: ConvertOptions): TResult;
304
+ data<TResult = any>(this: DdbObj, options?: ConvertOptions): TResult;
305
305
  [inspect.custom](depth: number, _options: InspectOptions, _inspect: any): string;
306
306
  inspect_type(): string;
307
307
  /** 自动转换 js string, boolean 为 DdbObj */
@@ -343,9 +343,13 @@ export interface InspectOptions extends UtilInspectOptions {
343
343
  export declare function format(type: DdbType, value: DdbValue, le: boolean, options?: InspectOptions): string;
344
344
  /** 格式化向量、集合中的第 index 项为字符串,空值返回 'null' 字符串 formatted vector, the index-th item in the collection is a string, a null value returns a 'null' string */
345
345
  export declare function formati(obj: DdbVectorObj, index: number, options?: InspectOptions): string;
346
- export declare function convert(type: DdbType, value: DdbValue, le: boolean): string | number | bigint | boolean | Uint8Array | Int32Array | BigInt64Array | string[] | Int8Array | Int16Array | Float32Array | Float64Array | Uint8Array[] | number[] | DdbFunctionDefValue | DdbDurationValue | DdbDecimal32Value | DdbDecimal64Value | DdbDecimal32VectorValue | DdbDecimal64VectorValue | DdbDecimal128VectorValue | BigInt128Array | DdbSymbolExtendedValue | DdbArrayVectorValue | DdbMatrixValue | DdbObj<DdbValue>[] | DdbDurationVectorValue | DdbChartValue;
346
+ export interface ConvertOptions {
347
+ /** `'string'` blob 类型数据的格式 */
348
+ blob?: 'string' | 'binary';
349
+ }
350
+ export declare function convert(type: DdbType, value: DdbValue, le: boolean, { blob }?: ConvertOptions): string | number | bigint | boolean | Uint8Array | DdbFunctionDefValue | DdbDurationValue | DdbDecimal32Value | DdbDecimal64Value | DdbDecimal32VectorValue | Int32Array | DdbDecimal64VectorValue | BigInt64Array | DdbDecimal128VectorValue | BigInt128Array | DdbSymbolExtendedValue | string[] | Int8Array | Int16Array | Float32Array | Float64Array | DdbArrayVectorValue | DdbMatrixValue | Uint8Array[] | DdbObj<DdbValue>[] | DdbDurationVectorValue | DdbChartValue | number[];
347
351
  /** 转换一个向量到 js 原生数组 */
348
- export declare function converts(type: DdbType, value: DdbVectorValue, rows: number, le: boolean): any[];
352
+ export declare function converts(type: DdbType, value: DdbVectorValue, rows: number, le: boolean, options?: ConvertOptions): any[];
349
353
  export declare class DdbVoid extends DdbObj<undefined> {
350
354
  constructor(value?: DdbVoidType);
351
355
  }
@@ -489,31 +493,28 @@ export interface StreamingParams {
489
493
  };
490
494
  handler(message: StreamingMessage): any;
491
495
  }
492
- export interface StreamingMessage extends StreamingParams {
496
+ export interface StreamingMessage<TRows = any> extends StreamingParams {
493
497
  /** server 发送消息的时间 (nano seconds since epoch) The time the server sent the message (nano seconds since epoch)
494
498
  std::chrono::system_clock::now().time_since_epoch() / std::chrono::nanoseconds(1) */
495
499
  time: bigint;
496
500
  /** message id */
497
501
  id: bigint;
498
- colnames: string[];
499
- /** 最新的 server 有可能先推一个 table schema 过来 */
500
- schema?: DdbTableObj;
501
502
  /** 订阅主题,即一个订阅的名称。 Subscription topic, which is the name of a subscription.
502
503
  它是一个字符串,由订阅表所在节点的别名、流数据表名称和订阅任务名称(如果指定了 actionName)组合而成,使用 `/` 分隔
503
504
  It is a string consisting of the alias of the node where the subscription table is located, the stream data table name, and the subscription task name (if actionName is specified), separated by `/` */
504
505
  topic: string;
505
506
  /** 流数据,类型是 any vector, 其中的每一个元素对应被订阅表的一个列 (没有 name),列 (DdbObj<DdbVectorValue>) 中的内容是新增的数据值
506
507
  Stream data, the type is any vector, each element of which corresponds to a column (without name) of the subscribed table, and the content in the column (DdbObj<DdbVectorValue>) is the new data value */
507
- data: DdbObj<DdbVectorObj[]>;
508
- /** 新增的流数据行数 Number of new streaming data rows */
509
- rows: number;
508
+ obj: DdbObj<DdbVectorObj[]>;
509
+ /** 流数据 */
510
+ data: DdbTableData<TRows>;
510
511
  window: {
511
512
  /** 建立连接开始 offset = 0, 随着 window 的移动逐渐增加 The establishment of the connection starts offset = 0, and gradually increases as the window moves */
512
513
  offset: number;
513
514
  /** segments 中 segment.row 的总和 sum of segment.row in segments */
514
515
  rows: number;
515
516
  /** 每次接收到的 data 组成的数组 An array of data received each time */
516
- segments: DdbObj<DdbVectorObj[]>[];
517
+ segments: DdbTableData<TRows>[];
517
518
  };
518
519
  /** 成功订阅后,后续推送过来的 message 解析错误,则会设置 error 并调用 handler
519
520
  After successfully subscribed, if the subsequently pushed message is parsed incorrectly, the error will be set and the handler will be called. */
package/index.js CHANGED
@@ -3,7 +3,7 @@ import DayjsCustomParseFormat from 'dayjs/plugin/customParseFormat.js';
3
3
  dayjs.extend(DayjsCustomParseFormat);
4
4
  import ipaddrjs from 'ipaddr.js';
5
5
  const { fromByteArray: buf2ipaddr } = ipaddrjs;
6
- import { concat, assert, inspect, typed_array_to_buffer, connect_websocket, Lock, genid, seq, zip_object, WebSocketOpen, WebSocketClosed, WebSocketClosing, } from 'xshell';
6
+ import { concat, assert, inspect, typed_array_to_buffer, connect_websocket, Lock, genid, seq, zip_object, WebSocketOpen, WebSocketClosed, WebSocketClosing, decode, } from 'xshell';
7
7
  import { t } from './i18n/index.js';
8
8
  export const nulls = {
9
9
  int8: -0x80, // -128
@@ -1122,20 +1122,20 @@ export class DdbObj {
1122
1122
  throw new Error(t('vector {{type}} 暂不支持序列化', { type: get_type_name(type) }));
1123
1123
  }
1124
1124
  }
1125
- data() {
1125
+ data(options) {
1126
1126
  const { form, type, value, le, rows, name } = this;
1127
1127
  switch (form) {
1128
1128
  case DdbForm.scalar:
1129
- return convert(type, value, le);
1129
+ return convert(type, value, le, options);
1130
1130
  case DdbForm.vector:
1131
1131
  case DdbForm.pair:
1132
1132
  case DdbForm.set: {
1133
- const data = converts(type, value, rows, le);
1133
+ const data = converts(type, value, rows, le, options);
1134
1134
  return (form === DdbForm.set ? new Set(data) : data);
1135
1135
  }
1136
1136
  case DdbForm.table: {
1137
1137
  const cols = value;
1138
- const jscols = cols.map(col => col.data());
1138
+ const jscols = cols.map(col => col.data(options));
1139
1139
  const keys = cols.map(({ name }) => name);
1140
1140
  return {
1141
1141
  name: name || '',
@@ -1146,20 +1146,20 @@ export class DdbObj {
1146
1146
  }
1147
1147
  case DdbForm.dict: {
1148
1148
  const [keys, values] = value;
1149
- return zip_object(keys.data(), values.data());
1149
+ return zip_object(keys.data(options), values.data(options));
1150
1150
  }
1151
1151
  case DdbForm.chart:
1152
1152
  return value;
1153
1153
  case DdbForm.matrix: {
1154
1154
  const ncolumns = this.cols;
1155
1155
  const { rows: _rows, cols: _cols, data } = value;
1156
- const jsdata = converts(type, data, rows * ncolumns, le);
1156
+ const jsdata = converts(type, data, rows * ncolumns, le, options);
1157
1157
  return {
1158
1158
  type,
1159
1159
  nrows: rows,
1160
1160
  ncolumns,
1161
- rows: _rows?.data(),
1162
- columns: _cols?.data(),
1161
+ rows: _rows?.data(options),
1162
+ columns: _cols?.data(options),
1163
1163
  data: seq(rows, i => seq(ncolumns, j => jsdata[j * rows + i]))
1164
1164
  };
1165
1165
  }
@@ -1742,7 +1742,7 @@ export function formati(obj, index, options = {}) {
1742
1742
  }
1743
1743
  }
1744
1744
  }
1745
- export function convert(type, value, le) {
1745
+ export function convert(type, value, le, { blob = 'string' } = {}) {
1746
1746
  switch (type) {
1747
1747
  case DdbType.void:
1748
1748
  return value === DdbVoidType.null ? null : undefined;
@@ -1768,8 +1768,9 @@ export function convert(type, value, le) {
1768
1768
  case DdbType.handle:
1769
1769
  case DdbType.datasource:
1770
1770
  case DdbType.resource:
1771
- case DdbType.blob:
1772
1771
  return value;
1772
+ case DdbType.blob:
1773
+ return blob === 'string' ? decode(value) : value;
1773
1774
  case DdbType.complex:
1774
1775
  case DdbType.point: {
1775
1776
  const [x, y] = value;
@@ -1802,7 +1803,7 @@ export function convert(type, value, le) {
1802
1803
  }
1803
1804
  }
1804
1805
  /** 转换一个向量到 js 原生数组 */
1805
- export function converts(type, value, rows, le) {
1806
+ export function converts(type, value, rows, le, options) {
1806
1807
  if (type < 64 || type >= 128)
1807
1808
  switch (type) {
1808
1809
  // 可以直接用下标取值再转换的类型
@@ -1832,7 +1833,7 @@ export function converts(type, value, rows, le) {
1832
1833
  case DdbType.resource:
1833
1834
  case DdbType.functiondef:
1834
1835
  case DdbType.blob:
1835
- return Array.prototype.map.call(value, (x) => convert(type, x, le));
1836
+ return Array.prototype.map.call(value, (x) => convert(type, x, le, options));
1836
1837
  case DdbType.void:
1837
1838
  return [];
1838
1839
  case DdbType.symbol_extended: {
@@ -1841,11 +1842,15 @@ export function converts(type, value, rows, le) {
1841
1842
  }
1842
1843
  case DdbType.complex:
1843
1844
  case DdbType.point:
1844
- return seq(rows, i => [value[2 * i], value[2 * (i + 1)]]);
1845
+ return seq(rows, i => {
1846
+ const x = value[2 * i];
1847
+ const y = value[2 * i + 1];
1848
+ return [x === nulls.double ? null : x, y === nulls.double ? null : y];
1849
+ });
1845
1850
  case DdbType.uuid:
1846
1851
  case DdbType.ipaddr:
1847
1852
  case DdbType.int128:
1848
- return seq(rows, i => convert(type, value.subarray(16 * i, 16 * (i + 1)), le));
1853
+ return seq(rows, i => convert(type, value.subarray(16 * i, 16 * (i + 1)), le, options));
1849
1854
  case DdbType.decimal32:
1850
1855
  case DdbType.decimal64:
1851
1856
  case DdbType.decimal128: {
@@ -1858,7 +1863,7 @@ export function converts(type, value, rows, le) {
1858
1863
  });
1859
1864
  }
1860
1865
  case DdbType.any:
1861
- return value.map(x => x.data());
1866
+ return value.map(x => x.data(options));
1862
1867
  default:
1863
1868
  throw new Error(String(DdbType[type] || type) + '[]' + t(' 暂时不支持转换为 js 对象'));
1864
1869
  }
@@ -1867,25 +1872,21 @@ export function converts(type, value, rows, le) {
1867
1872
  return value.map(({ lengths, data, rows }) => {
1868
1873
  let acc_len = 0;
1869
1874
  return Array.prototype.map.call(lengths, (length) => {
1870
- const result = (() => {
1871
- switch (type_) {
1872
- case DdbType.decimal32:
1873
- case DdbType.decimal64:
1874
- case DdbType.decimal128:
1875
- return converts(type_, { scale: value.scale, data: data.subarray(acc_len, acc_len + length) }, length, le);
1876
- case DdbType.complex:
1877
- case DdbType.point:
1878
- return converts(type_, data.subarray(acc_len, acc_len + 2 * length), length, le);
1879
- case DdbType.uuid:
1880
- case DdbType.int128:
1881
- case DdbType.ipaddr:
1882
- return converts(type_, data.subarray(acc_len, acc_len + 16 * length), length, le);
1883
- default:
1884
- return converts(type_, data.subarray(acc_len, acc_len + length), length, le);
1885
- }
1886
- })();
1887
- acc_len += length;
1888
- return result;
1875
+ switch (type_) {
1876
+ case DdbType.decimal32:
1877
+ case DdbType.decimal64:
1878
+ case DdbType.decimal128:
1879
+ return converts(type_, { scale: value.scale, data: data.subarray(acc_len, acc_len += length) }, length, le, options);
1880
+ case DdbType.complex:
1881
+ case DdbType.point:
1882
+ return converts(type_, data.subarray(acc_len, acc_len += 2 * length), length, le, options);
1883
+ case DdbType.uuid:
1884
+ case DdbType.int128:
1885
+ case DdbType.ipaddr:
1886
+ return converts(type_, data.subarray(acc_len, acc_len += 16 * length), length, le, options);
1887
+ default:
1888
+ return converts(type_, data.subarray(acc_len, acc_len += length), length, le, options);
1889
+ }
1889
1890
  });
1890
1891
  }).flat();
1891
1892
  }
@@ -3042,8 +3043,9 @@ export class DDB {
3042
3043
  rows: 0,
3043
3044
  segments: [],
3044
3045
  };
3045
- let schema;
3046
- const { value: colnames } = await this.call('publishTable', [
3046
+ // 流表推送过来的第一条数据是 schema,需要特殊处理
3047
+ let first = true;
3048
+ const { value: columns } = await this.call('publishTable', [
3047
3049
  'localhost',
3048
3050
  new DdbInt(0),
3049
3051
  this.streaming.table,
@@ -3057,41 +3059,56 @@ export class DDB {
3057
3059
  // 先准备好收到 websocket message 的 callback
3058
3060
  on_more_messages: buffer => {
3059
3061
  try {
3062
+ let data;
3060
3063
  const dv = new DataView(buffer.buffer, buffer.byteOffset, buffer.byteLength);
3061
3064
  const i_topic_end = buffer.indexOf(0, 17);
3062
3065
  // 首个 message 一定是 table schema, 后续消息是 column 片段组成的 any vector
3063
- const data = DdbObj.parse(buffer.subarray(i_topic_end + 1), this.le);
3066
+ const obj = DdbObj.parse(buffer.subarray(i_topic_end + 1), this.le);
3064
3067
  // server 推数据时遇到错误会返回 string,一般格式为 error.xxx: 错误信息
3065
- if (data.form === DdbForm.scalar && data.type === DdbType.string) {
3068
+ if (obj.form === DdbForm.scalar && obj.type === DdbType.string) {
3066
3069
  this.disconnect();
3067
- const value = data.value;
3070
+ const value = obj.value;
3068
3071
  throw new Error(value.slice(value.indexOf(':') + 1).trim());
3069
3072
  }
3070
- if (!schema) {
3071
- schema = data;
3072
- return;
3073
+ if (first) {
3074
+ data = obj.data();
3075
+ data.name ||= this.streaming.table;
3076
+ first = false;
3073
3077
  }
3074
- const { rows } = data.value[0];
3075
- win.rows += rows;
3076
- win.segments.push(data);
3077
- if (win.rows >= winsize * 2 && win.segments.length >= 2) {
3078
- let winsize_ = 0;
3079
- let i = win.segments.length - 1;
3080
- // 往前移动至首个累计 winsize_ 超过 winsize 的位置
3081
- for (; winsize_ < winsize; i--)
3082
- winsize_ += win.segments[i].value[0].rows;
3083
- win.segments = win.segments.slice(i);
3084
- win.offset += win.rows - winsize_;
3085
- win.rows = winsize_;
3078
+ else {
3079
+ const { rows } = obj.value[0];
3080
+ const types = [];
3081
+ const js_cols = [];
3082
+ // 遍历每一列
3083
+ obj.value.forEach(({ type, value }) => {
3084
+ types.push(type);
3085
+ js_cols.push(converts(type, value, rows, obj.le));
3086
+ });
3087
+ data = {
3088
+ name: this.streaming.table || '',
3089
+ columns,
3090
+ types,
3091
+ data: seq(rows, i => zip_object(columns, seq(columns.length, j => js_cols[j][i])))
3092
+ };
3093
+ win.rows += rows;
3094
+ win.segments.push(data);
3095
+ if (win.rows >= winsize * 2 && win.segments.length >= 2) {
3096
+ let winsize_ = 0;
3097
+ let i = win.segments.length - 1;
3098
+ // 往前移动至首个累计 winsize_ 超过 winsize 的位置
3099
+ for (; winsize_ < winsize; i--)
3100
+ winsize_ += win.segments[i].data.length;
3101
+ win.segments = win.segments.slice(i);
3102
+ win.offset += win.rows - winsize_;
3103
+ win.rows = winsize_;
3104
+ }
3086
3105
  }
3087
3106
  this.streaming.handler({
3088
3107
  ...this.streaming,
3089
3108
  id: dv.getBigInt64(9, this.le),
3090
3109
  time: dv.getBigInt64(1, this.le),
3091
- rows,
3092
3110
  topic: this.dec.decode(buffer.subarray(17, i_topic_end)),
3093
- colnames,
3094
- schema,
3111
+ obj,
3095
3112
  data,
3096
3113
  window: win,
3097
3114
  });
@@ -3102,9 +3119,9 @@ export class DDB {
3102
3119
  }
3103
3120
  }
3104
3121
  });
3105
- console.log(t('订阅流表成功') + ', colnames:',
3122
+ console.log(t('订阅流表成功') + ', columns:',
3106
3123
  // string[3](['time', 'stock', 'price'])
3107
- colnames);
3124
+ columns);
3108
3125
  }
3109
3126
  }
3110
3127
  export class BigInt128Array {