diff --git a/CHANGELOG.md b/CHANGELOG.md index 032ebdd..4c88f80 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,14 @@ All notable changes to this project will be documented in this file. This change - Timestamps for historical versions - Optimize (speed+size of) low level index format +## [0.9.0-beta20] - 2025-01-06 + +"Happy New Year"-release! + +### Enhanced + +- Throwing a sane error, when opening an EMPTY database! + ## [0.9.0-beta19] - 2024-10-25 Support `nippy` based files in a `zip` based database - called `zippy` diff --git a/README.md b/README.md index fcba068..3b930ae 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ # nd-db ```clojure -[com.luposlip/nd-db "0.9.0-beta19"] +[com.luposlip/nd-db "0.9.0-beta20"] ``` _Newline Delimited (read-only) Databases!_ diff --git a/project.clj b/project.clj index 4ff32a4..9588064 100644 --- a/project.clj +++ b/project.clj @@ -1,4 +1,4 @@ -(defproject com.luposlip/nd-db "0.9.0-beta19" +(defproject com.luposlip/nd-db "0.9.0-beta20" :description "Clojure library to use newline delimited files as fast read-only databases." :url "https://github.com/luposlip/nd-db" :license {:name "Apache License, Version 2.0" diff --git a/resources/test/empty.ndnippy b/resources/test/empty.ndnippy new file mode 100644 index 0000000..e69de29 diff --git a/src/nd_db/index.clj b/src/nd_db/index.clj index e91bb60..98fea77 100644 --- a/src/nd_db/index.clj +++ b/src/nd_db/index.clj @@ -52,30 +52,34 @@ ;; without parallelization: 18s ;; with: 6s (partition size 2048, fold size 32 ;; that's around 2/3 less processing time - (let [[fline & rlines] (line-seq rdr) - [lines init-offset] (if skip-first? - [rlines (inc (count (.getBytes ^String fline)))] - [(cons fline rlines) 0])] - (with-meta - (->> lines - (partition-all 2048) - (reduce - (fn [[offset _ :as acc] part] - (let [res (->> part - (into []) - (r/fold 32 - idx-combinr - (idx-reducr id-fn))) - [s l] (->> res peek rest)] - (-> acc - (update 0 #(+ 1 % s l)) - (update 1 merge (reduce - (fn [a [id s l]] - (assoc a id [(+ offset s) l])) - {} res))))) - [init-offset {}]) - second) - {:as-of (Instant/now)})))) + (let [[fline & rlines :as all-lines] (line-seq rdr) ] + (if all-lines + (let [[lines init-offset] (if skip-first? + [rlines (inc (count (.getBytes ^String fline)))] + [(cons fline rlines) 0])] + (with-meta + (->> lines + (partition-all 2048) + (reduce + (fn [[offset _ :as acc] part] + (let [res (->> part + (into []) + (r/fold 32 + idx-combinr + (idx-reducr id-fn))) + [s l] (->> res peek rest)] + (-> acc + (update 0 #(+ 1 % s l)) + (update 1 merge (reduce + (fn [a [id s l]] + (assoc a id [(+ offset s) l])) + {} res))))) + [init-offset {}]) + second) + {:as-of (Instant/now)})) + (throw (ex-info "Cannot use empty database!" {:filename filename + :id-fn? (ifn? id-fn) + :skip-first? skip-first?})))))) (defn reader "Returns a BufferedReader of the database index. diff --git a/test/nd_db/index_test.clj b/test/nd_db/index_test.clj index 234ada2..b82335f 100644 --- a/test/nd_db/index_test.clj +++ b/test/nd_db/index_test.clj @@ -4,7 +4,8 @@ [nd-db [core :as nddb] [index :as sut]] - [nd-db.util :as ndut])) + [nd-db.util :as ndut]) + (:import clojure.lang.ExceptionInfo)) (def by-id #(Integer. ^String (second (re-find #"^\{\"id\":(\d+)" %)))) @@ -50,3 +51,9 @@ new-index-keys (-> new-db :index deref keys set)] (is (ndut/db? new-db)) (is (= new-id (new-index-keys new-id))))) + +(deftest empty-db + (is (thrown? + ExceptionInfo + (nddb/db :filename "resources/test/empty.ndnippy" + :id-path :id))))