Skip to main content

feox_server/
watch_registry.rs

1use dashmap::DashMap;
2use once_cell::sync::Lazy;
3use std::sync::atomic::{AtomicU64, Ordering};
4
5static GLOBAL_VERSION: AtomicU64 = AtomicU64::new(1);
6static KEY_VERSIONS: Lazy<DashMap<Vec<u8>, u64>> = Lazy::new(DashMap::new);
7
8pub struct WatchRegistry;
9
10impl WatchRegistry {
11    pub fn notify_key_modified(key: &[u8]) {
12        let version = GLOBAL_VERSION.fetch_add(1, Ordering::SeqCst);
13        KEY_VERSIONS.insert(key.to_vec(), version);
14    }
15
16    pub fn get_key_version(key: &[u8]) -> u64 {
17        KEY_VERSIONS.get(key).map(|v| *v).unwrap_or(0)
18    }
19
20    pub fn check_keys_unchanged(watched_versions: &[(Vec<u8>, u64)]) -> bool {
21        for (key, watched_version) in watched_versions {
22            let current_version = Self::get_key_version(key);
23            if current_version != *watched_version {
24                return false;
25            }
26        }
27        true
28    }
29}