drive/util/common/encode.rs
1#![allow(clippy::result_large_err)] // Encoding helpers bubble up drive::Error with context
2//! Encoding.
3//!
4//! This module defines encoding functions.
5//!
6
7use crate::error::drive::DriveError;
8use crate::error::Error;
9use byteorder::{BigEndian, ByteOrder, WriteBytesExt};
10
11/// Encodes an unsigned integer on 64 bits.
12pub fn encode_u64(val: u64) -> Vec<u8> {
13 // Positive integers are represented in binary with the signed bit set to 0
14 // Negative integers are represented in 2's complement form
15
16 // Encode the integer in big endian form
17 // This ensures that most significant bits are compared first
18 // a bigger positive number would be greater than a smaller one
19 // and a bigger negative number would be greater than a smaller one
20 // maintains sort order for each domain
21 let mut wtr = vec![];
22 wtr.write_u64::<BigEndian>(val).unwrap();
23
24 // Flip the sign bit
25 // to deal with interaction between the domains
26 // 2's complement values have the sign bit set to 1
27 // this makes them greater than the positive domain in terms of sort order
28 // to fix this, we just flip the sign bit
29 // so positive integers have the high bit and negative integers have the low bit
30 // the relative order of elements in each domain is still maintained, as the
31 // change was uniform across all elements
32 wtr[0] ^= 0b1000_0000;
33
34 wtr
35}
36
37/// Decodes a 64-bit unsigned integer from a vector of bytes encoded with `encode_u64`.
38///
39/// # Arguments
40///
41/// * `bytes` - A vector of bytes representing the encoded 64-bit unsigned integer.
42///
43/// # Returns
44///
45/// * A 64-bit unsigned integer decoded from the input bytes.
46///
47/// # Panics
48///
49/// This function will panic if the input vector does not have exactly 8 bytes.
50pub fn decode_u64_owned(mut bytes: Vec<u8>) -> Result<u64, Error> {
51 // Ensure the input vector has exactly 8 bytes
52 if bytes.len() != 8 {
53 return Err(Error::Drive(DriveError::CorruptedDriveState(format!(
54 "Trying to decode a u64 from {} bytes {}",
55 bytes.len(),
56 hex::encode(bytes)
57 ))));
58 }
59
60 // Flip the sign bit back to its original state
61 // This reverses the transformation done in `encode_u64`
62 bytes[0] ^= 0b1000_0000;
63
64 // Read the integer from the modified bytes
65 // The bytes are in big endian form, which preserves the correct order
66 // when they were written in the encode function
67 Ok(BigEndian::read_u64(&bytes))
68}
69
70/// Decodes a 64-bit unsigned integer from a vector of bytes encoded with `encode_u64`.
71///
72/// # Arguments
73///
74/// * `bytes` - A vector of bytes representing the encoded 64-bit unsigned integer.
75///
76/// # Returns
77///
78/// * A 64-bit unsigned integer decoded from the input bytes.
79///
80/// # Panics
81///
82/// This function will panic if the input vector does not have exactly 8 bytes.
83pub fn decode_u64(bytes: &[u8]) -> Result<u64, Error> {
84 // Ensure the input vector has exactly 8 bytes
85 if bytes.len() != 8 {
86 return Err(Error::Drive(DriveError::CorruptedDriveState(format!(
87 "Trying to decode a u64 from {} bytes {}",
88 bytes.len(),
89 hex::encode(bytes)
90 ))));
91 }
92
93 let mut wtr = bytes.to_vec();
94
95 // Flip the sign bit back to its original state
96 // This reverses the transformation done in `encode_u64`
97 wtr[0] ^= 0b1000_0000;
98
99 // Read the integer from the modified bytes
100 // The bytes are in big endian form, which preserves the correct order
101 // when they were written in the encode function
102 Ok(BigEndian::read_u64(&wtr))
103}
104
105/// Encodes a signed integer on 64 bits.
106pub fn encode_i64(val: i64) -> Vec<u8> {
107 // Positive integers are represented in binary with the signed bit set to 0
108 // Negative integers are represented in 2's complement form
109
110 // Encode the integer in big endian form
111 // This ensures that most significant bits are compared first
112 // a bigger positive number would be greater than a smaller one
113 // and a bigger negative number would be greater than a smaller one
114 // maintains sort order for each domain
115 let mut wtr = vec![];
116 wtr.write_i64::<BigEndian>(val).unwrap();
117
118 // Flip the sign bit
119 // to deal with interaction between the domains
120 // 2's complement values have the sign bit set to 1
121 // this makes them greater than the positive domain in terms of sort order
122 // to fix this, we just flip the sign bit
123 // so positive integers have the high bit and negative integers have the low bit
124 // the relative order of elements in each domain is still maintained, as the
125 // change was uniform across all elements
126 wtr[0] ^= 0b1000_0000;
127
128 wtr
129}
130
131/// Encodes a float.
132pub fn encode_float(val: f64) -> Vec<u8> {
133 // Floats are represented based on the IEEE 754-2008 standard
134 // [sign bit] [biased exponent] [mantissa]
135
136 // when comparing floats, the sign bit has the greatest impact
137 // any positive number is greater than all negative numbers
138 // if the numbers come from the same domain then the exponent is the next factor to consider
139 // the exponent gives a sense of how many digits are in the non fractional part of the number
140 // for example in base 10, 10 has an exponent of 1 (1.0 * 10^1)
141 // while 5000 (5.0 * 10^3) has an exponent of 3
142 // for the positive domain, the bigger the exponent the larger the number i.e 5000 > 10
143 // for the negative domain, the bigger the exponent the smaller the number i.e -10 > -5000
144 // if the exponents are the same, then the mantissa is used to determine the greater number
145 // the inverse relationship still holds
146 // i.e bigger mantissa (bigger number in positive domain but smaller number in negative domain)
147
148 // There are two things to fix to achieve total sort order
149 // 1. Place positive domain above negative domain (i.e flip the sign bit)
150 // 2. Exponent and mantissa for a smaller number like -5000 is greater than that of -10
151 // so bit level comparison would say -5000 is greater than -10
152 // we fix this by flipping the exponent and mantissa values, which has the effect of reversing
153 // the order (0000 [smallest] -> 1111 [largest])
154
155 // Encode in big endian form, so most significant bits are compared first
156 let mut wtr = vec![];
157 wtr.write_f64::<BigEndian>(val).unwrap();
158
159 // Check if the value is negative, if it is
160 // flip all the bits i.e sign, exponent and mantissa
161 if val < 0.0 {
162 wtr = wtr.iter().map(|byte| !byte).collect();
163 } else {
164 // for positive values, just flip the sign bit
165 wtr[0] ^= 0b1000_0000;
166 }
167
168 wtr
169}
170
171/// Encodes an unsigned integer on 16 bits.
172pub fn encode_u16(val: u16) -> Vec<u8> {
173 // Positive integers are represented in binary with the signed bit set to 0
174 // Negative integers are represented in 2's complement form
175
176 // Encode the integer in big endian form
177 // This ensures that most significant bits are compared first
178 // a bigger positive number would be greater than a smaller one
179 // and a bigger negative number would be greater than a smaller one
180 // maintains sort order for each domain
181 let mut wtr = vec![];
182 wtr.write_u16::<BigEndian>(val).unwrap();
183
184 // Flip the sign bit
185 // to deal with interaction between the domains
186 // 2's complement values have the sign bit set to 1
187 // this makes them greater than the positive domain in terms of sort order
188 // to fix this, we just flip the sign bit
189 // so positive integers have the high bit and negative integers have the low bit
190 // the relative order of elements in each domain is still maintained, as the
191 // change was uniform across all elements
192 wtr[0] ^= 0b1000_0000;
193
194 wtr
195}
196
197/// Encodes an unsigned integer on 32 bits.
198pub fn encode_u32(val: u32) -> Vec<u8> {
199 // Positive integers are represented in binary with the signed bit set to 0
200 // Negative integers are represented in 2's complement form
201
202 // Encode the integer in big endian form
203 // This ensures that most significant bits are compared first
204 // a bigger positive number would be greater than a smaller one
205 // and a bigger negative number would be greater than a smaller one
206 // maintains sort order for each domain
207 let mut wtr = vec![];
208 wtr.write_u32::<BigEndian>(val).unwrap();
209
210 // Flip the sign bit
211 // to deal with interaction between the domains
212 // 2's complement values have the sign bit set to 1
213 // this makes them greater than the positive domain in terms of sort order
214 // to fix this, we just flip the sign bit
215 // so positive integers have the high bit and negative integers have the low bit
216 // the relative order of elements in each domain is still maintained, as the
217 // change was uniform across all elements
218 wtr[0] ^= 0b1000_0000;
219
220 wtr
221}