Created
November 1, 2012 22:20
-
-
Save ninjudd/3997074 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn codec-finder | |
"Returns a function to find the first [path, format] pair in the applicable layout that matches | |
keyseq exactly." | |
[layer codec-type] | |
(let [layout-fn (memoize (fn [id] (layout :read layer id)))] | |
(fn [keyseq] | |
(let [layout (layout-fn (first keyseq))] | |
(when-let [[path format] (first (filter #(match-path? (first %) keyseq) | |
layout))] | |
(or (get format key) | |
(get format :codec))))))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn- delete-in-node! | |
"Given a layer and a keyseq, delete every key beneath that keyseq. We need the | |
node-format in case the layer is in append-only mode so we can encode a reset." | |
[layer keyseq] | |
(when (seq keyseq) | |
(let [{:keys [key-codec append-only?]} layer | |
{:keys [start stop]} (bounds key-codec keyseq) | |
reset-codec (codec-finder layer :reset)] | |
(loop [cur (db/cursor (:db layer) start)] | |
(when cur | |
(when-let [^bytes key (cursor/key cur)] | |
(when (neg? (compare-bytes key stop)) | |
(recur (if append-only? | |
(let [keyseq (decode key-codec key) | |
deleted (encode (reset-codec keyseq) {})] | |
(-> cur | |
(cursor/append deleted) | |
(cursor/next))) | |
(cursor/delete cur)))))))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment