-
Notifications
You must be signed in to change notification settings - Fork 105
Fix the uniqueItems implementation to accommodate non-primitive values #511
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
dist/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
141 changes: 141 additions & 0 deletions
141
smithy-typescript-ssdk-libs/server-common/src/unique.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
/* | ||
* Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed | ||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
* express or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
|
||
import * as util from "util"; | ||
|
||
import { findDuplicates, Input } from "./unique"; | ||
|
||
describe("findDuplicates", () => { | ||
describe("finds duplicates in", () => { | ||
it("strings", () => { | ||
expect(findDuplicates(["a", "b", "c", "a", "b", "a"])).toEqual(["a", "b"]); | ||
expect(findDuplicates(["x", "y", "z", "a", "b", "c", "a", "b"])).toEqual(["a", "b"]); | ||
}); | ||
it("numbers", () => { | ||
expect(findDuplicates([1, 2, 3, 4, 1, 2])).toEqual([1, 2]); | ||
}); | ||
it("booleans", () => { | ||
expect(findDuplicates([true, false, true])).toEqual([true]); | ||
}); | ||
it("arrays", () => { | ||
expect( | ||
findDuplicates([ | ||
[5, 6], | ||
[2, 3], | ||
[1, 2], | ||
[3, 4], | ||
[1, 2], | ||
]) | ||
).toEqual([[1, 2]]); | ||
}); | ||
it("Dates", () => { | ||
expect(findDuplicates([new Date(1000), new Date(2000), new Date(1000)])).toEqual([new Date(1000)]); | ||
}); | ||
it("Blobs", () => { | ||
expect(findDuplicates([Uint8Array.of(1, 2, 3), Uint8Array.of(4, 5, 6), Uint8Array.of(4, 5, 6)])).toEqual([ | ||
Uint8Array.of(4, 5, 6), | ||
]); | ||
}); | ||
it("objects", () => { | ||
expect(findDuplicates([{ a: "b" }, { b: [1, 2, 3] }, { a: "b" }, { a: "b" }])).toEqual([{ a: "b" }]); | ||
expect(findDuplicates([{ a: "b" }, { b: 1, c: 2 }, { c: 2, b: 1 }])).toEqual([{ b: 1, c: 2 }]); | ||
}); | ||
it("deeply nested objects", () => { | ||
expect( | ||
findDuplicates([ | ||
{ a: { b: { c: [1, { d: 2 }, [3]] } } }, | ||
2, | ||
[3, 4], | ||
{ b: "c" }, | ||
{ a: { b: { c: [1, { d: 2 }, [3]] } } }, | ||
]) | ||
).toEqual([{ a: { b: { c: [1, { d: 2 }, [3]] } } }]); | ||
}); | ||
}); | ||
describe("correctly does not find duplicates in", () => { | ||
it("strings", () => { | ||
expect(findDuplicates(["a", "b", "c"])).toEqual([]); | ||
}); | ||
it("numbers", () => { | ||
expect(findDuplicates([1, 2, 3, 4])).toEqual([]); | ||
expect(findDuplicates([1, 2, "1", "2"])).toEqual([]); | ||
}); | ||
it("booleans", () => { | ||
expect(findDuplicates([true, false])).toEqual([]); | ||
expect(findDuplicates([true, false, "true", "false"])).toEqual([]); | ||
}); | ||
it("arrays", () => { | ||
expect( | ||
findDuplicates([ | ||
[1, 2], | ||
[2, 3], | ||
[3, 4], | ||
]) | ||
).toEqual([]); | ||
expect( | ||
findDuplicates([ | ||
[1, 2], | ||
["1", "2"], | ||
]) | ||
).toEqual([]); | ||
}); | ||
it("objects", () => { | ||
expect(findDuplicates([{ a: "b" }, { b: [1, 2, 3] }])).toEqual([]); | ||
expect(findDuplicates([{ a: 1 }, { a: "1" }])).toEqual([]); | ||
}); | ||
it("Dates", () => { | ||
expect(findDuplicates([new Date(100), new Date(200), new Date(101)])).toEqual([]); | ||
}); | ||
it("blobs", () => { | ||
expect(findDuplicates([Uint8Array.of(1, 2, 3), Uint8Array.of(1, 2)])).toEqual([]); | ||
}); | ||
}); | ||
|
||
// This is relatively slow and may be flaky if the input size is tuned to let it run reasonably fast | ||
it.skip("is faster than the naive implementation", () => { | ||
const input: Input[] = [true, false, 1, 2, 3, 4, 5, 6]; | ||
for (let i = 0; i < 10_000; i++) { | ||
input.push({ a: [1, 2, 3, i], b: { nested: [true] } }); | ||
} | ||
|
||
const uniqueStart = Date.now(); | ||
expect(findDuplicates(input)).toEqual([]); | ||
const uniqueEnd = Date.now(); | ||
const uniqueDuration = (uniqueEnd - uniqueStart) / 1000; | ||
|
||
console.log(`findDuplicates finished in ${uniqueDuration} seconds`); | ||
|
||
const naiveStart = Date.now(); | ||
expect(naivefindDuplicates(input)).toEqual([]); | ||
const naiveEnd = Date.now(); | ||
const naiveDuration = (naiveEnd - naiveStart) / 1000; | ||
|
||
console.log(`naivefindDuplicates finished in ${naiveDuration} seconds`); | ||
|
||
expect(naiveDuration).toBeGreaterThan(uniqueDuration); | ||
}); | ||
|
||
// This isn't even correct! It should be even slower, it just returns the first duplicate! | ||
function naivefindDuplicates(input: Array<Input>): Input | undefined { | ||
for (let i = 0; i < input.length - 1; i++) { | ||
for (let j = i + 1; j < input.length; j++) { | ||
if (util.isDeepStrictEqual(input[i], input[j])) { | ||
return [input[i]]; | ||
} | ||
} | ||
} | ||
return []; | ||
} | ||
}); |
101 changes: 101 additions & 0 deletions
101
smithy-typescript-ssdk-libs/server-common/src/unique.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
/* | ||
* Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed | ||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
* express or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
|
||
import { createHash } from "crypto"; | ||
import * as util from "util"; | ||
|
||
/** | ||
* A shortcut for JSON and Smithy primitives, as well as documents and Smithy- | ||
* modeled structures composed of those primitives | ||
*/ | ||
export type Input = { [key: string]: Input } | Array<Input> | Date | Uint8Array | string | number | boolean | null; | ||
|
||
/** | ||
* Returns an array of duplicated values in the input. This is equivalent to using | ||
* {@link util#isDeepStrictEqual} to compare every member of the input to all the | ||
* other members, but with an optimization to make the runtime complexity O(n) | ||
* instead of O(n^2). | ||
* | ||
* @param input an array of {@link Input} | ||
* @return an array containing one instance of every duplicated member of the input, | ||
* or an empty array if there are no duplicates | ||
*/ | ||
export const findDuplicates = (input: Array<Input>): Array<Input> => { | ||
const potentialCollisions: { [hash: string]: { value: Input; alreadyFound: boolean }[] } = {}; | ||
const collisions: Array<Input> = []; | ||
|
||
for (const value of input) { | ||
const valueHash = hash(value); | ||
if (!potentialCollisions.hasOwnProperty(valueHash)) { | ||
potentialCollisions[valueHash] = [{ value: value, alreadyFound: false }]; | ||
} else { | ||
let duplicateFound = false; | ||
for (const potentialCollision of potentialCollisions[valueHash]) { | ||
if (util.isDeepStrictEqual(value, potentialCollision.value)) { | ||
duplicateFound = true; | ||
if (!potentialCollision.alreadyFound) { | ||
collisions.push(value); | ||
potentialCollision.alreadyFound = true; | ||
} | ||
} | ||
} | ||
if (!duplicateFound) { | ||
potentialCollisions[valueHash].push({ value: value, alreadyFound: false }); | ||
} | ||
} | ||
} | ||
return collisions; | ||
}; | ||
|
||
const hash = (input: Input): string => { | ||
return createHash("sha256").update(canonicalize(input)).digest("base64"); | ||
}; | ||
|
||
/** | ||
* Since node's hash functions operate on strings or buffers, we need a canonical format for | ||
* our objects in order to hash them correctly. This function turns them into string representations | ||
* where the types are encoded in order to avoid ambiguity, for instance, between the string "1" and | ||
* the number 1. This method sorts object keys lexicographically in order to maintain consistency. | ||
* | ||
* This doesn't just call JSON.stringify because we want to have firm control over the ordering of map | ||
* keys and the handling of blobs and dates | ||
* | ||
* @param input a JSON-like object | ||
* @return a canonical string representation | ||
*/ | ||
const canonicalize = (input: Input): string => { | ||
if (input === null) { | ||
return "null()"; | ||
} | ||
if (typeof input === "string" || typeof input === "number" || typeof input === "boolean") { | ||
return `${typeof input}(${input.toString()})`; | ||
} | ||
if (Array.isArray(input)) { | ||
return "array(" + input.map((i) => canonicalize(i)).join(",") + ")"; | ||
} | ||
if (input instanceof Date) { | ||
return "date(" + input.getTime() + ")"; | ||
} | ||
if (input instanceof Uint8Array) { | ||
// hashing the blob just to avoid allocating another base64 copy of its data | ||
return "blob(" + createHash("sha256").update(input).digest("base64") + ")"; | ||
} | ||
|
||
const contents: Array<string> = []; | ||
for (const key of Object.keys(input).slice().sort()) { | ||
contents.push("key(" + key + ")->value(" + canonicalize(input[key]) + ")"); | ||
} | ||
return "map(" + contents.join(",") + ")"; | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
nice!