Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add append mode to Mcap Writer in the Typescript Core package #1016

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion typescript/core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@mcap/core",
"version": "2.0.2",
"version": "2.1.2",
"description": "MCAP file support in TypeScript",
"license": "MIT",
"repository": {
Expand Down
9 changes: 9 additions & 0 deletions typescript/core/src/ISeekableWriter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { IWritable } from "./IWritable";

/**
* ISeekableWriter describes a writer interface with seek abilities.
*/
export interface ISeekableWriter extends IWritable {
// Seek the cursor to the given position
seek(position: bigint): Promise<void>;
}
21 changes: 20 additions & 1 deletion typescript/core/src/McapIndexedReader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ type McapIndexedReaderArgs = {
summaryOffsetsByOpcode: ReadonlyMap<number, TypedMcapRecords["SummaryOffset"]>;
header: TypedMcapRecords["Header"];
footer: TypedMcapRecords["Footer"];
dataEndOffset?: bigint;
dataSectionCrc?: number;
};

export class McapIndexedReader {
Expand All @@ -30,6 +32,9 @@ export class McapIndexedReader {
readonly summaryOffsetsByOpcode: ReadonlyMap<number, TypedMcapRecords["SummaryOffset"]>;
readonly header: TypedMcapRecords["Header"];
readonly footer: TypedMcapRecords["Footer"];
// Used for appending attachments/metadata to existing MCAP files
readonly dataEndOffset?: bigint;
readonly dataSectionCrc?: number;

#readable: IReadable;
#decompressHandlers?: DecompressHandlers;
Expand All @@ -51,6 +56,8 @@ export class McapIndexedReader {
this.summaryOffsetsByOpcode = args.summaryOffsetsByOpcode;
this.header = args.header;
this.footer = args.footer;
this.dataEndOffset = args.dataEndOffset;
this.dataSectionCrc = args.dataSectionCrc;

for (const chunk of args.chunkIndexes) {
if (this.#messageStartTime == undefined || chunk.messageStartTime < this.#messageStartTime) {
Expand Down Expand Up @@ -203,6 +210,13 @@ export class McapIndexedReader {
throw errorWithLibrary("File is not indexed");
}

const dataEndOffset = BigInt(
footer.summaryStart -
/* data end length */ 4n -
/* record content length */ 8n -
/* Opcode.FOOTER */ 1n,
);

// Copy the footer prefix before reading the summary because calling readable.read() may reuse the buffer.
const footerPrefix = new Uint8Array(
/* Opcode.FOOTER */ 1 +
Expand Down Expand Up @@ -248,6 +262,7 @@ export class McapIndexedReader {
const metadataIndexes: TypedMcapRecords["MetadataIndex"][] = [];
const summaryOffsetsByOpcode = new Map<number, TypedMcapRecords["SummaryOffset"]>();
let statistics: TypedMcapRecords["Statistics"] | undefined;
let dataSectionCrc = 0;

let offset = 0;
for (
Expand Down Expand Up @@ -281,14 +296,16 @@ export class McapIndexedReader {
case "SummaryOffset":
summaryOffsetsByOpcode.set(result.record.groupOpcode, result.record);
break;
case "DataEnd":
dataSectionCrc = result.record.dataSectionCrc;
break;
case "Header":
case "Footer":
case "Message":
case "Chunk":
case "MessageIndex":
case "Attachment":
case "Metadata":
case "DataEnd":
throw errorWithLibrary(`${result.record.type} record not allowed in index section`);
case "Unknown":
break;
Expand All @@ -310,6 +327,8 @@ export class McapIndexedReader {
summaryOffsetsByOpcode,
header,
footer,
dataEndOffset,
dataSectionCrc,
});
}

Expand Down
Loading
Loading