 
 ;; ## Construction
 
-(defn- store-index!
-  "Recursively store index nodes from the given sequence of children. Loads
-  each child to calculate the aggregate first-key, last-key, and count
-  attributes. Returns persisted index node data."
-  [store height children]
-  (loop [record-count 0
-         new-children []
-         child-keys []
-         last-last-key nil
-         children children]
-    (if (seq children)
-      ; Load and update aggregate stats from next child.
-      (let [child (first children)
-            link-name (format "%03d" (count new-children))
-            data (if (mdag/link? child)
-                   (graph/get-link! store child)
-                   child)
-            target (cond
-                     (mdag/link? child) child
-                     (= 1 height) (meta child)
-                     :else (meta (store-index! store (dec height) (::children data))))
-            link (mdag/link link-name target)]
-        (recur (int (+ record-count (::record/count data)))
-               (conj new-children link)
-               (conj child-keys (::record/first-key data))
-               (::record/last-key data)
-               (next children)))
-      ; No more children, serialize node.
-      (->>
-        {:data/type data-type
-         ::height height
-         ::keys (vec (rest child-keys))
-         ::children new-children
-         ::record/count record-count
-         ::record/first-key (first child-keys)
-         ::record/last-key last-last-key}
-        (mdag/store-node! store nil)
-        (::node/data)))))
+(defn- numbered-child-links
+  "Write a vector of childen into numbered links."
+  [children]
+  (let [width (count (pr-str (dec (count children))))
+        fmt (str "%0" width "d")]
+    (into []
+          (map-indexed #(mdag/link (format fmt %1) %2))
+          children)))
+
+
+(defn- index-children
+  "Aggregates index node values from a sequence of child data. Returns the node
+  data map for the constructed index."
+  [height children]
+  (let [ckeys (into [] (map ::record/first-key) (rest children))
+        links (numbered-child-links children)
+        record-count (reduce + 0 (map ::record/count children))]
+    {:data/type data-type
+     ::height height
+     ::keys ckeys
+     ::children links
+     ::record/count record-count
+     ::record/first-key (::record/first-key (first children))
+     ::record/last-key (::record/last-key (last children))}))
 
 
 (defn from-partitions

      (let [index-groups (part/partition-limited
                           (::branching-factor params default-branching-factor)
                           layer)]
        (recur (mapv (partial store-index! store height) index-groups)
               (inc height))))))




    ; Root is a partition node.
    (= part/data-type (:data/type root))
      (let [result (part/update-partitions! store params [[root changes]])]
        (if (neg? (first result))
          (part/from-records store params (second result))
          (from-partitions store params (second result))))

    ; Root is an index node.
    (= data-type (:data/type root))
      (letfn [(store! ; TODO: delay storage like this?
                [node]
                (if (mdag/link? node)
                  node
