                                                       :my-range ["0" "1"]}]
                                           :attrs []}}))
 
-(defn- merge-unprocessed
-  [more-f last-result]
-  (loop [{:keys [items unprocessed consumed-capacity]} last-result]
-    (if-not (seq unprocessed)
-      (if items (with-meta items {:consumed-capacity consumed-capacity})
-                last-result)
-
-      (let [{items* :items unprocessed* :unprocessed
-             consumed-capacity* :consumed-capacity}
-            (more-f unprocessed)]
-
-        (recur (merge {:unprocessed       unprocessed*
-                       :consumed-capacity (when consumed-capacity
-                                            (+ consumed-capacity
-                                               consumed-capacity*))}
-                      (when (or items items*)
-                        {:items (into (or items []) items*)})))))))
+(defn- merge-more
+  "Enables auto paging for batch batch-get/write and query/scan requests.
+  Particularly useful for throughput limitations."
+  [more-f max-idx last-result]
+  (loop [{:keys [unprocessed last-prim-kvs] :as last-result} last-result idx 1]
+    (let [more (or unprocessed last-prim-kvs)]
+      (if (or (empty? more) (>= idx max-idx))
+        (if-let [items (:items last-result)]
+          (with-meta items (dissoc last-result :items))
+          last-result)
+        (let [merge-results (fn [l r] (cond (number? l) (+    l r)
+                                           (vector? l) (into l r)
+                                           :else               r))]
+          (recur (merge-with merge-results last-result (more-f more))
+                 (inc idx)))))))
 
 (defn batch-get-item
   "Retrieves a batch of items in a single request.

  [creds table-name status & [{:keys [poll-ms timeout-ms timeout-val]
                               :or   {poll-ms 5000}}]]
  (assert (#{:creating :updating :deleting :active} (utils/un-enum status))
          (throw (Exception. (str "Invalid table status: " status))))
  (let [polling-future
        (future
          (loop []

               :consistent? true}
     :friends {:prim-kvs [{:catagory \"favorites\" :id [1 2 3]}
                          {:catagory \"recent\"    :id [7 8 9]}]}})"
  [creds requests & [{:keys [return-cc? max-reqs] :as opts
                      :or   {max-reqs 5}}]]
  (letfn [(run1 [raw-req]
            (as-map
             (.batchGetItem (db-client creds)
               (doto-maybe (BatchGetItemRequest.) g ; {table-str KeysAndAttributes}
                 :always    (.setRequestItems raw-req)
                 return-cc? (.setReturnConsumedCapacity (utils/enum :total))))))]
    (merge-more run1 max-reqs (run1 (batch-request-items requests)))))

(comment
  (def bigval (.getBytes (apply str (range 14000))))
  (defn- ids [from to] (for [n (range from to)] {:id n :name bigval}))
  (batch-write-item creds {:faraday.tests.main {:put (ids 0 10)}})
  (batch-get-item   creds {:faraday.tests.main {:prim-kvs {:id (range 20)}}})
  (scan creds :faraday.tests.main))

(defn- write-request [action item]
  (case action
