From 393b228696e8132bf5e7b20845caf1dd81ffbc33 Mon Sep 17 00:00:00 2001 From: james-rms Date: Fri, 17 Feb 2023 09:10:53 +1100 Subject: [PATCH] cli: filter does not duplicate schema records (#816) --- go/cli/mcap/cmd/filter.go | 4 +-- go/cli/mcap/cmd/filter_test.go | 65 ++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 2 deletions(-) diff --git a/go/cli/mcap/cmd/filter.go b/go/cli/mcap/cmd/filter.go index dda14b7293..a42fb58592 100644 --- a/go/cli/mcap/cmd/filter.go +++ b/go/cli/mcap/cmd/filter.go @@ -314,12 +314,12 @@ func filter( if err = mcapWriter.WriteSchema(schema.Schema); err != nil { return err } - schema.written = true + schemas[channel.SchemaID] = markableSchema{schema.Schema, true} } if err = mcapWriter.WriteChannel(channel.Channel); err != nil { return err } - channel.written = true + channels[message.ChannelID] = markableChannel{channel.Channel, true} } if err = mcapWriter.WriteMessage(message); err != nil { return err diff --git a/go/cli/mcap/cmd/filter_test.go b/go/cli/mcap/cmd/filter_test.go index 2227113907..0b902c141e 100644 --- a/go/cli/mcap/cmd/filter_test.go +++ b/go/cli/mcap/cmd/filter_test.go @@ -61,6 +61,71 @@ func writeFilterTestInput(t *testing.T, w io.Writer) { })) assert.Nil(t, writer.Close()) } +func TestPassthrough(t *testing.T) { + opts := &filterOpts{ + compressionFormat: mcap.CompressionLZ4, + start: 0, + end: 1000, + includeAttachments: true, + includeMetadata: true, + } + + writeBuf := bytes.Buffer{} + readBuf := bytes.Buffer{} + + writeFilterTestInput(t, &readBuf) + assert.Nil(t, filter(&readBuf, &writeBuf, opts)) + attachmentCounter := 0 + metadataCounter := 0 + schemaCounter := 0 + messageCounter := map[uint16]int{ + 1: 0, + 2: 0, + 3: 0, + } + channelCounter := map[uint16]int{ + 1: 0, + 2: 0, + 3: 0, + } + lexer, err := mcap.NewLexer(&writeBuf, &mcap.LexerOptions{ + AttachmentCallback: func(ar *mcap.AttachmentReader) error { + attachmentCounter++ + return nil + }, + }) + assert.Nil(t, err) + defer lexer.Close() + for { + token, record, err := lexer.Next(nil) + if err != nil { + assert.ErrorIs(t, err, io.EOF) + break + } + switch token { + case mcap.TokenMessage: + message, err := mcap.ParseMessage(record) + assert.Nil(t, err) + messageCounter[message.ChannelID]++ + case mcap.TokenChannel: + channel, err := mcap.ParseChannel(record) + assert.Nil(t, err) + channelCounter[channel.ID]++ + case mcap.TokenSchema: + schemaCounter++ + case mcap.TokenMetadata: + metadataCounter++ + } + } + assert.Equal(t, 1, attachmentCounter) + assert.Equal(t, 1, metadataCounter) + assert.InDeltaMapValues(t, map[uint16]int{1: 100, 2: 100, 3: 100}, messageCounter, 0.0) + // schemas and channels should be duplicated once into the summary section + assert.Equal(t, 2, schemaCounter) + assert.InDeltaMapValues(t, map[uint16]int{1: 2, 2: 2, 3: 2}, channelCounter, 0.0) + +} + func TestFiltering(t *testing.T) { cases := []struct { name string