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