use std::sync::Arc;
use nonblocking::non_blocking;
use crate::ops::DagAlgorithm;
use crate::NameSet;
use crate::Result;
use crate::VerLink;
use crate::VertexName;
#[derive(Debug, Clone)]
pub(crate) struct DummyDag {
version: VerLink,
}
impl DummyDag {
pub fn new() -> Self {
Self {
version: VerLink::new(),
}
}
}
#[async_trait::async_trait]
impl DagAlgorithm for DummyDag {
async fn sort(&self, set: &NameSet) -> Result<NameSet> {
Ok(set.clone())
}
async fn parent_names(&self, name: VertexName) -> Result<Vec<VertexName>> {
let _ = name;
Ok(Vec::new())
}
async fn all(&self) -> Result<NameSet> {
crate::errors::programming("DummyDag does not support all()")
}
async fn master_group(&self) -> Result<NameSet> {
crate::errors::programming("DummyDag does not support master_group()")
}
async fn dirty(&self) -> Result<NameSet> {
Ok(NameSet::empty())
}
async fn ancestors(&self, set: NameSet) -> Result<NameSet> {
Ok(set)
}
async fn parents(&self, set: NameSet) -> Result<NameSet> {
let _ = set;
Ok(NameSet::empty())
}
async fn first_ancestor_nth(&self, name: VertexName, n: u64) -> Result<Option<VertexName>> {
if n == 0 {
Ok(Some(name))
} else {
crate::errors::programming("DummyDag does not resolve x~n where n > 1")
}
}
async fn heads(&self, set: NameSet) -> Result<NameSet> {
Ok(set)
}
async fn children(&self, set: NameSet) -> Result<NameSet> {
let _ = set;
Ok(NameSet::empty())
}
async fn roots(&self, set: NameSet) -> Result<NameSet> {
Ok(set)
}
async fn gca_one(&self, set: NameSet) -> Result<Option<VertexName>> {
if non_blocking(set.count())?? == 1 {
non_blocking(set.first())?
} else {
Ok(None)
}
}
async fn gca_all(&self, set: NameSet) -> Result<NameSet> {
self.common_ancestors(set).await
}
async fn common_ancestors(&self, set: NameSet) -> Result<NameSet> {
if non_blocking(set.count())?? == 1 {
Ok(set)
} else {
Ok(NameSet::empty())
}
}
async fn is_ancestor(&self, ancestor: VertexName, descendant: VertexName) -> Result<bool> {
Ok(ancestor == descendant)
}
async fn heads_ancestors(&self, set: NameSet) -> Result<NameSet> {
Ok(set)
}
async fn range(&self, roots: NameSet, heads: NameSet) -> Result<NameSet> {
Ok(roots & heads)
}
async fn descendants(&self, set: NameSet) -> Result<NameSet> {
Ok(set)
}
fn is_vertex_lazy(&self) -> bool {
false
}
fn dag_snapshot(&self) -> Result<Arc<dyn DagAlgorithm + Send + Sync>> {
Ok(Arc::new(self.clone()))
}
fn dag_id(&self) -> &str {
"dummy_dag"
}
fn dag_version(&self) -> &VerLink {
&self.version
}
}