vfs crate scaffold

This commit is contained in:
Aleksey Kladov 2018-12-18 13:18:55 +03:00
parent 7509901fa0
commit 2ae05a6163
6 changed files with 89 additions and 68 deletions

2
Cargo.lock generated
View file

@ -749,8 +749,10 @@ name = "ra_vfs"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"crossbeam-channel 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)", "crossbeam-channel 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)",
"log 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
"relative-path 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "relative-path 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
"rustc-hash 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", "rustc-hash 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)",
"thread_worker 0.1.0",
"walkdir 2.2.7 (registry+https://github.com/rust-lang/crates.io-index)", "walkdir 2.2.7 (registry+https://github.com/rust-lang/crates.io-index)",
] ]

View file

@ -88,8 +88,8 @@ pub fn main_loop(
drop(pool); drop(pool);
log::info!("...threadpool has finished"); log::info!("...threadpool has finished");
let fs_res = fs_watcher.stop(); let fs_res = fs_watcher.shutdown();
let ws_res = ws_watcher.stop(); let ws_res = ws_watcher.shutdown();
main_res?; main_res?;
fs_res.map_err(|_| format_err!("fs watcher died"))?; fs_res.map_err(|_| format_err!("fs watcher died"))?;

View file

@ -9,3 +9,6 @@ walkdir = "2.2.7"
relative-path = "0.4.0" relative-path = "0.4.0"
rustc-hash = "1.0" rustc-hash = "1.0"
crossbeam-channel = "0.2.4" crossbeam-channel = "0.2.4"
log = "0.4.6"
thread_worker = { path = "../thread_worker" }

View file

@ -6,67 +6,64 @@ use std::{
use walkdir::WalkDir; use walkdir::WalkDir;
use crossbeam_channel::{Sender, Receiver}; use crossbeam_channel::{Sender, Receiver};
use thread_worker::{WorkerHandle, Worker};
pub(crate) fn start_io() -> (JoinHandle<(), Sender<()>, Receiver()>) {} #[derive(Debug)]
pub struct FileEvent {
pub path: PathBuf,
pub kind: FileEventKind,
}
// use crate::thread_watcher::{ThreadWatcher, Worker}; #[derive(Debug)]
pub enum FileEventKind {
Add(String),
}
// #[derive(Debug)] pub fn start() -> (Worker<PathBuf, (PathBuf, Vec<FileEvent>)>, WorkerHandle) {
// pub struct FileEvent { thread_worker::spawn::<PathBuf, (PathBuf, Vec<FileEvent>), _>(
// pub path: PathBuf, "vfs",
// pub kind: FileEventKind, 128,
// } |input_receiver, output_sender| {
input_receiver
.map(|path| {
log::debug!("loading {} ...", path.as_path().display());
let events = load_root(path.as_path());
log::debug!("... loaded {}", path.as_path().display());
(path, events)
})
.for_each(|it| output_sender.send(it))
},
)
}
// #[derive(Debug)] fn load_root(path: &Path) -> Vec<FileEvent> {
// pub enum FileEventKind { let mut res = Vec::new();
// Add(String), for entry in WalkDir::new(path) {
// } let entry = match entry {
Ok(entry) => entry,
// pub fn roots_loader() -> (Worker<PathBuf, (PathBuf, Vec<FileEvent>)>, ThreadWatcher) { Err(e) => {
// Worker::<PathBuf, (PathBuf, Vec<FileEvent>)>::spawn( log::warn!("watcher error: {}", e);
// "roots loader", continue;
// 128, }
// |input_receiver, output_sender| { };
// input_receiver if !entry.file_type().is_file() {
// .map(|path| { continue;
// log::debug!("loading {} ...", path.as_path().display()); }
// let events = load_root(path.as_path()); let path = entry.path();
// log::debug!("... loaded {}", path.as_path().display()); if path.extension().and_then(|os| os.to_str()) != Some("rs") {
// (path, events) continue;
// }) }
// .for_each(|it| output_sender.send(it)) let text = match fs::read_to_string(path) {
// }, Ok(text) => text,
// ) Err(e) => {
// } log::warn!("watcher error: {}", e);
continue;
// fn load_root(path: &Path) -> Vec<FileEvent> { }
// let mut res = Vec::new(); };
// for entry in WalkDir::new(path) { res.push(FileEvent {
// let entry = match entry { path: path.to_owned(),
// Ok(entry) => entry, kind: FileEventKind::Add(text),
// Err(e) => { })
// log::warn!("watcher error: {}", e); }
// continue; res
// } }
// };
// if !entry.file_type().is_file() {
// continue;
// }
// let path = entry.path();
// if path.extension().and_then(|os| os.to_str()) != Some("rs") {
// continue;
// }
// let text = match fs::read_to_string(path) {
// Ok(text) => text,
// Err(e) => {
// log::warn!("watcher error: {}", e);
// continue;
// }
// };
// res.push(FileEvent {
// path: path.to_owned(),
// kind: FileEventKind::Add(text),
// })
// }
// res
// }

View file

@ -15,6 +15,7 @@ mod arena;
mod io; mod io;
use std::{ use std::{
thread,
cmp::Reverse, cmp::Reverse,
path::{Path, PathBuf}, path::{Path, PathBuf},
ffi::OsStr, ffi::OsStr,
@ -22,7 +23,12 @@ use std::{
}; };
use relative_path::RelativePathBuf; use relative_path::RelativePathBuf;
use crate::arena::{ArenaId, Arena}; use thread_worker::{WorkerHandle, Worker};
use crate::{
arena::{ArenaId, Arena},
io::FileEvent,
};
/// `RootFilter` is a predicate that checks if a file can belong to a root /// `RootFilter` is a predicate that checks if a file can belong to a root
struct RootFilter { struct RootFilter {
@ -76,16 +82,24 @@ struct VfsFileData {
text: Arc<String>, text: Arc<String>,
} }
#[derive(Default)]
struct Vfs { struct Vfs {
roots: Arena<VfsRoot, RootFilter>, roots: Arena<VfsRoot, RootFilter>,
files: Arena<VfsFile, VfsFileData>, files: Arena<VfsFile, VfsFileData>,
// pending_changes: Vec<PendingChange>, // pending_changes: Vec<PendingChange>,
worker: Worker<PathBuf, (PathBuf, Vec<FileEvent>)>,
worker_handle: WorkerHandle,
} }
impl Vfs { impl Vfs {
pub fn new(mut roots: Vec<PathBuf>) -> Vfs { pub fn new(mut roots: Vec<PathBuf>) -> Vfs {
let mut res = Vfs::default(); let (worker, worker_handle) = io::start();
let mut res = Vfs {
roots: Arena::default(),
files: Arena::default(),
worker,
worker_handle,
};
roots.sort_by_key(|it| Reverse(it.as_os_str().len())); roots.sort_by_key(|it| Reverse(it.as_os_str().len()));
@ -104,6 +118,11 @@ impl Vfs {
pub fn commit_changes(&mut self) -> Vec<VfsChange> { pub fn commit_changes(&mut self) -> Vec<VfsChange> {
unimplemented!() unimplemented!()
} }
pub fn shutdown(self) -> thread::Result<()> {
let _ = self.worker.shutdown();
self.worker_handle.shutdown()
}
} }
#[derive(Debug, Clone)] #[derive(Debug, Clone)]

View file

@ -30,7 +30,7 @@ where
impl<I, O> Worker<I, O> { impl<I, O> Worker<I, O> {
/// Stops the worker. Returns the message receiver to fetch results which /// Stops the worker. Returns the message receiver to fetch results which
/// have become ready before the worker is stopped. /// have become ready before the worker is stopped.
pub fn stop(self) -> Receiver<O> { pub fn shutdown(self) -> Receiver<O> {
self.out self.out
} }
@ -45,11 +45,11 @@ impl WorkerHandle {
WorkerHandle { WorkerHandle {
name, name,
thread, thread,
bomb: DropBomb::new(format!("WorkerHandle {} was not stopped", name)), bomb: DropBomb::new(format!("WorkerHandle {} was not shutdown", name)),
} }
} }
pub fn stop(mut self) -> thread::Result<()> { pub fn shutdown(mut self) -> thread::Result<()> {
log::info!("waiting for {} to finish ...", self.name); log::info!("waiting for {} to finish ...", self.name);
let name = self.name; let name = self.name;
self.bomb.defuse(); self.bomb.defuse();