src/Pure/Thy/store.scala
author wenzelm
Wed, 21 Jun 2023 11:05:20 +0200
changeset 78184 4309bcc8f28b
parent 78182 31835adf148a
child 78185 26b9b40ec1af
permissions -rw-r--r--
tuned;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
78178
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
     1
/*  Title:      Pure/Thy/store.scala
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
     3
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
     4
Persistent store for session content: within file-system and/or SQL database.
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
     5
*/
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
     6
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
     7
package isabelle
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
     8
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
     9
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
    10
import java.sql.SQLException
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
    11
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
    12
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
    13
object Store {
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
    14
  def apply(options: Options, cache: Term.Cache = Term.Cache.make()): Store =
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
    15
    new Store(options, cache)
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
    16
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
    17
78179
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
    18
  /* session build info */
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
    19
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
    20
  sealed case class Build_Info(
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
    21
    sources: SHA1.Shasum,
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
    22
    input_heaps: SHA1.Shasum,
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
    23
    output_heap: SHA1.Shasum,
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
    24
    return_code: Int,
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
    25
    uuid: String
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
    26
  ) {
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
    27
    def ok: Boolean = return_code == 0
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
    28
  }
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
    29
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
    30
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
    31
  /* session sources */
78178
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
    32
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
    33
  sealed case class Source_File(
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
    34
    name: String,
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
    35
    digest: SHA1.Digest,
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
    36
    compressed: Boolean,
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
    37
    body: Bytes,
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
    38
    cache: Compress.Cache
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
    39
  ) {
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
    40
    override def toString: String = name
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
    41
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
    42
    def bytes: Bytes = if (compressed) body.uncompress(cache = cache) else body
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
    43
  }
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
    44
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
    45
  object Sources {
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
    46
    def load(session_base: Sessions.Base, cache: Compress.Cache = Compress.Cache.none): Sources =
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
    47
      new Sources(
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
    48
        session_base.session_sources.foldLeft(Map.empty) {
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
    49
          case (sources, (path, digest)) =>
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
    50
            def err(): Nothing = error("Incoherent digest for source file: " + path)
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
    51
            val name = File.symbolic_path(path)
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
    52
            sources.get(name) match {
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
    53
              case Some(source_file) =>
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
    54
                if (source_file.digest == digest) sources else err()
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
    55
              case None =>
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
    56
                val bytes = Bytes.read(path)
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
    57
                if (bytes.sha1_digest == digest) {
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
    58
                  val (compressed, body) =
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
    59
                    bytes.maybe_compress(Compress.Options_Zstd(), cache = cache)
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
    60
                  val file = Source_File(name, digest, compressed, body, cache)
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
    61
                  sources + (name -> file)
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
    62
                }
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
    63
                else err()
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
    64
            }
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
    65
        })
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
    66
  }
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
    67
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
    68
  class Sources private(rep: Map[String, Source_File]) extends Iterable[Source_File] {
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
    69
    override def toString: String = rep.values.toList.sortBy(_.name).mkString("Sources(", ", ", ")")
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
    70
    override def iterator: Iterator[Source_File] = rep.valuesIterator
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
    71
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
    72
    def get(name: String): Option[Source_File] = rep.get(name)
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
    73
    def apply(name: String): Source_File =
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
    74
      get(name).getOrElse(error("Missing session sources entry " + quote(name)))
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
    75
  }
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
    76
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
    77
78179
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
    78
  /* SQL data model */
78178
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
    79
78179
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
    80
  object Data {
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
    81
    object Session_Info {
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
    82
      val session_name = SQL.Column.string("session_name").make_primary_key
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
    83
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
    84
      // Build_Log.Session_Info
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
    85
      val session_timing = SQL.Column.bytes("session_timing")
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
    86
      val command_timings = SQL.Column.bytes("command_timings")
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
    87
      val theory_timings = SQL.Column.bytes("theory_timings")
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
    88
      val ml_statistics = SQL.Column.bytes("ml_statistics")
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
    89
      val task_statistics = SQL.Column.bytes("task_statistics")
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
    90
      val errors = SQL.Column.bytes("errors")
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
    91
      val build_log_columns =
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
    92
        List(session_name, session_timing, command_timings, theory_timings,
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
    93
          ml_statistics, task_statistics, errors)
78178
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
    94
78179
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
    95
      // Build_Info
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
    96
      val sources = SQL.Column.string("sources")
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
    97
      val input_heaps = SQL.Column.string("input_heaps")
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
    98
      val output_heap = SQL.Column.string("output_heap")
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
    99
      val return_code = SQL.Column.int("return_code")
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
   100
      val uuid = SQL.Column.string("uuid")
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
   101
      val build_columns = List(sources, input_heaps, output_heap, return_code, uuid)
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
   102
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
   103
      val table = SQL.Table("isabelle_session_info", build_log_columns ::: build_columns)
78178
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   104
78179
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
   105
      val augment_table: PostgreSQL.Source =
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
   106
        "ALTER TABLE IF EXISTS " + table.ident +
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
   107
        " ADD COLUMN IF NOT EXISTS " + uuid.decl(SQL.sql_type_postgresql)
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
   108
    }
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
   109
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
   110
    object Sources {
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
   111
      val session_name = SQL.Column.string("session_name").make_primary_key
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
   112
      val name = SQL.Column.string("name").make_primary_key
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
   113
      val digest = SQL.Column.string("digest")
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
   114
      val compressed = SQL.Column.bool("compressed")
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
   115
      val body = SQL.Column.bytes("body")
78178
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   116
78179
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
   117
      val table =
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
   118
        SQL.Table("isabelle_sources", List(session_name, name, digest, compressed, body))
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
   119
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
   120
      def where_equal(session_name: String, name: String = ""): SQL.Source =
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
   121
        SQL.where_and(
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
   122
          Sources.session_name.equal(session_name),
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
   123
          if_proper(name, Sources.name.equal(name)))
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
   124
    }
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
   125
78181
c2fbe48e9df4 clarified modules;
wenzelm
parents: 78180
diff changeset
   126
    def write_session_info(
c2fbe48e9df4 clarified modules;
wenzelm
parents: 78180
diff changeset
   127
      db: SQL.Database,
c2fbe48e9df4 clarified modules;
wenzelm
parents: 78180
diff changeset
   128
      cache: Compress.Cache,
c2fbe48e9df4 clarified modules;
wenzelm
parents: 78180
diff changeset
   129
      session_name: String,
c2fbe48e9df4 clarified modules;
wenzelm
parents: 78180
diff changeset
   130
      build_log: Build_Log.Session_Info,
c2fbe48e9df4 clarified modules;
wenzelm
parents: 78180
diff changeset
   131
      build: Build_Info
c2fbe48e9df4 clarified modules;
wenzelm
parents: 78180
diff changeset
   132
    ): Unit = {
c2fbe48e9df4 clarified modules;
wenzelm
parents: 78180
diff changeset
   133
      db.execute_statement(Store.Data.Session_Info.table.insert(), body =
c2fbe48e9df4 clarified modules;
wenzelm
parents: 78180
diff changeset
   134
        { stmt =>
c2fbe48e9df4 clarified modules;
wenzelm
parents: 78180
diff changeset
   135
          stmt.string(1) = session_name
c2fbe48e9df4 clarified modules;
wenzelm
parents: 78180
diff changeset
   136
          stmt.bytes(2) = Properties.encode(build_log.session_timing)
c2fbe48e9df4 clarified modules;
wenzelm
parents: 78180
diff changeset
   137
          stmt.bytes(3) = Properties.compress(build_log.command_timings, cache = cache)
c2fbe48e9df4 clarified modules;
wenzelm
parents: 78180
diff changeset
   138
          stmt.bytes(4) = Properties.compress(build_log.theory_timings, cache = cache)
c2fbe48e9df4 clarified modules;
wenzelm
parents: 78180
diff changeset
   139
          stmt.bytes(5) = Properties.compress(build_log.ml_statistics, cache = cache)
c2fbe48e9df4 clarified modules;
wenzelm
parents: 78180
diff changeset
   140
          stmt.bytes(6) = Properties.compress(build_log.task_statistics, cache = cache)
c2fbe48e9df4 clarified modules;
wenzelm
parents: 78180
diff changeset
   141
          stmt.bytes(7) = Build_Log.compress_errors(build_log.errors, cache = cache)
c2fbe48e9df4 clarified modules;
wenzelm
parents: 78180
diff changeset
   142
          stmt.string(8) = build.sources.toString
c2fbe48e9df4 clarified modules;
wenzelm
parents: 78180
diff changeset
   143
          stmt.string(9) = build.input_heaps.toString
c2fbe48e9df4 clarified modules;
wenzelm
parents: 78180
diff changeset
   144
          stmt.string(10) = build.output_heap.toString
c2fbe48e9df4 clarified modules;
wenzelm
parents: 78180
diff changeset
   145
          stmt.int(11) = build.return_code
c2fbe48e9df4 clarified modules;
wenzelm
parents: 78180
diff changeset
   146
          stmt.string(12) = build.uuid
c2fbe48e9df4 clarified modules;
wenzelm
parents: 78180
diff changeset
   147
        })
c2fbe48e9df4 clarified modules;
wenzelm
parents: 78180
diff changeset
   148
    }
c2fbe48e9df4 clarified modules;
wenzelm
parents: 78180
diff changeset
   149
78179
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
   150
    def write_sources(db: SQL.Database, session_name: String, sources: Sources): Unit =
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
   151
      for (source_file <- sources) {
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
   152
        db.execute_statement(Sources.table.insert(), body =
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
   153
          { stmt =>
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
   154
            stmt.string(1) = session_name
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
   155
            stmt.string(2) = source_file.name
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
   156
            stmt.string(3) = source_file.digest.toString
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
   157
            stmt.bool(4) = source_file.compressed
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
   158
            stmt.bytes(5) = source_file.body
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
   159
          })
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
   160
      }
78178
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   161
78179
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
   162
    def read_sources(
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
   163
      db: SQL.Database,
78180
02c5488b8c9c tuned signature;
wenzelm
parents: 78179
diff changeset
   164
      cache: Compress.Cache,
78179
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
   165
      session_name: String,
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
   166
      name: String = ""
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
   167
    ): List[Source_File] = {
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
   168
      db.execute_query_statement(
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
   169
        Sources.table.select(
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
   170
          sql = Sources.where_equal(session_name, name = name) + SQL.order_by(List(Sources.name))),
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
   171
        List.from[Source_File],
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
   172
        { res =>
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
   173
          val res_name = res.string(Sources.name)
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
   174
          val digest = SHA1.fake_digest(res.string(Sources.digest))
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
   175
          val compressed = res.bool(Sources.compressed)
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
   176
          val body = res.bytes(Sources.body)
78180
02c5488b8c9c tuned signature;
wenzelm
parents: 78179
diff changeset
   177
          Source_File(res_name, digest, compressed, body, cache)
78179
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
   178
        }
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
   179
      )
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
   180
    }
78178
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   181
78179
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
   182
    val all_tables: SQL.Tables =
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
   183
      SQL.Tables(Session_Info.table, Sources.table, Export.Data.table, Document_Build.Data.table)
78178
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   184
  }
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   185
}
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   186
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   187
class Store private(val options: Options, val cache: Term.Cache) {
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   188
  store =>
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   189
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   190
  override def toString: String = "Store(output_dir = " + output_dir.absolute + ")"
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   191
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   192
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   193
  /* directories */
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   194
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   195
  val system_output_dir: Path = Path.explode("$ISABELLE_HEAPS_SYSTEM/$ML_IDENTIFIER")
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   196
  val user_output_dir: Path = Path.explode("$ISABELLE_HEAPS/$ML_IDENTIFIER")
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   197
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   198
  def system_heaps: Boolean = options.bool("system_heaps")
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   199
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   200
  val output_dir: Path =
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   201
    if (system_heaps) system_output_dir else user_output_dir
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   202
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   203
  val input_dirs: List[Path] =
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   204
    if (system_heaps) List(system_output_dir)
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   205
    else List(user_output_dir, system_output_dir)
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   206
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   207
  def presentation_dir: Path =
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   208
    if (system_heaps) Path.explode("$ISABELLE_BROWSER_INFO_SYSTEM")
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   209
    else Path.explode("$ISABELLE_BROWSER_INFO")
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   210
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   211
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   212
  /* file names */
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   213
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   214
  def heap(name: String): Path = Path.basic(name)
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   215
  def database(name: String): Path = Path.basic("log") + Path.basic(name).db
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   216
  def log(name: String): Path = Path.basic("log") + Path.basic(name)
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   217
  def log_gz(name: String): Path = log(name).gz
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   218
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   219
  def output_heap(name: String): Path = output_dir + heap(name)
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   220
  def output_database(name: String): Path = output_dir + database(name)
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   221
  def output_log(name: String): Path = output_dir + log(name)
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   222
  def output_log_gz(name: String): Path = output_dir + log_gz(name)
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   223
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   224
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   225
  /* heap */
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   226
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   227
  def find_heap(name: String): Option[Path] =
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   228
    input_dirs.map(_ + heap(name)).find(_.is_file)
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   229
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   230
  def find_heap_shasum(name: String): SHA1.Shasum =
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   231
    (for {
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   232
      path <- find_heap(name)
78182
31835adf148a tuned signature;
wenzelm
parents: 78181
diff changeset
   233
      digest <- ML_Heap.read_file_digest(path)
78178
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   234
    } yield SHA1.shasum(digest, name)).getOrElse(SHA1.no_shasum)
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   235
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   236
  def the_heap(name: String): Path =
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   237
    find_heap(name) getOrElse
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   238
      error("Missing heap image for session " + quote(name) + " -- expected in:\n" +
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   239
        cat_lines(input_dirs.map(dir => "  " + File.standard_path(dir))))
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   240
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   241
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   242
  /* databases for build process and session content */
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   243
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   244
  def find_database(name: String): Option[Path] =
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   245
    input_dirs.map(_ + database(name)).find(_.is_file)
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   246
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   247
  def build_database_server: Boolean = options.bool("build_database_server")
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   248
  def build_database_test: Boolean = options.bool("build_database_test")
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   249
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   250
  def open_database_server(): PostgreSQL.Database =
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   251
    PostgreSQL.open_database(
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   252
      user = options.string("build_database_user"),
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   253
      password = options.string("build_database_password"),
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   254
      database = options.string("build_database_name"),
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   255
      host = options.string("build_database_host"),
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   256
      port = options.int("build_database_port"),
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   257
      ssh =
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   258
        proper_string(options.string("build_database_ssh_host")).map(ssh_host =>
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   259
          SSH.open_session(options,
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   260
            host = ssh_host,
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   261
            user = options.string("build_database_ssh_user"),
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   262
            port = options.int("build_database_ssh_port"))),
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   263
      ssh_close = true)
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   264
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   265
  def open_build_database(path: Path): SQL.Database =
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   266
    if (build_database_server) open_database_server()
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   267
    else SQLite.open_database(path, restrict = true)
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   268
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   269
  def maybe_open_build_database(path: Path): Option[SQL.Database] =
78184
wenzelm
parents: 78182
diff changeset
   270
    if (build_database_test) Some(open_build_database(path)) else None
wenzelm
parents: 78182
diff changeset
   271
wenzelm
parents: 78182
diff changeset
   272
  def maybe_open_heaps_database(): Option[SQL.Database] =
wenzelm
parents: 78182
diff changeset
   273
    if (build_database_test && build_database_server) Some(open_database_server()) else None
78178
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   274
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   275
  def try_open_database(
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   276
    name: String,
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   277
    output: Boolean = false,
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   278
    server: Boolean = build_database_server
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   279
  ): Option[SQL.Database] = {
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   280
    def check(db: SQL.Database): Option[SQL.Database] =
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   281
      if (output || session_info_exists(db)) Some(db) else { db.close(); None }
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   282
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   283
    if (server) check(open_database_server())
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   284
    else if (output) Some(SQLite.open_database(output_database(name)))
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   285
    else {
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   286
      (for {
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   287
        dir <- input_dirs.view
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   288
        path = dir + database(name) if path.is_file
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   289
        db <- check(SQLite.open_database(path))
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   290
      } yield db).headOption
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   291
    }
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   292
  }
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   293
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   294
  def error_database(name: String): Nothing =
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   295
    error("Missing build database for session " + quote(name))
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   296
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   297
  def open_database(name: String, output: Boolean = false): SQL.Database =
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   298
    try_open_database(name, output = output) getOrElse error_database(name)
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   299
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   300
  def prepare_output(): Unit = Isabelle_System.make_directory(output_dir + Path.basic("log"))
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   301
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   302
  def clean_output(name: String): Option[Boolean] = {
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   303
    val relevant_db =
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   304
      build_database_server &&
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   305
        using_option(try_open_database(name))(init_session_info(_, name)).getOrElse(false)
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   306
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   307
    val del =
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   308
      for {
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   309
        dir <-
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   310
          (if (system_heaps) List(user_output_dir, system_output_dir) else List(user_output_dir))
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   311
        file <- List(heap(name), database(name), log(name), log_gz(name))
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   312
        path = dir + file if path.is_file
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   313
      } yield path.file.delete
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   314
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   315
    if (relevant_db || del.nonEmpty) Some(del.forall(identity)) else None
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   316
  }
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   317
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   318
  def init_output(name: String): Unit = {
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   319
    clean_output(name)
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   320
    using(open_database(name, output = true))(init_session_info(_, name))
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   321
  }
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   322
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   323
  def check_output(
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   324
    name: String,
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   325
    session_options: Options,
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   326
    sources_shasum: SHA1.Shasum,
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   327
    input_shasum: SHA1.Shasum,
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   328
    fresh_build: Boolean,
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   329
    store_heap: Boolean
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   330
  ): (Boolean, SHA1.Shasum) = {
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   331
    try_open_database(name) match {
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   332
      case Some(db) =>
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   333
        using(db)(read_build(_, name)) match {
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   334
          case Some(build) =>
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   335
            val output_shasum = find_heap_shasum(name)
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   336
            val current =
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   337
              !fresh_build &&
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   338
              build.ok &&
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   339
              Sessions.eq_sources(session_options, build.sources, sources_shasum) &&
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   340
              build.input_heaps == input_shasum &&
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   341
              build.output_heap == output_shasum &&
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   342
              !(store_heap && output_shasum.is_empty)
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   343
            (current, output_shasum)
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   344
          case None => (false, SHA1.no_shasum)
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   345
        }
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   346
      case None => (false, SHA1.no_shasum)
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   347
    }
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   348
  }
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   349
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   350
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   351
  /* SQL database content */
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   352
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   353
  def read_bytes(db: SQL.Database, name: String, column: SQL.Column): Bytes =
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   354
    db.execute_query_statementO[Bytes](
78179
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
   355
      Store.Data.Session_Info.table.select(List(column),
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
   356
        sql = Store.Data.Session_Info.session_name.where_equal(name)),
78178
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   357
      res => res.bytes(column)
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   358
    ).getOrElse(Bytes.empty)
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   359
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   360
  def read_properties(db: SQL.Database, name: String, column: SQL.Column): List[Properties.T] =
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   361
    Properties.uncompress(read_bytes(db, name, column), cache = cache)
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   362
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   363
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   364
  /* session info */
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   365
78181
c2fbe48e9df4 clarified modules;
wenzelm
parents: 78180
diff changeset
   366
  def session_info_exists(db: SQL.Database): Boolean = {
c2fbe48e9df4 clarified modules;
wenzelm
parents: 78180
diff changeset
   367
    val tables = db.tables
c2fbe48e9df4 clarified modules;
wenzelm
parents: 78180
diff changeset
   368
    Store.Data.all_tables.forall(table => tables.contains(table.name))
c2fbe48e9df4 clarified modules;
wenzelm
parents: 78180
diff changeset
   369
  }
c2fbe48e9df4 clarified modules;
wenzelm
parents: 78180
diff changeset
   370
c2fbe48e9df4 clarified modules;
wenzelm
parents: 78180
diff changeset
   371
  def session_info_defined(db: SQL.Database, name: String): Boolean =
c2fbe48e9df4 clarified modules;
wenzelm
parents: 78180
diff changeset
   372
    db.execute_query_statementB(
c2fbe48e9df4 clarified modules;
wenzelm
parents: 78180
diff changeset
   373
      Store.Data.Session_Info.table.select(List(Store.Data.Session_Info.session_name),
c2fbe48e9df4 clarified modules;
wenzelm
parents: 78180
diff changeset
   374
        sql = Store.Data.Session_Info.session_name.where_equal(name)))
c2fbe48e9df4 clarified modules;
wenzelm
parents: 78180
diff changeset
   375
78178
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   376
  def init_session_info(db: SQL.Database, name: String): Boolean =
78179
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
   377
    db.transaction_lock(Store.Data.all_tables, create = true) {
78178
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   378
      val already_defined = session_info_defined(db, name)
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   379
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   380
      db.execute_statement(
78179
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
   381
        Store.Data.Session_Info.table.delete(
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
   382
          sql = Store.Data.Session_Info.session_name.where_equal(name)))
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
   383
      if (db.is_postgresql) db.execute_statement(Store.Data.Session_Info.augment_table)
78178
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   384
78179
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
   385
      db.execute_statement(Store.Data.Sources.table.delete(
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
   386
        sql = Store.Data.Sources.where_equal(name)))
78178
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   387
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   388
      db.execute_statement(
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   389
        Export.Data.table.delete(sql = Export.Data.session_name.where_equal(name)))
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   390
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   391
      db.execute_statement(
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   392
        Document_Build.Data.table.delete(sql = Document_Build.Data.session_name.where_equal(name)))
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   393
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   394
      already_defined
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   395
    }
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   396
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   397
  def write_session_info(
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   398
    db: SQL.Database,
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   399
    session_name: String,
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   400
    sources: Store.Sources,
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   401
    build_log: Build_Log.Session_Info,
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   402
    build: Store.Build_Info
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   403
  ): Unit = {
78179
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
   404
    db.transaction_lock(Store.Data.all_tables) {
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
   405
      Store.Data.write_sources(db, session_name, sources)
78181
c2fbe48e9df4 clarified modules;
wenzelm
parents: 78180
diff changeset
   406
      Store.Data.write_session_info(db, cache.compress, session_name, build_log, build)
78178
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   407
    }
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   408
  }
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   409
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   410
  def read_session_timing(db: SQL.Database, name: String): Properties.T =
78179
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
   411
    Properties.decode(read_bytes(db, name, Store.Data.Session_Info.session_timing), cache = cache)
78178
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   412
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   413
  def read_command_timings(db: SQL.Database, name: String): Bytes =
78179
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
   414
    read_bytes(db, name, Store.Data.Session_Info.command_timings)
78178
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   415
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   416
  def read_theory_timings(db: SQL.Database, name: String): List[Properties.T] =
78179
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
   417
    read_properties(db, name, Store.Data.Session_Info.theory_timings)
78178
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   418
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   419
  def read_ml_statistics(db: SQL.Database, name: String): List[Properties.T] =
78179
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
   420
    read_properties(db, name, Store.Data.Session_Info.ml_statistics)
78178
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   421
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   422
  def read_task_statistics(db: SQL.Database, name: String): List[Properties.T] =
78179
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
   423
    read_properties(db, name, Store.Data.Session_Info.task_statistics)
78178
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   424
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   425
  def read_theories(db: SQL.Database, name: String): List[String] =
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   426
    read_theory_timings(db, name).flatMap(Markup.Name.unapply)
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   427
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   428
  def read_errors(db: SQL.Database, name: String): List[String] =
78179
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
   429
    Build_Log.uncompress_errors(read_bytes(db, name, Store.Data.Session_Info.errors), cache = cache)
78178
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   430
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   431
  def read_build(db: SQL.Database, name: String): Option[Store.Build_Info] = {
78179
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
   432
    if (db.tables.contains(Store.Data.Session_Info.table.name)) {
78178
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   433
      db.execute_query_statementO[Store.Build_Info](
78179
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
   434
        Store.Data.Session_Info.table.select(
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
   435
          sql = Store.Data.Session_Info.session_name.where_equal(name)),
78178
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   436
        { res =>
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   437
          val uuid =
78179
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
   438
            try { Option(res.string(Store.Data.Session_Info.uuid)).getOrElse("") }
78178
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   439
            catch { case _: SQLException => "" }
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   440
          Store.Build_Info(
78179
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
   441
            SHA1.fake_shasum(res.string(Store.Data.Session_Info.sources)),
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
   442
            SHA1.fake_shasum(res.string(Store.Data.Session_Info.input_heaps)),
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
   443
            SHA1.fake_shasum(res.string(Store.Data.Session_Info.output_heap)),
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
   444
            res.int(Store.Data.Session_Info.return_code),
78178
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   445
            uuid)
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   446
        }
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   447
      )
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   448
    }
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   449
    else None
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   450
  }
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   451
78179
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
   452
  def read_sources(db: SQL.Database, session: String, name: String = ""): List[Store.Source_File] =
78180
02c5488b8c9c tuned signature;
wenzelm
parents: 78179
diff changeset
   453
    Store.Data.read_sources(db, cache.compress, session, name = name)
78178
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   454
}