|
1 | 1 | (ns core.fdb) |
2 | 2 |
|
3 | | -(defrecord Entity [e_id name attrs]) |
| 3 | +(defrecord Entity [id name attrs]) |
4 | 4 | (defrecord Attr [name type value ts prev-ts]) |
5 | 5 |
|
6 | 6 | (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})) |
9 | 8 |
|
10 | 9 | (defn make-entity ([name] (make-entity :no-id-yet name)) |
11 | 10 | ([id name] (Entity. id name {}))) |
12 | | -(defn val-from-ref[attr-type attr-val](if (= :REF attr-type) (:e_id attr-val) attr-val )) |
13 | 11 |
|
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)) |
15 | 15 | (defn add-attr[ ent attr] (assoc-in ent [:attrs (keyword (:name attr))] attr)) |
16 | 16 |
|
17 | 17 | (defn next-ts [db] (inc (:curr-time db))) |
18 | 18 |
|
19 | 19 | (defn nextId[db ent] (let [ topId (:topId db) |
20 | | - entId (:e_id ent) |
| 20 | + entId (:id ent) |
21 | 21 | [idToUse nextTop] (if (= entId :no-id-yet) [(inc topId) (inc topId)] [entId topId])] |
22 | 22 | [idToUse (keyword (str idToUse)) nextTop])) |
23 | 23 |
|
|
35 | 35 | (let [reffed-id (:value attr) |
36 | 36 | attr-name (:name attr) |
37 | 37 | 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)) |
39 | 39 | ] (assoc-in aevt [reffed-id attr-name] new-back-reffing-set))) |
40 | 40 |
|
41 | 41 | (defn update-aevt[old-aevt ent operation] |
|
47 | 47 | (defn add-entity[db ent] (let [[ent-id ent-id-key next-top] (nextId db ent) |
48 | 48 | new-ts (next-ts db) |
49 | 49 | indices (last (:timestamped db)) |
50 | | - fixed-ent (assoc ent :e_id ent-id-key) |
| 50 | + fixed-ent (assoc ent :id ent-id-key) |
51 | 51 | new-eavt (assoc (:EAVT indices) ent-id-key (update-creation-ts fixed-ent new-ts) ) |
52 | 52 | new-aevt (update-aevt (:AEVT indices) fixed-ent conj) |
53 | 53 | 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))) |
57 | 56 |
|
58 | 57 | (defn remove-entity[db ent] |
59 | | - (let [ent-id (:e_id ent) |
| 58 | + (let [ent-id (:id ent) |
60 | 59 | indices (last (:timestamped db)) |
61 | 60 | aevt (update-aevt (:AEVT indices) ent disj) |
62 | 61 | new-eavt (dissoc (:EAVT indices) ent-id) ; removing the entity |
|
73 | 72 | new-indices (last (:timestamped transacted)) |
74 | 73 | res (assoc initial-db :timestamped (conj initial-indices new-indices) |
75 | 74 | :curr-time (next-ts initial-db) |
76 | | - :topId (:topId transacted)) ] |
77 | | - res)))) |
| 75 | + :topId (:topId transacted))] |
| 76 | + res)))) |
78 | 77 |
|
79 | 78 | (defmacro transact_ [db op & txs] |
80 | 79 | (when txs |
|
0 commit comments