1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
|
use std::{
sync::{mpsc::channel, Arc, RwLock, RwLockReadGuard},
thread::JoinHandle,
};
use mlua::prelude::*;
use super::{cell::Cell, Sheet};
pub trait EvalFunction {
fn eval_function(
&self,
func: String,
range: Vec<(usize, usize)>,
) -> JoinHandle<Result<Sheet, String>>;
}
impl EvalFunction for Arc<RwLock<Sheet>> {
fn eval_function(
&self,
func_text: String,
range: Vec<(usize, usize)>,
) -> JoinHandle<Result<Sheet, String>> {
*self.read().unwrap().progress.lock().unwrap() = 0;
let this = Arc::clone(self);
std::thread::spawn(move || {
let chunks = range.chunks(
(range.len() / std::thread::available_parallelism().unwrap())
.min(range.len())
.max(1000),
);
if chunks.len() > 4 {
let (tx, rx) = channel();
std::thread::scope(|s| {
for chunk in chunks {
let tx = tx.clone();
let this = Arc::clone(&this);
let func_text = func_text.clone();
s.spawn(move || {
let tx = tx.clone();
let read_sheet = this.read().unwrap();
let result = EvalRange::new(chunk.iter(), func_text, read_sheet);
match result {
Ok(evaluator) => {
for result in evaluator {
if result.is_err() {
tx.send(result).unwrap();
break;
}
tx.send(result).unwrap();
}
}
Err(error) => tx.send(Err(error)).unwrap(),
}
});
}
drop(tx);
let read_sheet = this.read().unwrap();
let mut sheet = read_sheet.clone();
let mut count = 0;
for result in rx {
count += 1;
*read_sheet.progress.lock().unwrap() = (count * 100 / range.len()) as u8;
match result {
Ok((row, column, cell)) => sheet.set_cell(row, column, cell),
Err(error) => return Err(error.to_string()),
}
}
crate::lua::new_instance().unwrap().expire_registry_values();
Ok(sheet)
})
} else {
let read_sheet = this.read().unwrap();
let mut sheet = read_sheet.clone();
let result = EvalRange::new((&range).iter(), func_text, read_sheet);
match result {
Ok(evaluator) => {
for result in evaluator {
match result {
Ok((row, column, cell)) => sheet.set_cell(row, column, cell),
Err(error) => return Err(error.to_string()),
}
}
}
Err(error) => return Err(error),
}
crate::lua::new_instance().unwrap().expire_registry_values();
Ok(sheet)
}
})
}
}
struct EvalRange<'a, I>
where
I: Iterator<Item = &'a (usize, usize)>,
{
lua: Lua,
reg_func_key: LuaRegistryKey,
range: I,
read_sheet: RwLockReadGuard<'a, Sheet>,
}
impl<'a, I> EvalRange<'a, I>
where
I: Iterator<Item = &'a (usize, usize)>,
{
fn new(
range: I,
script: String,
read_sheet: RwLockReadGuard<'a, Sheet>,
) -> Result<Self, String> {
let lua = crate::lua::new_instance().unwrap();
let result = lua
.load(script.clone())
.set_name("Temp Script")
.eval::<LuaFunction>();
if result.is_err() {
return Err(result.err().unwrap().to_string());
}
let lua = crate::lua::new_instance().unwrap();
let func = lua
.load(script)
.set_name("Temp Script")
.eval::<LuaFunction>()
.unwrap();
Ok(Self {
reg_func_key: lua.create_registry_value(func).unwrap(),
lua,
range,
read_sheet,
})
}
}
impl<'a, I> Iterator for EvalRange<'a, I>
where
I: Iterator<Item = &'a (usize, usize)>,
{
type Item = Result<(usize, usize, Cell), String>;
fn next(&mut self) -> Option<Self::Item> {
let func: LuaFunction = self.lua.registry_value(&self.reg_func_key).unwrap();
if let Some((row, column)) = self.range.next() {
let cellref = self.read_sheet.get_ref(*row, *column);
match func.call::<_, Cell>(cellref) {
Ok(cell) => Some(Ok((*row, *column, cell))),
Err(error) => Some(Err(error.to_string())),
}
} else {
None
}
}
}
|