Skip to content
Snippets Groups Projects
Commit fe7eeb85 authored by Bruno Burke's avatar Bruno Burke :hamburger:
Browse files

simple in-memory docstorage

parent 1e0124d9
No related branches found
No related tags found
No related merge requests found
Pipeline #96270 passed
(ns document-storage.inmemory.core
(:require [document-storage.protocol :as protocol]
[document-storage.query :refer [query-fits? update-queries
update-queries-remove-document]]
[clj-helper.vector :as vhelper]
[clj-helper.string :as strhelper]))
(defprotocol db-storage
"Base Protocol for document storages"
(get-cache [this] "Return the internal cache atom")
(get-document-cache [this] "Return the internal document cache atom")
)
(defrecord inmemory-storage [storage-id params]
db-storage
(get-cache [this]
(get (.-params this) :cache))
(get-document-cache [this]
(get (.-params this) :documents))
protocol/storage
(init-storage [this]
(reset! (get (.-params this) :documents) {}))
(list-repositories [this]
(.-repositories (.-params this)))
(load-document [this id repository]
(when-let [version (get-in @(.get-document-cache this) [repository id :version-counter])]
(get-in @(.get-document-cache this) [repository id :versions version :data])))
(get-document-size [this id repository]
(count (.load-document this id repository)))
(save-document [this id document repository options]
(let [doc-cache (.get-document-cache this)
new-version (inc (get-in @doc-cache [repository id :version-counter] -1))
version {:data document
:version new-version
:uuid (strhelper/get-unique-id "dv")}]
(swap! doc-cache update-in [repository id :versions] vhelper/vconj version)
(swap! doc-cache assoc-in [repository id :version-counter] new-version)))
(save-document [this id document repository]
(.save-document this id document repository {}))
(delete-document [this id repository]
(swap! (.get-document-cache this) update-in [repository] dissoc id))
(list-documents [this repository]
(keys (get @(.get-document-cache this) repository)))
(versioned-repository? [this repository]
(:versioning (get (.-repositories (.-params this)) repository) false))
(find-documents [this query repository]
(let [docs (.list-documents this repository)
result (into {} (keep (fn [fname]
(let [doc (.load-document this fname repository)]
(when (query-fits? doc query)
[fname doc])))
docs))]
result))
(shutdown [this]
(reset! (.get-cache this) {})
(reset! (.get-document-cache this) {}))
(list-versions [this id repository]
(let [doc-cache (.get-document-cache this)]
(sort-by :version (get-in @doc-cache [repository id :versions]))))
(load-version [this id version repository]
(let [doc-cache (.get-document-cache this)]
(:data (vhelper/get-by (get-in @doc-cache [repository id :versions]) :version version)))))
(defn make-inmemory-ds [config]
(let [id (str "ds" (name (or (:id config) "main")))
storage (->inmemory-storage id
{:repositories (:repositories config)
:cache (atom {})
:documents (atom {})})]
(.init-storage storage)
storage))
(ns document-storage.inmemory.core-test
(:require [clojure.test :refer :all]
[document-storage.core :as ds]
[document-storage.inmemory.core :as dsmemory]
[clojure.java.io :as io]
[fipp.edn :refer [pprint] :rename {pprint fipp}]
[document-storage.protocol :as dsprot]
[environ.core :refer [env]]
[document-storage.protocol-test :as protocol-tests]))
(def ds-atom (atom nil))
(defn storage-fixture [f]
(reset! ds-atom
(dsmemory/make-inmemory-ds
{:id :testing
:repositories {:testrepo {:caching true}}}))
(f)
(.shutdown @ds-atom))
(use-fixtures :once storage-fixture)
(deftest simple-test
(protocol-tests/test-save-document ds-atom))
(deftest versioning-test
(protocol-tests/test-versioning ds-atom))
(deftest find-document-test
(protocol-tests/test-find-documents ds-atom))
......@@ -26,7 +26,7 @@
(deftest versioning-test
(protocol-tests/test-versioning ds-atom))
(protocol-tests/test-versioning ds-atom))
(deftest find-document-test
(protocol-tests/test-find-documents ds-atom))
......@@ -35,28 +35,28 @@
(testing "create repository and check if update repository changes repository options"
(let [reponame (str (gensym "r"))]
(.create-repository @ds-atom {:name reponame
:caching false
:versioning false})
:caching false
:versioning false})
(is (= (.cached-repository? @ds-atom reponame) false) "Initial repo with caching false should return false for cached-repository? method")
(is (= (.versioned-repository? @ds-atom reponame) false) "Initial repo with versioning false should return false for versioned-repository? method")
(.update-repository @ds-atom {:name reponame
:caching true
:versioning false})
:caching true
:versioning false})
(is (= (.cached-repository? @ds-atom reponame) true) "Updated repo with caching true should return true for cached-repository? method")
(is (= (.versioned-repository? @ds-atom reponame) false) "Updated repo with versioning false should return false for versioned-repository? method")
(.update-repository @ds-atom {:name reponame
:caching true
:versioning true})
:caching true
:versioning true})
(is (= (.cached-repository? @ds-atom reponame) true) "Updated repo with caching true should return true for cached-repository? method")
(is (= (.versioned-repository? @ds-atom reponame) true) "Updated repo with versioning true should return true for versioned-repository? method")
(.update-repository @ds-atom {:name reponame
:caching false
:versioning true})
:caching false
:versioning true})
(is (= (.cached-repository? @ds-atom reponame) false) "Updated repo with caching false should return false for cached-repository? method")
(is (= (.versioned-repository? @ds-atom reponame) true) "Updated repo with versioning true should return true for versioned-repository? method")
(.update-repository @ds-atom {:name reponame
:caching false
:versioning false})
:caching false
:versioning false})
(is (= (.cached-repository? @ds-atom reponame) false) "Updated repo with caching false should return false for cached-repository? method")
(is (= (.versioned-repository? @ds-atom reponame) false) "Updated repo with versioning false should return false for versioned-repository? method")
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment