Esempio n. 1
0
        //==============================================================================

        /// <summary>
        /// Returns a list of nodes (a subset of 'candidates') that can be safely removed in a way
        /// that no other remaining node depends (directly or indirectly) on removed nodes.
        /// </summary>
        /// <param name="candidates">Nodes to be removed.</param>
        /// <param name="dependencies">Dependency: Item2 depends on Item1.</param>
        public static List <T> RemovableLeaves <T>(IEnumerable <T> candidates, IEnumerable <Tuple <T, T> > dependencies)
        {
            CsUtility.Materialize(ref candidates);
            CsUtility.Materialize(ref dependencies);

            dependencies = dependencies.Distinct().ToArray();
            var all = candidates.Union(dependencies.Select(d => d.Item1)).Union(dependencies.Select(d => d.Item2)).ToArray();

            var numberOfDependents = all.ToDictionary(node => node, node => 0);

            foreach (var relation in dependencies)
            {
                numberOfDependents[relation.Item1]++;
            }

            var dependsOn = all.ToDictionary(node => node, node => new List <T>());

            foreach (var relation in dependencies)
            {
                dependsOn[relation.Item2].Add(relation.Item1);
            }

            var removed         = new HashSet <T>();
            var candidatesIndex = new HashSet <T>(candidates);

            foreach (var cand in candidates)
            {
                if (numberOfDependents[cand] == 0 && !removed.Contains(cand))
                {
                    RemoveLeaf(cand, removed, numberOfDependents, dependsOn, candidatesIndex);
                }
            }
            return(removed.ToList());
        }
Esempio n. 2
0
        /// <summary>
        /// Copies the files from cache only if all of the extensions are found in the cache,
        /// and if the sourceContent matches the corresponding sourceFile in the cache.
        /// </summary>
        /// <param name="sampleSourceFile">Any file from the cached file group, extension will be ignored.</param>
        /// <returns>List of the restored files, if the files are copied from the cache, null otherwise.</returns>
        public List <string> RestoreCachedFiles(string sampleSourceFile, byte[] sourceHash, string targetFolder, IEnumerable <string> copyExtensions)
        {
            CsUtility.Materialize(ref copyExtensions);
            var cachedFiles = ListCachedFiles(Path.GetFileNameWithoutExtension(sampleSourceFile), sourceHash, copyExtensions);

            List <string> targetFiles;
            string        report;

            if (!cachedFiles.IsError)
            {
                targetFiles = cachedFiles.Value.Select(source =>
                                                       _filesUtility.SafeCopyFileToFolder(source, targetFolder)).ToList();
                report = "Restored " + string.Join(", ", copyExtensions) + ".";
            }
            else
            {
                targetFiles = null;
                report      = cachedFiles.Error;
            }

            _logger.Trace(() => "RestoreCachedFiles for " + Path.GetFileName(sampleSourceFile) + ": " + report);
            return(targetFiles);
        }