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