   (loop [node (.-root tree)]
     (if (nil? (.-children node))
       (if (> (alength (.-keys node)) 0)
-        (Iterator. (.-max-keys tree) node 0 #js [] #js [] false)
-        (Iterator. (.-max-keys tree) node 0 #js [] #js [] true))
+        (Iterator. (.-max-keys tree) node 0 #js [] #js [] false (.-key-len tree))
+        (Iterator. (.-max-keys tree) node 0 #js [] #js [] true (.-key-len tree)))
       (recur (aget (.-children node) 0)))))
 
-(deftype Trieterator [iterator seek-key ^:mutable pinned-key ^:mutable pinned-key-ix ^:mutable moving-key-ix ^:mutable end?]
+;; trie view of a tree
+(deftype Trieterator [iterator seek-key ^:mutable pinned-key ^:mutable pinned-key-ix ^:mutable moving-key-ix ^:mutable end? key-len]
   Object
   (key [this]
        (aget (.key iterator) moving-key-ix))

            (set! end? true)
            (.search this 1)))))

;; TODO treeterator (key-ix->needed? , key-ix->produced?)

(defn intersection [iterators]
  (if (> (alength iterators) 1)
    (if (some #(.-end? %) iterators)
      (Intersection. iterators true)
      (let [intersection (Intersection. (into-array (sort-by #(.key %) key-compare iterators)) false)]
        (.search intersection 0)
        intersection))
    (aget iterators 0)))

(defn iter-seq [iterator]
  (let [results #js []]
    (while (false? (.-end? iterator))
      (.push results (.key iterator))
      (.next iterator))
    results))

(deftype Join [^:mutable key-ix iterators key-ix->intersection key-ix->iterator->present? ^:mutable end?]
  Object
  (key [this]
       (when (false? end?)
         (.key (aget key-ix->intersection key-ix))))
  (maintain [this]
            (set! end? (.-end? (aget key-ix->intersection key-ix))))
  (next [this]


;; NOTE iterators are not write-safe

(deftype Iterator [max-keys ^:mutable node ^:mutable ix marked-nodes marked-ixes ^:mutable end? key-len]
  Object
  (key [this]
       (when (false? end?)
