src/Tools/cache_io.ML
changeset 41954 fb94df4505a0
parent 41945 8e32c3992cb3
child 42127 8223e7f4b0da
equal deleted inserted replaced
41953:994d088fbfbc 41954:fb94df4505a0
    65       File.shell_path cache_path)
    65       File.shell_path cache_path)
    66 
    66 
    67     fun int_of_string s =
    67     fun int_of_string s =
    68       (case read_int (raw_explode s) of
    68       (case read_int (raw_explode s) of
    69         (i, []) => i
    69         (i, []) => i
    70       | _ => err ())    
    70       | _ => err ())
    71 
    71 
    72     fun split line =
    72     fun split line =
    73       (case space_explode " " line of
    73       (case space_explode " " line of
    74         [key, len1, len2] => (key, int_of_string len1, int_of_string len2)
    74         [key, len1, len2] => (key, int_of_string len1, int_of_string len2)
    75       | _ => err ())
    75       | _ => err ())
    78       if i = l
    78       if i = l
    79       then
    79       then
    80         let val (key, l1, l2) = split line
    80         let val (key, l1, l2) = split line
    81         in ((i+1, l+l1+l2+1), Symtab.update (key, (i+1, l1, l2)) tab) end
    81         in ((i+1, l+l1+l2+1), Symtab.update (key, (i+1, l1, l2)) tab) end
    82       else ((i+1, l), tab)
    82       else ((i+1, l), tab)
    83   in apfst fst (File.fold_lines parse cache_path ((1, 1), Symtab.empty)) end 
    83   in apfst fst (File.fold_lines parse cache_path ((1, 1), Symtab.empty)) end
    84 
    84 
    85 fun make path =
    85 fun make path =
    86   let val table = if File.exists path then load path else (1, Symtab.empty)
    86   let val table = if File.exists path then load path else (1, Symtab.empty)
    87   in Cache {path=path, table=Synchronized.var "Cache_IO" table} end
    87   in Cache {path=path, table=Synchronized.var "Cache_IO" table} end
    88 
    88 
    96     val (out, err) =
    96     val (out, err) =
    97       pairself rev (snd (File.fold_lines load cache_path (1, ([], []))))
    97       pairself rev (snd (File.fold_lines load cache_path (1, ([], []))))
    98   in {output=err, redirected_output=out, return_code=0} end
    98   in {output=err, redirected_output=out, return_code=0} end
    99 
    99 
   100 fun lookup (Cache {path=cache_path, table}) str =
   100 fun lookup (Cache {path=cache_path, table}) str =
   101   let val key = SHA1.digest str
   101   let val key = SHA1.rep (SHA1.digest str)
   102   in
   102   in
   103     (case Symtab.lookup (snd (Synchronized.value table)) key of
   103     (case Symtab.lookup (snd (Synchronized.value table)) key of
   104       NONE => (NONE, key)
   104       NONE => (NONE, key)
   105     | SOME pos => (SOME (load_cached_result cache_path pos), key))
   105     | SOME pos => (SOME (load_cached_result cache_path pos), key))
   106   end
   106   end