annotate src/toy_cache.sml @ 2231:67e801cf42c6

Add missing file.
author Ziv Scully <ziv@mit.edu>
date Wed, 06 May 2015 14:50:55 -0400
parents
children af1585e7d645
rev   line source
ziv@2231 1 structure ToyCache = struct
ziv@2231 2
ziv@2231 3 (* Mono *)
ziv@2231 4
ziv@2231 5 open Mono
ziv@2231 6
ziv@2231 7 val dummyLoc = ErrorMsg.dummySpan
ziv@2231 8 val stringTyp = (TFfi ("Basis", "string"), dummyLoc)
ziv@2231 9 val optionStringTyp = (TOption stringTyp, dummyLoc)
ziv@2231 10 fun withTyp typ = map (fn exp => (exp, typ))
ziv@2231 11
ziv@2231 12 fun ffiAppCache' (func, index, argTyps) =
ziv@2231 13 EFfiApp ("Sqlcache", func ^ Int.toString index, argTyps)
ziv@2231 14
ziv@2231 15 fun check (index, keys) =
ziv@2231 16 ffiAppCache' ("check", index, withTyp stringTyp keys)
ziv@2231 17
ziv@2231 18 fun store (index, keys, value) =
ziv@2231 19 ffiAppCache' ("store", index, (value, stringTyp) :: withTyp stringTyp keys)
ziv@2231 20
ziv@2231 21 fun flush (index, keys) =
ziv@2231 22 ffiAppCache' ("flush", index, withTyp optionStringTyp keys)
ziv@2231 23
ziv@2231 24
ziv@2231 25 (* Cjr *)
ziv@2231 26
ziv@2231 27 open Print
ziv@2231 28 open Print.PD
ziv@2231 29
ziv@2231 30 fun setupQuery {index, params} =
ziv@2231 31 let
ziv@2231 32
ziv@2231 33 val i = Int.toString index
ziv@2231 34
ziv@2231 35 fun paramRepeat itemi sep =
ziv@2231 36 let
ziv@2231 37 fun f n =
ziv@2231 38 if n < 0 then ""
ziv@2231 39 else if n = 0 then itemi (Int.toString 0)
ziv@2231 40 else f (n-1) ^ sep ^ itemi (Int.toString n)
ziv@2231 41 in
ziv@2231 42 f (params - 1)
ziv@2231 43 end
ziv@2231 44
ziv@2231 45 fun paramRepeatInit itemi sep =
ziv@2231 46 if params = 0 then "" else sep ^ paramRepeat itemi sep
ziv@2231 47
ziv@2231 48 val args = paramRepeatInit (fn p => "uw_Basis_string p" ^ p) ", "
ziv@2231 49
ziv@2231 50 val decls = paramRepeat (fn p => "uw_Basis_string param" ^ i ^ "_"
ziv@2231 51 ^ p ^ " = NULL;")
ziv@2231 52 "\n"
ziv@2231 53
ziv@2231 54 val sets = paramRepeat (fn p => "param" ^ i ^ "_" ^ p
ziv@2231 55 ^ " = strdup(p" ^ p ^ ");")
ziv@2231 56 "\n"
ziv@2231 57
ziv@2231 58 val frees = paramRepeat (fn p => "free(param" ^ i ^ "_" ^ p ^ ");")
ziv@2231 59 "\n"
ziv@2231 60
ziv@2231 61 val eqs = paramRepeatInit (fn p => "strcmp(param" ^ i ^ "_" ^ p
ziv@2231 62 ^ ", p" ^ p ^ ")")
ziv@2231 63 " || "
ziv@2231 64
ziv@2231 65 (* Using [!=] instead of [==] to mimic [strcmp]. *)
ziv@2231 66 val eqsNull = paramRepeatInit (fn p => "(p" ^ p ^ " == NULL || "
ziv@2231 67 ^ "!strcmp(param" ^ i ^ "_"
ziv@2231 68 ^ p ^ ", p" ^ p ^ "))")
ziv@2231 69 " && "
ziv@2231 70
ziv@2231 71 in
ziv@2231 72 Print.box
ziv@2231 73 [string "static char *cacheQuery",
ziv@2231 74 string i,
ziv@2231 75 string " = NULL;",
ziv@2231 76 newline,
ziv@2231 77 string "static char *cacheWrite",
ziv@2231 78 string i,
ziv@2231 79 string " = NULL;",
ziv@2231 80 newline,
ziv@2231 81 string decls,
ziv@2231 82 newline,
ziv@2231 83 string "static uw_Basis_string uw_Sqlcache_check",
ziv@2231 84 string i,
ziv@2231 85 string "(uw_context ctx",
ziv@2231 86 string args,
ziv@2231 87 string ") {",
ziv@2231 88 newline,
ziv@2231 89 string "if (cacheQuery",
ziv@2231 90 string i,
ziv@2231 91 (* ASK: is returning the pointer okay? Should we duplicate? *)
ziv@2231 92 string " == NULL",
ziv@2231 93 string eqs,
ziv@2231 94 string ") {",
ziv@2231 95 newline,
ziv@2231 96 string "puts(\"SQLCACHE: miss ",
ziv@2231 97 string i,
ziv@2231 98 string ".\");",
ziv@2231 99 newline,
ziv@2231 100 string "uw_recordingStart(ctx);",
ziv@2231 101 newline,
ziv@2231 102 string "return NULL;",
ziv@2231 103 newline,
ziv@2231 104 string "} else {",
ziv@2231 105 newline,
ziv@2231 106 string "puts(\"SQLCACHE: hit ",
ziv@2231 107 string i,
ziv@2231 108 string ".\");",
ziv@2231 109 newline,
ziv@2231 110 string "uw_write(ctx, cacheWrite",
ziv@2231 111 string i,
ziv@2231 112 string ");",
ziv@2231 113 newline,
ziv@2231 114 string "return cacheQuery",
ziv@2231 115 string i,
ziv@2231 116 string ";",
ziv@2231 117 newline,
ziv@2231 118 string "} };",
ziv@2231 119 newline,
ziv@2231 120 string "static uw_unit uw_Sqlcache_store",
ziv@2231 121 string i,
ziv@2231 122 string "(uw_context ctx, uw_Basis_string s",
ziv@2231 123 string args,
ziv@2231 124 string ") {",
ziv@2231 125 newline,
ziv@2231 126 string "free(cacheQuery",
ziv@2231 127 string i,
ziv@2231 128 string "); free(cacheWrite",
ziv@2231 129 string i,
ziv@2231 130 string ");",
ziv@2231 131 newline,
ziv@2231 132 string frees,
ziv@2231 133 newline,
ziv@2231 134 string "cacheQuery",
ziv@2231 135 string i,
ziv@2231 136 string " = strdup(s); cacheWrite",
ziv@2231 137 string i,
ziv@2231 138 string " = uw_recordingRead(ctx);",
ziv@2231 139 newline,
ziv@2231 140 string sets,
ziv@2231 141 newline,
ziv@2231 142 string "puts(\"SQLCACHE: store ",
ziv@2231 143 string i,
ziv@2231 144 string ".\");",
ziv@2231 145 newline,
ziv@2231 146 string "return uw_unit_v;",
ziv@2231 147 newline,
ziv@2231 148 string "};",
ziv@2231 149 newline,
ziv@2231 150 string "static uw_unit uw_Sqlcache_flush",
ziv@2231 151 string i,
ziv@2231 152 string "(uw_context ctx",
ziv@2231 153 string args,
ziv@2231 154 string ") {",
ziv@2231 155 newline,
ziv@2231 156 string "if (cacheQuery",
ziv@2231 157 string i,
ziv@2231 158 string " != NULL",
ziv@2231 159 string eqsNull,
ziv@2231 160 string ") {",
ziv@2231 161 newline,
ziv@2231 162 string "free(cacheQuery",
ziv@2231 163 string i,
ziv@2231 164 string ");",
ziv@2231 165 newline,
ziv@2231 166 string "cacheQuery",
ziv@2231 167 string i,
ziv@2231 168 string " = NULL;",
ziv@2231 169 newline,
ziv@2231 170 string "puts(\"SQLCACHE: flush ",
ziv@2231 171 string i,
ziv@2231 172 string ".\");}",
ziv@2231 173 newline,
ziv@2231 174 string "else { puts(\"SQLCACHE: keep ",
ziv@2231 175 string i,
ziv@2231 176 string ".\"); } return uw_unit_v;",
ziv@2231 177 newline,
ziv@2231 178 string "};",
ziv@2231 179 newline,
ziv@2231 180 newline]
ziv@2231 181 end
ziv@2231 182
ziv@2231 183 val setupGlobal = string "/* No global setup for toy cache. */"
ziv@2231 184
ziv@2231 185 end