Skip to content

Commit

Permalink
Change callback parameter to client.
Browse files Browse the repository at this point in the history
  • Loading branch information
junghoon-vans authored Nov 8, 2023
1 parent cb380ec commit b371ed6
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

package io.vanslog.spring.data.meilisearch.core;

import com.meilisearch.sdk.Index;
import com.meilisearch.sdk.Client;
import com.meilisearch.sdk.exceptions.MeilisearchException;

/**
Expand All @@ -26,5 +26,5 @@
@FunctionalInterface
public interface MeilisearchCallback<T> {

T doWithIndex(Index index) throws MeilisearchException;
T doWithClient(Client client) throws MeilisearchException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package io.vanslog.spring.data.meilisearch.core;

import io.vanslog.spring.data.meilisearch.DocumentAccessException;
import io.vanslog.spring.data.meilisearch.IndexAccessException;
import io.vanslog.spring.data.meilisearch.TaskStatusException;
import io.vanslog.spring.data.meilisearch.UncategorizedMeilisearchException;
import io.vanslog.spring.data.meilisearch.annotations.Document;
Expand All @@ -38,9 +37,10 @@
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;

import com.meilisearch.sdk.Index;
import com.meilisearch.sdk.Client;
import com.meilisearch.sdk.exceptions.MeilisearchApiException;
import com.meilisearch.sdk.exceptions.MeilisearchException;
import com.meilisearch.sdk.json.JsonHandler;
import com.meilisearch.sdk.model.TaskInfo;
import com.meilisearch.sdk.model.TaskStatus;

Expand All @@ -53,17 +53,24 @@
*/
public class MeilisearchTemplate implements MeilisearchOperations {

private final MeilisearchClient client;
private final Client meilisearchClient;
private final JsonHandler jsonHandler;
private final int requestTimeout;
private final int requestInterval;
private final MeilisearchConverter meilisearchConverter;

public MeilisearchTemplate(MeilisearchClient client) {
this.client = client;
this.meilisearchConverter = new MappingMeilisearchConverter(new SimpleMeilisearchMappingContext());
this(client, null);
}

public MeilisearchTemplate(MeilisearchClient client, MeilisearchConverter meilisearchConverter) {
this.client = client;
this.meilisearchConverter = meilisearchConverter;
public MeilisearchTemplate(MeilisearchClient client, @Nullable MeilisearchConverter meilisearchConverter) {

this.meilisearchClient = client.getClient();
this.jsonHandler = client.getJsonHandler();
this.requestTimeout = client.getRequestTimeout();
this.requestInterval = client.getRequestInterval();
this.meilisearchConverter = meilisearchConverter != null ? meilisearchConverter
: new MappingMeilisearchConverter(new SimpleMeilisearchMappingContext());
}

@Override
Expand All @@ -75,15 +82,17 @@ public <T> T save(T entity) {
@Override
public <T> List<T> save(List<T> entities) {
Class<?> clazz = entities.iterator().next().getClass();
Index index = getIndexFor(clazz);

String indexUid = getPersistentEntityFor(clazz).getIndexUid();
MeilisearchPersistentProperty idProperty = getPersistentEntityFor(clazz).getIdProperty();
Assert.notNull(idProperty, "Id property must not be null.");
String primaryKey = Objects.requireNonNull(idProperty.getField()).getName();

TaskInfo taskInfo = execute(i -> i.addDocuments(client.getJsonHandler().encode(entities), primaryKey), index);
TaskInfo taskInfo = execute(client -> {
String document = jsonHandler.encode(entities);
return client.index(indexUid).addDocuments(document, primaryKey);
});

if (!isTaskSucceeded(index, taskInfo)) {
if (!isTaskSucceeded(indexUid, taskInfo)) {
throw new TaskStatusException(taskInfo.getStatus(), "Failed to save entities.");
}
return entities;
Expand All @@ -92,18 +101,18 @@ public <T> List<T> save(List<T> entities) {
@Override
@Nullable
public <T> T get(String documentId, Class<T> clazz) {
Index index = getIndexFor(clazz);
String indexUid = getIndexUidFor(clazz);
try {
return execute(i -> i.getDocument(documentId, clazz), index);
return execute(client -> client.index(indexUid).getDocument(documentId, clazz));
} catch (DocumentAccessException e) {
return null;
}
}

@Override
public <T> List<T> multiGet(Class<T> clazz) {
Index index = getIndexFor(clazz);
T[] results = execute(i -> i.getDocuments(clazz).getResults(), index);
String indexUid = getIndexUidFor(clazz);
T[] results = execute(client -> client.index(indexUid).getDocuments(clazz).getResults());
return Arrays.asList(results);
}

Expand All @@ -120,15 +129,15 @@ public boolean exists(String documentId, Class<?> clazz) {

@Override
public long count(Class<?> clazz) {
Index index = getIndexFor(clazz);
return execute(i -> i.getDocuments(clazz).getTotal(), index).longValue();
String indexUid = getIndexUidFor(clazz);
return execute(client -> client.index(indexUid).getDocuments(clazz).getTotal()).longValue();
}

@Override
public boolean delete(String documentId, Class<?> clazz) {
Index index = getIndexFor(clazz);
TaskInfo taskInfo = execute(i -> i.deleteDocument(documentId), index);
return isTaskSucceeded(index, taskInfo);
String indexUid = getIndexUidFor(clazz);
TaskInfo taskInfo = execute(client -> client.index(indexUid).deleteDocument(documentId));
return isTaskSucceeded(indexUid, taskInfo);
}

@Override
Expand All @@ -140,9 +149,9 @@ public <T> boolean delete(T entity) {

@Override
public boolean delete(Class<?> clazz, List<String> documentIds) {
Index index = getIndexFor(clazz);
TaskInfo taskInfo = execute(i -> i.deleteDocuments(documentIds), index);
return isTaskSucceeded(index, taskInfo);
String indexUid = getIndexUidFor(clazz);
TaskInfo taskInfo = execute(client -> client.index(indexUid).deleteDocuments(documentIds));
return isTaskSucceeded(indexUid, taskInfo);
}

@Override
Expand All @@ -154,25 +163,24 @@ public <T> boolean delete(List<T> entities) {

@Override
public boolean deleteAll(Class<?> clazz) {
Index index = getIndexFor(clazz);
TaskInfo taskInfo = execute(Index::deleteAllDocuments, index);
return isTaskSucceeded(index, taskInfo);
String indexUid = getIndexUidFor(clazz);
TaskInfo taskInfo = execute(client -> client.index(indexUid).deleteAllDocuments());
return isTaskSucceeded(indexUid, taskInfo);
}

/**
* Execute the given {@link MeilisearchCallback} within the {@link Index} for the given {@link Class}.
* Execute the given {@link MeilisearchCallback}.
*
* @param callback must not be {@literal null}.
* @param index must not be {@literal null}.
* @return a result object returned by the action or {@literal null}.
* @param <T> the type of the result object
*/
public <T> T execute(MeilisearchCallback<T> callback, Index index) {
public <T> T execute(MeilisearchCallback<T> callback) {

Assert.notNull(callback, "callback must not be null");

try {
return callback.doWithIndex(index);
return callback.doWithClient(meilisearchClient);
} catch (MeilisearchException e) {
MeilisearchApiException ex = (MeilisearchApiException) e;

Expand All @@ -186,19 +194,19 @@ public <T> T execute(MeilisearchCallback<T> callback, Index index) {
/**
* Checks if the given {@link TaskInfo} is succeeded.
*
* @param index the {@link Index} to check
* @param indexUid the index uid
* @param taskInfo the {@link TaskInfo} to check
* @return {@literal true} if the task is succeeded
*/
private boolean isTaskSucceeded(Index index, TaskInfo taskInfo) {
private boolean isTaskSucceeded(String indexUid, TaskInfo taskInfo) {
int taskUid = taskInfo.getTaskUid();

execute((MeilisearchCallback<Void>) i -> {
i.waitForTask(taskUid, client.getRequestTimeout(), client.getRequestInterval());
execute(client -> {
client.index(indexUid).waitForTask(taskUid, requestTimeout, requestInterval);
return null;
}, index);
});

TaskStatus taskStatus = execute(i -> i.getTask(taskUid).getStatus(), index);
TaskStatus taskStatus = execute(client -> client.index(indexUid).getTask(taskUid).getStatus());
return taskStatus == TaskStatus.SUCCEEDED;
}

Expand All @@ -216,14 +224,9 @@ private <T> String getDocumentIdFor(T entity) {
}
}

private <T> Index getIndexFor(Class<T> clazz) {
private <T> String getIndexUidFor(Class<T> clazz) {
MeilisearchPersistentEntity<?> persistentEntity = getPersistentEntityFor(clazz);
String uid = persistentEntity.getIndexUid();
try {
return client.getClient().index(uid);
} catch (MeilisearchException e) {
throw new IndexAccessException(uid);
}
return persistentEntity.getIndexUid();
}

private MeilisearchPersistentEntity<?> getPersistentEntityFor(Class<?> clazz) {
Expand Down

0 comments on commit b371ed6

Please sign in to comment.