-
Notifications
You must be signed in to change notification settings - Fork 0
/
rewriting_history.clj
114 lines (96 loc) · 4.29 KB
/
rewriting_history.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
(ns no.nsd.rewriting-history
(:require [no.nsd.rewriting-history.impl :as impl]
[no.nsd.rewriting-history.replay-impl :as replay]
[no.nsd.rewriting-history.schedule-impl :as schedule]
[no.nsd.rewriting-history.patch :as patch]
[no.nsd.rewriting-history.wipe :as wipe]
[no.nsd.rewriting-history.rollback :as rollback]
[clojure.string :as str]
[clojure.tools.logging :as log]
[clojure.set :as set]
[datomic.api :as d])
(:import (java.util Date)))
; public API
(def schema impl/schema)
(declare pull-flat-history)
(declare pending-replacements)
(defn schedule-replacement!
"Schedule a replacement of ^String match with ^String replacement
for lookup-ref.
Creates or updates a rewrite-job."
[conn lookup-ref match replacement]
(assert (string? match))
(assert (string? replacement))
(assert (some? (impl/resolve-lookup-ref conn lookup-ref))
(str "Expected to find lookup-ref " lookup-ref))
(let [curr-history (pull-flat-history conn lookup-ref)
found-match-in-history? (->> curr-history
(map (fn [[e a v t o]] v))
(filter string?)
(into #{})
(reduce (fn [_ v]
(when (str/includes? v match)
(reduced true)))
false))]
(cond (and (empty? match)
(not-empty replacement))
(do (log/warn "empty match given and non-empty replacement given, ignoring")
(pending-replacements conn lookup-ref))
found-match-in-history? (schedule/schedule-replacement! conn lookup-ref match replacement)
:else (pending-replacements conn lookup-ref))))
(defn schedule-patch!
[conn lookup-ref org-history new-history]
(patch/schedule-patch! conn lookup-ref org-history new-history))
(defn all-pending-patches [conn]
(patch/all-pending-patches conn))
(defn cancel-replacement! [conn lookup-ref match replacement]
"Cancel a scheduled replacement for lookup-ref. Updates an existing rewrite-job."
(schedule/cancel-replacement! conn lookup-ref match replacement))
(defn pending-replacements [conn lookup-ref]
"Get pending replacements for lookup-ref."
(schedule/pending-replacements conn lookup-ref))
(defn all-pending-replacements [conn]
"Get all pending replacements in the form of
[{:attr attr
:ref ref
:match ...
:replacement ...}
...]"
(schedule/all-pending-replacements conn))
(defn rewrite-scheduled! [conn]
"Process all rewrite-jobs that are scheduled."
(replay/process-state! conn :scheduled))
(defn excise-old-rewrite-jobs! [conn older-than-days]
"Excise all rewrite-job metadata that are older than older-than-days days."
(wipe/wipe-old-rewrite-jobs! conn (Date.) older-than-days))
(defn available-rollback-times [conn lookup-ref]
"Get available rollback times for a lookup ref. Returns a set of java.util.Dates."
(rollback/available-times conn lookup-ref))
(defn rollback! [conn lookup-ref ^Date t]
"Rollback lookup-ref to a previous ^java.util.Date t."
(rollback/rollback! conn lookup-ref t))
(defn assoc-lookup-ref [eavtos [attr idval] & kvs]
(assert (vector? eavtos))
(let [new-vals (into {} (mapv vec (partition 2 kvs)))]
(if-let [eid (->> eavtos
(filter (fn [[e a v t o]]
(and (= a attr)
(= v idval))))
(ffirst))]
(->> eavtos
(mapv (fn [[e a v t o :as eavto]]
(if (= e eid)
[e a (get new-vals a v) t o]
eavto))))
eavtos)))
; convenience methods
(defn pull-flat-history
"Returns a vector of all the EAVTOs that are reachable from lookup-ref,
i.e. the full history of lookup-ref, with normalized values for E and T."
[db lookup-ref]
(impl/pull-flat-history-simple db lookup-ref))
(defn get-org-history [db lookup-ref]
"Get the original flat history of lookup-ref as it was before rewriting started."
(impl/get-org-history db lookup-ref))
(defn get-job-state [conn lookup-ref]
(impl/job-state conn lookup-ref))