-
Notifications
You must be signed in to change notification settings - Fork 23
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
cascading PUTs #73
Merged
Merged
cascading PUTs #73
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,7 +15,8 @@ var _ = require('lodash'), | |
is = require('../assert-is'), | ||
path = require('path'), | ||
config = require('config'), | ||
glob = require('glob'); | ||
glob = require('glob'), | ||
referenceProperty = '_ref'; | ||
|
||
/** | ||
* Takes a ref, and returns the component name within it. | ||
|
@@ -56,11 +57,11 @@ function get(ref, locals) { | |
*/ | ||
function putLatest(ref, data) { | ||
data = JSON.stringify(data); | ||
return db.batch([ | ||
return [ | ||
{ type: 'put', key: ref, value: data }, | ||
{ type: 'put', key: ref + '@latest', value: data }, | ||
{ type: 'put', key: ref + '@' + uid(), value: data} | ||
]); | ||
]; | ||
} | ||
|
||
/** | ||
|
@@ -71,10 +72,10 @@ function putLatest(ref, data) { | |
*/ | ||
function putPublished(ref, data) { | ||
data = JSON.stringify(data); | ||
return db.batch([ | ||
return [ | ||
{ type: 'put', key: ref + '@published', value: data }, | ||
{ type: 'put', key: ref + '@' + uid(), value: data} | ||
]); | ||
]; | ||
} | ||
|
||
/** | ||
|
@@ -86,9 +87,9 @@ function putPublished(ref, data) { | |
*/ | ||
function putTag(ref, data, tag) { | ||
data = JSON.stringify(data); | ||
return db.batch([ | ||
return [ | ||
{ type: 'put', key: ref + '@' + tag, value: data } | ||
]); | ||
]; | ||
} | ||
|
||
/** | ||
|
@@ -123,16 +124,76 @@ function putDefaultBehavior(ref, data) { | |
* @param data | ||
*/ | ||
function put(ref, data) { | ||
var promise, | ||
var result, | ||
componentModule = files.getComponentModule(getName(ref)); | ||
|
||
if (componentModule && _.isFunction(componentModule.put)) { | ||
promise = componentModule.put(ref, data); | ||
result = componentModule.put(ref, data); | ||
} else { | ||
promise = putDefaultBehavior(ref, data); | ||
result = putDefaultBehavior(ref, data); | ||
} | ||
|
||
return is.promise(promise, ref).return(data); | ||
return result; | ||
} | ||
|
||
/** | ||
* Clear all of an object properties (in place), not a new object | ||
* | ||
* No need to return anything since it's in-place | ||
* | ||
* @param {object} obj | ||
*/ | ||
function clearOwnProperties(obj) { | ||
_.forOwn(obj, function (value, key) { | ||
delete obj[key]; | ||
}); | ||
} | ||
|
||
/** | ||
* True if this is a reference object that also has real data in it. | ||
* | ||
* Used to determine if that data should be preserved or not. | ||
* | ||
* @param {object} obj | ||
* @returns {boolean} | ||
*/ | ||
function isReferencedAndReal(obj) { | ||
return _.isString(obj[referenceProperty]) && _.size(obj) > 1; | ||
} | ||
|
||
/** | ||
* @param {string} ref | ||
* @param {object} data | ||
* @returns {Promise.object} | ||
*/ | ||
function cascadingPut(ref, data) { | ||
//search for _ref with _size greater than 1 | ||
var ops = [], | ||
deepObjects = _.listDeepObjects(data, isReferencedAndReal); | ||
|
||
//reverse; children should be before parents | ||
_.each(deepObjects.reverse(), function (obj) { | ||
var ref = obj[referenceProperty]; | ||
|
||
// since children are before parents, no one will see data below them | ||
ops.push({key: ref, value: _.omit(obj, referenceProperty)}); | ||
|
||
// omit cloned 1 level deep and we clear what omit cloned from obj | ||
// so the op gets the first level of data, but it's removed from the main obj | ||
clearOwnProperties(obj); | ||
obj[referenceProperty] = ref; | ||
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. @cruzanmo Specifically, note this comment. It's also the reason why ordering is important for the Algorithms are fun! :D |
||
}); | ||
|
||
//add the cleaned root object at the end | ||
ops.push({key: ref, value: data}); | ||
|
||
return bluebird.map(ops, function (op) { | ||
//run each through the normal put, which may or may not hit custom component logic | ||
return put(op.key, op.value); | ||
}).then(function (ops) { | ||
//flatten to list of final batch ops | ||
return db.batch(_.filter(_.flattenDeep(ops), _.identity)); | ||
}).return(data); //return root object if successful | ||
} | ||
|
||
/** | ||
|
@@ -202,7 +263,7 @@ function getTemplate(ref) { | |
|
||
//outsiders can act on components too | ||
module.exports.get = get; | ||
module.exports.put = put; | ||
module.exports.put = cascadingPut; //special: could lead to multiple put operations | ||
module.exports.del = del; | ||
|
||
//maybe server.js people want to reach in and reuse these? | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Why not just
obj = {};
?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.
Because it needs to be a particular object, specifically the one linked to by the original object. That's what it means by "in-place". Creating a new object might as well be {}, like you say.
In this case, there is a root object with many objects within it, and we're clearing those deep objects located somewhere deep within that root object. We don't know where these deep objects are -- they could even be within several layers of other objects within the root object.