Metadata per instance is a go

This commit is contained in:
Lucien Greathouse 2019-01-17 15:33:17 -08:00
parent f2dc1d8898
commit 3f526cd127
4 changed files with 65 additions and 48 deletions

View file

@ -1,7 +1,7 @@
use std::{ use std::{
borrow::Cow, borrow::Cow,
collections::HashMap, collections::HashMap,
path::Path, path::{Path, PathBuf},
str, str,
sync::{Arc, Mutex}, sync::{Arc, Mutex},
}; };
@ -23,17 +23,17 @@ const INIT_SCRIPT: &str = "init.lua";
const INIT_SERVER_SCRIPT: &str = "init.server.lua"; const INIT_SERVER_SCRIPT: &str = "init.server.lua";
const INIT_CLIENT_SCRIPT: &str = "init.client.lua"; const INIT_CLIENT_SCRIPT: &str = "init.client.lua";
#[derive(Debug, Default, Serialize, Deserialize)] #[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct MetadataPerPath { pub struct MetadataPerPath {
pub instance_id: Option<RbxId>, pub instance_id: Option<RbxId>,
pub instance_name: Option<String>, pub instance_name: Option<String>,
} }
// #[derive(Debug, Default, Serialize, Deserialize)] #[derive(Debug, Clone, Default, Serialize, Deserialize)]
// pub struct MetadataPerInstance { pub struct MetadataPerInstance {
// pub source_path: Option<PathBuf>, pub source_path: Option<PathBuf>,
// pub ignore_unknown_instances: bool, pub ignore_unknown_instances: bool,
// } }
pub struct RbxSession { pub struct RbxSession {
tree: RbxTree, tree: RbxTree,
@ -45,7 +45,7 @@ pub struct RbxSession {
// TODO: Hold this map, inverted from metadata_per_path // TODO: Hold this map, inverted from metadata_per_path
// metadata_per_instance: PathMap<MetadataPerInstance>, // metadata_per_instance: PathMap<MetadataPerInstance>,
instance_metadata_map: HashMap<RbxId, InstanceProjectNodeMetadata>, instance_metadata_map: HashMap<RbxId, MetadataPerInstance>,
message_queue: Arc<MessageQueue<InstanceChanges>>, message_queue: Arc<MessageQueue<InstanceChanges>>,
imfs: Arc<Mutex<Imfs>>, imfs: Arc<Mutex<Imfs>>,
} }
@ -182,7 +182,7 @@ impl RbxSession {
&self.tree &self.tree
} }
pub fn get_instance_metadata(&self, id: RbxId) -> Option<&InstanceProjectNodeMetadata> { pub fn get_instance_metadata(&self, id: RbxId) -> Option<&MetadataPerInstance> {
self.instance_metadata_map.get(&id) self.instance_metadata_map.get(&id)
} }
@ -201,7 +201,7 @@ fn reify_initial_tree(
project: &Project, project: &Project,
imfs: &Imfs, imfs: &Imfs,
metadata_per_path: &mut PathMap<MetadataPerPath>, metadata_per_path: &mut PathMap<MetadataPerPath>,
instance_metadata_map: &mut HashMap<RbxId, InstanceProjectNodeMetadata>, instance_metadata_map: &mut HashMap<RbxId, MetadataPerInstance>,
) -> RbxTree { ) -> RbxTree {
let mut meta = SnapshotMetadata { let mut meta = SnapshotMetadata {
metadata_per_path, metadata_per_path,

View file

@ -30,7 +30,7 @@ use crate::{
}, },
path_map::PathMap, path_map::PathMap,
// TODO: Move MetadataPerPath into this module? // TODO: Move MetadataPerPath into this module?
rbx_session::MetadataPerPath, rbx_session::{MetadataPerPath, MetadataPerInstance},
}; };
const INIT_MODULE_NAME: &str = "init.lua"; const INIT_MODULE_NAME: &str = "init.lua";
@ -120,8 +120,10 @@ fn snapshot_instance_node<'source>(
name: instance_name, name: instance_name,
properties: node.properties.clone(), properties: node.properties.clone(),
children, children,
source_path: None, metadata: MetadataPerInstance {
metadata: Some(node.metadata.clone()), source_path: None,
ignore_unknown_instances: node.metadata.ignore_unknown_instances,
},
})) }))
} }
@ -202,8 +204,10 @@ fn snapshot_imfs_directory<'source>(
name: snapshot_name, name: snapshot_name,
properties: HashMap::new(), properties: HashMap::new(),
children: Vec::new(), children: Vec::new(),
source_path: Some(directory.path.to_owned()), metadata: MetadataPerInstance {
metadata: None, source_path: Some(directory.path.to_owned()),
ignore_unknown_instances: false,
},
} }
}; };
@ -285,8 +289,10 @@ fn snapshot_lua_file<'source>(
}, },
}, },
children: Vec::new(), children: Vec::new(),
metadata: None, metadata: MetadataPerInstance {
source_path: Some(file.path.to_path_buf()), source_path: Some(file.path.to_path_buf()),
ignore_unknown_instances: false,
},
})) }))
} }
@ -321,8 +327,10 @@ fn snapshot_txt_file<'source>(
}, },
}, },
children: Vec::new(), children: Vec::new(),
metadata: None, metadata: MetadataPerInstance {
source_path: Some(file.path.to_path_buf()), source_path: Some(file.path.to_path_buf()),
ignore_unknown_instances: false,
},
})) }))
} }
@ -352,8 +360,10 @@ fn snapshot_csv_file<'source>(
}, },
}, },
children: Vec::new(), children: Vec::new(),
metadata: None, metadata: MetadataPerInstance {
source_path: Some(file.path.to_path_buf()), source_path: Some(file.path.to_path_buf()),
ignore_unknown_instances: false,
},
})) }))
} }

View file

@ -11,8 +11,7 @@ use serde_derive::{Serialize, Deserialize};
use crate::{ use crate::{
path_map::PathMap, path_map::PathMap,
rbx_session::MetadataPerPath, rbx_session::{MetadataPerPath, MetadataPerInstance},
project::InstanceProjectNodeMetadata,
}; };
#[derive(Debug, Clone, Default, Serialize, Deserialize)] #[derive(Debug, Clone, Default, Serialize, Deserialize)]
@ -63,10 +62,7 @@ pub struct RbxSnapshotInstance<'a> {
pub class_name: Cow<'a, str>, pub class_name: Cow<'a, str>,
pub properties: HashMap<String, RbxValue>, pub properties: HashMap<String, RbxValue>,
pub children: Vec<RbxSnapshotInstance<'a>>, pub children: Vec<RbxSnapshotInstance<'a>>,
pub metadata: MetadataPerInstance,
// TODO: Replace source_path and metadata with MetadataPerInstance
pub source_path: Option<PathBuf>,
pub metadata: Option<InstanceProjectNodeMetadata>,
} }
pub fn snapshot_from_tree(tree: &RbxTree, id: RbxId) -> Option<RbxSnapshotInstance<'static>> { pub fn snapshot_from_tree(tree: &RbxTree, id: RbxId) -> Option<RbxSnapshotInstance<'static>> {
@ -82,29 +78,29 @@ pub fn snapshot_from_tree(tree: &RbxTree, id: RbxId) -> Option<RbxSnapshotInstan
class_name: Cow::Owned(instance.class_name.to_owned()), class_name: Cow::Owned(instance.class_name.to_owned()),
properties: instance.properties.clone(), properties: instance.properties.clone(),
children, children,
source_path: None, metadata: MetadataPerInstance {
metadata: None, source_path: None,
ignore_unknown_instances: false,
},
}) })
} }
pub fn reify_root( pub fn reify_root(
snapshot: &RbxSnapshotInstance, snapshot: &RbxSnapshotInstance,
metadata_per_path: &mut PathMap<MetadataPerPath>, metadata_per_path: &mut PathMap<MetadataPerPath>,
instance_metadata_map: &mut HashMap<RbxId, InstanceProjectNodeMetadata>, instance_metadata_map: &mut HashMap<RbxId, MetadataPerInstance>,
changes: &mut InstanceChanges, changes: &mut InstanceChanges,
) -> RbxTree { ) -> RbxTree {
let instance = reify_core(snapshot); let instance = reify_core(snapshot);
let mut tree = RbxTree::new(instance); let mut tree = RbxTree::new(instance);
let root_id = tree.get_root_id(); let root_id = tree.get_root_id();
if let Some(source_path) = &snapshot.source_path { if let Some(source_path) = &snapshot.metadata.source_path {
let path_meta = metadata_per_path.entry(source_path.to_owned()).or_default(); let path_meta = metadata_per_path.entry(source_path.to_owned()).or_default();
path_meta.instance_id = Some(root_id); path_meta.instance_id = Some(root_id);
} }
if let Some(metadata) = &snapshot.metadata { instance_metadata_map.insert(root_id, snapshot.metadata.clone());
instance_metadata_map.insert(root_id, metadata.clone());
}
changes.added.insert(root_id); changes.added.insert(root_id);
@ -120,20 +116,18 @@ pub fn reify_subtree(
tree: &mut RbxTree, tree: &mut RbxTree,
parent_id: RbxId, parent_id: RbxId,
metadata_per_path: &mut PathMap<MetadataPerPath>, metadata_per_path: &mut PathMap<MetadataPerPath>,
instance_metadata_map: &mut HashMap<RbxId, InstanceProjectNodeMetadata>, instance_metadata_map: &mut HashMap<RbxId, MetadataPerInstance>,
changes: &mut InstanceChanges, changes: &mut InstanceChanges,
) { ) {
let instance = reify_core(snapshot); let instance = reify_core(snapshot);
let id = tree.insert_instance(instance, parent_id); let id = tree.insert_instance(instance, parent_id);
if let Some(source_path) = &snapshot.source_path { if let Some(source_path) = &snapshot.metadata.source_path {
let path_meta = metadata_per_path.entry(source_path.clone()).or_default(); let path_meta = metadata_per_path.entry(source_path.clone()).or_default();
path_meta.instance_id = Some(id); path_meta.instance_id = Some(id);
} }
if let Some(metadata) = &snapshot.metadata { instance_metadata_map.insert(id, snapshot.metadata.clone());
instance_metadata_map.insert(id, metadata.clone());
}
changes.added.insert(id); changes.added.insert(id);
@ -147,17 +141,15 @@ pub fn reconcile_subtree(
id: RbxId, id: RbxId,
snapshot: &RbxSnapshotInstance, snapshot: &RbxSnapshotInstance,
metadata_per_path: &mut PathMap<MetadataPerPath>, metadata_per_path: &mut PathMap<MetadataPerPath>,
instance_metadata_map: &mut HashMap<RbxId, InstanceProjectNodeMetadata>, instance_metadata_map: &mut HashMap<RbxId, MetadataPerInstance>,
changes: &mut InstanceChanges, changes: &mut InstanceChanges,
) { ) {
if let Some(source_path) = &snapshot.source_path { if let Some(source_path) = &snapshot.metadata.source_path {
let path_meta = metadata_per_path.entry(source_path.to_owned()).or_default(); let path_meta = metadata_per_path.entry(source_path.to_owned()).or_default();
path_meta.instance_id = Some(id); path_meta.instance_id = Some(id);
} }
if let Some(metadata) = &snapshot.metadata { instance_metadata_map.insert(id, snapshot.metadata.clone());
instance_metadata_map.insert(id, metadata.clone());
}
if reconcile_instance_properties(tree.get_instance_mut(id).unwrap(), snapshot) { if reconcile_instance_properties(tree.get_instance_mut(id).unwrap(), snapshot) {
changes.updated.insert(id); changes.updated.insert(id);
@ -244,7 +236,7 @@ fn reconcile_instance_children(
id: RbxId, id: RbxId,
snapshot: &RbxSnapshotInstance, snapshot: &RbxSnapshotInstance,
metadata_per_path: &mut PathMap<MetadataPerPath>, metadata_per_path: &mut PathMap<MetadataPerPath>,
instance_metadata_map: &mut HashMap<RbxId, InstanceProjectNodeMetadata>, instance_metadata_map: &mut HashMap<RbxId, MetadataPerInstance>,
changes: &mut InstanceChanges, changes: &mut InstanceChanges,
) { ) {
let mut visited_snapshot_indices = HashSet::new(); let mut visited_snapshot_indices = HashSet::new();

View file

@ -20,8 +20,23 @@ use crate::{
project::InstanceProjectNodeMetadata, project::InstanceProjectNodeMetadata,
snapshot_reconciler::InstanceChanges, snapshot_reconciler::InstanceChanges,
visualize::{VisualizeRbxSession, VisualizeImfs, graphviz_to_svg}, visualize::{VisualizeRbxSession, VisualizeImfs, graphviz_to_svg},
rbx_session::{MetadataPerInstance},
}; };
/// Contains the instance metadata relevant to Rojo clients.
#[derive(Debug, Serialize, Deserialize)]
pub struct InstanceMetadata {
ignore_unknown_instances: bool,
}
impl InstanceMetadata {
fn from_session_metadata(meta: &MetadataPerInstance) -> InstanceMetadata {
InstanceMetadata {
ignore_unknown_instances: meta.ignore_unknown_instances,
}
}
}
/// Used to attach metadata specific to Rojo to instances, which come from the /// Used to attach metadata specific to Rojo to instances, which come from the
/// rbx_tree crate. /// rbx_tree crate.
/// ///
@ -33,7 +48,7 @@ pub struct InstanceWithMetadata<'a> {
pub instance: Cow<'a, RbxInstance>, pub instance: Cow<'a, RbxInstance>,
#[serde(rename = "Metadata")] #[serde(rename = "Metadata")]
pub metadata: Option<Cow<'a, InstanceProjectNodeMetadata>>, pub metadata: Option<InstanceMetadata>,
} }
#[derive(Debug, Serialize, Deserialize)] #[derive(Debug, Serialize, Deserialize)]
@ -163,7 +178,7 @@ impl Server {
for &requested_id in &requested_ids { for &requested_id in &requested_ids {
if let Some(instance) = tree.get_instance(requested_id) { if let Some(instance) = tree.get_instance(requested_id) {
let metadata = rbx_session.get_instance_metadata(requested_id) let metadata = rbx_session.get_instance_metadata(requested_id)
.map(Cow::Borrowed); .map(InstanceMetadata::from_session_metadata);
instances.insert(instance.get_id(), InstanceWithMetadata { instances.insert(instance.get_id(), InstanceWithMetadata {
instance: Cow::Borrowed(instance), instance: Cow::Borrowed(instance),
@ -172,7 +187,7 @@ impl Server {
for descendant in tree.descendants(requested_id) { for descendant in tree.descendants(requested_id) {
let descendant_meta = rbx_session.get_instance_metadata(descendant.get_id()) let descendant_meta = rbx_session.get_instance_metadata(descendant.get_id())
.map(Cow::Borrowed); .map(InstanceMetadata::from_session_metadata);
instances.insert(descendant.get_id(), InstanceWithMetadata { instances.insert(descendant.get_id(), InstanceWithMetadata {
instance: Cow::Borrowed(descendant), instance: Cow::Borrowed(descendant),