| 
									
										
										
										
											2023-05-09 19:47:26 +08:00
										 |  |  | use std::{cell::RefCell, io};
 | 
					
						
							| 
									
										
										
										
											2024-07-02 00:18:38 +08:00
										 |  |  | use zstd::bulk::Compressor;
 | 
					
						
							| 
									
										
										
										
											2021-03-29 15:59:14 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-09 19:47:26 +08:00
										 |  |  | // The library supports regular compression levels from 1 up to ZSTD_maxCLevel(),
 | 
					
						
							|  |  |  | // which is currently 22. Levels >= 20
 | 
					
						
							|  |  |  | // Default level is ZSTD_CLEVEL_DEFAULT==3.
 | 
					
						
							|  |  |  | // value 0 means default, which is controlled by ZSTD_CLEVEL_DEFAULT
 | 
					
						
							| 
									
										
										
										
											2021-03-29 15:59:14 +08:00
										 |  |  | thread_local! {
 | 
					
						
							| 
									
										
										
										
											2023-05-09 19:47:26 +08:00
										 |  |  |     static COMPRESSOR: RefCell<io::Result<Compressor<'static>>> = RefCell::new(Compressor::new(crate::config::COMPRESS_LEVEL));
 | 
					
						
							| 
									
										
										
										
											2021-03-29 15:59:14 +08:00
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-05-09 19:47:26 +08:00
										 |  |  | pub fn compress(data: &[u8]) -> Vec<u8> {
 | 
					
						
							| 
									
										
										
										
											2021-03-29 15:59:14 +08:00
										 |  |  |     let mut out = Vec::new();
 | 
					
						
							|  |  |  |     COMPRESSOR.with(|c| {
 | 
					
						
							|  |  |  |         if let Ok(mut c) = c.try_borrow_mut() {
 | 
					
						
							| 
									
										
										
										
											2023-05-09 19:47:26 +08:00
										 |  |  |             match &mut *c {
 | 
					
						
							|  |  |  |                 Ok(c) => match c.compress(data) {
 | 
					
						
							|  |  |  |                     Ok(res) => out = res,
 | 
					
						
							|  |  |  |                     Err(err) => {
 | 
					
						
							|  |  |  |                         crate::log::debug!("Failed to compress: {}", err);
 | 
					
						
							|  |  |  |                     }
 | 
					
						
							|  |  |  |                 },
 | 
					
						
							| 
									
										
										
										
											2021-03-29 15:59:14 +08:00
										 |  |  |                 Err(err) => {
 | 
					
						
							| 
									
										
										
										
											2023-05-09 19:47:26 +08:00
										 |  |  |                     crate::log::debug!("Failed to get compressor: {}", err);
 | 
					
						
							| 
									
										
										
										
											2021-03-29 15:59:14 +08:00
										 |  |  |                 }
 | 
					
						
							|  |  |  |             }
 | 
					
						
							|  |  |  |         }
 | 
					
						
							|  |  |  |     });
 | 
					
						
							|  |  |  |     out
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | pub fn decompress(data: &[u8]) -> Vec<u8> {
 | 
					
						
							| 
									
										
										
										
											2024-07-02 00:18:38 +08:00
										 |  |  |     zstd::decode_all(data).unwrap_or_default()
 | 
					
						
							| 
									
										
										
										
											2021-03-29 15:59:14 +08:00
										 |  |  | }
 |