public async Task <AuxiliaryFileResult <DownloadData> > ReadLatestIndexedAsync(
            IAccessCondition accessCondition,
            StringCache stringCache)
        {
            var stopwatch     = Stopwatch.StartNew();
            var blobName      = GetLatestIndexedBlobName();
            var blobReference = Container.GetBlobReference(blobName);

            _logger.LogInformation("Reading the latest indexed downloads from {BlobName}.", blobName);

            bool modified;
            var  downloads = new DownloadData();
            AuxiliaryFileMetadata metadata;

            try
            {
                using (var stream = await blobReference.OpenReadAsync(accessCondition))
                {
                    ReadStream(
                        stream,
                        (id, version, downloadCount) =>
                    {
                        id      = stringCache.Dedupe(id);
                        version = stringCache.Dedupe(version);
                        downloads.SetDownloadCount(id, version, downloadCount);
                    });
                    modified = true;
                    metadata = new AuxiliaryFileMetadata(
                        lastModified: new DateTimeOffset(blobReference.LastModifiedUtc, TimeSpan.Zero),
                        loadDuration: stopwatch.Elapsed,
                        fileSize: blobReference.Properties.Length,
                        etag: blobReference.ETag);
                }
            }
            catch (StorageException ex) when(ex.RequestInformation.HttpStatusCode == (int)HttpStatusCode.NotModified)
            {
                _logger.LogInformation("The blob {BlobName} has not changed.", blobName);
                modified  = false;
                downloads = null;
                metadata  = null;
            }

            stopwatch.Stop();
            _telemetryService.TrackReadLatestIndexedDownloads(downloads?.Count, modified, stopwatch.Elapsed);

            return(new AuxiliaryFileResult <DownloadData>(
                       modified,
                       downloads,
                       metadata));
        }
Exemple #2
0
        public async Task <AuxiliaryFileResult <HashSet <string> > > ReadLatestAsync(
            IAccessCondition accessCondition,
            StringCache stringCache)
        {
            var stopwatch     = Stopwatch.StartNew();
            var blobName      = GetLatestIndexedBlobName();
            var blobReference = Container.GetBlobReference(blobName);

            _logger.LogInformation("Reading the latest verified packages from {BlobName}.", blobName);

            bool modified;
            var  data = new HashSet <string>(StringComparer.OrdinalIgnoreCase);
            AuxiliaryFileMetadata metadata;

            try
            {
                using (var stream = await blobReference.OpenReadAsync(accessCondition))
                {
                    ReadStream(stream, id => data.Add(stringCache.Dedupe(id)));
                    modified = true;
                    metadata = new AuxiliaryFileMetadata(
                        lastModified: new DateTimeOffset(blobReference.LastModifiedUtc, TimeSpan.Zero),
                        loadDuration: stopwatch.Elapsed,
                        fileSize: blobReference.Properties.Length,
                        etag: blobReference.ETag);
                }
            }
            catch (StorageException ex) when(ex.RequestInformation.HttpStatusCode == (int)HttpStatusCode.NotModified)
            {
                _logger.LogInformation("The blob {BlobName} has not changed.", blobName);
                modified = false;
                data     = null;
                metadata = null;
            }

            stopwatch.Stop();
            _telemetryService.TrackReadLatestVerifiedPackages(data?.Count, modified, stopwatch.Elapsed);

            return(new AuxiliaryFileResult <HashSet <string> >(
                       modified,
                       data,
                       metadata));
        }
        public AuxiliaryFileResult(
            bool modified,
            T data,
            AuxiliaryFileMetadata metadata)
        {
            Modified = modified;
            if (modified)
            {
                Data     = data ?? throw new ArgumentNullException(nameof(data));
                Metadata = metadata ?? throw new ArgumentNullException(nameof(metadata));
            }
            else
            {
                if (data != null)
                {
                    throw new ArgumentException("The fetched data must be null if it was not modified.", nameof(data));
                }

                if (metadata != null)
                {
                    throw new ArgumentException("The file metadata must be null if it was not modified.", nameof(data));
                }
            }
        }