-
Notifications
You must be signed in to change notification settings - Fork 50
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow REST access to raw node data from REST. #646
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -311,9 +311,10 @@ Storage.prototype.loadObjects = function (data, callback) { | |
* @param {string} rootHash | ||
* @param {Object<string, object>} loadedObjects | ||
* @param {string} path | ||
* @param {boolean} excludeParents - if true will only include the node at the path | ||
* @returns {function|promise} | ||
*/ | ||
function loadPath(dbProject, rootHash, loadedObjects, path) { | ||
function loadPath(dbProject, rootHash, loadedObjects, path, excludeParents) { | ||
var deferred = Q.defer(), | ||
pathArray = path.split('/'); | ||
|
||
|
@@ -330,11 +331,14 @@ function loadPath(dbProject, rootHash, loadedObjects, path) { | |
} else { | ||
dbProject.loadObject(parentHash) | ||
.then(function (object) { | ||
loadedObjects[parentHash] = object; | ||
if (relPath) { | ||
hash = object[relPath]; | ||
if (!excludeParents) { | ||
loadedObjects[parentHash] = object; | ||
} | ||
loadParent(hash, pathArray.shift()); | ||
} else { | ||
loadedObjects[parentHash] = object; | ||
deferred.resolve(); | ||
} | ||
}) | ||
|
@@ -357,35 +361,53 @@ Storage.prototype.loadPaths = function (data, callback) { | |
|
||
this.mongo.openProject(data.projectId) | ||
.then(function (dbProject) { | ||
var loadedObjects = {}; | ||
|
||
//self.logger.error('paths:', data.paths); | ||
var loadedObjects = {}, | ||
throttleDeferred = Q.defer(), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what is saved here by having this 'throttle' function? shouldn't we use a library throttle (and for the loadObjects instead of the loadPath(s))? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. An example: Without the throttle the languageNode will be loaded N-times from the mongo database. With the throttle it is only loaded once. The loadObject-calls are already throttled for a one path (or what do you mean?). Q doesn't have a throttle and I wouldn't like to use another library. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see. I thought you were trying to throttle against too many paralel calls towards the db, but it okay then. |
||
counter = data.pathsInfo.length; | ||
|
||
Q.allSettled(data.pathsInfo.map(function (pathInfo) { | ||
return loadPath(dbProject, pathInfo.parentHash, loadedObjects, pathInfo.path); | ||
})) | ||
.then(function (results) { | ||
var keys = Object.keys(loadedObjects), | ||
i; | ||
function throttleLoad() { | ||
var pathInfo; | ||
|
||
for (i = 0; i < results.length; i += 1) { | ||
if (results[i].state === 'rejected') { | ||
self.logger.error('loadPaths failed, ignoring', data.pathsInfo[i].path, { | ||
metadata: results[i].reason, | ||
if (counter === 0) { | ||
throttleDeferred.resolve(); | ||
} else { | ||
counter -= 1; | ||
pathInfo = data.pathsInfo[counter]; | ||
loadPath(dbProject, pathInfo.parentHash, loadedObjects, pathInfo.path, data.excludeParents) | ||
.then(function () { | ||
throttleLoad(); | ||
}) | ||
.catch(function (err) { | ||
self.logger.error('loadPaths failed, ignoring', pathInfo.path, { | ||
metadata: err, | ||
}); | ||
} | ||
} | ||
throttleLoad(); | ||
}); | ||
} | ||
|
||
for (i = 0; i < keys.length; i += 1) { | ||
if (data.excludes.indexOf(keys[i]) > -1) { | ||
// https://jsperf.com/delete-vs-setting-undefined-vs-new-object | ||
// When sending the data these keys will be removed after JSON.stringify. | ||
loadedObjects[keys[i]] = undefined; | ||
return throttleDeferred.promise; | ||
} | ||
|
||
//Q.allSettled(data.pathsInfo.map(function (pathInfo) { | ||
// return loadPath(dbProject, pathInfo.parentHash, loadedObjects, pathInfo.path, data.excludeParents); | ||
//})) | ||
throttleLoad() | ||
.then(function () { | ||
var keys = Object.keys(loadedObjects), | ||
i; | ||
if (data.excludes) { | ||
for (i = 0; i < keys.length; i += 1) { | ||
if (data.excludes.indexOf(keys[i]) > -1) { | ||
// https://jsperf.com/delete-vs-setting-undefined-vs-new-object | ||
// When sending the data these keys will be removed after JSON.stringify. | ||
loadedObjects[keys[i]] = undefined; | ||
} | ||
} | ||
} | ||
|
||
deferred.resolve(loadedObjects); | ||
}); | ||
}) | ||
.catch(deferred.reject); | ||
}) | ||
.catch(function (err) { | ||
deferred.reject(err); | ||
|
@@ -407,7 +429,14 @@ Storage.prototype.getCommits = function (data, callback) { | |
self.logger.debug('commitHash was given will load commit', data.before); | ||
project.loadObject(data.before) | ||
.then(function (commitObject) { | ||
return project.getCommits(commitObject.time + 1, data.number); | ||
if (commitObject.type !== 'commit') { | ||
throw new Error('Commit object does not exist ' + data.before); | ||
} | ||
if (data.number === 1) { | ||
return [commitObject]; | ||
} else { | ||
return project.getCommits(commitObject.time + 1, data.number); | ||
} | ||
}) | ||
.then(function (commits) { | ||
deferred.resolve(commits); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not authorized status code is 403
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It was requested that if the user doesn't have read access the return code should be 404.
For two reasons;
The storage just checks if the user has the project in his/her project list (with read access) and does't check whether the project exists or not.
If the user doesn't have read access, he shouldn't even be able to get information about whether the project exists or not.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks