Lines
88.35 %
Functions
41.98 %
Branches
100 %
use std::ops::Deref;
use std::sync::Arc;
use async_trait::async_trait;
use bonsaidb_core::connection::{
AccessPolicy, AsyncConnection, AsyncLowLevelConnection, HasSchema, HasSession, Range,
SerializedQueryKey, Session, Sort,
};
use bonsaidb_core::document::{DocumentId, Header, OwnedDocument};
use bonsaidb_core::networking::{
ApplyTransaction, Compact, CompactCollection, CompactKeyValueStore, Count, DeleteDocs, Get,
GetMultiple, LastTransactionId, List, ListExecutedTransactions, ListHeaders, Query,
QueryWithDocs, Reduce, ReduceGrouped,
use bonsaidb_core::schema::view::map::MappedSerializedValue;
use bonsaidb_core::schema::{self, CollectionName, Schematic, ViewName};
use bonsaidb_core::transaction::{Executed, OperationResult, Transaction};
use crate::AsyncClient;
mod pubsub;
pub use pubsub::*;
mod keyvalue;
/// A database on a remote server.
#[derive(Debug, Clone)]
pub struct AsyncRemoteDatabase {
pub(crate) client: AsyncClient,
pub(crate) name: Arc<String>,
pub(crate) schema: Arc<Schematic>,
}
impl AsyncRemoteDatabase {
/// Returns the name of the database.
#[must_use]
pub fn name(&self) -> &str {
self.name.as_ref()
impl Deref for AsyncRemoteDatabase {
type Target = AsyncClient;
fn deref(&self) -> &Self::Target {
&self.client
pub(crate) fn new(client: AsyncClient, name: String, schema: Arc<Schematic>) -> Self {
Self {
client,
name: Arc::new(name),
schema,
impl HasSession for AsyncRemoteDatabase {
fn session(&self) -> Option<&Session> {
self.client.session()
#[async_trait]
impl AsyncConnection for AsyncRemoteDatabase {
type Storage = AsyncClient;
fn storage(&self) -> Self::Storage {
self.client.clone()
async fn list_executed_transactions(
&self,
starting_id: Option<u64>,
result_limit: Option<u32>,
) -> Result<Vec<Executed>, bonsaidb_core::Error> {
Ok(self
.client
.send_api_request(&ListExecutedTransactions {
database: self.name.to_string(),
starting_id,
result_limit,
})
.await?)
async fn last_transaction_id(&self) -> Result<Option<u64>, bonsaidb_core::Error> {
.send_api_request(&LastTransactionId {
async fn compact(&self) -> Result<(), bonsaidb_core::Error> {
self.send_api_request(&Compact {
.await?;
Ok(())
async fn compact_key_value_store(&self) -> Result<(), bonsaidb_core::Error> {
self.send_api_request(&CompactKeyValueStore {
impl AsyncLowLevelConnection for AsyncRemoteDatabase {
async fn apply_transaction(
transaction: Transaction,
) -> Result<Vec<OperationResult>, bonsaidb_core::Error> {
.send_api_request(&ApplyTransaction {
transaction,
async fn get_from_collection(
id: DocumentId,
collection: &CollectionName,
) -> Result<Option<OwnedDocument>, bonsaidb_core::Error> {
.send_api_request(&Get {
collection: collection.clone(),
id,
async fn get_multiple_from_collection(
ids: &[DocumentId],
) -> Result<Vec<OwnedDocument>, bonsaidb_core::Error> {
.send_api_request(&GetMultiple {
ids: ids.to_vec(),
async fn list_from_collection(
ids: Range<DocumentId>,
order: Sort,
limit: Option<u32>,
.send_api_request(&List {
ids,
order,
limit,
async fn list_headers_from_collection(
) -> Result<Vec<Header>, bonsaidb_core::Error> {
.send_api_request(&ListHeaders(List {
}))
async fn count_from_collection(
) -> Result<u64, bonsaidb_core::Error> {
.send_api_request(&Count {
async fn compact_collection_by_name(
collection: CollectionName,
) -> Result<(), bonsaidb_core::Error> {
self.send_api_request(&CompactCollection {
name: collection,
async fn query_by_name(
view: &ViewName,
key: Option<SerializedQueryKey>,
access_policy: AccessPolicy,
) -> Result<Vec<schema::view::map::Serialized>, bonsaidb_core::Error> {
.send_api_request(&Query {
view: view.clone(),
key,
access_policy,
async fn query_by_name_with_docs(
) -> Result<schema::view::map::MappedSerializedDocuments, bonsaidb_core::Error> {
.send_api_request(&QueryWithDocs(Query {
async fn reduce_by_name(
) -> Result<Vec<u8>, bonsaidb_core::Error> {
.send_api_request(&Reduce {
.await?
.into_vec())
async fn reduce_grouped_by_name(
) -> Result<Vec<MappedSerializedValue>, bonsaidb_core::Error> {
.send_api_request(&ReduceGrouped(Reduce {
async fn delete_docs_by_name(
.send_api_request(&DeleteDocs {
impl HasSchema for AsyncRemoteDatabase {
fn schematic(&self) -> &Schematic {
&self.schema