-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Feat/#122 local problem registry
- Loading branch information
Showing
12 changed files
with
244 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,4 @@ pub mod job; | |
pub mod problem_registry; | ||
pub mod procedure; | ||
pub mod runner; | ||
pub mod writer_schema_transpiler; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
#![allow(unused_variables)] | ||
|
||
use crate::{ | ||
problem_registry::*, | ||
procedure::{writer_schema::*, *}, | ||
*, | ||
}; | ||
use std::collections::HashMap; | ||
|
||
pub fn transpile( | ||
problem: writer_schema::Procedure, | ||
) -> Result< | ||
( | ||
registered::Procedure, | ||
HashMap<identifiers::ResourceId, String>, | ||
), | ||
RegistrationError, | ||
> { | ||
let mut name_to_id = HashMap::new(); | ||
for resource in problem.resources.iter() { | ||
let name = match resource { | ||
ResourceKind::TextFile(content) => content.name.clone(), | ||
ResourceKind::EmptyDirectory(empty_dir) => empty_dir.name.clone(), | ||
ResourceKind::RuntimeTextFile(runtime_text) => runtime_text.name.clone(), | ||
}; | ||
let id = identifiers::DepId::new(); | ||
name_to_id.insert(name.clone(), id); | ||
} | ||
for script in problem.scripts.iter() { | ||
let name = script.name.clone(); | ||
let id = identifiers::DepId::new(); | ||
name_to_id.insert(name.clone(), id); | ||
} | ||
for execution in problem.executions.iter() { | ||
let name = execution.name.clone(); | ||
let id = identifiers::DepId::new(); | ||
name_to_id.insert(name.clone(), id); | ||
} | ||
let mut content_to_id = HashMap::new(); | ||
for resource in problem.resources.iter() { | ||
if let ResourceKind::TextFile(content) = resource { | ||
let id = identifiers::ResourceId::new(); | ||
content_to_id.insert(content.content.clone(), id); | ||
} | ||
} | ||
let mut runtime_texts = Vec::new(); | ||
let mut texts = Vec::new(); | ||
let mut empty_directories = Vec::new(); | ||
for resource in problem.resources.iter() { | ||
match resource { | ||
ResourceKind::TextFile(content) => { | ||
let dep_id = name_to_id | ||
.get(&content.name) | ||
.ok_or(RegistrationError::InvalidSchema( | ||
"TextFile name not found".to_string(), | ||
))? | ||
.clone(); | ||
let text = registered::Text { | ||
resource_id: content_to_id | ||
.get(&content.content) | ||
.ok_or(RegistrationError::InvalidSchema( | ||
"TextFile content not found".to_string(), | ||
))? | ||
.clone(), | ||
dep_id: dep_id.clone(), | ||
}; | ||
texts.push(text); | ||
} | ||
ResourceKind::EmptyDirectory(empty_dir) => { | ||
let dep_id = name_to_id | ||
.get(&empty_dir.name) | ||
.ok_or(RegistrationError::InvalidSchema( | ||
"EmptyDirectory name not found".to_string(), | ||
))? | ||
.clone(); | ||
let empty_directory = registered::EmptyDirectory { | ||
dep_id: dep_id.clone(), | ||
}; | ||
empty_directories.push(empty_directory); | ||
} | ||
ResourceKind::RuntimeTextFile(runtime_text) => { | ||
let dep_id = name_to_id | ||
.get(&runtime_text.name) | ||
.ok_or(RegistrationError::InvalidSchema( | ||
"RuntimeText name not found".to_string(), | ||
))? | ||
.clone(); | ||
let runtime_text = registered::RuntimeText { | ||
label: runtime_text.label.clone(), | ||
dep_id: dep_id.clone(), | ||
}; | ||
runtime_texts.push(runtime_text); | ||
} | ||
} | ||
} | ||
let mut executions = Vec::new(); | ||
for execution in problem.executions.iter() { | ||
let script = execution.script_name.clone(); | ||
let mut dependencies = Vec::new(); | ||
for dep in execution.depends_on.iter() { | ||
let dep_id = name_to_id | ||
.get(&dep.ref_to) | ||
.ok_or(RegistrationError::InvalidSchema( | ||
"Dependency name not found".to_string(), | ||
))? | ||
.clone(); | ||
let depends_on = registered::DependsOn { | ||
dep_id: dep_id.clone(), | ||
envvar_name: dep.envvar_name.clone(), | ||
}; | ||
dependencies.push(depends_on); | ||
} | ||
let dep_id = name_to_id | ||
.get(&execution.name) | ||
.ok_or(RegistrationError::InvalidSchema( | ||
"Execution name not found".to_string(), | ||
))? | ||
.clone(); | ||
let priority = 0; | ||
let execution = registered::Execution { | ||
script: script, | ||
depends_on: dependencies, | ||
dep_id: dep_id, | ||
priority: priority, | ||
}; | ||
executions.push(execution); | ||
} | ||
let procedure = registered::Procedure { | ||
runtime_texts, | ||
texts, | ||
empty_directories, | ||
executions, | ||
}; | ||
Ok(( | ||
procedure, | ||
content_to_id | ||
.iter() | ||
.map(|(k, v)| (v.clone(), k.clone())) | ||
.collect(), | ||
)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
[package] | ||
name = "local_problem_registry" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[dependencies] | ||
tokio = { workspace = true } | ||
futures = { workspace = true } | ||
judge_core = { path = "../judge_core" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
pub mod registry_client; | ||
pub mod registry_server; | ||
|
||
pub fn new_registry() -> ( | ||
registry_server::RegistryServer, | ||
registry_client::RegistryClient, | ||
) { | ||
let registry = std::sync::Arc::new(tokio::sync::Mutex::new(std::collections::HashMap::new())); | ||
let server = registry_server::RegistryServer { | ||
registry: registry.clone(), | ||
}; | ||
let client = registry_client::RegistryClient { registry }; | ||
(server, client) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
use judge_core::*; | ||
use std::collections::HashMap; | ||
use std::sync::Arc; | ||
use tokio::sync::Mutex; | ||
|
||
pub struct RegistryClient { | ||
pub(crate) registry: Arc<Mutex<HashMap<identifiers::ResourceId, String>>>, | ||
} | ||
|
||
impl problem_registry::ProblemRegistryClient for RegistryClient { | ||
async fn fetch( | ||
&self, | ||
resource_id: identifiers::ResourceId, | ||
) -> Result<String, problem_registry::ResourceFetchError> { | ||
let registry = self.registry.lock().await; | ||
match registry.get(&resource_id) { | ||
Some(contents) => Ok(contents.clone()), | ||
None => Err(problem_registry::ResourceFetchError::NotFound(resource_id)), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
use judge_core::{problem_registry::*, procedure::*, writer_schema_transpiler::transpile, *}; | ||
use std::collections::HashMap; | ||
use std::sync::Arc; | ||
use tokio::sync::Mutex; | ||
|
||
pub struct RegistryServer { | ||
pub(crate) registry: Arc<Mutex<HashMap<identifiers::ResourceId, String>>>, | ||
} | ||
|
||
impl ProblemRegistryServer for RegistryServer { | ||
async fn register( | ||
&self, | ||
procedure: writer_schema::Procedure, | ||
) -> Result<registered::Procedure, RegistrationError> { | ||
let (transpiled_procedure, content_to_register) = transpile(procedure)?; | ||
let mut registry = self.registry.lock().await; | ||
for (resource_id, content) in content_to_register { | ||
registry.insert(resource_id, content); | ||
} | ||
Ok(transpiled_procedure) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters