Skip to content

Commit

Permalink
Added v2 lib
Browse files Browse the repository at this point in the history
  • Loading branch information
Gifted-s committed Jul 29, 2024
1 parent f7c69de commit d7ff25b
Show file tree
Hide file tree
Showing 10 changed files with 493 additions and 4 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
/target
/Cargo.lock
**/target
**/Cargo.lock
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ According to the benchmarks presented in the WiscKey paper, implementations can
- **2.5x to 111x** for database loading
- **1.6x to 14x** for random lookups

## Designed for asynchronous runtime
## Designed for asynchronous runtime (unstable)
Based on the introduction and efficiency of async IO at the OS kernel level e.g **io_uring** for the Linux kernel, VelarixDB is designed for asynchronous runtime. In this case Tokio runtime.
Tokio allows for efficient and scalable asynchronous operations, making the most of modern multi-core processors. Frankly, most OS File System does not provide async API currently but Tokio uses a thread pool to offload blocking file system operations.
This means that even though the file system operations themselves are blocking at the OS level, Tokio can handle them without blocking the main async task executor.
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
//! - **2.5x to 111x** for database loading
//! - **1.6x to 14x** for random lookups
//!
//! ## Designed for asynchronous runtime
//! ## Designed for asynchronous runtime (unstable)
//!
//! Based on the introduction and efficiency of async IO at the OS kernel level e.g **io_uring** for the Linux kernel, VelarixDB is designed for asynchronous runtime. In this case Tokio runtime.
//! Tokio allows for efficient and scalable asynchronous operations, making the most of modern multi-core processors. Frankly, most OS File System does not provide async API currently but Tokio uses a thread pool to offload blocking file system operations.
Expand Down
Binary file not shown.
15 changes: 15 additions & 0 deletions v2/Cargo.toml
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"
107 changes: 107 additions & 0 deletions v2/src/bloom/bit_array.rs
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));
}
}
Loading

0 comments on commit d7ff25b

Please sign in to comment.