mirror of
https://github.com/rojo-rbx/rojo.git
synced 2026-04-23 22:25:26 +00:00
Break out PathMap from RbxSession
This commit is contained in:
@@ -10,11 +10,12 @@ extern crate tempfile;
|
|||||||
// pub mod roblox_studio;
|
// pub mod roblox_studio;
|
||||||
pub mod commands;
|
pub mod commands;
|
||||||
pub mod fs_watcher;
|
pub mod fs_watcher;
|
||||||
|
pub mod imfs;
|
||||||
pub mod message_queue;
|
pub mod message_queue;
|
||||||
|
pub mod path_map;
|
||||||
pub mod project;
|
pub mod project;
|
||||||
pub mod rbx_session;
|
pub mod rbx_session;
|
||||||
pub mod session;
|
pub mod session;
|
||||||
pub mod session_id;
|
pub mod session_id;
|
||||||
pub mod imfs;
|
|
||||||
pub mod web;
|
pub mod web;
|
||||||
pub mod web_util;
|
pub mod web_util;
|
||||||
73
server/src/path_map.rs
Normal file
73
server/src/path_map.rs
Normal file
@@ -0,0 +1,73 @@
|
|||||||
|
use std::{
|
||||||
|
path::{Path, PathBuf},
|
||||||
|
collections::{HashMap, HashSet},
|
||||||
|
};
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
struct PathMapNode<T> {
|
||||||
|
value: T,
|
||||||
|
children: HashSet<PathBuf>,
|
||||||
|
}
|
||||||
|
|
||||||
|
/// A map from paths to instance IDs, with a bit of additional data that enables
|
||||||
|
/// removing a path and all of its child paths from the tree in constant time.
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct PathMap<T> {
|
||||||
|
nodes: HashMap<PathBuf, PathMapNode<T>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T> PathMap<T> {
|
||||||
|
pub fn new() -> PathMap<T> {
|
||||||
|
PathMap {
|
||||||
|
nodes: HashMap::new(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get(&self, path: &Path) -> Option<&T> {
|
||||||
|
self.nodes.get(path).map(|v| &v.value)
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn insert(&mut self, path: PathBuf, value: T) {
|
||||||
|
if let Some(parent_path) = path.parent() {
|
||||||
|
if let Some(parent) = self.nodes.get_mut(parent_path) {
|
||||||
|
parent.children.insert(path.to_path_buf());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
self.nodes.insert(path, PathMapNode {
|
||||||
|
value,
|
||||||
|
children: HashSet::new(),
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn remove(&mut self, root_path: &Path) -> Option<T> {
|
||||||
|
if let Some(parent_path) = root_path.parent() {
|
||||||
|
if let Some(parent) = self.nodes.get_mut(parent_path) {
|
||||||
|
parent.children.remove(root_path);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut root_node = match self.nodes.remove(root_path) {
|
||||||
|
Some(node) => node,
|
||||||
|
None => return None,
|
||||||
|
};
|
||||||
|
|
||||||
|
let root_value = root_node.value;
|
||||||
|
let mut to_visit: Vec<PathBuf> = root_node.children.drain().collect();
|
||||||
|
|
||||||
|
while let Some(path) = to_visit.pop() {
|
||||||
|
match self.nodes.remove(&path) {
|
||||||
|
Some(mut node) => {
|
||||||
|
for child in node.children.drain() {
|
||||||
|
to_visit.push(child);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
None => {
|
||||||
|
warn!("Consistency issue; tried to remove {} but it was already removed", path.display());
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Some(root_value)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
use std::{
|
use std::{
|
||||||
collections::{HashMap, HashSet},
|
collections::HashMap,
|
||||||
path::{Path, PathBuf},
|
path::Path,
|
||||||
sync::{Arc, Mutex},
|
sync::{Arc, Mutex},
|
||||||
str,
|
str,
|
||||||
};
|
};
|
||||||
@@ -11,80 +11,12 @@ use crate::{
|
|||||||
project::{Project, ProjectNode, InstanceProjectNode},
|
project::{Project, ProjectNode, InstanceProjectNode},
|
||||||
message_queue::{Message, MessageQueue},
|
message_queue::{Message, MessageQueue},
|
||||||
imfs::{Imfs, ImfsItem, ImfsFile},
|
imfs::{Imfs, ImfsItem, ImfsFile},
|
||||||
|
path_map::PathMap,
|
||||||
};
|
};
|
||||||
|
|
||||||
#[derive(Debug)]
|
|
||||||
struct PathIdNode {
|
|
||||||
id: RbxId,
|
|
||||||
children: HashSet<PathBuf>,
|
|
||||||
}
|
|
||||||
|
|
||||||
/// A map from paths to instance IDs, with a bit of additional data that enables
|
|
||||||
/// removing a path and all of its child paths from the tree in constant time.
|
|
||||||
#[derive(Debug)]
|
|
||||||
struct PathIdTree {
|
|
||||||
nodes: HashMap<PathBuf, PathIdNode>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl PathIdTree {
|
|
||||||
pub fn new() -> PathIdTree {
|
|
||||||
PathIdTree {
|
|
||||||
nodes: HashMap::new(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn get_id(&self, path: &Path) -> Option<RbxId> {
|
|
||||||
self.nodes.get(path).map(|v| v.id)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn insert(&mut self, path: &Path, id: RbxId) {
|
|
||||||
if let Some(parent_path) = path.parent() {
|
|
||||||
if let Some(parent) = self.nodes.get_mut(parent_path) {
|
|
||||||
parent.children.insert(path.to_path_buf());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
self.nodes.insert(path.to_path_buf(), PathIdNode {
|
|
||||||
id,
|
|
||||||
children: HashSet::new(),
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn remove(&mut self, root_path: &Path) -> Option<RbxId> {
|
|
||||||
if let Some(parent_path) = root_path.parent() {
|
|
||||||
if let Some(parent) = self.nodes.get_mut(parent_path) {
|
|
||||||
parent.children.remove(root_path);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut root_node = match self.nodes.remove(root_path) {
|
|
||||||
Some(node) => node,
|
|
||||||
None => return None,
|
|
||||||
};
|
|
||||||
|
|
||||||
let root_id = root_node.id;
|
|
||||||
let mut to_visit: Vec<PathBuf> = root_node.children.drain().collect();
|
|
||||||
|
|
||||||
while let Some(path) = to_visit.pop() {
|
|
||||||
match self.nodes.remove(&path) {
|
|
||||||
Some(mut node) => {
|
|
||||||
for child in node.children.drain() {
|
|
||||||
to_visit.push(child);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
None => {
|
|
||||||
warn!("Consistency issue; tried to remove {} but it was already removed", path.display());
|
|
||||||
},
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Some(root_id)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct RbxSession {
|
pub struct RbxSession {
|
||||||
tree: RbxTree,
|
tree: RbxTree,
|
||||||
path_id_tree: PathIdTree,
|
path_id_tree: PathMap<RbxId>,
|
||||||
ids_to_project_paths: HashMap<RbxId, String>,
|
ids_to_project_paths: HashMap<RbxId, String>,
|
||||||
message_queue: Arc<MessageQueue>,
|
message_queue: Arc<MessageQueue>,
|
||||||
imfs: Arc<Mutex<Imfs>>,
|
imfs: Arc<Mutex<Imfs>>,
|
||||||
@@ -177,15 +109,15 @@ pub fn construct_oneoff_tree(project: &Project, imfs: &Imfs) -> RbxTree {
|
|||||||
struct ConstructContext<'a> {
|
struct ConstructContext<'a> {
|
||||||
tree: Option<RbxTree>,
|
tree: Option<RbxTree>,
|
||||||
imfs: &'a Imfs,
|
imfs: &'a Imfs,
|
||||||
path_id_tree: PathIdTree,
|
path_id_tree: PathMap<RbxId>,
|
||||||
ids_to_project_paths: HashMap<RbxId, String>,
|
ids_to_project_paths: HashMap<RbxId, String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn construct_initial_tree(
|
fn construct_initial_tree(
|
||||||
project: &Project,
|
project: &Project,
|
||||||
imfs: &Imfs,
|
imfs: &Imfs,
|
||||||
) -> (RbxTree, PathIdTree, HashMap<RbxId, String>) {
|
) -> (RbxTree, PathMap<RbxId>, HashMap<RbxId, String>) {
|
||||||
let path_id_tree = PathIdTree::new();
|
let path_id_tree = PathMap::new();
|
||||||
let ids_to_project_paths = HashMap::new();
|
let ids_to_project_paths = HashMap::new();
|
||||||
|
|
||||||
let mut context = ConstructContext {
|
let mut context = ConstructContext {
|
||||||
@@ -320,7 +252,7 @@ fn construct_sync_point_node(
|
|||||||
|
|
||||||
let id = insert_or_create_tree(context, parent_instance_id, instance);
|
let id = insert_or_create_tree(context, parent_instance_id, instance);
|
||||||
|
|
||||||
context.path_id_tree.insert(&file.path, id);
|
context.path_id_tree.insert(file.path.clone(), id);
|
||||||
|
|
||||||
id
|
id
|
||||||
},
|
},
|
||||||
@@ -337,7 +269,7 @@ fn construct_sync_point_node(
|
|||||||
};
|
};
|
||||||
|
|
||||||
let id = insert_or_create_tree(context, parent_instance_id, instance);
|
let id = insert_or_create_tree(context, parent_instance_id, instance);
|
||||||
context.path_id_tree.insert(&directory.path, id);
|
context.path_id_tree.insert(directory.path.clone(), id);
|
||||||
id
|
id
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user