diff --git a/src/xitdb/array_list.clj b/src/xitdb/array_list.clj index 2a7e67d..82b18d6 100644 --- a/src/xitdb/array_list.clj +++ b/src/xitdb/array_list.clj @@ -40,8 +40,11 @@ clojure.lang.Indexed (nth [_ i] - (let [cursor (.getCursor ral (long i))] - (common/-read-from-cursor cursor))) + (let [count (.count ral) + idx (long i)] + (if (and (>= idx 0) (< idx count)) + (common/-read-from-cursor (.getCursor ral idx)) + (throw (IndexOutOfBoundsException. (str "Index: " i ", Size: " count)))))) (nth [_ i not-found] (let [cursor (.getCursor ral (long i))] diff --git a/src/xitdb/util/operations.clj b/src/xitdb/util/operations.clj index 01950c2..410bf78 100644 --- a/src/xitdb/util/operations.clj +++ b/src/xitdb/util/operations.clj @@ -185,7 +185,7 @@ cursor (.getCursor rhs hash-code)] (some? cursor))) -(defn ^WriteHashMap set-empty! +(defn ^WriteHashSet set-empty! "Replaces the whs value with an empty set." [^WriteHashSet whs] (let [empty-set (conversion/v->slot! (.cursor whs) #{})] diff --git a/test/xitdb/cursor_test.clj b/test/xitdb/cursor_test.clj index adcae77..acbe375 100644 --- a/test/xitdb/cursor_test.clj +++ b/test/xitdb/cursor_test.clj @@ -24,4 +24,7 @@ (swap! cursor1 assoc-in [3 :hidden] :changed-by-swap!) (is (= [1 2 3 {:hidden :changed-by-swap!} 5]) (xdb/materialize @cursor1)) (is (= :changed-by-swap! @cursor3)) - (is (= 3 @cursor2)))))) + (is (= 3 @cursor2))) + + (testing "Correctly handles invalid cursor path" + (is (thrown? IndexOutOfBoundsException @(xdb/xdb-cursor db [:foo :bar 999])))))))