use async_trait::async_trait;
use schema_core::{GenericValue, IndexMapping, IndexName, TableName};
use crate::{Result, RowKey, SnapshotTable};
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct DocumentId {
pub index: IndexName,
pub key: RowKey,
}
#[derive(Debug, Clone)]
pub enum Document {
Upsert { id: DocumentId, body: GenericValue },
Delete { id: DocumentId },
}
impl Document {
pub fn id(&self) -> &DocumentId {
match self {
Document::Upsert { id, .. } | Document::Delete { id } => id,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct IndexScope {
pub index: IndexName,
pub root: SnapshotTable,
}
#[async_trait]
pub trait DocumentBuilder: std::fmt::Debug + Send + Sync {
async fn resolve(&self, table: &TableName, key: &RowKey) -> Result<Vec<DocumentId>>;
async fn build(&self, id: &DocumentId) -> Result<Document>;
async fn build_many(&self, ids: &[DocumentId]) -> Result<Vec<Document>> {
let mut out = Vec::with_capacity(ids.len());
for id in ids {
out.push(self.build(id).await?);
}
Ok(out)
}
fn backfill_scopes(&self) -> Vec<IndexScope> {
Vec::new()
}
async fn index_mappings(&self) -> Result<Vec<IndexMapping>> {
Ok(Vec::new())
}
}