From 304aadd4f7632afec6b1fd9ced03ee41f60398dc Mon Sep 17 00:00:00 2001
From: Brian Warner <warner@lothar.com>
Date: Mon, 12 Oct 2009 17:24:40 -0700
Subject: [PATCH] dirnode.set_children: take a dict, not a list

---
 src/allmydata/dirnode.py             | 12 ++++++------
 src/allmydata/interfaces.py          |  9 +++++----
 src/allmydata/test/test_deepcheck.py |  4 ++--
 src/allmydata/test/test_dirnode.py   | 19 +++++++++++--------
 src/allmydata/web/directory.py       |  4 ++--
 5 files changed, 26 insertions(+), 22 deletions(-)

diff --git a/src/allmydata/dirnode.py b/src/allmydata/dirnode.py
index 20faef2e..8dc9139c 100644
--- a/src/allmydata/dirnode.py
+++ b/src/allmydata/dirnode.py
@@ -400,14 +400,14 @@ class DirectoryNode:
         # this takes URIs
         a = Adder(self, overwrite=overwrite)
         node_entries = []
-        for e in entries:
-            if len(e) == 3:
-                name, writecap, readcap = e
+        for (name, e) in entries.iteritems():
+            assert isinstance(name, unicode)
+            if len(e) == 2:
+                writecap, readcap = e
                 metadata = None
             else:
-                assert len(e) == 4
-                name, writecap, readcap, metadata = e
-            assert isinstance(name, unicode)
+                assert len(e) == 3
+                writecap, readcap, metadata = e
             precondition(isinstance(writecap, (str,type(None))), writecap)
             precondition(isinstance(readcap, (str,type(None))), readcap)
             child_node = self._create_node(writecap, readcap)
diff --git a/src/allmydata/interfaces.py b/src/allmydata/interfaces.py
index ce6bc5b2..d2a93e3e 100644
--- a/src/allmydata/interfaces.py
+++ b/src/allmydata/interfaces.py
@@ -898,10 +898,11 @@ class IDirectoryNode(IMutableFilesystemNode):
         NotMutableError."""
 
     def set_children(entries, overwrite=True):
-        """Add multiple (name, writecap, readcap) triples (or (name,
-        writecap, readcap, metadata) 4-tuples) to a directory node. Returns a
-        Deferred that fires (with None) when the operation finishes. This is
-        equivalent to calling set_uri() multiple times, but is much more
+        """Add multiple children (by writecap+readcap) to a directory node.
+        Takes a dictionary, with childname as keys and (writecap, readcap)
+        tuples (or (writecap, readcap, metadata) triples) as values. Returns
+        a Deferred that fires (with None) when the operation finishes. This
+        is equivalent to calling set_uri() multiple times, but is much more
         efficient. All child names must be unicode strings.
         """
 
diff --git a/src/allmydata/test/test_deepcheck.py b/src/allmydata/test/test_deepcheck.py
index be3f35a6..0ec2ef84 100644
--- a/src/allmydata/test/test_deepcheck.py
+++ b/src/allmydata/test/test_deepcheck.py
@@ -1184,10 +1184,10 @@ class Large(DeepCheckBase, unittest.TestCase):
         d.addCallback(lambda root: root.create_empty_directory(u"subdir"))
         def _add_children(subdir_node):
             self.subdir_node = subdir_node
-            kids = []
+            kids = {}
             for i in range(1, COUNT):
                 litcap = LiteralFileURI("%03d-data" % i).to_string()
-                kids.append( (u"%03d-small" % i, litcap, litcap) )
+                kids[u"%03d-small" % i] = (litcap, litcap)
             return subdir_node.set_children(kids)
         d.addCallback(_add_children)
         up = upload.Data("large enough for CHK" * 100, "")
diff --git a/src/allmydata/test/test_dirnode.py b/src/allmydata/test/test_dirnode.py
index aee125bf..b4c2f760 100644
--- a/src/allmydata/test/test_dirnode.py
+++ b/src/allmydata/test/test_dirnode.py
@@ -449,18 +449,21 @@ class Dirnode(GridTestMixin, unittest.TestCase,
 
             # metadata through set_children()
             d.addCallback(lambda res:
-                          n.set_children([
-                              (u"e1", fake_file_uri, fake_file_uri),
-                              (u"e2", fake_file_uri, fake_file_uri, {}),
-                              (u"e3", fake_file_uri, fake_file_uri,
-                               {"key": "value"}),
-                              ]))
+                          n.set_children({
+                              u"e1": (fake_file_uri, fake_file_uri),
+                              u"e2": (fake_file_uri, fake_file_uri, {}),
+                              u"e3": (fake_file_uri, fake_file_uri,
+                                      {"key": "value"}),
+                              }))
             d.addCallback(lambda res:
                           self.shouldFail(ExistingChildError, "set_children-no",
                                           "child 'e1' already exists",
                                           n.set_children,
-                                          [ (u"e1", other_file_uri, other_file_uri),
-                                            (u"new", other_file_uri, other_file_uri), ],
+                                          { u"e1": (other_file_uri,
+                                                    other_file_uri),
+                                            u"new": (other_file_uri,
+                                                     other_file_uri),
+                                            },
                                           overwrite=False))
             # and 'new' should not have been created
             d.addCallback(lambda res: n.list())
diff --git a/src/allmydata/web/directory.py b/src/allmydata/web/directory.py
index 89b08725..37dc574b 100644
--- a/src/allmydata/web/directory.py
+++ b/src/allmydata/web/directory.py
@@ -467,7 +467,7 @@ class DirectoryNodeHandler(RenderMixin, rend.Page, ReplaceMeMixin):
             le.args = tuple(le.args + (body,))
             # TODO test handling of bad JSON
             raise
-        cs = []
+        cs = {}
         for name, (file_or_dir, mddict) in children.iteritems():
             name = unicode(name) # simplejson-2.0.1 returns str *or* unicode
             writecap = mddict.get('rw_uri')
@@ -476,7 +476,7 @@ class DirectoryNodeHandler(RenderMixin, rend.Page, ReplaceMeMixin):
             readcap = mddict.get('ro_uri')
             if readcap is not None:
                 readcap = str(readcap)
-            cs.append((name, writecap, readcap, mddict.get('metadata')))
+            cs[name] = (writecap, readcap, mddict.get('metadata'))
         d = self.node.set_children(cs, replace)
         d.addCallback(lambda res: "Okay so I did it.")
         # TODO: results
-- 
2.45.2