src/Pure/Thy/sessions.scala
author wenzelm
Tue Apr 04 18:43:58 2017 +0200 (2017-04-04 ago)
changeset 65374 ce09e947c1d5
parent 65360 3ff88fece1f6
child 65375 b722ee40c26c
permissions -rw-r--r--
tuned signature;
     1 /*  Title:      Pure/Thy/sessions.scala
     2     Author:     Makarius
     3 
     4 Isabelle session information.
     5 */
     6 
     7 package isabelle
     8 
     9 import java.nio.ByteBuffer
    10 import java.nio.channels.FileChannel
    11 import java.nio.file.StandardOpenOption
    12 import java.sql.PreparedStatement
    13 
    14 import scala.collection.SortedSet
    15 import scala.collection.mutable
    16 
    17 
    18 object Sessions
    19 {
    20   /* base info and source dependencies */
    21 
    22   def is_pure(name: String): Boolean = name == Thy_Header.PURE
    23 
    24   object Base
    25   {
    26     def pure(options: Options): Base = session_base(options, Thy_Header.PURE)
    27 
    28     lazy val bootstrap: Base =
    29       Base(keywords = Thy_Header.bootstrap_header, syntax = Thy_Header.bootstrap_syntax)
    30   }
    31 
    32   sealed case class Base(
    33     loaded_theories: Set[String] = Set.empty,
    34     known_theories: Map[String, Document.Node.Name] = Map.empty,
    35     keywords: Thy_Header.Keywords = Nil,
    36     syntax: Outer_Syntax = Outer_Syntax.empty,
    37     sources: List[(Path, SHA1.Digest)] = Nil,
    38     session_graph: Graph_Display.Graph = Graph_Display.empty_graph)
    39   {
    40     def loaded_theory(name: Document.Node.Name): Boolean =
    41       loaded_theories.contains(name.theory)
    42   }
    43 
    44   sealed case class Deps(deps: Map[String, Base])
    45   {
    46     def is_empty: Boolean = deps.isEmpty
    47     def apply(name: String): Base = deps(name)
    48     def sources(name: String): List[SHA1.Digest] = deps(name).sources.map(_._2)
    49   }
    50 
    51   def dependencies(
    52       progress: Progress = No_Progress,
    53       inlined_files: Boolean = false,
    54       verbose: Boolean = false,
    55       list_files: Boolean = false,
    56       check_keywords: Set[String] = Set.empty,
    57       tree: Tree): Deps =
    58     Deps((Map.empty[String, Base] /: tree.topological_order)(
    59       { case (deps, (name, info)) =>
    60           if (progress.stopped) throw Exn.Interrupt()
    61 
    62           try {
    63             val parent_base =
    64               info.parent match {
    65                 case None => Base.bootstrap
    66                 case Some(parent) => deps(parent)
    67               }
    68             val resources = new Resources(name, parent_base)
    69 
    70             if (verbose || list_files) {
    71               val groups =
    72                 if (info.groups.isEmpty) ""
    73                 else info.groups.mkString(" (", " ", ")")
    74               progress.echo("Session " + info.chapter + "/" + name + groups)
    75             }
    76 
    77             val thy_deps =
    78             {
    79               val root_theories =
    80                 info.theories.flatMap({
    81                   case (global, _, thys) =>
    82                     thys.map(thy =>
    83                       (resources.init_name(global, info.dir + resources.thy_path(thy)), info.pos))
    84                 })
    85               val thy_deps = resources.thy_info.dependencies(root_theories)
    86 
    87               thy_deps.errors match {
    88                 case Nil => thy_deps
    89                 case errs => error(cat_lines(errs))
    90               }
    91             }
    92 
    93             val known_theories =
    94               (parent_base.known_theories /: thy_deps.deps)({ case (known, dep) =>
    95                 val name = dep.name
    96                 known.get(name.theory) match {
    97                   case Some(name1) if name != name1 =>
    98                     error("Duplicate theory " + quote(name.node) + " vs. " + quote(name1.node))
    99                   case _ =>
   100                     known + (name.theory -> name) + (Long_Name.base_name(name.theory) -> name)
   101                 }
   102               })
   103 
   104             val loaded_theories = thy_deps.loaded_theories
   105             val keywords = thy_deps.keywords
   106             val syntax = thy_deps.syntax
   107 
   108             val theory_files = thy_deps.deps.map(dep => Path.explode(dep.name.node))
   109             val loaded_files =
   110               if (inlined_files) {
   111                 val pure_files =
   112                   if (is_pure(name)) {
   113                     val roots = Thy_Header.ml_roots.map(p => info.dir + Path.explode(p._1))
   114                     val files =
   115                       roots.flatMap(root => resources.loaded_files(syntax, File.read(root))).
   116                         map(file => info.dir + Path.explode(file))
   117                     roots ::: files
   118                   }
   119                   else Nil
   120                 pure_files ::: thy_deps.loaded_files
   121               }
   122               else Nil
   123 
   124             val all_files =
   125               (theory_files ::: loaded_files :::
   126                 info.files.map(file => info.dir + file) :::
   127                 info.document_files.map(file => info.dir + file._1 + file._2)).map(_.expand)
   128 
   129             if (list_files)
   130               progress.echo(cat_lines(all_files.map(_.implode).sorted.map("  " + _)))
   131 
   132             if (check_keywords.nonEmpty)
   133               Check_Keywords.check_keywords(progress, syntax.keywords, check_keywords, theory_files)
   134 
   135             val sources = all_files.map(p => (p, SHA1.digest(p.file)))
   136 
   137             val session_graph =
   138               Present.session_graph(info.parent getOrElse "",
   139                 parent_base.loaded_theories, thy_deps.deps)
   140 
   141             val base =
   142               Base(loaded_theories, known_theories, keywords, syntax, sources, session_graph)
   143             deps + (name -> base)
   144           }
   145           catch {
   146             case ERROR(msg) =>
   147               cat_error(msg, "The error(s) above occurred in session " +
   148                 quote(name) + Position.here(info.pos))
   149           }
   150       }))
   151 
   152   def session_base(options: Options, session: String, dirs: List[Path] = Nil): Base =
   153   {
   154     val (_, tree) = load(options, dirs = dirs).selection(sessions = List(session))
   155     dependencies(tree = tree)(session)
   156   }
   157 
   158 
   159   /* session tree */
   160 
   161   sealed case class Info(
   162     chapter: String,
   163     select: Boolean,
   164     pos: Position.T,
   165     groups: List[String],
   166     dir: Path,
   167     parent: Option[String],
   168     description: String,
   169     options: Options,
   170     theories: List[(Boolean, Options, List[Path])],
   171     files: List[Path],
   172     document_files: List[(Path, Path)],
   173     meta_digest: SHA1.Digest)
   174   {
   175     def timeout: Time = Time.seconds(options.real("timeout") * options.real("timeout_scale"))
   176   }
   177 
   178   object Tree
   179   {
   180     def apply(infos: Traversable[(String, Info)]): Tree =
   181     {
   182       val graph1 =
   183         (Graph.string[Info] /: infos) {
   184           case (graph, (name, info)) =>
   185             if (graph.defined(name))
   186               error("Duplicate session " + quote(name) + Position.here(info.pos) +
   187                 Position.here(graph.get_node(name).pos))
   188             else graph.new_node(name, info)
   189         }
   190       val graph2 =
   191         (graph1 /: graph1.iterator) {
   192           case (graph, (name, (info, _))) =>
   193             info.parent match {
   194               case None => graph
   195               case Some(parent) =>
   196                 if (!graph.defined(parent))
   197                   error("Bad parent session " + quote(parent) + " for " +
   198                     quote(name) + Position.here(info.pos))
   199 
   200                 try { graph.add_edge_acyclic(parent, name) }
   201                 catch {
   202                   case exn: Graph.Cycles[_] =>
   203                     error(cat_lines(exn.cycles.map(cycle =>
   204                       "Cyclic session dependency of " +
   205                         cycle.map(c => quote(c.toString)).mkString(" via "))) +
   206                           Position.here(info.pos))
   207                 }
   208             }
   209         }
   210       new Tree(graph2)
   211     }
   212   }
   213 
   214   final class Tree private(val graph: Graph[String, Info])
   215     extends PartialFunction[String, Info]
   216   {
   217     def apply(name: String): Info = graph.get_node(name)
   218     def isDefinedAt(name: String): Boolean = graph.defined(name)
   219 
   220     def selection(
   221       requirements: Boolean = false,
   222       all_sessions: Boolean = false,
   223       exclude_session_groups: List[String] = Nil,
   224       exclude_sessions: List[String] = Nil,
   225       session_groups: List[String] = Nil,
   226       sessions: List[String] = Nil): (List[String], Tree) =
   227     {
   228       val bad_sessions =
   229         SortedSet((exclude_sessions ::: sessions).filterNot(isDefinedAt(_)): _*).toList
   230       if (bad_sessions.nonEmpty) error("Undefined session(s): " + commas_quote(bad_sessions))
   231 
   232       val excluded =
   233       {
   234         val exclude_group = exclude_session_groups.toSet
   235         val exclude_group_sessions =
   236           (for {
   237             (name, (info, _)) <- graph.iterator
   238             if apply(name).groups.exists(exclude_group)
   239           } yield name).toList
   240         graph.all_succs(exclude_group_sessions ::: exclude_sessions).toSet
   241       }
   242 
   243       val pre_selected =
   244       {
   245         if (all_sessions) graph.keys
   246         else {
   247           val select_group = session_groups.toSet
   248           val select = sessions.toSet
   249           (for {
   250             (name, (info, _)) <- graph.iterator
   251             if info.select || select(name) || apply(name).groups.exists(select_group)
   252           } yield name).toList
   253         }
   254       }.filterNot(excluded)
   255 
   256       val selected =
   257         if (requirements) (graph.all_preds(pre_selected).toSet -- pre_selected).toList
   258         else pre_selected
   259 
   260       val graph1 = graph.restrict(graph.all_preds(selected).toSet)
   261       (selected, new Tree(graph1))
   262     }
   263 
   264     def ancestors(name: String): List[String] =
   265       graph.all_preds(List(name)).tail.reverse
   266 
   267     def topological_order: List[(String, Info)] =
   268       graph.topological_order.map(name => (name, apply(name)))
   269 
   270     override def toString: String = graph.keys_iterator.mkString("Sessions.Tree(", ", ", ")")
   271   }
   272 
   273 
   274   /* parser */
   275 
   276   val ROOT = Path.explode("ROOT")
   277   val ROOTS = Path.explode("ROOTS")
   278 
   279   private val CHAPTER = "chapter"
   280   private val SESSION = "session"
   281   private val IN = "in"
   282   private val DESCRIPTION = "description"
   283   private val OPTIONS = "options"
   284   private val GLOBAL_THEORIES = "global_theories"
   285   private val THEORIES = "theories"
   286   private val FILES = "files"
   287   private val DOCUMENT_FILES = "document_files"
   288 
   289   lazy val root_syntax =
   290     Outer_Syntax.init() + "(" + ")" + "+" + "," + "=" + "[" + "]" + IN +
   291       (CHAPTER, Keyword.THY_DECL) +
   292       (SESSION, Keyword.THY_DECL) +
   293       (DESCRIPTION, Keyword.QUASI_COMMAND) +
   294       (OPTIONS, Keyword.QUASI_COMMAND) +
   295       (GLOBAL_THEORIES, Keyword.QUASI_COMMAND) +
   296       (THEORIES, Keyword.QUASI_COMMAND) +
   297       (FILES, Keyword.QUASI_COMMAND) +
   298       (DOCUMENT_FILES, Keyword.QUASI_COMMAND)
   299 
   300   private object Parser extends Parse.Parser with Options.Parser
   301   {
   302     private abstract class Entry
   303     private sealed case class Chapter(name: String) extends Entry
   304     private sealed case class Session_Entry(
   305       pos: Position.T,
   306       name: String,
   307       groups: List[String],
   308       path: String,
   309       parent: Option[String],
   310       description: String,
   311       options: List[Options.Spec],
   312       theories: List[(Boolean, List[Options.Spec], List[String])],
   313       files: List[String],
   314       document_files: List[(String, String)]) extends Entry
   315 
   316     private val chapter: Parser[Chapter] =
   317     {
   318       val chapter_name = atom("chapter name", _.is_name)
   319 
   320       command(CHAPTER) ~! chapter_name ^^ { case _ ~ a => Chapter(a) }
   321     }
   322 
   323     private val session_entry: Parser[Session_Entry] =
   324     {
   325       val session_name = atom("session name", _.is_name)
   326 
   327       val option =
   328         option_name ~ opt($$$("=") ~! option_value ^^
   329           { case _ ~ x => x }) ^^ { case x ~ y => (x, y) }
   330       val options = $$$("[") ~> rep1sep(option, $$$(",")) <~ $$$("]")
   331 
   332       val theories =
   333         ($$$(GLOBAL_THEORIES) | $$$(THEORIES)) ~!
   334           ((options | success(Nil)) ~ rep(theory_name)) ^^
   335           { case x ~ (y ~ z) => (x == GLOBAL_THEORIES, y, z) }
   336 
   337       val document_files =
   338         $$$(DOCUMENT_FILES) ~!
   339           (($$$("(") ~! ($$$(IN) ~! (path ~ $$$(")"))) ^^
   340               { case _ ~ (_ ~ (x ~ _)) => x } | success("document")) ~
   341             rep1(path)) ^^ { case _ ~ (x ~ y) => y.map((x, _)) }
   342 
   343       command(SESSION) ~!
   344         (position(session_name) ~
   345           (($$$("(") ~! (rep1(name) <~ $$$(")")) ^^ { case _ ~ x => x }) | success(Nil)) ~
   346           (($$$(IN) ~! path ^^ { case _ ~ x => x }) | success(".")) ~
   347           ($$$("=") ~!
   348             (opt(session_name ~! $$$("+") ^^ { case x ~ _ => x }) ~
   349               (($$$(DESCRIPTION) ~! text ^^ { case _ ~ x => x }) | success("")) ~
   350               (($$$(OPTIONS) ~! options ^^ { case _ ~ x => x }) | success(Nil)) ~
   351               rep1(theories) ~
   352               (($$$(FILES) ~! rep1(path) ^^ { case _ ~ x => x }) | success(Nil)) ~
   353               (rep(document_files) ^^ (x => x.flatten))))) ^^
   354         { case _ ~ ((a, pos) ~ b ~ c ~ (_ ~ (d ~ e ~ f ~ g ~ h ~ i))) =>
   355             Session_Entry(pos, a, b, c, d, e, f, g, h, i) }
   356     }
   357 
   358     def parse(options: Options, select: Boolean, dir: Path): List[(String, Info)] =
   359     {
   360       def make_info(entry_chapter: String, entry: Session_Entry): (String, Info) =
   361       {
   362         try {
   363           val name = entry.name
   364 
   365           if (name == "") error("Bad session name")
   366           if (is_pure(name) && entry.parent.isDefined) error("Illegal parent session")
   367           if (!is_pure(name) && !entry.parent.isDefined) error("Missing parent session")
   368 
   369           val session_options = options ++ entry.options
   370 
   371           val theories =
   372             entry.theories.map({ case (global, opts, thys) =>
   373               (global, session_options ++ opts, thys.map(Path.explode(_))) })
   374           val files = entry.files.map(Path.explode(_))
   375           val document_files =
   376             entry.document_files.map({ case (s1, s2) => (Path.explode(s1), Path.explode(s2)) })
   377 
   378           val meta_digest =
   379             SHA1.digest((entry_chapter, name, entry.parent, entry.options,
   380               entry.theories, entry.files, entry.document_files).toString)
   381 
   382           val info =
   383             Info(entry_chapter, select, entry.pos, entry.groups, dir + Path.explode(entry.path),
   384               entry.parent, entry.description, session_options, theories, files,
   385               document_files, meta_digest)
   386 
   387           (name, info)
   388         }
   389         catch {
   390           case ERROR(msg) =>
   391             error(msg + "\nThe error(s) above occurred in session entry " +
   392               quote(entry.name) + Position.here(entry.pos))
   393         }
   394       }
   395 
   396       val root = dir + ROOT
   397       if (root.is_file) {
   398         val toks = Token.explode(root_syntax.keywords, File.read(root))
   399         val start = Token.Pos.file(root.implode)
   400 
   401         parse_all(rep(chapter | session_entry), Token.reader(toks, start)) match {
   402           case Success(result, _) =>
   403             var entry_chapter = "Unsorted"
   404             val infos = new mutable.ListBuffer[(String, Info)]
   405             result.foreach {
   406               case Chapter(name) => entry_chapter = name
   407               case entry: Session_Entry => infos += make_info(entry_chapter, entry)
   408             }
   409             infos.toList
   410           case bad => error(bad.toString)
   411         }
   412       }
   413       else Nil
   414     }
   415   }
   416 
   417 
   418   /* load sessions from certain directories */
   419 
   420   private def is_session_dir(dir: Path): Boolean =
   421     (dir + ROOT).is_file || (dir + ROOTS).is_file
   422 
   423   private def check_session_dir(dir: Path): Path =
   424     if (is_session_dir(dir)) dir
   425     else error("Bad session root directory: " + dir.toString)
   426 
   427   def load(options: Options, dirs: List[Path] = Nil, select_dirs: List[Path] = Nil): Tree =
   428   {
   429     def load_dir(select: Boolean, dir: Path): List[(String, Info)] =
   430       load_root(select, dir) ::: load_roots(select, dir)
   431 
   432     def load_root(select: Boolean, dir: Path): List[(String, Info)] =
   433       Parser.parse(options, select, dir)
   434 
   435     def load_roots(select: Boolean, dir: Path): List[(String, Info)] =
   436     {
   437       val roots = dir + ROOTS
   438       if (roots.is_file) {
   439         for {
   440           line <- split_lines(File.read(roots))
   441           if !(line == "" || line.startsWith("#"))
   442           dir1 =
   443             try { check_session_dir(dir + Path.explode(line)) }
   444             catch {
   445               case ERROR(msg) =>
   446                 error(msg + "\nThe error(s) above occurred in session catalog " + roots.toString)
   447             }
   448           info <- load_dir(select, dir1)
   449         } yield info
   450       }
   451       else Nil
   452     }
   453 
   454     val default_dirs = Isabelle_System.components().filter(is_session_dir(_))
   455     dirs.foreach(check_session_dir(_))
   456     select_dirs.foreach(check_session_dir(_))
   457 
   458     Tree(
   459       for {
   460         (select, dir) <- (default_dirs ::: dirs).map((false, _)) ::: select_dirs.map((true, _))
   461         info <- load_dir(select, dir)
   462       } yield info)
   463   }
   464 
   465 
   466 
   467   /** heap file with SHA1 digest **/
   468 
   469   private val sha1_prefix = "SHA1:"
   470 
   471   def read_heap_digest(heap: Path): Option[String] =
   472   {
   473     if (heap.is_file) {
   474       val file = FileChannel.open(heap.file.toPath, StandardOpenOption.READ)
   475       try {
   476         val len = file.size
   477         val n = sha1_prefix.length + SHA1.digest_length
   478         if (len >= n) {
   479           file.position(len - n)
   480 
   481           val buf = ByteBuffer.allocate(n)
   482           var i = 0
   483           var m = 0
   484           do {
   485             m = file.read(buf)
   486             if (m != -1) i += m
   487           }
   488           while (m != -1 && n > i)
   489 
   490           if (i == n) {
   491             val prefix = new String(buf.array(), 0, sha1_prefix.length, UTF8.charset)
   492             val s = new String(buf.array(), sha1_prefix.length, SHA1.digest_length, UTF8.charset)
   493             if (prefix == sha1_prefix) Some(s) else None
   494           }
   495           else None
   496         }
   497         else None
   498       }
   499       finally { file.close }
   500     }
   501     else None
   502   }
   503 
   504   def write_heap_digest(heap: Path): String =
   505     read_heap_digest(heap) match {
   506       case None =>
   507         val s = SHA1.digest(heap).rep
   508         File.append(heap, sha1_prefix + s)
   509         s
   510       case Some(s) => s
   511     }
   512 
   513 
   514 
   515   /** persistent store **/
   516 
   517   object Session_Info
   518   {
   519     val session_name = SQL.Column.string("session_name", primary_key = true)
   520 
   521     // Build_Log.Session_Info
   522     val session_timing = SQL.Column.bytes("session_timing")
   523     val command_timings = SQL.Column.bytes("command_timings")
   524     val ml_statistics = SQL.Column.bytes("ml_statistics")
   525     val task_statistics = SQL.Column.bytes("task_statistics")
   526     val build_log_columns =
   527       List(session_name, session_timing, command_timings, ml_statistics, task_statistics)
   528 
   529     // Build.Session_Info
   530     val sources = SQL.Column.string("sources")
   531     val input_heaps = SQL.Column.string("input_heaps")
   532     val output_heap = SQL.Column.string("output_heap")
   533     val return_code = SQL.Column.int("return_code")
   534     val build_columns = List(sources, input_heaps, output_heap, return_code)
   535 
   536     val table = SQL.Table("isabelle_session_info", build_log_columns ::: build_columns)
   537 
   538     def where_session_name(name: String): String =
   539       "WHERE " + session_name.sql_name + " = " + SQL.quote_string(name)
   540 
   541     def select_statement(db: SQL.Database, name: String, columns: List[SQL.Column])
   542         : PreparedStatement =
   543       db.select_statement(table, columns, where_session_name(name))
   544 
   545     def delete_statement(db: SQL.Database, name: String): PreparedStatement =
   546       db.delete_statement(table, where_session_name(name))
   547   }
   548 
   549   def store(system_mode: Boolean = false): Store = new Store(system_mode)
   550 
   551   class Store private[Sessions](system_mode: Boolean)
   552   {
   553     /* file names */
   554 
   555     def database(name: String): Path = Path.basic("log") + Path.basic(name).ext("db")
   556     def log(name: String): Path = Path.basic("log") + Path.basic(name)
   557     def log_gz(name: String): Path = log(name).ext("gz")
   558 
   559 
   560     /* SQL database content */
   561 
   562     val xml_cache: XML.Cache = new XML.Cache()
   563 
   564     def encode_properties(ps: Properties.T): Bytes =
   565       Bytes(YXML.string_of_body(XML.Encode.properties(ps)))
   566 
   567     def decode_properties(bs: Bytes): Properties.T =
   568       xml_cache.props(XML.Decode.properties(YXML.parse_body(bs.text)))
   569 
   570     def compress_properties(ps: List[Properties.T], options: XZ.Options = XZ.options()): Bytes =
   571     {
   572       if (ps.isEmpty) Bytes.empty
   573       else Bytes(YXML.string_of_body(XML.Encode.list(XML.Encode.properties)(ps))).compress(options)
   574     }
   575 
   576     def uncompress_properties(bs: Bytes): List[Properties.T] =
   577     {
   578       if (bs.isEmpty) Nil
   579       else
   580         XML.Decode.list(XML.Decode.properties)(YXML.parse_body(bs.uncompress().text)).
   581           map(xml_cache.props(_))
   582     }
   583 
   584     def read_bytes(db: SQL.Database, name: String, column: SQL.Column): Bytes =
   585       using(Session_Info.select_statement(db, name, List(column)))(stmt =>
   586       {
   587         val rs = stmt.executeQuery
   588         if (!rs.next) Bytes.empty else db.bytes(rs, column)
   589       })
   590 
   591     def read_properties(db: SQL.Database, name: String, column: SQL.Column): List[Properties.T] =
   592       uncompress_properties(read_bytes(db, name, column))
   593 
   594 
   595     /* output */
   596 
   597     val browser_info: Path =
   598       if (system_mode) Path.explode("~~/browser_info")
   599       else Path.explode("$ISABELLE_BROWSER_INFO")
   600 
   601     val output_dir: Path =
   602       if (system_mode) Path.explode("~~/heaps/$ML_IDENTIFIER")
   603       else Path.explode("$ISABELLE_OUTPUT")
   604 
   605     override def toString: String = "Store(output_dir = " + output_dir.expand + ")"
   606 
   607     def prepare_output() { Isabelle_System.mkdirs(output_dir + Path.basic("log")) }
   608 
   609 
   610     /* input */
   611 
   612     private val input_dirs =
   613       if (system_mode) List(output_dir)
   614       else {
   615         val ml_ident = Path.explode("$ML_IDENTIFIER").expand
   616         output_dir :: Path.split(Isabelle_System.getenv_strict("ISABELLE_PATH")).map(_ + ml_ident)
   617       }
   618 
   619     def find_database_heap(name: String): Option[(Path, Option[String])] =
   620       input_dirs.find(dir => (dir + database(name)).is_file).map(dir =>
   621         (dir + database(name), read_heap_digest(dir + Path.basic(name))))
   622 
   623     def find_database(name: String): Option[Path] =
   624       input_dirs.map(_ + database(name)).find(_.is_file)
   625 
   626     def heap(name: String): Path =
   627       input_dirs.map(_ + Path.basic(name)).find(_.is_file) getOrElse
   628         error("Unknown logic " + quote(name) + " -- no heap file found in:\n" +
   629           cat_lines(input_dirs.map(dir => "  " + dir.expand.implode)))
   630 
   631 
   632     /* session info */
   633 
   634     def write_session_info(
   635       db: SQL.Database,
   636       name: String,
   637       build_log: Build_Log.Session_Info,
   638       build: Build.Session_Info)
   639     {
   640       db.transaction {
   641         db.create_table(Session_Info.table)
   642         using(Session_Info.delete_statement(db, name))(_.execute)
   643         using(db.insert_statement(Session_Info.table))(stmt =>
   644         {
   645           db.set_string(stmt, 1, name)
   646           db.set_bytes(stmt, 2, encode_properties(build_log.session_timing))
   647           db.set_bytes(stmt, 3, compress_properties(build_log.command_timings))
   648           db.set_bytes(stmt, 4, compress_properties(build_log.ml_statistics))
   649           db.set_bytes(stmt, 5, compress_properties(build_log.task_statistics))
   650           db.set_string(stmt, 6, cat_lines(build.sources))
   651           db.set_string(stmt, 7, cat_lines(build.input_heaps))
   652           db.set_string(stmt, 8, build.output_heap getOrElse "")
   653           db.set_int(stmt, 9, build.return_code)
   654           stmt.execute()
   655         })
   656       }
   657     }
   658 
   659     def read_session_timing(db: SQL.Database, name: String): Properties.T =
   660       decode_properties(read_bytes(db, name, Session_Info.session_timing))
   661 
   662     def read_command_timings(db: SQL.Database, name: String): List[Properties.T] =
   663       read_properties(db, name, Session_Info.command_timings)
   664 
   665     def read_ml_statistics(db: SQL.Database, name: String): List[Properties.T] =
   666       read_properties(db, name, Session_Info.ml_statistics)
   667 
   668     def read_task_statistics(db: SQL.Database, name: String): List[Properties.T] =
   669       read_properties(db, name, Session_Info.task_statistics)
   670 
   671     def read_build_log(db: SQL.Database, name: String,
   672       command_timings: Boolean = false,
   673       ml_statistics: Boolean = false,
   674       task_statistics: Boolean = false): Build_Log.Session_Info =
   675     {
   676       Build_Log.Session_Info(
   677         session_timing = read_session_timing(db, name),
   678         command_timings = if (command_timings) read_command_timings(db, name) else Nil,
   679         ml_statistics = if (ml_statistics) read_ml_statistics(db, name) else Nil,
   680         task_statistics = if (task_statistics) read_task_statistics(db, name) else Nil)
   681     }
   682 
   683     def read_build(db: SQL.Database, name: String): Option[Build.Session_Info] =
   684       using(Session_Info.select_statement(db, name, Session_Info.build_columns))(stmt =>
   685       {
   686         val rs = stmt.executeQuery
   687         if (!rs.next) None
   688         else {
   689           Some(
   690             Build.Session_Info(
   691               split_lines(db.string(rs, Session_Info.sources)),
   692               split_lines(db.string(rs, Session_Info.input_heaps)),
   693               db.string(rs, Session_Info.output_heap) match { case "" => None case s => Some(s) },
   694               db.int(rs, Session_Info.return_code)))
   695         }
   696       })
   697   }
   698 }