Skip to content

DATAES-972 - Entity returned from BeforeConvertCallback should be used for IndexQuery. #555

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -147,13 +147,14 @@ public <T> T save(T entity, IndexCoordinates index) {
Assert.notNull(entity, "entity must not be null");
Assert.notNull(index, "index must not be null");

IndexQuery query = getIndexQuery(entity);
index(query, index);
T entityAfterBeforeConvert = maybeCallbackBeforeConvert(entity, index);

// suppressing because it's either entity itself or something of a correct type returned by an entity callback
@SuppressWarnings("unchecked")
T castResult = (T) query.getObject();
return castResult;
IndexQuery query = getIndexQuery(entityAfterBeforeConvert);
doIndex(query, index);

T entityAfterAfterSave = maybeCallbackAfterSave(entityAfterBeforeConvert, index);

return entityAfterAfterSave;
}

@Override
Expand Down Expand Up @@ -192,6 +193,20 @@ public <T> Iterable<T> save(T... entities) {
return save(Arrays.asList(entities));
}

@Override
public String index(IndexQuery query, IndexCoordinates index) {

maybeCallbackBeforeConvertWithQuery(query, index);

String documentId = doIndex(query, index);

maybeCallbackAfterSaveWithQuery(query, index);

return documentId;
}

public abstract String doIndex(IndexQuery query, IndexCoordinates indexCoordinates);

@Override
@Nullable
public <T> T get(String id, Class<T> clazz) {
Expand Down Expand Up @@ -261,11 +276,38 @@ public List<IndexedObjectInformation> bulkIndex(List<IndexQuery> queries, BulkOp
return bulkIndex(queries, bulkOptions, getIndexCoordinatesFor(clazz));
}

@Override
public final List<IndexedObjectInformation> bulkIndex(List<IndexQuery> queries, BulkOptions bulkOptions,
IndexCoordinates index) {

Assert.notNull(queries, "List of IndexQuery must not be null");
Assert.notNull(bulkOptions, "BulkOptions must not be null");

return bulkOperation(queries, bulkOptions, index);
}

@Override
public void bulkUpdate(List<UpdateQuery> queries, Class<?> clazz) {
bulkUpdate(queries, getIndexCoordinatesFor(clazz));
}

public List<IndexedObjectInformation> bulkOperation(List<?> queries, BulkOptions bulkOptions,
IndexCoordinates index) {

Assert.notNull(queries, "List of IndexQuery must not be null");
Assert.notNull(bulkOptions, "BulkOptions must not be null");

maybeCallbackBeforeConvertWithQueries(queries, index);

List<IndexedObjectInformation> indexedObjectInformations = doBulkOperation(queries, bulkOptions, index);

maybeCallbackAfterSaveWithQueries(queries, index);

return indexedObjectInformations;
}

public abstract List<IndexedObjectInformation> doBulkOperation(List<?> queries, BulkOptions bulkOptions,
IndexCoordinates index);
// endregion

// region SearchOperations
Expand Down Expand Up @@ -620,6 +662,20 @@ protected void maybeCallbackBeforeConvertWithQuery(Object query, IndexCoordinate
if (queryObject != null) {
queryObject = maybeCallbackBeforeConvert(queryObject, index);
indexQuery.setObject(queryObject);
// the callback might have set som values relevant for the IndexQuery
IndexQuery newQuery = getIndexQuery(queryObject);

if (indexQuery.getRouting() == null && newQuery.getRouting() != null) {
indexQuery.setRouting(newQuery.getRouting());
}

if (indexQuery.getSeqNo() == null && newQuery.getSeqNo() != null) {
indexQuery.setSeqNo(newQuery.getSeqNo());
}

if (indexQuery.getPrimaryTerm() == null && newQuery.getPrimaryTerm() != null) {
indexQuery.setPrimaryTerm(newQuery.getPrimaryTerm());
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,7 @@ public IndexOperations indexOps(IndexCoordinates index) {
// endregion

// region DocumentOperations
@Override
public String index(IndexQuery query, IndexCoordinates index) {

maybeCallbackBeforeConvertWithQuery(query, index);
public String doIndex(IndexQuery query, IndexCoordinates index) {

IndexRequest request = requestFactory.indexRequest(query, index);
IndexResponse indexResponse = execute(client -> client.index(request, RequestOptions.DEFAULT));
Expand All @@ -152,8 +149,6 @@ public String index(IndexQuery query, IndexCoordinates index) {
indexResponse.getPrimaryTerm(), indexResponse.getVersion()));
}

maybeCallbackAfterSaveWithQuery(query, index);

return indexResponse.getId();
}

Expand Down Expand Up @@ -187,16 +182,6 @@ protected boolean doExists(String id, IndexCoordinates index) {
return execute(client -> client.get(request, RequestOptions.DEFAULT).isExists());
}

@Override
public List<IndexedObjectInformation> bulkIndex(List<IndexQuery> queries, BulkOptions bulkOptions,
IndexCoordinates index) {

Assert.notNull(queries, "List of IndexQuery must not be null");
Assert.notNull(bulkOptions, "BulkOptions must not be null");

return doBulkOperation(queries, bulkOptions, index);
}

@Override
public void bulkUpdate(List<UpdateQuery> queries, BulkOptions bulkOptions, IndexCoordinates index) {

Expand Down Expand Up @@ -237,14 +222,12 @@ public UpdateResponse update(UpdateQuery query, IndexCoordinates index) {
return new UpdateResponse(result);
}

private List<IndexedObjectInformation> doBulkOperation(List<?> queries, BulkOptions bulkOptions,
public List<IndexedObjectInformation> doBulkOperation(List<?> queries, BulkOptions bulkOptions,
IndexCoordinates index) {
maybeCallbackBeforeConvertWithQueries(queries, index);
BulkRequest bulkRequest = requestFactory.bulkRequest(queries, bulkOptions, index);
List<IndexedObjectInformation> indexedObjectInformationList = checkForBulkOperationFailure(
execute(client -> client.bulk(bulkRequest, RequestOptions.DEFAULT)));
updateIndexedObjectsWithQueries(queries, indexedObjectInformationList);
maybeCallbackAfterSaveWithQueries(queries, index);
return indexedObjectInformationList;
}
// endregion
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,7 @@ public void setSearchTimeout(String searchTimeout) {
// endregion

// region DocumentOperations
@Override
public String index(IndexQuery query, IndexCoordinates index) {

maybeCallbackBeforeConvertWithQuery(query, index);
public String doIndex(IndexQuery query, IndexCoordinates index) {

IndexRequestBuilder indexRequestBuilder = requestFactory.indexRequestBuilder(client, query, index);
ActionFuture<IndexResponse> future = indexRequestBuilder.execute();
Expand All @@ -166,8 +163,6 @@ public String index(IndexQuery query, IndexCoordinates index) {
response.getPrimaryTerm(), response.getVersion()));
}

maybeCallbackAfterSaveWithQuery(query, index);

return documentId;
}

Expand Down Expand Up @@ -201,22 +196,6 @@ protected boolean doExists(String id, IndexCoordinates index) {
return getRequestBuilder.execute().actionGet().isExists();
}

@Override
public List<IndexedObjectInformation> bulkIndex(List<IndexQuery> queries, BulkOptions bulkOptions,
IndexCoordinates index) {

Assert.notNull(queries, "List of IndexQuery must not be null");
Assert.notNull(bulkOptions, "BulkOptions must not be null");

List<IndexedObjectInformation> indexedObjectInformations = doBulkOperation(queries, bulkOptions, index);

updateIndexedObjectsWithQueries(queries, indexedObjectInformations);

maybeCallbackAfterSaveWithQueries(queries, index);

return indexedObjectInformations;
}

@Override
public void bulkUpdate(List<UpdateQuery> queries, BulkOptions bulkOptions, IndexCoordinates index) {

Expand Down Expand Up @@ -261,11 +240,13 @@ public UpdateResponse update(UpdateQuery query, IndexCoordinates index) {
return new UpdateResponse(result);
}

private List<IndexedObjectInformation> doBulkOperation(List<?> queries, BulkOptions bulkOptions,
public List<IndexedObjectInformation> doBulkOperation(List<?> queries, BulkOptions bulkOptions,
IndexCoordinates index) {
maybeCallbackBeforeConvertWithQueries(queries, index);
BulkRequestBuilder bulkRequest = requestFactory.bulkRequestBuilder(client, queries, bulkOptions, index);
return checkForBulkOperationFailure(bulkRequest.execute().actionGet());
final List<IndexedObjectInformation> indexedObjectInformations = checkForBulkOperationFailure(
bulkRequest.execute().actionGet());
updateIndexedObjectsWithQueries(queries, indexedObjectInformations);
return indexedObjectInformations;
}
// endregion

Expand Down
Loading