-
Notifications
You must be signed in to change notification settings - Fork 228
feat(compass-collection): (re-merge) Schema Analysis Redux Integration for Collection Plugin CLOUDP-333846 #7203
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
Show all changes
8 commits
Select commit
Hold shift + click to select a range
716814b
CLOUDP-333846 Schema Analysis Redux Integration for Collection Plugin
ncarbon 45b2604
cleanup
ncarbon 5b8bef4
Address comments and remove abort controller code
ncarbon abe6b33
address comments
ncarbon d7538c7
remove export of CollectionActions
ncarbon 08b17ac
use .sample and add more precise error typing and handling
ncarbon 46bdb33
add reset schemaAnalysis and fix typing
ncarbon e57dc96
use mongodb driver version 6.17.0 in compass-collection
ncarbon 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
165 changes: 165 additions & 0 deletions
165
packages/compass-collection/src/calculate-schema-depth.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,165 @@ | ||
import { expect } from 'chai'; | ||
import { calculateSchemaDepth } from './calculate-schema-depth'; | ||
import type { | ||
Schema, | ||
SchemaField, | ||
DocumentSchemaType, | ||
ArraySchemaType, | ||
} from 'mongodb-schema'; | ||
|
||
describe('calculateSchemaDepth', function () { | ||
it('returns 1 for flat schema', async function () { | ||
const schema: Schema = { | ||
fields: [ | ||
{ name: 'a', types: [{ bsonType: 'String' }] } as SchemaField, | ||
{ name: 'b', types: [{ bsonType: 'Number' }] } as SchemaField, | ||
], | ||
count: 2, | ||
}; | ||
const depth = await calculateSchemaDepth(schema); | ||
expect(depth).to.equal(1); | ||
}); | ||
|
||
it('returns correct depth for nested document', async function () { | ||
const schema: Schema = { | ||
fields: [ | ||
{ | ||
name: 'a', | ||
types: [ | ||
{ | ||
bsonType: 'Document', | ||
fields: [ | ||
{ | ||
name: 'b', | ||
types: [ | ||
{ | ||
bsonType: 'Document', | ||
fields: [ | ||
{ | ||
name: 'c', | ||
types: [{ bsonType: 'String' }], | ||
} as SchemaField, | ||
], | ||
} as DocumentSchemaType, | ||
], | ||
} as SchemaField, | ||
], | ||
} as DocumentSchemaType, | ||
], | ||
} as SchemaField, | ||
], | ||
count: 1, | ||
}; | ||
const depth = await calculateSchemaDepth(schema); | ||
expect(depth).to.equal(3); | ||
}); | ||
|
||
it('returns correct depth for nested arrays', async function () { | ||
const schema: Schema = { | ||
fields: [ | ||
{ | ||
name: 'arr', | ||
types: [ | ||
{ | ||
bsonType: 'Array', | ||
types: [ | ||
{ | ||
bsonType: 'Array', | ||
types: [ | ||
{ | ||
bsonType: 'Document', | ||
fields: [ | ||
{ | ||
name: 'x', | ||
types: [{ bsonType: 'String' }], | ||
} as SchemaField, | ||
], | ||
} as DocumentSchemaType, | ||
], | ||
} as ArraySchemaType, | ||
], | ||
} as ArraySchemaType, | ||
], | ||
} as SchemaField, | ||
], | ||
count: 1, | ||
}; | ||
const depth = await calculateSchemaDepth(schema); | ||
expect(depth).to.equal(4); | ||
}); | ||
|
||
it('returns 0 for empty schema', async function () { | ||
const schema: Schema = { fields: [], count: 0 }; | ||
const depth = await calculateSchemaDepth(schema); | ||
expect(depth).to.equal(0); | ||
}); | ||
|
||
it('handles mixed types at root', async function () { | ||
const schema: Schema = { | ||
fields: [ | ||
{ | ||
name: 'a', | ||
types: [ | ||
{ bsonType: 'String' }, | ||
{ | ||
bsonType: 'Document', | ||
fields: [ | ||
{ | ||
name: 'b', | ||
types: [{ bsonType: 'Number' }], | ||
} as SchemaField, | ||
], | ||
} as DocumentSchemaType, | ||
], | ||
} as SchemaField, | ||
], | ||
count: 1, | ||
}; | ||
const depth = await calculateSchemaDepth(schema); | ||
expect(depth).to.equal(2); | ||
}); | ||
|
||
it('handles deeply nested mixed arrays and documents', async function () { | ||
const schema: Schema = { | ||
fields: [ | ||
{ | ||
name: 'root', | ||
types: [ | ||
{ | ||
bsonType: 'Array', | ||
types: [ | ||
{ | ||
bsonType: 'Document', | ||
fields: [ | ||
{ | ||
name: 'nestedArr', | ||
types: [ | ||
{ | ||
bsonType: 'Array', | ||
types: [ | ||
{ | ||
bsonType: 'Document', | ||
fields: [ | ||
{ | ||
name: 'leaf', | ||
types: [{ bsonType: 'String' }], | ||
} as SchemaField, | ||
], | ||
} as DocumentSchemaType, | ||
], | ||
} as ArraySchemaType, | ||
], | ||
} as SchemaField, | ||
], | ||
} as DocumentSchemaType, | ||
], | ||
} as ArraySchemaType, | ||
], | ||
} as SchemaField, | ||
], | ||
count: 1, | ||
}; | ||
const depth = await calculateSchemaDepth(schema); | ||
expect(depth).to.equal(5); | ||
}); | ||
}); |
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,57 @@ | ||
import type { | ||
ArraySchemaType, | ||
DocumentSchemaType, | ||
Schema, | ||
SchemaField, | ||
SchemaType, | ||
} from 'mongodb-schema'; | ||
|
||
// Every 1000 iterations, unblock the thread. | ||
const UNBLOCK_INTERVAL_COUNT = 1000; | ||
const unblockThread = async () => | ||
new Promise<void>((resolve) => setTimeout(resolve)); | ||
|
||
export async function calculateSchemaDepth(schema: Schema): Promise<number> { | ||
let unblockThreadCounter = 0; | ||
let deepestPath = 0; | ||
|
||
async function traverseSchemaTree( | ||
fieldsOrTypes: SchemaField[] | SchemaType[], | ||
depth: number | ||
): Promise<void> { | ||
unblockThreadCounter++; | ||
if (unblockThreadCounter === UNBLOCK_INTERVAL_COUNT) { | ||
unblockThreadCounter = 0; | ||
await unblockThread(); | ||
} | ||
|
||
if (!fieldsOrTypes || fieldsOrTypes.length === 0) { | ||
return; | ||
} | ||
|
||
deepestPath = Math.max(depth, deepestPath); | ||
|
||
for (const fieldOrType of fieldsOrTypes) { | ||
if ((fieldOrType as DocumentSchemaType).bsonType === 'Document') { | ||
await traverseSchemaTree( | ||
(fieldOrType as DocumentSchemaType).fields, | ||
depth + 1 // Increment by one when we go a level deeper. | ||
); | ||
} else if ( | ||
(fieldOrType as ArraySchemaType).bsonType === 'Array' || | ||
(fieldOrType as SchemaField).types | ||
) { | ||
const increment = | ||
(fieldOrType as ArraySchemaType).bsonType === 'Array' ? 1 : 0; | ||
await traverseSchemaTree( | ||
(fieldOrType as ArraySchemaType | SchemaField).types, | ||
depth + increment // Increment by one when we go a level deeper. | ||
); | ||
} | ||
} | ||
} | ||
|
||
await traverseSchemaTree(schema.fields, 1); | ||
|
||
return deepestPath; | ||
} |
Oops, something went wrong.
Oops, something went wrong.
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.
[nitpick] The magic number 1000 should be documented or made configurable. Consider adding a comment explaining why this specific interval was chosen for thread unblocking.
Copilot uses AI. Check for mistakes.