Skip to content

Commit a86d3ee

Browse files
committed
Improving what-if actions
1 parent 298527d commit a86d3ee

2 files changed

Lines changed: 15 additions & 14 deletions

File tree

functionalDB/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,5 @@ The core idea is to add the notion of time to the data, and provide on top of th
99
The database itself is a collection of datums. A datum is built of an entity (think of a row in a table)that has its attributes (a column) and the value of that entity's attribute at a given time.
1010
Any update does not overwrite the prvious value, but addes another datum to the database.
1111

12+
The database supports both DB "modifying" (modifying as in creating a new DB value) transactions and what-if actions.
13+

functionalDB/src/core/fdb.clj

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
(ns core.fdb)
22

3-
(defrecord Entity [e_id name attrs])
3+
(defrecord Entity [id name attrs])
44
(defrecord Attr [name type value ts prev-ts])
55

66
(defn make-db[] ; EAVT: all the entity info, AEVT for attrs who are REFs, we hold the back-pointing (from the REFFed entity to the REFing entities)
7-
(atom {:timestamped [{:EAVT {} :AEVT {}}]
8-
:topId 0 :curr-time 0}))
7+
(atom {:timestamped [{:EAVT {} :AEVT {}}] :topId 0 :curr-time 0}))
98

109
(defn make-entity ([name] (make-entity :no-id-yet name))
1110
([id name] (Entity. id name {})))
12-
(defn val-from-ref[attr-type attr-val](if (= :REF attr-type) (:e_id attr-val) attr-val ))
1311

14-
(defn make-attr[name val type] (Attr. name type (val-from-ref type val) -1 -1))
12+
(defn val-from-ref[attr-type attr-val](if (= :REF attr-type) (:id attr-val) attr-val ))
13+
14+
(defn make-attr[name value type] (Attr. name type (val-from-ref type value) -1 -1))
1515
(defn add-attr[ ent attr] (assoc-in ent [:attrs (keyword (:name attr))] attr))
1616

1717
(defn next-ts [db] (inc (:curr-time db)))
1818

1919
(defn nextId[db ent] (let [ topId (:topId db)
20-
entId (:e_id ent)
20+
entId (:id ent)
2121
[idToUse nextTop] (if (= entId :no-id-yet) [(inc topId) (inc topId)] [entId topId])]
2222
[idToUse (keyword (str idToUse)) nextTop]))
2323

@@ -35,7 +35,7 @@
3535
(let [reffed-id (:value attr)
3636
attr-name (:name attr)
3737
back-reffing-set (get-in aevt [reffed-id attr-name] #{} )
38-
new-back-reffing-set (operation back-reffing-set (:e_id ent))
38+
new-back-reffing-set (operation back-reffing-set (:id ent))
3939
] (assoc-in aevt [reffed-id attr-name] new-back-reffing-set)))
4040

4141
(defn update-aevt[old-aevt ent operation]
@@ -47,16 +47,15 @@
4747
(defn add-entity[db ent] (let [[ent-id ent-id-key next-top] (nextId db ent)
4848
new-ts (next-ts db)
4949
indices (last (:timestamped db))
50-
fixed-ent (assoc ent :e_id ent-id-key)
50+
fixed-ent (assoc ent :id ent-id-key)
5151
new-eavt (assoc (:EAVT indices) ent-id-key (update-creation-ts fixed-ent new-ts) )
5252
new-aevt (update-aevt (:AEVT indices) fixed-ent conj)
5353
new-indices (assoc indices :AEVT new-aevt :EAVT new-eavt )
54-
](assoc db
55-
:timestamped (conj (:timestamped db) new-indices)
56-
:topId next-top)))
54+
](assoc db :timestamped (conj (:timestamped db) new-indices)
55+
:topId next-top)))
5756

5857
(defn remove-entity[db ent]
59-
(let [ent-id (:e_id ent)
58+
(let [ent-id (:id ent)
6059
indices (last (:timestamped db))
6160
aevt (update-aevt (:AEVT indices) ent disj)
6261
new-eavt (dissoc (:EAVT indices) ent-id) ; removing the entity
@@ -73,8 +72,8 @@
7372
new-indices (last (:timestamped transacted))
7473
res (assoc initial-db :timestamped (conj initial-indices new-indices)
7574
:curr-time (next-ts initial-db)
76-
:topId (:topId transacted)) ]
77-
res))))
75+
:topId (:topId transacted))]
76+
res))))
7877

7978
(defmacro transact_ [db op & txs]
8079
(when txs

0 commit comments

Comments
 (0)