src/Pure/Thy/store.scala
author wenzelm
Sat, 08 Jul 2023 19:32:57 +0200
changeset 78272 30d035a83dbe
parent 78265 03eb7f7bb990
child 78347 72fc2ff08e07
permissions -rw-r--r--
eliminate somewhat obsolete augment_tables (see ff164add75cd), to support obsolete versions 10 and 9.x;
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
78187
2df0f3604a67 clarified signature: more explicit class SQL.Data;
wenzelm
parents: 78186
diff changeset
    80
  object Data extends SQL.Data() {
78260
0a7f7abbe4f0 more robust transaction_lock: avoid overlapping data spaces;
wenzelm
parents: 78227
diff changeset
    81
    override lazy val tables = SQL.Tables(Session_Info.table, Sources.table)
78187
2df0f3604a67 clarified signature: more explicit class SQL.Data;
wenzelm
parents: 78186
diff changeset
    82
78179
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
    83
    object Session_Info {
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
    84
      val session_name = SQL.Column.string("session_name").make_primary_key
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
    85
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
    86
      // Build_Log.Session_Info
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
    87
      val session_timing = SQL.Column.bytes("session_timing")
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
    88
      val command_timings = SQL.Column.bytes("command_timings")
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
    89
      val theory_timings = SQL.Column.bytes("theory_timings")
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
    90
      val ml_statistics = SQL.Column.bytes("ml_statistics")
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
    91
      val task_statistics = SQL.Column.bytes("task_statistics")
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
    92
      val errors = SQL.Column.bytes("errors")
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
    93
      val build_log_columns =
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
    94
        List(session_name, session_timing, command_timings, theory_timings,
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
    95
          ml_statistics, task_statistics, errors)
78178
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
    96
78179
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
    97
      // Build_Info
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
    98
      val sources = SQL.Column.string("sources")
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
    99
      val input_heaps = SQL.Column.string("input_heaps")
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
   100
      val output_heap = SQL.Column.string("output_heap")
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
   101
      val return_code = SQL.Column.int("return_code")
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
   102
      val uuid = SQL.Column.string("uuid")
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
   103
      val build_columns = List(sources, input_heaps, output_heap, return_code, uuid)
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
   104
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
   105
      val table = SQL.Table("isabelle_session_info", build_log_columns ::: build_columns)
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
   106
    }
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
   107
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
   108
    object Sources {
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
   109
      val session_name = SQL.Column.string("session_name").make_primary_key
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
   110
      val name = SQL.Column.string("name").make_primary_key
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
   111
      val digest = SQL.Column.string("digest")
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
   112
      val compressed = SQL.Column.bool("compressed")
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
   113
      val body = SQL.Column.bytes("body")
78178
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   114
78179
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
   115
      val table =
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
   116
        SQL.Table("isabelle_sources", List(session_name, name, digest, compressed, body))
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
   117
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
   118
      def where_equal(session_name: String, name: String = ""): SQL.Source =
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
   119
        SQL.where_and(
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
   120
          Sources.session_name.equal(session_name),
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
   121
          if_proper(name, Sources.name.equal(name)))
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
   122
    }
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
   123
78265
03eb7f7bb990 proper transaction_lock;
wenzelm
parents: 78262
diff changeset
   124
    def read_bytes(db: SQL.Database, name: String, column: SQL.Column): Bytes =
03eb7f7bb990 proper transaction_lock;
wenzelm
parents: 78262
diff changeset
   125
      db.execute_query_statementO[Bytes](
03eb7f7bb990 proper transaction_lock;
wenzelm
parents: 78262
diff changeset
   126
        Session_Info.table.select(List(column), sql = Session_Info.session_name.where_equal(name)),
03eb7f7bb990 proper transaction_lock;
wenzelm
parents: 78262
diff changeset
   127
        res => res.bytes(column)
03eb7f7bb990 proper transaction_lock;
wenzelm
parents: 78262
diff changeset
   128
      ).getOrElse(Bytes.empty)
03eb7f7bb990 proper transaction_lock;
wenzelm
parents: 78262
diff changeset
   129
03eb7f7bb990 proper transaction_lock;
wenzelm
parents: 78262
diff changeset
   130
    def read_properties(
03eb7f7bb990 proper transaction_lock;
wenzelm
parents: 78262
diff changeset
   131
      db: SQL.Database, name: String, column: SQL.Column, cache: Term.Cache
03eb7f7bb990 proper transaction_lock;
wenzelm
parents: 78262
diff changeset
   132
    ): List[Properties.T] = Properties.uncompress(read_bytes(db, name, column), cache = cache)
03eb7f7bb990 proper transaction_lock;
wenzelm
parents: 78262
diff changeset
   133
03eb7f7bb990 proper transaction_lock;
wenzelm
parents: 78262
diff changeset
   134
    def read_session_timing(db: SQL.Database, name: String, cache: Term.Cache): Properties.T =
03eb7f7bb990 proper transaction_lock;
wenzelm
parents: 78262
diff changeset
   135
      Properties.decode(read_bytes(db, name, Session_Info.session_timing), cache = cache)
03eb7f7bb990 proper transaction_lock;
wenzelm
parents: 78262
diff changeset
   136
03eb7f7bb990 proper transaction_lock;
wenzelm
parents: 78262
diff changeset
   137
    def read_command_timings(db: SQL.Database, name: String): Bytes =
03eb7f7bb990 proper transaction_lock;
wenzelm
parents: 78262
diff changeset
   138
      read_bytes(db, name, Session_Info.command_timings)
03eb7f7bb990 proper transaction_lock;
wenzelm
parents: 78262
diff changeset
   139
03eb7f7bb990 proper transaction_lock;
wenzelm
parents: 78262
diff changeset
   140
    def read_theory_timings(db: SQL.Database, name: String, cache: Term.Cache): List[Properties.T] =
03eb7f7bb990 proper transaction_lock;
wenzelm
parents: 78262
diff changeset
   141
      read_properties(db, name, Session_Info.theory_timings, cache)
03eb7f7bb990 proper transaction_lock;
wenzelm
parents: 78262
diff changeset
   142
03eb7f7bb990 proper transaction_lock;
wenzelm
parents: 78262
diff changeset
   143
    def read_ml_statistics(db: SQL.Database, name: String, cache: Term.Cache): List[Properties.T] =
03eb7f7bb990 proper transaction_lock;
wenzelm
parents: 78262
diff changeset
   144
      read_properties(db, name, Session_Info.ml_statistics, cache)
03eb7f7bb990 proper transaction_lock;
wenzelm
parents: 78262
diff changeset
   145
03eb7f7bb990 proper transaction_lock;
wenzelm
parents: 78262
diff changeset
   146
    def read_task_statistics(db: SQL.Database, name: String, cache: Term.Cache): List[Properties.T] =
03eb7f7bb990 proper transaction_lock;
wenzelm
parents: 78262
diff changeset
   147
      read_properties(db, name, Session_Info.task_statistics, cache)
03eb7f7bb990 proper transaction_lock;
wenzelm
parents: 78262
diff changeset
   148
03eb7f7bb990 proper transaction_lock;
wenzelm
parents: 78262
diff changeset
   149
    def read_errors(db: SQL.Database, name: String, cache: Term.Cache): List[String] =
03eb7f7bb990 proper transaction_lock;
wenzelm
parents: 78262
diff changeset
   150
      Build_Log.uncompress_errors(read_bytes(db, name, Session_Info.errors), cache = cache)
03eb7f7bb990 proper transaction_lock;
wenzelm
parents: 78262
diff changeset
   151
03eb7f7bb990 proper transaction_lock;
wenzelm
parents: 78262
diff changeset
   152
    def read_build(db: SQL.Database, name: String): Option[Store.Build_Info] = {
03eb7f7bb990 proper transaction_lock;
wenzelm
parents: 78262
diff changeset
   153
      if (db.tables.contains(Session_Info.table.name)) {
03eb7f7bb990 proper transaction_lock;
wenzelm
parents: 78262
diff changeset
   154
        db.execute_query_statementO[Store.Build_Info](
03eb7f7bb990 proper transaction_lock;
wenzelm
parents: 78262
diff changeset
   155
          Session_Info.table.select(sql = Session_Info.session_name.where_equal(name)),
03eb7f7bb990 proper transaction_lock;
wenzelm
parents: 78262
diff changeset
   156
          { res =>
03eb7f7bb990 proper transaction_lock;
wenzelm
parents: 78262
diff changeset
   157
            val uuid =
03eb7f7bb990 proper transaction_lock;
wenzelm
parents: 78262
diff changeset
   158
              try { Option(res.string(Session_Info.uuid)).getOrElse("") }
03eb7f7bb990 proper transaction_lock;
wenzelm
parents: 78262
diff changeset
   159
              catch { case _: SQLException => "" }
03eb7f7bb990 proper transaction_lock;
wenzelm
parents: 78262
diff changeset
   160
            Store.Build_Info(
03eb7f7bb990 proper transaction_lock;
wenzelm
parents: 78262
diff changeset
   161
              SHA1.fake_shasum(res.string(Session_Info.sources)),
03eb7f7bb990 proper transaction_lock;
wenzelm
parents: 78262
diff changeset
   162
              SHA1.fake_shasum(res.string(Session_Info.input_heaps)),
03eb7f7bb990 proper transaction_lock;
wenzelm
parents: 78262
diff changeset
   163
              SHA1.fake_shasum(res.string(Session_Info.output_heap)),
03eb7f7bb990 proper transaction_lock;
wenzelm
parents: 78262
diff changeset
   164
              res.int(Session_Info.return_code),
03eb7f7bb990 proper transaction_lock;
wenzelm
parents: 78262
diff changeset
   165
              uuid)
03eb7f7bb990 proper transaction_lock;
wenzelm
parents: 78262
diff changeset
   166
          }
03eb7f7bb990 proper transaction_lock;
wenzelm
parents: 78262
diff changeset
   167
        )
03eb7f7bb990 proper transaction_lock;
wenzelm
parents: 78262
diff changeset
   168
      }
03eb7f7bb990 proper transaction_lock;
wenzelm
parents: 78262
diff changeset
   169
      else None
03eb7f7bb990 proper transaction_lock;
wenzelm
parents: 78262
diff changeset
   170
    }
03eb7f7bb990 proper transaction_lock;
wenzelm
parents: 78262
diff changeset
   171
78181
c2fbe48e9df4 clarified modules;
wenzelm
parents: 78180
diff changeset
   172
    def write_session_info(
c2fbe48e9df4 clarified modules;
wenzelm
parents: 78180
diff changeset
   173
      db: SQL.Database,
c2fbe48e9df4 clarified modules;
wenzelm
parents: 78180
diff changeset
   174
      cache: Compress.Cache,
c2fbe48e9df4 clarified modules;
wenzelm
parents: 78180
diff changeset
   175
      session_name: String,
c2fbe48e9df4 clarified modules;
wenzelm
parents: 78180
diff changeset
   176
      build_log: Build_Log.Session_Info,
c2fbe48e9df4 clarified modules;
wenzelm
parents: 78180
diff changeset
   177
      build: Build_Info
c2fbe48e9df4 clarified modules;
wenzelm
parents: 78180
diff changeset
   178
    ): Unit = {
78262
wenzelm
parents: 78260
diff changeset
   179
      db.execute_statement(Session_Info.table.insert(), body =
78181
c2fbe48e9df4 clarified modules;
wenzelm
parents: 78180
diff changeset
   180
        { stmt =>
c2fbe48e9df4 clarified modules;
wenzelm
parents: 78180
diff changeset
   181
          stmt.string(1) = session_name
c2fbe48e9df4 clarified modules;
wenzelm
parents: 78180
diff changeset
   182
          stmt.bytes(2) = Properties.encode(build_log.session_timing)
c2fbe48e9df4 clarified modules;
wenzelm
parents: 78180
diff changeset
   183
          stmt.bytes(3) = Properties.compress(build_log.command_timings, cache = cache)
c2fbe48e9df4 clarified modules;
wenzelm
parents: 78180
diff changeset
   184
          stmt.bytes(4) = Properties.compress(build_log.theory_timings, cache = cache)
c2fbe48e9df4 clarified modules;
wenzelm
parents: 78180
diff changeset
   185
          stmt.bytes(5) = Properties.compress(build_log.ml_statistics, cache = cache)
c2fbe48e9df4 clarified modules;
wenzelm
parents: 78180
diff changeset
   186
          stmt.bytes(6) = Properties.compress(build_log.task_statistics, cache = cache)
c2fbe48e9df4 clarified modules;
wenzelm
parents: 78180
diff changeset
   187
          stmt.bytes(7) = Build_Log.compress_errors(build_log.errors, cache = cache)
c2fbe48e9df4 clarified modules;
wenzelm
parents: 78180
diff changeset
   188
          stmt.string(8) = build.sources.toString
c2fbe48e9df4 clarified modules;
wenzelm
parents: 78180
diff changeset
   189
          stmt.string(9) = build.input_heaps.toString
c2fbe48e9df4 clarified modules;
wenzelm
parents: 78180
diff changeset
   190
          stmt.string(10) = build.output_heap.toString
c2fbe48e9df4 clarified modules;
wenzelm
parents: 78180
diff changeset
   191
          stmt.int(11) = build.return_code
c2fbe48e9df4 clarified modules;
wenzelm
parents: 78180
diff changeset
   192
          stmt.string(12) = build.uuid
c2fbe48e9df4 clarified modules;
wenzelm
parents: 78180
diff changeset
   193
        })
c2fbe48e9df4 clarified modules;
wenzelm
parents: 78180
diff changeset
   194
    }
c2fbe48e9df4 clarified modules;
wenzelm
parents: 78180
diff changeset
   195
78179
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
   196
    def write_sources(db: SQL.Database, session_name: String, sources: Sources): Unit =
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
   197
      for (source_file <- sources) {
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
   198
        db.execute_statement(Sources.table.insert(), body =
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
   199
          { stmt =>
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
   200
            stmt.string(1) = session_name
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
   201
            stmt.string(2) = source_file.name
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
   202
            stmt.string(3) = source_file.digest.toString
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
   203
            stmt.bool(4) = source_file.compressed
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
   204
            stmt.bytes(5) = source_file.body
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
   205
          })
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
   206
      }
78178
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   207
78179
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
   208
    def read_sources(
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
   209
      db: SQL.Database,
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
   210
      session_name: String,
78265
03eb7f7bb990 proper transaction_lock;
wenzelm
parents: 78262
diff changeset
   211
      name: String,
03eb7f7bb990 proper transaction_lock;
wenzelm
parents: 78262
diff changeset
   212
      cache: Compress.Cache
78179
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
   213
    ): List[Source_File] = {
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
   214
      db.execute_query_statement(
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
   215
        Sources.table.select(
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
   216
          sql = Sources.where_equal(session_name, name = name) + SQL.order_by(List(Sources.name))),
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
   217
        List.from[Source_File],
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
   218
        { res =>
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
   219
          val res_name = res.string(Sources.name)
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
   220
          val digest = SHA1.fake_digest(res.string(Sources.digest))
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
   221
          val compressed = res.bool(Sources.compressed)
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
   222
          val body = res.bytes(Sources.body)
78180
02c5488b8c9c tuned signature;
wenzelm
parents: 78179
diff changeset
   223
          Source_File(res_name, digest, compressed, body, cache)
78179
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
   224
        }
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
   225
      )
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
   226
    }
78178
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   227
  }
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   228
}
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   229
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   230
class Store private(val options: Options, val cache: Term.Cache) {
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   231
  store =>
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   232
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   233
  override def toString: String = "Store(output_dir = " + output_dir.absolute + ")"
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   234
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   235
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   236
  /* directories */
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   237
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   238
  val system_output_dir: Path = Path.explode("$ISABELLE_HEAPS_SYSTEM/$ML_IDENTIFIER")
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   239
  val user_output_dir: Path = Path.explode("$ISABELLE_HEAPS/$ML_IDENTIFIER")
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   240
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   241
  def system_heaps: Boolean = options.bool("system_heaps")
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   242
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   243
  val output_dir: Path =
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   244
    if (system_heaps) system_output_dir else user_output_dir
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   245
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   246
  val input_dirs: List[Path] =
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   247
    if (system_heaps) List(system_output_dir)
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   248
    else List(user_output_dir, system_output_dir)
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   249
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   250
  def presentation_dir: Path =
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   251
    if (system_heaps) Path.explode("$ISABELLE_BROWSER_INFO_SYSTEM")
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   252
    else Path.explode("$ISABELLE_BROWSER_INFO")
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   253
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   254
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   255
  /* file names */
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   256
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   257
  def heap(name: String): Path = Path.basic(name)
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   258
  def database(name: String): Path = Path.basic("log") + Path.basic(name).db
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   259
  def log(name: String): Path = Path.basic("log") + Path.basic(name)
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   260
  def log_gz(name: String): Path = log(name).gz
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   261
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   262
  def output_heap(name: String): Path = output_dir + heap(name)
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   263
  def output_database(name: String): Path = output_dir + database(name)
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   264
  def output_log(name: String): Path = output_dir + log(name)
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   265
  def output_log_gz(name: String): Path = output_dir + log_gz(name)
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   266
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   267
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   268
  /* heap */
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   269
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   270
  def find_heap(name: String): Option[Path] =
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   271
    input_dirs.map(_ + heap(name)).find(_.is_file)
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   272
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   273
  def the_heap(name: String): Path =
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   274
    find_heap(name) getOrElse
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   275
      error("Missing heap image for session " + quote(name) + " -- expected in:\n" +
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   276
        cat_lines(input_dirs.map(dir => "  " + File.standard_path(dir))))
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   277
78212
dfb172d7e40e tuned signature;
wenzelm
parents: 78208
diff changeset
   278
  def heap_shasum(database_server: Option[SQL.Database], name: String): SHA1.Shasum = {
dfb172d7e40e tuned signature;
wenzelm
parents: 78208
diff changeset
   279
    def get_database = database_server.flatMap(ML_Heap.get_entry(_, name))
78196
140a6f2e3728 restore heaps from database, which takes precedence over file-system;
wenzelm
parents: 78190
diff changeset
   280
    def get_file = find_heap(name).flatMap(ML_Heap.read_file_digest)
140a6f2e3728 restore heaps from database, which takes precedence over file-system;
wenzelm
parents: 78190
diff changeset
   281
140a6f2e3728 restore heaps from database, which takes precedence over file-system;
wenzelm
parents: 78190
diff changeset
   282
    get_database orElse get_file match {
140a6f2e3728 restore heaps from database, which takes precedence over file-system;
wenzelm
parents: 78190
diff changeset
   283
      case Some(digest) => SHA1.shasum(digest, name)
140a6f2e3728 restore heaps from database, which takes precedence over file-system;
wenzelm
parents: 78190
diff changeset
   284
      case None => SHA1.no_shasum
140a6f2e3728 restore heaps from database, which takes precedence over file-system;
wenzelm
parents: 78190
diff changeset
   285
    }
140a6f2e3728 restore heaps from database, which takes precedence over file-system;
wenzelm
parents: 78190
diff changeset
   286
  }
140a6f2e3728 restore heaps from database, which takes precedence over file-system;
wenzelm
parents: 78190
diff changeset
   287
78178
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   288
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   289
  /* databases for build process and session content */
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   290
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   291
  def find_database(name: String): Option[Path] =
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   292
    input_dirs.map(_ + database(name)).find(_.is_file)
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   293
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   294
  def build_database_server: Boolean = options.bool("build_database_server")
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   295
  def build_database_test: Boolean = options.bool("build_database_test")
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   296
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   297
  def open_database_server(): PostgreSQL.Database =
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   298
    PostgreSQL.open_database(
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   299
      user = options.string("build_database_user"),
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   300
      password = options.string("build_database_password"),
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   301
      database = options.string("build_database_name"),
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   302
      host = options.string("build_database_host"),
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   303
      port = options.int("build_database_port"),
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   304
      ssh =
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   305
        proper_string(options.string("build_database_ssh_host")).map(ssh_host =>
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   306
          SSH.open_session(options,
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   307
            host = ssh_host,
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   308
            user = options.string("build_database_ssh_user"),
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   309
            port = options.int("build_database_ssh_port"))),
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   310
      ssh_close = true)
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   311
78205
a40ae2df39ad clarified database for heaps: do not depend on build_database_test;
wenzelm
parents: 78198
diff changeset
   312
  def maybe_open_database_server(): Option[SQL.Database] =
a40ae2df39ad clarified database for heaps: do not depend on build_database_test;
wenzelm
parents: 78198
diff changeset
   313
    if (build_database_server) Some(open_database_server()) else None
a40ae2df39ad clarified database for heaps: do not depend on build_database_test;
wenzelm
parents: 78198
diff changeset
   314
78178
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   315
  def open_build_database(path: Path): SQL.Database =
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   316
    if (build_database_server) open_database_server()
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   317
    else SQLite.open_database(path, restrict = true)
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   318
78223
2d2417a63314 clarified signature: better default;
wenzelm
parents: 78217
diff changeset
   319
  def maybe_open_build_database(
2d2417a63314 clarified signature: better default;
wenzelm
parents: 78217
diff changeset
   320
    path: Path = Path.explode("$ISABELLE_HOME_USER/build.db")
2d2417a63314 clarified signature: better default;
wenzelm
parents: 78217
diff changeset
   321
  ): Option[SQL.Database] = if (build_database_test) Some(open_build_database(path)) else None
78184
wenzelm
parents: 78182
diff changeset
   322
78178
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   323
  def try_open_database(
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   324
    name: String,
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   325
    output: Boolean = false,
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   326
    server: Boolean = build_database_server
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   327
  ): Option[SQL.Database] = {
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   328
    def check(db: SQL.Database): Option[SQL.Database] =
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   329
      if (output || session_info_exists(db)) Some(db) else { db.close(); None }
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   330
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   331
    if (server) check(open_database_server())
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   332
    else if (output) Some(SQLite.open_database(output_database(name)))
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   333
    else {
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   334
      (for {
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   335
        dir <- input_dirs.view
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   336
        path = dir + database(name) if path.is_file
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   337
        db <- check(SQLite.open_database(path))
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   338
      } yield db).headOption
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   339
    }
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   340
  }
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   341
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   342
  def error_database(name: String): Nothing =
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   343
    error("Missing build database for session " + quote(name))
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   344
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   345
  def open_database(name: String, output: Boolean = false): SQL.Database =
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   346
    try_open_database(name, output = output) getOrElse error_database(name)
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   347
78213
fd0430a7b7a4 avoid repeated open_database_server: synchronized transaction_lock;
wenzelm
parents: 78212
diff changeset
   348
  def clean_output(
fd0430a7b7a4 avoid repeated open_database_server: synchronized transaction_lock;
wenzelm
parents: 78212
diff changeset
   349
    database_server: Option[SQL.Database],
fd0430a7b7a4 avoid repeated open_database_server: synchronized transaction_lock;
wenzelm
parents: 78212
diff changeset
   350
    name: String,
78217
wenzelm
parents: 78215
diff changeset
   351
    session_init: Boolean = false
78213
fd0430a7b7a4 avoid repeated open_database_server: synchronized transaction_lock;
wenzelm
parents: 78212
diff changeset
   352
  ): Option[Boolean] = {
78178
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   353
    val relevant_db =
78213
fd0430a7b7a4 avoid repeated open_database_server: synchronized transaction_lock;
wenzelm
parents: 78212
diff changeset
   354
      database_server match {
78217
wenzelm
parents: 78215
diff changeset
   355
        case Some(db) =>
wenzelm
parents: 78215
diff changeset
   356
          ML_Heap.clean_entry(db, name)
wenzelm
parents: 78215
diff changeset
   357
          clean_session_info(db, name)
78227
1ba48d402005 proper session_init *after* deleting db files (amending af6c493b0441);
wenzelm
parents: 78223
diff changeset
   358
        case None => false
78213
fd0430a7b7a4 avoid repeated open_database_server: synchronized transaction_lock;
wenzelm
parents: 78212
diff changeset
   359
      }
78178
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   360
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   361
    val del =
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   362
      for {
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   363
        dir <-
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   364
          (if (system_heaps) List(user_output_dir, system_output_dir) else List(user_output_dir))
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   365
        file <- List(heap(name), database(name), log(name), log_gz(name))
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   366
        path = dir + file if path.is_file
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   367
      } yield path.file.delete
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   368
78227
1ba48d402005 proper session_init *after* deleting db files (amending af6c493b0441);
wenzelm
parents: 78223
diff changeset
   369
    if (database_server.isEmpty && session_init) {
1ba48d402005 proper session_init *after* deleting db files (amending af6c493b0441);
wenzelm
parents: 78223
diff changeset
   370
      using(open_database(name, output = true))(clean_session_info(_, name))
1ba48d402005 proper session_init *after* deleting db files (amending af6c493b0441);
wenzelm
parents: 78223
diff changeset
   371
    }
1ba48d402005 proper session_init *after* deleting db files (amending af6c493b0441);
wenzelm
parents: 78223
diff changeset
   372
78185
26b9b40ec1af tuned signature;
wenzelm
parents: 78184
diff changeset
   373
    if (relevant_db || del.nonEmpty) Some(del.forall(identity)) else None
78178
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   374
  }
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   375
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   376
  def check_output(
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   377
    name: String,
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   378
    session_options: Options,
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   379
    sources_shasum: SHA1.Shasum,
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   380
    input_shasum: SHA1.Shasum,
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   381
    fresh_build: Boolean,
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   382
    store_heap: Boolean
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   383
  ): (Boolean, SHA1.Shasum) = {
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   384
    try_open_database(name) match {
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   385
      case Some(db) =>
78196
140a6f2e3728 restore heaps from database, which takes precedence over file-system;
wenzelm
parents: 78190
diff changeset
   386
        using(db) { _ =>
140a6f2e3728 restore heaps from database, which takes precedence over file-system;
wenzelm
parents: 78190
diff changeset
   387
          read_build(db, name) match {
140a6f2e3728 restore heaps from database, which takes precedence over file-system;
wenzelm
parents: 78190
diff changeset
   388
            case Some(build) =>
140a6f2e3728 restore heaps from database, which takes precedence over file-system;
wenzelm
parents: 78190
diff changeset
   389
              val output_shasum = heap_shasum(if (db.is_postgresql) Some(db) else None, name)
140a6f2e3728 restore heaps from database, which takes precedence over file-system;
wenzelm
parents: 78190
diff changeset
   390
              val current =
140a6f2e3728 restore heaps from database, which takes precedence over file-system;
wenzelm
parents: 78190
diff changeset
   391
                !fresh_build &&
140a6f2e3728 restore heaps from database, which takes precedence over file-system;
wenzelm
parents: 78190
diff changeset
   392
                  build.ok &&
140a6f2e3728 restore heaps from database, which takes precedence over file-system;
wenzelm
parents: 78190
diff changeset
   393
                  Sessions.eq_sources(session_options, build.sources, sources_shasum) &&
140a6f2e3728 restore heaps from database, which takes precedence over file-system;
wenzelm
parents: 78190
diff changeset
   394
                  build.input_heaps == input_shasum &&
140a6f2e3728 restore heaps from database, which takes precedence over file-system;
wenzelm
parents: 78190
diff changeset
   395
                  build.output_heap == output_shasum &&
140a6f2e3728 restore heaps from database, which takes precedence over file-system;
wenzelm
parents: 78190
diff changeset
   396
                  !(store_heap && output_shasum.is_empty)
140a6f2e3728 restore heaps from database, which takes precedence over file-system;
wenzelm
parents: 78190
diff changeset
   397
              (current, output_shasum)
140a6f2e3728 restore heaps from database, which takes precedence over file-system;
wenzelm
parents: 78190
diff changeset
   398
            case None => (false, SHA1.no_shasum)
140a6f2e3728 restore heaps from database, which takes precedence over file-system;
wenzelm
parents: 78190
diff changeset
   399
          }
78178
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   400
        }
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   401
      case None => (false, SHA1.no_shasum)
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   402
    }
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   403
  }
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   404
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   405
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   406
  /* session info */
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   407
78181
c2fbe48e9df4 clarified modules;
wenzelm
parents: 78180
diff changeset
   408
  def session_info_exists(db: SQL.Database): Boolean = {
c2fbe48e9df4 clarified modules;
wenzelm
parents: 78180
diff changeset
   409
    val tables = db.tables
78187
2df0f3604a67 clarified signature: more explicit class SQL.Data;
wenzelm
parents: 78186
diff changeset
   410
    Store.Data.tables.forall(table => tables.contains(table.name))
78181
c2fbe48e9df4 clarified modules;
wenzelm
parents: 78180
diff changeset
   411
  }
c2fbe48e9df4 clarified modules;
wenzelm
parents: 78180
diff changeset
   412
c2fbe48e9df4 clarified modules;
wenzelm
parents: 78180
diff changeset
   413
  def session_info_defined(db: SQL.Database, name: String): Boolean =
c2fbe48e9df4 clarified modules;
wenzelm
parents: 78180
diff changeset
   414
    db.execute_query_statementB(
c2fbe48e9df4 clarified modules;
wenzelm
parents: 78180
diff changeset
   415
      Store.Data.Session_Info.table.select(List(Store.Data.Session_Info.session_name),
c2fbe48e9df4 clarified modules;
wenzelm
parents: 78180
diff changeset
   416
        sql = Store.Data.Session_Info.session_name.where_equal(name)))
c2fbe48e9df4 clarified modules;
wenzelm
parents: 78180
diff changeset
   417
78260
0a7f7abbe4f0 more robust transaction_lock: avoid overlapping data spaces;
wenzelm
parents: 78227
diff changeset
   418
  def clean_session_info(db: SQL.Database, name: String): Boolean = {
0a7f7abbe4f0 more robust transaction_lock: avoid overlapping data spaces;
wenzelm
parents: 78227
diff changeset
   419
    Export.clean_session(db, name)
0a7f7abbe4f0 more robust transaction_lock: avoid overlapping data spaces;
wenzelm
parents: 78227
diff changeset
   420
    Document_Build.clean_session(db, name)
0a7f7abbe4f0 more robust transaction_lock: avoid overlapping data spaces;
wenzelm
parents: 78227
diff changeset
   421
78213
fd0430a7b7a4 avoid repeated open_database_server: synchronized transaction_lock;
wenzelm
parents: 78212
diff changeset
   422
    Store.Data.transaction_lock(db, create = true, synchronized = true) {
78178
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   423
      val already_defined = session_info_defined(db, name)
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   424
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   425
      db.execute_statement(
78179
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
   426
        Store.Data.Session_Info.table.delete(
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
   427
          sql = Store.Data.Session_Info.session_name.where_equal(name)))
78178
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   428
78179
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
   429
      db.execute_statement(Store.Data.Sources.table.delete(
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
   430
        sql = Store.Data.Sources.where_equal(name)))
78178
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   431
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   432
      already_defined
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   433
    }
78260
0a7f7abbe4f0 more robust transaction_lock: avoid overlapping data spaces;
wenzelm
parents: 78227
diff changeset
   434
  }
78178
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   435
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   436
  def write_session_info(
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   437
    db: SQL.Database,
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   438
    session_name: String,
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   439
    sources: Store.Sources,
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   440
    build_log: Build_Log.Session_Info,
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   441
    build: Store.Build_Info
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   442
  ): Unit = {
78213
fd0430a7b7a4 avoid repeated open_database_server: synchronized transaction_lock;
wenzelm
parents: 78212
diff changeset
   443
    Store.Data.transaction_lock(db, synchronized = true) {
78179
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
   444
      Store.Data.write_sources(db, session_name, sources)
78181
c2fbe48e9df4 clarified modules;
wenzelm
parents: 78180
diff changeset
   445
      Store.Data.write_session_info(db, cache.compress, session_name, build_log, build)
78178
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   446
    }
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   447
  }
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   448
78265
03eb7f7bb990 proper transaction_lock;
wenzelm
parents: 78262
diff changeset
   449
  def read_session_timing(db: SQL.Database, session: String): Properties.T =
03eb7f7bb990 proper transaction_lock;
wenzelm
parents: 78262
diff changeset
   450
    Store.Data.transaction_lock(db) { Store.Data.read_session_timing(db, session, cache) }
78178
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   451
78265
03eb7f7bb990 proper transaction_lock;
wenzelm
parents: 78262
diff changeset
   452
  def read_command_timings(db: SQL.Database, session: String): Bytes =
03eb7f7bb990 proper transaction_lock;
wenzelm
parents: 78262
diff changeset
   453
    Store.Data.transaction_lock(db) { Store.Data.read_command_timings(db, session) }
78178
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   454
78265
03eb7f7bb990 proper transaction_lock;
wenzelm
parents: 78262
diff changeset
   455
  def read_theory_timings(db: SQL.Database, session: String): List[Properties.T] =
03eb7f7bb990 proper transaction_lock;
wenzelm
parents: 78262
diff changeset
   456
    Store.Data.transaction_lock(db) { Store.Data.read_theory_timings(db, session, cache) }
78178
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   457
78265
03eb7f7bb990 proper transaction_lock;
wenzelm
parents: 78262
diff changeset
   458
  def read_ml_statistics(db: SQL.Database, session: String): List[Properties.T] =
03eb7f7bb990 proper transaction_lock;
wenzelm
parents: 78262
diff changeset
   459
    Store.Data.transaction_lock(db) { Store.Data.read_ml_statistics(db, session, cache) }
78178
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   460
78265
03eb7f7bb990 proper transaction_lock;
wenzelm
parents: 78262
diff changeset
   461
  def read_task_statistics(db: SQL.Database, session: String): List[Properties.T] =
03eb7f7bb990 proper transaction_lock;
wenzelm
parents: 78262
diff changeset
   462
    Store.Data.transaction_lock(db) { Store.Data.read_task_statistics(db, session, cache) }
03eb7f7bb990 proper transaction_lock;
wenzelm
parents: 78262
diff changeset
   463
03eb7f7bb990 proper transaction_lock;
wenzelm
parents: 78262
diff changeset
   464
  def read_theories(db: SQL.Database, session: String): List[String] =
03eb7f7bb990 proper transaction_lock;
wenzelm
parents: 78262
diff changeset
   465
    read_theory_timings(db, session).flatMap(Markup.Name.unapply)
03eb7f7bb990 proper transaction_lock;
wenzelm
parents: 78262
diff changeset
   466
03eb7f7bb990 proper transaction_lock;
wenzelm
parents: 78262
diff changeset
   467
  def read_errors(db: SQL.Database, session: String): List[String] =
03eb7f7bb990 proper transaction_lock;
wenzelm
parents: 78262
diff changeset
   468
    Store.Data.transaction_lock(db) { Store.Data.read_errors(db, session, cache) }
03eb7f7bb990 proper transaction_lock;
wenzelm
parents: 78262
diff changeset
   469
03eb7f7bb990 proper transaction_lock;
wenzelm
parents: 78262
diff changeset
   470
  def read_build(db: SQL.Database, session: String): Option[Store.Build_Info] =
03eb7f7bb990 proper transaction_lock;
wenzelm
parents: 78262
diff changeset
   471
    Store.Data.transaction_lock(db) { Store.Data.read_build(db, session) }
78178
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   472
78179
a49ad8d183af clarified modules;
wenzelm
parents: 78178
diff changeset
   473
  def read_sources(db: SQL.Database, session: String, name: String = ""): List[Store.Source_File] =
78265
03eb7f7bb990 proper transaction_lock;
wenzelm
parents: 78262
diff changeset
   474
    Store.Data.transaction_lock(db) { Store.Data.read_sources(db, session, name, cache.compress) }
78178
a177f71dc79f clarified modules;
wenzelm
parents:
diff changeset
   475
}