Hi there,
New to rust, and I'm trying to implement a project using fst crate. I want to flush every set time interval so I was trying to use Mutex to take a lock. Here is the code I have so far,
use chrono::Duration;
use fst::{Map, MapBuilder};
use futures::{SinkExt, StreamExt};
use std::fs::File;
use std::io::{BufWriter, Cursor, Write};
use std::ops::Deref;
use std::sync::{Arc, Mutex};
use std::thread::Thread;
use std::{io, thread};
pub struct TestFST {
pub _type: String,
//pub map: Arc<Mutex<Vec<String>>>
pub map: MapBuilder<BufWriter<File>>,
}
impl TestFST {
fn new(_type: String) -> Self {
let mut wtr = io::BufWriter::new(File::create("map.fst").unwrap());
Self {
_type,
map: MapBuilder::new(wtr).unwrap(),
}
}
pub fn init(_type: String, time: u32) -> Self {
TestFST::new(_type);
}
}
fn main() {
let fst = Arc::new(Mutex::new(TestFST::init("test".parse().unwrap(), 5)));
let mut fst_clone = Arc::clone(&fst);
// thread the mimics a stream of data to be inserted into fst
thread::spawn(move || loop {
let mut f = fst.lock().unwrap();
(*f).map.insert("somedata", 1u64);
});
// finish the fst after 5 secs
thread::spawn(move || {
loop {
thread::sleep(tokio::time::Duration::from_secs(5 as u64));
let mut f = fst_clone.lock().unwrap();
(*f).map.finish();
// after calling finish current fst is immutable. a new fst needs to be created for the insert thread to write to.
// maybe use a AtomicBoolean to see if the fst is writeable and create a new fst in the writer thread if it's not.
}
});
}
error[E0507]: cannot move out of dereference of `std::sync::MutexGuard<'_, TestFST>`
--> src/main.rs:161:13
|
161 | (*f).map.finish();
| ^^^^^^^^ move occurs because value has type `fst::MapBuilder<std::vec::Vec<u8>>`, which does not implement the `Copy` trait
Any pointers? Thank you