Advertisement
umuro

binary keys for partial key search in Rust for a key-value store

Mar 22nd, 2023
1,699
1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 2.22 KB | Software | 1 0
  1. // We use a vector of binary keys on a key-valute store, we need a separator for partial key searches.
  2. // We convert the vector of keys to 7 bit and use the unused bit as a separator.
  3. //    convert_to_7_bit: Converts a field to 7-bit representation.
  4. //    get_binary_key: Creates a binary key from an array of byte fields.
  5. //    convert_from_7_bit: Converts a 7-bit representation back to the original field.
  6. //    get_fields_from_key: Extracts the original fields from a binary key.
  7.  
  8. fn convert_to_7_bit(field: &[u8]) -> Vec<u8> {
  9.     let mut result = Vec::new();
  10.     let mut carry = 0;
  11.     let mut carry_bits = 0;
  12.  
  13.     for byte in field {
  14.         let bits = ((byte as u16) << carry_bits) | carry;
  15.         carry = (bits >> 7) as u8;
  16.         carry_bits += 1;
  17.         result.push((bits & 0x7F) as u8);
  18.         if carry_bits == 7 {
  19.             result.push(carry);
  20.             carry = 0;
  21.             carry_bits = 0;
  22.         }
  23.     }
  24.  
  25.     if carry_bits > 0 {
  26.         result.push(carry);
  27.     }
  28.  
  29.     result
  30. }
  31.  
  32. fn get_binary_key(fields: &[&[u8]]) -> Vec<u8> {
  33.     let delimiter = 0b10000000u8; // Delimiter byte (0x80)
  34.     let mut key = Vec::new();
  35.  
  36.     for field in fields {
  37.         let converted_field = convert_to_7_bit(field);
  38.         key.extend_from_slice(&converted_field);
  39.         key.push(delimiter);
  40.     }
  41.  
  42.     // key.pop(); // Not removing enables partial key search for all cases
  43.     key
  44. }
  45.  
  46. fn convert_from_7_bit(encoded: &[u8]) -> Vec<u8> {
  47.     let mut result = Vec::new();
  48.     let mut carry = 0;
  49.     let mut carry_bits = 0;
  50.  
  51.     for byte in encoded {
  52.         let bits = (u16::from(byte) << carry_bits) | carry;
  53.         carry = bits & ((1 << carry_bits) - 1);
  54.         carry_bits += 1;
  55.         result.push((bits >> carry_bits) as u8);
  56.  
  57.         if carry_bits == 7 {
  58.             carry_bits = 0;
  59.         }
  60.     }
  61.  
  62.     result
  63. }
  64.  
  65. fn get_fields_from_key(key: &[u8]) -> Vec<Vec<u8>> {
  66.     let delimiter = 0b10000000u8;
  67.     let mut fields = Vec::new();
  68.     let mut start = 0;
  69.  
  70.     for (i, byte) in key.iter().enumerate() {
  71.         if *byte == delimiter {
  72.             let field = convert_from_7_bit(&key[start..i]);
  73.             fields.push(field);
  74.             start = i + 1;
  75.         }
  76.     }
  77.  
  78.     fields
  79. }
  80.  
Tags: Key db Composite
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement