author | wenzelm |
Tue, 02 Aug 2022 19:25:37 +0200 | |
changeset 75738 | 9cc5ee625adb |
parent 75736 | 6b319113b3a4 |
child 75739 | 5b37466c1463 |
permissions | -rw-r--r-- |
68092
888d35a19866
store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff
changeset
|
1 |
/* Title: Pure/Thy/export.scala |
888d35a19866
store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff
changeset
|
2 |
Author: Makarius |
888d35a19866
store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff
changeset
|
3 |
|
68102 | 4 |
Manage theory exports: compressed blobs. |
68092
888d35a19866
store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff
changeset
|
5 |
*/ |
888d35a19866
store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff
changeset
|
6 |
|
888d35a19866
store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff
changeset
|
7 |
package isabelle |
888d35a19866
store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff
changeset
|
8 |
|
68116 | 9 |
|
10 |
import scala.annotation.tailrec |
|
11 |
import scala.util.matching.Regex |
|
12 |
||
13 |
||
75393 | 14 |
object Export { |
72691 | 15 |
/* artefact names */ |
16 |
||
72844 | 17 |
val DOCUMENT_ID = "PIDE/document_id" |
18 |
val FILES = "PIDE/files" |
|
72702 | 19 |
val MARKUP = "PIDE/markup" |
20 |
val MESSAGES = "PIDE/messages" |
|
72691 | 21 |
val DOCUMENT_PREFIX = "document/" |
73785 | 22 |
val DOCUMENT_LATEX = DOCUMENT_PREFIX + "latex" |
23 |
val DOCUMENT_CITATIONS = DOCUMENT_PREFIX + "citations" |
|
72691 | 24 |
val THEORY_PREFIX: String = "theory/" |
25 |
val PROOFS_PREFIX: String = "proofs/" |
|
69634 | 26 |
|
69756 | 27 |
def explode_name(s: String): List[String] = space_explode('/', s) |
28 |
def implode_name(elems: Iterable[String]): String = elems.mkString("/") |
|
69634 | 29 |
|
30 |
||
68092
888d35a19866
store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff
changeset
|
31 |
/* SQL data model */ |
888d35a19866
store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff
changeset
|
32 |
|
75393 | 33 |
object Data { |
68092
888d35a19866
store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff
changeset
|
34 |
val session_name = SQL.Column.string("session_name").make_primary_key |
888d35a19866
store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff
changeset
|
35 |
val theory_name = SQL.Column.string("theory_name").make_primary_key |
888d35a19866
store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff
changeset
|
36 |
val name = SQL.Column.string("name").make_primary_key |
69788 | 37 |
val executable = SQL.Column.bool("executable") |
68092
888d35a19866
store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff
changeset
|
38 |
val compressed = SQL.Column.bool("compressed") |
888d35a19866
store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff
changeset
|
39 |
val body = SQL.Column.bytes("body") |
888d35a19866
store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff
changeset
|
40 |
|
888d35a19866
store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff
changeset
|
41 |
val table = |
69788 | 42 |
SQL.Table("isabelle_exports", |
43 |
List(session_name, theory_name, name, executable, compressed, body)) |
|
68092
888d35a19866
store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff
changeset
|
44 |
|
68116 | 45 |
def where_equal(session_name: String, theory_name: String = "", name: String = ""): SQL.Source = |
68092
888d35a19866
store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff
changeset
|
46 |
"WHERE " + Data.session_name.equal(session_name) + |
68116 | 47 |
(if (theory_name == "") "" else " AND " + Data.theory_name.equal(theory_name)) + |
48 |
(if (name == "") "" else " AND " + Data.name.equal(name)) |
|
49 |
} |
|
50 |
||
75674 | 51 |
def compound_name(a: String, b: String): String = |
52 |
if (a.isEmpty) b else a + ":" + b |
|
53 |
||
75672 | 54 |
sealed case class Entry_Name(session: String = "", theory: String = "", name: String = "") { |
75674 | 55 |
val compound_name: String = Export.compound_name(theory, name) |
56 |
||
75680 | 57 |
def make_path(prune: Int = 0): Path = { |
75675 | 58 |
val elems = theory :: space_explode('/', name) |
59 |
if (elems.length < prune + 1) { |
|
60 |
error("Cannot prune path by " + prune + " element(s): " + Path.make(elems)) |
|
61 |
} |
|
75680 | 62 |
else Path.make(elems.drop(prune)) |
75675 | 63 |
} |
64 |
||
75671 | 65 |
def readable(db: SQL.Database): Boolean = { |
66 |
val select = Data.table.select(List(Data.name), Data.where_equal(session, theory, name)) |
|
67 |
db.using_statement(select)(stmt => stmt.execute_query().next()) |
|
68 |
} |
|
69 |
||
70 |
def read(db: SQL.Database, cache: XML.Cache): Option[Entry] = { |
|
71 |
val select = |
|
72 |
Data.table.select(List(Data.executable, Data.compressed, Data.body), |
|
73 |
Data.where_equal(session, theory, name)) |
|
74 |
db.using_statement(select) { stmt => |
|
75 |
val res = stmt.execute_query() |
|
76 |
if (res.next()) { |
|
77 |
val executable = res.bool(Data.executable) |
|
78 |
val compressed = res.bool(Data.compressed) |
|
79 |
val bytes = res.bytes(Data.body) |
|
80 |
val body = Future.value(compressed, bytes) |
|
81 |
Some(Entry(this, executable, body, cache)) |
|
82 |
} |
|
83 |
else None |
|
84 |
} |
|
85 |
} |
|
68092
888d35a19866
store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff
changeset
|
86 |
} |
888d35a19866
store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff
changeset
|
87 |
|
75393 | 88 |
def read_theory_names(db: SQL.Database, session_name: String): List[String] = { |
68222 | 89 |
val select = |
90 |
Data.table.select(List(Data.theory_name), Data.where_equal(session_name), distinct = true) |
|
91 |
db.using_statement(select)(stmt => |
|
92 |
stmt.execute_query().iterator(_.string(Data.theory_name)).toList) |
|
93 |
} |
|
94 |
||
75674 | 95 |
def read_entry_names(db: SQL.Database, session_name: String): List[Entry_Name] = { |
75658
5905c7f484b3
clarified signature: read_theory_exports is already ordered;
wenzelm
parents:
75394
diff
changeset
|
96 |
val select = |
5905c7f484b3
clarified signature: read_theory_exports is already ordered;
wenzelm
parents:
75394
diff
changeset
|
97 |
Data.table.select(List(Data.theory_name, Data.name), Data.where_equal(session_name)) + |
5905c7f484b3
clarified signature: read_theory_exports is already ordered;
wenzelm
parents:
75394
diff
changeset
|
98 |
" ORDER BY " + Data.theory_name + ", " + Data.name |
68116 | 99 |
db.using_statement(select)(stmt => |
100 |
stmt.execute_query().iterator(res => |
|
75674 | 101 |
Entry_Name(session = session_name, |
102 |
theory = res.string(Data.theory_name), |
|
103 |
name = res.string(Data.name))).toList) |
|
68115 | 104 |
} |
105 |
||
68104 | 106 |
def message(msg: String, theory_name: String, name: String): String = |
107 |
msg + " " + quote(name) + " for theory " + quote(theory_name) |
|
108 |
||
72854 | 109 |
def empty_entry(theory_name: String, name: String): Entry = |
75672 | 110 |
Entry(Entry_Name(theory = theory_name, name = name), |
111 |
false, Future.value(false, Bytes.empty), XML.Cache.none) |
|
72634
5cea0993ee4f
clarified access to single database server vs. collection of database files;
wenzelm
parents:
72375
diff
changeset
|
112 |
|
68092
888d35a19866
store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff
changeset
|
113 |
sealed case class Entry( |
75671 | 114 |
entry_name: Entry_Name, |
69788 | 115 |
executable: Boolean, |
72847
9dda93a753b1
clarified signature: provide XZ.Cache where Export.Entry is created;
wenzelm
parents:
72844
diff
changeset
|
116 |
body: Future[(Boolean, Bytes)], |
75393 | 117 |
cache: XML.Cache |
118 |
) { |
|
75671 | 119 |
def session_name: String = entry_name.session |
120 |
def theory_name: String = entry_name.theory |
|
121 |
def name: String = entry_name.name |
|
69635 | 122 |
override def toString: String = name |
69630 | 123 |
|
75674 | 124 |
def compound_name: String = entry_name.compound_name |
71141 | 125 |
|
72691 | 126 |
def name_has_prefix(s: String): Boolean = name.startsWith(s) |
69634 | 127 |
val name_elems: List[String] = explode_name(name) |
128 |
||
129 |
def name_extends(elems: List[String]): Boolean = |
|
130 |
name_elems.startsWith(elems) && name_elems != elems |
|
131 |
||
72847
9dda93a753b1
clarified signature: provide XZ.Cache where Export.Entry is created;
wenzelm
parents:
72844
diff
changeset
|
132 |
def text: String = uncompressed.text |
68092
888d35a19866
store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff
changeset
|
133 |
|
75393 | 134 |
def uncompressed: Bytes = { |
69629 | 135 |
val (compressed, bytes) = body.join |
73031
f93f0597f4fb
clarified signature: absorb XZ.Cache into XML.Cache;
wenzelm
parents:
73024
diff
changeset
|
136 |
if (compressed) bytes.uncompress(cache = cache.xz) else bytes |
69629 | 137 |
} |
138 |
||
72847
9dda93a753b1
clarified signature: provide XZ.Cache where Export.Entry is created;
wenzelm
parents:
72844
diff
changeset
|
139 |
def uncompressed_yxml: XML.Body = |
73033 | 140 |
YXML.parse_body(UTF8.decode_permissive(uncompressed), cache = cache) |
69629 | 141 |
|
75393 | 142 |
def write(db: SQL.Database): Unit = { |
68167 | 143 |
val (compressed, bytes) = body.join |
75394 | 144 |
db.using_statement(Data.table.insert()) { stmt => |
68092
888d35a19866
store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff
changeset
|
145 |
stmt.string(1) = session_name |
888d35a19866
store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff
changeset
|
146 |
stmt.string(2) = theory_name |
888d35a19866
store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff
changeset
|
147 |
stmt.string(3) = name |
69788 | 148 |
stmt.bool(4) = executable |
149 |
stmt.bool(5) = compressed |
|
150 |
stmt.bytes(6) = bytes |
|
68092
888d35a19866
store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff
changeset
|
151 |
stmt.execute() |
75394 | 152 |
} |
68092
888d35a19866
store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff
changeset
|
153 |
} |
68116 | 154 |
} |
155 |
||
75393 | 156 |
def make_regex(pattern: String): Regex = { |
68116 | 157 |
@tailrec def make(result: List[String], depth: Int, chs: List[Char]): Regex = |
158 |
chs match { |
|
159 |
case '*' :: '*' :: rest => make("[^:]*" :: result, depth, rest) |
|
160 |
case '*' :: rest => make("[^:/]*" :: result, depth, rest) |
|
161 |
case '?' :: rest => make("[^:/]" :: result, depth, rest) |
|
162 |
case '\\' :: c :: rest => make(("\\" + c) :: result, depth, rest) |
|
163 |
case '{' :: rest => make("(" :: result, depth + 1, rest) |
|
164 |
case ',' :: rest if depth > 0 => make("|" :: result, depth, rest) |
|
165 |
case '}' :: rest if depth > 0 => make(")" :: result, depth - 1, rest) |
|
166 |
case c :: rest if ".+()".contains(c) => make(("\\" + c) :: result, depth, rest) |
|
167 |
case c :: rest => make(c.toString :: result, depth, rest) |
|
168 |
case Nil => result.reverse.mkString.r |
|
169 |
} |
|
170 |
make(Nil, 0, pattern.toList) |
|
68092
888d35a19866
store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff
changeset
|
171 |
} |
888d35a19866
store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff
changeset
|
172 |
|
75674 | 173 |
def make_matcher(pats: List[String]): Entry_Name => Boolean = { |
75658
5905c7f484b3
clarified signature: read_theory_exports is already ordered;
wenzelm
parents:
75394
diff
changeset
|
174 |
val regs = pats.map(make_regex) |
75674 | 175 |
(entry_name: Entry_Name) => |
176 |
regs.exists(_.pattern.matcher(entry_name.compound_name).matches) |
|
68151 | 177 |
} |
178 |
||
72847
9dda93a753b1
clarified signature: provide XZ.Cache where Export.Entry is created;
wenzelm
parents:
72844
diff
changeset
|
179 |
def make_entry( |
75393 | 180 |
session_name: String, |
181 |
args: Protocol.Export.Args, |
|
182 |
bytes: Bytes, |
|
183 |
cache: XML.Cache |
|
184 |
): Entry = { |
|
72847
9dda93a753b1
clarified signature: provide XZ.Cache where Export.Entry is created;
wenzelm
parents:
72844
diff
changeset
|
185 |
val body = |
73031
f93f0597f4fb
clarified signature: absorb XZ.Cache into XML.Cache;
wenzelm
parents:
73024
diff
changeset
|
186 |
if (args.compress) Future.fork(bytes.maybe_compress(cache = cache.xz)) |
72847
9dda93a753b1
clarified signature: provide XZ.Cache where Export.Entry is created;
wenzelm
parents:
72844
diff
changeset
|
187 |
else Future.value((false, bytes)) |
75672 | 188 |
val entry_name = Entry_Name(session = session_name, theory = args.theory_name, name = args.name) |
75671 | 189 |
Entry(entry_name, args.executable, body, cache) |
68831 | 190 |
} |
191 |
||
68092
888d35a19866
store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff
changeset
|
192 |
|
75733 | 193 |
/* specific entries */ |
194 |
||
195 |
def read_document_id(read: String => Entry): Option[Long] = |
|
196 |
read(DOCUMENT_ID).text match { |
|
197 |
case Value.Long(id) => Some(id) |
|
198 |
case _ => None |
|
199 |
} |
|
200 |
||
201 |
def read_files(read: String => Entry): Option[(String, List[String])] = |
|
202 |
split_lines(read(FILES).text) match { |
|
203 |
case thy_file :: blobs_files => Some((thy_file, blobs_files)) |
|
204 |
case Nil => None |
|
205 |
} |
|
206 |
||
207 |
||
68092
888d35a19866
store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff
changeset
|
208 |
/* database consumer thread */ |
888d35a19866
store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff
changeset
|
209 |
|
74255 | 210 |
def consumer(db: SQL.Database, cache: XML.Cache, progress: Progress = new Progress): Consumer = |
211 |
new Consumer(db, cache, progress) |
|
68092
888d35a19866
store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff
changeset
|
212 |
|
75393 | 213 |
class Consumer private[Export](db: SQL.Database, cache: XML.Cache, progress: Progress) { |
68924 | 214 |
private val errors = Synchronized[List[String]](Nil) |
68092
888d35a19866
store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff
changeset
|
215 |
|
888d35a19866
store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff
changeset
|
216 |
private val consumer = |
71145
2f782d5f5d5a
improved performance of session exports via bulk transactions;
wenzelm
parents:
71141
diff
changeset
|
217 |
Consumer_Thread.fork_bulk[(Entry, Boolean)](name = "export")( |
2f782d5f5d5a
improved performance of session exports via bulk transactions;
wenzelm
parents:
71141
diff
changeset
|
218 |
bulk = { case (entry, _) => entry.body.is_finished }, |
2f782d5f5d5a
improved performance of session exports via bulk transactions;
wenzelm
parents:
71141
diff
changeset
|
219 |
consume = |
75394 | 220 |
{ (args: List[(Entry, Boolean)]) => |
71145
2f782d5f5d5a
improved performance of session exports via bulk transactions;
wenzelm
parents:
71141
diff
changeset
|
221 |
val results = |
2f782d5f5d5a
improved performance of session exports via bulk transactions;
wenzelm
parents:
71141
diff
changeset
|
222 |
db.transaction { |
74256 | 223 |
for ((entry, strict) <- args) |
71145
2f782d5f5d5a
improved performance of session exports via bulk transactions;
wenzelm
parents:
71141
diff
changeset
|
224 |
yield { |
74257 | 225 |
if (progress.stopped) { |
226 |
entry.body.cancel() |
|
227 |
Exn.Res(()) |
|
228 |
} |
|
75671 | 229 |
else if (entry.entry_name.readable(db)) { |
71145
2f782d5f5d5a
improved performance of session exports via bulk transactions;
wenzelm
parents:
71141
diff
changeset
|
230 |
if (strict) { |
2f782d5f5d5a
improved performance of session exports via bulk transactions;
wenzelm
parents:
71141
diff
changeset
|
231 |
val msg = message("Duplicate export", entry.theory_name, entry.name) |
2f782d5f5d5a
improved performance of session exports via bulk transactions;
wenzelm
parents:
71141
diff
changeset
|
232 |
errors.change(msg :: _) |
2f782d5f5d5a
improved performance of session exports via bulk transactions;
wenzelm
parents:
71141
diff
changeset
|
233 |
} |
2f782d5f5d5a
improved performance of session exports via bulk transactions;
wenzelm
parents:
71141
diff
changeset
|
234 |
Exn.Res(()) |
2f782d5f5d5a
improved performance of session exports via bulk transactions;
wenzelm
parents:
71141
diff
changeset
|
235 |
} |
2f782d5f5d5a
improved performance of session exports via bulk transactions;
wenzelm
parents:
71141
diff
changeset
|
236 |
else Exn.capture { entry.write(db) } |
2f782d5f5d5a
improved performance of session exports via bulk transactions;
wenzelm
parents:
71141
diff
changeset
|
237 |
} |
70499 | 238 |
} |
71145
2f782d5f5d5a
improved performance of session exports via bulk transactions;
wenzelm
parents:
71141
diff
changeset
|
239 |
(results, true) |
2f782d5f5d5a
improved performance of session exports via bulk transactions;
wenzelm
parents:
71141
diff
changeset
|
240 |
}) |
68092
888d35a19866
store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff
changeset
|
241 |
|
75393 | 242 |
def apply(session_name: String, args: Protocol.Export.Args, body: Bytes): Unit = { |
74257 | 243 |
if (!progress.stopped) { |
244 |
consumer.send(make_entry(session_name, args, body, cache) -> args.strict) |
|
245 |
} |
|
246 |
} |
|
68092
888d35a19866
store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff
changeset
|
247 |
|
75393 | 248 |
def shutdown(close: Boolean = false): List[String] = { |
68092
888d35a19866
store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff
changeset
|
249 |
consumer.shutdown() |
888d35a19866
store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff
changeset
|
250 |
if (close) db.close() |
74255 | 251 |
errors.value.reverse ::: (if (progress.stopped) List("Export stopped") else Nil) |
68092
888d35a19866
store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff
changeset
|
252 |
} |
888d35a19866
store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff
changeset
|
253 |
} |
68116 | 254 |
|
255 |
||
68418 | 256 |
/* abstract provider */ |
257 |
||
75393 | 258 |
object Provider { |
71014 | 259 |
def none: Provider = |
260 |
new Provider { |
|
261 |
def apply(export_name: String): Option[Entry] = None |
|
262 |
def focus(other_theory: String): Provider = this |
|
263 |
||
264 |
override def toString: String = "none" |
|
265 |
} |
|
266 |
||
75393 | 267 |
def database( |
268 |
db: SQL.Database, |
|
269 |
cache: XML.Cache, |
|
270 |
session_name: String, |
|
271 |
theory_name: String |
|
272 |
) : Provider = { |
|
68418 | 273 |
new Provider { |
274 |
def apply(export_name: String): Option[Entry] = |
|
75735 | 275 |
if (theory_name.isEmpty) None |
276 |
else { |
|
277 |
Entry_Name(session = session_name, theory = theory_name, name = export_name) |
|
278 |
.read(db, cache) |
|
279 |
} |
|
68832 | 280 |
|
70539
30b3c58a1933
support Export_Theory.read_proof, based on theory_name and serial;
wenzelm
parents:
70499
diff
changeset
|
281 |
def focus(other_theory: String): Provider = |
30b3c58a1933
support Export_Theory.read_proof, based on theory_name and serial;
wenzelm
parents:
70499
diff
changeset
|
282 |
if (other_theory == theory_name) this |
72847
9dda93a753b1
clarified signature: provide XZ.Cache where Export.Entry is created;
wenzelm
parents:
72844
diff
changeset
|
283 |
else Provider.database(db, cache, session_name, other_theory) |
70539
30b3c58a1933
support Export_Theory.read_proof, based on theory_name and serial;
wenzelm
parents:
70499
diff
changeset
|
284 |
|
68832 | 285 |
override def toString: String = db.toString |
68418 | 286 |
} |
73031
f93f0597f4fb
clarified signature: absorb XZ.Cache into XML.Cache;
wenzelm
parents:
73024
diff
changeset
|
287 |
} |
68418 | 288 |
|
289 |
def snapshot(snapshot: Document.Snapshot): Provider = |
|
290 |
new Provider { |
|
291 |
def apply(export_name: String): Option[Entry] = |
|
292 |
snapshot.exports_map.get(export_name) |
|
68832 | 293 |
|
70539
30b3c58a1933
support Export_Theory.read_proof, based on theory_name and serial;
wenzelm
parents:
70499
diff
changeset
|
294 |
def focus(other_theory: String): Provider = |
30b3c58a1933
support Export_Theory.read_proof, based on theory_name and serial;
wenzelm
parents:
70499
diff
changeset
|
295 |
if (other_theory == snapshot.node_name.theory) this |
30b3c58a1933
support Export_Theory.read_proof, based on theory_name and serial;
wenzelm
parents:
70499
diff
changeset
|
296 |
else { |
30b3c58a1933
support Export_Theory.read_proof, based on theory_name and serial;
wenzelm
parents:
70499
diff
changeset
|
297 |
val node_name = |
30b3c58a1933
support Export_Theory.read_proof, based on theory_name and serial;
wenzelm
parents:
70499
diff
changeset
|
298 |
snapshot.version.nodes.theory_name(other_theory) getOrElse |
30b3c58a1933
support Export_Theory.read_proof, based on theory_name and serial;
wenzelm
parents:
70499
diff
changeset
|
299 |
error("Bad theory " + quote(other_theory)) |
30b3c58a1933
support Export_Theory.read_proof, based on theory_name and serial;
wenzelm
parents:
70499
diff
changeset
|
300 |
Provider.snapshot(snapshot.state.snapshot(node_name)) |
30b3c58a1933
support Export_Theory.read_proof, based on theory_name and serial;
wenzelm
parents:
70499
diff
changeset
|
301 |
} |
30b3c58a1933
support Export_Theory.read_proof, based on theory_name and serial;
wenzelm
parents:
70499
diff
changeset
|
302 |
|
68832 | 303 |
override def toString: String = snapshot.toString |
68418 | 304 |
} |
305 |
} |
|
306 |
||
75393 | 307 |
trait Provider { |
68418 | 308 |
def apply(export_name: String): Option[Entry] |
309 |
||
72847
9dda93a753b1
clarified signature: provide XZ.Cache where Export.Entry is created;
wenzelm
parents:
72844
diff
changeset
|
310 |
def uncompressed_yxml(export_name: String): XML.Body = |
68418 | 311 |
apply(export_name) match { |
72847
9dda93a753b1
clarified signature: provide XZ.Cache where Export.Entry is created;
wenzelm
parents:
72844
diff
changeset
|
312 |
case Some(entry) => entry.uncompressed_yxml |
68418 | 313 |
case None => Nil |
314 |
} |
|
70539
30b3c58a1933
support Export_Theory.read_proof, based on theory_name and serial;
wenzelm
parents:
70499
diff
changeset
|
315 |
|
30b3c58a1933
support Export_Theory.read_proof, based on theory_name and serial;
wenzelm
parents:
70499
diff
changeset
|
316 |
def focus(other_theory: String): Provider |
68418 | 317 |
} |
318 |
||
319 |
||
68288 | 320 |
/* export to file-system */ |
321 |
||
322 |
def export_files( |
|
323 |
store: Sessions.Store, |
|
324 |
session_name: String, |
|
325 |
export_dir: Path, |
|
71726
a5fda30edae2
clarified signature: more uniform treatment of stopped/interrupted state;
wenzelm
parents:
71624
diff
changeset
|
326 |
progress: Progress = new Progress, |
69671 | 327 |
export_prune: Int = 0, |
68288 | 328 |
export_list: Boolean = false, |
75393 | 329 |
export_patterns: List[String] = Nil |
330 |
): Unit = { |
|
75394 | 331 |
using(store.open_database(session_name)) { db => |
68288 | 332 |
db.transaction { |
75674 | 333 |
val entry_names = read_entry_names(db, session_name) |
68288 | 334 |
|
335 |
// list |
|
336 |
if (export_list) { |
|
75674 | 337 |
for (entry_name <- entry_names) progress.echo(entry_name.compound_name) |
68288 | 338 |
} |
339 |
||
340 |
// export |
|
68290 | 341 |
if (export_patterns.nonEmpty) { |
75658
5905c7f484b3
clarified signature: read_theory_exports is already ordered;
wenzelm
parents:
75394
diff
changeset
|
342 |
val matcher = make_matcher(export_patterns) |
68288 | 343 |
for { |
75674 | 344 |
entry_name <- entry_names if matcher(entry_name) |
345 |
entry <- entry_name.read(db, store.cache) |
|
68288 | 346 |
} { |
75680 | 347 |
val path = export_dir + entry_name.make_path(prune = export_prune) |
69811
18f61ce86425
clarified 'export_files' in session ROOT: require explicit "isabelle build -e";
wenzelm
parents:
69789
diff
changeset
|
348 |
progress.echo("export " + path + (if (entry.executable) " (executable)" else "")) |
72375 | 349 |
Isabelle_System.make_directory(path.dir) |
74215
7515abfe18cf
avoid change of existing file, notably rebuild via ghc_stack;
wenzelm
parents:
73785
diff
changeset
|
350 |
val bytes = entry.uncompressed |
7515abfe18cf
avoid change of existing file, notably rebuild via ghc_stack;
wenzelm
parents:
73785
diff
changeset
|
351 |
if (!path.is_file || Bytes.read(path) != bytes) Bytes.write(path, bytes) |
69789
2c3e5e58d93f
more thorough File.set_executable, notably for Windows;
wenzelm
parents:
69788
diff
changeset
|
352 |
File.set_executable(path, entry.executable) |
68288 | 353 |
} |
354 |
} |
|
355 |
} |
|
75394 | 356 |
} |
68288 | 357 |
} |
358 |
||
359 |
||
68116 | 360 |
/* Isabelle tool wrapper */ |
361 |
||
71601 | 362 |
val default_export_dir: Path = Path.explode("export") |
68116 | 363 |
|
75394 | 364 |
val isabelle_tool = |
365 |
Isabelle_Tool("export", "retrieve theory exports", Scala_Project.here, |
|
366 |
{ args => |
|
367 |
/* arguments */ |
|
68116 | 368 |
|
75394 | 369 |
var export_dir = default_export_dir |
370 |
var dirs: List[Path] = Nil |
|
371 |
var export_list = false |
|
372 |
var no_build = false |
|
373 |
var options = Options.init() |
|
374 |
var export_prune = 0 |
|
375 |
var export_patterns: List[String] = Nil |
|
68116 | 376 |
|
75394 | 377 |
val getopts = Getopts(""" |
68116 | 378 |
Usage: isabelle export [OPTIONS] SESSION |
379 |
||
380 |
Options are: |
|
68314
2acbf8129d8b
clarified option -O: avoid conflict with build/dump option -D;
wenzelm
parents:
68305
diff
changeset
|
381 |
-O DIR output directory for exported files (default: """ + default_export_dir + """) |
68116 | 382 |
-d DIR include session directory |
383 |
-l list exports |
|
384 |
-n no build of session |
|
385 |
-o OPTION override Isabelle system OPTION (via NAME=VAL or NAME) |
|
69671 | 386 |
-p NUM prune path of exported files by NUM elements |
68116 | 387 |
-x PATTERN extract files matching pattern (e.g. "*:**" for all) |
388 |
||
389 |
List or export theory exports for SESSION: named blobs produced by |
|
68290 | 390 |
isabelle build. Option -l or -x is required; option -x may be repeated. |
68116 | 391 |
|
392 |
The PATTERN language resembles glob patterns in the shell, with ? and * |
|
393 |
(both excluding ":" and "/"), ** (excluding ":"), and [abc] or [^abc], |
|
394 |
and variants {pattern1,pattern2,pattern3}. |
|
395 |
""", |
|
75394 | 396 |
"O:" -> (arg => export_dir = Path.explode(arg)), |
397 |
"d:" -> (arg => dirs = dirs ::: List(Path.explode(arg))), |
|
398 |
"l" -> (_ => export_list = true), |
|
399 |
"n" -> (_ => no_build = true), |
|
400 |
"o:" -> (arg => options = options + arg), |
|
401 |
"p:" -> (arg => export_prune = Value.Int.parse(arg)), |
|
402 |
"x:" -> (arg => export_patterns ::= arg)) |
|
68116 | 403 |
|
75394 | 404 |
val more_args = getopts(args) |
405 |
val session_name = |
|
406 |
more_args match { |
|
407 |
case List(session_name) if export_list || export_patterns.nonEmpty => session_name |
|
408 |
case _ => getopts.usage() |
|
409 |
} |
|
68116 | 410 |
|
75394 | 411 |
val progress = new Console_Progress() |
68305 | 412 |
|
68116 | 413 |
|
75394 | 414 |
/* build */ |
68116 | 415 |
|
75394 | 416 |
if (!no_build) { |
417 |
val rc = |
|
418 |
progress.interrupt_handler { |
|
419 |
Build.build_logic(options, session_name, progress = progress, dirs = dirs) |
|
420 |
} |
|
421 |
if (rc != Process_Result.RC.ok) sys.exit(rc) |
|
68331 | 422 |
} |
68116 | 423 |
|
424 |
||
75394 | 425 |
/* export files */ |
68116 | 426 |
|
75394 | 427 |
val store = Sessions.store(options) |
428 |
export_files(store, session_name, export_dir, progress = progress, export_prune = export_prune, |
|
429 |
export_list = export_list, export_patterns = export_patterns) |
|
430 |
}) |
|
68092
888d35a19866
store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff
changeset
|
431 |
} |