annotate src/sqlcache.sml @ 2249:c05851bf7861

Merge.
author Ziv Scully <ziv@mit.edu>
date Sat, 12 Sep 2015 17:11:33 -0400
parents e09c3dc102ef
children c275bbc41194
rev   line source
ziv@2235 1 structure Sqlcache (* DEBUG: add back :> 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@2227 15 fun resetFfiInfo () = ffiInfo := []
ziv@2227 16
ziv@2213 17 fun getFfiInfo () = !ffiInfo
ziv@2213 18
ziv@2215 19 (* Some FFIs have writing as their only effect, which the caching records. *)
ziv@2215 20 val ffiEffectful =
ziv@2223 21 (* ASK: how can this be less hard-coded? *)
ziv@2215 22 let
ziv@2215 23 val fs = SS.fromList ["htmlifyInt_w",
ziv@2215 24 "htmlifyFloat_w",
ziv@2215 25 "htmlifyString_w",
ziv@2215 26 "htmlifyBool_w",
ziv@2215 27 "htmlifyTime_w",
ziv@2215 28 "attrifyInt_w",
ziv@2215 29 "attrifyFloat_w",
ziv@2215 30 "attrifyString_w",
ziv@2215 31 "attrifyChar_w",
ziv@2215 32 "urlifyInt_w",
ziv@2215 33 "urlifyFloat_w",
ziv@2215 34 "urlifyString_w",
ziv@2215 35 "urlifyBool_w",
ziv@2215 36 "urlifyChannel_w"]
ziv@2215 37 in
ziv@2215 38 fn (m, f) => Settings.isEffectful (m, f)
ziv@2215 39 andalso not (m = "Basis" andalso SS.member (fs, f))
ziv@2215 40 end
ziv@2215 41
ziv@2234 42 val cache = ref LruCache.cache
ziv@2233 43 fun setCache c = cache := c
ziv@2233 44 fun getCache () = !cache
ziv@2233 45
ziv@2248 46 (* Used to have type context for local variables in MonoUtil functions. *)
ziv@2248 47 val doBind =
ziv@2248 48 fn (ctx, MonoUtil.Exp.RelE (_, t)) => t :: ctx
ziv@2248 49 | (ctx, _) => ctx
ziv@2215 50
ziv@2248 51
ziv@2248 52 (*******************)
ziv@2248 53 (* Effect Analysis *)
ziv@2248 54 (*******************)
ziv@2215 55
ziv@2216 56 (* Makes an exception for [EWrite] (which is recorded when caching). *)
ziv@2248 57 fun effectful (effs : IS.set) =
ziv@2215 58 let
ziv@2248 59 val isFunction =
ziv@2248 60 fn (TFun _, _) => true
ziv@2248 61 | _ => false
ziv@2248 62 fun doExp (ctx, e) =
ziv@2248 63 case e of
ziv@2248 64 EPrim _ => false
ziv@2248 65 (* For now: variables of function type might be effectful, but
ziv@2248 66 others are fully evaluated and are therefore not effectful. *)
ziv@2248 67 | ERel n => isFunction (List.nth (ctx, n))
ziv@2248 68 | ENamed n => IS.member (effs, n)
ziv@2248 69 | EFfi (m, f) => ffiEffectful (m, f)
ziv@2248 70 | EFfiApp (m, f, _) => ffiEffectful (m, f)
ziv@2248 71 (* These aren't effectful unless a subexpression is. *)
ziv@2248 72 | ECon _ => false
ziv@2248 73 | ENone _ => false
ziv@2248 74 | ESome _ => false
ziv@2248 75 | EApp _ => false
ziv@2248 76 | EAbs _ => false
ziv@2248 77 | EUnop _ => false
ziv@2248 78 | EBinop _ => false
ziv@2248 79 | ERecord _ => false
ziv@2248 80 | EField _ => false
ziv@2248 81 | ECase _ => false
ziv@2248 82 | EStrcat _ => false
ziv@2248 83 (* EWrite is a special exception because we record writes when caching. *)
ziv@2248 84 | EWrite _ => false
ziv@2248 85 | ESeq _ => false
ziv@2248 86 | ELet _ => false
ziv@2248 87 (* ASK: what should we do about closures? *)
ziv@2248 88 | EClosure _ => false
ziv@2248 89 | EUnurlify _ => false
ziv@2248 90 (* Everything else is some sort of effect. We could flip this and
ziv@2248 91 explicitly list bits of Mono that are effectful, but this is
ziv@2248 92 conservatively robust to future changes (however unlikely). *)
ziv@2248 93 | _ => true
ziv@2215 94 in
ziv@2248 95 MonoUtil.Exp.existsB {typ = fn _ => false, exp = doExp, bind = doBind}
ziv@2215 96 end
ziv@2215 97
ziv@2215 98 (* TODO: test this. *)
ziv@2248 99 fun effectfulDecls (decls, _) =
ziv@2215 100 let
ziv@2248 101 fun doVal ((_, name, _, e, _), effs) =
ziv@2248 102 if effectful effs [] e
ziv@2248 103 then IS.add (effs, name)
ziv@2248 104 else effs
ziv@2215 105 val doDecl =
ziv@2248 106 fn ((DVal v, _), effs) => doVal (v, effs)
ziv@2248 107 (* Repeat the list of declarations a number of times equal to its size,
ziv@2248 108 making sure effectfulness propagates everywhere it should. This is
ziv@2248 109 analagous to the Bellman-Ford algorithm. *)
ziv@2248 110 | ((DValRec vs, _), effs) =>
ziv@2248 111 List.foldl doVal effs (List.concat (List.map (fn _ => vs) vs))
ziv@2215 112 (* ASK: any other cases? *)
ziv@2248 113 | (_, effs) => effs
ziv@2215 114 in
ziv@2248 115 List.foldl doDecl IS.empty decls
ziv@2215 116 end
ziv@2215 117
ziv@2215 118
ziv@2248 119 (*********************************)
ziv@2248 120 (* Boolean Formula Normalization *)
ziv@2248 121 (*********************************)
ziv@2216 122
ziv@2234 123 datatype junctionType = Conj | Disj
ziv@2216 124
ziv@2216 125 datatype 'atom formula =
ziv@2216 126 Atom of 'atom
ziv@2216 127 | Negate of 'atom formula
ziv@2234 128 | Combo of junctionType * 'atom formula list
ziv@2216 129
ziv@2243 130 (* Guaranteed to have all negation pushed to the atoms. *)
ziv@2243 131 datatype 'atom formula' =
ziv@2243 132 Atom' of 'atom
ziv@2243 133 | Combo' of junctionType * 'atom formula' list
ziv@2243 134
ziv@2234 135 val flipJt = fn Conj => Disj | Disj => Conj
ziv@2216 136
ziv@2236 137 fun concatMap f xs = List.concat (map f xs)
ziv@2216 138
ziv@2216 139 val rec cartesianProduct : 'a list list -> 'a list list =
ziv@2216 140 fn [] => [[]]
ziv@2236 141 | (xs :: xss) => concatMap (fn ys => concatMap (fn x => [x :: ys]) xs)
ziv@2236 142 (cartesianProduct xss)
ziv@2216 143
ziv@2218 144 (* Pushes all negation to the atoms.*)
ziv@2244 145 fun pushNegate (normalizeAtom : bool * 'atom -> 'atom) (negating : bool) =
ziv@2244 146 fn Atom x => Atom' (normalizeAtom (negating, x))
ziv@2244 147 | Negate f => pushNegate normalizeAtom (not negating) f
ziv@2244 148 | Combo (j, fs) => Combo' (if negating then flipJt j else j,
ziv@2244 149 map (pushNegate normalizeAtom negating) fs)
ziv@2218 150
ziv@2218 151 val rec flatten =
ziv@2243 152 fn Combo' (_, [f]) => flatten f
ziv@2243 153 | Combo' (j, fs) =>
ziv@2243 154 Combo' (j, List.foldr (fn (f, acc) =>
ziv@2243 155 case f of
ziv@2243 156 Combo' (j', fs') =>
ziv@2243 157 if j = j' orelse length fs' = 1
ziv@2243 158 then fs' @ acc
ziv@2243 159 else f :: acc
ziv@2243 160 | _ => f :: acc)
ziv@2243 161 []
ziv@2243 162 (map flatten fs))
ziv@2218 163 | f => f
ziv@2218 164
ziv@2243 165 (* [simplify] operates on the desired normal form. E.g., if [junc] is [Disj],
ziv@2243 166 consider the list of lists to be a disjunction of conjunctions. *)
ziv@2237 167 fun normalize' (simplify : 'a list list -> 'a list list)
ziv@2235 168 (junc : junctionType) =
ziv@2216 169 let
ziv@2235 170 fun norm junc =
ziv@2237 171 simplify
ziv@2243 172 o (fn Atom' x => [[x]]
ziv@2243 173 | Combo' (j, fs) =>
ziv@2235 174 let
ziv@2236 175 val fss = map (norm junc) fs
ziv@2235 176 in
ziv@2236 177 if j = junc
ziv@2236 178 then List.concat fss
ziv@2236 179 else map List.concat (cartesianProduct fss)
ziv@2235 180 end)
ziv@2216 181 in
ziv@2235 182 norm junc
ziv@2216 183 end
ziv@2216 184
ziv@2244 185 fun normalize simplify normalizeAtom junc =
ziv@2243 186 normalize' simplify junc
ziv@2235 187 o flatten
ziv@2244 188 o pushNegate normalizeAtom false
ziv@2216 189
ziv@2221 190 fun mapFormula mf =
ziv@2221 191 fn Atom x => Atom (mf x)
ziv@2221 192 | Negate f => Negate (mapFormula mf f)
ziv@2235 193 | Combo (j, fs) => Combo (j, map (mapFormula mf) fs)
ziv@2216 194
ziv@2230 195
ziv@2248 196 (****************)
ziv@2248 197 (* SQL Analysis *)
ziv@2248 198 (****************)
ziv@2213 199
ziv@2240 200 structure CmpKey = struct
ziv@2235 201
ziv@2235 202 type ord_key = Sql.cmp
ziv@2235 203
ziv@2235 204 val compare =
ziv@2235 205 fn (Sql.Eq, Sql.Eq) => EQUAL
ziv@2235 206 | (Sql.Eq, _) => LESS
ziv@2235 207 | (_, Sql.Eq) => GREATER
ziv@2235 208 | (Sql.Ne, Sql.Ne) => EQUAL
ziv@2235 209 | (Sql.Ne, _) => LESS
ziv@2235 210 | (_, Sql.Ne) => GREATER
ziv@2235 211 | (Sql.Lt, Sql.Lt) => EQUAL
ziv@2235 212 | (Sql.Lt, _) => LESS
ziv@2235 213 | (_, Sql.Lt) => GREATER
ziv@2235 214 | (Sql.Le, Sql.Le) => EQUAL
ziv@2235 215 | (Sql.Le, _) => LESS
ziv@2235 216 | (_, Sql.Le) => GREATER
ziv@2235 217 | (Sql.Gt, Sql.Gt) => EQUAL
ziv@2235 218 | (Sql.Gt, _) => LESS
ziv@2235 219 | (_, Sql.Gt) => GREATER
ziv@2235 220 | (Sql.Ge, Sql.Ge) => EQUAL
ziv@2235 221
ziv@2235 222 end
ziv@2235 223
ziv@2216 224 val rec chooseTwos : 'a list -> ('a * 'a) list =
ziv@2216 225 fn [] => []
ziv@2216 226 | x :: ys => map (fn y => (x, y)) ys @ chooseTwos ys
ziv@2213 227
ziv@2237 228 fun removeRedundant madeRedundantBy zs =
ziv@2237 229 let
ziv@2237 230 fun removeRedundant' (xs, ys) =
ziv@2237 231 case xs of
ziv@2237 232 [] => ys
ziv@2237 233 | x :: xs' =>
ziv@2237 234 removeRedundant' (xs',
ziv@2237 235 if List.exists (fn y => madeRedundantBy (x, y)) (xs' @ ys)
ziv@2237 236 then ys
ziv@2237 237 else x :: ys)
ziv@2237 238 in
ziv@2237 239 removeRedundant' (zs, [])
ziv@2237 240 end
ziv@2237 241
ziv@2216 242 datatype atomExp =
ziv@2216 243 QueryArg of int
ziv@2216 244 | DmlRel of int
ziv@2216 245 | Prim of Prim.t
ziv@2216 246 | Field of string * string
ziv@2216 247
ziv@2216 248 structure AtomExpKey : ORD_KEY = struct
ziv@2216 249
ziv@2234 250 type ord_key = atomExp
ziv@2216 251
ziv@2234 252 val compare =
ziv@2234 253 fn (QueryArg n1, QueryArg n2) => Int.compare (n1, n2)
ziv@2234 254 | (QueryArg _, _) => LESS
ziv@2234 255 | (_, QueryArg _) => GREATER
ziv@2234 256 | (DmlRel n1, DmlRel n2) => Int.compare (n1, n2)
ziv@2234 257 | (DmlRel _, _) => LESS
ziv@2234 258 | (_, DmlRel _) => GREATER
ziv@2234 259 | (Prim p1, Prim p2) => Prim.compare (p1, p2)
ziv@2234 260 | (Prim _, _) => LESS
ziv@2234 261 | (_, Prim _) => GREATER
ziv@2234 262 | (Field (t1, f1), Field (t2, f2)) =>
ziv@2234 263 case String.compare (t1, t2) of
ziv@2234 264 EQUAL => String.compare (f1, f2)
ziv@2234 265 | ord => ord
ziv@2216 266
ziv@2216 267 end
ziv@2216 268
ziv@2244 269 structure AtomOptionKey = OptionKeyFn(AtomExpKey)
ziv@2244 270
ziv@2216 271 structure UF = UnionFindFn(AtomExpKey)
ziv@2234 272
ziv@2235 273 structure ConflictMaps = struct
ziv@2235 274
ziv@2235 275 structure TK = TripleKeyFn(structure I = CmpKey
ziv@2244 276 structure J = AtomOptionKey
ziv@2244 277 structure K = AtomOptionKey)
ziv@2244 278 structure TS : ORD_SET = BinarySetFn(TK)
ziv@2235 279
ziv@2235 280 val toKnownEquality =
ziv@2235 281 (* [NONE] here means unkown. Anything that isn't a comparison between two
ziv@2235 282 knowns shouldn't be used, and simply dropping unused terms is okay in
ziv@2235 283 disjunctive normal form. *)
ziv@2235 284 fn (Sql.Eq, SOME e1, SOME e2) => SOME (e1, e2)
ziv@2235 285 | _ => NONE
ziv@2235 286
ziv@2235 287 val equivClasses : (Sql.cmp * atomExp option * atomExp option) list -> atomExp list list =
ziv@2235 288 UF.classes
ziv@2235 289 o List.foldl UF.union' UF.empty
ziv@2235 290 o List.mapPartial toKnownEquality
ziv@2235 291
ziv@2235 292 fun addToEqs (eqs, n, e) =
ziv@2235 293 case IM.find (eqs, n) of
ziv@2235 294 (* Comparing to a constant is probably better than comparing to a
ziv@2235 295 variable? Checking that existing constants match a new ones is
ziv@2235 296 handled by [accumulateEqs]. *)
ziv@2235 297 SOME (Prim _) => eqs
ziv@2235 298 | _ => IM.insert (eqs, n, e)
ziv@2235 299
ziv@2235 300 val accumulateEqs =
ziv@2235 301 (* [NONE] means we have a contradiction. *)
ziv@2235 302 fn (_, NONE) => NONE
ziv@2235 303 | ((Prim p1, Prim p2), eqso) =>
ziv@2235 304 (case Prim.compare (p1, p2) of
ziv@2235 305 EQUAL => eqso
ziv@2235 306 | _ => NONE)
ziv@2235 307 | ((QueryArg n, Prim p), SOME eqs) => SOME (addToEqs (eqs, n, Prim p))
ziv@2235 308 | ((QueryArg n, DmlRel r), SOME eqs) => SOME (addToEqs (eqs, n, DmlRel r))
ziv@2235 309 | ((Prim p, QueryArg n), SOME eqs) => SOME (addToEqs (eqs, n, Prim p))
ziv@2235 310 | ((DmlRel r, QueryArg n), SOME eqs) => SOME (addToEqs (eqs, n, DmlRel r))
ziv@2235 311 (* TODO: deal with equalities between [DmlRel]s and [Prim]s.
ziv@2235 312 This would involve guarding the invalidation with a check for the
ziv@2235 313 relevant comparisons. *)
ziv@2235 314 | (_, eqso) => eqso
ziv@2235 315
ziv@2235 316 val eqsOfClass : atomExp list -> atomExp IM.map option =
ziv@2235 317 List.foldl accumulateEqs (SOME IM.empty)
ziv@2235 318 o chooseTwos
ziv@2235 319
ziv@2235 320 fun toAtomExps rel (cmp, e1, e2) =
ziv@2235 321 let
ziv@2235 322 val qa =
ziv@2235 323 (* Here [NONE] means unkown. *)
ziv@2235 324 fn Sql.SqConst p => SOME (Prim p)
ziv@2235 325 | Sql.Field tf => SOME (Field tf)
ziv@2235 326 | Sql.Inj (EPrim p, _) => SOME (Prim p)
ziv@2235 327 | Sql.Inj (ERel n, _) => SOME (rel n)
ziv@2235 328 (* We can't deal with anything else, e.g., CURRENT_TIMESTAMP
ziv@2235 329 becomes Sql.Unmodeled, which becomes NONE here. *)
ziv@2235 330 | _ => NONE
ziv@2235 331 in
ziv@2235 332 (cmp, qa e1, qa e2)
ziv@2235 333 end
ziv@2235 334
ziv@2244 335 val negateCmp =
ziv@2244 336 fn Sql.Eq => Sql.Ne
ziv@2244 337 | Sql.Ne => Sql.Eq
ziv@2244 338 | Sql.Lt => Sql.Ge
ziv@2244 339 | Sql.Le => Sql.Gt
ziv@2244 340 | Sql.Gt => Sql.Le
ziv@2244 341 | Sql.Ge => Sql.Lt
ziv@2244 342
ziv@2244 343 fun normalizeAtom (negating, (cmp, e1, e2)) =
ziv@2244 344 (* Restricting to Le/Lt and sorting the expressions in Eq/Ne helps with
ziv@2244 345 simplification, where we put the triples in sets. *)
ziv@2244 346 case (if negating then negateCmp cmp else cmp) of
ziv@2244 347 Sql.Eq => (case AtomOptionKey.compare (e1, e2) of
ziv@2244 348 LESS => (Sql.Eq, e2, e1)
ziv@2244 349 | _ => (Sql.Eq, e1, e2))
ziv@2244 350 | Sql.Ne => (case AtomOptionKey.compare (e1, e2) of
ziv@2244 351 LESS => (Sql.Ne, e2, e1)
ziv@2244 352 | _ => (Sql.Ne, e1, e2))
ziv@2244 353 | Sql.Lt => (Sql.Lt, e1, e2)
ziv@2244 354 | Sql.Le => (Sql.Le, e1, e2)
ziv@2244 355 | Sql.Gt => (Sql.Lt, e2, e1)
ziv@2244 356 | Sql.Ge => (Sql.Le, e2, e1)
ziv@2235 357
ziv@2235 358 val markQuery : (Sql.cmp * Sql.sqexp * Sql.sqexp) formula ->
ziv@2235 359 (Sql.cmp * atomExp option * atomExp option) formula =
ziv@2235 360 mapFormula (toAtomExps QueryArg)
ziv@2235 361
ziv@2235 362 val markDml : (Sql.cmp * Sql.sqexp * Sql.sqexp) formula ->
ziv@2235 363 (Sql.cmp * atomExp option * atomExp option) formula =
ziv@2235 364 mapFormula (toAtomExps DmlRel)
ziv@2235 365 (* No eqs should have key conflicts because no variable is in two
ziv@2235 366 equivalence classes, so the [#1] could be [#2]. *)
ziv@2235 367
ziv@2235 368 val mergeEqs : (atomExp IntBinaryMap.map option list
ziv@2235 369 -> atomExp IntBinaryMap.map option) =
ziv@2235 370 List.foldr (fn (SOME eqs, SOME acc) => SOME (IM.unionWith #1 (eqs, acc)) | _ => NONE)
ziv@2235 371 (SOME IM.empty)
ziv@2235 372
ziv@2239 373 val simplify =
ziv@2239 374 map TS.listItems
ziv@2239 375 o removeRedundant (fn (x, y) => TS.isSubset (y, x))
ziv@2239 376 o map (fn xs => TS.addList (TS.empty, xs))
ziv@2239 377
ziv@2235 378 fun dnf (fQuery, fDml) =
ziv@2244 379 normalize simplify normalizeAtom Disj (Combo (Conj, [markQuery fQuery, markDml fDml]))
ziv@2235 380
ziv@2235 381 val conflictMaps = List.mapPartial (mergeEqs o map eqsOfClass o equivClasses) o dnf
ziv@2235 382
ziv@2235 383 end
ziv@2235 384
ziv@2235 385 val conflictMaps = ConflictMaps.conflictMaps
ziv@2213 386
ziv@2216 387 val rec sqexpToFormula =
ziv@2234 388 fn Sql.SqTrue => Combo (Conj, [])
ziv@2234 389 | Sql.SqFalse => Combo (Disj, [])
ziv@2216 390 | Sql.SqNot e => Negate (sqexpToFormula e)
ziv@2216 391 | Sql.Binop (Sql.RCmp c, e1, e2) => Atom (c, e1, e2)
ziv@2234 392 | Sql.Binop (Sql.RLop l, p1, p2) => Combo (case l of Sql.And => Conj | Sql.Or => Disj,
ziv@2216 393 [sqexpToFormula p1, sqexpToFormula p2])
ziv@2216 394 (* ASK: any other sqexps that can be props? *)
ziv@2216 395 | _ => raise Match
ziv@2213 396
ziv@2218 397 fun renameTables tablePairs =
ziv@2216 398 let
ziv@2216 399 fun renameString table =
ziv@2216 400 case List.find (fn (_, t) => table = t) tablePairs of
ziv@2216 401 NONE => table
ziv@2216 402 | SOME (realTable, _) => realTable
ziv@2216 403 val renameSqexp =
ziv@2216 404 fn Sql.Field (table, field) => Sql.Field (renameString table, field)
ziv@2216 405 | e => e
ziv@2218 406 fun renameAtom (cmp, e1, e2) = (cmp, renameSqexp e1, renameSqexp e2)
ziv@2216 407 in
ziv@2218 408 mapFormula renameAtom
ziv@2216 409 end
ziv@2218 410
ziv@2218 411 val rec queryToFormula =
ziv@2234 412 fn Sql.Query1 {Where = NONE, ...} => Combo (Conj, [])
ziv@2218 413 | Sql.Query1 {From = tablePairs, Where = SOME e, ...} =>
ziv@2218 414 renameTables tablePairs (sqexpToFormula e)
ziv@2234 415 | Sql.Union (q1, q2) => Combo (Disj, [queryToFormula q1, queryToFormula q2])
ziv@2216 416
ziv@2218 417 fun valsToFormula (table, vals) =
ziv@2234 418 Combo (Conj, map (fn (field, v) => Atom (Sql.Eq, Sql.Field (table, field), v)) vals)
ziv@2218 419
ziv@2216 420 val rec dmlToFormula =
ziv@2221 421 fn Sql.Insert (table, vals) => valsToFormula (table, vals)
ziv@2218 422 | Sql.Delete (table, wher) => renameTables [(table, "T")] (sqexpToFormula wher)
ziv@2218 423 | Sql.Update (table, vals, wher) =>
ziv@2218 424 let
ziv@2221 425 val fWhere = sqexpToFormula wher
ziv@2221 426 val fVals = valsToFormula (table, vals)
ziv@2237 427 val modifiedFields = SS.addList (SS.empty, map #1 vals)
ziv@2221 428 (* TODO: don't use field name hack. *)
ziv@2221 429 val markField =
ziv@2237 430 fn e as Sql.Field (t, v) => if SS.member (modifiedFields, v)
ziv@2237 431 then Sql.Field (t, v ^ "'")
ziv@2237 432 else e
ziv@2221 433 | e => e
ziv@2221 434 val mark = mapFormula (fn (cmp, e1, e2) => (cmp, markField e1, markField e2))
ziv@2218 435 in
ziv@2218 436 renameTables [(table, "T")]
ziv@2234 437 (Combo (Disj, [Combo (Conj, [fVals, mark fWhere]),
ziv@2244 438 Combo (Conj, [mark fVals, fWhere])]))
ziv@2218 439 end
ziv@2213 440
ziv@2213 441 val rec tablesQuery =
ziv@2216 442 fn Sql.Query1 {From = tablePairs, ...} => SS.fromList (map #1 tablePairs)
ziv@2216 443 | Sql.Union (q1, q2) => SS.union (tablesQuery q1, tablesQuery q2)
ziv@2213 444
ziv@2213 445 val tableDml =
ziv@2216 446 fn Sql.Insert (tab, _) => tab
ziv@2216 447 | Sql.Delete (tab, _) => tab
ziv@2216 448 | Sql.Update (tab, _, _) => tab
ziv@2213 449
ziv@2213 450
ziv@2248 451 (***************************)
ziv@2248 452 (* Program Instrumentation *)
ziv@2248 453 (***************************)
ziv@2213 454
ziv@2234 455 val varName =
ziv@2234 456 let
ziv@2234 457 val varNumber = ref 0
ziv@2234 458 in
ziv@2234 459 fn s => (varNumber := !varNumber + 1; s ^ Int.toString (!varNumber))
ziv@2234 460 end
ziv@2234 461
ziv@2233 462 val {check, store, flush, ...} = getCache ()
ziv@2233 463
ziv@2230 464 val dummyLoc = ErrorMsg.dummySpan
ziv@2216 465
ziv@2248 466 val dummyTyp = (TRecord [], dummyLoc)
ziv@2248 467
ziv@2230 468 fun stringExp s = (EPrim (Prim.String (Prim.Normal, s)), dummyLoc)
ziv@2230 469
ziv@2230 470 val stringTyp = (TFfi ("Basis", "string"), dummyLoc)
ziv@2213 471
ziv@2213 472 val sequence =
ziv@2213 473 fn (exp :: exps) =>
ziv@2213 474 let
ziv@2230 475 val loc = dummyLoc
ziv@2213 476 in
ziv@2213 477 List.foldl (fn (e', seq) => ESeq ((seq, loc), (e', loc))) exp exps
ziv@2213 478 end
ziv@2213 479 | _ => raise Match
ziv@2213 480
ziv@2248 481 (* Always increments negative indices as a hack we use later. *)
ziv@2248 482 fun incRels inc =
ziv@2215 483 MonoUtil.Exp.mapB
ziv@2248 484 {typ = fn t' => t',
ziv@2248 485 exp = fn bound =>
ziv@2248 486 (fn ERel n => ERel (if n >= bound orelse n < 0 then n + inc else n)
ziv@2248 487 | e' => e'),
ziv@2248 488 bind = fn (bound, MonoUtil.Exp.RelE _) => bound + 1 | (bound, _) => bound}
ziv@2248 489 0
ziv@2213 490
ziv@2223 491 fun cacheWrap (query, i, urlifiedRel0, resultTyp, args) =
ziv@2213 492 let
ziv@2223 493 val () = ffiInfo := {index = i, params = length args} :: !ffiInfo
ziv@2230 494 val loc = dummyLoc
ziv@2223 495 (* We ensure before this step that all arguments aren't effectful.
ziv@2227 496 by turning them into local variables as needed. *)
ziv@2230 497 val argsInc = map (incRels 1) args
ziv@2233 498 val check = (check (i, args), dummyLoc)
ziv@2233 499 val store = (store (i, argsInc, urlifiedRel0), dummyLoc)
ziv@2223 500 val rel0 = (ERel 0, loc)
ziv@2213 501 in
ziv@2223 502 ECase (check,
ziv@2223 503 [((PNone stringTyp, loc),
ziv@2234 504 (ELet (varName "q", resultTyp, query, (ESeq (store, rel0), loc)), loc)),
ziv@2234 505 ((PSome (stringTyp, (PVar (varName "hit", stringTyp), loc)), loc),
ziv@2223 506 (* Boolean is false because we're not unurlifying from a cookie. *)
ziv@2223 507 (EUnurlify (rel0, resultTyp, false), loc))],
ziv@2223 508 {disc = stringTyp, result = resultTyp})
ziv@2213 509 end
ziv@2213 510
ziv@2213 511 fun fileMapfold doExp file start =
ziv@2248 512 case MonoUtil.File.mapfoldB
ziv@2248 513 {typ = Search.return2,
ziv@2248 514 exp = fn ctx => fn e' => fn s => Search.Continue (doExp ctx e' s),
ziv@2248 515 decl = fn _ => Search.return2,
ziv@2248 516 bind = doBind}
ziv@2248 517 [] file start of
ziv@2213 518 Search.Continue x => x
ziv@2213 519 | Search.Return _ => raise Match
ziv@2213 520
ziv@2248 521 fun fileMap doExp file = #1 (fileMapfold (fn _ => fn e => fn _ => (doExp e, ())) file ())
ziv@2213 522
ziv@2221 523 fun factorOutNontrivial text =
ziv@2221 524 let
ziv@2230 525 val loc = dummyLoc
ziv@2221 526 fun strcat (e1, e2) = (EStrcat (e1, e2), loc)
ziv@2221 527 val chunks = Sql.chunkify text
ziv@2221 528 val (newText, newVariables) =
ziv@2221 529 (* Important that this is foldr (to oppose foldl below). *)
ziv@2221 530 List.foldr
ziv@2221 531 (fn (chunk, (qText, newVars)) =>
ziv@2221 532 (* Variable bound to the head of newBs will have the lowest index. *)
ziv@2221 533 case chunk of
ziv@2221 534 Sql.Exp (e as (EPrim _, _)) => (strcat (e, qText), newVars)
ziv@2221 535 | Sql.Exp e =>
ziv@2221 536 let
ziv@2221 537 val n = length newVars
ziv@2221 538 in
ziv@2221 539 (* This is the (n + 1)th new variable, so there are
ziv@2221 540 already n new variables bound, so we increment
ziv@2221 541 indices by n. *)
ziv@2221 542 (strcat ((ERel (~(n+1)), loc), qText), incRels n e :: newVars)
ziv@2221 543 end
ziv@2221 544 | Sql.String s => (strcat (stringExp s, qText), newVars))
ziv@2221 545 (stringExp "", [])
ziv@2221 546 chunks
ziv@2221 547 fun wrapLets e' =
ziv@2221 548 (* Important that this is foldl (to oppose foldr above). *)
ziv@2234 549 List.foldl (fn (v, e') => ELet (varName "sqlArg", stringTyp, v, (e', loc)))
ziv@2221 550 e'
ziv@2221 551 newVariables
ziv@2221 552 val numArgs = length newVariables
ziv@2221 553 in
ziv@2221 554 (newText, wrapLets, numArgs)
ziv@2221 555 end
ziv@2221 556
ziv@2215 557 fun addChecking file =
ziv@2213 558 let
ziv@2248 559 fun doExp ctx (queryInfo as (tableToIndices, indexToQueryNumArgs, index)) =
ziv@2223 560 fn e' as EQuery {query = origQueryText,
ziv@2223 561 sqlcacheInfo = urlifiedRel0,
ziv@2223 562 state = resultTyp,
ziv@2223 563 initial, body, tables, exps} =>
ziv@2213 564 let
ziv@2221 565 val (newQueryText, wrapLets, numArgs) = factorOutNontrivial origQueryText
ziv@2215 566 (* Increment once for each new variable just made. *)
ziv@2221 567 val queryExp = incRels numArgs
ziv@2215 568 (EQuery {query = newQueryText,
ziv@2223 569 sqlcacheInfo = urlifiedRel0,
ziv@2223 570 state = resultTyp,
ziv@2215 571 initial = initial,
ziv@2215 572 body = body,
ziv@2215 573 tables = tables,
ziv@2223 574 exps = exps},
ziv@2230 575 dummyLoc)
ziv@2215 576 val (EQuery {query = queryText, ...}, _) = queryExp
ziv@2235 577 (* DEBUG *)
ziv@2221 578 val () = Print.preface ("sqlcache> ", (MonoPrint.p_exp MonoEnv.empty queryText))
ziv@2230 579 val args = List.tabulate (numArgs, fn n => (ERel n, dummyLoc))
ziv@2213 580 fun bind x f = Option.mapPartial f x
ziv@2215 581 fun guard b x = if b then x else NONE
ziv@2248 582 val effs = effectfulDecls file
ziv@2248 583 (* We use dummyTyp here. I think this is okay because databases
ziv@2248 584 don't store (effectful) functions, but there could be some
ziv@2248 585 corner case I missed. *)
ziv@2248 586 fun safe bound =
ziv@2248 587 not o effectful effs (List.tabulate (bound, fn _ => dummyTyp) @ ctx)
ziv@2213 588 val attempt =
ziv@2213 589 (* Ziv misses Haskell's do notation.... *)
ziv@2215 590 guard (safe 0 queryText andalso safe 0 initial andalso safe 2 body) (
ziv@2216 591 bind (Sql.parse Sql.query queryText) (fn queryParsed =>
ziv@2223 592 SOME (wrapLets (cacheWrap (queryExp, index, urlifiedRel0, resultTyp, args)),
ziv@2218 593 (SS.foldr (fn (tab, qi) => SIMM.insert (qi, tab, index))
ziv@2218 594 tableToIndices
ziv@2218 595 (tablesQuery queryParsed),
ziv@2223 596 IM.insert (indexToQueryNumArgs, index, (queryParsed, numArgs)),
ziv@2223 597 index + 1))))
ziv@2213 598 in
ziv@2213 599 case attempt of
ziv@2213 600 SOME pair => pair
ziv@2213 601 | NONE => (e', queryInfo)
ziv@2213 602 end
ziv@2213 603 | e' => (e', queryInfo)
ziv@2213 604 in
ziv@2248 605 fileMapfold (fn ctx => fn exp => fn state => doExp ctx state exp)
ziv@2248 606 file
ziv@2248 607 (SIMM.empty, IM.empty, 0)
ziv@2213 608 end
ziv@2213 609
ziv@2235 610 structure Invalidations = struct
ziv@2235 611
ziv@2235 612 val loc = dummyLoc
ziv@2235 613
ziv@2235 614 val optionAtomExpToExp =
ziv@2235 615 fn NONE => (ENone stringTyp, loc)
ziv@2235 616 | SOME e => (ESome (stringTyp,
ziv@2235 617 (case e of
ziv@2235 618 DmlRel n => ERel n
ziv@2235 619 | Prim p => EPrim p
ziv@2235 620 (* TODO: make new type containing only these two. *)
ziv@2235 621 | _ => raise Match,
ziv@2235 622 loc)),
ziv@2235 623 loc)
ziv@2235 624
ziv@2235 625 fun eqsToInvalidation numArgs eqs =
ziv@2235 626 let
ziv@2235 627 fun inv n = if n < 0 then [] else IM.find (eqs, n) :: inv (n - 1)
ziv@2235 628 in
ziv@2235 629 inv (numArgs - 1)
ziv@2235 630 end
ziv@2235 631
ziv@2235 632 (* Tests if [ys] makes [xs] a redundant cache invalidation. [NONE] here
ziv@2235 633 represents unknown, which means a wider invalidation. *)
ziv@2235 634 val rec madeRedundantBy : atomExp option list * atomExp option list -> bool =
ziv@2235 635 fn ([], []) => true
ziv@2237 636 | (_ :: xs, NONE :: ys) => madeRedundantBy (xs, ys)
ziv@2235 637 | (SOME x :: xs, SOME y :: ys) => (case AtomExpKey.compare (x, y) of
ziv@2235 638 EQUAL => madeRedundantBy (xs, ys)
ziv@2235 639 | _ => false)
ziv@2235 640 | _ => false
ziv@2235 641
ziv@2235 642 fun eqss (query, dml) = conflictMaps (queryToFormula query, dmlToFormula dml)
ziv@2235 643
ziv@2235 644 fun invalidations ((query, numArgs), dml) =
ziv@2235 645 (map (map optionAtomExpToExp)
ziv@2237 646 o removeRedundant madeRedundantBy
ziv@2235 647 o map (eqsToInvalidation numArgs)
ziv@2235 648 o eqss)
ziv@2235 649 (query, dml)
ziv@2235 650
ziv@2235 651 end
ziv@2235 652
ziv@2235 653 val invalidations = Invalidations.invalidations
ziv@2235 654
ziv@2235 655 (* DEBUG *)
ziv@2235 656 val gunk : ((Sql.query * int) * Sql.dml) list ref = ref []
ziv@2216 657
ziv@2223 658 fun addFlushing (file, (tableToIndices, indexToQueryNumArgs, _)) =
ziv@2213 659 let
ziv@2221 660 val flushes = List.concat o
ziv@2233 661 map (fn (i, argss) => map (fn args => flush (i, args)) argss)
ziv@2213 662 val doExp =
ziv@2221 663 fn EDml (origDmlText, failureMode) =>
ziv@2213 664 let
ziv@2221 665 val (newDmlText, wrapLets, numArgs) = factorOutNontrivial origDmlText
ziv@2221 666 val dmlText = incRels numArgs newDmlText
ziv@2221 667 val dmlExp = EDml (dmlText, failureMode)
ziv@2235 668 (* DEBUG *)
ziv@2221 669 val () = Print.preface ("sqlcache> ", (MonoPrint.p_exp MonoEnv.empty dmlText))
ziv@2221 670 val invs =
ziv@2216 671 case Sql.parse Sql.dml dmlText of
ziv@2218 672 SOME dmlParsed =>
ziv@2221 673 map (fn i => (case IM.find (indexToQueryNumArgs, i) of
ziv@2221 674 SOME queryNumArgs =>
ziv@2235 675 (* DEBUG *)
ziv@2235 676 (gunk := (queryNumArgs, dmlParsed) :: !gunk;
ziv@2235 677 (i, invalidations (queryNumArgs, dmlParsed)))
ziv@2221 678 (* TODO: fail more gracefully. *)
ziv@2221 679 | NONE => raise Match))
ziv@2221 680 (SIMM.findList (tableToIndices, tableDml dmlParsed))
ziv@2221 681 (* TODO: fail more gracefully. *)
ziv@2221 682 | NONE => raise Match
ziv@2213 683 in
ziv@2221 684 wrapLets (sequence (flushes invs @ [dmlExp]))
ziv@2213 685 end
ziv@2213 686 | e' => e'
ziv@2213 687 in
ziv@2235 688 (* DEBUG *)
ziv@2235 689 gunk := [];
ziv@2213 690 fileMap doExp file
ziv@2213 691 end
ziv@2213 692
ziv@2221 693 val inlineSql =
ziv@2221 694 let
ziv@2221 695 val doExp =
ziv@2221 696 (* TODO: EQuery, too? *)
ziv@2221 697 (* ASK: should this live in [MonoOpt]? *)
ziv@2221 698 fn EDml ((ECase (disc, cases, {disc = dTyp, ...}), loc), failureMode) =>
ziv@2221 699 let
ziv@2221 700 val newCases = map (fn (p, e) => (p, (EDml (e, failureMode), loc))) cases
ziv@2221 701 in
ziv@2221 702 ECase (disc, newCases, {disc = dTyp, result = (TRecord [], loc)})
ziv@2221 703 end
ziv@2221 704 | e => e
ziv@2221 705 in
ziv@2221 706 fileMap doExp
ziv@2221 707 end
ziv@2221 708
ziv@2213 709 fun go file =
ziv@2213 710 let
ziv@2235 711 (* TODO: do something nicer than [Sql] being in one of two modes. *)
ziv@2227 712 val () = (resetFfiInfo (); Sql.sqlcacheMode := true)
ziv@2221 713 val file' = addFlushing (addChecking (inlineSql file))
ziv@2215 714 val () = Sql.sqlcacheMode := false
ziv@2213 715 in
ziv@2221 716 file'
ziv@2213 717 end
ziv@2213 718
ziv@2209 719 end