surrealdb/api/method/
export.rs

1use crate::api::conn::Method;
2use crate::api::conn::MlConfig;
3use crate::api::conn::Param;
4use crate::api::Connection;
5use crate::api::Error;
6use crate::api::ExtraFeatures;
7use crate::api::Result;
8use crate::method::Model;
9use crate::method::OnceLockExt;
10use crate::opt::ExportDestination;
11use crate::Surreal;
12use channel::Receiver;
13use futures::Stream;
14use futures::StreamExt;
15use semver::Version;
16use std::borrow::Cow;
17use std::future::Future;
18use std::future::IntoFuture;
19use std::marker::PhantomData;
20use std::path::PathBuf;
21use std::pin::Pin;
22use std::task::Context;
23use std::task::Poll;
24
25/// A database export future
26#[derive(Debug)]
27#[must_use = "futures do nothing unless you `.await` or poll them"]
28pub struct Export<'r, C: Connection, R, T = ()> {
29	pub(super) client: Cow<'r, Surreal<C>>,
30	pub(super) target: ExportDestination,
31	pub(super) ml_config: Option<MlConfig>,
32	pub(super) response: PhantomData<R>,
33	pub(super) export_type: PhantomData<T>,
34}
35
36impl<'r, C, R> Export<'r, C, R>
37where
38	C: Connection,
39{
40	/// Export machine learning model
41	pub fn ml(self, name: &str, version: Version) -> Export<'r, C, R, Model> {
42		Export {
43			client: self.client,
44			target: self.target,
45			ml_config: Some(MlConfig::Export {
46				name: name.to_owned(),
47				version: version.to_string(),
48			}),
49			response: self.response,
50			export_type: PhantomData,
51		}
52	}
53}
54
55impl<C, R, T> Export<'_, C, R, T>
56where
57	C: Connection,
58{
59	/// Converts to an owned type which can easily be moved to a different thread
60	pub fn into_owned(self) -> Export<'static, C, R, T> {
61		Export {
62			client: Cow::Owned(self.client.into_owned()),
63			..self
64		}
65	}
66}
67
68impl<'r, Client, T> IntoFuture for Export<'r, Client, PathBuf, T>
69where
70	Client: Connection,
71{
72	type Output = Result<()>;
73	type IntoFuture = Pin<Box<dyn Future<Output = Self::Output> + Send + Sync + 'r>>;
74
75	fn into_future(self) -> Self::IntoFuture {
76		Box::pin(async move {
77			let router = self.client.router.extract()?;
78			if !router.features.contains(&ExtraFeatures::Backup) {
79				return Err(Error::BackupsNotSupported.into());
80			}
81			let mut conn = Client::new(Method::Export);
82			let mut param = match self.target {
83				ExportDestination::File(path) => Param::file(path),
84				ExportDestination::Memory => unreachable!(),
85			};
86			param.ml_config = self.ml_config;
87			conn.execute_unit(router, param).await
88		})
89	}
90}
91
92impl<'r, Client, T> IntoFuture for Export<'r, Client, (), T>
93where
94	Client: Connection,
95{
96	type Output = Result<Backup>;
97	type IntoFuture = Pin<Box<dyn Future<Output = Self::Output> + Send + Sync + 'r>>;
98
99	fn into_future(self) -> Self::IntoFuture {
100		Box::pin(async move {
101			let router = self.client.router.extract()?;
102			if !router.features.contains(&ExtraFeatures::Backup) {
103				return Err(Error::BackupsNotSupported.into());
104			}
105			let (tx, rx) = crate::channel::bounded(1);
106			let mut conn = Client::new(Method::Export);
107			let ExportDestination::Memory = self.target else {
108				unreachable!();
109			};
110			let mut param = Param::bytes_sender(tx);
111			param.ml_config = self.ml_config;
112			conn.execute_unit(router, param).await?;
113			Ok(Backup {
114				rx,
115			})
116		})
117	}
118}
119
120/// A stream of exported data
121#[derive(Debug, Clone)]
122#[must_use = "streams do nothing unless you poll them"]
123pub struct Backup {
124	rx: Receiver<Result<Vec<u8>>>,
125}
126
127impl Stream for Backup {
128	type Item = Result<Vec<u8>>;
129
130	fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
131		self.as_mut().rx.poll_next_unpin(cx)
132	}
133}