     "Remove some records from the table, identified by a collection of id keys.
     Returns an updated table.")
 
+  (flush!
+    [table]
+    [table apply-patches?]
+    "Ensure that all local state has been persisted to the storage backend and
+    return an updated persisted table.
+
+    If `apply-patches?` is true, the current patch data will be merged into the
+    main data tree and the returned table will have no patch link.")
+
   ;; Partitions
 
   (list-partitions

  (.dirty table))


; TODO: put this in protocol?
(defn flush!
  "Ensure that all local state has been persisted to the storage backend and
  return an updated non-dirty table."
  ([table]
   (flush! table false))
  ([^Table table apply-patches?]
   (if (dirty? table)
     (let [[patch-link data-link]
             (if (or (seq (.pending table)) (::patch table))
               (if apply-patches?
                 ; Combine pending changes and patch tablet and update data tree.
                 [nil (throw (UnsupportedOperationException. "updated data-tree link"))]
                 ; flush any pending changes to the patch tablet
                 ; if patch tablet overflows, update main data tree
                 (throw (UnsupportedOperationException. "NYI")))
               ; No patch data or tablet.
               [nil (::data table)])
           root-data (-> (.root-data table)

                           patch-link (assoc ::patch patch-link)
                           data-link (assoc ::data data-link)))]
       (when-not (s/valid? ::node-data root-data)
         (throw (ex-info (str "Cannot write invalid table root node: "
                              (s/explain-str ::node-data root-data))
                         {:type ::invalid-root})))
       (let [node (mdag/store-node! (.store table) nil root-data)
             table-info (assoc (.table-info table)
                               ::node/id (::node/id node)
                               ::data/size (node/reachable-size node))
             table-meta (assoc (._meta table)
                               ::node/links (::node/links node))]
         (->Table
           (.store table)
           table-info
           (::node/data node)
           (sorted-map-by key/compare)
           false
           table-meta)))
     ; Table is clean, return directly.
     table)))

