ziv@2213
|
1 structure Sqlcache (* :> SQLCACHE *) = struct
|
ziv@2209
|
2
|
ziv@2209
|
3 open Mono
|
ziv@2209
|
4
|
ziv@2209
|
5 structure IS = IntBinarySet
|
ziv@2209
|
6 structure IM = IntBinaryMap
|
ziv@2213
|
7 structure SK = struct type ord_key = string val compare = String.compare end
|
ziv@2213
|
8 structure SS = BinarySetFn(SK)
|
ziv@2213
|
9 structure SM = BinaryMapFn(SK)
|
ziv@2213
|
10 structure SIMM = MultimapFn(structure KeyMap = SM structure ValSet = IS)
|
ziv@2209
|
11
|
ziv@2216
|
12 (* Filled in by [cacheWrap] during [Sqlcache]. *)
|
ziv@2213
|
13 val ffiInfo : {index : int, params : int} list ref = ref []
|
ziv@2209
|
14
|
ziv@2213
|
15 fun getFfiInfo () = !ffiInfo
|
ziv@2213
|
16
|
ziv@2215
|
17 (* Some FFIs have writing as their only effect, which the caching records. *)
|
ziv@2215
|
18 val ffiEffectful =
|
ziv@2216
|
19 (* TODO: have this less hard-coded. *)
|
ziv@2215
|
20 let
|
ziv@2215
|
21 val fs = SS.fromList ["htmlifyInt_w",
|
ziv@2215
|
22 "htmlifyFloat_w",
|
ziv@2215
|
23 "htmlifyString_w",
|
ziv@2215
|
24 "htmlifyBool_w",
|
ziv@2215
|
25 "htmlifyTime_w",
|
ziv@2215
|
26 "attrifyInt_w",
|
ziv@2215
|
27 "attrifyFloat_w",
|
ziv@2215
|
28 "attrifyString_w",
|
ziv@2215
|
29 "attrifyChar_w",
|
ziv@2215
|
30 "urlifyInt_w",
|
ziv@2215
|
31 "urlifyFloat_w",
|
ziv@2215
|
32 "urlifyString_w",
|
ziv@2215
|
33 "urlifyBool_w",
|
ziv@2215
|
34 "urlifyChannel_w"]
|
ziv@2215
|
35 in
|
ziv@2215
|
36 fn (m, f) => Settings.isEffectful (m, f)
|
ziv@2215
|
37 andalso not (m = "Basis" andalso SS.member (fs, f))
|
ziv@2215
|
38 end
|
ziv@2215
|
39
|
ziv@2215
|
40
|
ziv@2215
|
41 (* Effect analysis. *)
|
ziv@2215
|
42
|
ziv@2216
|
43 (* Makes an exception for [EWrite] (which is recorded when caching). *)
|
ziv@2215
|
44 fun effectful doPrint (effs : IS.set) (inFunction : bool) (bound : int) : Mono.exp -> bool =
|
ziv@2215
|
45 (* If result is true, expression is definitely effectful. If result is
|
ziv@2215
|
46 false, then expression is definitely not effectful if effs is fully
|
ziv@2215
|
47 populated. The intended pattern is to use this a number of times equal
|
ziv@2215
|
48 to the number of declarations in a file, Bellman-Ford style. *)
|
ziv@2215
|
49 (* TODO: make incrementing of bound less janky, probably by using MonoUtil
|
ziv@2215
|
50 instead of all this. *)
|
ziv@2215
|
51 let
|
ziv@2215
|
52 (* DEBUG: remove printing when done. *)
|
ziv@2215
|
53 fun tru msg = if doPrint then (print (msg ^ "\n"); true) else true
|
ziv@2215
|
54 val rec eff' =
|
ziv@2215
|
55 (* ASK: is there a better way? *)
|
ziv@2215
|
56 fn EPrim _ => false
|
ziv@2215
|
57 (* We don't know if local functions have effects when applied. *)
|
ziv@2215
|
58 | ERel idx => if inFunction andalso idx >= bound
|
ziv@2215
|
59 then tru ("rel" ^ Int.toString idx) else false
|
ziv@2215
|
60 | ENamed name => if IS.member (effs, name) then tru "named" else false
|
ziv@2215
|
61 | ECon (_, _, NONE) => false
|
ziv@2215
|
62 | ECon (_, _, SOME e) => eff e
|
ziv@2215
|
63 | ENone _ => false
|
ziv@2215
|
64 | ESome (_, e) => eff e
|
ziv@2215
|
65 | EFfi (m, f) => if ffiEffectful (m, f) then tru "ffi" else false
|
ziv@2215
|
66 | EFfiApp (m, f, _) => if ffiEffectful (m, f) then tru "ffiapp" else false
|
ziv@2215
|
67 (* ASK: we're calling functions effectful if they have effects when
|
ziv@2215
|
68 applied or if the function expressions themselves have effects.
|
ziv@2215
|
69 Is that okay? *)
|
ziv@2215
|
70 (* This is okay because the values we ultimately care about aren't
|
ziv@2215
|
71 functions, and this is a conservative approximation, anyway. *)
|
ziv@2215
|
72 | EApp (eFun, eArg) => effectful doPrint effs true bound eFun orelse eff eArg
|
ziv@2215
|
73 | EAbs (_, _, _, e) => effectful doPrint effs inFunction (bound+1) e
|
ziv@2215
|
74 | EUnop (_, e) => eff e
|
ziv@2215
|
75 | EBinop (_, _, e1, e2) => eff e1 orelse eff e2
|
ziv@2215
|
76 | ERecord xs => List.exists (fn (_, e, _) => eff e) xs
|
ziv@2215
|
77 | EField (e, _) => eff e
|
ziv@2215
|
78 (* If any case could be effectful, consider it effectful. *)
|
ziv@2215
|
79 | ECase (e, xs, _) => eff e orelse List.exists (fn (_, e) => eff e) xs
|
ziv@2215
|
80 | EStrcat (e1, e2) => eff e1 orelse eff e2
|
ziv@2215
|
81 (* ASK: how should we treat these three? *)
|
ziv@2215
|
82 | EError _ => tru "error"
|
ziv@2215
|
83 | EReturnBlob _ => tru "blob"
|
ziv@2215
|
84 | ERedirect _ => tru "redirect"
|
ziv@2215
|
85 (* EWrite is a special exception because we record writes when caching. *)
|
ziv@2215
|
86 | EWrite _ => false
|
ziv@2215
|
87 | ESeq (e1, e2) => eff e1 orelse eff e2
|
ziv@2215
|
88 (* TODO: keep context of which local variables aren't effectful? Only
|
ziv@2215
|
89 makes a difference for function expressions, though. *)
|
ziv@2215
|
90 | ELet (_, _, eBind, eBody) => eff eBind orelse
|
ziv@2215
|
91 effectful doPrint effs inFunction (bound+1) eBody
|
ziv@2215
|
92 | EClosure (_, es) => List.exists eff es
|
ziv@2215
|
93 (* TODO: deal with EQuery. *)
|
ziv@2215
|
94 | EQuery _ => tru "query"
|
ziv@2215
|
95 | EDml _ => tru "dml"
|
ziv@2215
|
96 | ENextval _ => tru "nextval"
|
ziv@2215
|
97 | ESetval _ => tru "setval"
|
ziv@2215
|
98 | EUnurlify (e, _, _) => eff e
|
ziv@2215
|
99 (* ASK: how should we treat this? *)
|
ziv@2215
|
100 | EJavaScript _ => tru "javascript"
|
ziv@2215
|
101 (* ASK: these are all effectful, right? *)
|
ziv@2215
|
102 | ESignalReturn _ => tru "signalreturn"
|
ziv@2215
|
103 | ESignalBind _ => tru "signalbind"
|
ziv@2215
|
104 | ESignalSource _ => tru "signalsource"
|
ziv@2215
|
105 | EServerCall _ => tru "servercall"
|
ziv@2215
|
106 | ERecv _ => tru "recv"
|
ziv@2215
|
107 | ESleep _ => tru "sleep"
|
ziv@2215
|
108 | ESpawn _ => tru "spawn"
|
ziv@2215
|
109 and eff = fn (e', _) => eff' e'
|
ziv@2215
|
110 in
|
ziv@2215
|
111 eff
|
ziv@2215
|
112 end
|
ziv@2215
|
113
|
ziv@2215
|
114 (* TODO: test this. *)
|
ziv@2215
|
115 val effectfulMap =
|
ziv@2215
|
116 let
|
ziv@2215
|
117 fun doVal ((_, name, _, e, _), effMap) =
|
ziv@2215
|
118 if effectful false effMap false 0 e
|
ziv@2215
|
119 then IS.add (effMap, name)
|
ziv@2215
|
120 else effMap
|
ziv@2215
|
121 val doDecl =
|
ziv@2215
|
122 fn (DVal v, effMap) => doVal (v, effMap)
|
ziv@2215
|
123 (* Repeat the list of declarations a number of times equal to its size. *)
|
ziv@2215
|
124 | (DValRec vs, effMap) =>
|
ziv@2215
|
125 List.foldl doVal effMap (List.concat (List.map (fn _ => vs) vs))
|
ziv@2215
|
126 (* ASK: any other cases? *)
|
ziv@2215
|
127 | (_, effMap) => effMap
|
ziv@2215
|
128 in
|
ziv@2215
|
129 MonoUtil.File.fold {typ = #2, exp = #2, decl = doDecl} IS.empty
|
ziv@2215
|
130 end
|
ziv@2215
|
131
|
ziv@2215
|
132
|
ziv@2216
|
133 (* Boolean formula normalization. *)
|
ziv@2216
|
134
|
ziv@2216
|
135 datatype normalForm = Cnf | Dnf
|
ziv@2216
|
136
|
ziv@2216
|
137 datatype 'atom formula =
|
ziv@2216
|
138 Atom of 'atom
|
ziv@2216
|
139 | Negate of 'atom formula
|
ziv@2216
|
140 | Combo of normalForm * 'atom formula list
|
ziv@2216
|
141
|
ziv@2216
|
142 val flipNf = fn Cnf => Dnf | Dnf => Cnf
|
ziv@2216
|
143
|
ziv@2216
|
144 fun bind xs f = List.concat (map f xs)
|
ziv@2216
|
145
|
ziv@2216
|
146 val rec cartesianProduct : 'a list list -> 'a list list =
|
ziv@2216
|
147 fn [] => [[]]
|
ziv@2216
|
148 | (xs :: xss) => bind (cartesianProduct xss)
|
ziv@2216
|
149 (fn ys => bind xs (fn x => [x :: ys]))
|
ziv@2216
|
150
|
ziv@2218
|
151 (* Pushes all negation to the atoms.*)
|
ziv@2218
|
152 fun pushNegate (negate : 'atom -> 'atom) (negating : bool) =
|
ziv@2218
|
153 fn Atom x => Atom (if negating then negate x else x)
|
ziv@2218
|
154 | Negate f => pushNegate negate (not negating) f
|
ziv@2218
|
155 | Combo (n, fs) => Combo (if negating then flipNf n else n, map (pushNegate negate negating) fs)
|
ziv@2218
|
156
|
ziv@2218
|
157 val rec flatten =
|
ziv@2218
|
158 fn Combo (n, fs) =>
|
ziv@2218
|
159 Combo (n, List.foldr (fn (f, acc) =>
|
ziv@2218
|
160 case f of
|
ziv@2218
|
161 Combo (n', fs') => if n = n' then fs' @ acc else f :: acc
|
ziv@2218
|
162 | _ => f :: acc)
|
ziv@2218
|
163 []
|
ziv@2218
|
164 (map flatten fs))
|
ziv@2218
|
165 | f => f
|
ziv@2218
|
166
|
ziv@2218
|
167 fun normalize' (negate : 'atom -> 'atom) (norm : normalForm) =
|
ziv@2216
|
168 fn Atom x => [[x]]
|
ziv@2218
|
169 | Negate f => map (map negate) (normalize' negate (flipNf norm) f)
|
ziv@2216
|
170 | Combo (n, fs) =>
|
ziv@2216
|
171 let
|
ziv@2218
|
172 val fss = bind fs (normalize' negate n)
|
ziv@2216
|
173 in
|
ziv@2216
|
174 if n = norm then fss else cartesianProduct fss
|
ziv@2216
|
175 end
|
ziv@2216
|
176
|
ziv@2218
|
177 fun normalize negate norm = normalize' negate norm o flatten o pushNegate negate false
|
ziv@2216
|
178
|
ziv@2218
|
179 fun mapFormulaSigned positive mf =
|
ziv@2218
|
180 fn Atom x => Atom (mf (positive, x))
|
ziv@2218
|
181 | Negate f => Negate (mapFormulaSigned (not positive) mf f)
|
ziv@2218
|
182 | Combo (n, fs) => Combo (n, map (mapFormulaSigned positive mf) fs)
|
ziv@2218
|
183
|
ziv@2218
|
184 fun mapFormula mf = mapFormulaSigned true (fn (_, x) => mf x)
|
ziv@2216
|
185
|
ziv@2215
|
186 (* SQL analysis. *)
|
ziv@2213
|
187
|
ziv@2216
|
188 val rec chooseTwos : 'a list -> ('a * 'a) list =
|
ziv@2216
|
189 fn [] => []
|
ziv@2216
|
190 | x :: ys => map (fn y => (x, y)) ys @ chooseTwos ys
|
ziv@2213
|
191
|
ziv@2216
|
192 datatype atomExp =
|
ziv@2216
|
193 QueryArg of int
|
ziv@2216
|
194 | DmlRel of int
|
ziv@2216
|
195 | Prim of Prim.t
|
ziv@2216
|
196 | Field of string * string
|
ziv@2216
|
197
|
ziv@2218
|
198 val equalAtomExp =
|
ziv@2218
|
199 let
|
ziv@2218
|
200 val isEqual = fn EQUAL => true | _ => false
|
ziv@2218
|
201 in
|
ziv@2218
|
202 fn (QueryArg n1, QueryArg n2) => n1 = n2
|
ziv@2218
|
203 | (DmlRel n1, DmlRel n2) => n1 = n2
|
ziv@2218
|
204 | (Prim p1, Prim p2) => isEqual (Prim.compare (p1, p2))
|
ziv@2218
|
205 | (Field (t1, f1), Field (t2, f2)) => isEqual (String.compare (t1 ^ "." ^ f1, t2 ^ "." ^ f2))
|
ziv@2218
|
206 | _ => false
|
ziv@2218
|
207 end
|
ziv@2218
|
208
|
ziv@2216
|
209 structure AtomExpKey : ORD_KEY = struct
|
ziv@2216
|
210
|
ziv@2216
|
211 type ord_key = atomExp
|
ziv@2216
|
212
|
ziv@2216
|
213 val compare =
|
ziv@2216
|
214 fn (QueryArg n1, QueryArg n2) => Int.compare (n1, n2)
|
ziv@2216
|
215 | (QueryArg _, _) => LESS
|
ziv@2216
|
216 | (_, QueryArg _) => GREATER
|
ziv@2216
|
217 | (DmlRel n1, DmlRel n2) => Int.compare (n1, n2)
|
ziv@2216
|
218 | (DmlRel _, _) => LESS
|
ziv@2216
|
219 | (_, DmlRel _) => GREATER
|
ziv@2216
|
220 | (Prim p1, Prim p2) => Prim.compare (p1, p2)
|
ziv@2216
|
221 | (Prim _, _) => LESS
|
ziv@2216
|
222 | (_, Prim _) => GREATER
|
ziv@2216
|
223 | (Field (t1, f1), Field (t2, f2)) => String.compare (t1 ^ "." ^ f1, t2 ^ "." ^ f2)
|
ziv@2216
|
224
|
ziv@2216
|
225 end
|
ziv@2216
|
226
|
ziv@2216
|
227 structure UF = UnionFindFn(AtomExpKey)
|
ziv@2216
|
228
|
ziv@2218
|
229 (* val conflictMaps : (Sql.cmp * Sql.sqexp * Sql.sqexp) formula *)
|
ziv@2218
|
230 (* * (Sql.cmp * Sql.sqexp * Sql.sqexp) formula *)
|
ziv@2218
|
231 (* -> Mono.exp' IM.map list = *)
|
ziv@2218
|
232 (* let *)
|
ziv@2216
|
233 val toKnownEquality =
|
ziv@2216
|
234 (* [NONE] here means unkown. Anything that isn't a comparison between
|
ziv@2216
|
235 two knowns shouldn't be used, and simply dropping unused terms is
|
ziv@2216
|
236 okay in disjunctive normal form. *)
|
ziv@2216
|
237 fn (Sql.Eq, SOME e1, SOME e2) => SOME (e1, e2)
|
ziv@2216
|
238 | _ => NONE
|
ziv@2216
|
239 val equivClasses : (Sql.cmp * atomExp option * atomExp option) list -> atomExp list list =
|
ziv@2216
|
240 UF.classes
|
ziv@2216
|
241 o List.foldl UF.union' UF.empty
|
ziv@2216
|
242 o List.mapPartial toKnownEquality
|
ziv@2216
|
243 fun addToEqs (eqs, n, e) =
|
ziv@2216
|
244 case IM.find (eqs, n) of
|
ziv@2216
|
245 (* Comparing to a constant seems better? *)
|
ziv@2218
|
246 SOME (Prim _) => eqs
|
ziv@2216
|
247 | _ => IM.insert (eqs, n, e)
|
ziv@2216
|
248 val accumulateEqs =
|
ziv@2216
|
249 (* [NONE] means we have a contradiction. *)
|
ziv@2216
|
250 fn (_, NONE) => NONE
|
ziv@2216
|
251 | ((Prim p1, Prim p2), eqso) =>
|
ziv@2216
|
252 (case Prim.compare (p1, p2) of
|
ziv@2216
|
253 EQUAL => eqso
|
ziv@2213
|
254 | _ => NONE)
|
ziv@2218
|
255 | ((QueryArg n, Prim p), SOME eqs) => SOME (addToEqs (eqs, n, Prim p))
|
ziv@2218
|
256 | ((QueryArg n, DmlRel r), SOME eqs) => SOME (addToEqs (eqs, n, DmlRel r))
|
ziv@2218
|
257 | ((Prim p, QueryArg n), SOME eqs) => SOME (addToEqs (eqs, n, Prim p))
|
ziv@2218
|
258 | ((DmlRel r, QueryArg n), SOME eqs) => SOME (addToEqs (eqs, n, DmlRel r))
|
ziv@2216
|
259 (* TODO: deal with equalities involving just [DmlRel]s and [Prim]s. *)
|
ziv@2216
|
260 | (_, eqso) => eqso
|
ziv@2218
|
261 val eqsOfClass : atomExp list -> atomExp IM.map option =
|
ziv@2216
|
262 List.foldl accumulateEqs (SOME IM.empty)
|
ziv@2216
|
263 o chooseTwos
|
ziv@2216
|
264 fun toAtomExps rel (cmp, e1, e2) =
|
ziv@2216
|
265 let
|
ziv@2216
|
266 val qa =
|
ziv@2216
|
267 (* Here [NONE] means unkown. *)
|
ziv@2216
|
268 fn Sql.SqConst p => SOME (Prim p)
|
ziv@2216
|
269 | Sql.Field tf => SOME (Field tf)
|
ziv@2216
|
270 | Sql.Inj (EPrim p, _) => SOME (Prim p)
|
ziv@2216
|
271 | Sql.Inj (ERel n, _) => SOME (rel n)
|
ziv@2216
|
272 (* We can't deal with anything else. *)
|
ziv@2216
|
273 | _ => NONE
|
ziv@2216
|
274 in
|
ziv@2216
|
275 (cmp, qa e1, qa e2)
|
ziv@2216
|
276 end
|
ziv@2216
|
277 fun negateCmp (cmp, e1, e2) =
|
ziv@2216
|
278 (case cmp of
|
ziv@2216
|
279 Sql.Eq => Sql.Ne
|
ziv@2216
|
280 | Sql.Ne => Sql.Eq
|
ziv@2216
|
281 | Sql.Lt => Sql.Ge
|
ziv@2216
|
282 | Sql.Le => Sql.Gt
|
ziv@2216
|
283 | Sql.Gt => Sql.Le
|
ziv@2216
|
284 | Sql.Ge => Sql.Lt,
|
ziv@2216
|
285 e1, e2)
|
ziv@2218
|
286 val markQuery : (Sql.cmp * Sql.sqexp * Sql.sqexp) formula ->
|
ziv@2218
|
287 (Sql.cmp * atomExp option * atomExp option) formula =
|
ziv@2218
|
288 mapFormula (toAtomExps QueryArg)
|
ziv@2218
|
289 val markDml : (Sql.cmp * Sql.sqexp * Sql.sqexp) formula ->
|
ziv@2218
|
290 (Sql.cmp * atomExp option * atomExp option) formula =
|
ziv@2218
|
291 mapFormula (toAtomExps DmlRel)
|
ziv@2218
|
292 (* No eqs should have key conflicts because no variable is in two
|
ziv@2218
|
293 equivalence classes, so the [#1] can be anything. *)
|
ziv@2218
|
294 val mergeEqs : (atomExp IntBinaryMap.map option list
|
ziv@2218
|
295 -> atomExp IntBinaryMap.map option) =
|
ziv@2218
|
296 List.foldr (fn (SOME eqs, SOME acc) => SOME (IM.unionWith #1 (eqs, acc)) | _ => NONE)
|
ziv@2218
|
297 (SOME IM.empty)
|
ziv@2218
|
298 fun dnf (fQuery, fDml) =
|
ziv@2218
|
299 normalize negateCmp Dnf (Combo (Cnf, [markQuery fQuery, markDml fDml]))
|
ziv@2218
|
300 (* in *)
|
ziv@2218
|
301 val conflictMaps : (Sql.cmp * Sql.sqexp * Sql.sqexp) formula
|
ziv@2218
|
302 * (Sql.cmp * Sql.sqexp * Sql.sqexp) formula
|
ziv@2218
|
303 -> atomExp IM.map list =
|
ziv@2218
|
304 List.mapPartial (mergeEqs o map eqsOfClass o equivClasses) o dnf
|
ziv@2218
|
305 (* end *)
|
ziv@2213
|
306
|
ziv@2216
|
307 val rec sqexpToFormula =
|
ziv@2216
|
308 fn Sql.SqTrue => Combo (Cnf, [])
|
ziv@2216
|
309 | Sql.SqFalse => Combo (Dnf, [])
|
ziv@2216
|
310 | Sql.SqNot e => Negate (sqexpToFormula e)
|
ziv@2216
|
311 | Sql.Binop (Sql.RCmp c, e1, e2) => Atom (c, e1, e2)
|
ziv@2216
|
312 | Sql.Binop (Sql.RLop l, p1, p2) => Combo (case l of Sql.And => Cnf | Sql.Or => Dnf,
|
ziv@2216
|
313 [sqexpToFormula p1, sqexpToFormula p2])
|
ziv@2216
|
314 (* ASK: any other sqexps that can be props? *)
|
ziv@2216
|
315 | _ => raise Match
|
ziv@2213
|
316
|
ziv@2218
|
317 fun renameTables tablePairs =
|
ziv@2216
|
318 let
|
ziv@2216
|
319 fun renameString table =
|
ziv@2216
|
320 case List.find (fn (_, t) => table = t) tablePairs of
|
ziv@2216
|
321 NONE => table
|
ziv@2216
|
322 | SOME (realTable, _) => realTable
|
ziv@2216
|
323 val renameSqexp =
|
ziv@2216
|
324 fn Sql.Field (table, field) => Sql.Field (renameString table, field)
|
ziv@2216
|
325 | e => e
|
ziv@2218
|
326 fun renameAtom (cmp, e1, e2) = (cmp, renameSqexp e1, renameSqexp e2)
|
ziv@2216
|
327 in
|
ziv@2218
|
328 mapFormula renameAtom
|
ziv@2216
|
329 end
|
ziv@2218
|
330
|
ziv@2218
|
331 val rec queryToFormula =
|
ziv@2218
|
332 fn Sql.Query1 {Where = NONE, ...} => Combo (Cnf, [])
|
ziv@2218
|
333 | Sql.Query1 {From = tablePairs, Where = SOME e, ...} =>
|
ziv@2218
|
334 renameTables tablePairs (sqexpToFormula e)
|
ziv@2216
|
335 | Sql.Union (q1, q2) => Combo (Dnf, [queryToFormula q1, queryToFormula q2])
|
ziv@2216
|
336
|
ziv@2218
|
337 fun valsToFormula (table, vals) =
|
ziv@2218
|
338 Combo (Cnf, map (fn (field, v) => Atom (Sql.Eq, Sql.Field (table, field), v)) vals)
|
ziv@2218
|
339
|
ziv@2216
|
340 val rec dmlToFormula =
|
ziv@2218
|
341 fn Sql.Insert tableVals => valsToFormula tableVals
|
ziv@2218
|
342 | Sql.Delete (table, wher) => renameTables [(table, "T")] (sqexpToFormula wher)
|
ziv@2216
|
343 (* TODO: refine formula for the vals part, which could take into account the wher part. *)
|
ziv@2219
|
344 (* TODO: use pushNegate instead of mapFormulaSigned? *)
|
ziv@2218
|
345 | Sql.Update (table, vals, wher) =>
|
ziv@2218
|
346 let
|
ziv@2218
|
347 val f = sqexpToFormula wher
|
ziv@2218
|
348 fun update (positive, a) =
|
ziv@2218
|
349 let
|
ziv@2218
|
350 fun updateIfNecessary field =
|
ziv@2218
|
351 case List.find (fn (f, _) => field = f) vals of
|
ziv@2218
|
352 SOME (_, v) => (if positive then Sql.Eq else Sql.Ne,
|
ziv@2218
|
353 Sql.Field (table, field),
|
ziv@2218
|
354 v)
|
ziv@2218
|
355 | NONE => a
|
ziv@2218
|
356 in
|
ziv@2218
|
357 case a of
|
ziv@2218
|
358 (_, Sql.Field (_, field), _) => updateIfNecessary field
|
ziv@2218
|
359 | (_, _, Sql.Field (_, field)) => updateIfNecessary field
|
ziv@2218
|
360 | _ => a
|
ziv@2218
|
361 end
|
ziv@2218
|
362 in
|
ziv@2218
|
363 renameTables [(table, "T")]
|
ziv@2218
|
364 (Combo (Dnf, [f,
|
ziv@2218
|
365 Combo (Cnf, [valsToFormula (table, vals),
|
ziv@2218
|
366 mapFormulaSigned true update f])]))
|
ziv@2218
|
367 end
|
ziv@2213
|
368
|
ziv@2213
|
369 val rec tablesQuery =
|
ziv@2216
|
370 fn Sql.Query1 {From = tablePairs, ...} => SS.fromList (map #1 tablePairs)
|
ziv@2216
|
371 | Sql.Union (q1, q2) => SS.union (tablesQuery q1, tablesQuery q2)
|
ziv@2213
|
372
|
ziv@2213
|
373 val tableDml =
|
ziv@2216
|
374 fn Sql.Insert (tab, _) => tab
|
ziv@2216
|
375 | Sql.Delete (tab, _) => tab
|
ziv@2216
|
376 | Sql.Update (tab, _, _) => tab
|
ziv@2213
|
377
|
ziv@2213
|
378
|
ziv@2213
|
379 (* Program instrumentation. *)
|
ziv@2213
|
380
|
ziv@2215
|
381 fun stringExp s = (EPrim (Prim.String (Prim.Normal, s)), ErrorMsg.dummySpan)
|
ziv@2216
|
382
|
ziv@2213
|
383 val stringTyp = (TFfi ("Basis", "string"), ErrorMsg.dummySpan)
|
ziv@2213
|
384
|
ziv@2213
|
385 val sequence =
|
ziv@2213
|
386 fn (exp :: exps) =>
|
ziv@2213
|
387 let
|
ziv@2213
|
388 val loc = ErrorMsg.dummySpan
|
ziv@2213
|
389 in
|
ziv@2213
|
390 List.foldl (fn (e', seq) => ESeq ((seq, loc), (e', loc))) exp exps
|
ziv@2213
|
391 end
|
ziv@2213
|
392 | _ => raise Match
|
ziv@2213
|
393
|
ziv@2213
|
394 fun ffiAppCache' (func, index, args) : Mono.exp' =
|
ziv@2213
|
395 EFfiApp ("Sqlcache", func ^ Int.toString index, args)
|
ziv@2213
|
396
|
ziv@2215
|
397 fun ffiAppCache (func, index, args) : Mono.exp =
|
ziv@2213
|
398 (ffiAppCache' (func, index, args), ErrorMsg.dummySpan)
|
ziv@2213
|
399
|
ziv@2213
|
400 val varPrefix = "queryResult"
|
ziv@2213
|
401
|
ziv@2213
|
402 fun indexOfName varName =
|
ziv@2213
|
403 if String.isPrefix varPrefix varName
|
ziv@2213
|
404 then Int.fromString (String.extract (varName, String.size varPrefix, NONE))
|
ziv@2213
|
405 else NONE
|
ziv@2213
|
406
|
ziv@2215
|
407 (* Always increments negative indices because that's what we need later. *)
|
ziv@2215
|
408 fun incRelsBound bound inc =
|
ziv@2215
|
409 MonoUtil.Exp.mapB
|
ziv@2215
|
410 {typ = fn x => x,
|
ziv@2215
|
411 exp = fn level =>
|
ziv@2215
|
412 (fn ERel n => ERel (if n >= level orelse n < 0 then n + inc else n)
|
ziv@2215
|
413 | x => x),
|
ziv@2215
|
414 bind = fn (level, MonoUtil.Exp.RelE _) => level + 1 | (level, _) => level}
|
ziv@2215
|
415 bound
|
ziv@2215
|
416
|
ziv@2215
|
417 val incRels = incRelsBound 0
|
ziv@2213
|
418
|
ziv@2216
|
419 (* Filled in by instrumentQuery during [Monoize], used during [Sqlcache]. *)
|
ziv@2213
|
420 val urlifiedRel0s : Mono.exp IM.map ref = ref IM.empty
|
ziv@2213
|
421
|
ziv@2216
|
422 (* Used by [Monoize]. *)
|
ziv@2213
|
423 val instrumentQuery =
|
ziv@2213
|
424 let
|
ziv@2213
|
425 val nextQuery = ref 0
|
ziv@2213
|
426 fun iq (query, urlifiedRel0) =
|
ziv@2213
|
427 case query of
|
ziv@2213
|
428 (EQuery {state = typ, ...}, loc) =>
|
ziv@2213
|
429 let
|
ziv@2213
|
430 val i = !nextQuery before nextQuery := !nextQuery + 1
|
ziv@2213
|
431 in
|
ziv@2213
|
432 urlifiedRel0s := IM.insert (!urlifiedRel0s, i, urlifiedRel0);
|
ziv@2213
|
433 (ELet (varPrefix ^ Int.toString i, typ, query,
|
ziv@2213
|
434 (* Uses a dummy FFI call to keep the urlified expression around, which
|
ziv@2213
|
435 in turn keeps the declarations required for urlification safe from
|
ziv@2216
|
436 [MonoShake]. The dummy call is removed during [Sqlcache]. *)
|
ziv@2216
|
437 (* TODO: thread a [Monoize.Fm.t] through this module. *)
|
ziv@2216
|
438 (ESeq ((EFfiApp ("Sqlcache",
|
ziv@2216
|
439 "dummy",
|
ziv@2216
|
440 [(urlifiedRel0, stringTyp)]),
|
ziv@2216
|
441 loc),
|
ziv@2213
|
442 (ERel 0, loc)),
|
ziv@2213
|
443 loc)),
|
ziv@2213
|
444 loc)
|
ziv@2213
|
445 end
|
ziv@2213
|
446 | _ => raise Match
|
ziv@2213
|
447 in
|
ziv@2213
|
448 iq
|
ziv@2213
|
449 end
|
ziv@2213
|
450
|
ziv@2216
|
451 fun cacheWrap (query, i, urlifiedRel0, args) =
|
ziv@2213
|
452 case query of
|
ziv@2213
|
453 (EQuery {state = typ, ...}, _) =>
|
ziv@2213
|
454 let
|
ziv@2216
|
455 val () = ffiInfo := {index = i, params = length args} :: !ffiInfo
|
ziv@2213
|
456 val loc = ErrorMsg.dummySpan
|
ziv@2215
|
457 (* We ensure before this step that all arguments aren't effectful.
|
ziv@2215
|
458 by turning them into local variables as needed. *)
|
ziv@2216
|
459 val argTyps = map (fn e => (e, stringTyp)) args
|
ziv@2216
|
460 val argTypsInc = map (fn (e, typ) => (incRels 1 e, typ)) argTyps
|
ziv@2216
|
461 val check = ffiAppCache ("check", i, argTyps)
|
ziv@2216
|
462 val store = ffiAppCache ("store", i, (urlifiedRel0, stringTyp) :: argTypsInc)
|
ziv@2215
|
463 val rel0 = (ERel 0, loc)
|
ziv@2213
|
464 in
|
ziv@2215
|
465 (ECase (check,
|
ziv@2213
|
466 [((PNone stringTyp, loc),
|
ziv@2215
|
467 (ELet ("q", typ, query, (ESeq (store, rel0), loc)), loc)),
|
ziv@2213
|
468 ((PSome (stringTyp, (PVar ("hit", stringTyp), loc)), loc),
|
ziv@2215
|
469 (* Boolean is false because we're not unurlifying from a cookie. *)
|
ziv@2215
|
470 (EUnurlify (rel0, typ, false), loc))],
|
ziv@2213
|
471 {disc = stringTyp, result = typ}),
|
ziv@2213
|
472 loc)
|
ziv@2213
|
473 end
|
ziv@2213
|
474 | _ => raise Match
|
ziv@2213
|
475
|
ziv@2213
|
476 fun fileMapfold doExp file start =
|
ziv@2213
|
477 case MonoUtil.File.mapfold {typ = Search.return2,
|
ziv@2213
|
478 exp = fn x => (fn s => Search.Continue (doExp x s)),
|
ziv@2213
|
479 decl = Search.return2} file start of
|
ziv@2213
|
480 Search.Continue x => x
|
ziv@2213
|
481 | Search.Return _ => raise Match
|
ziv@2213
|
482
|
ziv@2213
|
483 fun fileMap doExp file = #1 (fileMapfold (fn x => fn _ => (doExp x, ())) file ())
|
ziv@2213
|
484
|
ziv@2215
|
485 fun addChecking file =
|
ziv@2213
|
486 let
|
ziv@2218
|
487 fun doExp (queryInfo as (tableToIndices, indexToQuery)) =
|
ziv@2215
|
488 fn e' as ELet (v, t,
|
ziv@2215
|
489 queryExp' as (EQuery {query = origQueryText,
|
ziv@2215
|
490 initial, body, state, tables, exps}, queryLoc),
|
ziv@2215
|
491 letBody) =>
|
ziv@2213
|
492 let
|
ziv@2215
|
493 val loc = ErrorMsg.dummySpan
|
ziv@2216
|
494 val chunks = Sql.chunkify origQueryText
|
ziv@2215
|
495 fun strcat (e1, e2) = (EStrcat (e1, e2), loc)
|
ziv@2215
|
496 val (newQueryText, newVariables) =
|
ziv@2215
|
497 (* Important that this is foldr (to oppose foldl below). *)
|
ziv@2215
|
498 List.foldr
|
ziv@2215
|
499 (fn (chunk, (qText, newVars)) =>
|
ziv@2216
|
500 (* Variable bound to the head of newBs will have the lowest index. *)
|
ziv@2215
|
501 case chunk of
|
ziv@2216
|
502 Sql.Exp (e as (EPrim _, _)) => (strcat (e, qText), newVars)
|
ziv@2216
|
503 | Sql.Exp e =>
|
ziv@2215
|
504 let
|
ziv@2215
|
505 val n = length newVars
|
ziv@2215
|
506 in
|
ziv@2215
|
507 (* This is the (n + 1)th new variable, so
|
ziv@2215
|
508 there are already n new variables bound,
|
ziv@2215
|
509 so we increment indices by n. *)
|
ziv@2215
|
510 (strcat ((ERel (~(n+1)), loc), qText), incRels n e :: newVars)
|
ziv@2215
|
511 end
|
ziv@2216
|
512 | Sql.String s => (strcat (stringExp s, qText), newVars))
|
ziv@2215
|
513 (stringExp "", [])
|
ziv@2215
|
514 chunks
|
ziv@2215
|
515 fun wrapLets e' =
|
ziv@2215
|
516 (* Important that this is foldl (to oppose foldr above). *)
|
ziv@2216
|
517 List.foldl (fn (v, e') => ELet ("sqlArgz", stringTyp, v, (e', loc)))
|
ziv@2216
|
518 e'
|
ziv@2216
|
519 newVariables
|
ziv@2216
|
520 val numArgs = length newVariables
|
ziv@2215
|
521 (* Increment once for each new variable just made. *)
|
ziv@2215
|
522 val queryExp = incRels (length newVariables)
|
ziv@2215
|
523 (EQuery {query = newQueryText,
|
ziv@2215
|
524 initial = initial,
|
ziv@2215
|
525 body = body,
|
ziv@2215
|
526 state = state,
|
ziv@2215
|
527 tables = tables,
|
ziv@2215
|
528 exps = exps},
|
ziv@2215
|
529 queryLoc)
|
ziv@2215
|
530 val (EQuery {query = queryText, ...}, _) = queryExp
|
ziv@2218
|
531 val () = Print.preface ("sqlcache> ", (MonoPrint.p_exp MonoEnv.empty queryText));
|
ziv@2216
|
532 val args = List.tabulate (numArgs, fn n => (ERel n, loc))
|
ziv@2213
|
533 fun bind x f = Option.mapPartial f x
|
ziv@2215
|
534 fun guard b x = if b then x else NONE
|
ziv@2215
|
535 (* DEBUG: set first boolean argument to true to turn on printing. *)
|
ziv@2215
|
536 fun safe bound = not o effectful true (effectfulMap file) false bound
|
ziv@2213
|
537 val attempt =
|
ziv@2213
|
538 (* Ziv misses Haskell's do notation.... *)
|
ziv@2215
|
539 guard (safe 0 queryText andalso safe 0 initial andalso safe 2 body) (
|
ziv@2216
|
540 bind (Sql.parse Sql.query queryText) (fn queryParsed =>
|
ziv@2218
|
541 bind (indexOfName v) (fn index =>
|
ziv@2218
|
542 bind (IM.find (!urlifiedRel0s, index)) (fn urlifiedRel0 =>
|
ziv@2215
|
543 SOME (wrapLets (ELet (v, t,
|
ziv@2218
|
544 cacheWrap (queryExp, index, urlifiedRel0, args),
|
ziv@2215
|
545 incRelsBound 1 (length newVariables) letBody)),
|
ziv@2218
|
546 (SS.foldr (fn (tab, qi) => SIMM.insert (qi, tab, index))
|
ziv@2218
|
547 tableToIndices
|
ziv@2218
|
548 (tablesQuery queryParsed),
|
ziv@2218
|
549 IM.insert (indexToQuery, index, (queryParsed, numArgs))))))))
|
ziv@2213
|
550 in
|
ziv@2213
|
551 case attempt of
|
ziv@2213
|
552 SOME pair => pair
|
ziv@2213
|
553 | NONE => (e', queryInfo)
|
ziv@2213
|
554 end
|
ziv@2213
|
555 | ESeq ((EFfiApp ("Sqlcache", "dummy", _), _), (e', _)) => (e', queryInfo)
|
ziv@2213
|
556 | e' => (e', queryInfo)
|
ziv@2213
|
557 in
|
ziv@2218
|
558 fileMapfold (fn exp => fn state => doExp state exp) file (SIMM.empty, IM.empty)
|
ziv@2213
|
559 end
|
ziv@2213
|
560
|
ziv@2218
|
561 val gunk' : (((Sql.cmp * Sql.sqexp * Sql.sqexp) formula)
|
ziv@2218
|
562 * ((Sql.cmp * Sql.sqexp * Sql.sqexp) formula)) list ref = ref []
|
ziv@2218
|
563
|
ziv@2216
|
564 fun invalidations (nQueryArgs, query, dml) =
|
ziv@2216
|
565 let
|
ziv@2216
|
566 val loc = ErrorMsg.dummySpan
|
ziv@2218
|
567 val optionAtomExpToExp =
|
ziv@2216
|
568 fn NONE => (ENone stringTyp, loc)
|
ziv@2218
|
569 | SOME e => (ESome (stringTyp,
|
ziv@2218
|
570 (case e of
|
ziv@2218
|
571 DmlRel n => ERel n
|
ziv@2218
|
572 | Prim p => EPrim p
|
ziv@2218
|
573 (* TODO: make new type containing only these two. *)
|
ziv@2218
|
574 | _ => raise Match,
|
ziv@2218
|
575 loc)),
|
ziv@2218
|
576 loc)
|
ziv@2216
|
577 fun eqsToInvalidation eqs =
|
ziv@2216
|
578 let
|
ziv@2218
|
579 fun inv n = if n < 0 then [] else IM.find (eqs, n) :: inv (n - 1)
|
ziv@2216
|
580 in
|
ziv@2216
|
581 inv (nQueryArgs - 1)
|
ziv@2216
|
582 end
|
ziv@2218
|
583 (* *)
|
ziv@2218
|
584 val rec madeRedundantBy : atomExp option list * atomExp option list -> bool =
|
ziv@2218
|
585 fn ([], []) => true
|
ziv@2218
|
586 | (NONE :: xs, _ :: ys) => madeRedundantBy (xs, ys)
|
ziv@2218
|
587 | (SOME x :: xs, SOME y :: ys) => equalAtomExp (x, y) andalso madeRedundantBy (xs, ys)
|
ziv@2218
|
588 | _ => false
|
ziv@2218
|
589 fun removeRedundant' (xss, yss) =
|
ziv@2218
|
590 case xss of
|
ziv@2218
|
591 [] => yss
|
ziv@2218
|
592 | xs :: xss' =>
|
ziv@2218
|
593 removeRedundant' (xss',
|
ziv@2218
|
594 if List.exists (fn ys => madeRedundantBy (xs, ys)) (xss' @ yss)
|
ziv@2218
|
595 then yss
|
ziv@2218
|
596 else xs :: yss)
|
ziv@2218
|
597 fun removeRedundant xss = removeRedundant' (xss, [])
|
ziv@2218
|
598 val eqss = conflictMaps (queryToFormula query, dmlToFormula dml)
|
ziv@2216
|
599 in
|
ziv@2218
|
600 gunk' := (queryToFormula query, dmlToFormula dml) :: !gunk';
|
ziv@2218
|
601 (map (map optionAtomExpToExp) o removeRedundant o map eqsToInvalidation) eqss
|
ziv@2216
|
602 end
|
ziv@2216
|
603
|
ziv@2218
|
604 val gunk : Mono.exp list list list ref = ref []
|
ziv@2218
|
605
|
ziv@2218
|
606 fun addFlushing (file, queryInfo as (tableToIndices, indexToQuery)) =
|
ziv@2213
|
607 let
|
ziv@2218
|
608 val allIndices = SM.foldr (fn (x, acc) => IS.listItems x @ acc) [] tableToIndices
|
ziv@2218
|
609 val flushes = map (fn i => ffiAppCache' ("flush", i, []))
|
ziv@2213
|
610 val doExp =
|
ziv@2213
|
611 fn dmlExp as EDml (dmlText, _) =>
|
ziv@2213
|
612 let
|
ziv@2213
|
613 val indices =
|
ziv@2216
|
614 case Sql.parse Sql.dml dmlText of
|
ziv@2218
|
615 SOME dmlParsed =>
|
ziv@2218
|
616 map (fn i => ((case IM.find (indexToQuery, i) of
|
ziv@2218
|
617 NONE => ()
|
ziv@2218
|
618 | SOME (queryParsed, numArgs) =>
|
ziv@2218
|
619 gunk := invalidations (numArgs, queryParsed, dmlParsed) :: !gunk);
|
ziv@2218
|
620 i)) (SIMM.findList (tableToIndices, tableDml dmlParsed))
|
ziv@2213
|
621 | NONE => allIndices
|
ziv@2213
|
622 in
|
ziv@2213
|
623 sequence (flushes indices @ [dmlExp])
|
ziv@2213
|
624 end
|
ziv@2213
|
625 | e' => e'
|
ziv@2213
|
626 in
|
ziv@2213
|
627 fileMap doExp file
|
ziv@2213
|
628 end
|
ziv@2213
|
629
|
ziv@2213
|
630 fun go file =
|
ziv@2213
|
631 let
|
ziv@2213
|
632 val () = Sql.sqlcacheMode := true
|
ziv@2215
|
633 val file' = addFlushing (addChecking file)
|
ziv@2215
|
634 val () = Sql.sqlcacheMode := false
|
ziv@2213
|
635 in
|
ziv@2215
|
636 file'
|
ziv@2213
|
637 end
|
ziv@2213
|
638
|
ziv@2209
|
639 end
|