Coverage Report

Created: 2026-07-31 00:13

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/home/runner/work/feoxdb/feoxdb/src/core/store/json_patch.rs
Line
Count
Source
1
use crate::error::{FeoxError, Result};
2
use std::sync::Arc;
3
4
use super::FeoxStore;
5
6
impl FeoxStore {
7
    /// Apply a JSON patch to a value.
8
    ///
9
    /// Uses RFC 6902 JSON Patch format to modify specific fields in a JSON document.
10
    /// Both the existing value and the patch must be valid JSON.
11
    ///
12
    /// # Arguments
13
    ///
14
    /// * `key` - The key containing the JSON document to patch
15
    /// * `patch` - JSON Patch operations in RFC 6902 format
16
    /// * `timestamp` - Optional timestamp for conflict resolution
17
    ///
18
    /// # Returns
19
    ///
20
    /// Returns `Ok(())` if the update was applied.
21
    ///
22
    /// # Errors
23
    ///
24
    /// * `KeyNotFound` - Key does not exist
25
    /// * `OlderTimestamp` - Timestamp is not newer than existing record
26
    /// * `JsonPatchError` - Invalid JSON document or patch format
27
    ///
28
    /// # Example
29
    ///
30
    /// ```no_run
31
    /// # use feoxdb::FeoxStore;
32
    /// # fn main() -> feoxdb::Result<()> {
33
    /// # let store = FeoxStore::new(None)?;
34
    /// // Insert initial JSON value
35
    /// let initial = br#"{"name":"Alice","age":30}"#;
36
    /// store.insert(b"user:1", initial)?;
37
    ///
38
    /// // Apply JSON patch to update age
39
    /// let patch = br#"[{"op":"replace","path":"/age","value":31}]"#;
40
    /// store.json_patch(b"user:1", patch)?;
41
    ///
42
    /// // Value now has age updated to 31
43
    /// let updated = store.get(b"user:1")?;
44
    /// assert_eq!(updated.len(), initial.len()); // Same length, just age changed
45
    /// # Ok(())
46
    /// # }
47
    /// ```
48
3
    pub fn json_patch(&self, key: &[u8], patch: &[u8]) -> Result<()> {
49
3
        self.json_patch_with_timestamp(key, patch, None)
50
3
    }
51
52
    /// Apply JSON patch with explicit timestamp.
53
    ///
54
    /// This is the advanced version that allows manual timestamp control.
55
    /// Most users should use `json_patch()` instead.
56
    ///
57
    /// # Arguments
58
    ///
59
    /// * `key` - The key whose value to patch
60
    /// * `patch` - JSON Patch array (RFC 6902)
61
    /// * `timestamp` - Optional timestamp. If `None`, uses current time.
62
    ///
63
    /// # Errors
64
    ///
65
    /// * `OlderTimestamp` - Timestamp is not newer than existing record
66
7
    pub fn json_patch_with_timestamp(
67
7
        &self,
68
7
        key: &[u8],
69
7
        patch: &[u8],
70
7
        timestamp: Option<u64>,
71
7
    ) -> Result<()> {
72
7
        self.validate_key(key)
?0
;
73
7
        let timestamp = self.resolve_timestamp(key, timestamp);
74
7
        let timestamp_value = timestamp.0;
75
76
7
        let start = std::time::Instant::now();
77
7
        let mut observed = None;
78
        loop {
79
9
            let 
record8
= self
80
9
                .hash_table
81
9
                .read(key, |_, record| 
record8
.
clone8
())
82
9
                .ok_or(FeoxError::KeyNotFound)
?1
;
83
8
            let observed = observed.get_or_insert_with(|| 
Arc::clone6
(
&record6
));
84
8
            if !Arc::ptr_eq(observed, &record) && 
timestamp_value2
<= observed.retirement_timestamp()
85
            {
86
1
                return Err(FeoxError::OlderTimestamp);
87
7
            }
88
89
7
            if timestamp_value <= record.timestamp {
90
0
                return Err(FeoxError::OlderTimestamp);
91
7
            }
92
93
7
            let (current_value, _, source) = self.resolve_value(key, record)
?0
;
94
7
            if !Arc::ptr_eq(observed, &source) && 
timestamp_value1
<= observed.retirement_timestamp()
95
            {
96
0
                return Err(FeoxError::OlderTimestamp);
97
7
            }
98
7
            let 
new_value6
= crate::utils::json_patch::apply_json_patch(&current_value, patch)
?1
;
99
6
            self.validate_key_value(key, &new_value)
?0
;
100
6
            crate::test_hooks::pause_at(crate::test_hooks::AFTER_JSON_PATCH_READ);
101
102
6
            if self.replace_record_if_current(key, &source, &new_value, timestamp, 0, start)
?0
{
103
4
                return Ok(());
104
2
            }
105
        }
106
7
    }
107
}