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