-
Notifications
You must be signed in to change notification settings - Fork 75
/
ChunkBuilder.ts
87 lines (73 loc) · 2.18 KB
/
ChunkBuilder.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
import { McapRecordBuilder } from "./McapRecordBuilder";
import { Channel, Message, MessageIndex, Schema } from "./types";
type ChunkBuilderOptions = {
useMessageIndex?: boolean;
};
class ChunkBuilder {
#recordWriter = new McapRecordBuilder();
#messageIndices: Map<number, MessageIndex> | undefined;
#totalMessageCount = 0;
messageStartTime = 0n;
messageEndTime = 0n;
constructor({ useMessageIndex = true }: ChunkBuilderOptions) {
if (useMessageIndex) {
this.#messageIndices = new Map();
}
}
get numMessages(): number {
return this.#totalMessageCount;
}
get buffer(): Uint8Array {
return this.#recordWriter.buffer;
}
get byteLength(): number {
return this.#recordWriter.length;
}
get indices(): Iterable<MessageIndex> {
if (this.#messageIndices) {
return this.#messageIndices.values();
}
return [];
}
addSchema(schema: Schema): void {
this.#recordWriter.writeSchema(schema);
}
addChannel(info: Channel): void {
if (this.#messageIndices && !this.#messageIndices.has(info.id)) {
this.#messageIndices.set(info.id, {
channelId: info.id,
records: [],
});
}
this.#recordWriter.writeChannel(info);
}
addMessage(message: Message): void {
if (this.#totalMessageCount === 0 || message.logTime < this.messageStartTime) {
this.messageStartTime = message.logTime;
}
if (this.#totalMessageCount === 0 || message.logTime > this.messageEndTime) {
this.messageEndTime = message.logTime;
}
if (this.#messageIndices) {
let messageIndex = this.#messageIndices.get(message.channelId);
if (!messageIndex) {
messageIndex = {
channelId: message.channelId,
records: [],
};
this.#messageIndices.set(message.channelId, messageIndex);
}
messageIndex.records.push([message.logTime, BigInt(this.#recordWriter.length)]);
}
this.#totalMessageCount += 1;
this.#recordWriter.writeMessage(message);
}
reset(): void {
this.messageStartTime = 0n;
this.messageEndTime = 0n;
this.#totalMessageCount = 0;
this.#messageIndices?.clear();
this.#recordWriter.reset();
}
}
export { ChunkBuilder };