summaryrefslogtreecommitdiff
path: root/src/toy_cache.sml
blob: cfde027b5f5180515d3800c0dad7e6b43b163430 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
structure ToyCache : sig
    val cache : Cache.cache
end = struct


(* Mono *)

open Mono

val dummyLoc = ErrorMsg.dummySpan
val stringTyp = (TFfi ("Basis", "string"), dummyLoc)
val optionStringTyp = (TOption stringTyp, dummyLoc)
fun withTyp typ = map (fn exp => (exp, typ))

fun ffiAppCache' (func, index, argTyps) =
    let
        val m = "Sqlcache"
        val f = func ^ Int.toString index
    in
        Settings.addEffectful (m, f);
        EFfiApp (m, f, argTyps)
    end

fun check (index, keys) =
    ffiAppCache' ("check", index, withTyp stringTyp keys)

fun store (index, keys, value) =
    ffiAppCache' ("store", index, (value, stringTyp) :: withTyp stringTyp keys)

fun flush (index, keys) =
    ffiAppCache' ("flush", index, withTyp optionStringTyp keys)


(* Cjr *)

open Print
open Print.PD

fun setupQuery {index, params} =
    let

        val i = Int.toString index

        fun paramRepeat itemi sep =
            let
                fun f n =
                    if n < 0 then ""
                    else if n = 0 then itemi (Int.toString 0)
                    else f (n-1) ^ sep ^ itemi (Int.toString n)
            in
                f (params - 1)
            end

        fun paramRepeatInit itemi sep =
            if params = 0 then "" else sep ^ paramRepeat itemi sep

        val args = paramRepeatInit (fn p => "uw_Basis_string p" ^ p) ", "

        val decls = paramRepeat (fn p => "uw_Basis_string param" ^ i ^ "_"
                                         ^ p ^ " = NULL;")
                                "\n"

        val sets = paramRepeat (fn p => "param" ^ i ^ "_" ^ p
                                        ^ " = strdup(p" ^ p ^ ");")
                               "\n"

        val frees = paramRepeat (fn p => "free(param" ^ i ^ "_" ^ p ^ ");")
                                "\n"

        val eqs = paramRepeatInit (fn p => "strcmp(param" ^ i ^ "_" ^ p
                                           ^ ", p" ^ p ^ ")")
                                  " || "

        (* Using [!=] instead of [==] to mimic [strcmp]. *)
        val eqsNull = paramRepeatInit (fn p => "(p" ^ p ^ " == NULL || "
                                               ^ "!strcmp(param" ^ i ^ "_"
                                               ^ p ^ ", p" ^ p ^ "))")
                                      " && "

    in
        Print.box
            [string "static char *cacheQuery",
             string i,
             string " = NULL;",
             newline,
             string "static char *cacheWrite",
             string i,
             string " = NULL;",
             newline,
             string decls,
             newline,
             string "static uw_Basis_string uw_Sqlcache_check",
             string i,
             string "(uw_context ctx",
             string args,
             string ") {",
             newline,
             string "if (cacheWrite",
             string i,
             (* ASK: is returning the pointer okay? Should we duplicate? *)
             string " == NULL",
             string eqs,
             string ") {",
             newline,
             string "puts(\"SQLCACHE: miss ",
             string i,
             string ".\");",
             newline,
             string "uw_recordingStart(ctx);",
             newline,
             string "return NULL;",
             newline,
             string "} else {",
             newline,
             string "puts(\"SQLCACHE: hit ",
             string i,
             string ".\");",
             newline,
             string " if (cacheWrite",
             string i,
             string " != NULL) { uw_write(ctx, cacheWrite",
             string i,
             string "); }",
             newline,
             string "return cacheQuery",
             string i,
             string ";",
             newline,
             string "} };",
             newline,
             string "static uw_unit uw_Sqlcache_store",
             string i,
             string "(uw_context ctx, uw_Basis_string s",
             string args,
             string ") {",
             newline,
             string "free(cacheQuery",
             string i,
             string "); free(cacheWrite",
             string i,
             string ");",
             newline,
             string frees,
             newline,
             string "cacheQuery",
             string i,
             string " = strdup(s); cacheWrite",
             string i,
             string " = uw_recordingRead(ctx);",
             newline,
             string sets,
             newline,
             string "puts(\"SQLCACHE: store ",
             string i,
             string ".\");",
             newline,
             string "return uw_unit_v;",
             newline,
             string "};",
             newline,
             string "static uw_unit uw_Sqlcache_flush",
             string i,
             string "(uw_context ctx",
             string args,
             string ") {",
             newline,
             string "if (cacheQuery",
             string i,
             string " != NULL",
             string eqsNull,
             string ") {",
             newline,
             string "free(cacheQuery",
             string i,
             string ");",
             newline,
             string "cacheQuery",
             string i,
             string " = NULL;",
             newline,
             string "free(cacheWrite",
             string i,
             string ");",
             newline,
             string "cacheWrite",
             string i,
             string " = NULL;",
             newline,
             string "puts(\"SQLCACHE: flush ",
             string i,
             string ".\");}",
             newline,
             string "else { puts(\"SQLCACHE: keep ",
             string i,
             string ".\"); } return uw_unit_v;",
             newline,
             string "};",
             newline,
             newline]
    end

val setupGlobal = string "/* No global setup for toy cache. */"


(* Bundled up. *)

val cache = {check = check, store = store, flush = flush,
             setupQuery = setupQuery, setupGlobal = setupGlobal}

end