From afcc10aac522937096b0437f951b0beb6be58c68 Mon Sep 17 00:00:00 2001 From: Anush008 Date: Sat, 9 Dec 2023 09:58:46 +0530 Subject: [PATCH] feat: Download snapshots natively --- pom.xml | 7 +--- .../java/io/qdrant/client/QdrantClient.java | 34 ++++++++----------- 2 files changed, 16 insertions(+), 25 deletions(-) diff --git a/pom.xml b/pom.xml index e0ea2de4..b728c7c6 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ io.qdrant client - + 1.0 @@ -40,11 +40,6 @@ test - - org.apache.httpcomponents - httpclient - 4.5.13 - diff --git a/src/main/java/io/qdrant/client/QdrantClient.java b/src/main/java/io/qdrant/client/QdrantClient.java index 7e776ef7..7bd73bab 100644 --- a/src/main/java/io/qdrant/client/QdrantClient.java +++ b/src/main/java/io/qdrant/client/QdrantClient.java @@ -14,23 +14,18 @@ import io.qdrant.client.grpc.SnapshotsService; import io.qdrant.client.grpc.SnapshotsService.SnapshotDescription; import io.qdrant.client.utils.PointUtil; +import java.io.FileOutputStream; import java.io.IOException; +import java.io.InputStream; +import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; -import java.nio.file.Files; import java.nio.file.Path; -import java.nio.file.StandardOpenOption; import java.time.Duration; import java.util.List; import java.util.Map; import java.util.concurrent.TimeUnit; import javax.annotation.Nullable; -import org.apache.http.HttpEntity; -import org.apache.http.HttpResponse; -import org.apache.http.client.HttpClient; -import org.apache.http.client.methods.HttpGet; -import org.apache.http.impl.client.HttpClients; -import org.apache.http.util.EntityUtils; /** Client for interfacing with the Qdrant service. */ public class QdrantClient implements AutoCloseable { @@ -1366,22 +1361,23 @@ public void downloadSnapshot( collectionName, resolvedSnapshotName); } - HttpClient httpClient = HttpClients.createDefault(); - HttpGet httpGet = new HttpGet(uri); + URL url = new URL(uri); + HttpURLConnection connection = (HttpURLConnection) url.openConnection(); - HttpResponse response = httpClient.execute(httpGet); + if (connection.getResponseCode() == 200) { + try (InputStream in = connection.getInputStream(); + FileOutputStream fileOut = new FileOutputStream(outPath.toFile())) { + + byte[] buffer = new byte[8192]; + int bytesRead; + while ((bytesRead = in.read(buffer)) != -1) { + fileOut.write(buffer, 0, bytesRead); + } - if (response.getStatusLine().getStatusCode() == 200) { - HttpEntity entity = response.getEntity(); - if (entity != null) { - Files.write(outPath, EntityUtils.toByteArray(entity), StandardOpenOption.CREATE_NEW); System.out.println("Downloaded successfully"); - } else { - System.err.println("No response body"); } } else { - System.err.println( - "Download failed. HTTP Status Code: " + response.getStatusLine().getStatusCode()); + System.err.println("Download failed. HTTP Status Code: " + connection.getResponseCode()); } } catch (IOException e) { throw new RuntimeException("Error downloading snapshot " + e.getMessage());