Search in sources :

Example 1 with RecordRedirectDao

use of eu.europeana.metis.mongo.dao.RecordRedirectDao in project metis-framework by europeana.

the class RecordRedirectsUtil method introduceRedirection.

/**
 * Introduce a new redirect X -> Y. If X equals Y we do nothing. Otherwise, we do the following:
 * <ol>
 * <li>
 * We delete all mappings Y -> ?. We can do this because we are indexing record Y and it should
 * therefore not be redirected to any other link.
 * </li>
 * <li>
 * We delete all mappings X -> ? except X -> Y. We can do this because X will be redirected to
 * record Y only, and it should therefore not be redirected to any other record.
 * </li>
 * <li>
 * Introduce new redirect from X -> Y if it doesn't already exist. This new redirect will get the
 * record redirect date (passed to this method).
 * </li>
 * <li>
 * We update all existing redirects ? -> X to point to Y instead (so becoming ? -> Y).
 * </li>
 * </ol>
 * Note that after this method is called (and if X does not equal Y), X should only occur as
 * source of a redirect, and then only as part of redirect X -> Y, whereas Y should occur only as
 * destination of a redirect. Neither of them can therefore be part of a redirection cycle.
 *
 * @param recordRedirectDao The DAO object to manage redirects.
 * @param newIdentifier The new identifier (value Y).
 * @param oldIdentifier The old identifier (value X).
 * @param recordRedirectDate The date (timestamp) for any new redirects.
 */
private static void introduceRedirection(RecordRedirectDao recordRedirectDao, String newIdentifier, String oldIdentifier, Date recordRedirectDate) {
    // Sanity check: if old and new identifier are equal, do nothing.
    if (oldIdentifier.equals(newIdentifier)) {
        LOGGER.info("Encountered the request to create mappping from {} to itself. This will be ignored.", oldIdentifier);
        return;
    }
    // Remove any redirects Y -> ?.
    recordRedirectDao.getRecordRedirectsByOldId(newIdentifier).forEach(recordRedirectDao::delete);
    // Remove any redirects X -> ? except X -> Y.
    final List<RecordRedirect> existingRedirectsFromOldIdentifier = recordRedirectDao.getRecordRedirectsByOldId(oldIdentifier);
    existingRedirectsFromOldIdentifier.stream().filter(redirect -> !redirect.getNewId().equals(newIdentifier)).forEach(recordRedirectDao::delete);
    // Create the new redirect X -> Y if one doesn't already exist.
    final boolean mappingAlreadyExists = existingRedirectsFromOldIdentifier.stream().map(RecordRedirect::getNewId).anyMatch(newIdentifier::equals);
    if (!mappingAlreadyExists) {
        recordRedirectDao.createUpdate(new RecordRedirect(newIdentifier, oldIdentifier, recordRedirectDate));
    }
    // Update the redirects ? -> X to point to Y instead, becoming ? -> Y.
    recordRedirectDao.getRecordRedirectsByNewId(oldIdentifier).forEach(redirect -> {
        redirect.setNewId(newIdentifier);
        recordRedirectDao.createUpdate(redirect);
    });
}
Also used : Arrays(java.util.Arrays) RdfWrapper(eu.europeana.indexing.utils.RdfWrapper) Date(java.util.Date) SolrDocumentList(org.apache.solr.common.SolrDocumentList) RecordRedirect(eu.europeana.metis.mongo.model.RecordRedirect) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) UnaryOperator(java.util.function.UnaryOperator) StringUtils(org.apache.commons.lang3.StringUtils) RecordRedirectDao(eu.europeana.metis.mongo.dao.RecordRedirectDao) ArrayList(java.util.ArrayList) Pair(org.apache.commons.lang3.tuple.Pair) Map(java.util.Map) Collector(java.util.stream.Collector) Identifier(eu.europeana.metis.schema.jibx.Identifier) IndexingException(eu.europeana.indexing.exception.IndexingException) Title(eu.europeana.metis.schema.jibx.Title) Logger(org.slf4j.Logger) Predicate(java.util.function.Predicate) Collection(java.util.Collection) EdmLabel(eu.europeana.indexing.solr.EdmLabel) Collectors(java.util.stream.Collectors) ImmutablePair(org.apache.commons.lang3.tuple.ImmutablePair) Objects(java.util.Objects) SolrDocument(org.apache.solr.common.SolrDocument) List(java.util.List) Stream(java.util.stream.Stream) CollectionUtils(org.springframework.util.CollectionUtils) Entry(java.util.Map.Entry) Optional(java.util.Optional) IsShownBy(eu.europeana.metis.schema.jibx.IsShownBy) ClientUtils(org.apache.solr.client.solrj.util.ClientUtils) Collections(java.util.Collections) RecordRedirect(eu.europeana.metis.mongo.model.RecordRedirect)

Example 2 with RecordRedirectDao

use of eu.europeana.metis.mongo.dao.RecordRedirectDao in project metis-framework by europeana.

the class RecordRedirectDaoTest method setup.

@BeforeAll
static void setup() {
    embeddedLocalhostMongo = new EmbeddedLocalhostMongo();
    embeddedLocalhostMongo.start();
    final String mongoHost = embeddedLocalhostMongo.getMongoHost();
    final int mongoPort = embeddedLocalhostMongo.getMongoPort();
    final MongoClient mongoClient = MongoClients.create(String.format("mongodb://%s:%s", mongoHost, mongoPort));
    recordRedirectDao = new RecordRedirectDao(mongoClient, DATABASE_NAME);
}
Also used : MongoClient(com.mongodb.client.MongoClient) EmbeddedLocalhostMongo(eu.europeana.metis.mongo.embedded.EmbeddedLocalhostMongo) RecordRedirectDao(eu.europeana.metis.mongo.dao.RecordRedirectDao) BeforeAll(org.junit.jupiter.api.BeforeAll)

Aggregations

RecordRedirectDao (eu.europeana.metis.mongo.dao.RecordRedirectDao)2 MongoClient (com.mongodb.client.MongoClient)1 IndexingException (eu.europeana.indexing.exception.IndexingException)1 EdmLabel (eu.europeana.indexing.solr.EdmLabel)1 RdfWrapper (eu.europeana.indexing.utils.RdfWrapper)1 EmbeddedLocalhostMongo (eu.europeana.metis.mongo.embedded.EmbeddedLocalhostMongo)1 RecordRedirect (eu.europeana.metis.mongo.model.RecordRedirect)1 Identifier (eu.europeana.metis.schema.jibx.Identifier)1 IsShownBy (eu.europeana.metis.schema.jibx.IsShownBy)1 Title (eu.europeana.metis.schema.jibx.Title)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Entry (java.util.Map.Entry)1 Objects (java.util.Objects)1