Skip to content

Reactive implementation of the point in time API. #2275

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
merged 1 commit into from
Aug 20, 2022
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 @@ -276,6 +276,48 @@ public Mono<ClearScrollResponse> clearScroll(

return clearScroll(fn.apply(new ClearScrollRequest.Builder()).build());
}

/**
* @since 5.0
*/
public Mono<OpenPointInTimeResponse> openPointInTime(OpenPointInTimeRequest request) {

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

return Mono.fromFuture(transport.performRequestAsync(request, OpenPointInTimeRequest._ENDPOINT, transportOptions));
}

/**
* @since 5.0
*/
public Mono<OpenPointInTimeResponse> openPointInTime(
Function<OpenPointInTimeRequest.Builder, ObjectBuilder<OpenPointInTimeRequest>> fn) {

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

return openPointInTime(fn.apply(new OpenPointInTimeRequest.Builder()).build());
}

/**
* @since 5.0
*/
public Mono<ClosePointInTimeResponse> closePointInTime(ClosePointInTimeRequest request) {

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

return Mono.fromFuture(transport.performRequestAsync(request, ClosePointInTimeRequest._ENDPOINT, transportOptions));
}

/**
* @since 5.0
*/
public Mono<ClosePointInTimeResponse> closePointInTime(
Function<ClosePointInTimeRequest.Builder, ObjectBuilder<ClosePointInTimeRequest>> fn) {

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

return closePointInTime(fn.apply(new ClosePointInTimeRequest.Builder()).build());
}
// endregion

}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import reactor.core.publisher.Mono;
import reactor.util.function.Tuple2;

import java.time.Duration;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -408,6 +409,30 @@ public Flux<? extends AggregationContainer<?>> aggregate(Query query, Class<?> e
});
}

@Override
public Mono<String> openPointInTime(IndexCoordinates index, Duration keepAlive, Boolean ignoreUnavailable) {

Assert.notNull(index, "index must not be null");
Assert.notNull(keepAlive, "keepAlive must not be null");
Assert.notNull(ignoreUnavailable, "ignoreUnavailable must not be null");

var request = requestConverter.searchOpenPointInTimeRequest(index, keepAlive, ignoreUnavailable);
return Mono
.from(execute((ClientCallback<Publisher<OpenPointInTimeResponse>>) client -> client.openPointInTime(request)))
.map(OpenPointInTimeResponse::id);
}

@Override
public Mono<Boolean> closePointInTime(String pit) {

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

ClosePointInTimeRequest request = requestConverter.searchClosePointInTime(pit);
return Mono
.from(execute((ClientCallback<Publisher<ClosePointInTimeResponse>>) client -> client.closePointInTime(request)))
.map(ClosePointInTimeResponse::succeeded);
}

// endregion

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import reactor.core.publisher.Mono;
import reactor.util.function.Tuple2;

import java.time.Duration;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
Expand All @@ -29,6 +30,7 @@
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.data.convert.EntityReader;
import org.springframework.data.elasticsearch.client.UnsupportedClientOperationException;
import org.springframework.data.elasticsearch.core.convert.ElasticsearchConverter;
import org.springframework.data.elasticsearch.core.convert.MappingElasticsearchConverter;
import org.springframework.data.elasticsearch.core.document.Document;
Expand Down Expand Up @@ -476,6 +478,17 @@ public Mono<Long> count(Query query, Class<?> entityType, IndexCoordinates index
}

abstract protected Mono<Long> doCount(Query query, Class<?> entityType, IndexCoordinates index);

@Override
public Mono<String> openPointInTime(IndexCoordinates index, Duration keepAlive, Boolean ignoreUnavailable) {
throw new UnsupportedClientOperationException(getClass(), "openPointInTime");
}

@Override
public Mono<Boolean> closePointInTime(String pit) {
throw new UnsupportedClientOperationException(getClass(), "closePointInTime");
}

// endregion

// region callbacks
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,14 @@ public interface ReactiveSearchHits<T> {
* @return wether the {@link SearchHits} has a suggest response.
*/
boolean hasSuggest();

/**
* When doing a search with a point in time, the response contains a new point in time id value.
*
* @return the new point in time id, if one was returned from Elasticsearch
* @since 5.0
*/
@Nullable
String getPointInTimeId();

}
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,13 @@ public Suggest getSuggest() {
public boolean hasSuggest() {
return delegate.hasSuggest();
}

/**
* @since 5.0
*/
@Nullable
@Override
public String getPointInTimeId() {
return delegate.getPointInTimeId();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.time.Duration;
import java.util.List;

import org.springframework.data.domain.Pageable;
Expand Down Expand Up @@ -271,6 +272,38 @@ <T> Mono<ReactiveSearchHits<T>> searchForHits(Query query, Class<?> entityType,
*/
Mono<Suggest> suggest(Query query, Class<?> entityType, IndexCoordinates index);

/**
* Opens a point in time (pit) in Elasticsearch.
*
* @param index the index name(s) to use
* @param keepAlive the duration the pit shoult be kept alive
* @return the pit identifier
* @since 5.0
*/
default Mono<String> openPointInTime(IndexCoordinates index, Duration keepAlive) {
return openPointInTime(index, keepAlive, false);
}

/**
* Opens a point in time (pit) in Elasticsearch.
*
* @param index the index name(s) to use
* @param keepAlive the duration the pit shoult be kept alive
* @param ignoreUnavailable if {$literal true} the call will fail if any of the indices is missing or closed
* @return the pit identifier
* @since 5.0
*/
Mono<String> openPointInTime(IndexCoordinates index, Duration keepAlive, Boolean ignoreUnavailable);

/**
* Closes a point in time
*
* @param pit the pit identifier as returned by {@link #openPointInTime(IndexCoordinates, Duration, Boolean)}
* @return {@literal true} on success
* @since 5.0
*/
Mono<Boolean> closePointInTime(String pit);

// region helper
/**
* Creates a {@link Query} to find all documents. Must be implemented by the concrete implementations to provide an
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright 2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.elasticsearch.core;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.data.elasticsearch.junit.jupiter.ReactiveElasticsearchTemplateConfiguration;
import org.springframework.data.elasticsearch.utils.IndexNameProvider;
import org.springframework.test.context.ContextConfiguration;

/**
* @author Peter-Josef Meisch
* @since 5.0
*/
@ContextConfiguration(classes = ReactivePointInTimeELCIntegrationTests.Config.class)
public class ReactivePointInTimeELCIntegrationTests extends ReactivePointInTimeIntegrationTests {

@Configuration
@Import({ ReactiveElasticsearchTemplateConfiguration.class })
static class Config {
@Bean
IndexNameProvider indexNameProvider() {
return new IndexNameProvider("reactive-point-in-time");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright 2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.elasticsearch.core;

import org.junit.jupiter.api.Disabled;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.data.elasticsearch.junit.jupiter.ReactiveElasticsearchRestTemplateConfiguration;
import org.springframework.data.elasticsearch.utils.IndexNameProvider;
import org.springframework.test.context.ContextConfiguration;

/**
* This test class is disabled on purpose. PIT will be introduced in Spring Data Elasticsearch 5.0 where the old
* RestHighLevelClient and the {@link org.springframework.data.elasticsearch.client.erhlc.ElasticsearchRestTemplate} are
* deprecated. We therefore do not add new features to this implementation anymore. Furthermore we cannot copy the
* necessary code for the reactive implementation like we did before, as point in time was introduced in Elasticsearch
* 7.12 after the license change.
*
* @author Peter-Josef Meisch
*/
@Disabled
@ContextConfiguration(classes = ReactivePointInTimeERHLCIntegrationTests.Config.class)
public class ReactivePointInTimeERHLCIntegrationTests extends ReactivePointInTimeIntegrationTests {

@Configuration
@Import({ ReactiveElasticsearchRestTemplateConfiguration.class })
static class Config {
@Bean
IndexNameProvider indexNameProvider() {
return new IndexNameProvider("reactive-point-in-time-es7");
}
}
}
Loading