Skip to content

Commit

Permalink
cli: filter does not duplicate schema records (#816)
Browse files Browse the repository at this point in the history
  • Loading branch information
james-rms authored Feb 16, 2023
1 parent 849b0c9 commit 393b228
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 2 deletions.
4 changes: 2 additions & 2 deletions go/cli/mcap/cmd/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
65 changes: 65 additions & 0 deletions go/cli/mcap/cmd/filter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 393b228

Please sign in to comment.