ziv@2209: structure Sqlcache :> SQLCACHE = struct ziv@2209: ziv@2209: open Sql ziv@2209: open Mono ziv@2209: ziv@2209: structure IS = IntBinarySet ziv@2209: structure IM = IntBinaryMap ziv@2209: structure StringKey = struct type ord_key = string val compare = String.compare end ziv@2209: structure SS = BinarySetFn (StringKey) ziv@2209: structure SM = BinaryMapFn (StringKey) ziv@2209: structure SIMM = MultimapFn (structure KeyMap = SM structure ValSet = IS) ziv@2209: ziv@2209: val ffiIndices : int list ref = ref [] ziv@2209: ziv@2209: val rec tablesRead = ziv@2209: fn Query1 {From=tablePairs, ...} => SS.fromList (map #1 tablePairs) ziv@2209: | Union (q1,q2) => SS.union (tablesRead q1, tablesRead q2) ziv@2209: ziv@2209: val tableWritten = ziv@2209: fn Insert (tab, _) => tab ziv@2209: | Delete (tab, _) => tab ziv@2209: | Update (tab, _, _) => tab ziv@2209: ziv@2209: fun tablesInExp' exp' = ziv@2209: let ziv@2209: val nothing = {read = SS.empty, written = SS.empty} ziv@2209: in ziv@2209: case exp' of ziv@2209: EQuery {query=e, ...} => ziv@2209: (case parse query e of ziv@2209: SOME q => {read = tablesRead q, written = SS.empty} ziv@2209: | NONE => nothing) ziv@2209: | EDml (e, _) => ziv@2209: (case parse dml e of ziv@2209: SOME q => {read = SS.empty, written = SS.singleton (tableWritten q)} ziv@2209: | NONE => nothing) ziv@2209: | _ => nothing ziv@2209: end ziv@2209: ziv@2209: val tablesInExp = ziv@2209: let ziv@2209: fun addTables (exp', {read, written}) = ziv@2209: let val {read = r, written = w} = tablesInExp' exp' ziv@2209: in {read = SS.union (r, read), written = SS.union (w, written)} end ziv@2209: in ziv@2209: MonoUtil.Exp.fold {typ = #2, exp = addTables} ziv@2209: {read = SS.empty, written = SS.empty} ziv@2209: end ziv@2209: ziv@2209: fun intExp (n, loc) = (EPrim (Prim.Int (Int64.fromInt n)), loc) ziv@2209: fun intTyp loc = (TFfi ("Basis", "int"), loc) ziv@2209: fun boolPat (b, loc) = (PCon (Enum, ziv@2209: PConFfi {mod = "Basis", datatyp = "bool", arg = NONE, ziv@2209: con = if b then "True" else "False"}, ziv@2209: NONE), ziv@2209: loc) ziv@2209: fun boolTyp loc = (TFfi ("Basis", "int"), loc) ziv@2209: ziv@2209: fun ffiAppExp (module, func, index, loc) = ziv@2209: (EFfiApp (module, func ^ Int.toString index, []), loc) ziv@2209: ziv@2209: fun sequence (befores, center, afters, loc) = ziv@2209: List.foldr (fn (exp, seq) => (ESeq (exp, seq), loc)) ziv@2209: (List.foldl (fn (exp, seq) => (ESeq (seq, exp), loc)) ziv@2209: center ziv@2209: afters) ziv@2209: befores ziv@2209: ziv@2209: fun antiguardUnit (cond, exp, loc) = ziv@2209: (ECase (cond, ziv@2209: [(boolPat (false, loc), exp), ziv@2209: (boolPat (true, loc), (ERecord [], loc))], ziv@2209: {disc = boolTyp loc, result = (TRecord [], loc)}), ziv@2209: loc) ziv@2209: ziv@2209: fun underAbs f (exp as (exp', loc)) = ziv@2209: case exp' of ziv@2209: EAbs (x, y, z, body) => (EAbs (x, y, z, underAbs f body), loc) ziv@2209: | _ => f exp ziv@2209: ziv@2209: fun addCacheCheck (index, exp) = ziv@2209: let ziv@2209: fun f (body as (_, loc)) = ziv@2209: let ziv@2209: val check = ffiAppExp ("Cache", "check", index, loc) ziv@2209: val store = ffiAppExp ("Cache", "store", index, loc) ziv@2209: in ziv@2209: antiguardUnit (check, sequence ([], body, [store], loc), loc) ziv@2209: end ziv@2209: in ziv@2209: underAbs f exp ziv@2209: end ziv@2209: ziv@2209: fun addCacheFlush (exp, tablesToIndices) = ziv@2209: let ziv@2209: fun addIndices (table, indices) = IS.union (indices, SIMM.find (tablesToIndices, table)) ziv@2209: fun f (body as (_, loc)) = ziv@2209: let ziv@2209: fun mapFfi func = List.map (fn i => ffiAppExp ("Cache", func, i, loc)) ziv@2209: val flushes = ziv@2209: IS.listItems (SS.foldr addIndices IS.empty (#written (tablesInExp body))) ziv@2209: ziv@2209: in ziv@2209: sequence (mapFfi "flush" flushes, body, mapFfi "ready" flushes, loc) ziv@2209: end ziv@2209: in ziv@2209: underAbs f exp ziv@2209: end ziv@2209: ziv@2209: val handlerIndices = ziv@2209: let ziv@2209: val isUnit = ziv@2209: fn (TRecord [], _) => true ziv@2209: | _ => false ziv@2209: fun maybeAdd (d, soFar as {readers, writers}) = ziv@2209: case d of ziv@2209: DExport (Link ReadOnly, _, name, typs, typ, _) => ziv@2209: if List.all isUnit (typ::typs) ziv@2209: then {readers = IS.add (readers, name), writers = writers} ziv@2209: else soFar ziv@2209: | DExport (_, _, name, _, _, _) => (* Not read only. *) ziv@2209: {readers = readers, writers = IS.add (writers, name)} ziv@2209: | _ => soFar ziv@2209: in ziv@2209: MonoUtil.File.fold {typ = #2, exp = #2, decl = maybeAdd} ziv@2209: {readers = IS.empty, writers = IS.empty} ziv@2209: end ziv@2209: ziv@2209: fun fileFoldMapiSelected f init (file, indices) = ziv@2209: let ziv@2209: fun doExp (original as ((a, index, b, exp, c), state)) = ziv@2209: if IS.member (indices, index) ziv@2209: then let val (newExp, newState) = f (index, exp, state) ziv@2209: in ((a, index, b, newExp, c), newState) end ziv@2209: else original ziv@2209: fun doDecl decl state = ziv@2209: let ziv@2209: val result = ziv@2209: case decl of ziv@2209: DVal x => ziv@2209: let val (y, newState) = doExp (x, state) ziv@2209: in (DVal y, newState) end ziv@2209: | DValRec xs => ziv@2209: let val (ys, newState) = ListUtil.foldlMap doExp state xs ziv@2209: in (DValRec ys, newState) end ziv@2209: | _ => (decl, state) ziv@2209: in ziv@2209: Search.Continue result ziv@2209: end ziv@2209: fun nada x y = Search.Continue (x, y) ziv@2209: in ziv@2209: case MonoUtil.File.mapfold {typ = nada, exp = nada, decl = doDecl} file init of ziv@2209: Search.Continue x => x ziv@2209: | _ => (file, init) (* Should never happen. *) ziv@2209: end ziv@2209: ziv@2209: fun fileMapSelected f = #1 o fileFoldMapiSelected (fn (_, x, _) => (f x, ())) () ziv@2209: ziv@2209: val addCacheChecking = ziv@2209: let ziv@2209: fun f (index, exp, tablesToIndices) = ziv@2209: (addCacheCheck (index, exp), ziv@2209: SS.foldr (fn (table, tsToIs) => SIMM.insert (tsToIs, table, index)) ziv@2209: tablesToIndices ziv@2209: (#read (tablesInExp exp))) ziv@2209: in ziv@2209: fileFoldMapiSelected f (SM.empty) ziv@2209: end ziv@2209: ziv@2209: fun addCacheFlushing (file, tablesToIndices, writers) = ziv@2209: fileMapSelected (fn exp => addCacheFlush (exp, tablesToIndices)) (file, writers) ziv@2209: ziv@2209: fun go file = ziv@2209: let ziv@2209: val {readers, writers} = handlerIndices file ziv@2209: val (fileWithChecks, tablesToIndices) = addCacheChecking (file, readers) ziv@2209: in ziv@2209: ffiIndices := IS.listItems readers; ziv@2209: addCacheFlushing (fileWithChecks, tablesToIndices, writers) ziv@2209: end ziv@2209: ziv@2209: end