Example #1
0
        /// <summary>
        /// Bond-serializes a given <typeparamref name="T"/> and stores the result to the content cache.
        /// The returned content hash can be used to later deserialize the structure with <see cref="TryLoadAndDeserializeContent{T}"/>.
        /// </summary>
        public static Task <Possible <ContentHash> > TrySerializeAndStoreContent <T>(
            this IArtifactContentCache contentCache,
            T valueToSerialize,
            BoxRef <long> contentSize    = null,
            StoreArtifactOptions options = default)
        {
            return(BondExtensions.TrySerializeAndStoreContent(
                       valueToSerialize,
                       async(valueHash, valueBuffer) =>
            {
                using (var entryStream = new MemoryStream(
                           valueBuffer.Array,
                           valueBuffer.Offset,
                           valueBuffer.Count,
                           writable: false))
                {
                    Possible <Unit, Failure> maybeStored = await contentCache.TryStoreAsync(
                        entryStream,
                        contentHash: valueHash,
                        options: options);

                    return maybeStored.WithGenericFailure();
                }
            },
                       contentSize));
        }
Example #2
0
 /// <inheritdoc />
 public Task <Possible <ContentHash, Failure> > TryStoreAsync(
     FileRealizationMode fileRealizationModes,
     ExpandedAbsolutePath path,
     StoreArtifactOptions options = default)
 {
     return(TryStoreInternalAsync(
                path,
                fileRealizationModes,
                knownContentHash: null));
 }
Example #3
0
        /// <inheritdoc />
        public async Task <Possible <Unit, Failure> > TryStoreAsync(
            FileRealizationMode fileRealizationModes,
            ExpandedAbsolutePath path,
            ContentHash contentHash,
            StoreArtifactOptions options = default)
        {
            Possible <ContentHash, Failure> maybeStored = await TryStoreInternalAsync(
                path,
                fileRealizationModes,
                knownContentHash : contentHash);

            return(maybeStored.Then(hash => Unit.Void));
        }
Example #4
0
 /// <inheritdoc />
 public Task <Possible <Unit, Failure> > TryStoreAsync(Stream content, ContentHash contentHash, StoreArtifactOptions options)
 {
     return(m_innerCache.TryStoreAsync(content, contentHash, options));
 }
Example #5
0
 /// <inheritdoc />
 public Task <Possible <ContentHash, Failure> > TryStoreAsync(FileRealizationMode fileRealizationModes, ExpandedAbsolutePath path, StoreArtifactOptions options)
 {
     return(m_innerCache.TryStoreAsync(fileRealizationModes, path, options));
 }
Example #6
0
        public async Task <Possible <Unit, Failure> > TryStoreAsync(Stream content, ContentHash contentHash, StoreArtifactOptions options = default)
        {
            Possible <ContentHash, Failure> maybeStored = await TryStoreInternalAsync(content, knownContentHash : null);

            return(maybeStored.Then(hash => Unit.Void));
        }
Example #7
0
        private Task <Possible <ContentHash, Failure> > TryStoreInternalAsync(
            ExpandedAbsolutePath path,
            FileRealizationMode fileRealizationModes,
            ContentHash?knownContentHash,
            StoreArtifactOptions options = default)
        {
            return(Task.Run <Possible <ContentHash, Failure> >(
                       () =>
            {
                lock (m_lock)
                {
                    try
                    {
                        byte[] contentBytes = ExceptionUtilities.HandleRecoverableIOException(
                            () =>
                        {
                            var expandedPath = path.ExpandedPath;

                            using (FileStream fileStream = new FileStream(expandedPath, FileMode.Open, FileAccess.Read, FileShare.Read))
                                using (BinaryReader binaryReader = new BinaryReader(fileStream))
                                {
                                    // This will work with files up to 2GB in length, due to the 'int' API signature
                                    return binaryReader.ReadBytes((int)new FileInfo(expandedPath).Length);
                                }
                        },
                            ex => { throw new BuildXLException("Failed to store content (couldn't read new content from disk)", ex); });

                        ContentHash contentHash = ContentHashingUtilities.HashBytes(contentBytes);

                        if (knownContentHash.HasValue && contentHash != knownContentHash.Value)
                        {
                            return new Failure <string>(I($"Stored content had an unexpected hash. (expected: {knownContentHash.Value}; actual: {contentHash})"));
                        }

                        CacheEntry entry;
                        if (m_content.TryGetValue(contentHash, out entry))
                        {
                            // We assume that stores of content already present somewhere still cause replication
                            // to both the local and remote sites. See class remarks.
                            entry.Sites |= CacheSites.LocalAndRemote;
                            return contentHash;
                        }
                        else
                        {
                            try
                            {
                                if (m_pathRealizationModes != null)
                                {
                                    m_pathRealizationModes[path.ExpandedPath] = fileRealizationModes;
                                }

                                // We assume that stored content is instantly and magically replicated to some remote place.
                                // See class remarks.
                                m_content[contentHash] = new CacheEntry(contentBytes, CacheSites.LocalAndRemote);

                                return contentHash;
                            }
                            catch (BuildXLException ex)
                            {
                                return new RecoverableExceptionFailure(ex);
                            }
                        }
                    }
                    catch (BuildXLException ex)
                    {
                        return new RecoverableExceptionFailure(ex);
                    }
                }
            }));
        }