Skip to content
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
@@ -0,0 +1,14 @@
package au.org.aodn.ogcapi.server.core.configuration;

import au.org.aodn.ogcapi.server.core.service.CacheNoLandGeometry;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class CacheConfig {

@Bean
public CacheNoLandGeometry createCacheNoLandGeometry() {
return new CacheNoLandGeometry();
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package au.org.aodn.ogcapi.server.core.configuration;

import au.org.aodn.ogcapi.server.core.service.CacheNoLandGeometry;
import au.org.aodn.ogcapi.server.core.service.ElasticSearch;
import au.org.aodn.ogcapi.server.core.service.Search;
import co.elastic.clients.elasticsearch.ElasticsearchClient;
Expand Down Expand Up @@ -59,11 +60,12 @@ public ElasticsearchClient geoNetworkElasticsearchClient(RestClientTransport tra
*/
@Bean
public Search createElasticSearch(ElasticsearchClient client,
CacheNoLandGeometry cacheNoLandGeometry,
ObjectMapper mapper,
@Value("${elasticsearch.index.name}") String indexName,
@Value("${elasticsearch.index.pageSize:2200}") Integer pageSize,
@Value("${elasticsearch.search_as_you_type.size:10}") Integer searchAsYouTypeSize) {

return new ElasticSearch(client, mapper, indexName, pageSize, searchAsYouTypeSize);
return new ElasticSearch(client, cacheNoLandGeometry, mapper, indexName, pageSize, searchAsYouTypeSize);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ public enum CQLFields implements CQLFieldsInterface {
null,
null
),
centroid_nocache(
StacSummeries.GeometryNoLand.searchField,
StacSummeries.GeometryNoLand.displayField,
null,
null
),
temporal(
StacSummeries.Temporal.searchField,
StacSummeries.Temporal.displayField,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package au.org.aodn.ogcapi.server.core.service;

import au.org.aodn.ogcapi.server.core.model.StacCollectionModel;
import au.org.aodn.ogcapi.server.core.model.enumeration.CQLFields;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Scheduled;

import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
* This class is used to cache some of the search result which is very expensive to transfer.
*/
@Slf4j
public class CacheNoLandGeometry {

@Lazy
@Autowired
ElasticSearch elasticSearch;

@Autowired
@Lazy
private CacheNoLandGeometry self;

/**
* Init cache after 1 second but then never call again due to Long.MAX_VALUE
* @return A Map include the uuid and the noloand geometry
*/
@Scheduled(initialDelay = 1000, fixedDelay = Long.MAX_VALUE)
@Cacheable("all_noland_geometry")
public Map<String, StacCollectionModel> getAllNoLandGeometry() {
ElasticSearchBase.SearchResult result = elasticSearch.searchCollectionBy(
null,
null,
null,
List.of(CQLFields.id.name(), CQLFields.centroid_nocache.name()),
null,
null,
null,
null);

return result.collections
.stream()
.collect(Collectors.toMap(StacCollectionModel::getUuid, Function.identity()));
}
/**
* Refresh every 24 hrs
*/
@Scheduled(initialDelay = 86400000, fixedDelay = 86400000)
@CacheEvict(value = "all_noland_geometry", allEntries = true)
protected void refreshAllCache() {
// Refresh on Evict
log.info("Evict and refresh cache");
self.getAllNoLandGeometry();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public class ElasticSearch extends ElasticSearchBase implements Search {
protected String searchAfterSplitRegex;

public ElasticSearch(ElasticsearchClient client,
CacheNoLandGeometry cacheNoLandGeometry,
ObjectMapper mapper,
String indexName,
Integer pageSize,
Expand All @@ -69,6 +70,7 @@ public ElasticSearch(ElasticsearchClient client,
this.setIndexName(indexName);
this.setPageSize(pageSize);
this.setSearchAsYouTypeSize(searchAsYouTypeSize);
this.setCacheNoLandGeometry(cacheNoLandGeometry);
this.defaultElasticSetting = CQLToElasticFilterFactory.getDefaultSetting();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ abstract class ElasticSearchBase {
protected Integer pageSize;
protected ElasticsearchClient esClient;
protected ObjectMapper mapper;
protected CacheNoLandGeometry cacheNoLandGeometry;

@Getter
@Setter
Expand Down Expand Up @@ -88,7 +89,7 @@ protected SearchRequest buildSearchAsYouTypeRequest(
List<Query> searchAsYouTypeQueries,
List<Query> filters) {

// By default it is limited to 10 even not specify, we want to use a variable so that we can change it later if needed.
// By default, it is limited to 10 even not specify, we want to use a variable so that we can change it later if needed.
return new SearchRequest.Builder()
.size(searchAsYouTypeSize)
.index(indexName)
Expand Down Expand Up @@ -166,6 +167,8 @@ protected SearchResult searchCollectionBy(final List<Query> queries,
// Convert the income field name to the real field name in STAC
List<String> fs = properties
.stream()
// Centroid point is cached, so no need to set it in the query but need to back-fill it
.filter(f -> !f.equalsIgnoreCase(CQLFields.centroid.name()))
.flatMap(v -> CQLFields.valueOf(v).getDisplayField().stream())
.filter(Objects::nonNull)
.toList();
Expand Down Expand Up @@ -195,7 +198,20 @@ protected SearchResult searchCollectionBy(final List<Query> queries,
List<FieldValue> lastSortValue = null;
for(Hit<ObjectNode> i : response) {
if(i != null) {
result.collections.add(this.formatResult(i.source()));
StacCollectionModel model = this.formatResult(i.source());
// Use cache value of noland_geometry if user request centroid
if(properties != null && properties.contains(CQLFields.centroid.name())) {
StacCollectionModel cache = cacheNoLandGeometry.getAllNoLandGeometry().get(model.getUuid());
if(cache.getSummaries() != null) {
if(model.getSummaries() == null) {
model.setSummaries(cache.getSummaries());
}
else {
model.getSummaries().setGeometryNoLand(cache.getSummaries().getGeometryNoLand());
}
}
}
result.collections.add(model);
lastSortValue = i.sort();
}
}
Expand Down
Loading