dolphindb 3.0.38 → 3.0.40

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.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
 
package/browser.d.ts CHANGED
@@ -24,7 +24,8 @@ export declare enum DdbForm {
24
24
  /** 节点内部通信可能会使用,调用函数执行脚本一般不会返回这种类型 */
25
25
  chunk = 8,
26
26
  /** sysobj */
27
- object = 9
27
+ object = 9,
28
+ tensor = 10
28
29
  }
29
30
  /** DolphinDB DataType
30
31
  对应的 array vector 类型为 64 + 基本类型
@@ -173,6 +174,35 @@ export interface DdbMatrixValue {
173
174
  cols: DdbVectorObj | null;
174
175
  data: DdbVectorValue;
175
176
  }
177
+ /** 工具,取得某个 DdbType 的字节数 */
178
+ export declare const ddb_tensor_bytes: Record<DdbType.bool | DdbType.char | DdbType.short | DdbType.int | DdbType.long | DdbType.float | DdbType.double, number>;
179
+ type TensorElem = TensorData | boolean | number | bigint | null | string;
180
+ interface TensorData extends Array<TensorElem> {
181
+ }
182
+ interface DdbTensorMetadata {
183
+ /** Tensor 的元素的数据类型 */
184
+ data_type: DdbType;
185
+ /** Tensor 类型 */
186
+ tensor_type: number;
187
+ /** 设备类型 */
188
+ device_type: number;
189
+ /** Tensor Flags */
190
+ tensor_flags: number;
191
+ /** 维度 */
192
+ dimensions: number;
193
+ /** shape, shape[i] 表示第 i 个维度的 size */
194
+ shape: number[];
195
+ /** strides, strides[i] 表示在第 i 个维度,一个元素与下一个元素的距离 */
196
+ strides: number[];
197
+ /** 保留值 */
198
+ preserve_value: bigint;
199
+ /** 元素个数 */
200
+ elem_count: number;
201
+ }
202
+ export interface DdbTensorValue extends DdbTensorMetadata {
203
+ /** 数据 */
204
+ data: Uint8Array;
205
+ }
176
206
  export type DdbDictValue = [DdbVectorObj, DdbVectorObj];
177
207
  export interface DdbChartValue {
178
208
  /** 原属性 chartType original: chartType */
@@ -207,7 +237,7 @@ export type DdbVectorValue = null | Uint8Array | Int8Array | Int16Array | Int32A
207
237
  Uint8Array[] | // blob
208
238
  DdbObj[] | // any
209
239
  DdbSymbolExtendedValue | DdbArrayVectorValue | DdbDecimal32VectorValue | DdbDecimal64VectorValue | DdbDecimal128VectorValue | DdbDurationVectorValue;
210
- export type DdbValue = DdbScalarValue | DdbVectorValue | DdbMatrixValue | DdbDictValue | DdbChartValue;
240
+ export type DdbValue = DdbScalarValue | DdbVectorValue | DdbMatrixValue | DdbDictValue | DdbChartValue | DdbTensorValue;
211
241
  export type DdbStringObj = DdbObj<string>;
212
242
  export type DdbVectorObj<TValue extends DdbVectorValue = DdbVectorValue> = DdbObj<TValue>;
213
243
  export type DdbVectorAnyObj = DdbVectorObj<DdbObj[]>;
@@ -216,6 +246,7 @@ export type DdbTableObj<TColumns extends DdbVectorObj[] = DdbVectorObj[]> = DdbO
216
246
  export type DdbDictObj<TKeys extends DdbVectorObj = DdbVectorObj, TValues extends DdbVectorObj = DdbVectorObj> = DdbObj<[TKeys, TValues]>;
217
247
  export type DdbMatrixObj<TValue extends DdbMatrixValue = DdbMatrixValue> = DdbObj<TValue>;
218
248
  export type DdbChartObj = DdbObj<DdbChartValue>;
249
+ export type DdbTensorObj = DdbObj<DdbTensorValue>;
219
250
  /** DdbObj.data() 返回的表格对象 */
220
251
  export interface DdbTableData<TRow = any> {
221
252
  /** 表名 */
@@ -227,6 +258,11 @@ export interface DdbTableData<TRow = any> {
227
258
  /** 表格数据,每一行的对象组成的数组 */
228
259
  data: TRow[];
229
260
  }
261
+ /** DdbObj.data() 返回的 Tensor 对象 */
262
+ export interface DdbTensorData extends DdbTensorMetadata {
263
+ /** 数据 */
264
+ data: TensorData;
265
+ }
230
266
  /** DdbObj.data() 返回的矩阵对象 */
231
267
  export interface DdbMatrixData {
232
268
  /** 原始数据类型 */
@@ -294,7 +330,7 @@ export declare class DdbObj<TValue extends DdbValue = DdbValue> {
294
330
  pack(): Uint8Array;
295
331
  static pack_vector_body(value: DdbVectorValue, type: DdbType, length: number): ArrayBufferView[];
296
332
  /** 将 DdbObj 转换为 js 原生数据类型
297
- - 标量对应 number, bigint 或者字符串
333
+ - 标量对应 number, bigint 或者字符串 (其中时间类型转换为常用的字符串表示)
298
334
  - 数组对应 js 原生数组
299
335
  - 表格对应 DdbTableData
300
336
  - 矩阵对应 DdbMatrixData
@@ -302,6 +338,20 @@ export declare class DdbObj<TValue extends DdbValue = DdbValue> {
302
338
  - 图对应 DdbChartValue */
303
339
  data<TResult extends any[]>(this: DdbVectorObj, options?: ConvertOptions): TResult;
304
340
  data<TResult = any>(this: DdbObj, options?: ConvertOptions): TResult;
341
+ /** 构建 Tensor
342
+ @param buildParams 构建参数
343
+ @param limit 限制每个维度最大元素个数(仅用于 log,不作他用)
344
+ @returns */
345
+ static parse_tensor(buildParams: {
346
+ currentDim: number;
347
+ dimensions: number;
348
+ rawData: Uint8Array;
349
+ le: boolean;
350
+ dataByte: number;
351
+ dataType: DdbType;
352
+ shape: number[];
353
+ strides: number[];
354
+ }, limit?: number): TensorData;
305
355
  toString(options?: InspectOptions): string;
306
356
  inspect_type(): string;
307
357
  /** 自动转换 js string, boolean 为 DdbObj */
@@ -357,7 +407,7 @@ export interface ConvertOptions {
357
407
  /** `'string'` blob 类型数据的格式 */
358
408
  blob?: 'string' | 'binary';
359
409
  }
360
- 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[];
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;
361
411
  /** 转换一个向量到 js 原生数组 */
362
412
  export declare function converts(type: DdbType, value: DdbVectorValue, rows: number, le: boolean, options?: ConvertOptions): any[];
363
413
  export declare class DdbVoid extends DdbObj<undefined> {
package/browser.js CHANGED
@@ -33,6 +33,7 @@ export var DdbForm;
33
33
  DdbForm[DdbForm["chunk"] = 8] = "chunk";
34
34
  /** sysobj */
35
35
  DdbForm[DdbForm["object"] = 9] = "object";
36
+ DdbForm[DdbForm["tensor"] = 10] = "tensor";
36
37
  })(DdbForm || (DdbForm = {}));
37
38
  /** DolphinDB DataType
38
39
  对应的 array vector 类型为 64 + 基本类型
@@ -129,6 +130,16 @@ export var DdbVoidType;
129
130
  DdbVoidType[DdbVoidType["default"] = 2] = "default";
130
131
  })(DdbVoidType || (DdbVoidType = {}));
131
132
  export const dictables = new Set([DdbType.any, DdbType.string, DdbType.double, DdbType.float, DdbType.int, DdbType.long]);
133
+ /** 工具,取得某个 DdbType 的字节数 */
134
+ export const ddb_tensor_bytes = {
135
+ [DdbType.bool]: 1,
136
+ [DdbType.char]: 1,
137
+ [DdbType.short]: 2,
138
+ [DdbType.int]: 4,
139
+ [DdbType.long]: 8,
140
+ [DdbType.float]: 4,
141
+ [DdbType.double]: 8,
142
+ };
132
143
  /** 可以表示所有 DolphinDB 数据库中的数据类型 Can represent data types in all DolphinDB databases */
133
144
  export class DdbObj {
134
145
  static dec = new TextDecoder('utf-8');
@@ -366,6 +377,47 @@ export class DdbObj {
366
377
  },
367
378
  });
368
379
  }
380
+ case DdbForm.tensor: {
381
+ // 元数据
382
+ const tensorType = buf[2];
383
+ const deviceType = buf[3];
384
+ const dv = new DataView(buf.buffer, buf.byteOffset);
385
+ const tensorFlags = dv.getUint32(4, le);
386
+ const dimensions = dv.getInt32(8, le);
387
+ const shapes = [];
388
+ const strides = [];
389
+ const shapeStart = 12;
390
+ const stridesStart = shapeStart + dimensions * 8;
391
+ const preserveValueStart = stridesStart + dimensions * 8;
392
+ const preserveValue = dv.getBigInt64(preserveValueStart, le);
393
+ const storageStart = preserveValueStart + 8;
394
+ const elemCount = dv.getBigInt64(storageStart, le);
395
+ const dataStart = storageStart + 8;
396
+ for (let d = 0; d < dimensions; d++) {
397
+ const getNumOffset = d * 8;
398
+ shapes.push(Number(dv.getBigInt64(shapeStart + getNumOffset, le)));
399
+ strides.push(Number(dv.getBigInt64(stridesStart + getNumOffset, le)));
400
+ }
401
+ const dataBuffer = buf.subarray(dataStart);
402
+ return new this({
403
+ le,
404
+ form,
405
+ type,
406
+ length: i_data + buf_data.length,
407
+ value: {
408
+ data_type: type,
409
+ tensor_type: tensorType,
410
+ device_type: deviceType,
411
+ tensor_flags: tensorFlags,
412
+ dimensions,
413
+ shape: shapes,
414
+ strides,
415
+ preserve_value: preserveValue,
416
+ elem_count: Number(elemCount),
417
+ data: dataBuffer
418
+ }
419
+ });
420
+ }
369
421
  default:
370
422
  return new this({
371
423
  le,
@@ -1016,6 +1068,38 @@ export class DdbObj {
1016
1068
  ...DdbObj.pack_vector_body(data, this.type, this.rows * this.cols)
1017
1069
  ];
1018
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
+ }
1019
1103
  default:
1020
1104
  throw new Error(t('vector {{type}} 暂不支持序列化', { type: get_type_name(type) }));
1021
1105
  }
@@ -1166,10 +1250,99 @@ export class DdbObj {
1166
1250
  data: seq(rows, i => seq(ncolumns, j => jsdata[j * rows + i]))
1167
1251
  };
1168
1252
  }
1253
+ case DdbForm.tensor: {
1254
+ const { data_type, tensor_type, device_type, tensor_flags, dimensions, shape, strides, preserve_value, elem_count, data, } = this.value;
1255
+ const dataByte = ddb_tensor_bytes[data_type];
1256
+ const returnData = {
1257
+ data_type,
1258
+ tensor_type,
1259
+ device_type,
1260
+ tensor_flags,
1261
+ dimensions,
1262
+ shape,
1263
+ strides,
1264
+ preserve_value,
1265
+ elem_count,
1266
+ data: DdbObj.parse_tensor({ currentDim: 0, dimensions, rawData: data, le: this.le, dataByte, dataType: data_type, shape, strides })
1267
+ };
1268
+ return returnData;
1269
+ }
1169
1270
  default:
1170
1271
  throw new Error(t('{{form}} {{type}} 暂不支持 data()', { form, type: get_type_name(type) }));
1171
1272
  }
1172
1273
  }
1274
+ /** 构建 Tensor
1275
+ @param buildParams 构建参数
1276
+ @param limit 限制每个维度最大元素个数(仅用于 log,不作他用)
1277
+ @returns */
1278
+ static parse_tensor(buildParams, limit = -1) {
1279
+ const { currentDim, dimensions, rawData, le, dataByte, dataType, shape, strides } = buildParams;
1280
+ const tensor = [];
1281
+ const dv = new DataView(rawData.buffer, rawData.byteOffset);
1282
+ for (let i = 0; i < shape[currentDim]; i++)
1283
+ if (currentDim >= dimensions - 1) {
1284
+ if (limit > 0 && i >= limit) {
1285
+ tensor.push('...');
1286
+ break;
1287
+ }
1288
+ // 直接转换到对应的数组
1289
+ const offset = i * dataByte * Number(strides[currentDim]);
1290
+ switch (dataType) {
1291
+ case DdbType.bool: {
1292
+ const value = dv.getInt8(offset);
1293
+ tensor.push(value === nulls.int8 ? null : Boolean(value));
1294
+ break;
1295
+ }
1296
+ case DdbType.char: {
1297
+ const value = dv.getInt8(offset);
1298
+ tensor.push(value === nulls.int8 ? null : value);
1299
+ break;
1300
+ }
1301
+ case DdbType.short: {
1302
+ const value = dv.getInt16(offset, le);
1303
+ tensor.push(value === nulls.int16 ? null : value);
1304
+ break;
1305
+ }
1306
+ case DdbType.int: {
1307
+ const value = dv.getInt32(offset, le);
1308
+ tensor.push(value === nulls.int32 ? null : value);
1309
+ break;
1310
+ }
1311
+ case DdbType.long: {
1312
+ const value = dv.getBigInt64(offset, le);
1313
+ tensor.push(value === nulls.int64 ? null : value);
1314
+ break;
1315
+ }
1316
+ case DdbType.float: {
1317
+ const value = dv.getFloat32(offset, le);
1318
+ tensor.push(value === nulls.float32 ? null : value);
1319
+ break;
1320
+ }
1321
+ case DdbType.double: {
1322
+ const value = dv.getFloat64(offset, le);
1323
+ tensor.push(value === nulls.double ? null : value);
1324
+ break;
1325
+ }
1326
+ }
1327
+ }
1328
+ else {
1329
+ // 起点
1330
+ const start = strides[currentDim] * i * dataByte;
1331
+ // 终点
1332
+ const end = start + strides[currentDim] * 1 * dataByte;
1333
+ tensor.push(DdbObj.parse_tensor({
1334
+ currentDim: currentDim + 1,
1335
+ dimensions,
1336
+ rawData: rawData.subarray(Number(start), Number(end)),
1337
+ le,
1338
+ dataByte,
1339
+ shape,
1340
+ strides,
1341
+ dataType
1342
+ }, limit));
1343
+ }
1344
+ return tensor;
1345
+ }
1173
1346
  toString(options = { nullstr: true, quote: true }) {
1174
1347
  const type = this.inspect_type();
1175
1348
  const data = (() => {
@@ -1298,6 +1471,20 @@ export class DdbObj {
1298
1471
  }
1299
1472
  case DdbForm.chart:
1300
1473
  return JSON.stringify(this.value, (key, value) => key === 'data' ? value.toString(options) : value, 4);
1474
+ case DdbForm.tensor: {
1475
+ const tensorVal = this.value;
1476
+ const retd = DdbObj.parse_tensor({
1477
+ currentDim: 0,
1478
+ dimensions: tensorVal.dimensions,
1479
+ rawData: tensorVal.data,
1480
+ le: this.le,
1481
+ dataByte: ddb_tensor_bytes[tensorVal.data_type],
1482
+ dataType: tensorVal.data_type,
1483
+ shape: tensorVal.shape,
1484
+ strides: tensorVal.strides
1485
+ }, 5);
1486
+ return JSON.stringify(retd).replaceAll('"..."', '...');
1487
+ }
1301
1488
  }
1302
1489
  return String(this.value);
1303
1490
  })();
@@ -1330,6 +1517,8 @@ export class DdbObj {
1330
1517
  return `chart<${DdbChartType[this.value.type]}>`;
1331
1518
  case DdbForm.matrix:
1332
1519
  return `matrix<${tname}>[${this.rows}r][${this.cols}c]`;
1520
+ case DdbForm.tensor:
1521
+ return `tensor<${generate_array_type(DdbType[this.value.data_type], this.value.shape)}>`;
1333
1522
  default:
1334
1523
  return `${DdbForm[this.form]} ${tname}`;
1335
1524
  }
@@ -3361,6 +3550,13 @@ function set_big_uint_128(dataView, byte_offset, value, le = true) {
3361
3550
  function set_big_int_128(dataview, byte_offset, value, le = true) {
3362
3551
  set_big_uint_128(dataview, byte_offset, value, le);
3363
3552
  }
3553
+ function generate_array_type(baseType, dimensions) {
3554
+ let result = baseType;
3555
+ dimensions.forEach(dimension => {
3556
+ result += `[${dimension}]`;
3557
+ });
3558
+ return result;
3559
+ }
3364
3560
  // 大端
3365
3561
  // const dataBE = new ArrayBuffer(16)
3366
3562
  // const dataViewBE = new DataView(dataBE)