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