|
| 1 | +/*! @license ©2016 Ruben Taelman - Multimedia Lab / iMinds / Ghent University */ |
| 2 | + |
| 3 | +/** A CompositeDatasource delegates queries to an consecutive list of datasources. */ |
| 4 | + |
| 5 | +var Datasource = require('./Datasource'), |
| 6 | + LRU = require('lru-cache'); |
| 7 | + |
| 8 | +// Creates a new CompositeDatasource |
| 9 | +function CompositeDatasource(options) { |
| 10 | + if (!(this instanceof CompositeDatasource)) |
| 11 | + return new CompositeDatasource(options); |
| 12 | + Datasource.call(this, options); |
| 13 | + |
| 14 | + if (!options.references) { |
| 15 | + throw new Error("A CompositeDatasource requires a `references` array of datasource id's in its settings."); |
| 16 | + } |
| 17 | + |
| 18 | + var allDatasources = options.datasources; |
| 19 | + this._datasources = {}; |
| 20 | + this._datasourceNames = []; |
| 21 | + for (var i = 0; i < options.references.length; i++) { |
| 22 | + var datasourceName = options.references[i]; |
| 23 | + var datasource = allDatasources[datasourceName]; |
| 24 | + if (!datasource) { |
| 25 | + throw new Error("No datasource " + datasourceName + " could be found!"); |
| 26 | + } |
| 27 | + if (datasource.enabled !== false) { |
| 28 | + this._datasources[datasourceName] = datasource; |
| 29 | + this._datasourceNames.push(datasourceName); |
| 30 | + } |
| 31 | + } |
| 32 | + this._countCache = LRU({ max: 1000, maxAge: 1000 * 60 * 60 * 3 }); |
| 33 | +} |
| 34 | +Datasource.extend(CompositeDatasource); |
| 35 | + |
| 36 | +// Checks whether the data source can evaluate the given query |
| 37 | +CompositeDatasource.prototype.supportsQuery = function (query) { |
| 38 | + for (var datasourceName in this._datasources) { |
| 39 | + if (this._getDatasourceByName(datasourceName).supportsQuery(query)) { |
| 40 | + return true; |
| 41 | + } |
| 42 | + } |
| 43 | + return false; |
| 44 | +}; |
| 45 | + |
| 46 | +// Find a datasource by datasource name |
| 47 | +CompositeDatasource.prototype._getDatasourceByName = function(datasourceName) { |
| 48 | + return this._datasources[datasourceName].datasource; |
| 49 | +}; |
| 50 | + |
| 51 | +// Find a datasource by datasource id inside this composition |
| 52 | +CompositeDatasource.prototype._getDatasourceById = function(datasourceIndex) { |
| 53 | + return this._datasources[this._datasourceNames[datasourceIndex]].datasource; |
| 54 | +}; |
| 55 | + |
| 56 | +// Count the amount of triple in the query result to get an exact count. |
| 57 | +CompositeDatasource.prototype._getExactCount = function(datasource, query, cb) { |
| 58 | + // Try to find a cache match |
| 59 | + var cacheKey = query.subject + "|" + query.predicate + "|" + query.object; |
| 60 | + var cache = this._countCache, count = cache.get(cacheKey); |
| 61 | + if (count) return setImmediate(cb, count); |
| 62 | + |
| 63 | + // Otherwise, count all the triples manually |
| 64 | + var emptyQuery = { offset: 0, subject: query.subject, predicate: query.predicate, object: query.object }; |
| 65 | + var exactCount = 0; |
| 66 | + var countingTripleStream = { push: function(triple) { |
| 67 | + if (triple) { |
| 68 | + exactCount++; |
| 69 | + } else { |
| 70 | + // Cache large values; small ones are calculated fast anyway |
| 71 | + if (exactCount > 1000) |
| 72 | + cache.set(cacheKey, exactCount); |
| 73 | + cb(exactCount); |
| 74 | + } |
| 75 | + }}; |
| 76 | + datasource._executeQuery(emptyQuery, countingTripleStream, noop); |
| 77 | +}; |
| 78 | + |
| 79 | +// Recursively find all required datasource composition info to perform a query. |
| 80 | +// The callback will provide the parameters: |
| 81 | +// Datasource id to start querying from |
| 82 | +// The offset to use to start querying from the given datasource id |
| 83 | +// The total count for all datasources |
| 84 | +CompositeDatasource.prototype._getDatasourceInfo = function(query, absoluteOffset, cb) { |
| 85 | + var self = this; |
| 86 | + var emptyQuery = { |
| 87 | + offset: 0, limit: 1, |
| 88 | + subject: query.subject, predicate: query.predicate, object: query.object |
| 89 | + }; |
| 90 | + return findRecursive(0, absoluteOffset, -1, -1, 0, cb, true); |
| 91 | + |
| 92 | + function findRecursive(datasourceIndex, offset, chosenDatasource, chosenOffset, totalCount, hasExactCount) { |
| 93 | + if (datasourceIndex >= self._datasourceNames.length) { |
| 94 | + // We checked all datasources, return our accumulated information |
| 95 | + cb(chosenDatasource, chosenOffset, totalCount, hasExactCount); |
| 96 | + } else { |
| 97 | + var emptyTripleStream = { push: noop }; |
| 98 | + var datasource = self._getDatasourceById(datasourceIndex); |
| 99 | + datasource._executeQuery(emptyQuery, emptyTripleStream, function (metadata) { |
| 100 | + var count = metadata.totalCount; |
| 101 | + var exact = metadata.hasExactCount; |
| 102 | + // If we are still looking for an appropriate datasource, we need exact counts! |
| 103 | + if (offset > 0 && !exact) { |
| 104 | + self._getExactCount(datasource, query, function(exactCount) { |
| 105 | + count = exactCount; |
| 106 | + exact = true; |
| 107 | + continueRecursion(); |
| 108 | + }); |
| 109 | + } else { |
| 110 | + continueRecursion(); |
| 111 | + } |
| 112 | + |
| 113 | + function continueRecursion() { |
| 114 | + if (chosenDatasource < 0 && offset < count) { |
| 115 | + // We can start querying from this datasource |
| 116 | + setImmediate(function () { |
| 117 | + findRecursive(datasourceIndex + 1, offset - count, datasourceIndex, offset, |
| 118 | + totalCount + count, hasExactCount && exact); |
| 119 | + }); |
| 120 | + } else { |
| 121 | + // We forward our accumulated information and go check the next datasource |
| 122 | + setImmediate(function () { |
| 123 | + findRecursive(datasourceIndex + 1, offset - count, chosenDatasource, chosenOffset, |
| 124 | + totalCount + count, hasExactCount && exact); |
| 125 | + }); |
| 126 | + } |
| 127 | + } |
| 128 | + }); |
| 129 | + } |
| 130 | + } |
| 131 | +}; |
| 132 | + |
| 133 | +function noop() {} |
| 134 | + |
| 135 | +// Writes the results of the query to the given triple stream |
| 136 | +CompositeDatasource.prototype._executeQuery = function (query, tripleStream, metadataCallback) { |
| 137 | + var offset = query.offset || 0, limit = query.limit || Infinity; |
| 138 | + var self = this; |
| 139 | + this._getDatasourceInfo(query, offset, function(datasourceIndex, relativeOffset, totalCount, hasExactCount) { |
| 140 | + if (datasourceIndex < 0) { |
| 141 | + // No valid datasource has been found |
| 142 | + metadataCallback({ totalCount: totalCount, hasExactCount: hasExactCount }); |
| 143 | + tripleStream.push(null); |
| 144 | + } else { |
| 145 | + // Send query to first applicable datasource and optionally emit triples from consecutive datasources |
| 146 | + metadataCallback({ totalCount: totalCount, hasExactCount: hasExactCount }); |
| 147 | + var emitted = 0; |
| 148 | + |
| 149 | + // Modify our triple stream so that if all results from one datasource have arrived, |
| 150 | + // check if we haven't reached the limit and if so, trigger a new query for the next datasource. |
| 151 | + tripleStream.push = makeSmartPush(tripleStream, function(localEmittedCount) { |
| 152 | + // This is called after the last element has been pushed |
| 153 | + |
| 154 | + // If we haven't reached our limit, try to fill it with other datasource query results. |
| 155 | + emitted += localEmittedCount; |
| 156 | + datasourceIndex++; |
| 157 | + if (emitted < limit && datasourceIndex < self._datasourceNames.length) { |
| 158 | + var localLimit = limit - emitted; |
| 159 | + var subQuery = { offset: 0, limit: localLimit, |
| 160 | + subject: query.subject, predicate: query.predicate, object: query.object }; |
| 161 | + self._getDatasourceById(datasourceIndex)._executeQuery(subQuery, tripleStream, function(){}); |
| 162 | + return true; |
| 163 | + } else { |
| 164 | + return false; |
| 165 | + } |
| 166 | + }); |
| 167 | + |
| 168 | + // Initiate query to the first datasource. |
| 169 | + var subQuery = { offset: relativeOffset, limit: limit, |
| 170 | + subject: query.subject, predicate: query.predicate, object: query.object }; |
| 171 | + self._getDatasourceById(datasourceIndex)._executeQuery(subQuery, tripleStream, function(){}); |
| 172 | + } |
| 173 | + }); |
| 174 | + |
| 175 | + // Replaces a tripleStream.push |
| 176 | + // It takes the tripleStream as first argument and a callback as second argument. |
| 177 | + // The callback will be called when the push function is called with a falsy value. |
| 178 | + // Returning a falsy value inside the callback will delegate the falsy value to the original |
| 179 | + // push function anyways. |
| 180 | + function makeSmartPush(self, nullCb) { |
| 181 | + var count = 0; |
| 182 | + var originalPush = self.push; |
| 183 | + return function(element) { |
| 184 | + if (!element) { |
| 185 | + if(!nullCb(count)) { |
| 186 | + originalPush.call(self, element); |
| 187 | + } |
| 188 | + } else { |
| 189 | + count++; |
| 190 | + originalPush.call(self, element); |
| 191 | + } |
| 192 | + }; |
| 193 | + } |
| 194 | +}; |
| 195 | + |
| 196 | +module.exports = CompositeDatasource; |
0 commit comments