Search in sources :

Example 1 with ContainerRef

use of org.atlasapi.content.ContainerRef in project atlas-deer by atlasapi.

the class HierarchyWriter method writeEpisodeHierarchy.

private void writeEpisodeHierarchy(Episode episode, StatementRunner runner) {
    EpisodeRef episodeRef = episode.toRef();
    ContainerRef containerRef = episode.getContainerRef();
    if (containerRef != null) {
        addParent(episodeRef, containerRef, runner);
    }
    SeriesRef seriesRef = episode.getSeriesRef();
    if (seriesRef != null) {
        addParent(episodeRef, seriesRef, runner);
    }
}
Also used : ContainerRef(org.atlasapi.content.ContainerRef) SeriesRef(org.atlasapi.content.SeriesRef) EpisodeRef(org.atlasapi.content.EpisodeRef)

Example 2 with ContainerRef

use of org.atlasapi.content.ContainerRef in project atlas-deer by atlasapi.

the class CqlContentStore method removeItemRefsFromContainers.

private Collection<? extends Statement> removeItemRefsFromContainers(Item item, ContainerRef... containerRefs) {
    ItemRef ref = item.toRef();
    ImmutableMap<ItemRef, Iterable<BroadcastRef>> upcoming = ImmutableMap.of(item.toRef(), item.getUpcomingBroadcastRefs());
    Set<Ref> upcomingSerialised = upcoming.keySet().stream().map(refTranslator::serialize).collect(MoreCollectors.toImmutableSet());
    ImmutableMap<ItemRef, Iterable<LocationSummary>> availableLocations = ImmutableMap.of(item.toRef(), item.getAvailableLocations());
    Set<Ref> availableLocationsSerialised = availableLocations.keySet().stream().map(refTranslator::serialize).collect(MoreCollectors.toImmutableSet());
    Ref itemRefKey = refTranslator.serialize(ref);
    return Arrays.stream(containerRefs).filter(Objects::nonNull).flatMap(containerRef -> Lists.newArrayList(accessor.removeItemRefsFromContainer(containerRef.getId().longValue(), ImmutableSet.of(itemRefKey), upcomingSerialised, availableLocationsSerialised), accessor.removeItemSummariesFromContainer(containerRef.getId().longValue(), ImmutableSet.of(itemRefKey))).stream()).collect(Collectors.toList());
}
Also used : org.atlasapi.content(org.atlasapi.content) Broadcast(org.atlasapi.content.Broadcast) MissingResourceException(org.atlasapi.entity.util.MissingResourceException) LoggerFactory(org.slf4j.LoggerFactory) Resolved(org.atlasapi.entity.util.Resolved) MappingManager(com.datastax.driver.mapping.MappingManager) ConsistencyLevel(com.datastax.driver.core.ConsistencyLevel) Optional(com.google.common.base.Optional) Session(com.datastax.driver.core.Session) ContainerRef(org.atlasapi.content.ContainerRef) com.google.common.collect(com.google.common.collect) IdGenerator(com.metabroadcast.common.ids.IdGenerator) BatchStatement(com.datastax.driver.core.BatchStatement) Longs(com.google.common.primitives.Longs) Function(com.google.common.base.Function) OptionalMap(com.metabroadcast.common.collect.OptionalMap) Collectors(java.util.stream.Collectors) Promise(com.metabroadcast.promise.Promise) MoreCollectors(com.metabroadcast.common.stream.MoreCollectors) Statement(com.datastax.driver.core.Statement) ContainerSummary(org.atlasapi.content.ContainerSummary) java.util(java.util) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) Clock(com.metabroadcast.common.time.Clock) Description(org.atlasapi.content.v2.model.udt.Description) ImmutableOptionalMap(com.metabroadcast.common.collect.ImmutableOptionalMap) Timestamp(com.metabroadcast.common.time.Timestamp) LocationSummary(org.atlasapi.content.LocationSummary) EquivalenceGraph(org.atlasapi.equivalence.EquivalenceGraph) MapStream(com.codepoetics.protonpack.maps.MapStream) StreamSupport(java.util.stream.StreamSupport) BroadcastRef(org.atlasapi.content.BroadcastRef) SeriesRef(org.atlasapi.content.SeriesRef) org.atlasapi.content.v2.model.udt(org.atlasapi.content.v2.model.udt) WriteResult(org.atlasapi.entity.util.WriteResult) org.atlasapi.content.v2.serialization(org.atlasapi.content.v2.serialization) Nullable(javax.annotation.Nullable) MetricRegistry(com.codahale.metrics.MetricRegistry) Logger(org.slf4j.Logger) DateTime(org.joda.time.DateTime) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) Throwables(com.google.common.base.Throwables) Id(org.atlasapi.entity.Id) ResourceUpdatedMessage(org.atlasapi.messaging.ResourceUpdatedMessage) ItemSummary(org.atlasapi.content.ItemSummary) ExecutionException(java.util.concurrent.ExecutionException) Futures(com.google.common.util.concurrent.Futures) EquivalenceGraphStore(org.atlasapi.equivalence.EquivalenceGraphStore) Mapper(com.datastax.driver.mapping.Mapper) MessageSender(com.metabroadcast.common.queue.MessageSender) Instant(org.joda.time.Instant) WriteException(org.atlasapi.entity.util.WriteException) ContainerRef(org.atlasapi.content.ContainerRef) BroadcastRef(org.atlasapi.content.BroadcastRef) SeriesRef(org.atlasapi.content.SeriesRef)

Example 3 with ContainerRef

use of org.atlasapi.content.ContainerRef in project atlas-deer by atlasapi.

the class CqlContentStore method resolveContainer.

@Nullable
private Container resolveContainer(Content content) throws WriteException {
    ContainerRef containerRef = null;
    if (content instanceof Item) {
        Item item = (Item) content;
        containerRef = item.getContainerRef();
    }
    if (content instanceof Series) {
        Series series = (Series) content;
        containerRef = series.getBrandRef();
    }
    if (containerRef != null) {
        try {
            Optional<Content> container = resolveIds(ImmutableList.of(containerRef.getId())).get().getResources().first();
            if (container.isPresent()) {
                return (Container) container.get();
            } else {
                throw new WriteException(String.format("Failed to resolve container %s for %s", containerRef.getId(), content.getId()), new MissingResourceException(containerRef.getId()));
            }
        } catch (InterruptedException | ExecutionException e) {
            throw new WriteException(String.format("Failed to retrieve container %s for %s seconds", containerRef.getId(), content.getId()), e);
        }
    }
    return null;
}
Also used : ContainerRef(org.atlasapi.content.ContainerRef) WriteException(org.atlasapi.entity.util.WriteException) MissingResourceException(org.atlasapi.entity.util.MissingResourceException) ExecutionException(java.util.concurrent.ExecutionException) Nullable(javax.annotation.Nullable)

Example 4 with ContainerRef

use of org.atlasapi.content.ContainerRef in project atlas-deer by atlasapi.

the class CqlContentStore method updateWriteDates.

private List<Statement> updateWriteDates(Content content, DateTime now) {
    content.setLastUpdated(now);
    if (content.getFirstSeen() == null) {
        content.setFirstSeen(now);
    }
    content.setThisOrChildLastUpdated(now);
    List<Statement> result = Lists.newArrayList();
    if (content instanceof Item) {
        Item item = (Item) content;
        ContainerRef container = item.getContainerRef();
        if (container != null) {
            result.add(accessor.setLastUpdated(container.getId().longValue(), now.toInstant()));
        }
    }
    if (content instanceof Episode) {
        Episode episode = (Episode) content;
        SeriesRef series = episode.getSeriesRef();
        if (series != null) {
            result.add(accessor.setLastUpdated(series.getId().longValue(), now.toInstant()));
        }
    }
    if (content instanceof Series) {
        Series series = (Series) content;
        BrandRef brand = series.getBrandRef();
        if (brand != null) {
            result.add(accessor.setLastUpdated(brand.getId().longValue(), now.toInstant()));
        }
    }
    return result;
}
Also used : ContainerRef(org.atlasapi.content.ContainerRef) SeriesRef(org.atlasapi.content.SeriesRef) BatchStatement(com.datastax.driver.core.BatchStatement) Statement(com.datastax.driver.core.Statement)

Example 5 with ContainerRef

use of org.atlasapi.content.ContainerRef in project atlas-deer by atlasapi.

the class CqlContentStore method writeBroadcast.

@Override
public void writeBroadcast(ItemRef item, Optional<ContainerRef> containerRef, Optional<SeriesRef> seriesRef, Broadcast broadcast) {
    metricRegistry.meter(writeBroadcast + METER_CALLED).mark();
    try {
        BatchStatement batch = new BatchStatement();
        Instant now = clock.now().toInstant();
        org.atlasapi.content.v2.model.udt.Broadcast serialized = broadcastTranslator.serialize(broadcast);
        ImmutableMap<String, org.atlasapi.content.v2.model.udt.Broadcast> broadcasts = ImmutableMap.of(broadcast.getSourceId(), serialized);
        batch.add(accessor.addBroadcastToContent(item.getId().longValue(), broadcasts));
        batch.add(accessor.setLastUpdated(item.getId().longValue(), now));
        // item and return early
        if (broadcast.isActivelyPublished() && broadcast.isUpcoming()) {
            Ref ref = refTranslator.serialize(item);
            PartialItemRef itemRef = itemRefTranslator.serialize(item);
            Map<Ref, ItemRefAndBroadcastRefs> upcomingBroadcasts = ImmutableMap.of(ref, new ItemRefAndBroadcastRefs(itemRef, ImmutableList.of(broadcastRefTranslator.serialize(broadcast.toRef()))));
            if (containerRef.isPresent()) {
                long id = containerRef.get().getId().longValue();
                batch.add(accessor.addItemRefsToContainer(id, ImmutableMap.of(), upcomingBroadcasts, ImmutableMap.of()));
                batch.add(accessor.setLastUpdated(id, now));
            }
            if (seriesRef.isPresent()) {
                long id = seriesRef.get().getId().longValue();
                batch.add(accessor.addItemRefsToContainer(id, ImmutableMap.of(ref, itemRef), upcomingBroadcasts, ImmutableMap.of()));
                batch.add(accessor.setLastUpdated(id, now));
            }
        }
        session.execute(batch);
        sendMessages(ImmutableList.of(new ResourceUpdatedMessage(UUID.randomUUID().toString(), Timestamp.now(), item)));
    } catch (RuntimeException e) {
        metricRegistry.meter(writeBroadcast + METER_FAILURE).mark();
        Throwables.propagate(e);
    }
}
Also used : org.atlasapi.content.v2.model.udt(org.atlasapi.content.v2.model.udt) Instant(org.joda.time.Instant) ResourceUpdatedMessage(org.atlasapi.messaging.ResourceUpdatedMessage) org.atlasapi.content(org.atlasapi.content) ContainerRef(org.atlasapi.content.ContainerRef) BroadcastRef(org.atlasapi.content.BroadcastRef) SeriesRef(org.atlasapi.content.SeriesRef) Broadcast(org.atlasapi.content.Broadcast) BatchStatement(com.datastax.driver.core.BatchStatement)

Aggregations

ContainerRef (org.atlasapi.content.ContainerRef)11 SeriesRef (org.atlasapi.content.SeriesRef)7 BatchStatement (com.datastax.driver.core.BatchStatement)5 Statement (com.datastax.driver.core.Statement)4 ResourceUpdatedMessage (org.atlasapi.messaging.ResourceUpdatedMessage)4 ExecutionException (java.util.concurrent.ExecutionException)3 Nullable (javax.annotation.Nullable)3 org.atlasapi.content (org.atlasapi.content)3 Broadcast (org.atlasapi.content.Broadcast)3 BroadcastRef (org.atlasapi.content.BroadcastRef)3 ContainerSummary (org.atlasapi.content.ContainerSummary)3 MetricRegistry (com.codahale.metrics.MetricRegistry)2 MapStream (com.codepoetics.protonpack.maps.MapStream)2 ConsistencyLevel (com.datastax.driver.core.ConsistencyLevel)2 Session (com.datastax.driver.core.Session)2 Mapper (com.datastax.driver.mapping.Mapper)2 MappingManager (com.datastax.driver.mapping.MappingManager)2 Function (com.google.common.base.Function)2 Optional (com.google.common.base.Optional)2 Preconditions.checkNotNull (com.google.common.base.Preconditions.checkNotNull)2