-
Notifications
You must be signed in to change notification settings - Fork 5.6k
/
Copy pathcmd_object_utils.js
102 lines (92 loc) · 3.24 KB
/
cmd_object_utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import {sysCollNamePrefix} from "jstests/core/timeseries/libs/timeseries_writes_util.js";
/**
* Resolves the command name for the given 'cmdObj'.
*/
export function getCommandName(cmdObj) {
return Object.keys(cmdObj)[0];
}
/**
* Returns the inner command if 'cmdObj' represents an explain command, or simply 'cmdObj'
* otherwise.
*/
export function getInnerCommand(cmdObj) {
const isExplain = "explain" in cmdObj;
if (!isExplain) {
return cmdObj;
}
if (typeof cmdObj.explain === "object") {
return cmdObj.explain;
}
const {explain, ...cmdWithoutExplain} = cmdObj;
return cmdWithoutExplain;
}
/**
* Returns the explain command object for the given 'cmdObj'.
*/
export function getExplainCommand(cmdObj) {
const isAggregateCmd = getCommandName(cmdObj) === "aggregate";
// Extract the 'writeConcern' from the command, as it can not be passed to explain.
const {writeConcern, ...cmdWithoutWriteConcern} = cmdObj;
return isAggregateCmd ? {explain: {...cmdWithoutWriteConcern, cursor: {}}}
: {explain: cmdWithoutWriteConcern};
}
/**
* Resolves the collection name for the given 'cmdObj'. If the command targets a view, then this
* will recursively find the underlying collection's name and return it. Returns 'undefined' if the
* collection does not exist.
*/
export function getCollectionName(db, cmdObj) {
try {
const collectionsInfo = db.getCollectionInfos();
if (!collectionsInfo || collectionsInfo.length === 0) {
return undefined;
}
let viewOn;
let name = cmdObj[getCommandName(cmdObj)];
do {
let collInfo = collectionsInfo.find(c => c.name === name);
if (!collInfo) {
name = undefined;
}
viewOn = collInfo?.options?.viewOn;
if (viewOn) {
name = viewOn;
}
} while (viewOn);
return name;
} catch (ex) {
switch (ex.code) {
case ErrorCodes.InvalidViewDefinition: {
// The 'DB.prototype.getCollectionInfos()' implementation may throw an exception
// when faced with a malformed view definition. This is analogous to a missing
// collection for the purpose of passthrough suites.
return undefined;
}
default:
throw ex;
}
}
}
export function isSystemCollectionName(collectionName) {
return collectionName.startsWith("system.");
}
export function isInternalDbName(dbName) {
return ["admin", "local", "config"].includes(dbName);
}
/**
* Returns true iff the 'collectionName' exists and it is a timeseries collection.
*/
export function isTimeSeriesCollection(db, collectionName) {
const collectionInfo = db.getCollectionInfos({name: collectionName});
if (!collectionInfo || collectionInfo.length === 0) {
return false;
}
return collectionInfo[0].type === "timeseries" || collectionName.startsWith("system.bucket.");
}
/**
* Return true iff this is a "system.bucket.*" collection.
*/
export function isSystemBucketNss(innerCmd) {
const nss = innerCmd[getCommandName(innerCmd)];
return typeof nss === "string" && nss.startsWith(sysCollNamePrefix);
}