From 26399de7e959c3d7eda0639ec291d8181bacfd43 Mon Sep 17 00:00:00 2001 From: Florin Braghis Date: Wed, 13 Aug 2025 17:00:52 +0200 Subject: [PATCH 1/3] Fix return value for keys Add keys test --- src/xitdb/hash_map.clj | 9 ++++----- src/xitdb/hash_set.clj | 10 ++++++---- test/xitdb/map_test.clj | 15 ++++++++++++++- 3 files changed, 24 insertions(+), 10 deletions(-) diff --git a/src/xitdb/hash_map.clj b/src/xitdb/hash_map.clj index df01503..b253356 100644 --- a/src/xitdb/hash_map.clj +++ b/src/xitdb/hash_map.clj @@ -57,7 +57,8 @@ clojure.lang.Seqable (seq [this] - (map-seq rhm)) + (when (pos? (operations/map-item-count rhm)) + (map-seq rhm))) clojure.lang.IFn (invoke [this k] @@ -162,7 +163,8 @@ clojure.lang.Seqable (seq [this] - (map-seq whm)) + (when (pos? (operations/map-item-count whm)) + (map-seq whm))) clojure.core.protocols/IKVReduce (kv-reduce [this f init] @@ -192,6 +194,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..a5c1ccb 100644 --- a/src/xitdb/hash_set.clj +++ b/src/xitdb/hash_set.clj @@ -41,8 +41,9 @@ (operations/set-item-count rhs)) clojure.lang.Seqable - (seq [_] - (set-seq rhs)) + (seq [this] + (when (pos? (operations/set-item-count rhs)) + (set-seq rhs))) clojure.lang.ILookup (valAt [this k] @@ -117,8 +118,9 @@ (operations/set-item-count whs)) clojure.lang.Seqable - (seq [_] - (set-seq whs)) + (seq [this] + (when (pos? (operations/set-item-count whs)) + (set-seq whs))) clojure.lang.ILookup (valAt [this k] diff --git a/test/xitdb/map_test.clj b/test/xitdb/map_test.clj index 976690f..e2c8f3e 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 (xitdb.db/xit-db :memory)] + (reset! db {}) + (is (= nil (keys @db))) + (is (= 0 (count (keys @db)))))) + +(deftest KeysTestSet + (with-open [db (xitdb.db/xit-db :memory)] + (reset! db #{}) + (is (= 0 (count (keys @db)))) + (is (= nil (keys @db))))) From 1ae2d62cf17a9bb5eedf227685f18a20a5acae76 Mon Sep 17 00:00:00 2001 From: Florin Braghis Date: Wed, 13 Aug 2025 17:04:00 +0200 Subject: [PATCH 2/3] Fix the map-seq and hash-set to return nil --- src/xitdb/hash_map.clj | 6 ++---- src/xitdb/hash_set.clj | 6 ++---- src/xitdb/util/operations.clj | 40 ++++++++++++++++++----------------- 3 files changed, 25 insertions(+), 27 deletions(-) diff --git a/src/xitdb/hash_map.clj b/src/xitdb/hash_map.clj index b253356..badeaea 100644 --- a/src/xitdb/hash_map.clj +++ b/src/xitdb/hash_map.clj @@ -57,8 +57,7 @@ clojure.lang.Seqable (seq [this] - (when (pos? (operations/map-item-count rhm)) - (map-seq rhm))) + (map-seq rhm)) clojure.lang.IFn (invoke [this k] @@ -163,8 +162,7 @@ clojure.lang.Seqable (seq [this] - (when (pos? (operations/map-item-count whm)) - (map-seq whm))) + (map-seq whm)) clojure.core.protocols/IKVReduce (kv-reduce [this f init] diff --git a/src/xitdb/hash_set.clj b/src/xitdb/hash_set.clj index a5c1ccb..ff1110d 100644 --- a/src/xitdb/hash_set.clj +++ b/src/xitdb/hash_set.clj @@ -42,8 +42,7 @@ clojure.lang.Seqable (seq [this] - (when (pos? (operations/set-item-count rhs)) - (set-seq rhs))) + (set-seq rhs)) clojure.lang.ILookup (valAt [this k] @@ -119,8 +118,7 @@ clojure.lang.Seqable (seq [this] - (when (pos? (operations/set-item-count whs)) - (set-seq whs))) + (set-seq whs)) clojure.lang.ILookup (valAt [this k] 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. From 300e02e0e00668b0fad3ce9eb64bee0c3f8d4eba Mon Sep 17 00:00:00 2001 From: Florin Braghis Date: Wed, 13 Aug 2025 17:14:47 +0200 Subject: [PATCH 3/3] Cosmetic fixes --- src/xitdb/hash_map.clj | 4 ++-- test/xitdb/map_test.clj | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/xitdb/hash_map.clj b/src/xitdb/hash_map.clj index badeaea..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 diff --git a/test/xitdb/map_test.clj b/test/xitdb/map_test.clj index e2c8f3e..c7276e5 100644 --- a/test/xitdb/map_test.clj +++ b/test/xitdb/map_test.clj @@ -23,13 +23,13 @@ (tu/materialize @db)))))) (deftest KeysTest - (with-open [db (xitdb.db/xit-db :memory)] + (with-open [db (xdb/xit-db :memory)] (reset! db {}) (is (= nil (keys @db))) (is (= 0 (count (keys @db)))))) (deftest KeysTestSet - (with-open [db (xitdb.db/xit-db :memory)] + (with-open [db (xdb/xit-db :memory)] (reset! db #{}) (is (= 0 (count (keys @db)))) (is (= nil (keys @db)))))