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