Skip to content

Commit

Permalink
refactor: java.net.URL parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
Anush008 committed Dec 14, 2023
1 parent 4ee52fa commit b55989c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 14 deletions.
21 changes: 10 additions & 11 deletions src/main/java/io/qdrant/client/QdrantClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -2388,6 +2388,7 @@ public ListenableFuture<DeleteSnapshotResponse> deleteFullSnapshotAsync(String s
addLogFailureCallback(future, "Delete full snapshot");
return future;
}

/**
* Downloads a snapshot of a collection from the specified REST API URI and
* saves it to the given
Expand All @@ -2397,15 +2398,15 @@ public ListenableFuture<DeleteSnapshotResponse> deleteFullSnapshotAsync(String s
* @param collectionName The name of the collection.
* @param snapshotName The name of the snapshot. If null, the latest snapshot
* will be downloaded.
* @param restApiUri The URI of the REST API. If null, the default URI
* @param restApiUrl The URI of the REST API. If null, the default URI
* "http://localhost:6333"
* will be used.
*/
public void downloadSnapshot(
Path outPath,
String collectionName,
@Nullable String snapshotName,
@Nullable String restApiUri) throws InterruptedException, IOException, ExecutionException {
@Nullable URL restApiUrl) throws InterruptedException, IOException, ExecutionException {
String resolvedSnapshotName;
if (snapshotName != null) {
resolvedSnapshotName = snapshotName;
Expand All @@ -2418,17 +2419,15 @@ public void downloadSnapshot(
resolvedSnapshotName = snapshots.get(0).getName();
}

String uri;
if (restApiUri != null) {
uri = String.format(
"%s/collections/%s/snapshots/%s", restApiUri, collectionName, resolvedSnapshotName);
String snapshotPath = String.format(
"/collections/%s/snapshots/%s", collectionName, resolvedSnapshotName);
URL url;

if (restApiUrl != null) {
url = new URL(restApiUrl, snapshotPath);
} else {
uri = String.format(
"http://localhost:6333/collections/%s/snapshots/%s",
collectionName, resolvedSnapshotName);
url = new URL("http", "localhost", 6333, snapshotPath);
}

URL url = new URL(uri);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();

if (connection.getResponseCode() == 200) {
Expand Down
7 changes: 4 additions & 3 deletions src/test/java/io/qdrant/client/SnapshotsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import io.qdrant.client.container.QdrantContainer;

import java.io.IOException;
import java.net.URL;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.util.List;
Expand Down Expand Up @@ -141,7 +142,7 @@ public void listFullSnapshots() throws ExecutionException, InterruptedException

@Test
public void testDownloadSnapshot() throws ExecutionException, InterruptedException, IOException {
String restApiUri = "http://" + QDRANT_CONTAINER.getHttpHostAddress();
URL restApiUrl = new URL("http://" + QDRANT_CONTAINER.getHttpHostAddress());
createCollection(testName);

assertEquals(client.listSnapshotAsync(testName).get().size(), 0);
Expand All @@ -152,12 +153,12 @@ public void testDownloadSnapshot() throws ExecutionException, InterruptedExcepti

Path path = FileSystems.getDefault().getPath("./test.snapshot");

client.downloadSnapshot(path, testName, snapshotName, restApiUri);
client.downloadSnapshot(path, testName, snapshotName, restApiUrl);
assertTrue(path.toFile().exists());

// Test without snapshot name
path = FileSystems.getDefault().getPath("./test_2.snapshot");
client.downloadSnapshot(path, testName, null, restApiUri);
client.downloadSnapshot(path, testName, null, restApiUrl);
assertTrue(path.toFile().exists());
}

Expand Down

0 comments on commit b55989c

Please sign in to comment.