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