author | wenzelm |
Mon, 17 Jul 2023 15:31:42 +0200 | |
changeset 78379 | f6ec57648894 |
parent 78367 | 4978a158dc4c |
child 78396 | 7853d9072d1b |
permissions | -rw-r--r-- |
68092
888d35a19866
store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff
changeset
|
1 |
/* Title: Pure/Thy/export.scala |
888d35a19866
store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff
changeset
|
2 |
Author: Makarius |
888d35a19866
store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff
changeset
|
3 |
|
68102 | 4 |
Manage theory exports: compressed blobs. |
68092
888d35a19866
store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff
changeset
|
5 |
*/ |
888d35a19866
store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff
changeset
|
6 |
|
888d35a19866
store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff
changeset
|
7 |
package isabelle |
888d35a19866
store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff
changeset
|
8 |
|
68116 | 9 |
|
10 |
import scala.annotation.tailrec |
|
11 |
import scala.util.matching.Regex |
|
12 |
||
13 |
||
75393 | 14 |
object Export { |
72691 | 15 |
/* artefact names */ |
16 |
||
72844 | 17 |
val DOCUMENT_ID = "PIDE/document_id" |
18 |
val FILES = "PIDE/files" |
|
72702 | 19 |
val MARKUP = "PIDE/markup" |
20 |
val MESSAGES = "PIDE/messages" |
|
72691 | 21 |
val DOCUMENT_PREFIX = "document/" |
73785 | 22 |
val DOCUMENT_LATEX = DOCUMENT_PREFIX + "latex" |
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 |
|
78210 | 32 |
object Data extends SQL.Data() { |
33 |
override lazy val tables = SQL.Tables(Base.table) |
|
34 |
||
78208 | 35 |
object Base { |
36 |
val session_name = SQL.Column.string("session_name").make_primary_key |
|
37 |
val theory_name = SQL.Column.string("theory_name").make_primary_key |
|
38 |
val name = SQL.Column.string("name").make_primary_key |
|
39 |
val executable = SQL.Column.bool("executable") |
|
40 |
val compressed = SQL.Column.bool("compressed") |
|
41 |
val body = SQL.Column.bytes("body") |
|
68092
888d35a19866
store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff
changeset
|
42 |
|
78208 | 43 |
val table = |
44 |
SQL.Table("isabelle_exports", |
|
45 |
List(session_name, theory_name, name, executable, compressed, body)) |
|
46 |
} |
|
68092
888d35a19866
store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff
changeset
|
47 |
|
68116 | 48 |
def where_equal(session_name: String, theory_name: String = "", name: String = ""): SQL.Source = |
78153 | 49 |
SQL.where_and( |
78208 | 50 |
Base.session_name.equal(session_name), |
51 |
if_proper(theory_name, Base.theory_name.equal(theory_name)), |
|
52 |
if_proper(name, Base.name.equal(name))) |
|
78209 | 53 |
|
78260
0a7f7abbe4f0
more robust transaction_lock: avoid overlapping data spaces;
wenzelm
parents:
78211
diff
changeset
|
54 |
def clean_session(db: SQL.Database, session_name: String): Unit = |
0a7f7abbe4f0
more robust transaction_lock: avoid overlapping data spaces;
wenzelm
parents:
78211
diff
changeset
|
55 |
db.execute_statement(Base.table.delete(sql = where_equal(session_name))) |
0a7f7abbe4f0
more robust transaction_lock: avoid overlapping data spaces;
wenzelm
parents:
78211
diff
changeset
|
56 |
|
78209 | 57 |
def readable_entry(db: SQL.Database, entry_name: Entry_Name): Boolean = { |
58 |
db.execute_query_statementB( |
|
78210 | 59 |
Base.table.select(List(Base.name), |
78209 | 60 |
sql = where_equal(entry_name.session, entry_name.theory, entry_name.name))) |
61 |
} |
|
62 |
||
63 |
def read_entry(db: SQL.Database, entry_name: Entry_Name, cache: XML.Cache): Option[Entry] = |
|
64 |
db.execute_query_statementO[Entry]( |
|
78210 | 65 |
Base.table.select(List(Base.executable, Base.compressed, Base.body), |
78209 | 66 |
sql = Data.where_equal(entry_name.session, entry_name.theory, entry_name.name)), |
67 |
{ res => |
|
78210 | 68 |
val executable = res.bool(Base.executable) |
69 |
val compressed = res.bool(Base.compressed) |
|
70 |
val bytes = res.bytes(Base.body) |
|
78209 | 71 |
val body = Future.value(compressed, bytes) |
72 |
Entry(entry_name, executable, body, cache) |
|
73 |
} |
|
74 |
) |
|
75 |
||
76 |
def write_entry(db: SQL.Database, entry: Entry): Unit = { |
|
77 |
val (compressed, bs) = entry.body.join |
|
78 |
db.execute_statement(Base.table.insert(), body = { stmt => |
|
79 |
stmt.string(1) = entry.session_name |
|
80 |
stmt.string(2) = entry.theory_name |
|
81 |
stmt.string(3) = entry.name |
|
82 |
stmt.bool(4) = entry.executable |
|
83 |
stmt.bool(5) = compressed |
|
84 |
stmt.bytes(6) = bs |
|
85 |
}) |
|
86 |
} |
|
78210 | 87 |
|
88 |
def read_theory_names(db: SQL.Database, session_name: String): List[String] = |
|
89 |
db.execute_query_statement( |
|
90 |
Base.table.select(List(Base.theory_name), distinct = true, |
|
91 |
sql = Data.where_equal(session_name) + SQL.order_by(List(Base.theory_name))), |
|
92 |
List.from[String], res => res.string(Base.theory_name)) |
|
93 |
||
94 |
def read_entry_names(db: SQL.Database, session_name: String): List[Entry_Name] = |
|
95 |
db.execute_query_statement( |
|
96 |
Base.table.select(List(Base.theory_name, Base.name), |
|
97 |
sql = Data.where_equal(session_name)) + SQL.order_by(List(Base.theory_name, Base.name)), |
|
98 |
List.from[Entry_Name], |
|
99 |
{ res => |
|
100 |
Entry_Name( |
|
101 |
session = session_name, |
|
102 |
theory = res.string(Base.theory_name), |
|
103 |
name = res.string(Base.name)) |
|
104 |
}) |
|
68116 | 105 |
} |
106 |
||
75674 | 107 |
def compound_name(a: String, b: String): String = |
108 |
if (a.isEmpty) b else a + ":" + b |
|
109 |
||
75672 | 110 |
sealed case class Entry_Name(session: String = "", theory: String = "", name: String = "") { |
75674 | 111 |
val compound_name: String = Export.compound_name(theory, name) |
112 |
||
75680 | 113 |
def make_path(prune: Int = 0): Path = { |
75675 | 114 |
val elems = theory :: space_explode('/', name) |
115 |
if (elems.length < prune + 1) { |
|
116 |
error("Cannot prune path by " + prune + " element(s): " + Path.make(elems)) |
|
117 |
} |
|
75680 | 118 |
else Path.make(elems.drop(prune)) |
75675 | 119 |
} |
68222 | 120 |
} |
121 |
||
68104 | 122 |
def message(msg: String, theory_name: String, name: String): String = |
123 |
msg + " " + quote(name) + " for theory " + quote(theory_name) |
|
124 |
||
76851 | 125 |
object Entry { |
126 |
def apply( |
|
127 |
entry_name: Entry_Name, |
|
128 |
executable: Boolean, |
|
129 |
body: Future[(Boolean, Bytes)], |
|
130 |
cache: XML.Cache |
|
131 |
): Entry = new Entry(entry_name, executable, body, cache) |
|
132 |
||
133 |
def empty(theory_name: String, name: String): Entry = |
|
134 |
Entry(Entry_Name(theory = theory_name, name = name), |
|
135 |
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
|
136 |
|
76851 | 137 |
def make( |
138 |
session_name: String, |
|
139 |
args: Protocol.Export.Args, |
|
140 |
bytes: Bytes, |
|
141 |
cache: XML.Cache |
|
142 |
): Entry = { |
|
143 |
val body = |
|
144 |
if (args.compress) Future.fork(bytes.maybe_compress(cache = cache.compress)) |
|
145 |
else Future.value((false, bytes)) |
|
146 |
val entry_name = |
|
147 |
Entry_Name(session = session_name, theory = args.theory_name, name = args.name) |
|
148 |
Entry(entry_name, args.executable, body, cache) |
|
149 |
} |
|
150 |
} |
|
151 |
||
152 |
final class Entry private( |
|
153 |
val entry_name: Entry_Name, |
|
154 |
val executable: Boolean, |
|
78209 | 155 |
val body: Future[(Boolean, Bytes)], |
156 |
val cache: XML.Cache |
|
75393 | 157 |
) { |
75671 | 158 |
def session_name: String = entry_name.session |
159 |
def theory_name: String = entry_name.theory |
|
160 |
def name: String = entry_name.name |
|
69635 | 161 |
override def toString: String = name |
69630 | 162 |
|
76851 | 163 |
def is_finished: Boolean = body.is_finished |
164 |
def cancel(): Unit = body.cancel() |
|
165 |
||
75674 | 166 |
def compound_name: String = entry_name.compound_name |
71141 | 167 |
|
72691 | 168 |
def name_has_prefix(s: String): Boolean = name.startsWith(s) |
69634 | 169 |
val name_elems: List[String] = explode_name(name) |
170 |
||
171 |
def name_extends(elems: List[String]): Boolean = |
|
172 |
name_elems.startsWith(elems) && name_elems != elems |
|
173 |
||
76852 | 174 |
def bytes: Bytes = { |
175 |
val (compressed, bs) = body.join |
|
176 |
if (compressed) bs.uncompress(cache = cache.compress) else bs |
|
69629 | 177 |
} |
178 |
||
76852 | 179 |
def text: String = bytes.text |
180 |
||
181 |
def yxml: XML.Body = YXML.parse_body(UTF8.decode_permissive(bytes), cache = cache) |
|
68116 | 182 |
} |
183 |
||
75393 | 184 |
def make_regex(pattern: String): Regex = { |
68116 | 185 |
@tailrec def make(result: List[String], depth: Int, chs: List[Char]): Regex = |
186 |
chs match { |
|
187 |
case '*' :: '*' :: rest => make("[^:]*" :: result, depth, rest) |
|
188 |
case '*' :: rest => make("[^:/]*" :: result, depth, rest) |
|
189 |
case '?' :: rest => make("[^:/]" :: result, depth, rest) |
|
190 |
case '\\' :: c :: rest => make(("\\" + c) :: result, depth, rest) |
|
191 |
case '{' :: rest => make("(" :: result, depth + 1, rest) |
|
192 |
case ',' :: rest if depth > 0 => make("|" :: result, depth, rest) |
|
193 |
case '}' :: rest if depth > 0 => make(")" :: result, depth - 1, rest) |
|
194 |
case c :: rest if ".+()".contains(c) => make(("\\" + c) :: result, depth, rest) |
|
195 |
case c :: rest => make(c.toString :: result, depth, rest) |
|
196 |
case Nil => result.reverse.mkString.r |
|
197 |
} |
|
198 |
make(Nil, 0, pattern.toList) |
|
68092
888d35a19866
store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff
changeset
|
199 |
} |
888d35a19866
store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff
changeset
|
200 |
|
75674 | 201 |
def make_matcher(pats: List[String]): Entry_Name => Boolean = { |
75658
5905c7f484b3
clarified signature: read_theory_exports is already ordered;
wenzelm
parents:
75394
diff
changeset
|
202 |
val regs = pats.map(make_regex) |
76098 | 203 |
(entry_name: Entry_Name) => regs.exists(_.matches(entry_name.compound_name)) |
68151 | 204 |
} |
205 |
||
78260
0a7f7abbe4f0
more robust transaction_lock: avoid overlapping data spaces;
wenzelm
parents:
78211
diff
changeset
|
206 |
def clean_session(db: SQL.Database, session_name: String): Unit = |
78356 | 207 |
Data.transaction_lock(db, create = true, label = "Export.clean_session") { |
208 |
Data.clean_session(db, session_name) |
|
209 |
} |
|
78260
0a7f7abbe4f0
more robust transaction_lock: avoid overlapping data spaces;
wenzelm
parents:
78211
diff
changeset
|
210 |
|
78211 | 211 |
def read_theory_names(db: SQL.Database, session_name: String): List[String] = |
78356 | 212 |
Data.transaction_lock(db, label = "Export.read_theory_names") { |
213 |
Data.read_theory_names(db, session_name) |
|
214 |
} |
|
78211 | 215 |
|
216 |
def read_entry_names(db: SQL.Database, session_name: String): List[Entry_Name] = |
|
78356 | 217 |
Data.transaction_lock(db, label = "Export.read_entry_names") { |
218 |
Data.read_entry_names(db, session_name) |
|
219 |
} |
|
78211 | 220 |
|
221 |
def read_entry(db: SQL.Database, entry_name: Entry_Name, cache: XML.Cache): Option[Entry] = |
|
78356 | 222 |
Data.transaction_lock(db, label = "Export.read_entry") { |
223 |
Data.read_entry(db, entry_name, cache) |
|
224 |
} |
|
78211 | 225 |
|
68092
888d35a19866
store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff
changeset
|
226 |
|
888d35a19866
store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff
changeset
|
227 |
/* database consumer thread */ |
888d35a19866
store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff
changeset
|
228 |
|
74255 | 229 |
def consumer(db: SQL.Database, cache: XML.Cache, progress: Progress = new Progress): Consumer = |
230 |
new Consumer(db, cache, progress) |
|
68092
888d35a19866
store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff
changeset
|
231 |
|
75393 | 232 |
class Consumer private[Export](db: SQL.Database, cache: XML.Cache, progress: Progress) { |
68924 | 233 |
private val errors = Synchronized[List[String]](Nil) |
68092
888d35a19866
store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff
changeset
|
234 |
|
888d35a19866
store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff
changeset
|
235 |
private val consumer = |
71145
2f782d5f5d5a
improved performance of session exports via bulk transactions;
wenzelm
parents:
71141
diff
changeset
|
236 |
Consumer_Thread.fork_bulk[(Entry, Boolean)](name = "export")( |
76851 | 237 |
bulk = { case (entry, _) => entry.is_finished }, |
71145
2f782d5f5d5a
improved performance of session exports via bulk transactions;
wenzelm
parents:
71141
diff
changeset
|
238 |
consume = |
75394 | 239 |
{ (args: List[(Entry, Boolean)]) => |
71145
2f782d5f5d5a
improved performance of session exports via bulk transactions;
wenzelm
parents:
71141
diff
changeset
|
240 |
val results = |
78360 | 241 |
Data.transaction_lock(db, label = "Export.consumer(" + args.length + ")") { |
74256 | 242 |
for ((entry, strict) <- args) |
71145
2f782d5f5d5a
improved performance of session exports via bulk transactions;
wenzelm
parents:
71141
diff
changeset
|
243 |
yield { |
74257 | 244 |
if (progress.stopped) { |
76851 | 245 |
entry.cancel() |
74257 | 246 |
Exn.Res(()) |
247 |
} |
|
78209 | 248 |
else if (Data.readable_entry(db, entry.entry_name)) { |
71145
2f782d5f5d5a
improved performance of session exports via bulk transactions;
wenzelm
parents:
71141
diff
changeset
|
249 |
if (strict) { |
2f782d5f5d5a
improved performance of session exports via bulk transactions;
wenzelm
parents:
71141
diff
changeset
|
250 |
val msg = message("Duplicate export", entry.theory_name, entry.name) |
2f782d5f5d5a
improved performance of session exports via bulk transactions;
wenzelm
parents:
71141
diff
changeset
|
251 |
errors.change(msg :: _) |
2f782d5f5d5a
improved performance of session exports via bulk transactions;
wenzelm
parents:
71141
diff
changeset
|
252 |
} |
2f782d5f5d5a
improved performance of session exports via bulk transactions;
wenzelm
parents:
71141
diff
changeset
|
253 |
Exn.Res(()) |
2f782d5f5d5a
improved performance of session exports via bulk transactions;
wenzelm
parents:
71141
diff
changeset
|
254 |
} |
78209 | 255 |
else Exn.capture { Data.write_entry(db, entry) } |
71145
2f782d5f5d5a
improved performance of session exports via bulk transactions;
wenzelm
parents:
71141
diff
changeset
|
256 |
} |
70499 | 257 |
} |
71145
2f782d5f5d5a
improved performance of session exports via bulk transactions;
wenzelm
parents:
71141
diff
changeset
|
258 |
(results, true) |
2f782d5f5d5a
improved performance of session exports via bulk transactions;
wenzelm
parents:
71141
diff
changeset
|
259 |
}) |
68092
888d35a19866
store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff
changeset
|
260 |
|
77681
1db732e6c3d2
back to compression in Isabelle/Scala (in contrast to f7174238b5e3), e.g. relevant for old_command_timings_blob, but also for prospective heaps;
wenzelm
parents:
77599
diff
changeset
|
261 |
def make_entry(session_name: String, args: Protocol.Export.Args, body: Bytes): Unit = { |
75762
985c3a64748c
clarified signature: more uniform treatment of empty exports;
wenzelm
parents:
75759
diff
changeset
|
262 |
if (!progress.stopped && !body.is_empty) { |
76851 | 263 |
consumer.send(Entry.make(session_name, args, body, cache) -> args.strict) |
74257 | 264 |
} |
265 |
} |
|
68092
888d35a19866
store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff
changeset
|
266 |
|
75393 | 267 |
def shutdown(close: Boolean = false): List[String] = { |
68092
888d35a19866
store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff
changeset
|
268 |
consumer.shutdown() |
888d35a19866
store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff
changeset
|
269 |
if (close) db.close() |
74255 | 270 |
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
|
271 |
} |
888d35a19866
store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff
changeset
|
272 |
} |
68116 | 273 |
|
274 |
||
75786 | 275 |
/* context for database access */ |
75772 | 276 |
|
78379 | 277 |
def open_database_context(store: Store, server: SSH.Server = SSH.no_server): Database_Context = |
278 |
new Database_Context(store, store.maybe_open_database_server(server = server)) |
|
75786 | 279 |
|
78379 | 280 |
def open_session_context0( |
281 |
store: Store, |
|
282 |
session: String, |
|
283 |
server: SSH.Server = SSH.no_server |
|
284 |
): Session_Context = { |
|
285 |
open_database_context(store, server = server) |
|
286 |
.open_session0(session, close_database_context = true) |
|
287 |
} |
|
75772 | 288 |
|
75786 | 289 |
def open_session_context( |
78178 | 290 |
store: Store, |
76656 | 291 |
session_background: Sessions.Background, |
78379 | 292 |
document_snapshot: Option[Document.Snapshot] = None, |
293 |
server: SSH.Server = SSH.no_server |
|
75786 | 294 |
): Session_Context = { |
78379 | 295 |
open_database_context(store, server = server).open_session( |
76656 | 296 |
session_background, document_snapshot = document_snapshot, close_database_context = true) |
75786 | 297 |
} |
75771 | 298 |
|
75786 | 299 |
class Database_Context private[Export]( |
78178 | 300 |
val store: Store, |
75786 | 301 |
val database_server: Option[SQL.Database] |
302 |
) extends AutoCloseable { |
|
303 |
database_context => |
|
75782
dba571dd0ba9
clarified signature: prefer Export.Session_Context over Sessions.Database_Context;
wenzelm
parents:
75780
diff
changeset
|
304 |
|
75786 | 305 |
override def toString: String = { |
306 |
val s = |
|
307 |
database_server match { |
|
308 |
case Some(db) => db.toString |
|
309 |
case None => "input_dirs = " + store.input_dirs.map(_.absolute).mkString(", ") |
|
310 |
} |
|
311 |
"Database_Context(" + s + ")" |
|
312 |
} |
|
313 |
||
314 |
def cache: Term.Cache = store.cache |
|
75759
0cdccd0d1699
clarified context for retrieval: more explicit types, with optional close() operation;
wenzelm
parents:
75757
diff
changeset
|
315 |
|
75786 | 316 |
def close(): Unit = database_server.foreach(_.close()) |
317 |
||
75970 | 318 |
def open_database(session: String, output: Boolean = false): Session_Database = |
75786 | 319 |
database_server match { |
75787 | 320 |
case Some(db) => new Session_Database(session, db) |
321 |
case None => |
|
75970 | 322 |
new Session_Database(session, store.open_database(session, output = output)) { |
75787 | 323 |
override def close(): Unit = db.close() |
324 |
} |
|
75786 | 325 |
} |
326 |
||
327 |
def open_session0(session: String, close_database_context: Boolean = false): Session_Context = |
|
76656 | 328 |
open_session(Sessions.background0(session), close_database_context = close_database_context) |
75779
5470c67bd772
clarified signature: prefer Export.Session_Context;
wenzelm
parents:
75778
diff
changeset
|
329 |
|
75759
0cdccd0d1699
clarified context for retrieval: more explicit types, with optional close() operation;
wenzelm
parents:
75757
diff
changeset
|
330 |
def open_session( |
76656 | 331 |
session_background: Sessions.Background, |
75772 | 332 |
document_snapshot: Option[Document.Snapshot] = None, |
75786 | 333 |
close_database_context: Boolean = false |
75759
0cdccd0d1699
clarified context for retrieval: more explicit types, with optional close() operation;
wenzelm
parents:
75757
diff
changeset
|
334 |
): Session_Context = { |
76656 | 335 |
val session_name = session_background.check_errors.session_name |
336 |
val session_hierarchy = session_background.sessions_structure.build_hierarchy(session_name) |
|
75773
11b2bf6f90d8
clarified signature: less redundant -- Sessions.Base_Info already specifies the main session;
wenzelm
parents:
75772
diff
changeset
|
337 |
val session_databases = |
75786 | 338 |
database_server match { |
75771 | 339 |
case Some(db) => session_hierarchy.map(name => new Session_Database(name, db)) |
340 |
case None => |
|
75775
70a65ee4a738
clarified signature: more robust treatment of server;
wenzelm
parents:
75774
diff
changeset
|
341 |
val attempts = |
78367 | 342 |
for (name <- session_hierarchy) |
343 |
yield name -> store.try_open_database(name, server_mode = false) |
|
75771 | 344 |
attempts.collectFirst({ case (name, None) => name }) match { |
345 |
case Some(bad) => |
|
346 |
for ((_, Some(db)) <- attempts) db.close() |
|
75791 | 347 |
store.error_database(bad) |
75764
07e097f60b85
clarified signature: more robust close operation;
wenzelm
parents:
75762
diff
changeset
|
348 |
case None => |
75771 | 349 |
for ((name, Some(db)) <- attempts) yield { |
75764
07e097f60b85
clarified signature: more robust close operation;
wenzelm
parents:
75762
diff
changeset
|
350 |
new Session_Database(name, db) { override def close(): Unit = this.db.close() } |
07e097f60b85
clarified signature: more robust close operation;
wenzelm
parents:
75762
diff
changeset
|
351 |
} |
07e097f60b85
clarified signature: more robust close operation;
wenzelm
parents:
75762
diff
changeset
|
352 |
} |
75771 | 353 |
} |
76656 | 354 |
new Session_Context( |
355 |
database_context, session_background, session_databases, document_snapshot) { |
|
75772 | 356 |
override def close(): Unit = { |
357 |
session_databases.foreach(_.close()) |
|
75786 | 358 |
if (close_database_context) database_context.close() |
75772 | 359 |
} |
75759
0cdccd0d1699
clarified context for retrieval: more explicit types, with optional close() operation;
wenzelm
parents:
75757
diff
changeset
|
360 |
} |
0cdccd0d1699
clarified context for retrieval: more explicit types, with optional close() operation;
wenzelm
parents:
75757
diff
changeset
|
361 |
} |
0cdccd0d1699
clarified context for retrieval: more explicit types, with optional close() operation;
wenzelm
parents:
75757
diff
changeset
|
362 |
} |
0cdccd0d1699
clarified context for retrieval: more explicit types, with optional close() operation;
wenzelm
parents:
75757
diff
changeset
|
363 |
|
75789 | 364 |
class Session_Database private[Export](val session: String, val db: SQL.Database) |
365 |
extends AutoCloseable { |
|
366 |
def close(): Unit = () |
|
367 |
||
78211 | 368 |
lazy private [Export] val theory_names: List[String] = read_theory_names(db, session) |
369 |
lazy private [Export] val entry_names: List[Entry_Name] = read_entry_names(db, session) |
|
75789 | 370 |
} |
371 |
||
75759
0cdccd0d1699
clarified context for retrieval: more explicit types, with optional close() operation;
wenzelm
parents:
75757
diff
changeset
|
372 |
class Session_Context private[Export]( |
75786 | 373 |
val database_context: Database_Context, |
76656 | 374 |
session_background: Sessions.Background, |
75770
62e2c6f65f9a
clarified Document.Snapshot.all_exports: refer to material from this (virtual) session;
wenzelm
parents:
75769
diff
changeset
|
375 |
db_hierarchy: List[Session_Database], |
77005 | 376 |
val document_snapshot: Option[Document.Snapshot] |
75759
0cdccd0d1699
clarified context for retrieval: more explicit types, with optional close() operation;
wenzelm
parents:
75757
diff
changeset
|
377 |
) extends AutoCloseable { |
0cdccd0d1699
clarified context for retrieval: more explicit types, with optional close() operation;
wenzelm
parents:
75757
diff
changeset
|
378 |
session_context => |
0cdccd0d1699
clarified context for retrieval: more explicit types, with optional close() operation;
wenzelm
parents:
75757
diff
changeset
|
379 |
|
0cdccd0d1699
clarified context for retrieval: more explicit types, with optional close() operation;
wenzelm
parents:
75757
diff
changeset
|
380 |
def close(): Unit = () |
0cdccd0d1699
clarified context for retrieval: more explicit types, with optional close() operation;
wenzelm
parents:
75757
diff
changeset
|
381 |
|
75786 | 382 |
def cache: Term.Cache = database_context.cache |
75778
d18c96b9b955
prefer Export.Context/Session_Context/Theory_Context over Sessions.Database_Context;
wenzelm
parents:
75775
diff
changeset
|
383 |
|
76656 | 384 |
def sessions_structure: Sessions.Structure = session_background.sessions_structure |
75778
d18c96b9b955
prefer Export.Context/Session_Context/Theory_Context over Sessions.Database_Context;
wenzelm
parents:
75775
diff
changeset
|
385 |
|
76656 | 386 |
def session_base: Sessions.Base = session_background.base |
75778
d18c96b9b955
prefer Export.Context/Session_Context/Theory_Context over Sessions.Database_Context;
wenzelm
parents:
75775
diff
changeset
|
387 |
|
75770
62e2c6f65f9a
clarified Document.Snapshot.all_exports: refer to material from this (virtual) session;
wenzelm
parents:
75769
diff
changeset
|
388 |
def session_name: String = |
62e2c6f65f9a
clarified Document.Snapshot.all_exports: refer to material from this (virtual) session;
wenzelm
parents:
75769
diff
changeset
|
389 |
if (document_snapshot.isDefined) Sessions.DRAFT |
75773
11b2bf6f90d8
clarified signature: less redundant -- Sessions.Base_Info already specifies the main session;
wenzelm
parents:
75772
diff
changeset
|
390 |
else session_base.session_name |
75770
62e2c6f65f9a
clarified Document.Snapshot.all_exports: refer to material from this (virtual) session;
wenzelm
parents:
75769
diff
changeset
|
391 |
|
75780
f49c4f160b84
clarified signature: find session_database within Session_Context.db_hierarchy;
wenzelm
parents:
75779
diff
changeset
|
392 |
def session_database(session: String = session_name): Option[Session_Database] = |
f49c4f160b84
clarified signature: find session_database within Session_Context.db_hierarchy;
wenzelm
parents:
75779
diff
changeset
|
393 |
db_hierarchy.find(_.session == session) |
f49c4f160b84
clarified signature: find session_database within Session_Context.db_hierarchy;
wenzelm
parents:
75779
diff
changeset
|
394 |
|
f49c4f160b84
clarified signature: find session_database within Session_Context.db_hierarchy;
wenzelm
parents:
75779
diff
changeset
|
395 |
def session_db(session: String = session_name): Option[SQL.Database] = |
f49c4f160b84
clarified signature: find session_database within Session_Context.db_hierarchy;
wenzelm
parents:
75779
diff
changeset
|
396 |
session_database(session = session).map(_.db) |
f49c4f160b84
clarified signature: find session_database within Session_Context.db_hierarchy;
wenzelm
parents:
75779
diff
changeset
|
397 |
|
75770
62e2c6f65f9a
clarified Document.Snapshot.all_exports: refer to material from this (virtual) session;
wenzelm
parents:
75769
diff
changeset
|
398 |
def session_stack: List[String] = |
62e2c6f65f9a
clarified Document.Snapshot.all_exports: refer to material from this (virtual) session;
wenzelm
parents:
75769
diff
changeset
|
399 |
((if (document_snapshot.isDefined) List(session_name) else Nil) ::: |
62e2c6f65f9a
clarified Document.Snapshot.all_exports: refer to material from this (virtual) session;
wenzelm
parents:
75769
diff
changeset
|
400 |
db_hierarchy.map(_.session)).reverse |
62e2c6f65f9a
clarified Document.Snapshot.all_exports: refer to material from this (virtual) session;
wenzelm
parents:
75769
diff
changeset
|
401 |
|
62e2c6f65f9a
clarified Document.Snapshot.all_exports: refer to material from this (virtual) session;
wenzelm
parents:
75769
diff
changeset
|
402 |
private def select[A]( |
62e2c6f65f9a
clarified Document.Snapshot.all_exports: refer to material from this (virtual) session;
wenzelm
parents:
75769
diff
changeset
|
403 |
session: String, |
75860
2b2c09f4e7b5
proper export theory_names: theory/parents are not necessarily present (amending 4d27b520622a);
wenzelm
parents:
75825
diff
changeset
|
404 |
select: Session_Database => List[A], |
2b2c09f4e7b5
proper export theory_names: theory/parents are not necessarily present (amending 4d27b520622a);
wenzelm
parents:
75825
diff
changeset
|
405 |
project: Entry_Name => A, |
2b2c09f4e7b5
proper export theory_names: theory/parents are not necessarily present (amending 4d27b520622a);
wenzelm
parents:
75825
diff
changeset
|
406 |
sort_key: A => String |
75770
62e2c6f65f9a
clarified Document.Snapshot.all_exports: refer to material from this (virtual) session;
wenzelm
parents:
75769
diff
changeset
|
407 |
): List[A] = { |
75860
2b2c09f4e7b5
proper export theory_names: theory/parents are not necessarily present (amending 4d27b520622a);
wenzelm
parents:
75825
diff
changeset
|
408 |
def result(name: String): List[A] = |
75770
62e2c6f65f9a
clarified Document.Snapshot.all_exports: refer to material from this (virtual) session;
wenzelm
parents:
75769
diff
changeset
|
409 |
if (name == Sessions.DRAFT) { |
62e2c6f65f9a
clarified Document.Snapshot.all_exports: refer to material from this (virtual) session;
wenzelm
parents:
75769
diff
changeset
|
410 |
(for { |
62e2c6f65f9a
clarified Document.Snapshot.all_exports: refer to material from this (virtual) session;
wenzelm
parents:
75769
diff
changeset
|
411 |
snapshot <- document_snapshot.iterator |
62e2c6f65f9a
clarified Document.Snapshot.all_exports: refer to material from this (virtual) session;
wenzelm
parents:
75769
diff
changeset
|
412 |
entry_name <- snapshot.all_exports.keysIterator |
75860
2b2c09f4e7b5
proper export theory_names: theory/parents are not necessarily present (amending 4d27b520622a);
wenzelm
parents:
75825
diff
changeset
|
413 |
} yield project(entry_name)).toSet.toList.sortBy(sort_key) |
75770
62e2c6f65f9a
clarified Document.Snapshot.all_exports: refer to material from this (virtual) session;
wenzelm
parents:
75769
diff
changeset
|
414 |
} |
75860
2b2c09f4e7b5
proper export theory_names: theory/parents are not necessarily present (amending 4d27b520622a);
wenzelm
parents:
75825
diff
changeset
|
415 |
else session_database(name).map(select).getOrElse(Nil) |
2b2c09f4e7b5
proper export theory_names: theory/parents are not necessarily present (amending 4d27b520622a);
wenzelm
parents:
75825
diff
changeset
|
416 |
|
2b2c09f4e7b5
proper export theory_names: theory/parents are not necessarily present (amending 4d27b520622a);
wenzelm
parents:
75825
diff
changeset
|
417 |
if (session.nonEmpty) result(session) else session_stack.flatMap(result) |
75770
62e2c6f65f9a
clarified Document.Snapshot.all_exports: refer to material from this (virtual) session;
wenzelm
parents:
75769
diff
changeset
|
418 |
} |
62e2c6f65f9a
clarified Document.Snapshot.all_exports: refer to material from this (virtual) session;
wenzelm
parents:
75769
diff
changeset
|
419 |
|
62e2c6f65f9a
clarified Document.Snapshot.all_exports: refer to material from this (virtual) session;
wenzelm
parents:
75769
diff
changeset
|
420 |
def entry_names(session: String = session_name): List[Entry_Name] = |
75860
2b2c09f4e7b5
proper export theory_names: theory/parents are not necessarily present (amending 4d27b520622a);
wenzelm
parents:
75825
diff
changeset
|
421 |
select(session, _.entry_names, identity, _.compound_name) |
75770
62e2c6f65f9a
clarified Document.Snapshot.all_exports: refer to material from this (virtual) session;
wenzelm
parents:
75769
diff
changeset
|
422 |
|
62e2c6f65f9a
clarified Document.Snapshot.all_exports: refer to material from this (virtual) session;
wenzelm
parents:
75769
diff
changeset
|
423 |
def theory_names(session: String = session_name): List[String] = |
75860
2b2c09f4e7b5
proper export theory_names: theory/parents are not necessarily present (amending 4d27b520622a);
wenzelm
parents:
75825
diff
changeset
|
424 |
select(session, _.theory_names, _.theory, identity) |
75770
62e2c6f65f9a
clarified Document.Snapshot.all_exports: refer to material from this (virtual) session;
wenzelm
parents:
75769
diff
changeset
|
425 |
|
75759
0cdccd0d1699
clarified context for retrieval: more explicit types, with optional close() operation;
wenzelm
parents:
75757
diff
changeset
|
426 |
def get(theory: String, name: String): Option[Entry] = |
75770
62e2c6f65f9a
clarified Document.Snapshot.all_exports: refer to material from this (virtual) session;
wenzelm
parents:
75769
diff
changeset
|
427 |
{ |
75784 | 428 |
def snapshot_entry: Option[Entry] = |
75770
62e2c6f65f9a
clarified Document.Snapshot.all_exports: refer to material from this (virtual) session;
wenzelm
parents:
75769
diff
changeset
|
429 |
for { |
62e2c6f65f9a
clarified Document.Snapshot.all_exports: refer to material from this (virtual) session;
wenzelm
parents:
75769
diff
changeset
|
430 |
snapshot <- document_snapshot |
62e2c6f65f9a
clarified Document.Snapshot.all_exports: refer to material from this (virtual) session;
wenzelm
parents:
75769
diff
changeset
|
431 |
entry_name = Entry_Name(session = Sessions.DRAFT, theory = theory, name = name) |
62e2c6f65f9a
clarified Document.Snapshot.all_exports: refer to material from this (virtual) session;
wenzelm
parents:
75769
diff
changeset
|
432 |
entry <- snapshot.all_exports.get(entry_name) |
62e2c6f65f9a
clarified Document.Snapshot.all_exports: refer to material from this (virtual) session;
wenzelm
parents:
75769
diff
changeset
|
433 |
} yield entry |
75784 | 434 |
def db_entry: Option[Entry] = |
78209 | 435 |
db_hierarchy.view.map { database => |
436 |
val entry_name = Export.Entry_Name(session = database.session, theory = theory, name = name) |
|
78211 | 437 |
read_entry(database.db, entry_name, cache) |
78209 | 438 |
}.collectFirst({ case Some(entry) => entry }) |
75759
0cdccd0d1699
clarified context for retrieval: more explicit types, with optional close() operation;
wenzelm
parents:
75757
diff
changeset
|
439 |
|
75770
62e2c6f65f9a
clarified Document.Snapshot.all_exports: refer to material from this (virtual) session;
wenzelm
parents:
75769
diff
changeset
|
440 |
snapshot_entry orElse db_entry |
62e2c6f65f9a
clarified Document.Snapshot.all_exports: refer to material from this (virtual) session;
wenzelm
parents:
75769
diff
changeset
|
441 |
} |
62e2c6f65f9a
clarified Document.Snapshot.all_exports: refer to material from this (virtual) session;
wenzelm
parents:
75769
diff
changeset
|
442 |
|
75759
0cdccd0d1699
clarified context for retrieval: more explicit types, with optional close() operation;
wenzelm
parents:
75757
diff
changeset
|
443 |
def apply(theory: String, name: String, permissive: Boolean = false): Entry = |
0cdccd0d1699
clarified context for retrieval: more explicit types, with optional close() operation;
wenzelm
parents:
75757
diff
changeset
|
444 |
get(theory, name) match { |
76851 | 445 |
case None if permissive => Entry.empty(theory, name) |
75759
0cdccd0d1699
clarified context for retrieval: more explicit types, with optional close() operation;
wenzelm
parents:
75757
diff
changeset
|
446 |
case None => error("Missing export entry " + quote(compound_name(theory, name))) |
0cdccd0d1699
clarified context for retrieval: more explicit types, with optional close() operation;
wenzelm
parents:
75757
diff
changeset
|
447 |
case Some(entry) => entry |
0cdccd0d1699
clarified context for retrieval: more explicit types, with optional close() operation;
wenzelm
parents:
75757
diff
changeset
|
448 |
} |
0cdccd0d1699
clarified context for retrieval: more explicit types, with optional close() operation;
wenzelm
parents:
75757
diff
changeset
|
449 |
|
75790
0ab8a9177e41
clarified signature: more uniform treatment of cache for Export.read_session vs. Export.read_theory;
wenzelm
parents:
75789
diff
changeset
|
450 |
def theory(theory: String, other_cache: Option[Term.Cache] = None): Theory_Context = |
0ab8a9177e41
clarified signature: more uniform treatment of cache for Export.read_session vs. Export.read_theory;
wenzelm
parents:
75789
diff
changeset
|
451 |
new Theory_Context(session_context, theory, other_cache) |
75759
0cdccd0d1699
clarified context for retrieval: more explicit types, with optional close() operation;
wenzelm
parents:
75757
diff
changeset
|
452 |
|
78178 | 453 |
def get_source_file(name: String): Option[Store.Source_File] = { |
76909 | 454 |
val store = database_context.store |
455 |
(for { |
|
456 |
database <- db_hierarchy.iterator |
|
457 |
file <- store.read_sources(database.db, database.session, name = name).iterator |
|
458 |
} yield file).nextOption() |
|
459 |
} |
|
460 |
||
78178 | 461 |
def source_file(name: String): Store.Source_File = |
76910 | 462 |
get_source_file(name).getOrElse(error("Missing session source file " + quote(name))) |
463 |
||
77023
474a07221c27
more robust theory_source -- in contrast to node_source from fffb978dd683: theory name is more reliable than Document.Node.Name, explicit unicode_symbols;
wenzelm
parents:
77005
diff
changeset
|
464 |
def theory_source(theory: String, unicode_symbols: Boolean = false): String = { |
474a07221c27
more robust theory_source -- in contrast to node_source from fffb978dd683: theory name is more reliable than Document.Node.Name, explicit unicode_symbols;
wenzelm
parents:
77005
diff
changeset
|
465 |
def snapshot_source: Option[String] = |
474a07221c27
more robust theory_source -- in contrast to node_source from fffb978dd683: theory name is more reliable than Document.Node.Name, explicit unicode_symbols;
wenzelm
parents:
77005
diff
changeset
|
466 |
for { |
474a07221c27
more robust theory_source -- in contrast to node_source from fffb978dd683: theory name is more reliable than Document.Node.Name, explicit unicode_symbols;
wenzelm
parents:
77005
diff
changeset
|
467 |
snapshot <- document_snapshot |
474a07221c27
more robust theory_source -- in contrast to node_source from fffb978dd683: theory name is more reliable than Document.Node.Name, explicit unicode_symbols;
wenzelm
parents:
77005
diff
changeset
|
468 |
text <- snapshot.version.nodes.iterator.collectFirst( |
474a07221c27
more robust theory_source -- in contrast to node_source from fffb978dd683: theory name is more reliable than Document.Node.Name, explicit unicode_symbols;
wenzelm
parents:
77005
diff
changeset
|
469 |
{ case (name, node) if name.theory == theory => node.source }) |
474a07221c27
more robust theory_source -- in contrast to node_source from fffb978dd683: theory name is more reliable than Document.Node.Name, explicit unicode_symbols;
wenzelm
parents:
77005
diff
changeset
|
470 |
if text.nonEmpty |
474a07221c27
more robust theory_source -- in contrast to node_source from fffb978dd683: theory name is more reliable than Document.Node.Name, explicit unicode_symbols;
wenzelm
parents:
77005
diff
changeset
|
471 |
} yield Symbol.output(unicode_symbols, text) |
474a07221c27
more robust theory_source -- in contrast to node_source from fffb978dd683: theory name is more reliable than Document.Node.Name, explicit unicode_symbols;
wenzelm
parents:
77005
diff
changeset
|
472 |
|
474a07221c27
more robust theory_source -- in contrast to node_source from fffb978dd683: theory name is more reliable than Document.Node.Name, explicit unicode_symbols;
wenzelm
parents:
77005
diff
changeset
|
473 |
def db_source: Option[String] = { |
474a07221c27
more robust theory_source -- in contrast to node_source from fffb978dd683: theory name is more reliable than Document.Node.Name, explicit unicode_symbols;
wenzelm
parents:
77005
diff
changeset
|
474 |
val theory_context = session_context.theory(theory) |
474a07221c27
more robust theory_source -- in contrast to node_source from fffb978dd683: theory name is more reliable than Document.Node.Name, explicit unicode_symbols;
wenzelm
parents:
77005
diff
changeset
|
475 |
for { |
474a07221c27
more robust theory_source -- in contrast to node_source from fffb978dd683: theory name is more reliable than Document.Node.Name, explicit unicode_symbols;
wenzelm
parents:
77005
diff
changeset
|
476 |
name <- theory_context.files0(permissive = true).headOption |
474a07221c27
more robust theory_source -- in contrast to node_source from fffb978dd683: theory name is more reliable than Document.Node.Name, explicit unicode_symbols;
wenzelm
parents:
77005
diff
changeset
|
477 |
file <- get_source_file(name) |
77168
547d140f0780
more uniform use of Symbol.output, even in situations where its Symbol.encode is usually redundant;
wenzelm
parents:
77026
diff
changeset
|
478 |
} yield Symbol.output(unicode_symbols, file.bytes.text) |
77023
474a07221c27
more robust theory_source -- in contrast to node_source from fffb978dd683: theory name is more reliable than Document.Node.Name, explicit unicode_symbols;
wenzelm
parents:
77005
diff
changeset
|
479 |
} |
474a07221c27
more robust theory_source -- in contrast to node_source from fffb978dd683: theory name is more reliable than Document.Node.Name, explicit unicode_symbols;
wenzelm
parents:
77005
diff
changeset
|
480 |
|
474a07221c27
more robust theory_source -- in contrast to node_source from fffb978dd683: theory name is more reliable than Document.Node.Name, explicit unicode_symbols;
wenzelm
parents:
77005
diff
changeset
|
481 |
snapshot_source orElse db_source getOrElse "" |
474a07221c27
more robust theory_source -- in contrast to node_source from fffb978dd683: theory name is more reliable than Document.Node.Name, explicit unicode_symbols;
wenzelm
parents:
77005
diff
changeset
|
482 |
} |
474a07221c27
more robust theory_source -- in contrast to node_source from fffb978dd683: theory name is more reliable than Document.Node.Name, explicit unicode_symbols;
wenzelm
parents:
77005
diff
changeset
|
483 |
|
75825 | 484 |
def classpath(): List[File.Content] = { |
75782
dba571dd0ba9
clarified signature: prefer Export.Session_Context over Sessions.Database_Context;
wenzelm
parents:
75780
diff
changeset
|
485 |
(for { |
dba571dd0ba9
clarified signature: prefer Export.Session_Context over Sessions.Database_Context;
wenzelm
parents:
75780
diff
changeset
|
486 |
session <- session_stack.iterator |
dba571dd0ba9
clarified signature: prefer Export.Session_Context over Sessions.Database_Context;
wenzelm
parents:
75780
diff
changeset
|
487 |
info <- sessions_structure.get(session).iterator |
dba571dd0ba9
clarified signature: prefer Export.Session_Context over Sessions.Database_Context;
wenzelm
parents:
75780
diff
changeset
|
488 |
if info.export_classpath.nonEmpty |
dba571dd0ba9
clarified signature: prefer Export.Session_Context over Sessions.Database_Context;
wenzelm
parents:
75780
diff
changeset
|
489 |
matcher = make_matcher(info.export_classpath) |
dba571dd0ba9
clarified signature: prefer Export.Session_Context over Sessions.Database_Context;
wenzelm
parents:
75780
diff
changeset
|
490 |
entry_name <- entry_names(session = session).iterator |
dba571dd0ba9
clarified signature: prefer Export.Session_Context over Sessions.Database_Context;
wenzelm
parents:
75780
diff
changeset
|
491 |
if matcher(entry_name) |
dba571dd0ba9
clarified signature: prefer Export.Session_Context over Sessions.Database_Context;
wenzelm
parents:
75780
diff
changeset
|
492 |
entry <- get(entry_name.theory, entry_name.name).iterator |
76852 | 493 |
} yield File.content(entry.entry_name.make_path(), entry.bytes)).toList |
75918 | 494 |
} |
75782
dba571dd0ba9
clarified signature: prefer Export.Session_Context over Sessions.Database_Context;
wenzelm
parents:
75780
diff
changeset
|
495 |
|
75918 | 496 |
override def toString: String = |
75770
62e2c6f65f9a
clarified Document.Snapshot.all_exports: refer to material from this (virtual) session;
wenzelm
parents:
75769
diff
changeset
|
497 |
"Export.Session_Context(" + commas_quote(session_stack) + ")" |
75759
0cdccd0d1699
clarified context for retrieval: more explicit types, with optional close() operation;
wenzelm
parents:
75757
diff
changeset
|
498 |
} |
0cdccd0d1699
clarified context for retrieval: more explicit types, with optional close() operation;
wenzelm
parents:
75757
diff
changeset
|
499 |
|
75774
efc25bf4b795
discontinued Export.Provider in favour of Export.Context and its derivatives;
wenzelm
parents:
75773
diff
changeset
|
500 |
class Theory_Context private[Export]( |
efc25bf4b795
discontinued Export.Provider in favour of Export.Context and its derivatives;
wenzelm
parents:
75773
diff
changeset
|
501 |
val session_context: Session_Context, |
75790
0ab8a9177e41
clarified signature: more uniform treatment of cache for Export.read_session vs. Export.read_theory;
wenzelm
parents:
75789
diff
changeset
|
502 |
val theory: String, |
0ab8a9177e41
clarified signature: more uniform treatment of cache for Export.read_session vs. Export.read_theory;
wenzelm
parents:
75789
diff
changeset
|
503 |
other_cache: Option[Term.Cache] |
75774
efc25bf4b795
discontinued Export.Provider in favour of Export.Context and its derivatives;
wenzelm
parents:
75773
diff
changeset
|
504 |
) { |
75790
0ab8a9177e41
clarified signature: more uniform treatment of cache for Export.read_session vs. Export.read_theory;
wenzelm
parents:
75789
diff
changeset
|
505 |
def cache: Term.Cache = other_cache getOrElse session_context.cache |
75778
d18c96b9b955
prefer Export.Context/Session_Context/Theory_Context over Sessions.Database_Context;
wenzelm
parents:
75775
diff
changeset
|
506 |
|
75759
0cdccd0d1699
clarified context for retrieval: more explicit types, with optional close() operation;
wenzelm
parents:
75757
diff
changeset
|
507 |
def get(name: String): Option[Entry] = session_context.get(theory, name) |
0cdccd0d1699
clarified context for retrieval: more explicit types, with optional close() operation;
wenzelm
parents:
75757
diff
changeset
|
508 |
def apply(name: String, permissive: Boolean = false): Entry = |
0cdccd0d1699
clarified context for retrieval: more explicit types, with optional close() operation;
wenzelm
parents:
75757
diff
changeset
|
509 |
session_context.apply(theory, name, permissive = permissive) |
0cdccd0d1699
clarified context for retrieval: more explicit types, with optional close() operation;
wenzelm
parents:
75757
diff
changeset
|
510 |
|
76854 | 511 |
def yxml(name: String): XML.Body = |
75774
efc25bf4b795
discontinued Export.Provider in favour of Export.Context and its derivatives;
wenzelm
parents:
75773
diff
changeset
|
512 |
get(name) match { |
76852 | 513 |
case Some(entry) => entry.yxml |
75774
efc25bf4b795
discontinued Export.Provider in favour of Export.Context and its derivatives;
wenzelm
parents:
75773
diff
changeset
|
514 |
case None => Nil |
efc25bf4b795
discontinued Export.Provider in favour of Export.Context and its derivatives;
wenzelm
parents:
75773
diff
changeset
|
515 |
} |
efc25bf4b795
discontinued Export.Provider in favour of Export.Context and its derivatives;
wenzelm
parents:
75773
diff
changeset
|
516 |
|
75778
d18c96b9b955
prefer Export.Context/Session_Context/Theory_Context over Sessions.Database_Context;
wenzelm
parents:
75775
diff
changeset
|
517 |
def document_id(): Option[Long] = |
d18c96b9b955
prefer Export.Context/Session_Context/Theory_Context over Sessions.Database_Context;
wenzelm
parents:
75775
diff
changeset
|
518 |
apply(DOCUMENT_ID, permissive = true).text match { |
d18c96b9b955
prefer Export.Context/Session_Context/Theory_Context over Sessions.Database_Context;
wenzelm
parents:
75775
diff
changeset
|
519 |
case Value.Long(id) => Some(id) |
d18c96b9b955
prefer Export.Context/Session_Context/Theory_Context over Sessions.Database_Context;
wenzelm
parents:
75775
diff
changeset
|
520 |
case _ => None |
d18c96b9b955
prefer Export.Context/Session_Context/Theory_Context over Sessions.Database_Context;
wenzelm
parents:
75775
diff
changeset
|
521 |
} |
d18c96b9b955
prefer Export.Context/Session_Context/Theory_Context over Sessions.Database_Context;
wenzelm
parents:
75775
diff
changeset
|
522 |
|
75901 | 523 |
def files0(permissive: Boolean = false): List[String] = |
524 |
split_lines(apply(FILES, permissive = permissive).text) |
|
525 |
||
526 |
def files(permissive: Boolean = false): Option[(String, List[String])] = |
|
527 |
files0(permissive = permissive) match { |
|
75778
d18c96b9b955
prefer Export.Context/Session_Context/Theory_Context over Sessions.Database_Context;
wenzelm
parents:
75775
diff
changeset
|
528 |
case Nil => None |
75901 | 529 |
case a :: bs => Some((a, bs)) |
75778
d18c96b9b955
prefer Export.Context/Session_Context/Theory_Context over Sessions.Database_Context;
wenzelm
parents:
75775
diff
changeset
|
530 |
} |
d18c96b9b955
prefer Export.Context/Session_Context/Theory_Context over Sessions.Database_Context;
wenzelm
parents:
75775
diff
changeset
|
531 |
|
75759
0cdccd0d1699
clarified context for retrieval: more explicit types, with optional close() operation;
wenzelm
parents:
75757
diff
changeset
|
532 |
override def toString: String = "Export.Theory_Context(" + quote(theory) + ")" |
0cdccd0d1699
clarified context for retrieval: more explicit types, with optional close() operation;
wenzelm
parents:
75757
diff
changeset
|
533 |
} |
0cdccd0d1699
clarified context for retrieval: more explicit types, with optional close() operation;
wenzelm
parents:
75757
diff
changeset
|
534 |
|
0cdccd0d1699
clarified context for retrieval: more explicit types, with optional close() operation;
wenzelm
parents:
75757
diff
changeset
|
535 |
|
68288 | 536 |
/* export to file-system */ |
537 |
||
538 |
def export_files( |
|
78178 | 539 |
store: Store, |
68288 | 540 |
session_name: String, |
541 |
export_dir: Path, |
|
71726
a5fda30edae2
clarified signature: more uniform treatment of stopped/interrupted state;
wenzelm
parents:
71624
diff
changeset
|
542 |
progress: Progress = new Progress, |
69671 | 543 |
export_prune: Int = 0, |
68288 | 544 |
export_list: Boolean = false, |
75393 | 545 |
export_patterns: List[String] = Nil |
546 |
): Unit = { |
|
75394 | 547 |
using(store.open_database(session_name)) { db => |
78211 | 548 |
val entry_names = read_entry_names(db, session_name) |
68288 | 549 |
|
75739
5b37466c1463
removed somewhat pointless transaction: db is meant to be finished (or updated monotonically);
wenzelm
parents:
75736
diff
changeset
|
550 |
// list |
5b37466c1463
removed somewhat pointless transaction: db is meant to be finished (or updated monotonically);
wenzelm
parents:
75736
diff
changeset
|
551 |
if (export_list) { |
5b37466c1463
removed somewhat pointless transaction: db is meant to be finished (or updated monotonically);
wenzelm
parents:
75736
diff
changeset
|
552 |
for (entry_name <- entry_names) progress.echo(entry_name.compound_name) |
5b37466c1463
removed somewhat pointless transaction: db is meant to be finished (or updated monotonically);
wenzelm
parents:
75736
diff
changeset
|
553 |
} |
68288 | 554 |
|
75739
5b37466c1463
removed somewhat pointless transaction: db is meant to be finished (or updated monotonically);
wenzelm
parents:
75736
diff
changeset
|
555 |
// export |
5b37466c1463
removed somewhat pointless transaction: db is meant to be finished (or updated monotonically);
wenzelm
parents:
75736
diff
changeset
|
556 |
if (export_patterns.nonEmpty) { |
5b37466c1463
removed somewhat pointless transaction: db is meant to be finished (or updated monotonically);
wenzelm
parents:
75736
diff
changeset
|
557 |
val matcher = make_matcher(export_patterns) |
5b37466c1463
removed somewhat pointless transaction: db is meant to be finished (or updated monotonically);
wenzelm
parents:
75736
diff
changeset
|
558 |
for { |
5b37466c1463
removed somewhat pointless transaction: db is meant to be finished (or updated monotonically);
wenzelm
parents:
75736
diff
changeset
|
559 |
entry_name <- entry_names if matcher(entry_name) |
78211 | 560 |
entry <- read_entry(db, entry_name, store.cache) |
75739
5b37466c1463
removed somewhat pointless transaction: db is meant to be finished (or updated monotonically);
wenzelm
parents:
75736
diff
changeset
|
561 |
} { |
5b37466c1463
removed somewhat pointless transaction: db is meant to be finished (or updated monotonically);
wenzelm
parents:
75736
diff
changeset
|
562 |
val path = export_dir + entry_name.make_path(prune = export_prune) |
5b37466c1463
removed somewhat pointless transaction: db is meant to be finished (or updated monotonically);
wenzelm
parents:
75736
diff
changeset
|
563 |
progress.echo("export " + path + (if (entry.executable) " (executable)" else "")) |
5b37466c1463
removed somewhat pointless transaction: db is meant to be finished (or updated monotonically);
wenzelm
parents:
75736
diff
changeset
|
564 |
Isabelle_System.make_directory(path.dir) |
76852 | 565 |
val bytes = entry.bytes |
75739
5b37466c1463
removed somewhat pointless transaction: db is meant to be finished (or updated monotonically);
wenzelm
parents:
75736
diff
changeset
|
566 |
if (!path.is_file || Bytes.read(path) != bytes) Bytes.write(path, bytes) |
78298
3b0f8f1010f2
clarified signature, with subtle change of semantics (amending 8b5a2e4b16d4);
wenzelm
parents:
78260
diff
changeset
|
567 |
File.set_executable(path, reset = !entry.executable) |
68288 | 568 |
} |
569 |
} |
|
75394 | 570 |
} |
68288 | 571 |
} |
572 |
||
573 |
||
68116 | 574 |
/* Isabelle tool wrapper */ |
575 |
||
71601 | 576 |
val default_export_dir: Path = Path.explode("export") |
68116 | 577 |
|
75394 | 578 |
val isabelle_tool = |
579 |
Isabelle_Tool("export", "retrieve theory exports", Scala_Project.here, |
|
580 |
{ args => |
|
581 |
/* arguments */ |
|
68116 | 582 |
|
75394 | 583 |
var export_dir = default_export_dir |
584 |
var dirs: List[Path] = Nil |
|
585 |
var export_list = false |
|
586 |
var no_build = false |
|
587 |
var options = Options.init() |
|
588 |
var export_prune = 0 |
|
589 |
var export_patterns: List[String] = Nil |
|
68116 | 590 |
|
75394 | 591 |
val getopts = Getopts(""" |
68116 | 592 |
Usage: isabelle export [OPTIONS] SESSION |
593 |
||
594 |
Options are: |
|
68314
2acbf8129d8b
clarified option -O: avoid conflict with build/dump option -D;
wenzelm
parents:
68305
diff
changeset
|
595 |
-O DIR output directory for exported files (default: """ + default_export_dir + """) |
68116 | 596 |
-d DIR include session directory |
597 |
-l list exports |
|
598 |
-n no build of session |
|
599 |
-o OPTION override Isabelle system OPTION (via NAME=VAL or NAME) |
|
69671 | 600 |
-p NUM prune path of exported files by NUM elements |
68116 | 601 |
-x PATTERN extract files matching pattern (e.g. "*:**" for all) |
602 |
||
603 |
List or export theory exports for SESSION: named blobs produced by |
|
68290 | 604 |
isabelle build. Option -l or -x is required; option -x may be repeated. |
68116 | 605 |
|
606 |
The PATTERN language resembles glob patterns in the shell, with ? and * |
|
607 |
(both excluding ":" and "/"), ** (excluding ":"), and [abc] or [^abc], |
|
608 |
and variants {pattern1,pattern2,pattern3}. |
|
609 |
""", |
|
75394 | 610 |
"O:" -> (arg => export_dir = Path.explode(arg)), |
611 |
"d:" -> (arg => dirs = dirs ::: List(Path.explode(arg))), |
|
612 |
"l" -> (_ => export_list = true), |
|
613 |
"n" -> (_ => no_build = true), |
|
614 |
"o:" -> (arg => options = options + arg), |
|
615 |
"p:" -> (arg => export_prune = Value.Int.parse(arg)), |
|
616 |
"x:" -> (arg => export_patterns ::= arg)) |
|
68116 | 617 |
|
75394 | 618 |
val more_args = getopts(args) |
619 |
val session_name = |
|
620 |
more_args match { |
|
621 |
case List(session_name) if export_list || export_patterns.nonEmpty => session_name |
|
622 |
case _ => getopts.usage() |
|
623 |
} |
|
68116 | 624 |
|
75394 | 625 |
val progress = new Console_Progress() |
68305 | 626 |
|
68116 | 627 |
|
75394 | 628 |
/* build */ |
68116 | 629 |
|
75394 | 630 |
if (!no_build) { |
631 |
val rc = |
|
632 |
progress.interrupt_handler { |
|
633 |
Build.build_logic(options, session_name, progress = progress, dirs = dirs) |
|
634 |
} |
|
635 |
if (rc != Process_Result.RC.ok) sys.exit(rc) |
|
68331 | 636 |
} |
68116 | 637 |
|
638 |
||
75394 | 639 |
/* export files */ |
68116 | 640 |
|
78178 | 641 |
val store = Store(options) |
75394 | 642 |
export_files(store, session_name, export_dir, progress = progress, export_prune = export_prune, |
643 |
export_list = export_list, export_patterns = export_patterns) |
|
644 |
}) |
|
68092
888d35a19866
store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff
changeset
|
645 |
} |