Files
mongo/jstests/sharding/libs/find_chunks_util.js
Zac 591928c619 SERVER-108478 JS formatted by prettier and remove clang-format (#39656)
GitOrigin-RevId: 6c8f6aded47f260aa4f7c231b17dae3302cb1e04
2025-08-21 17:27:09 +00:00

71 lines
2.9 KiB
JavaScript

/*
* Utilities for looking up chunk metadata
*/
export var findChunksUtil = (function () {
/**
* Performs a find() on config.chunks on 'configDB', targeting chunks for the collection 'ns',
* and the optional 'extraQuery' and 'projection'.
* Chooses to query chunks by their 'ns' or uuid' fields according to it's config.collection
* entry having 'timestamp' or not.
*/
let findChunksByNs = function (configDB, ns, extraQuery = null, projection = null) {
const collection = configDB.collections.findOne({_id: ns});
if (collection.timestamp) {
const collectionUUID = configDB.collections.findOne({_id: ns}).uuid;
assert.neq(collectionUUID, null);
const chunksQuery = Object.assign({uuid: collectionUUID}, extraQuery);
return configDB.chunks.find(chunksQuery, projection);
} else {
const chunksQuery = Object.assign({ns: ns}, extraQuery);
return configDB.chunks.find(chunksQuery, projection);
}
};
/**
* Performs a findOne() on config.chunks on 'configDB', targeting chunks for the collection
* 'ns', and the optional 'extraQuery' and 'projection'. Chooses to query chunks by their 'ns'
* or uuid' fields according to it's config.collection entry having 'timestamp' or not.
*/
let findOneChunkByNs = function (configDB, ns, extraQuery = null, projection = null) {
const collection = configDB.collections.findOne({_id: ns});
if (collection.timestamp) {
const collectionUUID = configDB.collections.findOne({_id: ns}).uuid;
assert.neq(collectionUUID, null);
const chunksQuery = Object.assign({uuid: collectionUUID}, extraQuery);
return configDB.chunks.findOne(chunksQuery, projection);
} else {
const chunksQuery = Object.assign({ns: ns}, extraQuery);
return configDB.chunks.findOne(chunksQuery, projection);
}
};
/**
* Performs a count() on config.chunks on 'configDB', targeting chunks for the collection 'ns',
* and the optional 'extraQuery' and 'projection'.
* Chooses to query chunks by their 'ns' or uuid' fields according to it's config.collection
* entry having 'timestamp' or not.
*/
let countChunksForNs = function (configDB, ns, extraQuery = null) {
return findChunksByNs(configDB, ns, extraQuery).count();
};
/**
* Returns the appropriate chunks join clause for collection 'ns'.
*/
let getChunksJoinClause = function (configDB, ns) {
const collMetadata = configDB.collections.findOne({_id: ns});
if (collMetadata.timestamp) {
return {uuid: collMetadata.uuid};
} else {
return {ns: collMetadata._id};
}
};
return {
findChunksByNs,
findOneChunkByNs,
countChunksForNs,
getChunksJoinClause,
};
})();