Skip to content

Commit 6fc89f4

Browse files
committed
rollback when operation was wrong
1 parent c19353e commit 6fc89f4

File tree

5 files changed

+195
-220
lines changed

5 files changed

+195
-220
lines changed

src/main/clojure/clojask/classes/ColInfo.clj

Lines changed: 76 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@
2020
(getFormatter [])
2121
(delCol [col-to-del])
2222
(setColInfo [new-col-set])
23-
(renameColInfo [old-col new-col]))
23+
(renameColInfo [old-col new-col])
24+
(copy [] "copy all the information for rollback purpose")
25+
(rollback [] "undo the change making use of the copied")
26+
(commit [])
27+
)
2428

2529

2630
(deftype ColInfo
@@ -31,7 +35,8 @@
3135
^:unsynchronized-mutable col-dsp
3236
^:unsynchronized-mutable col-type
3337
^:unsynchronized-mutable col-format
34-
^:unsynchronized-mutable col-deleted]
38+
^:unsynchronized-mutable col-deleted
39+
^:unsynchronized-mutable hist]
3540

3641
;; method
3742
ColIntf
@@ -44,17 +49,48 @@
4449
(set! col-dsp (zipmap (take (count colNames) (iterate inc 0)) (map vector (map vector (iterate inc 0)))))
4550
(set! col-deleted (set nil)))
4651

52+
(getFormatter
53+
[this]
54+
col-format)
55+
56+
(getDesc
57+
[this]
58+
col-dsp)
59+
60+
(getType
61+
[this]
62+
col-type)
63+
64+
(getKeys
65+
[this]
66+
(mapv (fn [index] (get index-key index))
67+
(take (count index-key) (iterate inc 0))))
68+
69+
(getKeyIndex
70+
[this]
71+
key-index)
72+
73+
(getIndexKey
74+
[this]
75+
index-key)
76+
77+
(getDeletedCol
78+
[this]
79+
col-deleted)
80+
4781
(operate
4882
[this operation col]
83+
(.copy this)
4984
(if (contains? key-index col)
5085
(do
5186
(set! col-dsp (assoc col-dsp (get key-index col) (conj (get col-dsp (get key-index col)) operation)))
5287
;; "success"
53-
nil)
54-
(throw (OperationException. "Column name passed to operate not found"))))
88+
nil)
89+
(throw (OperationException. "Column name passed to operate not found"))))
5590

5691
(operate
5792
[this operation col newCol]
93+
(.copy this)
5894
(let [col (if (coll? col)
5995
col
6096
[col])
@@ -70,11 +106,11 @@
70106
;; "success"
71107
nil))
72108
(do
73-
(throw (OperationException. (str external " are not original column names")))
74-
))))
109+
(throw (OperationException. (str external " are not original column names")))))))
75110

76111
(setType
77112
[this operation col]
113+
(.copy this)
78114
(if (.contains col-keys col)
79115
;; if this column has been assigned a type
80116
(do
@@ -86,44 +122,18 @@
86122

87123
(setFormatter
88124
[this format col]
125+
(.copy this)
89126
(set! col-format (assoc col-format (get key-index col) format)))
90127

91-
(getFormatter
92-
[this]
93-
col-format)
94-
95-
(getDesc
96-
[this]
97-
col-dsp)
98-
99-
(getType
100-
[this]
101-
col-type)
102-
103-
(getKeys
104-
[this]
105-
(mapv (fn [index] (get index-key index))
106-
(take (count index-key) (iterate inc 0))))
107-
108-
(getKeyIndex
109-
[this]
110-
key-index)
111-
112-
(getIndexKey
113-
[this]
114-
index-key)
115-
116-
(getDeletedCol
117-
[this]
118-
col-deleted)
119-
120128
(delCol
121129
[this col-to-delete]
130+
(.copy this)
122131
(let [col-indices (set (map key-index col-to-delete))]
123132
(set! col-deleted (set/union col-deleted col-indices))))
124133

125134
(setColInfo
126135
[this new-col-set]
136+
(.copy this)
127137
(let [original-key-index (.getKeyIndex this)
128138
new-col-dsp-vals (vals (select-keys original-key-index new-col-set))
129139
original-type (.getType this)
@@ -133,13 +143,39 @@
133143
(set! index-key (zipmap (iterate inc 0) new-col-set))
134144
(set! col-dsp (zipmap (take (count col-keys) (iterate inc 0)) (map vector (map vector new-col-dsp-vals))))
135145
(if (not (empty? (.getType this)))
136-
(set! col-type (zipmap (map #(first (first (get col-dsp (first %)))) original-type) (map last original-type))))
146+
(set! col-type (zipmap (map #(first (first (get col-dsp (first %)))) original-type) (map last original-type))))
137147
(if (not (empty? (.getFormatter this)))
138-
(set! col-format (zipmap (map #(first (first (get col-dsp (first %)))) original-format) (map last original-format))))))
139-
148+
(set! col-format (zipmap (map #(first (first (get col-dsp (first %)))) original-format) (map last original-format))))))
149+
140150
(renameColInfo
141151
[this old-col new-col]
152+
(.copy this)
142153
(set! col-keys (mapv (fn [_] (if (= _ old-col) new-col _)) col-keys))
143154
(let [index (get key-index old-col)]
144-
(set! key-index (set/rename-keys key-index {old-col new-col}))
145-
(set! index-key (update index-key index (fn [_] new-col))))))
155+
(set! key-index (set/rename-keys key-index {old-col new-col}))
156+
(set! index-key (update index-key index (fn [_] new-col)))))
157+
158+
(copy
159+
[this]
160+
(set! hist {:col-keys col-keys
161+
:key-index key-index
162+
:index-key index-key
163+
:col-dsp col-dsp
164+
:col-type col-type
165+
:col-format col-format
166+
:col-deleted col-deleted}))
167+
168+
(rollback
169+
[this]
170+
(if (not= hist {})
171+
(do (set! col-keys (:col-keys hist)) ;; contains only the original keys
172+
(set! key-index (:key-index hist))
173+
(set! index-key (:index-key hist))
174+
(set! col-type (:col-type hist))
175+
(set! col-format (:col-format hist))
176+
(set! col-dsp (:col-dsp hist))
177+
(set! col-deleted (:col-deleted hist)))))
178+
179+
(commit
180+
[this]
181+
(set! hist {})))

src/main/clojure/clojask/classes/DataFrame.clj

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,9 @@
6161
(sort [a b] "sort the dataframe based on columns")
6262
(addFormatter [a b] "format the column as the last step of the computation")
6363
(previewDF [] "preview function used for error predetection")
64-
(errorPredetect [msg] "prints exception with msg if error is detected in preview"))
64+
(errorPredetect [msg] "prints exception with msg if error is detected in preview")
65+
(rollback [] "rollback the history of ColInfo and RowInfo")
66+
(commit [] "commit the change"))
6567

6668
;; each dataframe can have a delayed object
6769
(defrecord DataFrame
@@ -82,7 +84,7 @@
8284
path1 (.getAbsolutePath (io/file path1))
8385
path2 (.getAbsolutePath (io/file path))]
8486
(if (= path1 path2)
85-
(throw (OperationException. "Output path should be different from input path of dataframe argument."))))
87+
(throw (OperationException. "Output path should be different from input path of dataframe argument."))))
8688
(catch Exception e nil)))
8789

8890
(getOutput
@@ -190,7 +192,7 @@
190192
cols
191193
(vector cols))
192194
indices (map (fn [_] (get (.getKeyIndex (:col-info this)) _)) cols)]
193-
(cond (not (u/are-in cols this))
195+
(cond (not= [] (u/are-in cols this))
194196
(throw (TypeException. "Input includes non-existent column name(s).")))
195197
(if (nil? (.filter row-info indices predicate))
196198
this
@@ -406,6 +408,18 @@
406408
(.previewDF this)
407409
(catch Exception e
408410
(do
409-
(throw (OperationException. (format (str msg " (original error: %s)") (str (.getMessage e))))))))))
411+
(.rollback this)
412+
(throw (OperationException. (format (str msg " (original error: %s)") (str (.getMessage e)))))))))
413+
(.commit this))
414+
415+
(rollback
416+
[this]
417+
(.rollback col-info)
418+
(.rollback row-info))
419+
420+
(commit
421+
[this]
422+
(.commit col-info)
423+
(.commit row-info))
410424

411425
Object)

src/main/clojure/clojask/classes/RowInfo.clj

Lines changed: 65 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -15,71 +15,103 @@
1515
(groupby [a])
1616
(aggregate [func old-key new-key])
1717
(setRowInfo [new-col-desc new-col-set])
18-
(renameRowInfo [new-col-names]))
18+
(renameRowInfo [new-col-names])
19+
(copy [])
20+
(rollback [])
21+
(commit []))
1922

2023
(deftype RowInfo
2124
[^:unsynchronized-mutable filters
2225
^:unsynchronized-mutable groupby-key
2326
^:unsynchronized-mutable aggre-func
2427
;; ^:unsynchronized-mutable aggre-old-key
25-
^:unsynchronized-mutable aggre-new-key]
28+
^:unsynchronized-mutable aggre-new-key
29+
^:unsynchronized-mutable hist]
2630
RowIntf
2731
(getFilters
28-
[self]
32+
[this]
2933
filters)
34+
35+
(getGroupbyKeys
36+
[this]
37+
groupby-key)
38+
39+
;; (getAggreOldKeys
40+
;; [this]
41+
;; aggre-old-key)
42+
(getAggreNewKeys
43+
[this]
44+
aggre-new-key)
45+
46+
(getAggreFunc
47+
[this]
48+
aggre-func)
49+
3050
(filter
31-
[self cols predicate]
32-
(set! filters (conj filters [predicate cols]))
51+
[this cols predicate]
52+
(.copy this)
53+
(set! filters (conj filters [predicate cols]))
3354
;; "success"
34-
nil)
55+
nil)
56+
3557
(groupby
36-
[self key]
58+
[this key]
59+
(.copy this)
3760
(set! groupby-key key)
3861
;; "success"
3962
nil)
40-
(getGroupbyKeys
41-
[self]
42-
groupby-key)
43-
;; (getAggreOldKeys
44-
;; [self]
45-
;; aggre-old-key)
46-
(getAggreNewKeys
47-
[self]
48-
aggre-new-key)
49-
(getAggreFunc
50-
[self]
51-
aggre-func)
63+
5264
(aggregate
53-
[self func old-keys new-keys]
65+
[this func old-keys new-keys]
66+
(.copy this)
5467
(if true
5568
;; (not= groupby-key [])
5669
(do
5770
(doseq [old-key old-keys]
5871
(set! aggre-func (conj aggre-func [func old-key])))
5972
;; (set! aggre-old-key old-key)
6073
(doseq [new-key new-keys]
61-
(set! aggre-new-key (conj aggre-new-key new-key)))
74+
(set! aggre-new-key (conj aggre-new-key new-key)))
6275
; "success"
6376
nil)
64-
(throw (OperationException. "you must first group the dataframe by some keys then aggregate"))))
77+
(throw (OperationException. "you must first group the dataframe by some keys then aggregate"))))
78+
6579
(setRowInfo
66-
[self new-col-desc new-col-set]
67-
(let [original-filter (.getFilters self)
68-
original-groupby-keys (.getGroupbyKeys self)
69-
original-aggre-func (.getAggreFunc self)
80+
[this new-col-desc new-col-set]
81+
(.copy this)
82+
(let [original-filter (.getFilters this)
83+
original-groupby-keys (.getGroupbyKeys this)
84+
original-aggre-func (.getAggreFunc this)
7085
new-filter-fns (map #(first %) original-filter)
7186
new-filter-cols (map (fn [fcols] (map #(first (first (get new-col-desc %))) fcols)) (doall (map #(last %) original-filter)))
7287
new-groupby-fns (map #(first %) original-groupby-keys)
7388
new-groupby-cols (map #(first (first (get new-col-desc (last %)))) original-groupby-keys)
7489
new-aggre-fns (map #(first %) original-aggre-func)
7590
new-aggre-cols (map #(first (first (get new-col-desc (last %)))) original-aggre-func)]
76-
(if (not (empty? (.getFilters self)))
91+
(if (not (empty? (.getFilters this)))
7792
(set! filters (vec (map vector new-filter-fns new-filter-cols))))
78-
(if (not (empty? (.getGroupbyKeys self)))
79-
(set! groupby-key (vec (map vector new-groupby-fns new-groupby-cols)))
93+
(if (not (empty? (.getGroupbyKeys this)))
94+
(set! groupby-key (vec (map vector new-groupby-fns new-groupby-cols)))
8095
;(set! groupby-key (vec (map #(first (first (get new-col-desc %))) original-groupby-keys)))
8196
)
82-
(if (not (empty? (.getAggreFunc self)))
83-
(set! aggre-func (vec (map vector new-aggre-fns new-aggre-cols))))
84-
))
85-
)
97+
(if (not (empty? (.getAggreFunc this)))
98+
(set! aggre-func (vec (map vector new-aggre-fns new-aggre-cols))))))
99+
100+
(copy
101+
[this]
102+
(set! hist {:filters filters
103+
:groupby-key groupby-key
104+
:aggre-func aggre-func
105+
:aggre-new-key aggre-new-key}))
106+
107+
(rollback
108+
[this]
109+
(if (not= hist {})
110+
(do (set! filters (:filters hist))
111+
(set! groupby-key (:groupby-key hist))
112+
(set! aggre-func (:aggre-func hist))
113+
(set! aggre-new-key (:aggre-new-key hist)))))
114+
115+
(commit
116+
[this]
117+
(set! hist {})))

0 commit comments

Comments
 (0)