dolphindb 2.0.1013 → 2.0.1015

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
@@ -320,15 +320,17 @@ export declare function int1282str(buffer: Uint8Array, le?: boolean): string;
320
320
  export interface StreamingParams {
321
321
  table: string;
322
322
  action?: string;
323
- handler(message: StreamingData): any;
323
+ handler(message: StreamingMessage): any;
324
324
  }
325
- export interface StreamingData extends StreamingParams {
325
+ export interface StreamingMessage extends StreamingParams {
326
326
  /** server 发送消息的时间 (nano seconds since epoch) The time the server sent the message (nano seconds since epoch)
327
327
  std::chrono::system_clock::now().time_since_epoch() / std::chrono::nanoseconds(1) */
328
328
  time: bigint;
329
329
  /** message id */
330
330
  id: bigint;
331
331
  colnames: string[];
332
+ /** 最新的 server 有可能先推一个 table schema 过来 */
333
+ schema?: DdbTableObj;
332
334
  /** 订阅主题,即一个订阅的名称。 Subscription topic, which is the name of a subscription.
333
335
  它是一个字符串,由订阅表所在节点的别名、流数据表名称和订阅任务名称(如果指定了 actionName)组合而成,使用 `/` 分隔
334
336
  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 `/` */
@@ -417,7 +419,7 @@ export declare class DDB {
417
419
  /** 表示本次会话执行的 SQL 标准 */
418
420
  sql: SqlStandard;
419
421
  /** 是否为流数据连接,非流数据这个字段恒为 null Whether it is a streaming data connection, this field is always null for non-streaming data */
420
- streaming: StreamingData;
422
+ streaming: StreamingParams;
421
423
  /** 是否打印每个 rpc 的信息用于调试 */
422
424
  verbose: boolean;
423
425
  print_message_buffer: boolean;
@@ -462,7 +464,7 @@ export declare class DDB {
462
464
  constructor(url: string, options?: DdbOptions);
463
465
  private on_message;
464
466
  private on_error;
465
- /** 调用后会确保和数据库的连接是正常的 (this.connected === true && this.errored === false),否则抛出错误
467
+ /** 调用后会确保和数据库的连接是正常的 (this.connected === true),否则抛出错误
466
468
  这个方法是幂等的,首次调用建立实际的 WebSocket 连接到 URL 对应的 DolphinDB,然后执行自动登录,
467
469
  如果是流数据连接,还会调用 publishTable 订阅流表
468
470
  后续调用检查上面的条件
@@ -471,7 +473,7 @@ export declare class DDB {
471
473
  2. session 是有状态的,重连也无法恢复之前的状态
472
474
  3. 断线后所有之前的 ddb.call, ddb.eval 都应该抛出连接错误
473
475
 
474
- After calling, it will ensure that the connection with the database is normal (this.connected === true && this.errored === false), otherwise an error will be thrown
476
+ After calling, it will ensure that the connection with the database is normal (this.connected === true), otherwise an error will be thrown
475
477
  This method is idempotent, the first call establishes an actual WebSocket connection to the DolphinDB corresponding to the URL, and subsequent calls check the above conditions
476
478
  After the connection is disconnected, it is forbidden to call connect again to reconnect the original ddb object. You should use new DDB() to create a new connection object because:
477
479
  1. The on_error callback is bound to a certain websocket, and it is inconvenient to unbind and rebind
@@ -582,7 +584,7 @@ export declare class DDB {
582
584
  /** 解析服务端响应报文,返回去掉 header 的 data buf */
583
585
  parse_message(buf: Uint8Array, error: DdbDatabaseError): DdbMessage;
584
586
  /** 内部的流订阅方法 Internal stream subscription method */
585
- subscribe(): Promise<StreamingData>;
587
+ subscribe(): Promise<void>;
586
588
  }
587
589
  export interface DdbMessageListener {
588
590
  (message: DdbMessage, _this: DDB): any;
package/index.js CHANGED
@@ -2219,7 +2219,7 @@ export class DDB {
2219
2219
  on_error() {
2220
2220
  // 这里的实现一定会被 connect, rpc 中的实现覆盖
2221
2221
  }
2222
- /** 调用后会确保和数据库的连接是正常的 (this.connected === true && this.errored === false),否则抛出错误
2222
+ /** 调用后会确保和数据库的连接是正常的 (this.connected === true),否则抛出错误
2223
2223
  这个方法是幂等的,首次调用建立实际的 WebSocket 连接到 URL 对应的 DolphinDB,然后执行自动登录,
2224
2224
  如果是流数据连接,还会调用 publishTable 订阅流表
2225
2225
  后续调用检查上面的条件
@@ -2228,7 +2228,7 @@ export class DDB {
2228
2228
  2. session 是有状态的,重连也无法恢复之前的状态
2229
2229
  3. 断线后所有之前的 ddb.call, ddb.eval 都应该抛出连接错误
2230
2230
 
2231
- After calling, it will ensure that the connection with the database is normal (this.connected === true && this.errored === false), otherwise an error will be thrown
2231
+ After calling, it will ensure that the connection with the database is normal (this.connected === true), otherwise an error will be thrown
2232
2232
  This method is idempotent, the first call establishes an actual WebSocket connection to the DolphinDB corresponding to the URL, and subsequent calls check the above conditions
2233
2233
  After the connection is disconnected, it is forbidden to call connect again to reconnect the original ddb object. You should use new DDB() to create a new connection object because:
2234
2234
  1. The on_error callback is bound to a certain websocket, and it is inconvenient to unbind and rebind
@@ -2640,9 +2640,7 @@ export class DDB {
2640
2640
  }
2641
2641
  /** 内部的流订阅方法 Internal stream subscription method */
2642
2642
  async subscribe() {
2643
- console.log(t('订阅流表成功') + ', colnames:',
2644
- // string[3](['time', 'stock', 'price'])
2645
- this.streaming.colnames = (await this.call('publishTable', [
2643
+ const { value: colnames } = await this.call('publishTable', [
2646
2644
  'localhost',
2647
2645
  new DdbInt(0),
2648
2646
  this.streaming.table,
@@ -2650,27 +2648,30 @@ export class DDB {
2650
2648
  // new DdbInt(-1), // offset
2651
2649
  // filter
2652
2650
  // allow exists
2653
- ], { skip_connection_check: true })).value);
2654
- this.streaming.window = {
2651
+ ], { skip_connection_check: true });
2652
+ console.log(t('订阅流表成功') + ', colnames:',
2653
+ // string[3](['time', 'stock', 'price'])
2654
+ colnames);
2655
+ let win = {
2655
2656
  offset: 0,
2656
2657
  rows: 0,
2657
2658
  segments: [],
2658
2659
  };
2660
+ let schema;
2659
2661
  // 先准备好收到 websocket message 的 callback
2660
2662
  this.on_message = buffer => {
2661
- // 复制一份,避免 handler 中异步直接使用传入的 streaming 这个对象时属性发生变化
2662
- let streaming = { ...this.streaming, error: null };
2663
2663
  try {
2664
2664
  const dv = new DataView(buffer);
2665
2665
  const buf = new Uint8Array(buffer);
2666
- streaming.time = dv.getBigInt64(1, this.le);
2667
- streaming.id = dv.getBigInt64(9, this.le);
2668
2666
  const i_topic_end = buf.indexOf(0, 17);
2669
- streaming.topic = this.dec.decode(buf.subarray(17, i_topic_end));
2670
- // 是 column 片段组成的 any vector
2671
- const data = streaming.data = DdbObj.parse(buf.subarray(i_topic_end + 1), this.le);
2672
- let { window: win } = streaming;
2673
- win.rows += streaming.rows = data.value[0].rows;
2667
+ // 首个 message 可能是 table schema, 后续消息是 column 片段组成的 any vector
2668
+ const data = DdbObj.parse(buf.subarray(i_topic_end + 1), this.le);
2669
+ if (data.form === DdbForm.table) {
2670
+ schema = data;
2671
+ return;
2672
+ }
2673
+ const { rows } = data.value[0];
2674
+ win.rows += rows;
2674
2675
  win.segments.push(data);
2675
2676
  if (win.rows >= winsize * 2 && win.segments.length >= 2) {
2676
2677
  let winsize_ = 0;
@@ -2682,14 +2683,23 @@ export class DDB {
2682
2683
  win.offset += win.rows - winsize_;
2683
2684
  win.rows = winsize_;
2684
2685
  }
2686
+ this.streaming.handler({
2687
+ ...this.streaming,
2688
+ id: dv.getBigInt64(9, this.le),
2689
+ time: dv.getBigInt64(1, this.le),
2690
+ rows,
2691
+ topic: this.dec.decode(buf.subarray(17, i_topic_end)),
2692
+ colnames,
2693
+ schema,
2694
+ data,
2695
+ window: win,
2696
+ });
2685
2697
  }
2686
2698
  catch (error) {
2687
2699
  // 将 error 交给 handler 处理
2688
- streaming.error = error;
2700
+ this.streaming.handler({ ...this.streaming, error });
2689
2701
  }
2690
- this.streaming.handler(streaming);
2691
2702
  };
2692
- return this.streaming;
2693
2703
  }
2694
2704
  }
2695
2705
  // const ddb_gateway = {