dolphindb 3.0.39 → 3.0.41

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/README.md CHANGED
@@ -494,11 +494,11 @@ export interface StreamingMessage <TRows = any> extends StreamingParams {
494
494
  /** The establishment of the connection starts offset = 0, and gradually increases as the window moves */
495
495
  offset: number
496
496
 
497
- /** sum of segment.row in segments */
498
- rows: number
497
+ /** Historical data */
498
+ data: TRows[]
499
499
 
500
- /** An array of data received each time */
501
- segments: DdbTableData<TRows>[]
500
+ /** An array of obj received each time */
501
+ objs: DdbObj<DdbVectorObj[]>[]
502
502
  }
503
503
 
504
504
  /** After successfully subscribed, if the subsequently pushed message is parsed incorrectly, the error will be set and the handler will be called. */
package/README.zh.md CHANGED
@@ -64,20 +64,23 @@ https://www.npmjs.com/package/dolphindb
64
64
 
65
65
  ##### 1. 安装
66
66
 
67
- 1.1. 在机器上安装最新版的 Node.js 及浏览器。
68
- - windows: https://nodejs.org/en/download/current/
69
- - linux: https://github.com/nodesource/distributions?tab=readme-ov-file#debian-and-ubuntu-based-distributions
67
+ 1.1. 在机器上安装最新版的 Node.js 及浏览器。
68
+ - windows: https://nodejs.org/en/download/prebuilt-installer/current
69
+ - linux: https://github.com/nodesource/distributions?tab=readme-ov-file#debian-and-ubuntu-based-distributions
70
+
70
71
  1.2. (可选)使用以下命令创建新项目。如果已有项目,可跳过此步。
71
- ```bash
72
- mkdir dolphindb-example
73
- cd dolphindb-example
74
- npm init --yes
75
- ```
72
+ ```bash
73
+ mkdir dolphindb-example
74
+ cd dolphindb-example
75
+ npm init --yes
76
+ ```
77
+
76
78
  1.3. 用编辑器打开 package.json 文件,在 `"main": "./index.js"` 下方加入一行 "type": "module", 这样能够启用 ECMAScript modules,在后面代码中可以使用 `import { DDB } from 'dolphindb'` 导入 npm 包。
79
+
77
80
  1.4. 在项目中安装 npm 包。
78
- ```bash
79
- npm install dolphindb
80
- ```
81
+ ```bash
82
+ npm install dolphindb
83
+ ```
81
84
 
82
85
  ##### 2. 使用
83
86
 
@@ -574,11 +577,11 @@ export interface StreamingMessage <TRows = any> extends StreamingParams {
574
577
  /** 建立连接开始 offset = 0, 随着 window 的移动逐渐增加 */
575
578
  offset: number
576
579
 
577
- /** segments 中 segment.row 的总和 */
578
- rows: number
580
+ /** 历史数据 */
581
+ data: TRows[]
579
582
 
580
- /** 每次接收到的 data 组成的数组 */
581
- segments: DdbTableData<TRows>[]
583
+ /** 每次接收到的 obj 组成的数组 */
584
+ objs: DdbObj<DdbVectorObj[]>[]
582
585
  }
583
586
 
584
587
  /** 成功订阅后,后续推送过来的 message 解析错误,则会设置 error 并调用 handler */
package/browser.d.ts CHANGED
@@ -330,7 +330,7 @@ export declare class DdbObj<TValue extends DdbValue = DdbValue> {
330
330
  pack(): Uint8Array;
331
331
  static pack_vector_body(value: DdbVectorValue, type: DdbType, length: number): ArrayBufferView[];
332
332
  /** 将 DdbObj 转换为 js 原生数据类型
333
- - 标量对应 number, bigint 或者字符串
333
+ - 标量对应 number, bigint 或者字符串 (其中时间类型转换为常用的字符串表示)
334
334
  - 数组对应 js 原生数组
335
335
  - 表格对应 DdbTableData
336
336
  - 矩阵对应 DdbMatrixData
@@ -384,7 +384,7 @@ export declare class DdbObj<TValue extends DdbValue = DdbValue> {
384
384
  }
385
385
  /** 根据 DdbType 获取其名称,array vector type 自动在后面加上 [] */
386
386
  export declare function get_type_name(type: DdbType): string;
387
- export declare function is_decimal_type(type: DdbType): boolean;
387
+ export declare function is_decimal_type(type: DdbType): type is DdbType.decimal32 | DdbType.decimal64 | DdbType.decimal128;
388
388
  export declare function is_decimal_null_value(type: DdbType, value: number | bigint): boolean;
389
389
  export declare function get_duration_unit(code: number): string;
390
390
  export interface InspectOptions {
@@ -398,6 +398,8 @@ export interface InspectOptions {
398
398
  quote?: boolean;
399
399
  /** `true` 决定格式化后的数据是否有千分位 */
400
400
  grouping?: boolean;
401
+ /** timestamp 类型转换为字符串表示时显示到秒还是毫秒 */
402
+ timestamp?: 's' | 'ms';
401
403
  }
402
404
  /** 根据 DdbType 格式化单个元素 (value) 为字符串 */
403
405
  export declare function format(type: DdbType, value: DdbValue, le: boolean, options?: InspectOptions): string;
@@ -406,8 +408,10 @@ export declare function formati(obj: DdbVectorObj, index: number, options?: Insp
406
408
  export interface ConvertOptions {
407
409
  /** `'string'` blob 类型数据的格式 */
408
410
  blob?: 'string' | 'binary';
411
+ /** timestamp 类型转换为字符串表示时显示到秒还是毫秒 */
412
+ timestamp?: 's' | 'ms';
409
413
  }
410
- 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 | number[] | DdbTensorValue | DdbChartValue;
414
+ export declare function convert(type: DdbType, value: DdbValue, le: boolean, { blob, timestamp }?: 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 | number[] | DdbTensorValue | DdbChartValue;
411
415
  /** 转换一个向量到 js 原生数组 */
412
416
  export declare function converts(type: DdbType, value: DdbVectorValue, rows: number, le: boolean, options?: ConvertOptions): any[];
413
417
  export declare class DdbVoid extends DdbObj<undefined> {
@@ -571,10 +575,8 @@ export interface StreamingMessage<TRows = any> extends StreamingParams {
571
575
  window: {
572
576
  /** 建立连接开始 offset = 0, 随着 window 的移动逐渐增加 The establishment of the connection starts offset = 0, and gradually increases as the window moves */
573
577
  offset: number;
574
- /** segments 中 segment.row 的总和 sum of segment.row in segments */
575
- rows: number;
576
- /** 每次接收到的 data 组成的数组 An array of data received each time */
577
- segments: DdbTableData<TRows>[];
578
+ /** 历史数据 Historical data */
579
+ data: TRows[];
578
580
  /** 每次接收到的 obj 组成的数组 An array of obj received each time */
579
581
  objs: DdbObj<DdbVectorObj[]>[];
580
582
  };
package/browser.js CHANGED
@@ -1068,6 +1068,38 @@ export class DdbObj {
1068
1068
  ...DdbObj.pack_vector_body(data, this.type, this.rows * this.cols)
1069
1069
  ];
1070
1070
  }
1071
+ case DdbForm.tensor: {
1072
+ const { le, value } = this;
1073
+ const { tensor_type, device_type, tensor_flags, dimensions, shape, strides, preserve_value, elem_count, data } = value;
1074
+ // 计算总的字节长度
1075
+ const totalLength = 10 + dimensions * 8 * 2 + 8 + 8 + data.length; // 12 字节元数据 + 维度信息 + 保留值和元素数量 + 数据部分
1076
+ const buffer = new ArrayBuffer(totalLength);
1077
+ const dv = new DataView(buffer);
1078
+ const uint8Array = new Uint8Array(buffer);
1079
+ // 写入元数据
1080
+ // uint8Array[0] = type;
1081
+ // uint8Array[1] = form;
1082
+ uint8Array[0] = tensor_type;
1083
+ uint8Array[1] = device_type;
1084
+ dv.setUint32(2, tensor_flags, le);
1085
+ dv.setInt32(6, dimensions, le);
1086
+ // 写入形状和步长
1087
+ const shapeStart = 10;
1088
+ const stridesStart = shapeStart + dimensions * 8;
1089
+ for (let d = 0; d < dimensions; d++) {
1090
+ dv.setBigInt64(shapeStart + d * 8, BigInt(shape[d]), le);
1091
+ dv.setBigInt64(stridesStart + d * 8, BigInt(strides[d]), le);
1092
+ }
1093
+ // 写入保留值和元素数量
1094
+ const preserveValueStart = stridesStart + dimensions * 8;
1095
+ dv.setBigInt64(preserveValueStart, preserve_value, le);
1096
+ const storageStart = preserveValueStart + 8;
1097
+ dv.setBigInt64(storageStart, BigInt(elem_count), le);
1098
+ // 写入数据
1099
+ const dataStart = storageStart + 8;
1100
+ uint8Array.set(data, dataStart);
1101
+ return [uint8Array];
1102
+ }
1071
1103
  default:
1072
1104
  throw new Error(t('vector {{type}} 暂不支持序列化', { type: get_type_name(type) }));
1073
1105
  }
@@ -1638,7 +1670,7 @@ let _formatter = new Intl.NumberFormat('en-US', { maximumFractionDigits: 20 });
1638
1670
  let _datetime_formatter = new Intl.DateTimeFormat('zh-CN', { dateStyle: 'short', timeStyle: 'medium', timeZone: 'UTC', hour12: false });
1639
1671
  /** 根据 DdbType 格式化单个元素 (value) 为字符串 */
1640
1672
  export function format(type, value, le, options = {}) {
1641
- const { nullstr = false, colors = false, quote = false, grouping = true } = options;
1673
+ const { nullstr = false, colors = false, quote = false, grouping = true, timestamp = 'ms' } = options;
1642
1674
  const formatter = (() => {
1643
1675
  const decimals = options.decimals ?? _decimals;
1644
1676
  if (decimals !== _decimals || grouping !== _grouping) {
@@ -1666,7 +1698,7 @@ export function format(type, value, le, options = {}) {
1666
1698
  let str;
1667
1699
  // formatter 可能会在 value 不属于 new Date() 有效值时,调用 抛出错误,这里统一处理
1668
1700
  try {
1669
- str = formatter(value);
1701
+ str = formatter(value, (type === DdbType.timestamp && timestamp === 's') ? 'YYYY.MM.DD HH:mm:ss' : undefined);
1670
1702
  }
1671
1703
  catch (error) {
1672
1704
  if (error instanceof RangeError)
@@ -1925,7 +1957,7 @@ export function formati(obj, index, options = {}) {
1925
1957
  }
1926
1958
  }
1927
1959
  }
1928
- export function convert(type, value, le, { blob = 'string' } = {}) {
1960
+ export function convert(type, value, le, { blob = 'string', timestamp = 'ms' } = {}) {
1929
1961
  switch (type) {
1930
1962
  case DdbType.void:
1931
1963
  return value === DdbVoidType.null ? null : undefined;
@@ -1980,7 +2012,7 @@ export function convert(type, value, le, { blob = 'string' } = {}) {
1980
2012
  case DdbType.decimal32:
1981
2013
  case DdbType.decimal64:
1982
2014
  case DdbType.decimal128:
1983
- return format(type, value, le, { colors: false });
2015
+ return format(type, value, le, { colors: false, timestamp });
1984
2016
  default:
1985
2017
  throw new Error(String(DdbType[type] || type) + t(' 暂时不支持转换为 js 对象'));
1986
2018
  }
@@ -3251,8 +3283,7 @@ export class DDB {
3251
3283
  let first = true;
3252
3284
  let win = {
3253
3285
  offset: 0,
3254
- rows: 0,
3255
- segments: [],
3286
+ data: [],
3256
3287
  objs: []
3257
3288
  };
3258
3289
  let schema;
@@ -3306,19 +3337,17 @@ export class DDB {
3306
3337
  data: seq(rows, i => zip_object(columns, seq(columns.length, j => _data[j][i])))
3307
3338
  };
3308
3339
  }
3309
- win.rows += rows;
3310
- win.segments.push(data);
3340
+ data.data.forEach(row => { win.data.push(row); });
3311
3341
  win.objs.push(obj);
3312
- if (win.rows >= winsize * 2 && win.segments.length >= 2) {
3342
+ if (win.data.length >= winsize * 2 && win.objs.length >= 2) {
3313
3343
  let winsize_ = 0;
3314
- let i = win.segments.length - 1;
3344
+ let i = win.objs.length - 1;
3315
3345
  // 往前移动至首个累计 winsize_ 超过 winsize 的位置
3316
3346
  for (; winsize_ < winsize; i--)
3317
- winsize_ += win.segments[i].data.length;
3318
- win.segments = win.segments.slice(i);
3347
+ winsize_ += win.objs[i].value[0].rows;
3348
+ win.offset += win.data.length - winsize_;
3349
+ win.data = win.data.slice(-winsize_);
3319
3350
  win.objs = win.objs.slice(i);
3320
- win.offset += win.rows - winsize_;
3321
- win.rows = winsize_;
3322
3351
  }
3323
3352
  }
3324
3353
  this.streaming.handler({