src/Pure/ML/ml_heap.scala
author wenzelm
Sat, 24 Feb 2024 16:30:25 +0100
changeset 79720 deb3056ed823
parent 79719 8544f1045123
child 79769 e9e74577755f
permissions -rw-r--r--
minor performance tuning: just 1 transaction for slices <= 1;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
76991
6a078c80eab6 clarified modules;
wenzelm
parents:
diff changeset
     1
/*  Title:      Pure/ML/ml_heap.scala
6a078c80eab6 clarified modules;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
6a078c80eab6 clarified modules;
wenzelm
parents:
diff changeset
     3
6a078c80eab6 clarified modules;
wenzelm
parents:
diff changeset
     4
ML heap operations.
6a078c80eab6 clarified modules;
wenzelm
parents:
diff changeset
     5
*/
6a078c80eab6 clarified modules;
wenzelm
parents:
diff changeset
     6
6a078c80eab6 clarified modules;
wenzelm
parents:
diff changeset
     7
package isabelle
6a078c80eab6 clarified modules;
wenzelm
parents:
diff changeset
     8
6a078c80eab6 clarified modules;
wenzelm
parents:
diff changeset
     9
6a078c80eab6 clarified modules;
wenzelm
parents:
diff changeset
    10
object ML_Heap {
6a078c80eab6 clarified modules;
wenzelm
parents:
diff changeset
    11
  /** heap file with SHA1 digest **/
6a078c80eab6 clarified modules;
wenzelm
parents:
diff changeset
    12
6a078c80eab6 clarified modules;
wenzelm
parents:
diff changeset
    13
  private val sha1_prefix = "SHA1:"
6a078c80eab6 clarified modules;
wenzelm
parents:
diff changeset
    14
78182
31835adf148a tuned signature;
wenzelm
parents: 77720
diff changeset
    15
  def read_file_digest(heap: Path): Option[SHA1.Digest] = {
76991
6a078c80eab6 clarified modules;
wenzelm
parents:
diff changeset
    16
    if (heap.is_file) {
77711
25fd62cba347 clarified signature: more general operation Bytes.read_slice;
wenzelm
parents: 77206
diff changeset
    17
      val l = sha1_prefix.length
25fd62cba347 clarified signature: more general operation Bytes.read_slice;
wenzelm
parents: 77206
diff changeset
    18
      val m = l + SHA1.digest_length
78958
c125f75a5144 more robust: prefer strict operations;
wenzelm
parents: 78956
diff changeset
    19
      val n = File.size(heap)
78953
b6116a86d2ac clarified signature;
wenzelm
parents: 78510
diff changeset
    20
      val bs = Bytes.read_file(heap, offset = n - m)
77711
25fd62cba347 clarified signature: more general operation Bytes.read_slice;
wenzelm
parents: 77206
diff changeset
    21
      if (bs.length == m) {
25fd62cba347 clarified signature: more general operation Bytes.read_slice;
wenzelm
parents: 77206
diff changeset
    22
        val s = bs.text
25fd62cba347 clarified signature: more general operation Bytes.read_slice;
wenzelm
parents: 77206
diff changeset
    23
        if (s.startsWith(sha1_prefix)) Some(SHA1.fake_digest(s.substring(l)))
76991
6a078c80eab6 clarified modules;
wenzelm
parents:
diff changeset
    24
        else None
6a078c80eab6 clarified modules;
wenzelm
parents:
diff changeset
    25
      }
77711
25fd62cba347 clarified signature: more general operation Bytes.read_slice;
wenzelm
parents: 77206
diff changeset
    26
      else None
76991
6a078c80eab6 clarified modules;
wenzelm
parents:
diff changeset
    27
    }
6a078c80eab6 clarified modules;
wenzelm
parents:
diff changeset
    28
    else None
6a078c80eab6 clarified modules;
wenzelm
parents:
diff changeset
    29
  }
6a078c80eab6 clarified modules;
wenzelm
parents:
diff changeset
    30
78182
31835adf148a tuned signature;
wenzelm
parents: 77720
diff changeset
    31
  def write_file_digest(heap: Path): SHA1.Digest =
31835adf148a tuned signature;
wenzelm
parents: 77720
diff changeset
    32
    read_file_digest(heap) getOrElse {
77206
6784eaef7d0c prefer explicit shasum;
wenzelm
parents: 76992
diff changeset
    33
      val digest = SHA1.digest(heap)
6784eaef7d0c prefer explicit shasum;
wenzelm
parents: 76992
diff changeset
    34
      File.append(heap, sha1_prefix + digest.toString)
6784eaef7d0c prefer explicit shasum;
wenzelm
parents: 76992
diff changeset
    35
      digest
76991
6a078c80eab6 clarified modules;
wenzelm
parents:
diff changeset
    36
    }
77720
f750047e9386 tuned comments;
wenzelm
parents: 77718
diff changeset
    37
f750047e9386 tuned comments;
wenzelm
parents: 77718
diff changeset
    38
f750047e9386 tuned comments;
wenzelm
parents: 77718
diff changeset
    39
  /* SQL data model */
f750047e9386 tuned comments;
wenzelm
parents: 77718
diff changeset
    40
79682
1fa1b32b0379 build local log_db, with store/restore via optional database server;
wenzelm
parents: 79680
diff changeset
    41
  sealed case class Log_DB(uuid: String, content: Bytes)
1fa1b32b0379 build local log_db, with store/restore via optional database server;
wenzelm
parents: 79680
diff changeset
    42
78396
7853d9072d1b renamed object Data to private_data, to emphasize its intended scope (but it is publicly accessible in the database);
wenzelm
parents: 78369
diff changeset
    43
  object private_data extends SQL.Data("isabelle_heaps") {
78187
2df0f3604a67 clarified signature: more explicit class SQL.Data;
wenzelm
parents: 78186
diff changeset
    44
    override lazy val tables = SQL.Tables(Base.table, Slices.table)
78183
8d57ed9e27a7 store heaps within database server;
wenzelm
parents: 78182
diff changeset
    45
8d57ed9e27a7 store heaps within database server;
wenzelm
parents: 78182
diff changeset
    46
    object Generic {
8d57ed9e27a7 store heaps within database server;
wenzelm
parents: 78182
diff changeset
    47
      val name = SQL.Column.string("name").make_primary_key
8d57ed9e27a7 store heaps within database server;
wenzelm
parents: 78182
diff changeset
    48
    }
8d57ed9e27a7 store heaps within database server;
wenzelm
parents: 78182
diff changeset
    49
8d57ed9e27a7 store heaps within database server;
wenzelm
parents: 78182
diff changeset
    50
    object Base {
8d57ed9e27a7 store heaps within database server;
wenzelm
parents: 78182
diff changeset
    51
      val name = Generic.name
79688
3abfc5ebabad tuned names;
wenzelm
parents: 79687
diff changeset
    52
      val heap_size = SQL.Column.long("heap_size")
3abfc5ebabad tuned names;
wenzelm
parents: 79687
diff changeset
    53
      val heap_digest = SQL.Column.string("heap_digest")
79682
1fa1b32b0379 build local log_db, with store/restore via optional database server;
wenzelm
parents: 79680
diff changeset
    54
      val uuid = SQL.Column.string("uuid")
1fa1b32b0379 build local log_db, with store/restore via optional database server;
wenzelm
parents: 79680
diff changeset
    55
      val log_db = SQL.Column.bytes("log_db")
78183
8d57ed9e27a7 store heaps within database server;
wenzelm
parents: 78182
diff changeset
    56
79688
3abfc5ebabad tuned names;
wenzelm
parents: 79687
diff changeset
    57
      val table = make_table(List(name, heap_size, heap_digest, uuid, log_db))
78183
8d57ed9e27a7 store heaps within database server;
wenzelm
parents: 78182
diff changeset
    58
    }
8d57ed9e27a7 store heaps within database server;
wenzelm
parents: 78182
diff changeset
    59
79687
48628d2e30ef tuned names;
wenzelm
parents: 79686
diff changeset
    60
    object Size {
79686
d2cb610c4229 clarified database layout;
wenzelm
parents: 79685
diff changeset
    61
      val name = Generic.name
79688
3abfc5ebabad tuned names;
wenzelm
parents: 79687
diff changeset
    62
      val heap = SQL.Column.string("heap")
3abfc5ebabad tuned names;
wenzelm
parents: 79687
diff changeset
    63
      val log_db = SQL.Column.string("log_db")
79686
d2cb610c4229 clarified database layout;
wenzelm
parents: 79685
diff changeset
    64
79688
3abfc5ebabad tuned names;
wenzelm
parents: 79687
diff changeset
    65
      val table = make_table(List(name, heap, log_db),
79686
d2cb610c4229 clarified database layout;
wenzelm
parents: 79685
diff changeset
    66
        body =
79688
3abfc5ebabad tuned names;
wenzelm
parents: 79687
diff changeset
    67
          "SELECT name, pg_size_pretty(heap_size::bigint) as heap, " +
3abfc5ebabad tuned names;
wenzelm
parents: 79687
diff changeset
    68
          "  pg_size_pretty(length(log_db)::bigint) as log_db FROM " + Base.table.ident,
79686
d2cb610c4229 clarified database layout;
wenzelm
parents: 79685
diff changeset
    69
        name = "size")
d2cb610c4229 clarified database layout;
wenzelm
parents: 79685
diff changeset
    70
    }
d2cb610c4229 clarified database layout;
wenzelm
parents: 79685
diff changeset
    71
78183
8d57ed9e27a7 store heaps within database server;
wenzelm
parents: 78182
diff changeset
    72
    object Slices {
8d57ed9e27a7 store heaps within database server;
wenzelm
parents: 78182
diff changeset
    73
      val name = Generic.name
8d57ed9e27a7 store heaps within database server;
wenzelm
parents: 78182
diff changeset
    74
      val slice = SQL.Column.int("slice").make_primary_key
8d57ed9e27a7 store heaps within database server;
wenzelm
parents: 78182
diff changeset
    75
      val content = SQL.Column.bytes("content")
8d57ed9e27a7 store heaps within database server;
wenzelm
parents: 78182
diff changeset
    76
78266
d8c99a497502 clarified signature;
wenzelm
parents: 78213
diff changeset
    77
      val table = make_table(List(name, slice, content), name = "slices")
78183
8d57ed9e27a7 store heaps within database server;
wenzelm
parents: 78182
diff changeset
    78
    }
8d57ed9e27a7 store heaps within database server;
wenzelm
parents: 78182
diff changeset
    79
78278
5717310a0c6a create database view for diagnostic purposes;
wenzelm
parents: 78266
diff changeset
    80
    object Slices_Size {
5717310a0c6a create database view for diagnostic purposes;
wenzelm
parents: 78266
diff changeset
    81
      val name = Generic.name
79683
ade429ddb1fc more accurate types;
wenzelm
parents: 79682
diff changeset
    82
      val slice = Slices.slice
ade429ddb1fc more accurate types;
wenzelm
parents: 79682
diff changeset
    83
      val size = SQL.Column.string("size")
78278
5717310a0c6a create database view for diagnostic purposes;
wenzelm
parents: 78266
diff changeset
    84
5717310a0c6a create database view for diagnostic purposes;
wenzelm
parents: 78266
diff changeset
    85
      val table = make_table(List(name, slice, size),
5717310a0c6a create database view for diagnostic purposes;
wenzelm
parents: 78266
diff changeset
    86
        body = "SELECT name, slice, pg_size_pretty(length(content)::bigint) as size FROM " +
5717310a0c6a create database view for diagnostic purposes;
wenzelm
parents: 78266
diff changeset
    87
          Slices.table.ident,
5717310a0c6a create database view for diagnostic purposes;
wenzelm
parents: 78266
diff changeset
    88
        name = "slices_size")
5717310a0c6a create database view for diagnostic purposes;
wenzelm
parents: 78266
diff changeset
    89
    }
5717310a0c6a create database view for diagnostic purposes;
wenzelm
parents: 78266
diff changeset
    90
79706
4f218e6e9d23 minor performance tuning;
wenzelm
parents: 79698
diff changeset
    91
    def read_digests(db: SQL.Database, names: Iterable[String]): Map[String, SHA1.Digest] =
4f218e6e9d23 minor performance tuning;
wenzelm
parents: 79698
diff changeset
    92
      if (names.isEmpty) Map.empty
4f218e6e9d23 minor performance tuning;
wenzelm
parents: 79698
diff changeset
    93
      else {
4f218e6e9d23 minor performance tuning;
wenzelm
parents: 79698
diff changeset
    94
        db.execute_query_statement(
4f218e6e9d23 minor performance tuning;
wenzelm
parents: 79698
diff changeset
    95
          Base.table.select(List(Base.name, Base.heap_digest),
4f218e6e9d23 minor performance tuning;
wenzelm
parents: 79698
diff changeset
    96
            sql = Generic.name.where_member(names)),
4f218e6e9d23 minor performance tuning;
wenzelm
parents: 79698
diff changeset
    97
          List.from[(String, String)],
4f218e6e9d23 minor performance tuning;
wenzelm
parents: 79698
diff changeset
    98
          res => res.string(Base.name) -> res.string(Base.heap_digest)
4f218e6e9d23 minor performance tuning;
wenzelm
parents: 79698
diff changeset
    99
        ).collect({
4f218e6e9d23 minor performance tuning;
wenzelm
parents: 79698
diff changeset
   100
          case (name, digest) if digest.nonEmpty => name -> SHA1.fake_digest(digest)
4f218e6e9d23 minor performance tuning;
wenzelm
parents: 79698
diff changeset
   101
        }).toMap
4f218e6e9d23 minor performance tuning;
wenzelm
parents: 79698
diff changeset
   102
      }
78183
8d57ed9e27a7 store heaps within database server;
wenzelm
parents: 78182
diff changeset
   103
79677
49370f0f7911 clarified names;
wenzelm
parents: 78958
diff changeset
   104
    def read_slices(db: SQL.Database, name: String): List[Bytes] =
78196
140a6f2e3728 restore heaps from database, which takes precedence over file-system;
wenzelm
parents: 78193
diff changeset
   105
      db.execute_query_statement(
140a6f2e3728 restore heaps from database, which takes precedence over file-system;
wenzelm
parents: 78193
diff changeset
   106
        Slices.table.select(List(Slices.content),
140a6f2e3728 restore heaps from database, which takes precedence over file-system;
wenzelm
parents: 78193
diff changeset
   107
          sql = Generic.name.where_equal(name) + SQL.order_by(List(Slices.slice))),
140a6f2e3728 restore heaps from database, which takes precedence over file-system;
wenzelm
parents: 78193
diff changeset
   108
        List.from[Bytes], _.bytes(Slices.content))
140a6f2e3728 restore heaps from database, which takes precedence over file-system;
wenzelm
parents: 78193
diff changeset
   109
79682
1fa1b32b0379 build local log_db, with store/restore via optional database server;
wenzelm
parents: 79680
diff changeset
   110
    def read_log_db(db: SQL.Database, name: String, old_uuid: String = ""): Option[Log_DB] =
1fa1b32b0379 build local log_db, with store/restore via optional database server;
wenzelm
parents: 79680
diff changeset
   111
      db.execute_query_statement(
1fa1b32b0379 build local log_db, with store/restore via optional database server;
wenzelm
parents: 79680
diff changeset
   112
        Base.table.select(List(Base.uuid, Base.log_db), sql =
1fa1b32b0379 build local log_db, with store/restore via optional database server;
wenzelm
parents: 79680
diff changeset
   113
          SQL.where_and(
1fa1b32b0379 build local log_db, with store/restore via optional database server;
wenzelm
parents: 79680
diff changeset
   114
            Generic.name.equal(name),
1fa1b32b0379 build local log_db, with store/restore via optional database server;
wenzelm
parents: 79680
diff changeset
   115
            if_proper(old_uuid, Base.uuid.ident + " <> " + SQL.string(old_uuid)))),
1fa1b32b0379 build local log_db, with store/restore via optional database server;
wenzelm
parents: 79680
diff changeset
   116
        List.from[(String, Bytes)],
1fa1b32b0379 build local log_db, with store/restore via optional database server;
wenzelm
parents: 79680
diff changeset
   117
        res => (res.string(Base.uuid), res.bytes(Base.log_db))
1fa1b32b0379 build local log_db, with store/restore via optional database server;
wenzelm
parents: 79680
diff changeset
   118
      ).collectFirst(
1fa1b32b0379 build local log_db, with store/restore via optional database server;
wenzelm
parents: 79680
diff changeset
   119
        {
1fa1b32b0379 build local log_db, with store/restore via optional database server;
wenzelm
parents: 79680
diff changeset
   120
          case (uuid, content) if uuid.nonEmpty && !content.is_empty =>
1fa1b32b0379 build local log_db, with store/restore via optional database server;
wenzelm
parents: 79680
diff changeset
   121
            Log_DB(uuid, content)
1fa1b32b0379 build local log_db, with store/restore via optional database server;
wenzelm
parents: 79680
diff changeset
   122
        })
1fa1b32b0379 build local log_db, with store/restore via optional database server;
wenzelm
parents: 79680
diff changeset
   123
79680
wenzelm
parents: 79678
diff changeset
   124
    def write_slice(db: SQL.Database, name: String, slice: Int, content: Bytes): Unit =
wenzelm
parents: 79678
diff changeset
   125
      db.execute_statement(Slices.table.insert(), body =
wenzelm
parents: 79678
diff changeset
   126
      { stmt =>
wenzelm
parents: 79678
diff changeset
   127
        stmt.string(1) = name
wenzelm
parents: 79678
diff changeset
   128
        stmt.int(2) = slice
wenzelm
parents: 79678
diff changeset
   129
        stmt.bytes(3) = content
wenzelm
parents: 79678
diff changeset
   130
      })
wenzelm
parents: 79678
diff changeset
   131
78183
8d57ed9e27a7 store heaps within database server;
wenzelm
parents: 78182
diff changeset
   132
    def clean_entry(db: SQL.Database, name: String): Unit = {
8d57ed9e27a7 store heaps within database server;
wenzelm
parents: 78182
diff changeset
   133
      for (table <- List(Base.table, Slices.table)) {
8d57ed9e27a7 store heaps within database server;
wenzelm
parents: 78182
diff changeset
   134
        db.execute_statement(table.delete(sql = Base.name.where_equal(name)))
8d57ed9e27a7 store heaps within database server;
wenzelm
parents: 78182
diff changeset
   135
      }
79691
d298c5b65d8e clarified store_session: heap requires process_result.ok, but log_db is always stored;
wenzelm
parents: 79690
diff changeset
   136
    }
d298c5b65d8e clarified store_session: heap requires process_result.ok, but log_db is always stored;
wenzelm
parents: 79690
diff changeset
   137
79720
deb3056ed823 minor performance tuning: just 1 transaction for slices <= 1;
wenzelm
parents: 79719
diff changeset
   138
    def init_entry(
deb3056ed823 minor performance tuning: just 1 transaction for slices <= 1;
wenzelm
parents: 79719
diff changeset
   139
      db: SQL.Database,
deb3056ed823 minor performance tuning: just 1 transaction for slices <= 1;
wenzelm
parents: 79719
diff changeset
   140
      name: String,
deb3056ed823 minor performance tuning: just 1 transaction for slices <= 1;
wenzelm
parents: 79719
diff changeset
   141
      heap_size: Long,
deb3056ed823 minor performance tuning: just 1 transaction for slices <= 1;
wenzelm
parents: 79719
diff changeset
   142
      heap_digest: Option[SHA1.Digest],
deb3056ed823 minor performance tuning: just 1 transaction for slices <= 1;
wenzelm
parents: 79719
diff changeset
   143
      log_db: Option[Log_DB]
deb3056ed823 minor performance tuning: just 1 transaction for slices <= 1;
wenzelm
parents: 79719
diff changeset
   144
    ): Unit = {
79691
d298c5b65d8e clarified store_session: heap requires process_result.ok, but log_db is always stored;
wenzelm
parents: 79690
diff changeset
   145
      clean_entry(db, name)
79687
48628d2e30ef tuned names;
wenzelm
parents: 79686
diff changeset
   146
      for (table <- List(Size.table, Slices_Size.table)) {
79686
d2cb610c4229 clarified database layout;
wenzelm
parents: 79685
diff changeset
   147
        db.create_view(table)
d2cb610c4229 clarified database layout;
wenzelm
parents: 79685
diff changeset
   148
      }
78183
8d57ed9e27a7 store heaps within database server;
wenzelm
parents: 78182
diff changeset
   149
      db.execute_statement(Base.table.insert(), body =
8d57ed9e27a7 store heaps within database server;
wenzelm
parents: 78182
diff changeset
   150
        { stmt =>
8d57ed9e27a7 store heaps within database server;
wenzelm
parents: 78182
diff changeset
   151
          stmt.string(1) = name
79720
deb3056ed823 minor performance tuning: just 1 transaction for slices <= 1;
wenzelm
parents: 79719
diff changeset
   152
          stmt.long(2) = heap_size
deb3056ed823 minor performance tuning: just 1 transaction for slices <= 1;
wenzelm
parents: 79719
diff changeset
   153
          stmt.string(3) = heap_digest.map(_.toString)
79695
eb742d4e4dc9 minor performance tuning: just one transaction for log_db without heap;
wenzelm
parents: 79694
diff changeset
   154
          stmt.string(4) = log_db.map(_.uuid)
eb742d4e4dc9 minor performance tuning: just one transaction for log_db without heap;
wenzelm
parents: 79694
diff changeset
   155
          stmt.bytes(5) = log_db.map(_.content)
78183
8d57ed9e27a7 store heaps within database server;
wenzelm
parents: 78182
diff changeset
   156
        })
79691
d298c5b65d8e clarified store_session: heap requires process_result.ok, but log_db is always stored;
wenzelm
parents: 79690
diff changeset
   157
    }
78183
8d57ed9e27a7 store heaps within database server;
wenzelm
parents: 78182
diff changeset
   158
79720
deb3056ed823 minor performance tuning: just 1 transaction for slices <= 1;
wenzelm
parents: 79719
diff changeset
   159
    def update_entry(
79682
1fa1b32b0379 build local log_db, with store/restore via optional database server;
wenzelm
parents: 79680
diff changeset
   160
      db: SQL.Database,
1fa1b32b0379 build local log_db, with store/restore via optional database server;
wenzelm
parents: 79680
diff changeset
   161
      name: String,
79688
3abfc5ebabad tuned names;
wenzelm
parents: 79687
diff changeset
   162
      heap_size: Long,
3abfc5ebabad tuned names;
wenzelm
parents: 79687
diff changeset
   163
      heap_digest: Option[SHA1.Digest],
3abfc5ebabad tuned names;
wenzelm
parents: 79687
diff changeset
   164
      log_db: Option[Log_DB]
79682
1fa1b32b0379 build local log_db, with store/restore via optional database server;
wenzelm
parents: 79680
diff changeset
   165
    ): Unit =
78183
8d57ed9e27a7 store heaps within database server;
wenzelm
parents: 78182
diff changeset
   166
      db.execute_statement(
79688
3abfc5ebabad tuned names;
wenzelm
parents: 79687
diff changeset
   167
        Base.table.update(List(Base.heap_size, Base.heap_digest, Base.uuid, Base.log_db),
79682
1fa1b32b0379 build local log_db, with store/restore via optional database server;
wenzelm
parents: 79680
diff changeset
   168
          sql = Base.name.where_equal(name)),
78183
8d57ed9e27a7 store heaps within database server;
wenzelm
parents: 78182
diff changeset
   169
        body =
8d57ed9e27a7 store heaps within database server;
wenzelm
parents: 78182
diff changeset
   170
          { stmt =>
79688
3abfc5ebabad tuned names;
wenzelm
parents: 79687
diff changeset
   171
            stmt.long(1) = heap_size
3abfc5ebabad tuned names;
wenzelm
parents: 79687
diff changeset
   172
            stmt.string(2) = heap_digest.map(_.toString)
3abfc5ebabad tuned names;
wenzelm
parents: 79687
diff changeset
   173
            stmt.string(3) = log_db.map(_.uuid)
3abfc5ebabad tuned names;
wenzelm
parents: 79687
diff changeset
   174
            stmt.bytes(4) = log_db.map(_.content)
78183
8d57ed9e27a7 store heaps within database server;
wenzelm
parents: 78182
diff changeset
   175
          })
8d57ed9e27a7 store heaps within database server;
wenzelm
parents: 78182
diff changeset
   176
  }
8d57ed9e27a7 store heaps within database server;
wenzelm
parents: 78182
diff changeset
   177
78204
0aa5360fa88b clarified signature;
wenzelm
parents: 78197
diff changeset
   178
  def clean_entry(db: SQL.Database, session_name: String): Unit =
78396
7853d9072d1b renamed object Data to private_data, to emphasize its intended scope (but it is publicly accessible in the database);
wenzelm
parents: 78369
diff changeset
   179
    private_data.transaction_lock(db, create = true, label = "ML_Heap.clean_entry") {
7853d9072d1b renamed object Data to private_data, to emphasize its intended scope (but it is publicly accessible in the database);
wenzelm
parents: 78369
diff changeset
   180
      private_data.clean_entry(db, session_name)
78213
fd0430a7b7a4 avoid repeated open_database_server: synchronized transaction_lock;
wenzelm
parents: 78204
diff changeset
   181
    }
78186
721c118f7001 proper ML_Heap.clean_entry;
wenzelm
parents: 78183
diff changeset
   182
79677
49370f0f7911 clarified names;
wenzelm
parents: 78958
diff changeset
   183
  def read_digests(db: SQL.Database, names: Iterable[String]): Map[String, SHA1.Digest] =
79682
1fa1b32b0379 build local log_db, with store/restore via optional database server;
wenzelm
parents: 79680
diff changeset
   184
    if (names.isEmpty) Map.empty
1fa1b32b0379 build local log_db, with store/restore via optional database server;
wenzelm
parents: 79680
diff changeset
   185
    else {
1fa1b32b0379 build local log_db, with store/restore via optional database server;
wenzelm
parents: 79680
diff changeset
   186
      private_data.transaction_lock(db, create = true, label = "ML_Heap.read_digests") {
1fa1b32b0379 build local log_db, with store/restore via optional database server;
wenzelm
parents: 79680
diff changeset
   187
        private_data.read_digests(db, names)
1fa1b32b0379 build local log_db, with store/restore via optional database server;
wenzelm
parents: 79680
diff changeset
   188
      }
78213
fd0430a7b7a4 avoid repeated open_database_server: synchronized transaction_lock;
wenzelm
parents: 78204
diff changeset
   189
    }
78196
140a6f2e3728 restore heaps from database, which takes precedence over file-system;
wenzelm
parents: 78193
diff changeset
   190
78191
6e52cda26ad4 tuned signature;
wenzelm
parents: 78188
diff changeset
   191
  def store(
79682
1fa1b32b0379 build local log_db, with store/restore via optional database server;
wenzelm
parents: 79680
diff changeset
   192
    db: SQL.Database,
1fa1b32b0379 build local log_db, with store/restore via optional database server;
wenzelm
parents: 79680
diff changeset
   193
    session: Store.Session,
79678
5979ba127524 clarified signature;
wenzelm
parents: 79677
diff changeset
   194
    slice: Space,
79682
1fa1b32b0379 build local log_db, with store/restore via optional database server;
wenzelm
parents: 79680
diff changeset
   195
    cache: Compress.Cache = Compress.Cache.none,
1fa1b32b0379 build local log_db, with store/restore via optional database server;
wenzelm
parents: 79680
diff changeset
   196
    progress: Progress = new Progress
1fa1b32b0379 build local log_db, with store/restore via optional database server;
wenzelm
parents: 79680
diff changeset
   197
  ): Unit = {
79696
wenzelm
parents: 79695
diff changeset
   198
    val log_db =
wenzelm
parents: 79695
diff changeset
   199
      for {
wenzelm
parents: 79695
diff changeset
   200
        path <- session.log_db
wenzelm
parents: 79695
diff changeset
   201
        uuid <- proper_string(Store.read_build_uuid(path, session.name))
wenzelm
parents: 79695
diff changeset
   202
      } yield Log_DB(uuid, Bytes.read(path))
78183
8d57ed9e27a7 store heaps within database server;
wenzelm
parents: 78182
diff changeset
   203
79697
2e1f75c870e3 more robust: make double-sure that heap digest is present;
wenzelm
parents: 79696
diff changeset
   204
    val heap_digest = session.heap.map(write_file_digest)
79696
wenzelm
parents: 79695
diff changeset
   205
    val heap_size =
wenzelm
parents: 79695
diff changeset
   206
      session.heap match {
wenzelm
parents: 79695
diff changeset
   207
        case Some(heap) => File.size(heap) - sha1_prefix.length - SHA1.digest_length
wenzelm
parents: 79695
diff changeset
   208
        case None => 0L
wenzelm
parents: 79695
diff changeset
   209
      }
wenzelm
parents: 79695
diff changeset
   210
wenzelm
parents: 79695
diff changeset
   211
    val slice_size = slice.bytes max Space.MiB(1).bytes
wenzelm
parents: 79695
diff changeset
   212
    val slices = (heap_size.toDouble / slice_size.toDouble).ceil.toInt
79719
wenzelm
parents: 79706
diff changeset
   213
    val step = if (slices == 0) 0L else (heap_size.toDouble / slices.toDouble).ceil.toLong
wenzelm
parents: 79706
diff changeset
   214
wenzelm
parents: 79706
diff changeset
   215
    def slice_content(i: Int): Bytes = {
wenzelm
parents: 79706
diff changeset
   216
      val j = i + 1
wenzelm
parents: 79706
diff changeset
   217
      val offset = step * i
wenzelm
parents: 79706
diff changeset
   218
      val limit = if (j < slices) step * j else heap_size
wenzelm
parents: 79706
diff changeset
   219
      Bytes.read_file(session.the_heap, offset = offset, limit = limit)
wenzelm
parents: 79706
diff changeset
   220
        .compress(cache = cache)
wenzelm
parents: 79706
diff changeset
   221
    }
79694
wenzelm
parents: 79691
diff changeset
   222
79682
1fa1b32b0379 build local log_db, with store/restore via optional database server;
wenzelm
parents: 79680
diff changeset
   223
    try {
79720
deb3056ed823 minor performance tuning: just 1 transaction for slices <= 1;
wenzelm
parents: 79719
diff changeset
   224
      if (slices > 0) progress.echo("Storing " + session.name + " ...")
79695
eb742d4e4dc9 minor performance tuning: just one transaction for log_db without heap;
wenzelm
parents: 79694
diff changeset
   225
79720
deb3056ed823 minor performance tuning: just 1 transaction for slices <= 1;
wenzelm
parents: 79719
diff changeset
   226
      // init entry: slice 0 + initial log_db
deb3056ed823 minor performance tuning: just 1 transaction for slices <= 1;
wenzelm
parents: 79719
diff changeset
   227
      {
deb3056ed823 minor performance tuning: just 1 transaction for slices <= 1;
wenzelm
parents: 79719
diff changeset
   228
        val (heap_size0, heap_digest0) = if (slices > 1) (0L, None) else (heap_size, heap_digest)
deb3056ed823 minor performance tuning: just 1 transaction for slices <= 1;
wenzelm
parents: 79719
diff changeset
   229
        val log_db0 = if (slices <= 1) log_db else None
deb3056ed823 minor performance tuning: just 1 transaction for slices <= 1;
wenzelm
parents: 79719
diff changeset
   230
        val content0 = if (slices > 0) Some(slice_content(0)) else None
deb3056ed823 minor performance tuning: just 1 transaction for slices <= 1;
wenzelm
parents: 79719
diff changeset
   231
deb3056ed823 minor performance tuning: just 1 transaction for slices <= 1;
wenzelm
parents: 79719
diff changeset
   232
        if (log_db0.isDefined) progress.echo("Storing " + session.log_db_name + " ...")
deb3056ed823 minor performance tuning: just 1 transaction for slices <= 1;
wenzelm
parents: 79719
diff changeset
   233
deb3056ed823 minor performance tuning: just 1 transaction for slices <= 1;
wenzelm
parents: 79719
diff changeset
   234
        private_data.transaction_lock(db, create = true, label = "ML_Heap.store1") {
deb3056ed823 minor performance tuning: just 1 transaction for slices <= 1;
wenzelm
parents: 79719
diff changeset
   235
          private_data.init_entry(db, session.name, heap_size0, heap_digest0, log_db0)
deb3056ed823 minor performance tuning: just 1 transaction for slices <= 1;
wenzelm
parents: 79719
diff changeset
   236
          for (content <- content0) private_data.write_slice(db, session.name, 0, content)
deb3056ed823 minor performance tuning: just 1 transaction for slices <= 1;
wenzelm
parents: 79719
diff changeset
   237
        }
79682
1fa1b32b0379 build local log_db, with store/restore via optional database server;
wenzelm
parents: 79680
diff changeset
   238
      }
78183
8d57ed9e27a7 store heaps within database server;
wenzelm
parents: 78182
diff changeset
   239
79720
deb3056ed823 minor performance tuning: just 1 transaction for slices <= 1;
wenzelm
parents: 79719
diff changeset
   240
      // update entry: slice 1 ... + final log_db
deb3056ed823 minor performance tuning: just 1 transaction for slices <= 1;
wenzelm
parents: 79719
diff changeset
   241
      if (slices > 1) {
deb3056ed823 minor performance tuning: just 1 transaction for slices <= 1;
wenzelm
parents: 79719
diff changeset
   242
        for (i <- 1 until slices) {
79719
wenzelm
parents: 79706
diff changeset
   243
          val content = slice_content(i)
79695
eb742d4e4dc9 minor performance tuning: just one transaction for log_db without heap;
wenzelm
parents: 79694
diff changeset
   244
          private_data.transaction_lock(db, label = "ML_Heap.store2") {
eb742d4e4dc9 minor performance tuning: just one transaction for log_db without heap;
wenzelm
parents: 79694
diff changeset
   245
            private_data.write_slice(db, session.name, i, content)
eb742d4e4dc9 minor performance tuning: just one transaction for log_db without heap;
wenzelm
parents: 79694
diff changeset
   246
          }
79682
1fa1b32b0379 build local log_db, with store/restore via optional database server;
wenzelm
parents: 79680
diff changeset
   247
        }
79695
eb742d4e4dc9 minor performance tuning: just one transaction for log_db without heap;
wenzelm
parents: 79694
diff changeset
   248
eb742d4e4dc9 minor performance tuning: just one transaction for log_db without heap;
wenzelm
parents: 79694
diff changeset
   249
        if (log_db.isDefined) progress.echo("Storing " + session.log_db_name + " ...")
79682
1fa1b32b0379 build local log_db, with store/restore via optional database server;
wenzelm
parents: 79680
diff changeset
   250
79695
eb742d4e4dc9 minor performance tuning: just one transaction for log_db without heap;
wenzelm
parents: 79694
diff changeset
   251
        private_data.transaction_lock(db, label = "ML_Heap.store3") {
79720
deb3056ed823 minor performance tuning: just 1 transaction for slices <= 1;
wenzelm
parents: 79719
diff changeset
   252
          private_data.update_entry(db, session.name, heap_size, heap_digest, log_db)
79695
eb742d4e4dc9 minor performance tuning: just one transaction for log_db without heap;
wenzelm
parents: 79694
diff changeset
   253
        }
79682
1fa1b32b0379 build local log_db, with store/restore via optional database server;
wenzelm
parents: 79680
diff changeset
   254
      }
78183
8d57ed9e27a7 store heaps within database server;
wenzelm
parents: 78182
diff changeset
   255
    }
79682
1fa1b32b0379 build local log_db, with store/restore via optional database server;
wenzelm
parents: 79680
diff changeset
   256
    catch { case exn: Throwable =>
1fa1b32b0379 build local log_db, with store/restore via optional database server;
wenzelm
parents: 79680
diff changeset
   257
      private_data.transaction_lock(db, create = true, label = "ML_Heap.store4") {
1fa1b32b0379 build local log_db, with store/restore via optional database server;
wenzelm
parents: 79680
diff changeset
   258
        private_data.clean_entry(db, session.name)
1fa1b32b0379 build local log_db, with store/restore via optional database server;
wenzelm
parents: 79680
diff changeset
   259
      }
1fa1b32b0379 build local log_db, with store/restore via optional database server;
wenzelm
parents: 79680
diff changeset
   260
      throw exn
1fa1b32b0379 build local log_db, with store/restore via optional database server;
wenzelm
parents: 79680
diff changeset
   261
    }
78183
8d57ed9e27a7 store heaps within database server;
wenzelm
parents: 78182
diff changeset
   262
  }
78196
140a6f2e3728 restore heaps from database, which takes precedence over file-system;
wenzelm
parents: 78193
diff changeset
   263
140a6f2e3728 restore heaps from database, which takes precedence over file-system;
wenzelm
parents: 78193
diff changeset
   264
  def restore(
79698
b676998d7f97 clarified signature;
wenzelm
parents: 79697
diff changeset
   265
    db: SQL.Database,
79682
1fa1b32b0379 build local log_db, with store/restore via optional database server;
wenzelm
parents: 79680
diff changeset
   266
    sessions: List[Store.Session],
1fa1b32b0379 build local log_db, with store/restore via optional database server;
wenzelm
parents: 79680
diff changeset
   267
    cache: Compress.Cache = Compress.Cache.none,
1fa1b32b0379 build local log_db, with store/restore via optional database server;
wenzelm
parents: 79680
diff changeset
   268
    progress: Progress = new Progress
78196
140a6f2e3728 restore heaps from database, which takes precedence over file-system;
wenzelm
parents: 78193
diff changeset
   269
  ): Unit = {
79698
b676998d7f97 clarified signature;
wenzelm
parents: 79697
diff changeset
   270
    if (sessions.exists(_.defined)) {
b676998d7f97 clarified signature;
wenzelm
parents: 79697
diff changeset
   271
      private_data.transaction_lock(db, create = true, label = "ML_Heap.restore") {
b676998d7f97 clarified signature;
wenzelm
parents: 79697
diff changeset
   272
        /* heap */
79682
1fa1b32b0379 build local log_db, with store/restore via optional database server;
wenzelm
parents: 79680
diff changeset
   273
79698
b676998d7f97 clarified signature;
wenzelm
parents: 79697
diff changeset
   274
        val defined_heaps =
b676998d7f97 clarified signature;
wenzelm
parents: 79697
diff changeset
   275
          for (session <- sessions; heap <- session.heap)
b676998d7f97 clarified signature;
wenzelm
parents: 79697
diff changeset
   276
            yield session.name -> heap
79682
1fa1b32b0379 build local log_db, with store/restore via optional database server;
wenzelm
parents: 79680
diff changeset
   277
79698
b676998d7f97 clarified signature;
wenzelm
parents: 79697
diff changeset
   278
        val db_digests = private_data.read_digests(db, defined_heaps.map(_._1))
79682
1fa1b32b0379 build local log_db, with store/restore via optional database server;
wenzelm
parents: 79680
diff changeset
   279
79698
b676998d7f97 clarified signature;
wenzelm
parents: 79697
diff changeset
   280
        for ((session_name, heap) <- defined_heaps) {
b676998d7f97 clarified signature;
wenzelm
parents: 79697
diff changeset
   281
          val file_digest = read_file_digest(heap)
b676998d7f97 clarified signature;
wenzelm
parents: 79697
diff changeset
   282
          val db_digest = db_digests.get(session_name)
b676998d7f97 clarified signature;
wenzelm
parents: 79697
diff changeset
   283
          if (db_digest.isDefined && db_digest != file_digest) {
b676998d7f97 clarified signature;
wenzelm
parents: 79697
diff changeset
   284
            progress.echo("Restoring " + session_name + " ...")
b676998d7f97 clarified signature;
wenzelm
parents: 79697
diff changeset
   285
b676998d7f97 clarified signature;
wenzelm
parents: 79697
diff changeset
   286
            val base_dir = Isabelle_System.make_directory(heap.expand.dir)
b676998d7f97 clarified signature;
wenzelm
parents: 79697
diff changeset
   287
            Isabelle_System.with_tmp_file(session_name + "_", base_dir = base_dir.file) { tmp =>
b676998d7f97 clarified signature;
wenzelm
parents: 79697
diff changeset
   288
              Bytes.write(tmp, Bytes.empty)
b676998d7f97 clarified signature;
wenzelm
parents: 79697
diff changeset
   289
              for (slice <- private_data.read_slices(db, session_name)) {
b676998d7f97 clarified signature;
wenzelm
parents: 79697
diff changeset
   290
                Bytes.append(tmp, slice.uncompress(cache = cache))
78204
0aa5360fa88b clarified signature;
wenzelm
parents: 78197
diff changeset
   291
              }
79698
b676998d7f97 clarified signature;
wenzelm
parents: 79697
diff changeset
   292
              val digest = write_file_digest(tmp)
b676998d7f97 clarified signature;
wenzelm
parents: 79697
diff changeset
   293
              if (db_digest.get == digest) {
b676998d7f97 clarified signature;
wenzelm
parents: 79697
diff changeset
   294
                Isabelle_System.chmod("a+r", tmp)
b676998d7f97 clarified signature;
wenzelm
parents: 79697
diff changeset
   295
                Isabelle_System.move_file(tmp, heap)
b676998d7f97 clarified signature;
wenzelm
parents: 79697
diff changeset
   296
              }
b676998d7f97 clarified signature;
wenzelm
parents: 79697
diff changeset
   297
              else error("Incoherent content for session heap " + heap)
79682
1fa1b32b0379 build local log_db, with store/restore via optional database server;
wenzelm
parents: 79680
diff changeset
   298
            }
1fa1b32b0379 build local log_db, with store/restore via optional database server;
wenzelm
parents: 79680
diff changeset
   299
          }
78204
0aa5360fa88b clarified signature;
wenzelm
parents: 78197
diff changeset
   300
        }
79698
b676998d7f97 clarified signature;
wenzelm
parents: 79697
diff changeset
   301
b676998d7f97 clarified signature;
wenzelm
parents: 79697
diff changeset
   302
b676998d7f97 clarified signature;
wenzelm
parents: 79697
diff changeset
   303
        /* log_db */
b676998d7f97 clarified signature;
wenzelm
parents: 79697
diff changeset
   304
b676998d7f97 clarified signature;
wenzelm
parents: 79697
diff changeset
   305
        for (session <- sessions; path <- session.log_db) {
b676998d7f97 clarified signature;
wenzelm
parents: 79697
diff changeset
   306
          val file_uuid = Store.read_build_uuid(path, session.name)
b676998d7f97 clarified signature;
wenzelm
parents: 79697
diff changeset
   307
          private_data.read_log_db(db, session.name, old_uuid = file_uuid) match {
b676998d7f97 clarified signature;
wenzelm
parents: 79697
diff changeset
   308
            case Some(log_db) if file_uuid.isEmpty =>
b676998d7f97 clarified signature;
wenzelm
parents: 79697
diff changeset
   309
              progress.echo("Restoring " + session.log_db_name + " ...")
b676998d7f97 clarified signature;
wenzelm
parents: 79697
diff changeset
   310
              Isabelle_System.make_directory(path.expand.dir)
b676998d7f97 clarified signature;
wenzelm
parents: 79697
diff changeset
   311
              Bytes.write(path, log_db.content)
b676998d7f97 clarified signature;
wenzelm
parents: 79697
diff changeset
   312
            case Some(_) => error("Incoherent content for session database " + path)
b676998d7f97 clarified signature;
wenzelm
parents: 79697
diff changeset
   313
            case None =>
b676998d7f97 clarified signature;
wenzelm
parents: 79697
diff changeset
   314
          }
b676998d7f97 clarified signature;
wenzelm
parents: 79697
diff changeset
   315
        }
b676998d7f97 clarified signature;
wenzelm
parents: 79697
diff changeset
   316
      }
78196
140a6f2e3728 restore heaps from database, which takes precedence over file-system;
wenzelm
parents: 78193
diff changeset
   317
    }
140a6f2e3728 restore heaps from database, which takes precedence over file-system;
wenzelm
parents: 78193
diff changeset
   318
  }
76991
6a078c80eab6 clarified modules;
wenzelm
parents:
diff changeset
   319
}