Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit07c0b9d

Browse files
amirabbas-ghmohebifar
authored andcommitted
refactor: reafctor from text storing variables into Hashmap for better performance in concurrency saturation and add get_or_set_step_output function
1 parent8488e2b commit07c0b9d

File tree

6 files changed

+93
-252
lines changed

6 files changed

+93
-252
lines changed

‎crates/codemod-sandbox/src/sandbox/engine/execution_engine.rs‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,6 @@ where
100100
built_in_loader = built_in_loader.with_module("codemod:ast-grep",AstGrepModule);
101101

102102
// Add WorkflowGlobalModule (step outputs)
103-
let step_outputs_path = std::env::temp_dir().join("codemod_step_outputs.txt");
104-
std::env::set_var("STEP_OUTPUTS",&step_outputs_path);
105103
built_in_resolver = built_in_resolver.add_name("codemod:workflow");
106104
built_in_loader = built_in_loader.with_module("codemod:workflow",WorkflowGlobalModule);
107105

‎crates/codemod-sandbox/src/sandbox/engine/selector_engine.rs‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,6 @@ where
7979
built_in_loader = built_in_loader.with_module("codemod:ast-grep",AstGrepModule);
8080

8181
// Add WorkflowGlobalModule (step outputs)
82-
let step_outputs_path = std::env::temp_dir().join("codemod_step_outputs.txt");
83-
std::env::set_var("STEP_OUTPUTS",&step_outputs_path);
8482
built_in_resolver = built_in_resolver.add_name("codemod:workflow");
8583
built_in_loader = built_in_loader.with_module("codemod:workflow",WorkflowGlobalModule);
8684

Lines changed: 93 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,82 @@
11
use rquickjs::module::{Declarations,Exports,ModuleDef};
22
use rquickjs::{prelude::Func,Ctx,Exception,Object,Result};
3-
pubmod types;
3+
use std::collections::HashMap;
4+
use std::env;
5+
use std::sync::{Arc,Condvar,LazyLock,Mutex};
46

5-
#[cfg(feature ="native")]
6-
pubmod native;
7+
staticSTEP_OUTPUTS_STORE:LazyLock<Mutex<HashMap<String,HashMap<String,String>>>> =
8+
LazyLock::new(||Mutex::new(HashMap::new()));
79

8-
#[cfg(feature ="wasm")]
9-
pubmod wasm;
10+
staticSTEP_OUTPUT_NOTIFIER:LazyLock<Arc<Condvar>> =LazyLock::new(||Arc::new(Condvar::new()));
11+
12+
pubfnset_step_output(
13+
output_name:&str,
14+
value:&str,
15+
) -> std::result::Result<(),Box<dyn std::error::Error>>{
16+
let step_id = env::var("CODEMOD_STEP_ID").unwrap_or_default();
17+
18+
{
19+
letmut store =STEP_OUTPUTS_STORE.lock().unwrap();
20+
store
21+
.entry(step_id)
22+
.or_default()
23+
.insert(output_name.to_string(), value.to_string());
24+
}
25+
26+
STEP_OUTPUT_NOTIFIER.notify_all();
27+
28+
Ok(())
29+
}
30+
31+
pubfnget_step_output(
32+
step_id:&str,
33+
output_name:&str,
34+
) -> std::result::Result<Option<String>,Box<dyn std::error::Error>>{
35+
let store =STEP_OUTPUTS_STORE.lock().unwrap();
36+
37+
ifletSome(outputs) = store.get(step_id){
38+
ifletSome(value) = outputs.get(output_name){
39+
returnOk(Some(value.clone()));
40+
}
41+
}
42+
43+
Ok(None)
44+
}
45+
46+
pubfnget_step_outputs(
47+
step_id:&str,
48+
) -> std::result::Result<HashMap<String,String>,Box<dyn std::error::Error>>{
49+
let store =STEP_OUTPUTS_STORE.lock().unwrap();
50+
51+
ifletSome(outputs) = store.get(step_id){
52+
Ok(outputs.clone())
53+
}else{
54+
Ok(HashMap::new())
55+
}
56+
}
57+
58+
/// Get or set step output atomically
59+
/// If the output exists, returns it. If not, sets it to the provided value and returns it.
60+
pubfnget_or_set_step_output(
61+
step_id:&str,
62+
output_name:&str,
63+
default_value:&str,
64+
) -> std::result::Result<String,Box<dyn std::error::Error>>{
65+
letmut store =STEP_OUTPUTS_STORE.lock().unwrap();
66+
67+
let outputs = store.entry(step_id.to_string()).or_default();
68+
69+
ifletSome(value) = outputs.get(output_name){
70+
// Output already exists, return it
71+
Ok(value.clone())
72+
}else{
73+
// Output doesn't exist, set it and return the new value
74+
outputs.insert(output_name.to_string(), default_value.to_string());
75+
drop(store);// Release lock before notifying
76+
STEP_OUTPUT_NOTIFIER.notify_all();
77+
Ok(default_value.to_string())
78+
}
79+
}
1080

1181
#[allow(dead_code)]
1282
pub(crate)structWorkflowGlobalModule;
@@ -15,6 +85,8 @@ impl ModuleDef for WorkflowGlobalModule {
1585
fndeclare(declare:&Declarations) ->Result<()>{
1686
declare.declare("setStepOutput")?;
1787
declare.declare("getStepOutput")?;
88+
declare.declare("waitForStepOutput")?;
89+
declare.declare("getOrSetStepOutput")?;
1890
declare.declare("default")?;
1991
Ok(())
2092
}
@@ -24,21 +96,19 @@ impl ModuleDef for WorkflowGlobalModule {
2496

2597
default.set("setStepOutput",Func::from(set_step_output_rjs))?;
2698
default.set("getStepOutput",Func::from(get_step_output_rjs))?;
99+
default.set("getOrSetStepOutput",Func::from(get_or_set_step_output_rjs))?;
27100

28101
exports.export("setStepOutput",Func::from(set_step_output_rjs))?;
29102
exports.export("getStepOutput",Func::from(get_step_output_rjs))?;
103+
exports.export("getOrSetStepOutput",Func::from(get_or_set_step_output_rjs))?;
30104

31105
exports.export("default", default)?;
32106
Ok(())
33107
}
34108
}
35109

36110
fnset_step_output_rjs(ctx:Ctx<'_>,output_name:String,value:String) ->Result<()>{
37-
#[cfg(feature ="native")]
38-
let result = native::set_step_output(&output_name,&value);
39-
#[cfg(feature ="wasm")]
40-
let result = wasm::set_step_output(&output_name,&value);
41-
111+
let result =set_step_output(&output_name,&value);
42112
result.map_err(|e|Exception::throw_message(&ctx,&format!("Failed to set step output: {e}")))
43113
}
44114

@@ -47,10 +117,18 @@ fn get_step_output_rjs(
47117
step_id:String,
48118
output_name:String,
49119
) ->Result<Option<String>>{
50-
#[cfg(feature ="native")]
51-
let result = native::get_step_output(&step_id,&output_name);
52-
#[cfg(feature ="wasm")]
53-
let result = wasm::get_step_output(&step_id,&output_name);
54-
120+
let result =get_step_output(&step_id,&output_name);
55121
result.map_err(|e|Exception::throw_message(&ctx,&format!("Failed to get step output: {e}")))
56122
}
123+
124+
fnget_or_set_step_output_rjs(
125+
ctx:Ctx<'_>,
126+
step_id:String,
127+
output_name:String,
128+
default_value:String,
129+
) ->Result<String>{
130+
let result =get_or_set_step_output(&step_id,&output_name,&default_value);
131+
result.map_err(|e|{
132+
Exception::throw_message(&ctx,&format!("Failed to get or set step output: {e}"))
133+
})
134+
}

‎crates/codemod-sandbox/src/workflow_global/native.rs‎

Lines changed: 0 additions & 126 deletions
This file was deleted.

‎crates/codemod-sandbox/src/workflow_global/types.rs‎

Lines changed: 0 additions & 31 deletions
This file was deleted.

‎crates/codemod-sandbox/src/workflow_global/wasm.rs‎

Lines changed: 0 additions & 76 deletions
This file was deleted.

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp