project_note
Clone
https://git.tahinli.com/Tahinli/project_note.git
0 watching·0 forks·Rust·3.6 MiB·c49f5e7·Tahinli·1 month ago·2 commits
| 1 | use crate::sync::{Mutex, RwLock}; |
| 2 | use loro_common::ContainerID; |
| 3 | use rustc_hash::FxHashSet; |
| 4 | |
| 5 | pub use crate::container::richtext::config::{StyleConfig, StyleConfigMap}; |
| 6 | use crate::LoroDoc; |
| 7 | use std::sync::atomic::{AtomicBool, AtomicI64, AtomicUsize, Ordering}; |
| 8 | use std::sync::Arc; |
| 9 | |
| 10 | #[cfg(not(test))] |
| 11 | pub(crate) const DEFAULT_DECODED_VALUE_CACHE_LIMIT: usize = 2048; |
| 12 | // Keep eviction tests small without changing the production default. |
| 13 | #[cfg(test)] |
| 14 | pub(crate) const DEFAULT_DECODED_VALUE_CACHE_LIMIT: usize = 16; |
| 15 | |
| 16 | #[derive(Clone, Debug)] |
| 17 | pub struct Configure { |
| 18 | pub(crate) text_style_config: Arc<RwLock<StyleConfigMap>>, |
| 19 | record_timestamp: Arc<AtomicBool>, |
| 20 | pub(crate) merge_interval_in_s: Arc<AtomicI64>, |
| 21 | pub(crate) editable_detached_mode: Arc<AtomicBool>, |
| 22 | pub(crate) deleted_root_containers: Arc<Mutex<FxHashSet<ContainerID>>>, |
| 23 | pub(crate) hide_empty_root_containers: Arc<AtomicBool>, |
| 24 | decoded_value_cache_limit: Arc<AtomicUsize>, |
| 25 | } |
| 26 | |
| 27 | impl LoroDoc { |
| 28 | pub(crate) fn set_config(&self, config: &Configure) { |
| 29 | // Forks need both explicit styles and the fallback for unused keys. |
| 30 | // config_text_style intentionally only replaces the explicit map. |
| 31 | let text_style_config = config.text_style_config.read().clone(); |
| 32 | *self.config.text_style_config.write() = text_style_config; |
| 33 | self.set_record_timestamp(config.record_timestamp()); |
| 34 | self.set_change_merge_interval(config.merge_interval()); |
| 35 | self.set_detached_editing(config.detached_editing()); |
| 36 | self.set_decoded_value_cache_limit(config.decoded_value_cache_limit()); |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | impl Default for Configure { |
| 41 | fn default() -> Self { |
| 42 | Self { |
| 43 | text_style_config: Arc::new(RwLock::new(StyleConfigMap::default_rich_text_config())), |
| 44 | record_timestamp: Arc::new(AtomicBool::new(false)), |
| 45 | editable_detached_mode: Arc::new(AtomicBool::new(false)), |
| 46 | merge_interval_in_s: Arc::new(AtomicI64::new(1000)), |
| 47 | deleted_root_containers: Arc::new(Mutex::new(Default::default())), |
| 48 | hide_empty_root_containers: Arc::new(AtomicBool::new(false)), |
| 49 | decoded_value_cache_limit: Arc::new(AtomicUsize::new( |
| 50 | DEFAULT_DECODED_VALUE_CACHE_LIMIT, |
| 51 | )), |
| 52 | } |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | impl Configure { |
| 57 | pub fn fork(&self) -> Self { |
| 58 | Self { |
| 59 | text_style_config: Arc::new(RwLock::new(self.text_style_config.read().clone())), |
| 60 | record_timestamp: Arc::new(AtomicBool::new( |
| 61 | self.record_timestamp |
| 62 | .load(std::sync::atomic::Ordering::Relaxed), |
| 63 | )), |
| 64 | merge_interval_in_s: Arc::new(AtomicI64::new( |
| 65 | self.merge_interval_in_s |
| 66 | .load(std::sync::atomic::Ordering::Relaxed), |
| 67 | )), |
| 68 | editable_detached_mode: Arc::new(AtomicBool::new( |
| 69 | self.editable_detached_mode |
| 70 | .load(std::sync::atomic::Ordering::Relaxed), |
| 71 | )), |
| 72 | deleted_root_containers: Arc::new(Mutex::new( |
| 73 | self.deleted_root_containers.lock().clone(), |
| 74 | )), |
| 75 | hide_empty_root_containers: Arc::new(AtomicBool::new( |
| 76 | self.hide_empty_root_containers |
| 77 | .load(std::sync::atomic::Ordering::Relaxed), |
| 78 | )), |
| 79 | decoded_value_cache_limit: Arc::new(AtomicUsize::new(self.decoded_value_cache_limit())), |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | pub fn text_style_config(&self) -> &Arc<RwLock<StyleConfigMap>> { |
| 84 | &self.text_style_config |
| 85 | } |
| 86 | |
| 87 | pub(crate) fn decoded_value_cache_limit(&self) -> usize { |
| 88 | self.decoded_value_cache_limit.load(Ordering::Relaxed) |
| 89 | } |
| 90 | |
| 91 | pub(crate) fn set_decoded_value_cache_limit(&self, limit: usize) { |
| 92 | self.decoded_value_cache_limit |
| 93 | .store(limit, Ordering::Relaxed); |
| 94 | } |
| 95 | |
| 96 | pub fn record_timestamp(&self) -> bool { |
| 97 | self.record_timestamp |
| 98 | .load(std::sync::atomic::Ordering::Relaxed) |
| 99 | } |
| 100 | |
| 101 | pub fn set_record_timestamp(&self, record: bool) { |
| 102 | self.record_timestamp |
| 103 | .store(record, std::sync::atomic::Ordering::Relaxed); |
| 104 | } |
| 105 | |
| 106 | pub fn detached_editing(&self) -> bool { |
| 107 | self.editable_detached_mode |
| 108 | .load(std::sync::atomic::Ordering::Relaxed) |
| 109 | } |
| 110 | |
| 111 | pub fn set_detached_editing(&self, mode: bool) { |
| 112 | self.editable_detached_mode |
| 113 | .store(mode, std::sync::atomic::Ordering::Relaxed); |
| 114 | } |
| 115 | |
| 116 | pub fn merge_interval(&self) -> i64 { |
| 117 | self.merge_interval_in_s |
| 118 | .load(std::sync::atomic::Ordering::Relaxed) |
| 119 | } |
| 120 | |
| 121 | pub fn set_merge_interval(&self, interval: i64) { |
| 122 | self.merge_interval_in_s |
| 123 | .store(interval, std::sync::atomic::Ordering::Relaxed); |
| 124 | } |
| 125 | |
| 126 | pub fn set_hide_empty_root_containers(&self, hide: bool) { |
| 127 | self.hide_empty_root_containers |
| 128 | .store(hide, std::sync::atomic::Ordering::Relaxed); |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | #[derive(Debug)] |
| 133 | pub struct DefaultRandom; |
| 134 | |
| 135 | #[cfg(test)] |
| 136 | use std::sync::atomic::AtomicU64; |
| 137 | #[cfg(test)] |
| 138 | static mut TEST_RANDOM: AtomicU64 = AtomicU64::new(0); |
| 139 | |
| 140 | impl SecureRandomGenerator for DefaultRandom { |
| 141 | fn fill_byte(&self, dest: &mut [u8]) { |
| 142 | #[cfg(all(not(test), not(loom)))] |
| 143 | getrandom::getrandom(dest).unwrap(); |
| 144 | |
| 145 | // Loom requires every execution of a model to be deterministic, so |
| 146 | // peer ids come from a counter that loom resets per execution. |
| 147 | #[cfg(all(not(test), loom))] |
| 148 | { |
| 149 | loom::lazy_static! { |
| 150 | static ref LOOM_RANDOM: std::sync::atomic::AtomicU64 = |
| 151 | std::sync::atomic::AtomicU64::new(0); |
| 152 | } |
| 153 | let bytes = LOOM_RANDOM |
| 154 | .fetch_add(1, std::sync::atomic::Ordering::Relaxed) |
| 155 | .to_le_bytes(); |
| 156 | dest.copy_from_slice(&bytes[..dest.len()]); |
| 157 | } |
| 158 | |
| 159 | #[cfg(test)] |
| 160 | // SAFETY: this is only used in test |
| 161 | unsafe { |
| 162 | #[allow(static_mut_refs)] |
| 163 | let bytes = TEST_RANDOM |
| 164 | .fetch_add(1, std::sync::atomic::Ordering::Release) |
| 165 | .to_le_bytes(); |
| 166 | dest.copy_from_slice(&bytes[..dest.len()]); |
| 167 | } |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | pub trait SecureRandomGenerator: Send + Sync { |
| 172 | fn fill_byte(&self, dest: &mut [u8]); |
| 173 | fn next_u64(&self) -> u64 { |
| 174 | let mut buf = [0u8; 8]; |
| 175 | self.fill_byte(&mut buf); |
| 176 | u64::from_le_bytes(buf) |
| 177 | } |
| 178 | |
| 179 | fn next_u32(&self) -> u32 { |
| 180 | let mut buf = [0u8; 4]; |
| 181 | self.fill_byte(&mut buf); |
| 182 | u32::from_le_bytes(buf) |
| 183 | } |
| 184 | |
| 185 | fn next_i64(&self) -> i64 { |
| 186 | let mut buf = [0u8; 8]; |
| 187 | self.fill_byte(&mut buf); |
| 188 | i64::from_le_bytes(buf) |
| 189 | } |
| 190 | |
| 191 | fn next_i32(&self) -> i32 { |
| 192 | let mut buf = [0u8; 4]; |
| 193 | self.fill_byte(&mut buf); |
| 194 | i32::from_le_bytes(buf) |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | #[cfg(test)] |
| 199 | mod tests { |
| 200 | use std::sync::{atomic::Ordering, Mutex, OnceLock}; |
| 201 | |
| 202 | use loro_common::{ContainerID, ContainerType, InternalString}; |
| 203 | |
| 204 | use crate::container::richtext::ExpandType; |
| 205 | |
| 206 | use super::*; |
| 207 | |
| 208 | fn random_lock() -> &'static Mutex<()> { |
| 209 | static LOCK: OnceLock<Mutex<()>> = OnceLock::new(); |
| 210 | LOCK.get_or_init(|| Mutex::new(())) |
| 211 | } |
| 212 | |
| 213 | #[test] |
| 214 | fn configure_default_values_and_setters_match_the_public_contract() { |
| 215 | let config = Configure::default(); |
| 216 | |
| 217 | assert!(!config.record_timestamp()); |
| 218 | assert!(!config.detached_editing()); |
| 219 | assert_eq!(config.merge_interval(), 1000); |
| 220 | assert_eq!( |
| 221 | config.decoded_value_cache_limit(), |
| 222 | DEFAULT_DECODED_VALUE_CACHE_LIMIT |
| 223 | ); |
| 224 | assert!(!config.hide_empty_root_containers.load(Ordering::Relaxed)); |
| 225 | assert!(config.deleted_root_containers.lock().is_empty()); |
| 226 | |
| 227 | let styles = config.text_style_config.read(); |
| 228 | assert_eq!( |
| 229 | styles.get(&InternalString::from("bold")), |
| 230 | Some(StyleConfig { |
| 231 | expand: ExpandType::After, |
| 232 | }) |
| 233 | ); |
| 234 | assert_eq!( |
| 235 | styles.get(&InternalString::from("italic")), |
| 236 | Some(StyleConfig { |
| 237 | expand: ExpandType::After, |
| 238 | }) |
| 239 | ); |
| 240 | assert_eq!( |
| 241 | styles.get(&InternalString::from("link")), |
| 242 | Some(StyleConfig { |
| 243 | expand: ExpandType::None, |
| 244 | }) |
| 245 | ); |
| 246 | assert_eq!(styles.get(&InternalString::from("missing")), None); |
| 247 | |
| 248 | config.set_record_timestamp(true); |
| 249 | config.set_detached_editing(true); |
| 250 | config.set_merge_interval(42); |
| 251 | config.set_hide_empty_root_containers(true); |
| 252 | |
| 253 | assert!(config.record_timestamp()); |
| 254 | assert!(config.detached_editing()); |
| 255 | assert_eq!(config.merge_interval(), 42); |
| 256 | assert!(config.hide_empty_root_containers.load(Ordering::Relaxed)); |
| 257 | } |
| 258 | |
| 259 | #[test] |
| 260 | fn configure_fork_copies_current_state_and_then_diverges() { |
| 261 | let config = Configure::default(); |
| 262 | config.set_record_timestamp(true); |
| 263 | config.set_detached_editing(true); |
| 264 | config.set_merge_interval(25); |
| 265 | config.set_decoded_value_cache_limit(8192); |
| 266 | config.set_hide_empty_root_containers(true); |
| 267 | config |
| 268 | .deleted_root_containers |
| 269 | .lock() |
| 270 | .insert(ContainerID::Root { |
| 271 | name: InternalString::from("root"), |
| 272 | container_type: ContainerType::Map, |
| 273 | }); |
| 274 | config.text_style_config.write().insert( |
| 275 | InternalString::from("custom"), |
| 276 | StyleConfig { |
| 277 | expand: ExpandType::None, |
| 278 | }, |
| 279 | ); |
| 280 | |
| 281 | let forked = config.fork(); |
| 282 | |
| 283 | assert!(forked.record_timestamp()); |
| 284 | assert!(forked.detached_editing()); |
| 285 | assert_eq!(forked.merge_interval(), 25); |
| 286 | assert_eq!(forked.decoded_value_cache_limit(), 8192); |
| 287 | assert!(forked.hide_empty_root_containers.load(Ordering::Relaxed)); |
| 288 | assert_eq!(forked.deleted_root_containers.lock().len(), 1); |
| 289 | assert_eq!( |
| 290 | forked |
| 291 | .text_style_config |
| 292 | .read() |
| 293 | .get(&InternalString::from("custom")), |
| 294 | Some(StyleConfig { |
| 295 | expand: ExpandType::None, |
| 296 | }) |
| 297 | ); |
| 298 | |
| 299 | config.set_record_timestamp(false); |
| 300 | config.set_detached_editing(false); |
| 301 | config.set_merge_interval(99); |
| 302 | config.set_decoded_value_cache_limit(0); |
| 303 | config.set_hide_empty_root_containers(false); |
| 304 | config.deleted_root_containers.lock().clear(); |
| 305 | config |
| 306 | .text_style_config |
| 307 | .write() |
| 308 | .insert(InternalString::from("fork-only"), StyleConfig::default()); |
| 309 | |
| 310 | assert!(forked.record_timestamp()); |
| 311 | assert!(forked.detached_editing()); |
| 312 | assert_eq!(forked.merge_interval(), 25); |
| 313 | assert_eq!(forked.decoded_value_cache_limit(), 8192); |
| 314 | assert!(forked.hide_empty_root_containers.load(Ordering::Relaxed)); |
| 315 | assert_eq!(forked.deleted_root_containers.lock().len(), 1); |
| 316 | assert!(forked |
| 317 | .text_style_config |
| 318 | .read() |
| 319 | .get(&InternalString::from("fork-only")) |
| 320 | .is_none()); |
| 321 | } |
| 322 | |
| 323 | #[test] |
| 324 | fn default_random_test_mode_uses_the_incrementing_counter_for_integer_helpers() { |
| 325 | let _guard = random_lock().lock().unwrap(); |
| 326 | let random = DefaultRandom; |
| 327 | |
| 328 | let a = random.next_u64(); |
| 329 | let b = random.next_u32(); |
| 330 | let c = random.next_i64(); |
| 331 | let d = random.next_i32(); |
| 332 | |
| 333 | assert_eq!(b as u64, (a + 1) as u32 as u64); |
| 334 | assert_eq!(c as u64, a + 2); |
| 335 | assert_eq!(d as u64, (a + 3) as u32 as u64); |
| 336 | } |
| 337 | } |