pub struct ClientVariant<S: IClient> { /* private fields */ }
Expand description
Dgraph client has several variants which offer different behavior.
Implementations§
Source§impl ClientVariant<Http>
impl ClientVariant<Http>
Sourcepub fn new<S: TryInto<Uri>, E: Into<Endpoints<S>> + Debug>(
endpoints: E,
) -> Result<Self>
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");
Sourcepub fn new_with_endpoint_config<S: TryInto<Uri>, E: Into<Endpoints<S>> + Debug, C: EndpointConfig + 'static>(
endpoints: E,
endpoint_config: C,
) -> Result<Self>
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 endpointsendpoint_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>
impl<C: IClient> ClientVariant<C>
Sourcepub fn new_txn(&self) -> TxnType<C::Client>
pub fn new_txn(&self) -> TxnType<C::Client>
Return transaction in default state, which can be specialized into ReadOnly or Mutated
Sourcepub fn new_read_only_txn(&self) -> TxnReadOnlyType<C::Client>
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.
Sourcepub fn new_best_effort_txn(&self) -> TxnBestEffortType<C::Client>
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.
Sourcepub fn new_mutated_txn(&self) -> TxnMutatedType<C::Client>
pub fn new_mutated_txn(&self) -> TxnMutatedType<C::Client>
Create new transaction which can do mutate, commit and discard operations
Sourcepub async fn alter(&self, op: Operation) -> Result<Payload>
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(())
}
Sourcepub async fn set_schema<S: Into<String>>(&self, schema: S) -> Result<Payload>
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(())
}
Sourcepub async fn set_schema_in_background<S: Into<String>>(
&self,
schema: S,
) -> Result<Payload>
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(())
}
Sourcepub async fn drop_all(&self) -> Result<Payload>
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(())
}
Sourcepub async fn check_version(&self) -> Result<Version>
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: IClient> Deref for ClientVariant<S>
impl<S: IClient> Deref for ClientVariant<S>
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T
in a tonic::Request