1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
use std::future::Future;
use tonic::codegen::InterceptedService;
use tonic::transport::Channel;
use tonic::Status;
use crate::auth::TokenInterceptor;
use crate::qdrant::snapshots_client::SnapshotsClient;
use crate::qdrant::{
CreateFullSnapshotRequest, CreateSnapshotRequest, CreateSnapshotResponse,
DeleteFullSnapshotRequest, DeleteSnapshotRequest, DeleteSnapshotResponse,
ListFullSnapshotsRequest, ListSnapshotsRequest, ListSnapshotsResponse,
};
use crate::qdrant_client::{Qdrant, QdrantResult};
/// # Snapshot operations
///
/// Create, recover and manage snapshots for collections or a full Qdrant instance.
///
/// Documentation: <https://qdrant.tech/documentation/concepts/snapshots/>
impl Qdrant {
async fn with_snapshot_client<T, O: Future<Output = Result<T, Status>>>(
&self,
f: impl Fn(SnapshotsClient<InterceptedService<Channel, TokenInterceptor>>) -> O,
) -> QdrantResult<T> {
let result = self
.channel
.with_channel(
|channel| {
let service = self.with_api_key(channel);
let mut client =
SnapshotsClient::new(service).max_decoding_message_size(usize::MAX);
if let Some(compression) = self.config.compression {
client = client
.send_compressed(compression.into())
.accept_compressed(compression.into());
}
f(client)
},
false,
)
.await?;
Ok(result)
}
/// Create snapshot of a collection on this node.
///
/// ```no_run
///# use qdrant_client::{Qdrant, QdrantError};
///# async fn create_snapshot(client: &Qdrant)
///# -> Result<(), QdrantError> {
/// client.create_snapshot("my_collection").await?;
///# Ok(())
///# }
/// ```
///
/// Note: Snapshots are node-local. They only contain data of a single node. In distributed
/// mode you must create a snapshot on each node separately. Each node has their own list of
/// snapshots.
///
/// Documentation: <https://qdrant.tech/documentation/concepts/snapshots/#create-snapshot>
pub async fn create_snapshot(
&self,
request: impl Into<CreateSnapshotRequest>,
) -> QdrantResult<CreateSnapshotResponse> {
let request = &request.into();
self.with_snapshot_client(|mut client| async move {
let result = client.create(request.clone()).await?;
Ok(result.into_inner())
})
.await
}
/// List collection snapshots on this node.
///
/// ```no_run
///# use qdrant_client::{Qdrant, QdrantError};
///# async fn list_snapshots(client: &Qdrant)
///# -> Result<(), QdrantError> {
/// client.list_snapshots("my_collection").await?;
///# Ok(())
///# }
/// ```
///
/// Note: Snapshots are node-local. They only contain data of a single node. In distributed
/// mode you must create a snapshot on each node separately. Each node has their own list of
/// snapshots.
///
/// Documentation: <https://qdrant.tech/documentation/concepts/snapshots/#list-snapshot>
pub async fn list_snapshots(
&self,
request: impl Into<ListSnapshotsRequest>,
) -> QdrantResult<ListSnapshotsResponse> {
let request = &request.into();
self.with_snapshot_client(|mut client| async move {
let result = client.list(request.clone()).await?;
Ok(result.into_inner())
})
.await
}
/// Download a collection snapshot on this node.
///
/// ```no_run
///# use std::fs::File;
///# use qdrant_client::{Qdrant, QdrantError};
/// use qdrant_client::qdrant::SnapshotDownloadBuilder;
///
///# async fn download_snapshot(client: &Qdrant)
///# -> Result<File, QdrantError> {
/// client.download_snapshot(
/// SnapshotDownloadBuilder::new("./target_path.snapshot", "my_collection")
/// .snapshot_name("snapshot_name")
/// .rest_api_uri("http://localhost:6333")
/// ).await?;
///
/// let snapshot_file = File::open("./target_path.snapshot")?;
///# Ok(snapshot_file)
///# }
/// ```
///
/// Note: Snapshots are node-local. They only contain data of a single node. In distributed
/// mode you must create a snapshot on each node separately. Each node has their own list of
/// snapshots.
///
/// Documentation: <https://qdrant.tech/documentation/concepts/snapshots/#retrieve-snapshot>
#[cfg(feature = "download_snapshots")]
pub async fn download_snapshot(
&self,
download: impl Into<crate::qdrant::SnapshotDownload>,
) -> QdrantResult<()> {
use std::io::Write;
use futures_util::StreamExt;
use crate::qdrant_client::error::QdrantError;
let options = download.into();
let snapshot_name = match &options.snapshot_name {
Some(sn) => sn.to_string(),
_ => match self
.list_snapshots(options.collection_name.clone())
.await?
.snapshot_descriptions
.first()
{
Some(sn) => sn.name.clone(),
_ => {
return Err(QdrantError::NoSnapshotFound(
options.collection_name.clone(),
))
}
},
};
let mut stream = reqwest::get(format!(
"{}/collections/{}/snapshots/{snapshot_name}",
options
.rest_api_uri
.as_ref()
.map(|uri| uri.to_string())
.unwrap_or_else(|| String::from("http://localhost:6333")),
options.collection_name,
))
.await?
.bytes_stream();
let _ = std::fs::remove_file(&options.out_path);
let mut file = std::fs::OpenOptions::new()
.write(true)
.create_new(true)
.open(&options.out_path)?;
while let Some(chunk) = stream.next().await {
let _written = file.write(&chunk?)?;
}
Ok(())
}
/// Delete a collection snapshot on this node.
///
/// ```no_run
///# use qdrant_client::{Qdrant, QdrantError};
/// use qdrant_client::qdrant::DeleteSnapshotRequestBuilder;
///
///# async fn delete_snapshot(client: &Qdrant)
///# -> Result<(), QdrantError> {
/// client
/// .delete_snapshot(DeleteSnapshotRequestBuilder::new(
/// "my_collection",
/// "snapshot_name",
/// ))
/// .await?;
///# Ok(())
///# }
/// ```
///
/// Note: Snapshots are node-local. They only contain data of a single node. In distributed
/// mode you must create a snapshot on each node separately. Each node has their own list of
/// snapshots.
///
/// Documentation: <https://qdrant.tech/documentation/concepts/snapshots/#delete-snapshot>
pub async fn delete_snapshot(
&self,
request: impl Into<DeleteSnapshotRequest>,
) -> QdrantResult<DeleteSnapshotResponse> {
let request = &request.into();
self.with_snapshot_client(|mut client| async move {
let result = client.delete(request.clone()).await?;
Ok(result.into_inner())
})
.await
}
/// Create full snapshot of this entire node.
///
/// <div class="warning">Only supported in single-node deployment. Multi-node (<a href="https://qdrant.tech/documentation/guides/distributed_deployment/">distributed</a>) mode is not supported.</div>
///
/// ```no_run
///# use qdrant_client::{Qdrant, QdrantError};
///# async fn create_snapshot(client: &Qdrant)
///# -> Result<(), QdrantError> {
/// client.create_full_snapshot().await?;
///# Ok(())
///# }
/// ```
///
/// Documentation: <https://qdrant.tech/documentation/concepts/snapshots/#create-full-storage-snapshot>
pub async fn create_full_snapshot(&self) -> QdrantResult<CreateSnapshotResponse> {
self.with_snapshot_client(|mut client| async move {
let result = client.create_full(CreateFullSnapshotRequest {}).await?;
Ok(result.into_inner())
})
.await
}
/// List full snapshots of this node.
///
/// ```no_run
///# use qdrant_client::{Qdrant, QdrantError};
///# async fn list_full_snapshots(client: &Qdrant)
///# -> Result<(), QdrantError> {
/// client.list_full_snapshots().await?;
///# Ok(())
///# }
/// ```
///
/// Documentation: <https://qdrant.tech/documentation/concepts/snapshots/#list-full-storage-snapshots>
pub async fn list_full_snapshots(&self) -> QdrantResult<ListSnapshotsResponse> {
self.with_snapshot_client(|mut client| async move {
let result = client.list_full(ListFullSnapshotsRequest {}).await?;
Ok(result.into_inner())
})
.await
}
/// Delete full snapshots of this node.
///
/// ```no_run
///# use qdrant_client::{Qdrant, QdrantError};
///# async fn delete_snapshot(client: &Qdrant)
///# -> Result<(), QdrantError> {
/// client
/// .delete_full_snapshot("snapshot_name")
/// .await?;
///# Ok(())
///# }
/// ```
///
/// Documentation: <https://qdrant.tech/documentation/concepts/snapshots/#delete-full-storage-snapshot>
pub async fn delete_full_snapshot(
&self,
request: impl Into<DeleteFullSnapshotRequest>,
) -> QdrantResult<DeleteSnapshotResponse> {
let request = &request.into();
self.with_snapshot_client(|mut client| async move {
let result = client.delete_full(request.clone()).await?;
Ok(result.into_inner())
})
.await
}
}