-
Notifications
You must be signed in to change notification settings - Fork 0
/
browser.ts
5587 lines (4490 loc) · 199 KB
/
browser.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import dayjs from 'dayjs'
import DayjsCustomParseFormat from 'dayjs/plugin/customParseFormat.js'
dayjs.extend(DayjsCustomParseFormat)
import ipaddrjs from 'ipaddr.js'
const { fromByteArray: buf2ipaddr } = ipaddrjs
import 'xshell/prototype.browser.js'
import { blue, cyan, green, grey, magenta } from 'xshell/chalk.browser.js'
import { concat, assert, Lock, genid, seq, zip_object, decode, delay } from 'xshell/utils.browser.js'
import { connect_websocket, type WebSocketConnectionError } from 'xshell/net.browser.js'
import { t } from './i18n/index.ts'
export const nulls = {
int8: -0x80, // -128
int16: -0x80_00, // -32768
int32: -0x80_00_00_00, // -21_4748_3648
int64: -0x80_00_00_00_00_00_00_00n, // -922_3372_0368_5477_5808
int128: -0x80_00_00_00_00_00_00_00_00_00_00_00_00_00_00_00n, // -170_1411_8346_0469_2317_3168_7303_7158_8410_5728
float32: -3.4028234663852886e+38,
/** -Number.MAX_VALUE */
double: -Number.MAX_VALUE,
bytes16: Uint8Array.from(
new Array(16).fill(0)
)
} as const
export enum DdbForm {
scalar = 0,
vector = 1,
pair = 2,
matrix = 3,
set = 4,
dict = 5,
table = 6,
chart = 7,
/** 节点内部通信可能会使用,调用函数执行脚本一般不会返回这种类型 */
chunk = 8,
/** sysobj */
object = 9,
tensor = 10,
}
/** DolphinDB DataType
对应的 array vector 类型为 64 + 基本类型
对应的 extended 类型为 128 + 基本类型 */
export enum DdbType {
void = 0,
bool = 1,
char = 2,
short = 3,
int = 4,
long = 5,
date = 6,
month = 7,
time = 8,
minute = 9,
second = 10,
datetime = 11,
timestamp = 12,
nanotime = 13,
nanotimestamp = 14,
float = 15,
double = 16,
symbol = 17,
string = 18,
uuid = 19,
functiondef = 20,
handle = 21,
code = 22,
datasource = 23,
resource = 24,
any = 25,
compressed = 26,
dict = 27,
datehour = 28,
ipaddr = 30,
int128 = 31,
blob = 32,
complex = 34,
point = 35,
duration = 36,
decimal32 = 37,
decimal64 = 38,
decimal128 = 39,
object = 40,
iotany = 41,
symbol_extended = 145, // 128 + DdbType.symbol
}
export enum DdbFunctionType {
SystemFunc = 0,
SystemProc = 1,
OperatorFunc = 2,
UserDefinedFunc = 3,
PartialFunc = 4,
DynamicFunc = 5,
PiecewiseFunc = 6,
JitFunc = 7,
JitPartialFunc = 8,
}
export enum DdbDurationUnit {
ns = 0,
us = 1,
ms = 2,
s = 3,
m = 4,
H = 5,
d = 6,
w = 7,
M = 8,
y = 9,
B = 10
}
export enum DdbChartType {
area = 0,
bar = 1,
column = 2,
histogram = 3,
line = 4,
pie = 5,
scatter = 6,
trend = 7,
kline = 8,
}
// server 实现中区分了 0: NULL (nothing), 1: NULL (null), 2: DFLT (default)
// Void::serialize()
// (isNothing() ? 0 : 1) + (isDefault_ ? 2 : 0);
export enum DdbVoidType {
undefined = 0,
null = 1,
default = 2
}
export interface DdbFunctionDefValue {
type: DdbFunctionType
name: string
}
export interface DdbDurationValue {
unit: DdbDurationUnit
/** int32 */
data: number
}
export interface DdbDecimal32Value {
/** int32, data 需要除以 10^scale 得到原值 */
scale: number
/** int32, 空值为 null ddb null is js null */
data: number | null
}
export interface DdbDecimal64Value {
/** int32, data 需要除以 10^scale 得到原值 */
scale: number
/** int64, 空值为 null empty value is null */
data: bigint | null
}
export interface DdbDecimal128Value {
/** int32, data 需要除以 10^scale 得到原值 */
scale: number
/** int128, 空值为 null empty value is null */
data: bigint | null
}
export interface DdbDecimal32VectorValue {
scale: number
data: Int32Array
}
export interface DdbDecimal64VectorValue {
scale: number
data: BigInt64Array
}
export interface DdbDecimal128VectorValue {
scale: number
data: BigInt128Array
}
export type DdbDecimalVectorValue = DdbDecimal32VectorValue | DdbDecimal64VectorValue | DdbDecimal128VectorValue
export type DdbDurationVectorValue = DdbDurationValue[]
export interface DdbSymbolExtendedValue {
base_id: number
base: string[]
data: Uint32Array
}
export interface DdbArrayVectorBlock {
unit: 1 | 2 | 4
rows: number
lengths: Uint8Array | Uint16Array | Uint32Array
data: Int8Array | Int16Array | Int32Array | Float32Array | Float64Array | BigInt64Array | BigInt128Array
}
export type DdbArrayVectorValue = DdbArrayVectorBlock[] & /* decimal 数据会有这个属性 */ { scale?: number }
export const dictables = new Set([DdbType.any, DdbType.string, DdbType.double, DdbType.float, DdbType.int, DdbType.long])
export interface DdbMatrixValue {
rows: DdbVectorObj | null
cols: DdbVectorObj | null
data: DdbVectorValue
}
/** 工具,取得某个 DdbType 的字节数 */
export const ddb_tensor_bytes: Record<
DdbType.bool | DdbType.char | DdbType.short | DdbType.int | DdbType.long | DdbType.float | DdbType.double,
number
> = {
[DdbType.bool]: 1,
[DdbType.char]: 1,
[DdbType.short]: 2,
[DdbType.int]: 4,
[DdbType.long]: 8,
[DdbType.float]: 4,
[DdbType.double]: 8,
}
type TensorElem = TensorData | boolean | number | bigint | null | string
interface TensorData extends Array<TensorElem> { }
interface DdbTensorMetadata {
/** Tensor 的元素的数据类型 */
data_type: DdbType
/** Tensor 类型 */
tensor_type: number
/** 设备类型 */
device_type: number
/** Tensor Flags */
tensor_flags: number
/** 维度 */
dimensions: number
/** shape, shape[i] 表示第 i 个维度的 size */
shape: number[]
/** strides, strides[i] 表示在第 i 个维度,一个元素与下一个元素的距离 */
strides: number[]
/** 保留值 */
preserve_value: bigint
/** 元素个数 */
elem_count: number
}
export interface DdbTensorValue extends DdbTensorMetadata {
/** 数据 */
data: Uint8Array
}
export type DdbDictValue = [DdbVectorObj, DdbVectorObj]
export interface DdbChartValue {
/** 原属性 chartType original: chartType */
type: DdbChartType
stacking: boolean
/** 直方图 (Histogram), plotHist 函数返回的 chart 可能具有该属性
原属性 binStart
数值类型的 DdbObj 都有可能?
*/
bin_start?: DdbObj
/** 原属性 binEnd */
bin_end?: DdbObj
/** 原属性 binCount */
bin_count?: DdbObj
titles: {
chart: string
x_axis: string
y_axis: string
}
extras?: {
multi_y_axes: boolean
}
data: DdbMatrixObj
}
export type DdbScalarValue =
null | boolean | number | bigint | string |
Uint8Array | // uuid, ipaddr, int128, blob
[number, number] | // complex, point
DdbFunctionDefValue |
DdbDurationValue |
DdbDecimal32Value | DdbDecimal64Value | DdbDecimal128Value
export type DdbVectorValue =
null |
Uint8Array | Int8Array | Int16Array | Int32Array | Float32Array | Float64Array | BigInt64Array | BigInt128Array |
string[] | // string[]
Uint8Array[] | // blob
DdbObj[] | // any
IotVectorItemValue |
DdbSymbolExtendedValue |
DdbArrayVectorValue |
DdbDecimal32VectorValue | DdbDecimal64VectorValue | DdbDecimal128VectorValue |
DdbDurationVectorValue
export type DdbValue = DdbScalarValue | DdbVectorValue | DdbMatrixValue | DdbDictValue | DdbChartValue | DdbTensorValue
export type DdbStringObj = DdbObj<string>
export type DdbVectorObj <TValue extends DdbVectorValue = DdbVectorValue> = DdbObj<TValue>
export type DdbVectorAnyObj = DdbVectorObj<DdbObj[]>
export type DdbVectorStringObj = DdbVectorObj<string[]>
export type DdbTableObj <TColumns extends DdbVectorObj[] = DdbVectorObj[]> = DdbObj<TColumns>
export type DdbDictObj <TKeys extends DdbVectorObj = DdbVectorObj, TValues extends DdbVectorObj = DdbVectorObj> = DdbObj<[TKeys, TValues]>
export type DdbMatrixObj <TValue extends DdbMatrixValue = DdbMatrixValue> = DdbObj<TValue>
export type DdbChartObj = DdbObj<DdbChartValue>
export type DdbTensorObj = DdbObj<DdbTensorValue>
/** DdbObj.data() 返回的表格对象 */
export interface DdbTableData <TRow = any> {
/** 表名 */
name: string
/** 每一列的名称 */
columns: string[]
/** 每一列的原始 ddb 数据类型 */
types: DdbType[]
/** 表格数据,每一行的对象组成的数组 */
data: TRow[]
}
/** DdbObj.data() 返回的 Tensor 对象 */
export interface DdbTensorData extends DdbTensorMetadata {
/** 数据 */
data: TensorData
}
/** DdbObj.data() 返回的矩阵对象 */
export interface DdbMatrixData {
/** 原始数据类型 */
type: DdbType
/** 行数 */
nrows: number
/** 列数 */
ncolumns: number
/** 行名称 */
rows?: any[]
/** 列名称 */
columns?: any[]
/** 数据, data[0][1] 为第 0 行第 1 列数据 */
data: any[][]
}
export type IotVectorItemValue = [number | string | bigint | boolean][]
export type DdbIotAnyVector = DdbObj<IotVectorItemValue>
/** 可以表示所有 DolphinDB 数据库中的数据类型 Can represent data types in all DolphinDB databases */
export class DdbObj <TValue extends DdbValue = DdbValue> {
static dec = new TextDecoder('utf-8')
static enc = new TextEncoder()
/** 维护已解析的 symbol base,比如流数据中后续的 symbol 向量可能只发送一个 base.id, base.size == 0, 依赖之前发送的 symbol base ?
只是暂存,如果一张表有多个 symbol 列,可能这个 symbol base 会被复用,不同的对象之间 symbol base 一般不复用
*/
static symbol_bases: Record<number, string[]> = { }
/** little endian (client) */
static le_client = Boolean(
new Uint8Array(
Uint32Array.of(1).buffer
)[0]
)
/** 是否为小端 (little endian) */
le = DdbObj.le_client
/** 数据形式 https://www.dolphindb.cn/cn/help/DataTypesandStructures/DataForms/index.html */
form: DdbForm
/** 数据类型 https://www.dolphindb.cn/cn/help/DataTypesandStructures/DataTypes/index.html */
type: DdbType
/** 占用 parse 时传入的 buf 的长度 */
length: number
/** table name / column name */
name?: string
/** 最低维、第 1 维
- vector: rows = n, cols = 1
- pair: rows = 2, cols = 1
- matrix: rows = n, cols = m
- set: 同 vector
- dict: 包含 keys, values 向量
- table: 同 matrix */
rows?: number
/** 第 2 维 */
cols?: number
/** 实际数据。不同的 DdbForm, DdbType 使用 DdbValue 中不同的类型来表示实际数据
The actual data. Different DdbForm, DdbType use different types in DdbValue to represent actual data */
value: TValue
/** 原始二进制数据,仅在 parse_object 为 false 时通过 parse_message 生成的顶层对象有这个属性 */
buffer?: Uint8Array
constructor (data: Partial<DdbObj> & { form: DdbForm, type: DdbType /* (parse 对象时必须设置) , length: number */ }) {
Object.assign(this, data)
}
static parse (buf: Uint8Array, le: boolean) {
if (!buf.length)
return new this({
le,
form: DdbForm.scalar,
type: DdbType.void,
length: 0,
value: null
})
const type = buf[0]
const form = buf[1]
if (buf.length <= 2)
return new this({
le,
form,
type,
length: 2,
value: null,
})
// set 里面 data 嵌套了一个 vector, 跳过 vector 的 type 和 form
const i_data = form === DdbForm.set ? 4 : 2
const buf_data = buf.subarray(i_data)
switch (form) {
case DdbForm.scalar: {
const [length, value] = this.parse_scalar(buf_data, le, type)
return new this({
le,
form,
type,
length: i_data + length,
value,
})
}
case DdbForm.vector:
case DdbForm.pair:
case DdbForm.set: {
let vector = this.parse_vector(buf_data, le, type)
vector.length += i_data
vector.form = form
return vector
}
case DdbForm.table: {
// table([
// [1, 2] as a,
// [1, 2] as b
// ])
// <Buffer
// 00 06 form = table
// 02 00 00 00 02 00 00 00 rows = 2, cols = 2
// 00 行名称
// 61 00 62 00 列名称 a, b
// 04 01 form = vector, type = int
// 02 00 00 00 01 00 00 00 cols = 2, rows = 1
// 01 00 00 00 02 00 00 00
// 04 01
// 02 00 00 00 01 00 00 00
// 01 00 00 00 02 00 00 00>
const dv = new DataView(buf.buffer, buf.byteOffset + i_data)
const rows = dv.getUint32(0, le)
const cols = dv.getUint32(4, le)
const i_name_tail = buf_data.indexOf(0, 8)
const name = this.dec.decode(
buf_data.subarray(8, i_name_tail)
)
const i_items_start = i_name_tail + 1
const [len_items, colnames] = this.parse_vector_items(
buf_data.subarray(i_items_start),
le,
DdbType.string,
cols
) as [number, string[]]
let value = new Array(cols)
let i_start = i_items_start + len_items
for (let i = 0; i < cols; i++) {
const type = buf_data[i_start] as DdbType
if (type === DdbType.compressed)
throw new Error(t(
'{{form}}<{{type}}> 暂时不支持解析',
{ form: 'table', type: 'compress' }
))
let col = this.parse_vector(
buf_data.subarray(i_start + 2),
le,
type
)
col.length += 2
col.name = colnames[i]
value[i] = col
i_start += col.length
}
return new this({
le,
form,
type,
length: i_data + i_start,
name,
rows,
cols,
value,
})
}
case DdbForm.dict:
case DdbForm.chart: {
// <Buffer 19 05 type = any, form = dict
// 12 01 keys.type = string, keys.form = vector
// 03 00 00 00 01 00 00 00 keys.cols = 3, keys.rows = 1
// 63 00 62 00 61 00
// 19 01 values.type = any, values.form = vector
// 03 00 00 00 01 00 00 00 values.cols = 3, values.rows = 1
// 04 00 03 00 00 00 04 00 02 00 00 00 04 00 01 00 00 00>
let keys = this.parse_vector(
buf_data.subarray(2),
le,
buf_data[0]
)
keys.length += 2
let values = this.parse_vector(
buf_data.subarray(keys.length + 2),
le,
buf_data[keys.length]
)
values.length += 2
let dict = new this({
le,
form: DdbForm.dict,
type,
length: i_data + keys.length + values.length,
rows: keys.rows,
cols: 2,
value: [
keys,
values
],
})
if (form === DdbForm.dict)
return dict
else {
const {
chartType: type,
stacking,
binStart: bin_start,
binEnd: bin_end,
binCount: bin_count,
title: titles,
extras,
data,
... others
} = dict.to_dict<{
chartType: DdbObj<DdbChartType>
stacking: DdbBool
binStart: DdbObj
binEnd: DdbObj
binCount: DdbObj
title: DdbVectorString
extras?: DdbObj
data: DdbMatrixObj
}>()
const [chart, x_axis, y_axis] = titles.value
dict.form = DdbForm.chart
dict.value = {
type: type.value,
stacking: stacking.value,
titles: {
chart,
x_axis,
y_axis,
},
... bin_start ? { bin_start, bin_end, } : { },
... bin_count ? { bin_count } : { },
... extras ? (() => {
const { multiYAxes: multi_y_axes = false, ...extras_others } = extras.to_dict<{ multiYAxes: boolean }>({ strip: true })
return {
extras: {
multi_y_axes,
...extras_others,
}
}
})() : { },
data,
...others,
}
return dict
}
}
case DdbForm.matrix: {
// rename!(
// 1..9$3:3,
// [1, 2, 3],
// ['c1', 'c2', 'c3']
// )
// <Buffer 04 03 type = int, form = matrix
// 03 has_row_label (& 0x01) = 1, has_col_label (& 0x02) = 1
// row labels
// 04 01 type = int, form = vector
// 03 00 00 00 01 00 00 00 rows = 3, cols = 1
// 01 00 00 00 02 00 00 00 03 00 00 00 vector values
// col labels
// 12 01 type = string, form = vector
// 03 00 00 00 01 00 00 00 rows = 3, cols = 1
// 63 31 00 63 32 00 63 33 00
// matrix data
// 04 03 type = matrix.type, form = matrix
// 03 00 00 00 03 00 00 00 rows = 3, cols = 3
const dv = new DataView(buf.buffer, buf.byteOffset + i_data)
const label_flags = buf_data[0]
const has_row_labels = Boolean(label_flags & 0x01)
const has_col_labels = Boolean(label_flags & 0x02)
let row_labels: DdbVectorObj | null = null
let col_labels: DdbVectorObj | null = null
let offset = 1
if (has_row_labels) {
row_labels = this.parse_vector(
buf_data.subarray(offset + 2),
le,
buf_data[offset] as DdbType
)
row_labels.length += 2
offset += row_labels.length
}
if (has_col_labels) {
col_labels = this.parse_vector(
buf_data.subarray(offset + 2),
le,
buf_data[offset] as DdbType
)
col_labels.length += 2
offset += col_labels.length
}
assert(buf_data[offset] === type, 'matrix.datatype === matrix.type')
const rows = dv.getUint32(offset + 2, le)
const cols = dv.getUint32(offset + 6, le)
const [len_items, data] = this.parse_vector_items(
buf_data.subarray(offset + 10),
le,
type,
rows * cols // 假设小于 2**32
)
offset += 10 + len_items
return new this({
le,
form,
type,
length: i_data + offset,
rows,
cols,
value: {
rows: row_labels,
cols: col_labels,
data,
},
})
}
case DdbForm.tensor: {
// 元数据
const tensorType = buf[2]
const deviceType = buf[3]
const dv = new DataView(buf.buffer, buf.byteOffset)
const tensorFlags = dv.getUint32(4, le)
const dimensions = dv.getInt32(8, le)
const shapes: number[] = [ ]
const strides: number[] = [ ]
const shapeStart = 12
const stridesStart = shapeStart + dimensions * 8
const preserveValueStart = stridesStart + dimensions * 8
const preserveValue = dv.getBigInt64(preserveValueStart, le)
const storageStart = preserveValueStart + 8
const elemCount = dv.getBigInt64(storageStart, le)
const dataStart = storageStart + 8
for (let d = 0; d < dimensions; d++) {
const getNumOffset = d * 8
shapes.push(Number(dv.getBigInt64(shapeStart + getNumOffset, le)))
strides.push(Number(dv.getBigInt64(stridesStart + getNumOffset, le)))
}
const dataBuffer = buf.subarray(dataStart)
return new this({
le,
form,
type,
length: i_data + buf_data.length,
value: {
data_type: type,
tensor_type: tensorType,
device_type: deviceType,
tensor_flags: tensorFlags,
dimensions,
shape: shapes,
strides,
preserve_value: preserveValue,
elem_count: Number(elemCount),
data: dataBuffer
}
})
}
default:
return new this({
le,
form,
type,
length: i_data + buf_data.length,
value: buf_data
})
}
}
static parse_scalar (buf: Uint8Array, le: boolean, type: DdbType): [number, DdbScalarValue] {
switch (type) {
case DdbType.bool: {
const dv = new DataView(buf.buffer, buf.byteOffset)
const value = dv.getInt8(0)
return [1, value === nulls.int8 ? null : Boolean(value)]
}
case DdbType.void:
case DdbType.char: {
const dv = new DataView(buf.buffer, buf.byteOffset)
const value = dv.getInt8(0)
return [1, value === nulls.int8 ? null : value]
}
case DdbType.short: {
const dv = new DataView(buf.buffer, buf.byteOffset)
const value = dv.getInt16(0, le)
return [2, value === nulls.int16 ? null : value]
}
case DdbType.int:
// datetime
case DdbType.date:
case DdbType.month:
case DdbType.time:
case DdbType.minute:
case DdbType.second:
case DdbType.datetime:
case DdbType.datehour: {
const dv = new DataView(buf.buffer, buf.byteOffset)
const value = dv.getInt32(0, le)
return [4, value === nulls.int32 ? null : value]
}
case DdbType.float: {
const dv = new DataView(buf.buffer, buf.byteOffset)
const value = dv.getFloat32(0, le)
return [4, value === nulls.float32 ? null : value]
}
case DdbType.double: {
const dv = new DataView(buf.buffer, buf.byteOffset)
const value = dv.getFloat64(0, le)
return [8, value === nulls.double ? null : value]
}
case DdbType.long:
// timestamp
case DdbType.timestamp:
case DdbType.nanotime:
case DdbType.nanotimestamp: {
const dv = new DataView(buf.buffer, buf.byteOffset)
const value = dv.getBigInt64(0, le)
return [8, value === nulls.int64 ? null : value]
}
case DdbType.string:
case DdbType.symbol:
case DdbType.code:
case DdbType.handle:
// sqlDS 函数会返回包含 datasource 的 any vector
case DdbType.datasource:
case DdbType.functiondef:
// mysql 插件 connect 方法会返回 resource 类型的对象
case DdbType.resource: {
const i_head = type === DdbType.functiondef ? 1 : 0
let i_zero = buf.indexOf(0, i_head)
let i_end: number // 整个字符串(包括 0)的末尾,excluding
if (i_zero === -1)
i_end = i_zero = buf.length
else
i_end = i_zero + 1
// 调整了 i_zero 到字符串(不包括 0)的末尾,excluding
const str = this.dec.decode(
buf.subarray(i_head, i_zero)
)
return [
i_end,
type === DdbType.functiondef ?
{
type: buf[0] as DdbFunctionType,
name: str
}
:
str
]
}
case DdbType.uuid:
case DdbType.ipaddr:
case DdbType.int128:
return [16, buf.slice(0, 16)]
case DdbType.blob: {
const dv = new DataView(buf.buffer, buf.byteOffset)
const length = dv.getUint32(0, le)
return [4 + length, buf.slice(4, 4 + length)]
}
case DdbType.complex:
case DdbType.point: {
const dv = new DataView(buf.buffer, buf.byteOffset)
return [16, [dv.getFloat64(0, le), dv.getFloat64(8, le)]]
}
case DdbType.duration: {
const dv = new DataView(buf.buffer, buf.byteOffset)
return [8, { unit: dv.getUint32(4, le), data: dv.getInt32(0, le) } as DdbDurationValue]
}
case DdbType.decimal32: {
const dv = new DataView(buf.buffer, buf.byteOffset)
const data = dv.getInt32(4, le)
return [8, { scale: dv.getInt32(0, le), data: data === nulls.int32 ? null : data } as DdbDecimal32Value]
}
case DdbType.decimal64: {
const dv = new DataView(buf.buffer, buf.byteOffset)
const data = dv.getBigInt64(4, le)
return [12, { scale: dv.getInt32(0, le), data: data === nulls.int64 ? null : data } as DdbDecimal64Value]
}
case DdbType.decimal128: {
const dv = new DataView(buf.buffer, buf.byteOffset)
const data = get_big_int_128(dv, 4, le)
return [20, { scale: dv.getInt32(0, le), data: data === nulls.int128 ? null : data }]
}
default:
throw new Error(String(DdbType[type] || type) + t(' 暂时不支持解析'))
}
}
/** parse: rows, cols, items
返回的 ddbobj.length 不包括 vector 的 type 和 form */
static parse_vector (buf: Uint8Array, le: boolean, type: DdbType): DdbVectorObj {
const dv = new DataView(buf.buffer, buf.byteOffset)
const rows = dv.getUint32(0, le)
let i_items_start = 8