Created
May 2, 2020 20:50
-
-
Save jlricon/8ae9c1cfb9af787bda897481a4bffb6b to your computer and use it in GitHub Desktop.
Leaky box
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
use warp::Filter; | |
use std::collections::HashMap; | |
use std::fs::File; | |
use std::io::prelude::*; | |
use std::sync::Arc; | |
type Trigs<'a> = HashMap<&'a [u8], [u32; 256]>; | |
// Working from https://stackoverflow.com/questions/53045522/share-arc-between-closures/53045864#53045864 | |
// Related to https://stackoverflow.com/questions/32083065/borrow-data-out-of-a-mutex-borrowed-value-does-not-live-long-enough | |
#[tokio::main] | |
async fn main() { | |
let data: Box<Vec<u8>> = Box::new(Vec::new()); | |
let data_ref: &'static mut Vec<u8> = Box::leak(data); | |
let trig: Trigs = { | |
let mut f = File::open("src/main.rs").unwrap(); | |
f.read_to_end(data_ref).unwrap(); | |
get_data(data_ref) | |
}; | |
//let rez: Vec<&str> = trig.keys().map(|x| std::str::from_utf8(x).unwrap_or("utf8 err")).collect(); | |
//println!("{:?}", rez); | |
//let data: Arc<RwLock<Vec<u9>>> = Arc::new(RwLock::new(dat)); | |
let trigram: Arc<Trigs> = Arc::new(trig); | |
// Webview to return trigram count | |
// let datastore_filter = warp::any().map(|| trig); | |
//let data_filter = warp::any().map(move || data.clone()); | |
let hello = warp::path!("hello" / String).map(move |q: String| { | |
let tgm = Arc::clone(&trigram); | |
// TODO: Set this as the last three tokens rather than first three | |
let k = q[..3].as_bytes(); | |
println!("Running query {}", &q[..3]); | |
let r = tgm.get(k).unwrap_or(&[1u32; 256]); | |
let keys: HashMap<char, u32> = r | |
.iter() | |
.enumerate() | |
.filter(|(_, count)| **count != 0) | |
.map(|(idx, count)| ((idx as u8) as char, *count)) | |
.collect(); | |
// warp::reply::json(&r.to_vec()) | |
warp::reply::json(&keys) | |
}); | |
warp::serve(hello).run(([127, 0, 0, 1], 3030)).await; | |
} | |
fn get_data(data: &Vec<u8>) -> HashMap<&[u8], [u32; 256]> { | |
// Set up the data structures | |
let mut trig: HashMap<&[u8], [u32; 256]> = HashMap::new(); | |
// Convert the data to trigrams | |
for w in data.windows(4).into_iter() { | |
let k = &w[..3]; | |
let v = w[3]; | |
let counter = trig.entry(k).or_insert([0; 256]); | |
counter[v as usize] += 1; | |
} | |
trig | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment