Descent-based create/update mechanism

This commit is contained in:
Lucien Greathouse
2018-12-14 23:34:31 -08:00
parent f125814847
commit 5707b8c7e8
4 changed files with 48 additions and 12 deletions

View File

@@ -105,6 +105,16 @@ impl Imfs {
Ok(())
}
pub fn get_root_for_path<'a>(&'a self, path: &Path) -> Option<&'a Path> {
for root_path in &self.roots {
if path.starts_with(root_path) {
return Some(root_path);
}
}
None
}
fn remove_item(&mut self, path: &Path) {
if let Some(ImfsItem::Directory(directory)) = self.items.remove(path) {
for child_path in &directory.children {

View File

@@ -1,5 +1,5 @@
use std::{
path::{Path, PathBuf},
path::{self, Path, PathBuf},
collections::{HashMap, HashSet},
};
@@ -70,4 +70,27 @@ impl<T> PathMap<T> {
Some(root_value)
}
pub fn descend(&self, start_path: &Path, target_path: &Path) -> PathBuf {
let relative_path = target_path.strip_prefix(start_path)
.expect("target_path did not begin with start_path");
let mut current_path = start_path.to_path_buf();
for component in relative_path.components() {
match component {
path::Component::Normal(name) => {
let next_path = current_path.join(name);
if self.nodes.contains_key(&next_path) {
current_path = next_path;
} else {
return current_path;
}
},
_ => unreachable!(),
}
}
current_path
}
}

View File

@@ -13,7 +13,7 @@ use crate::{
message_queue::{Message, MessageQueue},
imfs::{Imfs, ImfsItem, ImfsFile},
path_map::PathMap,
rbx_snapshot::{RbxSnapshotInstance, reify_root},
rbx_snapshot::{RbxSnapshotInstance, reify_root, reconcile_subtree},
};
pub struct RbxSession {
@@ -47,13 +47,16 @@ impl RbxSession {
}
fn path_created_or_updated(&mut self, path: &Path) {
if let Some(instance_id) = self.paths_to_node_ids.get(path) {
// TODO: Replace instance with ID `instance_id` with new instance
}
let imfs = self.imfs.lock().unwrap();
let root_path = imfs.get_root_for_path(path)
.expect("Path was outside in-memory filesystem roots");
// TODO: Crawl up path to find first node represented in the tree or a
// sync point root. That path immediately before we find an existing
// node should be read into the tree.
let closest_path = self.paths_to_node_ids.descend(root_path, path);
let &instance_id = self.paths_to_node_ids.get(&closest_path).unwrap();
let snapshot = snapshot_instances_from_imfs(&imfs, &closest_path)
.expect("Could not generate instance snapshot");
reconcile_subtree(&mut self.tree, instance_id, &snapshot);
}
pub fn path_created(&mut self, path: &Path) {
@@ -167,7 +170,7 @@ fn construct_instance_node<'a>(
name: Cow::Borrowed(instance_name),
properties: HashMap::new(),
children,
update_trigger_paths: Vec::new(),
source_path: None,
}
}
@@ -225,7 +228,7 @@ fn snapshot_instances_from_imfs<'a>(imfs: &'a Imfs, imfs_path: &Path) -> Option<
class_name: Cow::Borrowed(class_name),
properties,
children: Vec::new(),
update_trigger_paths: vec![file.path.clone()],
source_path: Some(file.path.clone()),
})
},
ImfsItem::Directory(directory) => {
@@ -239,7 +242,7 @@ fn snapshot_instances_from_imfs<'a>(imfs: &'a Imfs, imfs_path: &Path) -> Option<
name: Cow::Borrowed(""), // Assigned later in the method
properties: HashMap::new(),
children: Vec::new(),
update_trigger_paths: vec![directory.path.clone()],
source_path: Some(directory.path.clone()),
}
};

View File

@@ -12,7 +12,7 @@ pub struct RbxSnapshotInstance<'a> {
pub class_name: Cow<'a, str>,
pub properties: HashMap<String, RbxValue>,
pub children: Vec<RbxSnapshotInstance<'a>>,
pub update_trigger_paths: Vec<PathBuf>,
pub source_path: Option<PathBuf>,
}
fn reify_core(snapshot: &RbxSnapshotInstance) -> RbxInstance {