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