use super::Record;
use crate::definition::FieldRef;
use crate::error::BaseError;
use async_trait::async_trait;
use serde_json::Value;
use std::collections::{BTreeMap, HashMap};
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RelationSpec {
source: FieldRef,
target: FieldRef,
display: Vec<FieldRef>,
#[cfg(feature = "mysql")]
target_table_ref: yang_db::TableRef,
#[cfg(feature = "mysql")]
target_field_ref: yang_db::FieldRef,
#[cfg(feature = "mysql")]
display_field_refs: Vec<yang_db::FieldRef>,
}
impl RelationSpec {
pub fn new(source: FieldRef, target: FieldRef) -> Self {
#[cfg(feature = "mysql")]
let target_table_ref =
yang_db::TableRef::__from_validated_owned(target.table().as_str().to_string());
#[cfg(feature = "mysql")]
let target_field_ref =
yang_db::FieldRef::__from_validated_owned(target.field().as_str().to_string());
Self {
source,
target,
display: Vec::new(),
#[cfg(feature = "mysql")]
target_table_ref,
#[cfg(feature = "mysql")]
target_field_ref,
#[cfg(feature = "mysql")]
display_field_refs: Vec::new(),
}
}
pub fn display<I>(mut self, fields: I) -> Self
where
I: IntoIterator<Item = FieldRef>,
{
self.display = fields.into_iter().collect();
#[cfg(feature = "mysql")]
{
self.display_field_refs = self
.display
.iter()
.map(|field| {
yang_db::FieldRef::__from_validated_owned(field.field().as_str().to_string())
})
.collect();
}
self
}
pub fn source(&self) -> &FieldRef {
&self.source
}
pub fn target(&self) -> &FieldRef {
&self.target
}
pub fn display_fields(&self) -> &[FieldRef] {
&self.display
}
}
#[cfg(feature = "mysql")]
pub struct DatabaseRelationExecutor<'a> {
database: &'a yang_db::Database,
}
#[cfg(feature = "mysql")]
impl<'a> DatabaseRelationExecutor<'a> {
pub const fn new(database: &'a yang_db::Database) -> Self {
Self { database }
}
}
#[cfg(feature = "mysql")]
#[async_trait]
impl RelationBatchExecutor for DatabaseRelationExecutor<'_> {
async fn load_batch(&self, batch: &RelationBatch) -> Result<Vec<Record>, BaseError> {
let spec = batch.spec();
let mut query = self
.database
.table(&spec.target_table_ref)
.field(&spec.target_field_ref);
for field in &spec.display_field_refs {
query = query.field(field);
}
query
.where_in(&spec.target_field_ref, batch.keys().to_vec())
.select::<Record>()
.await
.map_err(BaseError::DatabaseQueryFailed)
}
}
#[derive(Debug, Clone)]
pub struct RelationBatch {
spec: RelationSpec,
keys: Vec<Value>,
}
impl RelationBatch {
pub fn spec(&self) -> &RelationSpec {
&self.spec
}
pub fn keys(&self) -> &[Value] {
&self.keys
}
}
#[async_trait]
pub trait RelationBatchExecutor: Send + Sync {
async fn load_batch(&self, batch: &RelationBatch) -> Result<Vec<Record>, BaseError>;
}
pub type RelationData = HashMap<String, Vec<Record>>;
#[derive(Debug, Clone, Default)]
pub struct RelationLoader {
relations: Vec<RelationSpec>,
}
impl RelationLoader {
pub fn new(relations: Vec<RelationSpec>) -> Self {
Self { relations }
}
pub fn plan(&self, rows: &[Record]) -> Vec<RelationBatch> {
self.relations
.iter()
.filter_map(|relation| {
let mut values = BTreeMap::new();
for row in rows {
let Some(value) = row.get(relation.source.field().as_str()) else {
continue;
};
if value.is_null() {
continue;
}
if let Ok(key) = serde_json::to_string(value) {
values.entry(key).or_insert_with(|| value.clone());
}
}
(!values.is_empty()).then(|| RelationBatch {
spec: relation.clone(),
keys: values.into_values().collect(),
})
})
.collect()
}
pub async fn load<E>(&self, rows: &[Record], executor: &E) -> Result<RelationData, BaseError>
where
E: RelationBatchExecutor,
{
let mut data = HashMap::new();
for batch in self.plan(rows) {
let source = batch.spec.source().to_string();
data.insert(source, executor.load_batch(&batch).await?);
}
Ok(data)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::definition::{FieldName, TableName};
use std::sync::atomic::{AtomicUsize, Ordering};
fn field(table: &str, field: &str) -> FieldRef {
FieldRef::new(
TableName::new(table).expect("合法表名"),
FieldName::new(field).expect("合法字段名"),
)
}
struct CountingExecutor(AtomicUsize);
#[async_trait]
impl RelationBatchExecutor for CountingExecutor {
async fn load_batch(&self, _batch: &RelationBatch) -> Result<Vec<Record>, BaseError> {
self.0.fetch_add(1, Ordering::Relaxed);
Ok(Vec::new())
}
}
#[tokio::test]
async fn query_count_depends_on_relation_kinds_not_row_count() {
let loader = RelationLoader::new(vec![RelationSpec::new(
field("orders", "user_id"),
field("users", "id"),
)]);
let rows = (0..1_000)
.map(|index| Record::new().set("user_id", index % 10))
.collect::<Vec<_>>();
let executor = CountingExecutor(AtomicUsize::new(0));
let batches = loader.plan(&rows);
assert_eq!(batches.len(), 1);
assert_eq!(batches[0].keys().len(), 10);
loader
.load(&rows, &executor)
.await
.expect("批量关系加载应成功");
assert_eq!(executor.0.load(Ordering::Relaxed), 1);
}
}