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