Browse files

Delete ranges by appending :_reset, not by deleting the key

  • Loading branch information...
1 parent a00fc7e commit 1097f59b566a498f9fa9293fac225c246a023ff2 @amalloy amalloy committed Jan 13, 2012
Showing with 23 additions and 9 deletions.
  1. +1 −1 project.clj
  2. +22 −8 src/jiraph/masai_sorted_layer.clj
View
2 project.clj
@@ -2,7 +2,7 @@
:description "embedded graph db library for clojure"
:dependencies [[clojure "1.3.0"]
[useful "0.7.6-alpha1"]
- [masai "0.7.0-alpha6"]
+ [masai "0.7.0-alpha7"]
[cereal "0.2.0-alpha3"]
[retro "0.6.0-beta1"]
[io "0.1.0-alpha2"]
View
30 src/jiraph/masai_sorted_layer.clj
@@ -116,6 +116,24 @@
:else :node-format))]
[path (codec-fn {:revision revision, :id node-id})]))
+(defn- delete-ranges!
+ "Given a database and a sequence of [start, end) intervals, delete "
+ [layer deletion-ranges]
+ (let [db (:db layer)
+ delete-fn (if (:append-only? layer)
+ (fn [codec]
+ (let [deleted (bufseq->bytes (encode codec {:_reset true}))]
+ (fn [cursor]
+ (cursor/append cursor deleted))))
+ (constantly cursor/delete))]
+ (doseq [{:keys [start stop codec]} deletion-ranges]
+ (let [delete (delete-fn codec)]
+ (loop [cur (db/cursor db start)]
+ (when-let [k (cursor/key cur)]
+ (when-let [cur (and (neg? (compare (String. k) stop))
+ (delete cur))]
+ (recur cur))))))))
+
;; drop leading _ - NB must undo the meta-key impl in MasaiLayer
(defn- main-node-id [meta-id]
{:pre [(= "_" (first meta-id))]}
@@ -196,7 +214,9 @@
deletion-ranges (for [[path codec] codecs
:let [{:keys [start stop multi]} (bounds path)]
:when (and multi (prefix-of? (butlast path) keys))]
- [(str id ":" start) (str id ":" stop)])]
+ {:start (str id ":" start)
+ :stop (str id ":" stop)
+ :codec codec})]
(assert (seq codecs) "No codecs to write with")
;; ...TOOD special-case adjoin...
(or (and (not (next codecs)) ;; only one codec, see if we can optimize writing it
@@ -211,13 +231,7 @@
(let [old (-> (read-node codecs db id nil)
(get id))
new (apply update-in old keys f args)]
- (doseq [[start stop] deletion-ranges] ; delete the wildcard edges that are beneath
- ; the write position
- (loop [cur (db/cursor db start)]
- (when-let [k (cursor/key cur)]
- (when-let [cur (and (neg? (compare (String. k) stop))
- (cursor/delete cur))]
- (recur cur)))))
+ (delete-ranges! this deletion-ranges)
(loop [codecs codecs, node new]
(if-let [[[path codec] & more] (seq codecs)]
(recur more (reduce (fn [node path]

0 comments on commit 1097f59

Please sign in to comment.