nostrdb-rs

nostrdb in rust!
git clone git://jb55.com/nostrdb-rs
Log | Files | Refs | Submodules | README | LICENSE

config.rs (2583B)


      1 use crate::bindings;
      2 
      3 #[derive(Copy, Clone)]
      4 pub struct Config {
      5     pub config: bindings::ndb_config,
      6 }
      7 
      8 impl Default for Config {
      9     fn default() -> Self {
     10         Config::new()
     11     }
     12 }
     13 
     14 impl Config {
     15     pub fn new() -> Self {
     16         let mut config = bindings::ndb_config {
     17             filter_context: std::ptr::null_mut(),
     18             sub_cb: None,
     19             sub_cb_ctx: std::ptr::null_mut(),
     20             ingest_filter: None,
     21             flags: 0,
     22             ingester_threads: 0,
     23             writer_scratch_buffer_size: 1024 * 1024,
     24             mapsize: 0,
     25         };
     26 
     27         unsafe {
     28             bindings::ndb_default_config(&mut config);
     29         }
     30 
     31         Config { config }
     32     }
     33 
     34     //
     35     pub fn set_flags(mut self, flags: i32) -> Self {
     36         self.config.flags = flags;
     37         self
     38     }
     39 
     40     pub fn skip_validation(mut self, skip: bool) -> Self {
     41         let skip_note_verify: i32 = 1 << 1;
     42 
     43         if skip {
     44             self.config.flags |= skip_note_verify;
     45         } else {
     46             self.config.flags &= !skip_note_verify;
     47         }
     48 
     49         self
     50     }
     51 
     52     /// Set a callback to be notified on updated subscriptions. The function
     53     /// will be called with the corresponsing subscription id.
     54     pub fn set_sub_callback<F>(mut self, closure: F) -> Self
     55     where
     56         F: FnMut(u64) + 'static,
     57     {
     58         // Box the closure to ensure it has a stable address.
     59         let boxed_closure: Box<dyn FnMut(u64)> = Box::new(closure);
     60 
     61         // Convert it to a raw pointer to store in sub_cb_ctx.
     62         let ctx_ptr = Box::into_raw(Box::new(boxed_closure)) as *mut ::std::os::raw::c_void;
     63 
     64         self.config.sub_cb = Some(sub_callback_trampoline);
     65         self.config.sub_cb_ctx = ctx_ptr;
     66         self
     67     }
     68 
     69     pub fn set_mapsize(mut self, bytes: usize) -> Self {
     70         self.config.mapsize = bytes;
     71         self
     72     }
     73 
     74     pub fn set_ingester_threads(mut self, threads: i32) -> Self {
     75         self.config.ingester_threads = threads;
     76         self
     77     }
     78 
     79     // Internal method to get a raw pointer to the config, used in Ndb
     80     pub fn as_ptr(&self) -> *const bindings::ndb_config {
     81         &self.config
     82     }
     83 }
     84 
     85 extern "C" fn sub_callback_trampoline(ctx: *mut ::std::os::raw::c_void, subid: u64) {
     86     unsafe {
     87         // Convert the raw pointer back into a reference to our closure.
     88         // We know this pointer was created by Box::into_raw in `set_sub_callback_rust`.
     89         let closure_ptr = ctx as *mut Box<dyn FnMut(u64)>;
     90         assert!(!closure_ptr.is_null());
     91         let closure = &mut *closure_ptr;
     92         closure(subid);
     93     }
     94 }