@@ -51,6 +51,20 @@ CompositeDatasource.prototype._getDatasourceById = function(datasourceIndex) {
51
51
return this . _datasources [ this . _datasourceNames [ datasourceIndex ] ] . datasource ;
52
52
} ;
53
53
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
+
54
68
// Recursively find all required datasource composition info to perform a query.
55
69
// The callback will provide the parameters:
56
70
// Datasource id to start querying from
@@ -70,21 +84,35 @@ CompositeDatasource.prototype._getDatasourceInfo = function(query, absoluteOffse
70
84
cb ( chosenDatasource , chosenOffset , totalCount , exactCount ) ;
71
85
} else {
72
86
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 ) {
74
89
var count = metadata . totalCount ;
75
90
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 ( ) ;
81
97
} ) ;
82
98
} 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
+ }
88
116
}
89
117
} ) ;
90
118
}
0 commit comments