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
//! Schema management operations for Lambda context
use crate::ingestion::IngestionError;
use super::context::LambdaContext;
use crate::datafold_node::OperationProcessor;
use crate::error::FoldDbError;
impl LambdaContext {
/// List all schemas with their states
///
/// Returns schemas along with their approval/pending states.
///
/// # Arguments
///
/// * `user_id` - User ID for node context
pub async fn list_schemas(
user_id: String,
) -> Result<Vec<crate::schema::SchemaWithState>, IngestionError> {
let node_mutex = Self::get_node(&user_id).await?;
let node_guard = node_mutex.lock().await;
let processor = OperationProcessor::new(node_guard.clone());
processor
.list_schemas()
.await
.map_err(|e| IngestionError::InvalidInput(format!("Failed to list schemas: {}", e)))
}
/// Get a specific schema by name
///
/// Returns the schema with its state if it exists.
///
/// # Arguments
///
/// * `schema_name` - Name of the schema to retrieve
/// * `user_id` - User ID for node context
pub async fn get_schema(
schema_name: &str,
user_id: String,
) -> Result<Option<crate::schema::SchemaWithState>, IngestionError> {
let node_mutex = Self::get_node(&user_id).await?;
let node_guard = node_mutex.lock().await;
let processor = OperationProcessor::new(node_guard.clone());
processor
.get_schema(schema_name)
.await
.map_err(|e| IngestionError::InvalidInput(format!("Failed to get schema: {}", e)))
}
/// Block a schema from queries and mutations
///
/// # Arguments
///
/// * `schema_name` - Name of the schema to block
/// * `user_id` - User ID for node context
pub async fn block_schema(schema_name: &str, user_id: String) -> Result<(), IngestionError> {
let node_mutex = Self::get_node(&user_id).await?;
let node_guard = node_mutex.lock().await;
let processor = OperationProcessor::new(node_guard.clone());
processor
.block_schema(schema_name)
.await
.map_err(|e| IngestionError::InvalidInput(format!("Failed to block schema: {}", e)))
}
/// Load schemas from the schema service
///
/// Fetches available schemas from the configured schema service and loads them into the local database.
///
/// # Returns
///
/// Returns a tuple of (schemas_fetched, schemas_loaded, failed_schemas)
///
/// # Arguments
///
/// * `user_id` - User ID for node context
///
/// # Example
///
/// ```ignore
/// use datafold::lambda::LambdaContext;
///
/// async fn handler() -> Result<(), Box<dyn std::error::Error>> {
/// let (fetched, loaded, failed) = LambdaContext::load_schemas("user_123").await?;
/// println!("Fetched {} schemas, loaded {} successfully", fetched, loaded);
/// Ok(())
/// }
/// ```
pub async fn load_schemas(
user_id: String,
) -> Result<(usize, usize, Vec<String>), IngestionError> {
let node_mutex = Self::get_node(&user_id).await?;
let node_guard = node_mutex.lock().await;
let processor = OperationProcessor::new(node_guard.clone());
processor
.load_schemas()
.await
.map_err(|e| IngestionError::InvalidInput(format!("Failed to load schemas: {}", e)))
}
/// Approve a schema
///
/// Approves a schema if it's not already approved (idempotent).
///
/// # Arguments
///
/// * `schema_name` - Name of the schema to approve
/// * `user_id` - User ID for node context
///
/// # Example
///
/// ```ignore
/// use datafold::lambda::LambdaContext;
///
/// async fn handler() -> Result<(), Box<dyn std::error::Error>> {
/// LambdaContext::approve_schema("users", "user_123".to_string()).await?;
/// println!("Schema approved");
/// Ok(())
/// }
/// ```
pub async fn approve_schema(
schema_name: &str,
user_id: String,
) -> Result<Option<String>, IngestionError> {
let node_mutex = Self::get_node(&user_id).await?;
let node_guard = node_mutex.lock().await;
let processor = OperationProcessor::new(node_guard.clone());
processor
.approve_schema(schema_name)
.await
.map_err(|e| IngestionError::InvalidInput(format!("Failed to approve schema: {}", e)))
}
/// Get the state of a schema
///
/// # Arguments
///
/// * `schema_name` - Name of the schema
/// * `user_id` - User ID for node context
///
/// # Returns
///
/// Returns `Some(SchemaState)` if the schema exists, or `None` if not found.
///
/// # Example
///
/// ```ignore
/// use datafold::lambda::LambdaContext;
///
/// async fn handler() -> Result<(), Box<dyn std::error::Error>> {
/// if let Some(state) = LambdaContext::get_schema_state("users", "user_123".to_string()).await? {
/// println!("Schema state: {:?}", state);
/// }
/// Ok(())
/// }
/// ```
pub async fn get_schema_state(
schema_name: &str,
user_id: String,
) -> Result<Option<crate::schema::SchemaState>, IngestionError> {
let node_mutex = Self::get_node(&user_id).await?;
let node_guard = node_mutex.lock().await;
let processor = OperationProcessor::new(node_guard.clone());
if let Some(schema_with_state) = processor.get_schema(schema_name).await.map_err(|e| {
IngestionError::InvalidInput(format!("Failed to get schema state: {}", e))
})? {
Ok(Some(schema_with_state.state))
} else {
Ok(None)
}
}
/// Get backfill status by hash
///
/// # Arguments
///
/// * `backfill_hash` - The hash of the backfill to retrieve
/// * `user_id` - User ID for node context
///
/// # Returns
///
/// Returns `Some(BackfillInfo)` if found, or `None` if not found.
///
/// # Example
///
/// ```ignore
/// use datafold::lambda::LambdaContext;
///
/// async fn handler() -> Result<(), Box<dyn std::error::Error>> {
/// if let Some(info) = LambdaContext::get_backfill_status("abc123hash", "user_123".to_string()).await? {
/// println!("Backfill status: {:?}", info.status);
/// }
/// Ok(())
/// }
/// ```
pub async fn get_backfill_status(
backfill_hash: &str,
user_id: String,
) -> Result<
Option<crate::fold_db_core::infrastructure::backfill_tracker::BackfillInfo>,
IngestionError,
> {
let node_mutex = Self::get_node(&user_id).await?;
let node_guard = node_mutex.lock().await;
let processor = OperationProcessor::new(node_guard.clone());
processor.get_backfill(backfill_hash).await.map_err(|e| {
IngestionError::InvalidInput(format!("Failed to get backfill status: {}", e))
})
}
}