   (:require [aurora.btree :refer [tree iterator key-lt key-lte key-gt key-gte]])
   (:require-macros [aurora.macros :refer [typeof]]))
 
-(defn lt [a b]
+(defn nilless-lt [a b]
   (if (== a nil)
     false
     (or (and (identical? (typeof a) (typeof b))
              (< a b))
         (< (typeof a) (typeof b)))))
+
+(defn gt [a b]
+  (and (not (identical? a b))
+       (or (and (identical? (typeof a) (typeof b))
+                (> a b))
+           (> (typeof a) (typeof b)))))
+
+(defn lt [a b]
+  (and (not (identical? a b))
+       (or (and (identical? (typeof a) (typeof b))
+                (< a b))
+           (< (typeof a) (typeof b)))))
+
+(defn nilless-key-compare [as bs]
+  (let [as-len (alength as)
+        bs-len (alength bs)]
+    (assert (== as-len bs-len) (pr-str as bs))
+    (loop [i 0]
+      (if (< i as-len)
+        (let [a (aget as i)
+              b (aget bs i)]
+          (if (or (identical? a nil)
+                  (identical? b nil))
+            (recur (+ i 1))
+            (if (identical? a b)
+              (recur (+ i 1))
+              (if (lt a b)
+                -1
+                1))))
+        0))))
+
+(defn rewind-or-store
+  "Returns #js[ix, store?, rewind?]"
+  [as bs nil-ixs]
+  (let [len (alength nil-ixs)]
+    (loop [ix 0]
+      (if (< ix len)
+        (let [cur (aget nil-ixs ix)
+              rewind? (gt (aget as cur) (aget bs cur))]
+          (cond
+           (and (identical? cur 0) rewind?) #js [0 false rewind?]
+           (> cur 0) (let [store? (gt (aget as (- cur 1)) (aget bs (- cur 1)))]
+                       (if store?
+                         #js [cur store? false]
+                         (if rewind?
+                           #js [cur false rewind?]
+                           (recur (+ 1 ix)))))
+           :else (recur (+ 1 ix))))
+        #js [0 false false]))))
 
 (defn filled-key-compare [as bs fill]
   (let [as-len (alength as)

        results (array)
        keys (array)
        min-fill (array)
        tuple-len (alength (.-map (aget iterators 0)))
        start (let [arr (array)]
                (dotimes [x tuple-len]
                  (.push arr false))
                arr)]
    (loop [key start
           i 10]
      (when (and key (> i 0))
        (println "key: " key)
        (when (seek-all iterators len key)
          (->keys iterators keys len)
          (println keys)
          (find-least keys len tuple-len min-fill)
          (println "Min: " min-fill)
          (recur (find-greatest iterators keys len results min-fill) (dec i)))))
    results
    ))

(comment


                   (if-not (nil? map-ix)
                     (aset cur-key ix (aget found map-ix))
                     (aset cur-key ix nil)))
                 (recur (+ 1 ix))))))
  (seek [this key]
        (let [ros (rewind-or-store key prev-seek nil-ixs)
              ros-ix (aget ros 0)]
          (when (identical? (aget ros 2) true)
            (.rewind this (aget ros 0)))
          (when (identical? (aget ros 1) true)
            (dotimes [x (alength cur-seek)]
              (aset cur-seek x false))
            (aset cur-seek (aget map ros-ix) (aget key ros-ix))
            (.seek iterator cur-seek)
            (.mark this ros-ix)
            (.set-key this))
          (when (identical? -1 (nilless-key-compare (.-cur-key this) key))
            (loop [ix 0]
              (when (< ix map-len)
                (let [map-ix (aget map ix)]
                  (when-not (== map-ix nil)
                    (aset cur-seek map-ix (aget key ix))))
                (recur (+ 1 ix))))
            (.seek iterator cur-seek)
            (.set-key this))
          (set! prev-seek key)
          )
        ))

(deftype MagicIteratorWrapper [iterator map]
  Object
  (key [this]
       (.key iterator))
  (val [this]
