-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP [HTM-1076][HTM-1077] TMFeatureType event handler to propagate cha…
…nges to solr indexes
- Loading branch information
Showing
7 changed files
with
256 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
145 changes: 145 additions & 0 deletions
145
src/main/java/org/tailormap/api/repository/events/SolrTMFeatureTypeEventHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
/* | ||
* Copyright (C) 2024 B3Partners B.V. | ||
* | ||
* SPDX-License-Identifier: MIT | ||
*/ | ||
package org.tailormap.api.repository.events; | ||
|
||
import java.io.IOException; | ||
import java.lang.invoke.MethodHandles; | ||
import nl.b3p.tailormap.api.geotools.featuresources.FeatureSourceFactoryHelper; | ||
import nl.b3p.tailormap.api.persistence.SearchIndex; | ||
import nl.b3p.tailormap.api.persistence.TMFeatureType; | ||
import nl.b3p.tailormap.api.repository.GeoServiceRepository; | ||
import nl.b3p.tailormap.api.repository.SearchIndexRepository; | ||
import nl.b3p.tailormap.api.solr.SolrHelper; | ||
import nl.b3p.tailormap.api.solr.SolrService; | ||
import org.apache.solr.client.solrj.SolrServerException; | ||
import org.apache.solr.common.SolrException; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.data.rest.core.annotation.HandleAfterDelete; | ||
import org.springframework.data.rest.core.annotation.HandleBeforeSave; | ||
import org.springframework.data.rest.core.annotation.RepositoryEventHandler; | ||
|
||
/** Event handler for Solr indexes when a {@code TMFeatureType} is updated or deleted. */ | ||
@RepositoryEventHandler | ||
public class SolrTMFeatureTypeEventHandler { | ||
|
||
private static final Logger logger = | ||
LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); | ||
|
||
private final SearchIndexRepository searchIndexRepository; | ||
private final SolrService solrService; | ||
private final FeatureSourceFactoryHelper featureSourceFactoryHelper; | ||
private final GeoServiceRepository geoServiceRepository; | ||
|
||
public SolrTMFeatureTypeEventHandler( | ||
SearchIndexRepository searchIndexRepository, | ||
SolrService solrService, | ||
FeatureSourceFactoryHelper featureSourceFactoryHelper, | ||
GeoServiceRepository geoServiceRepository) { | ||
this.searchIndexRepository = searchIndexRepository; | ||
this.solrService = solrService; | ||
this.featureSourceFactoryHelper = featureSourceFactoryHelper; | ||
this.geoServiceRepository = geoServiceRepository; | ||
} | ||
|
||
/** | ||
* Handle the update of a TMFeatureType. | ||
* | ||
* @param tmFeatureType the TMFeatureType to handle | ||
*/ | ||
@HandleBeforeSave | ||
public void handleTMFeatureTypeUpdate(TMFeatureType tmFeatureType) { | ||
logger.debug("Handling TMFeatureType save event for: {}", tmFeatureType); | ||
// determine if it is a new FT or an update | ||
if (null == tmFeatureType.getId()) { | ||
// do nothing as there is no index defined for a new feature type | ||
logger.debug("New TMFeatureType: {}", tmFeatureType); | ||
} else { | ||
logger.debug("Updated TMFeatureType: {}", tmFeatureType); | ||
searchIndexRepository.findByFeatureTypeId(tmFeatureType.getId()).stream() | ||
.findAny() | ||
.ifPresent( | ||
searchIndex -> { | ||
logger.debug( | ||
"Updating search index {} for feature type: {}", | ||
searchIndex.getName(), | ||
searchIndex); | ||
|
||
try (SolrHelper solrHelper = | ||
new SolrHelper(solrService.getSolrClientForIndexing())) { | ||
solrHelper.addFeatureTypeIndex( | ||
searchIndex, tmFeatureType, featureSourceFactoryHelper); | ||
} catch (UnsupportedOperationException | ||
| IOException | ||
| SolrServerException | ||
| SolrException e) { | ||
logger.error("Error re-indexing", e); | ||
searchIndex.setStatus(SearchIndex.Status.ERROR); | ||
searchIndexRepository.save(searchIndex); | ||
} | ||
}); | ||
} | ||
} | ||
|
||
/** | ||
* Handle the deletion of a TMFeatureType. | ||
* | ||
* @param tmFeatureType the TMFeatureType to handle | ||
*/ | ||
@HandleAfterDelete | ||
public void handleTMFeatureTypeDeleteForSolr(TMFeatureType tmFeatureType) { | ||
logger.debug("Handling TMFeatureType delete event for: {}", tmFeatureType); | ||
searchIndexRepository.findByFeatureTypeId(tmFeatureType.getId()).stream() | ||
.findAny() | ||
.ifPresent( | ||
searchIndex -> { | ||
logger.info( | ||
"Deleting search index {} for feature type: {}", | ||
searchIndex.getName(), | ||
searchIndex); | ||
|
||
try (SolrHelper solrHelper = new SolrHelper(solrService.getSolrClientForIndexing())) { | ||
solrHelper.clearIndexForLayer(searchIndex.getId()); | ||
searchIndexRepository.delete(searchIndex); | ||
// find layers that use this index in the layer settings and clear them | ||
geoServiceRepository | ||
.findByIndexId(searchIndex.getId()) | ||
.forEach( | ||
geoService -> | ||
geoService | ||
.getLayers() | ||
.forEach( | ||
layer -> { | ||
logger.debug( | ||
"Checking layer {} for search index {}", | ||
layer.getName(), | ||
searchIndex.getName()); | ||
geoService | ||
.findSearchIndexForLayer(layer, searchIndexRepository) | ||
.ifPresent( | ||
searchIndex1 -> { | ||
if (searchIndex1 | ||
.getId() | ||
.equals(searchIndex.getId())) { | ||
logger.debug( | ||
"Clearing search index for layer {}", | ||
layer.getName()); | ||
geoService | ||
.getLayerSettings(layer.getName()) | ||
.setSearchIndex(null); | ||
geoServiceRepository.save(geoService); | ||
} | ||
}); | ||
})); | ||
} catch (UnsupportedOperationException | ||
| IOException | ||
| SolrServerException | ||
| SolrException e) { | ||
logger.error("Error deleting index for {}", searchIndex, e); | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
* Copyright (C) 2024 B3Partners B.V. | ||
* | ||
* SPDX-License-Identifier: MIT | ||
*/ | ||
package org.tailormap.api.solr; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
import org.apache.solr.client.solrj.SolrClient; | ||
import org.apache.solr.client.solrj.impl.ConcurrentUpdateHttp2SolrClient; | ||
import org.apache.solr.client.solrj.impl.Http2SolrClient; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
public class SolrService { | ||
@Value("${tailormap-api.solr-url}") | ||
private String solrUrl; | ||
|
||
@Value("${tailormap-api.solr-core-name:tailormap}") | ||
private String solrCoreName; | ||
|
||
/** | ||
* Get a concurrent update Solr client for bulk operations. | ||
* | ||
* @return the Solr client | ||
*/ | ||
public SolrClient getSolrClientForIndexing() { | ||
return new ConcurrentUpdateHttp2SolrClient.Builder( | ||
solrUrl + solrCoreName, | ||
new Http2SolrClient.Builder() | ||
.withFollowRedirects(true) | ||
.withConnectionTimeout(10000, TimeUnit.MILLISECONDS) | ||
.withRequestTimeout(60000, TimeUnit.MILLISECONDS) | ||
.build()) | ||
.withQueueSize(SolrHelper.SOLR_BATCH_SIZE * 2) | ||
.withThreadCount(10) | ||
.build(); | ||
} | ||
|
||
/** | ||
* Get a Solr client for searching. | ||
* | ||
* @return the Solr client | ||
*/ | ||
public SolrClient getSolrClientForSearching() { | ||
return new Http2SolrClient.Builder(solrUrl + solrCoreName) | ||
.withConnectionTimeout(10, TimeUnit.SECONDS) | ||
.withFollowRedirects(true) | ||
.build(); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
src/test/java/org/tailormap/api/repository/GeoServiceRepositoryIntegrationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* Copyright (C) 2024 B3Partners B.V. | ||
* | ||
* SPDX-License-Identifier: MIT | ||
*/ | ||
package org.tailormap.api.repository; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import java.util.List; | ||
import nl.b3p.tailormap.api.annotation.PostgresIntegrationTest; | ||
import nl.b3p.tailormap.api.persistence.GeoService; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
|
||
@PostgresIntegrationTest | ||
public class GeoServiceRepositoryIntegrationTest { | ||
|
||
@Autowired private GeoServiceRepository geoServiceRepository; | ||
|
||
@Test | ||
void it_should_find_service_using_findByIndexId_with_valid_ID() { | ||
final GeoService geoService = geoServiceRepository.findByIndexId(2L).get(0); | ||
assertNotNull(geoService); | ||
assertEquals("snapshot-geoserver", geoService.getId()); | ||
} | ||
|
||
@Test | ||
void it_should_not_find_services_findByIndexId_with_invalid_ID() { | ||
final List<GeoService> geoServices = geoServiceRepository.findByIndexId(-2L); | ||
assertTrue(geoServices.isEmpty()); | ||
} | ||
} |