 (defmethod exec :db-scan [{:keys [rmap]} plan]
   (vals rmap))
 
+(defmethod exec :rmap-scan [{:keys [rmap]} {:keys [dir]}]
+  (let [seq-fn (if (= dir :asc) seq rseq)]
+    (map val (seq-fn rmap))))
+
 (defmethod exec :index-scan [db {:keys [attr dir]}]
   (let [index    (index-get db {:on attr})
         seq-fn   (if (= dir :asc) seq rseq)

           :source  sub-plans})

      :else
        (raise "where op " op " not recognized"))))

(defn- order-plan [source order]
  (if (or (not order) (:ordered source))

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

(defmethod exec :rmap-lookup [{:keys [rmap]} {:keys [id]}]
  (get rmap id))

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

(defmethod exec :rmap-multilookup [{:keys [rmap]} {:keys [ids]}]
  (map rmap ids))

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

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

(defn- index-range [index dir op ival]
  (let [subseq-fn (if (= dir :asc) subseq rsubseq)
        pairs     (if-let [op-fn (one-sided-op-fns op)]
                    (subseq-fn index op-fn ival)
                    (let [[op-fn1 op-fn2] (two-sided-op-fns op)
                          [ival1  ival2]  ival]
                      (subseq-fn index op-fn1 ival1 op-fn2 ival2)))]
    (map val pairs)))

(defmethod exec :index-range [db {:keys [attr dir op aval]}]
  (let [index     (index-get db {:on attr})]
    (index-flatten (index-range index dir op aval))))

(defmethod exec :rmap-range [{:keys [rmap]} {:keys [dir op idval]}]
  (index-range rmap dir op idval))

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