private static void UpdateLatestVersionFacet(IDictionary <string, List <FacetedDocument> > existingValuesByFacet, IDictionary <string, FacetedDocument> candidateNewValueByFacet, string facet)
        {
            FacetedDocument newValue;

            if (candidateNewValueByFacet.TryGetValue(facet, out newValue))
            {
                List <FacetedDocument> existingValues;
                FacetedDocument        trueLatest = newValue;
                if (existingValuesByFacet.TryGetValue(facet, out existingValues))
                {
                    // Find the true latest
                    var oldLatest = existingValues.Where(d => d.Data.Package.Listed).OrderByDescending(d => d.Version).FirstOrDefault();
                    if (oldLatest != null && oldLatest.Version > trueLatest.Version)
                    {
                        trueLatest = oldLatest;
                    }

                    // Remove the facets from all the existing values, unless one of them happens to be the new one
                    foreach (var existing in existingValues)
                    {
                        if (existing != trueLatest)
                        {
                            existing.RemoveFacet(facet);
                        }
                    }
                }

                // Add the facet to the new value (this is idempotent, so it's ok if the document already has the facet)
                trueLatest.AddFacet(facet);
            }
        }
        private static void ProcessCompatibleVersion(string packageId, PerfEventTracker perfTracker, IDictionary <string, FacetedDocument> candidateNewFacets, FacetedDocument doc, FrameworkName projectFx)
        {
            using (perfTracker.TrackEvent("ProcessCompatibleVersion", "{0} v{1} (fx:{2})", packageId, doc.Version, projectFx))
            {
                // Add compatible facet
                doc.AddFacet(Facets.Compatible(projectFx));

                // If listed, process it against latest versions
                if (doc.Data.Package.Listed)
                {
                    // Check it against the current latest prerelease and swap latests if necessary
                    string latestPreFacet    = Facets.LatestPrereleaseVersion(projectFx);
                    string latestStableFacet = Facets.LatestStableVersion(projectFx);
                    if (!candidateNewFacets.ContainsKey(latestPreFacet))
                    {
                        candidateNewFacets[latestPreFacet] = doc;
                    }

                    // If this package is a stable version, do the same for latest stable
                    if (String.IsNullOrEmpty(doc.Version.SpecialVersion) && !candidateNewFacets.ContainsKey(latestStableFacet))
                    {
                        candidateNewFacets[latestStableFacet] = doc;
                    }
                }
            }
        }