use std::collections::hash_map::RandomState;
use std::collections::HashMap;
use std::fmt::Debug;
use std::hash::Hash;
use std::sync::{Arc, Mutex};
use anyhow::Result;
use async_trait::async_trait;
use tokio::runtime::Runtime;
use crate::client::ILazyClient;
use crate::sync::txn::{IState, Query, TxnType, TxnVariant};
use crate::txn::mutated::Mutate as AsyncMutate;
#[cfg(any(feature = "dgraph-1-1", feature = "dgraph-21-03"))]
use crate::txn::mutated::UpsertMutation;
use crate::txn::TxnMutatedType as AsyncMutatedTxn;
#[cfg(feature = "dgraph-1-0")]
use crate::Assigned;
use crate::Mutation;
use crate::Query as AsyncQuery;
use crate::Response;
#[cfg(feature = "dgraph-1-0")]
pub type MutationResponse = Assigned;
#[cfg(any(feature = "dgraph-1-1", feature = "dgraph-21-03"))]
pub type MutationResponse = Response;
#[derive(Clone, Debug)]
pub struct Mutated<C: ILazyClient> {
pub(crate) rt: Arc<Runtime>,
pub(crate) async_txn: Arc<Mutex<AsyncMutatedTxn<C>>>,
}
#[async_trait]
impl<C: ILazyClient> IState for Mutated<C> {
fn query_with_vars<Q, K, V>(
&mut self,
query: Q,
vars: HashMap<K, V, RandomState>,
) -> Result<Response>
where
Q: Into<String> + Send + Sync,
K: Into<String> + Send + Sync + Eq + Hash,
V: Into<String> + Send + Sync,
{
let async_txn = Arc::clone(&self.async_txn);
self.rt.block_on(async move {
let mut async_txn = async_txn.lock().expect("Async Txn");
async_txn.query_with_vars(query, vars).await
})
}
#[cfg(any(feature = "dgraph-1-1", feature = "dgraph-21-03"))]
fn query_rdf_with_vars<Q, K, V>(&mut self, query: Q, vars: HashMap<K, V>) -> Result<Response>
where
Q: Into<String> + Send + Sync,
K: Into<String> + Send + Sync + Eq + Hash,
V: Into<String> + Send + Sync,
{
let async_txn = Arc::clone(&self.async_txn);
self.rt.block_on(async move {
let mut async_txn = async_txn.lock().expect("Async Txn");
async_txn.query_rdf_with_vars(query, vars).await
})
}
}
pub type TxnMutatedType<C> = TxnVariant<Mutated<C>>;
impl<C: ILazyClient> TxnType<C> {
pub fn mutated(self) -> TxnMutatedType<C> {
let rt = self.extra.rt;
let txn = self
.extra
.async_txn
.lock()
.expect("Txn")
.to_owned()
.mutated();
TxnVariant {
state: self.state,
extra: Mutated {
rt,
async_txn: Arc::new(Mutex::new(txn)),
},
}
}
}
pub trait Mutate: Query {
fn discard(self) -> Result<()>;
fn commit(self) -> Result<()>;
fn mutate(&mut self, mu: Mutation) -> Result<MutationResponse>;
fn mutate_and_commit_now(self, mu: Mutation) -> Result<MutationResponse>;
#[cfg(any(feature = "dgraph-1-1", feature = "dgraph-21-03"))]
fn upsert<Q, M>(&mut self, query: Q, mu: M) -> Result<MutationResponse>
where
Q: Into<String> + Send + Sync,
M: Into<UpsertMutation> + Send + Sync;
#[cfg(any(feature = "dgraph-1-1", feature = "dgraph-21-03"))]
fn upsert_and_commit_now<Q, M>(self, query: Q, mu: M) -> Result<MutationResponse>
where
Q: Into<String> + Send + Sync,
M: Into<UpsertMutation> + Send + Sync;
#[cfg(any(feature = "dgraph-1-1", feature = "dgraph-21-03"))]
fn upsert_with_vars<Q, K, V, M>(
&mut self,
query: Q,
vars: HashMap<K, V>,
mu: M,
) -> Result<MutationResponse>
where
Q: Into<String> + Send + Sync,
K: Into<String> + Send + Sync + Eq + Hash,
V: Into<String> + Send + Sync,
M: Into<UpsertMutation> + Send + Sync;
#[cfg(any(feature = "dgraph-1-1", feature = "dgraph-21-03"))]
fn upsert_with_vars_and_commit_now<Q, K, V, M>(
self,
query: Q,
vars: HashMap<K, V>,
mu: M,
) -> Result<MutationResponse>
where
Q: Into<String> + Send + Sync,
K: Into<String> + Send + Sync + Eq + Hash,
V: Into<String> + Send + Sync,
M: Into<UpsertMutation> + Send + Sync;
}
impl<C: ILazyClient> Mutate for TxnMutatedType<C> {
fn discard(self) -> Result<()> {
let async_txn = self.extra.async_txn;
self.extra.rt.block_on(async move {
let async_txn = async_txn.lock().expect("MutatedTxn").to_owned();
async_txn.discard().await
})
}
fn commit(self) -> Result<()> {
let async_txn = self.extra.async_txn;
self.extra.rt.block_on(async move {
let async_txn = async_txn.lock().expect("MutatedTxn").to_owned();
async_txn.commit().await
})
}
fn mutate(&mut self, mu: Mutation) -> Result<MutationResponse> {
let async_txn = Arc::clone(&self.extra.async_txn);
self.extra.rt.block_on(async move {
let mut async_txn = async_txn.lock().expect("MutatedTxn");
async_txn.mutate(mu).await
})
}
fn mutate_and_commit_now(self, mu: Mutation) -> Result<MutationResponse> {
let async_txn = self.extra.async_txn;
self.extra.rt.block_on(async move {
let async_txn = async_txn.lock().expect("MutatedTxn").to_owned();
async_txn.mutate_and_commit_now(mu).await
})
}
#[cfg(any(feature = "dgraph-1-1", feature = "dgraph-21-03"))]
fn upsert<Q, M>(&mut self, query: Q, mu: M) -> Result<MutationResponse>
where
Q: Into<String> + Send + Sync,
M: Into<UpsertMutation> + Send + Sync,
{
let async_txn = Arc::clone(&self.extra.async_txn);
self.extra.rt.block_on(async move {
let mut async_txn = async_txn.lock().expect("MutatedTxn");
async_txn.upsert(query, mu).await
})
}
#[cfg(any(feature = "dgraph-1-1", feature = "dgraph-21-03"))]
fn upsert_and_commit_now<Q, M>(self, query: Q, mu: M) -> Result<MutationResponse>
where
Q: Into<String> + Send + Sync,
M: Into<UpsertMutation> + Send + Sync,
{
let async_txn = self.extra.async_txn;
self.extra.rt.block_on(async move {
let async_txn = async_txn.lock().expect("MutatedTxn").to_owned();
async_txn.upsert_and_commit_now(query, mu).await
})
}
#[cfg(any(feature = "dgraph-1-1", feature = "dgraph-21-03"))]
fn upsert_with_vars<Q, K, V, M>(
&mut self,
query: Q,
vars: HashMap<K, V>,
mu: M,
) -> Result<MutationResponse>
where
Q: Into<String> + Send + Sync,
K: Into<String> + Send + Sync + Eq + Hash,
V: Into<String> + Send + Sync,
M: Into<UpsertMutation> + Send + Sync,
{
let async_txn = Arc::clone(&self.extra.async_txn);
self.extra.rt.block_on(async move {
let mut async_txn = async_txn.lock().expect("MutatedTxn");
async_txn.upsert_with_vars(query, vars, mu).await
})
}
#[cfg(any(feature = "dgraph-1-1", feature = "dgraph-21-03"))]
fn upsert_with_vars_and_commit_now<Q, K, V, M>(
self,
query: Q,
vars: HashMap<K, V>,
mu: M,
) -> Result<MutationResponse>
where
Q: Into<String> + Send + Sync,
K: Into<String> + Send + Sync + Eq + Hash,
V: Into<String> + Send + Sync,
M: Into<UpsertMutation> + Send + Sync,
{
let async_txn = self.extra.async_txn;
self.extra.rt.block_on(async move {
let async_txn = async_txn.lock().expect("MutatedTxn").to_owned();
async_txn
.upsert_with_vars_and_commit_now(query, vars, mu)
.await
})
}
}