-
Notifications
You must be signed in to change notification settings - Fork 147
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
Add removeRecursive and listSubTreeBFS methods #88
Merged
Merged
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
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 |
---|---|---|
@@ -0,0 +1,20 @@ | ||
var zookeeper = require('../index.js'); | ||
|
||
var client = zookeeper.createClient(process.argv[2]); | ||
var path = process.argv[3]; | ||
|
||
client.once('connected', function () { | ||
console.log('Connected to the server.'); | ||
|
||
client.listSubTreeBFS(path, function (error, children) { | ||
if (error) { | ||
console.log('Failed to list all child nodes of %s due to:', path, error); | ||
return; | ||
} | ||
console.log('All child nodes of %s are: %j', path, children); | ||
|
||
client.close(); | ||
}); | ||
}); | ||
|
||
client.connect(); |
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 |
---|---|---|
@@ -0,0 +1,20 @@ | ||
var zookeeper = require('../index.js'); | ||
|
||
var client = zookeeper.createClient(process.argv[2]); | ||
var path = process.argv[3]; | ||
|
||
client.once('connected', function () { | ||
console.log('Connected to the server.'); | ||
|
||
client.removeRecursive(path, function (error) { | ||
if (error) { | ||
console.log('Failed to remove all nodes for %s: %s', path, error); | ||
} else { | ||
console.log('Removed all nodes for: %s', path); | ||
} | ||
|
||
client.close(); | ||
}); | ||
}); | ||
|
||
client.connect(); |
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 |
---|---|---|
|
@@ -467,6 +467,45 @@ Client.prototype.remove = function (path, version, callback) { | |
); | ||
}; | ||
|
||
/** | ||
* Deletes a node and all its children. | ||
* | ||
* @param path {String} The node path. | ||
* @param [version=-1] {Number} The version of the node. | ||
* @param callback {Function} The callback function. | ||
*/ | ||
Client.prototype.removeRecursive = function(path, version, callback) { | ||
if (!callback) { | ||
callback = version; | ||
version = -1; | ||
} | ||
|
||
Path.validate(path); | ||
|
||
assert(typeof callback === 'function', 'callback must be a function.'); | ||
assert(typeof version === 'number', 'version must be a number.'); | ||
|
||
var self = this; | ||
|
||
self.listSubTreeBFS(path, function (error, children) { | ||
if (error) { | ||
callback(error); | ||
return; | ||
} | ||
async.eachSeries(children.reverse(), function (nodePath, next) { | ||
self.remove(nodePath, version, function(err) { | ||
// Skip NO_NODE exception | ||
if (err && err.getCode() === Exception.NO_NODE) { | ||
next(null); | ||
return; | ||
} | ||
|
||
next(err); | ||
}); | ||
}, callback); | ||
}); | ||
}; | ||
|
||
/** | ||
* Set the data for the node of the given path if such a node exists and the | ||
* optional given version matches the version of the node (if the given | ||
|
@@ -814,6 +853,45 @@ Client.prototype.getChildren = function (path, watcher, callback) { | |
); | ||
}; | ||
|
||
/** | ||
* BFS list of the system under path. Note that this is not an atomic snapshot of | ||
* the tree, but the state as it exists across multiple RPCs from clients to the | ||
* ensemble. | ||
* | ||
* @method listSubTreeBFS | ||
* @param path {String} The node path. | ||
* @param callback {Function} The callback function. | ||
*/ | ||
Client.prototype.listSubTreeBFS = function(path, callback) { | ||
Path.validate(path); | ||
assert(typeof callback === 'function', 'callback must be a function.'); | ||
|
||
var self = this; | ||
var tree = [path]; | ||
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. This includes the path that was passed in as part of the results, which is also what |
||
|
||
async.reduce(tree, tree, function(memo, item, next) { | ||
self.getChildren(item, function (error, children) { | ||
if (error) { | ||
next(error); | ||
return; | ||
} | ||
if (!children || !Array.isArray(children) || !children.length) { | ||
next(null, tree); | ||
return; | ||
} | ||
children.forEach(function(child) { | ||
var childPath = item + '/' + child; | ||
|
||
if (item === '/') { | ||
childPath = item + child; | ||
} | ||
tree.push(childPath); | ||
}); | ||
next(null, tree); | ||
}); | ||
}, callback); | ||
}; | ||
|
||
/** | ||
* Create node path in the similar way of `mkdir -p` | ||
* | ||
|
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.
reverse
is called here to remove leaf nodes first