-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathindexOptimizer.js
More file actions
44 lines (40 loc) · 1.47 KB
/
indexOptimizer.js
File metadata and controls
44 lines (40 loc) · 1.47 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
var queryConverter = require('./indexFactory.js');
var Index = require("./mongoIndex.js");
var Collection = require("./collection.js");
/**
* Convenience class that receives all the profiling data, converts it into indices,
* adds all indices into collections while optimizing index creation for each collection and finally,
* takes all the indices and converts them into a script for building indices. (list of createIndex commands)
* This script can then be run manually through the shell.
* TODO: add an option to run the created indices here
* @param infos profiling info array
*/
var run = function(infos) {
var collections = {};
infos.forEach(function(x) {
var index = queryConverter(x);
if (index == null) {
console.log("Failed to convert query to Index object: "+JSON.stringify(index));
return;
}
var collection = collections[index.collection];
if (!collection) {
collections[index.collection] = new Collection(index.collection,[index]);
} else {
collection.addIndex(index);
}
});
return collectionsToScript(collections);
};
function collectionsToScript(collections) {
var responseData = "";
Object.values(collections).forEach(function(x) {
x.getIndices().map(function(y) {
return y.buildIndexCommand();
}).forEach(function(z) {
responseData += z + "\n";
});
});
return responseData;
}
module.exports = run;