Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Download snapshots natively #6

Merged
merged 1 commit into from
Dec 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 1 addition & 6 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>io.qdrant</groupId>
<artifactId>client</artifactId>
<!-- UPDATE THE VERSION IN README.MD TOO-->
<!-- UPDATE THE VERSION IN README.MD-->
<version>1.0</version>

<dependencies>
Expand Down Expand Up @@ -40,11 +40,6 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.13</version>
</dependency>
</dependencies>

<build>
Expand Down
34 changes: 15 additions & 19 deletions src/main/java/io/qdrant/client/QdrantClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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());
Expand Down