• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

getdozer / dozer / 4382580286

pending completion
4382580286

push

github

GitHub
feat: Separate cache operation log environment and index environments (#1199)

1370 of 1370 new or added lines in 33 files covered. (100.0%)

28671 of 41023 relevant lines covered (69.89%)

51121.29 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

82.18
/dozer-cache/src/cache/lmdb/cache/main_environment/operation_log/lmdb_val_impl.rs
1
use dozer_storage::{errors::StorageError, BorrowEncode, Decode, Encode, Encoded, LmdbVal};
2
use dozer_types::{
3
    borrow::{Borrow, Cow, IntoOwned},
4
    impl_borrow_for_clone_type,
5
};
6

7
use super::{Operation, OperationBorrow, RecordMetadata};
8

9
impl_borrow_for_clone_type!(RecordMetadata);
10

11
impl BorrowEncode for RecordMetadata {
12
    type Encode<'a> = &'a RecordMetadata;
13
}
14

15
impl<'a> Encode<'a> for &'a RecordMetadata {
16
    fn encode(self) -> Result<Encoded<'a>, StorageError> {
11,353✔
17
        let mut result = [0; 21];
11,353✔
18
        result[0..8].copy_from_slice(&self.id.to_be_bytes());
11,353✔
19
        result[8..12].copy_from_slice(&self.version.to_be_bytes());
11,353✔
20
        if let Some(insert_operation_id) = self.insert_operation_id {
11,353✔
21
            result[12] = 1;
11,338✔
22
            result[13..21].copy_from_slice(&insert_operation_id.to_be_bytes());
11,338✔
23
        } else {
11,338✔
24
            result[12] = 0;
15✔
25
        }
15✔
26
        Ok(Encoded::U8x21(result))
11,353✔
27
    }
11,353✔
28
}
29

30
impl Decode for RecordMetadata {
31
    fn decode(bytes: &[u8]) -> Result<Cow<Self>, StorageError> {
53✔
32
        let id = u64::from_be_bytes(bytes[0..8].try_into().unwrap());
53✔
33
        let version = u32::from_be_bytes(bytes[8..12].try_into().unwrap());
53✔
34
        let insert_operation_id = if bytes[12] == 1 {
53✔
35
            Some(u64::from_be_bytes(bytes[13..21].try_into().unwrap()))
41✔
36
        } else {
37
            None
12✔
38
        };
39
        Ok(Cow::Owned(RecordMetadata {
53✔
40
            id,
53✔
41
            version,
53✔
42
            insert_operation_id,
53✔
43
        }))
53✔
44
    }
53✔
45
}
46

47
unsafe impl LmdbVal for RecordMetadata {}
48

49
impl<'a> IntoOwned<Operation> for OperationBorrow<'a> {
50
    fn into_owned(self) -> Operation {
×
51
        match self {
×
52
            Self::Delete { operation_id } => Operation::Delete { operation_id },
×
53
            Self::Insert { record_id, record } => Operation::Insert {
×
54
                record_id,
×
55
                record: record.clone(),
×
56
            },
×
57
        }
58
    }
×
59
}
60

61
impl Borrow for Operation {
62
    type Borrowed<'a> = OperationBorrow<'a>;
63

64
    fn borrow(&self) -> Self::Borrowed<'_> {
2✔
65
        match self {
2✔
66
            Self::Delete { operation_id } => OperationBorrow::Delete {
1✔
67
                operation_id: *operation_id,
1✔
68
            },
1✔
69
            Self::Insert { record_id, record } => OperationBorrow::Insert {
1✔
70
                record_id: *record_id,
1✔
71
                record,
1✔
72
            },
1✔
73
        }
74
    }
2✔
75

76
    fn upcast<'b, 'a: 'b>(borrow: Self::Borrowed<'a>) -> Self::Borrowed<'b> {
×
77
        match borrow {
×
78
            OperationBorrow::Delete { operation_id } => OperationBorrow::Delete { operation_id },
×
79
            OperationBorrow::Insert { record_id, record } => {
×
80
                OperationBorrow::Insert { record_id, record }
×
81
            }
82
        }
83
    }
×
84
}
85

86
impl BorrowEncode for Operation {
87
    type Encode<'a> = OperationBorrow<'a>;
88
}
89

90
impl<'a> Encode<'a> for OperationBorrow<'a> {
91
    fn encode(self) -> Result<Encoded<'a>, StorageError> {
11,400✔
92
        dozer_types::bincode::serialize(&self)
11,400✔
93
            .map(Encoded::Vec)
11,400✔
94
            .map_err(|e| StorageError::SerializationError {
11,400✔
95
                typ: "Operation",
×
96
                reason: Box::new(e),
×
97
            })
11,400✔
98
    }
11,400✔
99
}
100

101
impl Decode for Operation {
102
    fn decode(bytes: &[u8]) -> Result<Cow<Self>, StorageError> {
1,207,697✔
103
        dozer_types::bincode::deserialize(bytes)
1,207,697✔
104
            .map(Cow::Owned)
1,207,697✔
105
            .map_err(|e| StorageError::DeserializationError {
1,207,697✔
106
                typ: "Operation",
×
107
                reason: Box::new(e),
×
108
            })
1,207,697✔
109
    }
1,207,697✔
110
}
111

112
unsafe impl LmdbVal for Operation {}
113

114
#[cfg(test)]
115
mod tests {
116
    use dozer_types::types::Record;
117

118
    use super::*;
119

120
    #[test]
1✔
121
    fn test_record_metadata_encode_decode() {
1✔
122
        let record_metadata = RecordMetadata {
1✔
123
            id: 1,
1✔
124
            version: 2,
1✔
125
            insert_operation_id: Some(3),
1✔
126
        };
1✔
127
        let encoded = record_metadata.encode().unwrap();
1✔
128
        let decoded = RecordMetadata::decode(encoded.as_ref())
1✔
129
            .unwrap()
1✔
130
            .into_owned();
1✔
131
        assert_eq!(record_metadata, decoded);
1✔
132

133
        let record_metadata = RecordMetadata {
1✔
134
            id: 1,
1✔
135
            version: 2,
1✔
136
            insert_operation_id: None,
1✔
137
        };
1✔
138
        let encoded = record_metadata.encode().unwrap();
1✔
139
        let decoded = RecordMetadata::decode(encoded.as_ref())
1✔
140
            .unwrap()
1✔
141
            .into_owned();
1✔
142
        assert_eq!(record_metadata, decoded);
1✔
143
    }
1✔
144

145
    #[test]
1✔
146
    fn test_operation_encode_decode() {
1✔
147
        let operation = Operation::Delete { operation_id: 1 };
1✔
148
        let encoded = operation.borrow().encode().unwrap();
1✔
149
        let decoded = Operation::decode(encoded.as_ref()).unwrap().into_owned();
1✔
150
        assert_eq!(operation, decoded);
1✔
151

152
        let operation = Operation::Insert {
1✔
153
            record_id: 1,
1✔
154
            record: Record::new(None, vec![1.into(), 2.into(), 3.into()], Some(1)),
1✔
155
        };
1✔
156
        let encoded = operation.borrow().encode().unwrap();
1✔
157
        let decoded = Operation::decode(encoded.as_ref()).unwrap().into_owned();
1✔
158
        assert_eq!(operation, decoded);
1✔
159
    }
1✔
160
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc