author | wenzelm |
Wed, 21 Feb 2024 19:36:53 +0100 | |
changeset 79682 | 1fa1b32b0379 |
parent 79680 | 0b58e85906a1 |
child 79683 | ade429ddb1fc |
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 |
import java.nio.ByteBuffer |
|
11 |
import java.nio.channels.FileChannel |
|
12 |
import java.nio.file.StandardOpenOption |
|
13 |
||
14 |
||
15 |
object ML_Heap { |
|
16 |
/** heap file with SHA1 digest **/ |
|
17 |
||
18 |
private val sha1_prefix = "SHA1:" |
|
19 |
||
78182 | 20 |
def read_file_digest(heap: Path): Option[SHA1.Digest] = { |
76991 | 21 |
if (heap.is_file) { |
77711
25fd62cba347
clarified signature: more general operation Bytes.read_slice;
wenzelm
parents:
77206
diff
changeset
|
22 |
val l = sha1_prefix.length |
25fd62cba347
clarified signature: more general operation Bytes.read_slice;
wenzelm
parents:
77206
diff
changeset
|
23 |
val m = l + SHA1.digest_length |
78958 | 24 |
val n = File.size(heap) |
78953 | 25 |
val bs = Bytes.read_file(heap, offset = n - m) |
77711
25fd62cba347
clarified signature: more general operation Bytes.read_slice;
wenzelm
parents:
77206
diff
changeset
|
26 |
if (bs.length == m) { |
25fd62cba347
clarified signature: more general operation Bytes.read_slice;
wenzelm
parents:
77206
diff
changeset
|
27 |
val s = bs.text |
25fd62cba347
clarified signature: more general operation Bytes.read_slice;
wenzelm
parents:
77206
diff
changeset
|
28 |
if (s.startsWith(sha1_prefix)) Some(SHA1.fake_digest(s.substring(l))) |
76991 | 29 |
else None |
30 |
} |
|
77711
25fd62cba347
clarified signature: more general operation Bytes.read_slice;
wenzelm
parents:
77206
diff
changeset
|
31 |
else None |
76991 | 32 |
} |
33 |
else None |
|
34 |
} |
|
35 |
||
78182 | 36 |
def write_file_digest(heap: Path): SHA1.Digest = |
37 |
read_file_digest(heap) getOrElse { |
|
77206 | 38 |
val digest = SHA1.digest(heap) |
39 |
File.append(heap, sha1_prefix + digest.toString) |
|
40 |
digest |
|
76991 | 41 |
} |
77720 | 42 |
|
43 |
||
44 |
/* SQL data model */ |
|
45 |
||
79682
1fa1b32b0379
build local log_db, with store/restore via optional database server;
wenzelm
parents:
79680
diff
changeset
|
46 |
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
|
47 |
|
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
|
48 |
object private_data extends SQL.Data("isabelle_heaps") { |
78187
2df0f3604a67
clarified signature: more explicit class SQL.Data;
wenzelm
parents:
78186
diff
changeset
|
49 |
override lazy val tables = SQL.Tables(Base.table, Slices.table) |
78183 | 50 |
|
51 |
object Generic { |
|
52 |
val name = SQL.Column.string("name").make_primary_key |
|
53 |
} |
|
54 |
||
55 |
object Base { |
|
56 |
val name = Generic.name |
|
57 |
val size = SQL.Column.long("size") |
|
58 |
val digest = SQL.Column.string("digest") |
|
79682
1fa1b32b0379
build local log_db, with store/restore via optional database server;
wenzelm
parents:
79680
diff
changeset
|
59 |
val uuid = SQL.Column.string("uuid") |
1fa1b32b0379
build local log_db, with store/restore via optional database server;
wenzelm
parents:
79680
diff
changeset
|
60 |
val log_db = SQL.Column.bytes("log_db") |
78183 | 61 |
|
79682
1fa1b32b0379
build local log_db, with store/restore via optional database server;
wenzelm
parents:
79680
diff
changeset
|
62 |
val table = make_table(List(name, size, digest, uuid, log_db)) |
78183 | 63 |
} |
64 |
||
65 |
object Slices { |
|
66 |
val name = Generic.name |
|
67 |
val slice = SQL.Column.int("slice").make_primary_key |
|
68 |
val content = SQL.Column.bytes("content") |
|
69 |
||
78266 | 70 |
val table = make_table(List(name, slice, content), name = "slices") |
78183 | 71 |
} |
72 |
||
78278 | 73 |
object Slices_Size { |
74 |
val name = Generic.name |
|
75 |
val slice = SQL.Column.int("slice").make_primary_key |
|
76 |
val size = SQL.Column.long("size") |
|
77 |
||
78 |
val table = make_table(List(name, slice, size), |
|
79 |
body = "SELECT name, slice, pg_size_pretty(length(content)::bigint) as size FROM " + |
|
80 |
Slices.table.ident, |
|
81 |
name = "slices_size") |
|
82 |
} |
|
83 |
||
79677 | 84 |
def read_digests(db: SQL.Database, names: Iterable[String]): Map[String, SHA1.Digest] = { |
78510
8f45302a9ff0
more thorough ML_Heap.restore: include ancestors; prefer simultaneous ML_Heap.get_entries: just one database access for heap hierarchy;
wenzelm
parents:
78509
diff
changeset
|
85 |
require(names.nonEmpty) |
8f45302a9ff0
more thorough ML_Heap.restore: include ancestors; prefer simultaneous ML_Heap.get_entries: just one database access for heap hierarchy;
wenzelm
parents:
78509
diff
changeset
|
86 |
|
8f45302a9ff0
more thorough ML_Heap.restore: include ancestors; prefer simultaneous ML_Heap.get_entries: just one database access for heap hierarchy;
wenzelm
parents:
78509
diff
changeset
|
87 |
db.execute_query_statement( |
8f45302a9ff0
more thorough ML_Heap.restore: include ancestors; prefer simultaneous ML_Heap.get_entries: just one database access for heap hierarchy;
wenzelm
parents:
78509
diff
changeset
|
88 |
Base.table.select(List(Base.name, Base.digest), |
8f45302a9ff0
more thorough ML_Heap.restore: include ancestors; prefer simultaneous ML_Heap.get_entries: just one database access for heap hierarchy;
wenzelm
parents:
78509
diff
changeset
|
89 |
sql = Generic.name.where_member(names)), |
8f45302a9ff0
more thorough ML_Heap.restore: include ancestors; prefer simultaneous ML_Heap.get_entries: just one database access for heap hierarchy;
wenzelm
parents:
78509
diff
changeset
|
90 |
List.from[(String, String)], |
8f45302a9ff0
more thorough ML_Heap.restore: include ancestors; prefer simultaneous ML_Heap.get_entries: just one database access for heap hierarchy;
wenzelm
parents:
78509
diff
changeset
|
91 |
res => res.string(Base.name) -> res.string(Base.digest) |
8f45302a9ff0
more thorough ML_Heap.restore: include ancestors; prefer simultaneous ML_Heap.get_entries: just one database access for heap hierarchy;
wenzelm
parents:
78509
diff
changeset
|
92 |
).flatMap({ |
8f45302a9ff0
more thorough ML_Heap.restore: include ancestors; prefer simultaneous ML_Heap.get_entries: just one database access for heap hierarchy;
wenzelm
parents:
78509
diff
changeset
|
93 |
case (_, "") => None |
8f45302a9ff0
more thorough ML_Heap.restore: include ancestors; prefer simultaneous ML_Heap.get_entries: just one database access for heap hierarchy;
wenzelm
parents:
78509
diff
changeset
|
94 |
case (name, digest) => Some(name -> SHA1.fake_digest(digest)) |
8f45302a9ff0
more thorough ML_Heap.restore: include ancestors; prefer simultaneous ML_Heap.get_entries: just one database access for heap hierarchy;
wenzelm
parents:
78509
diff
changeset
|
95 |
}).toMap |
8f45302a9ff0
more thorough ML_Heap.restore: include ancestors; prefer simultaneous ML_Heap.get_entries: just one database access for heap hierarchy;
wenzelm
parents:
78509
diff
changeset
|
96 |
} |
78183 | 97 |
|
79677 | 98 |
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
|
99 |
db.execute_query_statement( |
140a6f2e3728
restore heaps from database, which takes precedence over file-system;
wenzelm
parents:
78193
diff
changeset
|
100 |
Slices.table.select(List(Slices.content), |
140a6f2e3728
restore heaps from database, which takes precedence over file-system;
wenzelm
parents:
78193
diff
changeset
|
101 |
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
|
102 |
List.from[Bytes], _.bytes(Slices.content)) |
140a6f2e3728
restore heaps from database, which takes precedence over file-system;
wenzelm
parents:
78193
diff
changeset
|
103 |
|
79682
1fa1b32b0379
build local log_db, with store/restore via optional database server;
wenzelm
parents:
79680
diff
changeset
|
104 |
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
|
105 |
db.execute_query_statement( |
1fa1b32b0379
build local log_db, with store/restore via optional database server;
wenzelm
parents:
79680
diff
changeset
|
106 |
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
|
107 |
SQL.where_and( |
1fa1b32b0379
build local log_db, with store/restore via optional database server;
wenzelm
parents:
79680
diff
changeset
|
108 |
Generic.name.equal(name), |
1fa1b32b0379
build local log_db, with store/restore via optional database server;
wenzelm
parents:
79680
diff
changeset
|
109 |
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
|
110 |
List.from[(String, Bytes)], |
1fa1b32b0379
build local log_db, with store/restore via optional database server;
wenzelm
parents:
79680
diff
changeset
|
111 |
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
|
112 |
).collectFirst( |
1fa1b32b0379
build local log_db, with store/restore via optional database server;
wenzelm
parents:
79680
diff
changeset
|
113 |
{ |
1fa1b32b0379
build local log_db, with store/restore via optional database server;
wenzelm
parents:
79680
diff
changeset
|
114 |
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
|
115 |
Log_DB(uuid, content) |
1fa1b32b0379
build local log_db, with store/restore via optional database server;
wenzelm
parents:
79680
diff
changeset
|
116 |
}) |
1fa1b32b0379
build local log_db, with store/restore via optional database server;
wenzelm
parents:
79680
diff
changeset
|
117 |
|
79680 | 118 |
def write_slice(db: SQL.Database, name: String, slice: Int, content: Bytes): Unit = |
119 |
db.execute_statement(Slices.table.insert(), body = |
|
120 |
{ stmt => |
|
121 |
stmt.string(1) = name |
|
122 |
stmt.int(2) = slice |
|
123 |
stmt.bytes(3) = content |
|
124 |
}) |
|
125 |
||
78183 | 126 |
def clean_entry(db: SQL.Database, name: String): Unit = { |
127 |
for (table <- List(Base.table, Slices.table)) { |
|
128 |
db.execute_statement(table.delete(sql = Base.name.where_equal(name))) |
|
129 |
} |
|
78278 | 130 |
db.create_view(Slices_Size.table) |
78183 | 131 |
} |
132 |
||
79680 | 133 |
def init_entry(db: SQL.Database, name: String): Unit = |
78183 | 134 |
db.execute_statement(Base.table.insert(), body = |
135 |
{ stmt => |
|
136 |
stmt.string(1) = name |
|
137 |
stmt.long(2) = None |
|
138 |
stmt.string(3) = None |
|
79682
1fa1b32b0379
build local log_db, with store/restore via optional database server;
wenzelm
parents:
79680
diff
changeset
|
139 |
stmt.string(4) = None |
1fa1b32b0379
build local log_db, with store/restore via optional database server;
wenzelm
parents:
79680
diff
changeset
|
140 |
stmt.bytes(5) = None |
78183 | 141 |
}) |
142 |
||
79682
1fa1b32b0379
build local log_db, with store/restore via optional database server;
wenzelm
parents:
79680
diff
changeset
|
143 |
def finish_entry( |
1fa1b32b0379
build local log_db, with store/restore via optional database server;
wenzelm
parents:
79680
diff
changeset
|
144 |
db: SQL.Database, |
1fa1b32b0379
build local log_db, with store/restore via optional database server;
wenzelm
parents:
79680
diff
changeset
|
145 |
name: String, |
1fa1b32b0379
build local log_db, with store/restore via optional database server;
wenzelm
parents:
79680
diff
changeset
|
146 |
size: Long, |
1fa1b32b0379
build local log_db, with store/restore via optional database server;
wenzelm
parents:
79680
diff
changeset
|
147 |
opt_digest: Option[SHA1.Digest], |
1fa1b32b0379
build local log_db, with store/restore via optional database server;
wenzelm
parents:
79680
diff
changeset
|
148 |
opt_log_db: Option[Log_DB] |
1fa1b32b0379
build local log_db, with store/restore via optional database server;
wenzelm
parents:
79680
diff
changeset
|
149 |
): Unit = |
78183 | 150 |
db.execute_statement( |
79682
1fa1b32b0379
build local log_db, with store/restore via optional database server;
wenzelm
parents:
79680
diff
changeset
|
151 |
Base.table.update(List(Base.size, Base.digest, Base.uuid, Base.log_db), |
1fa1b32b0379
build local log_db, with store/restore via optional database server;
wenzelm
parents:
79680
diff
changeset
|
152 |
sql = Base.name.where_equal(name)), |
78183 | 153 |
body = |
154 |
{ stmt => |
|
155 |
stmt.long(1) = size |
|
79682
1fa1b32b0379
build local log_db, with store/restore via optional database server;
wenzelm
parents:
79680
diff
changeset
|
156 |
stmt.string(2) = opt_digest.map(_.toString) |
1fa1b32b0379
build local log_db, with store/restore via optional database server;
wenzelm
parents:
79680
diff
changeset
|
157 |
stmt.string(3) = opt_log_db.map(_.uuid) |
1fa1b32b0379
build local log_db, with store/restore via optional database server;
wenzelm
parents:
79680
diff
changeset
|
158 |
stmt.bytes(4) = opt_log_db.map(_.content) |
78183 | 159 |
}) |
160 |
} |
|
161 |
||
78204 | 162 |
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
|
163 |
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
|
164 |
private_data.clean_entry(db, session_name) |
78213
fd0430a7b7a4
avoid repeated open_database_server: synchronized transaction_lock;
wenzelm
parents:
78204
diff
changeset
|
165 |
} |
78186 | 166 |
|
79677 | 167 |
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
|
168 |
if (names.isEmpty) Map.empty |
1fa1b32b0379
build local log_db, with store/restore via optional database server;
wenzelm
parents:
79680
diff
changeset
|
169 |
else { |
1fa1b32b0379
build local log_db, with store/restore via optional database server;
wenzelm
parents:
79680
diff
changeset
|
170 |
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
|
171 |
private_data.read_digests(db, names) |
1fa1b32b0379
build local log_db, with store/restore via optional database server;
wenzelm
parents:
79680
diff
changeset
|
172 |
} |
78213
fd0430a7b7a4
avoid repeated open_database_server: synchronized transaction_lock;
wenzelm
parents:
78204
diff
changeset
|
173 |
} |
78196
140a6f2e3728
restore heaps from database, which takes precedence over file-system;
wenzelm
parents:
78193
diff
changeset
|
174 |
|
78191 | 175 |
def store( |
79682
1fa1b32b0379
build local log_db, with store/restore via optional database server;
wenzelm
parents:
79680
diff
changeset
|
176 |
db: SQL.Database, |
1fa1b32b0379
build local log_db, with store/restore via optional database server;
wenzelm
parents:
79680
diff
changeset
|
177 |
session: Store.Session, |
79678 | 178 |
slice: Space, |
79682
1fa1b32b0379
build local log_db, with store/restore via optional database server;
wenzelm
parents:
79680
diff
changeset
|
179 |
cache: Compress.Cache = Compress.Cache.none, |
1fa1b32b0379
build local log_db, with store/restore via optional database server;
wenzelm
parents:
79680
diff
changeset
|
180 |
progress: Progress = new Progress |
1fa1b32b0379
build local log_db, with store/restore via optional database server;
wenzelm
parents:
79680
diff
changeset
|
181 |
): Unit = { |
1fa1b32b0379
build local log_db, with store/restore via optional database server;
wenzelm
parents:
79680
diff
changeset
|
182 |
val size = |
1fa1b32b0379
build local log_db, with store/restore via optional database server;
wenzelm
parents:
79680
diff
changeset
|
183 |
session.heap match { |
1fa1b32b0379
build local log_db, with store/restore via optional database server;
wenzelm
parents:
79680
diff
changeset
|
184 |
case Some(heap) => File.size(heap) - sha1_prefix.length - SHA1.digest_length |
1fa1b32b0379
build local log_db, with store/restore via optional database server;
wenzelm
parents:
79680
diff
changeset
|
185 |
case None => 0L |
1fa1b32b0379
build local log_db, with store/restore via optional database server;
wenzelm
parents:
79680
diff
changeset
|
186 |
} |
78183 | 187 |
|
79682
1fa1b32b0379
build local log_db, with store/restore via optional database server;
wenzelm
parents:
79680
diff
changeset
|
188 |
val slice_size = slice.bytes max Space.MiB(1).bytes |
1fa1b32b0379
build local log_db, with store/restore via optional database server;
wenzelm
parents:
79680
diff
changeset
|
189 |
val slices = (size.toDouble / slice_size.toDouble).ceil.toInt |
1fa1b32b0379
build local log_db, with store/restore via optional database server;
wenzelm
parents:
79680
diff
changeset
|
190 |
val step = if (slices > 0) (size.toDouble / slices.toDouble).ceil.toLong else 0L |
78183 | 191 |
|
79682
1fa1b32b0379
build local log_db, with store/restore via optional database server;
wenzelm
parents:
79680
diff
changeset
|
192 |
try { |
1fa1b32b0379
build local log_db, with store/restore via optional database server;
wenzelm
parents:
79680
diff
changeset
|
193 |
private_data.transaction_lock(db, create = true, label = "ML_Heap.store1") { |
1fa1b32b0379
build local log_db, with store/restore via optional database server;
wenzelm
parents:
79680
diff
changeset
|
194 |
private_data.init_entry(db, session.name) |
1fa1b32b0379
build local log_db, with store/restore via optional database server;
wenzelm
parents:
79680
diff
changeset
|
195 |
} |
78183 | 196 |
|
79682
1fa1b32b0379
build local log_db, with store/restore via optional database server;
wenzelm
parents:
79680
diff
changeset
|
197 |
if (slices > 0) progress.echo("Storing " + session.name + " ...") |
1fa1b32b0379
build local log_db, with store/restore via optional database server;
wenzelm
parents:
79680
diff
changeset
|
198 |
for (i <- 0 until slices) { |
1fa1b32b0379
build local log_db, with store/restore via optional database server;
wenzelm
parents:
79680
diff
changeset
|
199 |
val j = i + 1 |
1fa1b32b0379
build local log_db, with store/restore via optional database server;
wenzelm
parents:
79680
diff
changeset
|
200 |
val offset = step * i |
1fa1b32b0379
build local log_db, with store/restore via optional database server;
wenzelm
parents:
79680
diff
changeset
|
201 |
val limit = if (j < slices) step * j else size |
1fa1b32b0379
build local log_db, with store/restore via optional database server;
wenzelm
parents:
79680
diff
changeset
|
202 |
val content = |
1fa1b32b0379
build local log_db, with store/restore via optional database server;
wenzelm
parents:
79680
diff
changeset
|
203 |
Bytes.read_file(session.the_heap, offset = offset, limit = limit) |
1fa1b32b0379
build local log_db, with store/restore via optional database server;
wenzelm
parents:
79680
diff
changeset
|
204 |
.compress(cache = cache) |
1fa1b32b0379
build local log_db, with store/restore via optional database server;
wenzelm
parents:
79680
diff
changeset
|
205 |
private_data.transaction_lock(db, label = "ML_Heap.store2") { |
1fa1b32b0379
build local log_db, with store/restore via optional database server;
wenzelm
parents:
79680
diff
changeset
|
206 |
private_data.write_slice(db, session.name, i, content) |
1fa1b32b0379
build local log_db, with store/restore via optional database server;
wenzelm
parents:
79680
diff
changeset
|
207 |
} |
1fa1b32b0379
build local log_db, with store/restore via optional database server;
wenzelm
parents:
79680
diff
changeset
|
208 |
} |
1fa1b32b0379
build local log_db, with store/restore via optional database server;
wenzelm
parents:
79680
diff
changeset
|
209 |
|
1fa1b32b0379
build local log_db, with store/restore via optional database server;
wenzelm
parents:
79680
diff
changeset
|
210 |
val opt_digest = |
1fa1b32b0379
build local log_db, with store/restore via optional database server;
wenzelm
parents:
79680
diff
changeset
|
211 |
for { |
1fa1b32b0379
build local log_db, with store/restore via optional database server;
wenzelm
parents:
79680
diff
changeset
|
212 |
path <- session.heap |
1fa1b32b0379
build local log_db, with store/restore via optional database server;
wenzelm
parents:
79680
diff
changeset
|
213 |
digest <- read_file_digest(path) |
1fa1b32b0379
build local log_db, with store/restore via optional database server;
wenzelm
parents:
79680
diff
changeset
|
214 |
} yield digest |
78183 | 215 |
|
79682
1fa1b32b0379
build local log_db, with store/restore via optional database server;
wenzelm
parents:
79680
diff
changeset
|
216 |
val opt_log_db = |
1fa1b32b0379
build local log_db, with store/restore via optional database server;
wenzelm
parents:
79680
diff
changeset
|
217 |
for { |
1fa1b32b0379
build local log_db, with store/restore via optional database server;
wenzelm
parents:
79680
diff
changeset
|
218 |
path <- session.log_db |
1fa1b32b0379
build local log_db, with store/restore via optional database server;
wenzelm
parents:
79680
diff
changeset
|
219 |
uuid <- proper_string(Store.read_build_uuid(path, session.name)) |
1fa1b32b0379
build local log_db, with store/restore via optional database server;
wenzelm
parents:
79680
diff
changeset
|
220 |
} yield Log_DB(uuid, Bytes.read(path)) |
1fa1b32b0379
build local log_db, with store/restore via optional database server;
wenzelm
parents:
79680
diff
changeset
|
221 |
|
1fa1b32b0379
build local log_db, with store/restore via optional database server;
wenzelm
parents:
79680
diff
changeset
|
222 |
if (opt_log_db.isDefined) progress.echo("Storing " + session.name + ".db ...") |
1fa1b32b0379
build local log_db, with store/restore via optional database server;
wenzelm
parents:
79680
diff
changeset
|
223 |
|
1fa1b32b0379
build local log_db, with store/restore via optional database server;
wenzelm
parents:
79680
diff
changeset
|
224 |
private_data.transaction_lock(db, label = "ML_Heap.store3") { |
1fa1b32b0379
build local log_db, with store/restore via optional database server;
wenzelm
parents:
79680
diff
changeset
|
225 |
private_data.finish_entry(db, session.name, size, opt_digest, opt_log_db) |
1fa1b32b0379
build local log_db, with store/restore via optional database server;
wenzelm
parents:
79680
diff
changeset
|
226 |
} |
78183 | 227 |
} |
79682
1fa1b32b0379
build local log_db, with store/restore via optional database server;
wenzelm
parents:
79680
diff
changeset
|
228 |
catch { case exn: Throwable => |
1fa1b32b0379
build local log_db, with store/restore via optional database server;
wenzelm
parents:
79680
diff
changeset
|
229 |
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
|
230 |
private_data.clean_entry(db, session.name) |
1fa1b32b0379
build local log_db, with store/restore via optional database server;
wenzelm
parents:
79680
diff
changeset
|
231 |
} |
1fa1b32b0379
build local log_db, with store/restore via optional database server;
wenzelm
parents:
79680
diff
changeset
|
232 |
throw exn |
1fa1b32b0379
build local log_db, with store/restore via optional database server;
wenzelm
parents:
79680
diff
changeset
|
233 |
} |
78183 | 234 |
} |
78196
140a6f2e3728
restore heaps from database, which takes precedence over file-system;
wenzelm
parents:
78193
diff
changeset
|
235 |
|
140a6f2e3728
restore heaps from database, which takes precedence over file-system;
wenzelm
parents:
78193
diff
changeset
|
236 |
def restore( |
78204 | 237 |
database: Option[SQL.Database], |
79682
1fa1b32b0379
build local log_db, with store/restore via optional database server;
wenzelm
parents:
79680
diff
changeset
|
238 |
sessions: List[Store.Session], |
1fa1b32b0379
build local log_db, with store/restore via optional database server;
wenzelm
parents:
79680
diff
changeset
|
239 |
cache: Compress.Cache = Compress.Cache.none, |
1fa1b32b0379
build local log_db, with store/restore via optional database server;
wenzelm
parents:
79680
diff
changeset
|
240 |
progress: Progress = new Progress |
78196
140a6f2e3728
restore heaps from database, which takes precedence over file-system;
wenzelm
parents:
78193
diff
changeset
|
241 |
): Unit = { |
78204 | 242 |
database match { |
79682
1fa1b32b0379
build local log_db, with store/restore via optional database server;
wenzelm
parents:
79680
diff
changeset
|
243 |
case Some(db) if sessions.exists(_.defined) => |
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
|
244 |
private_data.transaction_lock(db, create = true, label = "ML_Heap.restore") { |
79682
1fa1b32b0379
build local log_db, with store/restore via optional database server;
wenzelm
parents:
79680
diff
changeset
|
245 |
/* heap */ |
1fa1b32b0379
build local log_db, with store/restore via optional database server;
wenzelm
parents:
79680
diff
changeset
|
246 |
|
1fa1b32b0379
build local log_db, with store/restore via optional database server;
wenzelm
parents:
79680
diff
changeset
|
247 |
val defined_heaps = |
1fa1b32b0379
build local log_db, with store/restore via optional database server;
wenzelm
parents:
79680
diff
changeset
|
248 |
for (session <- sessions; heap <- session.heap) |
1fa1b32b0379
build local log_db, with store/restore via optional database server;
wenzelm
parents:
79680
diff
changeset
|
249 |
yield session.name -> heap |
1fa1b32b0379
build local log_db, with store/restore via optional database server;
wenzelm
parents:
79680
diff
changeset
|
250 |
|
1fa1b32b0379
build local log_db, with store/restore via optional database server;
wenzelm
parents:
79680
diff
changeset
|
251 |
val db_digests = private_data.read_digests(db, defined_heaps.map(_._1)) |
1fa1b32b0379
build local log_db, with store/restore via optional database server;
wenzelm
parents:
79680
diff
changeset
|
252 |
|
1fa1b32b0379
build local log_db, with store/restore via optional database server;
wenzelm
parents:
79680
diff
changeset
|
253 |
for ((session_name, heap) <- defined_heaps) { |
78510
8f45302a9ff0
more thorough ML_Heap.restore: include ancestors; prefer simultaneous ML_Heap.get_entries: just one database access for heap hierarchy;
wenzelm
parents:
78509
diff
changeset
|
254 |
val file_digest = read_file_digest(heap) |
8f45302a9ff0
more thorough ML_Heap.restore: include ancestors; prefer simultaneous ML_Heap.get_entries: just one database access for heap hierarchy;
wenzelm
parents:
78509
diff
changeset
|
255 |
val db_digest = db_digests.get(session_name) |
8f45302a9ff0
more thorough ML_Heap.restore: include ancestors; prefer simultaneous ML_Heap.get_entries: just one database access for heap hierarchy;
wenzelm
parents:
78509
diff
changeset
|
256 |
if (db_digest.isDefined && db_digest != file_digest) { |
79682
1fa1b32b0379
build local log_db, with store/restore via optional database server;
wenzelm
parents:
79680
diff
changeset
|
257 |
progress.echo("Restoring " + session_name + " ...") |
1fa1b32b0379
build local log_db, with store/restore via optional database server;
wenzelm
parents:
79680
diff
changeset
|
258 |
|
78510
8f45302a9ff0
more thorough ML_Heap.restore: include ancestors; prefer simultaneous ML_Heap.get_entries: just one database access for heap hierarchy;
wenzelm
parents:
78509
diff
changeset
|
259 |
val base_dir = Isabelle_System.make_directory(heap.expand.dir) |
8f45302a9ff0
more thorough ML_Heap.restore: include ancestors; prefer simultaneous ML_Heap.get_entries: just one database access for heap hierarchy;
wenzelm
parents:
78509
diff
changeset
|
260 |
Isabelle_System.with_tmp_file(session_name + "_", base_dir = base_dir.file) { tmp => |
8f45302a9ff0
more thorough ML_Heap.restore: include ancestors; prefer simultaneous ML_Heap.get_entries: just one database access for heap hierarchy;
wenzelm
parents:
78509
diff
changeset
|
261 |
Bytes.write(tmp, Bytes.empty) |
79677 | 262 |
for (slice <- private_data.read_slices(db, session_name)) { |
78510
8f45302a9ff0
more thorough ML_Heap.restore: include ancestors; prefer simultaneous ML_Heap.get_entries: just one database access for heap hierarchy;
wenzelm
parents:
78509
diff
changeset
|
263 |
Bytes.append(tmp, slice.uncompress(cache = cache)) |
8f45302a9ff0
more thorough ML_Heap.restore: include ancestors; prefer simultaneous ML_Heap.get_entries: just one database access for heap hierarchy;
wenzelm
parents:
78509
diff
changeset
|
264 |
} |
8f45302a9ff0
more thorough ML_Heap.restore: include ancestors; prefer simultaneous ML_Heap.get_entries: just one database access for heap hierarchy;
wenzelm
parents:
78509
diff
changeset
|
265 |
val digest = write_file_digest(tmp) |
8f45302a9ff0
more thorough ML_Heap.restore: include ancestors; prefer simultaneous ML_Heap.get_entries: just one database access for heap hierarchy;
wenzelm
parents:
78509
diff
changeset
|
266 |
if (db_digest.get == digest) { |
8f45302a9ff0
more thorough ML_Heap.restore: include ancestors; prefer simultaneous ML_Heap.get_entries: just one database access for heap hierarchy;
wenzelm
parents:
78509
diff
changeset
|
267 |
Isabelle_System.chmod("a+r", tmp) |
8f45302a9ff0
more thorough ML_Heap.restore: include ancestors; prefer simultaneous ML_Heap.get_entries: just one database access for heap hierarchy;
wenzelm
parents:
78509
diff
changeset
|
268 |
Isabelle_System.move_file(tmp, heap) |
8f45302a9ff0
more thorough ML_Heap.restore: include ancestors; prefer simultaneous ML_Heap.get_entries: just one database access for heap hierarchy;
wenzelm
parents:
78509
diff
changeset
|
269 |
} |
79682
1fa1b32b0379
build local log_db, with store/restore via optional database server;
wenzelm
parents:
79680
diff
changeset
|
270 |
else error("Incoherent content for session heap " + heap) |
78204 | 271 |
} |
78509
146468e05dd4
more robust: atomic file-system result via tmp file;
wenzelm
parents:
78396
diff
changeset
|
272 |
} |
78196
140a6f2e3728
restore heaps from database, which takes precedence over file-system;
wenzelm
parents:
78193
diff
changeset
|
273 |
} |
79682
1fa1b32b0379
build local log_db, with store/restore via optional database server;
wenzelm
parents:
79680
diff
changeset
|
274 |
|
1fa1b32b0379
build local log_db, with store/restore via optional database server;
wenzelm
parents:
79680
diff
changeset
|
275 |
|
1fa1b32b0379
build local log_db, with store/restore via optional database server;
wenzelm
parents:
79680
diff
changeset
|
276 |
/* log_db */ |
1fa1b32b0379
build local log_db, with store/restore via optional database server;
wenzelm
parents:
79680
diff
changeset
|
277 |
|
1fa1b32b0379
build local log_db, with store/restore via optional database server;
wenzelm
parents:
79680
diff
changeset
|
278 |
for (session <- sessions; path <- session.log_db) { |
1fa1b32b0379
build local log_db, with store/restore via optional database server;
wenzelm
parents:
79680
diff
changeset
|
279 |
val file_uuid = Store.read_build_uuid(path, session.name) |
1fa1b32b0379
build local log_db, with store/restore via optional database server;
wenzelm
parents:
79680
diff
changeset
|
280 |
private_data.read_log_db(db, session.name, old_uuid = file_uuid) match { |
1fa1b32b0379
build local log_db, with store/restore via optional database server;
wenzelm
parents:
79680
diff
changeset
|
281 |
case Some(log_db) if file_uuid.isEmpty => |
1fa1b32b0379
build local log_db, with store/restore via optional database server;
wenzelm
parents:
79680
diff
changeset
|
282 |
progress.echo("Restoring " + session.name + ".db ...") |
1fa1b32b0379
build local log_db, with store/restore via optional database server;
wenzelm
parents:
79680
diff
changeset
|
283 |
Isabelle_System.make_directory(path.expand.dir) |
1fa1b32b0379
build local log_db, with store/restore via optional database server;
wenzelm
parents:
79680
diff
changeset
|
284 |
Bytes.write(path, log_db.content) |
1fa1b32b0379
build local log_db, with store/restore via optional database server;
wenzelm
parents:
79680
diff
changeset
|
285 |
case Some(_) => error("Incoherent content for session database " + path) |
1fa1b32b0379
build local log_db, with store/restore via optional database server;
wenzelm
parents:
79680
diff
changeset
|
286 |
case None => |
1fa1b32b0379
build local log_db, with store/restore via optional database server;
wenzelm
parents:
79680
diff
changeset
|
287 |
} |
1fa1b32b0379
build local log_db, with store/restore via optional database server;
wenzelm
parents:
79680
diff
changeset
|
288 |
} |
78204 | 289 |
} |
78510
8f45302a9ff0
more thorough ML_Heap.restore: include ancestors; prefer simultaneous ML_Heap.get_entries: just one database access for heap hierarchy;
wenzelm
parents:
78509
diff
changeset
|
290 |
case _ => |
78196
140a6f2e3728
restore heaps from database, which takes precedence over file-system;
wenzelm
parents:
78193
diff
changeset
|
291 |
} |
140a6f2e3728
restore heaps from database, which takes precedence over file-system;
wenzelm
parents:
78193
diff
changeset
|
292 |
} |
76991 | 293 |
} |