|
| 1 | +use super::{Compressor, Encoding}; |
| 2 | +use arc_swap::ArcSwap; |
| 3 | +use std::collections::HashMap; |
| 4 | +use std::sync::{Arc, LazyLock}; |
| 5 | + |
| 6 | +/// A registry of compression implementations. |
| 7 | +#[derive(Default, Clone)] |
| 8 | +pub struct CompressionRegistry { |
| 9 | + codecs: HashMap<&'static str, Arc<dyn Compressor>>, |
| 10 | +} |
| 11 | + |
| 12 | +// The global registry using ArcSwap for lock-free reads to implement something |
| 13 | +// closer to the RCU pattern. |
| 14 | +static GLOBAL_REGISTRY: LazyLock<ArcSwap<CompressionRegistry>> = |
| 15 | + LazyLock::new(|| ArcSwap::from(Arc::new(CompressionRegistry::new()))); |
| 16 | + |
| 17 | +/// Get a codec from the global registry. |
| 18 | +/// This operation is extremely fast and lock-free. |
| 19 | +pub fn get_codec(name: &str) -> Option<Arc<dyn Compressor>> { |
| 20 | + GLOBAL_REGISTRY.load().get(name) |
| 21 | +} |
| 22 | + |
| 23 | +/// Add a new codec to the global registry using a copy-on-write strategy. |
| 24 | +pub fn add_codec(name: &'static str, codec: Arc<dyn Compressor>) { |
| 25 | + let current_registry = GLOBAL_REGISTRY.load(); |
| 26 | + let new_registry = current_registry.with_codec(name, codec); |
| 27 | + GLOBAL_REGISTRY.store(Arc::new(new_registry)); |
| 28 | +} |
| 29 | + |
| 30 | +impl CompressionRegistry { |
| 31 | + /// Creates a new compression registry with default codecs enabled by features. |
| 32 | + pub fn new() -> Self { |
| 33 | + let mut codecs: HashMap<&'static str, Arc<dyn Compressor>> = HashMap::new(); |
| 34 | + |
| 35 | + #[cfg(feature = "gzip")] |
| 36 | + { |
| 37 | + let gzip = Arc::new(super::gzip::Gzip::new()); |
| 38 | + codecs.insert(<super::gzip::Gzip as Encoding>::NAME, gzip); |
| 39 | + } |
| 40 | + |
| 41 | + #[cfg(feature = "deflate")] |
| 42 | + { |
| 43 | + let deflate = Arc::new(super::deflate::Deflate::new()); |
| 44 | + codecs.insert(<super::deflate::Deflate as Encoding>::NAME, deflate); |
| 45 | + } |
| 46 | + |
| 47 | + #[cfg(feature = "zstd")] |
| 48 | + { |
| 49 | + let zstd = Arc::new(super::zstd::Zstd::new()); |
| 50 | + codecs.insert(<super::zstd::Zstd as Encoding>::NAME, zstd); |
| 51 | + } |
| 52 | + |
| 53 | + Self { codecs } |
| 54 | + } |
| 55 | + |
| 56 | + /// Get a codec from this specific registry instance. |
| 57 | + pub fn get(&self, name: &str) -> Option<Arc<dyn Compressor>> { |
| 58 | + self.codecs.get(name).cloned() |
| 59 | + } |
| 60 | + |
| 61 | + /// Creates a new registry from an existing one, adding a new codec. |
| 62 | + pub fn with_codec(&self, name: &'static str, codec: Arc<dyn Compressor>) -> Self { |
| 63 | + // Clone the existing map of codecs |
| 64 | + let mut new_codecs = self.codecs.clone(); |
| 65 | + // Add the new one |
| 66 | + new_codecs.insert(name, codec); |
| 67 | + // Return a new CompressionRegistry instance |
| 68 | + Self { codecs: new_codecs } |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +#[cfg(test)] |
| 73 | +mod tests { |
| 74 | + use super::*; |
| 75 | + use crate::codec::compression::{Compressor, Encoding}; |
| 76 | + use bytes::{Buf, BufMut}; |
| 77 | + use std::io; |
| 78 | + |
| 79 | + #[derive(Debug, Clone, Copy)] |
| 80 | + struct MockCompression; |
| 81 | + |
| 82 | + impl Compressor for MockCompression { |
| 83 | + fn compress( |
| 84 | + &self, |
| 85 | + _source: &mut dyn Buf, |
| 86 | + _destination: &mut dyn BufMut, |
| 87 | + ) -> Result<(), io::Error> { |
| 88 | + Ok(()) |
| 89 | + } |
| 90 | + |
| 91 | + fn decompress( |
| 92 | + &self, |
| 93 | + _source: &mut dyn Buf, |
| 94 | + _destination: &mut dyn BufMut, |
| 95 | + ) -> Result<(), io::Error> { |
| 96 | + Ok(()) |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + impl Encoding for MockCompression { |
| 101 | + const NAME: &'static str = "mock"; |
| 102 | + } |
| 103 | + |
| 104 | + #[test] |
| 105 | + fn registry_get_with() { |
| 106 | + let registry = CompressionRegistry::new(); |
| 107 | + let registry = registry.with_codec("mock", Arc::new(MockCompression)); |
| 108 | + assert!(registry.get("mock").is_some()); |
| 109 | + } |
| 110 | + |
| 111 | + #[test] |
| 112 | + fn global_registry() { |
| 113 | + #[cfg(feature = "gzip")] |
| 114 | + assert!(get_codec("gzip").is_some()); |
| 115 | + #[cfg(feature = "deflate")] |
| 116 | + assert!(get_codec("deflate").is_some()); |
| 117 | + #[cfg(feature = "zstd")] |
| 118 | + assert!(get_codec("zstd").is_some()); |
| 119 | + |
| 120 | + add_codec("mock", Arc::new(MockCompression)); |
| 121 | + assert!(get_codec("mock").is_some()); |
| 122 | + } |
| 123 | +} |
0 commit comments