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

Fix SO export sorting algorithm #142078

Merged
merged 5 commits into from
Sep 29, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
* Side Public License, v 1.
*/

import { range } from 'lodash';
import { sortObjects } from './sort_objects';
import type { SavedObject } from '@kbn/core-saved-objects-common';

describe('sortObjects()', () => {
test('should return on empty array', () => {
Expand Down Expand Up @@ -309,6 +311,7 @@ describe('sortObjects()', () => {
]
`);
});

test('should not fail on complex circular dependencies', () => {
const docs = [
{
Expand Down Expand Up @@ -424,4 +427,38 @@ describe('sortObjects()', () => {
]
`);
});

test('should not fail on large graph of objects', () => {
// create an object that references all objects with a higher `index` up to `depth`.
const createComplexNode = (index: number, depth: number): SavedObject => {
Comment on lines +431 to +433
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tried it without the fix, and the test does timeout.

return {
type: 'test',
id: `${index}`,
attributes: {},
references: range(index + 1, depth).map((refIndex) => ({
type: 'test',
id: `${refIndex}`,
name: `test-${refIndex}`,
})),
};
};

const createComplexGraph = (depth: number): SavedObject[] => {
const nodes: SavedObject[] = [];
for (let i = 0; i < depth; i++) {
nodes.push(createComplexNode(i, depth));
}
return nodes;
};

const depth = 100;
const graph = createComplexGraph(depth);
const sorted = sortObjects(graph);

expect(sorted.map(({ type, id }) => `${type}:${id}`)).toEqual(
range(depth)
.reverse()
.map((index) => `test:${index}`)
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,36 @@

import type { SavedObject } from '@kbn/core-saved-objects-common';

const getId = (object: { type: string; id: string }) => `${object.type}:${object.id}`;

export function sortObjects(savedObjects: SavedObject[]): SavedObject[] {
const path = new Set<SavedObject>();
const traversed = new Set<string>();
const sorted = new Set<SavedObject>();
const objectsByTypeId = new Map(
savedObjects.map((object) => [`${object.type}:${object.id}`, object] as [string, SavedObject])
savedObjects.map((object) => [getId(object), object] as [string, SavedObject])
);

function includeObjects(objects: SavedObject[]) {
for (const object of objects) {
if (path.has(object)) {
const objectId = getId(object);
if (traversed.has(objectId)) {
continue;
}

const refdObjects = object.references
.map((ref) => objectsByTypeId.get(`${ref.type}:${ref.id}`))
const objectRefs = object.references
.map((ref) => objectsByTypeId.get(getId(ref)))
.filter((ref): ref is SavedObject => !!ref);

if (refdObjects.length) {
path.add(object);
includeObjects(refdObjects);
path.delete(object);
traversed.add(objectId);
if (objectRefs.length) {
includeObjects(objectRefs);
Comment on lines +31 to +33
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the significant change of the PR, the rest of the diff in this file is just minor enhancement/cleanup.

Previously, we were removing the current object from the 'path' when we're done traversing the children, causing the algo to eventually traverse the whole tree again if the same object is encountered in the tree of another node from a higher level.

Once a object has been traversed once, we now keep it in the path list (renamed to traversed as it better reflect its role now), avoiding the full traversal again later.

}

sorted.add(object);
}
}

includeObjects(savedObjects);

return [...sorted];
}