Search in sources :

Example 6 with BulkAtomEvent

use of won.matcher.service.common.event.BulkAtomEvent in project webofneeds by researchstudio-sat.

the class RematchSparqlService method registerMatchingAttempts.

/**
 * Bulk update of several meta data messages about the crawling process using a
 * separate graph.
 *
 * @param msg multiple messages that describe crawling meta data to update
 */
public void registerMatchingAttempts(BulkAtomEvent msg) {
    final AtomicInteger counter = new AtomicInteger();
    StringBuilder builder = new StringBuilder();
    msg.getAtomEvents().stream().map(e -> createMatchAttemptUpdate(e)).filter(o -> o.isPresent()).map(o -> o.get()).map(updateString -> parseUpdateQuery(updateString)).filter(o -> o.isPresent()).map(o -> o.get()).collect(Collectors.groupingBy(x -> counter.getAndIncrement() % MAX_UPDATES_PER_REQUEST)).values().stream().forEach(requests -> {
        requests.stream().reduce((UpdateRequest left, UpdateRequest right) -> {
            right.getOperations().stream().forEach(left::add);
            return left;
        }).ifPresent(request -> executeUpdate(request));
    });
}
Also used : LinkedDataSource(won.protocol.util.linkeddata.LinkedDataSource) LoggerFactory(org.slf4j.LoggerFactory) BulkAtomEvent(won.matcher.service.common.event.BulkAtomEvent) Autowired(org.springframework.beans.factory.annotation.Autowired) HashSet(java.util.HashSet) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) URI(java.net.URI) Logger(org.slf4j.Logger) org.apache.jena.query(org.apache.jena.query) StringWriter(java.io.StringWriter) MethodHandles(java.lang.invoke.MethodHandles) Cause(won.matcher.service.common.event.Cause) Set(java.util.Set) LinkedDataFetchingException(won.protocol.rest.LinkedDataFetchingException) Collectors(java.util.stream.Collectors) AtomEvent(won.matcher.service.common.event.AtomEvent) HttpStatus(org.springframework.http.HttpStatus) AtomModelWrapper(won.protocol.util.AtomModelWrapper) Component(org.springframework.stereotype.Component) SparqlService(won.matcher.service.common.service.sparql.SparqlService) RDFDataMgr(org.apache.jena.riot.RDFDataMgr) CrawlConfig(won.matcher.service.crawler.config.CrawlConfig) Optional(java.util.Optional) RDFFormat(org.apache.jena.riot.RDFFormat) TYPE(won.matcher.service.common.event.AtomEvent.TYPE) UpdateRequest(org.apache.jena.update.UpdateRequest) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) UpdateRequest(org.apache.jena.update.UpdateRequest)

Example 7 with BulkAtomEvent

use of won.matcher.service.common.event.BulkAtomEvent in project webofneeds by researchstudio-sat.

the class MatcherPubSubActor method onReceive.

@Override
public void onReceive(Object o) throws Exception {
    if (o.equals(TICK)) {
        if (!atomsUpdateRequestReceived) {
            // request missing atom events from matching service while this matcher was not
            // available
            long lastSeenAtomDate = Long.valueOf(appStateProps.getProperty(LAST_SEEN_ATOM_DATE_PROPERTY_NAME));
            LoadAtomEvent loadAtomEvent;
            if (lastSeenAtomDate == -1) {
                // request the last one atom event from matching service and accept every atom
                // event timestamp
                loadAtomEvent = new LoadAtomEvent(1);
            } else {
                // request atom events with date > last atom event date
                log.info("request missed atoms from matching service with crawl date > {}", lastSeenAtomDate);
                loadAtomEvent = new LoadAtomEvent(lastSeenAtomDate, Long.MAX_VALUE);
            }
            pubSubMediator.tell(new DistributedPubSubMediator.Publish(loadAtomEvent.getClass().getName(), loadAtomEvent), getSelf());
        }
    } else if (o instanceof AtomEvent) {
        AtomEvent atomEvent = (AtomEvent) o;
        log.info("AtomEvent received: " + atomEvent);
        // matching service
        if (atomsUpdateRequestReceived) {
            long lastSeenAtomDate = Long.valueOf(appStateProps.getProperty(LAST_SEEN_ATOM_DATE_PROPERTY_NAME));
            if (atomEvent.getCrawlDate() > lastSeenAtomDate) {
                appStateProps.setProperty(LAST_SEEN_ATOM_DATE_PROPERTY_NAME, String.valueOf(atomEvent.getCrawlDate()));
                saveLastSeenAtomDate();
            }
        }
        matcherActor.tell(atomEvent, getSelf());
    } else if (o instanceof BulkAtomEvent) {
        // receiving a bulk atom event means this is the answer for the request of atom
        // updates
        // there could arrive several of these bulk events
        atomsUpdateRequestReceived = true;
        BulkAtomEvent bulkAtomEvent = (BulkAtomEvent) o;
        log.info("BulkAtomEvent received with {} atom events", bulkAtomEvent.getAtomEvents().size());
        for (AtomEvent atomEvent : ((BulkAtomEvent) o).getAtomEvents()) {
            long lastSeenAtomDate = Long.valueOf(appStateProps.getProperty(LAST_SEEN_ATOM_DATE_PROPERTY_NAME));
            if (atomEvent.getCrawlDate() > lastSeenAtomDate) {
                appStateProps.setProperty(LAST_SEEN_ATOM_DATE_PROPERTY_NAME, String.valueOf(atomEvent.getCrawlDate()));
                saveLastSeenAtomDate();
            }
            matcherActor.tell(atomEvent, getSelf());
        }
    } else if (o instanceof HintEvent) {
        HintEvent hintEvent = (HintEvent) o;
        log.info("Publish hint event: " + hintEvent);
        pubSubMediator.tell(new DistributedPubSubMediator.Publish(hintEvent.getClass().getName(), hintEvent), getSelf());
    } else if (o instanceof BulkHintEvent) {
        BulkHintEvent bulkHintEvent = (BulkHintEvent) o;
        log.info("Publish bulk hint event: " + bulkHintEvent);
        pubSubMediator.tell(new DistributedPubSubMediator.Publish(bulkHintEvent.getClass().getName(), bulkHintEvent), getSelf());
    } else {
        unhandled(o);
    }
}
Also used : BulkAtomEvent(won.matcher.service.common.event.BulkAtomEvent) BulkHintEvent(won.matcher.service.common.event.BulkHintEvent) DistributedPubSubMediator(akka.cluster.pubsub.DistributedPubSubMediator) BulkAtomEvent(won.matcher.service.common.event.BulkAtomEvent) LoadAtomEvent(won.matcher.service.common.event.LoadAtomEvent) AtomEvent(won.matcher.service.common.event.AtomEvent) LoadAtomEvent(won.matcher.service.common.event.LoadAtomEvent) HintEvent(won.matcher.service.common.event.HintEvent) BulkHintEvent(won.matcher.service.common.event.BulkHintEvent)

Aggregations

BulkAtomEvent (won.matcher.service.common.event.BulkAtomEvent)7 AtomEvent (won.matcher.service.common.event.AtomEvent)6 StringWriter (java.io.StringWriter)3 LoadAtomEvent (won.matcher.service.common.event.LoadAtomEvent)3 DistributedPubSubMediator (akka.cluster.pubsub.DistributedPubSubMediator)2 HashSet (java.util.HashSet)2 HttpStatus (org.springframework.http.HttpStatus)2 BulkHintEvent (won.matcher.service.common.event.BulkHintEvent)2 HintEvent (won.matcher.service.common.event.HintEvent)2 LinkedDataFetchingException (won.protocol.rest.LinkedDataFetchingException)2 IOException (java.io.IOException)1 MethodHandles (java.lang.invoke.MethodHandles)1 URI (java.net.URI)1 Optional (java.util.Optional)1 Set (java.util.Set)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Collectors (java.util.stream.Collectors)1 org.apache.jena.query (org.apache.jena.query)1 RDFDataMgr (org.apache.jena.riot.RDFDataMgr)1 RDFFormat (org.apache.jena.riot.RDFFormat)1