Skip to content

Commit 315cf87

Browse files
committed
Pass datasource metadata as a property.
With the previous event-based implementation, views might miss the `metadata` event if they attach late.
1 parent 3a8a738 commit 315cf87

20 files changed

+101
-109
lines changed

bin/generate-summary

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ function fromDataSource(uri, datasource, callback, end, chunksize) {
8585
var count = 0;
8686

8787
var stream = datasource.select({ limit: limit, offset: offset }, console.error)
88-
.on('metadata', function (metadata) {
88+
.getProperty('metadata', function (metadata) {
8989
var progress = Math.round((offset / metadata.totalCount) * 100);
9090
console.log(progress);
9191

lib/controllers/SummaryController.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ SummaryController.prototype._handleRequest = function (request, response, next)
4242

4343
// Render the summary
4444
var view = this._negotiateView('Summary', request, response);
45-
view.render({ prefixes: this._prefixes, resultStream: streamParser }, request, response);
45+
view.render({ prefixes: this._prefixes, results: streamParser }, request, response);
4646
}
4747
else
4848
next();

lib/controllers/TriplePatternFragmentsController.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ TriplePatternFragmentsController.prototype._handleRequest = function (request, r
3636
// Generate the query result
3737
var view = this._negotiateView('TriplePatternFragments', request, response),
3838
settings = this._createFragmentMetadata(request, query, datasourceSettings);
39-
settings.resultStream = datasourceSettings.datasource.select(query,
40-
function (error) { error && next(error); });
39+
settings.results = datasourceSettings.datasource.select(query,
40+
function (error) { error && next(error); });
4141

4242
// Execute the extensions and render the query result
4343
var extensions = this._extensions, extensionId = 0;

lib/datasources/CompositeDatasource.js

Lines changed: 37 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -89,60 +89,64 @@ CompositeDatasource.prototype._getDatasourceInfo = function (query, absoluteOffs
8989
// We checked all datasources, return our accumulated information
9090
callback(chosenDatasource, chosenOffset, totalCount, hasExactCount);
9191
else {
92-
var ignoreTriples = { _push: noop, close: noop };
9392
var datasource = self._getDatasourceById(datasourceIndex);
94-
datasource._executeQuery(emptyQuery, ignoreTriples, function (metadata) {
95-
var count = metadata.totalCount;
96-
var exact = metadata.hasExactCount;
97-
// If we are still looking for an appropriate datasource, we need exact counts!
98-
if (offset > 0 && !exact) {
99-
self._getExactCount(datasource, query, function (exactCount) {
100-
count = exactCount;
101-
exact = true;
102-
continueRecursion();
103-
});
104-
}
105-
else
106-
continueRecursion();
107-
108-
function continueRecursion() {
109-
if (chosenDatasource < 0 && offset < count) {
110-
// We can start querying from this datasource
111-
setImmediate(function () {
112-
findRecursive(datasourceIndex + 1, offset - count, datasourceIndex, offset,
113-
totalCount + count, hasExactCount && exact);
93+
var metadataReader = {
94+
_push: noop,
95+
close: noop,
96+
setProperty: function (name, metadata) {
97+
if (name !== 'metadata') return;
98+
// If we are still looking for an appropriate datasource, we need exact counts
99+
var count = metadata.totalCount, exact = metadata.hasExactCount;
100+
if (offset > 0 && !exact) {
101+
self._getExactCount(datasource, query, function (exactCount) {
102+
count = exactCount;
103+
exact = true;
104+
continueRecursion();
114105
});
115106
}
116-
else {
117-
// We forward our accumulated information and go check the next datasource
118-
setImmediate(function () {
119-
findRecursive(datasourceIndex + 1, offset - count, chosenDatasource, chosenOffset,
120-
totalCount + count, hasExactCount && exact);
121-
});
107+
else
108+
continueRecursion();
109+
110+
function continueRecursion() {
111+
if (chosenDatasource < 0 && offset < count) {
112+
// We can start querying from this datasource
113+
setImmediate(function () {
114+
findRecursive(datasourceIndex + 1, offset - count, datasourceIndex, offset,
115+
totalCount + count, hasExactCount && exact);
116+
});
117+
}
118+
else {
119+
// We forward our accumulated information and go check the next datasource
120+
setImmediate(function () {
121+
findRecursive(datasourceIndex + 1, offset - count, chosenDatasource, chosenOffset,
122+
totalCount + count, hasExactCount && exact);
123+
});
124+
}
122125
}
123-
}
124-
});
126+
},
127+
};
128+
datasource._executeQuery(emptyQuery, metadataReader);
125129
}
126130
}
127131
};
128132

129133
// Writes the results of the query to the given triple stream
130-
CompositeDatasource.prototype._executeQuery = function (query, destination, metadataCallback) {
134+
CompositeDatasource.prototype._executeQuery = function (query, destination) {
131135
var offset = query.offset || 0, limit = query.limit || Infinity;
132136
var self = this;
133137
this._getDatasourceInfo(query, offset, function (datasourceIndex, relativeOffset, totalCount, hasExactCount) {
134138
if (datasourceIndex < 0) {
135139
// No valid datasource has been found
136-
metadataCallback({ totalCount: totalCount, hasExactCount: hasExactCount });
140+
destination.setProperty('metadata', { totalCount: totalCount, hasExactCount: hasExactCount });
137141
destination.close();
138142
}
139143
else {
140144
// Send query to first applicable datasource and optionally emit triples from consecutive datasources
141-
metadataCallback({ totalCount: totalCount, hasExactCount: hasExactCount });
142-
var emitted = 0;
145+
destination.setProperty('metadata', { totalCount: totalCount, hasExactCount: hasExactCount });
143146

144147
// Modify our triple stream so that if all results from one datasource have arrived,
145148
// check if we haven't reached the limit and if so, trigger a new query for the next datasource.
149+
var emitted = 0;
146150
countItems(destination, function (localEmittedCount) {
147151
// This is called after the last element has been pushed
148152

lib/datasources/Datasource.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -112,20 +112,17 @@ Datasource.prototype.select = function (query, onError) {
112112
if (triple.object[0] === '_') triple.object = blankNodePrefix + triple.object.substr(2);
113113
return triple;
114114
});
115+
outputTriples.copyProperties(destination, ['metadata']);
115116
onError && outputTriples.on('error', onError);
116117

117118
// Execute the query
118-
try {
119-
this._executeQuery(query, destination, function (metadata) {
120-
setImmediate(function () { outputTriples.emit('metadata', metadata); });
121-
});
122-
}
119+
try { this._executeQuery(query, destination); }
123120
catch (error) { outputTriples.emit('error', error); }
124121
return outputTriples;
125122
};
126123

127124
// Writes the results of the query to the given destination
128-
Datasource.prototype._executeQuery = function (query, destination, metadataCallback) {
125+
Datasource.prototype._executeQuery = function (query, destination) {
129126
throw new Error('_executeQuery has not been implemented');
130127
};
131128

lib/datasources/ExternalHdtDatasource.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ ExternalHdtDatasource.prototype._initialize = function (done) {
3333
};
3434

3535
// Writes the results of the query to the given triple stream
36-
ExternalHdtDatasource.prototype._executeQuery = function (query, destination, metadataCallback) {
36+
ExternalHdtDatasource.prototype._executeQuery = function (query, destination) {
3737
// Execute the external HDT utility
3838
var hdtFile = this._hdtFile, offset = query.offset || 0, limit = query.limit || Infinity,
3939
hdt = spawn(hdtUtility, ['--query', (query.subject || '?s') + ' ' +
@@ -52,7 +52,7 @@ ExternalHdtDatasource.prototype._executeQuery = function (query, destination, me
5252
// Ensure the estimated total count is as least as large as the number of triples
5353
if (tripleCount && estimatedTotalCount < offset + tripleCount)
5454
estimatedTotalCount = offset + (tripleCount < query.limit ? tripleCount : 2 * tripleCount);
55-
metadataCallback({ totalCount: estimatedTotalCount, hasExactCount: hasExactCount });
55+
destination.setProperty('metadata', { totalCount: estimatedTotalCount, hasExactCount: hasExactCount });
5656
destination.close();
5757
}
5858
});

lib/datasources/HdtDatasource.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ HdtDatasource.prototype._initialize = function (done) {
2828
};
2929

3030
// Writes the results of the query to the given triple stream
31-
HdtDatasource.prototype._executeQuery = function (query, destination, metadataCallback) {
31+
HdtDatasource.prototype._executeQuery = function (query, destination) {
3232
this._hdtDocument.search(query.subject, query.predicate, query.object,
3333
{ limit: query.limit, offset: query.offset },
3434
function (error, triples, estimatedTotalCount, hasExactCount) {
@@ -37,7 +37,7 @@ HdtDatasource.prototype._executeQuery = function (query, destination, metadataCa
3737
var tripleCount = triples.length, offset = query.offset || 0;
3838
if (tripleCount && estimatedTotalCount < offset + tripleCount)
3939
estimatedTotalCount = offset + (tripleCount < query.limit ? tripleCount : 2 * tripleCount);
40-
metadataCallback({ totalCount: estimatedTotalCount, hasExactCount: hasExactCount });
40+
destination.setProperty('metadata', { totalCount: estimatedTotalCount, hasExactCount: hasExactCount });
4141
// Add the triples to the output
4242
for (var i = 0; i < tripleCount; i++)
4343
destination._push(triples[i]);

lib/datasources/MemoryDatasource.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@ MemoryDatasource.prototype._getAllTriples = function (addTriple, done) {
2424
};
2525

2626
// Writes the results of the query to the given triple stream
27-
MemoryDatasource.prototype._executeQuery = function (query, destination, metadataCallback) {
28-
var offset = query.offset || 0, limit = query.limit || Infinity,
27+
MemoryDatasource.prototype._executeQuery = function (query, destination) {
28+
var offset = query.offset || 0, limit = query.limit || Infinity,
2929
triples = this._tripleStore.findByIRI(query.subject, query.predicate, query.object);
3030
// Send the metadata
31-
metadataCallback({ totalCount: triples.length, hasExactCount: true });
31+
destination.setProperty('metadata', { totalCount: triples.length, hasExactCount: true });
3232
// Send the requested subset of triples
3333
for (var i = offset, l = Math.min(offset + limit, triples.length); i < l; i++)
3434
destination._push(triples[i]);

lib/datasources/SparqlDatasource.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ function SparqlDatasource(options) {
2626
Datasource.extend(SparqlDatasource, ['triplePattern', 'limit', 'offset', 'totalCount']);
2727

2828
// Writes the results of the query to the given triple stream
29-
SparqlDatasource.prototype._executeQuery = function (query, destination, metadataCallback) {
29+
SparqlDatasource.prototype._executeQuery = function (query, destination) {
3030
// Create the HTTP request
3131
var sparqlPattern = this._createTriplePattern(query), self = this,
3232
constructQuery = this._createConstructQuery(sparqlPattern, query.offset, query.limit),
@@ -60,8 +60,10 @@ SparqlDatasource.prototype._executeQuery = function (query, destination, metadat
6060

6161
// Determine the total number of matching triples
6262
this._getPatternCount(sparqlPattern, function (error, totalCount) {
63-
if (error) emitError(error);
64-
else if (typeof totalCount === 'number') metadataCallback({ totalCount: totalCount, hasExactCount: true });
63+
if (error)
64+
emitError(error);
65+
else if (typeof totalCount === 'number')
66+
destination.setProperty('metadata', { totalCount: totalCount, hasExactCount: true });
6567
});
6668

6769
// Emits an error on the triple stream

lib/views/summary/SummaryRdfView.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ RdfView.extend(SummaryRdfView);
1414
// Generates triples and quads by sending them to the data and/or metadata callbacks
1515
SummaryRdfView.prototype._generateRdf = function (settings, data, metadata, done) {
1616
// Add summary triples
17-
settings.resultStream.on('data', data);
18-
settings.resultStream.on('end', done);
17+
settings.results.on('data', data);
18+
settings.results.on('end', done);
1919
};
2020

2121
module.exports = SummaryRdfView;

0 commit comments

Comments
 (0)