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

WIP: order latest first COMPASS-6706 #6361

Draft
wants to merge 11 commits into
base: main
Choose a base branch
from
3 changes: 2 additions & 1 deletion packages/collection-model/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { DataService } from 'mongodb-data-service';
import type { DataService, CollStatsIndexDetails } from 'mongodb-data-service';

type CollectionMetadata = {
/**
Expand Down Expand Up @@ -76,6 +76,7 @@ interface CollectionProps {
free_storage_size: number;
index_count: number;
index_size: number;
index_details: CollStatsIndexDetails
isTimeSeries: boolean;
isView: boolean;
sourceName: string | null;
Expand Down
1 change: 1 addition & 0 deletions packages/collection-model/lib/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ const CollectionModel = AmpersandModel.extend(debounceActions(['fetch']), {
free_storage_size: 'number',
index_count: 'number',
index_size: 'number',
index_details: 'object'
},
derived: {
ns: {
Expand Down
87 changes: 75 additions & 12 deletions packages/compass-crud/src/stores/crud-store.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2250,7 +2250,16 @@ describe('store', function () {
});

it('should call find with $bsonSize projection when mongodb version is >= 4.4, not connected to ADF and csfle is disabled', async function () {
await fetchDocuments(dataService, '5.0.0', false, 'test.test', {});
await fetchDocuments(
dataService,
{
serverVersion: '5.0.0',
isDataLake: false,
isTimeSeries: false,
},
'test.test',
{}
);
expect(find).to.have.been.calledOnce;
expect(find.getCall(0))
.to.have.nested.property('args.2.projection')
Expand All @@ -2261,8 +2270,11 @@ describe('store', function () {
findResult = [{ __size: new Int32(42), __doc: { _id: 1 } }];
const docs = await fetchDocuments(
dataService,
'4.0.0',
false,
{
serverVersion: '4.0.0',
isDataLake: false,
isTimeSeries: false,
},
'test.test',
{}
);
Expand All @@ -2272,7 +2284,16 @@ describe('store', function () {
});

it('should NOT call find with $bsonSize projection when mongodb version is < 4.4', async function () {
await fetchDocuments(dataService, '4.0.0', false, 'test.test', {});
await fetchDocuments(
dataService,
{
serverVersion: '4.0.0',
isDataLake: false,
isTimeSeries: false,
},
'test.test',
{}
);
expect(find).to.have.been.calledOnce;
expect(find.getCall(0)).to.have.nested.property(
'args.2.projection',
Expand All @@ -2281,7 +2302,16 @@ describe('store', function () {
});

it('should NOT call find with $bsonSize projection when connected to ADF', async function () {
await fetchDocuments(dataService, '5.0.0', true, 'test.test', {});
await fetchDocuments(
dataService,
{
serverVersion: '5.0.0',
isDataLake: true,
isTimeSeries: false,
},
'test.test',
{}
);
expect(find).to.have.been.calledOnce;
expect(find.getCall(0)).to.have.nested.property(
'args.2.projection',
Expand All @@ -2291,7 +2321,16 @@ describe('store', function () {

it('should NOT call find with $bsonSize projection when csfle is enabled', async function () {
csfleMode = 'enabled';
await fetchDocuments(dataService, '5.0.0', false, 'test.test', {});
await fetchDocuments(
dataService,
{
serverVersion: '5.0.0',
isDataLake: false,
isTimeSeries: false,
},
'test.test',
{}
);
expect(find).to.have.been.calledOnce;
expect(find.getCall(0)).to.have.nested.property(
'args.2.projection',
Expand All @@ -2302,8 +2341,11 @@ describe('store', function () {
it('should keep user projection when provided', async function () {
await fetchDocuments(
dataService,
'5.0.0',
false,
{
serverVersion: '5.0.0',
isDataLake: false,
isTimeSeries: false,
},
'test.test',
{},
{
Expand All @@ -2326,8 +2368,11 @@ describe('store', function () {

const docs = await fetchDocuments(
dataService,
'5.0.0',
false,
{
serverVersion: '5.0.0',
isDataLake: false,
isTimeSeries: false,
},
'test.test',
{}
);
Expand All @@ -2346,7 +2391,16 @@ describe('store', function () {
find = sinon.stub().rejects(new TypeError('🤷‍♂️'));

try {
await fetchDocuments(dataService, '5.0.0', false, 'test.test', {});
await fetchDocuments(
dataService,
{
serverVersion: '5.0.0',
isDataLake: false,
isTimeSeries: false,
},
'test.test',
{}
);
expect.fail('Expected fetchDocuments to fail with error');
} catch (err) {
expect(find).to.have.been.calledOnce;
Expand All @@ -2358,7 +2412,16 @@ describe('store', function () {
find = sinon.stub().rejects(new MongoServerError('Nope'));

try {
await fetchDocuments(dataService, '3.0.0', true, 'test.test', {});
await fetchDocuments(
dataService,
{
serverVersion: '3.0.0',
isDataLake: true,
isTimeSeries: false,
},
'test.test',
{}
);
expect.fail('Expected fetchDocuments to fail with error');
} catch (err) {
expect(find).to.have.been.calledOnce;
Expand Down
54 changes: 43 additions & 11 deletions packages/compass-crud/src/stores/crud-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import Reflux from 'reflux';
import toNS from 'mongodb-ns';
import { findIndex, isEmpty, isEqual } from 'lodash';
import semver from 'semver';
import type { Sort } from 'mongodb';
import StateMixin from '@mongodb-js/reflux-state-mixin';
import type { Element } from 'hadron-document';
import { Document } from 'hadron-document';
Expand Down Expand Up @@ -109,20 +110,26 @@ const INITIAL_BULK_UPDATE_TEXT = `{
},
}`;

type FetchDocumentsOptions = {
serverVersion: string;
isDataLake: boolean;
defaultSort?: Sort;
};

export const fetchDocuments: (
dataService: DataService,
serverVersion: string,
isDataLake: boolean,
fetchDocumentsOptions: FetchDocumentsOptions,
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 getting ugly 😆

...args: Parameters<DataService['find']>
) => Promise<HadronDocument[]> = async (
dataService: DataService,
serverVersion,
isDataLake,
fetchDocumentsOptions,
ns,
filter,
options,
executionOptions
) => {
const { isDataLake, serverVersion, defaultSort } = fetchDocumentsOptions;

const canCalculateDocSize =
// $bsonSize is only supported for mongodb >= 4.4.0
semver.gte(serverVersion, '4.4.0') &&
Expand All @@ -138,6 +145,7 @@ export const fetchDocuments: (

const modifiedOptions = {
...options,
sort: options?.sort ? options.sort : defaultSort,
projection: canCalculateDocSize
? { _id: 0, __doc: '$$ROOT', __size: { $bsonSize: '$$ROOT' } }
: options?.projection,
Expand Down Expand Up @@ -174,7 +182,11 @@ export const fetchDocuments: (

type CollectionStats = Pick<
Collection,
'document_count' | 'storage_size' | 'free_storage_size' | 'avg_document_size'
| 'document_count'
| 'storage_size'
| 'free_storage_size'
| 'avg_document_size'
| 'index_details'
>;
const extractCollectionStats = (collection: Collection): CollectionStats => {
const coll = collection.toJSON();
Expand All @@ -183,9 +195,20 @@ const extractCollectionStats = (collection: Collection): CollectionStats => {
storage_size: coll.storage_size,
free_storage_size: coll.free_storage_size,
avg_document_size: coll.avg_document_size,
index_details: coll.index_details,
};
};

function getDefaultSort(
collectionStats: CollectionStats | null
): Sort | undefined {
if (collectionStats?.index_details._id_) {
return { _id: -1 };
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm now not sure. It seems like { $natural: -1 } is actually just as fast as { $natural: 1 } *(the default) at least in most cases. If it is in MOST cases we'd have to just change this logic and return { $natural: -1 }. If it works and is fine in ALL cases, then most of this PR is probably not needed. We could just always use that as a default and we don't even need this function.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

But then.. I don't think we want $natural at all because it's just how documents are sorted in wiredtiger, which means any order really. For example, an update can bump an old document to the top of the natural order.

}

return undefined;
}

/**
* Default number of docs per page.
*/
Expand Down Expand Up @@ -408,6 +431,8 @@ class CrudStoreImpl
const isDataLake = !!this.instance.dataLake.isDataLake;
const isReadonly = !!this.options.isReadonly;

const collectionStats = extractCollectionStats(this.collection);

return {
ns: this.options.namespace,
collection: toNS(this.options.namespace).collection,
Expand Down Expand Up @@ -439,7 +464,7 @@ class CrudStoreImpl
isUpdatePreviewSupported:
this.instance.topologyDescription.type !== 'Single',
docsPerPage: this.getInitialDocsPerPage(),
collectionStats: extractCollectionStats(this.collection),
collectionStats,
};
}

Expand Down Expand Up @@ -884,8 +909,11 @@ class CrudStoreImpl
try {
documents = await fetchDocuments(
this.dataService,
this.state.version,
this.state.isDataLake,
{
serverVersion: this.state.version,
isDataLake: this.state.isDataLake,
defaultSort: getDefaultSort(this.state.collectionStats),
},
ns,
filter ?? {},
opts as any,
Expand Down Expand Up @@ -1539,8 +1567,9 @@ class CrudStoreImpl
}

collectionStatsFetched(model: Collection) {
const collectionStats = extractCollectionStats(model);
this.setState({
collectionStats: extractCollectionStats(model),
collectionStats,
});
}

Expand Down Expand Up @@ -1712,8 +1741,11 @@ class CrudStoreImpl
),
fetchDocuments(
this.dataService,
this.state.version,
this.state.isDataLake,
{
serverVersion: this.state.version,
isDataLake: this.state.isDataLake,
defaultSort: getDefaultSort(this.state.collectionStats),
},
ns,
query.filter ?? {},
findOptions as any,
Expand Down
3 changes: 2 additions & 1 deletion packages/compass-crud/src/utils/cancellable-queries.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ describe('cancellable-queries', function () {
dataService,
preferences,
'cancel.numbers',
null,
undefined,
{
signal,
}
Expand Down Expand Up @@ -138,6 +138,7 @@ describe('cancellable-queries', function () {
dataService,
preferences,
'cancel.numbers',
// @ts-expect-error this is deliberately wrong
'this is not a filter',
{
signal,
Expand Down
2 changes: 1 addition & 1 deletion packages/compass-crud/src/utils/cancellable-queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export async function countDocuments(
dataService: DataService,
preferences: PreferencesAccess,
ns: string,
filter: BSONObject,
filter: BSONObject | undefined,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

drive-by

{
signal,
skip,
Expand Down
2 changes: 2 additions & 0 deletions packages/data-service/src/data-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1108,6 +1108,7 @@ class DataServiceImpl extends WithLogContext implements DataService {
],
},
},
indexDetails: { $first: '$storageStats.indexDetails' },
nindexes: { $max: '$storageStats.nindexes' },
},
},
Expand Down Expand Up @@ -2581,6 +2582,7 @@ class DataServiceImpl extends WithLogContext implements DataService {
storage_size: data.storageSize ?? 0,
free_storage_size: data.freeStorageSize ?? 0,
index_count: data.nindexes ?? 0,
index_details: data.indexDetails ?? {},
index_size: data.totalIndexSize ?? 0,
};
}
Expand Down
1 change: 1 addition & 0 deletions packages/data-service/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export interface CollectionStats {
storage_size: number;
free_storage_size: number;
index_count: number;
index_details: CollStatsIndexDetails;
index_size: number;
}

Expand Down
Loading