src/Tools/cache_io.ML
author haftmann
Sun Sep 21 16:56:11 2014 +0200 (2014-09-21)
changeset 58410 6d46ad54a2ab
parent 50317 4d1590544b91
child 59058 a78612c67ec0
permissions -rw-r--r--
explicit separation of signed and unsigned numerals using existing lexical categories num and xnum
boehmes@35942
     1
(*  Title:      Tools/cache_io.ML
boehmes@35151
     2
    Author:     Sascha Boehme, TU Muenchen
boehmes@35151
     3
boehmes@35151
     4
Cache for output of external processes.
boehmes@35151
     5
*)
boehmes@35151
     6
boehmes@35151
     7
signature CACHE_IO =
boehmes@35151
     8
sig
boehmes@40425
     9
  (*IO wrapper*)
boehmes@40538
    10
  type result = {
boehmes@40538
    11
    output: string list,
boehmes@40538
    12
    redirected_output: string list,
boehmes@40538
    13
    return_code: int}
wenzelm@50316
    14
  val raw_run: (Path.T -> Path.T -> string) -> string -> Path.T -> Path.T -> result
boehmes@40538
    15
  val run: (Path.T -> Path.T -> string) -> string -> result
boehmes@35151
    16
boehmes@40425
    17
  (*cache*)
boehmes@35151
    18
  type cache
wenzelm@50317
    19
  val unsynchronized_init: Path.T -> cache
boehmes@35151
    20
  val cache_path_of: cache -> Path.T
boehmes@40538
    21
  val lookup: cache -> string -> result option * string
wenzelm@50316
    22
  val run_and_cache: cache -> string -> (Path.T -> Path.T -> string) -> string -> result
boehmes@40538
    23
  val run_cached: cache -> (Path.T -> Path.T -> string) -> string -> result
boehmes@35151
    24
end
boehmes@35151
    25
boehmes@35151
    26
structure Cache_IO : CACHE_IO =
boehmes@35151
    27
struct
boehmes@35151
    28
boehmes@40425
    29
(* IO wrapper *)
boehmes@40425
    30
boehmes@36086
    31
val cache_io_prefix = "cache-io-"
boehmes@36086
    32
boehmes@40538
    33
type result = {
boehmes@40538
    34
  output: string list,
boehmes@40538
    35
  redirected_output: string list,
boehmes@40538
    36
  return_code: int}
boehmes@40538
    37
boehmes@40578
    38
fun raw_run make_cmd str in_path out_path =
boehmes@40578
    39
  let
boehmes@40578
    40
    val _ = File.write in_path str
wenzelm@43850
    41
    val (out2, rc) = Isabelle_System.bash_output (make_cmd in_path out_path)
boehmes@40578
    42
    val out1 = the_default [] (try (rev o File.fold_lines cons out_path) [])
wenzelm@50316
    43
  in {output = split_lines out2, redirected_output = out1, return_code = rc} end
boehmes@40578
    44
boehmes@36086
    45
fun run make_cmd str =
wenzelm@42127
    46
  Isabelle_System.with_tmp_file cache_io_prefix "" (fn in_path =>
wenzelm@42127
    47
    Isabelle_System.with_tmp_file cache_io_prefix "" (fn out_path =>
wenzelm@41307
    48
      raw_run make_cmd str in_path out_path))
boehmes@35151
    49
boehmes@35151
    50
boehmes@40425
    51
(* cache *)
boehmes@35151
    52
boehmes@35151
    53
abstype cache = Cache of {
boehmes@35151
    54
  path: Path.T,
boehmes@35151
    55
  table: (int * (int * int * int) Symtab.table) Synchronized.var }
boehmes@35151
    56
with
boehmes@35151
    57
boehmes@35151
    58
fun cache_path_of (Cache {path, ...}) = path
boehmes@35151
    59
wenzelm@50317
    60
fun unsynchronized_init cache_path =
boehmes@35151
    61
  let
wenzelm@50316
    62
    val table =
wenzelm@50316
    63
      if File.exists cache_path then
wenzelm@50316
    64
        let
wenzelm@50316
    65
          fun err () = error ("Cache IO: corrupted cache file: " ^ File.shell_path cache_path)
boehmes@35151
    66
wenzelm@50316
    67
          fun int_of_string s =
wenzelm@50316
    68
            (case read_int (raw_explode s) of
wenzelm@50316
    69
              (i, []) => i
wenzelm@50316
    70
            | _ => err ())
boehmes@35151
    71
wenzelm@50316
    72
          fun split line =
wenzelm@50316
    73
            (case space_explode " " line of
wenzelm@50316
    74
              [key, len1, len2] => (key, int_of_string len1, int_of_string len2)
wenzelm@50316
    75
            | _ => err ())
boehmes@35151
    76
wenzelm@50316
    77
          fun parse line ((i, l), tab) =
wenzelm@50316
    78
            if i = l
wenzelm@50316
    79
            then
wenzelm@50316
    80
              let val (key, l1, l2) = split line
wenzelm@50316
    81
              in ((i+1, l+l1+l2+1), Symtab.update (key, (i+1, l1, l2)) tab) end
wenzelm@50316
    82
            else ((i+1, l), tab)
wenzelm@50316
    83
        in apfst fst (File.fold_lines parse cache_path ((1, 1), Symtab.empty)) end
wenzelm@50316
    84
      else (1, Symtab.empty)
wenzelm@50316
    85
  in Cache {path = cache_path, table = Synchronized.var "Cache_IO" table} end
boehmes@35151
    86
wenzelm@50316
    87
fun lookup (Cache {path = cache_path, table}) str =
wenzelm@41954
    88
  let val key = SHA1.rep (SHA1.digest str)
boehmes@35151
    89
  in
wenzelm@50317
    90
    Synchronized.change_result table (fn tab =>
wenzelm@50317
    91
      (case Symtab.lookup (snd tab) key of
wenzelm@50317
    92
        NONE => ((NONE, key), tab)
wenzelm@50317
    93
      | SOME (p, len1, len2) =>
wenzelm@50317
    94
          let
wenzelm@50317
    95
            fun load line (i, xsp) =
wenzelm@50317
    96
              if i < p then (i+1, xsp)
wenzelm@50317
    97
              else if i < p + len1 then (i+1, apfst (cons line) xsp)
wenzelm@50317
    98
              else if i < p + len2 then (i+1, apsnd (cons line) xsp)
wenzelm@50317
    99
              else (i, xsp)
wenzelm@50317
   100
            val (out, err) =
wenzelm@50317
   101
              pairself rev (snd (File.fold_lines load cache_path (1, ([], []))))
wenzelm@50317
   102
          in ((SOME {output = err, redirected_output = out, return_code = 0}, key), tab) end))
boehmes@35151
   103
  end
boehmes@35151
   104
wenzelm@50316
   105
fun run_and_cache (Cache {path = cache_path, table}) key make_cmd str =
boehmes@36086
   106
  let
wenzelm@50316
   107
    val {output = err, redirected_output=out, return_code} = run make_cmd str
boehmes@40538
   108
    val (l1, l2) = pairself length (out, err)
boehmes@36086
   109
    val header = key ^ " " ^ string_of_int l1 ^ " " ^ string_of_int l2
boehmes@36086
   110
    val lines = map (suffix "\n") (header :: out @ err)
boehmes@36086
   111
boehmes@36086
   112
    val _ = Synchronized.change table (fn (p, tab) =>
boehmes@36086
   113
      if Symtab.defined tab key then (p, tab)
boehmes@36086
   114
      else
boehmes@36086
   115
        let val _ = File.append_list cache_path lines
boehmes@36086
   116
        in (p+l1+l2+1, Symtab.update (key, (p+1, l1, l2)) tab) end)
wenzelm@50316
   117
  in {output = err, redirected_output = out, return_code = return_code} end
boehmes@36086
   118
boehmes@36086
   119
fun run_cached cache make_cmd str =
boehmes@36086
   120
  (case lookup cache str of
boehmes@36086
   121
    (NONE, key) => run_and_cache cache key make_cmd str
boehmes@36086
   122
  | (SOME output, _) => output)
boehmes@35151
   123
boehmes@35151
   124
end
boehmes@40425
   125
boehmes@35151
   126
end