Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
ma1581 committed Feb 9, 2025
1 parent 51674ce commit 6eb3c90
Show file tree
Hide file tree
Showing 13 changed files with 149 additions and 89 deletions.
3 changes: 2 additions & 1 deletion src/main/java/com/meilisearch/sdk/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
import com.meilisearch.sdk.exceptions.MeilisearchException;
import com.meilisearch.sdk.json.JsonHandler;
import com.meilisearch.sdk.model.*;
import com.meilisearch.sdk.model.batch_dto.Batch;
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
13 changes: 5 additions & 8 deletions src/main/java/com/meilisearch/sdk/TasksHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +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_dto.Batch;
import com.meilisearch.sdk.model.batch.req.BatchesQuery;
import com.meilisearch.sdk.model.batch.res.Batch;
import java.util.Date;

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

/**
* Retrieves a batch by its unique identifier.
* Retrieves a batch by uid.
*
* @param uid The unique identifier of the batch.
* @return The Batch object corresponding to the given uid.
Expand All @@ -175,7 +176,7 @@ public Batch getBatch(int uid) {
* Retrieves all batches based on the provided query parameters.
*
* @param batchesQuery An instance of BatchesQuery containing filtering criteria.
* @return A CursorResults object containing a list of Batch objects.
* @return A CursorResults object containing a paginated list of Batch objects.
*/
public CursorResults<Batch> getAllBatches(BatchesQuery batchesQuery) {
String urlPath = batchPath().addQuery(batchesQuery.toQuery()).getURL();
Expand All @@ -187,11 +188,7 @@ private URLBuilder tasksPath() {
return new URLBuilder("/tasks");
}

/**
* Constructs a URLBuilder instance for the "/batches" API endpoint.
*
* @return A URLBuilder object initialized with the "/batches" path.
*/
/** Constructs a URLBuilder instance for the "/batches" API endpoint. */
private URLBuilder batchPath() {
return new URLBuilder("/batches");
}
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;
}
}
9 changes: 0 additions & 9 deletions src/main/java/com/meilisearch/sdk/model/BatchResults.java

This file was deleted.

6 changes: 6 additions & 0 deletions src/main/java/com/meilisearch/sdk/model/CursorResults.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
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;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
package com.meilisearch.sdk.model;
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 {
Expand Down
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;
}
35 changes: 0 additions & 35 deletions src/main/java/com/meilisearch/sdk/model/batch_dto/Batch.java

This file was deleted.

12 changes: 0 additions & 12 deletions src/main/java/com/meilisearch/sdk/model/batch_dto/StatDetails.java

This file was deleted.

51 changes: 28 additions & 23 deletions src/test/java/com/meilisearch/integration/BatchTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
import static org.junit.jupiter.api.Assertions.*;

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

@Tag("integration")
Expand All @@ -15,38 +15,43 @@ public class BatchTest extends AbstractIT {
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 testGetAllBatches() {
void testGetBatch() {
CursorResults<Batch> allBatches = client.getAllBatches(new BatchesQuery());
assertNotNull(allBatches);
assertFalse(allBatches.getResults().isEmpty(), "Batch results should not be empty");
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 testGetOneBatch() {
CursorResults<Batch> batches = client.getAllBatches(new BatchesQuery());
assertFalse(batches.getResults().isEmpty(), "No batches found");

Batch firstBatch = client.getBatch(batches.getResults().get(0).getUid());

assertNotNull(firstBatch);
assertEquals(batches.getResults().get(0).getUid(), firstBatch.getUid());
assertNotNull(firstBatch.getDetails());
assertNotNull(firstBatch.getStats());
assertTrue(firstBatch.getStats().getTotalNbTasks() > 0);
assertNotNull(firstBatch.getStats().getStatus());
assertNotNull(firstBatch.getStats().getTypes());
assertNotNull(firstBatch.getStats().getIndexUids());
assertNotNull(firstBatch.getDuration());
assertNotNull(firstBatch.getStartedAt());
assertNotNull(firstBatch.getFinishedAt());
// TODO: Add Check for progress to be non-null, but response always provides null
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 6eb3c90

Please sign in to comment.