   (use (fleetdb util)))
 
 (defn init []
-  {:rmap {}
+  {:rmap (sorted-map)
    :imap {}})
 
 (def- conj-op?

        indexed (index aval)]
    (index-flatten1 indexed)))

(defmethod exec :index-multilookup [db {:keys [attr in]}]
  (let [index    (index-get db {:on attr})
        indexeds (map index in)]
    (index-flatten indexeds)))

(def- one-sided-op-fns
  {:< < :<= <= :> > :>= >=})

(def- two-sided-op-fns
  {:>< [> <] :>=< [>= <] :><= [> <=] :>=<= [>= <=]})

(defmethod exec :index-range [db {:keys [attr dir op aval]}]
  (let [index     (index-get db {:on attr})
        subseq-fn (if (= dir :asc) subseq rsubseq)
        pairs     (if-let [op-fn (one-sided-op-fns op)]
                    (subseq-fn index op-fn aval)
                    (let [[op-fn1 op-fn2] (two-sided-op-fns op)
                          [aval1  aval2]   aval]
                      (subseq-fn index op-fn1 aval1 op-fn2 aval2)))
        indexeds  (map val pairs)]
    (index-flatten indexeds)))

(defmethod exec :union [db {:keys [source]}]
  (union-stream (map #(exec db %) source)))

      ; when reasonable index and order -> may be suboptimal, but reasonable
      (= := op)
        (let [[attr aval] wrest]
          (cond
            (= attr :id)
              {:action  :rmap-lookup
               :id      aval
               :ordered false}
            (index-get db {:on attr})
              {:action  :index-lookup
               :attr    attr
               :aval    aval
               :ordered false}
            :no-index
              (root-filter-plan db where order)))

      ; as above
      (= :in op)
        (let [[attr in] wrest]
          (cond
            (= attr :id)
              {:action  :rmap-multilookup
               :ids     in
               :ordered false}
            (index-get db {:on attr})
              {:action  :index-multilookup
               :attr    attr
               :in      in
               :ordered false}
            :no-index
              (root-filter-plan db where order)))

      ; hmm
      (range-op? op)
        (let [[attr aval] wrest]
          (cond
            (= attr :id)
              (let [[oattr odir] order
                    ordered      (= :id oattr)]
                {:action  :rmap-range
                 :dir     (if ordered oattr)
                 :op      op
                 :idval   aval
                 :ordered ordered})
            (index-get db {:on attr})
              (let [[oattr odir] order
                    ordered      (= attr oattr)]
                {:action  :index-range
                 :attr    attr
                 :dir     (if ordered oattr)
                 :op      op
                 :aval    aval
                 :ordered ordered})
            :no-index
              (root-filter-plan db where order)))

      ; when no indexes use root-filter-plan -> optimal
      ; when some reasonable, most selective first index -> probably optimal
