public async Task <List <T> > CreateCollectionAsync <T>(ChecksumCollection checksums, CancellationToken cancellationToken)
        {
            var assets = new List <T>();

            foreach (var checksum in checksums)
            {
                cancellationToken.ThrowIfCancellationRequested();

                var asset = await GetAssetAsync <T>(checksum, cancellationToken).ConfigureAwait(false);

                assets.Add(asset);
            }

            return(assets);
        }
Exemple #2
0
        private async Task <List <T> > CreateCollectionAsync <T>(ChecksumCollection collections)
        {
            var assets = new List <T>();

            foreach (var checksum in collections)
            {
                _cancellationToken.ThrowIfCancellationRequested();

                var asset = await _assetService.GetAssetAsync <T>(checksum, _cancellationToken).ConfigureAwait(false);

                assets.Add(asset);
            }

            return(assets);
        }
Exemple #3
0
        private async Task <Project> UpdateDocumentsAsync(Project project, ChecksumCollection oldChecksums, ChecksumCollection newChecksums, bool additionalText)
        {
            using (var olds = SharedPools.Default <HashSet <Checksum> >().GetPooledObject())
                using (var news = SharedPools.Default <HashSet <Checksum> >().GetPooledObject())
                {
                    olds.Object.UnionWith(oldChecksums);
                    news.Object.UnionWith(newChecksums);

                    // remove projects that exist in both side
                    olds.Object.ExceptWith(newChecksums);
                    news.Object.ExceptWith(oldChecksums);

                    return(await UpdateDocumentsAsync(project, olds.Object, news.Object, additionalText).ConfigureAwait(false));
                }
        }
Exemple #4
0
        private async Task <Solution> UpdateProjectsAsync(Solution solution, ChecksumCollection oldChecksums, ChecksumCollection newChecksums)
        {
            using (var olds = SharedPools.Default <HashSet <Checksum> >().GetPooledObject())
                using (var news = SharedPools.Default <HashSet <Checksum> >().GetPooledObject())
                {
                    olds.Object.UnionWith(oldChecksums);
                    news.Object.UnionWith(newChecksums);

                    // remove projects that exist in both side
                    olds.Object.ExceptWith(newChecksums);
                    news.Object.ExceptWith(oldChecksums);

                    return(await UpdateProjectsAsync(solution, olds.Object, news.Object).ConfigureAwait(false));
                }
        }
Exemple #5
0
        private static void HashTree(string root, ChecksumCollection checksumCollection, Stats stats)
        {
            // List all files
            IEnumerable <string> files = null;

            try
            {
                files = Directory.EnumerateFiles(root);
            }
            catch (UnauthorizedAccessException)
            {
                Console.WriteLine("Can't enumerate files in: " + root);
                stats.Unauthorized.Add("Can't enumerate files in: " + root);
            }

            if (files is { }) //not-null test using property matching.  I really just want "is not null" to be a thing.
Exemple #6
0
 /// <summary>
 /// Create an empty puzzle with a preset checksum
 /// </summary>
 /// <param name="horizontal">Horizontal checksum</param>
 /// <param name="vertical">Vertical Checksum</param>
 public Puzzle(ChecksumCollection horizontal, ChecksumCollection vertical)
 {
     Horizontal = horizontal;
     Vertical   = vertical;
     SizeX      = Vertical.Count;
     SizeY      = Horizontal.Count;
     _grid      = new Cell[SizeX, SizeY];
     // populate grid
     for (int i = 0; i < SizeX; i++)
     {
         for (int j = 0; j < SizeY; j++)
         {
             _grid[i, j] = new Cell();
         }
     }
 }
Exemple #7
0
 /// <summary>
 /// Create empty puzzle with a defined height and width
 /// </summary>
 /// <param name="sizeX">Puzzle width</param>
 /// <param name="sizeY">Puzzle height</param>
 public Puzzle(int sizeX, int sizeY)
 {
     SizeX = sizeX;
     SizeY = sizeY;
     _grid = new Cell[sizeX, sizeY];
     // populate grid
     for (int i = 0; i < sizeX; i++)
     {
         for (int j = 0; j < sizeY; j++)
         {
             _grid[i, j] = new Cell();
         }
     }
     Horizontal = new ChecksumCollection(sizeY);
     Vertical   = new ChecksumCollection(sizeX);
 }
        private async Task <IEnumerable <DocumentInfo> > CreateDocumentInfosAsync(
            ChecksumCollection documentChecksums,
            CancellationToken cancellationToken
            )
        {
            var documentInfos = new List <DocumentInfo>();

            foreach (var documentChecksum in documentChecksums)
            {
                cancellationToken.ThrowIfCancellationRequested();
                documentInfos.Add(
                    await CreateDocumentInfoAsync(documentChecksum, cancellationToken)
                    .ConfigureAwait(false)
                    );
            }

            return(documentInfos);
        }
 public ProjectStateChecksums(
     Checksum infoChecksum,
     Checksum compilationOptionsChecksum,
     Checksum parseOptionsChecksum,
     ChecksumCollection documentChecksums,
     ChecksumCollection projectReferenceChecksums,
     ChecksumCollection metadataReferenceChecksums,
     ChecksumCollection analyzerReferenceChecksums,
     ChecksumCollection additionalDocumentChecksums,
     ChecksumCollection analyzerConfigDocumentChecksumCollection)
     : this(
         (object)infoChecksum,
         compilationOptionsChecksum,
         parseOptionsChecksum,
         documentChecksums,
         projectReferenceChecksums,
         metadataReferenceChecksums,
         analyzerReferenceChecksums,
         additionalDocumentChecksums,
         analyzerConfigDocumentChecksumCollection)
 {
 }
Exemple #10
0
 public ChecksumObjectCollection(ISolutionChecksumService service, ChecksumCollection collection) : base(collection.Checksum, collection.Kind)
 {
     Children = ImmutableArray.CreateRange(collection.Select(c => (T)service.GetChecksumObject(c, CancellationToken.None)));
 }
        public async Task FindAsync(
            ProjectState state,
            HashSet <Checksum> searchingChecksumsLeft,
            Dictionary <Checksum, object> result,
            CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();
            // verify input
            Contract.ThrowIfFalse(state.TryGetStateChecksums(out var stateChecksum));
            Contract.ThrowIfFalse(this == stateChecksum);

            if (searchingChecksumsLeft.Count == 0)
            {
                return;
            }

            if (searchingChecksumsLeft.Remove(Checksum))
            {
                result[Checksum] = this;
            }

            if (searchingChecksumsLeft.Remove(Info))
            {
                result[Info] = state.ProjectInfo.Attributes;
            }

            if (searchingChecksumsLeft.Remove(CompilationOptions))
            {
                Contract.ThrowIfNull(state.CompilationOptions, "We should not be trying to serialize a project with no compilation options; RemoteSupportedLanguages.IsSupported should have filtered it out.");
                result[CompilationOptions] = state.CompilationOptions;
            }

            if (searchingChecksumsLeft.Remove(ParseOptions))
            {
                Contract.ThrowIfNull(state.ParseOptions, "We should not be trying to serialize a project with no compilation options; RemoteSupportedLanguages.IsSupported should have filtered it out.");
                result[ParseOptions] = state.ParseOptions;
            }

            if (searchingChecksumsLeft.Remove(Documents.Checksum))
            {
                result[Documents.Checksum] = Documents;
            }

            if (searchingChecksumsLeft.Remove(ProjectReferences.Checksum))
            {
                result[ProjectReferences.Checksum] = ProjectReferences;
            }

            if (searchingChecksumsLeft.Remove(MetadataReferences.Checksum))
            {
                result[MetadataReferences.Checksum] = MetadataReferences;
            }

            if (searchingChecksumsLeft.Remove(AnalyzerReferences.Checksum))
            {
                result[AnalyzerReferences.Checksum] = AnalyzerReferences;
            }

            if (searchingChecksumsLeft.Remove(AdditionalDocuments.Checksum))
            {
                result[AdditionalDocuments.Checksum] = AdditionalDocuments;
            }

            if (searchingChecksumsLeft.Remove(AnalyzerConfigDocuments.Checksum))
            {
                result[AnalyzerConfigDocuments.Checksum] = AnalyzerConfigDocuments;
            }

            ChecksumCollection.Find(state.ProjectReferences, ProjectReferences, searchingChecksumsLeft, result, cancellationToken);
            ChecksumCollection.Find(state.MetadataReferences, MetadataReferences, searchingChecksumsLeft, result, cancellationToken);
            ChecksumCollection.Find(state.AnalyzerReferences, AnalyzerReferences, searchingChecksumsLeft, result, cancellationToken);

            await ChecksumCollection.FindAsync(state.DocumentStates, searchingChecksumsLeft, result, cancellationToken).ConfigureAwait(false);

            await ChecksumCollection.FindAsync(state.AdditionalDocumentStates, searchingChecksumsLeft, result, cancellationToken).ConfigureAwait(false);

            await ChecksumCollection.FindAsync(state.AnalyzerConfigDocumentStates, searchingChecksumsLeft, result, cancellationToken).ConfigureAwait(false);
        }
        internal static async Task VerifyCollectionInService(IRemotableDataService snapshotService, ChecksumCollection checksums, int expectedCount, WellKnownSynchronizationKind expectedItemKind)
        {
            await VerifyChecksumInServiceAsync(snapshotService, checksums.Checksum, checksums.GetWellKnownSynchronizationKind()).ConfigureAwait(false);

            Assert.Equal(checksums.Count, expectedCount);

            foreach (var checksum in checksums)
            {
                await VerifyChecksumInServiceAsync(snapshotService, checksum, expectedItemKind).ConfigureAwait(false);
            }
        }
Exemple #13
0
 public ChecksumObjectCollection(ISolutionSynchronizationService service, ChecksumCollection collection) : base(collection.Checksum, collection.GetWellKnownSynchronizationKind())
 {
     // using .Result here since we don't want to convert all calls to this to async.
     // and none of ChecksumWithChildren actually use async
     Children = ImmutableArray.CreateRange(collection.Select(c => service.GetValueAsync <T>(c).Result));
 }
Exemple #14
0
        static void Main(string[] args)
        {
            var checksumFile = new ChecksumFile();
            var stats        = checksumFile.Stats;

            // Set starting locations
            var roots = new List <string>();

            if (args.Length == 0)
            {
                roots.Add(Directory.GetCurrentDirectory());
            }
            else
            {
                foreach (var arg in args)
                {
                    if (File.Exists(arg) || Directory.Exists(arg))
                    {
                        roots.Add(arg);
                    }
                    else
                    {
                        Console.WriteLine("Root does not exist: " + arg);
                        stats.ArgNotValid.Add(arg);
                    }
                }
            }

            // Get checksums
            var checksumCollections = checksumFile.ChecksumCollections;

            foreach (var root in roots)
            {
                Console.WriteLine("Root: " + root);
                if (Directory.Exists(root))
                {
                    var checksumCollection = new ChecksumCollection(root, RootType.Directory);
                    HashTree(root, checksumCollection, stats);
                    checksumCollections.Add(checksumCollection);
                }
                else if (File.Exists(root))
                {
                    Console.WriteLine("Root is a file.");
                    try
                    {
                        var checksumCollection = new ChecksumCollection(root, RootType.File);
                        checksumCollection.Add(Hasher.HashFile(root));
                        checksumCollections.Add(checksumCollection);
                    }
                    catch (UnauthorizedAccessException)
                    {
                        stats.ChecksumFailure.Add("Unauthorized: " + root);
                    }
                    catch (Exception)
                    {
                        stats.ChecksumFailure.Add("Unknown cause: " + root);
                    }
                }
                else
                {
                    Console.WriteLine("Root disappeared?");
                    stats.RootWentMissing.Add(root);
                }
            }

            // Write stats
            Console.WriteLine($"Stats:");
            Console.WriteLine($"\tInvalid Arguments: {stats.ArgNotValid.Count}");
            foreach (var item in stats.ArgNotValid.Items)
            {
                Console.WriteLine($"\t\t{item}");
            }
            Console.WriteLine($"\tRoots that disappeared: {stats.RootWentMissing.Count}");
            foreach (var item in stats.RootWentMissing.Items)
            {
                Console.WriteLine($"\t\t{item}");
            }
            Console.WriteLine($"\tUnauthorized: {stats.Unauthorized.Count}");
            foreach (var item in stats.Unauthorized.Items)
            {
                Console.WriteLine($"\t\t{item}");
            }
            Console.WriteLine($"\tFiles/Directories that disappeared: {stats.Disappeared.Count}");
            foreach (var item in stats.Disappeared.Items)
            {
                Console.WriteLine($"\t\t{item}");
            }
            Console.WriteLine($"\tFiles that couldn't be checksummed: {stats.ChecksumFailure.Count}");
            foreach (var item in stats.ChecksumFailure.Items)
            {
                Console.WriteLine($"\t\t{item}");
            }
            Console.WriteLine($"Total files checksummed: {checksumCollections.Select(ci => ci.Checksums.Count).Sum()}");

            // Serialize data
            var serialized = checksumFile.SerializeToUtf8Json();

            // Save to file with random name.
            do
            {
                var outfilename = string.Format(@"{0}.checksums.json", Guid.NewGuid());
                //outfilename = string.Format(@"{0}.checksums", Path.GetRandomFileName());

                try
                {
                    using var outfile = File.Open(outfilename, FileMode.CreateNew, FileAccess.Write);
                    outfile.Write(serialized);
                    Console.WriteLine("ChecksumFile written to: " + Path.GetFullPath(outfilename));
                    break;
                }
                catch (IOException)
                {
                    continue;
                }
            } while (true);
        }
        internal static void VerifyCollectionInService(IRemotableDataService snapshotService, ChecksumCollection checksums, int expectedCount, WellKnownSynchronizationKind expectedItemKind)
        {
            VerifyChecksumInService(snapshotService, checksums.Checksum, checksums.GetWellKnownSynchronizationKind());
            Assert.Equal(checksums.Count, expectedCount);

            foreach (var checksum in checksums)
            {
                VerifyChecksumInService(snapshotService, checksum, expectedItemKind);
            }
        }
Exemple #16
0
 public static ChecksumObjectCollection <DocumentChecksumObject> ToDocumentObjects(this ChecksumCollection collection, ISolutionChecksumService service)
 {
     Contract.ThrowIfFalse(collection.Kind == WellKnownChecksumObjects.Documents || collection.Kind == WellKnownChecksumObjects.TextDocuments);
     return(new ChecksumObjectCollection <DocumentChecksumObject>(service, collection));
 }
        internal static void VerifyCollectionInService(ISolutionChecksumService snapshotService, ChecksumCollection checksums, int expectedCount, string expectedItemKind)
        {
            VerifyChecksumObjectInService(snapshotService, checksums);
            Assert.Equal(checksums.Count, expectedCount);

            foreach (var checksum in checksums)
            {
                VerifyChecksumInService(snapshotService, checksum, expectedItemKind);
            }
        }
Exemple #18
0
            private async Task <Project> UpdateDocumentsAsync(
                Project project,
                IEnumerable <TextDocumentState> existingTextDocumentStates,
                ChecksumCollection oldChecksums,
                ChecksumCollection newChecksums,
                Func <Solution, ImmutableArray <DocumentInfo>, Solution> addDocuments,
                Func <Solution, DocumentId, Solution> removeDocument)
            {
                using var olds = SharedPools.Default <HashSet <Checksum> >().GetPooledObject();
                using var news = SharedPools.Default <HashSet <Checksum> >().GetPooledObject();

                olds.Object.UnionWith(oldChecksums);
                news.Object.UnionWith(newChecksums);

                // remove documents that exist in both side
                olds.Object.ExceptWith(newChecksums);
                news.Object.ExceptWith(oldChecksums);

                var oldMap = await GetDocumentMapAsync(existingTextDocumentStates, olds.Object).ConfigureAwait(false);

                var newMap = await GetDocumentMapAsync(_assetProvider, news.Object).ConfigureAwait(false);

                // added document
                ImmutableArray <DocumentInfo> .Builder?lazyDocumentsToAdd = null;
                foreach (var(documentId, newDocumentChecksums) in newMap)
                {
                    if (!oldMap.ContainsKey(documentId))
                    {
                        lazyDocumentsToAdd ??= ImmutableArray.CreateBuilder <DocumentInfo>();

                        // we have new document added
                        var documentInfo = await _assetProvider.CreateDocumentInfoAsync(newDocumentChecksums.Checksum, _cancellationToken).ConfigureAwait(false);

                        lazyDocumentsToAdd.Add(documentInfo);
                    }
                }

                if (lazyDocumentsToAdd != null)
                {
                    project = addDocuments(project.Solution, lazyDocumentsToAdd.ToImmutable()).GetProject(project.Id) !;
                }

                // changed document
                foreach (var(documentId, newDocumentChecksums) in newMap)
                {
                    if (!oldMap.TryGetValue(documentId, out var oldDocumentChecksums))
                    {
                        continue;
                    }

                    Contract.ThrowIfTrue(oldDocumentChecksums.Checksum == newDocumentChecksums.Checksum);

                    var document = project.GetDocument(documentId) ?? project.GetAdditionalDocument(documentId) ?? project.GetAnalyzerConfigDocument(documentId);
                    Contract.ThrowIfNull(document);

                    project = await UpdateDocumentAsync(document, oldDocumentChecksums, newDocumentChecksums).ConfigureAwait(false);
                }

                // removed document
                foreach (var(documentId, _) in oldMap)
                {
                    if (!newMap.ContainsKey(documentId))
                    {
                        // we have a document removed
                        project = removeDocument(project.Solution, documentId).GetProject(project.Id) !;
                    }
                }

                return(project);
            }
            private async Task <Project> UpdateDocumentsAsync(
                Project project,
                ProjectStateChecksums projectChecksums,
                IEnumerable <TextDocumentState> existingTextDocumentStates,
                ChecksumCollection oldChecksums,
                ChecksumCollection newChecksums,
                Func <Solution, ImmutableArray <DocumentInfo>, Solution> addDocuments,
                Func <Solution, ImmutableArray <DocumentId>, Solution> removeDocuments,
                CancellationToken cancellationToken)
            {
                using var olds = SharedPools.Default <HashSet <Checksum> >().GetPooledObject();
                using var news = SharedPools.Default <HashSet <Checksum> >().GetPooledObject();

                olds.Object.UnionWith(oldChecksums);
                news.Object.UnionWith(newChecksums);

                // remove documents that exist in both side
                olds.Object.ExceptWith(newChecksums);
                news.Object.ExceptWith(oldChecksums);

                var oldMap = await GetDocumentMapAsync(existingTextDocumentStates, olds.Object, cancellationToken).ConfigureAwait(false);

                var newMap = await GetDocumentMapAsync(_assetProvider, news.Object, cancellationToken).ConfigureAwait(false);

                // If more than two documents changed during a single update, perform a bulk synchronization on the
                // project to avoid large numbers of small synchronization calls during document updates.
                // 🔗 https://devdiv.visualstudio.com/DevDiv/_workitems/edit/1365014
                if (newMap.Count > 2)
                {
                    await _assetProvider.SynchronizeProjectAssetsAsync(new[] { projectChecksums.Checksum }, cancellationToken).ConfigureAwait(false);
                }

                // added document
                ImmutableArray <DocumentInfo> .Builder?lazyDocumentsToAdd = null;
                foreach (var(documentId, newDocumentChecksums) in newMap)
                {
                    if (!oldMap.ContainsKey(documentId))
                    {
                        lazyDocumentsToAdd ??= ImmutableArray.CreateBuilder <DocumentInfo>();

                        // we have new document added
                        var documentInfo = await _assetProvider.CreateDocumentInfoAsync(newDocumentChecksums.Checksum, cancellationToken).ConfigureAwait(false);

                        lazyDocumentsToAdd.Add(documentInfo);
                    }
                }

                if (lazyDocumentsToAdd != null)
                {
                    project = addDocuments(project.Solution, lazyDocumentsToAdd.ToImmutable()).GetProject(project.Id) !;
                }

                // changed document
                foreach (var(documentId, newDocumentChecksums) in newMap)
                {
                    if (!oldMap.TryGetValue(documentId, out var oldDocumentChecksums))
                    {
                        continue;
                    }

                    Contract.ThrowIfTrue(oldDocumentChecksums.Checksum == newDocumentChecksums.Checksum);

                    var document = project.GetDocument(documentId) ?? project.GetAdditionalDocument(documentId) ?? project.GetAnalyzerConfigDocument(documentId);
                    Contract.ThrowIfNull(document);

                    project = await UpdateDocumentAsync(document, oldDocumentChecksums, newDocumentChecksums, cancellationToken).ConfigureAwait(false);
                }

                // removed document
                ImmutableArray <DocumentId> .Builder?lazyDocumentsToRemove = null;
                foreach (var(documentId, _) in oldMap)
                {
                    if (!newMap.ContainsKey(documentId))
                    {
                        // we have a document removed
                        lazyDocumentsToRemove ??= ImmutableArray.CreateBuilder <DocumentId>();
                        lazyDocumentsToRemove.Add(documentId);
                    }
                }

                if (lazyDocumentsToRemove is not null)
                {
                    project = removeDocuments(project.Solution, lazyDocumentsToRemove.ToImmutable()).GetProject(project.Id) !;
                }

                return(project);
            }