-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathview.ts
379 lines (362 loc) · 11.8 KB
/
view.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
import type {
UnknownViewConstructor,
ViewConstructor,
ViewFieldLayout,
ViewInstance,
ViewLayout,
ViewSchema,
} from "./view-types.ts";
import { BooleanView } from "./boolean-view.ts";
import {
BigInt64View,
BigUint64View,
Float32View,
Float64View,
Int16View,
Int32View,
Int8View,
Uint16View,
Uint32View,
Uint8View,
} from "./numeric-view.ts";
import { ObjectView } from "./object-view.ts";
import { ArrayView } from "./array-view.ts";
import { VectorView } from "./vector-view.ts";
import { MapView } from "./map-view.ts";
import { DictView } from "./dict-view.ts";
import { StringView } from "./string-view.ts";
import { TypedArrayView } from "./typed-array-view.ts";
import { BinaryView } from "./binary-view.ts";
import { log2 } from "./utilities.ts";
import type { Constructor } from "./utility-types.ts";
export class View {
Views = new Map<string, UnknownViewConstructor>([
["array", ArrayView],
["typedarray", TypedArrayView],
["vector", VectorView],
["object", ObjectView as unknown as UnknownViewConstructor],
["map", MapView as UnknownViewConstructor],
["dict", DictView as UnknownViewConstructor],
["int8", Int8View],
["uint8", Uint8View],
["int16", Int16View],
["uint16", Uint16View],
["int32", Int32View],
["number", Float64View],
["integer", Int32View],
["uint32", Uint32View],
["float32", Float32View],
["float64", Float64View],
["bigint64", BigInt64View],
["biguint64", BigUint64View],
["boolean", BooleanView],
["string", StringView],
["binary", BinaryView],
]);
TaggedViews = new Map<number, UnknownViewConstructor>();
static AbstractViews = new Set([
"object",
"array",
"typedarray",
"vector",
"map",
"dict",
]);
constructor(public maxView = new DataView(new ArrayBuffer(8192))) {
}
create<T>(
schema: ViewSchema<T>,
constructor?: T extends object ? Constructor<T> : never,
): ViewConstructor<T> {
const { getSchemaId, getSchemaOrdering } = this
.constructor as typeof View;
const schemas = getSchemaOrdering(schema as ViewSchema<unknown>);
for (let i = schemas.length - 1; i >= 0; i--) {
const objectSchema = schemas[i];
const id = getSchemaId(objectSchema);
if (this.Views.has(id)) continue;
// use provided constructor for top object
const objectCtor = objectSchema === schema ? constructor : undefined;
const View = objectSchema.btype === "map"
? this.Views.get("map")!.initialize(objectSchema, this, objectCtor)
: objectSchema.btype === "dict"
? this.Views.get("dict")!.initialize(objectSchema, this)
: this.Views.get("object")!.initialize(objectSchema, this, objectCtor);
// cache the view by id
this.Views.set(id, View);
// cache by tag if present
const tag = this.getSchemaTag(objectSchema);
if (tag) this.TaggedViews.set(tag, View);
}
if (schema.type === "array") return this.getArray<T>(schema)[0];
return this.getExistingView<T>(schema);
}
view<T>(view: DataView): ViewInstance<T> | undefined {
const tag = this.getTag(view);
const ViewClass = this.TaggedViews.get(tag);
if (!ViewClass) return undefined;
return new (ViewClass as ViewConstructor<T>)(view.buffer, view.byteOffset);
}
decode<T>(view: DataView): T | undefined {
const tag = this.getTag(view);
const ViewClass = this.TaggedViews.get(tag);
if (!ViewClass) return undefined;
return (ViewClass as ViewConstructor<T>).decode(view, 0);
}
encode<T extends { tag: number }>(
value: T,
view?: DataView,
offset = 0,
): ViewInstance<T> | undefined {
const ViewClass = this.TaggedViews.get(value.tag) as ViewConstructor<T>;
if (!ViewClass) return undefined;
if (!view) return ViewClass.from(value);
ViewClass.encode(value, view, offset);
return new ViewClass(view.buffer, view.byteOffset);
}
getTag(view: DataView): number {
return view.getUint8(0);
}
getSchemaTag(
schema: ViewSchema<object>,
): number | undefined {
const tag = (schema.properties as { tag: ViewSchema<unknown> })?.tag
?.default;
return typeof tag === "number" ? tag : undefined;
}
getArray<T>(
schema: ViewSchema<T>,
): [view: ViewConstructor<T>, length: number] {
const { getSchemaId } = this.constructor as typeof View;
const arrays: Array<ViewSchema<Array<unknown>>> = [];
let currentField = schema as ViewSchema<unknown>;
// go down the array(s) to the item field
while (currentField && currentField.type === "array") {
arrays.push(currentField as ViewSchema<Array<unknown>>);
currentField = currentField.items!;
}
let currentArray = arrays.pop()!;
// get existing view of the item
const itemView = this.getExistingView(currentField);
// check
const itemId = getSchemaId(currentField as ViewSchema<unknown>);
const isArray = currentArray.btype !== "vector";
const viewId = isArray ? `ArrayView_${itemId}` : `VectorView_${itemId}`;
let CurrentView: UnknownViewConstructor;
if (!this.Views.has(viewId)) {
CurrentView = isArray
? this.getArrayView(
currentField,
itemView,
currentField.maxLength,
)
: this.Views.get("vector")!.initialize(
currentField,
this,
itemView,
);
// cache array views of unspecified length
if (currentField.maxLength === undefined) {
this.Views.set(viewId, CurrentView);
}
} else {
CurrentView = this.Views.get(viewId)!;
}
// initialize nested arrays
let itemLength = isArray ? CurrentView.getLength(currentArray.maxItems) : 0;
for (let i = arrays.length - 1; i >= 0; i--) {
currentArray = arrays[i];
if (currentArray.btype !== "vector") {
CurrentView = this.getArrayView(
currentArray.items as ViewSchema<unknown>,
CurrentView as ViewConstructor<unknown>,
itemLength,
);
itemLength = CurrentView.getLength(currentArray.maxItems);
} else {
CurrentView = this.Views.get("vector")!.initialize(
currentArray.items,
this,
CurrentView,
);
}
}
return [(CurrentView as unknown) as ViewConstructor<T>, itemLength];
}
getArrayView<T>(
schema: ViewSchema<T>,
SchemaView?: ViewConstructor<T>,
length?: number,
): ViewConstructor<Array<T>> {
const itemLength = length || SchemaView?.viewLength;
const isTypedArray = Reflect.has(log2, itemLength!);
return this.Views.get(isTypedArray ? "typedarray" : "array")!
.initialize(schema, this, SchemaView, itemLength) as ViewConstructor<
Array<T>
>;
}
static getDefaultData<T extends unknown>(
layout: ViewLayout<T>,
viewLength: number,
fields: Array<keyof T>,
): Uint8Array {
const buffer = new ArrayBuffer(viewLength);
const view = new DataView(buffer);
const array = new Uint8Array(buffer);
for (const name of fields) {
const field = layout[name];
if (Reflect.has(field, "default")) {
field.View.encode(field.default!, view, field.start, field.length);
} else if (field.View.defaultData) {
array.set(field.View.defaultData, field.start);
}
}
return array;
}
static getDefaultConstructor<T>(
fields: Array<keyof T>,
layout: ViewLayout<T>,
): Constructor<T> {
const content: Array<string> = [];
for (const field of fields) {
const View: ViewConstructor<unknown, unknown> = layout[field].View;
let value = "";
switch (View) {
case Int8View:
case Int16View:
case Uint16View:
case Uint8View:
case Int32View:
value = "0";
break;
case Float32View:
case Float64View:
value = "0.0";
break;
case BigInt64View:
case BigUint64View:
value = "0n";
break;
case BooleanView:
value = "false";
break;
case StringView:
value = "''";
break;
default:
value = "null";
}
content.push(`${field as string}:${value}`);
}
return new Function(
"return {" + content.join(",") +
"}",
) as Constructor<T>;
}
getExistingView<T>(schema: ViewSchema<T>): ViewConstructor<T> {
const { AbstractViews } = this.constructor as typeof View;
let type = schema.$id || schema.$ref?.slice(1);
if (type) {
if (!this.Views.has(type)) throw Error(`View "${type}" is not found.`);
} else {
type = schema.btype || schema.type;
if (!this.Views.has(type)) {
throw TypeError(`Type "${type}" is not supported.`);
} else if (AbstractViews.has(type)) {
throw TypeError(`Type ${type} is abstract.`);
}
}
return this.Views.get(type) as ViewConstructor<T>;
}
getFieldLayout<T>(
field: ViewSchema<T>,
start: number,
required: boolean,
name: string,
): ViewFieldLayout<T> {
let View: ViewConstructor<T>;
let length = 0;
if (field.type !== "array") {
View = this.getExistingView(field);
length = field.maxLength || View.viewLength;
} else {
[View, length] = this.getArray(field);
}
if (!length) length = Infinity;
if (required && length === Infinity) {
throw new TypeError(
`The length of a required field "${name}" is undefined.`,
);
}
const layout: ViewFieldLayout<T> = { start, View, length, required };
if (Reflect.has(field, "default")) {
layout.default = (field.default as unknown) as T;
}
return layout;
}
// deno-lint-ignore no-explicit-any
static getSchemaId(schema: ViewSchema<any>): string {
return schema.$id || schema.$ref?.slice(1) || schema.btype || schema.type;
}
static getSchemaOrdering(
schema: ViewSchema<unknown>,
): Array<ViewSchema<object>> {
// create graph
let object = schema;
// reach the nested object if an array is provided
while (object.type === "array") object = object.items!;
// return if no object found
if (object.type !== "object") return [];
const mainId = object.$id!;
let id = mainId;
const objects = { [id]: object };
const adjacency: Record<string, Array<string>> = { [id]: [] };
const indegrees = { [id]: 0 };
const processing = [id];
while (processing.length) {
id = processing.pop()!;
object = objects[id];
if (!object.properties) continue;
const properties = Object.keys(object.properties);
for (const property of properties) {
let field: ViewSchema<unknown> =
(object.properties as Record<string, ViewSchema<unknown>>)[property];
if (field.type === "array") {
while (field.type === "array") field = field.items!;
}
const { $id, $ref } = field;
if ($id) {
objects[$id] = field;
adjacency[id].push($id);
adjacency[$id] = [];
indegrees[$id] = indegrees[$id] ? indegrees[$id] + 1 : 1;
processing.push($id);
} else if ($ref) {
const refId = $ref.slice(1);
indegrees[refId] = indegrees[refId] ? indegrees[refId] + 1 : 1;
adjacency[id].push(refId);
}
}
}
// topologically sort the graph
let visited = 0;
const order: Array<ViewSchema<object>> = [];
processing.push(mainId);
while (processing.length) {
id = processing.shift()!;
const children = adjacency[id];
if (!children) continue; // $ref no external links
order.push(objects[id] as ViewSchema<object>);
for (const child of children) {
indegrees[child] -= 1;
if (indegrees[child] === 0) processing.push(child);
}
visited++;
}
// check for recursive links
if (visited !== Object.keys(objects).length) {
throw TypeError("The schema has recursive references.");
}
return order;
}
}