src/Tools/cache_io.ML
author boehmes
Wed, 24 Mar 2010 09:43:34 +0100
changeset 35942 667fd8553cd5
parent 35941 63f0d628edff
child 36086 8e5454761f26
permissions -rw-r--r--
use internal SHA1 digest implementation for generating hash keys
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
35942
667fd8553cd5 use internal SHA1 digest implementation for generating hash keys
boehmes
parents: 35941
diff changeset
     1
(*  Title:      Tools/cache_io.ML
35151
117247018b54 added Cache_IO: cache for output of external tools,
boehmes
parents:
diff changeset
     2
    Author:     Sascha Boehme, TU Muenchen
117247018b54 added Cache_IO: cache for output of external tools,
boehmes
parents:
diff changeset
     3
117247018b54 added Cache_IO: cache for output of external tools,
boehmes
parents:
diff changeset
     4
Cache for output of external processes.
117247018b54 added Cache_IO: cache for output of external tools,
boehmes
parents:
diff changeset
     5
*)
117247018b54 added Cache_IO: cache for output of external tools,
boehmes
parents:
diff changeset
     6
117247018b54 added Cache_IO: cache for output of external tools,
boehmes
parents:
diff changeset
     7
signature CACHE_IO =
117247018b54 added Cache_IO: cache for output of external tools,
boehmes
parents:
diff changeset
     8
sig
117247018b54 added Cache_IO: cache for output of external tools,
boehmes
parents:
diff changeset
     9
  val with_tmp_file: string -> (Path.T -> 'a) -> 'a
117247018b54 added Cache_IO: cache for output of external tools,
boehmes
parents:
diff changeset
    10
  val run: (Path.T -> string) -> Path.T -> string list
117247018b54 added Cache_IO: cache for output of external tools,
boehmes
parents:
diff changeset
    11
  val run': (Path.T -> Path.T -> string) -> Path.T -> string list * string list
117247018b54 added Cache_IO: cache for output of external tools,
boehmes
parents:
diff changeset
    12
117247018b54 added Cache_IO: cache for output of external tools,
boehmes
parents:
diff changeset
    13
  type cache
117247018b54 added Cache_IO: cache for output of external tools,
boehmes
parents:
diff changeset
    14
  val make: Path.T -> cache
117247018b54 added Cache_IO: cache for output of external tools,
boehmes
parents:
diff changeset
    15
  val cache_path_of: cache -> Path.T
117247018b54 added Cache_IO: cache for output of external tools,
boehmes
parents:
diff changeset
    16
  val cached: cache -> (Path.T -> string) -> Path.T -> string list
117247018b54 added Cache_IO: cache for output of external tools,
boehmes
parents:
diff changeset
    17
  val cached': cache -> (Path.T -> Path.T -> string) -> Path.T ->
117247018b54 added Cache_IO: cache for output of external tools,
boehmes
parents:
diff changeset
    18
    string list * string list
117247018b54 added Cache_IO: cache for output of external tools,
boehmes
parents:
diff changeset
    19
end
117247018b54 added Cache_IO: cache for output of external tools,
boehmes
parents:
diff changeset
    20
117247018b54 added Cache_IO: cache for output of external tools,
boehmes
parents:
diff changeset
    21
structure Cache_IO : CACHE_IO =
117247018b54 added Cache_IO: cache for output of external tools,
boehmes
parents:
diff changeset
    22
struct
117247018b54 added Cache_IO: cache for output of external tools,
boehmes
parents:
diff changeset
    23
117247018b54 added Cache_IO: cache for output of external tools,
boehmes
parents:
diff changeset
    24
fun with_tmp_file name f =
117247018b54 added Cache_IO: cache for output of external tools,
boehmes
parents:
diff changeset
    25
  let
117247018b54 added Cache_IO: cache for output of external tools,
boehmes
parents:
diff changeset
    26
    val path = File.tmp_path (Path.explode (name ^ serial_string ()))
117247018b54 added Cache_IO: cache for output of external tools,
boehmes
parents:
diff changeset
    27
    val x = Exn.capture f path
117247018b54 added Cache_IO: cache for output of external tools,
boehmes
parents:
diff changeset
    28
    val _ = try File.rm path
117247018b54 added Cache_IO: cache for output of external tools,
boehmes
parents:
diff changeset
    29
  in Exn.release x end
117247018b54 added Cache_IO: cache for output of external tools,
boehmes
parents:
diff changeset
    30
117247018b54 added Cache_IO: cache for output of external tools,
boehmes
parents:
diff changeset
    31
fun run' make_cmd in_path =
117247018b54 added Cache_IO: cache for output of external tools,
boehmes
parents:
diff changeset
    32
  with_tmp_file "cache-io-" (fn out_path =>
117247018b54 added Cache_IO: cache for output of external tools,
boehmes
parents:
diff changeset
    33
    let
117247018b54 added Cache_IO: cache for output of external tools,
boehmes
parents:
diff changeset
    34
      val (out2, _) = bash_output (make_cmd in_path out_path)
117247018b54 added Cache_IO: cache for output of external tools,
boehmes
parents:
diff changeset
    35
      val out1 = the_default [] (try (rev o File.fold_lines cons out_path) [])
117247018b54 added Cache_IO: cache for output of external tools,
boehmes
parents:
diff changeset
    36
    in (out1, split_lines out2) end)
117247018b54 added Cache_IO: cache for output of external tools,
boehmes
parents:
diff changeset
    37
117247018b54 added Cache_IO: cache for output of external tools,
boehmes
parents:
diff changeset
    38
fun run make_cmd = snd o run' (fn in_path => fn _ => make_cmd in_path)
117247018b54 added Cache_IO: cache for output of external tools,
boehmes
parents:
diff changeset
    39
117247018b54 added Cache_IO: cache for output of external tools,
boehmes
parents:
diff changeset
    40
117247018b54 added Cache_IO: cache for output of external tools,
boehmes
parents:
diff changeset
    41
117247018b54 added Cache_IO: cache for output of external tools,
boehmes
parents:
diff changeset
    42
abstype cache = Cache of {
117247018b54 added Cache_IO: cache for output of external tools,
boehmes
parents:
diff changeset
    43
  path: Path.T,
117247018b54 added Cache_IO: cache for output of external tools,
boehmes
parents:
diff changeset
    44
  table: (int * (int * int * int) Symtab.table) Synchronized.var }
117247018b54 added Cache_IO: cache for output of external tools,
boehmes
parents:
diff changeset
    45
with
117247018b54 added Cache_IO: cache for output of external tools,
boehmes
parents:
diff changeset
    46
117247018b54 added Cache_IO: cache for output of external tools,
boehmes
parents:
diff changeset
    47
117247018b54 added Cache_IO: cache for output of external tools,
boehmes
parents:
diff changeset
    48
fun cache_path_of (Cache {path, ...}) = path
117247018b54 added Cache_IO: cache for output of external tools,
boehmes
parents:
diff changeset
    49
117247018b54 added Cache_IO: cache for output of external tools,
boehmes
parents:
diff changeset
    50
117247018b54 added Cache_IO: cache for output of external tools,
boehmes
parents:
diff changeset
    51
fun load cache_path =
117247018b54 added Cache_IO: cache for output of external tools,
boehmes
parents:
diff changeset
    52
  let
117247018b54 added Cache_IO: cache for output of external tools,
boehmes
parents:
diff changeset
    53
    fun err () = error ("Cache IO: corrupted cache file: " ^
117247018b54 added Cache_IO: cache for output of external tools,
boehmes
parents:
diff changeset
    54
      File.shell_path cache_path)
117247018b54 added Cache_IO: cache for output of external tools,
boehmes
parents:
diff changeset
    55
117247018b54 added Cache_IO: cache for output of external tools,
boehmes
parents:
diff changeset
    56
    fun int_of_string s =
117247018b54 added Cache_IO: cache for output of external tools,
boehmes
parents:
diff changeset
    57
      (case read_int (explode s) of
117247018b54 added Cache_IO: cache for output of external tools,
boehmes
parents:
diff changeset
    58
        (i, []) => i
117247018b54 added Cache_IO: cache for output of external tools,
boehmes
parents:
diff changeset
    59
      | _ => err ())    
117247018b54 added Cache_IO: cache for output of external tools,
boehmes
parents:
diff changeset
    60
117247018b54 added Cache_IO: cache for output of external tools,
boehmes
parents:
diff changeset
    61
    fun split line =
117247018b54 added Cache_IO: cache for output of external tools,
boehmes
parents:
diff changeset
    62
      (case space_explode " " line of
117247018b54 added Cache_IO: cache for output of external tools,
boehmes
parents:
diff changeset
    63
        [key, len1, len2] => (key, int_of_string len1, int_of_string len2)
117247018b54 added Cache_IO: cache for output of external tools,
boehmes
parents:
diff changeset
    64
      | _ => err ())
117247018b54 added Cache_IO: cache for output of external tools,
boehmes
parents:
diff changeset
    65
117247018b54 added Cache_IO: cache for output of external tools,
boehmes
parents:
diff changeset
    66
    fun parse line ((i, l), tab) =
117247018b54 added Cache_IO: cache for output of external tools,
boehmes
parents:
diff changeset
    67
      if i = l
117247018b54 added Cache_IO: cache for output of external tools,
boehmes
parents:
diff changeset
    68
      then
117247018b54 added Cache_IO: cache for output of external tools,
boehmes
parents:
diff changeset
    69
        let val (key, l1, l2) = split line
117247018b54 added Cache_IO: cache for output of external tools,
boehmes
parents:
diff changeset
    70
        in ((i+1, l+l1+l2+1), Symtab.update (key, (i+1, l1, l2)) tab) end
117247018b54 added Cache_IO: cache for output of external tools,
boehmes
parents:
diff changeset
    71
      else ((i+1, l), tab)
117247018b54 added Cache_IO: cache for output of external tools,
boehmes
parents:
diff changeset
    72
  in apfst fst (File.fold_lines parse cache_path ((1, 1), Symtab.empty)) end 
117247018b54 added Cache_IO: cache for output of external tools,
boehmes
parents:
diff changeset
    73
117247018b54 added Cache_IO: cache for output of external tools,
boehmes
parents:
diff changeset
    74
fun make path =
117247018b54 added Cache_IO: cache for output of external tools,
boehmes
parents:
diff changeset
    75
  let val table = if File.exists path then load path else (1, Symtab.empty)
117247018b54 added Cache_IO: cache for output of external tools,
boehmes
parents:
diff changeset
    76
  in Cache {path=path, table=Synchronized.var (Path.implode path) table} end
117247018b54 added Cache_IO: cache for output of external tools,
boehmes
parents:
diff changeset
    77
117247018b54 added Cache_IO: cache for output of external tools,
boehmes
parents:
diff changeset
    78
117247018b54 added Cache_IO: cache for output of external tools,
boehmes
parents:
diff changeset
    79
fun load_cached_result cache_path (p, len1, len2) =
117247018b54 added Cache_IO: cache for output of external tools,
boehmes
parents:
diff changeset
    80
  let
117247018b54 added Cache_IO: cache for output of external tools,
boehmes
parents:
diff changeset
    81
    fun load line (i, xsp) =
117247018b54 added Cache_IO: cache for output of external tools,
boehmes
parents:
diff changeset
    82
      if i < p then (i+1, xsp)
117247018b54 added Cache_IO: cache for output of external tools,
boehmes
parents:
diff changeset
    83
      else if i < p + len1 then (i+1, apfst (cons line) xsp)
117247018b54 added Cache_IO: cache for output of external tools,
boehmes
parents:
diff changeset
    84
      else if i < p + len2 then (i+1, apsnd (cons line) xsp)
117247018b54 added Cache_IO: cache for output of external tools,
boehmes
parents:
diff changeset
    85
      else (i, xsp)
117247018b54 added Cache_IO: cache for output of external tools,
boehmes
parents:
diff changeset
    86
  in pairself rev (snd (File.fold_lines load cache_path (1, ([], [])))) end
117247018b54 added Cache_IO: cache for output of external tools,
boehmes
parents:
diff changeset
    87
117247018b54 added Cache_IO: cache for output of external tools,
boehmes
parents:
diff changeset
    88
fun cached' (Cache {path=cache_path, table}) make_cmd in_path =
35942
667fd8553cd5 use internal SHA1 digest implementation for generating hash keys
boehmes
parents: 35941
diff changeset
    89
  let val key = SHA1.digest (File.read in_path)
35151
117247018b54 added Cache_IO: cache for output of external tools,
boehmes
parents:
diff changeset
    90
  in
117247018b54 added Cache_IO: cache for output of external tools,
boehmes
parents:
diff changeset
    91
    (case Symtab.lookup (snd (Synchronized.value table)) key of
117247018b54 added Cache_IO: cache for output of external tools,
boehmes
parents:
diff changeset
    92
      SOME pos => load_cached_result cache_path pos
117247018b54 added Cache_IO: cache for output of external tools,
boehmes
parents:
diff changeset
    93
    | NONE =>
117247018b54 added Cache_IO: cache for output of external tools,
boehmes
parents:
diff changeset
    94
        let
117247018b54 added Cache_IO: cache for output of external tools,
boehmes
parents:
diff changeset
    95
          val res as (out, err) = run' make_cmd in_path
117247018b54 added Cache_IO: cache for output of external tools,
boehmes
parents:
diff changeset
    96
          val (l1, l2) = pairself length res
117247018b54 added Cache_IO: cache for output of external tools,
boehmes
parents:
diff changeset
    97
          val header = key ^ " " ^ string_of_int l1 ^ " " ^ string_of_int l2
117247018b54 added Cache_IO: cache for output of external tools,
boehmes
parents:
diff changeset
    98
          val lines = map (suffix "\n") (header :: out @ err)
117247018b54 added Cache_IO: cache for output of external tools,
boehmes
parents:
diff changeset
    99
117247018b54 added Cache_IO: cache for output of external tools,
boehmes
parents:
diff changeset
   100
          val _ = Synchronized.change table (fn (p, tab) =>
117247018b54 added Cache_IO: cache for output of external tools,
boehmes
parents:
diff changeset
   101
            if Symtab.defined tab key then (p, tab)
117247018b54 added Cache_IO: cache for output of external tools,
boehmes
parents:
diff changeset
   102
            else
117247018b54 added Cache_IO: cache for output of external tools,
boehmes
parents:
diff changeset
   103
              let val _ = File.append_list cache_path lines
117247018b54 added Cache_IO: cache for output of external tools,
boehmes
parents:
diff changeset
   104
              in (p+l1+l2+1, Symtab.update (key, (p+1, l1, l2)) tab) end)
117247018b54 added Cache_IO: cache for output of external tools,
boehmes
parents:
diff changeset
   105
        in res end)
117247018b54 added Cache_IO: cache for output of external tools,
boehmes
parents:
diff changeset
   106
  end
117247018b54 added Cache_IO: cache for output of external tools,
boehmes
parents:
diff changeset
   107
117247018b54 added Cache_IO: cache for output of external tools,
boehmes
parents:
diff changeset
   108
fun cached cache make_cmd =
117247018b54 added Cache_IO: cache for output of external tools,
boehmes
parents:
diff changeset
   109
  snd o cached' cache (fn in_path => fn _ => make_cmd in_path)
117247018b54 added Cache_IO: cache for output of external tools,
boehmes
parents:
diff changeset
   110
117247018b54 added Cache_IO: cache for output of external tools,
boehmes
parents:
diff changeset
   111
end
117247018b54 added Cache_IO: cache for output of external tools,
boehmes
parents:
diff changeset
   112
end