|
12 | 12 | import io.qdrant.client.grpc.QdrantOuterClass; |
13 | 13 | import io.qdrant.client.grpc.SnapshotsGrpc; |
14 | 14 | import io.qdrant.client.grpc.SnapshotsService; |
| 15 | +import io.qdrant.client.grpc.SnapshotsService.SnapshotDescription; |
15 | 16 | import io.qdrant.client.utils.PointUtil; |
| 17 | +import java.io.IOException; |
16 | 18 | import java.net.MalformedURLException; |
17 | 19 | import java.net.URL; |
| 20 | +import java.nio.file.Files; |
| 21 | +import java.nio.file.Path; |
| 22 | +import java.nio.file.StandardOpenOption; |
18 | 23 | import java.time.Duration; |
19 | 24 | import java.util.List; |
20 | 25 | import java.util.Map; |
21 | 26 | import java.util.concurrent.TimeUnit; |
22 | 27 | import javax.annotation.Nullable; |
| 28 | +import org.apache.http.HttpEntity; |
| 29 | +import org.apache.http.HttpResponse; |
| 30 | +import org.apache.http.client.HttpClient; |
| 31 | +import org.apache.http.client.methods.HttpGet; |
| 32 | +import org.apache.http.impl.client.HttpClients; |
| 33 | +import org.apache.http.util.EntityUtils; |
23 | 34 |
|
24 | 35 | /** Client for interfacing with the Qdrant service. */ |
25 | 36 | public class QdrantClient implements AutoCloseable { |
@@ -1311,10 +1322,74 @@ public SnapshotsService.DeleteSnapshotResponse deleteFullSnapshot(String snapsho |
1311 | 1322 | return snapshotsStub.deleteFull(request); |
1312 | 1323 | } |
1313 | 1324 |
|
| 1325 | + /** |
| 1326 | + * Downloads a snapshot of a collection from the specified REST API URI and saves it to the given |
| 1327 | + * output path. |
| 1328 | + * |
| 1329 | + * @param outPath The path where the snapshot will be saved. |
| 1330 | + * @param collectionName The name of the collection. |
| 1331 | + * @param snapshotName The name of the snapshot. If null, the latest snapshot will be downloaded. |
| 1332 | + * @param restApiUri The URI of the REST API. If null, the default URI "http://localhost:6333" |
| 1333 | + * will be used. |
| 1334 | + * @throws RuntimeException If an error occurs while downloading the snapshot. |
| 1335 | + */ |
| 1336 | + public void downloadSnapshot( |
| 1337 | + Path outPath, |
| 1338 | + String collectionName, |
| 1339 | + @Nullable String snapshotName, |
| 1340 | + @Nullable String restApiUri) { |
| 1341 | + try { |
| 1342 | + String resolvedSnapshotName; |
| 1343 | + |
| 1344 | + if (snapshotName != null) { |
| 1345 | + resolvedSnapshotName = snapshotName; |
| 1346 | + } else { |
| 1347 | + // Get the latest(0th) snapshot of the collection |
| 1348 | + List<SnapshotDescription> snapshots = |
| 1349 | + listSnapshots(collectionName).getSnapshotDescriptionsList(); |
| 1350 | + if (snapshots.isEmpty()) { |
| 1351 | + throw new RuntimeException("No snapshots found"); |
| 1352 | + } |
| 1353 | + resolvedSnapshotName = |
| 1354 | + listSnapshots(collectionName).getSnapshotDescriptionsList().get(0).getName(); |
| 1355 | + } |
| 1356 | + |
| 1357 | + String uri; |
| 1358 | + if (restApiUri != null) { |
| 1359 | + uri = |
| 1360 | + String.format( |
| 1361 | + "%s/collections/%s/snapshots/%s", restApiUri, collectionName, resolvedSnapshotName); |
| 1362 | + } else { |
| 1363 | + uri = |
| 1364 | + String.format( |
| 1365 | + "http://localhost:6333/collections/%s/snapshots/%s", |
| 1366 | + collectionName, resolvedSnapshotName); |
| 1367 | + } |
| 1368 | + |
| 1369 | + HttpClient httpClient = HttpClients.createDefault(); |
| 1370 | + HttpGet httpGet = new HttpGet(uri); |
| 1371 | + |
| 1372 | + HttpResponse response = httpClient.execute(httpGet); |
| 1373 | + |
| 1374 | + if (response.getStatusLine().getStatusCode() == 200) { |
| 1375 | + HttpEntity entity = response.getEntity(); |
| 1376 | + if (entity != null) { |
| 1377 | + Files.write(outPath, EntityUtils.toByteArray(entity), StandardOpenOption.WRITE); |
| 1378 | + System.out.println("Downloaded successfully"); |
| 1379 | + } else { |
| 1380 | + System.err.println("No response body"); |
| 1381 | + } |
| 1382 | + } else { |
| 1383 | + System.err.println( |
| 1384 | + "Download failed. HTTP Status Code: " + response.getStatusLine().getStatusCode()); |
| 1385 | + } |
| 1386 | + } catch (IOException e) { |
| 1387 | + throw new RuntimeException("Error downloading snapshot " + e.getMessage()); |
| 1388 | + } |
| 1389 | + } |
| 1390 | + |
1314 | 1391 | @Override |
1315 | 1392 | public void close() throws InterruptedException { |
1316 | 1393 | this.channel.shutdown().awaitTermination(10, TimeUnit.SECONDS); |
1317 | 1394 | } |
1318 | | - |
1319 | | - // TODO: Download snapshots REST |
1320 | 1395 | } |
0 commit comments