-
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
Changes from 1 commit
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 |
---|---|---|
@@ -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.getAllChildren(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(); |
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.removeAll(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(); |
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.removeAll = 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.getAllChildren(path, function (error, children) { | ||
if (error) { | ||
callback(error); | ||
return; | ||
} | ||
async.eachSeries(children.reverse(), function (nodePath, next) { | ||
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.
|
||
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 getAllChildren | ||
* @param path {String} The node path. | ||
* @param callback {Function} The callback function. | ||
*/ | ||
Client.prototype.getAllChildren = function(path, callback) { | ||
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. Why do we need to expose this method? Again, the the 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. It's also a public method on ZKUtil. I'll rename this to |
||
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` | ||
* | ||
|
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.
I like the ZKUtil names better:
removeRecursively
, which makes it clear that it will remove the provided root node as well.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's currently named
deleteRecursive
(https://github.com/apache/zookeeper/blob/master/zookeeper-server/src/main/java/org/apache/zookeeper/ZKUtil.java#L51) but to keep theremove
naming convention of this library, should I change toremoveRecursive
?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.
Renamed to
removeRecursive
andlistSubTreeBFS