-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
493 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
/target | ||
/Cargo.lock | ||
**/target | ||
**/Cargo.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file modified
BIN
+36 Bytes
(110%)
.../data/buckets/bucket1201eb6b-8903-4557-a5bd-d87cb725f1d8/sstable_1720785462309/summary.db
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
[package] | ||
name = "v2" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
byteorder = "1.5.0" | ||
lz4_flex = "0.11.3" | ||
nanoid = "0.4.0" | ||
seahash = "4.1.0" | ||
tempfile = "3.10.1" | ||
test-log = "0.2.16" | ||
thiserror = "1.0.63" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
/// Gets a bit from the byte | ||
fn get_bit(byte: u8, idx: usize) -> bool { | ||
let bit_mask = 0b1000_0000_u8; | ||
let bit_mask = bit_mask >> idx; | ||
|
||
let masked = byte & bit_mask; | ||
masked > 0 | ||
} | ||
|
||
/// Sets a bit in the byte | ||
fn set_bit(byte: u8, idx: usize, value: bool) -> u8 { | ||
let bit_mask = 0b1000_0000_u8; | ||
let bit_mask = bit_mask >> idx; | ||
|
||
if value { | ||
byte | bit_mask | ||
} else { | ||
byte & !bit_mask | ||
} | ||
} | ||
|
||
/// Fixed-size bit array | ||
#[derive(Debug, Eq, PartialEq)] | ||
pub struct BitArray(Box<[u8]>); | ||
|
||
impl BitArray { | ||
#[must_use] | ||
pub fn with_capacity(bytes: usize) -> Self { | ||
let vec = vec![0; bytes]; | ||
Self(vec.into_boxed_slice()) | ||
} | ||
|
||
#[must_use] | ||
pub fn from_bytes(bytes: Box<[u8]>) -> Self { | ||
Self(bytes) | ||
} | ||
|
||
/// Size in bytes | ||
#[must_use] | ||
pub fn len(&self) -> usize { | ||
self.0.len() | ||
} | ||
|
||
#[allow(dead_code)] | ||
pub fn is_empty(&self) -> bool { | ||
self.len() == 0 | ||
} | ||
|
||
#[must_use] | ||
pub fn bytes(&self) -> &[u8] { | ||
&self.0 | ||
} | ||
|
||
/// Sets the i-th bit | ||
pub fn set(&mut self, idx: usize, val: bool) { | ||
let byte_idx = idx / 8; | ||
let byte = self.0.get_mut(byte_idx).expect("should be in bounds"); | ||
|
||
let bit_idx = idx % 8; | ||
*byte = set_bit(*byte, bit_idx, val); | ||
} | ||
|
||
/// Gets the i-th bit | ||
#[must_use] | ||
pub fn get(&self, idx: usize) -> bool { | ||
let byte_idx = idx / 8; | ||
let byte = self.0.get(byte_idx).expect("should be in bounds"); | ||
|
||
let bit_idx = idx % 8; | ||
get_bit(*byte, bit_idx) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use test_log::test; | ||
|
||
#[test] | ||
fn bit_set_true() { | ||
assert_eq!(0b0000_0010, set_bit(0, 6, true)); | ||
assert_eq!(0b1000_0000, set_bit(0, 0, true)); | ||
assert_eq!(0b0100_0000, set_bit(0, 1, true)); | ||
assert_eq!(0b0100_0110, set_bit(0b0000_0110, 1, true)); | ||
} | ||
|
||
#[test] | ||
fn bit_set_false() { | ||
assert_eq!(0b1111_1101, set_bit(0xFF, 6, false)); | ||
assert_eq!(0b0111_1111, set_bit(0xFF, 0, false)); | ||
assert_eq!(0b1011_1111, set_bit(0xFF, 1, false)); | ||
|
||
assert_eq!(0b0000_0110, set_bit(0b0100_0110, 1, false)); | ||
} | ||
|
||
#[test] | ||
fn bit_set_get() { | ||
assert_eq!(0b1111_1101, set_bit(0xFF, 6, false)); | ||
assert_eq!(0b0111_1111, set_bit(0xFF, 0, false)); | ||
assert_eq!(0b1011_1111, set_bit(0xFF, 1, false)); | ||
|
||
assert!(!get_bit(0b0100_0110, 0)); | ||
assert!(get_bit(0b0100_0110, 1)); | ||
assert!(get_bit(0b0100_0110, 6)); | ||
assert!(!get_bit(0b0100_0110, 7)); | ||
} | ||
} |
Oops, something went wrong.