44import java .util .List ;
55import java .util .Optional ;
66import java .util .concurrent .Callable ;
7+ import java .util .concurrent .CompletableFuture ;
78import java .util .concurrent .TimeoutException ;
89import java .util .function .Function ;
10+ import java .util .function .Supplier ;
911
1012import com .google .gson .annotations .SerializedName ;
1113
1214import io .weaviate .client6 .v1 .api .WeaviateClient ;
15+ import io .weaviate .client6 .v1 .api .WeaviateClientAsync ;
1316import io .weaviate .client6 .v1 .internal .ObjectBuilder ;
1417
1518public record Backup (
@@ -45,8 +48,8 @@ public enum Operation {
4548 /**
4649 * Block until the backup has been created / restored successfully.
4750 *
48- * @param client Weaviate client. Make sure {@link WeaviateClient#close} is not
49- * called before this method returns.
51+ * @param client Weaviate client. Make sure {@link WeaviateClient#close}
52+ * is NOT called before this method returns.
5053 * @throws IllegalStateException if {@link #operation} is not set (null).
5154 * @throws TimeoutException in case the wait times out without reaching
5255 * BackupStatus.SUCCESS.
@@ -61,8 +64,8 @@ public Backup waitForCompletion(WeaviateClient client) throws IOException, Timeo
6164 /**
6265 * Block until the backup has been created / restored successfully.
6366 *
64- * @param client Weaviate client. Make sure {@link WeaviateClient#close} is not
65- * called before this method returns.
67+ * @param client Weaviate client. Make sure {@link WeaviateClient#close}
68+ * is NOT called before this method returns.
6669 * @param fn Lambda expression for optional parameters.
6770 * @throws IllegalStateException if {@link #operation} is not set (null).
6871 * @throws TimeoutException in case the wait times out without reaching
@@ -79,8 +82,8 @@ public Backup waitForCompletion(WeaviateClient client, Function<WaitOptions.Buil
7982 /**
8083 * Block until the backup operation reaches a certain status.
8184 *
82- * @param client Weaviate client. Make sure {@link WeaviateClient#close} is not
83- * called before this method returns.
85+ * @param client Weaviate client. Make sure {@link WeaviateClient#close}
86+ * is NOT called before this method returns.
8487 * @param status Target status.
8588 * @throws IllegalStateException if {@link #operation} is not set (null).
8689 * @throws TimeoutException in case the wait times out without reaching
@@ -96,8 +99,8 @@ public Backup waitForStatus(WeaviateClient client, BackupStatus status) throws I
9699 /**
97100 * Block until the backup operation reaches a certain status.
98101 *
99- * @param client Weaviate client. Make sure {@link WeaviateClient#close} is not
100- * called before this method returns.
102+ * @param client Weaviate client. Make sure {@link WeaviateClient#close}
103+ * is NOT called before this method returns.
101104 * @param status Target status.
102105 * @param fn Lambda expression for optional parameters.
103106 * @throws IllegalStateException if {@link #operation} is not set (null).
@@ -117,22 +120,95 @@ public Backup waitForStatus(WeaviateClient client, BackupStatus status,
117120 final Callable <Optional <Backup >> poll = operation == Operation .CREATE
118121 ? () -> client .backup .getCreateStatus (id , backend )
119122 : () -> client .backup .getRestoreStatus (id , backend );
120- return new Waiter (this , poll , options ).waitForStatus (status );
123+ return new Waiter (this , options ).waitForStatus (status , poll );
121124 }
122125
123126 /**
124127 * Cancel backup creation.
125128 *
126129 * <p>
127- * This method cannot be called cancel backup restore.
130+ * This method cannot be called to cancel backup restore.
128131 *
129- * @param client Weaviate client. Make sure {@link WeaviateClient#close} is not
130- * called before this method returns.
132+ * @param client Weaviate client. Make sure {@link WeaviateClient#close}
133+ * is NOT called before this method returns.
131134 * @throws IOException in case the request was not sent successfully
132135 * due to a malformed request, a networking error
133136 * or the server being unavailable.
134137 */
135138 public void cancel (WeaviateClient client ) throws IOException {
136139 client .backup .cancel (id (), backend ());
137140 }
141+
142+ /**
143+ * Poll until backup's been created / restored successfully.
144+ *
145+ * @param client Weaviate client. Make sure {@link WeaviateClientAsync#close}
146+ * is NOT called before this method returns.
147+ * @throws IllegalStateException if {@link #operation} is not set (null).
148+ * @throws TimeoutException in case the wait times out without reaching
149+ * BackupStatus.SUCCESS.
150+ * @throws IOException in case the request was not sent successfully
151+ * due to a malformed request, a networking error
152+ * or the server being unavailable.
153+ */
154+ public CompletableFuture <Backup > waitForCompletion (WeaviateClientAsync client ) {
155+ return waitForStatus (client , BackupStatus .SUCCESS );
156+ }
157+
158+ /**
159+ * Poll until backup's been created / restored successfully.
160+ *
161+ * @param client Weaviate client. Make sure {@link WeaviateClientAsync#close}
162+ * is NOT called before this method returns.
163+ * @param fn Lambda expression for optional parameters.
164+ */
165+ public CompletableFuture <Backup > waitForCompletion (WeaviateClientAsync client ,
166+ Function <WaitOptions .Builder , ObjectBuilder <WaitOptions >> fn ) {
167+ return waitForStatus (client , BackupStatus .SUCCESS , fn );
168+ }
169+
170+ /**
171+ * Poll until backup reaches a certain status or the wait times out.
172+ *
173+ * @param client Weaviate client. Make sure {@link WeaviateClientAsync#close}
174+ * is NOT called before this method returns.
175+ * @param status Target status.
176+ */
177+ public CompletableFuture <Backup > waitForStatus (WeaviateClientAsync client , BackupStatus status ) {
178+ return waitForStatus (client , status , ObjectBuilder .identity ());
179+ }
180+
181+ /**
182+ * Poll until backup reaches a certain status or the wait times out.
183+ *
184+ * @param client Weaviate client. Make sure {@link WeaviateClientAsync#close}
185+ * is NOT called before this method returns.
186+ * @param status Target status.
187+ * @param fn Lambda expression for optional parameters.
188+ */
189+ public CompletableFuture <Backup > waitForStatus (WeaviateClientAsync client , BackupStatus status ,
190+ Function <WaitOptions .Builder , ObjectBuilder <WaitOptions >> fn ) {
191+ if (operation == null ) {
192+ throw new IllegalStateException ("backup.operation is null" );
193+ }
194+
195+ final var options = WaitOptions .of (fn );
196+ final Supplier <CompletableFuture <Optional <Backup >>> poll = operation == Operation .CREATE
197+ ? () -> client .backup .getCreateStatus (id , backend )
198+ : () -> client .backup .getRestoreStatus (id , backend );
199+ return new Waiter (this , options ).waitForStatusAsync (status , poll );
200+ }
201+
202+ /**
203+ * Cancel backup creation.
204+ *
205+ * <p>
206+ * This method cannot be called to cancel backup restore.
207+ *
208+ * @param client Weaviate client. Make sure {@link WeaviateClientAsync#close}
209+ * is NOT called before this method returns.
210+ */
211+ public CompletableFuture <Void > cancel (WeaviateClientAsync client ) {
212+ return client .backup .cancel (id (), backend ());
213+ }
138214}
0 commit comments