protected virtual async ValueTask <AnalyzeOperationContext> GetOperationContextAsync(
     IImageSource source,
     string endpoint,
     CancellationToken cancellationToken)
 {
     cancellationToken.ThrowIfCancellationRequested();
     if (source is ISerializableImageResource ssource)
     {
         // source image is json-serializable
         if (await IsJsonSerializationSupportedAsync(endpoint, cancellationToken).ConfigureAwait(false))
         {
             // both remote server and image source support json-serialization
             // NOTE: Destination is always inline --> not used on server
             var payload  = new SourceAndDestination(ssource.Uri, null);
             var producer = StreamProducer.Create((ouput, cancellationToken) => new ValueTask(JsonSerializer.SerializeAsync(ouput, payload, _sourceAndDestinationSerializationOptions, cancellationToken)));
             return(AnalyzeOperationContext.Json(producer));
         }
         // remote server does not support json-serialization
         if (Configuration.AllowInlineData)
         {
             // remote server does not support json-serialized images but the inline data is enabled --> proceed
             Logger.LogWarning("Source image supports json serialization but remote server does not thus inline data will be used.");
             return(AnalyzeOperationContext.Inline(source.CreateProducer()));
         }
         // remote server does not support json-serialized images and the inline data is disabled --> throw exception
         throw new InvalidOperationException("Source image supports json serialization but remote server does not. Either enable inline data or use compatible server.");
     }
     // source image is not json-serializable
     if (!Configuration.AllowInlineData)
     {
         throw new InvalidOperationException("Source image does not support json serialization. Either enable inline data or use json-serializable image source.");
     }
     return(AnalyzeOperationContext.Inline(source.CreateProducer()));
 }
 protected virtual async ValueTask <ResizeOperationContext> GetOperationContextAsync(
     IImageSource source,
     IImageDestination destination,
     string endpoint,
     CancellationToken cancellationToken)
 {
     cancellationToken.ThrowIfCancellationRequested();
     if (source is ISerializableImageResource ssource)
     {
         // source image is json-serializable
         if (await IsJsonSerializationSupportedAsync(endpoint, cancellationToken).ConfigureAwait(false))
         {
             // both remote server and image source support json-serialization
             SourceAndDestination payload;
             IImageDestination?   dest;
             if (destination is ISerializableImageResource sdestination)
             {
                 // image destination does also support json-serialization --> destination in payload, no consumer.
                 payload = new SourceAndDestination(ssource.Uri, sdestination.Uri);
                 dest    = default;
             }
             else
             {
                 // image destination does also support json-serialization --> null in payload, destination is the consumer.
                 payload = new SourceAndDestination(ssource.Uri, default);
                 dest    = destination;
             }
             var producer = StreamProducer.Create((ouput, cancellationToken) => new ValueTask(JsonSerializer.SerializeAsync(ouput, payload, _sourceAndDestinationSerializationOptions, cancellationToken)));
             return(ResizeOperationContext.Json(producer, dest));
         }
         // remote server does not support json-serialization
         if (Configuration.AllowInlineData)
         {
             // remote server does not support json-serialized images but the inline data is enabled --> proceed
             Logger.LogWarning("Source image supports json serialization but remote server does not thus inline data will be used.");
             return(ResizeOperationContext.Inline(source.CreateProducer(), destination));
         }
         // remote server does not support json-serialized images and the inline data is disabled --> throw exception
         throw new InvalidOperationException("Source image supports json serialization but remote server does not. Either enable inline data or use compatible server.");
     }
     // source image is not json-serializable
     if (!Configuration.AllowInlineData)
     {
         throw new InvalidOperationException("Source image does not support json serialization. Either enable inline data or use json-serializable image source.");
     }
     if (destination is ISerializableImageResource)
     {
         if (Configuration.AllowInlineData)
         {
             Logger.LogWarning("Json serializable destination is ignored as source is not json serializable.");
         }
         else
         {
             throw new InvalidOperationException("Destination cannot be json serialized when source is not json serializable.");
         }
     }
     return(ResizeOperationContext.Inline(source.CreateProducer(), destination));
 }
 public IStreamProducer CreateProducer()
 => StreamProducer.Create((output, cancellationToken) =>
 {
     if (_stream.CanSeek)
     {
         _stream.Seek(0, SeekOrigin.Begin);
     }
     return(new ValueTask(_stream.CopyToAsync(output, 16 * 1024, cancellationToken)));
 });
 public IStreamProducer CreateProducer()
 => StreamProducer.FromStream(new FileStream(
                                  AbsolutePath,
                                  FileMode.Open,
                                  FileAccess.Read,
                                  FileShare.Read,
                                  BufferSize ?? DefaultBufferSize,
                                  true
                                  ), BufferSize ?? DefaultBufferSize);
        public void Should_Merge_BatchAndSpeedLayerViews()
        {
            // arrange
            var eventId = 1;
            var meetupLondonMessages =
                new []
            {
                CreateMeetupMessageFromTemplate(eventId, new DateTime(2016, 01, 01, 08, 00, 10), "UK", "London"),
                CreateMeetupMessageFromTemplate(eventId, new DateTime(2016, 01, 01, 08, 00, 20), "UK", "London"),
                CreateMeetupMessageFromTemplate(eventId, new DateTime(2016, 01, 01, 08, 00, 50), "UK", "London"),
                CreateMeetupMessageFromTemplate(eventId, new DateTime(2016, 01, 01, 08, 01, 40), "UK", "London")
            };
            StreamProducer producer = new StreamProducer(DeploymentConfiguration.Default.ServiceBus);

            producer.Produce(meetupLondonMessages);

            // stream layer processing
            var streamLayerConsumer = new SpeedLayer.MeetupMessageConsumer(
                new MeetupStreamAnalytics(DeploymentConfiguration.Default.Redis),
                DeploymentConfiguration.Default.ServiceBus);

            while (streamLayerConsumer.ProcessedMessagesCount != meetupLondonMessages.Length)
            {
            }

            // batch layer processing
            var batchLayerConsumer = new BatchLayer.MeetupMessageConsumer(
                new MeetupRepository(DeploymentConfiguration.Default.Storage),
                DeploymentConfiguration.Default.ServiceBus);

            while (batchLayerConsumer.ProcessedMessagesCount != meetupLondonMessages.Length)
            {
            }

            // batch view recalculation
            RecalculationJob job = new RecalculationJob(
                DeploymentConfiguration.Default.Storage,
                DeploymentConfiguration.Default.HDInsight);

            job.Execute(new DateTime(2016, 01, 01, 08, 01, 00));

            // act
            long count = new MeetupQueries().GetAllMeetupVisitors(eventId, "UK", "London");

            // assert
            Assert.Equal(meetupLondonMessages.Length, count);
        }
 public IStreamProducer CreateProducer()
 => StreamProducer.Create((stream, cancellationToken) => SerializeAsync(_data, stream, cancellationToken));