Skip to main content

linkrs_server/client/
space_context.rs

1use parking_lot::RwLock;
2use std::sync::Arc;
3
4use linkrs_core::types::SpaceSummary;
5
6#[derive(Debug)]
7pub struct SpaceContext {
8    space: Arc<RwLock<Option<SpaceSummary>>>,
9}
10
11impl Default for SpaceContext {
12    fn default() -> Self {
13        Self::new()
14    }
15}
16
17impl SpaceContext {
18    pub fn new() -> Self {
19        Self {
20            space: Arc::new(RwLock::new(None)),
21        }
22    }
23
24    pub fn space(&self) -> Option<SpaceSummary> {
25        self.space.read().clone()
26    }
27
28    pub fn set_space(&self, space: SpaceSummary) {
29        *self.space.write() = Some(space);
30    }
31
32    pub fn clear_space(&self) {
33        *self.space.write() = None;
34    }
35}
36
37#[cfg(test)]
38mod tests {
39    use super::*;
40    use linkrs_core::types::DataType;
41
42    #[test]
43    fn test_space_context() {
44        let context = SpaceContext::new();
45        assert!(context.space().is_none());
46
47        let space_info = SpaceSummary::new(456, "test_space".to_string(), DataType::BigInt);
48        context.set_space(space_info.clone());
49
50        let space = context.space().expect("space should exist");
51        assert_eq!(space.id, 456);
52        assert_eq!(space.name, "test_space");
53
54        context.clear_space();
55        assert!(context.space().is_none());
56    }
57}