-
Notifications
You must be signed in to change notification settings - Fork 29
/
store.cljc
285 lines (259 loc) · 13.4 KB
/
store.cljc
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
(ns ^{:doc "The implements the Block storage version of a Graph/Database/Connection"
:author "Paula Gearon"}
asami.durable.store
(:require [asami.storage :as storage :refer [ConnectionType DatabaseType UpdateData]]
[asami.graph :as graph]
[asami.internal :as i :refer [now instant? long-time]]
[asami.durable.common :as common
:refer [append-tx! commit! get-tx latest tx-count find-tx close delete!]]
[asami.durable.common-utils :as common-utils]
[asami.durable.macros :as m :include-macros true]
[asami.durable.pool :as pool]
[asami.durable.tuples :as tuples]
[asami.durable.graph :as dgraph]
[zuko.schema :refer [Triple]]
[asami.entities.general :refer [GraphType]]
[asami.entities.reader :as reader]
[schema.core :as s :include-macros true]
#?(:clj [asami.durable.flat-file :as flat-file])
#?(:clj [clojure.java.io :as io]))
#?(:clj (:import [java.util.concurrent.locks Lock ReentrantLock]
[java.io File]
[java.nio.channels FileLock])))
#?(:clj (set! *warn-on-reflection* true))
(def tx-name "tx.dat")
;; transactions contain tree roots for the 3 tree indices,
;; the tree root for the data pool,
;; the 3 block counts for the tuples index tree, the tuples blocks, and the data pool index tree
;; the internal node counter
(def tx-record-size (* 8 common/long-size))
(def TxRecord {(s/required-key :r-spot) (s/maybe s/Int)
(s/required-key :r-post) (s/maybe s/Int)
(s/required-key :r-ospt) (s/maybe s/Int)
(s/required-key :r-pool) (s/maybe s/Int)
(s/required-key :nr-index-node) (s/maybe s/Int)
(s/required-key :nr-index-block) (s/maybe s/Int)
(s/required-key :nr-pool-node) (s/maybe s/Int)
(s/required-key :nodes) s/Int
(s/required-key :timestamp) s/Int})
(def TxRecordPacked {(s/required-key :timestamp) s/Int
(s/required-key :tx-data) [(s/one s/Int "spot root id")
(s/one s/Int "post root id")
(s/one s/Int "ospt root id")
(s/one s/Int "pool root id")
(s/one s/Int "number of index nodes allocated")
(s/one s/Int "number of index blocks allocated")
(s/one s/Int "number of pool index nodes allocated")
(s/one s/Int "node id counter")]})
(s/defn pack-tx :- TxRecordPacked
"Packs a transaction into a vector for serialization"
[{:keys [r-spot r-post r-ospt r-pool nr-index-node nr-index-block nr-pool-node nodes timestamp]} :- TxRecord]
{:timestamp timestamp :tx-data [(or r-spot 0) (or r-post 0) (or r-ospt 0) (or r-pool 0)
(or nr-index-node 0) (or nr-index-block 0) (or nr-pool-node 0)
nodes]})
(s/defn unpack-tx :- TxRecord
"Unpacks a transaction vector into a structure when deserializing"
[{[r-spot r-post r-ospt r-pool
nr-index-node nr-index-block nr-pool-node nodes] :tx-data
timestamp :timestamp} :- TxRecordPacked]
(letfn [(non-zero [v] (and v (when-not (zero? v) v)))]
{:r-spot (non-zero r-spot)
:r-post (non-zero r-post)
:r-ospt (non-zero r-ospt)
:r-pool (non-zero r-pool)
:nr-index-node (non-zero nr-index-node)
:nr-index-block (non-zero nr-index-block)
:nr-pool-node (non-zero nr-pool-node)
:nodes nodes
:timestamp timestamp}))
(s/defn new-db :- TxRecordPacked
[]
{:timestamp (long-time (now)) :tx-data [0 0 0 0 0 0 0 0]})
(declare ->DurableDatabase)
(s/defn as-of* :- DatabaseType
"Returns a database value for a provided t-value.
If t-val is the transaction number for an older database, then returns that database. Otherwise, will return this database
If t-val is a timestamp then returns the most recent database that was valid at that time."
[{{:keys [tx-manager] :as connection} :connection
bgraph :bgraph
timestamp :timestamp
t :t :as database} :- DatabaseType
t-val]
(if-let [new-t (cond
(instant? t-val) (let [requested-time (long-time t-val)]
(and (< requested-time timestamp)
(find-tx tx-manager requested-time)))
(int? t-val) (and (< t-val t) t-val)
:default (throw (ex-info (str "Unable to retrieve database for datatype " (type t-val))
{:value t-val :type (type t-val)})))]
(let [bounded-t (min (max 0 new-t) (dec (tx-count tx-manager)))
{new-ts :timestamp :as tx} (unpack-tx (get-tx tx-manager bounded-t))]
(->DurableDatabase connection (dgraph/graph-at bgraph tx) bounded-t new-ts))
database))
(s/defn since* :- (s/maybe DatabaseType)
"Returns the next database value after the provided t-value.
If t-val is a transaction number for an older databse, then it returns the next database. If it refers to the current
database or later, then returns nil (even if more recent databases exist, since this database is stateless.
If t-val is a timestamp, then it returns the next database after that time, unless the timestamp is at or after
the timestamp on the current database."
[{{tx-manager :tx-manager :as connection} :connection
timestamp :timestamp
bgraph :bgraph
t :t :as database} :- DatabaseType
t-val]
(letfn [(set-database [tx txid ts]
(->DurableDatabase connection (dgraph/graph-at bgraph tx) txid ts))
(db-for [txid]
(let [{ts :timestamp :as tx} (unpack-tx (get-tx tx-manager txid))]
(set-database tx txid ts)))]
;; check that the database isn't empty
(when (> (tx-count tx-manager) 0)
(cond
;; look for a since point by timestamp
(instant? t-val) (let [requested-time (long-time t-val)]
(when (< requested-time timestamp) ;; if at or after the final timestamp, then nil
(let [{fts :timestamp :as first-tx} (unpack-tx (get-tx tx-manager 0))]
(if (< requested-time fts) ;; before the first timestamp, so the first commit point
(set-database first-tx 0 fts)
(let [txid (inc (find-tx tx-manager requested-time))]
(db-for txid))))))
;; look for a since point by tx ID.
;; If it's at or after the time of the latest database, then return nil
(int? t-val) (when (< t-val t)
(let [txid (max 0 (inc t-val))]
(db-for txid)))
:default (throw (ex-info (str "Unable to retrieve database for datatype " (type t-val))
{:value t-val :type (type t-val)}))))))
(s/defn entity* :- (s/maybe {s/Keyword s/Any})
[{bgraph :bgraph :as database}
id
nested? :- s/Bool]
(reader/ident->entity bgraph id nested?))
(defrecord DurableDatabase [connection bgraph t timestamp]
storage/Database
(as-of [this t-val] (as-of* this t-val))
(as-of-t [this] t)
(as-of-time [this] timestamp)
(since [this t-val] (since* this t-val))
(since-t [this] t)
(graph [this] bgraph)
(entity [this id nested?] (entity* this id nested?)))
(s/defn db* :- DatabaseType
"Returns the most recent database value from the connection."
[{:keys [name tx-manager grapha] :as connection} :- ConnectionType]
(let [tx (latest tx-manager)
{:keys [r-spot r-post r-ospt timestamp]} (and tx (unpack-tx tx))
{:keys [spot post ospt] :as g} @grapha
tx-id (dec (common/tx-count tx-manager))]
(assert (= r-spot (:root-id spot)))
(assert (= r-post (:root-id post)))
(assert (= r-ospt (:root-id ospt)))
(->DurableDatabase connection g tx-id timestamp)))
(s/defn delete-database*
"Delete the graph, which will recursively delete all resources"
[{:keys [name grapha tx-manager] :as connection} :- ConnectionType]
(close @grapha)
(delete! @grapha)
(reset! grapha nil)
(close tx-manager)
(delete! tx-manager)
#?(:clj (when-let [d (common-utils/get-directory name)]
(.delete ^File d))
:cljs true))
(s/defn release*
"Closes the transaction manager, and the graph, which will recursively close all resources"
[{:keys [name grapha tx-manager] :as connection} :- ConnectionType]
(close @grapha)
(reset! grapha nil)
(close tx-manager))
(def DBsBeforeAfter [(s/one DatabaseType "db-before")
(s/one DatabaseType "db-after")])
;; Update functions return a Graph, and accept a Graph and an integer
(def UpdateFunction (s/=> GraphType GraphType s/Int))
(s/defn transact-update* :- DBsBeforeAfter
"Updates a graph according to a provided function. This will be done in a new, single transaction."
[{:keys [tx-manager grapha nodea] :as connection} :- ConnectionType
update-fn :- UpdateFunction]
;; multithreaded environments require exclusive writing for the graph
;; this also ensures no writing between the read/write operations of the update-fn
;; Locking is required, as opposed to using atoms, since I/O operations cannot be repeated.
(let [file-lock (volatile! nil)]
(m/with-lock connection
(m/with-open* #?(:clj [^FileLock file-lock (common/acquire-lock! tx-manager)]
:cljs [file-lock (common/acquire-lock! tx-manager)])
;; keep a reference of what the data looks like now
(let [{:keys [bgraph t timestamp] :as db-before} (db* connection)
;; figure out the next transaction number to use
tx-id (common/tx-count tx-manager)
;; do the modification on the graph
next-graph (update-fn @grapha tx-id)
;; step each underlying index to its new transaction point
graph-after (commit! next-graph)
;; get the metadata (tree roots) for all the transactions
new-timestamp (long-time (now))
tx (assoc (common/get-tx-data graph-after)
:nodes @nodea
:timestamp new-timestamp)]
;; save the transaction metadata
(common/append-tx! tx-manager (pack-tx tx))
;; update the connection to refer to the latest graph
(reset! grapha graph-after)
;; return the required database values
[db-before (->DurableDatabase connection graph-after tx-id new-timestamp)])))))
(s/defn transact-data* :- DBsBeforeAfter
"Removes a series of tuples from the latest graph, and asserts new tuples into the graph.
Updates the connection to the new graph."
([conn :- ConnectionType
updates! :- UpdateData
asserts :- [Triple] ;; triples to insert
retracts :- [Triple]] ;; triples to remove
(transact-update* conn (fn [graph tx-id] (graph/graph-transact graph tx-id asserts retracts updates!))))
([conn :- ConnectionType
updates! :- UpdateData
generator-fn]
(transact-update* conn
(fn [graph tx-id]
(let [[asserts retracts] (generator-fn graph)]
(graph/graph-transact graph tx-id asserts retracts updates!))))))
(s/defn get-url* :- s/Str
[{:keys [name]} :- ConnectionType]
(str "asami:local://" name))
(defrecord DurableConnection [name tx-manager grapha nodea lock]
storage/Connection
(get-name [this] name)
(get-url [this] (get-url* this))
(next-tx [this] (common/tx-count tx-manager))
(db [this] (db* this))
(delete-database [this] (delete-database* this))
(release [this] (release* this))
(transact-update [this update-fn] (transact-update* this update-fn))
(transact-data [this updates! asserts retracts] (transact-data* this updates! asserts retracts))
(transact-data [this updates! generator-fn] (transact-data* this updates! generator-fn))
common/Lockable
(lock! [this] #?(:clj (.lock ^Lock lock)))
(unlock! [this] #?(:clj (.unlock ^Lock lock))))
(s/defn db-exists? :- s/Bool
"Tests if this database exists by looking for the transaction file"
[store-name :- s/Str]
#?(:clj (flat-file/store-exists? store-name tx-name) :cljs nil))
(defn- create-lock
"Creates a lock object for the connection. This is a noop in ClojureScript"
[]
#?(:clj (ReentrantLock.)))
(s/defn create-database :- ConnectionType
"This opens a connection to an existing database by the name of the location for resources.
If the database does not exist then it is created."
[name :- s/Str]
(let [ex (db-exists? name)
tx-manager #?(:clj (flat-file/tx-store name tx-name tx-record-size) :cljs nil)
;; initialize new databases with a transaction that indicates an empty store
_ (when-not ex (common/append-tx! tx-manager (new-db)))
tx (latest tx-manager)
unpacked-tx (and tx (unpack-tx tx))
node-ct (get unpacked-tx :nodes 0)
node-counter (atom node-ct)
node-allocator (fn [] (graph/new-node (swap! node-counter inc)))
;; the following function is called under locked conditions
id-checker (fn [id] (when (> id @node-counter) (reset! node-counter id)))
block-graph (dgraph/new-merged-block-graph name unpacked-tx node-allocator id-checker)]
(->DurableConnection name tx-manager (atom block-graph) node-counter (create-lock))))