dolphindb 0.0.15 → 2.0.500

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
@@ -81,17 +81,24 @@ export interface DdbArrayVectorBlock {
81
81
  export declare type DdbValue = null | boolean | number | [number, number] | bigint | string | string[] | Uint8Array | Int16Array | Int32Array | Float32Array | Float64Array | BigInt64Array | Uint8Array[] | DdbObj[] | DdbFunctionDefValue | DdbSymbolExtendedValue | DdbArrayVectorBlock[];
82
82
  export declare type DdbVectorValue = string | string[] | Uint8Array | Int16Array | Int32Array | Float32Array | Float64Array | BigInt64Array | Uint8Array[] | DdbObj[] | DdbSymbolExtendedValue | DdbArrayVectorBlock[];
83
83
  export declare const nulls: {
84
+ readonly bool: 128;
84
85
  readonly char: "€";
85
86
  readonly int16: -32768;
86
87
  readonly int32: -2147483648;
87
88
  readonly int64: -9223372036854775808n;
89
+ readonly float32: -3.4028234663852886e+38;
90
+ /** -Number.MAX_VALUE */
91
+ readonly double: number;
92
+ readonly bytes16: Uint8Array;
88
93
  };
89
94
  export declare const timezone_offset: number;
90
95
  /** 可以表示所有 DolphinDB 数据库中的数据类型 */
91
96
  export declare class DdbObj<T extends DdbValue = DdbValue> {
92
97
  static dec: TextDecoder;
93
98
  static enc: TextEncoder;
94
- /** 维护已解析的 symbol base,比如流数据中后续的 symbol 向量可能只发送一个 base.id, base.size == 0, 依赖之前发送的 symbol base */
99
+ /** 维护已解析的 symbol base,比如流数据中后续的 symbol 向量可能只发送一个 base.id, base.size == 0, 依赖之前发送的 symbol base ?
100
+ 只是暂存,如果一张表有多个 symbol 列,可能这个 symbol base 会被复用,不同的对象之间 symbol base 一般不复用
101
+ */
95
102
  static symbol_bases: Record<number, string[]>;
96
103
  /** little endian (client) */
97
104
  static le_client: boolean;
@@ -147,46 +154,49 @@ export declare class DdbVoid extends DdbObj<undefined> {
147
154
  constructor();
148
155
  }
149
156
  export declare class DdbBool extends DdbObj<boolean> {
150
- constructor(value: boolean);
157
+ constructor(value: boolean | null);
151
158
  }
152
159
  export declare class DdbChar extends DdbObj<string> {
153
- constructor(value: string);
160
+ constructor(value: string | null);
154
161
  }
155
162
  export declare class DdbInt extends DdbObj<number> {
156
- constructor(value: number);
163
+ constructor(value: number | null);
157
164
  }
158
165
  export declare class DdbString extends DdbObj<string> {
159
166
  constructor(value: string);
160
167
  }
161
168
  export declare class DdbLong extends DdbObj<bigint> {
162
- constructor(value: bigint);
169
+ constructor(value: bigint | null);
163
170
  }
164
171
  export declare class DdbDouble extends DdbObj<number> {
165
- constructor(value: number);
172
+ constructor(value: number | null);
166
173
  }
167
174
  export declare class DdbDateTime extends DdbObj<number> {
168
- constructor(value: number);
175
+ constructor(value: number | null);
169
176
  }
170
177
  export declare class DdbTimeStamp extends DdbObj<bigint> {
171
- constructor(value: bigint);
178
+ constructor(value: bigint | null);
172
179
  }
173
180
  export declare class DdbNanoTimeStamp extends DdbObj<bigint> {
174
- constructor(value: bigint);
181
+ constructor(value: bigint | null);
175
182
  }
176
183
  export declare class DdbVectorInt extends DdbObj<Int32Array> {
177
- constructor(value: number[], name?: string);
184
+ constructor(value: (number | null)[] | Int32Array, name?: string);
178
185
  }
179
186
  export declare class DdbVectorString extends DdbObj<string[]> {
180
187
  constructor(value: string[], name?: string);
181
188
  }
182
189
  export declare class DdbVectorDouble extends DdbObj<Float64Array> {
183
- constructor(value: number[], name?: string);
190
+ constructor(value: (number | null)[] | Float64Array, name?: string);
184
191
  }
185
192
  export declare class DdbVectorAny extends DdbObj {
186
193
  constructor(value: DdbObj<DdbValue>[], name?: string);
187
194
  }
195
+ export declare class DdbVectorSymbol extends DdbObj<DdbSymbolExtendedValue> {
196
+ constructor(value: string[], name?: string);
197
+ }
188
198
  export declare class DdbPair extends DdbObj<Int32Array> {
189
- constructor(l: number, r?: number);
199
+ constructor(l: number | null, r?: number | null);
190
200
  }
191
201
  export declare class DdbFunction extends DdbObj<DdbFunctionDefValue> {
192
202
  constructor(name: string, type: DdbFunctionType);
@@ -248,6 +258,7 @@ export declare class DDB {
248
258
  presolver(buf: Uint8Array): void;
249
259
  prejector(error: Error): void;
250
260
  presult: Promise<Uint8Array>;
261
+ get connected(): boolean;
251
262
  /**
252
263
  Initialize an instance of DolphinDB Client using the WebSocket URL
253
264
  (without establishing an actual network connection)
@@ -348,7 +359,7 @@ export declare class DDB {
348
359
  };
349
360
  /** 自动转换 js string, boolean 为 DdbObj */
350
361
  to_ddbobj(value: DdbObj | string | boolean): DdbObj;
351
- /** 转换 js 数组为 DdbObj[] (in place, 会修改原数组) */
362
+ /** 转换 js 数组为 DdbObj[] */
352
363
  to_ddbobjs(values: any[]): DdbObj<DdbValue>[];
353
364
  }
354
365
  export default DDB;
package/index.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.DDB = exports.str2nanotimestamp = exports.nanotimestamp2str = exports.str2timestamp = exports.timestamp2str = exports.datetime2str = exports.date2str = exports.DdbFunction = exports.DdbPair = exports.DdbVectorAny = exports.DdbVectorDouble = exports.DdbVectorString = exports.DdbVectorInt = exports.DdbNanoTimeStamp = exports.DdbTimeStamp = exports.DdbDateTime = exports.DdbDouble = exports.DdbLong = exports.DdbString = exports.DdbInt = exports.DdbChar = exports.DdbBool = exports.DdbVoid = exports.DdbObj = exports.timezone_offset = exports.nulls = exports.DdbFunctionType = exports.ddb_sizes = exports.DdbType = exports.DdbForm = void 0;
3
+ exports.DDB = exports.str2nanotimestamp = exports.nanotimestamp2str = exports.str2timestamp = exports.timestamp2str = exports.datetime2str = exports.date2str = exports.DdbFunction = exports.DdbPair = exports.DdbVectorSymbol = exports.DdbVectorAny = exports.DdbVectorDouble = exports.DdbVectorString = exports.DdbVectorInt = exports.DdbNanoTimeStamp = exports.DdbTimeStamp = exports.DdbDateTime = exports.DdbDouble = exports.DdbLong = exports.DdbString = exports.DdbInt = exports.DdbChar = exports.DdbBool = exports.DdbVoid = exports.DdbObj = exports.timezone_offset = exports.nulls = exports.DdbFunctionType = exports.ddb_sizes = exports.DdbType = exports.DdbForm = void 0;
4
4
  const tslib_1 = require("tslib");
5
5
  const ws_1 = require("ws");
6
6
  const dayjs_1 = tslib_1.__importDefault(require("dayjs"));
@@ -109,10 +109,15 @@ var DdbFunctionType;
109
109
  DdbFunctionType[DdbFunctionType["JitPartialFunc"] = 8] = "JitPartialFunc";
110
110
  })(DdbFunctionType = exports.DdbFunctionType || (exports.DdbFunctionType = {}));
111
111
  exports.nulls = {
112
+ bool: 0x80,
112
113
  char: '\x80',
113
114
  int16: -32768,
114
115
  int32: -2147483648,
115
- int64: -0x8000000000000000n, // -922_3372_0368_5477_5808
116
+ int64: -0x8000000000000000n,
117
+ float32: -3.4028234663852886e+38,
118
+ /** -Number.MAX_VALUE */
119
+ double: -Number.MAX_VALUE,
120
+ bytes16: Uint8Array.from(new Array(16).fill(0))
116
121
  };
117
122
  exports.timezone_offset = 1000 * 60 * new Date().getTimezoneOffset();
118
123
  /** 可以表示所有 DolphinDB 数据库中的数据类型 */
@@ -129,7 +134,7 @@ class DdbObj {
129
134
  form: DdbForm.scalar,
130
135
  type: DdbType.void,
131
136
  length: 0,
132
- value: undefined
137
+ value: null
133
138
  });
134
139
  const type = buf[0];
135
140
  const form = buf[1];
@@ -278,13 +283,18 @@ class DdbObj {
278
283
  switch (type) {
279
284
  case DdbType.void:
280
285
  return [1, null];
281
- case DdbType.bool:
282
- return [1, Boolean(buf[0])];
283
- case DdbType.char:
284
- return [1, String.fromCharCode(buf[0])];
286
+ case DdbType.bool: {
287
+ const value = buf[0];
288
+ return [1, value === exports.nulls.bool ? null : Boolean(value)];
289
+ }
290
+ case DdbType.char: {
291
+ const value = String.fromCharCode(buf[0]);
292
+ return [1, value === exports.nulls.char ? null : value];
293
+ }
285
294
  case DdbType.short: {
286
295
  const dv = new DataView(buf.buffer, buf.byteOffset);
287
- return [2, dv.getInt16(0, le)];
296
+ const value = dv.getInt16(0, le);
297
+ return [2, value === exports.nulls.int16 ? null : value];
288
298
  }
289
299
  case DdbType.int:
290
300
  // datetime
@@ -296,15 +306,18 @@ class DdbObj {
296
306
  case DdbType.datetime:
297
307
  case DdbType.datehour: {
298
308
  const dv = new DataView(buf.buffer, buf.byteOffset);
299
- return [4, dv.getInt32(0, le)];
309
+ const value = dv.getInt32(0, le);
310
+ return [4, value === exports.nulls.int32 ? null : value];
300
311
  }
301
312
  case DdbType.float: {
302
313
  const dv = new DataView(buf.buffer, buf.byteOffset);
303
- return [4, dv.getFloat32(0, le)];
314
+ const value = dv.getFloat32(0, le);
315
+ return [4, value === exports.nulls.float32 ? null : value];
304
316
  }
305
317
  case DdbType.double: {
306
318
  const dv = new DataView(buf.buffer, buf.byteOffset);
307
- return [8, dv.getFloat64(0, le)];
319
+ const value = dv.getFloat64(0, le);
320
+ return [8, value === exports.nulls.double ? null : value];
308
321
  }
309
322
  case DdbType.long:
310
323
  // timestamp
@@ -312,7 +325,8 @@ class DdbObj {
312
325
  case DdbType.nanotime:
313
326
  case DdbType.nanotimestamp: {
314
327
  const dv = new DataView(buf.buffer, buf.byteOffset);
315
- return [8, dv.getBigInt64(0, le)];
328
+ const value = dv.getBigInt64(0, le);
329
+ return [8, value === exports.nulls.int64 ? null : value];
316
330
  }
317
331
  case DdbType.string:
318
332
  case DdbType.symbol:
@@ -613,11 +627,26 @@ class DdbObj {
613
627
  case DdbType.void:
614
628
  return [Uint8Array.of(1)];
615
629
  case DdbType.bool:
616
- return [Uint8Array.of(Number(value))];
630
+ return [
631
+ Uint8Array.of(value === null ?
632
+ exports.nulls.bool
633
+ :
634
+ Number(value))
635
+ ];
617
636
  case DdbType.char:
618
- return [Uint8Array.of(value.charCodeAt(0))];
637
+ return [
638
+ Uint8Array.of((value === null ?
639
+ exports.nulls.char
640
+ :
641
+ value).charCodeAt(0))
642
+ ];
619
643
  case DdbType.short:
620
- return [Int16Array.of(value)];
644
+ return [
645
+ Int16Array.of(value === null ?
646
+ exports.nulls.int16
647
+ :
648
+ value)
649
+ ];
621
650
  case DdbType.int:
622
651
  // datetime
623
652
  case DdbType.date:
@@ -627,17 +656,37 @@ class DdbObj {
627
656
  case DdbType.second:
628
657
  case DdbType.datetime:
629
658
  case DdbType.datehour:
630
- return [Int32Array.of(value)];
659
+ return [
660
+ Int32Array.of(value === null ?
661
+ exports.nulls.int32
662
+ :
663
+ value)
664
+ ];
631
665
  case DdbType.float:
632
- return [Float32Array.of(value)];
666
+ return [
667
+ Float32Array.of(value === null ?
668
+ exports.nulls.float32
669
+ :
670
+ value)
671
+ ];
633
672
  case DdbType.double:
634
- return [Float64Array.of(value)];
673
+ return [
674
+ Float64Array.of(value === null ?
675
+ exports.nulls.double
676
+ :
677
+ value)
678
+ ];
635
679
  case DdbType.long:
636
680
  // timestamp
637
681
  case DdbType.timestamp:
638
682
  case DdbType.nanotime:
639
683
  case DdbType.nanotimestamp:
640
- return [BigInt64Array.of(this.value)];
684
+ return [
685
+ BigInt64Array.of(value === null ?
686
+ exports.nulls.int64
687
+ :
688
+ value)
689
+ ];
641
690
  case DdbType.string:
642
691
  case DdbType.symbol:
643
692
  case DdbType.code:
@@ -801,7 +850,7 @@ class DdbObj {
801
850
  case DdbType.symbol_extended: {
802
851
  const { base_id, base, data } = value;
803
852
  return [
804
- Uint32Array.of(base_id, base.length),
853
+ Uint32Array.of(base_id || 0, base.length),
805
854
  ...this.pack_vector_body(base, DdbType.string, base.length),
806
855
  data
807
856
  ];
@@ -888,7 +937,9 @@ class DdbObj {
888
937
  exports.DdbObj = DdbObj;
889
938
  DdbObj.dec = new TextDecoder('utf-8');
890
939
  DdbObj.enc = new TextEncoder();
891
- /** 维护已解析的 symbol base,比如流数据中后续的 symbol 向量可能只发送一个 base.id, base.size == 0, 依赖之前发送的 symbol base */
940
+ /** 维护已解析的 symbol base,比如流数据中后续的 symbol 向量可能只发送一个 base.id, base.size == 0, 依赖之前发送的 symbol base ?
941
+ 只是暂存,如果一张表有多个 symbol 列,可能这个 symbol base 会被复用,不同的对象之间 symbol base 一般不复用
942
+ */
892
943
  DdbObj.symbol_bases = {};
893
944
  /** little endian (client) */
894
945
  DdbObj.le_client = Boolean(new Uint8Array(Uint32Array.of(1).buffer)[0]);
@@ -898,6 +949,7 @@ class DdbVoid extends DdbObj {
898
949
  form: DdbForm.scalar,
899
950
  type: DdbType.void,
900
951
  length: 0,
952
+ value: null,
901
953
  });
902
954
  }
903
955
  }
@@ -930,7 +982,7 @@ class DdbInt extends DdbObj {
930
982
  form: DdbForm.scalar,
931
983
  type: DdbType.int,
932
984
  length: 4,
933
- value
985
+ value,
934
986
  });
935
987
  }
936
988
  }
@@ -1009,7 +1061,13 @@ class DdbVectorInt extends DdbObj {
1009
1061
  length: 0,
1010
1062
  rows: value.length,
1011
1063
  cols: 1,
1012
- value: Int32Array.from(value),
1064
+ value: value instanceof Int32Array ?
1065
+ value
1066
+ :
1067
+ Int32Array.from(value, v => v === null ?
1068
+ exports.nulls.int32
1069
+ :
1070
+ v),
1013
1071
  name,
1014
1072
  });
1015
1073
  }
@@ -1037,7 +1095,13 @@ class DdbVectorDouble extends DdbObj {
1037
1095
  length: 0,
1038
1096
  rows: value.length,
1039
1097
  cols: 1,
1040
- value: Float64Array.from(value),
1098
+ value: value instanceof Float64Array ?
1099
+ value
1100
+ :
1101
+ Float64Array.from(value, v => v === null ?
1102
+ exports.nulls.double
1103
+ :
1104
+ v),
1041
1105
  name,
1042
1106
  });
1043
1107
  }
@@ -1057,15 +1121,46 @@ class DdbVectorAny extends DdbObj {
1057
1121
  }
1058
1122
  }
1059
1123
  exports.DdbVectorAny = DdbVectorAny;
1124
+ class DdbVectorSymbol extends DdbObj {
1125
+ constructor(value, name) {
1126
+ let map = new Map([
1127
+ ['', 0]
1128
+ ]);
1129
+ let data = new Uint32Array(value.length);
1130
+ for (let i = 0; i < value.length; i++) {
1131
+ const x = value[i];
1132
+ let index = map.get(x);
1133
+ if (index === undefined) {
1134
+ index = map.size;
1135
+ map.set(x, index);
1136
+ }
1137
+ data[i] = index;
1138
+ }
1139
+ super({
1140
+ form: DdbForm.vector,
1141
+ type: DdbType.symbol_extended,
1142
+ length: 0,
1143
+ rows: value.length,
1144
+ cols: 1,
1145
+ value: {
1146
+ base: [...map.keys()],
1147
+ base_id: null,
1148
+ data,
1149
+ },
1150
+ name
1151
+ });
1152
+ }
1153
+ }
1154
+ exports.DdbVectorSymbol = DdbVectorSymbol;
1060
1155
  class DdbPair extends DdbObj {
1061
- constructor(l, r = -2147483648) {
1156
+ constructor(l, r = null) {
1062
1157
  super({
1063
1158
  form: DdbForm.pair,
1064
1159
  type: DdbType.int,
1065
1160
  length: 0,
1066
1161
  rows: 2,
1067
1162
  cols: 1,
1068
- value: Int32Array.of(l, r)
1163
+ value: Int32Array.of(l === null ? exports.nulls.int32 : l, r === null ? exports.nulls.int32 : r)
1069
1164
  });
1070
1165
  }
1071
1166
  }
@@ -1201,12 +1296,14 @@ class DDB {
1201
1296
  /** resolver, rejector, promise of last rpc */
1202
1297
  presolver(buf) { }
1203
1298
  prejector(error) { }
1299
+ get connected() {
1300
+ return this.websocket?.readyState === ws_1.WebSocket.OPEN;
1301
+ }
1204
1302
  /** Establish the actual WebSocket connection to the DolphinDB corresponding to the URL */
1205
1303
  async connect({ url = this.url, login = true, username = 'admin', password = '123456', python = false, } = {}) {
1206
1304
  this.url = url;
1207
1305
  this.python = python;
1208
- if (this.websocket?.readyState === ws_1.WebSocket.OPEN)
1209
- this.disconnect();
1306
+ this.disconnect();
1210
1307
  let websocket = new ws_1.WebSocket(url, python ? ['python'] : [], {
1211
1308
  maxPayload: 2 ** 33 // 8 GB
1212
1309
  });
@@ -1299,7 +1396,11 @@ class DDB {
1299
1396
  return `/ ${options.join('_')}`;
1300
1397
  }
1301
1398
  disconnect() {
1302
- this.websocket?.close(1000);
1399
+ if (this.connected)
1400
+ this.websocket.close(1000);
1401
+ this.presolver = buf => { };
1402
+ this.prejector = error => { };
1403
+ this.presult = Promise.resolve(null);
1303
1404
  }
1304
1405
  /** rpc through websocket (function/script/variable command)
1305
1406
  - type: API 类型: 'script' | 'function' | 'variable'
@@ -1310,7 +1411,7 @@ class DDB {
1310
1411
  async rpc(type, { script, func, args = [], vars = [], urgent, }) {
1311
1412
  if (!this.websocket)
1312
1413
  await this.connect();
1313
- if (this.websocket.readyState !== ws_1.WebSocket.OPEN)
1414
+ if (!this.connected)
1314
1415
  throw new Error(`${this.url} is already disconnected`);
1315
1416
  // 临界区:保证多个 rpc 并发时形成 promise 链
1316
1417
  // ddb 世界观:需要等待上一个 rpc 结果从 server 返回之后才能发起下一个调用
@@ -1333,7 +1434,7 @@ class DDB {
1333
1434
  // 临界区结束,只有一个 rpc 请求可以写 WebSocket
1334
1435
  if (urgent && type !== 'script' && type !== 'function')
1335
1436
  throw new Error('urgent 只适用于 script 和 funciton');
1336
- this.to_ddbobjs(args);
1437
+ args = this.to_ddbobjs(args);
1337
1438
  const command = this.enc.encode((() => {
1338
1439
  switch (type) {
1339
1440
  case 'function':
@@ -1479,11 +1580,9 @@ class DDB {
1479
1580
  throw new Error(`不能自动转换 ${type} 至 DdbObj`);
1480
1581
  }
1481
1582
  }
1482
- /** 转换 js 数组为 DdbObj[] (in place, 会修改原数组) */
1583
+ /** 转换 js 数组为 DdbObj[] */
1483
1584
  to_ddbobjs(values) {
1484
- for (let i = 0; i < values.length; i++)
1485
- values[i] = this.to_ddbobj(values[i]);
1486
- return values;
1585
+ return values.map(value => this.to_ddbobj(value));
1487
1586
  }
1488
1587
  }
1489
1588
  exports.DDB = DDB;