diff --git a/src/xitdb/hash_map.clj b/src/xitdb/hash_map.clj index df01503..8be3add 100644 --- a/src/xitdb/hash_map.clj +++ b/src/xitdb/hash_map.clj @@ -56,7 +56,7 @@ (= (into {} this) (into {} other)))) clojure.lang.Seqable - (seq [this] + (seq [_] (map-seq rhm)) clojure.lang.IFn @@ -161,7 +161,7 @@ (common/-read-from-cursor (conversion/map-write-cursor whm key))))) clojure.lang.Seqable - (seq [this] + (seq [_] (map-seq whm)) clojure.core.protocols/IKVReduce @@ -192,6 +192,3 @@ (defn xhash-map-counted [^ReadCursor read-cursor] (->XITDBHashMap (ReadCountedHashMap. read-cursor))) - - - diff --git a/src/xitdb/hash_set.clj b/src/xitdb/hash_set.clj index 6d2da8b..ff1110d 100644 --- a/src/xitdb/hash_set.clj +++ b/src/xitdb/hash_set.clj @@ -41,7 +41,7 @@ (operations/set-item-count rhs)) clojure.lang.Seqable - (seq [_] + (seq [this] (set-seq rhs)) clojure.lang.ILookup @@ -117,7 +117,7 @@ (operations/set-item-count whs)) clojure.lang.Seqable - (seq [_] + (seq [this] (set-seq whs)) clojure.lang.ILookup diff --git a/src/xitdb/util/operations.clj b/src/xitdb/util/operations.clj index 410bf78..848c858 100644 --- a/src/xitdb/util/operations.clj +++ b/src/xitdb/util/operations.clj @@ -197,31 +197,33 @@ ;; ============================================================================ (defn map-seq - "Return a lazy seq of key-value MapEntry pairs." + "Return a lazy seq of key-value MapEntry pairs, or nil if empty." [^ReadHashMap rhm read-from-cursor] (let [it (.iterator rhm)] - (letfn [(step [] - (lazy-seq - (when (.hasNext it) - (let [cursor (.next it) - kv (.readKeyValuePair cursor) - k (read-from-cursor (.-keyCursor kv))] - (let [v (read-from-cursor (.-valueCursor kv))] - (cons (clojure.lang.MapEntry. k v) (step)))))))] - (step)))) + (when (.hasNext it) + (letfn [(step [] + (lazy-seq + (when (.hasNext it) + (let [cursor (.next it) + kv (.readKeyValuePair cursor) + k (read-from-cursor (.-keyCursor kv))] + (let [v (read-from-cursor (.-valueCursor kv))] + (cons (clojure.lang.MapEntry. k v) (step)))))))] + (step))))) (defn set-seq - "Return a lazy seq values from the set." + "Return a lazy seq values from the set, or nil if empty." [rhm read-from-cursor] (let [it (.iterator rhm)] - (letfn [(step [] - (lazy-seq - (when (.hasNext it) - (let [cursor (.next it) - kv (.readKeyValuePair cursor) - v (read-from-cursor (.-keyCursor kv))] - (cons v (step))))))] - (step)))) + (when (.hasNext it) + (letfn [(step [] + (lazy-seq + (when (.hasNext it) + (let [cursor (.next it) + kv (.readKeyValuePair cursor) + v (read-from-cursor (.-keyCursor kv))] + (cons v (step))))))] + (step))))) (defn array-seq "Creates a lazy sequence from a ReadArrayList. diff --git a/test/xitdb/map_test.clj b/test/xitdb/map_test.clj index 976690f..c7276e5 100644 --- a/test/xitdb/map_test.clj +++ b/test/xitdb/map_test.clj @@ -1,6 +1,7 @@ (ns xitdb.map-test (:require [clojure.test :refer :all] + [xitdb.db :as xdb] [xitdb.test-utils :as tu :refer [with-db]])) (deftest map-with-complex-keys @@ -19,4 +20,16 @@ (swap! db update :foo dissoc [2 :baz]) (is (= {:foo {[1 :bar] 31}} - (tu/materialize @db)))))) \ No newline at end of file + (tu/materialize @db)))))) + +(deftest KeysTest + (with-open [db (xdb/xit-db :memory)] + (reset! db {}) + (is (= nil (keys @db))) + (is (= 0 (count (keys @db)))))) + +(deftest KeysTestSet + (with-open [db (xdb/xit-db :memory)] + (reset! db #{}) + (is (= 0 (count (keys @db)))) + (is (= nil (keys @db)))))