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 a0c0ef9
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,12 @@ private static void RegisterAzureSearchJobStorageServices(ContainerBuilder conta
c.Resolve<IOptionsSnapshot<AzureSearchJobConfiguration>>(),
c.Resolve<ILogger<BlobContainerBuilder>>()));

containerBuilder
.Register<IOwnerDataClient>(c => new OwnerDataClient(
c.ResolveKeyed<ICloudBlobClient>(key),
c.Resolve<IOptionsSnapshot<AzureSearchJobConfiguration>>(),
c.Resolve<ILogger<OwnerDataClient>>()));

containerBuilder
.Register(c => new Catalog2AzureSearchCommand(
c.Resolve<ICollector>(),
Expand Down Expand Up @@ -217,12 +223,12 @@ 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>();
services.AddTransient<IIndexBuilder, IndexBuilder>();
services.AddTransient<INewPackageRegistrationProducer, NewPackageRegistrationProducer>();
services.AddTransient<IOwnerDataClient, OwnerDataClient>();
services.AddTransient<IOwnerSetComparer, OwnerSetComparer>();
services.AddTransient<IPackageEntityIndexActionBuilder, PackageEntityIndexActionBuilder>();
services.AddTransient<IRegistrationClient, RegistrationClient>();
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 @@ -20,13 +20,13 @@ public class OwnerDataClient : IOwnerDataClient

private readonly ICloudBlobClient _cloudBlobClient;
private readonly IOptionsSnapshot<AzureSearchJobConfiguration> _options;
private readonly ILogger<VersionListDataClient> _logger;
private readonly ILogger<OwnerDataClient> _logger;
private readonly Lazy<ICloudBlobContainer> _lazyContainer;

public OwnerDataClient(
ICloudBlobClient cloudBlobClient,
IOptionsSnapshot<AzureSearchJobConfiguration> options,
ILogger<VersionListDataClient> logger)
ILogger<OwnerDataClient> logger)
{
_cloudBlobClient = cloudBlobClient ?? throw new ArgumentNullException(nameof(cloudBlobClient));
_options = options ?? throw new ArgumentNullException(nameof(cloudBlobClient));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ public class OwnerSetComparer : IOwnerSetComparer
{
private static readonly string[] EmptyStringArray = new string[0];

private readonly ILogger _logger;
private readonly ILogger<OwnerSetComparer> _logger;

public OwnerSetComparer(ILogger logger)
public OwnerSetComparer(ILogger<OwnerSetComparer> logger)
{
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
}
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 a0c0ef9

Please sign in to comment.