Skip to content

Commit 63a55c3

Browse files
committed
Require exact counts for determining the starting datasource in CompositeDatasource
1 parent 5afdd9b commit 63a55c3

File tree

1 file changed

+39
-11
lines changed

1 file changed

+39
-11
lines changed

lib/datasources/CompositeDatasource.js

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,20 @@ CompositeDatasource.prototype._getDatasourceById = function(datasourceIndex) {
5151
return this._datasources[this._datasourceNames[datasourceIndex]].datasource;
5252
};
5353

54+
// Count the amount of triple in the query result to get an exact count.
55+
CompositeDatasource.prototype._getExactCount = function(datasource, query, cb) {
56+
var emptyQuery = { offset: 0, subject: query.subject, predicate: query.predicate, object: query.object };
57+
var exactCount = 0;
58+
var countingTripleStream = { push: function(triple) {
59+
if (triple) {
60+
exactCount++;
61+
} else {
62+
cb(exactCount);
63+
}
64+
}};
65+
datasource._executeQuery(emptyQuery, countingTripleStream, noop);
66+
};
67+
5468
// Recursively find all required datasource composition info to perform a query.
5569
// The callback will provide the parameters:
5670
// Datasource id to start querying from
@@ -70,21 +84,35 @@ CompositeDatasource.prototype._getDatasourceInfo = function(query, absoluteOffse
7084
cb(chosenDatasource, chosenOffset, totalCount, exactCount);
7185
} else {
7286
var emptyTripleStream = { push: noop };
73-
self._getDatasourceById(datasourceIndex)._executeQuery(emptyQuery, emptyTripleStream, function (metadata) {
87+
var datasource = self._getDatasourceById(datasourceIndex);
88+
datasource._executeQuery(emptyQuery, emptyTripleStream, function (metadata) {
7489
var count = metadata.totalCount;
7590
var exact = metadata.exactCount;
76-
if (chosenDatasource < 0 && offset < count) {
77-
// We can start querying from this datasource
78-
setImmediate(function () {
79-
findRecursive(datasourceIndex + 1, offset - count, datasourceIndex, offset,
80-
totalCount + count, exactCount && exact);
91+
// If we are still looking for an appropriate datasource, we need exact counts!
92+
if (offset > 0 && !exact) {
93+
self._getExactCount(datasource, query, function(exactCount) {
94+
count = exactCount;
95+
exact = true;
96+
continueRecursion();
8197
});
8298
} else {
83-
// We forward our accumulated information and go check the next datasource
84-
setImmediate(function () {
85-
findRecursive(datasourceIndex + 1, offset - count, chosenDatasource, chosenOffset,
86-
totalCount + count, exactCount && exact);
87-
});
99+
continueRecursion();
100+
}
101+
102+
function continueRecursion() {
103+
if (chosenDatasource < 0 && offset < count) {
104+
// We can start querying from this datasource
105+
setImmediate(function () {
106+
findRecursive(datasourceIndex + 1, offset - count, datasourceIndex, offset,
107+
totalCount + count, exactCount && exact);
108+
});
109+
} else {
110+
// We forward our accumulated information and go check the next datasource
111+
setImmediate(function () {
112+
findRecursive(datasourceIndex + 1, offset - count, chosenDatasource, chosenOffset,
113+
totalCount + count, exactCount && exact);
114+
});
115+
}
88116
}
89117
});
90118
}

0 commit comments

Comments
 (0)