Skip to content

Commit

Permalink
Fix append for ndnippy
Browse files Browse the repository at this point in the history
  • Loading branch information
luposlip committed Oct 3, 2023
1 parent 292befd commit d64052d
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/nd_db/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,13 @@
:id-name - Convenience parameter - if you just want to supply the name of the ID in the text
based data to search for - creates a regex under the hood. Should be used with
the next parameter for optimal speed.
:id-path - Use with nippy databases. Docs can be indexed directly by path vector
:id-type - The type of data to store as ID (key) in the index
:source-type - If the source-type is different from the ID type to store in the index
:index-folder - Folder to persist index in, defaults to system temp folder
:index-persist? - Set to false to inhibit storing the index on disk, defaults to true. Will also
inhibit the use of previously persisted indices!
:filename - .ndnippy input filename (full path)
:index-path - Use with .ndnippy file, docs can be index directly by path vector
:log-limit - Read the documents log until and including this index (for versioning)"
[& _params]
{:post [(ndut/db? %)]}
Expand Down
3 changes: 2 additions & 1 deletion src/nd_db/io.clj
Original file line number Diff line number Diff line change
Expand Up @@ -216,9 +216,10 @@
:filename filename
:index-persist? (not (false? index-persist?)))
(number? log-limit) (assoc :log-limit log-limit)
(or (keyword? id-path)
(vector? id-path)) (assoc :id-path id-path)
(= :csv doc-type)
(assoc :col-separator col-separator
:id-path id-path
:cols (with-open [r (io/reader filename)]
(ndcs/col-str->key-vec
(re-pattern col-separator)
Expand Down
24 changes: 20 additions & 4 deletions test/nd_db/core_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@
(set (nddb/q db (nddb/lazy-ids db))))
"Check that all docs, old and new, can be correctly read from db"))
(delete-meta db))
(io/delete-file "resources/test/tmp-test.csv")))
(io/delete-file tmp-filename)))

(deftest append-new-versions
(let [tmp-filename "resources/test/tmp-test.csv"
Expand All @@ -232,7 +232,7 @@
(is (= {:a 1 :b 7 :c "a"} (nddb/q db 1)) "Old db returns old doc")
(is (= doc (nddb/q new-db 1)) "New db returns new version")
(delete-meta db)
(io/delete-file "resources/test/tmp-test.csv")))
(io/delete-file tmp-filename)))

(deftest append-new-version-of-last-doc
(let [tmp-filename "resources/test/tmp-test.csv"
Expand All @@ -248,7 +248,7 @@
(is (= {:a "c" :b 5 :c 6} (nddb/q db "c")) "Old db returns old doc")
(is (= doc (nddb/q new-db "c")) "New db returns new version")
(delete-meta db)
(io/delete-file "resources/test/tmp-test.csv")))
(io/delete-file tmp-filename)))

(deftest append-new-versions
(let [tmp-filename "resources/test/tmp-test.csv"
Expand All @@ -266,7 +266,23 @@
(is (= {:a "c" :b 5 :c 6} (nddb/q db "c")) "Old db returns old doc")
(is (= newest-doc (nddb/q new-db "c")) "New db returns newest version")
(delete-meta db)
(io/delete-file "resources/test/tmp-test.csv")))
(io/delete-file tmp-filename)))

(deftest append-to-nippy
(let [tmp-filename "resources/test/tmp-test.ndnippy"
inf (io/file "resources/test/test.ndnippy")
outf (io/file tmp-filename)
_ (io/copy inf outf)
db (nddb/db :filename tmp-filename
:id-path :id)
_ (-> db :index deref)
doc {:id 1 :b "c" :d "e"}
new-db (sut/append db doc)]
(-> new-db :index deref)
(is (not= doc (nddb/q db 1)) "Old db returns old doc")
(is (= doc (nddb/q new-db 1)) "New db returns new version")
(delete-meta db)
(io/delete-file tmp-filename)))

(deftest query-historical-db
(let [tmp-filename "resources/test/tmp-test.csv"
Expand Down

0 comments on commit d64052d

Please sign in to comment.