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
//! # Distributed Processing Context
//!
//! This module provides a high-level context for distributed processing,
//! enabling management of multiple datasets.
use std::sync::{Arc, Mutex};
use std::collections::HashMap;
use std::path::Path;
use crate::error::{Result, Error};
use crate::lock_safe;
use crate::dataframe::DataFrame;
use super::config::DistributedConfig;
use super::execution::{ExecutionEngine, ExecutionContext};
use super::dataframe::DistributedDataFrame;
use super::ToDistributed;
/// A context for managing distributed processing operations
pub struct DistributedContext {
/// Configuration for distributed processing
config: DistributedConfig,
/// Execution engine
engine: Box<dyn ExecutionEngine>,
/// Execution context
context: Arc<Mutex<Box<dyn ExecutionContext>>>,
/// Registered datasets
datasets: HashMap<String, DistributedDataFrame>,
}
impl DistributedContext {
/// Creates a new distributed context
pub fn new(config: DistributedConfig) -> Result<Self> {
#[cfg(feature = "distributed")]
{
// Create the engine based on the config
let mut engine: Box<dyn ExecutionEngine> = match config.executor_type() {
crate::distributed::config::ExecutorType::DataFusion => {
Box::new(crate::distributed::datafusion::DataFusionEngine::new())
},
_ => {
// Default to DataFusion for now
Box::new(crate::distributed::datafusion::DataFusionEngine::new())
}
};
// Initialize the engine
engine.initialize(&config)?;
// Create the execution context
let context = engine.create_context(&config)?;
Ok(Self {
config,
engine,
context: Arc::new(Mutex::new(context)),
datasets: HashMap::new(),
})
}
#[cfg(not(feature = "distributed"))]
{
Err(Error::FeatureNotAvailable(
"Distributed processing is not available. Recompile with the 'distributed' feature flag.".to_string()
))
}
}
/// Registers a DataFrame with the context under the given name
pub fn register_dataframe(&mut self, name: &str, df: &DataFrame) -> Result<()> {
#[cfg(feature = "distributed")]
{
// Convert the DataFrame to a distributed DataFrame
let dist_df = df.to_distributed(self.config.clone())?;
// Clone the context reference for the distributed DataFrame
let dist_df_with_context = DistributedDataFrame::new(
self.config.clone(),
self.engine.clone(),
lock_safe!(self.context, "distributed context lock")?.as_ref().clone(),
name.to_string(),
);
// Register the distributed DataFrame
self.datasets.insert(name.to_string(), dist_df_with_context);
Ok(())
}
#[cfg(not(feature = "distributed"))]
{
Err(Error::FeatureNotAvailable(
"Distributed processing is not available. Recompile with the 'distributed' feature flag.".to_string()
))
}
}
/// Registers a CSV file with the context under the given name
pub fn register_csv(&mut self, name: &str, path: &str) -> Result<()> {
#[cfg(feature = "distributed")]
{
// Validate file path
if !Path::new(path).exists() {
return Err(Error::IoError(format!("CSV file not found: {}", path)));
}
// Register the CSV file with the execution context
let mut context = lock_safe!(self.context, "distributed context lock")?;
context.register_csv(name, path)?;
// Create a placeholder distributed DataFrame
let dist_df = DistributedDataFrame::new(
self.config.clone(),
self.engine.clone(),
context.as_ref().clone(),
name.to_string(),
);
// Register the distributed DataFrame
self.datasets.insert(name.to_string(), dist_df);
Ok(())
}
#[cfg(not(feature = "distributed"))]
{
Err(Error::FeatureNotAvailable(
"Distributed processing is not available. Recompile with the 'distributed' feature flag.".to_string()
))
}
}
/// Registers a Parquet file with the context under the given name
pub fn register_parquet(&mut self, name: &str, path: &str) -> Result<()> {
#[cfg(feature = "distributed")]
{
// Validate file path
if !Path::new(path).exists() {
return Err(Error::IoError(format!("Parquet file not found: {}", path)));
}
// Register the Parquet file with the execution context
let mut context = lock_safe!(self.context, "distributed context lock")?;
context.register_parquet(name, path)?;
// Create a placeholder distributed DataFrame
let dist_df = DistributedDataFrame::new(
self.config.clone(),
self.engine.clone(),
context.as_ref().clone(),
name.to_string(),
);
// Register the distributed DataFrame
self.datasets.insert(name.to_string(), dist_df);
Ok(())
}
#[cfg(not(feature = "distributed"))]
{
Err(Error::FeatureNotAvailable(
"Distributed processing is not available. Recompile with the 'distributed' feature flag.".to_string()
))
}
}
/// Gets a registered dataset by name
pub fn get_dataset(&self, name: &str) -> Option<&DistributedDataFrame> {
self.datasets.get(name)
}
/// Lists all registered dataset names
pub fn dataset_names(&self) -> Vec<String> {
self.datasets.keys().cloned().collect()
}
/// Returns the configuration used by this context
pub fn config(&self) -> &DistributedConfig {
&self.config
}
}