recursive zk delete

This commit is contained in:
Spencer Herzberg 2015-05-12 11:25:22 -05:00
parent f3f6466730
commit 3a6a060b2e
2 changed files with 34 additions and 1 deletions

View file

@ -112,6 +112,19 @@ func testBackend(t *testing.T, b Backend) {
t.Fatalf("err: %v", err)
}
// Make a nested entry
e = &Entry{Key: "foo/bar", Value: []byte("baz")}
err = b.Put(e)
if err != nil {
t.Fatalf("err: %v", err)
}
// Delete with children should work
err = b.Delete("foo")
if err != nil {
t.Fatalf("err: %v", err)
}
}
func testBackend_ListPrefix(t *testing.T, b Backend) {

View file

@ -88,6 +88,26 @@ func (c *ZookeeperBackend) ensurePath(path string, value []byte) error {
return nil
}
// deletePath is a helper that will recursively delete
// a given path
func (c *ZookeeperBackend) deletePath(path string) error {
children, _, _ := c.client.Children(path)
for _, childPath := range children {
err := c.deletePath(path + "/" + childPath)
if err != nil {
return err
}
}
err := c.client.Delete(path, -1)
if err != nil {
return err
}
return nil
}
// Put is used to insert or update an entry
func (c *ZookeeperBackend) Put(entry *Entry) error {
defer metrics.MeasureSince([]string{"zookeeper", "put"}, time.Now())
@ -136,7 +156,7 @@ func (c *ZookeeperBackend) Delete(key string) error {
// Delete the full path
fullPath := c.path + key
err := c.client.Delete(fullPath, -1)
err := c.deletePath(fullPath)
// Mask if the node does not exist
if err == zk.ErrNoNode {