-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
AvroReader.ts
182 lines (150 loc) · 5.36 KB
/
AvroReader.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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
import { AvroReadable } from "./AvroReadable";
import {
AVRO_SYNC_MARKER_SIZE,
AVRO_INIT_BYTES,
AVRO_CODEC_KEY,
AVRO_SCHEMA_KEY
} from "./AvroConstants";
import { arraysEqual } from "./utils/utils.common";
import { AvroType, AvroParser } from "./AvroParser";
import "@azure/core-paging";
import { AbortSignalLike } from "@azure/abort-controller";
/**
* Options to configure the {@link AvroReader.parseObjects} operation.
*
* @export
* @interface AvroParseOptions
*/
export interface AvroParseOptions {
/**
* An implementation of the `AbortSignalLike` interface to signal the request to cancel the operation.
* For example, use the @azure/abort-controller to create an `AbortSignal`.
*
* @type {AbortSignalLike}
* @memberof AvroParseOptions
*/
abortSignal?: AbortSignalLike;
}
export class AvroReader {
private readonly _dataStream: AvroReadable;
private readonly _headerStream: AvroReadable;
private _syncMarker?: Uint8Array;
private _metadata?: Record<string, string>;
private _itemType?: AvroType;
private _itemsRemainingInBlock?: number;
// Remembers where we started if partial data stream was provided.
private readonly _initialBlockOffset: number;
/// The byte offset within the Avro file (both header and data)
/// of the start of the current block.
private _blockOffset: number;
public get blockOffset(): number {
return this._blockOffset;
}
private _objectIndex: number;
public get objectIndex(): number {
return this._objectIndex;
}
private _initialized: boolean;
constructor(dataStream: AvroReadable);
constructor(
dataStream: AvroReadable,
headerStream: AvroReadable,
currentBlockOffset: number,
indexWithinCurrentBlock: number
);
constructor(
dataStream: AvroReadable,
headerStream?: AvroReadable,
currentBlockOffset?: number,
indexWithinCurrentBlock?: number
) {
this._dataStream = dataStream;
this._headerStream = headerStream || dataStream;
this._initialized = false;
this._blockOffset = currentBlockOffset || 0;
this._objectIndex = indexWithinCurrentBlock || 0;
this._initialBlockOffset = currentBlockOffset || 0;
}
private async initialize(options: AvroParseOptions = {}) {
const header = await AvroParser.readFixedBytes(this._headerStream, AVRO_INIT_BYTES.length, {
abortSignal: options.abortSignal
});
if (!arraysEqual(header, AVRO_INIT_BYTES)) {
throw new Error("Stream is not an Avro file.");
}
// File metadata is written as if defined by the following map schema:
// { "type": "map", "values": "bytes"}
this._metadata = await AvroParser.readMap(this._headerStream, AvroParser.readString, {
abortSignal: options.abortSignal
});
// Validate codec
const codec = this._metadata![AVRO_CODEC_KEY];
if (!(codec == undefined || codec == "null")) {
throw new Error("Codecs are not supported");
}
// The 16-byte, randomly-generated sync marker for this file.
this._syncMarker = await AvroParser.readFixedBytes(this._headerStream, AVRO_SYNC_MARKER_SIZE, {
abortSignal: options.abortSignal
});
// Parse the schema
const schema = JSON.parse(this._metadata![AVRO_SCHEMA_KEY]);
this._itemType = AvroType.fromSchema(schema);
if (this._blockOffset == 0) {
this._blockOffset = this._initialBlockOffset + this._dataStream.position;
}
this._itemsRemainingInBlock = await AvroParser.readLong(this._dataStream, {
abortSignal: options.abortSignal
});
// skip block length
await AvroParser.readLong(this._dataStream, { abortSignal: options.abortSignal });
this._initialized = true;
if (this._objectIndex && this._objectIndex > 0) {
for (let i = 0; i < this._objectIndex; i++) {
await this._itemType.read(this._dataStream, { abortSignal: options.abortSignal });
this._itemsRemainingInBlock!--;
}
}
}
public hasNext(): boolean {
return !this._initialized || this._itemsRemainingInBlock! > 0;
}
public async *parseObjects(
options: AvroParseOptions = {}
): AsyncIterableIterator<Record<string, any> | null> {
if (!this._initialized) {
await this.initialize(options);
}
while (this.hasNext()) {
const result = await this._itemType!.read(this._dataStream, {
abortSignal: options.abortSignal
});
this._itemsRemainingInBlock!--;
this._objectIndex!++;
if (this._itemsRemainingInBlock == 0) {
const marker = await AvroParser.readFixedBytes(this._dataStream, AVRO_SYNC_MARKER_SIZE, {
abortSignal: options.abortSignal
});
this._blockOffset = this._initialBlockOffset + this._dataStream.position;
this._objectIndex = 0;
if (!arraysEqual(this._syncMarker!, marker)) {
throw new Error("Stream is not a valid Avro file.");
}
try {
this._itemsRemainingInBlock = await AvroParser.readLong(this._dataStream, {
abortSignal: options.abortSignal
});
} catch (err) {
// We hit the end of the stream.
this._itemsRemainingInBlock = 0;
}
if (this._itemsRemainingInBlock! > 0) {
// Ignore block size
await AvroParser.readLong(this._dataStream, { abortSignal: options.abortSignal });
}
}
yield result;
}
}
}