mangle_db/source.rs
1// Copyright 2025 Google LLC
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! EDB source trait and supporting types.
16
17use anyhow::Result;
18use mangle_common::Value;
19
20/// Metadata about a relation provided by an EDB source.
21#[derive(Debug, Clone)]
22pub struct RelationInfo {
23 pub name: String,
24 pub estimated_rows: usize,
25}
26
27/// A fingerprint for staleness detection.
28/// Typically a SHA-256 hash of source metadata (file mtimes, sizes, etc.).
29#[derive(Debug, Clone, PartialEq, Eq)]
30pub struct Fingerprint(pub Vec<u8>);
31
32/// Readonly provider of extensional (base) facts.
33///
34/// Implementations load facts from external sources (files, databases, etc.)
35/// into the working store during `Database::open()`.
36pub trait EdbSource: Send + Sync {
37 /// A human-readable name for this source.
38 fn name(&self) -> &str;
39
40 /// Returns metadata about the relations this source provides.
41 fn relations(&self) -> Result<Vec<RelationInfo>>;
42
43 /// Returns all tuples for the given relation.
44 fn scan(&self, relation: &str) -> Result<Vec<Vec<Value>>>;
45
46 /// Returns a fingerprint for staleness detection.
47 /// `None` means "always recompute" (no caching possible).
48 fn fingerprint(&self) -> Result<Option<Fingerprint>>;
49}