datafold 0.1.55

A personal database for data sovereignty with AI-powered ingestion
Documentation
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
use std::collections::HashMap;
use std::fs;
use std::sync::{Arc, Mutex};

use serde_json;

use crate::fold_db_core::infrastructure::message_bus::events::schema_events::SchemaApproved;
use crate::fold_db_core::infrastructure::message_bus::{AsyncMessageBus, Event};
use crate::schema::types::field::Field;
use crate::schema::types::{DeclarativeSchemaDefinition, Schema, SchemaError};
use crate::schema::{SchemaState, SchemaWithState};

/// Core schema management system that combines schema interpretation, validation, and management.
///
/// SchemaCore is responsible for:
/// - Loading and validating schemas from JSON
/// - Managing schema storage and persistence
/// - Handling schema field mappings
/// - Providing schema access and validation services
///
/// This unified component simplifies the schema system by combining the functionality
/// previously split across SchemaManager and SchemaInterpreter.
pub struct SchemaCore {
    /// Storage for loaded schemas
    schemas: Arc<Mutex<HashMap<String, Schema>>>,
    /// Storage for all schemas known to the system and their load state
    schema_states: Arc<Mutex<HashMap<String, SchemaState>>>,
    /// Unified database operations with storage abstraction
    db_ops: std::sync::Arc<crate::db_operations::DbOperations>,
    /// Message bus for event-driven communication
    message_bus: Arc<AsyncMessageBus>,
}

impl SchemaCore {
    /// Creates a new SchemaCore with DbOperations (storage abstraction)
    pub async fn new(
        db_ops: std::sync::Arc<crate::db_operations::DbOperations>,
        message_bus: Arc<AsyncMessageBus>,
    ) -> Result<Self, SchemaError> {
        // load schemas from db (async)
        let schemas = db_ops.get_all_schemas().await?;

        let schema_states = db_ops.get_all_schema_states().await?;

        let schema_core = Self {
            schemas: Arc::new(Mutex::new(schemas.clone())),
            schema_states: Arc::new(Mutex::new(schema_states)),
            db_ops,
            message_bus,
        };

        // Register transforms for all schemas that have transform_fields
        // This ensures transforms are available when schemas are loaded from database
        for (_, schema) in schemas {
            if let Some(transform_fields) = &schema.transform_fields {
                schema_core
                    .register_declarative_transforms(&schema, transform_fields)
                    .await?;
            }
        }

        Ok(schema_core)
    }

    pub fn get_schemas(&self) -> Result<HashMap<String, Schema>, SchemaError> {
        Ok(self
            .schemas
            .lock()
            .map_err(|_| SchemaError::InvalidData("Failed to acquire schemas lock".to_string()))?
            .clone())
    }

    pub fn get_schema_states(&self) -> Result<HashMap<String, SchemaState>, SchemaError> {
        Ok(self
            .schema_states
            .lock()
            .map_err(|_| {
                SchemaError::InvalidData("Failed to acquire schema_states lock".to_string())
            })?
            .clone())
    }

    pub fn get_schemas_with_states(&self) -> Result<Vec<SchemaWithState>, SchemaError> {
        let schemas = self.get_schemas()?;
        let schema_states = self.get_schema_states()?;

        let mut with_states = Vec::with_capacity(schemas.len());
        for (name, schema) in schemas {
            let state = schema_states.get(&name).copied().unwrap_or_default();
            with_states.push(SchemaWithState::new(schema, state));
        }

        Ok(with_states)
    }

    pub async fn set_schema_state(
        &self,
        schema_name: &str,
        schema_state: SchemaState,
    ) -> Result<(), SchemaError> {
        self.set_schema_state_with_backfill(schema_name, schema_state, None)
            .await
    }

    /// Approve a schema if it's not already approved (idempotent operation)
    /// This is a convenience method that checks the current state before approving
    pub async fn approve(&self, schema_name: &str) -> Result<(), SchemaError> {
        self.approve_with_backfill(schema_name, None).await
    }

    /// Approve a schema with optional backfill hash if it's not already approved
    pub async fn approve_with_backfill(
        &self,
        schema_name: &str,
        backfill_hash: Option<String>,
    ) -> Result<(), SchemaError> {
        // Check current state
        let current_state = self
            .get_schema_states()?
            .get(schema_name)
            .copied()
            .unwrap_or_default();

        // Only approve if not already approved
        if current_state != SchemaState::Approved {
            self.set_schema_state_with_backfill(schema_name, SchemaState::Approved, backfill_hash)
                .await?;
        }

        Ok(())
    }

    pub async fn set_schema_state_with_backfill(
        &self,
        schema_name: &str,
        schema_state: SchemaState,
        backfill_hash: Option<String>,
    ) -> Result<(), SchemaError> {
        if schema_state == SchemaState::Approved {
            self.apply_field_mappers(schema_name).await?;
        }

        // Persist to database first - this is the source of truth
        self.db_ops
            .store_schema_state(schema_name, &schema_state)
            .await?;

        // Update in-memory cache only after successful persistence
        self.schema_states
            .lock()
            .map_err(|_| {
                SchemaError::InvalidData("Failed to acquire schema_states lock".to_string())
            })?
            .insert(schema_name.to_string(), schema_state);

        // If schema is being approved, publish SchemaApproved event to trigger backfill
        if schema_state == SchemaState::Approved {
            let event = SchemaApproved {
                schema_name: schema_name.to_string(),
                backfill_hash,
            };
            self.message_bus
                .publish_event(Event::SchemaApproved(event))
                .await
                .map_err(|e| {
                    SchemaError::InvalidData(format!(
                        "Failed to publish SchemaApproved event: {}",
                        e
                    ))
                })?;
        }

        Ok(())
    }

    async fn apply_field_mappers(&self, schema_name: &str) -> Result<(), SchemaError> {
        let mut schema = self.db_ops.get_schema(schema_name).await?.ok_or_else(|| {
            SchemaError::InvalidData(format!("Schema '{}' not found in database", schema_name))
        })?;

        let Some(field_mappers) = schema.field_mappers().cloned() else {
            return Ok(());
        };

        if field_mappers.is_empty() {
            return Ok(());
        }

        let mut source_cache: HashMap<String, Schema> = HashMap::new();
        let mut updated = false;

        for (target_field, mapper) in field_mappers {
            let source_schema_name = mapper.source_schema().to_string();
            let source_schema = if let Some(schema) = source_cache.get(&source_schema_name) {
                schema
            } else {
                let fetched = self
                    .db_ops
                    .get_schema(&source_schema_name)
                    .await?
                    .ok_or_else(|| {
                        SchemaError::InvalidData(format!(
                            "Source schema '{}' for field mapper not found",
                            source_schema_name
                        ))
                    })?;
                source_cache.insert(source_schema_name.clone(), fetched);
                source_cache
                    .get(&source_schema_name)
                    .expect("source schema inserted")
            };

            let source_field = source_schema
                .runtime_fields
                .get(mapper.source_field())
                .ok_or_else(|| {
                    SchemaError::InvalidData(format!(
                        "Source field '{}.{}' not found for mapper",
                        source_schema_name,
                        mapper.source_field()
                    ))
                })?;

            let molecule_uuid =
                source_field
                    .common()
                    .molecule_uuid()
                    .cloned()
                    .ok_or_else(|| {
                        SchemaError::InvalidData(format!(
                            "Source field '{}.{}' is missing a molecule UUID",
                            source_schema_name,
                            mapper.source_field()
                        ))
                    })?;

            let target_runtime_field =
                schema
                    .runtime_fields
                    .get_mut(&target_field)
                    .ok_or_else(|| {
                        SchemaError::InvalidData(format!(
                            "Target field '{}' not found while applying field mapper",
                            target_field
                        ))
                    })?;

            target_runtime_field
                .common_mut()
                .set_molecule_uuid(molecule_uuid.clone());
            target_runtime_field
                .common_mut()
                .set_field_mappers(HashMap::from([(target_field.clone(), mapper.clone())]));

            updated = true;
        }

        if updated {
            schema.sync_molecule_uuids();
            self.db_ops.store_schema(schema_name, &schema).await?;
            self.schemas
                .lock()
                .map_err(|_| {
                    SchemaError::InvalidData("Failed to acquire schemas lock".to_string())
                })?
                .insert(schema_name.to_string(), schema);
        }

        Ok(())
    }

    pub async fn block_schema(&self, schema_name: &str) -> Result<(), SchemaError> {
        self.set_schema_state(schema_name, SchemaState::Blocked)
            .await
    }

    pub fn get_message_bus(&self) -> Arc<AsyncMessageBus> {
        Arc::clone(&self.message_bus)
    }

    pub fn get_schema(&self, schema_name: &str) -> Result<Option<Schema>, SchemaError> {
        Ok(self
            .schemas
            .lock()
            .map_err(|_| SchemaError::InvalidData("Failed to acquire schemas lock".to_string()))?
            .get(schema_name)
            .cloned())
    }

    /// Fetches a schema by name, checking both in-memory cache and database.
    /// This handles scenarios where the schema was added by another node (stale cache).
    /// Note: This is STRICTLY case-sensitive.
    pub async fn fetch_schema(&self, schema_name: &str) -> Result<Option<Schema>, SchemaError> {
        // 1. Try exact match in memory
        if let Some(schema) = self.get_schema(schema_name)? {
            return Ok(Some(schema));
        }

        // 2. Try exact match in database (refresh cache if found)
        if let Some(schema) = self
            .db_ops
            .get_schema(schema_name)
            .await
            .map_err(|e| SchemaError::InvalidData(e.to_string()))?
        {
            // Update memory
            self.load_schema_internal(schema.clone()).await?;
            log::info!(
                "Refreshed schema '{}' from database (stale cache)",
                schema.name
            );
            return Ok(Some(schema));
        }

        Ok(None)
    }

    pub fn add_schema_available(&self, schema: Schema) -> Result<(), SchemaError> {
        let mut schemas = self
            .schemas
            .lock()
            .map_err(|_| SchemaError::InvalidData("Failed to acquire schemas lock".to_string()))?;
        let mut schema_states = self.schema_states.lock().map_err(|_| {
            SchemaError::InvalidData("Failed to acquire schema_states lock".to_string())
        })?;
        schemas.insert(schema.name.clone(), schema.clone());
        schema_states.insert(schema.name.clone(), SchemaState::Available);
        Ok(())
    }

    pub async fn load_schema_internal(&self, schema: Schema) -> Result<(), SchemaError> {
        let name = schema.name.clone();

        // Check if schema exists in database
        let existing_schema = self.db_ops.get_schema(&name).await?;

        if existing_schema.is_some() {
            // Existing schema - update in-memory cache with new schema
            // The schema already has field_molecule_uuids persisted and restored
            {
                let mut schemas = self.schemas.lock().map_err(|_| {
                    SchemaError::InvalidData("Failed to acquire schemas lock".to_string())
                })?;
                schemas.insert(name.clone(), schema);
            } // Drop the lock before await

            // Preserve existing state from database
            let existing_state = self.db_ops.get_schema_state(&name).await?;
            let mut schema_states = self.schema_states.lock().map_err(|_| {
                SchemaError::InvalidData("Failed to acquire schema_states lock".to_string())
            })?;
            let state = existing_state.unwrap_or(SchemaState::Available);
            schema_states.insert(name.clone(), state);
        } else {
            // New schema - persist to database and update in-memory caches
            self.db_ops.store_schema(&name, &schema).await?;
            self.db_ops
                .store_schema_state(&name, &SchemaState::Available)
                .await?;

            {
                let mut schemas = self.schemas.lock().map_err(|_| {
                    SchemaError::InvalidData("Failed to acquire schemas lock".to_string())
                })?;
                schemas.insert(name.clone(), schema);
            } // Drop lock

            {
                let mut schema_states = self.schema_states.lock().map_err(|_| {
                    SchemaError::InvalidData("Failed to acquire schema_states lock".to_string())
                })?;
                schema_states.insert(name.clone(), SchemaState::Available);
            } // Drop lock
        }

        Ok(())
    }

    /// Load schema from JSON string (creates Available schema)
    /// Only supports declarative schema format
    pub async fn load_schema_from_json(&self, json_str: &str) -> Result<(), SchemaError> {
        // Parse JSON string to DeclarativeSchemaDefinition
        let declarative_schema: DeclarativeSchemaDefinition = serde_json::from_str(json_str)
            .map_err(|e| {
                SchemaError::InvalidData(format!("Failed to parse declarative schema: {}", e))
            })?;

        // Convert declarative schema to Schema
        let schema = self
            .interpret_declarative_schema(declarative_schema)
            .await?;

        // Load the schema using the existing method
        self.load_schema_internal(schema).await
    }

    /// Load schema from file (creates Available schema)
    /// Only supports declarative schema format
    pub async fn load_schema_from_file<P: AsRef<std::path::Path>>(
        &self,
        path: P,
    ) -> Result<(), SchemaError> {
        // Use the existing parse_schema_file method which handles declarative schemas
        if let Some(schema) = self.parse_schema_file(path.as_ref()).await? {
            self.load_schema_internal(schema).await
        } else {
            Err(SchemaError::InvalidData(
                "No schema found in file".to_string(),
            ))
        }
    }
    /// Load all schema files from a directory (creates Available schemas)
    /// Only processes .json files; ignores non-existent directories
    pub async fn load_schemas_from_directory<P: AsRef<std::path::Path>>(
        &self,
        directory: P,
    ) -> Result<usize, SchemaError> {
        let dir_path = directory.as_ref();
        if !dir_path.exists() {
            return Ok(0);
        }

        let mut loaded_count: usize = 0;
        let entries = fs::read_dir(dir_path).map_err(|e| {
            SchemaError::InvalidData(format!(
                "Failed to read directory {}: {}",
                dir_path.display(),
                e
            ))
        })?;

        for entry in entries {
            let entry = entry.map_err(|e| {
                SchemaError::InvalidData(format!(
                    "Failed to read entry in {}: {}",
                    dir_path.display(),
                    e
                ))
            })?;
            let path = entry.path();
            if path.extension().map(|ext| ext == "json").unwrap_or(false) {
                match self.load_schema_from_file(&path).await {
                    Ok(()) => {
                        loaded_count += 1;
                    }
                    Err(e) => {
                        log::warn!("Failed to load schema from file {}: {}", path.display(), e);
                    }
                }
            }
        }

        Ok(loaded_count)
    }

    /// Creates a new SchemaCore for testing purposes with a temporary database
    pub async fn new_for_testing() -> Result<Self, SchemaError> {
        let db = sled::Config::new()
            .temporary(true)
            .open()
            .map_err(|e| SchemaError::InvalidData(e.to_string()))?;
        let db_ops = std::sync::Arc::new(
            crate::db_operations::DbOperations::from_sled(db)
                .await
                .map_err(|e| SchemaError::InvalidData(e.to_string()))?,
        );
        let message_bus = Arc::new(AsyncMessageBus::new());
        Self::new(db_ops, message_bus).await
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn blogpost_schema_json() -> String {
        // Declarative schema format used in available_schemas
        // Minimal fields map is acceptable per current parser
        r#"{
            "name": "BlogPost",
            "key": { "range_field": "publish_date" },
            "fields": {
                "title": {},
                "content": {},
                "author": {},
                "publish_date": {}
            }
        }"#
        .to_string()
    }

    fn wordindex_schema_json() -> String {
        r#"{
            "name": "BlogPostWordIndex",
            "key": { "hash_field": "word", "range_field": "publish_date" },
            "fields": {
                "word": {},
                "publish_date": {}
            }
        }"#
        .to_string()
    }

    #[tokio::test]
    async fn new_for_testing_starts_with_empty_schemas() {
        let core = SchemaCore::new_for_testing().await.expect("init core");
        let schemas = core.get_schemas().expect("get_schemas");
        assert!(schemas.is_empty(), "expected no schemas at start");
    }

    #[tokio::test]
    async fn load_schema_from_json_adds_available_schema() {
        let core = SchemaCore::new_for_testing().await.expect("init core");
        core.load_schema_from_json(&blogpost_schema_json())
            .await
            .expect("load blogpost");

        let schemas = core.get_schemas().expect("get_schemas");
        assert!(
            schemas.contains_key("BlogPost"),
            "BlogPost should be loaded"
        );

        let states = core.get_schema_states().expect("get states");
        assert_eq!(states.get("BlogPost"), Some(&SchemaState::Available));
    }

    #[tokio::test]
    async fn get_schemas_with_states_returns_default_available() {
        let core = SchemaCore::new_for_testing().await.expect("init core");
        core.load_schema_from_json(&blogpost_schema_json())
            .await
            .expect("load blogpost");

        let schemas_with_states = core.get_schemas_with_states().expect("get with states");
        assert_eq!(schemas_with_states.len(), 1);
        let schema_entry = schemas_with_states
            .iter()
            .find(|entry| entry.name() == "BlogPost")
            .expect("BlogPost entry");
        assert_eq!(schema_entry.state, SchemaState::Available);
    }

    #[tokio::test]
    async fn load_multiple_schemas_from_json() {
        let core = SchemaCore::new_for_testing().await.expect("init core");
        core.load_schema_from_json(&blogpost_schema_json())
            .await
            .expect("load blogpost");
        core.load_schema_from_json(&wordindex_schema_json())
            .await
            .expect("load wordindex");

        let schemas = core.get_schemas().expect("get_schemas");
        assert!(schemas.contains_key("BlogPost"));
        assert!(schemas.contains_key("BlogPostWordIndex"));

        let states = core.get_schema_states().expect("get states");
        assert_eq!(states.get("BlogPost"), Some(&SchemaState::Available));
        assert_eq!(
            states.get("BlogPostWordIndex"),
            Some(&SchemaState::Available)
        );
    }

    #[tokio::test]
    async fn load_schema_from_file_works_with_declarative_format() {
        let core = SchemaCore::new_for_testing().await.expect("init core");
        let dir = tempfile::tempdir().expect("tempdir");
        let path = dir.path().join("BlogPost.json");
        std::fs::write(&path, blogpost_schema_json()).expect("write schema json");

        core.load_schema_from_file(&path)
            .await
            .expect("load from file");
        let schemas = core.get_schemas().expect("get_schemas");
        assert!(schemas.contains_key("BlogPost"));
    }

    #[tokio::test]
    async fn blogpost_wordindex_sets_hashrange_keyconfig() {
        use crate::schema::types::SchemaType;

        let core = SchemaCore::new_for_testing().await.expect("init core");
        core.load_schema_from_json(&wordindex_schema_json())
            .await
            .expect("load wordindex");

        let schemas = core.get_schemas().expect("get_schemas");
        let s = schemas.get("BlogPostWordIndex").expect("schema exists");

        // Verify schema_type is HashRange
        assert_eq!(s.schema_type, SchemaType::HashRange);

        // Verify key configuration
        let key = s.key.as_ref().expect("key should be present");
        assert_eq!(key.hash_field.as_deref(), Some("word"));
        assert_eq!(key.range_field.as_deref(), Some("publish_date"));
    }

    #[tokio::test]
    async fn load_wordindex_schema_from_file() {
        let core = SchemaCore::new_for_testing().await.expect("init core");
        let dir = tempfile::tempdir().expect("tempdir");
        let path = dir.path().join("BlogPostWordIndex.json");
        std::fs::write(&path, wordindex_schema_json()).expect("write schema json");

        core.load_schema_from_file(&path)
            .await
            .expect("load from file");
        let schemas = core.get_schemas().expect("get_schemas");
        assert!(schemas.contains_key("BlogPostWordIndex"));
    }
}