Skip to content
This repository has been archived by the owner on Nov 9, 2017. It is now read-only.

Commit

Permalink
ZooKeeper: Make reduction of data optional in LiveList.get().
Browse files Browse the repository at this point in the history
If we need the raw list of objects so that we can know what objects can
be deleted etc. then we'll need to disable the reduction.
  • Loading branch information
spladug committed Aug 9, 2012
1 parent cf527d1 commit d896b9a
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions r2/r2/lib/zookeeper.py
Expand Up @@ -91,16 +91,20 @@ def __init__(self, client, root, map_fn=None, reduce_fn=lambda L: L,

@client.ChildrenWatch(root)
def watcher(children):
self.data = self._normalize_children(children)
self.data = self._normalize_children(children, reduce=True)

def _nodepath(self, item):
escaped = urllib.quote(str(item), safe=":")
return os.path.join(self.root, escaped)

def _normalize_children(self, children):
def _normalize_children(self, children, reduce):
unquoted = (urllib.unquote(c) for c in children)
mapped = map(self.map_fn, unquoted)
return list(self.reduce_fn(mapped))

if reduce:
return list(self.reduce_fn(mapped))
else:
return list(mapped)

def add(self, item):
path = self._nodepath(item)
Expand All @@ -114,9 +118,9 @@ def remove(self, item):
except NoNodeException:
raise ValueError("not in list")

def get(self):
def get(self, reduce=True):
children = self.client.get_children(self.root)
return self._normalize_children(children)
return self._normalize_children(children, reduce)

def __iter__(self):
if not self.is_watching:
Expand Down

0 comments on commit d896b9a

Please sign in to comment.