Skip to content

Commit 5e06343

Browse files
committed
feat: sort listed backups by starting time
1 parent 03d711f commit 5e06343

6 files changed

Lines changed: 98 additions & 12 deletions

File tree

src/it/java/io/weaviate/integration/BackupITest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,10 @@ public void test_lifecycle() throws IOException, TimeoutException {
103103
.returns(BackupStatus.CANCELED, Backup::status);
104104

105105
// Assert: all 3 backups are present
106-
var all = client.backup.list(backend);
106+
var all = client.backup.list(backend, bu -> bu.sortByStartingTimeAsc(true));
107107
Assertions.assertThat(all).as("all backups")
108108
.extracting(Backup::id)
109-
.contains(backup_1, backup_2, backup_3);
109+
.containsExactly(backup_1, backup_2, backup_3);
110110

111111
// Act: delete data and restore backup #1
112112
client.collections.delete(nsA);
@@ -225,8 +225,8 @@ public void test_lifecycle_async() throws ExecutionException, InterruptedExcepti
225225

226226
@Test(expected = IllegalStateException.class)
227227
public void test_waitForCompletion_unknown() throws IOException, TimeoutException {
228-
var backup = new Backup("#1", "/tmp/bak/#1", "filesystem", List.of("Things"), BackupStatus.STARTED, null,
229-
null);
228+
var backup = new Backup("#1", "/tmp/bak/#1", "filesystem", List.of("Things"), BackupStatus.STARTED,
229+
null, null, null, null, null);
230230
backup.waitForCompletion(client);
231231
}
232232

src/main/java/io/weaviate/client6/v1/api/backup/Backup.java

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.weaviate.client6.v1.api.backup;
22

33
import java.io.IOException;
4+
import java.time.OffsetDateTime;
45
import java.util.List;
56
import java.util.Optional;
67
import java.util.concurrent.Callable;
@@ -28,6 +29,12 @@ public record Backup(
2829
@SerializedName("status") BackupStatus status,
2930
/** Backup creation / restoration error. */
3031
@SerializedName("error") String error,
32+
/** Time at which the backup creation. */
33+
@SerializedName("startedAt") OffsetDateTime startedAt,
34+
/** Time at which the backup was completed, successfully or otherwise. */
35+
@SerializedName("completedAt") OffsetDateTime completedAt,
36+
/** Backup size in GiB. */
37+
@SerializedName("size") Integer sizeGiB,
3138
/**
3239
* This value indicates if a backup is being created or restored from.
3340
* For operations like LIST this value is null.
@@ -37,8 +44,18 @@ public record Backup(
3744
@SerializedName("__operation__") Operation operation) {
3845

3946
/** Set operation associated with this backup. */
40-
public Backup withOperation(Operation operation) {
41-
return new Backup(id, path, backend, includesCollections, status, error, operation);
47+
Backup withOperation(Operation operation) {
48+
return new Backup(
49+
id,
50+
path,
51+
backend,
52+
includesCollections,
53+
status,
54+
error,
55+
startedAt,
56+
completedAt,
57+
sizeGiB,
58+
operation);
4259
}
4360

4461
public enum Operation {
Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,61 @@
11
package io.weaviate.client6.v1.api.backup;
22

3-
import java.util.Collections;
3+
import java.util.HashMap;
44
import java.util.List;
5+
import java.util.function.Function;
56

67
import com.google.gson.reflect.TypeToken;
78

9+
import io.weaviate.client6.v1.internal.ObjectBuilder;
810
import io.weaviate.client6.v1.internal.json.JSON;
911
import io.weaviate.client6.v1.internal.rest.Endpoint;
1012
import io.weaviate.client6.v1.internal.rest.SimpleEndpoint;
1113

12-
public record ListBackupsRequest(String backend) {
14+
public record ListBackupsRequest(String backend, boolean startingTimeAsc) {
1315

1416
@SuppressWarnings("unchecked")
1517
public static Endpoint<ListBackupsRequest, List<Backup>> _ENDPOINT = SimpleEndpoint.noBody(
1618
request -> "GET",
1719
request -> "/backups/" + request.backend,
18-
request -> Collections.emptyMap(),
20+
request -> new HashMap<>() {
21+
{
22+
if (request.startingTimeAsc) {
23+
put("order", "asc");
24+
}
25+
}
26+
},
1927
(statusCode, response) -> (List<Backup>) JSON.deserialize(
2028
response, TypeToken.getParameterized(List.class, Backup.class)));
29+
30+
public static ListBackupsRequest of(String backend) {
31+
return of(backend, ObjectBuilder.identity());
32+
}
33+
34+
public static ListBackupsRequest of(String backend, Function<Builder, ObjectBuilder<ListBackupsRequest>> fn) {
35+
return fn.apply(new Builder(backend)).build();
36+
}
37+
38+
public ListBackupsRequest(Builder builder) {
39+
this(builder.backend, builder.startingTimeAsc);
40+
}
41+
42+
public static class Builder implements ObjectBuilder<ListBackupsRequest> {
43+
private final String backend;
44+
private boolean startingTimeAsc = false;
45+
46+
public Builder(String backend) {
47+
this.backend = backend;
48+
}
49+
50+
/** Sort the backups by their starting time, oldest to newest. */
51+
public Builder sortByStartingTimeAsc(boolean enable) {
52+
this.startingTimeAsc = enable;
53+
return this;
54+
}
55+
56+
@Override
57+
public ListBackupsRequest build() {
58+
return new ListBackupsRequest(this);
59+
}
60+
}
2161
}

src/main/java/io/weaviate/client6/v1/api/backup/WeaviateBackupClient.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,23 @@ public Optional<Backup> getRestoreStatus(String backupId, String backend) throws
157157
* or the server being unavailable.
158158
*/
159159
public List<Backup> list(String backend) throws IOException {
160-
return this.restTransport.performRequest(new ListBackupsRequest(backend), ListBackupsRequest._ENDPOINT);
160+
return this.restTransport.performRequest(ListBackupsRequest.of(backend), ListBackupsRequest._ENDPOINT);
161+
}
162+
163+
/**
164+
* List backups in the backend storage.
165+
*
166+
* @param backend Backup storage backend.
167+
* @param fn Lambda expression for optional parameters.
168+
* @throws WeaviateApiException in case the server returned with an
169+
* error status code.
170+
* @throws IOException in case the request was not sent successfully
171+
* due to a malformed request, a networking error
172+
* or the server being unavailable.
173+
*/
174+
public List<Backup> list(String backend, Function<ListBackupsRequest.Builder, ObjectBuilder<ListBackupsRequest>> fn)
175+
throws IOException {
176+
return this.restTransport.performRequest(ListBackupsRequest.of(backend, fn), ListBackupsRequest._ENDPOINT);
161177
}
162178

163179
/**

src/main/java/io/weaviate/client6/v1/api/backup/WeaviateBackupClientAsync.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,18 @@ public CompletableFuture<Optional<Backup>> getRestoreStatus(String backupId, Str
109109
* @param backend Backup storage backend.
110110
*/
111111
public CompletableFuture<List<Backup>> list(String backend) {
112-
return this.restTransport.performRequestAsync(new ListBackupsRequest(backend), ListBackupsRequest._ENDPOINT);
112+
return this.restTransport.performRequestAsync(ListBackupsRequest.of(backend), ListBackupsRequest._ENDPOINT);
113+
}
114+
115+
/**
116+
* List backups in the backend storage.
117+
*
118+
* @param backend Backup storage backend.
119+
* @param fn Lambda expression for optional parameters.
120+
*/
121+
public CompletableFuture<List<Backup>> list(String backend,
122+
Function<ListBackupsRequest.Builder, ObjectBuilder<ListBackupsRequest>> fn) {
123+
return this.restTransport.performRequestAsync(ListBackupsRequest.of(backend, fn), ListBackupsRequest._ENDPOINT);
113124
}
114125

115126
/**

src/main/java/io/weaviate/client6/v1/internal/rest/UrlEncoder.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ public static String encodeQuery(Map<String, Object> queryParams) {
3333
if (qp == null) {
3434
return false;
3535
}
36-
if (qp.getValue() instanceof String str) {
36+
if (qp.getValue() == null) {
37+
return false;
38+
} else if (qp.getValue() instanceof String str) {
3739
return !str.isBlank();
3840
}
3941
return true;

0 commit comments

Comments
 (0)