src/Pure/Thy/export.scala
author wenzelm
Wed Jan 16 17:55:26 2019 +0100 (10 months ago)
changeset 69671 2486792eaf61
parent 69635 95dc926fa39c
child 69756 1907222d974e
permissions -rw-r--r--
support pruning of export names;
     1 /*  Title:      Pure/Thy/export.scala
     2     Author:     Makarius
     3 
     4 Manage theory exports: compressed blobs.
     5 */
     6 
     7 package isabelle
     8 
     9 
    10 import scala.annotation.tailrec
    11 import scala.util.matching.Regex
    12 
    13 
    14 object Export
    15 {
    16   /* structured name */
    17 
    18   val sep_char: Char = '/'
    19   val sep: String = sep_char.toString
    20 
    21   def explode_name(s: String): List[String] = space_explode(sep_char, s)
    22   def implode_name(elems: Iterable[String]): String = elems.mkString(sep)
    23 
    24 
    25   /* SQL data model */
    26 
    27   object Data
    28   {
    29     val session_name = SQL.Column.string("session_name").make_primary_key
    30     val theory_name = SQL.Column.string("theory_name").make_primary_key
    31     val name = SQL.Column.string("name").make_primary_key
    32     val compressed = SQL.Column.bool("compressed")
    33     val body = SQL.Column.bytes("body")
    34 
    35     val table =
    36       SQL.Table("isabelle_exports", List(session_name, theory_name, name, compressed, body))
    37 
    38     def where_equal(session_name: String, theory_name: String = "", name: String = ""): SQL.Source =
    39       "WHERE " + Data.session_name.equal(session_name) +
    40         (if (theory_name == "") "" else " AND " + Data.theory_name.equal(theory_name)) +
    41         (if (name == "") "" else " AND " + Data.name.equal(name))
    42   }
    43 
    44   def read_name(db: SQL.Database, session_name: String, theory_name: String, name: String): Boolean =
    45   {
    46     val select =
    47       Data.table.select(List(Data.name), Data.where_equal(session_name, theory_name, name))
    48     db.using_statement(select)(stmt => stmt.execute_query().next())
    49   }
    50 
    51   def read_names(db: SQL.Database, session_name: String, theory_name: String): List[String] =
    52   {
    53     val select = Data.table.select(List(Data.name), Data.where_equal(session_name, theory_name))
    54     db.using_statement(select)(stmt =>
    55       stmt.execute_query().iterator(res => res.string(Data.name)).toList)
    56   }
    57 
    58   def read_theory_names(db: SQL.Database, session_name: String): List[String] =
    59   {
    60     val select =
    61       Data.table.select(List(Data.theory_name), Data.where_equal(session_name), distinct = true)
    62     db.using_statement(select)(stmt =>
    63       stmt.execute_query().iterator(_.string(Data.theory_name)).toList)
    64   }
    65 
    66   def read_theory_exports(db: SQL.Database, session_name: String): List[(String, String)] =
    67   {
    68     val select = Data.table.select(List(Data.theory_name, Data.name), Data.where_equal(session_name))
    69     db.using_statement(select)(stmt =>
    70       stmt.execute_query().iterator(res =>
    71         (res.string(Data.theory_name), res.string(Data.name))).toList)
    72   }
    73 
    74   def message(msg: String, theory_name: String, name: String): String =
    75     msg + " " + quote(name) + " for theory " + quote(theory_name)
    76 
    77   def compound_name(a: String, b: String): String = a + ":" + b
    78 
    79   sealed case class Entry(
    80     session_name: String,
    81     theory_name: String,
    82     name: String,
    83     body: Future[(Boolean, Bytes)])
    84   {
    85     override def toString: String = name
    86 
    87     val name_elems: List[String] = explode_name(name)
    88 
    89     def name_extends(elems: List[String]): Boolean =
    90       name_elems.startsWith(elems) && name_elems != elems
    91 
    92     def text: String = uncompressed().text
    93 
    94     def uncompressed(cache: XZ.Cache = XZ.cache()): Bytes =
    95     {
    96       val (compressed, bytes) = body.join
    97       if (compressed) bytes.uncompress(cache = cache) else bytes
    98     }
    99 
   100     def uncompressed_yxml(cache: XZ.Cache = XZ.cache()): XML.Body =
   101       YXML.parse_body(UTF8.decode_permissive(uncompressed(cache = cache)))
   102 
   103     def write(db: SQL.Database)
   104     {
   105       val (compressed, bytes) = body.join
   106       db.using_statement(Data.table.insert())(stmt =>
   107       {
   108         stmt.string(1) = session_name
   109         stmt.string(2) = theory_name
   110         stmt.string(3) = name
   111         stmt.bool(4) = compressed
   112         stmt.bytes(5) = bytes
   113         stmt.execute()
   114       })
   115     }
   116   }
   117 
   118   def make_regex(pattern: String): Regex =
   119   {
   120     @tailrec def make(result: List[String], depth: Int, chs: List[Char]): Regex =
   121       chs match {
   122         case '*' :: '*' :: rest => make("[^:]*" :: result, depth, rest)
   123         case '*' :: rest => make("[^:/]*" :: result, depth, rest)
   124         case '?' :: rest => make("[^:/]" :: result, depth, rest)
   125         case '\\' :: c :: rest => make(("\\" + c) :: result, depth, rest)
   126         case '{' :: rest => make("(" :: result, depth + 1, rest)
   127         case ',' :: rest if depth > 0 => make("|" :: result, depth, rest)
   128         case '}' :: rest if depth > 0 => make(")" :: result, depth - 1, rest)
   129         case c :: rest if ".+()".contains(c) => make(("\\" + c) :: result, depth, rest)
   130         case c :: rest => make(c.toString :: result, depth, rest)
   131         case Nil => result.reverse.mkString.r
   132       }
   133     make(Nil, 0, pattern.toList)
   134   }
   135 
   136   def make_matcher(pattern: String): (String, String) => Boolean =
   137   {
   138     val regex = make_regex(pattern)
   139     (theory_name: String, name: String) =>
   140       regex.pattern.matcher(compound_name(theory_name, name)).matches
   141   }
   142 
   143   def make_entry(session_name: String, args: Markup.Export.Args, body: Bytes,
   144     cache: XZ.Cache = XZ.cache()): Entry =
   145   {
   146     Entry(session_name, args.theory_name, args.name,
   147       if (args.compress) Future.fork(body.maybe_compress(cache = cache))
   148       else Future.value((false, body)))
   149   }
   150 
   151   def read_entry(db: SQL.Database, session_name: String, theory_name: String, name: String)
   152     : Option[Entry] =
   153   {
   154     val select =
   155       Data.table.select(List(Data.compressed, Data.body),
   156         Data.where_equal(session_name, theory_name, name))
   157     db.using_statement(select)(stmt =>
   158     {
   159       val res = stmt.execute_query()
   160       if (res.next()) {
   161         val compressed = res.bool(Data.compressed)
   162         val body = res.bytes(Data.body)
   163         Some(Entry(session_name, theory_name, name, Future.value(compressed, body)))
   164       }
   165       else None
   166     })
   167   }
   168 
   169   def read_entry(dir: Path, session_name: String, theory_name: String, name: String): Option[Entry] =
   170   {
   171     val path = dir + Path.basic(theory_name) + Path.explode(name)
   172     if (path.is_file) {
   173       val uncompressed = Bytes.read(path)
   174       Some(Entry(session_name, theory_name, name, Future.value((false, uncompressed))))
   175     }
   176     else None
   177   }
   178 
   179 
   180   /* database consumer thread */
   181 
   182   def consumer(db: SQL.Database, cache: XZ.Cache = XZ.cache()): Consumer = new Consumer(db, cache)
   183 
   184   class Consumer private[Export](db: SQL.Database, cache: XZ.Cache)
   185   {
   186     private val errors = Synchronized[List[String]](Nil)
   187 
   188     private val consumer =
   189       Consumer_Thread.fork(name = "export")(consume = (entry: Entry) =>
   190         {
   191           entry.body.join
   192           db.transaction {
   193             if (read_name(db, entry.session_name, entry.theory_name, entry.name)) {
   194               val msg = message("Duplicate export", entry.theory_name, entry.name)
   195               errors.change(msg :: _)
   196             }
   197             else entry.write(db)
   198           }
   199           true
   200         })
   201 
   202     def apply(session_name: String, args: Markup.Export.Args, body: Bytes): Unit =
   203       consumer.send(make_entry(session_name, args, body, cache = cache))
   204 
   205     def shutdown(close: Boolean = false): List[String] =
   206     {
   207       consumer.shutdown()
   208       if (close) db.close()
   209       errors.value.reverse
   210     }
   211   }
   212 
   213 
   214   /* abstract provider */
   215 
   216   object Provider
   217   {
   218     def database(db: SQL.Database, session_name: String, theory_name: String): Provider =
   219       new Provider {
   220         def apply(export_name: String): Option[Entry] =
   221           read_entry(db, session_name, theory_name, export_name)
   222 
   223         override def toString: String = db.toString
   224       }
   225 
   226     def snapshot(snapshot: Document.Snapshot): Provider =
   227       new Provider {
   228         def apply(export_name: String): Option[Entry] =
   229           snapshot.exports_map.get(export_name)
   230 
   231         override def toString: String = snapshot.toString
   232       }
   233 
   234     def directory(dir: Path, session_name: String, theory_name: String): Provider =
   235       new Provider {
   236         def apply(export_name: String): Option[Entry] =
   237           read_entry(dir, session_name, theory_name, export_name)
   238 
   239         override def toString: String = dir.toString
   240       }
   241   }
   242 
   243   trait Provider
   244   {
   245     def apply(export_name: String): Option[Entry]
   246 
   247     def uncompressed_yxml(export_name: String, cache: XZ.Cache = XZ.cache()): XML.Body =
   248       apply(export_name) match {
   249         case Some(entry) => entry.uncompressed_yxml(cache = cache)
   250         case None => Nil
   251       }
   252   }
   253 
   254 
   255   /* export to file-system */
   256 
   257   def export_files(
   258     store: Sessions.Store,
   259     session_name: String,
   260     export_dir: Path,
   261     progress: Progress = No_Progress,
   262     export_prune: Int = 0,
   263     export_list: Boolean = false,
   264     export_patterns: List[String] = Nil,
   265     export_prefix: String = "")
   266   {
   267     using(store.open_database(session_name))(db =>
   268     {
   269       db.transaction {
   270         val export_names = read_theory_exports(db, session_name)
   271 
   272         // list
   273         if (export_list) {
   274           (for ((theory_name, name) <- export_names) yield compound_name(theory_name, name)).
   275             sorted.foreach(progress.echo(_))
   276         }
   277 
   278         // export
   279         if (export_patterns.nonEmpty) {
   280           val exports =
   281             (for {
   282               export_pattern <- export_patterns.iterator
   283               matcher = make_matcher(export_pattern)
   284               (theory_name, name) <- export_names if matcher(theory_name, name)
   285             } yield (theory_name, name)).toSet
   286           for {
   287             (theory_name, group) <- exports.toList.groupBy(_._1).toList.sortBy(_._1)
   288             name <- group.map(_._2).sorted
   289             entry <- read_entry(db, session_name, theory_name, name)
   290           } {
   291             val elems = theory_name :: space_explode('/', name)
   292             val path =
   293               if (elems.length < export_prune + 1) {
   294                 error("Cannot prune path by " + export_prune + " element(s): " + Path.make(elems))
   295               }
   296               else export_dir + Path.make(elems.drop(export_prune))
   297 
   298             progress.echo(export_prefix + "export " + path)
   299             Isabelle_System.mkdirs(path.dir)
   300             Bytes.write(path, entry.uncompressed(cache = store.xz_cache))
   301           }
   302         }
   303       }
   304     })
   305   }
   306 
   307 
   308   /* Isabelle tool wrapper */
   309 
   310   val default_export_dir = Path.explode("export")
   311 
   312   val isabelle_tool = Isabelle_Tool("export", "retrieve theory exports", args =>
   313   {
   314     /* arguments */
   315 
   316     var export_dir = default_export_dir
   317     var dirs: List[Path] = Nil
   318     var export_list = false
   319     var no_build = false
   320     var options = Options.init()
   321     var export_prune = 0
   322     var system_mode = false
   323     var export_patterns: List[String] = Nil
   324 
   325     val getopts = Getopts("""
   326 Usage: isabelle export [OPTIONS] SESSION
   327 
   328   Options are:
   329     -O DIR       output directory for exported files (default: """ + default_export_dir + """)
   330     -d DIR       include session directory
   331     -l           list exports
   332     -n           no build of session
   333     -o OPTION    override Isabelle system OPTION (via NAME=VAL or NAME)
   334     -p NUM       prune path of exported files by NUM elements
   335     -s           system build mode for session image
   336     -x PATTERN   extract files matching pattern (e.g. "*:**" for all)
   337 
   338   List or export theory exports for SESSION: named blobs produced by
   339   isabelle build. Option -l or -x is required; option -x may be repeated.
   340 
   341   The PATTERN language resembles glob patterns in the shell, with ? and *
   342   (both excluding ":" and "/"), ** (excluding ":"), and [abc] or [^abc],
   343   and variants {pattern1,pattern2,pattern3}.
   344 """,
   345       "O:" -> (arg => export_dir = Path.explode(arg)),
   346       "d:" -> (arg => dirs = dirs ::: List(Path.explode(arg))),
   347       "l" -> (_ => export_list = true),
   348       "n" -> (_ => no_build = true),
   349       "o:" -> (arg => options = options + arg),
   350       "p:" -> (arg => export_prune = Value.Int.parse(arg)),
   351       "s" -> (_ => system_mode = true),
   352       "x:" -> (arg => export_patterns ::= arg))
   353 
   354     val more_args = getopts(args)
   355     val session_name =
   356       more_args match {
   357         case List(session_name) if export_list || export_patterns.nonEmpty => session_name
   358         case _ => getopts.usage()
   359       }
   360 
   361     val progress = new Console_Progress()
   362 
   363 
   364     /* build */
   365 
   366     if (!no_build) {
   367       val rc =
   368         progress.interrupt_handler {
   369           Build.build_logic(options, session_name, progress = progress,
   370             dirs = dirs, system_mode = system_mode)
   371         }
   372       if (rc != 0) sys.exit(rc)
   373     }
   374 
   375 
   376     /* export files */
   377 
   378     val store = Sessions.store(options, system_mode)
   379     export_files(store, session_name, export_dir, progress = progress, export_prune = export_prune,
   380       export_list = export_list, export_patterns = export_patterns)
   381   })
   382 }