Skip to content

Commit

Permalink
start rust API, move raw FFI into another module
Browse files Browse the repository at this point in the history
  • Loading branch information
luizirber committed Nov 4, 2019
1 parent 9182606 commit 4f08dff
Show file tree
Hide file tree
Showing 2 changed files with 152 additions and 0 deletions.
75 changes: 75 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
mod raw;

use std::ffi::CString;
use std::ptr;

pub struct MQF {
Expand All @@ -17,3 +18,77 @@ impl Default for MQF {
}
}
}

impl MQF {
pub fn new(counter_size: u64, qbits: u64) -> MQF {
let mut mqf = MQF::default();

let num_hash_bits = qbits + 8;
let maximum_count = (1u64 << counter_size) - 1;

let s = CString::new("").unwrap();

unsafe {
raw::qf_init(
&mut mqf.inner,
1u64 << qbits, // nslots
num_hash_bits, // key_bits
0, // label_bits
counter_size, // fixed_counter_size
0, // blocksLabelSize
true, // mem
s.as_ptr(), // path
0, // seed (doesn't matter)
);
};

mqf
}

pub fn insert(&mut self, key: u64, count: u64) {
unsafe { raw::qf_insert(&mut self.inner, key, count, false, false) };
}

pub fn count_key(&self, key: u64) -> u64 {
unsafe { raw::qf_count_key(&self.inner, key) }
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn simple_counting_test_api() {
//except first item is inserted 5 times to full test _insert1
let counter_size = 2;
let qbits = 5;
let mut qf: MQF = MQF::new(counter_size, qbits);

let mut count = 0;
let mut fixed_counter = 0;

for i in 0..=10 {
qf.insert(100, 1);
count = qf.count_key(100);
dbg!((count, fixed_counter));
assert_eq!(count, 1 + i);
}

qf.insert(1500, 50);

count = qf.count_key(1500);
dbg!((count, fixed_counter));
assert_eq!(count, 50);

qf.insert(1600, 60);
count = qf.count_key(1600);
dbg!((count, fixed_counter));
assert_eq!(count, 60);

qf.insert(2000, 4000);
count = qf.count_key(2000);
dbg!((count, fixed_counter));
assert_eq!(count, 4000);
}
}
77 changes: 77 additions & 0 deletions src/raw.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#![allow(non_upper_case_globals)]
#![allow(non_camel_case_types)]
#![allow(non_snake_case)]
#![allow(dead_code)]
include!(concat!(env!("OUT_DIR"), "/bindings.rs"));

#[cfg(test)]
mod tests {
use super::*;
use std::ffi::CString;
use std::ptr;

#[test]
fn simple_counting_test() {
//except first item is inserted 5 times to full test _insert1
let mut qf: QF = QF {
mem: ptr::null_mut(),
metadata: ptr::null_mut(),
blocks: ptr::null_mut(),
};

let counter_size = 2;
let qbits = 5;
let num_hash_bits = qbits + 8;
let maximum_count = (1u64 << counter_size) - 1;
let mut count = 0;
let mut fixed_counter = 0;

let s = CString::new("").unwrap();

//INFO("Counter size = "<<counter_size<<" max count= "<<maximum_count);
unsafe {
qf_init(
&mut qf,
1u64 << qbits,
num_hash_bits,
0,
counter_size,
0,
true,
s.as_ptr(),
2038074761,
);
}

for i in 0..=10 {
unsafe {
qf_insert(&mut qf, 100, 1, false, false);
};
count = unsafe { qf_count_key(&qf, 100) };
dbg!((count, fixed_counter));
assert_eq!(count, 1 + i);
}

unsafe {
qf_insert(&mut qf, 1500, 50, false, false);
}

count = unsafe { qf_count_key(&qf, 1500) };
dbg!((count, fixed_counter));
assert_eq!(count, 50);

unsafe {
qf_insert(&mut qf, 1600, 60, false, false);
}
count = unsafe { qf_count_key(&qf, 1600) };
dbg!((count, fixed_counter));
assert_eq!(count, 60);

unsafe {
qf_insert(&mut qf, 2000, 4000, false, false);
}
count = unsafe { qf_count_key(&qf, 2000) };
dbg!((count, fixed_counter));
assert_eq!(count, 4000);
}
}

0 comments on commit 4f08dff

Please sign in to comment.