Skip to content
This repository has been archived by the owner on Mar 16, 2021. It is now read-only.

Commit

Permalink
Add DatabaseOwnerFetcher to query for current owner information (#501)
Browse files Browse the repository at this point in the history
  • Loading branch information
joelverhagen committed Apr 10, 2019
1 parent aa7a233 commit 07c8d23
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ public static IServiceCollection AddAzureSearch(this IServiceCollection services
services.AddTransient<ICatalogLeafFetcher, CatalogLeafFetcher>();
services.AddTransient<ICollector, AzureSearchCollector>();
services.AddTransient<ICommitCollectorLogic, AzureSearchCollectorLogic>();
services.AddTransient<IDatabaseOwnerFetcher, DatabaseOwnerFetcher>();
services.AddTransient<IDiagnosticsService, LoggerDiagnosticsService>();
services.AddTransient<IEntitiesContextFactory, EntitiesContextFactory>();
services.AddTransient<IHijackDocumentBuilder, HijackDocumentBuilder>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@
<Compile Include="Analysis\IdentifierCustomTokenFilter.cs" />
<Compile Include="Analysis\PackageIdCustomAnalyzer.cs" />
<Compile Include="Analysis\PackageIdCustomTokenizer.cs" />
<Compile Include="Owners2AzureSearch\DatabaseOwnerFetcher.cs" />
<Compile Include="Owners2AzureSearch\IDatabaseOwnerFetcher.cs" />
<Compile Include="Owners2AzureSearch\IOwnerSetComparer.cs" />
<Compile Include="Owners2AzureSearch\OwnerSetComparer.cs" />
<Compile Include="Owners2AzureSearch\PackageIdToOwnersBuilder.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using NuGet.Jobs;
using NuGet.Jobs.Configuration;

namespace NuGet.Services.AzureSearch.Owners2AzureSearch
{
public class DatabaseOwnerFetcher : IDatabaseOwnerFetcher
{
private readonly ISqlConnectionFactory<GalleryDbConfiguration> _connectionFactory;
private readonly ILogger<DatabaseOwnerFetcher> _logger;

private const string Sql = @"
SELECT
pr.Id,
u.Username
FROM PackageRegistrations pr (NOLOCK)
INNER JOIN PackageRegistrationOwners pro (NOLOCK) ON pro.PackageRegistrationKey = pr.[Key]
INNER JOIN Users u (NOLOCK) ON pro.UserKey = u.[Key]
";

public DatabaseOwnerFetcher(
ISqlConnectionFactory<GalleryDbConfiguration> connectionFactory,
ILogger<DatabaseOwnerFetcher> logger)
{
_connectionFactory = connectionFactory ?? throw new ArgumentNullException(nameof(connectionFactory));
_logger = logger;
}

public async Task<SortedDictionary<string, SortedSet<string>>> GetPackageIdToOwnersAsync()
{
using (var connection = await _connectionFactory.OpenAsync())
using (var command = connection.CreateCommand())
{
command.CommandText = Sql;

using (var reader = await command.ExecuteReaderAsync())
{
var builder = new PackageIdToOwnersBuilder(_logger);
while (await reader.ReadAsync())
{
var id = reader.GetString(0);
var username = reader.GetString(1);

builder.Add(id, username);
}

return builder.GetResult();
}
}
}
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System.Collections.Generic;
using System.Threading.Tasks;

namespace NuGet.Services.AzureSearch.Owners2AzureSearch
{
/// <summary>
/// Fetches the current owner information from the database.
/// </summary>
public interface IDatabaseOwnerFetcher
{
/// <summary>
/// Fetch a mapping from package ID to set of owners for each package registration (i.e. package ID) in the
/// gallery database.
/// </summary>
Task<SortedDictionary<string, SortedSet<string>>> GetPackageIdToOwnersAsync();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,18 @@

namespace NuGet.Services.AzureSearch.Owners2AzureSearch
{
/// <summary>
/// Used to compare two sets of owners to determine the changes.
/// </summary>
public interface IOwnerSetComparer
{
/// <summary>
/// Compares two sets of owners to determine the package IDs that have changed. The returned dictionary
/// contains owners that were added, removed, and changed. For the "added" and "changed" cases, the owner set
/// is the new data. For the "removed" case, the set is empty.
/// </summary>
/// <param name="oldData">The old owner information, typically from storage.</param>
/// <param name="newData">The new owner information, typically from gallery DB.</param>
SortedDictionary<string, string[]> Compare(
SortedDictionary<string, SortedSet<string>> oldData,
SortedDictionary<string, SortedSet<string>> newData);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ public Facts(ITestOutputHelper output)
CloudBlobContainer = new Mock<ICloudBlobContainer>();
CloudBlob = new Mock<ISimpleCloudBlob>();
Options = new Mock<IOptionsSnapshot<AzureSearchJobConfiguration>>();
Logger = output.GetLogger<VersionListDataClient>();
Logger = output.GetLogger<OwnerDataClient>();
Config = new AzureSearchJobConfiguration
{
StorageContainer = "unit-test-container",
Expand Down Expand Up @@ -422,7 +422,7 @@ public Facts(ITestOutputHelper output)
public Mock<ICloudBlobContainer> CloudBlobContainer { get; }
public Mock<ISimpleCloudBlob> CloudBlob { get; }
public Mock<IOptionsSnapshot<AzureSearchJobConfiguration>> Options { get; }
public RecordingLogger<VersionListDataClient> Logger { get; }
public RecordingLogger<OwnerDataClient> Logger { get; }
public AzureSearchJobConfiguration Config { get; }
public string ETag { get; }
public Mock<IAccessCondition> AccessCondition { get; }
Expand Down

0 comments on commit 07c8d23

Please sign in to comment.