Die DVZ sucht studentische Hilfskräfte! Weitere Informationen…

Commit c2d23009 authored by Bruno Burke's avatar Bruno Burke 🍔
Browse files

add document views functionality

parent 1bc9f22c
Pipeline #34778 passed with stages
in 1 minute and 7 seconds
(defproject document-storage "0.1.6.3"
(defproject document-storage "0.1.7"
:description "FIXME: write description"
:url "http://example.com/FIXME"
:license {:name "Eclipse Public License"
......
......@@ -5,21 +5,23 @@
[clojure.edn :refer [read-string]]
[document-storage.query :refer [query-fits? update-queries update-queries-remove-document]]
[document-storage.versioning :as versioning]
[document-storage.repository :refer [repositories init-repository valid-id?
default-repository
no-commit-repository?
cached-repository?
get-file-path
get-repo
get-repo-data
get-repository-path]]
[document-storage.views :refer [get-views generate-views]]
[fipp.edn :refer [pprint] :rename {pprint fipp}]))
(defonce repo-dir (atom ""))
(defonce repositories (atom {}))
(defonce default-repository :default)
(defonce upstream-ssh-identity "upstream")
(defonce storage-directory (atom ""))
(defonce cache (atom {}))
(defn init-repository [dir]
(when-not (jp/discover-repo dir)
(log/info dir " is not a Git-Repository")
(log/info "Initialize Git-Repository in " dir)
(jp/git-init :dir dir))
(jp/load-repo (jp/discover-repo dir)))
(defn load-config [dir]
(-> dir
......@@ -40,40 +42,9 @@
(get (:repositories config) repo))))
(log/error "Unable to initialize Document Storage - no config!")))
(defn get-file-path [id repository]
(let [repo-directory (get-in @repositories [repository :directory])
file-path (str repo-directory "/" id)]
file-path))
(defn cached-repository? [repository]
(get-in @repositories [repository :caching]))
(defn no-commit-repository? [repository]
(get-in @repositories [repository :no-commit]))
(defn reset-cache! []
(reset! cache {}))
(defn get-repository-path [repository]
(let [repo-directory (get-in @repositories [repository :directory])]
repo-directory))
(defn valid-id? [id repository]
(let [file-path (get-file-path id repository)]
(.exists (io/as-file file-path))))
(defn get-repo-data [id]
(if-let [repo (get-in @repositories [id])]
repo
(log/error "invalid repository " id)
))
(defn- get-repo
([]
(get-repo default-repository))
([id]
(:git-repo (get-repo-data id))))
(defn- commit-document-change [message repository]
(jp/git-commit (get-repo repository) message
:committer {:name "Document-Manager" :email "burke@fh-muenster.de"}))
......@@ -101,6 +72,7 @@
(and cached-repository (get-in @cache [repository :documents id]))
(when (valid-id? id repository)
(let [result (read-string (slurp (get-file-path id repository)))]
(generate-views repository id result)
(if cached-repository
(cache-document repository id result)
result
......@@ -112,6 +84,7 @@
(when-not (and (valid-id? id repository)
(= (slurp file-path) document-str))
(update-cache-document repository id document)
(generate-views repository id document)
(with-open [w (io/writer file-path)]
(.write w document-str))
(when-not (no-commit-repository? repository)
......@@ -185,8 +158,6 @@
(push-upstreams id)
) @repositories))
(defn list-versions [id & {:keys [repository]
:or {repository default-repository}}]
(let [repo (get-repo repository)]
......@@ -199,3 +170,4 @@
(when-let [commit (-> (filter #(= (:version %) version) versions)
first :commit)]
(versioning/load-revision commit repo id))))
(ns document-storage.repository
(:require [clj-jgit.porcelain :as jp]
[clojure.tools.logging :as log]
[clojure.java.io :as io]))
(defonce repositories (atom {}))
(defonce default-repository :default)
(defn init-repository [dir]
(when-not (jp/discover-repo dir)
(log/info dir " is not a Git-Repository")
(log/info "Initialize Git-Repository in " dir)
(jp/git-init :dir dir))
(jp/load-repo (jp/discover-repo dir)))
(defn get-file-path [id repository]
(let [repo-directory (get-in @repositories [repository :directory])
file-path (str repo-directory "/" id)]
file-path))
(defn cached-repository? [repository]
(get-in @repositories [repository :caching]))
(defn no-commit-repository? [repository]
(get-in @repositories [repository :no-commit]))
(defn get-repo-data [id]
(if-let [repo (get-in @repositories [id])]
repo
(log/error "invalid repository " id)
))
(defn get-repo
([]
(get-repo default-repository))
([id]
(:git-repo (get-repo-data id))))
(defn get-repository-path [repository]
(let [repo-directory (get-in @repositories [repository :directory])]
repo-directory))
(defn valid-id? [id repository]
(let [file-path (get-file-path id repository)]
(.exists (io/as-file file-path))))
(ns document-storage.views
(:require [document-storage.repository :refer [repositories default-repository]]))
(defonce view-cache (atom {}))
(defn get-views [repository]
(get-in @repositories [repository :views] {}))
(defn add-view [repository id function]
(swap! repositories assoc-in [repository :views id] function))
(defn remove-view [repository id]
(swap! repositories update-in [repository :views] dissoc id))
(defn load-views [id & {:keys [repository] :or {repository default-repository}}]
(get-in @view-cache [repository id]))
(defn process-view [document view-fn]
(try
(view-fn document)
(catch Exception e
(str "Exception: " e))))
(defn generate-views [repository id document]
(swap! view-cache update repository dissoc id)
(doseq [[view-id view-fn] (get-views repository)]
(swap! view-cache assoc-in [repository id view-id]
(process-view document view-fn))))
(ns document-storage.core-test
(:require [clojure.test :refer :all]
[document-storage.core :as ds]
[document-storage.views :as dsv]
[clojure.java.io :as io]
[fipp.edn :refer [pprint] :rename {pprint fipp}]))
......@@ -38,3 +39,29 @@
(ds/save-document id structure :repository repo)
(is (= structure
(ds/load-document id :repository repo))))))
(deftest views-test
(testing "save views, save basic structure, load it and load views"
(let [structure {:a 5 :c [1 2 3 4 99 5 -4] :b 88}
id "simple-test1"
repo :testrepo
fn1 (fn [d]
{:sum1 (+ (:a d)
(:b d))
:sum2 (reduce + (:c d))})
fn2 (fn [d]
{:sum1 (- (:a d)
(:b d))
:c-copy (:c d)})]
(dsv/add-view repo :fn1 fn1)
(dsv/add-view repo :fn2 fn2)
(ds/save-document id structure :repository repo)
(is (= structure
(ds/load-document id :repository repo)))
(let [views (dsv/load-views id :repository repo)
v1 (:fn1 views)
v2 (:fn2 views)]
(is (= (:sum1 v1) 93))
(is (= (:sum2 v1) 110))
(is (= (:sum1 v2) -83))))))
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment