MCPcopy Create free account
hub / github.com/NodeDB-Lab/nodedb / decode

Function decode

nodedb-codec/src/delta.rs:112–159  ·  view source on GitHub ↗

Decode Delta-compressed bytes back to i64 values.

(data: &[u8])

Source from the content-addressed store, hash-verified

110
111/// Decode Delta-compressed bytes back to i64 values.
112pub fn decode(data: &[u8]) -> Result<Vec<i64>, CodecError> {
113 if data.len() < 4 {
114 return Err(CodecError::Truncated {
115 expected: 4,
116 actual: data.len(),
117 });
118 }
119
120 let count = u32::from_le_bytes(data[0..4].try_into().map_err(|_| CodecError::Corrupt {
121 detail: "invalid header".into(),
122 })?) as usize;
123
124 if count == 0 {
125 return Ok(Vec::new());
126 }
127
128 if data.len() < 12 {
129 return Err(CodecError::Truncated {
130 expected: 12,
131 actual: data.len(),
132 });
133 }
134
135 let first_value =
136 i64::from_le_bytes(data[4..12].try_into().map_err(|_| CodecError::Corrupt {
137 detail: "invalid first value".into(),
138 })?);
139
140 let mut values = Vec::with_capacity(count);
141 values.push(first_value);
142
143 let mut offset = 12;
144 for _ in 1..count {
145 if offset >= data.len() {
146 return Err(CodecError::Truncated {
147 expected: offset + 1,
148 actual: data.len(),
149 });
150 }
151 let (encoded_delta, consumed) = read_varint(&data[offset..])?;
152 let delta = zigzag_decode(encoded_delta);
153 let value = values[values.len() - 1].wrapping_add(delta);
154 values.push(value);
155 offset += consumed;
156 }
157
158 Ok(values)
159}
160
161// ---------------------------------------------------------------------------
162// Streaming encoder / decoder types

Callers 11

newMethod · 0.70
decode_allMethod · 0.70
empty_roundtripFunction · 0.70
single_valueFunction · 0.70
monotonic_counterFunction · 0.70
counter_resetFunction · 0.70
non_monotonic_gaugeFunction · 0.70
negative_valuesFunction · 0.70
large_valuesFunction · 0.70
boundary_valuesFunction · 0.70

Calls 4

read_varintFunction · 0.70
zigzag_decodeFunction · 0.70
lenMethod · 0.45
pushMethod · 0.45

Tested by 9

empty_roundtripFunction · 0.56
single_valueFunction · 0.56
monotonic_counterFunction · 0.56
counter_resetFunction · 0.56
non_monotonic_gaugeFunction · 0.56
negative_valuesFunction · 0.56
large_valuesFunction · 0.56
boundary_valuesFunction · 0.56