Skip to content

Commit

Permalink
Merge branch 'main' into bump-meilisearch-v1.13
Browse files Browse the repository at this point in the history
  • Loading branch information
Strift authored Feb 18, 2025
2 parents 5058702 + 5138fe1 commit 24afb6a
Show file tree
Hide file tree
Showing 11 changed files with 286 additions and 17 deletions.
6 changes: 3 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ configurations {

dependencies {
// This dependency is used internally, and not exposed to consumers on their own compile classpath.
implementation 'com.google.code.gson:gson:2.11.0'
implementation 'org.json:json:20240303'
implementation 'com.google.code.gson:gson:2.12.1'
implementation 'org.json:json:20250107'
// https://mvnrepository.com/artifact/org.apache.httpcomponents.client5/httpclient5
api 'com.squareup.okhttp3:okhttp:[4.10.0,5.0.0)'

Expand All @@ -61,7 +61,7 @@ dependencies {
// https://mvnrepository.com/artifact/org.mockito/mockito-core
testImplementation 'org.mockito:mockito-core:4.9.0'
testImplementation 'org.hamcrest:hamcrest:3.0'
testImplementation 'com.squareup.okio:okio:3.9.1'
testImplementation 'com.squareup.okio:okio:3.10.2'
testImplementation 'com.squareup.okhttp3:okhttp:4.12.0'
testImplementation 'com.fasterxml.jackson.core:jackson-databind:2.18.2'

Expand Down
40 changes: 26 additions & 14 deletions src/main/java/com/meilisearch/sdk/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,9 @@
import com.auth0.jwt.algorithms.Algorithm;
import com.meilisearch.sdk.exceptions.MeilisearchException;
import com.meilisearch.sdk.json.JsonHandler;
import com.meilisearch.sdk.model.CancelTasksQuery;
import com.meilisearch.sdk.model.DeleteTasksQuery;
import com.meilisearch.sdk.model.IndexesQuery;
import com.meilisearch.sdk.model.Key;
import com.meilisearch.sdk.model.KeyUpdate;
import com.meilisearch.sdk.model.KeysQuery;
import com.meilisearch.sdk.model.MultiSearchResult;
import com.meilisearch.sdk.model.Results;
import com.meilisearch.sdk.model.Stats;
import com.meilisearch.sdk.model.SwapIndexesParams;
import com.meilisearch.sdk.model.Task;
import com.meilisearch.sdk.model.TaskInfo;
import com.meilisearch.sdk.model.TasksQuery;
import com.meilisearch.sdk.model.TasksResults;
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;
import java.util.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -352,6 +341,29 @@ public void waitForTask(int uid) throws MeilisearchException {
this.tasksHandler.waitForTask(uid);
}

/**
* Retrieves a batch by its unique identifier, with exception handling.
*
* @param uid The unique identifier of the batch.
* @return The Batch object corresponding to the given uid.
* @throws MeilisearchException If an error occurs during the request.
*/
public Batch getBatch(int uid) throws MeilisearchException {
return this.tasksHandler.getBatch(uid);
}

/**
* Retrieves all batches based on the provided query parameters, with exception handling.
*
* @param batchesQuery An instance of BatchesQuery containing filtering criteria.
* @return A CursorResults object containing a list of Batch objects.
* @throws MeilisearchException If an error occurs during the request.
*/
public CursorResults<Batch> getAllBatches(BatchesQuery batchesQuery)
throws MeilisearchException {
return this.tasksHandler.getAllBatches(batchesQuery);
}

/**
* Retrieves the key with the specified uid
*
Expand Down
29 changes: 29 additions & 0 deletions src/main/java/com/meilisearch/sdk/TasksHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
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;

/**
Expand Down Expand Up @@ -159,11 +161,38 @@ void waitForTask(int taskUid, int timeoutInMs, int intervalInMs) throws Meilisea
}
}

/**
* 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) {
Expand Down
46 changes: 46 additions & 0 deletions src/main/java/com/meilisearch/sdk/enums/OperationType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.meilisearch.sdk.enums;

import com.fasterxml.jackson.annotation.JsonValue;
import com.google.gson.annotations.SerializedName;

/**
* Enum for Operation Type
*
* @see <a href="https://www.meilisearch.com/docs/reference/api/tasks#type">API specification</a>
*/
public enum OperationType {
@SerializedName("indexCreation")
INDEX_CREATION("indexCreation"),
@SerializedName("indexUpdate")
INDEX_UPDATE("indexUpdate"),
@SerializedName("indexDeletion")
INDEX_DELETE("indexDeletion"),
@SerializedName("indexSwap")
INDEX_SWAP("indexSwap"),
@SerializedName("documentAdditionOrUpdate")
DOCUMENT_UPSERT("documentAdditionOrUpdate"),
@SerializedName("documentDeletion")
DOCUMENT_DELETE("documentDeletion"),
@SerializedName("settingsUpdate")
SETTINGS_UPDATE("settingsUpdate"),
@SerializedName("dumpCreation")
DUMP_CREATE("dumpCreation"),
@SerializedName("taskCancelation")
TASK_CANCEL("taskCancelation"),
@SerializedName("taskDeletion")
TASK_DELETE("taskDeletion"),
@SerializedName("snapshotCreation")
SNAPSHOT_CREATE("snapshotCreation");

public final String operationType;

OperationType(String operationType) {
this.operationType = operationType;
}

@JsonValue
@Override
public String toString() {
return this.operationType;
}
}
19 changes: 19 additions & 0 deletions src/main/java/com/meilisearch/sdk/model/CursorResults.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.meilisearch.sdk.model;

import java.util.List;
import lombok.Data;

/**
* Data structure paginated response Currently used in : Batches.
*
* @see <a href="https://www.meilisearch.com/docs/reference/api/batches#response">API
* specification</a>
*/
@Data
public class CursorResults<T> {
private List<T> results;
private int limit;
private int from;
private Integer next;
private int total;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package com.meilisearch.sdk.model.batch.req;

import com.meilisearch.sdk.http.URLBuilder;
import java.util.Date;
import lombok.Data;
import lombok.experimental.Accessors;

/**
* Data structure of a query parameter for batches route
*
* @see <a href="https://www.meilisearch.com/docs/reference/api/batches#query-parameters">API
* specification</a>
*/
@Data
@Accessors(chain = true)
public class BatchesQuery {
private int[] uids;
private int[] batchUids;
private String[] types;
private String[] statuses;
private String[] indexUids;
private int[] canceledBy;
private Date beforeEnqueuedAt;
private Date afterEnqueuedAt;
private Date beforeStartedAt;
private Date afterStartedAt;
private Date beforeFinishedAt;
private Date afterFinishedAt;
private int limit = -1;
private int from = -1;

public String toQuery() {
URLBuilder urlb =
new URLBuilder()
.addParameter("uids", this.getUids())
.addParameter("batchUids", this.getBatchUids())
.addParameter("types", this.getTypes())
.addParameter("statuses", this.getStatuses())
.addParameter("indexUids", this.getIndexUids())
.addParameter("canceledBy", this.getCanceledBy())
.addParameter("beforeEnqueuedAt", this.getBeforeEnqueuedAt())
.addParameter("afterEnqueuedAt", this.getAfterEnqueuedAt())
.addParameter("beforeStartedAt", this.getBeforeStartedAt())
.addParameter("afterStartedAt", this.getAfterStartedAt())
.addParameter("beforeFinishedAt", this.getBeforeFinishedAt())
.addParameter("afterFinishedAt", this.getAfterFinishedAt())
.addParameter("limit", this.getLimit())
.addParameter("from", this.getFrom());
return urlb.getURL();
}
}
21 changes: 21 additions & 0 deletions src/main/java/com/meilisearch/sdk/model/batch/res/Batch.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.meilisearch.sdk.model.batch.res;

import com.meilisearch.sdk.model.TaskDetails;
import lombok.Data;

/**
* Data structure of the batch object response
*
* @see <a href="https://www.meilisearch.com/docs/reference/api/batches#batch-object">API
* specification</a>
*/
@Data
public class Batch {
private int uid = 0;
private TaskDetails details;
private BatchProgress progress;
private StatDetails stats;
private String startedAt;
private String finishedAt;
private String duration;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.meilisearch.sdk.model.batch.res;

import java.util.List;
import lombok.Data;

@Data
class BatchProgress {
private List<StepDetails> steps;
private int percentage;
}
14 changes: 14 additions & 0 deletions src/main/java/com/meilisearch/sdk/model/batch/res/StatDetails.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.meilisearch.sdk.model.batch.res;

import com.meilisearch.sdk.enums.OperationType;
import com.meilisearch.sdk.model.TaskStatus;
import java.util.Map;
import lombok.Data;

@Data
public class StatDetails {
private int totalNbTasks;
private Map<TaskStatus, Integer> status;
private Map<OperationType, Integer> types;
private Map<String, Integer> indexUids;
}
10 changes: 10 additions & 0 deletions src/main/java/com/meilisearch/sdk/model/batch/res/StepDetails.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.meilisearch.sdk.model.batch.res;

import lombok.Data;

@Data
class StepDetails {
private String currentStep;
private int finished;
private int total;
}
57 changes: 57 additions & 0 deletions src/test/java/com/meilisearch/integration/BatchTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.meilisearch.integration;

import static org.junit.jupiter.api.Assertions.*;

import com.meilisearch.integration.classes.AbstractIT;
import com.meilisearch.sdk.model.CursorResults;
import com.meilisearch.sdk.model.batch.req.BatchesQuery;
import com.meilisearch.sdk.model.batch.res.Batch;
import org.junit.jupiter.api.*;

@Tag("integration")
public class BatchTest extends AbstractIT {

@BeforeEach
void setup() throws Exception {
this.setUp();
this.setUpJacksonClient();
cleanup();
}

@AfterAll
static void cleanMeilisearch() {
cleanup();
}

/** Check if able to fetch a single batch by uid */
@Test
void testGetBatch() {
CursorResults<Batch> allBatches = client.getAllBatches(new BatchesQuery());
assertFalse(allBatches.getResults().isEmpty(), "No batches found");
int batchUid = allBatches.getResults().get(0).getUid();

Batch batch = client.getBatch(batchUid);

assertNotNull(batch);
assertEquals(batchUid, batch.getUid());
assertNotNull(batch.getDetails());
assertNotNull(batch.getStats());
assertTrue(batch.getStats().getTotalNbTasks() > 0);
assertNotNull(batch.getStats().getStatus());
assertNotNull(batch.getStats().getTypes());
assertNotNull(batch.getStats().getIndexUids());
assertNotNull(batch.getDuration());
assertNotNull(batch.getStartedAt());
assertNotNull(batch.getFinishedAt());
// TODO: Add Check for progress to be non-null, but response always provides null
}

/** Check if able to fetch all batch data as page */
@Test
void testGetAllBatches() {
CursorResults<Batch> allBatches = client.getAllBatches(new BatchesQuery());

assertNotNull(allBatches);
assertFalse(allBatches.getResults().isEmpty(), "Batch results should not be empty");
}
}

0 comments on commit 24afb6a

Please sign in to comment.