-
Notifications
You must be signed in to change notification settings - Fork 106
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
Remove lodash dependency from @mcap/core #597
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,15 @@ | ||
import { crc32 } from "@foxglove/crc"; | ||
import { isEqual } from "lodash"; | ||
|
||
import StreamBuffer from "../common/StreamBuffer"; | ||
import { MCAP0_MAGIC } from "./constants"; | ||
import { parseMagic, parseRecord } from "./parse"; | ||
import { DecompressHandlers, McapStreamReader, TypedMcapRecord, TypedMcapRecords } from "./types"; | ||
import { | ||
Channel, | ||
DecompressHandlers, | ||
McapStreamReader, | ||
TypedMcapRecord, | ||
TypedMcapRecords, | ||
} from "./types"; | ||
|
||
type McapReaderOptions = { | ||
/** | ||
|
@@ -99,7 +104,7 @@ export default class Mcap0StreamReader implements McapStreamReader { | |
if (result.value?.type === "Channel") { | ||
const existing = this.channelsById.get(result.value.id); | ||
this.channelsById.set(result.value.id, result.value); | ||
if (existing && !isEqual(existing, result.value)) { | ||
if (existing && !isChannelEqual(existing, result.value)) { | ||
throw new Error( | ||
`Channel record for id ${result.value.id} (topic: ${result.value.topic}) differs from previous channel record of the same id.`, | ||
); | ||
|
@@ -258,3 +263,24 @@ export default class Mcap0StreamReader implements McapStreamReader { | |
} | ||
} | ||
} | ||
|
||
function isChannelEqual(a: Channel, b: Channel): boolean { | ||
if ( | ||
!( | ||
a.id === b.id && | ||
a.messageEncoding === b.messageEncoding && | ||
a.schemaId === b.schemaId && | ||
a.topic === b.topic && | ||
a.metadata.size === b.metadata.size | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there anything we can do to protect against adding fields but forgetting to define them here for equality comparison? Random thought: might it be better to store a crc of the original record bytes and compare that? |
||
) | ||
) { | ||
return false; | ||
} | ||
for (const [keyA, valueA] of a.metadata.entries()) { | ||
const valueB = b.metadata.get(keyA); | ||
if (valueA !== valueB) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import { sortedIndexBy } from "./sortedIndexBy"; | ||
|
||
describe("sortedIndexBy", () => { | ||
it("handles an empty array", () => { | ||
const array: [bigint, bigint][] = []; | ||
|
||
expect(sortedIndexBy(array, 0n, (x) => x)).toEqual(0); | ||
expect(sortedIndexBy(array, 42n, (x) => x)).toEqual(0); | ||
}); | ||
|
||
it("handles a contiguous array", () => { | ||
const array: [bigint, bigint][] = [ | ||
[1n, 42n], | ||
[2n, 42n], | ||
[3n, 42n], | ||
]; | ||
|
||
expect(sortedIndexBy(array, 0n, (x) => x)).toEqual(0); | ||
expect(sortedIndexBy(array, 1n, (x) => x)).toEqual(0); | ||
expect(sortedIndexBy(array, 2n, (x) => x)).toEqual(1); | ||
expect(sortedIndexBy(array, 3n, (x) => x)).toEqual(2); | ||
expect(sortedIndexBy(array, 4n, (x) => x)).toEqual(3); | ||
}); | ||
|
||
it("handles a sparse array", () => { | ||
const array: [bigint, bigint][] = [ | ||
[1n, 42n], | ||
[3n, 42n], | ||
]; | ||
|
||
expect(sortedIndexBy(array, 0n, (x) => x)).toEqual(0); | ||
expect(sortedIndexBy(array, 1n, (x) => x)).toEqual(0); | ||
expect(sortedIndexBy(array, 2n, (x) => x)).toEqual(1); | ||
expect(sortedIndexBy(array, 3n, (x) => x)).toEqual(1); | ||
expect(sortedIndexBy(array, 4n, (x) => x)).toEqual(2); | ||
}); | ||
|
||
it("handles negation", () => { | ||
const array: [bigint, bigint][] = [ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test seems invalid? The input array is not sorted by (x)=>-x. |
||
[1n, 42n], | ||
[2n, 42n], | ||
[3n, 42n], | ||
[4n, 42n], | ||
]; | ||
|
||
expect(sortedIndexBy(array, 0n, (x) => -x)).toEqual(4); | ||
expect(sortedIndexBy(array, 1n, (x) => -x)).toEqual(4); | ||
expect(sortedIndexBy(array, 2n, (x) => -x)).toEqual(4); | ||
expect(sortedIndexBy(array, 3n, (x) => -x)).toEqual(0); | ||
expect(sortedIndexBy(array, 4n, (x) => -x)).toEqual(0); | ||
expect(sortedIndexBy(array, 5n, (x) => -x)).toEqual(0); | ||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/** | ||
* Return the lowest index of `array` where an element can be inserted and maintain its sorted | ||
* order. This is a specialization of lodash's sortedIndexBy(). | ||
*/ | ||
export function sortedIndexBy( | ||
array: [bigint, bigint][], | ||
value: bigint, | ||
iteratee: (value: bigint) => bigint, | ||
): number { | ||
let low = 0; | ||
let high = array.length; | ||
if (high === 0) { | ||
return 0; | ||
} | ||
|
||
const computedValue = iteratee(value); | ||
|
||
while (low < high) { | ||
const mid = (low + high) >>> 1; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The other math adjustment I was referring to was low + (high - low) >>>1, to avoid overflow during addition |
||
const curComputedValue = iteratee(array[mid]![0]); | ||
|
||
if (curComputedValue < computedValue) { | ||
low = mid + 1; | ||
} else { | ||
high = mid; | ||
} | ||
} | ||
return high; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Similar thoughts to the channel info comparison below - maybe a crc on the bytes would be effective and work if we ever add a new field.