Struct ClientVariant

Source
pub struct ClientVariant<S: IClient> { /* private fields */ }
Expand description

Dgraph client has several variants which offer different behavior.

Implementations§

Source§

impl ClientVariant<Http>

Source

pub fn new<S: TryInto<Uri>, E: Into<Endpoints<S>> + Debug>( endpoints: E, ) -> Result<Self>

Create new Dgraph client for interacting v DB.

The client can be backed by multiple endpoints (to the same server, or multiple servers in a cluster).

§Arguments
  • endpoints - one endpoint or vector of endpoints
§Errors
  • endpoints vector is empty
  • item in vector cannot by converted into Uri
§Example
use dgraph_tonic::Client;

// vector of endpoints
let client = Client::new(vec!["http://127.0.0.1:19080", "http://127.0.0.1:19080"]).expect("Dgraph client");
// one endpoint
let client = Client::new("http://127.0.0.1:19080").expect("Dgraph client");
Source

pub fn new_with_endpoint_config<S: TryInto<Uri>, E: Into<Endpoints<S>> + Debug, C: EndpointConfig + 'static>( endpoints: E, endpoint_config: C, ) -> Result<Self>

Create new Dgraph client with custom endpoint configuration for interacting with DB.

The client can be backed by multiple endpoints (to the same server, or multiple servers in a cluster).

§Arguments
  • endpoints - one endpoint or vector of endpoints
  • endpoint_config - custom endpoint configuration
§Errors
  • endpoints vector is empty
  • item in vector cannot by converted into Uri
§Example
use dgraph_tonic::{Endpoint, EndpointConfig, Client};

use std::time::Duration;

#[derive(Debug, Default, Clone)]
struct EndpointWithTimeout {}

impl EndpointConfig for EndpointWithTimeout {
    fn configure_endpoint(&self, endpoint: Endpoint) -> Endpoint {
        endpoint.timeout(Duration::from_secs(5))
    }
}

// custom configuration
let endpoint_config = EndpointWithTimeout::default();
// vector of endpoints
let client = Client::new_with_endpoint_config(vec!["http://127.0.0.1:19080", "http://127.0.0.1:19080"], endpoint_config.clone()).expect("Dgraph client");
// one endpoint
let client = Client::new_with_endpoint_config("http://127.0.0.1:19080", endpoint_config).expect("Dgraph client");
Source§

impl<C: IClient> ClientVariant<C>

Source

pub fn new_txn(&self) -> TxnType<C::Client>

Return transaction in default state, which can be specialized into ReadOnly or Mutated

Source

pub fn new_read_only_txn(&self) -> TxnReadOnlyType<C::Client>

Create new transaction which can only do queries.

Read-only transactions are useful to increase read speed because they can circumvent the usual consensus protocol.

Source

pub fn new_best_effort_txn(&self) -> TxnBestEffortType<C::Client>

Create new transaction which can only do queries in best effort mode.

Read-only queries can optionally be set as best-effort. Using this flag will ask the Dgraph Alpha to try to get timestamps from memory on a best-effort basis to reduce the number of outbound requests to Zero. This may yield improved latencies in read-bound workloads where linearizable reads are not strictly needed.

Source

pub fn new_mutated_txn(&self) -> TxnMutatedType<C::Client>

Create new transaction which can do mutate, commit and discard operations

Source

pub async fn alter(&self, op: Operation) -> Result<Payload>

The /alter endpoint is used to create or change the schema.

§Arguments
  • op: Alter operation
§Errors
  • gRPC error
  • DB reject alter command
§Example

Install a schema into dgraph. A name predicate is string type and has exact index.

use dgraph_tonic::{Client, Operation};
#[cfg(feature = "acl")]
use dgraph_tonic::{AclClientType, LazyChannel};

#[cfg(not(feature = "acl"))]
async fn client() -> Client {
    Client::new("http://127.0.0.1:19080").expect("Dgraph client")
}

#[cfg(feature = "acl")]
async fn client() -> AclClientType<LazyChannel> {
    let default = Client::new("http://127.0.0.1:19080").unwrap();
    default.login("groot", "password").await.expect("Acl client")
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let client = client().await;
    let op = Operation {
        schema: "name: string @index(exact) .".into(),
        ..Default::default()
    };
    client.alter(op).await.expect("Schema is not updated");
    Ok(())
}
Source

pub async fn set_schema<S: Into<String>>(&self, schema: S) -> Result<Payload>

Create or change the schema.

§Arguments
  • schema: Schema modification
§Errors
  • gRPC error
  • DB reject alter command
§Example

Install a schema into dgraph. A name predicate is string type and has exact index.

use dgraph_tonic::{Client, Operation};
#[cfg(feature = "acl")]
use dgraph_tonic::{AclClientType, LazyChannel};

#[cfg(not(feature = "acl"))]
async fn client() -> Client {
    Client::new("http://127.0.0.1:19080").expect("Dgraph client")
}

#[cfg(feature = "acl")]
async fn client() -> AclClientType<LazyChannel> {
    let default = Client::new("http://127.0.0.1:19080").unwrap();
    default.login("groot", "password").await.expect("Acl client")
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let client = client().await;
    client.set_schema("name: string @index(exact) .").await.expect("Schema is not updated");
    Ok(())
}
Source

pub async fn set_schema_in_background<S: Into<String>>( &self, schema: S, ) -> Result<Payload>

Create or change the schema in background.

§Arguments
  • schema: Schema modification
§Errors
  • gRPC error
  • DB reject alter command
§Example

Install a schema into dgraph. A name predicate is string type and has exact index.

use dgraph_tonic::{Client, Operation};
#[cfg(feature = "acl")]
use dgraph_tonic::{AclClientType, LazyChannel};

#[cfg(not(feature = "acl"))]
async fn client() -> Client {
    Client::new("http://127.0.0.1:19080").expect("Dgraph client")
}

#[cfg(feature = "acl")]
async fn client() -> AclClientType<LazyChannel> {
    let default = Client::new("http://127.0.0.1:19080").unwrap();
    default.login("groot", "password").await.expect("Acl client")
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let client = client().await;
    client.set_schema_in_background("name: string @index(exact) .").await.expect("Schema is not updated");
    Ok(())
}
Source

pub async fn drop_all(&self) -> Result<Payload>

Drop all data in DB

§Errors
  • gRPC error
  • DB reject alter command
§Example
use dgraph_tonic::{Client, Operation};
#[cfg(feature = "acl")]
use dgraph_tonic::{AclClientType, LazyChannel};

#[cfg(not(feature = "acl"))]
async fn client() -> Client {
    Client::new("http://127.0.0.1:19080").expect("Dgraph client")
}

#[cfg(feature = "acl")]
async fn client() -> AclClientType<LazyChannel> {
    let default = Client::new("http://127.0.0.1:19080").unwrap();
    default.login("groot", "password").await.expect("Acl client")
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let client = client().await;
    client.drop_all().await.expect("Data not dropped");
    Ok(())
}
Source

pub async fn check_version(&self) -> Result<Version>

Check DB version

§Errors
  • gRPC error
§Example
use dgraph_tonic::{Client, Operation};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let client = Client::new(vec!["http://127.0.0.1:19080"]).expect("Dgraph client");
    let version = client.check_version().await.expect("Version");
    println!("{:#?}", version);
    Ok(())
}

Trait Implementations§

Source§

impl<S: Debug + IClient> Debug for ClientVariant<S>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<S: IClient> Deref for ClientVariant<S>

Source§

type Target = Box<ClientState>

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.
Source§

impl<S: IClient> DerefMut for ClientVariant<S>

Source§

fn deref_mut(&mut self) -> &mut Self::Target

Mutably dereferences the value.

Auto Trait Implementations§

§

impl<S> Freeze for ClientVariant<S>
where S: Freeze,

§

impl<S> RefUnwindSafe for ClientVariant<S>
where S: RefUnwindSafe,

§

impl<S> Send for ClientVariant<S>

§

impl<S> Sync for ClientVariant<S>

§

impl<S> Unpin for ClientVariant<S>
where S: Unpin,

§

impl<S> UnwindSafe for ClientVariant<S>
where S: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoRequest<T> for T

Source§

fn into_request(self) -> Request<T>

Wrap the input message T in a tonic::Request
Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more