| author | wenzelm | 
| Tue, 10 Dec 2024 21:06:04 +0100 | |
| changeset 81572 | 693a95492008 | 
| parent 76362 | 1928405a409b | 
| permissions | -rw-r--r-- | 
| 76351 
2cee31cd92f0
generic support for XZ and Zstd compression in Isabelle/Scala;
 wenzelm parents: diff
changeset | 1 | /* Title: Pure/General/compress.scala | 
| 
2cee31cd92f0
generic support for XZ and Zstd compression in Isabelle/Scala;
 wenzelm parents: diff
changeset | 2 | Author: Makarius | 
| 
2cee31cd92f0
generic support for XZ and Zstd compression in Isabelle/Scala;
 wenzelm parents: diff
changeset | 3 | |
| 
2cee31cd92f0
generic support for XZ and Zstd compression in Isabelle/Scala;
 wenzelm parents: diff
changeset | 4 | Support for generic data compression. | 
| 
2cee31cd92f0
generic support for XZ and Zstd compression in Isabelle/Scala;
 wenzelm parents: diff
changeset | 5 | */ | 
| 
2cee31cd92f0
generic support for XZ and Zstd compression in Isabelle/Scala;
 wenzelm parents: diff
changeset | 6 | |
| 
2cee31cd92f0
generic support for XZ and Zstd compression in Isabelle/Scala;
 wenzelm parents: diff
changeset | 7 | package isabelle | 
| 
2cee31cd92f0
generic support for XZ and Zstd compression in Isabelle/Scala;
 wenzelm parents: diff
changeset | 8 | |
| 
2cee31cd92f0
generic support for XZ and Zstd compression in Isabelle/Scala;
 wenzelm parents: diff
changeset | 9 | |
| 76353 | 10 | import org.tukaani.xz | 
| 11 | import com.github.luben.zstd | |
| 76351 
2cee31cd92f0
generic support for XZ and Zstd compression in Isabelle/Scala;
 wenzelm parents: diff
changeset | 12 | |
| 
2cee31cd92f0
generic support for XZ and Zstd compression in Isabelle/Scala;
 wenzelm parents: diff
changeset | 13 | |
| 
2cee31cd92f0
generic support for XZ and Zstd compression in Isabelle/Scala;
 wenzelm parents: diff
changeset | 14 | object Compress {
 | 
| 
2cee31cd92f0
generic support for XZ and Zstd compression in Isabelle/Scala;
 wenzelm parents: diff
changeset | 15 | /* options */ | 
| 
2cee31cd92f0
generic support for XZ and Zstd compression in Isabelle/Scala;
 wenzelm parents: diff
changeset | 16 | |
| 
2cee31cd92f0
generic support for XZ and Zstd compression in Isabelle/Scala;
 wenzelm parents: diff
changeset | 17 |   object Options {
 | 
| 76362 
1928405a409b
prefer Zstd compression, notably for database exports;
 wenzelm parents: 
76353diff
changeset | 18 | def apply(): Options = Options_Zstd() | 
| 76351 
2cee31cd92f0
generic support for XZ and Zstd compression in Isabelle/Scala;
 wenzelm parents: diff
changeset | 19 | } | 
| 
2cee31cd92f0
generic support for XZ and Zstd compression in Isabelle/Scala;
 wenzelm parents: diff
changeset | 20 | sealed abstract class Options | 
| 
2cee31cd92f0
generic support for XZ and Zstd compression in Isabelle/Scala;
 wenzelm parents: diff
changeset | 21 |   case class Options_XZ(level: Int = 3) extends Options {
 | 
| 76353 | 22 |     def make: xz.LZMA2Options = {
 | 
| 23 | val opts = new xz.LZMA2Options | |
| 76351 
2cee31cd92f0
generic support for XZ and Zstd compression in Isabelle/Scala;
 wenzelm parents: diff
changeset | 24 | opts.setPreset(level) | 
| 
2cee31cd92f0
generic support for XZ and Zstd compression in Isabelle/Scala;
 wenzelm parents: diff
changeset | 25 | opts | 
| 
2cee31cd92f0
generic support for XZ and Zstd compression in Isabelle/Scala;
 wenzelm parents: diff
changeset | 26 | } | 
| 
2cee31cd92f0
generic support for XZ and Zstd compression in Isabelle/Scala;
 wenzelm parents: diff
changeset | 27 | } | 
| 
2cee31cd92f0
generic support for XZ and Zstd compression in Isabelle/Scala;
 wenzelm parents: diff
changeset | 28 | case class Options_Zstd(level: Int = 3) extends Options | 
| 
2cee31cd92f0
generic support for XZ and Zstd compression in Isabelle/Scala;
 wenzelm parents: diff
changeset | 29 | |
| 
2cee31cd92f0
generic support for XZ and Zstd compression in Isabelle/Scala;
 wenzelm parents: diff
changeset | 30 | |
| 
2cee31cd92f0
generic support for XZ and Zstd compression in Isabelle/Scala;
 wenzelm parents: diff
changeset | 31 | /* cache */ | 
| 
2cee31cd92f0
generic support for XZ and Zstd compression in Isabelle/Scala;
 wenzelm parents: diff
changeset | 32 | |
| 76353 | 33 | class Cache private(val for_xz: xz.ArrayCache, val for_zstd: zstd.BufferPool) | 
| 76351 
2cee31cd92f0
generic support for XZ and Zstd compression in Isabelle/Scala;
 wenzelm parents: diff
changeset | 34 | |
| 
2cee31cd92f0
generic support for XZ and Zstd compression in Isabelle/Scala;
 wenzelm parents: diff
changeset | 35 |   object Cache {
 | 
| 76353 | 36 |     def none: Cache = {
 | 
| 37 | Zstd.init() | |
| 38 | new Cache(xz.ArrayCache.getDummyCache(), zstd.NoPool.INSTANCE) | |
| 39 | } | |
| 76352 
f2b98eb6a7a9
prefer new instance, following "make" signature terminology;
 wenzelm parents: 
76351diff
changeset | 40 |     def make(): Cache = {
 | 
| 
f2b98eb6a7a9
prefer new instance, following "make" signature terminology;
 wenzelm parents: 
76351diff
changeset | 41 | Zstd.init() | 
| 76353 | 42 | val pool = Untyped.constructor(classOf[zstd.RecyclingBufferPool]).newInstance() | 
| 43 | new Cache(new xz.BasicArrayCache, pool) | |
| 76352 
f2b98eb6a7a9
prefer new instance, following "make" signature terminology;
 wenzelm parents: 
76351diff
changeset | 44 | } | 
| 76351 
2cee31cd92f0
generic support for XZ and Zstd compression in Isabelle/Scala;
 wenzelm parents: diff
changeset | 45 | } | 
| 
2cee31cd92f0
generic support for XZ and Zstd compression in Isabelle/Scala;
 wenzelm parents: diff
changeset | 46 | |
| 
2cee31cd92f0
generic support for XZ and Zstd compression in Isabelle/Scala;
 wenzelm parents: diff
changeset | 47 | |
| 
2cee31cd92f0
generic support for XZ and Zstd compression in Isabelle/Scala;
 wenzelm parents: diff
changeset | 48 | /* Scala functions in ML */ | 
| 
2cee31cd92f0
generic support for XZ and Zstd compression in Isabelle/Scala;
 wenzelm parents: diff
changeset | 49 | |
| 
2cee31cd92f0
generic support for XZ and Zstd compression in Isabelle/Scala;
 wenzelm parents: diff
changeset | 50 |   object XZ_Compress extends Scala.Fun_Bytes("XZ.compress") {
 | 
| 
2cee31cd92f0
generic support for XZ and Zstd compression in Isabelle/Scala;
 wenzelm parents: diff
changeset | 51 | val here = Scala_Project.here | 
| 
2cee31cd92f0
generic support for XZ and Zstd compression in Isabelle/Scala;
 wenzelm parents: diff
changeset | 52 | def apply(arg: Bytes): Bytes = arg.compress(Options_XZ()) | 
| 
2cee31cd92f0
generic support for XZ and Zstd compression in Isabelle/Scala;
 wenzelm parents: diff
changeset | 53 | } | 
| 
2cee31cd92f0
generic support for XZ and Zstd compression in Isabelle/Scala;
 wenzelm parents: diff
changeset | 54 | |
| 
2cee31cd92f0
generic support for XZ and Zstd compression in Isabelle/Scala;
 wenzelm parents: diff
changeset | 55 |   object XZ_Uncompress extends Scala.Fun_Bytes("XZ.uncompress") {
 | 
| 
2cee31cd92f0
generic support for XZ and Zstd compression in Isabelle/Scala;
 wenzelm parents: diff
changeset | 56 | val here = Scala_Project.here | 
| 
2cee31cd92f0
generic support for XZ and Zstd compression in Isabelle/Scala;
 wenzelm parents: diff
changeset | 57 | def apply(arg: Bytes): Bytes = arg.uncompress_xz() | 
| 
2cee31cd92f0
generic support for XZ and Zstd compression in Isabelle/Scala;
 wenzelm parents: diff
changeset | 58 | } | 
| 
2cee31cd92f0
generic support for XZ and Zstd compression in Isabelle/Scala;
 wenzelm parents: diff
changeset | 59 | |
| 
2cee31cd92f0
generic support for XZ and Zstd compression in Isabelle/Scala;
 wenzelm parents: diff
changeset | 60 |   object Zstd_Compress extends Scala.Fun_Bytes("Zstd.compress") {
 | 
| 
2cee31cd92f0
generic support for XZ and Zstd compression in Isabelle/Scala;
 wenzelm parents: diff
changeset | 61 | val here = Scala_Project.here | 
| 
2cee31cd92f0
generic support for XZ and Zstd compression in Isabelle/Scala;
 wenzelm parents: diff
changeset | 62 | def apply(arg: Bytes): Bytes = arg.compress(Options_Zstd()) | 
| 
2cee31cd92f0
generic support for XZ and Zstd compression in Isabelle/Scala;
 wenzelm parents: diff
changeset | 63 | } | 
| 
2cee31cd92f0
generic support for XZ and Zstd compression in Isabelle/Scala;
 wenzelm parents: diff
changeset | 64 | |
| 
2cee31cd92f0
generic support for XZ and Zstd compression in Isabelle/Scala;
 wenzelm parents: diff
changeset | 65 |   object Zstd_Uncompress extends Scala.Fun_Bytes("Zstd.uncompress") {
 | 
| 
2cee31cd92f0
generic support for XZ and Zstd compression in Isabelle/Scala;
 wenzelm parents: diff
changeset | 66 | val here = Scala_Project.here | 
| 
2cee31cd92f0
generic support for XZ and Zstd compression in Isabelle/Scala;
 wenzelm parents: diff
changeset | 67 | def apply(arg: Bytes): Bytes = arg.uncompress_zstd() | 
| 
2cee31cd92f0
generic support for XZ and Zstd compression in Isabelle/Scala;
 wenzelm parents: diff
changeset | 68 | } | 
| 
2cee31cd92f0
generic support for XZ and Zstd compression in Isabelle/Scala;
 wenzelm parents: diff
changeset | 69 | } |