forked from meilisearch/meilisearch-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTasksHandler.java
More file actions
225 lines (204 loc) · 8.15 KB
/
TasksHandler.java
File metadata and controls
225 lines (204 loc) · 8.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
package com.meilisearch.sdk;
import com.meilisearch.sdk.exceptions.MeilisearchException;
import com.meilisearch.sdk.exceptions.MeilisearchTimeoutException;
import com.meilisearch.sdk.http.URLBuilder;
import com.meilisearch.sdk.model.*;
import com.meilisearch.sdk.model.batch.req.BatchesQuery;
import com.meilisearch.sdk.model.batch.res.Batch;
import java.util.Date;
/**
* Class covering the Meilisearch Task API
*
* @see <a href="https://www.meilisearch.com/docs/reference/api/tasks">API specification</a>
*/
public class TasksHandler {
private final HttpClient httpClient;
/**
* Creates and sets up an instance of Task to simplify MeiliSearch API calls to manage tasks
*
* @param config MeiliSearch configuration
*/
protected TasksHandler(Config config) {
this.httpClient = config.httpClient;
}
/**
* Retrieves one task with the specified task uid
*
* @param taskUid Identifier of the requested Task
* @return Task instance
* @throws MeilisearchException if client request causes an error
*/
Task getTask(int taskUid) throws MeilisearchException {
URLBuilder urlb = new URLBuilder();
urlb.addSubroute("tasks").addSubroute(Integer.toString(taskUid));
String urlPath = urlb.getURL();
return httpClient.get(urlPath, Task.class);
}
/**
* Retrieves the documents of a task with the specified task uid
*
* @param taskUid Identifier of the requested Task
* @return String (NDJSON) of documents processed by the task
* @throws MeilisearchException if client request causes an error
*/
String getTaskDocuments(int taskUid) throws MeilisearchException {
URLBuilder urlb = new URLBuilder();
urlb.addSubroute("tasks").addSubroute(Integer.toString(taskUid)).addSubroute("documents");
String urlPath = urlb.getURL();
return httpClient.get(urlPath, String.class);
}
/**
* Retrieves all tasks from the client
*
* @return TasksResults containing a list of task instance
* @throws MeilisearchException if client request causes an error
*/
TasksResults getTasks() throws MeilisearchException {
TasksResults result = httpClient.get(tasksPath().getURL(), TasksResults.class);
return result;
}
/**
* Retrieves all tasks from the client
*
* @param param accept by the tasks route
* @return TasksResults containing a list of task instance
* @throws MeilisearchException if client request causes an error
*/
TasksResults getTasks(TasksQuery param) throws MeilisearchException {
TasksResults result =
httpClient.get(tasksPath().addQuery(param.toQuery()).getURL(), TasksResults.class);
return result;
}
/**
* Retrieves all tasks from specified index uid
*
* @param indexUid Index identifier to index of the requested Tasks
* @return TasksResults containing a list of task instance
* @throws MeilisearchException if client request causes an error
*/
TasksResults getTasks(String indexUid) throws MeilisearchException {
URLBuilder urlb = tasksPath().addParameter("indexUid", indexUid);
TasksResults result = httpClient.get(urlb.getURL(), TasksResults.class);
return result;
}
/**
* Retrieves all tasks from specified index uid
*
* @param indexUid Index identifier to index of the requested Tasks
* @param param accept by the tasks route
* @return TasksResults containing a list of task instance
* @throws MeilisearchException if client request causes an error
*/
TasksResults getTasks(String indexUid, TasksQuery param) throws MeilisearchException {
param = addIndexUidToQuery(indexUid, param);
TasksResults result =
httpClient.get(tasksPath().addQuery(param.toQuery()).getURL(), TasksResults.class);
return result;
}
/**
* Delete tasks from the client
*
* @param param accept by the tasks route
* @return Meilisearch API response as TaskInfo
* @throws MeilisearchException if client request causes an error
*/
TaskInfo cancelTasks(CancelTasksQuery param) throws MeilisearchException {
URLBuilder urlb = tasksPath().addSubroute("cancel");
TaskInfo result =
httpClient.post(urlb.addQuery(param.toQuery()).getURL(), null, TaskInfo.class);
return result;
}
/**
* Delete tasks from the client
*
* @param param accept by the tasks route
* @return Meilisearch API response as TaskInfo
* @throws MeilisearchException if client request causes an error
*/
TaskInfo deleteTasks(DeleteTasksQuery param) throws MeilisearchException {
TaskInfo result =
httpClient.delete(tasksPath().addQuery(param.toQuery()).getURL(), TaskInfo.class);
return result;
}
/**
* Waits for a task to be processed
*
* @param taskUid Identifier of the Task
* @throws MeilisearchException if timeout is reached
*/
void waitForTask(int taskUid) throws MeilisearchException {
this.waitForTask(taskUid, 5000, 50);
}
/**
* Waits for a task to be processed
*
* @param taskUid Identifier of the Task
* @param timeoutInMs number of milliseconds before throwing an Exception
* @param intervalInMs number of milliseconds before requesting the status again
* @throws MeilisearchException if timeout is reached
*/
void waitForTask(int taskUid, int timeoutInMs, int intervalInMs) throws MeilisearchException {
Task task;
TaskStatus status = null;
long startTime = new Date().getTime();
long elapsedTime = 0;
while (status == null
|| (status.equals(TaskStatus.ENQUEUED) || status.equals(TaskStatus.PROCESSING))) {
if (elapsedTime >= timeoutInMs) {
throw new MeilisearchTimeoutException();
}
task = this.getTask(taskUid);
status = task.getStatus();
try {
Thread.sleep(intervalInMs);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new MeilisearchTimeoutException();
}
elapsedTime = new Date().getTime() - startTime;
}
}
/**
* Retrieves a batch by uid.
*
* @param uid The unique identifier of the batch.
* @return The Batch object corresponding to the given uid.
*/
public Batch getBatch(int uid) {
String urlPath = batchPath().addSubroute(Integer.toString(uid)).getURL();
return httpClient.get(urlPath, Batch.class);
}
/**
* Retrieves all batches based on the provided query parameters.
*
* @param batchesQuery An instance of BatchesQuery containing filtering criteria.
* @return A CursorResults object containing a paginated list of Batch objects.
*/
public CursorResults<Batch> getAllBatches(BatchesQuery batchesQuery) {
String urlPath = batchPath().addQuery(batchesQuery.toQuery()).getURL();
return (CursorResults<Batch>) httpClient.get(urlPath, CursorResults.class, Batch.class);
}
/** Creates an URLBuilder for the constant route tasks */
private URLBuilder tasksPath() {
return new URLBuilder("/tasks");
}
/** Constructs a URLBuilder instance for the "/batches" API endpoint. */
private URLBuilder batchPath() {
return new URLBuilder("/batches");
}
/** Add index uid to index uids list in task query */
TasksQuery addIndexUidToQuery(String indexUid, TasksQuery param) {
if (param != null && param.getIndexUids() != null) {
String[] newIndexUid = new String[param.getIndexUids().length + 1];
for (int i = 0; i < param.getIndexUids().length; i++)
newIndexUid[i] = param.getIndexUids()[i];
newIndexUid[param.getIndexUids().length] = indexUid;
param.setIndexUids(newIndexUid);
} else if (param != null) {
param.setIndexUids(new String[] {indexUid});
} else {
param = new TasksQuery().setIndexUids(new String[] {indexUid});
}
return param;
}
}