annotate src/sql_cache.sml @ 2203:39faa4a037f4

ML half of initial prototype. (Doesn't compile because there's no C yet.)
author Ziv Scully <ziv@mit.edu>
date Tue, 25 Mar 2014 02:04:06 -0400
parents
children 01c8aceac480
rev   line source
ziv@2203 1 structure SqlCache = struct
ziv@2203 2
ziv@2203 3 open Sql
ziv@2203 4 open Mono
ziv@2203 5
ziv@2203 6 structure IS = IntBinarySet
ziv@2203 7 structure IM = IntBinaryMap
ziv@2203 8 structure StringKey = struct type ord_key = string val compare = String.compare end
ziv@2203 9 structure SS = BinarySetFn (StringKey)
ziv@2203 10 structure SM = BinaryMapFn (StringKey)
ziv@2203 11 structure SIMM = MultimapFn (structure KeyMap = SM structure ValSet = IS)
ziv@2203 12
ziv@2203 13 val rec tablesRead =
ziv@2203 14 fn Query1 {From=tablePairs, ...} => SS.fromList (map #1 tablePairs)
ziv@2203 15 | Union (q1,q2) => SS.union (tablesRead q1, tablesRead q2)
ziv@2203 16
ziv@2203 17 val tableWritten =
ziv@2203 18 fn Insert (tab, _) => tab
ziv@2203 19 | Delete (tab, _) => tab
ziv@2203 20 | Update (tab, _, _) => tab
ziv@2203 21
ziv@2203 22 fun tablesInExp' exp' =
ziv@2203 23 let
ziv@2203 24 val nothing = {read = SS.empty, written = SS.empty}
ziv@2203 25 in
ziv@2203 26 case exp' of
ziv@2203 27 EQuery {query=e, ...} =>
ziv@2203 28 (case parse query e of
ziv@2203 29 SOME q => {read = tablesRead q, written = SS.empty}
ziv@2203 30 | NONE => nothing)
ziv@2203 31 | EDml (e, _) =>
ziv@2203 32 (case parse dml e of
ziv@2203 33 SOME q => {read = SS.empty, written = SS.singleton (tableWritten q)}
ziv@2203 34 | NONE => nothing)
ziv@2203 35 | _ => nothing
ziv@2203 36 end
ziv@2203 37
ziv@2203 38 val tablesInExp =
ziv@2203 39 let
ziv@2203 40 fun addTables (exp', {read, written}) =
ziv@2203 41 let val {read = r, written = w} = tablesInExp' exp'
ziv@2203 42 in {read = SS.union (r, read), written = SS.union (w, written)} end
ziv@2203 43 in
ziv@2203 44 MonoUtil.Exp.fold {typ = #2, exp = addTables}
ziv@2203 45 {read = SS.empty, written = SS.empty}
ziv@2203 46 end
ziv@2203 47
ziv@2203 48 fun intExp (n, loc) = (EPrim (Prim.Int (Int64.fromInt n)), loc)
ziv@2203 49 fun intTyp loc = (TFfi ("Basis", "int"), loc)
ziv@2203 50 fun boolPat (b, loc) = (PCon (Enum,
ziv@2203 51 PConFfi {mod = "Basis", datatyp = "bool", arg = NONE,
ziv@2203 52 con = if b then "True" else "False"},
ziv@2203 53 NONE),
ziv@2203 54 loc)
ziv@2203 55 fun boolTyp loc = (TFfi ("Basis", "int"), loc)
ziv@2203 56
ziv@2203 57 fun ffiAppExp (module, func, arg, loc) =
ziv@2203 58 (EFfiApp (module, func, [(intExp (arg, loc), intTyp loc)]), loc)
ziv@2203 59
ziv@2203 60 fun sequence (befores, center, afters, loc) =
ziv@2203 61 List.foldr (fn (exp, seq) => (ESeq (exp, seq), loc))
ziv@2203 62 (List.foldl (fn (exp, seq) => (ESeq (seq, exp), loc))
ziv@2203 63 center
ziv@2203 64 afters)
ziv@2203 65 befores
ziv@2203 66
ziv@2203 67 fun antiguardUnit (cond, exp, loc) =
ziv@2203 68 (ECase (cond,
ziv@2203 69 [(boolPat (false, loc), exp),
ziv@2203 70 (boolPat (true, loc), (ERecord [], loc))],
ziv@2203 71 {disc = boolTyp loc, result = (TRecord [], loc)}),
ziv@2203 72 loc)
ziv@2203 73
ziv@2203 74 fun underAbs f (exp as (exp', loc)) =
ziv@2203 75 case exp' of
ziv@2203 76 EAbs (x, y, z, body) => (EAbs (x, y, z, underAbs f body), loc)
ziv@2203 77 | _ => f exp
ziv@2203 78
ziv@2203 79 fun addCacheCheck (index, exp) =
ziv@2203 80 let
ziv@2203 81 fun f (body as (_, loc)) =
ziv@2203 82 let
ziv@2203 83 val check = ffiAppExp ("Cache", "check", index, loc)
ziv@2203 84 val store = ffiAppExp ("Cache", "store", index, loc)
ziv@2203 85 in
ziv@2203 86 antiguardUnit (check, sequence ([], body, [store], loc), loc)
ziv@2203 87 end
ziv@2203 88 in
ziv@2203 89 underAbs f exp
ziv@2203 90 end
ziv@2203 91
ziv@2203 92 fun addCacheFlush (exp, tablesToIndices) =
ziv@2203 93 let
ziv@2203 94 fun addIndices (table, indices) = IS.union (indices, SIMM.find (tablesToIndices, table))
ziv@2203 95 fun f (body as (_, loc)) =
ziv@2203 96 let
ziv@2203 97 fun mapFfi func = List.map (fn i => ffiAppExp ("Cache", func, i, loc))
ziv@2203 98 val flushes =
ziv@2203 99 IS.listItems (SS.foldr addIndices IS.empty (#written (tablesInExp body)))
ziv@2203 100
ziv@2203 101 in
ziv@2203 102 sequence (mapFfi "flush" flushes, body, mapFfi "ready" flushes, loc)
ziv@2203 103 end
ziv@2203 104 in
ziv@2203 105 underAbs f exp
ziv@2203 106 end
ziv@2203 107
ziv@2203 108 val handlerIndices =
ziv@2203 109 let
ziv@2203 110 val isUnit =
ziv@2203 111 fn (TRecord [], _) => true
ziv@2203 112 | _ => false
ziv@2203 113 fun maybeAdd (d, soFar as {readers, writers}) =
ziv@2203 114 case d of
ziv@2203 115 DExport (Link ReadOnly, _, name, typs, typ, _) =>
ziv@2203 116 if List.all isUnit (typ::typs)
ziv@2203 117 then {readers = IS.add (readers, name), writers = writers}
ziv@2203 118 else soFar
ziv@2203 119 | DExport (_, _, name, _, _, _) => (* Not read only. *)
ziv@2203 120 {readers = readers, writers = IS.add (writers, name)}
ziv@2203 121 | _ => soFar
ziv@2203 122 in
ziv@2203 123 MonoUtil.File.fold {typ = #2, exp = #2, decl = maybeAdd}
ziv@2203 124 {readers = IS.empty, writers = IS.empty}
ziv@2203 125 end
ziv@2203 126
ziv@2203 127 fun fileFoldMapiSelected f init (file, indices) =
ziv@2203 128 let
ziv@2203 129 fun doExp (original as ((a, index, b, exp, c), state)) =
ziv@2203 130 if IS.member (indices, index)
ziv@2203 131 then let val (newExp, newState) = f (index, exp, state)
ziv@2203 132 in ((a, index, b, newExp, c), newState) end
ziv@2203 133 else original
ziv@2203 134 fun doDecl decl state =
ziv@2203 135 let
ziv@2203 136 val result =
ziv@2203 137 case decl of
ziv@2203 138 DVal x =>
ziv@2203 139 let val (y, newState) = doExp (x, state)
ziv@2203 140 in (DVal y, newState) end
ziv@2203 141 | DValRec xs =>
ziv@2203 142 let val (ys, newState) = ListUtil.foldlMap doExp state xs
ziv@2203 143 in (DValRec ys, newState) end
ziv@2203 144 | _ => (decl, state)
ziv@2203 145 in
ziv@2203 146 Search.Continue result
ziv@2203 147 end
ziv@2203 148 fun nada x y = Search.Continue (x, y)
ziv@2203 149 in
ziv@2203 150 case MonoUtil.File.mapfold {typ = nada, exp = nada, decl = doDecl} file init of
ziv@2203 151 Search.Continue x => x
ziv@2203 152 | _ => (file, init) (* Should never happen. *)
ziv@2203 153 end
ziv@2203 154
ziv@2203 155 fun fileMapSelected f = #1 o fileFoldMapiSelected (fn (_, x, _) => (f x, ())) ()
ziv@2203 156
ziv@2203 157 val addCacheChecking =
ziv@2203 158 let
ziv@2203 159 fun f (index, exp, tablesToIndices) =
ziv@2203 160 (addCacheCheck (index, exp),
ziv@2203 161 SS.foldr (fn (table, tsToIs) => SIMM.insert (tsToIs, table, index))
ziv@2203 162 tablesToIndices
ziv@2203 163 (#read (tablesInExp exp)))
ziv@2203 164 in
ziv@2203 165 fileFoldMapiSelected f (SM.empty)
ziv@2203 166 end
ziv@2203 167
ziv@2203 168 fun addCacheFlushing (file, tablesToIndices, writers) =
ziv@2203 169 fileMapSelected (fn exp => addCacheFlush (exp, tablesToIndices)) (file, writers)
ziv@2203 170
ziv@2203 171 fun go file =
ziv@2203 172 let
ziv@2203 173 val {readers, writers} = handlerIndices file
ziv@2203 174 val (fileWithChecks, tablesToIndices) = addCacheChecking (file, readers)
ziv@2203 175 in
ziv@2203 176 addCacheFlushing (fileWithChecks, tablesToIndices, writers)
ziv@2203 177 end
ziv@2203 178
ziv@2203 179 end