/
queryplanner.js
148 lines (122 loc) · 3.66 KB
/
queryplanner.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
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
var utilities = require("./utilities")
, queryMask = utilities.queryMask
, JoinStream = require("./joinstream")
, LiveJoinStream = require("./livejoinstream")
, SortJoinStream = require("./sortjoinstream")
, doSortQueryPlan
, async = require("async");
function queryplanner(db, options) {
return function planner(query, callback) {
var result = [].concat(query)
, live = false
async.parallel(query.map(function(q) {
return function(cb) {
var newq = queryMask(q)
, start = utilities.createQuery(newq).start;
db.approximateSize(start + "::", start + "\xff", function(err, size) {
if (err) {
callback(err);
return;
}
q.size = size;
cb();
});
};
}), function(err) {
if (err) {
callback(err);
return;
}
result.sort(function compare(a, b) {
if (a.live || a.size < b.size) {
return -1;
}
if (b.live || a.size > b.size) {
return 1;
}
return 0;
});
result.forEach(function(q) {
if (q.live) {
live = true;
}
delete q.size;
delete q.live;
});
if (live) {
result[0].stream = LiveJoinStream;
}
if (!live && options.joinAlgorithm === "sort" && result.length > 1) {
result.reduce(doSortQueryPlan);
}
result.forEach(function(q) {
if (!q.stream) {
q.stream = JoinStream;
}
});
callback(null, result);
});
};
}
doSortQueryPlan = function(first, second) {
if (first === null || first.stream === JoinStream) {
return null;
}
var firstQueryMask = utilities.queryMask(first)
, secondQueryMask = utilities.queryMask(second)
, firstVariablesMask = utilities.variablesMask(first)
, secondVariablesMask = utilities.variablesMask(second)
, firstVariables = Object.keys(firstVariablesMask).map(function(key) {
return firstVariablesMask[key];
})
, secondVariables = Object.keys(secondVariablesMask).map(function(key) {
return secondVariablesMask[key];
})
, variableKey = function(obj, variable) {
return Object.keys(obj).filter(function(key) {
return obj[key].name === variable.name;
})[0];
}
, commonVariables = firstVariables.filter(function(firstVar) {
return secondVariables.some(function(secondVar) {
return firstVar.name === secondVar.name;
});
})
, firstIndexArray = Object.keys(firstQueryMask)
, secondIndexArray = Object.keys(secondQueryMask)
, firstIndexes
, secondIndexes;
if (commonVariables.length === 0) {
return null;
}
first.stream = first.stream ? first.stream : JoinStream;
firstIndexArray.sort();
secondIndexArray.sort();
commonVariables.sort(function(a, b) {
if (a.name < b.name) {
return -1;
} else if (a.name > b.name) {
return 1;
} else {
return 0;
}
});
commonVariables.forEach(function(commonVar) {
firstIndexArray.push(variableKey(firstVariablesMask, commonVar));
secondIndexArray.push(variableKey(secondVariablesMask, commonVar));
});
firstIndexes = orderedPossibleIndex(firstIndexArray);
secondIndexes = orderedPossibleIndex(secondIndexArray);
first.index = first.index || firstIndexes[0];
second.index = secondIndexes[0];
second.stream = SortJoinStream;
return second;
}
function orderedPossibleIndex(keys) {
return Object.keys(utilities.defs).filter(function(index) {
return keys.every(function(key, pos) {
return utilities.defs[index][pos] === key;
});
});
}
module.exports = queryplanner;