author | wenzelm |
Sat, 24 Feb 2024 16:30:25 +0100 | |
changeset 79720 | deb3056ed823 |
parent 79719 | 8544f1045123 |
child 79769 | e9e74577755f |
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:" |
|
14 |
||
78182 | 15 |
def read_file_digest(heap: Path): Option[SHA1.Digest] = { |
76991 | 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 | 19 |
val n = File.size(heap) |
78953 | 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 | 24 |
else None |
25 |
} |
|
77711
25fd62cba347
clarified signature: more general operation Bytes.read_slice;
wenzelm
parents:
77206
diff
changeset
|
26 |
else None |
76991 | 27 |
} |
28 |
else None |
|
29 |
} |
|
30 |
||
78182 | 31 |
def write_file_digest(heap: Path): SHA1.Digest = |
32 |
read_file_digest(heap) getOrElse { |
|
77206 | 33 |
val digest = SHA1.digest(heap) |
34 |
File.append(heap, sha1_prefix + digest.toString) |
|
35 |
digest |
|
76991 | 36 |
} |
77720 | 37 |
|
38 |
||
39 |
/* SQL data model */ |
|
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 | 45 |
|
46 |
object Generic { |
|
47 |
val name = SQL.Column.string("name").make_primary_key |
|
48 |
} |
|
49 |
||
50 |
object Base { |
|
51 |
val name = Generic.name |
|
79688 | 52 |
val heap_size = SQL.Column.long("heap_size") |
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 | 56 |
|
79688 | 57 |
val table = make_table(List(name, heap_size, heap_digest, uuid, log_db)) |
78183 | 58 |
} |
59 |
||
79687 | 60 |
object Size { |
79686 | 61 |
val name = Generic.name |
79688 | 62 |
val heap = SQL.Column.string("heap") |
63 |
val log_db = SQL.Column.string("log_db") |
|
79686 | 64 |
|
79688 | 65 |
val table = make_table(List(name, heap, log_db), |
79686 | 66 |
body = |
79688 | 67 |
"SELECT name, pg_size_pretty(heap_size::bigint) as heap, " + |
68 |
" pg_size_pretty(length(log_db)::bigint) as log_db FROM " + Base.table.ident, |
|
79686 | 69 |
name = "size") |
70 |
} |
|
71 |
||
78183 | 72 |
object Slices { |
73 |
val name = Generic.name |
|
74 |
val slice = SQL.Column.int("slice").make_primary_key |
|
75 |
val content = SQL.Column.bytes("content") |
|
76 |
||
78266 | 77 |
val table = make_table(List(name, slice, content), name = "slices") |
78183 | 78 |
} |
79 |
||
78278 | 80 |
object Slices_Size { |
81 |
val name = Generic.name |
|
79683 | 82 |
val slice = Slices.slice |
83 |
val size = SQL.Column.string("size") |
|
78278 | 84 |
|
85 |
val table = make_table(List(name, slice, size), |
|
86 |
body = "SELECT name, slice, pg_size_pretty(length(content)::bigint) as size FROM " + |
|
87 |
Slices.table.ident, |
|
88 |
name = "slices_size") |
|
89 |
} |
|
90 |
||
79706 | 91 |
def read_digests(db: SQL.Database, names: Iterable[String]): Map[String, SHA1.Digest] = |
92 |
if (names.isEmpty) Map.empty |
|
93 |
else { |
|
94 |
db.execute_query_statement( |
|
95 |
Base.table.select(List(Base.name, Base.heap_digest), |
|
96 |
sql = Generic.name.where_member(names)), |
|
97 |
List.from[(String, String)], |
|
98 |
res => res.string(Base.name) -> res.string(Base.heap_digest) |
|
99 |
).collect({ |
|
100 |
case (name, digest) if digest.nonEmpty => name -> SHA1.fake_digest(digest) |
|
101 |
}).toMap |
|
102 |
} |
|
78183 | 103 |
|
79677 | 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 | 124 |
def write_slice(db: SQL.Database, name: String, slice: Int, content: Bytes): Unit = |
125 |
db.execute_statement(Slices.table.insert(), body = |
|
126 |
{ stmt => |
|
127 |
stmt.string(1) = name |
|
128 |
stmt.int(2) = slice |
|
129 |
stmt.bytes(3) = content |
|
130 |
}) |
|
131 |
||
78183 | 132 |
def clean_entry(db: SQL.Database, name: String): Unit = { |
133 |
for (table <- List(Base.table, Slices.table)) { |
|
134 |
db.execute_statement(table.delete(sql = Base.name.where_equal(name))) |
|
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 | 146 |
for (table <- List(Size.table, Slices_Size.table)) { |
79686 | 147 |
db.create_view(table) |
148 |
} |
|
78183 | 149 |
db.execute_statement(Base.table.insert(), body = |
150 |
{ stmt => |
|
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 | 156 |
}) |
79691
d298c5b65d8e
clarified store_session: heap requires process_result.ok, but log_db is always stored;
wenzelm
parents:
79690
diff
changeset
|
157 |
} |
78183 | 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 | 162 |
heap_size: Long, |
163 |
heap_digest: Option[SHA1.Digest], |
|
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 | 166 |
db.execute_statement( |
79688 | 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 | 169 |
body = |
170 |
{ stmt => |
|
79688 | 171 |
stmt.long(1) = heap_size |
172 |
stmt.string(2) = heap_digest.map(_.toString) |
|
173 |
stmt.string(3) = log_db.map(_.uuid) |
|
174 |
stmt.bytes(4) = log_db.map(_.content) |
|
78183 | 175 |
}) |
176 |
} |
|
177 |
||
78204 | 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 | 182 |
|
79677 | 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 | 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 | 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 | 198 |
val log_db = |
199 |
for { |
|
200 |
path <- session.log_db |
|
201 |
uuid <- proper_string(Store.read_build_uuid(path, session.name)) |
|
202 |
} yield Log_DB(uuid, Bytes.read(path)) |
|
78183 | 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 | 205 |
val heap_size = |
206 |
session.heap match { |
|
207 |
case Some(heap) => File.size(heap) - sha1_prefix.length - SHA1.digest_length |
|
208 |
case None => 0L |
|
209 |
} |
|
210 |
||
211 |
val slice_size = slice.bytes max Space.MiB(1).bytes |
|
212 |
val slices = (heap_size.toDouble / slice_size.toDouble).ceil.toInt |
|
79719 | 213 |
val step = if (slices == 0) 0L else (heap_size.toDouble / slices.toDouble).ceil.toLong |
214 |
||
215 |
def slice_content(i: Int): Bytes = { |
|
216 |
val j = i + 1 |
|
217 |
val offset = step * i |
|
218 |
val limit = if (j < slices) step * j else heap_size |
|
219 |
Bytes.read_file(session.the_heap, offset = offset, limit = limit) |
|
220 |
.compress(cache = cache) |
|
221 |
} |
|
79694 | 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 | 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 | 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 | 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 | 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 | 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 | 270 |
if (sessions.exists(_.defined)) { |
271 |
private_data.transaction_lock(db, create = true, label = "ML_Heap.restore") { |
|
272 |
/* heap */ |
|
79682
1fa1b32b0379
build local log_db, with store/restore via optional database server;
wenzelm
parents:
79680
diff
changeset
|
273 |
|
79698 | 274 |
val defined_heaps = |
275 |
for (session <- sessions; heap <- session.heap) |
|
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 | 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 | 280 |
for ((session_name, heap) <- defined_heaps) { |
281 |
val file_digest = read_file_digest(heap) |
|
282 |
val db_digest = db_digests.get(session_name) |
|
283 |
if (db_digest.isDefined && db_digest != file_digest) { |
|
284 |
progress.echo("Restoring " + session_name + " ...") |
|
285 |
||
286 |
val base_dir = Isabelle_System.make_directory(heap.expand.dir) |
|
287 |
Isabelle_System.with_tmp_file(session_name + "_", base_dir = base_dir.file) { tmp => |
|
288 |
Bytes.write(tmp, Bytes.empty) |
|
289 |
for (slice <- private_data.read_slices(db, session_name)) { |
|
290 |
Bytes.append(tmp, slice.uncompress(cache = cache)) |
|
78204 | 291 |
} |
79698 | 292 |
val digest = write_file_digest(tmp) |
293 |
if (db_digest.get == digest) { |
|
294 |
Isabelle_System.chmod("a+r", tmp) |
|
295 |
Isabelle_System.move_file(tmp, heap) |
|
296 |
} |
|
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 | 300 |
} |
79698 | 301 |
|
302 |
||
303 |
/* log_db */ |
|
304 |
||
305 |
for (session <- sessions; path <- session.log_db) { |
|
306 |
val file_uuid = Store.read_build_uuid(path, session.name) |
|
307 |
private_data.read_log_db(db, session.name, old_uuid = file_uuid) match { |
|
308 |
case Some(log_db) if file_uuid.isEmpty => |
|
309 |
progress.echo("Restoring " + session.log_db_name + " ...") |
|
310 |
Isabelle_System.make_directory(path.expand.dir) |
|
311 |
Bytes.write(path, log_db.content) |
|
312 |
case Some(_) => error("Incoherent content for session database " + path) |
|
313 |
case None => |
|
314 |
} |
|
315 |
} |
|
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 | 319 |
} |