Browse files

add recursive find

  • Loading branch information...
1 parent 1fc6ead commit 00243acda8e080a0977ba97df1ac488192205c1b @slyphon slyphon committed Mar 18, 2011
Showing with 50 additions and 2 deletions.
  1. +17 −0 lib/z_k.rb
  2. +5 −2 lib/z_k/client.rb
  3. +28 −0 lib/z_k/find.rb
View
17 lib/z_k.rb
@@ -21,6 +21,7 @@
require 'z_k/mongoid'
require 'z_k/client'
require 'z_k/pool'
+require 'z_k/find'
module ZK
ZK_ROOT = File.expand_path('../..', __FILE__)
@@ -69,5 +70,21 @@ def self.open(*args)
def self.new_pool(host, opts={})
ZK::Pool::Bounded.new(host, opts)
end
+
+ # Eventually this will implement proper File.join-like behavior, but only
+ # using the '/' char for a separator. for right now, this simply delegates to
+ # File.join
+ #--
+ # like File.join but ignores $INPUT_RECORD_SEPARATOR (i.e. $/, which is
+ # platform dependent) and only uses the '/' character
+ def self.join(*paths)
+ File.join(*paths)
+ end
+
+ protected
+ def self.chomp_sep(str)
+ p = (p[0] == ?/ ) ? p[1..-1] : p
+ p = (p[-1] == ?/) ? p[0..-2] : p
+ end
end
View
7 lib/z_k/client.rb
@@ -649,8 +649,6 @@ def set_acl(path, acls, opts={})
opts[:callback] ? nil : rv[:stat]
end
-
-
#--
#
# EXTENSIONS
@@ -705,6 +703,11 @@ def rm_rf(paths)
end
end
+ # see ZK::Find for explanation
+ def find(*paths, &block)
+ ZK::Find.find(self, *paths, &block)
+ end
+
# will block the caller until +abs_node_path+ has been removed
#
# NOTE: this is dangerous to use in callbacks! there is only one
View
28 lib/z_k/find.rb
@@ -0,0 +1,28 @@
+module ZK
+ module Find
+ # like ruby's Find module, will call the given block with each _absolute_ znode path
+ # under +paths+. you can call ZK::Find.prune if you want to not recurse
+ # deeper under the current directory path.
+ def find(zk, *paths) #:yield: znode_path
+ paths.collect!{|d| d.dup}
+
+ while p = paths.shift
+ catch(:prune) do
+ yield p.dup.taint
+ next unless zk.exists?(p)
+
+ zk.children(p).each do |ch|
+ paths.unshift ZK.join(p, ch).untaint
+ end
+ end
+ end
+ end
+
+ def prune
+ throw :prune
+ end
+
+ module_function :find, :prune
+ end
+end
+

0 comments on commit 00243ac

Please sign in to comment.