@@ -13,8 +13,8 @@ use crate::docbuilder::Limits;
1313use crate :: error:: Result ;
1414use crate :: repositories:: RepositoryStatsUpdater ;
1515use crate :: storage:: {
16- RustdocJsonFormatVersion , get_file_list , rustdoc_archive_path , rustdoc_json_path ,
17- source_archive_path,
16+ CompressionAlgorithm , RustdocJsonFormatVersion , compress , get_file_list , rustdoc_archive_path ,
17+ rustdoc_json_path , source_archive_path,
1818} ;
1919use crate :: utils:: {
2020 CargoMetadata , ConfigName , copy_dir_all, get_config, parse_rustc_version, report_error,
@@ -45,6 +45,9 @@ const COMPONENTS: &[&str] = &["llvm-tools-preview", "rustc-dev", "rustfmt"];
4545const DUMMY_CRATE_NAME : & str = "empty-library" ;
4646const DUMMY_CRATE_VERSION : & str = "1.0.0" ;
4747
48+ pub const RUSTDOC_JSON_COMPRESSION_ALGORITHMS : & [ CompressionAlgorithm ] =
49+ & [ CompressionAlgorithm :: Zstd , CompressionAlgorithm :: Gzip ] ;
50+
4851/// read the format version from a rustdoc JSON file.
4952fn read_format_version_from_rustdoc_json (
5053 reader : impl std:: io:: Read ,
@@ -909,12 +912,25 @@ impl RustwideBuilder {
909912 . context ( "couldn't parse rustdoc json to find format version" ) ?
910913 } ;
911914
912- for format_version in [ format_version, RustdocJsonFormatVersion :: Latest ] {
913- let _span = info_span ! ( "store_json" , %format_version) . entered ( ) ;
914- let path = rustdoc_json_path ( name, version, target, format_version) ;
915+ for alg in RUSTDOC_JSON_COMPRESSION_ALGORITHMS {
916+ let compressed_json: Vec < u8 > = {
917+ let _span =
918+ info_span ! ( "compress_json" , file_size = json_filename. metadata( ) ?. len( ) , algorithm=%alg)
919+ . entered ( ) ;
920+
921+ compress ( BufReader :: new ( File :: open ( & json_filename) ?) , * alg) ?
922+ } ;
915923
916- self . storage . store_path ( & path, & json_filename) ?;
917- self . storage . set_public_access ( & path, true ) ?;
924+ for format_version in [ format_version, RustdocJsonFormatVersion :: Latest ] {
925+ let path = rustdoc_json_path ( name, version, target, format_version, Some ( * alg) ) ;
926+ let _span =
927+ info_span ! ( "store_json" , %format_version, algorithm=%alg, target_path=%path)
928+ . entered ( ) ;
929+
930+ self . storage
931+ . store_one_uncompressed ( & path, compressed_json. clone ( ) ) ?;
932+ self . storage . set_public_access ( & path, true ) ?;
933+ }
918934 }
919935
920936 Ok ( ( ) )
@@ -1279,7 +1295,7 @@ mod tests {
12791295 use super :: * ;
12801296 use crate :: db:: types:: Feature ;
12811297 use crate :: registry_api:: ReleaseData ;
1282- use crate :: storage:: CompressionAlgorithm ;
1298+ use crate :: storage:: { CompressionAlgorithm , compression } ;
12831299 use crate :: test:: { AxumRouterTestExt , TestEnvironment , wrapper} ;
12841300 use std:: { io, iter} ;
12851301 use test_case:: test_case;
@@ -1467,29 +1483,39 @@ mod tests {
14671483
14681484 // other targets too
14691485 for target in DEFAULT_TARGETS {
1470- // check if rustdoc json files exist for all targets
1471- let path = rustdoc_json_path (
1472- crate_,
1473- version,
1474- target,
1475- RustdocJsonFormatVersion :: Latest ,
1476- ) ;
1477- assert ! ( storage. exists( & path) ?) ;
1478- assert ! ( storage. get_public_access( & path) ?) ;
1479-
1480- let json_prefix = format ! ( "rustdoc-json/{crate_}/{version}/{target}/" ) ;
1481- let mut json_files: Vec < _ > = storage
1482- . list_prefix ( & json_prefix)
1483- . filter_map ( |res| res. ok ( ) )
1484- . map ( |f| f. strip_prefix ( & json_prefix) . unwrap ( ) . to_owned ( ) )
1485- . collect ( ) ;
1486- json_files. sort ( ) ;
1487- assert ! ( json_files[ 0 ] . starts_with( & format!( "empty-library_1.0.0_{target}_" ) ) ) ;
1488- assert ! ( json_files[ 0 ] . ends_with( ".json" ) ) ;
1489- assert_eq ! (
1490- json_files[ 1 ] ,
1491- format!( "empty-library_1.0.0_{target}_latest.json" )
1492- ) ;
1486+ for alg in RUSTDOC_JSON_COMPRESSION_ALGORITHMS {
1487+ // check if rustdoc json files exist for all targets
1488+ let path = rustdoc_json_path (
1489+ crate_,
1490+ version,
1491+ target,
1492+ RustdocJsonFormatVersion :: Latest ,
1493+ Some ( * alg) ,
1494+ ) ;
1495+ assert ! ( storage. exists( & path) ?) ;
1496+ assert ! ( storage. get_public_access( & path) ?) ;
1497+
1498+ let ext = compression:: file_extension_for ( * alg) ;
1499+
1500+ let json_prefix = format ! ( "rustdoc-json/{crate_}/{version}/{target}/" ) ;
1501+ let mut json_files: Vec < _ > = storage
1502+ . list_prefix ( & json_prefix)
1503+ . filter_map ( |res| res. ok ( ) )
1504+ . map ( |f| f. strip_prefix ( & json_prefix) . unwrap ( ) . to_owned ( ) )
1505+ . collect ( ) ;
1506+ json_files. retain ( |f| f. ends_with ( & format ! ( ".json.{ext}" ) ) ) ;
1507+ json_files. sort ( ) ;
1508+ dbg ! ( & json_files) ;
1509+ assert ! (
1510+ json_files[ 0 ] . starts_with( & format!( "empty-library_1.0.0_{target}_" ) )
1511+ ) ;
1512+
1513+ assert ! ( json_files[ 0 ] . ends_with( & format!( ".json.{ext}" ) ) ) ;
1514+ assert_eq ! (
1515+ json_files[ 1 ] ,
1516+ format!( "empty-library_1.0.0_{target}_latest.json.{ext}" )
1517+ ) ;
1518+ }
14931519
14941520 if target == & default_target {
14951521 continue ;
0 commit comments