Skip to content

Commit

Permalink
Refactor repositories (#596)
Browse files Browse the repository at this point in the history
  • Loading branch information
vpaturet authored Nov 20, 2024
1 parent a5c3de8 commit c0df969
Show file tree
Hide file tree
Showing 25 changed files with 320 additions and 431 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
<entur.google.pubsub.emulator.download.skip>false</entur.google.pubsub.emulator.download.skip>
<camel.version>4.4.4</camel.version>
<entur.helpers.version>3.0</entur.helpers.version>
<netex-validator-java.version>4.3.0</netex-validator-java.version>
<netex-validator-java.version>5.0.0</netex-validator-java.version>
<netex-parser-java.version>3.1.29</netex-parser-java.version>
<commons-io.version>2.11.0</commons-io.version>
<zt-zip.version>1.17</zt-zip.version>
Expand Down
34 changes: 34 additions & 0 deletions src/main/java/no/entur/antu/config/CommonDataConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package no.entur.antu.config;

import static no.entur.antu.config.cache.CacheConfig.*;

import java.util.Map;
import no.entur.antu.netexdata.RedisCommonDataRepository;
import org.entur.netex.validation.validator.jaxb.CommonDataRepositoryLoader;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;

@Configuration
public class CommonDataConfig {

@Bean
@Profile("!test")
CommonDataRepositoryLoader commonDataRepository(
RedissonClient redissonClient,
@Qualifier(
SCHEDULED_STOP_POINT_AND_QUAY_ID_CACHE
) Map<String, Map<String, String>> scheduledStopPointAndQuayIdCache,
@Qualifier(
SERVICE_LINKS_AND_SCHEDULED_STOP_POINT_IDS_CACHE
) Map<String, Map<String, String>> serviceLinksAndFromToScheduledStopPointIdCache
) {
return new RedisCommonDataRepository(
redissonClient,
scheduledStopPointAndQuayIdCache,
serviceLinksAndFromToScheduledStopPointIdCache
);
}
}
14 changes: 2 additions & 12 deletions src/main/java/no/entur/antu/config/NetexDataConfig.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package no.entur.antu.config;

import static no.entur.antu.config.cache.CacheConfig.LINE_INFO_CACHE;
import static no.entur.antu.config.cache.CacheConfig.SCHEDULED_STOP_POINT_AND_QUAY_ID_CACHE;
import static no.entur.antu.config.cache.CacheConfig.SERVICE_JOURNEY_INTERCHANGE_INFO_CACHE;
import static no.entur.antu.config.cache.CacheConfig.SERVICE_JOURNEY_STOPS_CACHE;
import static no.entur.antu.config.cache.CacheConfig.SERVICE_LINKS_AND_SCHEDULED_STOP_POINT_IDS_CACHE;

import java.util.List;
import java.util.Map;
import no.entur.antu.netexdata.DefaultNetexDataRepository;
import no.entur.antu.netexdata.RedisNetexDataRepository;
import org.entur.netex.validation.validator.jaxb.NetexDataRepository;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Qualifier;
Expand All @@ -23,12 +21,6 @@ public class NetexDataConfig {
@Profile("!test")
NetexDataRepository netexDataRepository(
RedissonClient redissonClient,
@Qualifier(
SCHEDULED_STOP_POINT_AND_QUAY_ID_CACHE
) Map<String, Map<String, String>> scheduledStopPointAndQuayIdCache,
@Qualifier(
SERVICE_LINKS_AND_SCHEDULED_STOP_POINT_IDS_CACHE
) Map<String, Map<String, String>> serviceLinksAndFromToScheduledStopPointIdCache,
@Qualifier(LINE_INFO_CACHE) Map<String, List<String>> lineInfoCache,
@Qualifier(
SERVICE_JOURNEY_STOPS_CACHE
Expand All @@ -37,10 +29,8 @@ NetexDataRepository netexDataRepository(
SERVICE_JOURNEY_INTERCHANGE_INFO_CACHE
) Map<String, List<String>> serviceJourneyInterchangeInfoCache
) {
return new DefaultNetexDataRepository(
return new RedisNetexDataRepository(
redissonClient,
scheduledStopPointAndQuayIdCache,
serviceLinksAndFromToScheduledStopPointIdCache,
lineInfoCache,
serviceJourneyStopsCache,
serviceJourneyInterchangeInfoCache
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import org.entur.netex.validation.validator.id.ReferenceToValidEntityTypeValidator;
import org.entur.netex.validation.validator.id.VersionOnLocalNetexIdValidator;
import org.entur.netex.validation.validator.id.VersionOnRefToLocalNetexIdValidator;
import org.entur.netex.validation.validator.jaxb.CommonDataRepositoryLoader;
import org.entur.netex.validation.validator.jaxb.JAXBValidator;
import org.entur.netex.validation.validator.jaxb.NetexDataCollector;
import org.entur.netex.validation.validator.jaxb.NetexDataRepository;
Expand Down Expand Up @@ -285,6 +286,7 @@ public NetexValidatorsRunner timetableDataValidatorsRunner(
LineInfoCollector lineInfoCollector,
ServiceJourneyStopsCollector serviceJourneyStopsCollector,
ServiceJourneyInterchangeInfoCollector serviceJourneyInterchangeInfoCollector,
CommonDataRepositoryLoader commonDataRepository,
NetexDataRepository netexDataRepository,
StopPlaceRepository stopPlaceRepository
) {
Expand Down Expand Up @@ -338,6 +340,7 @@ public NetexValidatorsRunner timetableDataValidatorsRunner(
.withJaxbValidators(jaxbValidators)
.withDatasetValidators(netexTimetableDatasetValidators)
.withNetexDataCollectors(commonDataCollectors)
.withCommonDataRepository(commonDataRepository)
.withNetexDataRepository(netexDataRepository)
.withStopPlaceRepository(stopPlaceRepository)
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.entur.netex.validation.validator.id.ReferenceToValidEntityTypeValidator;
import org.entur.netex.validation.validator.id.VersionOnLocalNetexIdValidator;
import org.entur.netex.validation.validator.id.VersionOnRefToLocalNetexIdValidator;
import org.entur.netex.validation.validator.jaxb.CommonDataRepositoryLoader;
import org.entur.netex.validation.validator.jaxb.JAXBValidator;
import org.entur.netex.validation.validator.jaxb.NetexDataRepository;
import org.entur.netex.validation.validator.jaxb.StopPlaceRepository;
Expand Down Expand Up @@ -143,6 +144,7 @@ public NetexValidatorsRunner flexTimetableDataValidatorsRunner(
NetexReferenceValidator netexReferenceValidator,
FileNameValidator fileNameValidator,
InvalidFlexibleAreaValidator invalidFlexibleAreaValidator,
CommonDataRepositoryLoader commonDataRepository,
NetexDataRepository netexDataRepository,
StopPlaceRepository stopPlaceRepository
) {
Expand All @@ -165,6 +167,7 @@ public NetexValidatorsRunner flexTimetableDataValidatorsRunner(
.withNetexSchemaValidator(netexSchemaValidator)
.withXPathValidators(xpathValidators)
.withJaxbValidators(jaxbValidators)
.withCommonDataRepository(commonDataRepository)
.withNetexDataRepository(netexDataRepository)
.withStopPlaceRepository(stopPlaceRepository)
.build();
Expand All @@ -189,6 +192,7 @@ public NetexValidatorsRunner importFlexTimetableDataValidatorsRunner(
NetexReferenceValidator netexReferenceValidator,
FileNameValidator fileNameValidator,
InvalidFlexibleAreaValidator invalidFlexibleAreaValidator,
CommonDataRepositoryLoader commonDataRepository,
NetexDataRepository netexDataRepository,
StopPlaceRepository stopPlaceRepository
) {
Expand All @@ -213,6 +217,7 @@ public NetexValidatorsRunner importFlexTimetableDataValidatorsRunner(
.withNetexSchemaValidator(netexSchemaValidator)
.withXPathValidators(xpathValidators)
.withJaxbValidators(jaxbValidators)
.withCommonDataRepository(commonDataRepository)
.withNetexDataRepository(netexDataRepository)
.withStopPlaceRepository(stopPlaceRepository)
.build();
Expand Down
117 changes: 14 additions & 103 deletions src/main/java/no/entur/antu/netexdata/DefaultNetexDataRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,95 +2,38 @@

import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import no.entur.antu.exception.AntuException;
import org.entur.netex.validation.validator.jaxb.*;
import org.entur.netex.validation.validator.model.*;
import org.redisson.api.RedissonClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.entur.netex.validation.validator.model.ActiveDates;
import org.entur.netex.validation.validator.model.DayTypeId;
import org.entur.netex.validation.validator.model.ServiceJourneyId;
import org.entur.netex.validation.validator.model.ServiceJourneyInterchangeInfo;
import org.entur.netex.validation.validator.model.ServiceJourneyStop;
import org.entur.netex.validation.validator.model.SimpleLine;

/**
* Default implementation of NetexDataRepository.
* This repository is used to store and retrieve common data from Redis cache.
* This repository is used to store and retrieve collected data from Redis cache.
*/
public class DefaultNetexDataRepository implements NetexDataRepository {
public class DefaultNetexDataRepository implements NetexDataRepositoryLoader {

private static final Logger LOGGER = LoggerFactory.getLogger(
DefaultNetexDataRepository.class
);

private final RedissonClient redissonClient;
private final Map<String, Map<String, String>> scheduledStopPointAndQuayIdCache;
private final Map<String, Map<String, String>> serviceLinksAndFromToScheduledStopPointIdCache;
private final Map<String, List<String>> lineInfoCache;
private final Map<String, Map<String, List<String>>> serviceJourneyStopsCache;
private final Map<String, List<String>> serviceJourneyInterchangeInfoCache;

public DefaultNetexDataRepository(
RedissonClient redissonClient,
Map<String, Map<String, String>> scheduledStopPointAndQuayIdCache,
Map<String, Map<String, String>> serviceLinksAndFromToScheduledStopPointIdCache,
Map<String, List<String>> lineInfoCache,
Map<String, Map<String, List<String>>> serviceJourneyStopsCache,
Map<String, List<String>> serviceJourneyInterchangeInfoCache
) {
this.redissonClient = redissonClient;
this.scheduledStopPointAndQuayIdCache = scheduledStopPointAndQuayIdCache;
this.serviceLinksAndFromToScheduledStopPointIdCache =
serviceLinksAndFromToScheduledStopPointIdCache;
this.lineInfoCache = lineInfoCache;
this.serviceJourneyStopsCache = serviceJourneyStopsCache;
this.serviceJourneyInterchangeInfoCache =
serviceJourneyInterchangeInfoCache;
}

@Override
public boolean hasQuayIds(String validationReportId) {
Map<String, String> idsForReport = scheduledStopPointAndQuayIdCache.get(
validationReportId
);
return idsForReport != null && !idsForReport.isEmpty();
}

@Override
public QuayId quayIdForScheduledStopPoint(
ScheduledStopPointId scheduledStopPointId,
String validationReportId
) {
Map<String, String> idsForReport = scheduledStopPointAndQuayIdCache.get(
validationReportId
);
if (idsForReport == null) {
throw new AntuException(
"Quay ids cache not found for validation report with id: " +
validationReportId
);
}
return QuayId.ofValidId(idsForReport.get(scheduledStopPointId.id()));
}

@Override
public FromToScheduledStopPointId fromToScheduledStopPointIdForServiceLink(
ServiceLinkId serviceLinkId,
String validationReportId
) {
Map<String, String> idsForReport =
serviceLinksAndFromToScheduledStopPointIdCache.get(validationReportId);
if (idsForReport == null) {
throw new AntuException(
"Service links cache not found for validation report with id: " +
validationReportId
);
}
return FromToScheduledStopPointId.fromString(
idsForReport.get(serviceLinkId.id())
);
}

@Override
public List<SimpleLine> lineNames(String validationReportId) {
List<String> lineInfoForReportId = lineInfoCache.get(validationReportId);
Expand Down Expand Up @@ -138,52 +81,20 @@ public List<ServiceJourneyInterchangeInfo> serviceJourneyInterchangeInfos(
}

@Override
public void fillNetexDataCache(
byte[] fileContent,
public Map<ServiceJourneyId, List<DayTypeId>> serviceJourneyDayTypes(
String validationReportId
) {
NetexDataResource netexDataResource = new NetexDataResource();
netexDataResource.loadNetexData(fileContent);
// Merging with the existing map, for handing the case where there are
// multiple common files in the dataset.
scheduledStopPointAndQuayIdCache.merge(
validationReportId,
netexDataResource.getQuayIdsPerScheduledStopPoints(),
(existingMap, newMap) -> {
existingMap.putAll(newMap);
return existingMap;
}
);

Map<String, String> scheduledStopPointIdsPerServiceLinkId =
netexDataResource
.getFromToScheduledStopPointIdPerServiceLinkId()
.entrySet()
.stream()
.collect(Collectors.toMap(Entry::getKey, Entry::getValue));

serviceLinksAndFromToScheduledStopPointIdCache.merge(
validationReportId,
scheduledStopPointIdsPerServiceLinkId,
(existingMap, newMap) -> {
existingMap.putAll(newMap);
return existingMap;
}
);
throw new UnsupportedOperationException();
}

LOGGER.info(
"{} Quay ids for ScheduledStopPoints cached for validation report with id: {}",
scheduledStopPointAndQuayIdCache.get(validationReportId).size(),
validationReportId
);
@Override
public Map<DayTypeId, ActiveDates> activeDates(String validationReportId) {
throw new UnsupportedOperationException();
}

@Override
public void cleanUp(String validationReportId) {
scheduledStopPointAndQuayIdCache.remove(validationReportId);
serviceLinksAndFromToScheduledStopPointIdCache.remove(validationReportId);
lineInfoCache.remove(validationReportId);
redissonClient.getKeys().deleteByPattern(validationReportId + '*');
serviceJourneyStopsCache
.keySet()
.removeIf(k -> k.startsWith(validationReportId));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package no.entur.antu.netexdata;

import org.entur.netex.validation.validator.jaxb.NetexDataRepository;

/**
* This interface extends the read-only interface {@link NetexDataRepository} with methods for cleaning up the repository.
*/
public interface NetexDataRepositoryLoader extends NetexDataRepository {
/**
* Clean up the NeTEx data repository for the given validation report.
*/
void cleanUp(String validationReportId);
}
Loading

0 comments on commit c0df969

Please sign in to comment.