diff src/cjr_print.sml @ 2230:a749acc51ae4

Factor out cache implementation from Sqlcache.
author Ziv Scully <ziv@mit.edu>
date Wed, 06 May 2015 14:50:29 -0400
parents e10881cd92da
children af1585e7d645
line wrap: on
line diff
--- a/src/cjr_print.sml	Tue Apr 07 17:26:53 2015 -0400
+++ b/src/cjr_print.sml	Wed May 06 14:50:29 2015 -0400
@@ -3404,111 +3404,7 @@
              newline,
 
              (* For sqlcache. *)
-             box (List.map
-                      (fn {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 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 ") {\n if (cacheQuery",
-                                  string i,
-                                  (* ASK: is returning the pointer okay? Should we duplicate? *)
-                                  string " == NULL",
-                                  string eqs,
-                                  string ") {\n puts(\"SQLCACHE: miss ",
-                                  string i,
-                                  string ".\");\n uw_recordingStart(ctx);\n return NULL;\n } else {\n puts(\"SQLCACHE: hit ",
-                                  string i,
-                                  string ".\");\n uw_write(ctx, cacheWrite",
-                                  string i,
-                                  string ");\n return cacheQuery",
-                                  string i,
-                                  string ";\n } };",
-                                  newline,
-                                  string "static uw_unit uw_Sqlcache_store",
-                                  string i,
-                                  string "(uw_context ctx, uw_Basis_string s",
-                                  string args,
-                                  string ") {\n 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 ".\");\n return uw_unit_v;\n };",
-                                  newline,
-                                  string "static uw_unit uw_Sqlcache_flush",
-                                  string i,
-                                  string "(uw_context ctx",
-                                  string args,
-                                  string ") {\n if (cacheQuery",
-                                  string i,
-                                  string " != NULL",
-                                  string eqsNull,
-                                  string ") {\n free(cacheQuery",
-                                  string i,
-                                  string ");\n cacheQuery",
-                                  string i,
-                                  string " = NULL;\n puts(\"SQLCACHE: flush ",
-                                  string i,
-                                  string ".\");}\n else { puts(\"SQLCACHE: keep ",
-                                  string i,
-                                  string ".\"); } return uw_unit_v;\n };",
-                                  newline,
-                                  newline]
-                          end)
-                      (Sqlcache.getFfiInfo ())),
+             box (List.map ToyCache.setupQuery (Sqlcache.getFfiInfo ())),
              newline,
 
              p_list_sep newline (fn x => x) pds,