author | wenzelm |
Sat, 19 May 2018 20:05:13 +0200 | |
changeset 68221 | dbef88c2b6c5 |
parent 68210 | 65f79c0ddb0d |
child 68222 | 3c1a716e7f59 |
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 |
{ |
888d35a19866
store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff
changeset
|
16 |
/* SQL data model */ |
888d35a19866
store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff
changeset
|
17 |
|
888d35a19866
store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff
changeset
|
18 |
object Data |
888d35a19866
store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff
changeset
|
19 |
{ |
888d35a19866
store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff
changeset
|
20 |
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
|
21 |
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
|
22 |
val name = SQL.Column.string("name").make_primary_key |
888d35a19866
store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff
changeset
|
23 |
val compressed = SQL.Column.bool("compressed") |
888d35a19866
store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff
changeset
|
24 |
val body = SQL.Column.bytes("body") |
888d35a19866
store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff
changeset
|
25 |
|
888d35a19866
store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff
changeset
|
26 |
val table = |
888d35a19866
store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff
changeset
|
27 |
SQL.Table("isabelle_exports", List(session_name, theory_name, name, compressed, body)) |
888d35a19866
store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff
changeset
|
28 |
|
68116 | 29 |
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
|
30 |
"WHERE " + Data.session_name.equal(session_name) + |
68116 | 31 |
(if (theory_name == "") "" else " AND " + Data.theory_name.equal(theory_name)) + |
32 |
(if (name == "") "" else " AND " + Data.name.equal(name)) |
|
33 |
} |
|
34 |
||
35 |
def read_name(db: SQL.Database, session_name: String, theory_name: String, name: String): Boolean = |
|
36 |
{ |
|
37 |
val select = |
|
38 |
Data.table.select(List(Data.name), Data.where_equal(session_name, theory_name, name)) |
|
39 |
db.using_statement(select)(stmt => stmt.execute_query().next()) |
|
68092
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 |
|
888d35a19866
store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff
changeset
|
42 |
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
|
43 |
{ |
888d35a19866
store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff
changeset
|
44 |
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
|
45 |
db.using_statement(select)(stmt => |
888d35a19866
store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff
changeset
|
46 |
stmt.execute_query().iterator(res => res.string(Data.name)).toList) |
888d35a19866
store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff
changeset
|
47 |
} |
888d35a19866
store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff
changeset
|
48 |
|
68116 | 49 |
def read_theory_names(db: SQL.Database, session_name: String): List[(String, String)] = |
68115 | 50 |
{ |
68116 | 51 |
val select = Data.table.select(List(Data.theory_name, Data.name), Data.where_equal(session_name)) |
52 |
db.using_statement(select)(stmt => |
|
53 |
stmt.execute_query().iterator(res => |
|
54 |
(res.string(Data.theory_name), res.string(Data.name))).toList) |
|
68115 | 55 |
} |
56 |
||
68104 | 57 |
def message(msg: String, theory_name: String, name: String): String = |
58 |
msg + " " + quote(name) + " for theory " + quote(theory_name) |
|
59 |
||
68116 | 60 |
def compound_name(a: String, b: String): String = a + ":" + b |
61 |
||
68092
888d35a19866
store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff
changeset
|
62 |
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
|
63 |
session_name: String, |
c5764b8b2a87
more robust (synchronous) management of Export.Entry: Future.fork happens inside the data structure;
wenzelm
parents:
68102
diff
changeset
|
64 |
theory_name: String, |
c5764b8b2a87
more robust (synchronous) management of Export.Entry: Future.fork happens inside the data structure;
wenzelm
parents:
68102
diff
changeset
|
65 |
name: String, |
68167 | 66 |
body: Future[(Boolean, Bytes)]) |
68092
888d35a19866
store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff
changeset
|
67 |
{ |
68116 | 68 |
override def toString: String = compound_name(theory_name, name) |
68092
888d35a19866
store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff
changeset
|
69 |
|
888d35a19866
store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff
changeset
|
70 |
def write(db: SQL.Database) |
888d35a19866
store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff
changeset
|
71 |
{ |
68167 | 72 |
val (compressed, bytes) = body.join |
68092
888d35a19866
store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff
changeset
|
73 |
db.using_statement(Data.table.insert())(stmt => |
888d35a19866
store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff
changeset
|
74 |
{ |
888d35a19866
store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff
changeset
|
75 |
stmt.string(1) = session_name |
888d35a19866
store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff
changeset
|
76 |
stmt.string(2) = theory_name |
888d35a19866
store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff
changeset
|
77 |
stmt.string(3) = name |
888d35a19866
store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff
changeset
|
78 |
stmt.bool(4) = compressed |
68103
c5764b8b2a87
more robust (synchronous) management of Export.Entry: Future.fork happens inside the data structure;
wenzelm
parents:
68102
diff
changeset
|
79 |
stmt.bytes(5) = bytes |
68092
888d35a19866
store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff
changeset
|
80 |
stmt.execute() |
888d35a19866
store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff
changeset
|
81 |
}) |
888d35a19866
store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff
changeset
|
82 |
} |
68116 | 83 |
|
68167 | 84 |
def uncompressed(cache: XZ.Cache = XZ.cache()): Bytes = |
85 |
{ |
|
86 |
val (compressed, bytes) = body.join |
|
87 |
if (compressed) bytes.uncompress(cache = cache) else bytes |
|
88 |
} |
|
68171 | 89 |
|
90 |
def uncompressed_yxml(cache: XZ.Cache = XZ.cache()): XML.Body = |
|
91 |
YXML.parse_body(UTF8.decode_permissive(uncompressed(cache = cache))) |
|
68116 | 92 |
} |
93 |
||
94 |
def make_regex(pattern: String): Regex = |
|
95 |
{ |
|
96 |
@tailrec def make(result: List[String], depth: Int, chs: List[Char]): Regex = |
|
97 |
chs match { |
|
98 |
case '*' :: '*' :: rest => make("[^:]*" :: result, depth, rest) |
|
99 |
case '*' :: rest => make("[^:/]*" :: result, depth, rest) |
|
100 |
case '?' :: rest => make("[^:/]" :: result, depth, rest) |
|
101 |
case '\\' :: c :: rest => make(("\\" + c) :: result, depth, rest) |
|
102 |
case '{' :: rest => make("(" :: result, depth + 1, rest) |
|
103 |
case ',' :: rest if depth > 0 => make("|" :: result, depth, rest) |
|
104 |
case '}' :: rest if depth > 0 => make(")" :: result, depth - 1, rest) |
|
105 |
case c :: rest if ".+()".contains(c) => make(("\\" + c) :: result, depth, rest) |
|
106 |
case c :: rest => make(c.toString :: result, depth, rest) |
|
107 |
case Nil => result.reverse.mkString.r |
|
108 |
} |
|
109 |
make(Nil, 0, pattern.toList) |
|
68092
888d35a19866
store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff
changeset
|
110 |
} |
888d35a19866
store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff
changeset
|
111 |
|
68151 | 112 |
def make_matcher(pattern: String): (String, String) => Boolean = |
113 |
{ |
|
114 |
val regex = make_regex(pattern) |
|
115 |
(theory_name: String, name: String) => |
|
116 |
regex.pattern.matcher(compound_name(theory_name, name)).matches |
|
117 |
} |
|
118 |
||
68166 | 119 |
def make_entry(session_name: String, args: Markup.Export.Args, body: Bytes, |
120 |
cache: XZ.Cache = XZ.cache()): Entry = |
|
68101 | 121 |
{ |
68167 | 122 |
Entry(session_name, args.theory_name, args.name, |
123 |
if (args.compress) Future.fork(body.maybe_compress(cache = cache)) |
|
124 |
else Future.value((false, body))) |
|
68101 | 125 |
} |
126 |
||
68202 | 127 |
def read_entry(db: SQL.Database, session_name: String, theory_name: String, name: String) |
128 |
: Option[Entry] = |
|
68092
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 |
val select = |
888d35a19866
store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff
changeset
|
131 |
Data.table.select(List(Data.compressed, Data.body), |
68116 | 132 |
Data.where_equal(session_name, theory_name, name)) |
68092
888d35a19866
store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff
changeset
|
133 |
db.using_statement(select)(stmt => |
888d35a19866
store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff
changeset
|
134 |
{ |
888d35a19866
store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff
changeset
|
135 |
val res = stmt.execute_query() |
888d35a19866
store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff
changeset
|
136 |
if (res.next()) { |
888d35a19866
store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff
changeset
|
137 |
val compressed = res.bool(Data.compressed) |
888d35a19866
store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff
changeset
|
138 |
val body = res.bytes(Data.body) |
68202 | 139 |
Some(Entry(session_name, theory_name, name, Future.value(compressed, body))) |
68092
888d35a19866
store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff
changeset
|
140 |
} |
68202 | 141 |
else None |
68092
888d35a19866
store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff
changeset
|
142 |
}) |
888d35a19866
store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff
changeset
|
143 |
} |
888d35a19866
store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff
changeset
|
144 |
|
888d35a19866
store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff
changeset
|
145 |
|
888d35a19866
store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff
changeset
|
146 |
/* database consumer thread */ |
888d35a19866
store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff
changeset
|
147 |
|
888d35a19866
store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff
changeset
|
148 |
def consumer(db: SQL.Database): Consumer = new Consumer(db) |
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 |
class Consumer private[Export](db: SQL.Database) |
888d35a19866
store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff
changeset
|
151 |
{ |
68166 | 152 |
val xz_cache = XZ.make_cache() |
153 |
||
68092
888d35a19866
store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff
changeset
|
154 |
private val export_errors = Synchronized[List[String]](Nil) |
888d35a19866
store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff
changeset
|
155 |
|
888d35a19866
store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff
changeset
|
156 |
private val consumer = |
68103
c5764b8b2a87
more robust (synchronous) management of Export.Entry: Future.fork happens inside the data structure;
wenzelm
parents:
68102
diff
changeset
|
157 |
Consumer_Thread.fork(name = "export")(consume = (entry: Entry) => |
68092
888d35a19866
store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff
changeset
|
158 |
{ |
68103
c5764b8b2a87
more robust (synchronous) management of Export.Entry: Future.fork happens inside the data structure;
wenzelm
parents:
68102
diff
changeset
|
159 |
entry.body.join |
68092
888d35a19866
store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff
changeset
|
160 |
db.transaction { |
68115 | 161 |
if (read_name(db, entry.session_name, entry.theory_name, entry.name)) { |
68104 | 162 |
val err = message("Duplicate export", entry.theory_name, entry.name) |
163 |
export_errors.change(errs => err :: errs) |
|
68092
888d35a19866
store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff
changeset
|
164 |
} |
888d35a19866
store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff
changeset
|
165 |
else entry.write(db) |
888d35a19866
store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff
changeset
|
166 |
} |
888d35a19866
store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff
changeset
|
167 |
true |
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 |
|
68103
c5764b8b2a87
more robust (synchronous) management of Export.Entry: Future.fork happens inside the data structure;
wenzelm
parents:
68102
diff
changeset
|
170 |
def apply(session_name: String, args: Markup.Export.Args, body: Bytes): Unit = |
68166 | 171 |
consumer.send(make_entry(session_name, args, body, cache = xz_cache)) |
68092
888d35a19866
store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff
changeset
|
172 |
|
888d35a19866
store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff
changeset
|
173 |
def shutdown(close: Boolean = false): List[String] = |
888d35a19866
store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff
changeset
|
174 |
{ |
888d35a19866
store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff
changeset
|
175 |
consumer.shutdown() |
888d35a19866
store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff
changeset
|
176 |
if (close) db.close() |
888d35a19866
store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff
changeset
|
177 |
export_errors.value.reverse |
888d35a19866
store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff
changeset
|
178 |
} |
888d35a19866
store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff
changeset
|
179 |
} |
68116 | 180 |
|
181 |
||
182 |
/* Isabelle tool wrapper */ |
|
183 |
||
184 |
val default_export_dir = Path.explode("export") |
|
185 |
||
186 |
val isabelle_tool = Isabelle_Tool("export", "retrieve theory exports", args => |
|
187 |
{ |
|
188 |
/* arguments */ |
|
189 |
||
190 |
var export_dir = default_export_dir |
|
191 |
var dirs: List[Path] = Nil |
|
192 |
var export_list = false |
|
193 |
var no_build = false |
|
194 |
var options = Options.init() |
|
195 |
var system_mode = false |
|
196 |
var export_pattern = "" |
|
197 |
||
198 |
val getopts = Getopts(""" |
|
199 |
Usage: isabelle export [OPTIONS] SESSION |
|
200 |
||
201 |
Options are: |
|
202 |
-D DIR target directory for exported files (default: """ + default_export_dir + """) |
|
203 |
-d DIR include session directory |
|
204 |
-l list exports |
|
205 |
-n no build of session |
|
206 |
-o OPTION override Isabelle system OPTION (via NAME=VAL or NAME) |
|
207 |
-s system build mode for session image |
|
208 |
-x PATTERN extract files matching pattern (e.g. "*:**" for all) |
|
209 |
||
210 |
List or export theory exports for SESSION: named blobs produced by |
|
211 |
isabelle build. Option -l or -x is required. |
|
212 |
||
213 |
The PATTERN language resembles glob patterns in the shell, with ? and * |
|
214 |
(both excluding ":" and "/"), ** (excluding ":"), and [abc] or [^abc], |
|
215 |
and variants {pattern1,pattern2,pattern3}. |
|
216 |
""", |
|
217 |
"D:" -> (arg => export_dir = Path.explode(arg)), |
|
218 |
"d:" -> (arg => dirs = dirs ::: List(Path.explode(arg))), |
|
219 |
"l" -> (_ => export_list = true), |
|
220 |
"n" -> (_ => no_build = true), |
|
221 |
"o:" -> (arg => options = options + arg), |
|
222 |
"s" -> (_ => system_mode = true), |
|
223 |
"x:" -> (arg => export_pattern = arg)) |
|
224 |
||
225 |
val more_args = getopts(args) |
|
226 |
val session_name = |
|
227 |
more_args match { |
|
228 |
case List(session_name) if export_list || export_pattern != "" => session_name |
|
229 |
case _ => getopts.usage() |
|
230 |
} |
|
231 |
||
232 |
||
233 |
/* build */ |
|
234 |
||
235 |
val progress = new Console_Progress() |
|
236 |
||
237 |
if (!no_build && |
|
238 |
!Build.build(options, no_build = true, dirs = dirs, system_mode = system_mode, |
|
239 |
sessions = List(session_name)).ok) |
|
240 |
{ |
|
241 |
progress.echo("Build started for Isabelle/" + session_name + " ...") |
|
242 |
progress.interrupt_handler { |
|
243 |
val res = |
|
244 |
Build.build(options, progress = progress, dirs = dirs, system_mode = system_mode, |
|
245 |
sessions = List(session_name)) |
|
246 |
if (!res.ok) sys.exit(res.rc) |
|
247 |
} |
|
248 |
} |
|
249 |
||
250 |
||
251 |
/* database */ |
|
252 |
||
68209 | 253 |
val store = Sessions.store(options, system_mode) |
68116 | 254 |
|
68210 | 255 |
using(store.open_database(session_name))(db => |
68116 | 256 |
{ |
257 |
db.transaction { |
|
258 |
val export_names = read_theory_names(db, session_name) |
|
259 |
||
260 |
// list |
|
261 |
if (export_list) { |
|
262 |
(for ((theory_name, name) <- export_names) yield compound_name(theory_name, name)). |
|
263 |
sorted.foreach(Output.writeln(_, stdout = true)) |
|
264 |
} |
|
265 |
||
266 |
// export |
|
267 |
if (export_pattern != "") { |
|
68166 | 268 |
val xz_cache = XZ.make_cache() |
269 |
||
68151 | 270 |
val matcher = make_matcher(export_pattern) |
68202 | 271 |
for { |
272 |
(theory_name, name) <- export_names if matcher(theory_name, name) |
|
273 |
entry <- read_entry(db, session_name, theory_name, name) |
|
274 |
} { |
|
68116 | 275 |
val path = export_dir + Path.basic(theory_name) + Path.explode(name) |
276 |
progress.echo("exporting " + path) |
|
277 |
Isabelle_System.mkdirs(path.dir) |
|
68167 | 278 |
Bytes.write(path, entry.uncompressed(cache = xz_cache)) |
68116 | 279 |
} |
280 |
} |
|
281 |
} |
|
282 |
}) |
|
283 |
}) |
|
68092
888d35a19866
store exports in session database, with asynchronous / parallel compression;
wenzelm
parents:
diff
changeset
|
284 |
} |