-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathindexFactory.js
More file actions
257 lines (240 loc) · 10.3 KB
/
indexFactory.js
File metadata and controls
257 lines (240 loc) · 10.3 KB
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/**
* This class handles documents extracted from the profilingInfo collection and turns them into indices.
* exported method: convertQueryToIndex
*/
var Index = require("./mongoIndex.js"),
ObjectID = require('mongodb').ObjectID;
/**
* Full list of operators by category. This is later re-classified into equality operators and range operators.
* Copied from https://docs.mongodb.com/manual/reference/operator/query/
*/
const equalityComparisonOperators = ["$eq", "$ne", "$in", "$nen"];
const rangeComparisonOperators = ["$gt","$gte", "$lt", "$lte"];
const logicalOperators = ["$and", "$not", "$nor", "$or"];
const elementOperatos = ["$exists", "$type"];
const evaluationOperators = ["$mod", "$regex", "$text", "$where"];
const geoSpatialOperators = ["$geoIntersects", "$geoWithin", "$near", "$nearSphere"];
const equalityArrayOperators = ["$size"];
const recursiveArrayOperators = ["$all", "$elemMatch"];
const bitwiseOperators = ["$bitsAllClear", "$bitsAllSet", "$bitsAnyClear", "$bitsAnySet"];
const projectionOperators = ["$", "$meta", "$slice"];
/** Combined lists of operators by type */
const equalityOperators = equalityComparisonOperators.concat(elementOperatos, evaluationOperators, equalityArrayOperators, bitwiseOperators, projectionOperators, geoSpatialOperators);
const rangeOperators = rangeComparisonOperators;
const recursiveOperators = recursiveArrayOperators.concat(logicalOperators);
const simpleEqualityObjectTypes = ['ObjectID', 'Date'];
/** sort orders */
const ASCENDING = 1;
const DESCENDING = -1;
/**
* Converts a query profiling info into an Index object.
* @param profilingInfoDoc - one profiling info document
* @return {Index} parsed index or null in case profiling info doc is weird
*/
var convertQueryToIndex = function(profilingInfoDoc) {
if (profilingInfoDoc.command && profilingInfoDoc.command.mapreduce) {
return buildIndexFromMapReduce(profilingInfoDoc);
}
if (profilingInfoDoc.command && profilingInfoDoc.command.aggregate) {
return buildIndexFromAggregation(profilingInfoDoc);
}
if ((profilingInfoDoc.query && profilingInfoDoc.ns) || (profilingInfoDoc.command && (profilingInfoDoc.command.findandmodify || profilingInfoDoc.command.count))) {
return buildIndexFromFind(profilingInfoDoc);
}
console.log("info doc is in a format not implemented yet: "+JSON.stringify(profilingInfoDoc));
return null;
};
/**
* Builds an index from the given profiling info parameter, assuming the audited query is of type mapreduce
* @param profilingInfo profiling info document
* @return {Index} The index created
*/
var buildIndexFromMapReduce = function (profilingInfo) {
var collection = profilingInfo.command.mapreduce;
if (!collection) {
return null;
}
var filter = parseFilter(profilingInfo.command.query);
var sort = parseSort(profilingInfo.command.sort);
return buildIndexFromColumnsAndOrders(filter, sort, collection);
};
/**
* Builds an index from the given profiling info parameter, assuming the audited query is of type aggregation
* @param profilingInfo profiling info document
* @return {Index} The index created
*/
function buildIndexFromAggregation(profilingInfo) {
var collection = profilingInfo.command.aggregate;
var firstMatchPosition = 0;
var pipeline = profilingInfo.command.pipeline;
for (; firstMatchPosition < pipeline.length; firstMatchPosition++) {
if (pipeline[firstMatchPosition].hasOwnProperty("$match")) {
break;
}
}
var equalityAndRange = parseFilter(pipeline[firstMatchPosition]["$match"]);
var equalityOrders = Array(equalityAndRange.equality.length).fill(ASCENDING);
var rangeOrders = Array(equalityAndRange.equality.length).fill(ASCENDING);
var result = concatUniqueColsAndOrders(equalityAndRange.equality,equalityOrders, equalityAndRange.range, rangeOrders);
var flippableOrders = [];
for (var i=0; i<equalityAndRange.equality.length; i++) {
flippableOrders.push(i);
}
return new Index(collection,result.cols,result.orders,flippableOrders);
}
/**
* Builds an index from parsed filter and sort data.
* @param filterColsAndOrders array of columns and sort orders used for the filter part of the query
* @param sortColsAndOrders array of columns and sort orders used for the sort part of the query
* @param collection collection name
* @return {Index} The index created
*/
function buildIndexFromColumnsAndOrders(filterColsAndOrders, sortColsAndOrders, collection) {
var cols = filterColsAndOrders.equality;
var equalitySize = cols.length;
var orders = Array(equalitySize).fill(ASCENDING);
var result = concatUniqueColsAndOrders(cols,orders,sortColsAndOrders.cols,sortColsAndOrders.orders);
var range = filterColsAndOrders.range;
var rangeOrders = Array(range.length).fill(ASCENDING);
result = concatUniqueColsAndOrders(result.cols,result.orders,range,rangeOrders);
var flippableOrders = [];
// if only sort then all columns are flippable
if (equalitySize === 0 && range.length === 0) {
for (var p = 0; p < cols.length; p++) {
flippableOrders.push(p);
}
} else {
for (var p = 0; p < filterColsAndOrders.equality.length; p++) {
flippableOrders.push(p);
}
for (var p = 0; p < rangeOrders.length; p++) {
var flippableRangePos = sortColsAndOrders.cols.length + equalitySize + p;
flippableOrders.push(flippableRangePos);
}
}
// TODO duplicate columns can be found in different positions but mongo seems to ignore them
return new Index(collection, result.cols, result.orders, flippableOrders);
}
/**
* Builds an index from the given profiling info parameter, assuming the audited query is of type find
* @param profilingInfo profiling info document
* @return {Index} The index created
*/
function buildIndexFromFind(profilingInfo) {
var collection = findCollectionInFindQuery(profilingInfo);
if (!collection) {
return null;
}
var filter = profilingInfo.query ? (profilingInfo.query.filter || profilingInfo.query["$query"] || profilingInfo.query) : profilingInfo.command.query;
var equalityAndRange = parseFilter(filter);
var sort = profilingInfo.query ? (profilingInfo.query.sort || profilingInfo.query["$orderby"]) : null;
var sortColsAndOrders = parseSort(sort);
return buildIndexFromColumnsAndOrders(equalityAndRange, sortColsAndOrders, collection);
}
/**
* Collection name can be found in weird places for different query types.
* This method handles all the options I found out.
* TODO Might be more options
* @param profilingInfo one profiling query document
* @return collection name
*/
function findCollectionInFindQuery(profilingInfo) {
var collection = (profilingInfo.query && profilingInfo.query.find) || (profilingInfo.command && (profilingInfo.command.count || profilingInfo.command.findandmodify));
if (!collection) {
var ns = profilingInfo.ns.split('.');
if (!ns || !ns.length) {
collection = null;
} else {
collection = ns[ns.length-1];
}
}
if (!collection || collection === "$cmd") {
console.log("couldn't find collection name for "+JSON.stringify(profilingInfo)+" returning "+collection);
}
return collection;
}
function pushUnique(ar, el) {
if (ar.indexOf(el) == -1) {
ar.push(el);
}
}
function concatUniqueColsAndOrders(cols1, orders1, cols2, orders2) {
var cols = cols1.slice(), orders = orders1.slice();
for (var i=0; i < cols2.length; i++) {
if (cols.indexOf(cols2[i]) == -1) {
cols.push(cols2[i]);
orders.push(orders2[i]);
}
}
return {cols: cols, orders: orders};
}
/**
* recursively parse filter object into two arrays - one for column names by which we query for equality, and a second for querying by range.
* @param filter filter object or an inner object of filter.
* @return {range: Array, equality: Array} - an object with two arrays - equality and range, each holding a list of column names.
*/
function parseFilter(filter) {
var equality = [];
var range = [];
for (var key in filter) {
if (!filter.hasOwnProperty(key)) {
continue;
}
var value = filter[key];
// simple value implies simple query {key=value}
if (value !== Object(value) || value instanceof RegExp) {
pushUnique(equality,key);
}
// query with operators -> value is object
else if (typeof value == 'object') {
var op = Object.keys(value)[0];
// equality operators
if (equalityOperators.indexOf(op) > -1) {
pushUnique(equality,key);
}
// Known objects like: _id : ObjectID("123")
else if (simpleEqualityObjectTypes.indexOf(value.constructor.name) > -1) {
pushUnique(equality,key);
}
//range operations
else if (rangeOperators.indexOf(op) > -1) {
pushUnique(range,key);
}
// recursive (logical) operations
else if (recursiveOperators.indexOf(key) > -1 && value.constructor === Array) {
value.map(function(x) {
var clause = parseFilter(x);
return clause;
}).forEach(function(x) {
equality = concatUniqueColsAndOrders(equality, [], x.equality, []).cols;
range = concatUniqueColsAndOrders(range, [], x.range, []).cols;
});
} else {
// defaults to complex object equality check like phone: {countryCode: "+1", nationalNumber: "50505050"}
pushUnique(equality,key);
}
}
else {
console.log("Unexpected combination of filter values: " + key + " = " + value);
}
}
return {range: range, equality: equality};
}
/**
* Parses sort clauses into two arrays of column names and index order
* @param sort - sort clause
* @return {{cols: Array, orders: Array}}
*/
function parseSort(sort) {
var result = {cols:[], orders:[]};
if (sort) {
Object.keys(sort).forEach(function(x) {
if (result.cols.indexOf(x) > -1) {
pushUnique(result.cols,x);
pushUnique(result.orders,sort[x]);
}
});
}
return result;
}
module.exports = convertQueryToIndex;