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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
//! Data cache and provider management for ExecutionContext
//!
//! Handles async data loading, prefetching, and live data feeds.
use shape_ast::error::{Result, ShapeError};
/// Data loading execution mode (Phase 8)
///
/// Determines how runtime data access behaves:
/// - Async: Data must be prefetched before execution (scripts, backtests)
/// - Sync: Data requests can block during execution (REPL only)
#[derive(Debug, Clone, Copy, PartialEq, Eq, serde::Serialize, serde::Deserialize, Default)]
pub enum DataLoadMode {
/// Async mode - data must be prefetched.
/// Data requests return cached data or errors.
/// Used for: scripts, backtests, production
#[default]
Async,
/// Sync mode - data requests can block.
/// Uses tokio::runtime::Handle::current().block_on()
/// Used for: REPL, interactive exploration
Sync,
}
impl super::ExecutionContext {
/// Prefetch data before execution (Phase 6)
///
/// This async method loads all required data concurrently and populates the cache.
/// Must be called before execution starts.
///
/// # Arguments
///
/// * `queries` - List of DataQuery objects specifying what data to load
///
/// # Example
///
/// ```ignore
/// let queries = vec![
/// DataQuery::new("AAPL", Timeframe::d1()).limit(1000),
/// ];
/// ctx.prefetch_data(queries).await?;
/// ```
pub async fn prefetch_data(&mut self, queries: Vec<crate::data::DataQuery>) -> Result<()> {
if let Some(cache) = &self.data_cache {
cache
.prefetch(queries)
.await
.map_err(|e| ShapeError::DataError {
message: format!("Failed to prefetch data: {}", e),
symbol: None,
timeframe: None,
})?;
}
Ok(())
}
/// Start live data feed (Phase 6)
///
/// Subscribes to live bar updates for the current symbol/timeframe.
/// New bars will be appended to the live buffer as they arrive.
pub fn start_live_feed(&mut self) -> Result<()> {
let id = self.get_current_id()?;
let timeframe = self.get_current_timeframe()?;
if let Some(cache) = &mut self.data_cache {
cache
.subscribe_live(&id, &timeframe)
.map_err(|e| ShapeError::RuntimeError {
message: format!("Failed to start live feed: {}", e),
location: None,
})?;
}
Ok(())
}
/// Stop live data feed (Phase 6)
pub fn stop_live_feed(&mut self) -> Result<()> {
let id = self.get_current_id()?;
let timeframe = self.get_current_timeframe()?;
if let Some(cache) = &mut self.data_cache {
cache.unsubscribe_live(&id, &timeframe);
}
Ok(())
}
/// Check if using async data cache (Phase 6)
pub fn has_data_cache(&self) -> bool {
self.data_cache.is_some()
}
/// Get reference to data cache (Phase 8)
pub fn data_cache(&self) -> Option<&crate::data::DataCache> {
self.data_cache.as_ref()
}
/// Get async data provider (Phase 7)
///
/// Returns the AsyncDataProvider from the data cache if available.
/// This is used for constructing TableRef and other lazy data references.
pub fn async_provider(&self) -> Option<crate::data::SharedAsyncProvider> {
self.data_cache.as_ref().map(|cache| cache.provider())
}
/// Register a data provider (Phase 8)
///
/// Registers a named provider in the registry.
pub fn register_provider(&self, name: &str, provider: crate::data::SharedAsyncProvider) {
self.provider_registry.register(name, provider);
}
/// Get provider by name (Phase 8)
pub fn get_provider(&self, name: &str) -> Result<crate::data::SharedAsyncProvider> {
self.provider_registry
.get(name)
.ok_or_else(|| ShapeError::RuntimeError {
message: format!("Provider '{}' not registered", name),
location: None,
})
}
/// Get default provider (Phase 8)
pub fn get_default_provider(&self) -> Result<crate::data::SharedAsyncProvider> {
self.provider_registry
.get_default()
.ok_or_else(|| ShapeError::RuntimeError {
message: "No default provider configured".to_string(),
location: None,
})
}
/// Set default provider (Phase 8)
pub fn set_default_provider(&self, name: &str) -> Result<()> {
self.provider_registry.set_default(name)
}
/// Register a type mapping (Phase 8)
///
/// Registers a type mapping that defines the expected DataFrame structure.
pub fn register_type_mapping(
&self,
type_name: &str,
mapping: super::super::type_mapping::TypeMapping,
) {
self.type_mapping_registry.register(type_name, mapping);
}
/// Get type mapping (Phase 8)
///
/// Retrieves the type mapping for validation.
pub fn get_type_mapping(
&self,
type_name: &str,
) -> Result<super::super::type_mapping::TypeMapping> {
self.type_mapping_registry
.get(type_name)
.ok_or_else(|| ShapeError::RuntimeError {
message: format!("Type mapping for '{}' not found", type_name),
location: None,
})
}
/// Check if type mapping exists (Phase 8)
pub fn has_type_mapping(&self, type_name: &str) -> bool {
self.type_mapping_registry.has(type_name)
}
// ========================================================================
// Extension Management
// ========================================================================
/// Load a data source extension from a shared library
///
/// # Arguments
///
/// * `path` - Path to the extension shared library (.so, .dll, .dylib)
/// * `config` - Configuration value for the extension
pub fn load_extension(
&self,
path: &std::path::Path,
config: &serde_json::Value,
) -> Result<super::super::extensions::LoadedExtension> {
self.provider_registry.load_extension(path, config)
}
/// Unload an extension by name
pub fn unload_extension(&self, name: &str) -> bool {
self.provider_registry.unload_extension(name)
}
/// List all loaded extension names
pub fn list_extensions(&self) -> Vec<String> {
self.provider_registry.list_extensions()
}
/// Get query schema for an extension (for LSP autocomplete)
pub fn get_extension_query_schema(
&self,
name: &str,
) -> Option<super::super::extensions::ParsedQuerySchema> {
self.provider_registry.get_extension_query_schema(name)
}
/// Get output schema for an extension (for LSP autocomplete)
pub fn get_extension_output_schema(
&self,
name: &str,
) -> Option<super::super::extensions::ParsedOutputSchema> {
self.provider_registry.get_extension_output_schema(name)
}
/// Get an extension data source by name
pub fn get_extension(
&self,
name: &str,
) -> Option<std::sync::Arc<super::super::extensions::ExtensionDataSource>> {
self.provider_registry.get_extension(name)
}
/// Get extension module schema by module namespace.
pub fn get_extension_module_schema(
&self,
module_name: &str,
) -> Option<super::super::extensions::ParsedModuleSchema> {
self.provider_registry
.get_extension_module_schema(module_name)
}
/// Get a language runtime by its language identifier (e.g., "python").
pub fn get_language_runtime(
&self,
language_id: &str,
) -> Option<std::sync::Arc<super::super::plugins::language_runtime::PluginLanguageRuntime>>
{
self.provider_registry.get_language_runtime(language_id)
}
/// Return all loaded language runtimes, keyed by language identifier.
pub fn language_runtimes(
&self,
) -> std::collections::HashMap<
String,
std::sync::Arc<super::super::plugins::language_runtime::PluginLanguageRuntime>,
> {
self.provider_registry.language_runtimes()
}
/// Build VM extension modules from loaded extension module capabilities.
pub fn module_exports_from_extensions(
&self,
) -> Vec<super::super::module_exports::ModuleExports> {
self.provider_registry.module_exports_from_extensions()
}
/// Invoke one loaded module export via module namespace.
pub fn invoke_extension_module_wire(
&self,
module_name: &str,
function: &str,
args: &[shape_wire::WireValue],
) -> Result<shape_wire::WireValue> {
self.provider_registry
.invoke_extension_module_wire(module_name, function, args)
}
/// Get current data load mode (Phase 8)
pub fn data_load_mode(&self) -> DataLoadMode {
self.data_load_mode
}
/// Set data load mode (Phase 8)
pub fn set_data_load_mode(&mut self, mode: DataLoadMode) {
self.data_load_mode = mode;
}
/// Check if in REPL mode (sync loading allowed)
pub fn is_repl_mode(&self) -> bool {
self.data_load_mode == DataLoadMode::Sync
}
/// Set the DuckDB provider
pub fn set_data_provider(&mut self, provider: std::sync::Arc<dyn std::any::Any + Send + Sync>) {
self.data_provider = Some(provider);
}
/// Get DataProvider (legacy compatibility - returns type-erased Arc)
#[inline]
pub fn data_provider(&self) -> Result<std::sync::Arc<dyn std::any::Any + Send + Sync>> {
self.data_provider
.as_ref()
.ok_or_else(|| ShapeError::RuntimeError {
message: "No DataProvider configured. Use engine's async provider.".to_string(),
location: None,
})
.cloned()
}
}