Skip to content

Commit

Permalink
Remove unnecessary API, improve two-way sync instance writing
Browse files Browse the repository at this point in the history
  • Loading branch information
DervexDev committed Apr 18, 2024
1 parent 7340904 commit a1d3fcf
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 83 deletions.
6 changes: 1 addition & 5 deletions src/core/processor/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::{
time::{Duration, Instant},
};

use super::{changes::Changes, queue::Queue, snapshot::Snapshot, tree::Tree};
use super::{changes::Changes, queue::Queue, tree::Tree};
use crate::{
argon_error,
constants::{BLACKLISTED_PATHS, CHANGES_TRESHOLD},
Expand Down Expand Up @@ -67,10 +67,6 @@ impl Processor {
pub fn write(&self, changes: Changes) {
self.writer.send(changes).unwrap();
}

pub fn write_all(&self, _snapshot: Snapshot) {
// TODO
}
}

struct Handler {
Expand Down
61 changes: 50 additions & 11 deletions src/core/processor/write.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use anyhow::{anyhow, Context as AnyhowContext, Result};
use colored::Colorize;
use log::{error, trace, warn};
use rbx_dom_weak::{types::Ref, Instance};
use std::{
Expand All @@ -7,6 +8,7 @@ use std::{
};

use crate::{
argon_error,
core::{
meta::{Meta, NodePath, Source, SourceEntry, SourceKind},
snapshot::{AddedSnapshot, Snapshot, UpdatedSnapshot},
Expand All @@ -20,6 +22,15 @@ use crate::{
Properties,
};

macro_rules! path_exists {
($path:expr) => {
argon_error!(
"Instance with path: {} already exists! Skipping..",
$path.to_string().bold()
)
};
}

pub fn apply_addition(snapshot: AddedSnapshot, tree: &mut Tree, vfs: &Vfs) -> Result<()> {
trace!("Adding {:?} with parent {:?}", snapshot.id, snapshot.parent);

Expand All @@ -37,7 +48,13 @@ pub fn apply_addition(snapshot: AddedSnapshot, tree: &mut Tree, vfs: &Vfs) -> Re

snapshot.properties = validate_properties(snapshot.properties);

fn write_instance(is_dir: bool, path: &Path, snapshot: &Snapshot, parent_meta: &Meta, vfs: &Vfs) -> Result<Meta> {
fn write_instance(
has_children: bool,
path: &Path,
snapshot: &Snapshot,
parent_meta: &Meta,
vfs: &Vfs,
) -> Result<Option<Meta>> {
let mut meta = snapshot.meta.clone().with_context(&parent_meta.context);
let properties = snapshot.properties.clone();

Expand All @@ -46,26 +63,44 @@ pub fn apply_addition(snapshot: AddedSnapshot, tree: &mut Tree, vfs: &Vfs) -> Re
.context
.sync_rules_of_type(&middleware)
.iter()
.find_map(|rule| rule.locate(path, &snapshot.name, is_dir))
.find_map(|rule| rule.locate(path, &snapshot.name, has_children))
.with_context(|| format!("Failed to locate file path for parent: {}", path.display()))?;

let properties = middleware.write(properties, &file_path, vfs)?;
if has_children {
if vfs.exists(path) {
return Ok(None);
}

dir::write_dir(path, vfs)?;

meta.source = Source::child_file(path, &file_path);
} else {
if vfs.exists(&file_path) {
return Ok(None);
}

meta.source = Source::file(&file_path);
meta.source = Source::file(&file_path);
}

let properties = middleware.write(properties, &file_path, vfs)?;

if !properties.is_empty() {
let data_path = parent_meta
.context
.sync_rules_of_type(&Middleware::InstanceData)
.iter()
.find_map(|rule| rule.locate(path, &snapshot.name, is_dir))
.find_map(|rule| rule.locate(path, &snapshot.name, has_children))
.with_context(|| format!("Failed to locate data path for parent: {}", path.display()))?;

let data_path = data::write_data(true, &snapshot.class, properties, &data_path, &meta, vfs)?;

meta.source.set_data(data_path);
}
} else {
if vfs.exists(path) {
return Ok(None);
}

dir::write_dir(path, vfs)?;

meta.source = Source::directory(path);
Expand All @@ -82,7 +117,7 @@ pub fn apply_addition(snapshot: AddedSnapshot, tree: &mut Tree, vfs: &Vfs) -> Re
meta.source.set_data(data_path);
}

Ok(meta)
Ok(Some(meta))
}

fn add_non_project_instances(
Expand Down Expand Up @@ -144,19 +179,23 @@ pub fn apply_addition(snapshot: AddedSnapshot, tree: &mut Tree, vfs: &Vfs) -> Re
let path = parent_path.join(&snapshot.name);

if snapshot.children.is_empty() {
let meta = write_instance(false, &path, &snapshot, parent_meta, vfs)?;
let snapshot = snapshot.with_meta(meta);
if let Some(meta) = write_instance(false, &path, &snapshot, parent_meta, vfs)? {
let snapshot = snapshot.with_meta(meta);

tree.insert_instance_with_ref(snapshot, parent_id);
} else {
let meta = write_instance(false, &path, &snapshot, parent_meta, vfs)?;
tree.insert_instance_with_ref(snapshot, parent_id);
} else {
path_exists!(&path)
}
} else if let Some(meta) = write_instance(true, &path, &snapshot, parent_meta, vfs)? {
let snapshot = snapshot.with_meta(meta.clone());

tree.insert_instance_with_ref(snapshot.clone(), parent_id);

for child in snapshot.children {
add_non_project_instances(snapshot.id, &path, child, &meta, tree, vfs)?;
}
} else {
path_exists!(&path)
}

Ok(parent_source)
Expand Down
5 changes: 2 additions & 3 deletions src/middleware/dir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::{
meta::{Context, Meta, Source},
snapshot::Snapshot,
},
ext::{PathExt, ResultExt},
ext::PathExt,
vfs::Vfs,
};

Expand All @@ -30,8 +30,7 @@ pub fn read_dir(path: &Path, context: &Context, vfs: &Vfs) -> Result<Snapshot> {

#[profiling::function]
pub fn write_dir(path: &Path, vfs: &Vfs) -> Result<()> {
vfs.create_dir(path)
.with_desc(|| format!("Instance with path: {} already exists", path.display()))?;
vfs.create_dir(path)?;

Ok(())
}
10 changes: 4 additions & 6 deletions src/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,11 @@ mod exec;
mod home;
mod open;
mod read;
mod read_all;
mod snapshot;
mod stop;
mod subscribe;
mod unsubscribe;
mod write;
mod write_all;

async fn default_redirect() -> impl Responder {
web::Redirect::to("/")
Expand Down Expand Up @@ -47,14 +46,13 @@ impl Server {
.service(details::main)
.service(subscribe::main)
.service(unsubscribe::main)
.service(home::main)
.service(stop::main)
.service(snapshot::main)
.service(read::main)
.service(write::main)
.service(read_all::main)
.service(write_all::main)
.service(exec::main)
.service(open::main)
.service(stop::main)
.service(home::main)
.default_service(web::to(default_redirect))
})
.backlog(0)
Expand Down
28 changes: 0 additions & 28 deletions src/server/read_all.rs

This file was deleted.

14 changes: 14 additions & 0 deletions src/server/snapshot.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use actix_msgpack::MsgPackResponseBuilder;
use actix_web::{get, web::Data, HttpResponse, Responder};
use serde::Serialize;
use std::sync::Arc;

use crate::core::{snapshot::Snapshot, Core};

#[derive(Serialize)]
struct Response(Snapshot);

#[get("/snapshot")]
async fn main(core: Data<Arc<Core>>) -> impl Responder {
HttpResponse::Ok().msgpack(core.snapshot())
}
30 changes: 0 additions & 30 deletions src/server/write_all.rs

This file was deleted.

0 comments on commit a1d3fcf

Please sign in to comment.